1*182ae2bbSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0 21e3929efSAlexandre Belloni /* 31e3929efSAlexandre Belloni * RTC driver for the Micro Crystal RV8803 41e3929efSAlexandre Belloni * 51e3929efSAlexandre Belloni * Copyright (C) 2015 Micro Crystal SA 61e3929efSAlexandre Belloni * Alexandre Belloni <alexandre.belloni@free-electrons.com> 71e3929efSAlexandre Belloni * 81e3929efSAlexandre Belloni */ 91e3929efSAlexandre Belloni 101e3929efSAlexandre Belloni #include <linux/bcd.h> 111e3929efSAlexandre Belloni #include <linux/bitops.h> 12a1e98e09SBenoît Thébaudeau #include <linux/log2.h> 131e3929efSAlexandre Belloni #include <linux/i2c.h> 141e3929efSAlexandre Belloni #include <linux/interrupt.h> 151e3929efSAlexandre Belloni #include <linux/kernel.h> 161e3929efSAlexandre Belloni #include <linux/module.h> 17740ad8f4SJavier Martinez Canillas #include <linux/of_device.h> 181e3929efSAlexandre Belloni #include <linux/rtc.h> 191e3929efSAlexandre Belloni 20d522649eSBenoît Thébaudeau #define RV8803_I2C_TRY_COUNT 4 21d522649eSBenoît Thébaudeau 221e3929efSAlexandre Belloni #define RV8803_SEC 0x00 231e3929efSAlexandre Belloni #define RV8803_MIN 0x01 241e3929efSAlexandre Belloni #define RV8803_HOUR 0x02 251e3929efSAlexandre Belloni #define RV8803_WEEK 0x03 261e3929efSAlexandre Belloni #define RV8803_DAY 0x04 271e3929efSAlexandre Belloni #define RV8803_MONTH 0x05 281e3929efSAlexandre Belloni #define RV8803_YEAR 0x06 291e3929efSAlexandre Belloni #define RV8803_RAM 0x07 301e3929efSAlexandre Belloni #define RV8803_ALARM_MIN 0x08 311e3929efSAlexandre Belloni #define RV8803_ALARM_HOUR 0x09 321e3929efSAlexandre Belloni #define RV8803_ALARM_WEEK_OR_DAY 0x0A 331e3929efSAlexandre Belloni #define RV8803_EXT 0x0D 341e3929efSAlexandre Belloni #define RV8803_FLAG 0x0E 351e3929efSAlexandre Belloni #define RV8803_CTRL 0x0F 361e3929efSAlexandre Belloni 371e3929efSAlexandre Belloni #define RV8803_EXT_WADA BIT(6) 381e3929efSAlexandre Belloni 391e3929efSAlexandre Belloni #define RV8803_FLAG_V1F BIT(0) 401e3929efSAlexandre Belloni #define RV8803_FLAG_V2F BIT(1) 411e3929efSAlexandre Belloni #define RV8803_FLAG_AF BIT(3) 421e3929efSAlexandre Belloni #define RV8803_FLAG_TF BIT(4) 431e3929efSAlexandre Belloni #define RV8803_FLAG_UF BIT(5) 441e3929efSAlexandre Belloni 451e3929efSAlexandre Belloni #define RV8803_CTRL_RESET BIT(0) 461e3929efSAlexandre Belloni 471e3929efSAlexandre Belloni #define RV8803_CTRL_EIE BIT(2) 481e3929efSAlexandre Belloni #define RV8803_CTRL_AIE BIT(3) 491e3929efSAlexandre Belloni #define RV8803_CTRL_TIE BIT(4) 501e3929efSAlexandre Belloni #define RV8803_CTRL_UIE BIT(5) 511e3929efSAlexandre Belloni 521cd71376SOleksij Rempel #define RX8900_BACKUP_CTRL 0x18 531cd71376SOleksij Rempel #define RX8900_FLAG_SWOFF BIT(2) 541cd71376SOleksij Rempel #define RX8900_FLAG_VDETOFF BIT(3) 551cd71376SOleksij Rempel 561cd71376SOleksij Rempel enum rv8803_type { 571cd71376SOleksij Rempel rv_8803, 581cd71376SOleksij Rempel rx_8900 591cd71376SOleksij Rempel }; 601cd71376SOleksij Rempel 611e3929efSAlexandre Belloni struct rv8803_data { 621e3929efSAlexandre Belloni struct i2c_client *client; 631e3929efSAlexandre Belloni struct rtc_device *rtc; 649d1fa4c3SOleksij Rempel struct mutex flags_lock; 651e3929efSAlexandre Belloni u8 ctrl; 661cd71376SOleksij Rempel enum rv8803_type type; 671e3929efSAlexandre Belloni }; 681e3929efSAlexandre Belloni 69d522649eSBenoît Thébaudeau static int rv8803_read_reg(const struct i2c_client *client, u8 reg) 70d522649eSBenoît Thébaudeau { 71d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 72d522649eSBenoît Thébaudeau s32 ret; 73d522649eSBenoît Thébaudeau 74d522649eSBenoît Thébaudeau /* 75d522649eSBenoît Thébaudeau * There is a 61µs window during which the RTC does not acknowledge I2C 76d522649eSBenoît Thébaudeau * transfers. In that case, ensure that there are multiple attempts. 77d522649eSBenoît Thébaudeau */ 78d522649eSBenoît Thébaudeau do 79d522649eSBenoît Thébaudeau ret = i2c_smbus_read_byte_data(client, reg); 80d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 81d522649eSBenoît Thébaudeau if (ret < 0) 82d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to read register 0x%02x\n", reg); 83d522649eSBenoît Thébaudeau 84d522649eSBenoît Thébaudeau return ret; 85d522649eSBenoît Thébaudeau } 86d522649eSBenoît Thébaudeau 87d522649eSBenoît Thébaudeau static int rv8803_read_regs(const struct i2c_client *client, 88d522649eSBenoît Thébaudeau u8 reg, u8 count, u8 *values) 89d522649eSBenoît Thébaudeau { 90d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 91d522649eSBenoît Thébaudeau s32 ret; 92d522649eSBenoît Thébaudeau 93d522649eSBenoît Thébaudeau do 94d522649eSBenoît Thébaudeau ret = i2c_smbus_read_i2c_block_data(client, reg, count, values); 95d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 96d522649eSBenoît Thébaudeau if (ret != count) { 97d522649eSBenoît Thébaudeau dev_err(&client->dev, 98d522649eSBenoît Thébaudeau "Unable to read registers 0x%02x..0x%02x\n", 99d522649eSBenoît Thébaudeau reg, reg + count - 1); 100d522649eSBenoît Thébaudeau return ret < 0 ? ret : -EIO; 101d522649eSBenoît Thébaudeau } 102d522649eSBenoît Thébaudeau 103d522649eSBenoît Thébaudeau return 0; 104d522649eSBenoît Thébaudeau } 105d522649eSBenoît Thébaudeau 106d522649eSBenoît Thébaudeau static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) 107d522649eSBenoît Thébaudeau { 108d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 109d522649eSBenoît Thébaudeau s32 ret; 110d522649eSBenoît Thébaudeau 111d522649eSBenoît Thébaudeau do 112d522649eSBenoît Thébaudeau ret = i2c_smbus_write_byte_data(client, reg, value); 113d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 114d522649eSBenoît Thébaudeau if (ret) 115d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to write register 0x%02x\n", reg); 116d522649eSBenoît Thébaudeau 117d522649eSBenoît Thébaudeau return ret; 118d522649eSBenoît Thébaudeau } 119d522649eSBenoît Thébaudeau 120d522649eSBenoît Thébaudeau static int rv8803_write_regs(const struct i2c_client *client, 121d522649eSBenoît Thébaudeau u8 reg, u8 count, const u8 *values) 122d522649eSBenoît Thébaudeau { 123d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 124d522649eSBenoît Thébaudeau s32 ret; 125d522649eSBenoît Thébaudeau 126d522649eSBenoît Thébaudeau do 127d522649eSBenoît Thébaudeau ret = i2c_smbus_write_i2c_block_data(client, reg, count, 128d522649eSBenoît Thébaudeau values); 129d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 130d522649eSBenoît Thébaudeau if (ret) 131d522649eSBenoît Thébaudeau dev_err(&client->dev, 132d522649eSBenoît Thébaudeau "Unable to write registers 0x%02x..0x%02x\n", 133d522649eSBenoît Thébaudeau reg, reg + count - 1); 134d522649eSBenoît Thébaudeau 135d522649eSBenoît Thébaudeau return ret; 136d522649eSBenoît Thébaudeau } 137d522649eSBenoît Thébaudeau 1381e3929efSAlexandre Belloni static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) 1391e3929efSAlexandre Belloni { 1401e3929efSAlexandre Belloni struct i2c_client *client = dev_id; 1411e3929efSAlexandre Belloni struct rv8803_data *rv8803 = i2c_get_clientdata(client); 1421e3929efSAlexandre Belloni unsigned long events = 0; 143d522649eSBenoît Thébaudeau int flags; 1441e3929efSAlexandre Belloni 1459d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 1461e3929efSAlexandre Belloni 147d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 1481e3929efSAlexandre Belloni if (flags <= 0) { 1499d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 1501e3929efSAlexandre Belloni return IRQ_NONE; 1511e3929efSAlexandre Belloni } 1521e3929efSAlexandre Belloni 1531e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 1541e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 1551e3929efSAlexandre Belloni 1561e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 1571e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 1581e3929efSAlexandre Belloni 1591e3929efSAlexandre Belloni if (flags & RV8803_FLAG_TF) { 1601e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_TF; 1611e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_TIE; 1621e3929efSAlexandre Belloni events |= RTC_PF; 1631e3929efSAlexandre Belloni } 1641e3929efSAlexandre Belloni 1651e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) { 1661e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_AF; 1671e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_AIE; 1681e3929efSAlexandre Belloni events |= RTC_AF; 1691e3929efSAlexandre Belloni } 1701e3929efSAlexandre Belloni 1711e3929efSAlexandre Belloni if (flags & RV8803_FLAG_UF) { 1721e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_UF; 1731e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_UIE; 1741e3929efSAlexandre Belloni events |= RTC_UF; 1751e3929efSAlexandre Belloni } 1761e3929efSAlexandre Belloni 1771e3929efSAlexandre Belloni if (events) { 1781e3929efSAlexandre Belloni rtc_update_irq(rv8803->rtc, 1, events); 179d522649eSBenoît Thébaudeau rv8803_write_reg(client, RV8803_FLAG, flags); 180d522649eSBenoît Thébaudeau rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl); 1811e3929efSAlexandre Belloni } 1821e3929efSAlexandre Belloni 1839d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 1841e3929efSAlexandre Belloni 1851e3929efSAlexandre Belloni return IRQ_HANDLED; 1861e3929efSAlexandre Belloni } 1871e3929efSAlexandre Belloni 1881e3929efSAlexandre Belloni static int rv8803_get_time(struct device *dev, struct rtc_time *tm) 1891e3929efSAlexandre Belloni { 1901e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 1911e3929efSAlexandre Belloni u8 date1[7]; 1921e3929efSAlexandre Belloni u8 date2[7]; 1931e3929efSAlexandre Belloni u8 *date = date1; 1941e3929efSAlexandre Belloni int ret, flags; 1951e3929efSAlexandre Belloni 196d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 1971e3929efSAlexandre Belloni if (flags < 0) 1981e3929efSAlexandre Belloni return flags; 1991e3929efSAlexandre Belloni 2001e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) { 2011e3929efSAlexandre Belloni dev_warn(dev, "Voltage low, data is invalid.\n"); 2021e3929efSAlexandre Belloni return -EINVAL; 2031e3929efSAlexandre Belloni } 2041e3929efSAlexandre Belloni 205d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date); 206d522649eSBenoît Thébaudeau if (ret) 207d522649eSBenoît Thébaudeau return ret; 2081e3929efSAlexandre Belloni 2091e3929efSAlexandre Belloni if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { 210d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2); 211d522649eSBenoît Thébaudeau if (ret) 212d522649eSBenoît Thébaudeau return ret; 2131e3929efSAlexandre Belloni 2141e3929efSAlexandre Belloni if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) 2151e3929efSAlexandre Belloni date = date2; 2161e3929efSAlexandre Belloni } 2171e3929efSAlexandre Belloni 2181e3929efSAlexandre Belloni tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); 2191e3929efSAlexandre Belloni tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); 2201e3929efSAlexandre Belloni tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); 221a1e98e09SBenoît Thébaudeau tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); 2221e3929efSAlexandre Belloni tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); 2231e3929efSAlexandre Belloni tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; 2241e3929efSAlexandre Belloni tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; 2251e3929efSAlexandre Belloni 22696acb25cSBenoît Thébaudeau return 0; 2271e3929efSAlexandre Belloni } 2281e3929efSAlexandre Belloni 2291e3929efSAlexandre Belloni static int rv8803_set_time(struct device *dev, struct rtc_time *tm) 2301e3929efSAlexandre Belloni { 2311e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2321e3929efSAlexandre Belloni u8 date[7]; 233d3700b6bSBenoît Thébaudeau int ctrl, flags, ret; 2341e3929efSAlexandre Belloni 235d3700b6bSBenoît Thébaudeau ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL); 236d3700b6bSBenoît Thébaudeau if (ctrl < 0) 237d3700b6bSBenoît Thébaudeau return ctrl; 238d3700b6bSBenoît Thébaudeau 239d3700b6bSBenoît Thébaudeau /* Stop the clock */ 240d3700b6bSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 241d3700b6bSBenoît Thébaudeau ctrl | RV8803_CTRL_RESET); 242d3700b6bSBenoît Thébaudeau if (ret) 243d3700b6bSBenoît Thébaudeau return ret; 244d3700b6bSBenoît Thébaudeau 2451e3929efSAlexandre Belloni date[RV8803_SEC] = bin2bcd(tm->tm_sec); 2461e3929efSAlexandre Belloni date[RV8803_MIN] = bin2bcd(tm->tm_min); 2471e3929efSAlexandre Belloni date[RV8803_HOUR] = bin2bcd(tm->tm_hour); 2481e3929efSAlexandre Belloni date[RV8803_WEEK] = 1 << (tm->tm_wday); 2491e3929efSAlexandre Belloni date[RV8803_DAY] = bin2bcd(tm->tm_mday); 2501e3929efSAlexandre Belloni date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); 2511e3929efSAlexandre Belloni date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); 2521e3929efSAlexandre Belloni 253d522649eSBenoît Thébaudeau ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date); 254d522649eSBenoît Thébaudeau if (ret) 2551e3929efSAlexandre Belloni return ret; 2561e3929efSAlexandre Belloni 257d3700b6bSBenoît Thébaudeau /* Restart the clock */ 258d3700b6bSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 259d3700b6bSBenoît Thébaudeau ctrl & ~RV8803_CTRL_RESET); 260d3700b6bSBenoît Thébaudeau if (ret) 261d3700b6bSBenoît Thébaudeau return ret; 262d3700b6bSBenoît Thébaudeau 2639d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 2641e3929efSAlexandre Belloni 265d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 2661e3929efSAlexandre Belloni if (flags < 0) { 2679d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2681e3929efSAlexandre Belloni return flags; 2691e3929efSAlexandre Belloni } 2701e3929efSAlexandre Belloni 271d522649eSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_FLAG, 2726f367788SBenoît Thébaudeau flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F)); 2731e3929efSAlexandre Belloni 2749d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2751e3929efSAlexandre Belloni 2761e3929efSAlexandre Belloni return ret; 2771e3929efSAlexandre Belloni } 2781e3929efSAlexandre Belloni 2791e3929efSAlexandre Belloni static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 2801e3929efSAlexandre Belloni { 2811e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2821e3929efSAlexandre Belloni struct i2c_client *client = rv8803->client; 2831e3929efSAlexandre Belloni u8 alarmvals[3]; 2841e3929efSAlexandre Belloni int flags, ret; 2851e3929efSAlexandre Belloni 286d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals); 287d522649eSBenoît Thébaudeau if (ret) 288d522649eSBenoît Thébaudeau return ret; 2891e3929efSAlexandre Belloni 290d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 2911e3929efSAlexandre Belloni if (flags < 0) 2921e3929efSAlexandre Belloni return flags; 2931e3929efSAlexandre Belloni 2941e3929efSAlexandre Belloni alrm->time.tm_sec = 0; 2951e3929efSAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 2961e3929efSAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 2971e3929efSAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 2981e3929efSAlexandre Belloni 2991e3929efSAlexandre Belloni alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); 3001e3929efSAlexandre Belloni alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; 3011e3929efSAlexandre Belloni 3021e3929efSAlexandre Belloni return 0; 3031e3929efSAlexandre Belloni } 3041e3929efSAlexandre Belloni 3051e3929efSAlexandre Belloni static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3061e3929efSAlexandre Belloni { 3071e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3081e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3091e3929efSAlexandre Belloni u8 alarmvals[3]; 3101e3929efSAlexandre Belloni u8 ctrl[2]; 3111e3929efSAlexandre Belloni int ret, err; 3121e3929efSAlexandre Belloni 3131e3929efSAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 3141e3929efSAlexandre Belloni if (alrm->time.tm_sec) { 3151e3929efSAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 3161e3929efSAlexandre Belloni 3171e3929efSAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 3181e3929efSAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 3191e3929efSAlexandre Belloni } 3201e3929efSAlexandre Belloni 3219d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 3221e3929efSAlexandre Belloni 323d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl); 324d522649eSBenoît Thébaudeau if (ret) { 3259d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 326d522649eSBenoît Thébaudeau return ret; 3271e3929efSAlexandre Belloni } 3281e3929efSAlexandre Belloni 3291e3929efSAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 3301e3929efSAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 3311e3929efSAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 3321e3929efSAlexandre Belloni 3331e3929efSAlexandre Belloni if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { 3341e3929efSAlexandre Belloni rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); 335d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3361e3929efSAlexandre Belloni rv8803->ctrl); 3371e3929efSAlexandre Belloni if (err) { 3389d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3391e3929efSAlexandre Belloni return err; 3401e3929efSAlexandre Belloni } 3411e3929efSAlexandre Belloni } 3421e3929efSAlexandre Belloni 3431e3929efSAlexandre Belloni ctrl[1] &= ~RV8803_FLAG_AF; 344d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]); 3459d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3461e3929efSAlexandre Belloni if (err) 3471e3929efSAlexandre Belloni return err; 3481e3929efSAlexandre Belloni 349d522649eSBenoît Thébaudeau err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals); 3501e3929efSAlexandre Belloni if (err) 3511e3929efSAlexandre Belloni return err; 3521e3929efSAlexandre Belloni 3531e3929efSAlexandre Belloni if (alrm->enabled) { 3541e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3551e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_UIE; 3561e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3571e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_AIE; 3581e3929efSAlexandre Belloni 359d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3601e3929efSAlexandre Belloni rv8803->ctrl); 3611e3929efSAlexandre Belloni if (err) 3621e3929efSAlexandre Belloni return err; 3631e3929efSAlexandre Belloni } 3641e3929efSAlexandre Belloni 3651e3929efSAlexandre Belloni return 0; 3661e3929efSAlexandre Belloni } 3671e3929efSAlexandre Belloni 3681e3929efSAlexandre Belloni static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) 3691e3929efSAlexandre Belloni { 3701e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3711e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3721e3929efSAlexandre Belloni int ctrl, flags, err; 3731e3929efSAlexandre Belloni 3741e3929efSAlexandre Belloni ctrl = rv8803->ctrl; 3751e3929efSAlexandre Belloni 3761e3929efSAlexandre Belloni if (enabled) { 3771e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3781e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_UIE; 3791e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3801e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_AIE; 3811e3929efSAlexandre Belloni } else { 3821e3929efSAlexandre Belloni if (!rv8803->rtc->uie_rtctimer.enabled) 3831e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_UIE; 3841e3929efSAlexandre Belloni if (!rv8803->rtc->aie_timer.enabled) 3851e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_AIE; 3861e3929efSAlexandre Belloni } 3871e3929efSAlexandre Belloni 3889d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 389d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 3901e3929efSAlexandre Belloni if (flags < 0) { 3919d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3921e3929efSAlexandre Belloni return flags; 3931e3929efSAlexandre Belloni } 3941e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); 395d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_FLAG, flags); 3969d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3971e3929efSAlexandre Belloni if (err) 3981e3929efSAlexandre Belloni return err; 3991e3929efSAlexandre Belloni 4001e3929efSAlexandre Belloni if (ctrl != rv8803->ctrl) { 4011e3929efSAlexandre Belloni rv8803->ctrl = ctrl; 402d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl); 4031e3929efSAlexandre Belloni if (err) 4041e3929efSAlexandre Belloni return err; 4051e3929efSAlexandre Belloni } 4061e3929efSAlexandre Belloni 4071e3929efSAlexandre Belloni return 0; 4081e3929efSAlexandre Belloni } 4091e3929efSAlexandre Belloni 4101e3929efSAlexandre Belloni static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 4111e3929efSAlexandre Belloni { 4121e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4131e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 4141e3929efSAlexandre Belloni int flags, ret = 0; 4151e3929efSAlexandre Belloni 4161e3929efSAlexandre Belloni switch (cmd) { 4171e3929efSAlexandre Belloni case RTC_VL_READ: 418d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4191e3929efSAlexandre Belloni if (flags < 0) 4201e3929efSAlexandre Belloni return flags; 4211e3929efSAlexandre Belloni 4221e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 4231e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 4241e3929efSAlexandre Belloni 4251e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 4261e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 4271e3929efSAlexandre Belloni 4281e3929efSAlexandre Belloni flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F; 4291e3929efSAlexandre Belloni 4301e3929efSAlexandre Belloni if (copy_to_user((void __user *)arg, &flags, sizeof(int))) 4311e3929efSAlexandre Belloni return -EFAULT; 4321e3929efSAlexandre Belloni 4331e3929efSAlexandre Belloni return 0; 4341e3929efSAlexandre Belloni 4351e3929efSAlexandre Belloni case RTC_VL_CLR: 4369d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 437d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4381e3929efSAlexandre Belloni if (flags < 0) { 4399d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 4401e3929efSAlexandre Belloni return flags; 4411e3929efSAlexandre Belloni } 4421e3929efSAlexandre Belloni 4431e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F); 444d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_FLAG, flags); 4459d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 446d522649eSBenoît Thébaudeau if (ret) 4471e3929efSAlexandre Belloni return ret; 4481e3929efSAlexandre Belloni 4491e3929efSAlexandre Belloni return 0; 4501e3929efSAlexandre Belloni 4511e3929efSAlexandre Belloni default: 4521e3929efSAlexandre Belloni return -ENOIOCTLCMD; 4531e3929efSAlexandre Belloni } 4541e3929efSAlexandre Belloni } 4551e3929efSAlexandre Belloni 45616d70a78SAlexandre Belloni static int rv8803_nvram_write(void *priv, unsigned int offset, void *val, 45716d70a78SAlexandre Belloni size_t bytes) 4581e3929efSAlexandre Belloni { 4591e3929efSAlexandre Belloni int ret; 4601e3929efSAlexandre Belloni 46116d70a78SAlexandre Belloni ret = rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val); 462d522649eSBenoît Thébaudeau if (ret) 4631e3929efSAlexandre Belloni return ret; 4641e3929efSAlexandre Belloni 46516d70a78SAlexandre Belloni return 0; 4661e3929efSAlexandre Belloni } 4671e3929efSAlexandre Belloni 46816d70a78SAlexandre Belloni static int rv8803_nvram_read(void *priv, unsigned int offset, 46916d70a78SAlexandre Belloni void *val, size_t bytes) 4701e3929efSAlexandre Belloni { 4711e3929efSAlexandre Belloni int ret; 4721e3929efSAlexandre Belloni 47316d70a78SAlexandre Belloni ret = rv8803_read_reg(priv, RV8803_RAM); 4741e3929efSAlexandre Belloni if (ret < 0) 4751e3929efSAlexandre Belloni return ret; 4761e3929efSAlexandre Belloni 47716d70a78SAlexandre Belloni *(u8 *)val = ret; 4781e3929efSAlexandre Belloni 47916d70a78SAlexandre Belloni return 0; 4801e3929efSAlexandre Belloni } 4811e3929efSAlexandre Belloni 4821e3929efSAlexandre Belloni static struct rtc_class_ops rv8803_rtc_ops = { 4831e3929efSAlexandre Belloni .read_time = rv8803_get_time, 4841e3929efSAlexandre Belloni .set_time = rv8803_set_time, 4851e3929efSAlexandre Belloni .ioctl = rv8803_ioctl, 4861e3929efSAlexandre Belloni }; 4871e3929efSAlexandre Belloni 4881cd71376SOleksij Rempel static int rx8900_trickle_charger_init(struct rv8803_data *rv8803) 4891cd71376SOleksij Rempel { 4901cd71376SOleksij Rempel struct i2c_client *client = rv8803->client; 4911cd71376SOleksij Rempel struct device_node *node = client->dev.of_node; 4921cd71376SOleksij Rempel int err; 4931cd71376SOleksij Rempel u8 flags; 4941cd71376SOleksij Rempel 4951cd71376SOleksij Rempel if (!node) 4961cd71376SOleksij Rempel return 0; 4971cd71376SOleksij Rempel 4981cd71376SOleksij Rempel if (rv8803->type != rx_8900) 4991cd71376SOleksij Rempel return 0; 5001cd71376SOleksij Rempel 5011cd71376SOleksij Rempel err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL); 5021cd71376SOleksij Rempel if (err < 0) 5031cd71376SOleksij Rempel return err; 5041cd71376SOleksij Rempel 5051cd71376SOleksij Rempel flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err; 5061cd71376SOleksij Rempel 5071cd71376SOleksij Rempel if (of_property_read_bool(node, "epson,vdet-disable")) 5081cd71376SOleksij Rempel flags |= RX8900_FLAG_VDETOFF; 5091cd71376SOleksij Rempel 5101cd71376SOleksij Rempel if (of_property_read_bool(node, "trickle-diode-disable")) 5111cd71376SOleksij Rempel flags |= RX8900_FLAG_SWOFF; 5121cd71376SOleksij Rempel 5131cd71376SOleksij Rempel return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL, 5141cd71376SOleksij Rempel flags); 5151cd71376SOleksij Rempel } 5161cd71376SOleksij Rempel 5171e3929efSAlexandre Belloni static int rv8803_probe(struct i2c_client *client, 5181e3929efSAlexandre Belloni const struct i2c_device_id *id) 5191e3929efSAlexandre Belloni { 5201e3929efSAlexandre Belloni struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 5211e3929efSAlexandre Belloni struct rv8803_data *rv8803; 522d522649eSBenoît Thébaudeau int err, flags; 523c07fd9deSAlexandre Belloni struct nvmem_config nvmem_cfg = { 524c07fd9deSAlexandre Belloni .name = "rv8803_nvram", 525c07fd9deSAlexandre Belloni .word_size = 1, 526c07fd9deSAlexandre Belloni .stride = 1, 527c07fd9deSAlexandre Belloni .size = 1, 528c07fd9deSAlexandre Belloni .reg_read = rv8803_nvram_read, 529c07fd9deSAlexandre Belloni .reg_write = rv8803_nvram_write, 530c07fd9deSAlexandre Belloni .priv = client, 531c07fd9deSAlexandre Belloni }; 5321e3929efSAlexandre Belloni 5331e3929efSAlexandre Belloni if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 5341e3929efSAlexandre Belloni I2C_FUNC_SMBUS_I2C_BLOCK)) { 5351e3929efSAlexandre Belloni dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); 5361e3929efSAlexandre Belloni return -EIO; 5371e3929efSAlexandre Belloni } 5381e3929efSAlexandre Belloni 5391e3929efSAlexandre Belloni rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), 5401e3929efSAlexandre Belloni GFP_KERNEL); 5411e3929efSAlexandre Belloni if (!rv8803) 5421e3929efSAlexandre Belloni return -ENOMEM; 5431e3929efSAlexandre Belloni 5449d1fa4c3SOleksij Rempel mutex_init(&rv8803->flags_lock); 5451e3929efSAlexandre Belloni rv8803->client = client; 546740ad8f4SJavier Martinez Canillas if (client->dev.of_node) 547740ad8f4SJavier Martinez Canillas rv8803->type = (enum rv8803_type) 548740ad8f4SJavier Martinez Canillas of_device_get_match_data(&client->dev); 549740ad8f4SJavier Martinez Canillas else 5501cd71376SOleksij Rempel rv8803->type = id->driver_data; 5511e3929efSAlexandre Belloni i2c_set_clientdata(client, rv8803); 5521e3929efSAlexandre Belloni 553d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 5541e3929efSAlexandre Belloni if (flags < 0) 5551e3929efSAlexandre Belloni return flags; 5561e3929efSAlexandre Belloni 5571e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 5581e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 5591e3929efSAlexandre Belloni 5601e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 5611e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 5621e3929efSAlexandre Belloni 5631e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) 5641e3929efSAlexandre Belloni dev_warn(&client->dev, "An alarm maybe have been missed.\n"); 5651e3929efSAlexandre Belloni 5667133eca1SAlexandre Belloni rv8803->rtc = devm_rtc_allocate_device(&client->dev); 5677133eca1SAlexandre Belloni if (IS_ERR(rv8803->rtc)) { 5687133eca1SAlexandre Belloni return PTR_ERR(rv8803->rtc); 5697133eca1SAlexandre Belloni } 5707133eca1SAlexandre Belloni 5711e3929efSAlexandre Belloni if (client->irq > 0) { 5721e3929efSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, 5731e3929efSAlexandre Belloni NULL, rv8803_handle_irq, 5741e3929efSAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 5751e3929efSAlexandre Belloni "rv8803", client); 5761e3929efSAlexandre Belloni if (err) { 5771e3929efSAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 5781e3929efSAlexandre Belloni client->irq = 0; 5791e3929efSAlexandre Belloni } else { 5801e3929efSAlexandre Belloni rv8803_rtc_ops.read_alarm = rv8803_get_alarm; 5811e3929efSAlexandre Belloni rv8803_rtc_ops.set_alarm = rv8803_set_alarm; 5821e3929efSAlexandre Belloni rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; 5831e3929efSAlexandre Belloni } 5841e3929efSAlexandre Belloni } 5851e3929efSAlexandre Belloni 586d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA); 5871e3929efSAlexandre Belloni if (err) 5881e3929efSAlexandre Belloni return err; 5891e3929efSAlexandre Belloni 5901cd71376SOleksij Rempel err = rx8900_trickle_charger_init(rv8803); 5911cd71376SOleksij Rempel if (err) { 5921cd71376SOleksij Rempel dev_err(&client->dev, "failed to init charger\n"); 5931cd71376SOleksij Rempel return err; 5941cd71376SOleksij Rempel } 5951cd71376SOleksij Rempel 596ce1ae8ebSAlexandre Belloni rv8803->rtc->ops = &rv8803_rtc_ops; 597ce1ae8ebSAlexandre Belloni rv8803->rtc->nvram_old_abi = true; 5982e17f8b9SAlexandre Belloni rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 5992e17f8b9SAlexandre Belloni rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099; 600ce1ae8ebSAlexandre Belloni err = rtc_register_device(rv8803->rtc); 601ce1ae8ebSAlexandre Belloni if (err) 602ce1ae8ebSAlexandre Belloni return err; 603ce1ae8ebSAlexandre Belloni 604ce1ae8ebSAlexandre Belloni rtc_nvmem_register(rv8803->rtc, &nvmem_cfg); 605ce1ae8ebSAlexandre Belloni 6061e3929efSAlexandre Belloni rv8803->rtc->max_user_freq = 1; 6071e3929efSAlexandre Belloni 6081e3929efSAlexandre Belloni return 0; 6091e3929efSAlexandre Belloni } 6101e3929efSAlexandre Belloni 6111e3929efSAlexandre Belloni static const struct i2c_device_id rv8803_id[] = { 6121cd71376SOleksij Rempel { "rv8803", rv_8803 }, 613ac771ed7SAlexandre Belloni { "rx8803", rv_8803 }, 6141cd71376SOleksij Rempel { "rx8900", rx_8900 }, 6151e3929efSAlexandre Belloni { } 6161e3929efSAlexandre Belloni }; 6171e3929efSAlexandre Belloni MODULE_DEVICE_TABLE(i2c, rv8803_id); 6181e3929efSAlexandre Belloni 619740ad8f4SJavier Martinez Canillas static const struct of_device_id rv8803_of_match[] = { 620740ad8f4SJavier Martinez Canillas { 621740ad8f4SJavier Martinez Canillas .compatible = "microcrystal,rv8803", 622c856618dSAlexandre Belloni .data = (void *)rv_8803 623740ad8f4SJavier Martinez Canillas }, 624740ad8f4SJavier Martinez Canillas { 625ac771ed7SAlexandre Belloni .compatible = "epson,rx8803", 626ac771ed7SAlexandre Belloni .data = (void *)rv_8803 627ac771ed7SAlexandre Belloni }, 628ac771ed7SAlexandre Belloni { 629740ad8f4SJavier Martinez Canillas .compatible = "epson,rx8900", 630740ad8f4SJavier Martinez Canillas .data = (void *)rx_8900 631740ad8f4SJavier Martinez Canillas }, 632740ad8f4SJavier Martinez Canillas { } 633740ad8f4SJavier Martinez Canillas }; 634740ad8f4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, rv8803_of_match); 635740ad8f4SJavier Martinez Canillas 6361e3929efSAlexandre Belloni static struct i2c_driver rv8803_driver = { 6371e3929efSAlexandre Belloni .driver = { 6381e3929efSAlexandre Belloni .name = "rtc-rv8803", 639740ad8f4SJavier Martinez Canillas .of_match_table = of_match_ptr(rv8803_of_match), 6401e3929efSAlexandre Belloni }, 6411e3929efSAlexandre Belloni .probe = rv8803_probe, 6421e3929efSAlexandre Belloni .id_table = rv8803_id, 6431e3929efSAlexandre Belloni }; 6441e3929efSAlexandre Belloni module_i2c_driver(rv8803_driver); 6451e3929efSAlexandre Belloni 6461e3929efSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); 6471e3929efSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); 6481e3929efSAlexandre Belloni MODULE_LICENSE("GPL v2"); 649