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