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> 211e3929efSAlexandre Belloni #include <linux/rtc.h> 221e3929efSAlexandre Belloni 23d522649eSBenoît Thébaudeau #define RV8803_I2C_TRY_COUNT 4 24d522649eSBenoît Thébaudeau 251e3929efSAlexandre Belloni #define RV8803_SEC 0x00 261e3929efSAlexandre Belloni #define RV8803_MIN 0x01 271e3929efSAlexandre Belloni #define RV8803_HOUR 0x02 281e3929efSAlexandre Belloni #define RV8803_WEEK 0x03 291e3929efSAlexandre Belloni #define RV8803_DAY 0x04 301e3929efSAlexandre Belloni #define RV8803_MONTH 0x05 311e3929efSAlexandre Belloni #define RV8803_YEAR 0x06 321e3929efSAlexandre Belloni #define RV8803_RAM 0x07 331e3929efSAlexandre Belloni #define RV8803_ALARM_MIN 0x08 341e3929efSAlexandre Belloni #define RV8803_ALARM_HOUR 0x09 351e3929efSAlexandre Belloni #define RV8803_ALARM_WEEK_OR_DAY 0x0A 361e3929efSAlexandre Belloni #define RV8803_EXT 0x0D 371e3929efSAlexandre Belloni #define RV8803_FLAG 0x0E 381e3929efSAlexandre Belloni #define RV8803_CTRL 0x0F 391e3929efSAlexandre Belloni 401e3929efSAlexandre Belloni #define RV8803_EXT_WADA BIT(6) 411e3929efSAlexandre Belloni 421e3929efSAlexandre Belloni #define RV8803_FLAG_V1F BIT(0) 431e3929efSAlexandre Belloni #define RV8803_FLAG_V2F BIT(1) 441e3929efSAlexandre Belloni #define RV8803_FLAG_AF BIT(3) 451e3929efSAlexandre Belloni #define RV8803_FLAG_TF BIT(4) 461e3929efSAlexandre Belloni #define RV8803_FLAG_UF BIT(5) 471e3929efSAlexandre Belloni 481e3929efSAlexandre Belloni #define RV8803_CTRL_RESET BIT(0) 491e3929efSAlexandre Belloni 501e3929efSAlexandre Belloni #define RV8803_CTRL_EIE BIT(2) 511e3929efSAlexandre Belloni #define RV8803_CTRL_AIE BIT(3) 521e3929efSAlexandre Belloni #define RV8803_CTRL_TIE BIT(4) 531e3929efSAlexandre Belloni #define RV8803_CTRL_UIE BIT(5) 541e3929efSAlexandre Belloni 551e3929efSAlexandre Belloni struct rv8803_data { 561e3929efSAlexandre Belloni struct i2c_client *client; 571e3929efSAlexandre Belloni struct rtc_device *rtc; 589d1fa4c3SOleksij Rempel struct mutex flags_lock; 591e3929efSAlexandre Belloni u8 ctrl; 601e3929efSAlexandre Belloni }; 611e3929efSAlexandre Belloni 62d522649eSBenoît Thébaudeau static int rv8803_read_reg(const struct i2c_client *client, u8 reg) 63d522649eSBenoît Thébaudeau { 64d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 65d522649eSBenoît Thébaudeau s32 ret; 66d522649eSBenoît Thébaudeau 67d522649eSBenoît Thébaudeau /* 68d522649eSBenoît Thébaudeau * There is a 61µs window during which the RTC does not acknowledge I2C 69d522649eSBenoît Thébaudeau * transfers. In that case, ensure that there are multiple attempts. 70d522649eSBenoît Thébaudeau */ 71d522649eSBenoît Thébaudeau do 72d522649eSBenoît Thébaudeau ret = i2c_smbus_read_byte_data(client, reg); 73d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 74d522649eSBenoît Thébaudeau if (ret < 0) 75d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to read register 0x%02x\n", reg); 76d522649eSBenoît Thébaudeau 77d522649eSBenoît Thébaudeau return ret; 78d522649eSBenoît Thébaudeau } 79d522649eSBenoît Thébaudeau 80d522649eSBenoît Thébaudeau static int rv8803_read_regs(const struct i2c_client *client, 81d522649eSBenoît Thébaudeau u8 reg, u8 count, u8 *values) 82d522649eSBenoît Thébaudeau { 83d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 84d522649eSBenoît Thébaudeau s32 ret; 85d522649eSBenoît Thébaudeau 86d522649eSBenoît Thébaudeau do 87d522649eSBenoît Thébaudeau ret = i2c_smbus_read_i2c_block_data(client, reg, count, values); 88d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 89d522649eSBenoît Thébaudeau if (ret != count) { 90d522649eSBenoît Thébaudeau dev_err(&client->dev, 91d522649eSBenoît Thébaudeau "Unable to read registers 0x%02x..0x%02x\n", 92d522649eSBenoît Thébaudeau reg, reg + count - 1); 93d522649eSBenoît Thébaudeau return ret < 0 ? ret : -EIO; 94d522649eSBenoît Thébaudeau } 95d522649eSBenoît Thébaudeau 96d522649eSBenoît Thébaudeau return 0; 97d522649eSBenoît Thébaudeau } 98d522649eSBenoît Thébaudeau 99d522649eSBenoît Thébaudeau static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) 100d522649eSBenoît Thébaudeau { 101d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 102d522649eSBenoît Thébaudeau s32 ret; 103d522649eSBenoît Thébaudeau 104d522649eSBenoît Thébaudeau do 105d522649eSBenoît Thébaudeau ret = i2c_smbus_write_byte_data(client, reg, value); 106d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 107d522649eSBenoît Thébaudeau if (ret) 108d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to write register 0x%02x\n", reg); 109d522649eSBenoît Thébaudeau 110d522649eSBenoît Thébaudeau return ret; 111d522649eSBenoît Thébaudeau } 112d522649eSBenoît Thébaudeau 113d522649eSBenoît Thébaudeau static int rv8803_write_regs(const struct i2c_client *client, 114d522649eSBenoît Thébaudeau u8 reg, u8 count, const u8 *values) 115d522649eSBenoît Thébaudeau { 116d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 117d522649eSBenoît Thébaudeau s32 ret; 118d522649eSBenoît Thébaudeau 119d522649eSBenoît Thébaudeau do 120d522649eSBenoît Thébaudeau ret = i2c_smbus_write_i2c_block_data(client, reg, count, 121d522649eSBenoît Thébaudeau values); 122d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 123d522649eSBenoît Thébaudeau if (ret) 124d522649eSBenoît Thébaudeau dev_err(&client->dev, 125d522649eSBenoît Thébaudeau "Unable to write registers 0x%02x..0x%02x\n", 126d522649eSBenoît Thébaudeau reg, reg + count - 1); 127d522649eSBenoît Thébaudeau 128d522649eSBenoît Thébaudeau return ret; 129d522649eSBenoît Thébaudeau } 130d522649eSBenoît Thébaudeau 1311e3929efSAlexandre Belloni static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) 1321e3929efSAlexandre Belloni { 1331e3929efSAlexandre Belloni struct i2c_client *client = dev_id; 1341e3929efSAlexandre Belloni struct rv8803_data *rv8803 = i2c_get_clientdata(client); 1351e3929efSAlexandre Belloni unsigned long events = 0; 136d522649eSBenoît Thébaudeau int flags; 1371e3929efSAlexandre Belloni 1389d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 1391e3929efSAlexandre Belloni 140d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 1411e3929efSAlexandre Belloni if (flags <= 0) { 1429d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 1431e3929efSAlexandre Belloni return IRQ_NONE; 1441e3929efSAlexandre Belloni } 1451e3929efSAlexandre Belloni 1461e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 1471e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 1481e3929efSAlexandre Belloni 1491e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 1501e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 1511e3929efSAlexandre Belloni 1521e3929efSAlexandre Belloni if (flags & RV8803_FLAG_TF) { 1531e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_TF; 1541e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_TIE; 1551e3929efSAlexandre Belloni events |= RTC_PF; 1561e3929efSAlexandre Belloni } 1571e3929efSAlexandre Belloni 1581e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) { 1591e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_AF; 1601e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_AIE; 1611e3929efSAlexandre Belloni events |= RTC_AF; 1621e3929efSAlexandre Belloni } 1631e3929efSAlexandre Belloni 1641e3929efSAlexandre Belloni if (flags & RV8803_FLAG_UF) { 1651e3929efSAlexandre Belloni flags &= ~RV8803_FLAG_UF; 1661e3929efSAlexandre Belloni rv8803->ctrl &= ~RV8803_CTRL_UIE; 1671e3929efSAlexandre Belloni events |= RTC_UF; 1681e3929efSAlexandre Belloni } 1691e3929efSAlexandre Belloni 1701e3929efSAlexandre Belloni if (events) { 1711e3929efSAlexandre Belloni rtc_update_irq(rv8803->rtc, 1, events); 172d522649eSBenoît Thébaudeau rv8803_write_reg(client, RV8803_FLAG, flags); 173d522649eSBenoît Thébaudeau rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl); 1741e3929efSAlexandre Belloni } 1751e3929efSAlexandre Belloni 1769d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 1771e3929efSAlexandre Belloni 1781e3929efSAlexandre Belloni return IRQ_HANDLED; 1791e3929efSAlexandre Belloni } 1801e3929efSAlexandre Belloni 1811e3929efSAlexandre Belloni static int rv8803_get_time(struct device *dev, struct rtc_time *tm) 1821e3929efSAlexandre Belloni { 1831e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 1841e3929efSAlexandre Belloni u8 date1[7]; 1851e3929efSAlexandre Belloni u8 date2[7]; 1861e3929efSAlexandre Belloni u8 *date = date1; 1871e3929efSAlexandre Belloni int ret, flags; 1881e3929efSAlexandre Belloni 189d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 1901e3929efSAlexandre Belloni if (flags < 0) 1911e3929efSAlexandre Belloni return flags; 1921e3929efSAlexandre Belloni 1931e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) { 1941e3929efSAlexandre Belloni dev_warn(dev, "Voltage low, data is invalid.\n"); 1951e3929efSAlexandre Belloni return -EINVAL; 1961e3929efSAlexandre Belloni } 1971e3929efSAlexandre Belloni 198d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date); 199d522649eSBenoît Thébaudeau if (ret) 200d522649eSBenoît Thébaudeau return ret; 2011e3929efSAlexandre Belloni 2021e3929efSAlexandre Belloni if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { 203d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2); 204d522649eSBenoît Thébaudeau if (ret) 205d522649eSBenoît Thébaudeau return ret; 2061e3929efSAlexandre Belloni 2071e3929efSAlexandre Belloni if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) 2081e3929efSAlexandre Belloni date = date2; 2091e3929efSAlexandre Belloni } 2101e3929efSAlexandre Belloni 2111e3929efSAlexandre Belloni tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); 2121e3929efSAlexandre Belloni tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); 2131e3929efSAlexandre Belloni tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); 214a1e98e09SBenoît Thébaudeau tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); 2151e3929efSAlexandre Belloni tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); 2161e3929efSAlexandre Belloni tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; 2171e3929efSAlexandre Belloni tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; 2181e3929efSAlexandre Belloni 21996acb25cSBenoît Thébaudeau return 0; 2201e3929efSAlexandre Belloni } 2211e3929efSAlexandre Belloni 2221e3929efSAlexandre Belloni static int rv8803_set_time(struct device *dev, struct rtc_time *tm) 2231e3929efSAlexandre Belloni { 2241e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2251e3929efSAlexandre Belloni u8 date[7]; 226*d3700b6bSBenoît Thébaudeau int ctrl, flags, ret; 2271e3929efSAlexandre Belloni 2281e3929efSAlexandre Belloni if ((tm->tm_year < 100) || (tm->tm_year > 199)) 2291e3929efSAlexandre Belloni return -EINVAL; 2301e3929efSAlexandre Belloni 231*d3700b6bSBenoît Thébaudeau ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL); 232*d3700b6bSBenoît Thébaudeau if (ctrl < 0) 233*d3700b6bSBenoît Thébaudeau return ctrl; 234*d3700b6bSBenoît Thébaudeau 235*d3700b6bSBenoît Thébaudeau /* Stop the clock */ 236*d3700b6bSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 237*d3700b6bSBenoît Thébaudeau ctrl | RV8803_CTRL_RESET); 238*d3700b6bSBenoît Thébaudeau if (ret) 239*d3700b6bSBenoît Thébaudeau return ret; 240*d3700b6bSBenoît Thébaudeau 2411e3929efSAlexandre Belloni date[RV8803_SEC] = bin2bcd(tm->tm_sec); 2421e3929efSAlexandre Belloni date[RV8803_MIN] = bin2bcd(tm->tm_min); 2431e3929efSAlexandre Belloni date[RV8803_HOUR] = bin2bcd(tm->tm_hour); 2441e3929efSAlexandre Belloni date[RV8803_WEEK] = 1 << (tm->tm_wday); 2451e3929efSAlexandre Belloni date[RV8803_DAY] = bin2bcd(tm->tm_mday); 2461e3929efSAlexandre Belloni date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); 2471e3929efSAlexandre Belloni date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); 2481e3929efSAlexandre Belloni 249d522649eSBenoît Thébaudeau ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date); 250d522649eSBenoît Thébaudeau if (ret) 2511e3929efSAlexandre Belloni return ret; 2521e3929efSAlexandre Belloni 253*d3700b6bSBenoît Thébaudeau /* Restart the clock */ 254*d3700b6bSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, 255*d3700b6bSBenoît Thébaudeau ctrl & ~RV8803_CTRL_RESET); 256*d3700b6bSBenoît Thébaudeau if (ret) 257*d3700b6bSBenoît Thébaudeau return ret; 258*d3700b6bSBenoît Thébaudeau 2599d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 2601e3929efSAlexandre Belloni 261d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 2621e3929efSAlexandre Belloni if (flags < 0) { 2639d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2641e3929efSAlexandre Belloni return flags; 2651e3929efSAlexandre Belloni } 2661e3929efSAlexandre Belloni 267d522649eSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_FLAG, 2681e3929efSAlexandre Belloni flags & ~RV8803_FLAG_V2F); 2691e3929efSAlexandre Belloni 2709d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2711e3929efSAlexandre Belloni 2721e3929efSAlexandre Belloni return ret; 2731e3929efSAlexandre Belloni } 2741e3929efSAlexandre Belloni 2751e3929efSAlexandre Belloni static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 2761e3929efSAlexandre Belloni { 2771e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2781e3929efSAlexandre Belloni struct i2c_client *client = rv8803->client; 2791e3929efSAlexandre Belloni u8 alarmvals[3]; 2801e3929efSAlexandre Belloni int flags, ret; 2811e3929efSAlexandre Belloni 282d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals); 283d522649eSBenoît Thébaudeau if (ret) 284d522649eSBenoît Thébaudeau return ret; 2851e3929efSAlexandre Belloni 286d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 2871e3929efSAlexandre Belloni if (flags < 0) 2881e3929efSAlexandre Belloni return flags; 2891e3929efSAlexandre Belloni 2901e3929efSAlexandre Belloni alrm->time.tm_sec = 0; 2911e3929efSAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 2921e3929efSAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 2931e3929efSAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 2941e3929efSAlexandre Belloni 2951e3929efSAlexandre Belloni alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); 2961e3929efSAlexandre Belloni alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; 2971e3929efSAlexandre Belloni 2981e3929efSAlexandre Belloni return 0; 2991e3929efSAlexandre Belloni } 3001e3929efSAlexandre Belloni 3011e3929efSAlexandre Belloni static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 3021e3929efSAlexandre Belloni { 3031e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3041e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3051e3929efSAlexandre Belloni u8 alarmvals[3]; 3061e3929efSAlexandre Belloni u8 ctrl[2]; 3071e3929efSAlexandre Belloni int ret, err; 3081e3929efSAlexandre Belloni 3091e3929efSAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 3101e3929efSAlexandre Belloni if (alrm->time.tm_sec) { 3111e3929efSAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 3121e3929efSAlexandre Belloni 3131e3929efSAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 3141e3929efSAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 3151e3929efSAlexandre Belloni } 3161e3929efSAlexandre Belloni 3179d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 3181e3929efSAlexandre Belloni 319d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl); 320d522649eSBenoît Thébaudeau if (ret) { 3219d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 322d522649eSBenoît Thébaudeau return ret; 3231e3929efSAlexandre Belloni } 3241e3929efSAlexandre Belloni 3251e3929efSAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 3261e3929efSAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 3271e3929efSAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 3281e3929efSAlexandre Belloni 3291e3929efSAlexandre Belloni if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { 3301e3929efSAlexandre Belloni rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); 331d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3321e3929efSAlexandre Belloni rv8803->ctrl); 3331e3929efSAlexandre Belloni if (err) { 3349d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3351e3929efSAlexandre Belloni return err; 3361e3929efSAlexandre Belloni } 3371e3929efSAlexandre Belloni } 3381e3929efSAlexandre Belloni 3391e3929efSAlexandre Belloni ctrl[1] &= ~RV8803_FLAG_AF; 340d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]); 3419d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3421e3929efSAlexandre Belloni if (err) 3431e3929efSAlexandre Belloni return err; 3441e3929efSAlexandre Belloni 345d522649eSBenoît Thébaudeau err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals); 3461e3929efSAlexandre Belloni if (err) 3471e3929efSAlexandre Belloni return err; 3481e3929efSAlexandre Belloni 3491e3929efSAlexandre Belloni if (alrm->enabled) { 3501e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3511e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_UIE; 3521e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3531e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_AIE; 3541e3929efSAlexandre Belloni 355d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3561e3929efSAlexandre Belloni rv8803->ctrl); 3571e3929efSAlexandre Belloni if (err) 3581e3929efSAlexandre Belloni return err; 3591e3929efSAlexandre Belloni } 3601e3929efSAlexandre Belloni 3611e3929efSAlexandre Belloni return 0; 3621e3929efSAlexandre Belloni } 3631e3929efSAlexandre Belloni 3641e3929efSAlexandre Belloni static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) 3651e3929efSAlexandre Belloni { 3661e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3671e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3681e3929efSAlexandre Belloni int ctrl, flags, err; 3691e3929efSAlexandre Belloni 3701e3929efSAlexandre Belloni ctrl = rv8803->ctrl; 3711e3929efSAlexandre Belloni 3721e3929efSAlexandre Belloni if (enabled) { 3731e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3741e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_UIE; 3751e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3761e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_AIE; 3771e3929efSAlexandre Belloni } else { 3781e3929efSAlexandre Belloni if (!rv8803->rtc->uie_rtctimer.enabled) 3791e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_UIE; 3801e3929efSAlexandre Belloni if (!rv8803->rtc->aie_timer.enabled) 3811e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_AIE; 3821e3929efSAlexandre Belloni } 3831e3929efSAlexandre Belloni 3849d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 385d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 3861e3929efSAlexandre Belloni if (flags < 0) { 3879d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3881e3929efSAlexandre Belloni return flags; 3891e3929efSAlexandre Belloni } 3901e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); 391d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_FLAG, flags); 3929d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3931e3929efSAlexandre Belloni if (err) 3941e3929efSAlexandre Belloni return err; 3951e3929efSAlexandre Belloni 3961e3929efSAlexandre Belloni if (ctrl != rv8803->ctrl) { 3971e3929efSAlexandre Belloni rv8803->ctrl = ctrl; 398d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl); 3991e3929efSAlexandre Belloni if (err) 4001e3929efSAlexandre Belloni return err; 4011e3929efSAlexandre Belloni } 4021e3929efSAlexandre Belloni 4031e3929efSAlexandre Belloni return 0; 4041e3929efSAlexandre Belloni } 4051e3929efSAlexandre Belloni 4061e3929efSAlexandre Belloni static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 4071e3929efSAlexandre Belloni { 4081e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4091e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 4101e3929efSAlexandre Belloni int flags, ret = 0; 4111e3929efSAlexandre Belloni 4121e3929efSAlexandre Belloni switch (cmd) { 4131e3929efSAlexandre Belloni case RTC_VL_READ: 414d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4151e3929efSAlexandre Belloni if (flags < 0) 4161e3929efSAlexandre Belloni return flags; 4171e3929efSAlexandre Belloni 4181e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 4191e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 4201e3929efSAlexandre Belloni 4211e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 4221e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 4231e3929efSAlexandre Belloni 4241e3929efSAlexandre Belloni flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F; 4251e3929efSAlexandre Belloni 4261e3929efSAlexandre Belloni if (copy_to_user((void __user *)arg, &flags, sizeof(int))) 4271e3929efSAlexandre Belloni return -EFAULT; 4281e3929efSAlexandre Belloni 4291e3929efSAlexandre Belloni return 0; 4301e3929efSAlexandre Belloni 4311e3929efSAlexandre Belloni case RTC_VL_CLR: 4329d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 433d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4341e3929efSAlexandre Belloni if (flags < 0) { 4359d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 4361e3929efSAlexandre Belloni return flags; 4371e3929efSAlexandre Belloni } 4381e3929efSAlexandre Belloni 4391e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F); 440d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_FLAG, flags); 4419d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 442d522649eSBenoît Thébaudeau if (ret) 4431e3929efSAlexandre Belloni return ret; 4441e3929efSAlexandre Belloni 4451e3929efSAlexandre Belloni return 0; 4461e3929efSAlexandre Belloni 4471e3929efSAlexandre Belloni default: 4481e3929efSAlexandre Belloni return -ENOIOCTLCMD; 4491e3929efSAlexandre Belloni } 4501e3929efSAlexandre Belloni } 4511e3929efSAlexandre Belloni 4521e3929efSAlexandre Belloni static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj, 4531e3929efSAlexandre Belloni struct bin_attribute *attr, 4541e3929efSAlexandre Belloni char *buf, loff_t off, size_t count) 4551e3929efSAlexandre Belloni { 4561e3929efSAlexandre Belloni struct device *dev = kobj_to_dev(kobj); 4571e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4581e3929efSAlexandre Belloni int ret; 4591e3929efSAlexandre Belloni 460d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_RAM, buf[0]); 461d522649eSBenoît Thébaudeau if (ret) 4621e3929efSAlexandre Belloni return ret; 4631e3929efSAlexandre Belloni 4641e3929efSAlexandre Belloni return 1; 4651e3929efSAlexandre Belloni } 4661e3929efSAlexandre Belloni 4671e3929efSAlexandre Belloni static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj, 4681e3929efSAlexandre Belloni struct bin_attribute *attr, 4691e3929efSAlexandre Belloni char *buf, loff_t off, size_t count) 4701e3929efSAlexandre Belloni { 4711e3929efSAlexandre Belloni struct device *dev = kobj_to_dev(kobj); 4721e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4731e3929efSAlexandre Belloni int ret; 4741e3929efSAlexandre Belloni 475d522649eSBenoît Thébaudeau ret = rv8803_read_reg(client, RV8803_RAM); 4761e3929efSAlexandre Belloni if (ret < 0) 4771e3929efSAlexandre Belloni return ret; 4781e3929efSAlexandre Belloni 4791e3929efSAlexandre Belloni buf[0] = ret; 4801e3929efSAlexandre Belloni 4811e3929efSAlexandre Belloni return 1; 4821e3929efSAlexandre Belloni } 4831e3929efSAlexandre Belloni 4841e3929efSAlexandre Belloni static struct bin_attribute rv8803_nvram_attr = { 4851e3929efSAlexandre Belloni .attr = { 4861e3929efSAlexandre Belloni .name = "nvram", 4871e3929efSAlexandre Belloni .mode = S_IRUGO | S_IWUSR, 4881e3929efSAlexandre Belloni }, 4891e3929efSAlexandre Belloni .size = 1, 4901e3929efSAlexandre Belloni .read = rv8803_nvram_read, 4911e3929efSAlexandre Belloni .write = rv8803_nvram_write, 4921e3929efSAlexandre Belloni }; 4931e3929efSAlexandre Belloni 4941e3929efSAlexandre Belloni static struct rtc_class_ops rv8803_rtc_ops = { 4951e3929efSAlexandre Belloni .read_time = rv8803_get_time, 4961e3929efSAlexandre Belloni .set_time = rv8803_set_time, 4971e3929efSAlexandre Belloni .ioctl = rv8803_ioctl, 4981e3929efSAlexandre Belloni }; 4991e3929efSAlexandre Belloni 5001e3929efSAlexandre Belloni static int rv8803_probe(struct i2c_client *client, 5011e3929efSAlexandre Belloni const struct i2c_device_id *id) 5021e3929efSAlexandre Belloni { 5031e3929efSAlexandre Belloni struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 5041e3929efSAlexandre Belloni struct rv8803_data *rv8803; 505d522649eSBenoît Thébaudeau int err, flags; 5061e3929efSAlexandre Belloni 5071e3929efSAlexandre Belloni if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 5081e3929efSAlexandre Belloni I2C_FUNC_SMBUS_I2C_BLOCK)) { 5091e3929efSAlexandre Belloni dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); 5101e3929efSAlexandre Belloni return -EIO; 5111e3929efSAlexandre Belloni } 5121e3929efSAlexandre Belloni 5131e3929efSAlexandre Belloni rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), 5141e3929efSAlexandre Belloni GFP_KERNEL); 5151e3929efSAlexandre Belloni if (!rv8803) 5161e3929efSAlexandre Belloni return -ENOMEM; 5171e3929efSAlexandre Belloni 5189d1fa4c3SOleksij Rempel mutex_init(&rv8803->flags_lock); 5191e3929efSAlexandre Belloni rv8803->client = client; 5201e3929efSAlexandre Belloni i2c_set_clientdata(client, rv8803); 5211e3929efSAlexandre Belloni 522d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 5231e3929efSAlexandre Belloni if (flags < 0) 5241e3929efSAlexandre Belloni return flags; 5251e3929efSAlexandre Belloni 5261e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 5271e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 5281e3929efSAlexandre Belloni 5291e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 5301e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 5311e3929efSAlexandre Belloni 5321e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) 5331e3929efSAlexandre Belloni dev_warn(&client->dev, "An alarm maybe have been missed.\n"); 5341e3929efSAlexandre Belloni 5351e3929efSAlexandre Belloni if (client->irq > 0) { 5361e3929efSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, 5371e3929efSAlexandre Belloni NULL, rv8803_handle_irq, 5381e3929efSAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 5391e3929efSAlexandre Belloni "rv8803", client); 5401e3929efSAlexandre Belloni if (err) { 5411e3929efSAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 5421e3929efSAlexandre Belloni client->irq = 0; 5431e3929efSAlexandre Belloni } else { 5441e3929efSAlexandre Belloni rv8803_rtc_ops.read_alarm = rv8803_get_alarm; 5451e3929efSAlexandre Belloni rv8803_rtc_ops.set_alarm = rv8803_set_alarm; 5461e3929efSAlexandre Belloni rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; 5471e3929efSAlexandre Belloni } 5481e3929efSAlexandre Belloni } 5491e3929efSAlexandre Belloni 5501e3929efSAlexandre Belloni rv8803->rtc = devm_rtc_device_register(&client->dev, client->name, 5511e3929efSAlexandre Belloni &rv8803_rtc_ops, THIS_MODULE); 5521e3929efSAlexandre Belloni if (IS_ERR(rv8803->rtc)) { 5531e3929efSAlexandre Belloni dev_err(&client->dev, "unable to register the class device\n"); 5541e3929efSAlexandre Belloni return PTR_ERR(rv8803->rtc); 5551e3929efSAlexandre Belloni } 5561e3929efSAlexandre Belloni 557d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA); 5581e3929efSAlexandre Belloni if (err) 5591e3929efSAlexandre Belloni return err; 5601e3929efSAlexandre Belloni 5611e3929efSAlexandre Belloni err = device_create_bin_file(&client->dev, &rv8803_nvram_attr); 5621e3929efSAlexandre Belloni if (err) 5631e3929efSAlexandre Belloni return err; 5641e3929efSAlexandre Belloni 5651e3929efSAlexandre Belloni rv8803->rtc->max_user_freq = 1; 5661e3929efSAlexandre Belloni 5671e3929efSAlexandre Belloni return 0; 5681e3929efSAlexandre Belloni } 5691e3929efSAlexandre Belloni 5701e3929efSAlexandre Belloni static int rv8803_remove(struct i2c_client *client) 5711e3929efSAlexandre Belloni { 5721e3929efSAlexandre Belloni device_remove_bin_file(&client->dev, &rv8803_nvram_attr); 5731e3929efSAlexandre Belloni 5741e3929efSAlexandre Belloni return 0; 5751e3929efSAlexandre Belloni } 5761e3929efSAlexandre Belloni 5771e3929efSAlexandre Belloni static const struct i2c_device_id rv8803_id[] = { 5781e3929efSAlexandre Belloni { "rv8803", 0 }, 57978ef5f2dSGregory CLEMENT { "rx8900", 0 }, 5801e3929efSAlexandre Belloni { } 5811e3929efSAlexandre Belloni }; 5821e3929efSAlexandre Belloni MODULE_DEVICE_TABLE(i2c, rv8803_id); 5831e3929efSAlexandre Belloni 5841e3929efSAlexandre Belloni static struct i2c_driver rv8803_driver = { 5851e3929efSAlexandre Belloni .driver = { 5861e3929efSAlexandre Belloni .name = "rtc-rv8803", 5871e3929efSAlexandre Belloni }, 5881e3929efSAlexandre Belloni .probe = rv8803_probe, 5891e3929efSAlexandre Belloni .remove = rv8803_remove, 5901e3929efSAlexandre Belloni .id_table = rv8803_id, 5911e3929efSAlexandre Belloni }; 5921e3929efSAlexandre Belloni module_i2c_driver(rv8803_driver); 5931e3929efSAlexandre Belloni 5941e3929efSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); 5951e3929efSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); 5961e3929efSAlexandre Belloni MODULE_LICENSE("GPL v2"); 597