12fcdf5fdSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
24d61ff6bSPhilippe De Muyter /*
34d61ff6bSPhilippe De Muyter * A driver for the I2C members of the Abracon AB x8xx RTC family,
44d61ff6bSPhilippe De Muyter * and compatible: AB 1805 and AB 0805
54d61ff6bSPhilippe De Muyter *
64d61ff6bSPhilippe De Muyter * Copyright 2014-2015 Macq S.A.
74d61ff6bSPhilippe De Muyter *
84d61ff6bSPhilippe De Muyter * Author: Philippe De Muyter <phdm@macqel.be>
97d1e5bfeSAlexandre Belloni * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
104d61ff6bSPhilippe De Muyter *
114d61ff6bSPhilippe De Muyter */
124d61ff6bSPhilippe De Muyter
134d61ff6bSPhilippe De Muyter #include <linux/bcd.h>
14e90ff8edSSean Anderson #include <linux/bitfield.h>
154d61ff6bSPhilippe De Muyter #include <linux/i2c.h>
16e59b3c73SChristophe JAILLET #include <linux/kstrtox.h>
174d61ff6bSPhilippe De Muyter #include <linux/module.h>
1848144c28SRob Herring #include <linux/of.h>
194d61ff6bSPhilippe De Muyter #include <linux/rtc.h>
20749e36d0SJeremy Gebben #include <linux/watchdog.h>
214d61ff6bSPhilippe De Muyter
224d61ff6bSPhilippe De Muyter #define ABX8XX_REG_HTH 0x00
234d61ff6bSPhilippe De Muyter #define ABX8XX_REG_SC 0x01
244d61ff6bSPhilippe De Muyter #define ABX8XX_REG_MN 0x02
254d61ff6bSPhilippe De Muyter #define ABX8XX_REG_HR 0x03
264d61ff6bSPhilippe De Muyter #define ABX8XX_REG_DA 0x04
274d61ff6bSPhilippe De Muyter #define ABX8XX_REG_MO 0x05
284d61ff6bSPhilippe De Muyter #define ABX8XX_REG_YR 0x06
294d61ff6bSPhilippe De Muyter #define ABX8XX_REG_WD 0x07
304d61ff6bSPhilippe De Muyter
31718a820aSAlexandre Belloni #define ABX8XX_REG_AHTH 0x08
32718a820aSAlexandre Belloni #define ABX8XX_REG_ASC 0x09
33718a820aSAlexandre Belloni #define ABX8XX_REG_AMN 0x0a
34718a820aSAlexandre Belloni #define ABX8XX_REG_AHR 0x0b
35718a820aSAlexandre Belloni #define ABX8XX_REG_ADA 0x0c
36718a820aSAlexandre Belloni #define ABX8XX_REG_AMO 0x0d
37718a820aSAlexandre Belloni #define ABX8XX_REG_AWD 0x0e
38718a820aSAlexandre Belloni
39718a820aSAlexandre Belloni #define ABX8XX_REG_STATUS 0x0f
40718a820aSAlexandre Belloni #define ABX8XX_STATUS_AF BIT(2)
41ffe1c5a2SMarek Vasut #define ABX8XX_STATUS_BLF BIT(4)
42*a990130fSNobuhiro Iwamatsu #define ABX8XX_STATUS_WDT BIT(5)
43718a820aSAlexandre Belloni
444d61ff6bSPhilippe De Muyter #define ABX8XX_REG_CTRL1 0x10
455f1b2f77SMitja Spes #define ABX8XX_CTRL_WRITE BIT(0)
46718a820aSAlexandre Belloni #define ABX8XX_CTRL_ARST BIT(2)
474d61ff6bSPhilippe De Muyter #define ABX8XX_CTRL_12_24 BIT(6)
484d61ff6bSPhilippe De Muyter
4975455e25SMarek Vasut #define ABX8XX_REG_CTRL2 0x11
5075455e25SMarek Vasut #define ABX8XX_CTRL2_RSVD BIT(5)
5175455e25SMarek Vasut
52718a820aSAlexandre Belloni #define ABX8XX_REG_IRQ 0x12
53718a820aSAlexandre Belloni #define ABX8XX_IRQ_AIE BIT(2)
54718a820aSAlexandre Belloni #define ABX8XX_IRQ_IM_1_4 (0x3 << 5)
55718a820aSAlexandre Belloni
56718a820aSAlexandre Belloni #define ABX8XX_REG_CD_TIMER_CTL 0x18
57718a820aSAlexandre Belloni
5859a8383aSMylène Josserand #define ABX8XX_REG_OSC 0x1c
5959a8383aSMylène Josserand #define ABX8XX_OSC_FOS BIT(3)
6059a8383aSMylène Josserand #define ABX8XX_OSC_BOS BIT(4)
6159a8383aSMylène Josserand #define ABX8XX_OSC_ACAL_512 BIT(5)
6259a8383aSMylène Josserand #define ABX8XX_OSC_ACAL_1024 BIT(6)
6359a8383aSMylène Josserand
6459a8383aSMylène Josserand #define ABX8XX_OSC_OSEL BIT(7)
6559a8383aSMylène Josserand
6659a8383aSMylène Josserand #define ABX8XX_REG_OSS 0x1d
67ee087744SMylène Josserand #define ABX8XX_OSS_OF BIT(1)
6859a8383aSMylène Josserand #define ABX8XX_OSS_OMODE BIT(4)
6959a8383aSMylène Josserand
70749e36d0SJeremy Gebben #define ABX8XX_REG_WDT 0x1b
71749e36d0SJeremy Gebben #define ABX8XX_WDT_WDS BIT(7)
72749e36d0SJeremy Gebben #define ABX8XX_WDT_BMB_MASK 0x7c
73749e36d0SJeremy Gebben #define ABX8XX_WDT_BMB_SHIFT 2
74749e36d0SJeremy Gebben #define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT)
75749e36d0SJeremy Gebben #define ABX8XX_WDT_WRB_MASK 0x03
76749e36d0SJeremy Gebben #define ABX8XX_WDT_WRB_1HZ 0x02
77749e36d0SJeremy Gebben
784d61ff6bSPhilippe De Muyter #define ABX8XX_REG_CFG_KEY 0x1f
7959a8383aSMylène Josserand #define ABX8XX_CFG_KEY_OSC 0xa1
804d61ff6bSPhilippe De Muyter #define ABX8XX_CFG_KEY_MISC 0x9d
814d61ff6bSPhilippe De Muyter
824d61ff6bSPhilippe De Muyter #define ABX8XX_REG_ID0 0x28
834d61ff6bSPhilippe De Muyter
8475455e25SMarek Vasut #define ABX8XX_REG_OUT_CTRL 0x30
8575455e25SMarek Vasut #define ABX8XX_OUT_CTRL_EXDS BIT(4)
8675455e25SMarek Vasut
874d61ff6bSPhilippe De Muyter #define ABX8XX_REG_TRICKLE 0x20
884d61ff6bSPhilippe De Muyter #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0
894d61ff6bSPhilippe De Muyter #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
904d61ff6bSPhilippe De Muyter #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4
914d61ff6bSPhilippe De Muyter
92e90ff8edSSean Anderson #define ABX8XX_REG_EXTRAM 0x3f
93e90ff8edSSean Anderson #define ABX8XX_EXTRAM_XADS GENMASK(1, 0)
94e90ff8edSSean Anderson
95e90ff8edSSean Anderson #define ABX8XX_SRAM_BASE 0x40
96e90ff8edSSean Anderson #define ABX8XX_SRAM_WIN_SIZE 0x40
97e90ff8edSSean Anderson #define ABX8XX_RAM_SIZE 256
98e90ff8edSSean Anderson
99e90ff8edSSean Anderson #define NVMEM_ADDR_LOWER GENMASK(5, 0)
100e90ff8edSSean Anderson #define NVMEM_ADDR_UPPER GENMASK(7, 6)
101e90ff8edSSean Anderson
1024d61ff6bSPhilippe De Muyter static u8 trickle_resistors[] = {0, 3, 6, 11};
1034d61ff6bSPhilippe De Muyter
1044d61ff6bSPhilippe De Muyter enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
10575455e25SMarek Vasut AB1801, AB1803, AB1804, AB1805, RV1805, ABX80X};
1064d61ff6bSPhilippe De Muyter
1074d61ff6bSPhilippe De Muyter struct abx80x_cap {
1084d61ff6bSPhilippe De Muyter u16 pn;
1094d61ff6bSPhilippe De Muyter bool has_tc;
110749e36d0SJeremy Gebben bool has_wdog;
1114d61ff6bSPhilippe De Muyter };
1124d61ff6bSPhilippe De Muyter
1134d61ff6bSPhilippe De Muyter static struct abx80x_cap abx80x_caps[] = {
1144d61ff6bSPhilippe De Muyter [AB0801] = {.pn = 0x0801},
1154d61ff6bSPhilippe De Muyter [AB0803] = {.pn = 0x0803},
116749e36d0SJeremy Gebben [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true},
117749e36d0SJeremy Gebben [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true},
1184d61ff6bSPhilippe De Muyter [AB1801] = {.pn = 0x1801},
1194d61ff6bSPhilippe De Muyter [AB1803] = {.pn = 0x1803},
120749e36d0SJeremy Gebben [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true},
121749e36d0SJeremy Gebben [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
12275455e25SMarek Vasut [RV1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true},
1234d61ff6bSPhilippe De Muyter [ABX80X] = {.pn = 0}
1244d61ff6bSPhilippe De Muyter };
1254d61ff6bSPhilippe De Muyter
126af69f9a7SJeremy Gebben struct abx80x_priv {
127af69f9a7SJeremy Gebben struct rtc_device *rtc;
128af69f9a7SJeremy Gebben struct i2c_client *client;
129749e36d0SJeremy Gebben struct watchdog_device wdog;
130af69f9a7SJeremy Gebben };
131af69f9a7SJeremy Gebben
abx80x_write_config_key(struct i2c_client * client,u8 key)13249dfc1f1SKevin P. Fleming static int abx80x_write_config_key(struct i2c_client *client, u8 key)
13349dfc1f1SKevin P. Fleming {
13449dfc1f1SKevin P. Fleming if (i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, key) < 0) {
13549dfc1f1SKevin P. Fleming dev_err(&client->dev, "Unable to write configuration key\n");
13649dfc1f1SKevin P. Fleming return -EIO;
13749dfc1f1SKevin P. Fleming }
13849dfc1f1SKevin P. Fleming
13949dfc1f1SKevin P. Fleming return 0;
14049dfc1f1SKevin P. Fleming }
14149dfc1f1SKevin P. Fleming
abx80x_is_rc_mode(struct i2c_client * client)14259a8383aSMylène Josserand static int abx80x_is_rc_mode(struct i2c_client *client)
14359a8383aSMylène Josserand {
14459a8383aSMylène Josserand int flags = 0;
14559a8383aSMylène Josserand
14659a8383aSMylène Josserand flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
14759a8383aSMylène Josserand if (flags < 0) {
14859a8383aSMylène Josserand dev_err(&client->dev,
14959a8383aSMylène Josserand "Failed to read autocalibration attribute\n");
15059a8383aSMylène Josserand return flags;
15159a8383aSMylène Josserand }
15259a8383aSMylène Josserand
15359a8383aSMylène Josserand return (flags & ABX8XX_OSS_OMODE) ? 1 : 0;
15459a8383aSMylène Josserand }
15559a8383aSMylène Josserand
abx80x_enable_trickle_charger(struct i2c_client * client,u8 trickle_cfg)1564d61ff6bSPhilippe De Muyter static int abx80x_enable_trickle_charger(struct i2c_client *client,
1574d61ff6bSPhilippe De Muyter u8 trickle_cfg)
1584d61ff6bSPhilippe De Muyter {
1594d61ff6bSPhilippe De Muyter int err;
1604d61ff6bSPhilippe De Muyter
1614d61ff6bSPhilippe De Muyter /*
1624d61ff6bSPhilippe De Muyter * Write the configuration key register to enable access to the Trickle
1634d61ff6bSPhilippe De Muyter * register
1644d61ff6bSPhilippe De Muyter */
16549dfc1f1SKevin P. Fleming if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_MISC) < 0)
1664d61ff6bSPhilippe De Muyter return -EIO;
1674d61ff6bSPhilippe De Muyter
1684d61ff6bSPhilippe De Muyter err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
1694d61ff6bSPhilippe De Muyter ABX8XX_TRICKLE_CHARGE_ENABLE |
1704d61ff6bSPhilippe De Muyter trickle_cfg);
1714d61ff6bSPhilippe De Muyter if (err < 0) {
1724d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unable to write trickle register\n");
1734d61ff6bSPhilippe De Muyter return -EIO;
1744d61ff6bSPhilippe De Muyter }
1754d61ff6bSPhilippe De Muyter
1764d61ff6bSPhilippe De Muyter return 0;
1774d61ff6bSPhilippe De Muyter }
1784d61ff6bSPhilippe De Muyter
abx80x_rtc_read_time(struct device * dev,struct rtc_time * tm)1794d61ff6bSPhilippe De Muyter static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
1804d61ff6bSPhilippe De Muyter {
1814d61ff6bSPhilippe De Muyter struct i2c_client *client = to_i2c_client(dev);
1824d61ff6bSPhilippe De Muyter unsigned char buf[8];
183ee087744SMylène Josserand int err, flags, rc_mode = 0;
184ee087744SMylène Josserand
185ee087744SMylène Josserand /* Read the Oscillator Failure only in XT mode */
186ee087744SMylène Josserand rc_mode = abx80x_is_rc_mode(client);
187ee087744SMylène Josserand if (rc_mode < 0)
188ee087744SMylène Josserand return rc_mode;
189ee087744SMylène Josserand
190ee087744SMylène Josserand if (!rc_mode) {
191ee087744SMylène Josserand flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
192ee087744SMylène Josserand if (flags < 0)
193ee087744SMylène Josserand return flags;
194ee087744SMylène Josserand
195ee087744SMylène Josserand if (flags & ABX8XX_OSS_OF) {
196ee087744SMylène Josserand dev_err(dev, "Oscillator failure, data is invalid.\n");
197ee087744SMylène Josserand return -EINVAL;
198ee087744SMylène Josserand }
199ee087744SMylène Josserand }
2004d61ff6bSPhilippe De Muyter
2014d61ff6bSPhilippe De Muyter err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
2024d61ff6bSPhilippe De Muyter sizeof(buf), buf);
2034d61ff6bSPhilippe De Muyter if (err < 0) {
2044d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unable to read date\n");
2054d61ff6bSPhilippe De Muyter return -EIO;
2064d61ff6bSPhilippe De Muyter }
2074d61ff6bSPhilippe De Muyter
2084d61ff6bSPhilippe De Muyter tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
2094d61ff6bSPhilippe De Muyter tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
2104d61ff6bSPhilippe De Muyter tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
2114d61ff6bSPhilippe De Muyter tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
2124d61ff6bSPhilippe De Muyter tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
2134d61ff6bSPhilippe De Muyter tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
2144d61ff6bSPhilippe De Muyter tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
2154d61ff6bSPhilippe De Muyter
216fbfd36fdSAlexandre Belloni return 0;
2174d61ff6bSPhilippe De Muyter }
2184d61ff6bSPhilippe De Muyter
abx80x_rtc_set_time(struct device * dev,struct rtc_time * tm)2194d61ff6bSPhilippe De Muyter static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
2204d61ff6bSPhilippe De Muyter {
2214d61ff6bSPhilippe De Muyter struct i2c_client *client = to_i2c_client(dev);
2224d61ff6bSPhilippe De Muyter unsigned char buf[8];
223ee087744SMylène Josserand int err, flags;
2244d61ff6bSPhilippe De Muyter
2254d61ff6bSPhilippe De Muyter if (tm->tm_year < 100)
2264d61ff6bSPhilippe De Muyter return -EINVAL;
2274d61ff6bSPhilippe De Muyter
2284d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_HTH] = 0;
2294d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
2304d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
2314d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
2324d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
2334d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
2344d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
2354d61ff6bSPhilippe De Muyter buf[ABX8XX_REG_WD] = tm->tm_wday;
2364d61ff6bSPhilippe De Muyter
2374d61ff6bSPhilippe De Muyter err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
2384d61ff6bSPhilippe De Muyter sizeof(buf), buf);
2394d61ff6bSPhilippe De Muyter if (err < 0) {
2404d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unable to write to date registers\n");
2414d61ff6bSPhilippe De Muyter return -EIO;
2424d61ff6bSPhilippe De Muyter }
2434d61ff6bSPhilippe De Muyter
244ee087744SMylène Josserand /* Clear the OF bit of Oscillator Status Register */
245ee087744SMylène Josserand flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS);
246ee087744SMylène Josserand if (flags < 0)
247ee087744SMylène Josserand return flags;
248ee087744SMylène Josserand
249ee087744SMylène Josserand err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS,
250ee087744SMylène Josserand flags & ~ABX8XX_OSS_OF);
251ee087744SMylène Josserand if (err < 0) {
252ee087744SMylène Josserand dev_err(&client->dev, "Unable to write oscillator status register\n");
253ee087744SMylène Josserand return err;
254ee087744SMylène Josserand }
255ee087744SMylène Josserand
2564d61ff6bSPhilippe De Muyter return 0;
2574d61ff6bSPhilippe De Muyter }
2584d61ff6bSPhilippe De Muyter
abx80x_handle_irq(int irq,void * dev_id)259718a820aSAlexandre Belloni static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
260718a820aSAlexandre Belloni {
261718a820aSAlexandre Belloni struct i2c_client *client = dev_id;
262af69f9a7SJeremy Gebben struct abx80x_priv *priv = i2c_get_clientdata(client);
263af69f9a7SJeremy Gebben struct rtc_device *rtc = priv->rtc;
264718a820aSAlexandre Belloni int status;
265718a820aSAlexandre Belloni
266718a820aSAlexandre Belloni status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
267718a820aSAlexandre Belloni if (status < 0)
268718a820aSAlexandre Belloni return IRQ_NONE;
269718a820aSAlexandre Belloni
270718a820aSAlexandre Belloni if (status & ABX8XX_STATUS_AF)
271718a820aSAlexandre Belloni rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
272718a820aSAlexandre Belloni
273749e36d0SJeremy Gebben /*
274749e36d0SJeremy Gebben * It is unclear if we'll get an interrupt before the external
275749e36d0SJeremy Gebben * reset kicks in.
276749e36d0SJeremy Gebben */
277749e36d0SJeremy Gebben if (status & ABX8XX_STATUS_WDT)
278749e36d0SJeremy Gebben dev_alert(&client->dev, "watchdog timeout interrupt.\n");
279749e36d0SJeremy Gebben
280718a820aSAlexandre Belloni i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
281718a820aSAlexandre Belloni
282718a820aSAlexandre Belloni return IRQ_HANDLED;
283718a820aSAlexandre Belloni }
284718a820aSAlexandre Belloni
abx80x_read_alarm(struct device * dev,struct rtc_wkalrm * t)285718a820aSAlexandre Belloni static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
286718a820aSAlexandre Belloni {
287718a820aSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev);
288718a820aSAlexandre Belloni unsigned char buf[7];
289718a820aSAlexandre Belloni
290718a820aSAlexandre Belloni int irq_mask, err;
291718a820aSAlexandre Belloni
292718a820aSAlexandre Belloni if (client->irq <= 0)
293718a820aSAlexandre Belloni return -EINVAL;
294718a820aSAlexandre Belloni
295718a820aSAlexandre Belloni err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
296718a820aSAlexandre Belloni sizeof(buf), buf);
297718a820aSAlexandre Belloni if (err)
298718a820aSAlexandre Belloni return err;
299718a820aSAlexandre Belloni
300718a820aSAlexandre Belloni irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
301718a820aSAlexandre Belloni if (irq_mask < 0)
302718a820aSAlexandre Belloni return irq_mask;
303718a820aSAlexandre Belloni
304718a820aSAlexandre Belloni t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
305718a820aSAlexandre Belloni t->time.tm_min = bcd2bin(buf[1] & 0x7F);
306718a820aSAlexandre Belloni t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
307718a820aSAlexandre Belloni t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
308718a820aSAlexandre Belloni t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
309718a820aSAlexandre Belloni t->time.tm_wday = buf[5] & 0x7;
310718a820aSAlexandre Belloni
311718a820aSAlexandre Belloni t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
312718a820aSAlexandre Belloni t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
313718a820aSAlexandre Belloni
314718a820aSAlexandre Belloni return err;
315718a820aSAlexandre Belloni }
316718a820aSAlexandre Belloni
abx80x_set_alarm(struct device * dev,struct rtc_wkalrm * t)317718a820aSAlexandre Belloni static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
318718a820aSAlexandre Belloni {
319718a820aSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev);
320718a820aSAlexandre Belloni u8 alarm[6];
321718a820aSAlexandre Belloni int err;
322718a820aSAlexandre Belloni
323718a820aSAlexandre Belloni if (client->irq <= 0)
324718a820aSAlexandre Belloni return -EINVAL;
325718a820aSAlexandre Belloni
326718a820aSAlexandre Belloni alarm[0] = 0x0;
327718a820aSAlexandre Belloni alarm[1] = bin2bcd(t->time.tm_sec);
328718a820aSAlexandre Belloni alarm[2] = bin2bcd(t->time.tm_min);
329718a820aSAlexandre Belloni alarm[3] = bin2bcd(t->time.tm_hour);
330718a820aSAlexandre Belloni alarm[4] = bin2bcd(t->time.tm_mday);
331718a820aSAlexandre Belloni alarm[5] = bin2bcd(t->time.tm_mon + 1);
332718a820aSAlexandre Belloni
333718a820aSAlexandre Belloni err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
334718a820aSAlexandre Belloni sizeof(alarm), alarm);
335718a820aSAlexandre Belloni if (err < 0) {
336718a820aSAlexandre Belloni dev_err(&client->dev, "Unable to write alarm registers\n");
337718a820aSAlexandre Belloni return -EIO;
338718a820aSAlexandre Belloni }
339718a820aSAlexandre Belloni
340718a820aSAlexandre Belloni if (t->enabled) {
341718a820aSAlexandre Belloni err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
342718a820aSAlexandre Belloni (ABX8XX_IRQ_IM_1_4 |
343718a820aSAlexandre Belloni ABX8XX_IRQ_AIE));
344718a820aSAlexandre Belloni if (err)
345718a820aSAlexandre Belloni return err;
346718a820aSAlexandre Belloni }
347718a820aSAlexandre Belloni
348718a820aSAlexandre Belloni return 0;
349718a820aSAlexandre Belloni }
350718a820aSAlexandre Belloni
abx80x_rtc_set_autocalibration(struct device * dev,int autocalibration)35159a8383aSMylène Josserand static int abx80x_rtc_set_autocalibration(struct device *dev,
35259a8383aSMylène Josserand int autocalibration)
35359a8383aSMylène Josserand {
35459a8383aSMylène Josserand struct i2c_client *client = to_i2c_client(dev);
35559a8383aSMylène Josserand int retval, flags = 0;
35659a8383aSMylène Josserand
35759a8383aSMylène Josserand if ((autocalibration != 0) && (autocalibration != 1024) &&
35859a8383aSMylène Josserand (autocalibration != 512)) {
35959a8383aSMylène Josserand dev_err(dev, "autocalibration value outside permitted range\n");
36059a8383aSMylène Josserand return -EINVAL;
36159a8383aSMylène Josserand }
36259a8383aSMylène Josserand
36359a8383aSMylène Josserand flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
36459a8383aSMylène Josserand if (flags < 0)
36559a8383aSMylène Josserand return flags;
36659a8383aSMylène Josserand
36759a8383aSMylène Josserand if (autocalibration == 0) {
36859a8383aSMylène Josserand flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024);
36959a8383aSMylène Josserand } else if (autocalibration == 1024) {
37059a8383aSMylène Josserand /* 1024 autocalibration is 0x10 */
37159a8383aSMylène Josserand flags |= ABX8XX_OSC_ACAL_1024;
37259a8383aSMylène Josserand flags &= ~(ABX8XX_OSC_ACAL_512);
37359a8383aSMylène Josserand } else {
37459a8383aSMylène Josserand /* 512 autocalibration is 0x11 */
37559a8383aSMylène Josserand flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512);
37659a8383aSMylène Josserand }
37759a8383aSMylène Josserand
37859a8383aSMylène Josserand /* Unlock write access to Oscillator Control Register */
37949dfc1f1SKevin P. Fleming if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_OSC) < 0)
38049dfc1f1SKevin P. Fleming return -EIO;
38159a8383aSMylène Josserand
38259a8383aSMylène Josserand retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
38359a8383aSMylène Josserand
38459a8383aSMylène Josserand return retval;
38559a8383aSMylène Josserand }
38659a8383aSMylène Josserand
abx80x_rtc_get_autocalibration(struct device * dev)38759a8383aSMylène Josserand static int abx80x_rtc_get_autocalibration(struct device *dev)
38859a8383aSMylène Josserand {
38959a8383aSMylène Josserand struct i2c_client *client = to_i2c_client(dev);
39059a8383aSMylène Josserand int flags = 0, autocalibration;
39159a8383aSMylène Josserand
39259a8383aSMylène Josserand flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
39359a8383aSMylène Josserand if (flags < 0)
39459a8383aSMylène Josserand return flags;
39559a8383aSMylène Josserand
39659a8383aSMylène Josserand if (flags & ABX8XX_OSC_ACAL_512)
39759a8383aSMylène Josserand autocalibration = 512;
39859a8383aSMylène Josserand else if (flags & ABX8XX_OSC_ACAL_1024)
39959a8383aSMylène Josserand autocalibration = 1024;
40059a8383aSMylène Josserand else
40159a8383aSMylène Josserand autocalibration = 0;
40259a8383aSMylène Josserand
40359a8383aSMylène Josserand return autocalibration;
40459a8383aSMylène Josserand }
40559a8383aSMylène Josserand
autocalibration_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)40659a8383aSMylène Josserand static ssize_t autocalibration_store(struct device *dev,
40759a8383aSMylène Josserand struct device_attribute *attr,
40859a8383aSMylène Josserand const char *buf, size_t count)
40959a8383aSMylène Josserand {
41059a8383aSMylène Josserand int retval;
41159a8383aSMylène Josserand unsigned long autocalibration = 0;
41259a8383aSMylène Josserand
41359a8383aSMylène Josserand retval = kstrtoul(buf, 10, &autocalibration);
41459a8383aSMylène Josserand if (retval < 0) {
41559a8383aSMylène Josserand dev_err(dev, "Failed to store RTC autocalibration attribute\n");
41659a8383aSMylène Josserand return -EINVAL;
41759a8383aSMylène Josserand }
41859a8383aSMylène Josserand
419559e883eSAlexandre Belloni retval = abx80x_rtc_set_autocalibration(dev->parent, autocalibration);
42059a8383aSMylène Josserand
42159a8383aSMylène Josserand return retval ? retval : count;
42259a8383aSMylène Josserand }
42359a8383aSMylène Josserand
autocalibration_show(struct device * dev,struct device_attribute * attr,char * buf)42459a8383aSMylène Josserand static ssize_t autocalibration_show(struct device *dev,
42559a8383aSMylène Josserand struct device_attribute *attr, char *buf)
42659a8383aSMylène Josserand {
42759a8383aSMylène Josserand int autocalibration = 0;
42859a8383aSMylène Josserand
429559e883eSAlexandre Belloni autocalibration = abx80x_rtc_get_autocalibration(dev->parent);
43059a8383aSMylène Josserand if (autocalibration < 0) {
43159a8383aSMylène Josserand dev_err(dev, "Failed to read RTC autocalibration\n");
43259a8383aSMylène Josserand sprintf(buf, "0\n");
43359a8383aSMylène Josserand return autocalibration;
43459a8383aSMylène Josserand }
43559a8383aSMylène Josserand
43659a8383aSMylène Josserand return sprintf(buf, "%d\n", autocalibration);
43759a8383aSMylène Josserand }
43859a8383aSMylène Josserand
43959a8383aSMylène Josserand static DEVICE_ATTR_RW(autocalibration);
44059a8383aSMylène Josserand
oscillator_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)44159a8383aSMylène Josserand static ssize_t oscillator_store(struct device *dev,
44259a8383aSMylène Josserand struct device_attribute *attr,
44359a8383aSMylène Josserand const char *buf, size_t count)
44459a8383aSMylène Josserand {
445559e883eSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev->parent);
44659a8383aSMylène Josserand int retval, flags, rc_mode = 0;
44759a8383aSMylène Josserand
44859a8383aSMylène Josserand if (strncmp(buf, "rc", 2) == 0) {
44959a8383aSMylène Josserand rc_mode = 1;
45059a8383aSMylène Josserand } else if (strncmp(buf, "xtal", 4) == 0) {
45159a8383aSMylène Josserand rc_mode = 0;
45259a8383aSMylène Josserand } else {
45359a8383aSMylène Josserand dev_err(dev, "Oscillator selection value outside permitted ones\n");
45459a8383aSMylène Josserand return -EINVAL;
45559a8383aSMylène Josserand }
45659a8383aSMylène Josserand
45759a8383aSMylène Josserand flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC);
45859a8383aSMylène Josserand if (flags < 0)
45959a8383aSMylène Josserand return flags;
46059a8383aSMylène Josserand
46159a8383aSMylène Josserand if (rc_mode == 0)
46259a8383aSMylène Josserand flags &= ~(ABX8XX_OSC_OSEL);
46359a8383aSMylène Josserand else
46459a8383aSMylène Josserand flags |= (ABX8XX_OSC_OSEL);
46559a8383aSMylène Josserand
46659a8383aSMylène Josserand /* Unlock write access on Oscillator Control register */
46749dfc1f1SKevin P. Fleming if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_OSC) < 0)
46849dfc1f1SKevin P. Fleming return -EIO;
46959a8383aSMylène Josserand
47059a8383aSMylène Josserand retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags);
47159a8383aSMylène Josserand if (retval < 0) {
47259a8383aSMylène Josserand dev_err(dev, "Failed to write Oscillator Control register\n");
47359a8383aSMylène Josserand return retval;
47459a8383aSMylène Josserand }
47559a8383aSMylène Josserand
47659a8383aSMylène Josserand return retval ? retval : count;
47759a8383aSMylène Josserand }
47859a8383aSMylène Josserand
oscillator_show(struct device * dev,struct device_attribute * attr,char * buf)47959a8383aSMylène Josserand static ssize_t oscillator_show(struct device *dev,
48059a8383aSMylène Josserand struct device_attribute *attr, char *buf)
48159a8383aSMylène Josserand {
48259a8383aSMylène Josserand int rc_mode = 0;
483559e883eSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev->parent);
48459a8383aSMylène Josserand
48559a8383aSMylène Josserand rc_mode = abx80x_is_rc_mode(client);
48659a8383aSMylène Josserand
48759a8383aSMylène Josserand if (rc_mode < 0) {
48859a8383aSMylène Josserand dev_err(dev, "Failed to read RTC oscillator selection\n");
48959a8383aSMylène Josserand sprintf(buf, "\n");
49059a8383aSMylène Josserand return rc_mode;
49159a8383aSMylène Josserand }
49259a8383aSMylène Josserand
49359a8383aSMylène Josserand if (rc_mode)
49459a8383aSMylène Josserand return sprintf(buf, "rc\n");
49559a8383aSMylène Josserand else
49659a8383aSMylène Josserand return sprintf(buf, "xtal\n");
49759a8383aSMylène Josserand }
49859a8383aSMylène Josserand
49959a8383aSMylène Josserand static DEVICE_ATTR_RW(oscillator);
50059a8383aSMylène Josserand
50159a8383aSMylène Josserand static struct attribute *rtc_calib_attrs[] = {
50259a8383aSMylène Josserand &dev_attr_autocalibration.attr,
50359a8383aSMylène Josserand &dev_attr_oscillator.attr,
50459a8383aSMylène Josserand NULL,
50559a8383aSMylène Josserand };
50659a8383aSMylène Josserand
50759a8383aSMylène Josserand static const struct attribute_group rtc_calib_attr_group = {
50859a8383aSMylène Josserand .attrs = rtc_calib_attrs,
50959a8383aSMylène Josserand };
51059a8383aSMylène Josserand
abx80x_alarm_irq_enable(struct device * dev,unsigned int enabled)511718a820aSAlexandre Belloni static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
512718a820aSAlexandre Belloni {
513718a820aSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev);
514718a820aSAlexandre Belloni int err;
515718a820aSAlexandre Belloni
516718a820aSAlexandre Belloni if (enabled)
517718a820aSAlexandre Belloni err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
518718a820aSAlexandre Belloni (ABX8XX_IRQ_IM_1_4 |
519718a820aSAlexandre Belloni ABX8XX_IRQ_AIE));
520718a820aSAlexandre Belloni else
521718a820aSAlexandre Belloni err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
522718a820aSAlexandre Belloni ABX8XX_IRQ_IM_1_4);
523718a820aSAlexandre Belloni return err;
524718a820aSAlexandre Belloni }
525718a820aSAlexandre Belloni
abx80x_ioctl(struct device * dev,unsigned int cmd,unsigned long arg)526ffe1c5a2SMarek Vasut static int abx80x_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
527ffe1c5a2SMarek Vasut {
528ffe1c5a2SMarek Vasut struct i2c_client *client = to_i2c_client(dev);
529ffe1c5a2SMarek Vasut int status, tmp;
530ffe1c5a2SMarek Vasut
531ffe1c5a2SMarek Vasut switch (cmd) {
532ffe1c5a2SMarek Vasut case RTC_VL_READ:
533ffe1c5a2SMarek Vasut status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
534ffe1c5a2SMarek Vasut if (status < 0)
535ffe1c5a2SMarek Vasut return status;
536ffe1c5a2SMarek Vasut
5379f05342aSAlexandre Belloni tmp = status & ABX8XX_STATUS_BLF ? RTC_VL_BACKUP_LOW : 0;
538ffe1c5a2SMarek Vasut
5399f05342aSAlexandre Belloni return put_user(tmp, (unsigned int __user *)arg);
540ffe1c5a2SMarek Vasut
541ffe1c5a2SMarek Vasut case RTC_VL_CLR:
542ffe1c5a2SMarek Vasut status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
543ffe1c5a2SMarek Vasut if (status < 0)
544ffe1c5a2SMarek Vasut return status;
545ffe1c5a2SMarek Vasut
546ffe1c5a2SMarek Vasut status &= ~ABX8XX_STATUS_BLF;
547ffe1c5a2SMarek Vasut
548ffe1c5a2SMarek Vasut tmp = i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
549ffe1c5a2SMarek Vasut if (tmp < 0)
550ffe1c5a2SMarek Vasut return tmp;
551ffe1c5a2SMarek Vasut
552ffe1c5a2SMarek Vasut return 0;
553ffe1c5a2SMarek Vasut
554ffe1c5a2SMarek Vasut default:
555ffe1c5a2SMarek Vasut return -ENOIOCTLCMD;
556ffe1c5a2SMarek Vasut }
557ffe1c5a2SMarek Vasut }
558ffe1c5a2SMarek Vasut
5594d61ff6bSPhilippe De Muyter static const struct rtc_class_ops abx80x_rtc_ops = {
5604d61ff6bSPhilippe De Muyter .read_time = abx80x_rtc_read_time,
5614d61ff6bSPhilippe De Muyter .set_time = abx80x_rtc_set_time,
562718a820aSAlexandre Belloni .read_alarm = abx80x_read_alarm,
563718a820aSAlexandre Belloni .set_alarm = abx80x_set_alarm,
564718a820aSAlexandre Belloni .alarm_irq_enable = abx80x_alarm_irq_enable,
565ffe1c5a2SMarek Vasut .ioctl = abx80x_ioctl,
5664d61ff6bSPhilippe De Muyter };
5674d61ff6bSPhilippe De Muyter
abx80x_dt_trickle_cfg(struct i2c_client * client)5686e429f6bSKevin P. Fleming static int abx80x_dt_trickle_cfg(struct i2c_client *client)
5694d61ff6bSPhilippe De Muyter {
5706e429f6bSKevin P. Fleming struct device_node *np = client->dev.of_node;
5714d61ff6bSPhilippe De Muyter const char *diode;
5724d61ff6bSPhilippe De Muyter int trickle_cfg = 0;
5734d61ff6bSPhilippe De Muyter int i, ret;
5744d61ff6bSPhilippe De Muyter u32 tmp;
5754d61ff6bSPhilippe De Muyter
5764d61ff6bSPhilippe De Muyter ret = of_property_read_string(np, "abracon,tc-diode", &diode);
5774d61ff6bSPhilippe De Muyter if (ret)
5784d61ff6bSPhilippe De Muyter return ret;
5794d61ff6bSPhilippe De Muyter
5806e429f6bSKevin P. Fleming if (!strcmp(diode, "standard")) {
5814d61ff6bSPhilippe De Muyter trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
5826e429f6bSKevin P. Fleming } else if (!strcmp(diode, "schottky")) {
5834d61ff6bSPhilippe De Muyter trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
5846e429f6bSKevin P. Fleming } else {
5856e429f6bSKevin P. Fleming dev_dbg(&client->dev, "Invalid tc-diode value: %s\n", diode);
5864d61ff6bSPhilippe De Muyter return -EINVAL;
5876e429f6bSKevin P. Fleming }
5884d61ff6bSPhilippe De Muyter
5894d61ff6bSPhilippe De Muyter ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
5904d61ff6bSPhilippe De Muyter if (ret)
5914d61ff6bSPhilippe De Muyter return ret;
5924d61ff6bSPhilippe De Muyter
5934d61ff6bSPhilippe De Muyter for (i = 0; i < sizeof(trickle_resistors); i++)
5944d61ff6bSPhilippe De Muyter if (trickle_resistors[i] == tmp)
5954d61ff6bSPhilippe De Muyter break;
5964d61ff6bSPhilippe De Muyter
5976e429f6bSKevin P. Fleming if (i == sizeof(trickle_resistors)) {
5986e429f6bSKevin P. Fleming dev_dbg(&client->dev, "Invalid tc-resistor value: %u\n", tmp);
5994d61ff6bSPhilippe De Muyter return -EINVAL;
6006e429f6bSKevin P. Fleming }
6014d61ff6bSPhilippe De Muyter
6024d61ff6bSPhilippe De Muyter return (trickle_cfg | i);
6034d61ff6bSPhilippe De Muyter }
6044d61ff6bSPhilippe De Muyter
605749e36d0SJeremy Gebben #ifdef CONFIG_WATCHDOG
606749e36d0SJeremy Gebben
timeout_bits(unsigned int timeout)607749e36d0SJeremy Gebben static inline u8 timeout_bits(unsigned int timeout)
608749e36d0SJeremy Gebben {
609749e36d0SJeremy Gebben return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) |
610749e36d0SJeremy Gebben ABX8XX_WDT_WRB_1HZ;
611749e36d0SJeremy Gebben }
612749e36d0SJeremy Gebben
__abx80x_wdog_set_timeout(struct watchdog_device * wdog,unsigned int timeout)613749e36d0SJeremy Gebben static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog,
614749e36d0SJeremy Gebben unsigned int timeout)
615749e36d0SJeremy Gebben {
616749e36d0SJeremy Gebben struct abx80x_priv *priv = watchdog_get_drvdata(wdog);
617749e36d0SJeremy Gebben u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout);
618749e36d0SJeremy Gebben
619749e36d0SJeremy Gebben /*
620749e36d0SJeremy Gebben * Writing any timeout to the WDT register resets the watchdog timer.
621749e36d0SJeremy Gebben * Writing 0 disables it.
622749e36d0SJeremy Gebben */
623749e36d0SJeremy Gebben return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val);
624749e36d0SJeremy Gebben }
625749e36d0SJeremy Gebben
abx80x_wdog_set_timeout(struct watchdog_device * wdog,unsigned int new_timeout)626749e36d0SJeremy Gebben static int abx80x_wdog_set_timeout(struct watchdog_device *wdog,
627749e36d0SJeremy Gebben unsigned int new_timeout)
628749e36d0SJeremy Gebben {
629749e36d0SJeremy Gebben int err = 0;
630749e36d0SJeremy Gebben
631749e36d0SJeremy Gebben if (watchdog_hw_running(wdog))
632749e36d0SJeremy Gebben err = __abx80x_wdog_set_timeout(wdog, new_timeout);
633749e36d0SJeremy Gebben
634749e36d0SJeremy Gebben if (err == 0)
635749e36d0SJeremy Gebben wdog->timeout = new_timeout;
636749e36d0SJeremy Gebben
637749e36d0SJeremy Gebben return err;
638749e36d0SJeremy Gebben }
639749e36d0SJeremy Gebben
abx80x_wdog_ping(struct watchdog_device * wdog)640749e36d0SJeremy Gebben static int abx80x_wdog_ping(struct watchdog_device *wdog)
641749e36d0SJeremy Gebben {
642749e36d0SJeremy Gebben return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
643749e36d0SJeremy Gebben }
644749e36d0SJeremy Gebben
abx80x_wdog_start(struct watchdog_device * wdog)645749e36d0SJeremy Gebben static int abx80x_wdog_start(struct watchdog_device *wdog)
646749e36d0SJeremy Gebben {
647749e36d0SJeremy Gebben return __abx80x_wdog_set_timeout(wdog, wdog->timeout);
648749e36d0SJeremy Gebben }
649749e36d0SJeremy Gebben
abx80x_wdog_stop(struct watchdog_device * wdog)650749e36d0SJeremy Gebben static int abx80x_wdog_stop(struct watchdog_device *wdog)
651749e36d0SJeremy Gebben {
652749e36d0SJeremy Gebben return __abx80x_wdog_set_timeout(wdog, 0);
653749e36d0SJeremy Gebben }
654749e36d0SJeremy Gebben
655749e36d0SJeremy Gebben static const struct watchdog_info abx80x_wdog_info = {
656749e36d0SJeremy Gebben .identity = "abx80x watchdog",
657749e36d0SJeremy Gebben .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
658749e36d0SJeremy Gebben };
659749e36d0SJeremy Gebben
660749e36d0SJeremy Gebben static const struct watchdog_ops abx80x_wdog_ops = {
661749e36d0SJeremy Gebben .owner = THIS_MODULE,
662749e36d0SJeremy Gebben .start = abx80x_wdog_start,
663749e36d0SJeremy Gebben .stop = abx80x_wdog_stop,
664749e36d0SJeremy Gebben .ping = abx80x_wdog_ping,
665749e36d0SJeremy Gebben .set_timeout = abx80x_wdog_set_timeout,
666749e36d0SJeremy Gebben };
667749e36d0SJeremy Gebben
abx80x_setup_watchdog(struct abx80x_priv * priv)668749e36d0SJeremy Gebben static int abx80x_setup_watchdog(struct abx80x_priv *priv)
669749e36d0SJeremy Gebben {
670749e36d0SJeremy Gebben priv->wdog.parent = &priv->client->dev;
671749e36d0SJeremy Gebben priv->wdog.ops = &abx80x_wdog_ops;
672749e36d0SJeremy Gebben priv->wdog.info = &abx80x_wdog_info;
673749e36d0SJeremy Gebben priv->wdog.min_timeout = 1;
674749e36d0SJeremy Gebben priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME;
675749e36d0SJeremy Gebben priv->wdog.timeout = ABX8XX_WDT_MAX_TIME;
676749e36d0SJeremy Gebben
677749e36d0SJeremy Gebben watchdog_set_drvdata(&priv->wdog, priv);
678749e36d0SJeremy Gebben
679749e36d0SJeremy Gebben return devm_watchdog_register_device(&priv->client->dev, &priv->wdog);
680749e36d0SJeremy Gebben }
681749e36d0SJeremy Gebben #else
abx80x_setup_watchdog(struct abx80x_priv * priv)682749e36d0SJeremy Gebben static int abx80x_setup_watchdog(struct abx80x_priv *priv)
683749e36d0SJeremy Gebben {
684749e36d0SJeremy Gebben return 0;
685749e36d0SJeremy Gebben }
686749e36d0SJeremy Gebben #endif
687749e36d0SJeremy Gebben
abx80x_nvmem_xfer(struct abx80x_priv * priv,unsigned int offset,void * val,size_t bytes,bool write)688e90ff8edSSean Anderson static int abx80x_nvmem_xfer(struct abx80x_priv *priv, unsigned int offset,
689e90ff8edSSean Anderson void *val, size_t bytes, bool write)
690e90ff8edSSean Anderson {
691e90ff8edSSean Anderson int ret;
692e90ff8edSSean Anderson
693e90ff8edSSean Anderson while (bytes) {
694e90ff8edSSean Anderson u8 extram, reg, len, lower, upper;
695e90ff8edSSean Anderson
696e90ff8edSSean Anderson lower = FIELD_GET(NVMEM_ADDR_LOWER, offset);
697e90ff8edSSean Anderson upper = FIELD_GET(NVMEM_ADDR_UPPER, offset);
698e90ff8edSSean Anderson extram = FIELD_PREP(ABX8XX_EXTRAM_XADS, upper);
699e90ff8edSSean Anderson reg = ABX8XX_SRAM_BASE + lower;
700e90ff8edSSean Anderson len = min(lower + bytes, (size_t)ABX8XX_SRAM_WIN_SIZE) - lower;
701e90ff8edSSean Anderson len = min_t(u8, len, I2C_SMBUS_BLOCK_MAX);
702e90ff8edSSean Anderson
703e90ff8edSSean Anderson ret = i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_EXTRAM,
704e90ff8edSSean Anderson extram);
705e90ff8edSSean Anderson if (ret)
706e90ff8edSSean Anderson return ret;
707e90ff8edSSean Anderson
7080cff6d49SJoy Chakraborty if (write) {
709e90ff8edSSean Anderson ret = i2c_smbus_write_i2c_block_data(priv->client, reg,
710e90ff8edSSean Anderson len, val);
711e90ff8edSSean Anderson if (ret)
712e90ff8edSSean Anderson return ret;
7130cff6d49SJoy Chakraborty } else {
7140cff6d49SJoy Chakraborty ret = i2c_smbus_read_i2c_block_data(priv->client, reg,
7150cff6d49SJoy Chakraborty len, val);
7160cff6d49SJoy Chakraborty if (ret <= 0)
7170cff6d49SJoy Chakraborty return ret ? ret : -EIO;
7180cff6d49SJoy Chakraborty len = ret;
7190cff6d49SJoy Chakraborty }
720e90ff8edSSean Anderson
721e90ff8edSSean Anderson offset += len;
722e90ff8edSSean Anderson val += len;
723e90ff8edSSean Anderson bytes -= len;
724e90ff8edSSean Anderson }
725e90ff8edSSean Anderson
726e90ff8edSSean Anderson return 0;
727e90ff8edSSean Anderson }
728e90ff8edSSean Anderson
abx80x_nvmem_read(void * priv,unsigned int offset,void * val,size_t bytes)729e90ff8edSSean Anderson static int abx80x_nvmem_read(void *priv, unsigned int offset, void *val,
730e90ff8edSSean Anderson size_t bytes)
731e90ff8edSSean Anderson {
732e90ff8edSSean Anderson return abx80x_nvmem_xfer(priv, offset, val, bytes, false);
733e90ff8edSSean Anderson }
734e90ff8edSSean Anderson
abx80x_nvmem_write(void * priv,unsigned int offset,void * val,size_t bytes)735e90ff8edSSean Anderson static int abx80x_nvmem_write(void *priv, unsigned int offset, void *val,
736e90ff8edSSean Anderson size_t bytes)
737e90ff8edSSean Anderson {
738e90ff8edSSean Anderson return abx80x_nvmem_xfer(priv, offset, val, bytes, true);
739e90ff8edSSean Anderson }
740e90ff8edSSean Anderson
abx80x_setup_nvmem(struct abx80x_priv * priv)741e90ff8edSSean Anderson static int abx80x_setup_nvmem(struct abx80x_priv *priv)
742e90ff8edSSean Anderson {
743e90ff8edSSean Anderson struct nvmem_config config = {
744e90ff8edSSean Anderson .type = NVMEM_TYPE_BATTERY_BACKED,
745e90ff8edSSean Anderson .reg_read = abx80x_nvmem_read,
746e90ff8edSSean Anderson .reg_write = abx80x_nvmem_write,
747e90ff8edSSean Anderson .size = ABX8XX_RAM_SIZE,
748e90ff8edSSean Anderson .priv = priv,
749e90ff8edSSean Anderson };
750e90ff8edSSean Anderson
751e90ff8edSSean Anderson return devm_rtc_nvmem_register(priv->rtc, &config);
752e90ff8edSSean Anderson }
753e90ff8edSSean Anderson
75465d9cc3fSUwe Kleine-König static const struct i2c_device_id abx80x_id[] = {
75565d9cc3fSUwe Kleine-König { "abx80x", ABX80X },
75665d9cc3fSUwe Kleine-König { "ab0801", AB0801 },
75765d9cc3fSUwe Kleine-König { "ab0803", AB0803 },
75865d9cc3fSUwe Kleine-König { "ab0804", AB0804 },
75965d9cc3fSUwe Kleine-König { "ab0805", AB0805 },
76065d9cc3fSUwe Kleine-König { "ab1801", AB1801 },
76165d9cc3fSUwe Kleine-König { "ab1803", AB1803 },
76265d9cc3fSUwe Kleine-König { "ab1804", AB1804 },
76365d9cc3fSUwe Kleine-König { "ab1805", AB1805 },
76465d9cc3fSUwe Kleine-König { "rv1805", RV1805 },
76565d9cc3fSUwe Kleine-König { }
76665d9cc3fSUwe Kleine-König };
76765d9cc3fSUwe Kleine-König MODULE_DEVICE_TABLE(i2c, abx80x_id);
76865d9cc3fSUwe Kleine-König
abx80x_probe(struct i2c_client * client)76965d9cc3fSUwe Kleine-König static int abx80x_probe(struct i2c_client *client)
7704d61ff6bSPhilippe De Muyter {
7714d61ff6bSPhilippe De Muyter struct device_node *np = client->dev.of_node;
772af69f9a7SJeremy Gebben struct abx80x_priv *priv;
7734d61ff6bSPhilippe De Muyter int i, data, err, trickle_cfg = -EINVAL;
7744d61ff6bSPhilippe De Muyter char buf[7];
77565d9cc3fSUwe Kleine-König const struct i2c_device_id *id = i2c_match_id(abx80x_id, client);
7764d61ff6bSPhilippe De Muyter unsigned int part = id->driver_data;
7774d61ff6bSPhilippe De Muyter unsigned int partnumber;
7784d61ff6bSPhilippe De Muyter unsigned int majrev, minrev;
7794d61ff6bSPhilippe De Muyter unsigned int lot;
7804d61ff6bSPhilippe De Muyter unsigned int wafer;
7814d61ff6bSPhilippe De Muyter unsigned int uid;
7824d61ff6bSPhilippe De Muyter
7834d61ff6bSPhilippe De Muyter if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
7844d61ff6bSPhilippe De Muyter return -ENODEV;
7854d61ff6bSPhilippe De Muyter
7864d61ff6bSPhilippe De Muyter err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
7874d61ff6bSPhilippe De Muyter sizeof(buf), buf);
7884d61ff6bSPhilippe De Muyter if (err < 0) {
7894d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unable to read partnumber\n");
7904d61ff6bSPhilippe De Muyter return -EIO;
7914d61ff6bSPhilippe De Muyter }
7924d61ff6bSPhilippe De Muyter
7934d61ff6bSPhilippe De Muyter partnumber = (buf[0] << 8) | buf[1];
7944d61ff6bSPhilippe De Muyter majrev = buf[2] >> 3;
7954d61ff6bSPhilippe De Muyter minrev = buf[2] & 0x7;
7964d61ff6bSPhilippe De Muyter lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
7974d61ff6bSPhilippe De Muyter uid = ((buf[4] & 0x7f) << 8) | buf[5];
7984d61ff6bSPhilippe De Muyter wafer = (buf[6] & 0x7c) >> 2;
7994d61ff6bSPhilippe De Muyter dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
8004d61ff6bSPhilippe De Muyter partnumber, majrev, minrev, lot, wafer, uid);
8014d61ff6bSPhilippe De Muyter
8024d61ff6bSPhilippe De Muyter data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
8034d61ff6bSPhilippe De Muyter if (data < 0) {
8044d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unable to read control register\n");
8054d61ff6bSPhilippe De Muyter return -EIO;
8064d61ff6bSPhilippe De Muyter }
8074d61ff6bSPhilippe De Muyter
8084d61ff6bSPhilippe De Muyter err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
809718a820aSAlexandre Belloni ((data & ~(ABX8XX_CTRL_12_24 |
810718a820aSAlexandre Belloni ABX8XX_CTRL_ARST)) |
8114d61ff6bSPhilippe De Muyter ABX8XX_CTRL_WRITE));
8124d61ff6bSPhilippe De Muyter if (err < 0) {
8134d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unable to write control register\n");
8144d61ff6bSPhilippe De Muyter return -EIO;
8154d61ff6bSPhilippe De Muyter }
8164d61ff6bSPhilippe De Muyter
81775455e25SMarek Vasut /* Configure RV1805 specifics */
81875455e25SMarek Vasut if (part == RV1805) {
81975455e25SMarek Vasut /*
82075455e25SMarek Vasut * Avoid accidentally entering test mode. This can happen
82175455e25SMarek Vasut * on the RV1805 in case the reserved bit 5 in control2
82275455e25SMarek Vasut * register is set. RV-1805-C3 datasheet indicates that
82375455e25SMarek Vasut * the bit should be cleared in section 11h - Control2.
82475455e25SMarek Vasut */
82575455e25SMarek Vasut data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL2);
82675455e25SMarek Vasut if (data < 0) {
82775455e25SMarek Vasut dev_err(&client->dev,
82875455e25SMarek Vasut "Unable to read control2 register\n");
82975455e25SMarek Vasut return -EIO;
83075455e25SMarek Vasut }
83175455e25SMarek Vasut
83275455e25SMarek Vasut err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL2,
83375455e25SMarek Vasut data & ~ABX8XX_CTRL2_RSVD);
83475455e25SMarek Vasut if (err < 0) {
83575455e25SMarek Vasut dev_err(&client->dev,
83675455e25SMarek Vasut "Unable to write control2 register\n");
83775455e25SMarek Vasut return -EIO;
83875455e25SMarek Vasut }
83975455e25SMarek Vasut
84075455e25SMarek Vasut /*
84175455e25SMarek Vasut * Avoid extra power leakage. The RV1805 uses smaller
84275455e25SMarek Vasut * 10pin package and the EXTI input is not present.
84375455e25SMarek Vasut * Disable it to avoid leakage.
84475455e25SMarek Vasut */
84575455e25SMarek Vasut data = i2c_smbus_read_byte_data(client, ABX8XX_REG_OUT_CTRL);
84675455e25SMarek Vasut if (data < 0) {
84775455e25SMarek Vasut dev_err(&client->dev,
84875455e25SMarek Vasut "Unable to read output control register\n");
84975455e25SMarek Vasut return -EIO;
85075455e25SMarek Vasut }
85175455e25SMarek Vasut
85275455e25SMarek Vasut /*
85375455e25SMarek Vasut * Write the configuration key register to enable access to
85475455e25SMarek Vasut * the config2 register
85575455e25SMarek Vasut */
85649dfc1f1SKevin P. Fleming if (abx80x_write_config_key(client, ABX8XX_CFG_KEY_MISC) < 0)
85775455e25SMarek Vasut return -EIO;
85875455e25SMarek Vasut
85975455e25SMarek Vasut err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OUT_CTRL,
86075455e25SMarek Vasut data | ABX8XX_OUT_CTRL_EXDS);
86175455e25SMarek Vasut if (err < 0) {
86275455e25SMarek Vasut dev_err(&client->dev,
86375455e25SMarek Vasut "Unable to write output control register\n");
86475455e25SMarek Vasut return -EIO;
86575455e25SMarek Vasut }
86675455e25SMarek Vasut }
86775455e25SMarek Vasut
8684d61ff6bSPhilippe De Muyter /* part autodetection */
8694d61ff6bSPhilippe De Muyter if (part == ABX80X) {
8704d61ff6bSPhilippe De Muyter for (i = 0; abx80x_caps[i].pn; i++)
8714d61ff6bSPhilippe De Muyter if (partnumber == abx80x_caps[i].pn)
8724d61ff6bSPhilippe De Muyter break;
8734d61ff6bSPhilippe De Muyter if (abx80x_caps[i].pn == 0) {
8744d61ff6bSPhilippe De Muyter dev_err(&client->dev, "Unknown part: %04x\n",
8754d61ff6bSPhilippe De Muyter partnumber);
8764d61ff6bSPhilippe De Muyter return -EINVAL;
8774d61ff6bSPhilippe De Muyter }
8784d61ff6bSPhilippe De Muyter part = i;
8794d61ff6bSPhilippe De Muyter }
8804d61ff6bSPhilippe De Muyter
8814d61ff6bSPhilippe De Muyter if (partnumber != abx80x_caps[part].pn) {
8824d61ff6bSPhilippe De Muyter dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
8834d61ff6bSPhilippe De Muyter partnumber, abx80x_caps[part].pn);
8844d61ff6bSPhilippe De Muyter return -EINVAL;
8854d61ff6bSPhilippe De Muyter }
8864d61ff6bSPhilippe De Muyter
8874d61ff6bSPhilippe De Muyter if (np && abx80x_caps[part].has_tc)
8886e429f6bSKevin P. Fleming trickle_cfg = abx80x_dt_trickle_cfg(client);
8894d61ff6bSPhilippe De Muyter
8904d61ff6bSPhilippe De Muyter if (trickle_cfg > 0) {
8914d61ff6bSPhilippe De Muyter dev_info(&client->dev, "Enabling trickle charger: %02x\n",
8924d61ff6bSPhilippe De Muyter trickle_cfg);
8934d61ff6bSPhilippe De Muyter abx80x_enable_trickle_charger(client, trickle_cfg);
8944d61ff6bSPhilippe De Muyter }
8954d61ff6bSPhilippe De Muyter
896718a820aSAlexandre Belloni err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
897718a820aSAlexandre Belloni BIT(2));
898718a820aSAlexandre Belloni if (err)
899718a820aSAlexandre Belloni return err;
900718a820aSAlexandre Belloni
901af69f9a7SJeremy Gebben priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
902af69f9a7SJeremy Gebben if (priv == NULL)
903af69f9a7SJeremy Gebben return -ENOMEM;
9044d61ff6bSPhilippe De Muyter
905af69f9a7SJeremy Gebben priv->rtc = devm_rtc_allocate_device(&client->dev);
906af69f9a7SJeremy Gebben if (IS_ERR(priv->rtc))
907af69f9a7SJeremy Gebben return PTR_ERR(priv->rtc);
9089360a6a8SAlexandre Belloni
909af69f9a7SJeremy Gebben priv->rtc->ops = &abx80x_rtc_ops;
910af69f9a7SJeremy Gebben priv->client = client;
911af69f9a7SJeremy Gebben
912af69f9a7SJeremy Gebben i2c_set_clientdata(client, priv);
9134d61ff6bSPhilippe De Muyter
914749e36d0SJeremy Gebben if (abx80x_caps[part].has_wdog) {
915749e36d0SJeremy Gebben err = abx80x_setup_watchdog(priv);
916749e36d0SJeremy Gebben if (err)
917749e36d0SJeremy Gebben return err;
918749e36d0SJeremy Gebben }
919749e36d0SJeremy Gebben
920e90ff8edSSean Anderson err = abx80x_setup_nvmem(priv);
921e90ff8edSSean Anderson if (err)
922e90ff8edSSean Anderson return err;
923e90ff8edSSean Anderson
924718a820aSAlexandre Belloni if (client->irq > 0) {
925718a820aSAlexandre Belloni dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
926718a820aSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
927718a820aSAlexandre Belloni abx80x_handle_irq,
928718a820aSAlexandre Belloni IRQF_SHARED | IRQF_ONESHOT,
929718a820aSAlexandre Belloni "abx8xx",
930718a820aSAlexandre Belloni client);
931718a820aSAlexandre Belloni if (err) {
932718a820aSAlexandre Belloni dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
933718a820aSAlexandre Belloni client->irq = 0;
934718a820aSAlexandre Belloni }
935718a820aSAlexandre Belloni }
936718a820aSAlexandre Belloni
937559e883eSAlexandre Belloni err = rtc_add_group(priv->rtc, &rtc_calib_attr_group);
93859a8383aSMylène Josserand if (err) {
93959a8383aSMylène Josserand dev_err(&client->dev, "Failed to create sysfs group: %d\n",
94059a8383aSMylène Josserand err);
94159a8383aSMylène Josserand return err;
94259a8383aSMylène Josserand }
94359a8383aSMylène Josserand
944fdcfd854SBartosz Golaszewski return devm_rtc_register_device(priv->rtc);
9454d61ff6bSPhilippe De Muyter }
9464d61ff6bSPhilippe De Muyter
947ac363aceSKevin P. Fleming #ifdef CONFIG_OF
948ac363aceSKevin P. Fleming static const struct of_device_id abx80x_of_match[] = {
949ac363aceSKevin P. Fleming {
950ac363aceSKevin P. Fleming .compatible = "abracon,abx80x",
951ac363aceSKevin P. Fleming .data = (void *)ABX80X
952ac363aceSKevin P. Fleming },
953ac363aceSKevin P. Fleming {
954ac363aceSKevin P. Fleming .compatible = "abracon,ab0801",
955ac363aceSKevin P. Fleming .data = (void *)AB0801
956ac363aceSKevin P. Fleming },
957ac363aceSKevin P. Fleming {
958ac363aceSKevin P. Fleming .compatible = "abracon,ab0803",
959ac363aceSKevin P. Fleming .data = (void *)AB0803
960ac363aceSKevin P. Fleming },
961ac363aceSKevin P. Fleming {
962ac363aceSKevin P. Fleming .compatible = "abracon,ab0804",
963ac363aceSKevin P. Fleming .data = (void *)AB0804
964ac363aceSKevin P. Fleming },
965ac363aceSKevin P. Fleming {
966ac363aceSKevin P. Fleming .compatible = "abracon,ab0805",
967ac363aceSKevin P. Fleming .data = (void *)AB0805
968ac363aceSKevin P. Fleming },
969ac363aceSKevin P. Fleming {
970ac363aceSKevin P. Fleming .compatible = "abracon,ab1801",
971ac363aceSKevin P. Fleming .data = (void *)AB1801
972ac363aceSKevin P. Fleming },
973ac363aceSKevin P. Fleming {
974ac363aceSKevin P. Fleming .compatible = "abracon,ab1803",
975ac363aceSKevin P. Fleming .data = (void *)AB1803
976ac363aceSKevin P. Fleming },
977ac363aceSKevin P. Fleming {
978ac363aceSKevin P. Fleming .compatible = "abracon,ab1804",
979ac363aceSKevin P. Fleming .data = (void *)AB1804
980ac363aceSKevin P. Fleming },
981ac363aceSKevin P. Fleming {
982ac363aceSKevin P. Fleming .compatible = "abracon,ab1805",
983ac363aceSKevin P. Fleming .data = (void *)AB1805
984ac363aceSKevin P. Fleming },
985ac363aceSKevin P. Fleming {
986ac363aceSKevin P. Fleming .compatible = "microcrystal,rv1805",
987ac363aceSKevin P. Fleming .data = (void *)RV1805
988ac363aceSKevin P. Fleming },
989ac363aceSKevin P. Fleming { }
990ac363aceSKevin P. Fleming };
991ac363aceSKevin P. Fleming MODULE_DEVICE_TABLE(of, abx80x_of_match);
992ac363aceSKevin P. Fleming #endif
993ac363aceSKevin P. Fleming
9944d61ff6bSPhilippe De Muyter static struct i2c_driver abx80x_driver = {
9954d61ff6bSPhilippe De Muyter .driver = {
9964d61ff6bSPhilippe De Muyter .name = "rtc-abx80x",
997ac363aceSKevin P. Fleming .of_match_table = of_match_ptr(abx80x_of_match),
9984d61ff6bSPhilippe De Muyter },
99931b0cecbSUwe Kleine-König .probe = abx80x_probe,
10004d61ff6bSPhilippe De Muyter .id_table = abx80x_id,
10014d61ff6bSPhilippe De Muyter };
10024d61ff6bSPhilippe De Muyter
10034d61ff6bSPhilippe De Muyter module_i2c_driver(abx80x_driver);
10044d61ff6bSPhilippe De Muyter
10054d61ff6bSPhilippe De Muyter MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
10067d1e5bfeSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
10074d61ff6bSPhilippe De Muyter MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
10084d61ff6bSPhilippe De Muyter MODULE_LICENSE("GPL v2");
1009