2 * TI LP8788 MFD - rtc driver
4 * Copyright 2012 Texas Instruments
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/err.h>
15 #include <linux/irqdomain.h>
16 #include <linux/mfd/lp8788.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
22 /* register address */
23 #define LP8788_INTEN_3 0x05
24 #define LP8788_RTC_UNLOCK 0x64
25 #define LP8788_RTC_SEC 0x70
26 #define LP8788_ALM1_SEC 0x77
27 #define LP8788_ALM1_EN 0x7D
28 #define LP8788_ALM2_SEC 0x7E
29 #define LP8788_ALM2_EN 0x84
32 #define LP8788_INT_RTC_ALM1_M BIT(1) /* Addr 05h */
33 #define LP8788_INT_RTC_ALM1_S 1
34 #define LP8788_INT_RTC_ALM2_M BIT(2) /* Addr 05h */
35 #define LP8788_INT_RTC_ALM2_S 2
36 #define LP8788_ALM_EN_M BIT(7) /* Addr 7Dh or 84h */
37 #define LP8788_ALM_EN_S 7
39 #define DEFAULT_ALARM_SEL LP8788_ALARM_1
40 #define LP8788_MONTH_OFFSET 1
41 #define LP8788_BASE_YEAR 2000
42 #define MAX_WDAY_BITS 7
43 #define LP8788_WDAY_SET 1
44 #define RTC_UNLOCK 0x1
46 #define ALARM_IRQ_FLAG (RTC_IRQF | RTC_AF)
61 struct rtc_device *rdev;
62 enum lp8788_alarm_sel alarm;
66 static const u8 addr_alarm_sec[LP8788_ALARM_MAX] = {
71 static const u8 addr_alarm_en[LP8788_ALARM_MAX] = {
76 static const u8 mask_alarm_en[LP8788_ALARM_MAX] = {
77 LP8788_INT_RTC_ALM1_M,
78 LP8788_INT_RTC_ALM2_M,
81 static const u8 shift_alarm_en[LP8788_ALARM_MAX] = {
82 LP8788_INT_RTC_ALM1_S,
83 LP8788_INT_RTC_ALM2_S,
86 static int _to_tm_wday(u8 lp8788_wday)
93 /* lookup defined weekday from read register value */
94 for (i = 0; i < MAX_WDAY_BITS; i++) {
95 if ((lp8788_wday >> i) == LP8788_WDAY_SET)
102 static inline int _to_lp8788_wday(int tm_wday)
104 return LP8788_WDAY_SET << (tm_wday - 1);
107 static void lp8788_rtc_unlock(struct lp8788 *lp)
109 lp8788_write_byte(lp, LP8788_RTC_UNLOCK, RTC_UNLOCK);
110 lp8788_write_byte(lp, LP8788_RTC_UNLOCK, RTC_LATCH);
113 static int lp8788_rtc_read_time(struct device *dev, struct rtc_time *tm)
115 struct lp8788_rtc *rtc = dev_get_drvdata(dev);
116 struct lp8788 *lp = rtc->lp;
120 lp8788_rtc_unlock(lp);
122 ret = lp8788_read_multi_bytes(lp, LP8788_RTC_SEC, data, LPTIME_MAX);
126 tm->tm_sec = data[LPTIME_SEC];
127 tm->tm_min = data[LPTIME_MIN];
128 tm->tm_hour = data[LPTIME_HOUR];
129 tm->tm_mday = data[LPTIME_MDAY];
130 tm->tm_mon = data[LPTIME_MON] - LP8788_MONTH_OFFSET;
131 tm->tm_year = data[LPTIME_YEAR] + LP8788_BASE_YEAR - 1900;
132 tm->tm_wday = _to_tm_wday(data[LPTIME_WDAY]);
137 static int lp8788_rtc_set_time(struct device *dev, struct rtc_time *tm)
139 struct lp8788_rtc *rtc = dev_get_drvdata(dev);
140 struct lp8788 *lp = rtc->lp;
141 u8 data[LPTIME_MAX - 1];
144 year = tm->tm_year + 1900 - LP8788_BASE_YEAR;
146 dev_err(lp->dev, "invalid year: %d\n", year);
150 /* because rtc weekday is a readonly register, do not update */
151 data[LPTIME_SEC] = tm->tm_sec;
152 data[LPTIME_MIN] = tm->tm_min;
153 data[LPTIME_HOUR] = tm->tm_hour;
154 data[LPTIME_MDAY] = tm->tm_mday;
155 data[LPTIME_MON] = tm->tm_mon + LP8788_MONTH_OFFSET;
156 data[LPTIME_YEAR] = year;
158 for (i = 0; i < ARRAY_SIZE(data); i++) {
159 ret = lp8788_write_byte(lp, LP8788_RTC_SEC + i, data[i]);
167 static int lp8788_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
169 struct lp8788_rtc *rtc = dev_get_drvdata(dev);
170 struct lp8788 *lp = rtc->lp;
171 struct rtc_time *tm = &alarm->time;
172 u8 addr, data[LPTIME_MAX];
175 addr = addr_alarm_sec[rtc->alarm];
176 ret = lp8788_read_multi_bytes(lp, addr, data, LPTIME_MAX);
180 tm->tm_sec = data[LPTIME_SEC];
181 tm->tm_min = data[LPTIME_MIN];
182 tm->tm_hour = data[LPTIME_HOUR];
183 tm->tm_mday = data[LPTIME_MDAY];
184 tm->tm_mon = data[LPTIME_MON] - LP8788_MONTH_OFFSET;
185 tm->tm_year = data[LPTIME_YEAR] + LP8788_BASE_YEAR - 1900;
186 tm->tm_wday = _to_tm_wday(data[LPTIME_WDAY]);
187 alarm->enabled = data[LPTIME_WDAY] & LP8788_ALM_EN_M;
192 static int lp8788_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
194 struct lp8788_rtc *rtc = dev_get_drvdata(dev);
195 struct lp8788 *lp = rtc->lp;
196 struct rtc_time *tm = &alarm->time;
197 u8 addr, data[LPTIME_MAX];
200 year = tm->tm_year + 1900 - LP8788_BASE_YEAR;
202 dev_err(lp->dev, "invalid year: %d\n", year);
206 data[LPTIME_SEC] = tm->tm_sec;
207 data[LPTIME_MIN] = tm->tm_min;
208 data[LPTIME_HOUR] = tm->tm_hour;
209 data[LPTIME_MDAY] = tm->tm_mday;
210 data[LPTIME_MON] = tm->tm_mon + LP8788_MONTH_OFFSET;
211 data[LPTIME_YEAR] = year;
212 data[LPTIME_WDAY] = _to_lp8788_wday(tm->tm_wday);
214 for (i = 0; i < ARRAY_SIZE(data); i++) {
215 addr = addr_alarm_sec[rtc->alarm] + i;
216 ret = lp8788_write_byte(lp, addr, data[i]);
222 addr = addr_alarm_en[rtc->alarm];
224 return lp8788_update_bits(lp, addr, LP8788_ALM_EN_M,
225 alarm->enabled << LP8788_ALM_EN_S);
228 static int lp8788_alarm_irq_enable(struct device *dev, unsigned int enable)
230 struct lp8788_rtc *rtc = dev_get_drvdata(dev);
231 struct lp8788 *lp = rtc->lp;
237 mask = mask_alarm_en[rtc->alarm];
238 shift = shift_alarm_en[rtc->alarm];
240 return lp8788_update_bits(lp, LP8788_INTEN_3, mask, enable << shift);
243 static const struct rtc_class_ops lp8788_rtc_ops = {
244 .read_time = lp8788_rtc_read_time,
245 .set_time = lp8788_rtc_set_time,
246 .read_alarm = lp8788_read_alarm,
247 .set_alarm = lp8788_set_alarm,
248 .alarm_irq_enable = lp8788_alarm_irq_enable,
251 static irqreturn_t lp8788_alarm_irq_handler(int irq, void *ptr)
253 struct lp8788_rtc *rtc = ptr;
255 rtc_update_irq(rtc->rdev, 1, ALARM_IRQ_FLAG);
259 static int lp8788_alarm_irq_register(struct platform_device *pdev,
260 struct lp8788_rtc *rtc)
263 struct lp8788 *lp = rtc->lp;
264 struct irq_domain *irqdm = lp->irqdm;
269 /* even the alarm IRQ number is not specified, rtc time should work */
270 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, LP8788_ALM_IRQ);
274 if (rtc->alarm == LP8788_ALARM_1)
279 rtc->irq = irq_create_mapping(irqdm, irq);
281 return devm_request_threaded_irq(&pdev->dev, rtc->irq, NULL,
282 lp8788_alarm_irq_handler,
283 0, LP8788_ALM_IRQ, rtc);
286 static int lp8788_rtc_probe(struct platform_device *pdev)
288 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
289 struct lp8788_rtc *rtc;
290 struct device *dev = &pdev->dev;
292 rtc = devm_kzalloc(dev, sizeof(struct lp8788_rtc), GFP_KERNEL);
297 rtc->alarm = lp->pdata ? lp->pdata->alarm_sel : DEFAULT_ALARM_SEL;
298 platform_set_drvdata(pdev, rtc);
300 device_init_wakeup(dev, 1);
302 rtc->rdev = rtc_device_register("lp8788_rtc", dev,
303 &lp8788_rtc_ops, THIS_MODULE);
304 if (IS_ERR(rtc->rdev)) {
305 dev_err(dev, "can not register rtc device\n");
306 return PTR_ERR(rtc->rdev);
309 if (lp8788_alarm_irq_register(pdev, rtc))
310 dev_warn(lp->dev, "no rtc irq handler\n");
315 static int lp8788_rtc_remove(struct platform_device *pdev)
317 struct lp8788_rtc *rtc = platform_get_drvdata(pdev);
319 rtc_device_unregister(rtc->rdev);
320 platform_set_drvdata(pdev, NULL);
325 static struct platform_driver lp8788_rtc_driver = {
326 .probe = lp8788_rtc_probe,
327 .remove = lp8788_rtc_remove,
329 .name = LP8788_DEV_RTC,
330 .owner = THIS_MODULE,
333 module_platform_driver(lp8788_rtc_driver);
335 MODULE_DESCRIPTION("Texas Instruments LP8788 RTC Driver");
336 MODULE_AUTHOR("Milo Kim");
337 MODULE_LICENSE("GPL");
338 MODULE_ALIAS("platform:lp8788-rtc");