blob: 7184a0eda79384f8a59a5c8be8155018ba1a792e [file] [log] [blame]
Jonghwa Leefca1dd02013-02-21 16:44:26 -08001/*
2 * RTC driver for Maxim MAX77686
3 *
4 * Copyright (C) 2012 Samsung Electronics Co.Ltd
5 *
6 * based on rtc-max8997.c
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 */
14
Joe Perchesa737e832015-04-16 12:46:14 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Jonghwa Leefca1dd02013-02-21 16:44:26 -080017#include <linux/slab.h>
18#include <linux/rtc.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/mfd/max77686-private.h>
24#include <linux/irqdomain.h>
25#include <linux/regmap.h>
26
27/* RTC Control Register */
28#define BCD_EN_SHIFT 0
Jingoo Hanac60bf32013-04-29 16:18:31 -070029#define BCD_EN_MASK (1 << BCD_EN_SHIFT)
Jonghwa Leefca1dd02013-02-21 16:44:26 -080030#define MODEL24_SHIFT 1
31#define MODEL24_MASK (1 << MODEL24_SHIFT)
32/* RTC Update Register1 */
33#define RTC_UDR_SHIFT 0
34#define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
35#define RTC_RBUDR_SHIFT 4
36#define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
Jonghwa Leefca1dd02013-02-21 16:44:26 -080037/* RTC Hour register */
38#define HOUR_PM_SHIFT 6
39#define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
40/* RTC Alarm Enable */
41#define ALARM_ENABLE_SHIFT 7
42#define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
43
44#define MAX77686_RTC_UPDATE_DELAY 16
Jonghwa Leefca1dd02013-02-21 16:44:26 -080045
46enum {
47 RTC_SEC = 0,
48 RTC_MIN,
49 RTC_HOUR,
50 RTC_WEEKDAY,
51 RTC_MONTH,
52 RTC_YEAR,
53 RTC_DATE,
54 RTC_NR_TIME
55};
56
57struct max77686_rtc_info {
58 struct device *dev;
59 struct max77686_dev *max77686;
60 struct i2c_client *rtc;
61 struct rtc_device *rtc_dev;
62 struct mutex lock;
63
64 struct regmap *regmap;
65
66 int virq;
67 int rtc_24hr_mode;
68};
69
70enum MAX77686_RTC_OP {
71 MAX77686_RTC_WRITE,
72 MAX77686_RTC_READ,
73};
74
Jonghwa Leefca1dd02013-02-21 16:44:26 -080075static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
76 int rtc_24hr_mode)
77{
78 tm->tm_sec = data[RTC_SEC] & 0x7f;
79 tm->tm_min = data[RTC_MIN] & 0x7f;
80 if (rtc_24hr_mode)
81 tm->tm_hour = data[RTC_HOUR] & 0x1f;
82 else {
83 tm->tm_hour = data[RTC_HOUR] & 0x0f;
84 if (data[RTC_HOUR] & HOUR_PM_MASK)
85 tm->tm_hour += 12;
86 }
87
Javier Martinez Canillasa20cd882014-10-13 15:53:03 -070088 /* Only a single bit is set in data[], so fls() would be equivalent */
89 tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f) - 1;
Jonghwa Leefca1dd02013-02-21 16:44:26 -080090 tm->tm_mday = data[RTC_DATE] & 0x1f;
91 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
92 tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
93 tm->tm_yday = 0;
94 tm->tm_isdst = 0;
95}
96
97static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
98{
99 data[RTC_SEC] = tm->tm_sec;
100 data[RTC_MIN] = tm->tm_min;
101 data[RTC_HOUR] = tm->tm_hour;
102 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
103 data[RTC_DATE] = tm->tm_mday;
104 data[RTC_MONTH] = tm->tm_mon + 1;
Sachin Kamatcdf5f4a2013-07-03 15:05:55 -0700105 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800106
107 if (tm->tm_year < 100) {
Joe Perchesa737e832015-04-16 12:46:14 -0700108 pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
109 1900 + tm->tm_year);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800110 return -EINVAL;
111 }
112 return 0;
113}
114
115static int max77686_rtc_update(struct max77686_rtc_info *info,
116 enum MAX77686_RTC_OP op)
117{
118 int ret;
119 unsigned int data;
120
121 if (op == MAX77686_RTC_WRITE)
122 data = 1 << RTC_UDR_SHIFT;
123 else
124 data = 1 << RTC_RBUDR_SHIFT;
125
126 ret = regmap_update_bits(info->max77686->rtc_regmap,
127 MAX77686_RTC_UPDATE0, data, data);
128 if (ret < 0)
129 dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
130 __func__, ret, data);
131 else {
132 /* Minimum 16ms delay required before RTC update. */
133 msleep(MAX77686_RTC_UPDATE_DELAY);
134 }
135
136 return ret;
137}
138
139static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
140{
141 struct max77686_rtc_info *info = dev_get_drvdata(dev);
142 u8 data[RTC_NR_TIME];
143 int ret;
144
145 mutex_lock(&info->lock);
146
147 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
148 if (ret < 0)
149 goto out;
150
151 ret = regmap_bulk_read(info->max77686->rtc_regmap,
152 MAX77686_RTC_SEC, data, RTC_NR_TIME);
153 if (ret < 0) {
154 dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__, ret);
155 goto out;
156 }
157
158 max77686_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
159
160 ret = rtc_valid_tm(tm);
161
162out:
163 mutex_unlock(&info->lock);
164 return ret;
165}
166
167static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
168{
169 struct max77686_rtc_info *info = dev_get_drvdata(dev);
170 u8 data[RTC_NR_TIME];
171 int ret;
172
173 ret = max77686_rtc_tm_to_data(tm, data);
174 if (ret < 0)
175 return ret;
176
177 mutex_lock(&info->lock);
178
179 ret = regmap_bulk_write(info->max77686->rtc_regmap,
180 MAX77686_RTC_SEC, data, RTC_NR_TIME);
181 if (ret < 0) {
182 dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
183 ret);
184 goto out;
185 }
186
187 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
188
189out:
190 mutex_unlock(&info->lock);
191 return ret;
192}
193
194static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
195{
196 struct max77686_rtc_info *info = dev_get_drvdata(dev);
197 u8 data[RTC_NR_TIME];
198 unsigned int val;
199 int i, ret;
200
201 mutex_lock(&info->lock);
202
203 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
204 if (ret < 0)
205 goto out;
206
207 ret = regmap_bulk_read(info->max77686->rtc_regmap,
208 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
209 if (ret < 0) {
210 dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
211 __func__, __LINE__, ret);
212 goto out;
213 }
214
215 max77686_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
216
217 alrm->enabled = 0;
218 for (i = 0; i < RTC_NR_TIME; i++) {
219 if (data[i] & ALARM_ENABLE_MASK) {
220 alrm->enabled = 1;
221 break;
222 }
223 }
224
225 alrm->pending = 0;
Sangjung Woo1748cbf2013-09-11 14:24:21 -0700226 ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800227 if (ret < 0) {
Sangjung Woo1748cbf2013-09-11 14:24:21 -0700228 dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800229 __func__, __LINE__, ret);
230 goto out;
231 }
232
233 if (val & (1 << 4)) /* RTCA1 */
234 alrm->pending = 1;
235
236out:
237 mutex_unlock(&info->lock);
238 return 0;
239}
240
241static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
242{
243 u8 data[RTC_NR_TIME];
244 int ret, i;
245 struct rtc_time tm;
246
247 if (!mutex_is_locked(&info->lock))
248 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
249
250 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
251 if (ret < 0)
252 goto out;
253
254 ret = regmap_bulk_read(info->max77686->rtc_regmap,
255 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
256 if (ret < 0) {
257 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
258 __func__, ret);
259 goto out;
260 }
261
262 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
263
264 for (i = 0; i < RTC_NR_TIME; i++)
265 data[i] &= ~ALARM_ENABLE_MASK;
266
267 ret = regmap_bulk_write(info->max77686->rtc_regmap,
268 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
269 if (ret < 0) {
270 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
271 __func__, ret);
272 goto out;
273 }
274
275 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
276out:
277 return ret;
278}
279
280static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
281{
282 u8 data[RTC_NR_TIME];
283 int ret;
284 struct rtc_time tm;
285
286 if (!mutex_is_locked(&info->lock))
287 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
288
289 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
290 if (ret < 0)
291 goto out;
292
293 ret = regmap_bulk_read(info->max77686->rtc_regmap,
294 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
295 if (ret < 0) {
296 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
297 __func__, ret);
298 goto out;
299 }
300
301 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
302
303 data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
304 data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
305 data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
306 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
307 if (data[RTC_MONTH] & 0xf)
308 data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
309 if (data[RTC_YEAR] & 0x7f)
310 data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
311 if (data[RTC_DATE] & 0x1f)
312 data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
313
314 ret = regmap_bulk_write(info->max77686->rtc_regmap,
315 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
316 if (ret < 0) {
317 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
318 __func__, ret);
319 goto out;
320 }
321
322 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
323out:
324 return ret;
325}
326
327static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
328{
329 struct max77686_rtc_info *info = dev_get_drvdata(dev);
330 u8 data[RTC_NR_TIME];
331 int ret;
332
333 ret = max77686_rtc_tm_to_data(&alrm->time, data);
334 if (ret < 0)
335 return ret;
336
337 mutex_lock(&info->lock);
338
339 ret = max77686_rtc_stop_alarm(info);
340 if (ret < 0)
341 goto out;
342
343 ret = regmap_bulk_write(info->max77686->rtc_regmap,
344 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
345
346 if (ret < 0) {
347 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
348 __func__, ret);
349 goto out;
350 }
351
352 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
353 if (ret < 0)
354 goto out;
355
356 if (alrm->enabled)
357 ret = max77686_rtc_start_alarm(info);
358out:
359 mutex_unlock(&info->lock);
360 return ret;
361}
362
363static int max77686_rtc_alarm_irq_enable(struct device *dev,
364 unsigned int enabled)
365{
366 struct max77686_rtc_info *info = dev_get_drvdata(dev);
367 int ret;
368
369 mutex_lock(&info->lock);
370 if (enabled)
371 ret = max77686_rtc_start_alarm(info);
372 else
373 ret = max77686_rtc_stop_alarm(info);
374 mutex_unlock(&info->lock);
375
376 return ret;
377}
378
379static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
380{
381 struct max77686_rtc_info *info = data;
382
383 dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
384
385 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
386
387 return IRQ_HANDLED;
388}
389
390static const struct rtc_class_ops max77686_rtc_ops = {
391 .read_time = max77686_rtc_read_time,
392 .set_time = max77686_rtc_set_time,
393 .read_alarm = max77686_rtc_read_alarm,
394 .set_alarm = max77686_rtc_set_alarm,
395 .alarm_irq_enable = max77686_rtc_alarm_irq_enable,
396};
397
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800398static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
399{
400 u8 data[2];
401 int ret;
402
403 /* Set RTC control register : Binary mode, 24hour mdoe */
404 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
405 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
406
407 info->rtc_24hr_mode = 1;
408
409 ret = regmap_bulk_write(info->max77686->rtc_regmap, MAX77686_RTC_CONTROLM, data, 2);
410 if (ret < 0) {
411 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
412 __func__, ret);
413 return ret;
414 }
415
416 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
417 return ret;
418}
419
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800420static int max77686_rtc_probe(struct platform_device *pdev)
421{
422 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
423 struct max77686_rtc_info *info;
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200424 int ret;
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800425
Jingoo Han3cebeb52013-02-21 16:45:24 -0800426 dev_info(&pdev->dev, "%s\n", __func__);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800427
Jingoo Han0f64f852013-04-29 16:18:30 -0700428 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
429 GFP_KERNEL);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800430 if (!info)
431 return -ENOMEM;
432
433 mutex_init(&info->lock);
434 info->dev = &pdev->dev;
435 info->max77686 = max77686;
436 info->rtc = max77686->rtc;
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200437
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800438 platform_set_drvdata(pdev, info);
439
440 ret = max77686_rtc_init_reg(info);
441
442 if (ret < 0) {
443 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
444 goto err_rtc;
445 }
446
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800447 device_init_wakeup(&pdev->dev, 1);
448
Jingoo Hanf56950e2013-04-29 16:19:05 -0700449 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
450 &max77686_rtc_ops, THIS_MODULE);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800451
452 if (IS_ERR(info->rtc_dev)) {
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800453 ret = PTR_ERR(info->rtc_dev);
454 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
455 if (ret == 0)
456 ret = -EINVAL;
457 goto err_rtc;
458 }
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200459
Javier Martinez Canillas1745d6d2014-10-13 15:52:59 -0700460 if (!max77686->rtc_irq_data) {
461 ret = -EINVAL;
462 dev_err(&pdev->dev, "%s: no RTC regmap IRQ chip\n", __func__);
463 goto err_rtc;
464 }
465
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200466 info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
467 MAX77686_RTCIRQ_RTCA1);
468 if (!info->virq) {
Sachin Kamatad819032013-04-29 16:20:12 -0700469 ret = -ENXIO;
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800470 goto err_rtc;
Sachin Kamatad819032013-04-29 16:20:12 -0700471 }
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800472
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200473 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
474 max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
Sachin Kamatad819032013-04-29 16:20:12 -0700475 if (ret < 0)
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800476 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
477 info->virq, ret);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800478
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800479err_rtc:
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800480 return ret;
481}
482
Doug Andersone7f7fc72014-10-13 15:52:55 -0700483#ifdef CONFIG_PM_SLEEP
484static int max77686_rtc_suspend(struct device *dev)
485{
486 if (device_may_wakeup(dev)) {
487 struct max77686_rtc_info *info = dev_get_drvdata(dev);
488
489 return enable_irq_wake(info->virq);
490 }
491
492 return 0;
493}
494
495static int max77686_rtc_resume(struct device *dev)
496{
497 if (device_may_wakeup(dev)) {
498 struct max77686_rtc_info *info = dev_get_drvdata(dev);
499
500 return disable_irq_wake(info->virq);
501 }
502
503 return 0;
504}
505#endif
506
507static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
508 max77686_rtc_suspend, max77686_rtc_resume);
509
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800510static const struct platform_device_id rtc_id[] = {
511 { "max77686-rtc", 0 },
512 {},
513};
Javier Martinez Canillas2d0cca02015-05-13 17:18:01 +0200514MODULE_DEVICE_TABLE(platform, rtc_id);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800515
516static struct platform_driver max77686_rtc_driver = {
517 .driver = {
518 .name = "max77686-rtc",
Doug Andersone7f7fc72014-10-13 15:52:55 -0700519 .pm = &max77686_rtc_pm_ops,
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800520 },
521 .probe = max77686_rtc_probe,
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800522 .id_table = rtc_id,
523};
524
Jingoo Han0c58ff52013-04-29 16:18:28 -0700525module_platform_driver(max77686_rtc_driver);
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800526
527MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
Jingoo Hanf5b1d3c2013-04-29 16:18:29 -0700528MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
Jonghwa Leefca1dd02013-02-21 16:44:26 -0800529MODULE_LICENSE("GPL");