18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * A driver for the I2C members of the Abracon AB x8xx RTC family, 48c2ecf20Sopenharmony_ci * and compatible: AB 1805 and AB 0805 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright 2014-2015 Macq S.A. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Author: Philippe De Muyter <phdm@macqel.be> 98c2ecf20Sopenharmony_ci * Author: Alexandre Belloni <alexandre.belloni@bootlin.com> 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/bcd.h> 148c2ecf20Sopenharmony_ci#include <linux/i2c.h> 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/of_device.h> 178c2ecf20Sopenharmony_ci#include <linux/rtc.h> 188c2ecf20Sopenharmony_ci#include <linux/watchdog.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define ABX8XX_REG_HTH 0x00 218c2ecf20Sopenharmony_ci#define ABX8XX_REG_SC 0x01 228c2ecf20Sopenharmony_ci#define ABX8XX_REG_MN 0x02 238c2ecf20Sopenharmony_ci#define ABX8XX_REG_HR 0x03 248c2ecf20Sopenharmony_ci#define ABX8XX_REG_DA 0x04 258c2ecf20Sopenharmony_ci#define ABX8XX_REG_MO 0x05 268c2ecf20Sopenharmony_ci#define ABX8XX_REG_YR 0x06 278c2ecf20Sopenharmony_ci#define ABX8XX_REG_WD 0x07 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define ABX8XX_REG_AHTH 0x08 308c2ecf20Sopenharmony_ci#define ABX8XX_REG_ASC 0x09 318c2ecf20Sopenharmony_ci#define ABX8XX_REG_AMN 0x0a 328c2ecf20Sopenharmony_ci#define ABX8XX_REG_AHR 0x0b 338c2ecf20Sopenharmony_ci#define ABX8XX_REG_ADA 0x0c 348c2ecf20Sopenharmony_ci#define ABX8XX_REG_AMO 0x0d 358c2ecf20Sopenharmony_ci#define ABX8XX_REG_AWD 0x0e 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define ABX8XX_REG_STATUS 0x0f 388c2ecf20Sopenharmony_ci#define ABX8XX_STATUS_AF BIT(2) 398c2ecf20Sopenharmony_ci#define ABX8XX_STATUS_BLF BIT(4) 408c2ecf20Sopenharmony_ci#define ABX8XX_STATUS_WDT BIT(6) 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define ABX8XX_REG_CTRL1 0x10 438c2ecf20Sopenharmony_ci#define ABX8XX_CTRL_WRITE BIT(0) 448c2ecf20Sopenharmony_ci#define ABX8XX_CTRL_ARST BIT(2) 458c2ecf20Sopenharmony_ci#define ABX8XX_CTRL_12_24 BIT(6) 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define ABX8XX_REG_CTRL2 0x11 488c2ecf20Sopenharmony_ci#define ABX8XX_CTRL2_RSVD BIT(5) 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define ABX8XX_REG_IRQ 0x12 518c2ecf20Sopenharmony_ci#define ABX8XX_IRQ_AIE BIT(2) 528c2ecf20Sopenharmony_ci#define ABX8XX_IRQ_IM_1_4 (0x3 << 5) 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define ABX8XX_REG_CD_TIMER_CTL 0x18 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci#define ABX8XX_REG_OSC 0x1c 578c2ecf20Sopenharmony_ci#define ABX8XX_OSC_FOS BIT(3) 588c2ecf20Sopenharmony_ci#define ABX8XX_OSC_BOS BIT(4) 598c2ecf20Sopenharmony_ci#define ABX8XX_OSC_ACAL_512 BIT(5) 608c2ecf20Sopenharmony_ci#define ABX8XX_OSC_ACAL_1024 BIT(6) 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#define ABX8XX_OSC_OSEL BIT(7) 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define ABX8XX_REG_OSS 0x1d 658c2ecf20Sopenharmony_ci#define ABX8XX_OSS_OF BIT(1) 668c2ecf20Sopenharmony_ci#define ABX8XX_OSS_OMODE BIT(4) 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci#define ABX8XX_REG_WDT 0x1b 698c2ecf20Sopenharmony_ci#define ABX8XX_WDT_WDS BIT(7) 708c2ecf20Sopenharmony_ci#define ABX8XX_WDT_BMB_MASK 0x7c 718c2ecf20Sopenharmony_ci#define ABX8XX_WDT_BMB_SHIFT 2 728c2ecf20Sopenharmony_ci#define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT) 738c2ecf20Sopenharmony_ci#define ABX8XX_WDT_WRB_MASK 0x03 748c2ecf20Sopenharmony_ci#define ABX8XX_WDT_WRB_1HZ 0x02 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci#define ABX8XX_REG_CFG_KEY 0x1f 778c2ecf20Sopenharmony_ci#define ABX8XX_CFG_KEY_OSC 0xa1 788c2ecf20Sopenharmony_ci#define ABX8XX_CFG_KEY_MISC 0x9d 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci#define ABX8XX_REG_ID0 0x28 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci#define ABX8XX_REG_OUT_CTRL 0x30 838c2ecf20Sopenharmony_ci#define ABX8XX_OUT_CTRL_EXDS BIT(4) 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci#define ABX8XX_REG_TRICKLE 0x20 868c2ecf20Sopenharmony_ci#define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0 878c2ecf20Sopenharmony_ci#define ABX8XX_TRICKLE_STANDARD_DIODE 0x8 888c2ecf20Sopenharmony_ci#define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic u8 trickle_resistors[] = {0, 3, 6, 11}; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cienum abx80x_chip {AB0801, AB0803, AB0804, AB0805, 938c2ecf20Sopenharmony_ci AB1801, AB1803, AB1804, AB1805, RV1805, ABX80X}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistruct abx80x_cap { 968c2ecf20Sopenharmony_ci u16 pn; 978c2ecf20Sopenharmony_ci bool has_tc; 988c2ecf20Sopenharmony_ci bool has_wdog; 998c2ecf20Sopenharmony_ci}; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic struct abx80x_cap abx80x_caps[] = { 1028c2ecf20Sopenharmony_ci [AB0801] = {.pn = 0x0801}, 1038c2ecf20Sopenharmony_ci [AB0803] = {.pn = 0x0803}, 1048c2ecf20Sopenharmony_ci [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true}, 1058c2ecf20Sopenharmony_ci [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true}, 1068c2ecf20Sopenharmony_ci [AB1801] = {.pn = 0x1801}, 1078c2ecf20Sopenharmony_ci [AB1803] = {.pn = 0x1803}, 1088c2ecf20Sopenharmony_ci [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true}, 1098c2ecf20Sopenharmony_ci [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true}, 1108c2ecf20Sopenharmony_ci [RV1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true}, 1118c2ecf20Sopenharmony_ci [ABX80X] = {.pn = 0} 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistruct abx80x_priv { 1158c2ecf20Sopenharmony_ci struct rtc_device *rtc; 1168c2ecf20Sopenharmony_ci struct i2c_client *client; 1178c2ecf20Sopenharmony_ci struct watchdog_device wdog; 1188c2ecf20Sopenharmony_ci}; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic int abx80x_is_rc_mode(struct i2c_client *client) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci int flags = 0; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); 1258c2ecf20Sopenharmony_ci if (flags < 0) { 1268c2ecf20Sopenharmony_ci dev_err(&client->dev, 1278c2ecf20Sopenharmony_ci "Failed to read autocalibration attribute\n"); 1288c2ecf20Sopenharmony_ci return flags; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci return (flags & ABX8XX_OSS_OMODE) ? 1 : 0; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic int abx80x_enable_trickle_charger(struct i2c_client *client, 1358c2ecf20Sopenharmony_ci u8 trickle_cfg) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci int err; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* 1408c2ecf20Sopenharmony_ci * Write the configuration key register to enable access to the Trickle 1418c2ecf20Sopenharmony_ci * register 1428c2ecf20Sopenharmony_ci */ 1438c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 1448c2ecf20Sopenharmony_ci ABX8XX_CFG_KEY_MISC); 1458c2ecf20Sopenharmony_ci if (err < 0) { 1468c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to write configuration key\n"); 1478c2ecf20Sopenharmony_ci return -EIO; 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE, 1518c2ecf20Sopenharmony_ci ABX8XX_TRICKLE_CHARGE_ENABLE | 1528c2ecf20Sopenharmony_ci trickle_cfg); 1538c2ecf20Sopenharmony_ci if (err < 0) { 1548c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to write trickle register\n"); 1558c2ecf20Sopenharmony_ci return -EIO; 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci return 0; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 1648c2ecf20Sopenharmony_ci unsigned char buf[8]; 1658c2ecf20Sopenharmony_ci int err, flags, rc_mode = 0; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci /* Read the Oscillator Failure only in XT mode */ 1688c2ecf20Sopenharmony_ci rc_mode = abx80x_is_rc_mode(client); 1698c2ecf20Sopenharmony_ci if (rc_mode < 0) 1708c2ecf20Sopenharmony_ci return rc_mode; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci if (!rc_mode) { 1738c2ecf20Sopenharmony_ci flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); 1748c2ecf20Sopenharmony_ci if (flags < 0) 1758c2ecf20Sopenharmony_ci return flags; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci if (flags & ABX8XX_OSS_OF) { 1788c2ecf20Sopenharmony_ci dev_err(dev, "Oscillator failure, data is invalid.\n"); 1798c2ecf20Sopenharmony_ci return -EINVAL; 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH, 1848c2ecf20Sopenharmony_ci sizeof(buf), buf); 1858c2ecf20Sopenharmony_ci if (err < 0) { 1868c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to read date\n"); 1878c2ecf20Sopenharmony_ci return -EIO; 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F); 1918c2ecf20Sopenharmony_ci tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F); 1928c2ecf20Sopenharmony_ci tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F); 1938c2ecf20Sopenharmony_ci tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7; 1948c2ecf20Sopenharmony_ci tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F); 1958c2ecf20Sopenharmony_ci tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1; 1968c2ecf20Sopenharmony_ci tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci return 0; 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2048c2ecf20Sopenharmony_ci unsigned char buf[8]; 2058c2ecf20Sopenharmony_ci int err, flags; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (tm->tm_year < 100) 2088c2ecf20Sopenharmony_ci return -EINVAL; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci buf[ABX8XX_REG_HTH] = 0; 2118c2ecf20Sopenharmony_ci buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec); 2128c2ecf20Sopenharmony_ci buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min); 2138c2ecf20Sopenharmony_ci buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour); 2148c2ecf20Sopenharmony_ci buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday); 2158c2ecf20Sopenharmony_ci buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1); 2168c2ecf20Sopenharmony_ci buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100); 2178c2ecf20Sopenharmony_ci buf[ABX8XX_REG_WD] = tm->tm_wday; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH, 2208c2ecf20Sopenharmony_ci sizeof(buf), buf); 2218c2ecf20Sopenharmony_ci if (err < 0) { 2228c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to write to date registers\n"); 2238c2ecf20Sopenharmony_ci return -EIO; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci /* Clear the OF bit of Oscillator Status Register */ 2278c2ecf20Sopenharmony_ci flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); 2288c2ecf20Sopenharmony_ci if (flags < 0) 2298c2ecf20Sopenharmony_ci return flags; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS, 2328c2ecf20Sopenharmony_ci flags & ~ABX8XX_OSS_OF); 2338c2ecf20Sopenharmony_ci if (err < 0) { 2348c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to write oscillator status register\n"); 2358c2ecf20Sopenharmony_ci return err; 2368c2ecf20Sopenharmony_ci } 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci return 0; 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_cistatic irqreturn_t abx80x_handle_irq(int irq, void *dev_id) 2428c2ecf20Sopenharmony_ci{ 2438c2ecf20Sopenharmony_ci struct i2c_client *client = dev_id; 2448c2ecf20Sopenharmony_ci struct abx80x_priv *priv = i2c_get_clientdata(client); 2458c2ecf20Sopenharmony_ci struct rtc_device *rtc = priv->rtc; 2468c2ecf20Sopenharmony_ci int status; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); 2498c2ecf20Sopenharmony_ci if (status < 0) 2508c2ecf20Sopenharmony_ci return IRQ_NONE; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci if (status & ABX8XX_STATUS_AF) 2538c2ecf20Sopenharmony_ci rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci /* 2568c2ecf20Sopenharmony_ci * It is unclear if we'll get an interrupt before the external 2578c2ecf20Sopenharmony_ci * reset kicks in. 2588c2ecf20Sopenharmony_ci */ 2598c2ecf20Sopenharmony_ci if (status & ABX8XX_STATUS_WDT) 2608c2ecf20Sopenharmony_ci dev_alert(&client->dev, "watchdog timeout interrupt.\n"); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2658c2ecf20Sopenharmony_ci} 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cistatic int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t) 2688c2ecf20Sopenharmony_ci{ 2698c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 2708c2ecf20Sopenharmony_ci unsigned char buf[7]; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci int irq_mask, err; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci if (client->irq <= 0) 2758c2ecf20Sopenharmony_ci return -EINVAL; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC, 2788c2ecf20Sopenharmony_ci sizeof(buf), buf); 2798c2ecf20Sopenharmony_ci if (err) 2808c2ecf20Sopenharmony_ci return err; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ); 2838c2ecf20Sopenharmony_ci if (irq_mask < 0) 2848c2ecf20Sopenharmony_ci return irq_mask; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci t->time.tm_sec = bcd2bin(buf[0] & 0x7F); 2878c2ecf20Sopenharmony_ci t->time.tm_min = bcd2bin(buf[1] & 0x7F); 2888c2ecf20Sopenharmony_ci t->time.tm_hour = bcd2bin(buf[2] & 0x3F); 2898c2ecf20Sopenharmony_ci t->time.tm_mday = bcd2bin(buf[3] & 0x3F); 2908c2ecf20Sopenharmony_ci t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1; 2918c2ecf20Sopenharmony_ci t->time.tm_wday = buf[5] & 0x7; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE); 2948c2ecf20Sopenharmony_ci t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci return err; 2978c2ecf20Sopenharmony_ci} 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 3028c2ecf20Sopenharmony_ci u8 alarm[6]; 3038c2ecf20Sopenharmony_ci int err; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci if (client->irq <= 0) 3068c2ecf20Sopenharmony_ci return -EINVAL; 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci alarm[0] = 0x0; 3098c2ecf20Sopenharmony_ci alarm[1] = bin2bcd(t->time.tm_sec); 3108c2ecf20Sopenharmony_ci alarm[2] = bin2bcd(t->time.tm_min); 3118c2ecf20Sopenharmony_ci alarm[3] = bin2bcd(t->time.tm_hour); 3128c2ecf20Sopenharmony_ci alarm[4] = bin2bcd(t->time.tm_mday); 3138c2ecf20Sopenharmony_ci alarm[5] = bin2bcd(t->time.tm_mon + 1); 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH, 3168c2ecf20Sopenharmony_ci sizeof(alarm), alarm); 3178c2ecf20Sopenharmony_ci if (err < 0) { 3188c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to write alarm registers\n"); 3198c2ecf20Sopenharmony_ci return -EIO; 3208c2ecf20Sopenharmony_ci } 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci if (t->enabled) { 3238c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, 3248c2ecf20Sopenharmony_ci (ABX8XX_IRQ_IM_1_4 | 3258c2ecf20Sopenharmony_ci ABX8XX_IRQ_AIE)); 3268c2ecf20Sopenharmony_ci if (err) 3278c2ecf20Sopenharmony_ci return err; 3288c2ecf20Sopenharmony_ci } 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return 0; 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic int abx80x_rtc_set_autocalibration(struct device *dev, 3348c2ecf20Sopenharmony_ci int autocalibration) 3358c2ecf20Sopenharmony_ci{ 3368c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 3378c2ecf20Sopenharmony_ci int retval, flags = 0; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci if ((autocalibration != 0) && (autocalibration != 1024) && 3408c2ecf20Sopenharmony_ci (autocalibration != 512)) { 3418c2ecf20Sopenharmony_ci dev_err(dev, "autocalibration value outside permitted range\n"); 3428c2ecf20Sopenharmony_ci return -EINVAL; 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); 3468c2ecf20Sopenharmony_ci if (flags < 0) 3478c2ecf20Sopenharmony_ci return flags; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci if (autocalibration == 0) { 3508c2ecf20Sopenharmony_ci flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024); 3518c2ecf20Sopenharmony_ci } else if (autocalibration == 1024) { 3528c2ecf20Sopenharmony_ci /* 1024 autocalibration is 0x10 */ 3538c2ecf20Sopenharmony_ci flags |= ABX8XX_OSC_ACAL_1024; 3548c2ecf20Sopenharmony_ci flags &= ~(ABX8XX_OSC_ACAL_512); 3558c2ecf20Sopenharmony_ci } else { 3568c2ecf20Sopenharmony_ci /* 512 autocalibration is 0x11 */ 3578c2ecf20Sopenharmony_ci flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512); 3588c2ecf20Sopenharmony_ci } 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci /* Unlock write access to Oscillator Control Register */ 3618c2ecf20Sopenharmony_ci retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 3628c2ecf20Sopenharmony_ci ABX8XX_CFG_KEY_OSC); 3638c2ecf20Sopenharmony_ci if (retval < 0) { 3648c2ecf20Sopenharmony_ci dev_err(dev, "Failed to write CONFIG_KEY register\n"); 3658c2ecf20Sopenharmony_ci return retval; 3668c2ecf20Sopenharmony_ci } 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci return retval; 3718c2ecf20Sopenharmony_ci} 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_cistatic int abx80x_rtc_get_autocalibration(struct device *dev) 3748c2ecf20Sopenharmony_ci{ 3758c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 3768c2ecf20Sopenharmony_ci int flags = 0, autocalibration; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); 3798c2ecf20Sopenharmony_ci if (flags < 0) 3808c2ecf20Sopenharmony_ci return flags; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci if (flags & ABX8XX_OSC_ACAL_512) 3838c2ecf20Sopenharmony_ci autocalibration = 512; 3848c2ecf20Sopenharmony_ci else if (flags & ABX8XX_OSC_ACAL_1024) 3858c2ecf20Sopenharmony_ci autocalibration = 1024; 3868c2ecf20Sopenharmony_ci else 3878c2ecf20Sopenharmony_ci autocalibration = 0; 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci return autocalibration; 3908c2ecf20Sopenharmony_ci} 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_cistatic ssize_t autocalibration_store(struct device *dev, 3938c2ecf20Sopenharmony_ci struct device_attribute *attr, 3948c2ecf20Sopenharmony_ci const char *buf, size_t count) 3958c2ecf20Sopenharmony_ci{ 3968c2ecf20Sopenharmony_ci int retval; 3978c2ecf20Sopenharmony_ci unsigned long autocalibration = 0; 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci retval = kstrtoul(buf, 10, &autocalibration); 4008c2ecf20Sopenharmony_ci if (retval < 0) { 4018c2ecf20Sopenharmony_ci dev_err(dev, "Failed to store RTC autocalibration attribute\n"); 4028c2ecf20Sopenharmony_ci return -EINVAL; 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci retval = abx80x_rtc_set_autocalibration(dev->parent, autocalibration); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci return retval ? retval : count; 4088c2ecf20Sopenharmony_ci} 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic ssize_t autocalibration_show(struct device *dev, 4118c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 4128c2ecf20Sopenharmony_ci{ 4138c2ecf20Sopenharmony_ci int autocalibration = 0; 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci autocalibration = abx80x_rtc_get_autocalibration(dev->parent); 4168c2ecf20Sopenharmony_ci if (autocalibration < 0) { 4178c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read RTC autocalibration\n"); 4188c2ecf20Sopenharmony_ci sprintf(buf, "0\n"); 4198c2ecf20Sopenharmony_ci return autocalibration; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", autocalibration); 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(autocalibration); 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_cistatic ssize_t oscillator_store(struct device *dev, 4288c2ecf20Sopenharmony_ci struct device_attribute *attr, 4298c2ecf20Sopenharmony_ci const char *buf, size_t count) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev->parent); 4328c2ecf20Sopenharmony_ci int retval, flags, rc_mode = 0; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci if (strncmp(buf, "rc", 2) == 0) { 4358c2ecf20Sopenharmony_ci rc_mode = 1; 4368c2ecf20Sopenharmony_ci } else if (strncmp(buf, "xtal", 4) == 0) { 4378c2ecf20Sopenharmony_ci rc_mode = 0; 4388c2ecf20Sopenharmony_ci } else { 4398c2ecf20Sopenharmony_ci dev_err(dev, "Oscillator selection value outside permitted ones\n"); 4408c2ecf20Sopenharmony_ci return -EINVAL; 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); 4448c2ecf20Sopenharmony_ci if (flags < 0) 4458c2ecf20Sopenharmony_ci return flags; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci if (rc_mode == 0) 4488c2ecf20Sopenharmony_ci flags &= ~(ABX8XX_OSC_OSEL); 4498c2ecf20Sopenharmony_ci else 4508c2ecf20Sopenharmony_ci flags |= (ABX8XX_OSC_OSEL); 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci /* Unlock write access on Oscillator Control register */ 4538c2ecf20Sopenharmony_ci retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 4548c2ecf20Sopenharmony_ci ABX8XX_CFG_KEY_OSC); 4558c2ecf20Sopenharmony_ci if (retval < 0) { 4568c2ecf20Sopenharmony_ci dev_err(dev, "Failed to write CONFIG_KEY register\n"); 4578c2ecf20Sopenharmony_ci return retval; 4588c2ecf20Sopenharmony_ci } 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); 4618c2ecf20Sopenharmony_ci if (retval < 0) { 4628c2ecf20Sopenharmony_ci dev_err(dev, "Failed to write Oscillator Control register\n"); 4638c2ecf20Sopenharmony_ci return retval; 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci return retval ? retval : count; 4678c2ecf20Sopenharmony_ci} 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_cistatic ssize_t oscillator_show(struct device *dev, 4708c2ecf20Sopenharmony_ci struct device_attribute *attr, char *buf) 4718c2ecf20Sopenharmony_ci{ 4728c2ecf20Sopenharmony_ci int rc_mode = 0; 4738c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev->parent); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci rc_mode = abx80x_is_rc_mode(client); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci if (rc_mode < 0) { 4788c2ecf20Sopenharmony_ci dev_err(dev, "Failed to read RTC oscillator selection\n"); 4798c2ecf20Sopenharmony_ci sprintf(buf, "\n"); 4808c2ecf20Sopenharmony_ci return rc_mode; 4818c2ecf20Sopenharmony_ci } 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci if (rc_mode) 4848c2ecf20Sopenharmony_ci return sprintf(buf, "rc\n"); 4858c2ecf20Sopenharmony_ci else 4868c2ecf20Sopenharmony_ci return sprintf(buf, "xtal\n"); 4878c2ecf20Sopenharmony_ci} 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(oscillator); 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_cistatic struct attribute *rtc_calib_attrs[] = { 4928c2ecf20Sopenharmony_ci &dev_attr_autocalibration.attr, 4938c2ecf20Sopenharmony_ci &dev_attr_oscillator.attr, 4948c2ecf20Sopenharmony_ci NULL, 4958c2ecf20Sopenharmony_ci}; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic const struct attribute_group rtc_calib_attr_group = { 4988c2ecf20Sopenharmony_ci .attrs = rtc_calib_attrs, 4998c2ecf20Sopenharmony_ci}; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_cistatic int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 5048c2ecf20Sopenharmony_ci int err; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci if (enabled) 5078c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, 5088c2ecf20Sopenharmony_ci (ABX8XX_IRQ_IM_1_4 | 5098c2ecf20Sopenharmony_ci ABX8XX_IRQ_AIE)); 5108c2ecf20Sopenharmony_ci else 5118c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, 5128c2ecf20Sopenharmony_ci ABX8XX_IRQ_IM_1_4); 5138c2ecf20Sopenharmony_ci return err; 5148c2ecf20Sopenharmony_ci} 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_cistatic int abx80x_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 5178c2ecf20Sopenharmony_ci{ 5188c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 5198c2ecf20Sopenharmony_ci int status, tmp; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci switch (cmd) { 5228c2ecf20Sopenharmony_ci case RTC_VL_READ: 5238c2ecf20Sopenharmony_ci status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); 5248c2ecf20Sopenharmony_ci if (status < 0) 5258c2ecf20Sopenharmony_ci return status; 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci tmp = status & ABX8XX_STATUS_BLF ? RTC_VL_BACKUP_LOW : 0; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci return put_user(tmp, (unsigned int __user *)arg); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci case RTC_VL_CLR: 5328c2ecf20Sopenharmony_ci status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); 5338c2ecf20Sopenharmony_ci if (status < 0) 5348c2ecf20Sopenharmony_ci return status; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci status &= ~ABX8XX_STATUS_BLF; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci tmp = i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); 5398c2ecf20Sopenharmony_ci if (tmp < 0) 5408c2ecf20Sopenharmony_ci return tmp; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci return 0; 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci default: 5458c2ecf20Sopenharmony_ci return -ENOIOCTLCMD; 5468c2ecf20Sopenharmony_ci } 5478c2ecf20Sopenharmony_ci} 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_cistatic const struct rtc_class_ops abx80x_rtc_ops = { 5508c2ecf20Sopenharmony_ci .read_time = abx80x_rtc_read_time, 5518c2ecf20Sopenharmony_ci .set_time = abx80x_rtc_set_time, 5528c2ecf20Sopenharmony_ci .read_alarm = abx80x_read_alarm, 5538c2ecf20Sopenharmony_ci .set_alarm = abx80x_set_alarm, 5548c2ecf20Sopenharmony_ci .alarm_irq_enable = abx80x_alarm_irq_enable, 5558c2ecf20Sopenharmony_ci .ioctl = abx80x_ioctl, 5568c2ecf20Sopenharmony_ci}; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_cistatic int abx80x_dt_trickle_cfg(struct i2c_client *client) 5598c2ecf20Sopenharmony_ci{ 5608c2ecf20Sopenharmony_ci struct device_node *np = client->dev.of_node; 5618c2ecf20Sopenharmony_ci const char *diode; 5628c2ecf20Sopenharmony_ci int trickle_cfg = 0; 5638c2ecf20Sopenharmony_ci int i, ret; 5648c2ecf20Sopenharmony_ci u32 tmp; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci ret = of_property_read_string(np, "abracon,tc-diode", &diode); 5678c2ecf20Sopenharmony_ci if (ret) 5688c2ecf20Sopenharmony_ci return ret; 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_ci if (!strcmp(diode, "standard")) { 5718c2ecf20Sopenharmony_ci trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE; 5728c2ecf20Sopenharmony_ci } else if (!strcmp(diode, "schottky")) { 5738c2ecf20Sopenharmony_ci trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE; 5748c2ecf20Sopenharmony_ci } else { 5758c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Invalid tc-diode value: %s\n", diode); 5768c2ecf20Sopenharmony_ci return -EINVAL; 5778c2ecf20Sopenharmony_ci } 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp); 5808c2ecf20Sopenharmony_ci if (ret) 5818c2ecf20Sopenharmony_ci return ret; 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci for (i = 0; i < sizeof(trickle_resistors); i++) 5848c2ecf20Sopenharmony_ci if (trickle_resistors[i] == tmp) 5858c2ecf20Sopenharmony_ci break; 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci if (i == sizeof(trickle_resistors)) { 5888c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Invalid tc-resistor value: %u\n", tmp); 5898c2ecf20Sopenharmony_ci return -EINVAL; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci return (trickle_cfg | i); 5938c2ecf20Sopenharmony_ci} 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci#ifdef CONFIG_WATCHDOG 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_cistatic inline u8 timeout_bits(unsigned int timeout) 5988c2ecf20Sopenharmony_ci{ 5998c2ecf20Sopenharmony_ci return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) | 6008c2ecf20Sopenharmony_ci ABX8XX_WDT_WRB_1HZ; 6018c2ecf20Sopenharmony_ci} 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_cistatic int __abx80x_wdog_set_timeout(struct watchdog_device *wdog, 6048c2ecf20Sopenharmony_ci unsigned int timeout) 6058c2ecf20Sopenharmony_ci{ 6068c2ecf20Sopenharmony_ci struct abx80x_priv *priv = watchdog_get_drvdata(wdog); 6078c2ecf20Sopenharmony_ci u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout); 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* 6108c2ecf20Sopenharmony_ci * Writing any timeout to the WDT register resets the watchdog timer. 6118c2ecf20Sopenharmony_ci * Writing 0 disables it. 6128c2ecf20Sopenharmony_ci */ 6138c2ecf20Sopenharmony_ci return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val); 6148c2ecf20Sopenharmony_ci} 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_cistatic int abx80x_wdog_set_timeout(struct watchdog_device *wdog, 6178c2ecf20Sopenharmony_ci unsigned int new_timeout) 6188c2ecf20Sopenharmony_ci{ 6198c2ecf20Sopenharmony_ci int err = 0; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci if (watchdog_hw_running(wdog)) 6228c2ecf20Sopenharmony_ci err = __abx80x_wdog_set_timeout(wdog, new_timeout); 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_ci if (err == 0) 6258c2ecf20Sopenharmony_ci wdog->timeout = new_timeout; 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_ci return err; 6288c2ecf20Sopenharmony_ci} 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_cistatic int abx80x_wdog_ping(struct watchdog_device *wdog) 6318c2ecf20Sopenharmony_ci{ 6328c2ecf20Sopenharmony_ci return __abx80x_wdog_set_timeout(wdog, wdog->timeout); 6338c2ecf20Sopenharmony_ci} 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_cistatic int abx80x_wdog_start(struct watchdog_device *wdog) 6368c2ecf20Sopenharmony_ci{ 6378c2ecf20Sopenharmony_ci return __abx80x_wdog_set_timeout(wdog, wdog->timeout); 6388c2ecf20Sopenharmony_ci} 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_cistatic int abx80x_wdog_stop(struct watchdog_device *wdog) 6418c2ecf20Sopenharmony_ci{ 6428c2ecf20Sopenharmony_ci return __abx80x_wdog_set_timeout(wdog, 0); 6438c2ecf20Sopenharmony_ci} 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_cistatic const struct watchdog_info abx80x_wdog_info = { 6468c2ecf20Sopenharmony_ci .identity = "abx80x watchdog", 6478c2ecf20Sopenharmony_ci .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, 6488c2ecf20Sopenharmony_ci}; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_cistatic const struct watchdog_ops abx80x_wdog_ops = { 6518c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 6528c2ecf20Sopenharmony_ci .start = abx80x_wdog_start, 6538c2ecf20Sopenharmony_ci .stop = abx80x_wdog_stop, 6548c2ecf20Sopenharmony_ci .ping = abx80x_wdog_ping, 6558c2ecf20Sopenharmony_ci .set_timeout = abx80x_wdog_set_timeout, 6568c2ecf20Sopenharmony_ci}; 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_cistatic int abx80x_setup_watchdog(struct abx80x_priv *priv) 6598c2ecf20Sopenharmony_ci{ 6608c2ecf20Sopenharmony_ci priv->wdog.parent = &priv->client->dev; 6618c2ecf20Sopenharmony_ci priv->wdog.ops = &abx80x_wdog_ops; 6628c2ecf20Sopenharmony_ci priv->wdog.info = &abx80x_wdog_info; 6638c2ecf20Sopenharmony_ci priv->wdog.min_timeout = 1; 6648c2ecf20Sopenharmony_ci priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME; 6658c2ecf20Sopenharmony_ci priv->wdog.timeout = ABX8XX_WDT_MAX_TIME; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci watchdog_set_drvdata(&priv->wdog, priv); 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci return devm_watchdog_register_device(&priv->client->dev, &priv->wdog); 6708c2ecf20Sopenharmony_ci} 6718c2ecf20Sopenharmony_ci#else 6728c2ecf20Sopenharmony_cistatic int abx80x_setup_watchdog(struct abx80x_priv *priv) 6738c2ecf20Sopenharmony_ci{ 6748c2ecf20Sopenharmony_ci return 0; 6758c2ecf20Sopenharmony_ci} 6768c2ecf20Sopenharmony_ci#endif 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_cistatic int abx80x_probe(struct i2c_client *client, 6798c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 6808c2ecf20Sopenharmony_ci{ 6818c2ecf20Sopenharmony_ci struct device_node *np = client->dev.of_node; 6828c2ecf20Sopenharmony_ci struct abx80x_priv *priv; 6838c2ecf20Sopenharmony_ci int i, data, err, trickle_cfg = -EINVAL; 6848c2ecf20Sopenharmony_ci char buf[7]; 6858c2ecf20Sopenharmony_ci unsigned int part = id->driver_data; 6868c2ecf20Sopenharmony_ci unsigned int partnumber; 6878c2ecf20Sopenharmony_ci unsigned int majrev, minrev; 6888c2ecf20Sopenharmony_ci unsigned int lot; 6898c2ecf20Sopenharmony_ci unsigned int wafer; 6908c2ecf20Sopenharmony_ci unsigned int uid; 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 6938c2ecf20Sopenharmony_ci return -ENODEV; 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0, 6968c2ecf20Sopenharmony_ci sizeof(buf), buf); 6978c2ecf20Sopenharmony_ci if (err < 0) { 6988c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to read partnumber\n"); 6998c2ecf20Sopenharmony_ci return -EIO; 7008c2ecf20Sopenharmony_ci } 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci partnumber = (buf[0] << 8) | buf[1]; 7038c2ecf20Sopenharmony_ci majrev = buf[2] >> 3; 7048c2ecf20Sopenharmony_ci minrev = buf[2] & 0x7; 7058c2ecf20Sopenharmony_ci lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3]; 7068c2ecf20Sopenharmony_ci uid = ((buf[4] & 0x7f) << 8) | buf[5]; 7078c2ecf20Sopenharmony_ci wafer = (buf[6] & 0x7c) >> 2; 7088c2ecf20Sopenharmony_ci dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n", 7098c2ecf20Sopenharmony_ci partnumber, majrev, minrev, lot, wafer, uid); 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1); 7128c2ecf20Sopenharmony_ci if (data < 0) { 7138c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to read control register\n"); 7148c2ecf20Sopenharmony_ci return -EIO; 7158c2ecf20Sopenharmony_ci } 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1, 7188c2ecf20Sopenharmony_ci ((data & ~(ABX8XX_CTRL_12_24 | 7198c2ecf20Sopenharmony_ci ABX8XX_CTRL_ARST)) | 7208c2ecf20Sopenharmony_ci ABX8XX_CTRL_WRITE)); 7218c2ecf20Sopenharmony_ci if (err < 0) { 7228c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to write control register\n"); 7238c2ecf20Sopenharmony_ci return -EIO; 7248c2ecf20Sopenharmony_ci } 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci /* Configure RV1805 specifics */ 7278c2ecf20Sopenharmony_ci if (part == RV1805) { 7288c2ecf20Sopenharmony_ci /* 7298c2ecf20Sopenharmony_ci * Avoid accidentally entering test mode. This can happen 7308c2ecf20Sopenharmony_ci * on the RV1805 in case the reserved bit 5 in control2 7318c2ecf20Sopenharmony_ci * register is set. RV-1805-C3 datasheet indicates that 7328c2ecf20Sopenharmony_ci * the bit should be cleared in section 11h - Control2. 7338c2ecf20Sopenharmony_ci */ 7348c2ecf20Sopenharmony_ci data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL2); 7358c2ecf20Sopenharmony_ci if (data < 0) { 7368c2ecf20Sopenharmony_ci dev_err(&client->dev, 7378c2ecf20Sopenharmony_ci "Unable to read control2 register\n"); 7388c2ecf20Sopenharmony_ci return -EIO; 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL2, 7428c2ecf20Sopenharmony_ci data & ~ABX8XX_CTRL2_RSVD); 7438c2ecf20Sopenharmony_ci if (err < 0) { 7448c2ecf20Sopenharmony_ci dev_err(&client->dev, 7458c2ecf20Sopenharmony_ci "Unable to write control2 register\n"); 7468c2ecf20Sopenharmony_ci return -EIO; 7478c2ecf20Sopenharmony_ci } 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci /* 7508c2ecf20Sopenharmony_ci * Avoid extra power leakage. The RV1805 uses smaller 7518c2ecf20Sopenharmony_ci * 10pin package and the EXTI input is not present. 7528c2ecf20Sopenharmony_ci * Disable it to avoid leakage. 7538c2ecf20Sopenharmony_ci */ 7548c2ecf20Sopenharmony_ci data = i2c_smbus_read_byte_data(client, ABX8XX_REG_OUT_CTRL); 7558c2ecf20Sopenharmony_ci if (data < 0) { 7568c2ecf20Sopenharmony_ci dev_err(&client->dev, 7578c2ecf20Sopenharmony_ci "Unable to read output control register\n"); 7588c2ecf20Sopenharmony_ci return -EIO; 7598c2ecf20Sopenharmony_ci } 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ci /* 7628c2ecf20Sopenharmony_ci * Write the configuration key register to enable access to 7638c2ecf20Sopenharmony_ci * the config2 register 7648c2ecf20Sopenharmony_ci */ 7658c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, 7668c2ecf20Sopenharmony_ci ABX8XX_CFG_KEY_MISC); 7678c2ecf20Sopenharmony_ci if (err < 0) { 7688c2ecf20Sopenharmony_ci dev_err(&client->dev, 7698c2ecf20Sopenharmony_ci "Unable to write configuration key\n"); 7708c2ecf20Sopenharmony_ci return -EIO; 7718c2ecf20Sopenharmony_ci } 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OUT_CTRL, 7748c2ecf20Sopenharmony_ci data | ABX8XX_OUT_CTRL_EXDS); 7758c2ecf20Sopenharmony_ci if (err < 0) { 7768c2ecf20Sopenharmony_ci dev_err(&client->dev, 7778c2ecf20Sopenharmony_ci "Unable to write output control register\n"); 7788c2ecf20Sopenharmony_ci return -EIO; 7798c2ecf20Sopenharmony_ci } 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci /* part autodetection */ 7838c2ecf20Sopenharmony_ci if (part == ABX80X) { 7848c2ecf20Sopenharmony_ci for (i = 0; abx80x_caps[i].pn; i++) 7858c2ecf20Sopenharmony_ci if (partnumber == abx80x_caps[i].pn) 7868c2ecf20Sopenharmony_ci break; 7878c2ecf20Sopenharmony_ci if (abx80x_caps[i].pn == 0) { 7888c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unknown part: %04x\n", 7898c2ecf20Sopenharmony_ci partnumber); 7908c2ecf20Sopenharmony_ci return -EINVAL; 7918c2ecf20Sopenharmony_ci } 7928c2ecf20Sopenharmony_ci part = i; 7938c2ecf20Sopenharmony_ci } 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci if (partnumber != abx80x_caps[part].pn) { 7968c2ecf20Sopenharmony_ci dev_err(&client->dev, "partnumber mismatch %04x != %04x\n", 7978c2ecf20Sopenharmony_ci partnumber, abx80x_caps[part].pn); 7988c2ecf20Sopenharmony_ci return -EINVAL; 7998c2ecf20Sopenharmony_ci } 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci if (np && abx80x_caps[part].has_tc) 8028c2ecf20Sopenharmony_ci trickle_cfg = abx80x_dt_trickle_cfg(client); 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci if (trickle_cfg > 0) { 8058c2ecf20Sopenharmony_ci dev_info(&client->dev, "Enabling trickle charger: %02x\n", 8068c2ecf20Sopenharmony_ci trickle_cfg); 8078c2ecf20Sopenharmony_ci abx80x_enable_trickle_charger(client, trickle_cfg); 8088c2ecf20Sopenharmony_ci } 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL, 8118c2ecf20Sopenharmony_ci BIT(2)); 8128c2ecf20Sopenharmony_ci if (err) 8138c2ecf20Sopenharmony_ci return err; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); 8168c2ecf20Sopenharmony_ci if (priv == NULL) 8178c2ecf20Sopenharmony_ci return -ENOMEM; 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci priv->rtc = devm_rtc_allocate_device(&client->dev); 8208c2ecf20Sopenharmony_ci if (IS_ERR(priv->rtc)) 8218c2ecf20Sopenharmony_ci return PTR_ERR(priv->rtc); 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci priv->rtc->ops = &abx80x_rtc_ops; 8248c2ecf20Sopenharmony_ci priv->client = client; 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_ci i2c_set_clientdata(client, priv); 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci if (abx80x_caps[part].has_wdog) { 8298c2ecf20Sopenharmony_ci err = abx80x_setup_watchdog(priv); 8308c2ecf20Sopenharmony_ci if (err) 8318c2ecf20Sopenharmony_ci return err; 8328c2ecf20Sopenharmony_ci } 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci if (client->irq > 0) { 8358c2ecf20Sopenharmony_ci dev_info(&client->dev, "IRQ %d supplied\n", client->irq); 8368c2ecf20Sopenharmony_ci err = devm_request_threaded_irq(&client->dev, client->irq, NULL, 8378c2ecf20Sopenharmony_ci abx80x_handle_irq, 8388c2ecf20Sopenharmony_ci IRQF_SHARED | IRQF_ONESHOT, 8398c2ecf20Sopenharmony_ci "abx8xx", 8408c2ecf20Sopenharmony_ci client); 8418c2ecf20Sopenharmony_ci if (err) { 8428c2ecf20Sopenharmony_ci dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); 8438c2ecf20Sopenharmony_ci client->irq = 0; 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci } 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci err = rtc_add_group(priv->rtc, &rtc_calib_attr_group); 8488c2ecf20Sopenharmony_ci if (err) { 8498c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to create sysfs group: %d\n", 8508c2ecf20Sopenharmony_ci err); 8518c2ecf20Sopenharmony_ci return err; 8528c2ecf20Sopenharmony_ci } 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci return rtc_register_device(priv->rtc); 8558c2ecf20Sopenharmony_ci} 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_cistatic const struct i2c_device_id abx80x_id[] = { 8588c2ecf20Sopenharmony_ci { "abx80x", ABX80X }, 8598c2ecf20Sopenharmony_ci { "ab0801", AB0801 }, 8608c2ecf20Sopenharmony_ci { "ab0803", AB0803 }, 8618c2ecf20Sopenharmony_ci { "ab0804", AB0804 }, 8628c2ecf20Sopenharmony_ci { "ab0805", AB0805 }, 8638c2ecf20Sopenharmony_ci { "ab1801", AB1801 }, 8648c2ecf20Sopenharmony_ci { "ab1803", AB1803 }, 8658c2ecf20Sopenharmony_ci { "ab1804", AB1804 }, 8668c2ecf20Sopenharmony_ci { "ab1805", AB1805 }, 8678c2ecf20Sopenharmony_ci { "rv1805", RV1805 }, 8688c2ecf20Sopenharmony_ci { } 8698c2ecf20Sopenharmony_ci}; 8708c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, abx80x_id); 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 8738c2ecf20Sopenharmony_cistatic const struct of_device_id abx80x_of_match[] = { 8748c2ecf20Sopenharmony_ci { 8758c2ecf20Sopenharmony_ci .compatible = "abracon,abx80x", 8768c2ecf20Sopenharmony_ci .data = (void *)ABX80X 8778c2ecf20Sopenharmony_ci }, 8788c2ecf20Sopenharmony_ci { 8798c2ecf20Sopenharmony_ci .compatible = "abracon,ab0801", 8808c2ecf20Sopenharmony_ci .data = (void *)AB0801 8818c2ecf20Sopenharmony_ci }, 8828c2ecf20Sopenharmony_ci { 8838c2ecf20Sopenharmony_ci .compatible = "abracon,ab0803", 8848c2ecf20Sopenharmony_ci .data = (void *)AB0803 8858c2ecf20Sopenharmony_ci }, 8868c2ecf20Sopenharmony_ci { 8878c2ecf20Sopenharmony_ci .compatible = "abracon,ab0804", 8888c2ecf20Sopenharmony_ci .data = (void *)AB0804 8898c2ecf20Sopenharmony_ci }, 8908c2ecf20Sopenharmony_ci { 8918c2ecf20Sopenharmony_ci .compatible = "abracon,ab0805", 8928c2ecf20Sopenharmony_ci .data = (void *)AB0805 8938c2ecf20Sopenharmony_ci }, 8948c2ecf20Sopenharmony_ci { 8958c2ecf20Sopenharmony_ci .compatible = "abracon,ab1801", 8968c2ecf20Sopenharmony_ci .data = (void *)AB1801 8978c2ecf20Sopenharmony_ci }, 8988c2ecf20Sopenharmony_ci { 8998c2ecf20Sopenharmony_ci .compatible = "abracon,ab1803", 9008c2ecf20Sopenharmony_ci .data = (void *)AB1803 9018c2ecf20Sopenharmony_ci }, 9028c2ecf20Sopenharmony_ci { 9038c2ecf20Sopenharmony_ci .compatible = "abracon,ab1804", 9048c2ecf20Sopenharmony_ci .data = (void *)AB1804 9058c2ecf20Sopenharmony_ci }, 9068c2ecf20Sopenharmony_ci { 9078c2ecf20Sopenharmony_ci .compatible = "abracon,ab1805", 9088c2ecf20Sopenharmony_ci .data = (void *)AB1805 9098c2ecf20Sopenharmony_ci }, 9108c2ecf20Sopenharmony_ci { 9118c2ecf20Sopenharmony_ci .compatible = "microcrystal,rv1805", 9128c2ecf20Sopenharmony_ci .data = (void *)RV1805 9138c2ecf20Sopenharmony_ci }, 9148c2ecf20Sopenharmony_ci { } 9158c2ecf20Sopenharmony_ci}; 9168c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, abx80x_of_match); 9178c2ecf20Sopenharmony_ci#endif 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_cistatic struct i2c_driver abx80x_driver = { 9208c2ecf20Sopenharmony_ci .driver = { 9218c2ecf20Sopenharmony_ci .name = "rtc-abx80x", 9228c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(abx80x_of_match), 9238c2ecf20Sopenharmony_ci }, 9248c2ecf20Sopenharmony_ci .probe = abx80x_probe, 9258c2ecf20Sopenharmony_ci .id_table = abx80x_id, 9268c2ecf20Sopenharmony_ci}; 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_cimodule_i2c_driver(abx80x_driver); 9298c2ecf20Sopenharmony_ci 9308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>"); 9318c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 9328c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Abracon ABX80X RTC driver"); 9338c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 934