Alexandre Belloni | 182ae2b | 2019-03-04 20:12:17 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 2 | /* |
| 3 | * RTC driver for the Micro Crystal RV8803 |
| 4 | * |
| 5 | * Copyright (C) 2015 Micro Crystal SA |
Alexandre Belloni | 7d1e5bf | 2019-03-04 20:17:38 +0100 | [diff] [blame] | 6 | * Alexandre Belloni <alexandre.belloni@bootlin.com> |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 7 | * |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/bcd.h> |
| 11 | #include <linux/bitops.h> |
Benoît Thébaudeau | a1e98e0 | 2016-07-21 12:41:29 +0200 | [diff] [blame] | 12 | #include <linux/log2.h> |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 13 | #include <linux/i2c.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Javier Martinez Canillas | 740ad8f | 2017-03-03 11:29:12 -0300 | [diff] [blame] | 17 | #include <linux/of_device.h> |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 18 | #include <linux/rtc.h> |
| 19 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 20 | #define RV8803_I2C_TRY_COUNT 4 |
| 21 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 22 | #define RV8803_SEC 0x00 |
| 23 | #define RV8803_MIN 0x01 |
| 24 | #define RV8803_HOUR 0x02 |
| 25 | #define RV8803_WEEK 0x03 |
| 26 | #define RV8803_DAY 0x04 |
| 27 | #define RV8803_MONTH 0x05 |
| 28 | #define RV8803_YEAR 0x06 |
| 29 | #define RV8803_RAM 0x07 |
| 30 | #define RV8803_ALARM_MIN 0x08 |
| 31 | #define RV8803_ALARM_HOUR 0x09 |
| 32 | #define RV8803_ALARM_WEEK_OR_DAY 0x0A |
| 33 | #define RV8803_EXT 0x0D |
| 34 | #define RV8803_FLAG 0x0E |
| 35 | #define RV8803_CTRL 0x0F |
| 36 | |
| 37 | #define RV8803_EXT_WADA BIT(6) |
| 38 | |
| 39 | #define RV8803_FLAG_V1F BIT(0) |
| 40 | #define RV8803_FLAG_V2F BIT(1) |
| 41 | #define RV8803_FLAG_AF BIT(3) |
| 42 | #define RV8803_FLAG_TF BIT(4) |
| 43 | #define RV8803_FLAG_UF BIT(5) |
| 44 | |
| 45 | #define RV8803_CTRL_RESET BIT(0) |
| 46 | |
| 47 | #define RV8803_CTRL_EIE BIT(2) |
| 48 | #define RV8803_CTRL_AIE BIT(3) |
| 49 | #define RV8803_CTRL_TIE BIT(4) |
| 50 | #define RV8803_CTRL_UIE BIT(5) |
| 51 | |
Oleksij Rempel | 1cd7137 | 2016-06-29 16:40:01 +0200 | [diff] [blame] | 52 | #define RX8900_BACKUP_CTRL 0x18 |
| 53 | #define RX8900_FLAG_SWOFF BIT(2) |
| 54 | #define RX8900_FLAG_VDETOFF BIT(3) |
| 55 | |
| 56 | enum rv8803_type { |
| 57 | rv_8803, |
| 58 | rx_8900 |
| 59 | }; |
| 60 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 61 | struct rv8803_data { |
| 62 | struct i2c_client *client; |
| 63 | struct rtc_device *rtc; |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 64 | struct mutex flags_lock; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 65 | u8 ctrl; |
Oleksij Rempel | 1cd7137 | 2016-06-29 16:40:01 +0200 | [diff] [blame] | 66 | enum rv8803_type type; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 69 | static int rv8803_read_reg(const struct i2c_client *client, u8 reg) |
| 70 | { |
| 71 | int try = RV8803_I2C_TRY_COUNT; |
| 72 | s32 ret; |
| 73 | |
| 74 | /* |
| 75 | * There is a 61µs window during which the RTC does not acknowledge I2C |
| 76 | * transfers. In that case, ensure that there are multiple attempts. |
| 77 | */ |
| 78 | do |
| 79 | ret = i2c_smbus_read_byte_data(client, reg); |
| 80 | while ((ret == -ENXIO || ret == -EIO) && --try); |
| 81 | if (ret < 0) |
| 82 | dev_err(&client->dev, "Unable to read register 0x%02x\n", reg); |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | static int rv8803_read_regs(const struct i2c_client *client, |
| 88 | u8 reg, u8 count, u8 *values) |
| 89 | { |
| 90 | int try = RV8803_I2C_TRY_COUNT; |
| 91 | s32 ret; |
| 92 | |
| 93 | do |
| 94 | ret = i2c_smbus_read_i2c_block_data(client, reg, count, values); |
| 95 | while ((ret == -ENXIO || ret == -EIO) && --try); |
| 96 | if (ret != count) { |
| 97 | dev_err(&client->dev, |
| 98 | "Unable to read registers 0x%02x..0x%02x\n", |
| 99 | reg, reg + count - 1); |
| 100 | return ret < 0 ? ret : -EIO; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int rv8803_write_reg(const struct i2c_client *client, u8 reg, u8 value) |
| 107 | { |
| 108 | int try = RV8803_I2C_TRY_COUNT; |
| 109 | s32 ret; |
| 110 | |
| 111 | do |
| 112 | ret = i2c_smbus_write_byte_data(client, reg, value); |
| 113 | while ((ret == -ENXIO || ret == -EIO) && --try); |
| 114 | if (ret) |
| 115 | dev_err(&client->dev, "Unable to write register 0x%02x\n", reg); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int rv8803_write_regs(const struct i2c_client *client, |
| 121 | u8 reg, u8 count, const u8 *values) |
| 122 | { |
| 123 | int try = RV8803_I2C_TRY_COUNT; |
| 124 | s32 ret; |
| 125 | |
| 126 | do |
| 127 | ret = i2c_smbus_write_i2c_block_data(client, reg, count, |
| 128 | values); |
| 129 | while ((ret == -ENXIO || ret == -EIO) && --try); |
| 130 | if (ret) |
| 131 | dev_err(&client->dev, |
| 132 | "Unable to write registers 0x%02x..0x%02x\n", |
| 133 | reg, reg + count - 1); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 138 | static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) |
| 139 | { |
| 140 | struct i2c_client *client = dev_id; |
| 141 | struct rv8803_data *rv8803 = i2c_get_clientdata(client); |
| 142 | unsigned long events = 0; |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 143 | int flags; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 144 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 145 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 146 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 147 | flags = rv8803_read_reg(client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 148 | if (flags <= 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 149 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 150 | return IRQ_NONE; |
| 151 | } |
| 152 | |
| 153 | if (flags & RV8803_FLAG_V1F) |
| 154 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); |
| 155 | |
| 156 | if (flags & RV8803_FLAG_V2F) |
| 157 | dev_warn(&client->dev, "Voltage low, data loss detected.\n"); |
| 158 | |
| 159 | if (flags & RV8803_FLAG_TF) { |
| 160 | flags &= ~RV8803_FLAG_TF; |
| 161 | rv8803->ctrl &= ~RV8803_CTRL_TIE; |
| 162 | events |= RTC_PF; |
| 163 | } |
| 164 | |
| 165 | if (flags & RV8803_FLAG_AF) { |
| 166 | flags &= ~RV8803_FLAG_AF; |
| 167 | rv8803->ctrl &= ~RV8803_CTRL_AIE; |
| 168 | events |= RTC_AF; |
| 169 | } |
| 170 | |
| 171 | if (flags & RV8803_FLAG_UF) { |
| 172 | flags &= ~RV8803_FLAG_UF; |
| 173 | rv8803->ctrl &= ~RV8803_CTRL_UIE; |
| 174 | events |= RTC_UF; |
| 175 | } |
| 176 | |
| 177 | if (events) { |
| 178 | rtc_update_irq(rv8803->rtc, 1, events); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 179 | rv8803_write_reg(client, RV8803_FLAG, flags); |
| 180 | rv8803_write_reg(rv8803->client, RV8803_CTRL, rv8803->ctrl); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 183 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 184 | |
| 185 | return IRQ_HANDLED; |
| 186 | } |
| 187 | |
| 188 | static int rv8803_get_time(struct device *dev, struct rtc_time *tm) |
| 189 | { |
| 190 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 191 | u8 date1[7]; |
| 192 | u8 date2[7]; |
| 193 | u8 *date = date1; |
| 194 | int ret, flags; |
| 195 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 196 | flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 197 | if (flags < 0) |
| 198 | return flags; |
| 199 | |
| 200 | if (flags & RV8803_FLAG_V2F) { |
| 201 | dev_warn(dev, "Voltage low, data is invalid.\n"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 205 | ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date); |
| 206 | if (ret) |
| 207 | return ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 208 | |
| 209 | if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 210 | ret = rv8803_read_regs(rv8803->client, RV8803_SEC, 7, date2); |
| 211 | if (ret) |
| 212 | return ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 213 | |
| 214 | if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) |
| 215 | date = date2; |
| 216 | } |
| 217 | |
| 218 | tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); |
| 219 | tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); |
| 220 | tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); |
Benoît Thébaudeau | a1e98e0 | 2016-07-21 12:41:29 +0200 | [diff] [blame] | 221 | tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 222 | tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); |
| 223 | tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; |
| 224 | tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; |
| 225 | |
Benoît Thébaudeau | 96acb25 | 2016-07-21 12:41:28 +0200 | [diff] [blame] | 226 | return 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static int rv8803_set_time(struct device *dev, struct rtc_time *tm) |
| 230 | { |
| 231 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 232 | u8 date[7]; |
Benoît Thébaudeau | d3700b6 | 2016-07-21 12:41:31 +0200 | [diff] [blame] | 233 | int ctrl, flags, ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 234 | |
Benoît Thébaudeau | d3700b6 | 2016-07-21 12:41:31 +0200 | [diff] [blame] | 235 | ctrl = rv8803_read_reg(rv8803->client, RV8803_CTRL); |
| 236 | if (ctrl < 0) |
| 237 | return ctrl; |
| 238 | |
| 239 | /* Stop the clock */ |
| 240 | ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, |
| 241 | ctrl | RV8803_CTRL_RESET); |
| 242 | if (ret) |
| 243 | return ret; |
| 244 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 245 | date[RV8803_SEC] = bin2bcd(tm->tm_sec); |
| 246 | date[RV8803_MIN] = bin2bcd(tm->tm_min); |
| 247 | date[RV8803_HOUR] = bin2bcd(tm->tm_hour); |
| 248 | date[RV8803_WEEK] = 1 << (tm->tm_wday); |
| 249 | date[RV8803_DAY] = bin2bcd(tm->tm_mday); |
| 250 | date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); |
| 251 | date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); |
| 252 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 253 | ret = rv8803_write_regs(rv8803->client, RV8803_SEC, 7, date); |
| 254 | if (ret) |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 255 | return ret; |
| 256 | |
Benoît Thébaudeau | d3700b6 | 2016-07-21 12:41:31 +0200 | [diff] [blame] | 257 | /* Restart the clock */ |
| 258 | ret = rv8803_write_reg(rv8803->client, RV8803_CTRL, |
| 259 | ctrl & ~RV8803_CTRL_RESET); |
| 260 | if (ret) |
| 261 | return ret; |
| 262 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 263 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 264 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 265 | flags = rv8803_read_reg(rv8803->client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 266 | if (flags < 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 267 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 268 | return flags; |
| 269 | } |
| 270 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 271 | ret = rv8803_write_reg(rv8803->client, RV8803_FLAG, |
Benoît Thébaudeau | 6f36778 | 2016-07-21 12:41:32 +0200 | [diff] [blame] | 272 | flags & ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F)); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 273 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 274 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 280 | { |
| 281 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 282 | struct i2c_client *client = rv8803->client; |
| 283 | u8 alarmvals[3]; |
| 284 | int flags, ret; |
| 285 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 286 | ret = rv8803_read_regs(client, RV8803_ALARM_MIN, 3, alarmvals); |
| 287 | if (ret) |
| 288 | return ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 289 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 290 | flags = rv8803_read_reg(client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 291 | if (flags < 0) |
| 292 | return flags; |
| 293 | |
| 294 | alrm->time.tm_sec = 0; |
| 295 | alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); |
| 296 | alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 297 | alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 298 | |
| 299 | alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); |
| 300 | alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 306 | { |
| 307 | struct i2c_client *client = to_i2c_client(dev); |
| 308 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 309 | u8 alarmvals[3]; |
| 310 | u8 ctrl[2]; |
| 311 | int ret, err; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 312 | |
| 313 | /* The alarm has no seconds, round up to nearest minute */ |
| 314 | if (alrm->time.tm_sec) { |
| 315 | time64_t alarm_time = rtc_tm_to_time64(&alrm->time); |
| 316 | |
| 317 | alarm_time += 60 - alrm->time.tm_sec; |
| 318 | rtc_time64_to_tm(alarm_time, &alrm->time); |
| 319 | } |
| 320 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 321 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 322 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 323 | ret = rv8803_read_regs(client, RV8803_FLAG, 2, ctrl); |
| 324 | if (ret) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 325 | mutex_unlock(&rv8803->flags_lock); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 326 | return ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | alarmvals[0] = bin2bcd(alrm->time.tm_min); |
| 330 | alarmvals[1] = bin2bcd(alrm->time.tm_hour); |
| 331 | alarmvals[2] = bin2bcd(alrm->time.tm_mday); |
| 332 | |
| 333 | if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { |
| 334 | rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 335 | err = rv8803_write_reg(rv8803->client, RV8803_CTRL, |
| 336 | rv8803->ctrl); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 337 | if (err) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 338 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 339 | return err; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | ctrl[1] &= ~RV8803_FLAG_AF; |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 344 | err = rv8803_write_reg(rv8803->client, RV8803_FLAG, ctrl[1]); |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 345 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 346 | if (err) |
| 347 | return err; |
| 348 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 349 | err = rv8803_write_regs(rv8803->client, RV8803_ALARM_MIN, 3, alarmvals); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 350 | if (err) |
| 351 | return err; |
| 352 | |
| 353 | if (alrm->enabled) { |
| 354 | if (rv8803->rtc->uie_rtctimer.enabled) |
| 355 | rv8803->ctrl |= RV8803_CTRL_UIE; |
| 356 | if (rv8803->rtc->aie_timer.enabled) |
| 357 | rv8803->ctrl |= RV8803_CTRL_AIE; |
| 358 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 359 | err = rv8803_write_reg(rv8803->client, RV8803_CTRL, |
| 360 | rv8803->ctrl); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 361 | if (err) |
| 362 | return err; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 369 | { |
| 370 | struct i2c_client *client = to_i2c_client(dev); |
| 371 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 372 | int ctrl, flags, err; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 373 | |
| 374 | ctrl = rv8803->ctrl; |
| 375 | |
| 376 | if (enabled) { |
| 377 | if (rv8803->rtc->uie_rtctimer.enabled) |
| 378 | ctrl |= RV8803_CTRL_UIE; |
| 379 | if (rv8803->rtc->aie_timer.enabled) |
| 380 | ctrl |= RV8803_CTRL_AIE; |
| 381 | } else { |
| 382 | if (!rv8803->rtc->uie_rtctimer.enabled) |
| 383 | ctrl &= ~RV8803_CTRL_UIE; |
| 384 | if (!rv8803->rtc->aie_timer.enabled) |
| 385 | ctrl &= ~RV8803_CTRL_AIE; |
| 386 | } |
| 387 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 388 | mutex_lock(&rv8803->flags_lock); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 389 | flags = rv8803_read_reg(client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 390 | if (flags < 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 391 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 392 | return flags; |
| 393 | } |
| 394 | flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 395 | err = rv8803_write_reg(client, RV8803_FLAG, flags); |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 396 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 397 | if (err) |
| 398 | return err; |
| 399 | |
| 400 | if (ctrl != rv8803->ctrl) { |
| 401 | rv8803->ctrl = ctrl; |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 402 | err = rv8803_write_reg(client, RV8803_CTRL, rv8803->ctrl); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 403 | if (err) |
| 404 | return err; |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 411 | { |
| 412 | struct i2c_client *client = to_i2c_client(dev); |
| 413 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
Alexandre Belloni | bcd17c5 | 2019-12-14 23:02:57 +0100 | [diff] [blame] | 414 | unsigned int vl = 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 415 | int flags, ret = 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 416 | |
| 417 | switch (cmd) { |
| 418 | case RTC_VL_READ: |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 419 | flags = rv8803_read_reg(client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 420 | if (flags < 0) |
| 421 | return flags; |
| 422 | |
Alexandre Belloni | bcd17c5 | 2019-12-14 23:02:57 +0100 | [diff] [blame] | 423 | if (flags & RV8803_FLAG_V1F) { |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 424 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); |
Alexandre Belloni | bcd17c5 | 2019-12-14 23:02:57 +0100 | [diff] [blame] | 425 | vl = RTC_VL_ACCURACY_LOW; |
| 426 | } |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 427 | |
| 428 | if (flags & RV8803_FLAG_V2F) |
Alexandre Belloni | bcd17c5 | 2019-12-14 23:02:57 +0100 | [diff] [blame] | 429 | vl |= RTC_VL_DATA_INVALID; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 430 | |
Alexandre Belloni | bcd17c5 | 2019-12-14 23:02:57 +0100 | [diff] [blame] | 431 | return put_user(vl, (unsigned int __user *)arg); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 432 | |
| 433 | case RTC_VL_CLR: |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 434 | mutex_lock(&rv8803->flags_lock); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 435 | flags = rv8803_read_reg(client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 436 | if (flags < 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 437 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 438 | return flags; |
| 439 | } |
| 440 | |
Alexandre Belloni | 7e890a0 | 2019-12-14 23:02:56 +0100 | [diff] [blame] | 441 | flags &= ~RV8803_FLAG_V1F; |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 442 | ret = rv8803_write_reg(client, RV8803_FLAG, flags); |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 443 | mutex_unlock(&rv8803->flags_lock); |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 444 | if (ret) |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 445 | return ret; |
| 446 | |
| 447 | return 0; |
| 448 | |
| 449 | default: |
| 450 | return -ENOIOCTLCMD; |
| 451 | } |
| 452 | } |
| 453 | |
Alexandre Belloni | 16d70a7 | 2017-07-06 11:42:04 +0200 | [diff] [blame] | 454 | static int rv8803_nvram_write(void *priv, unsigned int offset, void *val, |
| 455 | size_t bytes) |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 456 | { |
Liu Shixin | 179b4bc | 2020-09-21 16:24:49 +0800 | [diff] [blame] | 457 | return rv8803_write_reg(priv, RV8803_RAM, *(u8 *)val); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Alexandre Belloni | 16d70a7 | 2017-07-06 11:42:04 +0200 | [diff] [blame] | 460 | static int rv8803_nvram_read(void *priv, unsigned int offset, |
| 461 | void *val, size_t bytes) |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 462 | { |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 463 | int ret; |
| 464 | |
Alexandre Belloni | 16d70a7 | 2017-07-06 11:42:04 +0200 | [diff] [blame] | 465 | ret = rv8803_read_reg(priv, RV8803_RAM); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 466 | if (ret < 0) |
| 467 | return ret; |
| 468 | |
Alexandre Belloni | 16d70a7 | 2017-07-06 11:42:04 +0200 | [diff] [blame] | 469 | *(u8 *)val = ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 470 | |
Alexandre Belloni | 16d70a7 | 2017-07-06 11:42:04 +0200 | [diff] [blame] | 471 | return 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 474 | static struct rtc_class_ops rv8803_rtc_ops = { |
| 475 | .read_time = rv8803_get_time, |
| 476 | .set_time = rv8803_set_time, |
| 477 | .ioctl = rv8803_ioctl, |
| 478 | }; |
| 479 | |
Oleksij Rempel | 1cd7137 | 2016-06-29 16:40:01 +0200 | [diff] [blame] | 480 | static int rx8900_trickle_charger_init(struct rv8803_data *rv8803) |
| 481 | { |
| 482 | struct i2c_client *client = rv8803->client; |
| 483 | struct device_node *node = client->dev.of_node; |
| 484 | int err; |
| 485 | u8 flags; |
| 486 | |
| 487 | if (!node) |
| 488 | return 0; |
| 489 | |
| 490 | if (rv8803->type != rx_8900) |
| 491 | return 0; |
| 492 | |
| 493 | err = i2c_smbus_read_byte_data(rv8803->client, RX8900_BACKUP_CTRL); |
| 494 | if (err < 0) |
| 495 | return err; |
| 496 | |
| 497 | flags = ~(RX8900_FLAG_VDETOFF | RX8900_FLAG_SWOFF) & (u8)err; |
| 498 | |
| 499 | if (of_property_read_bool(node, "epson,vdet-disable")) |
| 500 | flags |= RX8900_FLAG_VDETOFF; |
| 501 | |
| 502 | if (of_property_read_bool(node, "trickle-diode-disable")) |
| 503 | flags |= RX8900_FLAG_SWOFF; |
| 504 | |
| 505 | return i2c_smbus_write_byte_data(rv8803->client, RX8900_BACKUP_CTRL, |
| 506 | flags); |
| 507 | } |
| 508 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 509 | static int rv8803_probe(struct i2c_client *client, |
| 510 | const struct i2c_device_id *id) |
| 511 | { |
Wolfram Sang | 5cb1726 | 2019-06-08 12:56:07 +0200 | [diff] [blame] | 512 | struct i2c_adapter *adapter = client->adapter; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 513 | struct rv8803_data *rv8803; |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 514 | int err, flags; |
Alexandre Belloni | c07fd9de | 2018-02-12 23:47:32 +0100 | [diff] [blame] | 515 | struct nvmem_config nvmem_cfg = { |
| 516 | .name = "rv8803_nvram", |
| 517 | .word_size = 1, |
| 518 | .stride = 1, |
| 519 | .size = 1, |
| 520 | .reg_read = rv8803_nvram_read, |
| 521 | .reg_write = rv8803_nvram_write, |
| 522 | .priv = client, |
| 523 | }; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 524 | |
| 525 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 526 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 527 | dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); |
| 528 | return -EIO; |
| 529 | } |
| 530 | |
| 531 | rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), |
| 532 | GFP_KERNEL); |
| 533 | if (!rv8803) |
| 534 | return -ENOMEM; |
| 535 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 536 | mutex_init(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 537 | rv8803->client = client; |
Javier Martinez Canillas | 740ad8f | 2017-03-03 11:29:12 -0300 | [diff] [blame] | 538 | if (client->dev.of_node) |
| 539 | rv8803->type = (enum rv8803_type) |
| 540 | of_device_get_match_data(&client->dev); |
| 541 | else |
| 542 | rv8803->type = id->driver_data; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 543 | i2c_set_clientdata(client, rv8803); |
| 544 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 545 | flags = rv8803_read_reg(client, RV8803_FLAG); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 546 | if (flags < 0) |
| 547 | return flags; |
| 548 | |
| 549 | if (flags & RV8803_FLAG_V1F) |
| 550 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); |
| 551 | |
| 552 | if (flags & RV8803_FLAG_V2F) |
| 553 | dev_warn(&client->dev, "Voltage low, data loss detected.\n"); |
| 554 | |
| 555 | if (flags & RV8803_FLAG_AF) |
| 556 | dev_warn(&client->dev, "An alarm maybe have been missed.\n"); |
| 557 | |
Alexandre Belloni | 7133eca | 2017-07-06 11:42:03 +0200 | [diff] [blame] | 558 | rv8803->rtc = devm_rtc_allocate_device(&client->dev); |
Alexandre Belloni | 44c638c | 2019-08-19 00:00:41 +0200 | [diff] [blame] | 559 | if (IS_ERR(rv8803->rtc)) |
Alexandre Belloni | 7133eca | 2017-07-06 11:42:03 +0200 | [diff] [blame] | 560 | return PTR_ERR(rv8803->rtc); |
Alexandre Belloni | 7133eca | 2017-07-06 11:42:03 +0200 | [diff] [blame] | 561 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 562 | if (client->irq > 0) { |
| 563 | err = devm_request_threaded_irq(&client->dev, client->irq, |
| 564 | NULL, rv8803_handle_irq, |
| 565 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 566 | "rv8803", client); |
| 567 | if (err) { |
| 568 | dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); |
| 569 | client->irq = 0; |
| 570 | } else { |
| 571 | rv8803_rtc_ops.read_alarm = rv8803_get_alarm; |
| 572 | rv8803_rtc_ops.set_alarm = rv8803_set_alarm; |
| 573 | rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; |
| 574 | } |
| 575 | } |
| 576 | |
Benoît Thébaudeau | d522649 | 2016-07-21 12:41:30 +0200 | [diff] [blame] | 577 | err = rv8803_write_reg(rv8803->client, RV8803_EXT, RV8803_EXT_WADA); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 578 | if (err) |
| 579 | return err; |
| 580 | |
Oleksij Rempel | 1cd7137 | 2016-06-29 16:40:01 +0200 | [diff] [blame] | 581 | err = rx8900_trickle_charger_init(rv8803); |
| 582 | if (err) { |
| 583 | dev_err(&client->dev, "failed to init charger\n"); |
| 584 | return err; |
| 585 | } |
| 586 | |
Alexandre Belloni | ce1ae8e | 2018-02-12 23:47:33 +0100 | [diff] [blame] | 587 | rv8803->rtc->ops = &rv8803_rtc_ops; |
Alexandre Belloni | 2e17f8b | 2019-03-04 11:03:46 +0100 | [diff] [blame] | 588 | rv8803->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
| 589 | rv8803->rtc->range_max = RTC_TIMESTAMP_END_2099; |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame^] | 590 | err = devm_rtc_register_device(rv8803->rtc); |
Alexandre Belloni | ce1ae8e | 2018-02-12 23:47:33 +0100 | [diff] [blame] | 591 | if (err) |
| 592 | return err; |
| 593 | |
Bartosz Golaszewski | 3a905c2d | 2020-11-09 17:34:06 +0100 | [diff] [blame] | 594 | devm_rtc_nvmem_register(rv8803->rtc, &nvmem_cfg); |
Alexandre Belloni | ce1ae8e | 2018-02-12 23:47:33 +0100 | [diff] [blame] | 595 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 596 | rv8803->rtc->max_user_freq = 1; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 601 | static const struct i2c_device_id rv8803_id[] = { |
Oleksij Rempel | 1cd7137 | 2016-06-29 16:40:01 +0200 | [diff] [blame] | 602 | { "rv8803", rv_8803 }, |
Alexandre Belloni | ac771ed | 2018-08-27 23:23:44 +0200 | [diff] [blame] | 603 | { "rx8803", rv_8803 }, |
Oleksij Rempel | 1cd7137 | 2016-06-29 16:40:01 +0200 | [diff] [blame] | 604 | { "rx8900", rx_8900 }, |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 605 | { } |
| 606 | }; |
| 607 | MODULE_DEVICE_TABLE(i2c, rv8803_id); |
| 608 | |
Javier Martinez Canillas | 740ad8f | 2017-03-03 11:29:12 -0300 | [diff] [blame] | 609 | static const struct of_device_id rv8803_of_match[] = { |
| 610 | { |
| 611 | .compatible = "microcrystal,rv8803", |
Alexandre Belloni | c856618 | 2018-08-27 23:23:43 +0200 | [diff] [blame] | 612 | .data = (void *)rv_8803 |
Javier Martinez Canillas | 740ad8f | 2017-03-03 11:29:12 -0300 | [diff] [blame] | 613 | }, |
| 614 | { |
Alexandre Belloni | ac771ed | 2018-08-27 23:23:44 +0200 | [diff] [blame] | 615 | .compatible = "epson,rx8803", |
| 616 | .data = (void *)rv_8803 |
| 617 | }, |
| 618 | { |
Javier Martinez Canillas | 740ad8f | 2017-03-03 11:29:12 -0300 | [diff] [blame] | 619 | .compatible = "epson,rx8900", |
| 620 | .data = (void *)rx_8900 |
| 621 | }, |
| 622 | { } |
| 623 | }; |
| 624 | MODULE_DEVICE_TABLE(of, rv8803_of_match); |
| 625 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 626 | static struct i2c_driver rv8803_driver = { |
| 627 | .driver = { |
| 628 | .name = "rtc-rv8803", |
Javier Martinez Canillas | 740ad8f | 2017-03-03 11:29:12 -0300 | [diff] [blame] | 629 | .of_match_table = of_match_ptr(rv8803_of_match), |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 630 | }, |
| 631 | .probe = rv8803_probe, |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 632 | .id_table = rv8803_id, |
| 633 | }; |
| 634 | module_i2c_driver(rv8803_driver); |
| 635 | |
Alexandre Belloni | 7d1e5bf | 2019-03-04 20:17:38 +0100 | [diff] [blame] | 636 | MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 637 | MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); |
| 638 | MODULE_LICENSE("GPL v2"); |