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 23*d522649eSBenoît Thébaudeau #define RV8803_I2C_TRY_COUNT 4 24*d522649eSBenoî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 62*d522649eSBenoît Thébaudeau static int rv8803_read_reg(const struct i2c_client *client, u8 reg) 63*d522649eSBenoît Thébaudeau { 64*d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 65*d522649eSBenoît Thébaudeau s32 ret; 66*d522649eSBenoît Thébaudeau 67*d522649eSBenoît Thébaudeau /* 68*d522649eSBenoît Thébaudeau * There is a 61µs window during which the RTC does not acknowledge I2C 69*d522649eSBenoît Thébaudeau * transfers. In that case, ensure that there are multiple attempts. 70*d522649eSBenoît Thébaudeau */ 71*d522649eSBenoît Thébaudeau do 72*d522649eSBenoît Thébaudeau ret = i2c_smbus_read_byte_data(client, reg); 73*d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 74*d522649eSBenoît Thébaudeau if (ret < 0) 75*d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to read register 0x%02x\n", reg); 76*d522649eSBenoît Thébaudeau 77*d522649eSBenoît Thébaudeau return ret; 78*d522649eSBenoît Thébaudeau } 79*d522649eSBenoît Thébaudeau 80*d522649eSBenoît Thébaudeau static int rv8803_read_regs(const struct i2c_client *client, 81*d522649eSBenoît Thébaudeau u8 reg, u8 count, u8 *values) 82*d522649eSBenoît Thébaudeau { 83*d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 84*d522649eSBenoît Thébaudeau s32 ret; 85*d522649eSBenoît Thébaudeau 86*d522649eSBenoît Thébaudeau do 87*d522649eSBenoît Thébaudeau ret = i2c_smbus_read_i2c_block_data(client, reg, count, values); 88*d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 89*d522649eSBenoît Thébaudeau if (ret != count) { 90*d522649eSBenoît Thébaudeau dev_err(&client->dev, 91*d522649eSBenoît Thébaudeau "Unable to read registers 0x%02x..0x%02x\n", 92*d522649eSBenoît Thébaudeau reg, reg + count - 1); 93*d522649eSBenoît Thébaudeau return ret < 0 ? ret : -EIO; 94*d522649eSBenoît Thébaudeau } 95*d522649eSBenoît Thébaudeau 96*d522649eSBenoît Thébaudeau return 0; 97*d522649eSBenoît Thébaudeau } 98*d522649eSBenoît Thébaudeau 99*d522649eSBenoît Thébaudeau static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) 100*d522649eSBenoît Thébaudeau { 101*d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 102*d522649eSBenoît Thébaudeau s32 ret; 103*d522649eSBenoît Thébaudeau 104*d522649eSBenoît Thébaudeau do 105*d522649eSBenoît Thébaudeau ret = i2c_smbus_write_byte_data(client, reg, value); 106*d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 107*d522649eSBenoît Thébaudeau if (ret) 108*d522649eSBenoît Thébaudeau dev_err(&client->dev, "Unable to write register 0x%02x\n", reg); 109*d522649eSBenoît Thébaudeau 110*d522649eSBenoît Thébaudeau return ret; 111*d522649eSBenoît Thébaudeau } 112*d522649eSBenoît Thébaudeau 113*d522649eSBenoît Thébaudeau static int rv8803_write_regs(const struct i2c_client *client, 114*d522649eSBenoît Thébaudeau u8 reg, u8 count, const u8 *values) 115*d522649eSBenoît Thébaudeau { 116*d522649eSBenoît Thébaudeau int try = RV8803_I2C_TRY_COUNT; 117*d522649eSBenoît Thébaudeau s32 ret; 118*d522649eSBenoît Thébaudeau 119*d522649eSBenoît Thébaudeau do 120*d522649eSBenoît Thébaudeau ret = i2c_smbus_write_i2c_block_data(client, reg, count, 121*d522649eSBenoît Thébaudeau values); 122*d522649eSBenoît Thébaudeau while ((ret == -ENXIO || ret == -EIO) && --try); 123*d522649eSBenoît Thébaudeau if (ret) 124*d522649eSBenoît Thébaudeau dev_err(&client->dev, 125*d522649eSBenoît Thébaudeau "Unable to write registers 0x%02x..0x%02x\n", 126*d522649eSBenoît Thébaudeau reg, reg + count - 1); 127*d522649eSBenoît Thébaudeau 128*d522649eSBenoît Thébaudeau return ret; 129*d522649eSBenoît Thébaudeau } 130*d522649eSBenoî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; 136*d522649eSBenoît Thébaudeau int flags; 1371e3929efSAlexandre Belloni 1389d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 1391e3929efSAlexandre Belloni 140*d522649eSBenoî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); 172*d522649eSBenoît Thébaudeau rv8803_write_reg(client, RV8803_FLAG, flags); 173*d522649eSBenoî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 189*d522649eSBenoî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 198*d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date); 199*d522649eSBenoît Thébaudeau if (ret) 200*d522649eSBenoît Thébaudeau return ret; 2011e3929efSAlexandre Belloni 2021e3929efSAlexandre Belloni if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { 203*d522649eSBenoît Thébaudeau ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2); 204*d522649eSBenoît Thébaudeau if (ret) 205*d522649eSBenoî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]; 2261e3929efSAlexandre Belloni int flags, ret; 2271e3929efSAlexandre Belloni 2281e3929efSAlexandre Belloni if ((tm->tm_year < 100) || (tm->tm_year > 199)) 2291e3929efSAlexandre Belloni return -EINVAL; 2301e3929efSAlexandre Belloni 2311e3929efSAlexandre Belloni date[RV8803_SEC] = bin2bcd(tm->tm_sec); 2321e3929efSAlexandre Belloni date[RV8803_MIN] = bin2bcd(tm->tm_min); 2331e3929efSAlexandre Belloni date[RV8803_HOUR] = bin2bcd(tm->tm_hour); 2341e3929efSAlexandre Belloni date[RV8803_WEEK] = 1 << (tm->tm_wday); 2351e3929efSAlexandre Belloni date[RV8803_DAY] = bin2bcd(tm->tm_mday); 2361e3929efSAlexandre Belloni date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); 2371e3929efSAlexandre Belloni date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); 2381e3929efSAlexandre Belloni 239*d522649eSBenoît Thébaudeau ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date); 240*d522649eSBenoît Thébaudeau if (ret) 2411e3929efSAlexandre Belloni return ret; 2421e3929efSAlexandre Belloni 2439d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 2441e3929efSAlexandre Belloni 245*d522649eSBenoît Thébaudeau flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); 2461e3929efSAlexandre Belloni if (flags < 0) { 2479d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2481e3929efSAlexandre Belloni return flags; 2491e3929efSAlexandre Belloni } 2501e3929efSAlexandre Belloni 251*d522649eSBenoît Thébaudeau ret = rv8803_write_reg(rv8803->client, RV8803_FLAG, 2521e3929efSAlexandre Belloni flags & ~RV8803_FLAG_V2F); 2531e3929efSAlexandre Belloni 2549d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 2551e3929efSAlexandre Belloni 2561e3929efSAlexandre Belloni return ret; 2571e3929efSAlexandre Belloni } 2581e3929efSAlexandre Belloni 2591e3929efSAlexandre Belloni static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 2601e3929efSAlexandre Belloni { 2611e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2621e3929efSAlexandre Belloni struct i2c_client *client = rv8803->client; 2631e3929efSAlexandre Belloni u8 alarmvals[3]; 2641e3929efSAlexandre Belloni int flags, ret; 2651e3929efSAlexandre Belloni 266*d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals); 267*d522649eSBenoît Thébaudeau if (ret) 268*d522649eSBenoît Thébaudeau return ret; 2691e3929efSAlexandre Belloni 270*d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 2711e3929efSAlexandre Belloni if (flags < 0) 2721e3929efSAlexandre Belloni return flags; 2731e3929efSAlexandre Belloni 2741e3929efSAlexandre Belloni alrm->time.tm_sec = 0; 2751e3929efSAlexandre Belloni alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 2761e3929efSAlexandre Belloni alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 2771e3929efSAlexandre Belloni alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 2781e3929efSAlexandre Belloni 2791e3929efSAlexandre Belloni alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); 2801e3929efSAlexandre Belloni alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; 2811e3929efSAlexandre Belloni 2821e3929efSAlexandre Belloni return 0; 2831e3929efSAlexandre Belloni } 2841e3929efSAlexandre Belloni 2851e3929efSAlexandre Belloni static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 2861e3929efSAlexandre Belloni { 2871e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 2881e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 2891e3929efSAlexandre Belloni u8 alarmvals[3]; 2901e3929efSAlexandre Belloni u8 ctrl[2]; 2911e3929efSAlexandre Belloni int ret, err; 2921e3929efSAlexandre Belloni 2931e3929efSAlexandre Belloni /* The alarm has no seconds, round up to nearest minute */ 2941e3929efSAlexandre Belloni if (alrm->time.tm_sec) { 2951e3929efSAlexandre Belloni time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 2961e3929efSAlexandre Belloni 2971e3929efSAlexandre Belloni alarm_time += 60 - alrm->time.tm_sec; 2981e3929efSAlexandre Belloni rtc_time64_to_tm(alarm_time, &alrm->time); 2991e3929efSAlexandre Belloni } 3001e3929efSAlexandre Belloni 3019d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 3021e3929efSAlexandre Belloni 303*d522649eSBenoît Thébaudeau ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl); 304*d522649eSBenoît Thébaudeau if (ret) { 3059d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 306*d522649eSBenoît Thébaudeau return ret; 3071e3929efSAlexandre Belloni } 3081e3929efSAlexandre Belloni 3091e3929efSAlexandre Belloni alarmvals[0] = bin2bcd(alrm->time.tm_min); 3101e3929efSAlexandre Belloni alarmvals[1] = bin2bcd(alrm->time.tm_hour); 3111e3929efSAlexandre Belloni alarmvals[2] = bin2bcd(alrm->time.tm_mday); 3121e3929efSAlexandre Belloni 3131e3929efSAlexandre Belloni if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { 3141e3929efSAlexandre Belloni rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); 315*d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3161e3929efSAlexandre Belloni rv8803->ctrl); 3171e3929efSAlexandre Belloni if (err) { 3189d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3191e3929efSAlexandre Belloni return err; 3201e3929efSAlexandre Belloni } 3211e3929efSAlexandre Belloni } 3221e3929efSAlexandre Belloni 3231e3929efSAlexandre Belloni ctrl[1] &= ~RV8803_FLAG_AF; 324*d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]); 3259d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3261e3929efSAlexandre Belloni if (err) 3271e3929efSAlexandre Belloni return err; 3281e3929efSAlexandre Belloni 329*d522649eSBenoît Thébaudeau err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals); 3301e3929efSAlexandre Belloni if (err) 3311e3929efSAlexandre Belloni return err; 3321e3929efSAlexandre Belloni 3331e3929efSAlexandre Belloni if (alrm->enabled) { 3341e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3351e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_UIE; 3361e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3371e3929efSAlexandre Belloni rv8803->ctrl |= RV8803_CTRL_AIE; 3381e3929efSAlexandre Belloni 339*d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_CTRL, 3401e3929efSAlexandre Belloni rv8803->ctrl); 3411e3929efSAlexandre Belloni if (err) 3421e3929efSAlexandre Belloni return err; 3431e3929efSAlexandre Belloni } 3441e3929efSAlexandre Belloni 3451e3929efSAlexandre Belloni return 0; 3461e3929efSAlexandre Belloni } 3471e3929efSAlexandre Belloni 3481e3929efSAlexandre Belloni static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) 3491e3929efSAlexandre Belloni { 3501e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3511e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3521e3929efSAlexandre Belloni int ctrl, flags, err; 3531e3929efSAlexandre Belloni 3541e3929efSAlexandre Belloni ctrl = rv8803->ctrl; 3551e3929efSAlexandre Belloni 3561e3929efSAlexandre Belloni if (enabled) { 3571e3929efSAlexandre Belloni if (rv8803->rtc->uie_rtctimer.enabled) 3581e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_UIE; 3591e3929efSAlexandre Belloni if (rv8803->rtc->aie_timer.enabled) 3601e3929efSAlexandre Belloni ctrl |= RV8803_CTRL_AIE; 3611e3929efSAlexandre Belloni } else { 3621e3929efSAlexandre Belloni if (!rv8803->rtc->uie_rtctimer.enabled) 3631e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_UIE; 3641e3929efSAlexandre Belloni if (!rv8803->rtc->aie_timer.enabled) 3651e3929efSAlexandre Belloni ctrl &= ~RV8803_CTRL_AIE; 3661e3929efSAlexandre Belloni } 3671e3929efSAlexandre Belloni 3689d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 369*d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 3701e3929efSAlexandre Belloni if (flags < 0) { 3719d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3721e3929efSAlexandre Belloni return flags; 3731e3929efSAlexandre Belloni } 3741e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); 375*d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_FLAG, flags); 3769d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 3771e3929efSAlexandre Belloni if (err) 3781e3929efSAlexandre Belloni return err; 3791e3929efSAlexandre Belloni 3801e3929efSAlexandre Belloni if (ctrl != rv8803->ctrl) { 3811e3929efSAlexandre Belloni rv8803->ctrl = ctrl; 382*d522649eSBenoît Thébaudeau err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl); 3831e3929efSAlexandre Belloni if (err) 3841e3929efSAlexandre Belloni return err; 3851e3929efSAlexandre Belloni } 3861e3929efSAlexandre Belloni 3871e3929efSAlexandre Belloni return 0; 3881e3929efSAlexandre Belloni } 3891e3929efSAlexandre Belloni 3901e3929efSAlexandre Belloni static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 3911e3929efSAlexandre Belloni { 3921e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 3931e3929efSAlexandre Belloni struct rv8803_data *rv8803 = dev_get_drvdata(dev); 3941e3929efSAlexandre Belloni int flags, ret = 0; 3951e3929efSAlexandre Belloni 3961e3929efSAlexandre Belloni switch (cmd) { 3971e3929efSAlexandre Belloni case RTC_VL_READ: 398*d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 3991e3929efSAlexandre Belloni if (flags < 0) 4001e3929efSAlexandre Belloni return flags; 4011e3929efSAlexandre Belloni 4021e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 4031e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 4041e3929efSAlexandre Belloni 4051e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 4061e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 4071e3929efSAlexandre Belloni 4081e3929efSAlexandre Belloni flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F; 4091e3929efSAlexandre Belloni 4101e3929efSAlexandre Belloni if (copy_to_user((void __user *)arg, &flags, sizeof(int))) 4111e3929efSAlexandre Belloni return -EFAULT; 4121e3929efSAlexandre Belloni 4131e3929efSAlexandre Belloni return 0; 4141e3929efSAlexandre Belloni 4151e3929efSAlexandre Belloni case RTC_VL_CLR: 4169d1fa4c3SOleksij Rempel mutex_lock(&rv8803->flags_lock); 417*d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 4181e3929efSAlexandre Belloni if (flags < 0) { 4199d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 4201e3929efSAlexandre Belloni return flags; 4211e3929efSAlexandre Belloni } 4221e3929efSAlexandre Belloni 4231e3929efSAlexandre Belloni flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F); 424*d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_FLAG, flags); 4259d1fa4c3SOleksij Rempel mutex_unlock(&rv8803->flags_lock); 426*d522649eSBenoît Thébaudeau if (ret) 4271e3929efSAlexandre Belloni return ret; 4281e3929efSAlexandre Belloni 4291e3929efSAlexandre Belloni return 0; 4301e3929efSAlexandre Belloni 4311e3929efSAlexandre Belloni default: 4321e3929efSAlexandre Belloni return -ENOIOCTLCMD; 4331e3929efSAlexandre Belloni } 4341e3929efSAlexandre Belloni } 4351e3929efSAlexandre Belloni 4361e3929efSAlexandre Belloni static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj, 4371e3929efSAlexandre Belloni struct bin_attribute *attr, 4381e3929efSAlexandre Belloni char *buf, loff_t off, size_t count) 4391e3929efSAlexandre Belloni { 4401e3929efSAlexandre Belloni struct device *dev = kobj_to_dev(kobj); 4411e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4421e3929efSAlexandre Belloni int ret; 4431e3929efSAlexandre Belloni 444*d522649eSBenoît Thébaudeau ret = rv8803_write_reg(client, RV8803_RAM, buf[0]); 445*d522649eSBenoît Thébaudeau if (ret) 4461e3929efSAlexandre Belloni return ret; 4471e3929efSAlexandre Belloni 4481e3929efSAlexandre Belloni return 1; 4491e3929efSAlexandre Belloni } 4501e3929efSAlexandre Belloni 4511e3929efSAlexandre Belloni static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj, 4521e3929efSAlexandre Belloni struct bin_attribute *attr, 4531e3929efSAlexandre Belloni char *buf, loff_t off, size_t count) 4541e3929efSAlexandre Belloni { 4551e3929efSAlexandre Belloni struct device *dev = kobj_to_dev(kobj); 4561e3929efSAlexandre Belloni struct i2c_client *client = to_i2c_client(dev); 4571e3929efSAlexandre Belloni int ret; 4581e3929efSAlexandre Belloni 459*d522649eSBenoît Thébaudeau ret = rv8803_read_reg(client, RV8803_RAM); 4601e3929efSAlexandre Belloni if (ret < 0) 4611e3929efSAlexandre Belloni return ret; 4621e3929efSAlexandre Belloni 4631e3929efSAlexandre Belloni buf[0] = ret; 4641e3929efSAlexandre Belloni 4651e3929efSAlexandre Belloni return 1; 4661e3929efSAlexandre Belloni } 4671e3929efSAlexandre Belloni 4681e3929efSAlexandre Belloni static struct bin_attribute rv8803_nvram_attr = { 4691e3929efSAlexandre Belloni .attr = { 4701e3929efSAlexandre Belloni .name = "nvram", 4711e3929efSAlexandre Belloni .mode = S_IRUGO | S_IWUSR, 4721e3929efSAlexandre Belloni }, 4731e3929efSAlexandre Belloni .size = 1, 4741e3929efSAlexandre Belloni .read = rv8803_nvram_read, 4751e3929efSAlexandre Belloni .write = rv8803_nvram_write, 4761e3929efSAlexandre Belloni }; 4771e3929efSAlexandre Belloni 4781e3929efSAlexandre Belloni static struct rtc_class_ops rv8803_rtc_ops = { 4791e3929efSAlexandre Belloni .read_time = rv8803_get_time, 4801e3929efSAlexandre Belloni .set_time = rv8803_set_time, 4811e3929efSAlexandre Belloni .ioctl = rv8803_ioctl, 4821e3929efSAlexandre Belloni }; 4831e3929efSAlexandre Belloni 4841e3929efSAlexandre Belloni static int rv8803_probe(struct i2c_client *client, 4851e3929efSAlexandre Belloni const struct i2c_device_id *id) 4861e3929efSAlexandre Belloni { 4871e3929efSAlexandre Belloni struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 4881e3929efSAlexandre Belloni struct rv8803_data *rv8803; 489*d522649eSBenoît Thébaudeau int err, flags; 4901e3929efSAlexandre Belloni 4911e3929efSAlexandre Belloni if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 4921e3929efSAlexandre Belloni I2C_FUNC_SMBUS_I2C_BLOCK)) { 4931e3929efSAlexandre Belloni dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); 4941e3929efSAlexandre Belloni return -EIO; 4951e3929efSAlexandre Belloni } 4961e3929efSAlexandre Belloni 4971e3929efSAlexandre Belloni rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), 4981e3929efSAlexandre Belloni GFP_KERNEL); 4991e3929efSAlexandre Belloni if (!rv8803) 5001e3929efSAlexandre Belloni return -ENOMEM; 5011e3929efSAlexandre Belloni 5029d1fa4c3SOleksij Rempel mutex_init(&rv8803->flags_lock); 5031e3929efSAlexandre Belloni rv8803->client = client; 5041e3929efSAlexandre Belloni i2c_set_clientdata(client, rv8803); 5051e3929efSAlexandre Belloni 506*d522649eSBenoît Thébaudeau flags = rv8803_read_reg(client, RV8803_FLAG); 5071e3929efSAlexandre Belloni if (flags < 0) 5081e3929efSAlexandre Belloni return flags; 5091e3929efSAlexandre Belloni 5101e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V1F) 5111e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 5121e3929efSAlexandre Belloni 5131e3929efSAlexandre Belloni if (flags & RV8803_FLAG_V2F) 5141e3929efSAlexandre Belloni dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 5151e3929efSAlexandre Belloni 5161e3929efSAlexandre Belloni if (flags & RV8803_FLAG_AF) 5171e3929efSAlexandre Belloni dev_warn(&client->dev, "An alarm maybe have been missed.\n"); 5181e3929efSAlexandre Belloni 5191e3929efSAlexandre Belloni if (client->irq > 0) { 5201e3929efSAlexandre Belloni err = devm_request_threaded_irq(&client->dev, client->irq, 5211e3929efSAlexandre Belloni NULL, rv8803_handle_irq, 5221e3929efSAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 5231e3929efSAlexandre Belloni "rv8803", client); 5241e3929efSAlexandre Belloni if (err) { 5251e3929efSAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 5261e3929efSAlexandre Belloni client->irq = 0; 5271e3929efSAlexandre Belloni } else { 5281e3929efSAlexandre Belloni rv8803_rtc_ops.read_alarm = rv8803_get_alarm; 5291e3929efSAlexandre Belloni rv8803_rtc_ops.set_alarm = rv8803_set_alarm; 5301e3929efSAlexandre Belloni rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; 5311e3929efSAlexandre Belloni } 5321e3929efSAlexandre Belloni } 5331e3929efSAlexandre Belloni 5341e3929efSAlexandre Belloni rv8803->rtc = devm_rtc_device_register(&client->dev, client->name, 5351e3929efSAlexandre Belloni &rv8803_rtc_ops, THIS_MODULE); 5361e3929efSAlexandre Belloni if (IS_ERR(rv8803->rtc)) { 5371e3929efSAlexandre Belloni dev_err(&client->dev, "unable to register the class device\n"); 5381e3929efSAlexandre Belloni return PTR_ERR(rv8803->rtc); 5391e3929efSAlexandre Belloni } 5401e3929efSAlexandre Belloni 541*d522649eSBenoît Thébaudeau err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA); 5421e3929efSAlexandre Belloni if (err) 5431e3929efSAlexandre Belloni return err; 5441e3929efSAlexandre Belloni 5451e3929efSAlexandre Belloni err = device_create_bin_file(&client->dev, &rv8803_nvram_attr); 5461e3929efSAlexandre Belloni if (err) 5471e3929efSAlexandre Belloni return err; 5481e3929efSAlexandre Belloni 5491e3929efSAlexandre Belloni rv8803->rtc->max_user_freq = 1; 5501e3929efSAlexandre Belloni 5511e3929efSAlexandre Belloni return 0; 5521e3929efSAlexandre Belloni } 5531e3929efSAlexandre Belloni 5541e3929efSAlexandre Belloni static int rv8803_remove(struct i2c_client *client) 5551e3929efSAlexandre Belloni { 5561e3929efSAlexandre Belloni device_remove_bin_file(&client->dev, &rv8803_nvram_attr); 5571e3929efSAlexandre Belloni 5581e3929efSAlexandre Belloni return 0; 5591e3929efSAlexandre Belloni } 5601e3929efSAlexandre Belloni 5611e3929efSAlexandre Belloni static const struct i2c_device_id rv8803_id[] = { 5621e3929efSAlexandre Belloni { "rv8803", 0 }, 56378ef5f2dSGregory CLEMENT { "rx8900", 0 }, 5641e3929efSAlexandre Belloni { } 5651e3929efSAlexandre Belloni }; 5661e3929efSAlexandre Belloni MODULE_DEVICE_TABLE(i2c, rv8803_id); 5671e3929efSAlexandre Belloni 5681e3929efSAlexandre Belloni static struct i2c_driver rv8803_driver = { 5691e3929efSAlexandre Belloni .driver = { 5701e3929efSAlexandre Belloni .name = "rtc-rv8803", 5711e3929efSAlexandre Belloni }, 5721e3929efSAlexandre Belloni .probe = rv8803_probe, 5731e3929efSAlexandre Belloni .remove = rv8803_remove, 5741e3929efSAlexandre Belloni .id_table = rv8803_id, 5751e3929efSAlexandre Belloni }; 5761e3929efSAlexandre Belloni module_i2c_driver(rv8803_driver); 5771e3929efSAlexandre Belloni 5781e3929efSAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); 5791e3929efSAlexandre Belloni MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); 5801e3929efSAlexandre Belloni MODULE_LICENSE("GPL v2"); 581