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/regmap.h>
19a9687aa2SEric Nelson
20a9687aa2SEric Nelson /*
21a9687aa2SEric Nelson * Date/Time registers
22a9687aa2SEric Nelson */
23a9687aa2SEric Nelson #define DT_100THS 0x00
24a9687aa2SEric Nelson #define DT_SECS 0x01
25a9687aa2SEric Nelson #define DT_MINUTES 0x02
26a9687aa2SEric Nelson #define DT_HOURS 0x03
27a9687aa2SEric Nelson #define DT_DAYS 0x04
28a9687aa2SEric Nelson #define DT_WEEKDAYS 0x05
29a9687aa2SEric Nelson #define DT_MONTHS 0x06
30a9687aa2SEric Nelson #define DT_YEARS 0x07
31a9687aa2SEric Nelson
32a9687aa2SEric Nelson /*
33a9687aa2SEric Nelson * Alarm registers
34a9687aa2SEric Nelson */
35a9687aa2SEric Nelson #define DT_SECOND_ALM1 0x08
36a9687aa2SEric Nelson #define DT_MINUTE_ALM1 0x09
37a9687aa2SEric Nelson #define DT_HOUR_ALM1 0x0a
38a9687aa2SEric Nelson #define DT_DAY_ALM1 0x0b
39a9687aa2SEric Nelson #define DT_MONTH_ALM1 0x0c
40a9687aa2SEric Nelson #define DT_MINUTE_ALM2 0x0d
41a9687aa2SEric Nelson #define DT_HOUR_ALM2 0x0e
42a9687aa2SEric Nelson #define DT_WEEKDAY_ALM2 0x0f
43a9687aa2SEric Nelson #define DT_ALARM_EN 0x10
44a9687aa2SEric Nelson
45a9687aa2SEric Nelson /*
46a9687aa2SEric Nelson * Time stamp registers
47a9687aa2SEric Nelson */
48a9687aa2SEric Nelson #define DT_TIMESTAMP1 0x11
49a9687aa2SEric Nelson #define DT_TIMESTAMP2 0x17
50a9687aa2SEric Nelson #define DT_TIMESTAMP3 0x1d
51a9687aa2SEric Nelson #define DT_TS_MODE 0x23
52a9687aa2SEric Nelson
53a9687aa2SEric Nelson /*
54a9687aa2SEric Nelson * control registers
55a9687aa2SEric Nelson */
56a9687aa2SEric Nelson #define CTRL_OFFSET 0x24
57a9687aa2SEric Nelson #define CTRL_OSCILLATOR 0x25
58a9687aa2SEric Nelson #define CTRL_BATTERY 0x26
59a9687aa2SEric Nelson #define CTRL_PIN_IO 0x27
60a9687aa2SEric Nelson #define CTRL_FUNCTION 0x28
61a9687aa2SEric Nelson #define CTRL_INTA_EN 0x29
62a9687aa2SEric Nelson #define CTRL_INTB_EN 0x2a
63a9687aa2SEric Nelson #define CTRL_FLAGS 0x2b
64a9687aa2SEric Nelson #define CTRL_RAMBYTE 0x2c
65a9687aa2SEric Nelson #define CTRL_WDOG 0x2d
66a9687aa2SEric Nelson #define CTRL_STOP_EN 0x2e
67a9687aa2SEric Nelson #define CTRL_RESETS 0x2f
68a9687aa2SEric Nelson #define CTRL_RAM 0x40
69a9687aa2SEric Nelson
70e5aac267SAlexandre Belloni #define ALRM_SEC_A1E BIT(0)
71e5aac267SAlexandre Belloni #define ALRM_MIN_A1E BIT(1)
72e5aac267SAlexandre Belloni #define ALRM_HR_A1E BIT(2)
73e5aac267SAlexandre Belloni #define ALRM_DAY_A1E BIT(3)
74e5aac267SAlexandre Belloni #define ALRM_MON_A1E BIT(4)
75e5aac267SAlexandre Belloni #define ALRM_MIN_A2E BIT(5)
76e5aac267SAlexandre Belloni #define ALRM_HR_A2E BIT(6)
77e5aac267SAlexandre Belloni #define ALRM_DAY_A2E BIT(7)
78e5aac267SAlexandre Belloni
79e5aac267SAlexandre Belloni #define INT_WDIE BIT(0)
80e5aac267SAlexandre Belloni #define INT_BSIE BIT(1)
81e5aac267SAlexandre Belloni #define INT_TSRIE BIT(2)
82e5aac267SAlexandre Belloni #define INT_A2IE BIT(3)
83e5aac267SAlexandre Belloni #define INT_A1IE BIT(4)
84e5aac267SAlexandre Belloni #define INT_OIE BIT(5)
85e5aac267SAlexandre Belloni #define INT_PIE BIT(6)
86e5aac267SAlexandre Belloni #define INT_ILP BIT(7)
87e5aac267SAlexandre Belloni
88e5aac267SAlexandre Belloni #define FLAGS_TSR1F BIT(0)
89e5aac267SAlexandre Belloni #define FLAGS_TSR2F BIT(1)
90e5aac267SAlexandre Belloni #define FLAGS_TSR3F BIT(2)
91e5aac267SAlexandre Belloni #define FLAGS_BSF BIT(3)
92e5aac267SAlexandre Belloni #define FLAGS_WDF BIT(4)
93e5aac267SAlexandre Belloni #define FLAGS_A1F BIT(5)
94e5aac267SAlexandre Belloni #define FLAGS_A2F BIT(6)
95e5aac267SAlexandre Belloni #define FLAGS_PIF BIT(7)
96e5aac267SAlexandre Belloni
97e5aac267SAlexandre Belloni #define PIN_IO_INTAPM GENMASK(1, 0)
98e5aac267SAlexandre Belloni #define PIN_IO_INTA_CLK 0
99e5aac267SAlexandre Belloni #define PIN_IO_INTA_BAT 1
100e5aac267SAlexandre Belloni #define PIN_IO_INTA_OUT 2
101e5aac267SAlexandre Belloni #define PIN_IO_INTA_HIZ 3
102e5aac267SAlexandre Belloni
103fd9a6a13SJavier Carrasco #define OSC_CAP_SEL GENMASK(1, 0)
104fd9a6a13SJavier Carrasco #define OSC_CAP_6000 0x01
105fd9a6a13SJavier Carrasco #define OSC_CAP_12500 0x02
106fd9a6a13SJavier Carrasco
107188306acSAlexandre Belloni #define STOP_EN_STOP BIT(0)
108188306acSAlexandre Belloni
109188306acSAlexandre Belloni #define RESET_CPR 0xa4
110188306acSAlexandre Belloni
111a9687aa2SEric Nelson #define NVRAM_SIZE 0x40
112a9687aa2SEric Nelson
113a9687aa2SEric Nelson struct pcf85363 {
114a9687aa2SEric Nelson struct rtc_device *rtc;
115a9687aa2SEric Nelson struct regmap *regmap;
116a9687aa2SEric Nelson };
117a9687aa2SEric Nelson
118fc979933SBiju Das struct pcf85x63_config {
119fc979933SBiju Das struct regmap_config regmap;
120fc979933SBiju Das unsigned int num_nvram;
121fc979933SBiju Das };
122fc979933SBiju Das
pcf85363_load_capacitance(struct pcf85363 * pcf85363,struct device_node * node)123fd9a6a13SJavier Carrasco static int pcf85363_load_capacitance(struct pcf85363 *pcf85363, struct device_node *node)
124fd9a6a13SJavier Carrasco {
125fd9a6a13SJavier Carrasco u32 load = 7000;
126fd9a6a13SJavier Carrasco u8 value = 0;
127fd9a6a13SJavier Carrasco
128fd9a6a13SJavier Carrasco of_property_read_u32(node, "quartz-load-femtofarads", &load);
129fd9a6a13SJavier Carrasco
130fd9a6a13SJavier Carrasco switch (load) {
131fd9a6a13SJavier Carrasco default:
132fd9a6a13SJavier Carrasco dev_warn(&pcf85363->rtc->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000",
133fd9a6a13SJavier Carrasco load);
134fd9a6a13SJavier Carrasco fallthrough;
135fd9a6a13SJavier Carrasco case 7000:
136fd9a6a13SJavier Carrasco break;
137fd9a6a13SJavier Carrasco case 6000:
138fd9a6a13SJavier Carrasco value = OSC_CAP_6000;
139fd9a6a13SJavier Carrasco break;
140fd9a6a13SJavier Carrasco case 12500:
141fd9a6a13SJavier Carrasco value = OSC_CAP_12500;
142fd9a6a13SJavier Carrasco break;
143fd9a6a13SJavier Carrasco }
144fd9a6a13SJavier Carrasco
145fd9a6a13SJavier Carrasco return regmap_update_bits(pcf85363->regmap, CTRL_OSCILLATOR,
146fd9a6a13SJavier Carrasco OSC_CAP_SEL, value);
147fd9a6a13SJavier Carrasco }
148fd9a6a13SJavier Carrasco
pcf85363_rtc_read_time(struct device * dev,struct rtc_time * tm)149a9687aa2SEric Nelson static int pcf85363_rtc_read_time(struct device *dev, struct rtc_time *tm)
150a9687aa2SEric Nelson {
151a9687aa2SEric Nelson struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
152a9687aa2SEric Nelson unsigned char buf[DT_YEARS + 1];
153a9687aa2SEric Nelson int ret, len = sizeof(buf);
154a9687aa2SEric Nelson
155a9687aa2SEric Nelson /* read the RTC date and time registers all at once */
156a9687aa2SEric Nelson ret = regmap_bulk_read(pcf85363->regmap, DT_100THS, buf, len);
157a9687aa2SEric Nelson if (ret) {
158a9687aa2SEric Nelson dev_err(dev, "%s: error %d\n", __func__, ret);
159a9687aa2SEric Nelson return ret;
160a9687aa2SEric Nelson }
161a9687aa2SEric Nelson
162a9687aa2SEric Nelson tm->tm_year = bcd2bin(buf[DT_YEARS]);
163a9687aa2SEric Nelson /* adjust for 1900 base of rtc_time */
164a9687aa2SEric Nelson tm->tm_year += 100;
165a9687aa2SEric Nelson
166a9687aa2SEric Nelson tm->tm_wday = buf[DT_WEEKDAYS] & 7;
167a9687aa2SEric Nelson buf[DT_SECS] &= 0x7F;
168a9687aa2SEric Nelson tm->tm_sec = bcd2bin(buf[DT_SECS]);
169a9687aa2SEric Nelson buf[DT_MINUTES] &= 0x7F;
170a9687aa2SEric Nelson tm->tm_min = bcd2bin(buf[DT_MINUTES]);
171a9687aa2SEric Nelson tm->tm_hour = bcd2bin(buf[DT_HOURS]);
172a9687aa2SEric Nelson tm->tm_mday = bcd2bin(buf[DT_DAYS]);
173a9687aa2SEric Nelson tm->tm_mon = bcd2bin(buf[DT_MONTHS]) - 1;
174a9687aa2SEric Nelson
175a9687aa2SEric Nelson return 0;
176a9687aa2SEric Nelson }
177a9687aa2SEric Nelson
pcf85363_rtc_set_time(struct device * dev,struct rtc_time * tm)178a9687aa2SEric Nelson static int pcf85363_rtc_set_time(struct device *dev, struct rtc_time *tm)
179a9687aa2SEric Nelson {
180a9687aa2SEric Nelson struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
181188306acSAlexandre Belloni unsigned char tmp[11];
182188306acSAlexandre Belloni unsigned char *buf = &tmp[2];
183188306acSAlexandre Belloni int ret;
184188306acSAlexandre Belloni
185188306acSAlexandre Belloni tmp[0] = STOP_EN_STOP;
186188306acSAlexandre Belloni tmp[1] = RESET_CPR;
187a9687aa2SEric Nelson
188a9687aa2SEric Nelson buf[DT_100THS] = 0;
189a9687aa2SEric Nelson buf[DT_SECS] = bin2bcd(tm->tm_sec);
190a9687aa2SEric Nelson buf[DT_MINUTES] = bin2bcd(tm->tm_min);
191a9687aa2SEric Nelson buf[DT_HOURS] = bin2bcd(tm->tm_hour);
192a9687aa2SEric Nelson buf[DT_DAYS] = bin2bcd(tm->tm_mday);
193a9687aa2SEric Nelson buf[DT_WEEKDAYS] = tm->tm_wday;
194a9687aa2SEric Nelson buf[DT_MONTHS] = bin2bcd(tm->tm_mon + 1);
195a9687aa2SEric Nelson buf[DT_YEARS] = bin2bcd(tm->tm_year % 100);
196a9687aa2SEric Nelson
197188306acSAlexandre Belloni ret = regmap_bulk_write(pcf85363->regmap, CTRL_STOP_EN,
1987ef66122SBiwen Li tmp, 2);
1997ef66122SBiwen Li if (ret)
2007ef66122SBiwen Li return ret;
2017ef66122SBiwen Li
2027ef66122SBiwen Li ret = regmap_bulk_write(pcf85363->regmap, DT_100THS,
2037ef66122SBiwen Li buf, sizeof(tmp) - 2);
204188306acSAlexandre Belloni if (ret)
205188306acSAlexandre Belloni return ret;
206188306acSAlexandre Belloni
207188306acSAlexandre Belloni return regmap_write(pcf85363->regmap, CTRL_STOP_EN, 0);
208a9687aa2SEric Nelson }
209a9687aa2SEric Nelson
pcf85363_rtc_read_alarm(struct device * dev,struct rtc_wkalrm * alrm)210e5aac267SAlexandre Belloni static int pcf85363_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
211e5aac267SAlexandre Belloni {
212e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
213e5aac267SAlexandre Belloni unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1];
214e5aac267SAlexandre Belloni unsigned int val;
215e5aac267SAlexandre Belloni int ret;
216e5aac267SAlexandre Belloni
217e5aac267SAlexandre Belloni ret = regmap_bulk_read(pcf85363->regmap, DT_SECOND_ALM1, buf,
218e5aac267SAlexandre Belloni sizeof(buf));
219e5aac267SAlexandre Belloni if (ret)
220e5aac267SAlexandre Belloni return ret;
221e5aac267SAlexandre Belloni
222e5aac267SAlexandre Belloni alrm->time.tm_sec = bcd2bin(buf[0]);
223e5aac267SAlexandre Belloni alrm->time.tm_min = bcd2bin(buf[1]);
224e5aac267SAlexandre Belloni alrm->time.tm_hour = bcd2bin(buf[2]);
225e5aac267SAlexandre Belloni alrm->time.tm_mday = bcd2bin(buf[3]);
226e5aac267SAlexandre Belloni alrm->time.tm_mon = bcd2bin(buf[4]) - 1;
227e5aac267SAlexandre Belloni
228e5aac267SAlexandre Belloni ret = regmap_read(pcf85363->regmap, CTRL_INTA_EN, &val);
229e5aac267SAlexandre Belloni if (ret)
230e5aac267SAlexandre Belloni return ret;
231e5aac267SAlexandre Belloni
232e5aac267SAlexandre Belloni alrm->enabled = !!(val & INT_A1IE);
233e5aac267SAlexandre Belloni
234e5aac267SAlexandre Belloni return 0;
235e5aac267SAlexandre Belloni }
236e5aac267SAlexandre Belloni
_pcf85363_rtc_alarm_irq_enable(struct pcf85363 * pcf85363,unsigned int enabled)237e5aac267SAlexandre Belloni static int _pcf85363_rtc_alarm_irq_enable(struct pcf85363 *pcf85363, unsigned
238e5aac267SAlexandre Belloni int enabled)
239e5aac267SAlexandre Belloni {
240e5aac267SAlexandre Belloni unsigned int alarm_flags = ALRM_SEC_A1E | ALRM_MIN_A1E | ALRM_HR_A1E |
241e5aac267SAlexandre Belloni ALRM_DAY_A1E | ALRM_MON_A1E;
242e5aac267SAlexandre Belloni int ret;
243e5aac267SAlexandre Belloni
244e5aac267SAlexandre Belloni ret = regmap_update_bits(pcf85363->regmap, DT_ALARM_EN, alarm_flags,
245e5aac267SAlexandre Belloni enabled ? alarm_flags : 0);
246e5aac267SAlexandre Belloni if (ret)
247e5aac267SAlexandre Belloni return ret;
248e5aac267SAlexandre Belloni
249e5aac267SAlexandre Belloni ret = regmap_update_bits(pcf85363->regmap, CTRL_INTA_EN,
250e5aac267SAlexandre Belloni INT_A1IE, enabled ? INT_A1IE : 0);
251e5aac267SAlexandre Belloni
252e5aac267SAlexandre Belloni if (ret || enabled)
253e5aac267SAlexandre Belloni return ret;
254e5aac267SAlexandre Belloni
255e5aac267SAlexandre Belloni /* clear current flags */
256e5aac267SAlexandre Belloni return regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0);
257e5aac267SAlexandre Belloni }
258e5aac267SAlexandre Belloni
pcf85363_rtc_alarm_irq_enable(struct device * dev,unsigned int enabled)259e5aac267SAlexandre Belloni static int pcf85363_rtc_alarm_irq_enable(struct device *dev,
260e5aac267SAlexandre Belloni unsigned int enabled)
261e5aac267SAlexandre Belloni {
262e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
263e5aac267SAlexandre Belloni
264e5aac267SAlexandre Belloni return _pcf85363_rtc_alarm_irq_enable(pcf85363, enabled);
265e5aac267SAlexandre Belloni }
266e5aac267SAlexandre Belloni
pcf85363_rtc_set_alarm(struct device * dev,struct rtc_wkalrm * alrm)267e5aac267SAlexandre Belloni static int pcf85363_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
268e5aac267SAlexandre Belloni {
269e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
270e5aac267SAlexandre Belloni unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1];
271e5aac267SAlexandre Belloni int ret;
272e5aac267SAlexandre Belloni
273e5aac267SAlexandre Belloni buf[0] = bin2bcd(alrm->time.tm_sec);
274e5aac267SAlexandre Belloni buf[1] = bin2bcd(alrm->time.tm_min);
275e5aac267SAlexandre Belloni buf[2] = bin2bcd(alrm->time.tm_hour);
276e5aac267SAlexandre Belloni buf[3] = bin2bcd(alrm->time.tm_mday);
277e5aac267SAlexandre Belloni buf[4] = bin2bcd(alrm->time.tm_mon + 1);
278e5aac267SAlexandre Belloni
279e5aac267SAlexandre Belloni /*
280e5aac267SAlexandre Belloni * Disable the alarm interrupt before changing the value to avoid
281e5aac267SAlexandre Belloni * spurious interrupts
282e5aac267SAlexandre Belloni */
283e5aac267SAlexandre Belloni ret = _pcf85363_rtc_alarm_irq_enable(pcf85363, 0);
284e5aac267SAlexandre Belloni if (ret)
285e5aac267SAlexandre Belloni return ret;
286e5aac267SAlexandre Belloni
287e5aac267SAlexandre Belloni ret = regmap_bulk_write(pcf85363->regmap, DT_SECOND_ALM1, buf,
288e5aac267SAlexandre Belloni sizeof(buf));
289e5aac267SAlexandre Belloni if (ret)
290e5aac267SAlexandre Belloni return ret;
291e5aac267SAlexandre Belloni
292e5aac267SAlexandre Belloni return _pcf85363_rtc_alarm_irq_enable(pcf85363, alrm->enabled);
293e5aac267SAlexandre Belloni }
294e5aac267SAlexandre Belloni
pcf85363_rtc_handle_irq(int irq,void * dev_id)295e5aac267SAlexandre Belloni static irqreturn_t pcf85363_rtc_handle_irq(int irq, void *dev_id)
296e5aac267SAlexandre Belloni {
297e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = i2c_get_clientdata(dev_id);
298e5aac267SAlexandre Belloni unsigned int flags;
299e5aac267SAlexandre Belloni int err;
300e5aac267SAlexandre Belloni
301e5aac267SAlexandre Belloni err = regmap_read(pcf85363->regmap, CTRL_FLAGS, &flags);
302e5aac267SAlexandre Belloni if (err)
303e5aac267SAlexandre Belloni return IRQ_NONE;
304e5aac267SAlexandre Belloni
305e5aac267SAlexandre Belloni if (flags & FLAGS_A1F) {
306e5aac267SAlexandre Belloni rtc_update_irq(pcf85363->rtc, 1, RTC_IRQF | RTC_AF);
307e5aac267SAlexandre Belloni regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0);
308e5aac267SAlexandre Belloni return IRQ_HANDLED;
309e5aac267SAlexandre Belloni }
310e5aac267SAlexandre Belloni
311e5aac267SAlexandre Belloni return IRQ_NONE;
312e5aac267SAlexandre Belloni }
313e5aac267SAlexandre Belloni
314a9687aa2SEric Nelson static const struct rtc_class_ops rtc_ops = {
315a9687aa2SEric Nelson .read_time = pcf85363_rtc_read_time,
316a9687aa2SEric Nelson .set_time = pcf85363_rtc_set_time,
317e5aac267SAlexandre Belloni .read_alarm = pcf85363_rtc_read_alarm,
318e5aac267SAlexandre Belloni .set_alarm = pcf85363_rtc_set_alarm,
319e5aac267SAlexandre Belloni .alarm_irq_enable = pcf85363_rtc_alarm_irq_enable,
320e5aac267SAlexandre Belloni };
321e5aac267SAlexandre Belloni
pcf85363_nvram_read(void * priv,unsigned int offset,void * val,size_t bytes)322a9687aa2SEric Nelson static int pcf85363_nvram_read(void *priv, unsigned int offset, void *val,
323a9687aa2SEric Nelson size_t bytes)
324a9687aa2SEric Nelson {
325a9687aa2SEric Nelson struct pcf85363 *pcf85363 = priv;
326a9687aa2SEric Nelson
327a9687aa2SEric Nelson return regmap_bulk_read(pcf85363->regmap, CTRL_RAM + offset,
328a9687aa2SEric Nelson val, bytes);
329a9687aa2SEric Nelson }
330a9687aa2SEric Nelson
pcf85363_nvram_write(void * priv,unsigned int offset,void * val,size_t bytes)331a9687aa2SEric Nelson static int pcf85363_nvram_write(void *priv, unsigned int offset, void *val,
332a9687aa2SEric Nelson size_t bytes)
333a9687aa2SEric Nelson {
334a9687aa2SEric Nelson struct pcf85363 *pcf85363 = priv;
335a9687aa2SEric Nelson
336a9687aa2SEric Nelson return regmap_bulk_write(pcf85363->regmap, CTRL_RAM + offset,
337a9687aa2SEric Nelson val, bytes);
338a9687aa2SEric Nelson }
339a9687aa2SEric Nelson
pcf85x63_nvram_read(void * priv,unsigned int offset,void * val,size_t bytes)340fc979933SBiju Das static int pcf85x63_nvram_read(void *priv, unsigned int offset, void *val,
341fc979933SBiju Das size_t bytes)
342fc979933SBiju Das {
343fc979933SBiju Das struct pcf85363 *pcf85363 = priv;
344fc979933SBiju Das unsigned int tmp_val;
345fc979933SBiju Das int ret;
346fc979933SBiju Das
347fc979933SBiju Das ret = regmap_read(pcf85363->regmap, CTRL_RAMBYTE, &tmp_val);
348fc979933SBiju Das (*(unsigned char *) val) = (unsigned char) tmp_val;
349fc979933SBiju Das
350fc979933SBiju Das return ret;
351fc979933SBiju Das }
352fc979933SBiju Das
pcf85x63_nvram_write(void * priv,unsigned int offset,void * val,size_t bytes)353fc979933SBiju Das static int pcf85x63_nvram_write(void *priv, unsigned int offset, void *val,
354fc979933SBiju Das size_t bytes)
355fc979933SBiju Das {
356fc979933SBiju Das struct pcf85363 *pcf85363 = priv;
357fc979933SBiju Das unsigned char tmp_val;
358fc979933SBiju Das
359fc979933SBiju Das tmp_val = *((unsigned char *)val);
360fc979933SBiju Das return regmap_write(pcf85363->regmap, CTRL_RAMBYTE,
361fc979933SBiju Das (unsigned int)tmp_val);
362fc979933SBiju Das }
363fc979933SBiju Das
364fc979933SBiju Das static const struct pcf85x63_config pcf_85263_config = {
365fc979933SBiju Das .regmap = {
366fc979933SBiju Das .reg_bits = 8,
367fc979933SBiju Das .val_bits = 8,
368fc979933SBiju Das .max_register = 0x2f,
369fc979933SBiju Das },
370fc979933SBiju Das .num_nvram = 1
371fc979933SBiju Das };
372fc979933SBiju Das
373fc979933SBiju Das static const struct pcf85x63_config pcf_85363_config = {
374fc979933SBiju Das .regmap = {
375a9687aa2SEric Nelson .reg_bits = 8,
376a9687aa2SEric Nelson .val_bits = 8,
377c57849ddSAlexandre Belloni .max_register = 0x7f,
378fc979933SBiju Das },
379fc979933SBiju Das .num_nvram = 2
380a9687aa2SEric Nelson };
381a9687aa2SEric Nelson
pcf85363_probe(struct i2c_client * client)3823f4a3322SStephen Kitt static int pcf85363_probe(struct i2c_client *client)
383a9687aa2SEric Nelson {
384a9687aa2SEric Nelson struct pcf85363 *pcf85363;
385fc979933SBiju Das const struct pcf85x63_config *config = &pcf_85363_config;
386fc979933SBiju Das const void *data = of_device_get_match_data(&client->dev);
387fc979933SBiju Das static struct nvmem_config nvmem_cfg[] = {
388fc979933SBiju Das {
389fc979933SBiju Das .name = "pcf85x63-",
390fc979933SBiju Das .word_size = 1,
391fc979933SBiju Das .stride = 1,
392fc979933SBiju Das .size = 1,
393fc979933SBiju Das .reg_read = pcf85x63_nvram_read,
394fc979933SBiju Das .reg_write = pcf85x63_nvram_write,
395fc979933SBiju Das }, {
3960e7a412fSAlexandre Belloni .name = "pcf85363-",
3970e7a412fSAlexandre Belloni .word_size = 1,
3980e7a412fSAlexandre Belloni .stride = 1,
3990e7a412fSAlexandre Belloni .size = NVRAM_SIZE,
4000e7a412fSAlexandre Belloni .reg_read = pcf85363_nvram_read,
4010e7a412fSAlexandre Belloni .reg_write = pcf85363_nvram_write,
402fc979933SBiju Das },
4030e7a412fSAlexandre Belloni };
404fd9a6a13SJavier Carrasco int ret, i, err;
4051e786b03SMike Looijmans bool wakeup_source;
406fc979933SBiju Das
407fc979933SBiju Das if (data)
408fc979933SBiju Das config = data;
409a9687aa2SEric Nelson
410a9687aa2SEric Nelson pcf85363 = devm_kzalloc(&client->dev, sizeof(struct pcf85363),
411a9687aa2SEric Nelson GFP_KERNEL);
412a9687aa2SEric Nelson if (!pcf85363)
413a9687aa2SEric Nelson return -ENOMEM;
414a9687aa2SEric Nelson
415fc979933SBiju Das pcf85363->regmap = devm_regmap_init_i2c(client, &config->regmap);
416a9687aa2SEric Nelson if (IS_ERR(pcf85363->regmap)) {
417a9687aa2SEric Nelson dev_err(&client->dev, "regmap allocation failed\n");
418a9687aa2SEric Nelson return PTR_ERR(pcf85363->regmap);
419a9687aa2SEric Nelson }
420a9687aa2SEric Nelson
421a9687aa2SEric Nelson i2c_set_clientdata(client, pcf85363);
422a9687aa2SEric Nelson
4238f7b1d71SAlexandre Belloni pcf85363->rtc = devm_rtc_allocate_device(&client->dev);
424a9687aa2SEric Nelson if (IS_ERR(pcf85363->rtc))
425a9687aa2SEric Nelson return PTR_ERR(pcf85363->rtc);
426a9687aa2SEric Nelson
427fd9a6a13SJavier Carrasco err = pcf85363_load_capacitance(pcf85363, client->dev.of_node);
428fd9a6a13SJavier Carrasco if (err < 0)
429fd9a6a13SJavier Carrasco dev_warn(&client->dev, "failed to set xtal load capacitance: %d",
430fd9a6a13SJavier Carrasco err);
431fd9a6a13SJavier Carrasco
432a9687aa2SEric Nelson pcf85363->rtc->ops = &rtc_ops;
433c0ec8319SAlexandre Belloni pcf85363->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
434c0ec8319SAlexandre Belloni pcf85363->rtc->range_max = RTC_TIMESTAMP_END_2099;
4351e786b03SMike Looijmans
4361e786b03SMike Looijmans wakeup_source = device_property_read_bool(&client->dev,
4371e786b03SMike Looijmans "wakeup-source");
4381e786b03SMike Looijmans if (client->irq > 0 || wakeup_source) {
4391e786b03SMike Looijmans regmap_write(pcf85363->regmap, CTRL_FLAGS, 0);
4401e786b03SMike Looijmans regmap_update_bits(pcf85363->regmap, CTRL_PIN_IO,
441*2be36c09SJavier Carrasco PIN_IO_INTAPM, PIN_IO_INTA_OUT);
4421e786b03SMike Looijmans }
443a9687aa2SEric Nelson
444e5aac267SAlexandre Belloni if (client->irq > 0) {
445dd7166c8SAlexandre Belloni unsigned long irqflags = IRQF_TRIGGER_LOW;
446dd7166c8SAlexandre Belloni
447dd7166c8SAlexandre Belloni if (dev_fwnode(&client->dev))
448dd7166c8SAlexandre Belloni irqflags = 0;
4498f7b1d71SAlexandre Belloni ret = devm_request_threaded_irq(&client->dev, client->irq,
450e5aac267SAlexandre Belloni NULL, pcf85363_rtc_handle_irq,
451dd7166c8SAlexandre Belloni irqflags | IRQF_ONESHOT,
452e5aac267SAlexandre Belloni "pcf85363", client);
4531e786b03SMike Looijmans if (ret) {
4541e786b03SMike Looijmans dev_warn(&client->dev,
4551e786b03SMike Looijmans "unable to request IRQ, alarms disabled\n");
4561e786b03SMike Looijmans client->irq = 0;
4571e786b03SMike Looijmans }
4581e786b03SMike Looijmans }
4591e786b03SMike Looijmans
4601e786b03SMike Looijmans if (client->irq > 0 || wakeup_source) {
4611e786b03SMike Looijmans device_init_wakeup(&client->dev, true);
462732b7341SAlexandre Belloni set_bit(RTC_FEATURE_ALARM, pcf85363->rtc->features);
4631e786b03SMike Looijmans } else {
4641e786b03SMike Looijmans clear_bit(RTC_FEATURE_ALARM, pcf85363->rtc->features);
465e5aac267SAlexandre Belloni }
466e5aac267SAlexandre Belloni
467fdcfd854SBartosz Golaszewski ret = devm_rtc_register_device(pcf85363->rtc);
46824849d17SAlexandre Belloni
469fc979933SBiju Das for (i = 0; i < config->num_nvram; i++) {
470fc979933SBiju Das nvmem_cfg[i].priv = pcf85363;
4713a905c2dSBartosz Golaszewski devm_rtc_nvmem_register(pcf85363->rtc, &nvmem_cfg[i]);
472fc979933SBiju Das }
47324849d17SAlexandre Belloni
47424849d17SAlexandre Belloni return ret;
475a9687aa2SEric Nelson }
476a9687aa2SEric Nelson
477c506bc10SAlexandre Belloni static const __maybe_unused struct of_device_id dev_ids[] = {
478fc979933SBiju Das { .compatible = "nxp,pcf85263", .data = &pcf_85263_config },
479fc979933SBiju Das { .compatible = "nxp,pcf85363", .data = &pcf_85363_config },
480fc979933SBiju Das { /* sentinel */ }
481a9687aa2SEric Nelson };
482a9687aa2SEric Nelson MODULE_DEVICE_TABLE(of, dev_ids);
483a9687aa2SEric Nelson
484a9687aa2SEric Nelson static struct i2c_driver pcf85363_driver = {
485a9687aa2SEric Nelson .driver = {
486a9687aa2SEric Nelson .name = "pcf85363",
487a9687aa2SEric Nelson .of_match_table = of_match_ptr(dev_ids),
488a9687aa2SEric Nelson },
48931b0cecbSUwe Kleine-König .probe = pcf85363_probe,
490a9687aa2SEric Nelson };
491a9687aa2SEric Nelson
492a9687aa2SEric Nelson module_i2c_driver(pcf85363_driver);
493a9687aa2SEric Nelson
494a9687aa2SEric Nelson MODULE_AUTHOR("Eric Nelson");
495fc979933SBiju Das MODULE_DESCRIPTION("pcf85263/pcf85363 I2C RTC driver");
496a9687aa2SEric Nelson MODULE_LICENSE("GPL");
497