Path: blob/master/drivers/firmware/arm_scmi/vendors/imx/imx-sm-bbm.c
54088 views
// SPDX-License-Identifier: GPL-2.01/*2* System Control and Management Interface (SCMI) NXP BBM Protocol3*4* Copyright 2024 NXP5*/67#define pr_fmt(fmt) "SCMI Notifications BBM - " fmt89#include <linux/bits.h>10#include <linux/io.h>11#include <linux/module.h>12#include <linux/of.h>13#include <linux/platform_device.h>14#include <linux/scmi_protocol.h>15#include <linux/scmi_imx_protocol.h>1617#include "../../protocols.h"18#include "../../notify.h"1920#define SCMI_PROTOCOL_SUPPORTED_VERSION 0x100002122enum scmi_imx_bbm_protocol_cmd {23IMX_BBM_GPR_SET = 0x3,24IMX_BBM_GPR_GET = 0x4,25IMX_BBM_RTC_ATTRIBUTES = 0x5,26IMX_BBM_RTC_TIME_SET = 0x6,27IMX_BBM_RTC_TIME_GET = 0x7,28IMX_BBM_RTC_ALARM_SET = 0x8,29IMX_BBM_BUTTON_GET = 0x9,30IMX_BBM_RTC_NOTIFY = 0xA,31IMX_BBM_BUTTON_NOTIFY = 0xB,32};3334#define GET_RTCS_NR(x) le32_get_bits((x), GENMASK(23, 16))35#define GET_GPRS_NR(x) le32_get_bits((x), GENMASK(15, 0))3637#define SCMI_IMX_BBM_NOTIFY_RTC_UPDATED BIT(2)38#define SCMI_IMX_BBM_NOTIFY_RTC_ROLLOVER BIT(1)39#define SCMI_IMX_BBM_NOTIFY_RTC_ALARM BIT(0)4041#define SCMI_IMX_BBM_RTC_ALARM_ENABLE_FLAG BIT(0)4243#define SCMI_IMX_BBM_NOTIFY_RTC_FLAG \44(SCMI_IMX_BBM_NOTIFY_RTC_UPDATED | SCMI_IMX_BBM_NOTIFY_RTC_ROLLOVER | \45SCMI_IMX_BBM_NOTIFY_RTC_ALARM)4647#define SCMI_IMX_BBM_EVENT_RTC_MASK GENMASK(31, 24)4849struct scmi_imx_bbm_info {50int nr_rtc;51int nr_gpr;52};5354struct scmi_msg_imx_bbm_protocol_attributes {55__le32 attributes;56};5758struct scmi_imx_bbm_set_time {59__le32 id;60__le32 flags;61__le32 value_low;62__le32 value_high;63};6465struct scmi_imx_bbm_get_time {66__le32 id;67__le32 flags;68};6970struct scmi_imx_bbm_alarm_time {71__le32 id;72__le32 flags;73__le32 value_low;74__le32 value_high;75};7677struct scmi_msg_imx_bbm_rtc_notify {78__le32 rtc_id;79__le32 flags;80};8182struct scmi_msg_imx_bbm_button_notify {83__le32 flags;84};8586struct scmi_imx_bbm_notify_payld {87__le32 flags;88};8990static int scmi_imx_bbm_attributes_get(const struct scmi_protocol_handle *ph,91struct scmi_imx_bbm_info *pi)92{93int ret;94struct scmi_xfer *t;95struct scmi_msg_imx_bbm_protocol_attributes *attr;9697ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, sizeof(*attr), &t);98if (ret)99return ret;100101attr = t->rx.buf;102103ret = ph->xops->do_xfer(ph, t);104if (!ret) {105pi->nr_rtc = GET_RTCS_NR(attr->attributes);106pi->nr_gpr = GET_GPRS_NR(attr->attributes);107}108109ph->xops->xfer_put(ph, t);110111return ret;112}113114static int scmi_imx_bbm_notify(const struct scmi_protocol_handle *ph,115u32 src_id, int message_id, bool enable)116{117int ret;118struct scmi_xfer *t;119120if (message_id == IMX_BBM_RTC_NOTIFY) {121struct scmi_msg_imx_bbm_rtc_notify *rtc_notify;122123ret = ph->xops->xfer_get_init(ph, message_id,124sizeof(*rtc_notify), 0, &t);125if (ret)126return ret;127128rtc_notify = t->tx.buf;129rtc_notify->rtc_id = cpu_to_le32(0);130rtc_notify->flags =131cpu_to_le32(enable ? SCMI_IMX_BBM_NOTIFY_RTC_FLAG : 0);132} else if (message_id == IMX_BBM_BUTTON_NOTIFY) {133struct scmi_msg_imx_bbm_button_notify *button_notify;134135ret = ph->xops->xfer_get_init(ph, message_id,136sizeof(*button_notify), 0, &t);137if (ret)138return ret;139140button_notify = t->tx.buf;141button_notify->flags = cpu_to_le32(enable ? 1 : 0);142} else {143return -EINVAL;144}145146ret = ph->xops->do_xfer(ph, t);147148ph->xops->xfer_put(ph, t);149return ret;150}151152static enum scmi_imx_bbm_protocol_cmd evt_2_cmd[] = {153IMX_BBM_RTC_NOTIFY,154IMX_BBM_BUTTON_NOTIFY155};156157static int scmi_imx_bbm_set_notify_enabled(const struct scmi_protocol_handle *ph,158u8 evt_id, u32 src_id, bool enable)159{160int ret, cmd_id;161162if (evt_id >= ARRAY_SIZE(evt_2_cmd))163return -EINVAL;164165cmd_id = evt_2_cmd[evt_id];166ret = scmi_imx_bbm_notify(ph, src_id, cmd_id, enable);167if (ret)168pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",169evt_id, src_id, ret);170171return ret;172}173174static void *scmi_imx_bbm_fill_custom_report(const struct scmi_protocol_handle *ph,175u8 evt_id, ktime_t timestamp,176const void *payld, size_t payld_sz,177void *report, u32 *src_id)178{179const struct scmi_imx_bbm_notify_payld *p = payld;180struct scmi_imx_bbm_notif_report *r = report;181182if (sizeof(*p) != payld_sz)183return NULL;184185if (evt_id == SCMI_EVENT_IMX_BBM_RTC) {186r->is_rtc = true;187r->is_button = false;188r->timestamp = timestamp;189r->rtc_id = le32_get_bits(p->flags, SCMI_IMX_BBM_EVENT_RTC_MASK);190r->rtc_evt = le32_get_bits(p->flags, SCMI_IMX_BBM_NOTIFY_RTC_FLAG);191dev_dbg(ph->dev, "RTC: %d evt: %x\n", r->rtc_id, r->rtc_evt);192*src_id = r->rtc_evt;193} else if (evt_id == SCMI_EVENT_IMX_BBM_BUTTON) {194r->is_rtc = false;195r->is_button = true;196r->timestamp = timestamp;197dev_dbg(ph->dev, "BBM Button\n");198*src_id = 0;199} else {200WARN_ON_ONCE(1);201return NULL;202}203204return r;205}206207static const struct scmi_event scmi_imx_bbm_events[] = {208{209.id = SCMI_EVENT_IMX_BBM_RTC,210.max_payld_sz = sizeof(struct scmi_imx_bbm_notify_payld),211.max_report_sz = sizeof(struct scmi_imx_bbm_notif_report),212},213{214.id = SCMI_EVENT_IMX_BBM_BUTTON,215.max_payld_sz = sizeof(struct scmi_imx_bbm_notify_payld),216.max_report_sz = sizeof(struct scmi_imx_bbm_notif_report),217},218};219220static const struct scmi_event_ops scmi_imx_bbm_event_ops = {221.set_notify_enabled = scmi_imx_bbm_set_notify_enabled,222.fill_custom_report = scmi_imx_bbm_fill_custom_report,223};224225static const struct scmi_protocol_events scmi_imx_bbm_protocol_events = {226.queue_sz = SCMI_PROTO_QUEUE_SZ,227.ops = &scmi_imx_bbm_event_ops,228.evts = scmi_imx_bbm_events,229.num_events = ARRAY_SIZE(scmi_imx_bbm_events),230.num_sources = 1,231};232233static int scmi_imx_bbm_rtc_time_set(const struct scmi_protocol_handle *ph,234u32 rtc_id, u64 sec)235{236struct scmi_imx_bbm_info *pi = ph->get_priv(ph);237struct scmi_imx_bbm_set_time *cfg;238struct scmi_xfer *t;239int ret;240241if (rtc_id >= pi->nr_rtc)242return -EINVAL;243244ret = ph->xops->xfer_get_init(ph, IMX_BBM_RTC_TIME_SET, sizeof(*cfg), 0, &t);245if (ret)246return ret;247248cfg = t->tx.buf;249cfg->id = cpu_to_le32(rtc_id);250cfg->flags = 0;251cfg->value_low = cpu_to_le32(lower_32_bits(sec));252cfg->value_high = cpu_to_le32(upper_32_bits(sec));253254ret = ph->xops->do_xfer(ph, t);255256ph->xops->xfer_put(ph, t);257258return ret;259}260261static int scmi_imx_bbm_rtc_time_get(const struct scmi_protocol_handle *ph,262u32 rtc_id, u64 *value)263{264struct scmi_imx_bbm_info *pi = ph->get_priv(ph);265struct scmi_imx_bbm_get_time *cfg;266struct scmi_xfer *t;267int ret;268269if (rtc_id >= pi->nr_rtc)270return -EINVAL;271272ret = ph->xops->xfer_get_init(ph, IMX_BBM_RTC_TIME_GET, sizeof(*cfg),273sizeof(u64), &t);274if (ret)275return ret;276277cfg = t->tx.buf;278cfg->id = cpu_to_le32(rtc_id);279cfg->flags = 0;280281ret = ph->xops->do_xfer(ph, t);282if (!ret)283*value = get_unaligned_le64(t->rx.buf);284285ph->xops->xfer_put(ph, t);286287return ret;288}289290static int scmi_imx_bbm_rtc_alarm_set(const struct scmi_protocol_handle *ph,291u32 rtc_id, bool enable, u64 sec)292{293struct scmi_imx_bbm_info *pi = ph->get_priv(ph);294struct scmi_imx_bbm_alarm_time *cfg;295struct scmi_xfer *t;296int ret;297298if (rtc_id >= pi->nr_rtc)299return -EINVAL;300301ret = ph->xops->xfer_get_init(ph, IMX_BBM_RTC_ALARM_SET, sizeof(*cfg), 0, &t);302if (ret)303return ret;304305cfg = t->tx.buf;306cfg->id = cpu_to_le32(rtc_id);307cfg->flags = enable ?308cpu_to_le32(SCMI_IMX_BBM_RTC_ALARM_ENABLE_FLAG) : 0;309cfg->value_low = cpu_to_le32(lower_32_bits(sec));310cfg->value_high = cpu_to_le32(upper_32_bits(sec));311312ret = ph->xops->do_xfer(ph, t);313314ph->xops->xfer_put(ph, t);315316return ret;317}318319static int scmi_imx_bbm_button_get(const struct scmi_protocol_handle *ph, u32 *state)320{321struct scmi_xfer *t;322int ret;323324ret = ph->xops->xfer_get_init(ph, IMX_BBM_BUTTON_GET, 0, sizeof(u32), &t);325if (ret)326return ret;327328ret = ph->xops->do_xfer(ph, t);329if (!ret)330*state = get_unaligned_le32(t->rx.buf);331332ph->xops->xfer_put(ph, t);333334return ret;335}336337static const struct scmi_imx_bbm_proto_ops scmi_imx_bbm_proto_ops = {338.rtc_time_get = scmi_imx_bbm_rtc_time_get,339.rtc_time_set = scmi_imx_bbm_rtc_time_set,340.rtc_alarm_set = scmi_imx_bbm_rtc_alarm_set,341.button_get = scmi_imx_bbm_button_get,342};343344static int scmi_imx_bbm_protocol_init(const struct scmi_protocol_handle *ph)345{346int ret;347struct scmi_imx_bbm_info *binfo;348349dev_info(ph->dev, "NXP SM BBM Version %d.%d\n",350PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version));351352binfo = devm_kzalloc(ph->dev, sizeof(*binfo), GFP_KERNEL);353if (!binfo)354return -ENOMEM;355356ret = scmi_imx_bbm_attributes_get(ph, binfo);357if (ret)358return ret;359360return ph->set_priv(ph, binfo);361}362363static const struct scmi_protocol scmi_imx_bbm = {364.id = SCMI_PROTOCOL_IMX_BBM,365.owner = THIS_MODULE,366.instance_init = &scmi_imx_bbm_protocol_init,367.ops = &scmi_imx_bbm_proto_ops,368.events = &scmi_imx_bbm_protocol_events,369.supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,370.vendor_id = SCMI_IMX_VENDOR,371.sub_vendor_id = SCMI_IMX_SUBVENDOR,372};373module_scmi_protocol(scmi_imx_bbm);374375MODULE_ALIAS("scmi-protocol-" __stringify(SCMI_PROTOCOL_IMX_BBM) "-" SCMI_IMX_VENDOR);376MODULE_DESCRIPTION("i.MX SCMI BBM driver");377MODULE_LICENSE("GPL");378379380