^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) // RTC driver for ChromeOS Embedded Controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // Copyright (C) 2017 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Author: Stephen Barber <smbarber@chromium.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/platform_data/cros_ec_commands.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/platform_data/cros_ec_proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/rtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define DRV_NAME "cros-ec-rtc"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * struct cros_ec_rtc - Driver data for EC RTC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @cros_ec: Pointer to EC device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @rtc: Pointer to RTC device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @notifier: Notifier info for responding to EC events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @saved_alarm: Alarm to restore when interrupts are reenabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct cros_ec_rtc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct cros_ec_device *cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct notifier_block notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 saved_alarm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int cros_ec_rtc_get(struct cros_ec_device *cros_ec, u32 command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u32 *response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct cros_ec_command msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct ec_response_rtc data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) } __packed msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) memset(&msg, 0, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) msg.msg.command = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) msg.msg.insize = sizeof(msg.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) dev_err(cros_ec->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) "error getting %s from EC: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) command == EC_CMD_RTC_GET_VALUE ? "time" : "alarm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *response = msg.data.time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int cros_ec_rtc_set(struct cros_ec_device *cros_ec, u32 command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u32 param)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct cros_ec_command msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct ec_response_rtc data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) } __packed msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) memset(&msg, 0, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) msg.msg.command = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) msg.msg.outsize = sizeof(msg.data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) msg.data.time = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dev_err(cros_ec->dev, "error setting %s on EC: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) command == EC_CMD_RTC_SET_VALUE ? "time" : "alarm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* Read the current time from the EC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static int cros_ec_rtc_read_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u32 time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, &time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dev_err(dev, "error getting time: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rtc_time64_to_tm(time, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* Set the current EC time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int cros_ec_rtc_set_time(struct device *dev, struct rtc_time *tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) time64_t time = rtc_tm_to_time64(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_VALUE, (u32)time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) dev_err(dev, "error setting time: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* Read alarm time from RTC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int cros_ec_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) u32 current_time, alarm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * The EC host command for getting the alarm is relative (i.e. 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * seconds from now) whereas rtc_wkalrm is absolute. Get the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * RTC time first so we can calculate the relative time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dev_err(dev, "error getting time: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM, &alarm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dev_err(dev, "error getting alarm: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) rtc_time64_to_tm(current_time + alarm_offset, &alrm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Set the EC's RTC alarm. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int cros_ec_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) time64_t alarm_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u32 current_time, alarm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * The EC host command for setting the alarm is relative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * (i.e. 5 seconds from now) whereas rtc_wkalrm is absolute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * Get the current RTC time first so we can calculate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * relative time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dev_err(dev, "error getting time: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) alarm_time = rtc_tm_to_time64(&alrm->time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (alarm_time < 0 || alarm_time > U32_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!alrm->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * If the alarm is being disabled, send an alarm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * clear command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) alarm_offset = EC_RTC_ALARM_CLEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) cros_ec_rtc->saved_alarm = (u32)alarm_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* Don't set an alarm in the past. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if ((u32)alarm_time <= current_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return -ETIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) alarm_offset = (u32)alarm_time - current_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM, alarm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dev_err(dev, "error setting alarm: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static int cros_ec_rtc_alarm_irq_enable(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned int enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct cros_ec_device *cros_ec = cros_ec_rtc->cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) u32 current_time, alarm_offset, alarm_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_VALUE, ¤t_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_err(dev, "error getting time: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Restore saved alarm if it's still in the future. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (cros_ec_rtc->saved_alarm < current_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) alarm_offset = EC_RTC_ALARM_CLEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) alarm_offset = cros_ec_rtc->saved_alarm - current_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) alarm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dev_err(dev, "error restoring alarm: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* Disable alarm, saving the old alarm value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = cros_ec_rtc_get(cros_ec, EC_CMD_RTC_GET_ALARM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) &alarm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) dev_err(dev, "error saving alarm: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) alarm_value = current_time + alarm_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * If the current EC alarm is already past, we don't want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * to set an alarm when we go through the alarm irq enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (alarm_value < current_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) cros_ec_rtc->saved_alarm = EC_RTC_ALARM_CLEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) cros_ec_rtc->saved_alarm = alarm_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) alarm_offset = EC_RTC_ALARM_CLEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ret = cros_ec_rtc_set(cros_ec, EC_CMD_RTC_SET_ALARM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) alarm_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) dev_err(dev, "error disabling alarm: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int cros_ec_rtc_event(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned long queued_during_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) void *_notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct cros_ec_rtc *cros_ec_rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct rtc_device *rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct cros_ec_device *cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) u32 host_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) cros_ec_rtc = container_of(nb, struct cros_ec_rtc, notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) rtc = cros_ec_rtc->rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) cros_ec = cros_ec_rtc->cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) host_event = cros_ec_get_host_event(cros_ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static const struct rtc_class_ops cros_ec_rtc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .read_time = cros_ec_rtc_read_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .set_time = cros_ec_rtc_set_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .read_alarm = cros_ec_rtc_read_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .set_alarm = cros_ec_rtc_set_alarm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .alarm_irq_enable = cros_ec_rtc_alarm_irq_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static int cros_ec_rtc_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return enable_irq_wake(cros_ec_rtc->cros_ec->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int cros_ec_rtc_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct cros_ec_rtc *cros_ec_rtc = dev_get_drvdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return disable_irq_wake(cros_ec_rtc->cros_ec->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static SIMPLE_DEV_PM_OPS(cros_ec_rtc_pm_ops, cros_ec_rtc_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) cros_ec_rtc_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static int cros_ec_rtc_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct cros_ec_device *cros_ec = ec_dev->ec_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct cros_ec_rtc *cros_ec_rtc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct rtc_time tm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) cros_ec_rtc = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_rtc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!cros_ec_rtc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) platform_set_drvdata(pdev, cros_ec_rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) cros_ec_rtc->cros_ec = cros_ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* Get initial time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = cros_ec_rtc_read_time(&pdev->dev, &tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) dev_err(&pdev->dev, "failed to read RTC time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ret = device_init_wakeup(&pdev->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) dev_err(&pdev->dev, "failed to initialize wakeup\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) cros_ec_rtc->rtc = devm_rtc_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (IS_ERR(cros_ec_rtc->rtc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return PTR_ERR(cros_ec_rtc->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) cros_ec_rtc->rtc->ops = &cros_ec_rtc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) cros_ec_rtc->rtc->range_max = U32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) ret = rtc_register_device(cros_ec_rtc->rtc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /* Get RTC events from the EC. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) cros_ec_rtc->notifier.notifier_call = cros_ec_rtc_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) &cros_ec_rtc->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) dev_err(&pdev->dev, "failed to register notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static int cros_ec_rtc_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct cros_ec_rtc *cros_ec_rtc = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret = blocking_notifier_chain_unregister(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) &cros_ec_rtc->cros_ec->event_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) &cros_ec_rtc->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) dev_err(dev, "failed to unregister notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static struct platform_driver cros_ec_rtc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) .probe = cros_ec_rtc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .remove = cros_ec_rtc_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .name = DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .pm = &cros_ec_rtc_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) module_platform_driver(cros_ec_rtc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) MODULE_DESCRIPTION("RTC driver for Chrome OS ECs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) MODULE_AUTHOR("Stephen Barber <smbarber@chromium.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) MODULE_ALIAS("platform:" DRV_NAME);