blob: 1afa6d9fa9fb26126a0ad6691f7f9765a36bab71 [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/
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +02008 *
9 * Copyright (C) 2019 Micro Crystal AG
10 * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
Søren Andersen796b7ab2014-08-08 14:20:22 -070011 */
12#include <linux/i2c.h>
13#include <linux/bcd.h>
14#include <linux/rtc.h>
15#include <linux/module.h>
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020016#include <linux/of_device.h>
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020017#include <linux/pm_wakeirq.h>
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020018#include <linux/regmap.h>
Søren Andersen796b7ab2014-08-08 14:20:22 -070019
Chris DeBruin0d981f82016-07-12 17:15:46 -040020/*
21 * Information for this driver was pulled from the following datasheets.
22 *
23 * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
24 * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
25 *
26 * PCF85063A -- Rev. 6 — 18 November 2015
27 * PCF85063TP -- Rev. 4 — 6 May 2015
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +020028 *
29 * https://www.microcrystal.com/fileadmin/Media/Products/RTC/App.Manual/RV-8263-C7_App-Manual.pdf
30 * RV8263 -- Rev. 1.0 — January 2019
31 */
Chris DeBruin0d981f82016-07-12 17:15:46 -040032
Søren Andersen796b7ab2014-08-08 14:20:22 -070033#define PCF85063_REG_CTRL1 0x00 /* status */
Sam Ravnborgbbb43832019-01-19 10:00:31 +010034#define PCF85063_REG_CTRL1_CAP_SEL BIT(0)
Juergen Borleis31d4d332016-02-09 11:57:27 +010035#define PCF85063_REG_CTRL1_STOP BIT(5)
Søren Andersen796b7ab2014-08-08 14:20:22 -070036
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020037#define PCF85063_REG_CTRL2 0x01
38#define PCF85063_CTRL2_AF BIT(6)
39#define PCF85063_CTRL2_AIE BIT(7)
40
Alexandre Belloni85370d32019-04-01 18:08:15 +020041#define PCF85063_REG_OFFSET 0x02
42#define PCF85063_OFFSET_SIGN_BIT 6 /* 2's complement sign bit */
43#define PCF85063_OFFSET_MODE BIT(7)
44#define PCF85063_OFFSET_STEP0 4340
45#define PCF85063_OFFSET_STEP1 4069
46
Alexandre Bellonifadfd092019-04-01 18:08:14 +020047#define PCF85063_REG_RAM 0x03
48
Søren Andersen796b7ab2014-08-08 14:20:22 -070049#define PCF85063_REG_SC 0x04 /* datetime */
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010050#define PCF85063_REG_SC_OS 0x80
Søren Andersen796b7ab2014-08-08 14:20:22 -070051
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020052#define PCF85063_REG_ALM_S 0x0b
53#define PCF85063_AEN BIT(7)
54
Alexandre Belloni0e2e8772019-04-01 18:08:11 +020055struct pcf85063_config {
56 struct regmap_config regmap;
Alexandre Belloni05cb3a52019-04-01 18:08:12 +020057 unsigned has_alarms:1;
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +020058 unsigned force_cap_7000:1;
Alexandre Belloni0e2e8772019-04-01 18:08:11 +020059};
60
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020061struct pcf85063 {
62 struct rtc_device *rtc;
63 struct regmap *regmap;
64};
Chris DeBruin0d981f82016-07-12 17:15:46 -040065
Alexandre Belloni965271d2018-02-21 16:09:27 +010066static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
Søren Andersen796b7ab2014-08-08 14:20:22 -070067{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020068 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
Juergen Borleis7b576842016-02-09 11:57:25 +010069 int rc;
Juergen Borleis7b576842016-02-09 11:57:25 +010070 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070071
Juergen Borleis7b576842016-02-09 11:57:25 +010072 /*
73 * while reading, the time/date registers are blocked and not updated
74 * anymore until the access is finished. To not lose a second
75 * event, the access must be finished within one second. So, read all
76 * time/date registers in one turn.
77 */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020078 rc = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_SC, regs,
79 sizeof(regs));
80 if (rc)
81 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -070082
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010083 /* if the clock has lost its power it makes no sense to use its time */
84 if (regs[0] & PCF85063_REG_SC_OS) {
Alexandre Bellonie89b60d2019-04-01 18:08:10 +020085 dev_warn(&pcf85063->rtc->dev, "Power loss detected, invalid time\n");
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010086 return -EINVAL;
87 }
88
Juergen Borleis7b576842016-02-09 11:57:25 +010089 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
90 tm->tm_min = bcd2bin(regs[1] & 0x7F);
91 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
92 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
93 tm->tm_wday = regs[4] & 0x07;
94 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
95 tm->tm_year = bcd2bin(regs[6]);
Alexandre Bellonic421ce72016-07-18 11:08:59 +020096 tm->tm_year += 100;
Søren Andersen796b7ab2014-08-08 14:20:22 -070097
Alexandre Belloni0a6b8882018-02-21 16:07:34 +010098 return 0;
Søren Andersen796b7ab2014-08-08 14:20:22 -070099}
100
Alexandre Belloni965271d2018-02-21 16:09:27 +0100101static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
Søren Andersen796b7ab2014-08-08 14:20:22 -0700102{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200103 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
Juergen Borleis31d4d332016-02-09 11:57:27 +0100104 int rc;
Chris DeBruin0d981f82016-07-12 17:15:46 -0400105 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -0700106
Juergen Borleis31d4d332016-02-09 11:57:27 +0100107 /*
108 * to accurately set the time, reset the divider chain and keep it in
109 * reset state until all time/date registers are written
110 */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200111 rc = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
112 PCF85063_REG_CTRL1_STOP,
113 PCF85063_REG_CTRL1_STOP);
114 if (rc)
Juergen Borleis31d4d332016-02-09 11:57:27 +0100115 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700116
117 /* hours, minutes and seconds */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100118 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
Søren Andersen796b7ab2014-08-08 14:20:22 -0700119
Juergen Borleis31d4d332016-02-09 11:57:27 +0100120 regs[1] = bin2bcd(tm->tm_min);
121 regs[2] = bin2bcd(tm->tm_hour);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700122
123 /* Day of month, 1 - 31 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100124 regs[3] = bin2bcd(tm->tm_mday);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700125
126 /* Day, 0 - 6 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100127 regs[4] = tm->tm_wday & 0x07;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700128
129 /* month, 1 - 12 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100130 regs[5] = bin2bcd(tm->tm_mon + 1);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700131
132 /* year and century */
Alexandre Bellonic421ce72016-07-18 11:08:59 +0200133 regs[6] = bin2bcd(tm->tm_year - 100);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700134
Juergen Borleis31d4d332016-02-09 11:57:27 +0100135 /* write all registers at once */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200136 rc = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_SC,
137 regs, sizeof(regs));
138 if (rc)
Juergen Borleis31d4d332016-02-09 11:57:27 +0100139 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700140
Chris DeBruin0d981f82016-07-12 17:15:46 -0400141 /*
142 * Write the control register as a separate action since the size of
143 * the register space is different between the PCF85063TP and
144 * PCF85063A devices. The rollover point can not be used.
145 */
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200146 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
147 PCF85063_REG_CTRL1_STOP, 0);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700148}
149
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200150static int pcf85063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
151{
152 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
153 u8 buf[4];
154 unsigned int val;
155 int ret;
156
157 ret = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_ALM_S,
158 buf, sizeof(buf));
159 if (ret)
160 return ret;
161
162 alrm->time.tm_sec = bcd2bin(buf[0]);
163 alrm->time.tm_min = bcd2bin(buf[1]);
164 alrm->time.tm_hour = bcd2bin(buf[2]);
165 alrm->time.tm_mday = bcd2bin(buf[3]);
166
167 ret = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL2, &val);
168 if (ret)
169 return ret;
170
171 alrm->enabled = !!(val & PCF85063_CTRL2_AIE);
172
173 return 0;
174}
175
176static int pcf85063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
177{
178 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
179 u8 buf[5];
180 int ret;
181
182 buf[0] = bin2bcd(alrm->time.tm_sec);
183 buf[1] = bin2bcd(alrm->time.tm_min);
184 buf[2] = bin2bcd(alrm->time.tm_hour);
185 buf[3] = bin2bcd(alrm->time.tm_mday);
186 buf[4] = PCF85063_AEN; /* Do not match on week day */
187
188 ret = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
189 PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF, 0);
190 if (ret)
191 return ret;
192
193 ret = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_ALM_S,
194 buf, sizeof(buf));
195 if (ret)
196 return ret;
197
198 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
199 PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF,
200 alrm->enabled ? PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF : PCF85063_CTRL2_AF);
201}
202
203static int pcf85063_rtc_alarm_irq_enable(struct device *dev,
204 unsigned int enabled)
205{
206 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
207
208 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
209 PCF85063_CTRL2_AIE,
210 enabled ? PCF85063_CTRL2_AIE : 0);
211}
212
213static irqreturn_t pcf85063_rtc_handle_irq(int irq, void *dev_id)
214{
215 struct pcf85063 *pcf85063 = dev_id;
216 unsigned int val;
217 int err;
218
219 err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL2, &val);
220 if (err)
221 return IRQ_NONE;
222
223 if (val & PCF85063_CTRL2_AF) {
224 rtc_update_irq(pcf85063->rtc, 1, RTC_IRQF | RTC_AF);
225 regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2,
226 PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF,
227 0);
228 return IRQ_HANDLED;
229 }
230
231 return IRQ_NONE;
232}
233
Alexandre Belloni85370d32019-04-01 18:08:15 +0200234static int pcf85063_read_offset(struct device *dev, long *offset)
235{
236 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
237 long val;
238 u32 reg;
239 int ret;
240
241 ret = regmap_read(pcf85063->regmap, PCF85063_REG_OFFSET, &reg);
242 if (ret < 0)
243 return ret;
244
245 val = sign_extend32(reg & ~PCF85063_OFFSET_MODE,
246 PCF85063_OFFSET_SIGN_BIT);
247
248 if (reg & PCF85063_OFFSET_MODE)
249 *offset = val * PCF85063_OFFSET_STEP1;
250 else
251 *offset = val * PCF85063_OFFSET_STEP0;
252
253 return 0;
254}
255
256static int pcf85063_set_offset(struct device *dev, long offset)
257{
258 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
259 s8 mode0, mode1, reg;
260 unsigned int error0, error1;
261
262 if (offset > PCF85063_OFFSET_STEP0 * 63)
263 return -ERANGE;
264 if (offset < PCF85063_OFFSET_STEP0 * -64)
265 return -ERANGE;
266
267 mode0 = DIV_ROUND_CLOSEST(offset, PCF85063_OFFSET_STEP0);
268 mode1 = DIV_ROUND_CLOSEST(offset, PCF85063_OFFSET_STEP1);
269
270 error0 = abs(offset - (mode0 * PCF85063_OFFSET_STEP0));
271 error1 = abs(offset - (mode1 * PCF85063_OFFSET_STEP1));
272 if (mode1 > 63 || mode1 < -64 || error0 < error1)
273 reg = mode0 & ~PCF85063_OFFSET_MODE;
274 else
275 reg = mode1 | PCF85063_OFFSET_MODE;
276
277 return regmap_write(pcf85063->regmap, PCF85063_REG_OFFSET, reg);
278}
279
Alexandre Belloni27ff8362019-04-01 18:08:16 +0200280static int pcf85063_ioctl(struct device *dev, unsigned int cmd,
281 unsigned long arg)
282{
283 struct pcf85063 *pcf85063 = dev_get_drvdata(dev);
284 int status, ret = 0;
285
286 switch (cmd) {
287 case RTC_VL_READ:
288 ret = regmap_read(pcf85063->regmap, PCF85063_REG_SC, &status);
289 if (ret < 0)
290 return ret;
291
292 if (status & PCF85063_REG_SC_OS)
293 dev_warn(&pcf85063->rtc->dev, "Voltage low, data loss detected.\n");
294
295 status &= PCF85063_REG_SC_OS;
296
297 if (copy_to_user((void __user *)arg, &status, sizeof(int)))
298 return -EFAULT;
299
300 return 0;
301
302 case RTC_VL_CLR:
303 ret = regmap_update_bits(pcf85063->regmap, PCF85063_REG_SC,
304 PCF85063_REG_SC_OS, 0);
305
306 return ret;
307
308 default:
309 return -ENOIOCTLCMD;
310 }
311}
312
Søren Andersen796b7ab2014-08-08 14:20:22 -0700313static const struct rtc_class_ops pcf85063_rtc_ops = {
314 .read_time = pcf85063_rtc_read_time,
Alexandre Belloni85370d32019-04-01 18:08:15 +0200315 .set_time = pcf85063_rtc_set_time,
316 .read_offset = pcf85063_read_offset,
317 .set_offset = pcf85063_set_offset,
Alexandre Belloni27ff8362019-04-01 18:08:16 +0200318 .ioctl = pcf85063_ioctl,
Søren Andersen796b7ab2014-08-08 14:20:22 -0700319};
320
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200321static const struct rtc_class_ops pcf85063_rtc_ops_alarm = {
322 .read_time = pcf85063_rtc_read_time,
323 .set_time = pcf85063_rtc_set_time,
Alexandre Belloni85370d32019-04-01 18:08:15 +0200324 .read_offset = pcf85063_read_offset,
325 .set_offset = pcf85063_set_offset,
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200326 .read_alarm = pcf85063_rtc_read_alarm,
327 .set_alarm = pcf85063_rtc_set_alarm,
328 .alarm_irq_enable = pcf85063_rtc_alarm_irq_enable,
Alexandre Belloni27ff8362019-04-01 18:08:16 +0200329 .ioctl = pcf85063_ioctl,
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200330};
331
Alexandre Bellonifadfd092019-04-01 18:08:14 +0200332static int pcf85063_nvmem_read(void *priv, unsigned int offset,
333 void *val, size_t bytes)
334{
335 return regmap_read(priv, PCF85063_REG_RAM, val);
336}
337
338static int pcf85063_nvmem_write(void *priv, unsigned int offset,
339 void *val, size_t bytes)
340{
341 return regmap_write(priv, PCF85063_REG_RAM, *(u8 *)val);
342}
343
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200344static int pcf85063_load_capacitance(struct pcf85063 *pcf85063,
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +0200345 const struct device_node *np,
346 unsigned int force_cap)
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100347{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200348 u32 load = 7000;
349 u8 reg = 0;
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100350
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +0200351 if (force_cap)
352 load = force_cap;
353 else
354 of_property_read_u32(np, "quartz-load-femtofarads", &load);
355
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100356 switch (load) {
357 default:
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200358 dev_warn(&pcf85063->rtc->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100359 load);
360 /* fall through */
361 case 7000:
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100362 break;
363 case 12500:
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200364 reg = PCF85063_REG_CTRL1_CAP_SEL;
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100365 break;
366 }
367
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200368 return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1,
369 PCF85063_REG_CTRL1_CAP_SEL, reg);
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100370}
371
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200372static const struct pcf85063_config pcf85063a_config = {
373 .regmap = {
374 .reg_bits = 8,
375 .val_bits = 8,
376 .max_register = 0x11,
377 },
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200378 .has_alarms = 1,
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200379};
380
381static const struct pcf85063_config pcf85063tp_config = {
382 .regmap = {
383 .reg_bits = 8,
384 .val_bits = 8,
385 .max_register = 0x0a,
386 },
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200387};
388
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +0200389static const struct pcf85063_config rv8263_config = {
390 .regmap = {
391 .reg_bits = 8,
392 .val_bits = 8,
393 .max_register = 0x11,
394 },
395 .has_alarms = 1,
396 .force_cap_7000 = 1,
397};
398
Alexandre Belloni0f217002019-04-01 18:08:05 +0200399static int pcf85063_probe(struct i2c_client *client)
Søren Andersen796b7ab2014-08-08 14:20:22 -0700400{
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200401 struct pcf85063 *pcf85063;
402 unsigned int tmp;
Mirza Krakc18b4c52016-10-17 15:53:31 +0200403 int err;
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200404 const struct pcf85063_config *config = &pcf85063tp_config;
405 const void *data = of_device_get_match_data(&client->dev);
Alexandre Bellonifadfd092019-04-01 18:08:14 +0200406 struct nvmem_config nvmem_cfg = {
407 .name = "pcf85063_nvram",
408 .reg_read = pcf85063_nvmem_read,
409 .reg_write = pcf85063_nvmem_write,
410 .type = NVMEM_TYPE_BATTERY_BACKED,
411 .size = 1,
412 };
Søren Andersen796b7ab2014-08-08 14:20:22 -0700413
414 dev_dbg(&client->dev, "%s\n", __func__);
415
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200416 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
417 GFP_KERNEL);
418 if (!pcf85063)
419 return -ENOMEM;
420
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200421 if (data)
422 config = data;
423
424 pcf85063->regmap = devm_regmap_init_i2c(client, &config->regmap);
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200425 if (IS_ERR(pcf85063->regmap))
426 return PTR_ERR(pcf85063->regmap);
427
428 i2c_set_clientdata(client, pcf85063);
429
430 err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL1, &tmp);
431 if (err) {
Mirza Krakc18b4c52016-10-17 15:53:31 +0200432 dev_err(&client->dev, "RTC chip is not present\n");
433 return err;
434 }
435
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200436 pcf85063->rtc = devm_rtc_allocate_device(&client->dev);
437 if (IS_ERR(pcf85063->rtc))
438 return PTR_ERR(pcf85063->rtc);
439
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +0200440 err = pcf85063_load_capacitance(pcf85063, client->dev.of_node,
441 config->force_cap_7000 ? 7000 : 0);
Sam Ravnborgbbb43832019-01-19 10:00:31 +0100442 if (err < 0)
443 dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
444 err);
445
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200446 pcf85063->rtc->ops = &pcf85063_rtc_ops;
447 pcf85063->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
448 pcf85063->rtc->range_max = RTC_TIMESTAMP_END_2099;
Alexandre Belloni05cb3a52019-04-01 18:08:12 +0200449 pcf85063->rtc->uie_unsupported = 1;
450
451 if (config->has_alarms && client->irq > 0) {
452 err = devm_request_threaded_irq(&client->dev, client->irq,
453 NULL, pcf85063_rtc_handle_irq,
454 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
455 "pcf85063", pcf85063);
456 if (err) {
457 dev_warn(&pcf85063->rtc->dev,
458 "unable to request IRQ, alarms disabled\n");
459 } else {
460 pcf85063->rtc->ops = &pcf85063_rtc_ops_alarm;
461 device_init_wakeup(&client->dev, true);
462 err = dev_pm_set_wake_irq(&client->dev, client->irq);
463 if (err)
464 dev_err(&pcf85063->rtc->dev,
465 "failed to enable irq wake\n");
466 }
467 }
Søren Andersen796b7ab2014-08-08 14:20:22 -0700468
Alexandre Bellonifadfd092019-04-01 18:08:14 +0200469 nvmem_cfg.priv = pcf85063->regmap;
470 rtc_nvmem_register(pcf85063->rtc, &nvmem_cfg);
471
Alexandre Bellonie89b60d2019-04-01 18:08:10 +0200472 return rtc_register_device(pcf85063->rtc);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700473}
474
Søren Andersen796b7ab2014-08-08 14:20:22 -0700475#ifdef CONFIG_OF
476static const struct of_device_id pcf85063_of_match[] = {
Alexandre Belloni0e2e8772019-04-01 18:08:11 +0200477 { .compatible = "nxp,pcf85063", .data = &pcf85063tp_config },
478 { .compatible = "nxp,pcf85063tp", .data = &pcf85063tp_config },
479 { .compatible = "nxp,pcf85063a", .data = &pcf85063a_config },
Alexandre Belloni5b3a3ad2019-04-01 18:08:13 +0200480 { .compatible = "microcrystal,rv8263", .data = &rv8263_config },
Søren Andersen796b7ab2014-08-08 14:20:22 -0700481 {}
482};
483MODULE_DEVICE_TABLE(of, pcf85063_of_match);
484#endif
485
486static struct i2c_driver pcf85063_driver = {
487 .driver = {
488 .name = "rtc-pcf85063",
Søren Andersen796b7ab2014-08-08 14:20:22 -0700489 .of_match_table = of_match_ptr(pcf85063_of_match),
490 },
Alexandre Belloni0f217002019-04-01 18:08:05 +0200491 .probe_new = pcf85063_probe,
Søren Andersen796b7ab2014-08-08 14:20:22 -0700492};
493
494module_i2c_driver(pcf85063_driver);
495
496MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
497MODULE_DESCRIPTION("PCF85063 RTC driver");
498MODULE_LICENSE("GPL");