18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * An rtc driver for the Dallas DS1511 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2006 Atsushi Nemoto <anemo@mba.ocn.ne.jp> 68c2ecf20Sopenharmony_ci * Copyright (C) 2007 Andrew Sharp <andy.sharp@lsi.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Real time clock driver for the Dallas 1511 chip, which also 98c2ecf20Sopenharmony_ci * contains a watchdog timer. There is a tiny amount of code that 108c2ecf20Sopenharmony_ci * platform code could use to mess with the watchdog device a little 118c2ecf20Sopenharmony_ci * bit, but not a full watchdog driver. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/bcd.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/kernel.h> 178c2ecf20Sopenharmony_ci#include <linux/gfp.h> 188c2ecf20Sopenharmony_ci#include <linux/delay.h> 198c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 208c2ecf20Sopenharmony_ci#include <linux/rtc.h> 218c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 228c2ecf20Sopenharmony_ci#include <linux/io.h> 238c2ecf20Sopenharmony_ci#include <linux/module.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cienum ds1511reg { 268c2ecf20Sopenharmony_ci DS1511_SEC = 0x0, 278c2ecf20Sopenharmony_ci DS1511_MIN = 0x1, 288c2ecf20Sopenharmony_ci DS1511_HOUR = 0x2, 298c2ecf20Sopenharmony_ci DS1511_DOW = 0x3, 308c2ecf20Sopenharmony_ci DS1511_DOM = 0x4, 318c2ecf20Sopenharmony_ci DS1511_MONTH = 0x5, 328c2ecf20Sopenharmony_ci DS1511_YEAR = 0x6, 338c2ecf20Sopenharmony_ci DS1511_CENTURY = 0x7, 348c2ecf20Sopenharmony_ci DS1511_AM1_SEC = 0x8, 358c2ecf20Sopenharmony_ci DS1511_AM2_MIN = 0x9, 368c2ecf20Sopenharmony_ci DS1511_AM3_HOUR = 0xa, 378c2ecf20Sopenharmony_ci DS1511_AM4_DATE = 0xb, 388c2ecf20Sopenharmony_ci DS1511_WD_MSEC = 0xc, 398c2ecf20Sopenharmony_ci DS1511_WD_SEC = 0xd, 408c2ecf20Sopenharmony_ci DS1511_CONTROL_A = 0xe, 418c2ecf20Sopenharmony_ci DS1511_CONTROL_B = 0xf, 428c2ecf20Sopenharmony_ci DS1511_RAMADDR_LSB = 0x10, 438c2ecf20Sopenharmony_ci DS1511_RAMDATA = 0x13 448c2ecf20Sopenharmony_ci}; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#define DS1511_BLF1 0x80 478c2ecf20Sopenharmony_ci#define DS1511_BLF2 0x40 488c2ecf20Sopenharmony_ci#define DS1511_PRS 0x20 498c2ecf20Sopenharmony_ci#define DS1511_PAB 0x10 508c2ecf20Sopenharmony_ci#define DS1511_TDF 0x08 518c2ecf20Sopenharmony_ci#define DS1511_KSF 0x04 528c2ecf20Sopenharmony_ci#define DS1511_WDF 0x02 538c2ecf20Sopenharmony_ci#define DS1511_IRQF 0x01 548c2ecf20Sopenharmony_ci#define DS1511_TE 0x80 558c2ecf20Sopenharmony_ci#define DS1511_CS 0x40 568c2ecf20Sopenharmony_ci#define DS1511_BME 0x20 578c2ecf20Sopenharmony_ci#define DS1511_TPE 0x10 588c2ecf20Sopenharmony_ci#define DS1511_TIE 0x08 598c2ecf20Sopenharmony_ci#define DS1511_KIE 0x04 608c2ecf20Sopenharmony_ci#define DS1511_WDE 0x02 618c2ecf20Sopenharmony_ci#define DS1511_WDS 0x01 628c2ecf20Sopenharmony_ci#define DS1511_RAM_MAX 0x100 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define RTC_CMD DS1511_CONTROL_B 658c2ecf20Sopenharmony_ci#define RTC_CMD1 DS1511_CONTROL_A 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci#define RTC_ALARM_SEC DS1511_AM1_SEC 688c2ecf20Sopenharmony_ci#define RTC_ALARM_MIN DS1511_AM2_MIN 698c2ecf20Sopenharmony_ci#define RTC_ALARM_HOUR DS1511_AM3_HOUR 708c2ecf20Sopenharmony_ci#define RTC_ALARM_DATE DS1511_AM4_DATE 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci#define RTC_SEC DS1511_SEC 738c2ecf20Sopenharmony_ci#define RTC_MIN DS1511_MIN 748c2ecf20Sopenharmony_ci#define RTC_HOUR DS1511_HOUR 758c2ecf20Sopenharmony_ci#define RTC_DOW DS1511_DOW 768c2ecf20Sopenharmony_ci#define RTC_DOM DS1511_DOM 778c2ecf20Sopenharmony_ci#define RTC_MON DS1511_MONTH 788c2ecf20Sopenharmony_ci#define RTC_YEAR DS1511_YEAR 798c2ecf20Sopenharmony_ci#define RTC_CENTURY DS1511_CENTURY 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci#define RTC_TIE DS1511_TIE 828c2ecf20Sopenharmony_ci#define RTC_TE DS1511_TE 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistruct rtc_plat_data { 858c2ecf20Sopenharmony_ci struct rtc_device *rtc; 868c2ecf20Sopenharmony_ci void __iomem *ioaddr; /* virtual base address */ 878c2ecf20Sopenharmony_ci int irq; 888c2ecf20Sopenharmony_ci unsigned int irqen; 898c2ecf20Sopenharmony_ci int alrm_sec; 908c2ecf20Sopenharmony_ci int alrm_min; 918c2ecf20Sopenharmony_ci int alrm_hour; 928c2ecf20Sopenharmony_ci int alrm_mday; 938c2ecf20Sopenharmony_ci spinlock_t lock; 948c2ecf20Sopenharmony_ci}; 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(ds1511_lock); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic __iomem char *ds1511_base; 998c2ecf20Sopenharmony_cistatic u32 reg_spacing = 1; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic noinline void 1028c2ecf20Sopenharmony_cirtc_write(uint8_t val, uint32_t reg) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci writeb(val, ds1511_base + (reg * reg_spacing)); 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic inline void 1088c2ecf20Sopenharmony_cirtc_write_alarm(uint8_t val, enum ds1511reg reg) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci rtc_write((val | 0x80), reg); 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic noinline uint8_t 1148c2ecf20Sopenharmony_cirtc_read(enum ds1511reg reg) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci return readb(ds1511_base + (reg * reg_spacing)); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic inline void 1208c2ecf20Sopenharmony_cirtc_disable_update(void) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci rtc_write((rtc_read(RTC_CMD) & ~RTC_TE), RTC_CMD); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic void 1268c2ecf20Sopenharmony_cirtc_enable_update(void) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci rtc_write((rtc_read(RTC_CMD) | RTC_TE), RTC_CMD); 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/* 1328c2ecf20Sopenharmony_ci * #define DS1511_WDOG_RESET_SUPPORT 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci * Uncomment this if you want to use these routines in 1358c2ecf20Sopenharmony_ci * some platform code. 1368c2ecf20Sopenharmony_ci */ 1378c2ecf20Sopenharmony_ci#ifdef DS1511_WDOG_RESET_SUPPORT 1388c2ecf20Sopenharmony_ci/* 1398c2ecf20Sopenharmony_ci * just enough code to set the watchdog timer so that it 1408c2ecf20Sopenharmony_ci * will reboot the system 1418c2ecf20Sopenharmony_ci */ 1428c2ecf20Sopenharmony_civoid 1438c2ecf20Sopenharmony_cids1511_wdog_set(unsigned long deciseconds) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci /* 1468c2ecf20Sopenharmony_ci * the wdog timer can take 99.99 seconds 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ci deciseconds %= 10000; 1498c2ecf20Sopenharmony_ci /* 1508c2ecf20Sopenharmony_ci * set the wdog values in the wdog registers 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_ci rtc_write(bin2bcd(deciseconds % 100), DS1511_WD_MSEC); 1538c2ecf20Sopenharmony_ci rtc_write(bin2bcd(deciseconds / 100), DS1511_WD_SEC); 1548c2ecf20Sopenharmony_ci /* 1558c2ecf20Sopenharmony_ci * set wdog enable and wdog 'steering' bit to issue a reset 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_ci rtc_write(rtc_read(RTC_CMD) | DS1511_WDE | DS1511_WDS, RTC_CMD); 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_civoid 1618c2ecf20Sopenharmony_cids1511_wdog_disable(void) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci /* 1648c2ecf20Sopenharmony_ci * clear wdog enable and wdog 'steering' bits 1658c2ecf20Sopenharmony_ci */ 1668c2ecf20Sopenharmony_ci rtc_write(rtc_read(RTC_CMD) & ~(DS1511_WDE | DS1511_WDS), RTC_CMD); 1678c2ecf20Sopenharmony_ci /* 1688c2ecf20Sopenharmony_ci * clear the wdog counter 1698c2ecf20Sopenharmony_ci */ 1708c2ecf20Sopenharmony_ci rtc_write(0, DS1511_WD_MSEC); 1718c2ecf20Sopenharmony_ci rtc_write(0, DS1511_WD_SEC); 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci#endif 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci/* 1768c2ecf20Sopenharmony_ci * set the rtc chip's idea of the time. 1778c2ecf20Sopenharmony_ci * stupidly, some callers call with year unmolested; 1788c2ecf20Sopenharmony_ci * and some call with year = year - 1900. thanks. 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_cistatic int ds1511_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci u8 mon, day, dow, hrs, min, sec, yrs, cen; 1838c2ecf20Sopenharmony_ci unsigned long flags; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci /* 1868c2ecf20Sopenharmony_ci * won't have to change this for a while 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci if (rtc_tm->tm_year < 1900) 1898c2ecf20Sopenharmony_ci rtc_tm->tm_year += 1900; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci if (rtc_tm->tm_year < 1970) 1928c2ecf20Sopenharmony_ci return -EINVAL; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci yrs = rtc_tm->tm_year % 100; 1958c2ecf20Sopenharmony_ci cen = rtc_tm->tm_year / 100; 1968c2ecf20Sopenharmony_ci mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */ 1978c2ecf20Sopenharmony_ci day = rtc_tm->tm_mday; 1988c2ecf20Sopenharmony_ci dow = rtc_tm->tm_wday & 0x7; /* automatic BCD */ 1998c2ecf20Sopenharmony_ci hrs = rtc_tm->tm_hour; 2008c2ecf20Sopenharmony_ci min = rtc_tm->tm_min; 2018c2ecf20Sopenharmony_ci sec = rtc_tm->tm_sec; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci if ((mon > 12) || (day == 0)) 2048c2ecf20Sopenharmony_ci return -EINVAL; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci if (day > rtc_month_days(rtc_tm->tm_mon, rtc_tm->tm_year)) 2078c2ecf20Sopenharmony_ci return -EINVAL; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci if ((hrs >= 24) || (min >= 60) || (sec >= 60)) 2108c2ecf20Sopenharmony_ci return -EINVAL; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci /* 2138c2ecf20Sopenharmony_ci * each register is a different number of valid bits 2148c2ecf20Sopenharmony_ci */ 2158c2ecf20Sopenharmony_ci sec = bin2bcd(sec) & 0x7f; 2168c2ecf20Sopenharmony_ci min = bin2bcd(min) & 0x7f; 2178c2ecf20Sopenharmony_ci hrs = bin2bcd(hrs) & 0x3f; 2188c2ecf20Sopenharmony_ci day = bin2bcd(day) & 0x3f; 2198c2ecf20Sopenharmony_ci mon = bin2bcd(mon) & 0x1f; 2208c2ecf20Sopenharmony_ci yrs = bin2bcd(yrs) & 0xff; 2218c2ecf20Sopenharmony_ci cen = bin2bcd(cen) & 0xff; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci spin_lock_irqsave(&ds1511_lock, flags); 2248c2ecf20Sopenharmony_ci rtc_disable_update(); 2258c2ecf20Sopenharmony_ci rtc_write(cen, RTC_CENTURY); 2268c2ecf20Sopenharmony_ci rtc_write(yrs, RTC_YEAR); 2278c2ecf20Sopenharmony_ci rtc_write((rtc_read(RTC_MON) & 0xe0) | mon, RTC_MON); 2288c2ecf20Sopenharmony_ci rtc_write(day, RTC_DOM); 2298c2ecf20Sopenharmony_ci rtc_write(hrs, RTC_HOUR); 2308c2ecf20Sopenharmony_ci rtc_write(min, RTC_MIN); 2318c2ecf20Sopenharmony_ci rtc_write(sec, RTC_SEC); 2328c2ecf20Sopenharmony_ci rtc_write(dow, RTC_DOW); 2338c2ecf20Sopenharmony_ci rtc_enable_update(); 2348c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ds1511_lock, flags); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci return 0; 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_cistatic int ds1511_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci unsigned int century; 2428c2ecf20Sopenharmony_ci unsigned long flags; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci spin_lock_irqsave(&ds1511_lock, flags); 2458c2ecf20Sopenharmony_ci rtc_disable_update(); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci rtc_tm->tm_sec = rtc_read(RTC_SEC) & 0x7f; 2488c2ecf20Sopenharmony_ci rtc_tm->tm_min = rtc_read(RTC_MIN) & 0x7f; 2498c2ecf20Sopenharmony_ci rtc_tm->tm_hour = rtc_read(RTC_HOUR) & 0x3f; 2508c2ecf20Sopenharmony_ci rtc_tm->tm_mday = rtc_read(RTC_DOM) & 0x3f; 2518c2ecf20Sopenharmony_ci rtc_tm->tm_wday = rtc_read(RTC_DOW) & 0x7; 2528c2ecf20Sopenharmony_ci rtc_tm->tm_mon = rtc_read(RTC_MON) & 0x1f; 2538c2ecf20Sopenharmony_ci rtc_tm->tm_year = rtc_read(RTC_YEAR) & 0x7f; 2548c2ecf20Sopenharmony_ci century = rtc_read(RTC_CENTURY); 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci rtc_enable_update(); 2578c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ds1511_lock, flags); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci rtc_tm->tm_sec = bcd2bin(rtc_tm->tm_sec); 2608c2ecf20Sopenharmony_ci rtc_tm->tm_min = bcd2bin(rtc_tm->tm_min); 2618c2ecf20Sopenharmony_ci rtc_tm->tm_hour = bcd2bin(rtc_tm->tm_hour); 2628c2ecf20Sopenharmony_ci rtc_tm->tm_mday = bcd2bin(rtc_tm->tm_mday); 2638c2ecf20Sopenharmony_ci rtc_tm->tm_wday = bcd2bin(rtc_tm->tm_wday); 2648c2ecf20Sopenharmony_ci rtc_tm->tm_mon = bcd2bin(rtc_tm->tm_mon); 2658c2ecf20Sopenharmony_ci rtc_tm->tm_year = bcd2bin(rtc_tm->tm_year); 2668c2ecf20Sopenharmony_ci century = bcd2bin(century) * 100; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci /* 2698c2ecf20Sopenharmony_ci * Account for differences between how the RTC uses the values 2708c2ecf20Sopenharmony_ci * and how they are defined in a struct rtc_time; 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_ci century += rtc_tm->tm_year; 2738c2ecf20Sopenharmony_ci rtc_tm->tm_year = century - 1900; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci rtc_tm->tm_mon--; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci return 0; 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci/* 2818c2ecf20Sopenharmony_ci * write the alarm register settings 2828c2ecf20Sopenharmony_ci * 2838c2ecf20Sopenharmony_ci * we only have the use to interrupt every second, otherwise 2848c2ecf20Sopenharmony_ci * known as the update interrupt, or the interrupt if the whole 2858c2ecf20Sopenharmony_ci * date/hours/mins/secs matches. the ds1511 has many more 2868c2ecf20Sopenharmony_ci * permutations, but the kernel doesn't. 2878c2ecf20Sopenharmony_ci */ 2888c2ecf20Sopenharmony_cistatic void 2898c2ecf20Sopenharmony_cids1511_rtc_update_alarm(struct rtc_plat_data *pdata) 2908c2ecf20Sopenharmony_ci{ 2918c2ecf20Sopenharmony_ci unsigned long flags; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci spin_lock_irqsave(&pdata->lock, flags); 2948c2ecf20Sopenharmony_ci rtc_write(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ? 2958c2ecf20Sopenharmony_ci 0x80 : bin2bcd(pdata->alrm_mday) & 0x3f, 2968c2ecf20Sopenharmony_ci RTC_ALARM_DATE); 2978c2ecf20Sopenharmony_ci rtc_write(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ? 2988c2ecf20Sopenharmony_ci 0x80 : bin2bcd(pdata->alrm_hour) & 0x3f, 2998c2ecf20Sopenharmony_ci RTC_ALARM_HOUR); 3008c2ecf20Sopenharmony_ci rtc_write(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ? 3018c2ecf20Sopenharmony_ci 0x80 : bin2bcd(pdata->alrm_min) & 0x7f, 3028c2ecf20Sopenharmony_ci RTC_ALARM_MIN); 3038c2ecf20Sopenharmony_ci rtc_write(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ? 3048c2ecf20Sopenharmony_ci 0x80 : bin2bcd(pdata->alrm_sec) & 0x7f, 3058c2ecf20Sopenharmony_ci RTC_ALARM_SEC); 3068c2ecf20Sopenharmony_ci rtc_write(rtc_read(RTC_CMD) | (pdata->irqen ? RTC_TIE : 0), RTC_CMD); 3078c2ecf20Sopenharmony_ci rtc_read(RTC_CMD1); /* clear interrupts */ 3088c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&pdata->lock, flags); 3098c2ecf20Sopenharmony_ci} 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_cistatic int 3128c2ecf20Sopenharmony_cids1511_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci struct rtc_plat_data *pdata = dev_get_drvdata(dev); 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci if (pdata->irq <= 0) 3178c2ecf20Sopenharmony_ci return -EINVAL; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci pdata->alrm_mday = alrm->time.tm_mday; 3208c2ecf20Sopenharmony_ci pdata->alrm_hour = alrm->time.tm_hour; 3218c2ecf20Sopenharmony_ci pdata->alrm_min = alrm->time.tm_min; 3228c2ecf20Sopenharmony_ci pdata->alrm_sec = alrm->time.tm_sec; 3238c2ecf20Sopenharmony_ci if (alrm->enabled) 3248c2ecf20Sopenharmony_ci pdata->irqen |= RTC_AF; 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci ds1511_rtc_update_alarm(pdata); 3278c2ecf20Sopenharmony_ci return 0; 3288c2ecf20Sopenharmony_ci} 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cistatic int 3318c2ecf20Sopenharmony_cids1511_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3328c2ecf20Sopenharmony_ci{ 3338c2ecf20Sopenharmony_ci struct rtc_plat_data *pdata = dev_get_drvdata(dev); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci if (pdata->irq <= 0) 3368c2ecf20Sopenharmony_ci return -EINVAL; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday; 3398c2ecf20Sopenharmony_ci alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour; 3408c2ecf20Sopenharmony_ci alrm->time.tm_min = pdata->alrm_min < 0 ? 0 : pdata->alrm_min; 3418c2ecf20Sopenharmony_ci alrm->time.tm_sec = pdata->alrm_sec < 0 ? 0 : pdata->alrm_sec; 3428c2ecf20Sopenharmony_ci alrm->enabled = (pdata->irqen & RTC_AF) ? 1 : 0; 3438c2ecf20Sopenharmony_ci return 0; 3448c2ecf20Sopenharmony_ci} 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_cistatic irqreturn_t 3478c2ecf20Sopenharmony_cids1511_interrupt(int irq, void *dev_id) 3488c2ecf20Sopenharmony_ci{ 3498c2ecf20Sopenharmony_ci struct platform_device *pdev = dev_id; 3508c2ecf20Sopenharmony_ci struct rtc_plat_data *pdata = platform_get_drvdata(pdev); 3518c2ecf20Sopenharmony_ci unsigned long events = 0; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci spin_lock(&pdata->lock); 3548c2ecf20Sopenharmony_ci /* 3558c2ecf20Sopenharmony_ci * read and clear interrupt 3568c2ecf20Sopenharmony_ci */ 3578c2ecf20Sopenharmony_ci if (rtc_read(RTC_CMD1) & DS1511_IRQF) { 3588c2ecf20Sopenharmony_ci events = RTC_IRQF; 3598c2ecf20Sopenharmony_ci if (rtc_read(RTC_ALARM_SEC) & 0x80) 3608c2ecf20Sopenharmony_ci events |= RTC_UF; 3618c2ecf20Sopenharmony_ci else 3628c2ecf20Sopenharmony_ci events |= RTC_AF; 3638c2ecf20Sopenharmony_ci rtc_update_irq(pdata->rtc, 1, events); 3648c2ecf20Sopenharmony_ci } 3658c2ecf20Sopenharmony_ci spin_unlock(&pdata->lock); 3668c2ecf20Sopenharmony_ci return events ? IRQ_HANDLED : IRQ_NONE; 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic int ds1511_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 3708c2ecf20Sopenharmony_ci{ 3718c2ecf20Sopenharmony_ci struct rtc_plat_data *pdata = dev_get_drvdata(dev); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci if (pdata->irq <= 0) 3748c2ecf20Sopenharmony_ci return -EINVAL; 3758c2ecf20Sopenharmony_ci if (enabled) 3768c2ecf20Sopenharmony_ci pdata->irqen |= RTC_AF; 3778c2ecf20Sopenharmony_ci else 3788c2ecf20Sopenharmony_ci pdata->irqen &= ~RTC_AF; 3798c2ecf20Sopenharmony_ci ds1511_rtc_update_alarm(pdata); 3808c2ecf20Sopenharmony_ci return 0; 3818c2ecf20Sopenharmony_ci} 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_cistatic const struct rtc_class_ops ds1511_rtc_ops = { 3848c2ecf20Sopenharmony_ci .read_time = ds1511_rtc_read_time, 3858c2ecf20Sopenharmony_ci .set_time = ds1511_rtc_set_time, 3868c2ecf20Sopenharmony_ci .read_alarm = ds1511_rtc_read_alarm, 3878c2ecf20Sopenharmony_ci .set_alarm = ds1511_rtc_set_alarm, 3888c2ecf20Sopenharmony_ci .alarm_irq_enable = ds1511_rtc_alarm_irq_enable, 3898c2ecf20Sopenharmony_ci}; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic int ds1511_nvram_read(void *priv, unsigned int pos, void *buf, 3928c2ecf20Sopenharmony_ci size_t size) 3938c2ecf20Sopenharmony_ci{ 3948c2ecf20Sopenharmony_ci int i; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci rtc_write(pos, DS1511_RAMADDR_LSB); 3978c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) 3988c2ecf20Sopenharmony_ci *(char *)buf++ = rtc_read(DS1511_RAMDATA); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci return 0; 4018c2ecf20Sopenharmony_ci} 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_cistatic int ds1511_nvram_write(void *priv, unsigned int pos, void *buf, 4048c2ecf20Sopenharmony_ci size_t size) 4058c2ecf20Sopenharmony_ci{ 4068c2ecf20Sopenharmony_ci int i; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci rtc_write(pos, DS1511_RAMADDR_LSB); 4098c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) 4108c2ecf20Sopenharmony_ci rtc_write(*(char *)buf++, DS1511_RAMDATA); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci return 0; 4138c2ecf20Sopenharmony_ci} 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_cistatic int ds1511_rtc_probe(struct platform_device *pdev) 4168c2ecf20Sopenharmony_ci{ 4178c2ecf20Sopenharmony_ci struct rtc_plat_data *pdata; 4188c2ecf20Sopenharmony_ci int ret = 0; 4198c2ecf20Sopenharmony_ci struct nvmem_config ds1511_nvmem_cfg = { 4208c2ecf20Sopenharmony_ci .name = "ds1511_nvram", 4218c2ecf20Sopenharmony_ci .word_size = 1, 4228c2ecf20Sopenharmony_ci .stride = 1, 4238c2ecf20Sopenharmony_ci .size = DS1511_RAM_MAX, 4248c2ecf20Sopenharmony_ci .reg_read = ds1511_nvram_read, 4258c2ecf20Sopenharmony_ci .reg_write = ds1511_nvram_write, 4268c2ecf20Sopenharmony_ci .priv = &pdev->dev, 4278c2ecf20Sopenharmony_ci }; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 4308c2ecf20Sopenharmony_ci if (!pdata) 4318c2ecf20Sopenharmony_ci return -ENOMEM; 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci ds1511_base = devm_platform_ioremap_resource(pdev, 0); 4348c2ecf20Sopenharmony_ci if (IS_ERR(ds1511_base)) 4358c2ecf20Sopenharmony_ci return PTR_ERR(ds1511_base); 4368c2ecf20Sopenharmony_ci pdata->ioaddr = ds1511_base; 4378c2ecf20Sopenharmony_ci pdata->irq = platform_get_irq(pdev, 0); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* 4408c2ecf20Sopenharmony_ci * turn on the clock and the crystal, etc. 4418c2ecf20Sopenharmony_ci */ 4428c2ecf20Sopenharmony_ci rtc_write(DS1511_BME, RTC_CMD); 4438c2ecf20Sopenharmony_ci rtc_write(0, RTC_CMD1); 4448c2ecf20Sopenharmony_ci /* 4458c2ecf20Sopenharmony_ci * clear the wdog counter 4468c2ecf20Sopenharmony_ci */ 4478c2ecf20Sopenharmony_ci rtc_write(0, DS1511_WD_MSEC); 4488c2ecf20Sopenharmony_ci rtc_write(0, DS1511_WD_SEC); 4498c2ecf20Sopenharmony_ci /* 4508c2ecf20Sopenharmony_ci * start the clock 4518c2ecf20Sopenharmony_ci */ 4528c2ecf20Sopenharmony_ci rtc_enable_update(); 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci /* 4558c2ecf20Sopenharmony_ci * check for a dying bat-tree 4568c2ecf20Sopenharmony_ci */ 4578c2ecf20Sopenharmony_ci if (rtc_read(RTC_CMD1) & DS1511_BLF1) 4588c2ecf20Sopenharmony_ci dev_warn(&pdev->dev, "voltage-low detected.\n"); 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci spin_lock_init(&pdata->lock); 4618c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, pdata); 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci pdata->rtc = devm_rtc_allocate_device(&pdev->dev); 4648c2ecf20Sopenharmony_ci if (IS_ERR(pdata->rtc)) 4658c2ecf20Sopenharmony_ci return PTR_ERR(pdata->rtc); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci pdata->rtc->ops = &ds1511_rtc_ops; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci pdata->rtc->nvram_old_abi = true; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci ret = rtc_register_device(pdata->rtc); 4728c2ecf20Sopenharmony_ci if (ret) 4738c2ecf20Sopenharmony_ci return ret; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci rtc_nvmem_register(pdata->rtc, &ds1511_nvmem_cfg); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci /* 4788c2ecf20Sopenharmony_ci * if the platform has an interrupt in mind for this device, 4798c2ecf20Sopenharmony_ci * then by all means, set it 4808c2ecf20Sopenharmony_ci */ 4818c2ecf20Sopenharmony_ci if (pdata->irq > 0) { 4828c2ecf20Sopenharmony_ci rtc_read(RTC_CMD1); 4838c2ecf20Sopenharmony_ci if (devm_request_irq(&pdev->dev, pdata->irq, ds1511_interrupt, 4848c2ecf20Sopenharmony_ci IRQF_SHARED, pdev->name, pdev) < 0) { 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci dev_warn(&pdev->dev, "interrupt not available.\n"); 4878c2ecf20Sopenharmony_ci pdata->irq = 0; 4888c2ecf20Sopenharmony_ci } 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci return 0; 4928c2ecf20Sopenharmony_ci} 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci/* work with hotplug and coldplug */ 4958c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:ds1511"); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic struct platform_driver ds1511_rtc_driver = { 4988c2ecf20Sopenharmony_ci .probe = ds1511_rtc_probe, 4998c2ecf20Sopenharmony_ci .driver = { 5008c2ecf20Sopenharmony_ci .name = "ds1511", 5018c2ecf20Sopenharmony_ci }, 5028c2ecf20Sopenharmony_ci}; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_cimodule_platform_driver(ds1511_rtc_driver); 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew Sharp <andy.sharp@lsi.com>"); 5078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Dallas DS1511 RTC driver"); 5088c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 509