diff options
author | Chen-Yu Tsai <wens@csie.org> | 2016-07-08 10:33:38 -0400 |
---|---|---|
committer | Lee Jones <lee.jones@linaro.org> | 2016-08-08 07:53:36 -0400 |
commit | d00a18a42c1483924de086ddfb0efe8da1dba3ac (patch) | |
tree | a8fc90f1c3d77893a13849b964307e3f1f7ce1e6 | |
parent | 585083c539ca3f5fb3d00057b25f9be3304d54c6 (diff) |
rtc: ac100: Add RTC driver for X-Powers AC100
X-Powers AC100 is a codec / RTC combo chip. This driver supports
the RTC sub-device.
The RTC block also has clock outputs and non-volatile storage.
Non-volatile storage wthin the RTC hardware is not supported.
Clock output support is added in the next patch.
Signed-off-by: Chen-Yu Tsai <wens@csie.org>
Acked-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
-rw-r--r-- | drivers/rtc/Kconfig | 10 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
-rw-r--r-- | drivers/rtc/rtc-ac100.c | 325 |
3 files changed, 336 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index e215f50794b6..7fc11cdfd27e 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig | |||
@@ -187,6 +187,16 @@ config RTC_DRV_ABX80X | |||
187 | This driver can also be built as a module. If so, the module | 187 | This driver can also be built as a module. If so, the module |
188 | will be called rtc-abx80x. | 188 | will be called rtc-abx80x. |
189 | 189 | ||
190 | config RTC_DRV_AC100 | ||
191 | tristate "X-Powers AC100" | ||
192 | depends on MFD_AC100 | ||
193 | help | ||
194 | If you say yes here you get support for the real-time clock found | ||
195 | in X-Powers AC100 family peripheral ICs. | ||
196 | |||
197 | This driver can also be built as a module. If so, the module | ||
198 | will be called rtc-ac100. | ||
199 | |||
190 | config RTC_DRV_AS3722 | 200 | config RTC_DRV_AS3722 |
191 | tristate "ams AS3722 RTC driver" | 201 | tristate "ams AS3722 RTC driver" |
192 | depends on MFD_AS3722 | 202 | depends on MFD_AS3722 |
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 7cf7ad559c79..8fb994bacdf7 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile | |||
@@ -27,6 +27,7 @@ obj-$(CONFIG_RTC_DRV_AB3100) += rtc-ab3100.o | |||
27 | obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o | 27 | obj-$(CONFIG_RTC_DRV_AB8500) += rtc-ab8500.o |
28 | obj-$(CONFIG_RTC_DRV_ABB5ZES3) += rtc-ab-b5ze-s3.o | 28 | obj-$(CONFIG_RTC_DRV_ABB5ZES3) += rtc-ab-b5ze-s3.o |
29 | obj-$(CONFIG_RTC_DRV_ABX80X) += rtc-abx80x.o | 29 | obj-$(CONFIG_RTC_DRV_ABX80X) += rtc-abx80x.o |
30 | obj-$(CONFIG_RTC_DRV_AC100) += rtc-ac100.o | ||
30 | obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o | 31 | obj-$(CONFIG_RTC_DRV_ARMADA38X) += rtc-armada38x.o |
31 | obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o | 32 | obj-$(CONFIG_RTC_DRV_AS3722) += rtc-as3722.o |
32 | obj-$(CONFIG_RTC_DRV_ASM9260) += rtc-asm9260.o | 33 | obj-$(CONFIG_RTC_DRV_ASM9260) += rtc-asm9260.o |
diff --git a/drivers/rtc/rtc-ac100.c b/drivers/rtc/rtc-ac100.c new file mode 100644 index 000000000000..5a9ca89d04c7 --- /dev/null +++ b/drivers/rtc/rtc-ac100.c | |||
@@ -0,0 +1,325 @@ | |||
1 | /* | ||
2 | * RTC Driver for X-Powers AC100 | ||
3 | * | ||
4 | * Copyright (c) 2016 Chen-Yu Tsai | ||
5 | * | ||
6 | * Chen-Yu Tsai <wens@csie.org> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License version 2 as | ||
10 | * published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, but WITHOUT | ||
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
15 | * more details. | ||
16 | */ | ||
17 | |||
18 | #include <linux/bcd.h> | ||
19 | #include <linux/device.h> | ||
20 | #include <linux/interrupt.h> | ||
21 | #include <linux/kernel.h> | ||
22 | #include <linux/mfd/ac100.h> | ||
23 | #include <linux/module.h> | ||
24 | #include <linux/mutex.h> | ||
25 | #include <linux/of.h> | ||
26 | #include <linux/platform_device.h> | ||
27 | #include <linux/regmap.h> | ||
28 | #include <linux/rtc.h> | ||
29 | #include <linux/types.h> | ||
30 | |||
31 | /* Control register */ | ||
32 | #define AC100_RTC_CTRL_24HOUR BIT(0) | ||
33 | |||
34 | /* RTC */ | ||
35 | #define AC100_RTC_SEC_MASK GENMASK(6, 0) | ||
36 | #define AC100_RTC_MIN_MASK GENMASK(6, 0) | ||
37 | #define AC100_RTC_HOU_MASK GENMASK(5, 0) | ||
38 | #define AC100_RTC_WEE_MASK GENMASK(2, 0) | ||
39 | #define AC100_RTC_DAY_MASK GENMASK(5, 0) | ||
40 | #define AC100_RTC_MON_MASK GENMASK(4, 0) | ||
41 | #define AC100_RTC_YEA_MASK GENMASK(7, 0) | ||
42 | #define AC100_RTC_YEA_LEAP BIT(15) | ||
43 | #define AC100_RTC_UPD_TRIGGER BIT(15) | ||
44 | |||
45 | /* Alarm (wall clock) */ | ||
46 | #define AC100_ALM_INT_ENABLE BIT(0) | ||
47 | |||
48 | #define AC100_ALM_SEC_MASK GENMASK(6, 0) | ||
49 | #define AC100_ALM_MIN_MASK GENMASK(6, 0) | ||
50 | #define AC100_ALM_HOU_MASK GENMASK(5, 0) | ||
51 | #define AC100_ALM_WEE_MASK GENMASK(2, 0) | ||
52 | #define AC100_ALM_DAY_MASK GENMASK(5, 0) | ||
53 | #define AC100_ALM_MON_MASK GENMASK(4, 0) | ||
54 | #define AC100_ALM_YEA_MASK GENMASK(7, 0) | ||
55 | #define AC100_ALM_ENABLE_FLAG BIT(15) | ||
56 | #define AC100_ALM_UPD_TRIGGER BIT(15) | ||
57 | |||
58 | /* | ||
59 | * The year parameter passed to the driver is usually an offset relative to | ||
60 | * the year 1900. This macro is used to convert this offset to another one | ||
61 | * relative to the minimum year allowed by the hardware. | ||
62 | * | ||
63 | * The year range is 1970 - 2069. This range is selected to match Allwinner's | ||
64 | * driver. | ||
65 | */ | ||
66 | #define AC100_YEAR_MIN 1970 | ||
67 | #define AC100_YEAR_MAX 2069 | ||
68 | #define AC100_YEAR_OFF (AC100_YEAR_MIN - 1900) | ||
69 | |||
70 | struct ac100_rtc_dev { | ||
71 | struct rtc_device *rtc; | ||
72 | struct device *dev; | ||
73 | struct regmap *regmap; | ||
74 | int irq; | ||
75 | unsigned long alarm; | ||
76 | }; | ||
77 | |||
78 | static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc_tm) | ||
79 | { | ||
80 | struct ac100_rtc_dev *chip = dev_get_drvdata(dev); | ||
81 | struct regmap *regmap = chip->regmap; | ||
82 | u16 reg[7]; | ||
83 | int ret; | ||
84 | |||
85 | ret = regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7); | ||
86 | if (ret) | ||
87 | return ret; | ||
88 | |||
89 | rtc_tm->tm_sec = bcd2bin(reg[0] & AC100_RTC_SEC_MASK); | ||
90 | rtc_tm->tm_min = bcd2bin(reg[1] & AC100_RTC_MIN_MASK); | ||
91 | rtc_tm->tm_hour = bcd2bin(reg[2] & AC100_RTC_HOU_MASK); | ||
92 | rtc_tm->tm_wday = bcd2bin(reg[3] & AC100_RTC_WEE_MASK); | ||
93 | rtc_tm->tm_mday = bcd2bin(reg[4] & AC100_RTC_DAY_MASK); | ||
94 | rtc_tm->tm_mon = bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1; | ||
95 | rtc_tm->tm_year = bcd2bin(reg[6] & AC100_RTC_YEA_MASK) + | ||
96 | AC100_YEAR_OFF; | ||
97 | |||
98 | return rtc_valid_tm(rtc_tm); | ||
99 | } | ||
100 | |||
101 | static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm) | ||
102 | { | ||
103 | struct ac100_rtc_dev *chip = dev_get_drvdata(dev); | ||
104 | struct regmap *regmap = chip->regmap; | ||
105 | int year; | ||
106 | u16 reg[8]; | ||
107 | |||
108 | /* our RTC has a limited year range... */ | ||
109 | year = rtc_tm->tm_year - AC100_YEAR_OFF; | ||
110 | if (year < 0 || year > (AC100_YEAR_MAX - 1900)) { | ||
111 | dev_err(dev, "rtc only supports year in range %d - %d\n", | ||
112 | AC100_YEAR_MIN, AC100_YEAR_MAX); | ||
113 | return -EINVAL; | ||
114 | } | ||
115 | |||
116 | /* convert to BCD */ | ||
117 | reg[0] = bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK; | ||
118 | reg[1] = bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK; | ||
119 | reg[2] = bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK; | ||
120 | reg[3] = bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK; | ||
121 | reg[4] = bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK; | ||
122 | reg[5] = bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK; | ||
123 | reg[6] = bin2bcd(year) & AC100_RTC_YEA_MASK; | ||
124 | /* trigger write */ | ||
125 | reg[7] = AC100_RTC_UPD_TRIGGER; | ||
126 | |||
127 | /* Is it a leap year? */ | ||
128 | if (is_leap_year(year + AC100_YEAR_OFF + 1900)) | ||
129 | reg[6] |= AC100_RTC_YEA_LEAP; | ||
130 | |||
131 | return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8); | ||
132 | } | ||
133 | |||
134 | static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int en) | ||
135 | { | ||
136 | struct ac100_rtc_dev *chip = dev_get_drvdata(dev); | ||
137 | struct regmap *regmap = chip->regmap; | ||
138 | unsigned int val; | ||
139 | |||
140 | val = en ? AC100_ALM_INT_ENABLE : 0; | ||
141 | |||
142 | return regmap_write(regmap, AC100_ALM_INT_ENA, val); | ||
143 | } | ||
144 | |||
145 | static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
146 | { | ||
147 | struct ac100_rtc_dev *chip = dev_get_drvdata(dev); | ||
148 | struct regmap *regmap = chip->regmap; | ||
149 | struct rtc_time *alrm_tm = &alrm->time; | ||
150 | u16 reg[7]; | ||
151 | unsigned int val; | ||
152 | int ret; | ||
153 | |||
154 | ret = regmap_read(regmap, AC100_ALM_INT_ENA, &val); | ||
155 | if (ret) | ||
156 | return ret; | ||
157 | |||
158 | alrm->enabled = !!(val & AC100_ALM_INT_ENABLE); | ||
159 | |||
160 | ret = regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7); | ||
161 | if (ret) | ||
162 | return ret; | ||
163 | |||
164 | alrm_tm->tm_sec = bcd2bin(reg[0] & AC100_ALM_SEC_MASK); | ||
165 | alrm_tm->tm_min = bcd2bin(reg[1] & AC100_ALM_MIN_MASK); | ||
166 | alrm_tm->tm_hour = bcd2bin(reg[2] & AC100_ALM_HOU_MASK); | ||
167 | alrm_tm->tm_wday = bcd2bin(reg[3] & AC100_ALM_WEE_MASK); | ||
168 | alrm_tm->tm_mday = bcd2bin(reg[4] & AC100_ALM_DAY_MASK); | ||
169 | alrm_tm->tm_mon = bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1; | ||
170 | alrm_tm->tm_year = bcd2bin(reg[6] & AC100_ALM_YEA_MASK) + | ||
171 | AC100_YEAR_OFF; | ||
172 | |||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) | ||
177 | { | ||
178 | struct ac100_rtc_dev *chip = dev_get_drvdata(dev); | ||
179 | struct regmap *regmap = chip->regmap; | ||
180 | struct rtc_time *alrm_tm = &alrm->time; | ||
181 | u16 reg[8]; | ||
182 | int year; | ||
183 | int ret; | ||
184 | |||
185 | /* our alarm has a limited year range... */ | ||
186 | year = alrm_tm->tm_year - AC100_YEAR_OFF; | ||
187 | if (year < 0 || year > (AC100_YEAR_MAX - 1900)) { | ||
188 | dev_err(dev, "alarm only supports year in range %d - %d\n", | ||
189 | AC100_YEAR_MIN, AC100_YEAR_MAX); | ||
190 | return -EINVAL; | ||
191 | } | ||
192 | |||
193 | /* convert to BCD */ | ||
194 | reg[0] = (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) | | ||
195 | AC100_ALM_ENABLE_FLAG; | ||
196 | reg[1] = (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) | | ||
197 | AC100_ALM_ENABLE_FLAG; | ||
198 | reg[2] = (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) | | ||
199 | AC100_ALM_ENABLE_FLAG; | ||
200 | /* Do not enable weekday alarm */ | ||
201 | reg[3] = bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK; | ||
202 | reg[4] = (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) | | ||
203 | AC100_ALM_ENABLE_FLAG; | ||
204 | reg[5] = (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) | | ||
205 | AC100_ALM_ENABLE_FLAG; | ||
206 | reg[6] = (bin2bcd(year) & AC100_ALM_YEA_MASK) | | ||
207 | AC100_ALM_ENABLE_FLAG; | ||
208 | /* trigger write */ | ||
209 | reg[7] = AC100_ALM_UPD_TRIGGER; | ||
210 | |||
211 | ret = regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8); | ||
212 | if (ret) | ||
213 | return ret; | ||
214 | |||
215 | return ac100_rtc_alarm_irq_enable(dev, alrm->enabled); | ||
216 | } | ||
217 | |||
218 | static irqreturn_t ac100_rtc_irq(int irq, void *data) | ||
219 | { | ||
220 | struct ac100_rtc_dev *chip = data; | ||
221 | struct regmap *regmap = chip->regmap; | ||
222 | unsigned int val = 0; | ||
223 | int ret; | ||
224 | |||
225 | mutex_lock(&chip->rtc->ops_lock); | ||
226 | |||
227 | /* read status */ | ||
228 | ret = regmap_read(regmap, AC100_ALM_INT_STA, &val); | ||
229 | if (ret) | ||
230 | goto out; | ||
231 | |||
232 | if (val & AC100_ALM_INT_ENABLE) { | ||
233 | /* signal rtc framework */ | ||
234 | rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF); | ||
235 | |||
236 | /* clear status */ | ||
237 | ret = regmap_write(regmap, AC100_ALM_INT_STA, val); | ||
238 | if (ret) | ||
239 | goto out; | ||
240 | |||
241 | /* disable interrupt */ | ||
242 | ret = ac100_rtc_alarm_irq_enable(chip->dev, 0); | ||
243 | if (ret) | ||
244 | goto out; | ||
245 | } | ||
246 | |||
247 | out: | ||
248 | mutex_unlock(&chip->rtc->ops_lock); | ||
249 | return IRQ_HANDLED; | ||
250 | } | ||
251 | |||
252 | static const struct rtc_class_ops ac100_rtc_ops = { | ||
253 | .read_time = ac100_rtc_get_time, | ||
254 | .set_time = ac100_rtc_set_time, | ||
255 | .read_alarm = ac100_rtc_get_alarm, | ||
256 | .set_alarm = ac100_rtc_set_alarm, | ||
257 | .alarm_irq_enable = ac100_rtc_alarm_irq_enable, | ||
258 | }; | ||
259 | |||
260 | static int ac100_rtc_probe(struct platform_device *pdev) | ||
261 | { | ||
262 | struct ac100_dev *ac100 = dev_get_drvdata(pdev->dev.parent); | ||
263 | struct ac100_rtc_dev *chip; | ||
264 | int ret; | ||
265 | |||
266 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); | ||
267 | platform_set_drvdata(pdev, chip); | ||
268 | chip->dev = &pdev->dev; | ||
269 | chip->regmap = ac100->regmap; | ||
270 | |||
271 | chip->irq = platform_get_irq(pdev, 0); | ||
272 | if (chip->irq < 0) { | ||
273 | dev_err(&pdev->dev, "No IRQ resource\n"); | ||
274 | return chip->irq; | ||
275 | } | ||
276 | |||
277 | ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL, | ||
278 | ac100_rtc_irq, | ||
279 | IRQF_SHARED | IRQF_ONESHOT, | ||
280 | dev_name(&pdev->dev), chip); | ||
281 | if (ret) { | ||
282 | dev_err(&pdev->dev, "Could not request IRQ\n"); | ||
283 | return ret; | ||
284 | } | ||
285 | |||
286 | /* always use 24 hour mode */ | ||
287 | regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR, | ||
288 | AC100_RTC_CTRL_24HOUR); | ||
289 | |||
290 | /* disable counter alarm interrupt */ | ||
291 | regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0); | ||
292 | |||
293 | /* clear counter alarm pending interrupts */ | ||
294 | regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE); | ||
295 | |||
296 | chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-ac100", | ||
297 | &ac100_rtc_ops, THIS_MODULE); | ||
298 | if (IS_ERR(chip->rtc)) { | ||
299 | dev_err(&pdev->dev, "unable to register device\n"); | ||
300 | return PTR_ERR(chip->rtc); | ||
301 | } | ||
302 | |||
303 | dev_info(&pdev->dev, "RTC enabled\n"); | ||
304 | |||
305 | return 0; | ||
306 | } | ||
307 | |||
308 | static const struct of_device_id ac100_rtc_match[] = { | ||
309 | { .compatible = "x-powers,ac100-rtc" }, | ||
310 | { }, | ||
311 | }; | ||
312 | MODULE_DEVICE_TABLE(of, ac100_rtc_match); | ||
313 | |||
314 | static struct platform_driver ac100_rtc_driver = { | ||
315 | .probe = ac100_rtc_probe, | ||
316 | .driver = { | ||
317 | .name = "ac100-rtc", | ||
318 | .of_match_table = of_match_ptr(ac100_rtc_match), | ||
319 | }, | ||
320 | }; | ||
321 | module_platform_driver(ac100_rtc_driver); | ||
322 | |||
323 | MODULE_DESCRIPTION("X-Powers AC100 RTC driver"); | ||
324 | MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>"); | ||
325 | MODULE_LICENSE("GPL v2"); | ||