1 /*
2 * rtc-palmas.c -- Palmas Real Time Clock driver.
3
4 * RTC driver for TI Palma series devices like TPS65913,
5 * TPS65914 power management IC.
6 *
7 * Copyright (c) 2012, NVIDIA Corporation.
8 *
9 * Author: Laxman Dewangan <ldewangan@nvidia.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation version 2.
14 *
15 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
16 * whether express or implied; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307, USA
24 */
25
26 #include <linux/bcd.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/mfd/palmas.h>
32 #include <linux/module.h>
33 #include <linux/of.h>
34 #include <linux/rtc.h>
35 #include <linux/types.h>
36 #include <linux/platform_device.h>
37 #include <linux/pm.h>
38
39 struct palmas_rtc {
40 struct rtc_device *rtc;
41 struct device *dev;
42 unsigned int irq;
43 };
44
45 /* Total number of RTC registers needed to set time*/
46 #define PALMAS_NUM_TIME_REGS (PALMAS_YEARS_REG - PALMAS_SECONDS_REG + 1)
47
48 /*
49 * Special bin2bcd mapping to deal with bcd storage of year.
50 *
51 * 0-69 -> 0xD0
52 * 70-99 (1970 - 1999) -> 0xD0 - 0xF9 (correctly rolls to 0x00)
53 * 100-199 (2000 - 2099) -> 0x00 - 0x99 (does not roll to 0xA0 :-( )
54 * 200-229 (2100 - 2129) -> 0xA0 - 0xC9 (really for completeness)
55 * 230- -> 0xC9
56 *
57 * Confirmed: the only transition that does not work correctly for this rtc
58 * clock is the transition from 2099 to 2100, it proceeds to 2000. We will
59 * accept this issue since the clock retains and transitions the year correctly
60 * in all other conditions.
61 */
year_bin2bcd(int val)62 static unsigned char year_bin2bcd(int val)
63 {
64 if (val < 70)
65 return 0xD0;
66 if (val < 100)
67 return bin2bcd(val - 20) | 0x80; /* KISS leverage of bin2bcd */
68 if (val >= 230)
69 return 0xC9;
70 if (val >= 200)
71 return bin2bcd(val - 180) | 0x80;
72 return bin2bcd(val - 100);
73 }
74
year_bcd2bin(unsigned char val)75 static int year_bcd2bin(unsigned char val)
76 {
77 if (val >= 0xD0)
78 return bcd2bin(val & 0x7F) + 20;
79 if (val >= 0xA0)
80 return bcd2bin(val & 0x7F) + 180;
81 return bcd2bin(val) + 100;
82 }
83
palmas_rtc_read_time(struct device * dev,struct rtc_time * tm)84 static int palmas_rtc_read_time(struct device *dev, struct rtc_time *tm)
85 {
86 unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
87 struct palmas *palmas = dev_get_drvdata(dev->parent);
88 int ret;
89
90 /* Copy RTC counting registers to static registers or latches */
91 ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
92 PALMAS_RTC_CTRL_REG_GET_TIME, PALMAS_RTC_CTRL_REG_GET_TIME);
93 if (ret < 0) {
94 dev_err(dev, "RTC CTRL reg update failed, err: %d\n", ret);
95 return ret;
96 }
97
98 ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
99 rtc_data, PALMAS_NUM_TIME_REGS);
100 if (ret < 0) {
101 dev_err(dev, "RTC_SECONDS reg read failed, err = %d\n", ret);
102 return ret;
103 }
104
105 tm->tm_sec = bcd2bin(rtc_data[0]);
106 tm->tm_min = bcd2bin(rtc_data[1]);
107 tm->tm_hour = bcd2bin(rtc_data[2]);
108 tm->tm_mday = bcd2bin(rtc_data[3]);
109 tm->tm_mon = bcd2bin(rtc_data[4]) - 1;
110 tm->tm_year = year_bcd2bin(rtc_data[5]);
111
112 return ret;
113 }
114
palmas_rtc_set_time(struct device * dev,struct rtc_time * tm)115 static int palmas_rtc_set_time(struct device *dev, struct rtc_time *tm)
116 {
117 unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
118 struct palmas *palmas = dev_get_drvdata(dev->parent);
119 int ret;
120
121 rtc_data[0] = bin2bcd(tm->tm_sec);
122 rtc_data[1] = bin2bcd(tm->tm_min);
123 rtc_data[2] = bin2bcd(tm->tm_hour);
124 rtc_data[3] = bin2bcd(tm->tm_mday);
125 rtc_data[4] = bin2bcd(tm->tm_mon + 1);
126 rtc_data[5] = year_bin2bcd(tm->tm_year);
127
128 /* Stop RTC while updating the RTC time registers */
129 ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
130 PALMAS_RTC_CTRL_REG_STOP_RTC, 0);
131 if (ret < 0) {
132 dev_err(dev, "RTC stop failed, err = %d\n", ret);
133 return ret;
134 }
135
136 ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
137 rtc_data, PALMAS_NUM_TIME_REGS);
138 if (ret < 0) {
139 dev_err(dev, "RTC_SECONDS reg write failed, err = %d\n", ret);
140 return ret;
141 }
142
143 /* Start back RTC */
144 ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
145 PALMAS_RTC_CTRL_REG_STOP_RTC, PALMAS_RTC_CTRL_REG_STOP_RTC);
146 if (ret < 0)
147 dev_err(dev, "RTC start failed, err = %d\n", ret);
148 return ret;
149 }
150
palmas_rtc_alarm_irq_enable(struct device * dev,unsigned enabled)151 static int palmas_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
152 {
153 struct palmas *palmas = dev_get_drvdata(dev->parent);
154 u8 val;
155
156 val = enabled ? PALMAS_RTC_INTERRUPTS_REG_IT_ALARM : 0;
157 return palmas_write(palmas, PALMAS_RTC_BASE,
158 PALMAS_RTC_INTERRUPTS_REG, val);
159 }
160
palmas_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alm)161 static int palmas_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
162 {
163 unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
164 u32 int_val;
165 struct palmas *palmas = dev_get_drvdata(dev->parent);
166 int ret;
167
168 ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE,
169 PALMAS_ALARM_SECONDS_REG,
170 alarm_data, PALMAS_NUM_TIME_REGS);
171 if (ret < 0) {
172 dev_err(dev, "RTC_ALARM_SECONDS read failed, err = %d\n", ret);
173 return ret;
174 }
175
176 alm->time.tm_sec = bcd2bin(alarm_data[0]);
177 alm->time.tm_min = bcd2bin(alarm_data[1]);
178 alm->time.tm_hour = bcd2bin(alarm_data[2]);
179 alm->time.tm_mday = bcd2bin(alarm_data[3]);
180 alm->time.tm_mon = bcd2bin(alarm_data[4]) - 1;
181 alm->time.tm_year = year_bcd2bin(alarm_data[5]);
182
183 ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_INTERRUPTS_REG,
184 &int_val);
185 if (ret < 0) {
186 dev_err(dev, "RTC_INTERRUPTS reg read failed, err = %d\n", ret);
187 return ret;
188 }
189
190 if (int_val & PALMAS_RTC_INTERRUPTS_REG_IT_ALARM)
191 alm->enabled = 1;
192 return ret;
193 }
194
palmas_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alm)195 static int palmas_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
196 {
197 unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
198 struct palmas *palmas = dev_get_drvdata(dev->parent);
199 int ret;
200
201 ret = palmas_rtc_alarm_irq_enable(dev, 0);
202 if (ret < 0) {
203 dev_err(dev, "Disable RTC alarm failed\n");
204 return ret;
205 }
206
207 alarm_data[0] = bin2bcd(alm->time.tm_sec);
208 alarm_data[1] = bin2bcd(alm->time.tm_min);
209 alarm_data[2] = bin2bcd(alm->time.tm_hour);
210 alarm_data[3] = bin2bcd(alm->time.tm_mday);
211 alarm_data[4] = bin2bcd(alm->time.tm_mon + 1);
212 alarm_data[5] = year_bin2bcd(alm->time.tm_year);
213
214 ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE,
215 PALMAS_ALARM_SECONDS_REG, alarm_data, PALMAS_NUM_TIME_REGS);
216 if (ret < 0) {
217 dev_err(dev, "ALARM_SECONDS_REG write failed, err = %d\n", ret);
218 return ret;
219 }
220
221 if (alm->enabled)
222 ret = palmas_rtc_alarm_irq_enable(dev, 1);
223 return ret;
224 }
225
palmas_clear_interrupts(struct device * dev)226 static int palmas_clear_interrupts(struct device *dev)
227 {
228 struct palmas *palmas = dev_get_drvdata(dev->parent);
229 unsigned int rtc_reg;
230 int ret;
231
232 ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
233 &rtc_reg);
234 if (ret < 0) {
235 dev_err(dev, "RTC_STATUS read failed, err = %d\n", ret);
236 return ret;
237 }
238
239 ret = palmas_write(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
240 rtc_reg);
241 if (ret < 0) {
242 dev_err(dev, "RTC_STATUS write failed, err = %d\n", ret);
243 return ret;
244 }
245 return 0;
246 }
247
palmas_rtc_interrupt(int irq,void * context)248 static irqreturn_t palmas_rtc_interrupt(int irq, void *context)
249 {
250 struct palmas_rtc *palmas_rtc = context;
251 struct device *dev = palmas_rtc->dev;
252 int ret;
253
254 ret = palmas_clear_interrupts(dev);
255 if (ret < 0) {
256 dev_err(dev, "RTC interrupt clear failed, err = %d\n", ret);
257 return IRQ_NONE;
258 }
259
260 rtc_update_irq(palmas_rtc->rtc, 1, RTC_IRQF | RTC_AF);
261 return IRQ_HANDLED;
262 }
263
264 static struct rtc_class_ops palmas_rtc_ops = {
265 .read_time = palmas_rtc_read_time,
266 .set_time = palmas_rtc_set_time,
267 .read_alarm = palmas_rtc_read_alarm,
268 .set_alarm = palmas_rtc_set_alarm,
269 .alarm_irq_enable = palmas_rtc_alarm_irq_enable,
270 };
271
palmas_rtc_probe(struct platform_device * pdev)272 static int palmas_rtc_probe(struct platform_device *pdev)
273 {
274 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
275 struct palmas_rtc *palmas_rtc = NULL;
276 int ret;
277
278 palmas_rtc = devm_kzalloc(&pdev->dev, sizeof(struct palmas_rtc),
279 GFP_KERNEL);
280 if (!palmas_rtc)
281 return -ENOMEM;
282
283 /* Clear pending interrupts */
284 ret = palmas_clear_interrupts(&pdev->dev);
285 if (ret < 0) {
286 dev_err(&pdev->dev, "clear RTC int failed, err = %d\n", ret);
287 return ret;
288 }
289
290 palmas_rtc->dev = &pdev->dev;
291 platform_set_drvdata(pdev, palmas_rtc);
292
293 /* Start RTC */
294 ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
295 PALMAS_RTC_CTRL_REG_STOP_RTC,
296 PALMAS_RTC_CTRL_REG_STOP_RTC);
297 if (ret < 0) {
298 dev_err(&pdev->dev, "RTC_CTRL write failed, err = %d\n", ret);
299 return ret;
300 }
301
302 palmas_rtc->irq = platform_get_irq(pdev, 0);
303
304 palmas_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
305 &palmas_rtc_ops, THIS_MODULE);
306 if (IS_ERR(palmas_rtc->rtc)) {
307 ret = PTR_ERR(palmas_rtc->rtc);
308 dev_err(&pdev->dev, "RTC register failed, err = %d\n", ret);
309 return ret;
310 }
311
312 ret = devm_request_threaded_irq(&pdev->dev, palmas_rtc->irq, NULL,
313 palmas_rtc_interrupt,
314 IRQF_TRIGGER_LOW | IRQF_ONESHOT |
315 IRQF_EARLY_RESUME,
316 dev_name(&pdev->dev), palmas_rtc);
317 if (ret < 0) {
318 dev_err(&pdev->dev, "IRQ request failed, err = %d\n", ret);
319 return ret;
320 }
321
322 device_set_wakeup_capable(&pdev->dev, 1);
323 return 0;
324 }
325
palmas_rtc_remove(struct platform_device * pdev)326 static int palmas_rtc_remove(struct platform_device *pdev)
327 {
328 palmas_rtc_alarm_irq_enable(&pdev->dev, 0);
329 return 0;
330 }
331
332 #ifdef CONFIG_PM_SLEEP
palmas_rtc_suspend(struct device * dev)333 static int palmas_rtc_suspend(struct device *dev)
334 {
335 struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
336
337 if (device_may_wakeup(dev))
338 enable_irq_wake(palmas_rtc->irq);
339 return 0;
340 }
341
palmas_rtc_resume(struct device * dev)342 static int palmas_rtc_resume(struct device *dev)
343 {
344 struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
345
346 if (device_may_wakeup(dev))
347 disable_irq_wake(palmas_rtc->irq);
348 return 0;
349 }
350 #endif
351
352 static const struct dev_pm_ops palmas_rtc_pm_ops = {
353 SET_SYSTEM_SLEEP_PM_OPS(palmas_rtc_suspend, palmas_rtc_resume)
354 };
355
356 #ifdef CONFIG_OF
357 static const struct of_device_id of_palmas_rtc_match[] = {
358 { .compatible = "ti,palmas-rtc"},
359 { },
360 };
361 MODULE_DEVICE_TABLE(of, of_palmas_rtc_match);
362 #endif
363
364 static struct platform_driver palmas_rtc_driver = {
365 .probe = palmas_rtc_probe,
366 .remove = palmas_rtc_remove,
367 .driver = {
368 .owner = THIS_MODULE,
369 .name = "palmas-rtc",
370 .pm = &palmas_rtc_pm_ops,
371 .of_match_table = of_match_ptr(of_palmas_rtc_match),
372 },
373 };
374
375 module_platform_driver(palmas_rtc_driver);
376
377 MODULE_ALIAS("platform:palmas_rtc");
378 MODULE_DESCRIPTION("TI PALMAS series RTC driver");
379 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
380 MODULE_LICENSE("GPL v2");
381