blob: 2131c55316f6e62cfc2315533d426fdf5cc4c215 [file] [log] [blame]
Alexandre Bellonide311aa2019-04-01 18:33:49 +02001// SPDX-License-Identifier: GPL-2.0
Søren Andersen796b7ab2014-08-08 14:20:22 -07002/*
3 * An I2C driver for the PCF85063 RTC
4 * Copyright 2014 Rose Technology
5 *
6 * Author: Søren Andersen <san@rosetechnology.dk>
7 * Maintainers: http://www.nslu2-linux.org/
Søren Andersen796b7ab2014-08-08 14:20:22 -07008 */
9#include <linux/i2c.h>
10#include <linux/bcd.h>
11#include <linux/rtc.h>
12#include <linux/module.h>
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020013#include <linux/of_device.h>
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020014#include <linux/pm_wakeirq.h>
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020015#include <linux/regmap.h>
Søren Andersen796b7ab2014-08-08 14:20:22 -070016
Chris DeBruin0d981f82016-07-12 17:15:46 -040017/*
18 * Information for this driver was pulled from the following datasheets.
19 *
20 * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
21 * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
22 *
23 * PCF85063A -- Rev. 6 — 18 November 2015
24 * PCF85063TP -- Rev. 4 — 6 May 2015
25*/
26
Søren Andersen796b7ab2014-08-08 14:20:22 -070027#define PCF85063_REG_CTRL1 0x00 /* status */
Sam Ravnborgbbb43832019-01-19 10:00:31 +010028#define PCF85063_REG_CTRL1_CAP_SEL BIT(0)
Juergen Borleis31d4d332016-02-09 11:57:27 +010029#define PCF85063_REG_CTRL1_STOP BIT(5)
Søren Andersen796b7ab2014-08-08 14:20:22 -070030
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020031#define PCF85063_REG_CTRL2 0x01
32#define PCF85063_CTRL2_AF BIT(6)
33#define PCF85063_CTRL2_AIE BIT(7)
34
Søren Andersen796b7ab2014-08-08 14:20:22 -070035#define PCF85063_REG_SC 0x04 /* datetime */
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010036#define PCF85063_REG_SC_OS 0x80
Søren Andersen796b7ab2014-08-08 14:20:22 -070037
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020038#define PCF85063_REG_ALM_S 0x0b
39#define PCF85063_AEN BIT(7)
40
Alexandre Belloni0e2e8772019-04-01 18:08:11 +020041struct pcf85063_config {
42 struct regmap_config regmap;
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020043 unsigned has_alarms:1;
Alexandre Belloni0e2e8772019-04-01 18:08:11 +020044};
45
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020046struct pcf85063 {
47 struct rtc_device *rtc;
48 struct regmap *regmap;
49};
Chris DeBruin0d981f82016-07-12 17:15:46 -040050
Alexandre Belloni965271d2018-02-21 16:09:27 +010051static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
Søren Andersen796b7ab2014-08-08 14:20:22 -070052{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020053 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
Juergen Borleis7b576842016-02-09 11:57:25 +010054 int rc;
Juergen Borleis7b576842016-02-09 11:57:25 +010055 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070056
Juergen Borleis7b576842016-02-09 11:57:25 +010057 /*
58 * while reading, the time/date registers are blocked and not updated
59 * anymore until the access is finished. To not lose a second
60 * event, the access must be finished within one second. So, read all
61 * time/date registers in one turn.
62 */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020063 rc = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_SC, regs,
64 sizeof(regs));
65 if (rc)
66 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -070067
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010068 /* if the clock has lost its power it makes no sense to use its time */
69 if (regs[0] & PCF85063_REG_SC_OS) {
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020070 dev_warn(&pcf85063->rtc->dev, "Power loss detected, invalid time\n");
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010071 return -EINVAL;
72 }
73
Juergen Borleis7b576842016-02-09 11:57:25 +010074 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
75 tm->tm_min = bcd2bin(regs[1] & 0x7F);
76 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
77 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
78 tm->tm_wday = regs[4] & 0x07;
79 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
80 tm->tm_year = bcd2bin(regs[6]);
Alexandre Bellonic421ce72016-07-18 11:08:59 +020081 tm->tm_year += 100;
Søren Andersen796b7ab2014-08-08 14:20:22 -070082
Alexandre Belloni0a6b8882018-02-21 16:07:34 +010083 return 0;
Søren Andersen796b7ab2014-08-08 14:20:22 -070084}
85
Alexandre Belloni965271d2018-02-21 16:09:27 +010086static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
Søren Andersen796b7ab2014-08-08 14:20:22 -070087{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020088 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
Juergen Borleis31d4d332016-02-09 11:57:27 +010089 int rc;
Chris DeBruin0d981f82016-07-12 17:15:46 -040090 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070091
Juergen Borleis31d4d332016-02-09 11:57:27 +010092 /*
93 * to accurately set the time, reset the divider chain and keep it in
94 * reset state until all time/date registers are written
95 */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020096 rc = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
97 PCF85063_REG_CTRL1_STOP,
98 PCF85063_REG_CTRL1_STOP);
99 if (rc)
Juergen Borleis31d4d332016-02-09 11:57:27 +0100100 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700101
102 /* hours, minutes and seconds */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100103 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
Søren Andersen796b7ab2014-08-08 14:20:22 -0700104
Juergen Borleis31d4d332016-02-09 11:57:27 +0100105 regs[1] = bin2bcd(tm->tm_min);
106 regs[2] = bin2bcd(tm->tm_hour);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700107
108 /* Day of month, 1 - 31 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100109 regs[3] = bin2bcd(tm->tm_mday);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700110
111 /* Day, 0 - 6 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100112 regs[4] = tm->tm_wday & 0x07;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700113
114 /* month, 1 - 12 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100115 regs[5] = bin2bcd(tm->tm_mon + 1);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700116
117 /* year and century */
Alexandre Bellonic421ce72016-07-18 11:08:59 +0200118 regs[6] = bin2bcd(tm->tm_year - 100);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700119
Juergen Borleis31d4d332016-02-09 11:57:27 +0100120 /* write all registers at once */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200121 rc = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_SC,
122 regs, sizeof(regs));
123 if (rc)
Juergen Borleis31d4d332016-02-09 11:57:27 +0100124 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700125
Chris DeBruin0d981f82016-07-12 17:15:46 -0400126 /*
127 * Write the control register as a separate action since the size of
128 * the register space is different between the PCF85063TP and
129 * PCF85063A devices. The rollover point can not be used.
130 */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200131 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
132 PCF85063_REG_CTRL1_STOP, 0);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700133}
134
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200135static int pcf85063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
136{
137 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
138 u8 buf[4];
139 unsigned int val;
140 int ret;
141
142 ret = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_ALM_S,
143 buf, sizeof(buf));
144 if (ret)
145 return ret;
146
147 alrm->time.tm_sec = bcd2bin(buf[0]);
148 alrm->time.tm_min = bcd2bin(buf[1]);
149 alrm->time.tm_hour = bcd2bin(buf[2]);
150 alrm->time.tm_mday = bcd2bin(buf[3]);
151
152 ret = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL2, &val);
153 if (ret)
154 return ret;
155
156 alrm->enabled = !!(val & PCF85063_CTRL2_AIE);
157
158 return 0;
159}
160
161static int pcf85063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
162{
163 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
164 u8 buf[5];
165 int ret;
166
167 buf[0] = bin2bcd(alrm->time.tm_sec);
168 buf[1] = bin2bcd(alrm->time.tm_min);
169 buf[2] = bin2bcd(alrm->time.tm_hour);
170 buf[3] = bin2bcd(alrm->time.tm_mday);
171 buf[4] = PCF85063_AEN; /* Do not match on week day */
172
173 ret = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
174 PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF, 0);
175 if (ret)
176 return ret;
177
178 ret = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_ALM_S,
179 buf, sizeof(buf));
180 if (ret)
181 return ret;
182
183 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
184 PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF,
185 alrm->enabled ? PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF : PCF85063_CTRL2_AF);
186}
187
188static int pcf85063_rtc_alarm_irq_enable(struct device *dev,
189 unsigned int enabled)
190{
191 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
192
193 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
194 PCF85063_CTRL2_AIE,
195 enabled ? PCF85063_CTRL2_AIE : 0);
196}
197
198static irqreturn_t pcf85063_rtc_handle_irq(int irq, void *dev_id)
199{
200 struct pcf85063 *pcf85063 = dev_id;
201 unsigned int val;
202 int err;
203
204 err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL2, &val);
205 if (err)
206 return IRQ_NONE;
207
208 if (val & PCF85063_CTRL2_AF) {
209 rtc_update_irq(pcf85063->rtc, 1, RTC_IRQF | RTC_AF);
210 regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
211 PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF,
212 0);
213 return IRQ_HANDLED;
214 }
215
216 return IRQ_NONE;
217}
218
Søren Andersen796b7ab2014-08-08 14:20:22 -0700219static const struct rtc_class_ops pcf85063_rtc_ops = {
220 .read_time = pcf85063_rtc_read_time,
221 .set_time = pcf85063_rtc_set_time
222};
223
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200224static const struct rtc_class_ops pcf85063_rtc_ops_alarm = {
225 .read_time = pcf85063_rtc_read_time,
226 .set_time = pcf85063_rtc_set_time,
227 .read_alarm = pcf85063_rtc_read_alarm,
228 .set_alarm = pcf85063_rtc_set_alarm,
229 .alarm_irq_enable = pcf85063_rtc_alarm_irq_enable,
230};
231
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200232static int pcf85063_load_capacitance(struct pcf85063 *pcf85063,
233 const struct device_node *np)
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100234{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200235 u32 load = 7000;
236 u8 reg = 0;
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100237
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200238 of_property_read_u32(np, "quartz-load-femtofarads", &load);
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100239 switch (load) {
240 default:
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200241 dev_warn(&pcf85063->rtc->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100242 load);
243 /* fall through */
244 case 7000:
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100245 break;
246 case 12500:
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200247 reg = PCF85063_REG_CTRL1_CAP_SEL;
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100248 break;
249 }
250
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200251 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
252 PCF85063_REG_CTRL1_CAP_SEL, reg);
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100253}
254
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200255static const struct pcf85063_config pcf85063a_config = {
256 .regmap = {
257 .reg_bits = 8,
258 .val_bits = 8,
259 .max_register = 0x11,
260 },
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200261 .has_alarms = 1,
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200262};
263
264static const struct pcf85063_config pcf85063tp_config = {
265 .regmap = {
266 .reg_bits = 8,
267 .val_bits = 8,
268 .max_register = 0x0a,
269 },
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200270};
271
Alexandre Belloni0f217002019-04-01 18:08:05 +0200272static int pcf85063_probe(struct i2c_client *client)
Søren Andersen796b7ab2014-08-08 14:20:22 -0700273{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200274 struct pcf85063 *pcf85063;
275 unsigned int tmp;
Mirza Krakc18b4c52016-10-17 15:53:31 +0200276 int err;
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200277 const struct pcf85063_config *config = &pcf85063tp_config;
278 const void *data = of_device_get_match_data(&client->dev);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700279
280 dev_dbg(&client->dev, "%s\n", __func__);
281
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200282 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
283 GFP_KERNEL);
284 if (!pcf85063)
285 return -ENOMEM;
286
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200287 if (data)
288 config = data;
289
290 pcf85063->regmap = devm_regmap_init_i2c(client, &config->regmap);
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200291 if (IS_ERR(pcf85063->regmap))
292 return PTR_ERR(pcf85063->regmap);
293
294 i2c_set_clientdata(client, pcf85063);
295
296 err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL1, &tmp);
297 if (err) {
Mirza Krakc18b4c52016-10-17 15:53:31 +0200298 dev_err(&client->dev, "RTC chip is not present\n");
299 return err;
300 }
301
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200302 pcf85063->rtc = devm_rtc_allocate_device(&client->dev);
303 if (IS_ERR(pcf85063->rtc))
304 return PTR_ERR(pcf85063->rtc);
305
306 err = pcf85063_load_capacitance(pcf85063, client->dev.of_node);
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100307 if (err < 0)
308 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
309 err);
310
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200311 pcf85063->rtc->ops = &pcf85063_rtc_ops;
312 pcf85063->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
313 pcf85063->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200314 pcf85063->rtc->uie_unsupported = 1;
315
316 if (config->has_alarms && client->irq > 0) {
317 err = devm_request_threaded_irq(&client->dev, client->irq,
318 NULL, pcf85063_rtc_handle_irq,
319 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
320 "pcf85063", pcf85063);
321 if (err) {
322 dev_warn(&pcf85063->rtc->dev,
323 "unable to request IRQ, alarms disabled\n");
324 } else {
325 pcf85063->rtc->ops = &pcf85063_rtc_ops_alarm;
326 device_init_wakeup(&client->dev, true);
327 err = dev_pm_set_wake_irq(&client->dev, client->irq);
328 if (err)
329 dev_err(&pcf85063->rtc->dev,
330 "failed to enable irq wake\n");
331 }
332 }
Søren Andersen796b7ab2014-08-08 14:20:22 -0700333
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200334 return rtc_register_device(pcf85063->rtc);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700335}
336
Søren Andersen796b7ab2014-08-08 14:20:22 -0700337#ifdef CONFIG_OF
338static const struct of_device_id pcf85063_of_match[] = {
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200339 { .compatible = "nxp,pcf85063", .data = &pcf85063tp_config },
340 { .compatible = "nxp,pcf85063tp", .data = &pcf85063tp_config },
341 { .compatible = "nxp,pcf85063a", .data = &pcf85063a_config },
Søren Andersen796b7ab2014-08-08 14:20:22 -0700342 {}
343};
344MODULE_DEVICE_TABLE(of, pcf85063_of_match);
345#endif
346
347static struct i2c_driver pcf85063_driver = {
348 .driver = {
349 .name = "rtc-pcf85063",
Søren Andersen796b7ab2014-08-08 14:20:22 -0700350 .of_match_table = of_match_ptr(pcf85063_of_match),
351 },
Alexandre Belloni0f217002019-04-01 18:08:05 +0200352 .probe_new = pcf85063_probe,
Søren Andersen796b7ab2014-08-08 14:20:22 -0700353};
354
355module_i2c_driver(pcf85063_driver);
356
357MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
358MODULE_DESCRIPTION("PCF85063 RTC driver");
359MODULE_LICENSE("GPL");