11e3929efSAlexandre Belloni /* 21e3929efSAlexandre Belloni * RTC driver for the Micro Crystal RV8803 31e3929efSAlexandre Belloni * 41e3929efSAlexandre Belloni * Copyright (C) 2015 Micro Crystal SA 51e3929efSAlexandre Belloni * 61e3929efSAlexandre Belloni * Alexandre Belloni <alexandre.belloni@free-electrons.com> 71e3929efSAlexandre Belloni * 81e3929efSAlexandre Belloni * This program is free software; you can redistribute it and/or modify 91e3929efSAlexandre Belloni * it under the terms of the GNU General Public License version 2 as 101e3929efSAlexandre Belloni * published by the Free Software Foundation. 111e3929efSAlexandre Belloni * 121e3929efSAlexandre Belloni */ 131e3929efSAlexandre Belloni 141e3929efSAlexandre Belloni #include <linux/bcd.h> 151e3929efSAlexandre Belloni #include <linux/bitops.h> 16a1e98e09SBenoît Thébaudeau #include <linux/log2.h> 171e3929efSAlexandre Belloni #include <linux/i2c.h> 181e3929efSAlexandre Belloni #include <linux/interrupt.h> 191e3929efSAlexandre Belloni #include <linux/kernel.h> 201e3929efSAlexandre Belloni #include <linux/module.h> 21*740ad8f4SJavier Martinez Canillas #include <linux/of_device.h> 221e3929efSAlexandre Belloni #include <linux/rtc.h> 231e3929efSAlexandre Belloni 24d522649eSBenoît Thébaudeau #define RV8803_I2C_TRY_COUNT 4 25d522649eSBenoît Thébaudeau 261e3929efSAlexandre Belloni #define RV8803_SEC 0x00 271e3929efSAlexandre Belloni #define RV8803_MIN 0x01 281e3929efSAlexandre Belloni #define RV8803_HOUR 0x02 291e3929efSAlexandre Belloni #define RV8803_WEEK 0x03 301e3929efSAlexandre Belloni #define RV8803_DAY 0x04 311e3929efSAlexandre Belloni #define RV8803_MONTH 0x05 321e3929efSAlexandre Belloni #define RV8803_YEAR 0x06 331e3929efSAlexandre Belloni #define RV8803_RAM 0x07 341e3929efSAlexandre Belloni #define RV8803_ALARM_MIN 0x08 351e3929efSAlexandre Belloni #define RV8803_ALARM_HOUR 0x09 361e3929efSAlexandre Belloni #define RV8803_ALARM_WEEK_OR_DAY 0x0A 371e3929efSAlexandre Belloni #define RV8803_EXT 0x0D 381e3929efSAlexandre Belloni #define RV8803_FLAG 0x0E 391e3929efSAlexandre Belloni #define RV8803_CTRL 0x0F 401e3929efSAlexandre Belloni 411e3929efSAlexandre Belloni #define RV8803_EXT_WADA BIT(6) 421e3929efSAlexandre Belloni 431e3929efSAlexandre Belloni #define RV8803_FLAG_V1F BIT(0) 441e3929efSAlexandre Belloni #define RV8803_FLAG_V2F BIT(1) 451e3929efSAlexandre Belloni #define RV8803_FLAG_AF BIT(3) 461e3929efSAlexandre Belloni #define RV8803_FLAG_TF BIT(4) 471e3929efSAlexandre Belloni #define RV8803_FLAG_UF BIT(5) 481e3929efSAlexandre Belloni 491e3929efSAlexandre Belloni #define RV8803_CTRL_RESET BIT(0) 501e3929efSAlexandre Belloni 511e3929efSAlexandre Belloni #define RV8803_CTRL_EIE BIT(2) 521e3929efSAlexandre Belloni #define RV8803_CTRL_AIE BIT(3) 531e3929efSAlexandre Belloni #define RV8803_CTRL_TIE BIT(4) 541e3929efSAlexandre Belloni #define RV8803_CTRL_UIE BIT(5) 551e3929efSAlexandre Belloni 561cd71376SOleksij Rempel #define RX8900_BACKUP_CTRL 0x18 571cd71376SOleksij Rempel #define RX8900_FLAG_SWOFF BIT(2) 581cd71376SOleksij Rempel #define RX8900_FLAG_VDETOFF BIT(3) 591cd71376SOleksij Rempel 601cd71376SOleksij Rempel enum rv8803_type { 611cd71376SOleksij Rempel rv_8803, 621cd71376SOleksij Rempel rx_8900 631cd71376SOleksij Rempel }; 641cd71376SOleksij Rempel 651e3929efSAlexandre Belloni struct rv8803_data { 661e3929efSAlexandre Belloni struct i2c_client *client; 671e3929efSAlexandre Belloni struct rtc_device *rtc; 689d1fa4c3SOleksij Rempel struct mutex flags_lock; 691e3929efSAlexandre Belloni u8 ctrl; 701cd71376SOleksij Rempel enum rv8803_type type; 711e3929efSAlexandre Belloni }; 721e3929efSAlexandre Belloni 73d522649eSBenoît Thébaudeau static int rv8803_read_reg(const struct i2c_client *client, u8 reg) 74d522649eSBenoît Thébaudeau { 75d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 76d522649eSBenoît Thébaudeau s32 ret; 77d522649eSBenoît Thébaudeau 78d522649eSBenoît Thébaudeau /* 79d522649eSBenoît Thébaudeau * There is a 61µs window during which the RTC does not acknowledge I2C 80d522649eSBenoît Thébaudeau * transfers. In that case, ensure that there are multiple attempts. 81d522649eSBenoît Thébaudeau */ 82d522649eSBenoît Thébaudeau do 83d522649eSBenoît Thébaudeau ret = i2c_smbus_read_byte_data(client, reg); 84d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 85d522649eSBenoît Thébaudeau if (ret < 0) 86d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to read register 0x%02x\n", reg); 87d522649eSBenoît Thébaudeau 88d522649eSBenoît Thébaudeau return ret; 89d522649eSBenoît Thébaudeau } 90d522649eSBenoît Thébaudeau 91d522649eSBenoît Thébaudeau static int rv8803_read_regs(const struct i2c_client *client, 92d522649eSBenoît Thébaudeau u8 reg, u8 count, u8 *values) 93d522649eSBenoît Thébaudeau { 94d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 95d522649eSBenoît Thébaudeau s32 ret; 96d522649eSBenoît Thébaudeau 97d522649eSBenoît Thébaudeau do 98d522649eSBenoît Thébaudeau ret = i2c_smbus_read_i2c_block_data(client, reg, count, values); 99d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 100d522649eSBenoît Thébaudeau if (ret != count) { 101d522649eSBenoît Thébaudeau dev_err(&client->dev, 102d522649eSBenoît Thébaudeau "Unable to read registers 0x%02x..0x%02x\n", 103d522649eSBenoît Thébaudeau reg, reg + count - 1); 104d522649eSBenoît Thébaudeau return ret < 0 ? ret : -EIO; 105d522649eSBenoît Thébaudeau } 106d522649eSBenoît Thébaudeau 107d522649eSBenoît Thébaudeau return 0; 108d522649eSBenoît Thébaudeau } 109d522649eSBenoît Thébaudeau 110d522649eSBenoît Thébaudeau static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) 111d522649eSBenoît Thébaudeau { 112d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 113d522649eSBenoît Thébaudeau s32 ret; 114d522649eSBenoît Thébaudeau 115d522649eSBenoît Thébaudeau do 116d522649eSBenoît Thébaudeau ret = i2c_smbus_write_byte_data(client, reg, value); 117d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 118d522649eSBenoît Thébaudeau if (ret) 119d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to write register 0x%02x\n", reg); 120d522649eSBenoît Thébaudeau 121d522649eSBenoît Thébaudeau return ret; 122d522649eSBenoît Thébaudeau } 123d522649eSBenoît Thébaudeau 124d522649eSBenoît Thébaudeau static int rv8803_write_regs(const struct i2c_client *client, 125d522649eSBenoît Thébaudeau u8 reg, u8 count, const u8 *values) 126d522649eSBenoît Thébaudeau { 127d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 128d522649eSBenoît Thébaudeau s32 ret; 129d522649eSBenoît Thébaudeau 130d522649eSBenoît Thébaudeau do 131d522649eSBenoît Thébaudeau ret = i2c_smbus_write_i2c_block_data(client, reg, count, 132d522649eSBenoît Thébaudeau values); 133d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 134d522649eSBenoît Thébaudeau if (ret) 135d522649eSBenoît Thébaudeau dev_err(&client->dev, 136d522649eSBenoît Thébaudeau "Unable to write registers 0x%02x..0x%02x\n", 137d522649eSBenoît Thébaudeau reg, reg + count - 1); 138d522649eSBenoît Thébaudeau 139d522649eSBenoît Thébaudeau return ret; 140d522649eSBenoît Thébaudeau } 141d522649eSBenoît Thébaudeau 1421e3929efSAlexandre Belloni static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) 1431e3929efSAlexandre Belloni { 1441e3929efSAlexandre Belloni struct i2c_client *client = dev_id; 1451e3929efSAlexandre Belloni struct rv8803_data *rv8803 = i2c_get_clientdata(client); 1461e3929efSAlexandre Belloni unsigned long events = 0; 147d522649eSBenoît Thébaudeau int flags; 1481e3929efSAlexandre Belloni 1499d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 1501e3929efSAlexandre Belloni 151d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 1521e3929efSAlexandre Belloni if (flags <= 0) { 1539d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 1541e3929efSAlexandre Belloni return IRQ_NONE; 1551e3929efSAlexandre Belloni } 1561e3929efSAlexandre Belloni 1571e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 1581e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 1591e3929efSAlexandre Belloni 1601e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 1611e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 1621e3929efSAlexandre Belloni 1631e3929efSAlexandre Belloni if (flags & RV8803_FLAG_TF) { 1641e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_TF; 1651e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_TIE; 1661e3929efSAlexandre Belloni events |= RTC_PF; 1671e3929efSAlexandre Belloni } 1681e3929efSAlexandre Belloni 1691e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) { 1701e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_AF; 1711e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_AIE; 1721e3929efSAlexandre Belloni events |= RTC_AF; 1731e3929efSAlexandre Belloni } 1741e3929efSAlexandre Belloni 1751e3929efSAlexandre Belloni if (flags & RV8803_FLAG_UF) { 1761e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_UF; 1771e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_UIE; 1781e3929efSAlexandre Belloni events |= RTC_UF; 1791e3929efSAlexandre Belloni } 1801e3929efSAlexandre Belloni 1811e3929efSAlexandre Belloni if (events) { 1821e3929efSAlexandre Belloni rtc_update_irq(rv8803->rtc, 1, events); 183d522649eSBenoît Thébaudeau rv8803_write_reg(client, RV8803_FLAG, flags); 184d522649eSBenoît Thébaudeau rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl); 1851e3929efSAlexandre Belloni } 1861e3929efSAlexandre Belloni 1879d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 1881e3929efSAlexandre Belloni 1891e3929efSAlexandre Belloni return IRQ_HANDLED; 1901e3929efSAlexandre Belloni } 1911e3929efSAlexandre Belloni 1921e3929efSAlexandre Belloni static int rv8803_get_time(struct device *dev, struct rtc_time *tm) 1931e3929efSAlexandre Belloni { 1941e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 1951e3929efSAlexandre Belloni u8 date1[7]; 1961e3929efSAlexandre Belloni u8 date2[7]; 1971e3929efSAlexandre Belloni u8 *date = date1; 1981e3929efSAlexandre Belloni int ret, flags; 1991e3929efSAlexandre Belloni 200d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 2011e3929efSAlexandre Belloni if (flags < 0) 2021e3929efSAlexandre Belloni return flags; 2031e3929efSAlexandre Belloni 2041e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) { 2051e3929efSAlexandre Belloni dev_warn(dev, "Voltage low, data is invalid.\n"); 2061e3929efSAlexandre Belloni return -EINVAL; 2071e3929efSAlexandre Belloni } 2081e3929efSAlexandre Belloni 209d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date); 210d522649eSBenoît Thébaudeau if (ret) 211d522649eSBenoît Thébaudeau return ret; 2121e3929efSAlexandre Belloni 2131e3929efSAlexandre Belloni if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { 214d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2); 215d522649eSBenoît Thébaudeau if (ret) 216d522649eSBenoît Thébaudeau return ret; 2171e3929efSAlexandre Belloni 2181e3929efSAlexandre Belloni if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) 2191e3929efSAlexandre Belloni date = date2; 2201e3929efSAlexandre Belloni } 2211e3929efSAlexandre Belloni 2221e3929efSAlexandre Belloni tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); 2231e3929efSAlexandre Belloni tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); 2241e3929efSAlexandre Belloni tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); 225a1e98e09SBenoît Thébaudeau tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); 2261e3929efSAlexandre Belloni tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); 2271e3929efSAlexandre Belloni tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; 2281e3929efSAlexandre Belloni tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; 2291e3929efSAlexandre Belloni 23096acb25cSBenoît Thébaudeau return 0; 2311e3929efSAlexandre Belloni } 2321e3929efSAlexandre Belloni 2331e3929efSAlexandre Belloni static int rv8803_set_time(struct device *dev, struct rtc_time *tm) 2341e3929efSAlexandre Belloni { 2351e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2361e3929efSAlexandre Belloni u8 date[7]; 237d3700b6bSBenoît Thébaudeau int ctrl, flags, ret; 2381e3929efSAlexandre Belloni 2391e3929efSAlexandre Belloni if ((tm->tm_year < 100) || (tm->tm_year > 199)) 2401e3929efSAlexandre Belloni return -EINVAL; 2411e3929efSAlexandre Belloni 242d3700b6bSBenoît Thébaudeau ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL); 243d3700b6bSBenoît Thébaudeau if (ctrl < 0) 244d3700b6bSBenoît Thébaudeau return ctrl; 245d3700b6bSBenoît Thébaudeau 246d3700b6bSBenoît Thébaudeau /* Stop the clock */ 247d3700b6bSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 248d3700b6bSBenoît Thébaudeau ctrl | RV8803_CTRL_RESET); 249d3700b6bSBenoît Thébaudeau if (ret) 250d3700b6bSBenoît Thébaudeau return ret; 251d3700b6bSBenoît Thébaudeau 2521e3929efSAlexandre Belloni date[RV8803_SEC] = bin2bcd(tm->tm_sec); 2531e3929efSAlexandre Belloni date[RV8803_MIN] = bin2bcd(tm->tm_min); 2541e3929efSAlexandre Belloni date[RV8803_HOUR] = bin2bcd(tm->tm_hour); 2551e3929efSAlexandre Belloni date[RV8803_WEEK] = 1 << (tm->tm_wday); 2561e3929efSAlexandre Belloni date[RV8803_DAY] = bin2bcd(tm->tm_mday); 2571e3929efSAlexandre Belloni date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); 2581e3929efSAlexandre Belloni date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); 2591e3929efSAlexandre Belloni 260d522649eSBenoît Thébaudeau ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date); 261d522649eSBenoît Thébaudeau if (ret) 2621e3929efSAlexandre Belloni return ret; 2631e3929efSAlexandre Belloni 264d3700b6bSBenoît Thébaudeau /* Restart the clock */ 265d3700b6bSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 266d3700b6bSBenoît Thébaudeau ctrl & ~RV8803_CTRL_RESET); 267d3700b6bSBenoît Thébaudeau if (ret) 268d3700b6bSBenoît Thébaudeau return ret; 269d3700b6bSBenoît Thébaudeau 2709d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 2711e3929efSAlexandre Belloni 272d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 2731e3929efSAlexandre Belloni if (flags < 0) { 2749d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2751e3929efSAlexandre Belloni return flags; 2761e3929efSAlexandre Belloni } 2771e3929efSAlexandre Belloni 278d522649eSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_FLAG, 2796f367788SBenoît Thébaudeau flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F)); 2801e3929efSAlexandre Belloni 2819d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2821e3929efSAlexandre Belloni 2831e3929efSAlexandre Belloni return ret; 2841e3929efSAlexandre Belloni } 2851e3929efSAlexandre Belloni 2861e3929efSAlexandre Belloni static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 2871e3929efSAlexandre Belloni { 2881e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2891e3929efSAlexandre Belloni struct i2c_client *client = rv8803->client; 2901e3929efSAlexandre Belloni u8 alarmvals[3]; 2911e3929efSAlexandre Belloni int flags, ret; 2921e3929efSAlexandre Belloni 293d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals); 294d522649eSBenoît Thébaudeau if (ret) 295d522649eSBenoît Thébaudeau return ret; 2961e3929efSAlexandre Belloni 297d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 2981e3929efSAlexandre Belloni if (flags < 0) 2991e3929efSAlexandre Belloni return flags; 3001e3929efSAlexandre Belloni 3011e3929efSAlexandre Belloni alrm->time.tm_sec = 0; 3021e3929efSAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 3031e3929efSAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 3041e3929efSAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 3051e3929efSAlexandre Belloni 3061e3929efSAlexandre Belloni alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); 3071e3929efSAlexandre Belloni alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; 3081e3929efSAlexandre Belloni 3091e3929efSAlexandre Belloni return 0; 3101e3929efSAlexandre Belloni } 3111e3929efSAlexandre Belloni 3121e3929efSAlexandre Belloni static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3131e3929efSAlexandre Belloni { 3141e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3151e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3161e3929efSAlexandre Belloni u8 alarmvals[3]; 3171e3929efSAlexandre Belloni u8 ctrl[2]; 3181e3929efSAlexandre Belloni int ret, err; 3191e3929efSAlexandre Belloni 3201e3929efSAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 3211e3929efSAlexandre Belloni if (alrm->time.tm_sec) { 3221e3929efSAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 3231e3929efSAlexandre Belloni 3241e3929efSAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 3251e3929efSAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 3261e3929efSAlexandre Belloni } 3271e3929efSAlexandre Belloni 3289d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 3291e3929efSAlexandre Belloni 330d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl); 331d522649eSBenoît Thébaudeau if (ret) { 3329d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 333d522649eSBenoît Thébaudeau return ret; 3341e3929efSAlexandre Belloni } 3351e3929efSAlexandre Belloni 3361e3929efSAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 3371e3929efSAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 3381e3929efSAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 3391e3929efSAlexandre Belloni 3401e3929efSAlexandre Belloni if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { 3411e3929efSAlexandre Belloni rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); 342d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3431e3929efSAlexandre Belloni rv8803->ctrl); 3441e3929efSAlexandre Belloni if (err) { 3459d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3461e3929efSAlexandre Belloni return err; 3471e3929efSAlexandre Belloni } 3481e3929efSAlexandre Belloni } 3491e3929efSAlexandre Belloni 3501e3929efSAlexandre Belloni ctrl[1] &= ~RV8803_FLAG_AF; 351d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]); 3529d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3531e3929efSAlexandre Belloni if (err) 3541e3929efSAlexandre Belloni return err; 3551e3929efSAlexandre Belloni 356d522649eSBenoît Thébaudeau err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals); 3571e3929efSAlexandre Belloni if (err) 3581e3929efSAlexandre Belloni return err; 3591e3929efSAlexandre Belloni 3601e3929efSAlexandre Belloni if (alrm->enabled) { 3611e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3621e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_UIE; 3631e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3641e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_AIE; 3651e3929efSAlexandre Belloni 366d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3671e3929efSAlexandre Belloni rv8803->ctrl); 3681e3929efSAlexandre Belloni if (err) 3691e3929efSAlexandre Belloni return err; 3701e3929efSAlexandre Belloni } 3711e3929efSAlexandre Belloni 3721e3929efSAlexandre Belloni return 0; 3731e3929efSAlexandre Belloni } 3741e3929efSAlexandre Belloni 3751e3929efSAlexandre Belloni static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) 3761e3929efSAlexandre Belloni { 3771e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3781e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3791e3929efSAlexandre Belloni int ctrl, flags, err; 3801e3929efSAlexandre Belloni 3811e3929efSAlexandre Belloni ctrl = rv8803->ctrl; 3821e3929efSAlexandre Belloni 3831e3929efSAlexandre Belloni if (enabled) { 3841e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3851e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_UIE; 3861e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3871e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_AIE; 3881e3929efSAlexandre Belloni } else { 3891e3929efSAlexandre Belloni if (!rv8803->rtc->uie_rtctimer.enabled) 3901e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_UIE; 3911e3929efSAlexandre Belloni if (!rv8803->rtc->aie_timer.enabled) 3921e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_AIE; 3931e3929efSAlexandre Belloni } 3941e3929efSAlexandre Belloni 3959d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 396d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 3971e3929efSAlexandre Belloni if (flags < 0) { 3989d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3991e3929efSAlexandre Belloni return flags; 4001e3929efSAlexandre Belloni } 4011e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); 402d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_FLAG, flags); 4039d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 4041e3929efSAlexandre Belloni if (err) 4051e3929efSAlexandre Belloni return err; 4061e3929efSAlexandre Belloni 4071e3929efSAlexandre Belloni if (ctrl != rv8803->ctrl) { 4081e3929efSAlexandre Belloni rv8803->ctrl = ctrl; 409d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl); 4101e3929efSAlexandre Belloni if (err) 4111e3929efSAlexandre Belloni return err; 4121e3929efSAlexandre Belloni } 4131e3929efSAlexandre Belloni 4141e3929efSAlexandre Belloni return 0; 4151e3929efSAlexandre Belloni } 4161e3929efSAlexandre Belloni 4171e3929efSAlexandre Belloni static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 4181e3929efSAlexandre Belloni { 4191e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4201e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 4211e3929efSAlexandre Belloni int flags, ret = 0; 4221e3929efSAlexandre Belloni 4231e3929efSAlexandre Belloni switch (cmd) { 4241e3929efSAlexandre Belloni case RTC_VL_READ: 425d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4261e3929efSAlexandre Belloni if (flags < 0) 4271e3929efSAlexandre Belloni return flags; 4281e3929efSAlexandre Belloni 4291e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 4301e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 4311e3929efSAlexandre Belloni 4321e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 4331e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 4341e3929efSAlexandre Belloni 4351e3929efSAlexandre Belloni flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F; 4361e3929efSAlexandre Belloni 4371e3929efSAlexandre Belloni if (copy_to_user((void __user *)arg, &flags, sizeof(int))) 4381e3929efSAlexandre Belloni return -EFAULT; 4391e3929efSAlexandre Belloni 4401e3929efSAlexandre Belloni return 0; 4411e3929efSAlexandre Belloni 4421e3929efSAlexandre Belloni case RTC_VL_CLR: 4439d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 444d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4451e3929efSAlexandre Belloni if (flags < 0) { 4469d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 4471e3929efSAlexandre Belloni return flags; 4481e3929efSAlexandre Belloni } 4491e3929efSAlexandre Belloni 4501e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F); 451d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_FLAG, flags); 4529d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 453d522649eSBenoît Thébaudeau if (ret) 4541e3929efSAlexandre Belloni return ret; 4551e3929efSAlexandre Belloni 4561e3929efSAlexandre Belloni return 0; 4571e3929efSAlexandre Belloni 4581e3929efSAlexandre Belloni default: 4591e3929efSAlexandre Belloni return -ENOIOCTLCMD; 4601e3929efSAlexandre Belloni } 4611e3929efSAlexandre Belloni } 4621e3929efSAlexandre Belloni 4631e3929efSAlexandre Belloni static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj, 4641e3929efSAlexandre Belloni struct bin_attribute *attr, 4651e3929efSAlexandre Belloni char *buf, loff_t off, size_t count) 4661e3929efSAlexandre Belloni { 4671e3929efSAlexandre Belloni struct device *dev = kobj_to_dev(kobj); 4681e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4691e3929efSAlexandre Belloni int ret; 4701e3929efSAlexandre Belloni 471d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_RAM, buf[0]); 472d522649eSBenoît Thébaudeau if (ret) 4731e3929efSAlexandre Belloni return ret; 4741e3929efSAlexandre Belloni 4751e3929efSAlexandre Belloni return 1; 4761e3929efSAlexandre Belloni } 4771e3929efSAlexandre Belloni 4781e3929efSAlexandre Belloni static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj, 4791e3929efSAlexandre Belloni struct bin_attribute *attr, 4801e3929efSAlexandre Belloni char *buf, loff_t off, size_t count) 4811e3929efSAlexandre Belloni { 4821e3929efSAlexandre Belloni struct device *dev = kobj_to_dev(kobj); 4831e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4841e3929efSAlexandre Belloni int ret; 4851e3929efSAlexandre Belloni 486d522649eSBenoît Thébaudeau ret = rv8803_read_reg(client, RV8803_RAM); 4871e3929efSAlexandre Belloni if (ret < 0) 4881e3929efSAlexandre Belloni return ret; 4891e3929efSAlexandre Belloni 4901e3929efSAlexandre Belloni buf[0] = ret; 4911e3929efSAlexandre Belloni 4921e3929efSAlexandre Belloni return 1; 4931e3929efSAlexandre Belloni } 4941e3929efSAlexandre Belloni 4951e3929efSAlexandre Belloni static struct bin_attribute rv8803_nvram_attr = { 4961e3929efSAlexandre Belloni .attr = { 4971e3929efSAlexandre Belloni .name = "nvram", 4981e3929efSAlexandre Belloni .mode = S_IRUGO | S_IWUSR, 4991e3929efSAlexandre Belloni }, 5001e3929efSAlexandre Belloni .size = 1, 5011e3929efSAlexandre Belloni .read = rv8803_nvram_read, 5021e3929efSAlexandre Belloni .write = rv8803_nvram_write, 5031e3929efSAlexandre Belloni }; 5041e3929efSAlexandre Belloni 5051e3929efSAlexandre Belloni static struct rtc_class_ops rv8803_rtc_ops = { 5061e3929efSAlexandre Belloni .read_time = rv8803_get_time, 5071e3929efSAlexandre Belloni .set_time = rv8803_set_time, 5081e3929efSAlexandre Belloni .ioctl = rv8803_ioctl, 5091e3929efSAlexandre Belloni }; 5101e3929efSAlexandre Belloni 5111cd71376SOleksij Rempel static int rx8900_trickle_charger_init(struct rv8803_data *rv8803) 5121cd71376SOleksij Rempel { 5131cd71376SOleksij Rempel struct i2c_client *client = rv8803->client; 5141cd71376SOleksij Rempel struct device_node *node = client->dev.of_node; 5151cd71376SOleksij Rempel int err; 5161cd71376SOleksij Rempel u8 flags; 5171cd71376SOleksij Rempel 5181cd71376SOleksij Rempel if (!node) 5191cd71376SOleksij Rempel return 0; 5201cd71376SOleksij Rempel 5211cd71376SOleksij Rempel if (rv8803->type != rx_8900) 5221cd71376SOleksij Rempel return 0; 5231cd71376SOleksij Rempel 5241cd71376SOleksij Rempel err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL); 5251cd71376SOleksij Rempel if (err < 0) 5261cd71376SOleksij Rempel return err; 5271cd71376SOleksij Rempel 5281cd71376SOleksij Rempel flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err; 5291cd71376SOleksij Rempel 5301cd71376SOleksij Rempel if (of_property_read_bool(node, "epson,vdet-disable")) 5311cd71376SOleksij Rempel flags |= RX8900_FLAG_VDETOFF; 5321cd71376SOleksij Rempel 5331cd71376SOleksij Rempel if (of_property_read_bool(node, "trickle-diode-disable")) 5341cd71376SOleksij Rempel flags |= RX8900_FLAG_SWOFF; 5351cd71376SOleksij Rempel 5361cd71376SOleksij Rempel return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL, 5371cd71376SOleksij Rempel flags); 5381cd71376SOleksij Rempel } 5391cd71376SOleksij Rempel 5401e3929efSAlexandre Belloni static int rv8803_probe(struct i2c_client *client, 5411e3929efSAlexandre Belloni const struct i2c_device_id *id) 5421e3929efSAlexandre Belloni { 5431e3929efSAlexandre Belloni struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 5441e3929efSAlexandre Belloni struct rv8803_data *rv8803; 545d522649eSBenoît Thébaudeau int err, flags; 5461e3929efSAlexandre Belloni 5471e3929efSAlexandre Belloni if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 5481e3929efSAlexandre Belloni I2C_FUNC_SMBUS_I2C_BLOCK)) { 5491e3929efSAlexandre Belloni dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); 5501e3929efSAlexandre Belloni return -EIO; 5511e3929efSAlexandre Belloni } 5521e3929efSAlexandre Belloni 5531e3929efSAlexandre Belloni rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), 5541e3929efSAlexandre Belloni GFP_KERNEL); 5551e3929efSAlexandre Belloni if (!rv8803) 5561e3929efSAlexandre Belloni return -ENOMEM; 5571e3929efSAlexandre Belloni 5589d1fa4c3SOleksij Rempel mutex_init(&rv8803->flags_lock); 5591e3929efSAlexandre Belloni rv8803->client = client; 560*740ad8f4SJavier Martinez Canillas if (client->dev.of_node) 561*740ad8f4SJavier Martinez Canillas rv8803->type = (enum rv8803_type) 562*740ad8f4SJavier Martinez Canillas of_device_get_match_data(&client->dev); 563*740ad8f4SJavier Martinez Canillas else 5641cd71376SOleksij Rempel rv8803->type = id->driver_data; 5651e3929efSAlexandre Belloni i2c_set_clientdata(client, rv8803); 5661e3929efSAlexandre Belloni 567d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 5681e3929efSAlexandre Belloni if (flags < 0) 5691e3929efSAlexandre Belloni return flags; 5701e3929efSAlexandre Belloni 5711e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 5721e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 5731e3929efSAlexandre Belloni 5741e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 5751e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 5761e3929efSAlexandre Belloni 5771e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) 5781e3929efSAlexandre Belloni dev_warn(&client->dev, "An alarm maybe have been missed.\n"); 5791e3929efSAlexandre Belloni 5801e3929efSAlexandre Belloni if (client->irq > 0) { 5811e3929efSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, 5821e3929efSAlexandre Belloni NULL, rv8803_handle_irq, 5831e3929efSAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 5841e3929efSAlexandre Belloni "rv8803", client); 5851e3929efSAlexandre Belloni if (err) { 5861e3929efSAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 5871e3929efSAlexandre Belloni client->irq = 0; 5881e3929efSAlexandre Belloni } else { 5891e3929efSAlexandre Belloni rv8803_rtc_ops.read_alarm = rv8803_get_alarm; 5901e3929efSAlexandre Belloni rv8803_rtc_ops.set_alarm = rv8803_set_alarm; 5911e3929efSAlexandre Belloni rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; 5921e3929efSAlexandre Belloni } 5931e3929efSAlexandre Belloni } 5941e3929efSAlexandre Belloni 5951e3929efSAlexandre Belloni rv8803->rtc = devm_rtc_device_register(&client->dev, client->name, 5961e3929efSAlexandre Belloni &rv8803_rtc_ops, THIS_MODULE); 5971e3929efSAlexandre Belloni if (IS_ERR(rv8803->rtc)) { 5981e3929efSAlexandre Belloni dev_err(&client->dev, "unable to register the class device\n"); 5991e3929efSAlexandre Belloni return PTR_ERR(rv8803->rtc); 6001e3929efSAlexandre Belloni } 6011e3929efSAlexandre Belloni 602d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA); 6031e3929efSAlexandre Belloni if (err) 6041e3929efSAlexandre Belloni return err; 6051e3929efSAlexandre Belloni 6061cd71376SOleksij Rempel err = rx8900_trickle_charger_init(rv8803); 6071cd71376SOleksij Rempel if (err) { 6081cd71376SOleksij Rempel dev_err(&client->dev, "failed to init charger\n"); 6091cd71376SOleksij Rempel return err; 6101cd71376SOleksij Rempel } 6111cd71376SOleksij Rempel 6121e3929efSAlexandre Belloni err = device_create_bin_file(&client->dev, &rv8803_nvram_attr); 6131e3929efSAlexandre Belloni if (err) 6141e3929efSAlexandre Belloni return err; 6151e3929efSAlexandre Belloni 6161e3929efSAlexandre Belloni rv8803->rtc->max_user_freq = 1; 6171e3929efSAlexandre Belloni 6181e3929efSAlexandre Belloni return 0; 6191e3929efSAlexandre Belloni } 6201e3929efSAlexandre Belloni 6211e3929efSAlexandre Belloni static int rv8803_remove(struct i2c_client *client) 6221e3929efSAlexandre Belloni { 6231e3929efSAlexandre Belloni device_remove_bin_file(&client->dev, &rv8803_nvram_attr); 6241e3929efSAlexandre Belloni 6251e3929efSAlexandre Belloni return 0; 6261e3929efSAlexandre Belloni } 6271e3929efSAlexandre Belloni 6281e3929efSAlexandre Belloni static const struct i2c_device_id rv8803_id[] = { 6291cd71376SOleksij Rempel { "rv8803", rv_8803 }, 6301cd71376SOleksij Rempel { "rx8900", rx_8900 }, 6311e3929efSAlexandre Belloni { } 6321e3929efSAlexandre Belloni }; 6331e3929efSAlexandre Belloni MODULE_DEVICE_TABLE(i2c, rv8803_id); 6341e3929efSAlexandre Belloni 635*740ad8f4SJavier Martinez Canillas static const struct of_device_id rv8803_of_match[] = { 636*740ad8f4SJavier Martinez Canillas { 637*740ad8f4SJavier Martinez Canillas .compatible = "microcrystal,rv8803", 638*740ad8f4SJavier Martinez Canillas .data = (void *)rx_8900 639*740ad8f4SJavier Martinez Canillas }, 640*740ad8f4SJavier Martinez Canillas { 641*740ad8f4SJavier Martinez Canillas .compatible = "epson,rx8900", 642*740ad8f4SJavier Martinez Canillas .data = (void *)rx_8900 643*740ad8f4SJavier Martinez Canillas }, 644*740ad8f4SJavier Martinez Canillas { } 645*740ad8f4SJavier Martinez Canillas }; 646*740ad8f4SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, rv8803_of_match); 647*740ad8f4SJavier Martinez Canillas 6481e3929efSAlexandre Belloni static struct i2c_driver rv8803_driver = { 6491e3929efSAlexandre Belloni .driver = { 6501e3929efSAlexandre Belloni .name = "rtc-rv8803", 651*740ad8f4SJavier Martinez Canillas .of_match_table = of_match_ptr(rv8803_of_match), 6521e3929efSAlexandre Belloni }, 6531e3929efSAlexandre Belloni .probe = rv8803_probe, 6541e3929efSAlexandre Belloni .remove = rv8803_remove, 6551e3929efSAlexandre Belloni .id_table = rv8803_id, 6561e3929efSAlexandre Belloni }; 6571e3929efSAlexandre Belloni module_i2c_driver(rv8803_driver); 6581e3929efSAlexandre Belloni 6591e3929efSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); 6601e3929efSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); 6611e3929efSAlexandre Belloni MODULE_LICENSE("GPL v2"); 662