staging/lustre: Get rid of cksum_type_t typedef
[linux/fpc-iii.git] / drivers / rtc / rtc-max77686.c
blob7184a0eda79384f8a59a5c8be8155018ba1a792e
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #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>
27 /* RTC Control Register */
28 #define BCD_EN_SHIFT 0
29 #define BCD_EN_MASK (1 << BCD_EN_SHIFT)
30 #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)
37 /* 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)
44 #define MAX77686_RTC_UPDATE_DELAY 16
46 enum {
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
57 struct 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;
64 struct regmap *regmap;
66 int virq;
67 int rtc_24hr_mode;
70 enum MAX77686_RTC_OP {
71 MAX77686_RTC_WRITE,
72 MAX77686_RTC_READ,
75 static void max77686_rtc_data_to_tm(u8 *data, struct rtc_time *tm,
76 int rtc_24hr_mode)
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;
88 /* Only a single bit is set in data[], so fls() would be equivalent */
89 tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f) - 1;
90 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;
97 static int max77686_rtc_tm_to_data(struct rtc_time *tm, u8 *data)
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;
105 data[RTC_YEAR] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
107 if (tm->tm_year < 100) {
108 pr_warn("RTC cannot handle the year %d. Assume it's 2000.\n",
109 1900 + tm->tm_year);
110 return -EINVAL;
112 return 0;
115 static int max77686_rtc_update(struct max77686_rtc_info *info,
116 enum MAX77686_RTC_OP op)
118 int ret;
119 unsigned int data;
121 if (op == MAX77686_RTC_WRITE)
122 data = 1 << RTC_UDR_SHIFT;
123 else
124 data = 1 << RTC_RBUDR_SHIFT;
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);
136 return ret;
139 static int max77686_rtc_read_time(struct device *dev, struct rtc_time *tm)
141 struct max77686_rtc_info *info = dev_get_drvdata(dev);
142 u8 data[RTC_NR_TIME];
143 int ret;
145 mutex_lock(&info->lock);
147 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
148 if (ret < 0)
149 goto out;
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;
158 max77686_rtc_data_to_tm(data, tm, info->rtc_24hr_mode);
160 ret = rtc_valid_tm(tm);
162 out:
163 mutex_unlock(&info->lock);
164 return ret;
167 static int max77686_rtc_set_time(struct device *dev, struct rtc_time *tm)
169 struct max77686_rtc_info *info = dev_get_drvdata(dev);
170 u8 data[RTC_NR_TIME];
171 int ret;
173 ret = max77686_rtc_tm_to_data(tm, data);
174 if (ret < 0)
175 return ret;
177 mutex_lock(&info->lock);
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;
187 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
189 out:
190 mutex_unlock(&info->lock);
191 return ret;
194 static int max77686_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
196 struct max77686_rtc_info *info = dev_get_drvdata(dev);
197 u8 data[RTC_NR_TIME];
198 unsigned int val;
199 int i, ret;
201 mutex_lock(&info->lock);
203 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
204 if (ret < 0)
205 goto out;
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;
215 max77686_rtc_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
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;
225 alrm->pending = 0;
226 ret = regmap_read(info->max77686->regmap, MAX77686_REG_STATUS2, &val);
227 if (ret < 0) {
228 dev_err(info->dev, "%s:%d fail to read status2 reg(%d)\n",
229 __func__, __LINE__, ret);
230 goto out;
233 if (val & (1 << 4)) /* RTCA1 */
234 alrm->pending = 1;
236 out:
237 mutex_unlock(&info->lock);
238 return 0;
241 static int max77686_rtc_stop_alarm(struct max77686_rtc_info *info)
243 u8 data[RTC_NR_TIME];
244 int ret, i;
245 struct rtc_time tm;
247 if (!mutex_is_locked(&info->lock))
248 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
250 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
251 if (ret < 0)
252 goto out;
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;
262 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
264 for (i = 0; i < RTC_NR_TIME; i++)
265 data[i] &= ~ALARM_ENABLE_MASK;
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;
275 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
276 out:
277 return ret;
280 static int max77686_rtc_start_alarm(struct max77686_rtc_info *info)
282 u8 data[RTC_NR_TIME];
283 int ret;
284 struct rtc_time tm;
286 if (!mutex_is_locked(&info->lock))
287 dev_warn(info->dev, "%s: should have mutex locked\n", __func__);
289 ret = max77686_rtc_update(info, MAX77686_RTC_READ);
290 if (ret < 0)
291 goto out;
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;
301 max77686_rtc_data_to_tm(data, &tm, info->rtc_24hr_mode);
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);
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;
322 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
323 out:
324 return ret;
327 static int max77686_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
329 struct max77686_rtc_info *info = dev_get_drvdata(dev);
330 u8 data[RTC_NR_TIME];
331 int ret;
333 ret = max77686_rtc_tm_to_data(&alrm->time, data);
334 if (ret < 0)
335 return ret;
337 mutex_lock(&info->lock);
339 ret = max77686_rtc_stop_alarm(info);
340 if (ret < 0)
341 goto out;
343 ret = regmap_bulk_write(info->max77686->rtc_regmap,
344 MAX77686_ALARM1_SEC, data, RTC_NR_TIME);
346 if (ret < 0) {
347 dev_err(info->dev, "%s: fail to write alarm reg(%d)\n",
348 __func__, ret);
349 goto out;
352 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
353 if (ret < 0)
354 goto out;
356 if (alrm->enabled)
357 ret = max77686_rtc_start_alarm(info);
358 out:
359 mutex_unlock(&info->lock);
360 return ret;
363 static int max77686_rtc_alarm_irq_enable(struct device *dev,
364 unsigned int enabled)
366 struct max77686_rtc_info *info = dev_get_drvdata(dev);
367 int ret;
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);
376 return ret;
379 static irqreturn_t max77686_rtc_alarm_irq(int irq, void *data)
381 struct max77686_rtc_info *info = data;
383 dev_info(info->dev, "%s:irq(%d)\n", __func__, irq);
385 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
387 return IRQ_HANDLED;
390 static 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,
398 static int max77686_rtc_init_reg(struct max77686_rtc_info *info)
400 u8 data[2];
401 int ret;
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);
407 info->rtc_24hr_mode = 1;
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;
416 ret = max77686_rtc_update(info, MAX77686_RTC_WRITE);
417 return ret;
420 static int max77686_rtc_probe(struct platform_device *pdev)
422 struct max77686_dev *max77686 = dev_get_drvdata(pdev->dev.parent);
423 struct max77686_rtc_info *info;
424 int ret;
426 dev_info(&pdev->dev, "%s\n", __func__);
428 info = devm_kzalloc(&pdev->dev, sizeof(struct max77686_rtc_info),
429 GFP_KERNEL);
430 if (!info)
431 return -ENOMEM;
433 mutex_init(&info->lock);
434 info->dev = &pdev->dev;
435 info->max77686 = max77686;
436 info->rtc = max77686->rtc;
438 platform_set_drvdata(pdev, info);
440 ret = max77686_rtc_init_reg(info);
442 if (ret < 0) {
443 dev_err(&pdev->dev, "Failed to initialize RTC reg:%d\n", ret);
444 goto err_rtc;
447 device_init_wakeup(&pdev->dev, 1);
449 info->rtc_dev = devm_rtc_device_register(&pdev->dev, "max77686-rtc",
450 &max77686_rtc_ops, THIS_MODULE);
452 if (IS_ERR(info->rtc_dev)) {
453 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;
460 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;
466 info->virq = regmap_irq_get_virq(max77686->rtc_irq_data,
467 MAX77686_RTCIRQ_RTCA1);
468 if (!info->virq) {
469 ret = -ENXIO;
470 goto err_rtc;
473 ret = devm_request_threaded_irq(&pdev->dev, info->virq, NULL,
474 max77686_rtc_alarm_irq, 0, "rtc-alarm1", info);
475 if (ret < 0)
476 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
477 info->virq, ret);
479 err_rtc:
480 return ret;
483 #ifdef CONFIG_PM_SLEEP
484 static int max77686_rtc_suspend(struct device *dev)
486 if (device_may_wakeup(dev)) {
487 struct max77686_rtc_info *info = dev_get_drvdata(dev);
489 return enable_irq_wake(info->virq);
492 return 0;
495 static int max77686_rtc_resume(struct device *dev)
497 if (device_may_wakeup(dev)) {
498 struct max77686_rtc_info *info = dev_get_drvdata(dev);
500 return disable_irq_wake(info->virq);
503 return 0;
505 #endif
507 static SIMPLE_DEV_PM_OPS(max77686_rtc_pm_ops,
508 max77686_rtc_suspend, max77686_rtc_resume);
510 static const struct platform_device_id rtc_id[] = {
511 { "max77686-rtc", 0 },
514 MODULE_DEVICE_TABLE(platform, rtc_id);
516 static struct platform_driver max77686_rtc_driver = {
517 .driver = {
518 .name = "max77686-rtc",
519 .pm = &max77686_rtc_pm_ops,
521 .probe = max77686_rtc_probe,
522 .id_table = rtc_id,
525 module_platform_driver(max77686_rtc_driver);
527 MODULE_DESCRIPTION("Maxim MAX77686 RTC driver");
528 MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
529 MODULE_LICENSE("GPL");