diff options
author | Stephen Warren <swarren@nvidia.com> | 2012-10-04 20:13:56 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-05 14:05:02 -0400 |
commit | 94c01ab6d754435333a989e3c6f4a20f57537754 (patch) | |
tree | d1a29e90c08b9ca50c5f5c9cc9a3f30e713bae6a /drivers/rtc | |
parent | 0e783980b84e7253ba98552d092d1f45ed262d66 (diff) |
rtc: add MAX8907 RTC driver
The MAX8907 is an I2C-based power-management IC containing voltage
regulators, a reset controller, a real-time clock, and a touch-screen
controller.
The driver is based on an original by or fixed by:
* Tom Cherry
* Prashant Gaikwad
* Joseph Yoon
During upstreaming, I (swarren):
* Converted to regmap.
* Fixed handling of RTC_HOUR register containing 12.
* Fixed handling of RTC_WEEKDAY register.
* General cleanup.
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Cc: Tom Cherry <tcherry@nvidia.com>
Cc: Prashant Gaikwad <pgaikwad@nvidia.com>
Cc: Joseph Yoon <tyoon@nvidia.com>
Cc: Alessandro Zummo <a.zummo@towertech.it>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/rtc')
-rw-r--r-- | drivers/rtc/Kconfig | 10 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
-rw-r--r-- | drivers/rtc/rtc-max8907.c | 244 |
3 files changed, 255 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 6309df4e2654..499fb9984abe 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
@@ -205,6 +205,16 @@ config RTC_DRV_MAX6900 | |||
205 | This driver can also be built as a module. If so, the module | 205 | This driver can also be built as a module. If so, the module |
206 | will be called rtc-max6900. | 206 | will be called rtc-max6900. |
207 | 207 | ||
208 | config RTC_DRV_MAX8907 | ||
209 | tristate "Maxim MAX8907" | ||
210 | depends on MFD_MAX8907 | ||
211 | help | ||
212 | If you say yes here you will get support for the | ||
213 | RTC of Maxim MAX8907 PMIC. | ||
214 | |||
215 | This driver can also be built as a module. If so, the module | ||
216 | will be called rtc-max8907. | ||
217 | |||
208 | config RTC_DRV_MAX8925 | 218 | config RTC_DRV_MAX8925 |
209 | tristate "Maxim MAX8925" | 219 | tristate "Maxim MAX8925" |
210 | depends on MFD_MAX8925 | 220 | depends on MFD_MAX8925 |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 5c8f106237da..d9526b549f87 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
@@ -65,6 +65,7 @@ obj-$(CONFIG_RTC_DRV_M48T59) += rtc-m48t59.o | |||
65 | obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o | 65 | obj-$(CONFIG_RTC_DRV_M48T86) += rtc-m48t86.o |
66 | obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.o | 66 | obj-$(CONFIG_RTC_DRV_MXC) += rtc-mxc.o |
67 | obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o | 67 | obj-$(CONFIG_RTC_DRV_MAX6900) += rtc-max6900.o |
68 | obj-$(CONFIG_RTC_DRV_MAX8907) += rtc-max8907.o | ||
68 | obj-$(CONFIG_RTC_DRV_MAX8925) += rtc-max8925.o | 69 | obj-$(CONFIG_RTC_DRV_MAX8925) += rtc-max8925.o |
69 | obj-$(CONFIG_RTC_DRV_MAX8998) += rtc-max8998.o | 70 | obj-$(CONFIG_RTC_DRV_MAX8998) += rtc-max8998.o |
70 | obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o | 71 | obj-$(CONFIG_RTC_DRV_MAX6902) += rtc-max6902.o |
diff --git a/drivers/rtc/rtc-max8907.c b/drivers/rtc/rtc-max8907.c new file mode 100644 index 000000000000..e094ffa434f8 --- /dev/null +++ b/drivers/rtc/rtc-max8907.c | |||
@@ -0,0 +1,244 @@ | |||
1 | /* | ||
2 | * RTC driver for Maxim MAX8907 | ||
3 | * | ||
4 | * Copyright (c) 2011-2012, NVIDIA Corporation. | ||
5 | * | ||
6 | * Based on drivers/rtc/rtc-max8925.c, | ||
7 | * Copyright (C) 2009-2010 Marvell International Ltd. | ||
8 | * | ||
9 | * This program is free software; you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License version 2 as | ||
11 | * published by the Free Software Foundation. | ||
12 | */ | ||
13 | |||
14 | #include <linux/bcd.h> | ||
15 | #include <linux/i2c.h> | ||
16 | #include <linux/mfd/max8907.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/platform_device.h> | ||
19 | #include <linux/regmap.h> | ||
20 | #include <linux/rtc.h> | ||
21 | #include <linux/slab.h> | ||
22 | |||
23 | enum { | ||
24 | RTC_SEC = 0, | ||
25 | RTC_MIN, | ||
26 | RTC_HOUR, | ||
27 | RTC_WEEKDAY, | ||
28 | RTC_DATE, | ||
29 | RTC_MONTH, | ||
30 | RTC_YEAR1, | ||
31 | RTC_YEAR2, | ||
32 | }; | ||
33 | |||
34 | #define TIME_NUM 8 | ||
35 | #define ALARM_1SEC (1 << 7) | ||
36 | #define HOUR_12 (1 << 7) | ||
37 | #define HOUR_AM_PM (1 << 5) | ||
38 | #define ALARM0_IRQ (1 << 3) | ||
39 | #define ALARM1_IRQ (1 << 2) | ||
40 | #define ALARM0_STATUS (1 << 2) | ||
41 | #define ALARM1_STATUS (1 << 1) | ||
42 | |||
43 | struct max8907_rtc { | ||
44 | struct max8907 *max8907; | ||
45 | struct regmap *regmap; | ||
46 | struct rtc_device *rtc_dev; | ||
47 | int irq; | ||
48 | }; | ||
49 | |||
50 | static irqreturn_t max8907_irq_handler(int irq, void *data) | ||
51 | { | ||
52 | struct max8907_rtc *rtc = data; | ||
53 | |||
54 | regmap_update_bits(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0x7f, 0); | ||
55 | |||
56 | rtc_update_irq(rtc->rtc_dev, 1, RTC_IRQF | RTC_AF); | ||
57 | |||
58 | return IRQ_HANDLED; | ||
59 | } | ||
60 | |||
61 | static void regs_to_tm(u8 *regs, struct rtc_time *tm) | ||
62 | { | ||
63 | tm->tm_year = bcd2bin(regs[RTC_YEAR2]) * 100 + | ||
64 | bcd2bin(regs[RTC_YEAR1]) - 1900; | ||
65 | tm->tm_mon = bcd2bin(regs[RTC_MONTH] & 0x1f) - 1; | ||
66 | tm->tm_mday = bcd2bin(regs[RTC_DATE] & 0x3f); | ||
67 | tm->tm_wday = (regs[RTC_WEEKDAY] & 0x07) - 1; | ||
68 | if (regs[RTC_HOUR] & HOUR_12) { | ||
69 | tm->tm_hour = bcd2bin(regs[RTC_HOUR] & 0x01f); | ||
70 | if (tm->tm_hour == 12) | ||
71 | tm->tm_hour = 0; | ||
72 | if (regs[RTC_HOUR] & HOUR_AM_PM) | ||
73 | tm->tm_hour += 12; | ||
74 | } else { | ||
75 | tm->tm_hour = bcd2bin(regs[RTC_HOUR] & 0x03f); | ||
76 | } | ||
77 | tm->tm_min = bcd2bin(regs[RTC_MIN] & 0x7f); | ||
78 | tm->tm_sec = bcd2bin(regs[RTC_SEC] & 0x7f); | ||
79 | } | ||
80 | |||
81 | static void tm_to_regs(struct rtc_time *tm, u8 *regs) | ||
82 | { | ||
83 | u8 high, low; | ||
84 | |||
85 | high = (tm->tm_year + 1900) / 100; | ||
86 | low = tm->tm_year % 100; | ||
87 | regs[RTC_YEAR2] = bin2bcd(high); | ||
88 | regs[RTC_YEAR1] = bin2bcd(low); | ||
89 | regs[RTC_MONTH] = bin2bcd(tm->tm_mon + 1); | ||
90 | regs[RTC_DATE] = bin2bcd(tm->tm_mday); | ||
91 | regs[RTC_WEEKDAY] = tm->tm_wday + 1; | ||
92 | regs[RTC_HOUR] = bin2bcd(tm->tm_hour); | ||
93 | regs[RTC_MIN] = bin2bcd(tm->tm_min); | ||
94 | regs[RTC_SEC] = bin2bcd(tm->tm_sec); | ||
95 | } | ||
96 | |||
97 | static int max8907_rtc_read_time(struct device *dev, struct rtc_time *tm) | ||
98 | { | ||
99 | struct max8907_rtc *rtc = dev_get_drvdata(dev); | ||
100 | u8 regs[TIME_NUM]; | ||
101 | int ret; | ||
102 | |||
103 | ret = regmap_bulk_read(rtc->regmap, MAX8907_REG_RTC_SEC, regs, | ||
104 | TIME_NUM); | ||
105 | if (ret < 0) | ||
106 | return ret; | ||
107 | |||
108 | regs_to_tm(regs, tm); | ||
109 | |||
110 | return 0; | ||
111 | } | ||
112 | |||
113 | static int max8907_rtc_set_time(struct device *dev, struct rtc_time *tm) | ||
114 | { | ||
115 | struct max8907_rtc *rtc = dev_get_drvdata(dev); | ||
116 | u8 regs[TIME_NUM]; | ||
117 | |||
118 | tm_to_regs(tm, regs); | ||
119 | |||
120 | return regmap_bulk_write(rtc->regmap, MAX8907_REG_RTC_SEC, regs, | ||
121 | TIME_NUM); | ||
122 | } | ||
123 | |||
124 | static int max8907_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
125 | { | ||
126 | struct max8907_rtc *rtc = dev_get_drvdata(dev); | ||
127 | u8 regs[TIME_NUM]; | ||
128 | unsigned int val; | ||
129 | int ret; | ||
130 | |||
131 | ret = regmap_bulk_read(rtc->regmap, MAX8907_REG_ALARM0_SEC, regs, | ||
132 | TIME_NUM); | ||
133 | if (ret < 0) | ||
134 | return ret; | ||
135 | |||
136 | regs_to_tm(regs, &alrm->time); | ||
137 | |||
138 | ret = regmap_read(rtc->regmap, MAX8907_REG_ALARM0_CNTL, &val); | ||
139 | if (ret < 0) | ||
140 | return ret; | ||
141 | |||
142 | alrm->enabled = !!(val & 0x7f); | ||
143 | |||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | static int max8907_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
148 | { | ||
149 | struct max8907_rtc *rtc = dev_get_drvdata(dev); | ||
150 | u8 regs[TIME_NUM]; | ||
151 | int ret; | ||
152 | |||
153 | tm_to_regs(&alrm->time, regs); | ||
154 | |||
155 | /* Disable alarm while we update the target time */ | ||
156 | ret = regmap_update_bits(rtc->regmap, MAX8907_REG_ALARM0_CNTL, 0x7f, 0); | ||
157 | if (ret < 0) | ||
158 | return ret; | ||
159 | |||
160 | ret = regmap_bulk_write(rtc->regmap, MAX8907_REG_ALARM0_SEC, regs, | ||
161 | TIME_NUM); | ||
162 | if (ret < 0) | ||
163 | return ret; | ||
164 | |||
165 | if (alrm->enabled) | ||
166 | ret = regmap_update_bits(rtc->regmap, MAX8907_REG_ALARM0_CNTL, | ||
167 | 0x7f, 0x7f); | ||
168 | |||
169 | return ret; | ||
170 | } | ||
171 | |||
172 | static const struct rtc_class_ops max8907_rtc_ops = { | ||
173 | .read_time = max8907_rtc_read_time, | ||
174 | .set_time = max8907_rtc_set_time, | ||
175 | .read_alarm = max8907_rtc_read_alarm, | ||
176 | .set_alarm = max8907_rtc_set_alarm, | ||
177 | }; | ||
178 | |||
179 | static int __devinit max8907_rtc_probe(struct platform_device *pdev) | ||
180 | { | ||
181 | struct max8907 *max8907 = dev_get_drvdata(pdev->dev.parent); | ||
182 | struct max8907_rtc *rtc; | ||
183 | int ret; | ||
184 | |||
185 | rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); | ||
186 | if (!rtc) | ||
187 | return -ENOMEM; | ||
188 | platform_set_drvdata(pdev, rtc); | ||
189 | |||
190 | rtc->max8907 = max8907; | ||
191 | rtc->regmap = max8907->regmap_rtc; | ||
192 | |||
193 | rtc->rtc_dev = rtc_device_register("max8907-rtc", &pdev->dev, | ||
194 | &max8907_rtc_ops, THIS_MODULE); | ||
195 | if (IS_ERR(rtc->rtc_dev)) { | ||
196 | ret = PTR_ERR(rtc->rtc_dev); | ||
197 | dev_err(&pdev->dev, "Failed to register RTC device: %d\n", ret); | ||
198 | return ret; | ||
199 | } | ||
200 | |||
201 | rtc->irq = regmap_irq_get_virq(max8907->irqc_rtc, | ||
202 | MAX8907_IRQ_RTC_ALARM0); | ||
203 | if (rtc->irq < 0) { | ||
204 | ret = rtc->irq; | ||
205 | goto err_unregister; | ||
206 | } | ||
207 | |||
208 | ret = request_threaded_irq(rtc->irq, NULL, max8907_irq_handler, | ||
209 | IRQF_ONESHOT, "max8907-alarm0", rtc); | ||
210 | if (ret < 0) { | ||
211 | dev_err(&pdev->dev, "Failed to request IRQ%d: %d\n", | ||
212 | rtc->irq, ret); | ||
213 | goto err_unregister; | ||
214 | } | ||
215 | |||
216 | return 0; | ||
217 | |||
218 | err_unregister: | ||
219 | rtc_device_unregister(rtc->rtc_dev); | ||
220 | return ret; | ||
221 | } | ||
222 | |||
223 | static int __devexit max8907_rtc_remove(struct platform_device *pdev) | ||
224 | { | ||
225 | struct max8907_rtc *rtc = platform_get_drvdata(pdev); | ||
226 | |||
227 | free_irq(rtc->irq, rtc); | ||
228 | rtc_device_unregister(rtc->rtc_dev); | ||
229 | |||
230 | return 0; | ||
231 | } | ||
232 | |||
233 | static struct platform_driver max8907_rtc_driver = { | ||
234 | .driver = { | ||
235 | .name = "max8907-rtc", | ||
236 | .owner = THIS_MODULE, | ||
237 | }, | ||
238 | .probe = max8907_rtc_probe, | ||
239 | .remove = __devexit_p(max8907_rtc_remove), | ||
240 | }; | ||
241 | module_platform_driver(max8907_rtc_driver); | ||
242 | |||
243 | MODULE_DESCRIPTION("Maxim MAX8907 RTC driver"); | ||
244 | MODULE_LICENSE("GPL v2"); | ||