diff options
author | Joonyoung Shim <jy0922.shim@samsung.com> | 2010-08-05 22:28:08 -0400 |
---|---|---|
committer | Samuel Ortiz <sameo@linux.intel.com> | 2010-10-28 18:29:06 -0400 |
commit | 9b16c0a43b74393cc18666a7748293812c61af1f (patch) | |
tree | f75874e30996e6d6b1dc5a975d19f7455c3e44d6 /drivers | |
parent | 2c7e6f5797140b33ec2b967ff28941e1c7eff4b2 (diff) |
rtc: Add MAX8998 rtc driver
This adds support for the RTC provided by the Maxim 8998 chip. This
driver was tested on a GONI board by using the rtc-test application from
the Documentation/rtc.txt.
Signed-off-by: Joonyoung Shim <jy0922.shim@samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
Acked-by: Alessandro Zummo <a.zummo@towertech.it>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/mfd/max8998.c | 24 | ||||
-rw-r--r-- | drivers/rtc/Kconfig | 10 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
-rw-r--r-- | drivers/rtc/rtc-max8998.c | 300 |
4 files changed, 334 insertions, 1 deletions
diff --git a/drivers/mfd/max8998.c b/drivers/mfd/max8998.c index 49c140a78ba9..310fd8054f35 100644 --- a/drivers/mfd/max8998.c +++ b/drivers/mfd/max8998.c | |||
@@ -30,10 +30,14 @@ | |||
30 | #include <linux/mfd/max8998.h> | 30 | #include <linux/mfd/max8998.h> |
31 | #include <linux/mfd/max8998-private.h> | 31 | #include <linux/mfd/max8998-private.h> |
32 | 32 | ||
33 | #define RTC_I2C_ADDR (0x0c >> 1) | ||
34 | |||
33 | static struct mfd_cell max8998_devs[] = { | 35 | static struct mfd_cell max8998_devs[] = { |
34 | { | 36 | { |
35 | .name = "max8998-pmic", | 37 | .name = "max8998-pmic", |
36 | } | 38 | }, { |
39 | .name = "max8998-rtc", | ||
40 | }, | ||
37 | }; | 41 | }; |
38 | 42 | ||
39 | int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) | 43 | int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) |
@@ -80,6 +84,21 @@ int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) | |||
80 | } | 84 | } |
81 | EXPORT_SYMBOL(max8998_write_reg); | 85 | EXPORT_SYMBOL(max8998_write_reg); |
82 | 86 | ||
87 | int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) | ||
88 | { | ||
89 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); | ||
90 | int ret; | ||
91 | |||
92 | mutex_lock(&max8998->iolock); | ||
93 | ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); | ||
94 | mutex_unlock(&max8998->iolock); | ||
95 | if (ret < 0) | ||
96 | return ret; | ||
97 | |||
98 | return 0; | ||
99 | } | ||
100 | EXPORT_SYMBOL(max8998_bulk_write); | ||
101 | |||
83 | int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) | 102 | int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) |
84 | { | 103 | { |
85 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); | 104 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
@@ -120,6 +139,9 @@ static int max8998_i2c_probe(struct i2c_client *i2c, | |||
120 | } | 139 | } |
121 | mutex_init(&max8998->iolock); | 140 | mutex_init(&max8998->iolock); |
122 | 141 | ||
142 | max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR); | ||
143 | i2c_set_clientdata(max8998->rtc, max8998); | ||
144 | |||
123 | max8998_irq_init(max8998); | 145 | max8998_irq_init(max8998); |
124 | 146 | ||
125 | ret = mfd_add_devices(max8998->dev, -1, | 147 | ret = mfd_add_devices(max8998->dev, -1, |
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 6a77437d4f5a..23579d6e4a6e 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
@@ -196,6 +196,16 @@ config RTC_DRV_MAX8925 | |||
196 | This driver can also be built as a module. If so, the module | 196 | This driver can also be built as a module. If so, the module |
197 | will be called rtc-max8925. | 197 | will be called rtc-max8925. |
198 | 198 | ||
199 | config RTC_DRV_MAX8998 | ||
200 | tristate "Maxim MAX8998" | ||
201 | depends on MFD_MAX8998 | ||
202 | help | ||
203 | If you say yes here you will get support for the | ||
204 | RTC of Maxim MAX8998 PMIC. | ||
205 | |||
206 | This driver can also be built as a module. If so, the module | ||
207 | will be called rtc-max8998. | ||
208 | |||
199 | config RTC_DRV_RS5C372 | 209 | config RTC_DRV_RS5C372 |
200 | tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A" | 210 | tristate "Ricoh R2025S/D, RS5C372A/B, RV5C386, RV5C387A" |
201 | help | 211 | help |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 7a7cb3228a1d..c3f3c2d9e1ed 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
@@ -60,6 +60,7 @@ obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o | |||
60 | obj-$(CONFIG_RTC_MXC) += rtc-mxc.o | 60 | obj-$(CONFIG_RTC_MXC) += rtc-mxc.o |
61 | obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o | 61 | obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o |
62 | obj-$(CONFIG_RTC_DRV_MAX8925) += rtc-max8925.o | 62 | obj-$(CONFIG_RTC_DRV_MAX8925) += rtc-max8925.o |
63 | obj-$(CONFIG_RTC_DRV_MAX8998) += rtc-max8998.o | ||
63 | obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o | 64 | obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o |
64 | obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o | 65 | obj-$(CONFIG_RTC_DRV_MC13783) += rtc-mc13783.o |
65 | obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o | 66 | obj-$(CONFIG_RTC_DRV_MSM6242) += rtc-msm6242.o |
diff --git a/drivers/rtc/rtc-max8998.c b/drivers/rtc/rtc-max8998.c new file mode 100644 index 000000000000..f22dee35f330 --- /dev/null +++ b/drivers/rtc/rtc-max8998.c | |||
@@ -0,0 +1,300 @@ | |||
1 | /* | ||
2 | * RTC driver for Maxim MAX8998 | ||
3 | * | ||
4 | * Copyright (C) 2010 Samsung Electronics Co.Ltd | ||
5 | * Author: Minkyu Kang <mk7.kang@samsung.com> | ||
6 | * Author: Joonyoung Shim <jy0922.shim@samsung.com> | ||
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 | |||
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 | |||
24 | #define MAX8998_RTC_SEC 0x00 | ||
25 | #define MAX8998_RTC_MIN 0x01 | ||
26 | #define MAX8998_RTC_HOUR 0x02 | ||
27 | #define MAX8998_RTC_WEEKDAY 0x03 | ||
28 | #define MAX8998_RTC_DATE 0x04 | ||
29 | #define MAX8998_RTC_MONTH 0x05 | ||
30 | #define MAX8998_RTC_YEAR1 0x06 | ||
31 | #define MAX8998_RTC_YEAR2 0x07 | ||
32 | #define MAX8998_ALARM0_SEC 0x08 | ||
33 | #define MAX8998_ALARM0_MIN 0x09 | ||
34 | #define MAX8998_ALARM0_HOUR 0x0a | ||
35 | #define MAX8998_ALARM0_WEEKDAY 0x0b | ||
36 | #define MAX8998_ALARM0_DATE 0x0c | ||
37 | #define MAX8998_ALARM0_MONTH 0x0d | ||
38 | #define MAX8998_ALARM0_YEAR1 0x0e | ||
39 | #define MAX8998_ALARM0_YEAR2 0x0f | ||
40 | #define MAX8998_ALARM1_SEC 0x10 | ||
41 | #define MAX8998_ALARM1_MIN 0x11 | ||
42 | #define MAX8998_ALARM1_HOUR 0x12 | ||
43 | #define MAX8998_ALARM1_WEEKDAY 0x13 | ||
44 | #define MAX8998_ALARM1_DATE 0x14 | ||
45 | #define MAX8998_ALARM1_MONTH 0x15 | ||
46 | #define MAX8998_ALARM1_YEAR1 0x16 | ||
47 | #define MAX8998_ALARM1_YEAR2 0x17 | ||
48 | #define MAX8998_ALARM0_CONF 0x18 | ||
49 | #define MAX8998_ALARM1_CONF 0x19 | ||
50 | #define MAX8998_RTC_STATUS 0x1a | ||
51 | #define MAX8998_WTSR_SMPL_CNTL 0x1b | ||
52 | #define MAX8998_TEST 0x1f | ||
53 | |||
54 | #define HOUR_12 (1 << 7) | ||
55 | #define HOUR_PM (1 << 5) | ||
56 | #define ALARM0_STATUS (1 << 1) | ||
57 | #define ALARM1_STATUS (1 << 2) | ||
58 | |||
59 | enum { | ||
60 | RTC_SEC = 0, | ||
61 | RTC_MIN, | ||
62 | RTC_HOUR, | ||
63 | RTC_WEEKDAY, | ||
64 | RTC_DATE, | ||
65 | RTC_MONTH, | ||
66 | RTC_YEAR1, | ||
67 | RTC_YEAR2, | ||
68 | }; | ||
69 | |||
70 | struct max8998_rtc_info { | ||
71 | struct device *dev; | ||
72 | struct max8998_dev *max8998; | ||
73 | struct i2c_client *rtc; | ||
74 | struct rtc_device *rtc_dev; | ||
75 | int irq; | ||
76 | }; | ||
77 | |||
78 | static void max8998_data_to_tm(u8 *data, struct rtc_time *tm) | ||
79 | { | ||
80 | tm->tm_sec = bcd2bin(data[RTC_SEC]); | ||
81 | tm->tm_min = bcd2bin(data[RTC_MIN]); | ||
82 | if (data[RTC_HOUR] & HOUR_12) { | ||
83 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f); | ||
84 | if (data[RTC_HOUR] & HOUR_PM) | ||
85 | tm->tm_hour += 12; | ||
86 | } else | ||
87 | tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f); | ||
88 | |||
89 | tm->tm_wday = data[RTC_WEEKDAY] & 0x07; | ||
90 | tm->tm_mday = bcd2bin(data[RTC_DATE]); | ||
91 | tm->tm_mon = bcd2bin(data[RTC_MONTH]); | ||
92 | tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100; | ||
93 | tm->tm_year -= 1900; | ||
94 | } | ||
95 | |||
96 | static void max8998_tm_to_data(struct rtc_time *tm, u8 *data) | ||
97 | { | ||
98 | data[RTC_SEC] = bin2bcd(tm->tm_sec); | ||
99 | data[RTC_MIN] = bin2bcd(tm->tm_min); | ||
100 | data[RTC_HOUR] = bin2bcd(tm->tm_hour); | ||
101 | data[RTC_WEEKDAY] = tm->tm_wday; | ||
102 | data[RTC_DATE] = bin2bcd(tm->tm_mday); | ||
103 | data[RTC_MONTH] = bin2bcd(tm->tm_mon); | ||
104 | data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100); | ||
105 | data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100); | ||
106 | } | ||
107 | |||
108 | static int max8998_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
109 | { | ||
110 | struct max8998_rtc_info *info = dev_get_drvdata(dev); | ||
111 | u8 data[8]; | ||
112 | int ret; | ||
113 | |||
114 | ret = max8998_bulk_read(info->rtc, MAX8998_RTC_SEC, 8, data); | ||
115 | if (ret < 0) | ||
116 | return ret; | ||
117 | |||
118 | max8998_data_to_tm(data, tm); | ||
119 | |||
120 | return rtc_valid_tm(tm); | ||
121 | } | ||
122 | |||
123 | static int max8998_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
124 | { | ||
125 | struct max8998_rtc_info *info = dev_get_drvdata(dev); | ||
126 | u8 data[8]; | ||
127 | |||
128 | max8998_tm_to_data(tm, data); | ||
129 | |||
130 | return max8998_bulk_write(info->rtc, MAX8998_RTC_SEC, 8, data); | ||
131 | } | ||
132 | |||
133 | static int max8998_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
134 | { | ||
135 | struct max8998_rtc_info *info = dev_get_drvdata(dev); | ||
136 | u8 data[8]; | ||
137 | u8 val; | ||
138 | int ret; | ||
139 | |||
140 | ret = max8998_bulk_read(info->rtc, MAX8998_ALARM0_SEC, 8, data); | ||
141 | if (ret < 0) | ||
142 | return ret; | ||
143 | |||
144 | max8998_data_to_tm(data, &alrm->time); | ||
145 | |||
146 | ret = max8998_read_reg(info->rtc, MAX8998_ALARM0_CONF, &val); | ||
147 | if (ret < 0) | ||
148 | return ret; | ||
149 | |||
150 | alrm->enabled = !!val; | ||
151 | |||
152 | ret = max8998_read_reg(info->rtc, MAX8998_RTC_STATUS, &val); | ||
153 | if (ret < 0) | ||
154 | return ret; | ||
155 | |||
156 | if (val & ALARM0_STATUS) | ||
157 | alrm->pending = 1; | ||
158 | else | ||
159 | alrm->pending = 0; | ||
160 | |||
161 | return 0; | ||
162 | } | ||
163 | |||
164 | static int max8998_rtc_stop_alarm(struct max8998_rtc_info *info) | ||
165 | { | ||
166 | return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0); | ||
167 | } | ||
168 | |||
169 | static int max8998_rtc_start_alarm(struct max8998_rtc_info *info) | ||
170 | { | ||
171 | return max8998_write_reg(info->rtc, MAX8998_ALARM0_CONF, 0x77); | ||
172 | } | ||
173 | |||
174 | static int max8998_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
175 | { | ||
176 | struct max8998_rtc_info *info = dev_get_drvdata(dev); | ||
177 | u8 data[8]; | ||
178 | int ret; | ||
179 | |||
180 | max8998_tm_to_data(&alrm->time, data); | ||
181 | |||
182 | ret = max8998_rtc_stop_alarm(info); | ||
183 | if (ret < 0) | ||
184 | return ret; | ||
185 | |||
186 | ret = max8998_bulk_write(info->rtc, MAX8998_ALARM0_SEC, 8, data); | ||
187 | if (ret < 0) | ||
188 | return ret; | ||
189 | |||
190 | if (alrm->enabled) | ||
191 | return max8998_rtc_start_alarm(info); | ||
192 | |||
193 | return 0; | ||
194 | } | ||
195 | |||
196 | static int max8998_rtc_alarm_irq_enable(struct device *dev, | ||
197 | unsigned int enabled) | ||
198 | { | ||
199 | struct max8998_rtc_info *info = dev_get_drvdata(dev); | ||
200 | |||
201 | if (enabled) | ||
202 | return max8998_rtc_start_alarm(info); | ||
203 | else | ||
204 | return max8998_rtc_stop_alarm(info); | ||
205 | } | ||
206 | |||
207 | static irqreturn_t max8998_rtc_alarm_irq(int irq, void *data) | ||
208 | { | ||
209 | struct max8998_rtc_info *info = data; | ||
210 | |||
211 | rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF); | ||
212 | |||
213 | return IRQ_HANDLED; | ||
214 | } | ||
215 | |||
216 | static const struct rtc_class_ops max8998_rtc_ops = { | ||
217 | .read_time = max8998_rtc_read_time, | ||
218 | .set_time = max8998_rtc_set_time, | ||
219 | .read_alarm = max8998_rtc_read_alarm, | ||
220 | .set_alarm = max8998_rtc_set_alarm, | ||
221 | .alarm_irq_enable = max8998_rtc_alarm_irq_enable, | ||
222 | }; | ||
223 | |||
224 | static int __devinit max8998_rtc_probe(struct platform_device *pdev) | ||
225 | { | ||
226 | struct max8998_dev *max8998 = dev_get_drvdata(pdev->dev.parent); | ||
227 | struct max8998_rtc_info *info; | ||
228 | int ret; | ||
229 | |||
230 | info = kzalloc(sizeof(struct max8998_rtc_info), GFP_KERNEL); | ||
231 | if (!info) | ||
232 | return -ENOMEM; | ||
233 | |||
234 | info->dev = &pdev->dev; | ||
235 | info->max8998 = max8998; | ||
236 | info->rtc = max8998->rtc; | ||
237 | info->irq = max8998->irq_base + MAX8998_IRQ_ALARM0; | ||
238 | |||
239 | info->rtc_dev = rtc_device_register("max8998-rtc", &pdev->dev, | ||
240 | &max8998_rtc_ops, THIS_MODULE); | ||
241 | |||
242 | if (IS_ERR(info->rtc_dev)) { | ||
243 | ret = PTR_ERR(info->rtc_dev); | ||
244 | dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); | ||
245 | goto out_rtc; | ||
246 | } | ||
247 | |||
248 | platform_set_drvdata(pdev, info); | ||
249 | |||
250 | ret = request_threaded_irq(info->irq, NULL, max8998_rtc_alarm_irq, 0, | ||
251 | "rtc-alarm0", info); | ||
252 | if (ret < 0) | ||
253 | dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n", | ||
254 | info->irq, ret); | ||
255 | |||
256 | return 0; | ||
257 | |||
258 | out_rtc: | ||
259 | kfree(info); | ||
260 | return ret; | ||
261 | } | ||
262 | |||
263 | static int __devexit max8998_rtc_remove(struct platform_device *pdev) | ||
264 | { | ||
265 | struct max8998_rtc_info *info = platform_get_drvdata(pdev); | ||
266 | |||
267 | if (info) { | ||
268 | free_irq(info->irq, info); | ||
269 | rtc_device_unregister(info->rtc_dev); | ||
270 | kfree(info); | ||
271 | } | ||
272 | |||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | static struct platform_driver max8998_rtc_driver = { | ||
277 | .driver = { | ||
278 | .name = "max8998-rtc", | ||
279 | .owner = THIS_MODULE, | ||
280 | }, | ||
281 | .probe = max8998_rtc_probe, | ||
282 | .remove = __devexit_p(max8998_rtc_remove), | ||
283 | }; | ||
284 | |||
285 | static int __init max8998_rtc_init(void) | ||
286 | { | ||
287 | return platform_driver_register(&max8998_rtc_driver); | ||
288 | } | ||
289 | module_init(max8998_rtc_init); | ||
290 | |||
291 | static void __exit max8998_rtc_exit(void) | ||
292 | { | ||
293 | platform_driver_unregister(&max8998_rtc_driver); | ||
294 | } | ||
295 | module_exit(max8998_rtc_exit); | ||
296 | |||
297 | MODULE_AUTHOR("Minkyu Kang <mk7.kang@samsung.com>"); | ||
298 | MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); | ||
299 | MODULE_DESCRIPTION("Maxim MAX8998 RTC driver"); | ||
300 | MODULE_LICENSE("GPL"); | ||