1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI LP8788 MFD - rtc driver
5 * Copyright 2012 Texas Instruments
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
10 #include <linux/err.h>
11 #include <linux/irqdomain.h>
12 #include <linux/mfd/lp8788.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/rtc.h>
16 #include <linux/slab.h>
18 /* register address */
19 #define LP8788_INTEN_3 0x05
20 #define LP8788_RTC_UNLOCK 0x64
21 #define LP8788_RTC_SEC 0x70
22 #define LP8788_ALM1_SEC 0x77
23 #define LP8788_ALM1_EN 0x7D
24 #define LP8788_ALM2_SEC 0x7E
25 #define LP8788_ALM2_EN 0x84
28 #define LP8788_INT_RTC_ALM1_M BIT(1) /* Addr 05h */
29 #define LP8788_INT_RTC_ALM1_S 1
30 #define LP8788_INT_RTC_ALM2_M BIT(2) /* Addr 05h */
31 #define LP8788_INT_RTC_ALM2_S 2
32 #define LP8788_ALM_EN_M BIT(7) /* Addr 7Dh or 84h */
33 #define LP8788_ALM_EN_S 7
35 #define DEFAULT_ALARM_SEL LP8788_ALARM_1
36 #define LP8788_MONTH_OFFSET 1
37 #define LP8788_BASE_YEAR 2000
38 #define MAX_WDAY_BITS 7
39 #define LP8788_WDAY_SET 1
40 #define RTC_UNLOCK 0x1
42 #define ALARM_IRQ_FLAG (RTC_IRQF | RTC_AF)
57 struct rtc_device
*rdev
;
58 enum lp8788_alarm_sel alarm
;
62 static const u8 addr_alarm_sec
[LP8788_ALARM_MAX
] = {
67 static const u8 addr_alarm_en
[LP8788_ALARM_MAX
] = {
72 static const u8 mask_alarm_en
[LP8788_ALARM_MAX
] = {
73 LP8788_INT_RTC_ALM1_M
,
74 LP8788_INT_RTC_ALM2_M
,
77 static const u8 shift_alarm_en
[LP8788_ALARM_MAX
] = {
78 LP8788_INT_RTC_ALM1_S
,
79 LP8788_INT_RTC_ALM2_S
,
82 static int _to_tm_wday(u8 lp8788_wday
)
89 /* lookup defined weekday from read register value */
90 for (i
= 0; i
< MAX_WDAY_BITS
; i
++) {
91 if ((lp8788_wday
>> i
) == LP8788_WDAY_SET
)
98 static inline int _to_lp8788_wday(int tm_wday
)
100 return LP8788_WDAY_SET
<< (tm_wday
- 1);
103 static void lp8788_rtc_unlock(struct lp8788
*lp
)
105 lp8788_write_byte(lp
, LP8788_RTC_UNLOCK
, RTC_UNLOCK
);
106 lp8788_write_byte(lp
, LP8788_RTC_UNLOCK
, RTC_LATCH
);
109 static int lp8788_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
111 struct lp8788_rtc
*rtc
= dev_get_drvdata(dev
);
112 struct lp8788
*lp
= rtc
->lp
;
116 lp8788_rtc_unlock(lp
);
118 ret
= lp8788_read_multi_bytes(lp
, LP8788_RTC_SEC
, data
, LPTIME_MAX
);
122 tm
->tm_sec
= data
[LPTIME_SEC
];
123 tm
->tm_min
= data
[LPTIME_MIN
];
124 tm
->tm_hour
= data
[LPTIME_HOUR
];
125 tm
->tm_mday
= data
[LPTIME_MDAY
];
126 tm
->tm_mon
= data
[LPTIME_MON
] - LP8788_MONTH_OFFSET
;
127 tm
->tm_year
= data
[LPTIME_YEAR
] + LP8788_BASE_YEAR
- 1900;
128 tm
->tm_wday
= _to_tm_wday(data
[LPTIME_WDAY
]);
133 static int lp8788_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
135 struct lp8788_rtc
*rtc
= dev_get_drvdata(dev
);
136 struct lp8788
*lp
= rtc
->lp
;
137 u8 data
[LPTIME_MAX
- 1];
140 year
= tm
->tm_year
+ 1900 - LP8788_BASE_YEAR
;
142 dev_err(lp
->dev
, "invalid year: %d\n", year
);
146 /* because rtc weekday is a readonly register, do not update */
147 data
[LPTIME_SEC
] = tm
->tm_sec
;
148 data
[LPTIME_MIN
] = tm
->tm_min
;
149 data
[LPTIME_HOUR
] = tm
->tm_hour
;
150 data
[LPTIME_MDAY
] = tm
->tm_mday
;
151 data
[LPTIME_MON
] = tm
->tm_mon
+ LP8788_MONTH_OFFSET
;
152 data
[LPTIME_YEAR
] = year
;
154 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
155 ret
= lp8788_write_byte(lp
, LP8788_RTC_SEC
+ i
, data
[i
]);
163 static int lp8788_read_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
165 struct lp8788_rtc
*rtc
= dev_get_drvdata(dev
);
166 struct lp8788
*lp
= rtc
->lp
;
167 struct rtc_time
*tm
= &alarm
->time
;
168 u8 addr
, data
[LPTIME_MAX
];
171 addr
= addr_alarm_sec
[rtc
->alarm
];
172 ret
= lp8788_read_multi_bytes(lp
, addr
, data
, LPTIME_MAX
);
176 tm
->tm_sec
= data
[LPTIME_SEC
];
177 tm
->tm_min
= data
[LPTIME_MIN
];
178 tm
->tm_hour
= data
[LPTIME_HOUR
];
179 tm
->tm_mday
= data
[LPTIME_MDAY
];
180 tm
->tm_mon
= data
[LPTIME_MON
] - LP8788_MONTH_OFFSET
;
181 tm
->tm_year
= data
[LPTIME_YEAR
] + LP8788_BASE_YEAR
- 1900;
182 tm
->tm_wday
= _to_tm_wday(data
[LPTIME_WDAY
]);
183 alarm
->enabled
= data
[LPTIME_WDAY
] & LP8788_ALM_EN_M
;
188 static int lp8788_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alarm
)
190 struct lp8788_rtc
*rtc
= dev_get_drvdata(dev
);
191 struct lp8788
*lp
= rtc
->lp
;
192 struct rtc_time
*tm
= &alarm
->time
;
193 u8 addr
, data
[LPTIME_MAX
];
196 year
= tm
->tm_year
+ 1900 - LP8788_BASE_YEAR
;
198 dev_err(lp
->dev
, "invalid year: %d\n", year
);
202 data
[LPTIME_SEC
] = tm
->tm_sec
;
203 data
[LPTIME_MIN
] = tm
->tm_min
;
204 data
[LPTIME_HOUR
] = tm
->tm_hour
;
205 data
[LPTIME_MDAY
] = tm
->tm_mday
;
206 data
[LPTIME_MON
] = tm
->tm_mon
+ LP8788_MONTH_OFFSET
;
207 data
[LPTIME_YEAR
] = year
;
208 data
[LPTIME_WDAY
] = _to_lp8788_wday(tm
->tm_wday
);
210 for (i
= 0; i
< ARRAY_SIZE(data
); i
++) {
211 addr
= addr_alarm_sec
[rtc
->alarm
] + i
;
212 ret
= lp8788_write_byte(lp
, addr
, data
[i
]);
218 addr
= addr_alarm_en
[rtc
->alarm
];
220 return lp8788_update_bits(lp
, addr
, LP8788_ALM_EN_M
,
221 alarm
->enabled
<< LP8788_ALM_EN_S
);
224 static int lp8788_alarm_irq_enable(struct device
*dev
, unsigned int enable
)
226 struct lp8788_rtc
*rtc
= dev_get_drvdata(dev
);
227 struct lp8788
*lp
= rtc
->lp
;
233 mask
= mask_alarm_en
[rtc
->alarm
];
234 shift
= shift_alarm_en
[rtc
->alarm
];
236 return lp8788_update_bits(lp
, LP8788_INTEN_3
, mask
, enable
<< shift
);
239 static const struct rtc_class_ops lp8788_rtc_ops
= {
240 .read_time
= lp8788_rtc_read_time
,
241 .set_time
= lp8788_rtc_set_time
,
242 .read_alarm
= lp8788_read_alarm
,
243 .set_alarm
= lp8788_set_alarm
,
244 .alarm_irq_enable
= lp8788_alarm_irq_enable
,
247 static irqreturn_t
lp8788_alarm_irq_handler(int irq
, void *ptr
)
249 struct lp8788_rtc
*rtc
= ptr
;
251 rtc_update_irq(rtc
->rdev
, 1, ALARM_IRQ_FLAG
);
255 static int lp8788_alarm_irq_register(struct platform_device
*pdev
,
256 struct lp8788_rtc
*rtc
)
259 struct lp8788
*lp
= rtc
->lp
;
260 struct irq_domain
*irqdm
= lp
->irqdm
;
265 /* even the alarm IRQ number is not specified, rtc time should work */
266 r
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, LP8788_ALM_IRQ
);
270 if (rtc
->alarm
== LP8788_ALARM_1
)
275 rtc
->irq
= irq_create_mapping(irqdm
, irq
);
277 return devm_request_threaded_irq(&pdev
->dev
, rtc
->irq
, NULL
,
278 lp8788_alarm_irq_handler
,
279 0, LP8788_ALM_IRQ
, rtc
);
282 static int lp8788_rtc_probe(struct platform_device
*pdev
)
284 struct lp8788
*lp
= dev_get_drvdata(pdev
->dev
.parent
);
285 struct lp8788_rtc
*rtc
;
286 struct device
*dev
= &pdev
->dev
;
288 rtc
= devm_kzalloc(dev
, sizeof(struct lp8788_rtc
), GFP_KERNEL
);
293 rtc
->alarm
= lp
->pdata
? lp
->pdata
->alarm_sel
: DEFAULT_ALARM_SEL
;
294 platform_set_drvdata(pdev
, rtc
);
296 device_init_wakeup(dev
, 1);
298 rtc
->rdev
= devm_rtc_device_register(dev
, "lp8788_rtc",
299 &lp8788_rtc_ops
, THIS_MODULE
);
300 if (IS_ERR(rtc
->rdev
)) {
301 dev_err(dev
, "can not register rtc device\n");
302 return PTR_ERR(rtc
->rdev
);
305 if (lp8788_alarm_irq_register(pdev
, rtc
))
306 dev_warn(lp
->dev
, "no rtc irq handler\n");
311 static struct platform_driver lp8788_rtc_driver
= {
312 .probe
= lp8788_rtc_probe
,
314 .name
= LP8788_DEV_RTC
,
317 module_platform_driver(lp8788_rtc_driver
);
319 MODULE_DESCRIPTION("Texas Instruments LP8788 RTC Driver");
320 MODULE_AUTHOR("Milo Kim");
321 MODULE_LICENSE("GPL");
322 MODULE_ALIAS("platform:lp8788-rtc");