blob: 613c43b7e9ae0daeb2eccc9282a44c689add67f3 [file] [log] [blame]
Ashish Jangamfef931f2012-03-23 15:02:36 -07001/*
2 * Real time clock driver for DA9052
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: Dajun Dajun Chen <dajun.chen@diasemi.com>
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 as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
14
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/rtc.h>
Sachin Kamat11f54d02013-07-03 15:07:48 -070018#include <linux/err.h>
Ashish Jangamfef931f2012-03-23 15:02:36 -070019
20#include <linux/mfd/da9052/da9052.h>
21#include <linux/mfd/da9052/reg.h>
22
Anthony Olech7c994c02014-06-06 14:35:54 -070023#define rtc_err(rtc, fmt, ...) \
24 dev_err(rtc->da9052->dev, "%s: " fmt, __func__, ##__VA_ARGS__)
Ashish Jangamfef931f2012-03-23 15:02:36 -070025
26struct da9052_rtc {
27 struct rtc_device *rtc;
28 struct da9052 *da9052;
Ashish Jangamfef931f2012-03-23 15:02:36 -070029};
30
Anthony Olech7c994c02014-06-06 14:35:54 -070031static int da9052_rtc_enable_alarm(struct da9052_rtc *rtc, bool enable)
Ashish Jangamfef931f2012-03-23 15:02:36 -070032{
33 int ret;
34 if (enable) {
Anthony Olech7c994c02014-06-06 14:35:54 -070035 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
36 DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON,
37 DA9052_ALARM_Y_ALARM_ON);
Ashish Jangamfef931f2012-03-23 15:02:36 -070038 if (ret != 0)
Anthony Olech7c994c02014-06-06 14:35:54 -070039 rtc_err(rtc, "Failed to enable ALM: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -070040 } else {
Anthony Olech7c994c02014-06-06 14:35:54 -070041 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
42 DA9052_ALARM_Y_ALARM_ON|DA9052_ALARM_Y_TICK_ON, 0);
Ashish Jangamfef931f2012-03-23 15:02:36 -070043 if (ret != 0)
Anthony Olech7c994c02014-06-06 14:35:54 -070044 rtc_err(rtc, "Write error: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -070045 }
46 return ret;
47}
48
49static irqreturn_t da9052_rtc_irq(int irq, void *data)
50{
51 struct da9052_rtc *rtc = data;
Ashish Jangamfef931f2012-03-23 15:02:36 -070052
Anthony Olech7c994c02014-06-06 14:35:54 -070053 rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
Ashish Jangamfef931f2012-03-23 15:02:36 -070054
55 return IRQ_HANDLED;
56}
57
Anthony Olech7c994c02014-06-06 14:35:54 -070058static int da9052_read_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
Ashish Jangamfef931f2012-03-23 15:02:36 -070059{
60 int ret;
61 uint8_t v[5];
62
Anthony Olech7c994c02014-06-06 14:35:54 -070063 ret = da9052_group_read(rtc->da9052, DA9052_ALARM_MI_REG, 5, v);
Ashish Jangamfef931f2012-03-23 15:02:36 -070064 if (ret != 0) {
Anthony Olech7c994c02014-06-06 14:35:54 -070065 rtc_err(rtc, "Failed to group read ALM: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -070066 return ret;
67 }
68
69 rtc_tm->tm_year = (v[4] & DA9052_RTC_YEAR) + 100;
70 rtc_tm->tm_mon = (v[3] & DA9052_RTC_MONTH) - 1;
71 rtc_tm->tm_mday = v[2] & DA9052_RTC_DAY;
72 rtc_tm->tm_hour = v[1] & DA9052_RTC_HOUR;
73 rtc_tm->tm_min = v[0] & DA9052_RTC_MIN;
74
75 ret = rtc_valid_tm(rtc_tm);
Ashish Jangamfef931f2012-03-23 15:02:36 -070076 return ret;
77}
78
Anthony Olech7c994c02014-06-06 14:35:54 -070079static int da9052_set_alarm(struct da9052_rtc *rtc, struct rtc_time *rtc_tm)
Ashish Jangamfef931f2012-03-23 15:02:36 -070080{
Anthony Olech7c994c02014-06-06 14:35:54 -070081 struct da9052 *da9052 = rtc->da9052;
82 unsigned long alm_time;
Ashish Jangamfef931f2012-03-23 15:02:36 -070083 int ret;
84 uint8_t v[3];
85
Anthony Olech7c994c02014-06-06 14:35:54 -070086 ret = rtc_tm_to_time(rtc_tm, &alm_time);
87 if (ret != 0)
88 return ret;
89
90 if (rtc_tm->tm_sec > 0) {
91 alm_time += 60 - rtc_tm->tm_sec;
92 rtc_time_to_tm(alm_time, rtc_tm);
93 }
94 BUG_ON(rtc_tm->tm_sec); /* it will cause repeated irqs if not zero */
95
Ashish Jangamfef931f2012-03-23 15:02:36 -070096 rtc_tm->tm_year -= 100;
97 rtc_tm->tm_mon += 1;
98
99 ret = da9052_reg_update(da9052, DA9052_ALARM_MI_REG,
100 DA9052_RTC_MIN, rtc_tm->tm_min);
101 if (ret != 0) {
Anthony Olech7c994c02014-06-06 14:35:54 -0700102 rtc_err(rtc, "Failed to write ALRM MIN: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700103 return ret;
104 }
105
106 v[0] = rtc_tm->tm_hour;
107 v[1] = rtc_tm->tm_mday;
108 v[2] = rtc_tm->tm_mon;
109
110 ret = da9052_group_write(da9052, DA9052_ALARM_H_REG, 3, v);
111 if (ret < 0)
112 return ret;
113
114 ret = da9052_reg_update(da9052, DA9052_ALARM_Y_REG,
115 DA9052_RTC_YEAR, rtc_tm->tm_year);
116 if (ret != 0)
Anthony Olech7c994c02014-06-06 14:35:54 -0700117 rtc_err(rtc, "Failed to write ALRM YEAR: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700118
119 return ret;
120}
121
Anthony Olech7c994c02014-06-06 14:35:54 -0700122static int da9052_rtc_get_alarm_status(struct da9052_rtc *rtc)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700123{
124 int ret;
125
Anthony Olech7c994c02014-06-06 14:35:54 -0700126 ret = da9052_reg_read(rtc->da9052, DA9052_ALARM_Y_REG);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700127 if (ret < 0) {
Anthony Olech7c994c02014-06-06 14:35:54 -0700128 rtc_err(rtc, "Failed to read ALM: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700129 return ret;
130 }
Anthony Olech7c994c02014-06-06 14:35:54 -0700131
132 return !!(ret&DA9052_ALARM_Y_ALARM_ON);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700133}
134
135static int da9052_rtc_read_time(struct device *dev, struct rtc_time *rtc_tm)
136{
137 struct da9052_rtc *rtc = dev_get_drvdata(dev);
138 uint8_t v[6];
139 int ret;
140
141 ret = da9052_group_read(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
142 if (ret < 0) {
Anthony Olech7c994c02014-06-06 14:35:54 -0700143 rtc_err(rtc, "Failed to read RTC time : %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700144 return ret;
145 }
146
147 rtc_tm->tm_year = (v[5] & DA9052_RTC_YEAR) + 100;
148 rtc_tm->tm_mon = (v[4] & DA9052_RTC_MONTH) - 1;
149 rtc_tm->tm_mday = v[3] & DA9052_RTC_DAY;
150 rtc_tm->tm_hour = v[2] & DA9052_RTC_HOUR;
151 rtc_tm->tm_min = v[1] & DA9052_RTC_MIN;
152 rtc_tm->tm_sec = v[0] & DA9052_RTC_SEC;
153
154 ret = rtc_valid_tm(rtc_tm);
Anthony Olech7c994c02014-06-06 14:35:54 -0700155 return ret;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700156}
157
158static int da9052_rtc_set_time(struct device *dev, struct rtc_time *tm)
159{
160 struct da9052_rtc *rtc;
161 uint8_t v[6];
Anthony Olech7c994c02014-06-06 14:35:54 -0700162 int ret;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700163
164 rtc = dev_get_drvdata(dev);
165
166 v[0] = tm->tm_sec;
167 v[1] = tm->tm_min;
168 v[2] = tm->tm_hour;
169 v[3] = tm->tm_mday;
170 v[4] = tm->tm_mon + 1;
171 v[5] = tm->tm_year - 100;
172
Anthony Olech7c994c02014-06-06 14:35:54 -0700173 ret = da9052_group_write(rtc->da9052, DA9052_COUNT_S_REG, 6, v);
174 if (ret < 0)
175 rtc_err(rtc, "failed to set RTC time: %d\n", ret);
176 return ret;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700177}
178
179static int da9052_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
180{
181 int ret;
182 struct rtc_time *tm = &alrm->time;
183 struct da9052_rtc *rtc = dev_get_drvdata(dev);
184
Anthony Olech7c994c02014-06-06 14:35:54 -0700185 ret = da9052_read_alarm(rtc, tm);
186 if (ret < 0) {
187 rtc_err(rtc, "failed to read RTC alarm: %d\n", ret);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700188 return ret;
Anthony Olech7c994c02014-06-06 14:35:54 -0700189 }
Ashish Jangamfef931f2012-03-23 15:02:36 -0700190
Anthony Olech7c994c02014-06-06 14:35:54 -0700191 alrm->enabled = da9052_rtc_get_alarm_status(rtc);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700192 return 0;
193}
194
195static int da9052_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
196{
197 int ret;
198 struct rtc_time *tm = &alrm->time;
199 struct da9052_rtc *rtc = dev_get_drvdata(dev);
200
Anthony Olech7c994c02014-06-06 14:35:54 -0700201 ret = da9052_rtc_enable_alarm(rtc, 0);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700202 if (ret < 0)
203 return ret;
204
Anthony Olech7c994c02014-06-06 14:35:54 -0700205 ret = da9052_set_alarm(rtc, tm);
206 if (ret < 0)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700207 return ret;
208
Anthony Olech7c994c02014-06-06 14:35:54 -0700209 ret = da9052_rtc_enable_alarm(rtc, 1);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700210 return ret;
211}
212
213static int da9052_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
214{
215 struct da9052_rtc *rtc = dev_get_drvdata(dev);
216
Anthony Olech7c994c02014-06-06 14:35:54 -0700217 return da9052_rtc_enable_alarm(rtc, enabled);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700218}
219
220static const struct rtc_class_ops da9052_rtc_ops = {
221 .read_time = da9052_rtc_read_time,
222 .set_time = da9052_rtc_set_time,
223 .read_alarm = da9052_rtc_read_alarm,
224 .set_alarm = da9052_rtc_set_alarm,
225 .alarm_irq_enable = da9052_rtc_alarm_irq_enable,
226};
227
Greg Kroah-Hartman5a167f42012-12-21 13:09:38 -0800228static int da9052_rtc_probe(struct platform_device *pdev)
Ashish Jangamfef931f2012-03-23 15:02:36 -0700229{
230 struct da9052_rtc *rtc;
231 int ret;
232
233 rtc = devm_kzalloc(&pdev->dev, sizeof(struct da9052_rtc), GFP_KERNEL);
234 if (!rtc)
235 return -ENOMEM;
236
237 rtc->da9052 = dev_get_drvdata(pdev->dev.parent);
238 platform_set_drvdata(pdev, rtc);
Anthony Olech7c994c02014-06-06 14:35:54 -0700239
240 ret = da9052_reg_write(rtc->da9052, DA9052_BBAT_CONT_REG, 0xFE);
241 if (ret < 0) {
242 rtc_err(rtc,
243 "Failed to setup RTC battery charging: %d\n", ret);
244 return ret;
245 }
246
247 ret = da9052_reg_update(rtc->da9052, DA9052_ALARM_Y_REG,
248 DA9052_ALARM_Y_TICK_ON, 0);
249 if (ret != 0)
250 rtc_err(rtc, "Failed to disable TICKS: %d\n", ret);
251
Anthony Olechc2c0eed2014-04-03 14:49:52 -0700252 ret = da9052_request_irq(rtc->da9052, DA9052_IRQ_ALARM, "ALM",
Ashish Jangam925e8ea2013-03-22 15:04:44 -0700253 da9052_rtc_irq, rtc);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700254 if (ret != 0) {
Anthony Olech7c994c02014-06-06 14:35:54 -0700255 rtc_err(rtc, "irq registration failed: %d\n", ret);
Devendra Naga007def02012-07-30 14:41:54 -0700256 return ret;
Ashish Jangamfef931f2012-03-23 15:02:36 -0700257 }
258
Jingoo Han3689cd72013-04-29 16:18:57 -0700259 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
Ashish Jangamfef931f2012-03-23 15:02:36 -0700260 &da9052_rtc_ops, THIS_MODULE);
Sachin Kamatdac30a92013-07-15 15:43:32 +0530261 return PTR_ERR_OR_ZERO(rtc->rtc);
Ashish Jangamfef931f2012-03-23 15:02:36 -0700262}
263
Ashish Jangamfef931f2012-03-23 15:02:36 -0700264static struct platform_driver da9052_rtc_driver = {
265 .probe = da9052_rtc_probe,
Ashish Jangamfef931f2012-03-23 15:02:36 -0700266 .driver = {
267 .name = "da9052-rtc",
Ashish Jangamfef931f2012-03-23 15:02:36 -0700268 },
269};
270
271module_platform_driver(da9052_rtc_driver);
272
Anthony Olech7c994c02014-06-06 14:35:54 -0700273MODULE_AUTHOR("Anthony Olech <Anthony.Olech@diasemi.com>");
Ashish Jangamfef931f2012-03-23 15:02:36 -0700274MODULE_DESCRIPTION("RTC driver for Dialog DA9052 PMIC");
275MODULE_LICENSE("GPL");
276MODULE_ALIAS("platform:da9052-rtc");