18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * RTC driver for Rockchip RK808 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Chris Zhong <zyw@rock-chips.com> 88c2ecf20Sopenharmony_ci * Author: Zhang Qing <zhangqing@rock-chips.com> 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/rtc.h> 148c2ecf20Sopenharmony_ci#include <linux/bcd.h> 158c2ecf20Sopenharmony_ci#include <linux/mfd/rk808.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/i2c.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci/* RTC_CTRL_REG bitfields */ 208c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_STOP_RTC_M BIT(0) 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* RK808 has a shadowed register for saving a "frozen" RTC time. 238c2ecf20Sopenharmony_ci * When user setting "GET_TIME" to 1, the time will save in this shadowed 248c2ecf20Sopenharmony_ci * register. If set "READSEL" to 1, user read rtc time register, actually 258c2ecf20Sopenharmony_ci * get the time of that moment. If we need the real time, clr this bit. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_RTC_GET_TIME BIT(6) 288c2ecf20Sopenharmony_ci#define BIT_RTC_CTRL_REG_RTC_READSEL_M BIT(7) 298c2ecf20Sopenharmony_ci#define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M BIT(3) 308c2ecf20Sopenharmony_ci#define RTC_STATUS_MASK 0xFE 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define SECONDS_REG_MSK 0x7F 338c2ecf20Sopenharmony_ci#define MINUTES_REG_MAK 0x7F 348c2ecf20Sopenharmony_ci#define HOURS_REG_MSK 0x3F 358c2ecf20Sopenharmony_ci#define DAYS_REG_MSK 0x3F 368c2ecf20Sopenharmony_ci#define MONTHS_REG_MSK 0x1F 378c2ecf20Sopenharmony_ci#define YEARS_REG_MSK 0xFF 388c2ecf20Sopenharmony_ci#define WEEKS_REG_MSK 0x7 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */ 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define NUM_TIME_REGS (RK808_WEEKS_REG - RK808_SECONDS_REG + 1) 438c2ecf20Sopenharmony_ci#define NUM_ALARM_REGS (RK808_ALARM_YEARS_REG - RK808_ALARM_SECONDS_REG + 1) 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistruct rk_rtc_compat_reg { 468c2ecf20Sopenharmony_ci unsigned int ctrl_reg; 478c2ecf20Sopenharmony_ci unsigned int status_reg; 488c2ecf20Sopenharmony_ci unsigned int alarm_seconds_reg; 498c2ecf20Sopenharmony_ci unsigned int int_reg; 508c2ecf20Sopenharmony_ci unsigned int seconds_reg; 518c2ecf20Sopenharmony_ci}; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistruct rk808_rtc { 548c2ecf20Sopenharmony_ci struct rk808 *rk808; 558c2ecf20Sopenharmony_ci struct rtc_device *rtc; 568c2ecf20Sopenharmony_ci struct rk_rtc_compat_reg *creg; 578c2ecf20Sopenharmony_ci int irq; 588c2ecf20Sopenharmony_ci}; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* 618c2ecf20Sopenharmony_ci * The Rockchip calendar used by the RK808 counts November with 31 days. We use 628c2ecf20Sopenharmony_ci * these translation functions to convert its dates to/from the Gregorian 638c2ecf20Sopenharmony_ci * calendar used by the rest of the world. We arbitrarily define Jan 1st, 2016 648c2ecf20Sopenharmony_ci * as the day when both calendars were in sync, and treat all other dates 658c2ecf20Sopenharmony_ci * relative to that. 668c2ecf20Sopenharmony_ci * NOTE: Other system software (e.g. firmware) that reads the same hardware must 678c2ecf20Sopenharmony_ci * implement this exact same conversion algorithm, with the same anchor date. 688c2ecf20Sopenharmony_ci */ 698c2ecf20Sopenharmony_cistatic time64_t nov2dec_transitions(struct rtc_time *tm) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci return (tm->tm_year + 1900) - 2016 + (tm->tm_mon + 1 > 11 ? 1 : 0); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic void rockchip_to_gregorian(struct rtc_time *tm) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci /* If it's Nov 31st, rtc_tm_to_time64() will count that like Dec 1st */ 778c2ecf20Sopenharmony_ci time64_t time = rtc_tm_to_time64(tm); 788c2ecf20Sopenharmony_ci rtc_time64_to_tm(time + nov2dec_transitions(tm) * 86400, tm); 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic void gregorian_to_rockchip(struct rtc_time *tm) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci time64_t extra_days = nov2dec_transitions(tm); 848c2ecf20Sopenharmony_ci time64_t time = rtc_tm_to_time64(tm); 858c2ecf20Sopenharmony_ci rtc_time64_to_tm(time - extra_days * 86400, tm); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* Compensate if we went back over Nov 31st (will work up to 2381) */ 888c2ecf20Sopenharmony_ci if (nov2dec_transitions(tm) < extra_days) { 898c2ecf20Sopenharmony_ci if (tm->tm_mon + 1 == 11) 908c2ecf20Sopenharmony_ci tm->tm_mday++; /* This may result in 31! */ 918c2ecf20Sopenharmony_ci else 928c2ecf20Sopenharmony_ci rtc_time64_to_tm(time - (extra_days - 1) * 86400, tm); 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci/* Read current time and date in RTC */ 978c2ecf20Sopenharmony_cistatic int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 1008c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 1018c2ecf20Sopenharmony_ci u8 rtc_data[NUM_TIME_REGS]; 1028c2ecf20Sopenharmony_ci int ret; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci /* Force an update of the shadowed registers right now */ 1058c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->ctrl_reg, 1068c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_RTC_GET_TIME, 1078c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_RTC_GET_TIME); 1088c2ecf20Sopenharmony_ci if (ret) { 1098c2ecf20Sopenharmony_ci dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); 1108c2ecf20Sopenharmony_ci return ret; 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci /* 1148c2ecf20Sopenharmony_ci * After we set the GET_TIME bit, the rtc time can't be read 1158c2ecf20Sopenharmony_ci * immediately. So we should wait up to 31.25 us, about one cycle of 1168c2ecf20Sopenharmony_ci * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer 1178c2ecf20Sopenharmony_ci * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency. 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->ctrl_reg, 1208c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_RTC_GET_TIME, 1218c2ecf20Sopenharmony_ci 0); 1228c2ecf20Sopenharmony_ci if (ret) { 1238c2ecf20Sopenharmony_ci dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); 1248c2ecf20Sopenharmony_ci return ret; 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci ret = regmap_bulk_read(rk808->regmap, rk808_rtc->creg->seconds_reg, 1288c2ecf20Sopenharmony_ci rtc_data, NUM_TIME_REGS); 1298c2ecf20Sopenharmony_ci if (ret) { 1308c2ecf20Sopenharmony_ci dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret); 1318c2ecf20Sopenharmony_ci return ret; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK); 1358c2ecf20Sopenharmony_ci tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK); 1368c2ecf20Sopenharmony_ci tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK); 1378c2ecf20Sopenharmony_ci tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK); 1388c2ecf20Sopenharmony_ci tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1; 1398c2ecf20Sopenharmony_ci tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100; 1408c2ecf20Sopenharmony_ci tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK); 1418c2ecf20Sopenharmony_ci rockchip_to_gregorian(tm); 1428c2ecf20Sopenharmony_ci dev_dbg(dev, "RTC date/time %ptRd(%d) %ptRt\n", tm, tm->tm_wday, tm); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci return ret; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci/* Set current time and date in RTC */ 1488c2ecf20Sopenharmony_cistatic int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 1518c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 1528c2ecf20Sopenharmony_ci u8 rtc_data[NUM_TIME_REGS]; 1538c2ecf20Sopenharmony_ci int ret; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci dev_dbg(dev, "set RTC date/time %ptRd(%d) %ptRt\n", tm, tm->tm_wday, tm); 1568c2ecf20Sopenharmony_ci gregorian_to_rockchip(tm); 1578c2ecf20Sopenharmony_ci rtc_data[0] = bin2bcd(tm->tm_sec); 1588c2ecf20Sopenharmony_ci rtc_data[1] = bin2bcd(tm->tm_min); 1598c2ecf20Sopenharmony_ci rtc_data[2] = bin2bcd(tm->tm_hour); 1608c2ecf20Sopenharmony_ci rtc_data[3] = bin2bcd(tm->tm_mday); 1618c2ecf20Sopenharmony_ci rtc_data[4] = bin2bcd(tm->tm_mon + 1); 1628c2ecf20Sopenharmony_ci rtc_data[5] = bin2bcd(tm->tm_year - 100); 1638c2ecf20Sopenharmony_ci rtc_data[6] = bin2bcd(tm->tm_wday); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci /* Stop RTC while updating the RTC registers */ 1668c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->ctrl_reg, 1678c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_STOP_RTC_M, 1688c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_STOP_RTC_M); 1698c2ecf20Sopenharmony_ci if (ret) { 1708c2ecf20Sopenharmony_ci dev_err(dev, "Failed to update RTC control: %d\n", ret); 1718c2ecf20Sopenharmony_ci return ret; 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci ret = regmap_bulk_write(rk808->regmap, rk808_rtc->creg->seconds_reg, 1758c2ecf20Sopenharmony_ci rtc_data, NUM_TIME_REGS); 1768c2ecf20Sopenharmony_ci if (ret) { 1778c2ecf20Sopenharmony_ci dev_err(dev, "Failed to bull write rtc_data: %d\n", ret); 1788c2ecf20Sopenharmony_ci return ret; 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci /* Start RTC again */ 1818c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->ctrl_reg, 1828c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_STOP_RTC_M, 0); 1838c2ecf20Sopenharmony_ci if (ret) { 1848c2ecf20Sopenharmony_ci dev_err(dev, "Failed to update RTC control: %d\n", ret); 1858c2ecf20Sopenharmony_ci return ret; 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci return 0; 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci/* Read alarm time and date in RTC */ 1918c2ecf20Sopenharmony_cistatic int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 1948c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 1958c2ecf20Sopenharmony_ci u8 alrm_data[NUM_ALARM_REGS]; 1968c2ecf20Sopenharmony_ci uint32_t int_reg; 1978c2ecf20Sopenharmony_ci int ret; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci ret = regmap_bulk_read(rk808->regmap, 2008c2ecf20Sopenharmony_ci rk808_rtc->creg->alarm_seconds_reg, 2018c2ecf20Sopenharmony_ci alrm_data, NUM_ALARM_REGS); 2028c2ecf20Sopenharmony_ci if (ret) { 2038c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read RTC alarm date REG: %d\n", ret); 2048c2ecf20Sopenharmony_ci return ret; 2058c2ecf20Sopenharmony_ci } 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK); 2088c2ecf20Sopenharmony_ci alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK); 2098c2ecf20Sopenharmony_ci alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK); 2108c2ecf20Sopenharmony_ci alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK); 2118c2ecf20Sopenharmony_ci alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1; 2128c2ecf20Sopenharmony_ci alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100; 2138c2ecf20Sopenharmony_ci rockchip_to_gregorian(&alrm->time); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci ret = regmap_read(rk808->regmap, rk808_rtc->creg->int_reg, &int_reg); 2168c2ecf20Sopenharmony_ci if (ret) { 2178c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read RTC INT REG: %d\n", ret); 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci dev_dbg(dev, "alrm read RTC date/time %ptRd(%d) %ptRt\n", 2228c2ecf20Sopenharmony_ci &alrm->time, alrm->time.tm_wday, &alrm->time); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci return 0; 2278c2ecf20Sopenharmony_ci} 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_cistatic int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 2328c2ecf20Sopenharmony_ci int ret; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->int_reg, 2358c2ecf20Sopenharmony_ci BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci return ret; 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistatic int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 2438c2ecf20Sopenharmony_ci int ret; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->int_reg, 2468c2ecf20Sopenharmony_ci BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 2478c2ecf20Sopenharmony_ci BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci return ret; 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 2558c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 2568c2ecf20Sopenharmony_ci u8 alrm_data[NUM_ALARM_REGS]; 2578c2ecf20Sopenharmony_ci int ret; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci ret = rk808_rtc_stop_alarm(rk808_rtc); 2608c2ecf20Sopenharmony_ci if (ret) { 2618c2ecf20Sopenharmony_ci dev_err(dev, "Failed to stop alarm: %d\n", ret); 2628c2ecf20Sopenharmony_ci return ret; 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci dev_dbg(dev, "alrm set RTC date/time %ptRd(%d) %ptRt\n", 2658c2ecf20Sopenharmony_ci &alrm->time, alrm->time.tm_wday, &alrm->time); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci gregorian_to_rockchip(&alrm->time); 2688c2ecf20Sopenharmony_ci alrm_data[0] = bin2bcd(alrm->time.tm_sec); 2698c2ecf20Sopenharmony_ci alrm_data[1] = bin2bcd(alrm->time.tm_min); 2708c2ecf20Sopenharmony_ci alrm_data[2] = bin2bcd(alrm->time.tm_hour); 2718c2ecf20Sopenharmony_ci alrm_data[3] = bin2bcd(alrm->time.tm_mday); 2728c2ecf20Sopenharmony_ci alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1); 2738c2ecf20Sopenharmony_ci alrm_data[5] = bin2bcd(alrm->time.tm_year - 100); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci ret = regmap_bulk_write(rk808->regmap, 2768c2ecf20Sopenharmony_ci rk808_rtc->creg->alarm_seconds_reg, 2778c2ecf20Sopenharmony_ci alrm_data, NUM_ALARM_REGS); 2788c2ecf20Sopenharmony_ci if (ret) { 2798c2ecf20Sopenharmony_ci dev_err(dev, "Failed to bulk write: %d\n", ret); 2808c2ecf20Sopenharmony_ci return ret; 2818c2ecf20Sopenharmony_ci } 2828c2ecf20Sopenharmony_ci if (alrm->enabled) { 2838c2ecf20Sopenharmony_ci ret = rk808_rtc_start_alarm(rk808_rtc); 2848c2ecf20Sopenharmony_ci if (ret) { 2858c2ecf20Sopenharmony_ci dev_err(dev, "Failed to start alarm: %d\n", ret); 2868c2ecf20Sopenharmony_ci return ret; 2878c2ecf20Sopenharmony_ci } 2888c2ecf20Sopenharmony_ci } 2898c2ecf20Sopenharmony_ci return 0; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic int rk808_rtc_alarm_irq_enable(struct device *dev, 2938c2ecf20Sopenharmony_ci unsigned int enabled) 2948c2ecf20Sopenharmony_ci{ 2958c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci if (enabled) 2988c2ecf20Sopenharmony_ci return rk808_rtc_start_alarm(rk808_rtc); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci return rk808_rtc_stop_alarm(rk808_rtc); 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci/* 3048c2ecf20Sopenharmony_ci * We will just handle setting the frequency and make use the framework for 3058c2ecf20Sopenharmony_ci * reading the periodic interupts. 3068c2ecf20Sopenharmony_ci * 3078c2ecf20Sopenharmony_ci * @freq: Current periodic IRQ freq: 3088c2ecf20Sopenharmony_ci * bit 0: every second 3098c2ecf20Sopenharmony_ci * bit 1: every minute 3108c2ecf20Sopenharmony_ci * bit 2: every hour 3118c2ecf20Sopenharmony_ci * bit 3: every day 3128c2ecf20Sopenharmony_ci */ 3138c2ecf20Sopenharmony_cistatic irqreturn_t rk808_alarm_irq(int irq, void *data) 3148c2ecf20Sopenharmony_ci{ 3158c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = data; 3168c2ecf20Sopenharmony_ci struct rk808 *rk808 = rk808_rtc->rk808; 3178c2ecf20Sopenharmony_ci struct i2c_client *client = rk808->i2c; 3188c2ecf20Sopenharmony_ci int ret; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci ret = regmap_write(rk808->regmap, rk808_rtc->creg->status_reg, 3218c2ecf20Sopenharmony_ci RTC_STATUS_MASK); 3228c2ecf20Sopenharmony_ci if (ret) { 3238c2ecf20Sopenharmony_ci dev_err(&client->dev, 3248c2ecf20Sopenharmony_ci "%s:Failed to update RTC status: %d\n", __func__, ret); 3258c2ecf20Sopenharmony_ci return ret; 3268c2ecf20Sopenharmony_ci } 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF); 3298c2ecf20Sopenharmony_ci dev_dbg(&client->dev, 3308c2ecf20Sopenharmony_ci "%s:irq=%d\n", __func__, irq); 3318c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_cistatic const struct rtc_class_ops rk808_rtc_ops = { 3358c2ecf20Sopenharmony_ci .read_time = rk808_rtc_readtime, 3368c2ecf20Sopenharmony_ci .set_time = rk808_rtc_set_time, 3378c2ecf20Sopenharmony_ci .read_alarm = rk808_rtc_readalarm, 3388c2ecf20Sopenharmony_ci .set_alarm = rk808_rtc_setalarm, 3398c2ecf20Sopenharmony_ci .alarm_irq_enable = rk808_rtc_alarm_irq_enable, 3408c2ecf20Sopenharmony_ci}; 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 3438c2ecf20Sopenharmony_ci/* Turn off the alarm if it should not be a wake source. */ 3448c2ecf20Sopenharmony_cistatic int rk808_rtc_suspend(struct device *dev) 3458c2ecf20Sopenharmony_ci{ 3468c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 3498c2ecf20Sopenharmony_ci enable_irq_wake(rk808_rtc->irq); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci return 0; 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci/* Enable the alarm if it should be enabled (in case it was disabled to 3558c2ecf20Sopenharmony_ci * prevent use as a wake source). 3568c2ecf20Sopenharmony_ci */ 3578c2ecf20Sopenharmony_cistatic int rk808_rtc_resume(struct device *dev) 3588c2ecf20Sopenharmony_ci{ 3598c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 3628c2ecf20Sopenharmony_ci disable_irq_wake(rk808_rtc->irq); 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci return 0; 3658c2ecf20Sopenharmony_ci} 3668c2ecf20Sopenharmony_ci#endif 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops, 3698c2ecf20Sopenharmony_ci rk808_rtc_suspend, rk808_rtc_resume); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_cistatic struct rk_rtc_compat_reg rk808_creg = { 3728c2ecf20Sopenharmony_ci .ctrl_reg = RK808_RTC_CTRL_REG, 3738c2ecf20Sopenharmony_ci .status_reg = RK808_RTC_STATUS_REG, 3748c2ecf20Sopenharmony_ci .alarm_seconds_reg = RK808_ALARM_SECONDS_REG, 3758c2ecf20Sopenharmony_ci .int_reg = RK808_RTC_INT_REG, 3768c2ecf20Sopenharmony_ci .seconds_reg = RK808_SECONDS_REG, 3778c2ecf20Sopenharmony_ci}; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_cistatic struct rk_rtc_compat_reg rk817_creg = { 3808c2ecf20Sopenharmony_ci .ctrl_reg = RK817_RTC_CTRL_REG, 3818c2ecf20Sopenharmony_ci .status_reg = RK817_RTC_STATUS_REG, 3828c2ecf20Sopenharmony_ci .alarm_seconds_reg = RK817_ALARM_SECONDS_REG, 3838c2ecf20Sopenharmony_ci .int_reg = RK817_RTC_INT_REG, 3848c2ecf20Sopenharmony_ci .seconds_reg = RK817_SECONDS_REG, 3858c2ecf20Sopenharmony_ci}; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_cistatic int rk808_rtc_probe(struct platform_device *pdev) 3888c2ecf20Sopenharmony_ci{ 3898c2ecf20Sopenharmony_ci struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); 3908c2ecf20Sopenharmony_ci struct rk808_rtc *rk808_rtc; 3918c2ecf20Sopenharmony_ci int ret; 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL); 3948c2ecf20Sopenharmony_ci if (rk808_rtc == NULL) 3958c2ecf20Sopenharmony_ci return -ENOMEM; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci switch (rk808->variant) { 3988c2ecf20Sopenharmony_ci case RK809_ID: 3998c2ecf20Sopenharmony_ci case RK817_ID: 4008c2ecf20Sopenharmony_ci rk808_rtc->creg = &rk817_creg; 4018c2ecf20Sopenharmony_ci break; 4028c2ecf20Sopenharmony_ci default: 4038c2ecf20Sopenharmony_ci rk808_rtc->creg = &rk808_creg; 4048c2ecf20Sopenharmony_ci break; 4058c2ecf20Sopenharmony_ci } 4068c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, rk808_rtc); 4078c2ecf20Sopenharmony_ci rk808_rtc->rk808 = rk808; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci /* start rtc running by default, and use shadowed timer. */ 4108c2ecf20Sopenharmony_ci ret = regmap_update_bits(rk808->regmap, rk808_rtc->creg->ctrl_reg, 4118c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_STOP_RTC_M | 4128c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_RTC_READSEL_M, 4138c2ecf20Sopenharmony_ci BIT_RTC_CTRL_REG_RTC_READSEL_M); 4148c2ecf20Sopenharmony_ci if (ret) { 4158c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 4168c2ecf20Sopenharmony_ci "Failed to update RTC control: %d\n", ret); 4178c2ecf20Sopenharmony_ci return ret; 4188c2ecf20Sopenharmony_ci } 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci ret = regmap_write(rk808->regmap, rk808_rtc->creg->status_reg, 4218c2ecf20Sopenharmony_ci RTC_STATUS_MASK); 4228c2ecf20Sopenharmony_ci if (ret) { 4238c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 4248c2ecf20Sopenharmony_ci "Failed to write RTC status: %d\n", ret); 4258c2ecf20Sopenharmony_ci return ret; 4268c2ecf20Sopenharmony_ci } 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci device_init_wakeup(&pdev->dev, 1); 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci rk808_rtc->rtc = devm_rtc_allocate_device(&pdev->dev); 4318c2ecf20Sopenharmony_ci if (IS_ERR(rk808_rtc->rtc)) 4328c2ecf20Sopenharmony_ci return PTR_ERR(rk808_rtc->rtc); 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci rk808_rtc->rtc->ops = &rk808_rtc_ops; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci rk808_rtc->irq = platform_get_irq(pdev, 0); 4378c2ecf20Sopenharmony_ci if (rk808_rtc->irq < 0) 4388c2ecf20Sopenharmony_ci return rk808_rtc->irq; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci /* request alarm irq of rk808 */ 4418c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL, 4428c2ecf20Sopenharmony_ci rk808_alarm_irq, 0, 4438c2ecf20Sopenharmony_ci "RTC alarm", rk808_rtc); 4448c2ecf20Sopenharmony_ci if (ret) { 4458c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n", 4468c2ecf20Sopenharmony_ci rk808_rtc->irq, ret); 4478c2ecf20Sopenharmony_ci return ret; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci return rtc_register_device(rk808_rtc->rtc); 4518c2ecf20Sopenharmony_ci} 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_cistatic struct platform_driver rk808_rtc_driver = { 4548c2ecf20Sopenharmony_ci .probe = rk808_rtc_probe, 4558c2ecf20Sopenharmony_ci .driver = { 4568c2ecf20Sopenharmony_ci .name = "rk808-rtc", 4578c2ecf20Sopenharmony_ci .pm = &rk808_rtc_pm_ops, 4588c2ecf20Sopenharmony_ci }, 4598c2ecf20Sopenharmony_ci}; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_cimodule_platform_driver(rk808_rtc_driver); 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("RTC driver for the rk808 series PMICs"); 4648c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 4658c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>"); 4668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 4678c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:rk808-rtc"); 468