xref: /linux/drivers/rtc/rtc-pcf85363.c (revision dd7166c8ba6e27568ba1c3fa8874316150ce694c)
156573ca7SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
2a9687aa2SEric Nelson /*
3a9687aa2SEric Nelson  * drivers/rtc/rtc-pcf85363.c
4a9687aa2SEric Nelson  *
5a9687aa2SEric Nelson  * Driver for NXP PCF85363 real-time clock.
6a9687aa2SEric Nelson  *
7a9687aa2SEric Nelson  * Copyright (C) 2017 Eric Nelson
8a9687aa2SEric Nelson  */
9a9687aa2SEric Nelson #include <linux/module.h>
10a9687aa2SEric Nelson #include <linux/i2c.h>
11a9687aa2SEric Nelson #include <linux/slab.h>
12a9687aa2SEric Nelson #include <linux/rtc.h>
13a9687aa2SEric Nelson #include <linux/init.h>
14a9687aa2SEric Nelson #include <linux/err.h>
15a9687aa2SEric Nelson #include <linux/errno.h>
16a9687aa2SEric Nelson #include <linux/bcd.h>
17a9687aa2SEric Nelson #include <linux/of.h>
18a9687aa2SEric Nelson #include <linux/of_device.h>
19a9687aa2SEric Nelson #include <linux/regmap.h>
20a9687aa2SEric Nelson 
21a9687aa2SEric Nelson /*
22a9687aa2SEric Nelson  * Date/Time registers
23a9687aa2SEric Nelson  */
24a9687aa2SEric Nelson #define DT_100THS	0x00
25a9687aa2SEric Nelson #define DT_SECS		0x01
26a9687aa2SEric Nelson #define DT_MINUTES	0x02
27a9687aa2SEric Nelson #define DT_HOURS	0x03
28a9687aa2SEric Nelson #define DT_DAYS		0x04
29a9687aa2SEric Nelson #define DT_WEEKDAYS	0x05
30a9687aa2SEric Nelson #define DT_MONTHS	0x06
31a9687aa2SEric Nelson #define DT_YEARS	0x07
32a9687aa2SEric Nelson 
33a9687aa2SEric Nelson /*
34a9687aa2SEric Nelson  * Alarm registers
35a9687aa2SEric Nelson  */
36a9687aa2SEric Nelson #define DT_SECOND_ALM1	0x08
37a9687aa2SEric Nelson #define DT_MINUTE_ALM1	0x09
38a9687aa2SEric Nelson #define DT_HOUR_ALM1	0x0a
39a9687aa2SEric Nelson #define DT_DAY_ALM1	0x0b
40a9687aa2SEric Nelson #define DT_MONTH_ALM1	0x0c
41a9687aa2SEric Nelson #define DT_MINUTE_ALM2	0x0d
42a9687aa2SEric Nelson #define DT_HOUR_ALM2	0x0e
43a9687aa2SEric Nelson #define DT_WEEKDAY_ALM2	0x0f
44a9687aa2SEric Nelson #define DT_ALARM_EN	0x10
45a9687aa2SEric Nelson 
46a9687aa2SEric Nelson /*
47a9687aa2SEric Nelson  * Time stamp registers
48a9687aa2SEric Nelson  */
49a9687aa2SEric Nelson #define DT_TIMESTAMP1	0x11
50a9687aa2SEric Nelson #define DT_TIMESTAMP2	0x17
51a9687aa2SEric Nelson #define DT_TIMESTAMP3	0x1d
52a9687aa2SEric Nelson #define DT_TS_MODE	0x23
53a9687aa2SEric Nelson 
54a9687aa2SEric Nelson /*
55a9687aa2SEric Nelson  * control registers
56a9687aa2SEric Nelson  */
57a9687aa2SEric Nelson #define CTRL_OFFSET	0x24
58a9687aa2SEric Nelson #define CTRL_OSCILLATOR	0x25
59a9687aa2SEric Nelson #define CTRL_BATTERY	0x26
60a9687aa2SEric Nelson #define CTRL_PIN_IO	0x27
61a9687aa2SEric Nelson #define CTRL_FUNCTION	0x28
62a9687aa2SEric Nelson #define CTRL_INTA_EN	0x29
63a9687aa2SEric Nelson #define CTRL_INTB_EN	0x2a
64a9687aa2SEric Nelson #define CTRL_FLAGS	0x2b
65a9687aa2SEric Nelson #define CTRL_RAMBYTE	0x2c
66a9687aa2SEric Nelson #define CTRL_WDOG	0x2d
67a9687aa2SEric Nelson #define CTRL_STOP_EN	0x2e
68a9687aa2SEric Nelson #define CTRL_RESETS	0x2f
69a9687aa2SEric Nelson #define CTRL_RAM	0x40
70a9687aa2SEric Nelson 
71e5aac267SAlexandre Belloni #define ALRM_SEC_A1E	BIT(0)
72e5aac267SAlexandre Belloni #define ALRM_MIN_A1E	BIT(1)
73e5aac267SAlexandre Belloni #define ALRM_HR_A1E	BIT(2)
74e5aac267SAlexandre Belloni #define ALRM_DAY_A1E	BIT(3)
75e5aac267SAlexandre Belloni #define ALRM_MON_A1E	BIT(4)
76e5aac267SAlexandre Belloni #define ALRM_MIN_A2E	BIT(5)
77e5aac267SAlexandre Belloni #define ALRM_HR_A2E	BIT(6)
78e5aac267SAlexandre Belloni #define ALRM_DAY_A2E	BIT(7)
79e5aac267SAlexandre Belloni 
80e5aac267SAlexandre Belloni #define INT_WDIE	BIT(0)
81e5aac267SAlexandre Belloni #define INT_BSIE	BIT(1)
82e5aac267SAlexandre Belloni #define INT_TSRIE	BIT(2)
83e5aac267SAlexandre Belloni #define INT_A2IE	BIT(3)
84e5aac267SAlexandre Belloni #define INT_A1IE	BIT(4)
85e5aac267SAlexandre Belloni #define INT_OIE		BIT(5)
86e5aac267SAlexandre Belloni #define INT_PIE		BIT(6)
87e5aac267SAlexandre Belloni #define INT_ILP		BIT(7)
88e5aac267SAlexandre Belloni 
89e5aac267SAlexandre Belloni #define FLAGS_TSR1F	BIT(0)
90e5aac267SAlexandre Belloni #define FLAGS_TSR2F	BIT(1)
91e5aac267SAlexandre Belloni #define FLAGS_TSR3F	BIT(2)
92e5aac267SAlexandre Belloni #define FLAGS_BSF	BIT(3)
93e5aac267SAlexandre Belloni #define FLAGS_WDF	BIT(4)
94e5aac267SAlexandre Belloni #define FLAGS_A1F	BIT(5)
95e5aac267SAlexandre Belloni #define FLAGS_A2F	BIT(6)
96e5aac267SAlexandre Belloni #define FLAGS_PIF	BIT(7)
97e5aac267SAlexandre Belloni 
98e5aac267SAlexandre Belloni #define PIN_IO_INTAPM	GENMASK(1, 0)
99e5aac267SAlexandre Belloni #define PIN_IO_INTA_CLK	0
100e5aac267SAlexandre Belloni #define PIN_IO_INTA_BAT	1
101e5aac267SAlexandre Belloni #define PIN_IO_INTA_OUT	2
102e5aac267SAlexandre Belloni #define PIN_IO_INTA_HIZ	3
103e5aac267SAlexandre Belloni 
104188306acSAlexandre Belloni #define STOP_EN_STOP	BIT(0)
105188306acSAlexandre Belloni 
106188306acSAlexandre Belloni #define RESET_CPR	0xa4
107188306acSAlexandre Belloni 
108a9687aa2SEric Nelson #define NVRAM_SIZE	0x40
109a9687aa2SEric Nelson 
110a9687aa2SEric Nelson struct pcf85363 {
111a9687aa2SEric Nelson 	struct rtc_device	*rtc;
112a9687aa2SEric Nelson 	struct regmap		*regmap;
113a9687aa2SEric Nelson };
114a9687aa2SEric Nelson 
115fc979933SBiju Das struct pcf85x63_config {
116fc979933SBiju Das 	struct regmap_config regmap;
117fc979933SBiju Das 	unsigned int num_nvram;
118fc979933SBiju Das };
119fc979933SBiju Das 
120a9687aa2SEric Nelson static int pcf85363_rtc_read_time(struct device *dev, struct rtc_time *tm)
121a9687aa2SEric Nelson {
122a9687aa2SEric Nelson 	struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
123a9687aa2SEric Nelson 	unsigned char buf[DT_YEARS + 1];
124a9687aa2SEric Nelson 	int ret, len = sizeof(buf);
125a9687aa2SEric Nelson 
126a9687aa2SEric Nelson 	/* read the RTC date and time registers all at once */
127a9687aa2SEric Nelson 	ret = regmap_bulk_read(pcf85363->regmap, DT_100THS, buf, len);
128a9687aa2SEric Nelson 	if (ret) {
129a9687aa2SEric Nelson 		dev_err(dev, "%s: error %d\n", __func__, ret);
130a9687aa2SEric Nelson 		return ret;
131a9687aa2SEric Nelson 	}
132a9687aa2SEric Nelson 
133a9687aa2SEric Nelson 	tm->tm_year = bcd2bin(buf[DT_YEARS]);
134a9687aa2SEric Nelson 	/* adjust for 1900 base of rtc_time */
135a9687aa2SEric Nelson 	tm->tm_year += 100;
136a9687aa2SEric Nelson 
137a9687aa2SEric Nelson 	tm->tm_wday = buf[DT_WEEKDAYS] & 7;
138a9687aa2SEric Nelson 	buf[DT_SECS] &= 0x7F;
139a9687aa2SEric Nelson 	tm->tm_sec = bcd2bin(buf[DT_SECS]);
140a9687aa2SEric Nelson 	buf[DT_MINUTES] &= 0x7F;
141a9687aa2SEric Nelson 	tm->tm_min = bcd2bin(buf[DT_MINUTES]);
142a9687aa2SEric Nelson 	tm->tm_hour = bcd2bin(buf[DT_HOURS]);
143a9687aa2SEric Nelson 	tm->tm_mday = bcd2bin(buf[DT_DAYS]);
144a9687aa2SEric Nelson 	tm->tm_mon = bcd2bin(buf[DT_MONTHS]) - 1;
145a9687aa2SEric Nelson 
146a9687aa2SEric Nelson 	return 0;
147a9687aa2SEric Nelson }
148a9687aa2SEric Nelson 
149a9687aa2SEric Nelson static int pcf85363_rtc_set_time(struct device *dev, struct rtc_time *tm)
150a9687aa2SEric Nelson {
151a9687aa2SEric Nelson 	struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
152188306acSAlexandre Belloni 	unsigned char tmp[11];
153188306acSAlexandre Belloni 	unsigned char *buf = &tmp[2];
154188306acSAlexandre Belloni 	int ret;
155188306acSAlexandre Belloni 
156188306acSAlexandre Belloni 	tmp[0] = STOP_EN_STOP;
157188306acSAlexandre Belloni 	tmp[1] = RESET_CPR;
158a9687aa2SEric Nelson 
159a9687aa2SEric Nelson 	buf[DT_100THS] = 0;
160a9687aa2SEric Nelson 	buf[DT_SECS] = bin2bcd(tm->tm_sec);
161a9687aa2SEric Nelson 	buf[DT_MINUTES] = bin2bcd(tm->tm_min);
162a9687aa2SEric Nelson 	buf[DT_HOURS] = bin2bcd(tm->tm_hour);
163a9687aa2SEric Nelson 	buf[DT_DAYS] = bin2bcd(tm->tm_mday);
164a9687aa2SEric Nelson 	buf[DT_WEEKDAYS] = tm->tm_wday;
165a9687aa2SEric Nelson 	buf[DT_MONTHS] = bin2bcd(tm->tm_mon + 1);
166a9687aa2SEric Nelson 	buf[DT_YEARS] = bin2bcd(tm->tm_year % 100);
167a9687aa2SEric Nelson 
168188306acSAlexandre Belloni 	ret = regmap_bulk_write(pcf85363->regmap, CTRL_STOP_EN,
1697ef66122SBiwen Li 				tmp, 2);
1707ef66122SBiwen Li 	if (ret)
1717ef66122SBiwen Li 		return ret;
1727ef66122SBiwen Li 
1737ef66122SBiwen Li 	ret = regmap_bulk_write(pcf85363->regmap, DT_100THS,
1747ef66122SBiwen Li 				buf, sizeof(tmp) - 2);
175188306acSAlexandre Belloni 	if (ret)
176188306acSAlexandre Belloni 		return ret;
177188306acSAlexandre Belloni 
178188306acSAlexandre Belloni 	return regmap_write(pcf85363->regmap, CTRL_STOP_EN, 0);
179a9687aa2SEric Nelson }
180a9687aa2SEric Nelson 
181e5aac267SAlexandre Belloni static int pcf85363_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
182e5aac267SAlexandre Belloni {
183e5aac267SAlexandre Belloni 	struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
184e5aac267SAlexandre Belloni 	unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1];
185e5aac267SAlexandre Belloni 	unsigned int val;
186e5aac267SAlexandre Belloni 	int ret;
187e5aac267SAlexandre Belloni 
188e5aac267SAlexandre Belloni 	ret = regmap_bulk_read(pcf85363->regmap, DT_SECOND_ALM1, buf,
189e5aac267SAlexandre Belloni 			       sizeof(buf));
190e5aac267SAlexandre Belloni 	if (ret)
191e5aac267SAlexandre Belloni 		return ret;
192e5aac267SAlexandre Belloni 
193e5aac267SAlexandre Belloni 	alrm->time.tm_sec = bcd2bin(buf[0]);
194e5aac267SAlexandre Belloni 	alrm->time.tm_min = bcd2bin(buf[1]);
195e5aac267SAlexandre Belloni 	alrm->time.tm_hour = bcd2bin(buf[2]);
196e5aac267SAlexandre Belloni 	alrm->time.tm_mday = bcd2bin(buf[3]);
197e5aac267SAlexandre Belloni 	alrm->time.tm_mon = bcd2bin(buf[4]) - 1;
198e5aac267SAlexandre Belloni 
199e5aac267SAlexandre Belloni 	ret = regmap_read(pcf85363->regmap, CTRL_INTA_EN, &val);
200e5aac267SAlexandre Belloni 	if (ret)
201e5aac267SAlexandre Belloni 		return ret;
202e5aac267SAlexandre Belloni 
203e5aac267SAlexandre Belloni 	alrm->enabled =  !!(val & INT_A1IE);
204e5aac267SAlexandre Belloni 
205e5aac267SAlexandre Belloni 	return 0;
206e5aac267SAlexandre Belloni }
207e5aac267SAlexandre Belloni 
208e5aac267SAlexandre Belloni static int _pcf85363_rtc_alarm_irq_enable(struct pcf85363 *pcf85363, unsigned
209e5aac267SAlexandre Belloni 					  int enabled)
210e5aac267SAlexandre Belloni {
211e5aac267SAlexandre Belloni 	unsigned int alarm_flags = ALRM_SEC_A1E | ALRM_MIN_A1E | ALRM_HR_A1E |
212e5aac267SAlexandre Belloni 				   ALRM_DAY_A1E | ALRM_MON_A1E;
213e5aac267SAlexandre Belloni 	int ret;
214e5aac267SAlexandre Belloni 
215e5aac267SAlexandre Belloni 	ret = regmap_update_bits(pcf85363->regmap, DT_ALARM_EN, alarm_flags,
216e5aac267SAlexandre Belloni 				 enabled ? alarm_flags : 0);
217e5aac267SAlexandre Belloni 	if (ret)
218e5aac267SAlexandre Belloni 		return ret;
219e5aac267SAlexandre Belloni 
220e5aac267SAlexandre Belloni 	ret = regmap_update_bits(pcf85363->regmap, CTRL_INTA_EN,
221e5aac267SAlexandre Belloni 				 INT_A1IE, enabled ? INT_A1IE : 0);
222e5aac267SAlexandre Belloni 
223e5aac267SAlexandre Belloni 	if (ret || enabled)
224e5aac267SAlexandre Belloni 		return ret;
225e5aac267SAlexandre Belloni 
226e5aac267SAlexandre Belloni 	/* clear current flags */
227e5aac267SAlexandre Belloni 	return regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0);
228e5aac267SAlexandre Belloni }
229e5aac267SAlexandre Belloni 
230e5aac267SAlexandre Belloni static int pcf85363_rtc_alarm_irq_enable(struct device *dev,
231e5aac267SAlexandre Belloni 					 unsigned int enabled)
232e5aac267SAlexandre Belloni {
233e5aac267SAlexandre Belloni 	struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
234e5aac267SAlexandre Belloni 
235e5aac267SAlexandre Belloni 	return _pcf85363_rtc_alarm_irq_enable(pcf85363, enabled);
236e5aac267SAlexandre Belloni }
237e5aac267SAlexandre Belloni 
238e5aac267SAlexandre Belloni static int pcf85363_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
239e5aac267SAlexandre Belloni {
240e5aac267SAlexandre Belloni 	struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
241e5aac267SAlexandre Belloni 	unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1];
242e5aac267SAlexandre Belloni 	int ret;
243e5aac267SAlexandre Belloni 
244e5aac267SAlexandre Belloni 	buf[0] = bin2bcd(alrm->time.tm_sec);
245e5aac267SAlexandre Belloni 	buf[1] = bin2bcd(alrm->time.tm_min);
246e5aac267SAlexandre Belloni 	buf[2] = bin2bcd(alrm->time.tm_hour);
247e5aac267SAlexandre Belloni 	buf[3] = bin2bcd(alrm->time.tm_mday);
248e5aac267SAlexandre Belloni 	buf[4] = bin2bcd(alrm->time.tm_mon + 1);
249e5aac267SAlexandre Belloni 
250e5aac267SAlexandre Belloni 	/*
251e5aac267SAlexandre Belloni 	 * Disable the alarm interrupt before changing the value to avoid
252e5aac267SAlexandre Belloni 	 * spurious interrupts
253e5aac267SAlexandre Belloni 	 */
254e5aac267SAlexandre Belloni 	ret = _pcf85363_rtc_alarm_irq_enable(pcf85363, 0);
255e5aac267SAlexandre Belloni 	if (ret)
256e5aac267SAlexandre Belloni 		return ret;
257e5aac267SAlexandre Belloni 
258e5aac267SAlexandre Belloni 	ret = regmap_bulk_write(pcf85363->regmap, DT_SECOND_ALM1, buf,
259e5aac267SAlexandre Belloni 				sizeof(buf));
260e5aac267SAlexandre Belloni 	if (ret)
261e5aac267SAlexandre Belloni 		return ret;
262e5aac267SAlexandre Belloni 
263e5aac267SAlexandre Belloni 	return _pcf85363_rtc_alarm_irq_enable(pcf85363, alrm->enabled);
264e5aac267SAlexandre Belloni }
265e5aac267SAlexandre Belloni 
266e5aac267SAlexandre Belloni static irqreturn_t pcf85363_rtc_handle_irq(int irq, void *dev_id)
267e5aac267SAlexandre Belloni {
268e5aac267SAlexandre Belloni 	struct pcf85363 *pcf85363 = i2c_get_clientdata(dev_id);
269e5aac267SAlexandre Belloni 	unsigned int flags;
270e5aac267SAlexandre Belloni 	int err;
271e5aac267SAlexandre Belloni 
272e5aac267SAlexandre Belloni 	err = regmap_read(pcf85363->regmap, CTRL_FLAGS, &flags);
273e5aac267SAlexandre Belloni 	if (err)
274e5aac267SAlexandre Belloni 		return IRQ_NONE;
275e5aac267SAlexandre Belloni 
276e5aac267SAlexandre Belloni 	if (flags & FLAGS_A1F) {
277e5aac267SAlexandre Belloni 		rtc_update_irq(pcf85363->rtc, 1, RTC_IRQF | RTC_AF);
278e5aac267SAlexandre Belloni 		regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0);
279e5aac267SAlexandre Belloni 		return IRQ_HANDLED;
280e5aac267SAlexandre Belloni 	}
281e5aac267SAlexandre Belloni 
282e5aac267SAlexandre Belloni 	return IRQ_NONE;
283e5aac267SAlexandre Belloni }
284e5aac267SAlexandre Belloni 
285a9687aa2SEric Nelson static const struct rtc_class_ops rtc_ops = {
286a9687aa2SEric Nelson 	.read_time	= pcf85363_rtc_read_time,
287a9687aa2SEric Nelson 	.set_time	= pcf85363_rtc_set_time,
288e5aac267SAlexandre Belloni 	.read_alarm	= pcf85363_rtc_read_alarm,
289e5aac267SAlexandre Belloni 	.set_alarm	= pcf85363_rtc_set_alarm,
290e5aac267SAlexandre Belloni 	.alarm_irq_enable = pcf85363_rtc_alarm_irq_enable,
291e5aac267SAlexandre Belloni };
292e5aac267SAlexandre Belloni 
293a9687aa2SEric Nelson static int pcf85363_nvram_read(void *priv, unsigned int offset, void *val,
294a9687aa2SEric Nelson 			       size_t bytes)
295a9687aa2SEric Nelson {
296a9687aa2SEric Nelson 	struct pcf85363 *pcf85363 = priv;
297a9687aa2SEric Nelson 
298a9687aa2SEric Nelson 	return regmap_bulk_read(pcf85363->regmap, CTRL_RAM + offset,
299a9687aa2SEric Nelson 				val, bytes);
300a9687aa2SEric Nelson }
301a9687aa2SEric Nelson 
302a9687aa2SEric Nelson static int pcf85363_nvram_write(void *priv, unsigned int offset, void *val,
303a9687aa2SEric Nelson 				size_t bytes)
304a9687aa2SEric Nelson {
305a9687aa2SEric Nelson 	struct pcf85363 *pcf85363 = priv;
306a9687aa2SEric Nelson 
307a9687aa2SEric Nelson 	return regmap_bulk_write(pcf85363->regmap, CTRL_RAM + offset,
308a9687aa2SEric Nelson 				 val, bytes);
309a9687aa2SEric Nelson }
310a9687aa2SEric Nelson 
311fc979933SBiju Das static int pcf85x63_nvram_read(void *priv, unsigned int offset, void *val,
312fc979933SBiju Das 			       size_t bytes)
313fc979933SBiju Das {
314fc979933SBiju Das 	struct pcf85363 *pcf85363 = priv;
315fc979933SBiju Das 	unsigned int tmp_val;
316fc979933SBiju Das 	int ret;
317fc979933SBiju Das 
318fc979933SBiju Das 	ret = regmap_read(pcf85363->regmap, CTRL_RAMBYTE, &tmp_val);
319fc979933SBiju Das 	(*(unsigned char *) val) = (unsigned char) tmp_val;
320fc979933SBiju Das 
321fc979933SBiju Das 	return ret;
322fc979933SBiju Das }
323fc979933SBiju Das 
324fc979933SBiju Das static int pcf85x63_nvram_write(void *priv, unsigned int offset, void *val,
325fc979933SBiju Das 				size_t bytes)
326fc979933SBiju Das {
327fc979933SBiju Das 	struct pcf85363 *pcf85363 = priv;
328fc979933SBiju Das 	unsigned char tmp_val;
329fc979933SBiju Das 
330fc979933SBiju Das 	tmp_val = *((unsigned char *)val);
331fc979933SBiju Das 	return regmap_write(pcf85363->regmap, CTRL_RAMBYTE,
332fc979933SBiju Das 				(unsigned int)tmp_val);
333fc979933SBiju Das }
334fc979933SBiju Das 
335fc979933SBiju Das static const struct pcf85x63_config pcf_85263_config = {
336fc979933SBiju Das 	.regmap = {
337fc979933SBiju Das 		.reg_bits = 8,
338fc979933SBiju Das 		.val_bits = 8,
339fc979933SBiju Das 		.max_register = 0x2f,
340fc979933SBiju Das 	},
341fc979933SBiju Das 	.num_nvram = 1
342fc979933SBiju Das };
343fc979933SBiju Das 
344fc979933SBiju Das static const struct pcf85x63_config pcf_85363_config = {
345fc979933SBiju Das 	.regmap = {
346a9687aa2SEric Nelson 		.reg_bits = 8,
347a9687aa2SEric Nelson 		.val_bits = 8,
348c57849ddSAlexandre Belloni 		.max_register = 0x7f,
349fc979933SBiju Das 	},
350fc979933SBiju Das 	.num_nvram = 2
351a9687aa2SEric Nelson };
352a9687aa2SEric Nelson 
3533f4a3322SStephen Kitt static int pcf85363_probe(struct i2c_client *client)
354a9687aa2SEric Nelson {
355a9687aa2SEric Nelson 	struct pcf85363 *pcf85363;
356fc979933SBiju Das 	const struct pcf85x63_config *config = &pcf_85363_config;
357fc979933SBiju Das 	const void *data = of_device_get_match_data(&client->dev);
358fc979933SBiju Das 	static struct nvmem_config nvmem_cfg[] = {
359fc979933SBiju Das 		{
360fc979933SBiju Das 			.name = "pcf85x63-",
361fc979933SBiju Das 			.word_size = 1,
362fc979933SBiju Das 			.stride = 1,
363fc979933SBiju Das 			.size = 1,
364fc979933SBiju Das 			.reg_read = pcf85x63_nvram_read,
365fc979933SBiju Das 			.reg_write = pcf85x63_nvram_write,
366fc979933SBiju Das 		}, {
3670e7a412fSAlexandre Belloni 			.name = "pcf85363-",
3680e7a412fSAlexandre Belloni 			.word_size = 1,
3690e7a412fSAlexandre Belloni 			.stride = 1,
3700e7a412fSAlexandre Belloni 			.size = NVRAM_SIZE,
3710e7a412fSAlexandre Belloni 			.reg_read = pcf85363_nvram_read,
3720e7a412fSAlexandre Belloni 			.reg_write = pcf85363_nvram_write,
373fc979933SBiju Das 		},
3740e7a412fSAlexandre Belloni 	};
375fc979933SBiju Das 	int ret, i;
376fc979933SBiju Das 
377fc979933SBiju Das 	if (data)
378fc979933SBiju Das 		config = data;
379a9687aa2SEric Nelson 
380a9687aa2SEric Nelson 	pcf85363 = devm_kzalloc(&client->dev, sizeof(struct pcf85363),
381a9687aa2SEric Nelson 				GFP_KERNEL);
382a9687aa2SEric Nelson 	if (!pcf85363)
383a9687aa2SEric Nelson 		return -ENOMEM;
384a9687aa2SEric Nelson 
385fc979933SBiju Das 	pcf85363->regmap = devm_regmap_init_i2c(client, &config->regmap);
386a9687aa2SEric Nelson 	if (IS_ERR(pcf85363->regmap)) {
387a9687aa2SEric Nelson 		dev_err(&client->dev, "regmap allocation failed\n");
388a9687aa2SEric Nelson 		return PTR_ERR(pcf85363->regmap);
389a9687aa2SEric Nelson 	}
390a9687aa2SEric Nelson 
391a9687aa2SEric Nelson 	i2c_set_clientdata(client, pcf85363);
392a9687aa2SEric Nelson 
3938f7b1d71SAlexandre Belloni 	pcf85363->rtc = devm_rtc_allocate_device(&client->dev);
394a9687aa2SEric Nelson 	if (IS_ERR(pcf85363->rtc))
395a9687aa2SEric Nelson 		return PTR_ERR(pcf85363->rtc);
396a9687aa2SEric Nelson 
397a9687aa2SEric Nelson 	pcf85363->rtc->ops = &rtc_ops;
398c0ec8319SAlexandre Belloni 	pcf85363->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
399c0ec8319SAlexandre Belloni 	pcf85363->rtc->range_max = RTC_TIMESTAMP_END_2099;
400732b7341SAlexandre Belloni 	clear_bit(RTC_FEATURE_ALARM, pcf85363->rtc->features);
401a9687aa2SEric Nelson 
402e5aac267SAlexandre Belloni 	if (client->irq > 0) {
403*dd7166c8SAlexandre Belloni 		unsigned long irqflags = IRQF_TRIGGER_LOW;
404*dd7166c8SAlexandre Belloni 
405*dd7166c8SAlexandre Belloni 		if (dev_fwnode(&client->dev))
406*dd7166c8SAlexandre Belloni 			irqflags = 0;
407*dd7166c8SAlexandre Belloni 
408e5aac267SAlexandre Belloni 		regmap_write(pcf85363->regmap, CTRL_FLAGS, 0);
409e5aac267SAlexandre Belloni 		regmap_update_bits(pcf85363->regmap, CTRL_PIN_IO,
410e5aac267SAlexandre Belloni 				   PIN_IO_INTA_OUT, PIN_IO_INTAPM);
4118f7b1d71SAlexandre Belloni 		ret = devm_request_threaded_irq(&client->dev, client->irq,
412e5aac267SAlexandre Belloni 						NULL, pcf85363_rtc_handle_irq,
413*dd7166c8SAlexandre Belloni 						irqflags | IRQF_ONESHOT,
414e5aac267SAlexandre Belloni 						"pcf85363", client);
415e5aac267SAlexandre Belloni 		if (ret)
416e5aac267SAlexandre Belloni 			dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
417e5aac267SAlexandre Belloni 		else
418732b7341SAlexandre Belloni 			set_bit(RTC_FEATURE_ALARM, pcf85363->rtc->features);
419e5aac267SAlexandre Belloni 	}
420e5aac267SAlexandre Belloni 
421fdcfd854SBartosz Golaszewski 	ret = devm_rtc_register_device(pcf85363->rtc);
42224849d17SAlexandre Belloni 
423fc979933SBiju Das 	for (i = 0; i < config->num_nvram; i++) {
424fc979933SBiju Das 		nvmem_cfg[i].priv = pcf85363;
4253a905c2dSBartosz Golaszewski 		devm_rtc_nvmem_register(pcf85363->rtc, &nvmem_cfg[i]);
426fc979933SBiju Das 	}
42724849d17SAlexandre Belloni 
42824849d17SAlexandre Belloni 	return ret;
429a9687aa2SEric Nelson }
430a9687aa2SEric Nelson 
431c506bc10SAlexandre Belloni static const __maybe_unused struct of_device_id dev_ids[] = {
432fc979933SBiju Das 	{ .compatible = "nxp,pcf85263", .data = &pcf_85263_config },
433fc979933SBiju Das 	{ .compatible = "nxp,pcf85363", .data = &pcf_85363_config },
434fc979933SBiju Das 	{ /* sentinel */ }
435a9687aa2SEric Nelson };
436a9687aa2SEric Nelson MODULE_DEVICE_TABLE(of, dev_ids);
437a9687aa2SEric Nelson 
438a9687aa2SEric Nelson static struct i2c_driver pcf85363_driver = {
439a9687aa2SEric Nelson 	.driver	= {
440a9687aa2SEric Nelson 		.name	= "pcf85363",
441a9687aa2SEric Nelson 		.of_match_table = of_match_ptr(dev_ids),
442a9687aa2SEric Nelson 	},
4433f4a3322SStephen Kitt 	.probe_new = pcf85363_probe,
444a9687aa2SEric Nelson };
445a9687aa2SEric Nelson 
446a9687aa2SEric Nelson module_i2c_driver(pcf85363_driver);
447a9687aa2SEric Nelson 
448a9687aa2SEric Nelson MODULE_AUTHOR("Eric Nelson");
449fc979933SBiju Das MODULE_DESCRIPTION("pcf85263/pcf85363 I2C RTC driver");
450a9687aa2SEric Nelson MODULE_LICENSE("GPL");
451