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