Staging: hv: hv_mouse: use an anonymous union for struct mousevsc_prt_msg
[wandboard.git] / drivers / rtc / rtc-max8998.c
blob3f7bc6b9fefa94186c020503e8614304606d336b
1 /*
2 * RTC driver for Maxim MAX8998
4 * Copyright (C) 2010 Samsung Electronics Co.Ltd
5 * Author: Minkyu Kang <mk7.kang@samsung.com>
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
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/module.h>
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/bcd.h>
19 #include <linux/rtc.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/max8998.h>
22 #include <linux/mfd/max8998-private.h>
23 #include <linux/delay.h>
25 #define MAX8998_RTC_SEC 0x00
26 #define MAX8998_RTC_MIN 0x01
27 #define MAX8998_RTC_HOUR 0x02
28 #define MAX8998_RTC_WEEKDAY 0x03
29 #define MAX8998_RTC_DATE 0x04
30 #define MAX8998_RTC_MONTH 0x05
31 #define MAX8998_RTC_YEAR1 0x06
32 #define MAX8998_RTC_YEAR2 0x07
33 #define MAX8998_ALARM0_SEC 0x08
34 #define MAX8998_ALARM0_MIN 0x09
35 #define MAX8998_ALARM0_HOUR 0x0a
36 #define MAX8998_ALARM0_WEEKDAY 0x0b
37 #define MAX8998_ALARM0_DATE 0x0c
38 #define MAX8998_ALARM0_MONTH 0x0d
39 #define MAX8998_ALARM0_YEAR1 0x0e
40 #define MAX8998_ALARM0_YEAR2 0x0f
41 #define MAX8998_ALARM1_SEC 0x10
42 #define MAX8998_ALARM1_MIN 0x11
43 #define MAX8998_ALARM1_HOUR 0x12
44 #define MAX8998_ALARM1_WEEKDAY 0x13
45 #define MAX8998_ALARM1_DATE 0x14
46 #define MAX8998_ALARM1_MONTH 0x15
47 #define MAX8998_ALARM1_YEAR1 0x16
48 #define MAX8998_ALARM1_YEAR2 0x17
49 #define MAX8998_ALARM0_CONF 0x18
50 #define MAX8998_ALARM1_CONF 0x19
51 #define MAX8998_RTC_STATUS 0x1a
52 #define MAX8998_WTSR_SMPL_CNTL 0x1b
53 #define MAX8998_TEST 0x1f
55 #define HOUR_12 (1 << 7)
56 #define HOUR_PM (1 << 5)
57 #define ALARM0_STATUS (1 << 1)
58 #define ALARM1_STATUS (1 << 2)
60 enum {
61 RTC_SEC = 0,
62 RTC_MIN,
63 RTC_HOUR,
64 RTC_WEEKDAY,
65 RTC_DATE,
66 RTC_MONTH,
67 RTC_YEAR1,
68 RTC_YEAR2,
71 struct max8998_rtc_info {
72 struct device *dev;
73 struct max8998_dev *max8998;
74 struct i2c_client *rtc;
75 struct rtc_device *rtc_dev;
76 int irq;
77 bool lp3974_bug_workaround;
80 static void max8998_data_to_tm(u8 *data, struct rtc_time *tm)
82 tm->tm_sec = bcd2bin(data[RTC_SEC]);
83 tm->tm_min = bcd2bin(data[RTC_MIN]);
84 if (data[RTC_HOUR] & HOUR_12) {
85 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
86 if (data[RTC_HOUR] & HOUR_PM)
87 tm->tm_hour += 12;
88 } else
89 tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
91 tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
92 tm->tm_mday = bcd2bin(data[RTC_DATE]);
93 tm->tm_mon = bcd2bin(data[RTC_MONTH]);
94 tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
95 tm->tm_year -= 1900;
98 static void max8998_tm_to_data(struct rtc_time *tm, u8 *data)
100 data[RTC_SEC] = bin2bcd(tm->tm_sec);
101 data[RTC_MIN] = bin2bcd(tm->tm_min);
102 data[RTC_HOUR] = bin2bcd(tm->tm_hour);
103 data[RTC_WEEKDAY] = tm->tm_wday;
104 data[RTC_DATE] = bin2bcd(tm->tm_mday);
105 data[RTC_MONTH] = bin2bcd(tm->tm_mon);
106 data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
107 data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
110 static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm)
112 struct max8998_rtc_info *info = dev_get_drvdata(dev);
113 u8 data[8];
114 int ret;
116 ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data);
117 if (ret < 0)
118 return ret;
120 max8998_data_to_tm(data, tm);
122 return rtc_valid_tm(tm);
125 static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm)
127 struct max8998_rtc_info *info = dev_get_drvdata(dev);
128 u8 data[8];
129 int ret;
131 max8998_tm_to_data(tm, data);
133 ret = max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data);
135 if (info->lp3974_bug_workaround)
136 msleep(2000);
138 return ret;
141 static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
143 struct max8998_rtc_info *info = dev_get_drvdata(dev);
144 u8 data[8];
145 u8 val;
146 int ret;
148 ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data);
149 if (ret < 0)
150 return ret;
152 max8998_data_to_tm(data, &alrm->time);
154 ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val);
155 if (ret < 0)
156 return ret;
158 alrm->enabled = !!val;
160 ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val);
161 if (ret < 0)
162 return ret;
164 if (val & ALARM0_STATUS)
165 alrm->pending = 1;
166 else
167 alrm->pending = 0;
169 return 0;
172 static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info)
174 int ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0);
176 if (info->lp3974_bug_workaround)
177 msleep(2000);
179 return ret;
182 static int max8998_rtc_start_alarm(struct max8998_rtc_info *info)
184 int ret;
185 u8 alarm0_conf = 0x77;
187 /* LP3974 with delay bug chips has rtc alarm bugs with "MONTH" field */
188 if (info->lp3974_bug_workaround)
189 alarm0_conf = 0x57;
191 ret = max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, alarm0_conf);
193 if (info->lp3974_bug_workaround)
194 msleep(2000);
196 return ret;
199 static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
201 struct max8998_rtc_info *info = dev_get_drvdata(dev);
202 u8 data[8];
203 int ret;
205 max8998_tm_to_data(&alrm->time, data);
207 ret = max8998_rtc_stop_alarm(info);
208 if (ret < 0)
209 return ret;
211 ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data);
212 if (ret < 0)
213 return ret;
215 if (info->lp3974_bug_workaround)
216 msleep(2000);
218 if (alrm->enabled)
219 ret = max8998_rtc_start_alarm(info);
221 return ret;
224 static int max8998_rtc_alarm_irq_enable(struct device *dev,
225 unsigned int enabled)
227 struct max8998_rtc_info *info = dev_get_drvdata(dev);
229 if (enabled)
230 return max8998_rtc_start_alarm(info);
231 else
232 return max8998_rtc_stop_alarm(info);
235 static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data)
237 struct max8998_rtc_info *info = data;
239 rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
241 return IRQ_HANDLED;
244 static const struct rtc_class_ops max8998_rtc_ops = {
245 .read_time = max8998_rtc_read_time,
246 .set_time = max8998_rtc_set_time,
247 .read_alarm = max8998_rtc_read_alarm,
248 .set_alarm = max8998_rtc_set_alarm,
249 .alarm_irq_enable = max8998_rtc_alarm_irq_enable,
252 static int __devinit max8998_rtc_probe(struct platform_device *pdev)
254 struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent);
255 struct max8998_platform_data *pdata = dev_get_platdata(max8998->dev);
256 struct max8998_rtc_info *info;
257 int ret;
259 info = kzalloc(sizeof(struct max8998_rtc_info), GFP_KERNEL);
260 if (!info)
261 return -ENOMEM;
263 info->dev = &pdev->dev;
264 info->max8998 = max8998;
265 info->rtc = max8998->rtc;
266 info->irq = max8998->irq_base + MAX8998_IRQ_ALARM0;
268 info->rtc_dev = rtc_device_register("max8998-rtc", &pdev->dev,
269 &max8998_rtc_ops, THIS_MODULE);
271 if (IS_ERR(info->rtc_dev)) {
272 ret = PTR_ERR(info->rtc_dev);
273 dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret);
274 goto out_rtc;
277 platform_set_drvdata(pdev, info);
279 ret = request_threaded_irq(info->irq, NULL, max8998_rtc_alarm_irq, 0,
280 "rtc-alarm0", info);
282 if (ret < 0)
283 dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
284 info->irq, ret);
286 dev_info(&pdev->dev, "RTC CHIP NAME: %s\n", pdev->id_entry->name);
287 if (pdata->rtc_delay) {
288 info->lp3974_bug_workaround = true;
289 dev_warn(&pdev->dev, "LP3974 with RTC REGERR option."
290 " RTC updates will be extremely slow.\n");
293 return 0;
295 out_rtc:
296 kfree(info);
297 return ret;
300 static int __devexit max8998_rtc_remove(struct platform_device *pdev)
302 struct max8998_rtc_info *info = platform_get_drvdata(pdev);
304 if (info) {
305 free_irq(info->irq, info);
306 rtc_device_unregister(info->rtc_dev);
307 kfree(info);
310 return 0;
313 static const struct platform_device_id max8998_rtc_id[] = {
314 { "max8998-rtc", TYPE_MAX8998 },
315 { "lp3974-rtc", TYPE_LP3974 },
319 static struct platform_driver max8998_rtc_driver = {
320 .driver = {
321 .name = "max8998-rtc",
322 .owner = THIS_MODULE,
324 .probe = max8998_rtc_probe,
325 .remove = __devexit_p(max8998_rtc_remove),
326 .id_table = max8998_rtc_id,
329 static int __init max8998_rtc_init(void)
331 return platform_driver_register(&max8998_rtc_driver);
333 module_init(max8998_rtc_init);
335 static void __exit max8998_rtc_exit(void)
337 platform_driver_unregister(&max8998_rtc_driver);
339 module_exit(max8998_rtc_exit);
341 MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>");
342 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
343 MODULE_DESCRIPTION("Maxim MAX8998 RTC driver");
344 MODULE_LICENSE("GPL");