blob: cdc9dc211eb0f990d4fa0048c6750c20d983c832 [file] [log] [blame]
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/rtc.h>
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070016#include <linux/platform_device.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070017#include <linux/pm.h>
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070018#include <linux/regmap.h>
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070019#include <linux/slab.h>
20#include <linux/spinlock.h>
21
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070022#include <linux/mfd/pm8xxx/rtc.h>
23
24
25/* RTC Register offsets from RTC CTRL REG */
26#define PM8XXX_ALARM_CTRL_OFFSET 0x01
27#define PM8XXX_RTC_WRITE_OFFSET 0x02
28#define PM8XXX_RTC_READ_OFFSET 0x06
29#define PM8XXX_ALARM_RW_OFFSET 0x0A
30
31/* RTC_CTRL register bit fields */
32#define PM8xxx_RTC_ENABLE BIT(7)
33#define PM8xxx_RTC_ALARM_ENABLE BIT(1)
34#define PM8xxx_RTC_ALARM_CLEAR BIT(0)
35
36#define NUM_8_BIT_RTC_REGS 0x4
37
38/**
39 * struct pm8xxx_rtc - rtc driver internal structure
40 * @rtc: rtc device for this driver.
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070041 * @regmap: regmap used to access RTC registers
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070042 * @rtc_alarm_irq: rtc alarm irq number.
43 * @rtc_base: address of rtc control register.
44 * @rtc_read_base: base address of read registers.
45 * @rtc_write_base: base address of write registers.
46 * @alarm_rw_base: base address of alarm registers.
47 * @ctrl_reg: rtc control register.
48 * @rtc_dev: device structure.
49 * @ctrl_reg_lock: spinlock protecting access to ctrl_reg.
50 */
51struct pm8xxx_rtc {
52 struct rtc_device *rtc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070053 struct regmap *regmap;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070054 int rtc_alarm_irq;
55 int rtc_base;
56 int rtc_read_base;
57 int rtc_write_base;
58 int alarm_rw_base;
Josh Cartwright5bed8112014-04-03 14:50:10 -070059 u8 ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070060 struct device *rtc_dev;
61 spinlock_t ctrl_reg_lock;
62};
63
64/*
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070065 * Steps to write the RTC registers.
66 * 1. Disable alarm if enabled.
67 * 2. Write 0x00 to LSB.
68 * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0].
69 * 4. Enable alarm if disabled in step 1.
70 */
71static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm)
72{
73 int rc, i;
74 unsigned long secs, irq_flags;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070075 u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070076 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
77
78 rtc_tm_to_time(tm, &secs);
79
80 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
81 value[i] = secs & 0xFF;
82 secs >>= 8;
83 }
84
85 dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs);
86
87 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
88 ctrl_reg = rtc_dd->ctrl_reg;
89
90 if (ctrl_reg & PM8xxx_RTC_ALARM_ENABLE) {
91 alarm_enabled = 1;
92 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -070093 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
94 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -070095 dev_err(dev, "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -070096 goto rtc_rw_fail;
97 }
98 rtc_dd->ctrl_reg = ctrl_reg;
Josh Cartwright5bed8112014-04-03 14:50:10 -070099 } else {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700100 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700101 }
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700102
103 /* Write 0 to Byte[0] */
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700104 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_write_base, 0);
105 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700106 dev_err(dev, "Write to RTC write data register failed\n");
107 goto rtc_rw_fail;
108 }
109
110 /* Write Byte[1], Byte[2], Byte[3] */
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700111 rc = regmap_bulk_write(rtc_dd->regmap, rtc_dd->rtc_write_base + 1,
112 &value[1], sizeof(value) - 1);
113 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700114 dev_err(dev, "Write to RTC write data register failed\n");
115 goto rtc_rw_fail;
116 }
117
118 /* Write Byte[0] */
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700119 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_write_base, value[0]);
120 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700121 dev_err(dev, "Write to RTC write data register failed\n");
122 goto rtc_rw_fail;
123 }
124
125 if (alarm_enabled) {
126 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700127 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
128 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700129 dev_err(dev, "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700130 goto rtc_rw_fail;
131 }
132 rtc_dd->ctrl_reg = ctrl_reg;
133 }
134
135rtc_rw_fail:
136 if (alarm_enabled)
137 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
138
139 return rc;
140}
141
142static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm)
143{
144 int rc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700145 u8 value[NUM_8_BIT_RTC_REGS];
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700146 unsigned long secs;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700147 unsigned int reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700148 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
149
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700150 rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->rtc_read_base,
151 value, sizeof(value));
152 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700153 dev_err(dev, "RTC read data register failed\n");
154 return rc;
155 }
156
157 /*
158 * Read the LSB again and check if there has been a carry over.
159 * If there is, redo the read operation.
160 */
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700161 rc = regmap_read(rtc_dd->regmap, rtc_dd->rtc_read_base, &reg);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700162 if (rc < 0) {
163 dev_err(dev, "RTC read data register failed\n");
164 return rc;
165 }
166
167 if (unlikely(reg < value[0])) {
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700168 rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->rtc_read_base,
169 value, sizeof(value));
170 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700171 dev_err(dev, "RTC read data register failed\n");
172 return rc;
173 }
174 }
175
176 secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
177
178 rtc_time_to_tm(secs, tm);
179
180 rc = rtc_valid_tm(tm);
181 if (rc < 0) {
182 dev_err(dev, "Invalid time read from RTC\n");
183 return rc;
184 }
185
186 dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700187 secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
188 tm->tm_mday, tm->tm_mon, tm->tm_year);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700189
190 return 0;
191}
192
193static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
194{
195 int rc, i;
196 u8 value[NUM_8_BIT_RTC_REGS], ctrl_reg;
197 unsigned long secs, irq_flags;
198 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
199
200 rtc_tm_to_time(&alarm->time, &secs);
201
202 for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) {
203 value[i] = secs & 0xFF;
204 secs >>= 8;
205 }
206
207 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
208
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700209 rc = regmap_bulk_write(rtc_dd->regmap, rtc_dd->alarm_rw_base, value,
210 sizeof(value));
211 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700212 dev_err(dev, "Write to RTC ALARM register failed\n");
213 goto rtc_rw_fail;
214 }
215
216 ctrl_reg = rtc_dd->ctrl_reg;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700217
218 if (alarm->enabled)
219 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE;
220 else
221 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700222
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700223 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
224 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700225 dev_err(dev, "Write to RTC control register failed\n");
226 goto rtc_rw_fail;
227 }
228
229 rtc_dd->ctrl_reg = ctrl_reg;
230
231 dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700232 alarm->time.tm_hour, alarm->time.tm_min,
233 alarm->time.tm_sec, alarm->time.tm_mday,
234 alarm->time.tm_mon, alarm->time.tm_year);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700235rtc_rw_fail:
236 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
237 return rc;
238}
239
240static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
241{
242 int rc;
243 u8 value[NUM_8_BIT_RTC_REGS];
244 unsigned long secs;
245 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
246
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700247 rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->alarm_rw_base, value,
248 sizeof(value));
249 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700250 dev_err(dev, "RTC alarm time read failed\n");
251 return rc;
252 }
253
254 secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
255
256 rtc_time_to_tm(secs, &alarm->time);
257
258 rc = rtc_valid_tm(&alarm->time);
259 if (rc < 0) {
260 dev_err(dev, "Invalid alarm time read from RTC\n");
261 return rc;
262 }
263
264 dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700265 alarm->time.tm_hour, alarm->time.tm_min,
266 alarm->time.tm_sec, alarm->time.tm_mday,
267 alarm->time.tm_mon, alarm->time.tm_year);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700268
269 return 0;
270}
271
272static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
273{
274 int rc;
275 unsigned long irq_flags;
276 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
277 u8 ctrl_reg;
278
279 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700280
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700281 ctrl_reg = rtc_dd->ctrl_reg;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700282
283 if (enable)
284 ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE;
285 else
286 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700287
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700288 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
289 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700290 dev_err(dev, "Write to RTC control register failed\n");
291 goto rtc_rw_fail;
292 }
293
294 rtc_dd->ctrl_reg = ctrl_reg;
295
296rtc_rw_fail:
297 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
298 return rc;
299}
300
301static struct rtc_class_ops pm8xxx_rtc_ops = {
302 .read_time = pm8xxx_rtc_read_time,
303 .set_alarm = pm8xxx_rtc_set_alarm,
304 .read_alarm = pm8xxx_rtc_read_alarm,
305 .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable,
306};
307
308static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id)
309{
310 struct pm8xxx_rtc *rtc_dd = dev_id;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700311 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700312 int rc;
313 unsigned long irq_flags;
314
315 rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
316
317 spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags);
318
319 /* Clear the alarm enable bit */
320 ctrl_reg = rtc_dd->ctrl_reg;
321 ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE;
322
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700323 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
324 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700325 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
Josh Cartwright5bed8112014-04-03 14:50:10 -0700326 dev_err(rtc_dd->rtc_dev,
327 "Write to RTC control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700328 goto rtc_alarm_handled;
329 }
330
331 rtc_dd->ctrl_reg = ctrl_reg;
332 spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags);
333
334 /* Clear RTC alarm register */
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700335 rc = regmap_read(rtc_dd->regmap,
336 rtc_dd->rtc_base + PM8XXX_ALARM_CTRL_OFFSET,
337 &ctrl_reg);
338 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700339 dev_err(rtc_dd->rtc_dev,
340 "RTC Alarm control register read failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700341 goto rtc_alarm_handled;
342 }
343
344 ctrl_reg &= ~PM8xxx_RTC_ALARM_CLEAR;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700345 rc = regmap_write(rtc_dd->regmap,
346 rtc_dd->rtc_base + PM8XXX_ALARM_CTRL_OFFSET,
347 ctrl_reg);
348 if (rc)
Josh Cartwright5bed8112014-04-03 14:50:10 -0700349 dev_err(rtc_dd->rtc_dev,
350 "Write to RTC Alarm control register failed\n");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700351
352rtc_alarm_handled:
353 return IRQ_HANDLED;
354}
355
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800356static int pm8xxx_rtc_probe(struct platform_device *pdev)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700357{
358 int rc;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700359 unsigned int ctrl_reg;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700360 bool rtc_write_enable = false;
361 struct pm8xxx_rtc *rtc_dd;
362 struct resource *rtc_resource;
363 const struct pm8xxx_rtc_platform_data *pdata =
364 dev_get_platdata(&pdev->dev);
365
366 if (pdata != NULL)
367 rtc_write_enable = pdata->rtc_write_enable;
368
Jingoo Hanc4172992013-07-03 15:07:09 -0700369 rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL);
Jingoo Han49ae4252014-04-03 14:49:43 -0700370 if (rtc_dd == NULL)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700371 return -ENOMEM;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700372
373 /* Initialise spinlock to protect RTC control register */
374 spin_lock_init(&rtc_dd->ctrl_reg_lock);
375
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700376 rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL);
377 if (!rtc_dd->regmap) {
378 dev_err(&pdev->dev, "Parent regmap unavailable.\n");
379 return -ENXIO;
380 }
381
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700382 rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0);
383 if (rtc_dd->rtc_alarm_irq < 0) {
384 dev_err(&pdev->dev, "Alarm IRQ resource absent!\n");
Jingoo Hanc4172992013-07-03 15:07:09 -0700385 return -ENXIO;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700386 }
387
388 rtc_resource = platform_get_resource_byname(pdev, IORESOURCE_IO,
Josh Cartwright5bed8112014-04-03 14:50:10 -0700389 "pmic_rtc_base");
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700390 if (!(rtc_resource && rtc_resource->start)) {
391 dev_err(&pdev->dev, "RTC IO resource absent!\n");
Jingoo Hanc4172992013-07-03 15:07:09 -0700392 return -ENXIO;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700393 }
394
395 rtc_dd->rtc_base = rtc_resource->start;
396
397 /* Setup RTC register addresses */
398 rtc_dd->rtc_write_base = rtc_dd->rtc_base + PM8XXX_RTC_WRITE_OFFSET;
399 rtc_dd->rtc_read_base = rtc_dd->rtc_base + PM8XXX_RTC_READ_OFFSET;
400 rtc_dd->alarm_rw_base = rtc_dd->rtc_base + PM8XXX_ALARM_RW_OFFSET;
401
402 rtc_dd->rtc_dev = &pdev->dev;
403
404 /* Check if the RTC is on, else turn it on */
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700405 rc = regmap_read(rtc_dd->regmap, rtc_dd->rtc_base, &ctrl_reg);
406 if (rc) {
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700407 dev_err(&pdev->dev, "RTC control register read failed!\n");
Jingoo Hanc4172992013-07-03 15:07:09 -0700408 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700409 }
410
411 if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) {
412 ctrl_reg |= PM8xxx_RTC_ENABLE;
Josh Cartwright5d7dc4c2014-04-03 14:50:11 -0700413 rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg);
414 if (rc) {
Josh Cartwright5bed8112014-04-03 14:50:10 -0700415 dev_err(&pdev->dev,
416 "Write to RTC control register failed\n");
Jingoo Hanc4172992013-07-03 15:07:09 -0700417 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700418 }
419 }
420
421 rtc_dd->ctrl_reg = ctrl_reg;
Josh Cartwright5bed8112014-04-03 14:50:10 -0700422 if (rtc_write_enable)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700423 pm8xxx_rtc_ops.set_time = pm8xxx_rtc_set_time;
424
425 platform_set_drvdata(pdev, rtc_dd);
426
427 /* Register the RTC device */
Jingoo Hanc4172992013-07-03 15:07:09 -0700428 rtc_dd->rtc = devm_rtc_device_register(&pdev->dev, "pm8xxx_rtc",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700429 &pm8xxx_rtc_ops, THIS_MODULE);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700430 if (IS_ERR(rtc_dd->rtc)) {
431 dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n",
Josh Cartwright5bed8112014-04-03 14:50:10 -0700432 __func__, PTR_ERR(rtc_dd->rtc));
Jingoo Hanc4172992013-07-03 15:07:09 -0700433 return PTR_ERR(rtc_dd->rtc);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700434 }
435
436 /* Request the alarm IRQ */
437 rc = request_any_context_irq(rtc_dd->rtc_alarm_irq,
Josh Cartwright5bed8112014-04-03 14:50:10 -0700438 pm8xxx_alarm_trigger, IRQF_TRIGGER_RISING,
439 "pm8xxx_rtc_alarm", rtc_dd);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700440 if (rc < 0) {
441 dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc);
Jingoo Hanc4172992013-07-03 15:07:09 -0700442 return rc;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700443 }
444
445 device_init_wakeup(&pdev->dev, 1);
446
447 dev_dbg(&pdev->dev, "Probe success !!\n");
448
449 return 0;
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700450}
451
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800452static int pm8xxx_rtc_remove(struct platform_device *pdev)
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700453{
454 struct pm8xxx_rtc *rtc_dd = platform_get_drvdata(pdev);
455
456 device_init_wakeup(&pdev->dev, 0);
457 free_irq(rtc_dd->rtc_alarm_irq, rtc_dd);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700458
459 return 0;
460}
461
462#ifdef CONFIG_PM_SLEEP
463static int pm8xxx_rtc_resume(struct device *dev)
464{
465 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
466
467 if (device_may_wakeup(dev))
468 disable_irq_wake(rtc_dd->rtc_alarm_irq);
469
470 return 0;
471}
472
473static int pm8xxx_rtc_suspend(struct device *dev)
474{
475 struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev);
476
477 if (device_may_wakeup(dev))
478 enable_irq_wake(rtc_dd->rtc_alarm_irq);
479
480 return 0;
481}
482#endif
483
Josh Cartwright5bed8112014-04-03 14:50:10 -0700484static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops,
485 pm8xxx_rtc_suspend,
486 pm8xxx_rtc_resume);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700487
488static struct platform_driver pm8xxx_rtc_driver = {
489 .probe = pm8xxx_rtc_probe,
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800490 .remove = pm8xxx_rtc_remove,
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700491 .driver = {
492 .name = PM8XXX_RTC_DEV_NAME,
493 .owner = THIS_MODULE,
494 .pm = &pm8xxx_rtc_pm_ops,
495 },
496};
497
Axel Lin0c4eae62012-01-10 15:10:48 -0800498module_platform_driver(pm8xxx_rtc_driver);
Anirudh Ghayal9a9a54a2011-07-25 17:13:33 -0700499
500MODULE_ALIAS("platform:rtc-pm8xxx");
501MODULE_DESCRIPTION("PMIC8xxx RTC driver");
502MODULE_LICENSE("GPL v2");
503MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>");