drm/i915: Make mmio flip wait for seqno in the work function
[linux/fpc-iii.git] / drivers / rtc / rtc-max77686.c
blobcf73e969c8cc9a1ac38add953e79f6f9f6892094
1 /*
2 * RTC driver for Maxim MAX77686
4 * Copyright (C) 2012 Samsung Electronics Co.Ltd
6 * based on rtc-max8997.c
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.
15 #include <linux/slab.h>
16 #include <linux/rtc.h>
17 #include <linux/delay.h>
18 #include <linux/mutex.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/max77686-private.h>
22 #include <linux/irqdomain.h>
23 #include <linux/regmap.h>
25 /* RTC Control Register */
26 #define BCD_EN_SHIFT 0
27 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
28 #define MODEL24_SHIFT 1
29 #define MODEL24_MASK (1 << MODEL24_SHIFT)
30 /* RTC Update Register1 */
31 #define RTC_UDR_SHIFT 0
32 #define RTC_UDR_MASK (1 << RTC_UDR_SHIFT)
33 #define RTC_RBUDR_SHIFT 4
34 #define RTC_RBUDR_MASK (1 << RTC_RBUDR_SHIFT)
35 /* RTC Hour register */
36 #define HOUR_PM_SHIFT 6
37 #define HOUR_PM_MASK (1 << HOUR_PM_SHIFT)
38 /* RTC Alarm Enable */
39 #define ALARM_ENABLE_SHIFT 7
40 #define ALARM_ENABLE_MASK (1 << ALARM_ENABLE_SHIFT)
42 #define MAX77686_RTC_UPDATE_DELAY 16
44 enum {
45 RTC_SEC = 0,
46 RTC_MIN,
47 RTC_HOUR,
48 RTC_WEEKDAY,
49 RTC_MONTH,
50 RTC_YEAR,
51 RTC_DATE,
52 RTC_NR_TIME
55 struct max77686_rtc_info {
56 struct device *dev;
57 struct max77686_dev *max77686;
58 struct i2c_client *rtc;
59 struct rtc_device *rtc_dev;
60 struct mutex lock;
62 struct regmap *regmap;
64 int virq;
65 int rtc_24hr_mode;
68 enum MAX77686_RTC_OP {
69 MAX77686_RTC_WRITE,
70 MAX77686_RTC_READ,
73 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
74 int rtc_24hr_mode)
76 tm->tm_sec = data[RTC_SEC] & 0x7f;
77 tm->tm_min = data[RTC_MIN] & 0x7f;
78 if (rtc_24hr_mode)
79 tm->tm_hour = data[RTC_HOUR] & 0x1f;
80 else {
81 tm->tm_hour = data[RTC_HOUR] & 0x0f;
82 if (data[RTC_HOUR] & HOUR_PM_MASK)
83 tm->tm_hour += 12;
86 /* Only a single bit is set in data[], so fls() would be equivalent */
87 tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f) - 1;
88 tm->tm_mday = data[RTC_DATE] & 0x1f;
89 tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
90 tm->tm_year = (data[RTC_YEAR] & 0x7f) + 100;
91 tm->tm_yday = 0;
92 tm->tm_isdst = 0;
95 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
97 data[RTC_SEC] = tm->tm_sec;
98 data[RTC_MIN] = tm->tm_min;
99 data[RTC_HOUR] = tm->tm_hour;
100 data[RTC_WEEKDAY] = 1 << tm->tm_wday;
101 data[RTC_DATE] = tm->tm_mday;
102 data[RTC_MONTH] = tm->tm_mon + 1;
103 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
105 if (tm->tm_year < 100) {
106 pr_warn("%s: MAX77686 RTC cannot handle the year %d."
107 "Assume it's 2000.\n", __func__, 1900 + tm->tm_year);
108 return -EINVAL;
110 return 0;
113 static int max77686_rtc_update(struct max77686_rtc_info *info,
114 enum MAX77686_RTC_OP op)
116 int ret;
117 unsigned int data;
119 if (op == MAX77686_RTC_WRITE)
120 data = 1 << RTC_UDR_SHIFT;
121 else
122 data = 1 << RTC_RBUDR_SHIFT;
124 ret = regmap_update_bits(info->max77686->rtc_regmap,
125 MAX77686_RTC_UPDATE0, data, data);
126 if (ret < 0)
127 dev_err(info->dev, "%s: fail to write update reg(ret=%d, data=0x%x)\n",
128 __func__, ret, data);
129 else {
130 /* Minimum 16ms delay required before RTC update. */
131 msleep(MAX77686_RTC_UPDATE_DELAY);
134 return ret;
137 static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
139 struct max77686_rtc_info *info = dev_get_drvdata(dev);
140 u8 data[RTC_NR_TIME];
141 int ret;
143 mutex_lock(&info->lock);
145 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
146 if (ret < 0)
147 goto out;
149 ret = regmap_bulk_read(info->max77686->rtc_regmap,
150 MAX77686_RTC_SEC, data, RTC_NR_TIME);
151 if (ret < 0) {
152 dev_err(info->dev, "%s: fail to read time reg(%d)\n", __func__, ret);
153 goto out;
156 max77686_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
158 ret = rtc_valid_tm(tm);
160 out:
161 mutex_unlock(&info->lock);
162 return ret;
165 static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
167 struct max77686_rtc_info *info = dev_get_drvdata(dev);
168 u8 data[RTC_NR_TIME];
169 int ret;
171 ret = max77686_rtc_tm_to_data(tm, data);
172 if (ret < 0)
173 return ret;
175 mutex_lock(&info->lock);
177 ret = regmap_bulk_write(info->max77686->rtc_regmap,
178 MAX77686_RTC_SEC, data, RTC_NR_TIME);
179 if (ret < 0) {
180 dev_err(info->dev, "%s: fail to write time reg(%d)\n", __func__,
181 ret);
182 goto out;
185 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
187 out:
188 mutex_unlock(&info->lock);
189 return ret;
192 static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
194 struct max77686_rtc_info *info = dev_get_drvdata(dev);
195 u8 data[RTC_NR_TIME];
196 unsigned int val;
197 int i, ret;
199 mutex_lock(&info->lock);
201 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
202 if (ret < 0)
203 goto out;
205 ret = regmap_bulk_read(info->max77686->rtc_regmap,
206 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
207 if (ret < 0) {
208 dev_err(info->dev, "%s:%d fail to read alarm reg(%d)\n",
209 __func__, __LINE__, ret);
210 goto out;
213 max77686_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
215 alrm->enabled = 0;
216 for (i = 0; i < RTC_NR_TIME; i++) {
217 if (data[i] & ALARM_ENABLE_MASK) {
218 alrm->enabled = 1;
219 break;
223 alrm->pending = 0;
224 ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
225 if (ret < 0) {
226 dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
227 __func__, __LINE__, ret);
228 goto out;
231 if (val & (1 << 4)) /* RTCA1 */
232 alrm->pending = 1;
234 out:
235 mutex_unlock(&info->lock);
236 return 0;
239 static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
241 u8 data[RTC_NR_TIME];
242 int ret, i;
243 struct rtc_time tm;
245 if (!mutex_is_locked(&info->lock))
246 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
248 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
249 if (ret < 0)
250 goto out;
252 ret = regmap_bulk_read(info->max77686->rtc_regmap,
253 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
254 if (ret < 0) {
255 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
256 __func__, ret);
257 goto out;
260 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
262 for (i = 0; i < RTC_NR_TIME; i++)
263 data[i] &= ~ALARM_ENABLE_MASK;
265 ret = regmap_bulk_write(info->max77686->rtc_regmap,
266 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
267 if (ret < 0) {
268 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
269 __func__, ret);
270 goto out;
273 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
274 out:
275 return ret;
278 static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
280 u8 data[RTC_NR_TIME];
281 int ret;
282 struct rtc_time tm;
284 if (!mutex_is_locked(&info->lock))
285 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
287 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
288 if (ret < 0)
289 goto out;
291 ret = regmap_bulk_read(info->max77686->rtc_regmap,
292 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
293 if (ret < 0) {
294 dev_err(info->dev, "%s: fail to read alarm reg(%d)\n",
295 __func__, ret);
296 goto out;
299 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
301 data[RTC_SEC] |= (1 << ALARM_ENABLE_SHIFT);
302 data[RTC_MIN] |= (1 << ALARM_ENABLE_SHIFT);
303 data[RTC_HOUR] |= (1 << ALARM_ENABLE_SHIFT);
304 data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
305 if (data[RTC_MONTH] & 0xf)
306 data[RTC_MONTH] |= (1 << ALARM_ENABLE_SHIFT);
307 if (data[RTC_YEAR] & 0x7f)
308 data[RTC_YEAR] |= (1 << ALARM_ENABLE_SHIFT);
309 if (data[RTC_DATE] & 0x1f)
310 data[RTC_DATE] |= (1 << ALARM_ENABLE_SHIFT);
312 ret = regmap_bulk_write(info->max77686->rtc_regmap,
313 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
314 if (ret < 0) {
315 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
316 __func__, ret);
317 goto out;
320 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
321 out:
322 return ret;
325 static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
327 struct max77686_rtc_info *info = dev_get_drvdata(dev);
328 u8 data[RTC_NR_TIME];
329 int ret;
331 ret = max77686_rtc_tm_to_data(&alrm->time, data);
332 if (ret < 0)
333 return ret;
335 mutex_lock(&info->lock);
337 ret = max77686_rtc_stop_alarm(info);
338 if (ret < 0)
339 goto out;
341 ret = regmap_bulk_write(info->max77686->rtc_regmap,
342 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
344 if (ret < 0) {
345 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
346 __func__, ret);
347 goto out;
350 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
351 if (ret < 0)
352 goto out;
354 if (alrm->enabled)
355 ret = max77686_rtc_start_alarm(info);
356 out:
357 mutex_unlock(&info->lock);
358 return ret;
361 static int max77686_rtc_alarm_irq_enable(struct device *dev,
362 unsigned int enabled)
364 struct max77686_rtc_info *info = dev_get_drvdata(dev);
365 int ret;
367 mutex_lock(&info->lock);
368 if (enabled)
369 ret = max77686_rtc_start_alarm(info);
370 else
371 ret = max77686_rtc_stop_alarm(info);
372 mutex_unlock(&info->lock);
374 return ret;
377 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
379 struct max77686_rtc_info *info = data;
381 dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
383 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
385 return IRQ_HANDLED;
388 static const struct rtc_class_ops max77686_rtc_ops = {
389 .read_time = max77686_rtc_read_time,
390 .set_time = max77686_rtc_set_time,
391 .read_alarm = max77686_rtc_read_alarm,
392 .set_alarm = max77686_rtc_set_alarm,
393 .alarm_irq_enable = max77686_rtc_alarm_irq_enable,
396 static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
398 u8 data[2];
399 int ret;
401 /* Set RTC control register : Binary mode, 24hour mdoe */
402 data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
403 data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
405 info->rtc_24hr_mode = 1;
407 ret = regmap_bulk_write(info->max77686->rtc_regmap, MAX77686_RTC_CONTROLM, data, 2);
408 if (ret < 0) {
409 dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
410 __func__, ret);
411 return ret;
414 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
415 return ret;
418 static int max77686_rtc_probe(struct platform_device *pdev)
420 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
421 struct max77686_rtc_info *info;
422 int ret;
424 dev_info(&pdev->dev, "%s\n", __func__);
426 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
427 GFP_KERNEL);
428 if (!info)
429 return -ENOMEM;
431 mutex_init(&info->lock);
432 info->dev = &pdev->dev;
433 info->max77686 = max77686;
434 info->rtc = max77686->rtc;
436 platform_set_drvdata(pdev, info);
438 ret = max77686_rtc_init_reg(info);
440 if (ret < 0) {
441 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
442 goto err_rtc;
445 device_init_wakeup(&pdev->dev, 1);
447 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
448 &max77686_rtc_ops, THIS_MODULE);
450 if (IS_ERR(info->rtc_dev)) {
451 ret = PTR_ERR(info->rtc_dev);
452 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
453 if (ret == 0)
454 ret = -EINVAL;
455 goto err_rtc;
458 if (!max77686->rtc_irq_data) {
459 ret = -EINVAL;
460 dev_err(&pdev->dev, "%s: no RTC regmap IRQ chip\n", __func__);
461 goto err_rtc;
464 info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
465 MAX77686_RTCIRQ_RTCA1);
466 if (!info->virq) {
467 ret = -ENXIO;
468 goto err_rtc;
471 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
472 max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
473 if (ret < 0)
474 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
475 info->virq, ret);
477 err_rtc:
478 return ret;
481 #ifdef CONFIG_PM_SLEEP
482 static int max77686_rtc_suspend(struct device *dev)
484 if (device_may_wakeup(dev)) {
485 struct max77686_rtc_info *info = dev_get_drvdata(dev);
487 return enable_irq_wake(info->virq);
490 return 0;
493 static int max77686_rtc_resume(struct device *dev)
495 if (device_may_wakeup(dev)) {
496 struct max77686_rtc_info *info = dev_get_drvdata(dev);
498 return disable_irq_wake(info->virq);
501 return 0;
503 #endif
505 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
506 max77686_rtc_suspend, max77686_rtc_resume);
508 static const struct platform_device_id rtc_id[] = {
509 { "max77686-rtc", 0 },
513 static struct platform_driver max77686_rtc_driver = {
514 .driver = {
515 .name = "max77686-rtc",
516 .owner = THIS_MODULE,
517 .pm = &max77686_rtc_pm_ops,
519 .probe = max77686_rtc_probe,
520 .id_table = rtc_id,
523 module_platform_driver(max77686_rtc_driver);
525 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
526 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
527 MODULE_LICENSE("GPL");