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> |
| 21 | #include <linux/rtc.h> |
| 22 | |
| 23 | #define RV8803_SEC 0x00 |
| 24 | #define RV8803_MIN 0x01 |
| 25 | #define RV8803_HOUR 0x02 |
| 26 | #define RV8803_WEEK 0x03 |
| 27 | #define RV8803_DAY 0x04 |
| 28 | #define RV8803_MONTH 0x05 |
| 29 | #define RV8803_YEAR 0x06 |
| 30 | #define RV8803_RAM 0x07 |
| 31 | #define RV8803_ALARM_MIN 0x08 |
| 32 | #define RV8803_ALARM_HOUR 0x09 |
| 33 | #define RV8803_ALARM_WEEK_OR_DAY 0x0A |
| 34 | #define RV8803_EXT 0x0D |
| 35 | #define RV8803_FLAG 0x0E |
| 36 | #define RV8803_CTRL 0x0F |
| 37 | |
| 38 | #define RV8803_EXT_WADA BIT(6) |
| 39 | |
| 40 | #define RV8803_FLAG_V1F BIT(0) |
| 41 | #define RV8803_FLAG_V2F BIT(1) |
| 42 | #define RV8803_FLAG_AF BIT(3) |
| 43 | #define RV8803_FLAG_TF BIT(4) |
| 44 | #define RV8803_FLAG_UF BIT(5) |
| 45 | |
| 46 | #define RV8803_CTRL_RESET BIT(0) |
| 47 | |
| 48 | #define RV8803_CTRL_EIE BIT(2) |
| 49 | #define RV8803_CTRL_AIE BIT(3) |
| 50 | #define RV8803_CTRL_TIE BIT(4) |
| 51 | #define RV8803_CTRL_UIE BIT(5) |
| 52 | |
| 53 | struct rv8803_data { |
| 54 | struct i2c_client *client; |
| 55 | struct rtc_device *rtc; |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 56 | struct mutex flags_lock; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 57 | u8 ctrl; |
| 58 | }; |
| 59 | |
| 60 | static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) |
| 61 | { |
| 62 | struct i2c_client *client = dev_id; |
| 63 | struct rv8803_data *rv8803 = i2c_get_clientdata(client); |
| 64 | unsigned long events = 0; |
Alexandre Belloni | 85062c9 | 2016-03-21 15:58:38 +0100 | [diff] [blame] | 65 | int flags, try = 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 66 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 67 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 68 | |
Alexandre Belloni | 85062c9 | 2016-03-21 15:58:38 +0100 | [diff] [blame] | 69 | do { |
| 70 | flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); |
| 71 | try++; |
Alexandre Belloni | cde0fe2 | 2016-06-07 20:44:05 +0200 | [diff] [blame] | 72 | } while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4)); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 73 | if (flags <= 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 74 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 75 | return IRQ_NONE; |
| 76 | } |
| 77 | |
| 78 | if (flags & RV8803_FLAG_V1F) |
| 79 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); |
| 80 | |
| 81 | if (flags & RV8803_FLAG_V2F) |
| 82 | dev_warn(&client->dev, "Voltage low, data loss detected.\n"); |
| 83 | |
| 84 | if (flags & RV8803_FLAG_TF) { |
| 85 | flags &= ~RV8803_FLAG_TF; |
| 86 | rv8803->ctrl &= ~RV8803_CTRL_TIE; |
| 87 | events |= RTC_PF; |
| 88 | } |
| 89 | |
| 90 | if (flags & RV8803_FLAG_AF) { |
| 91 | flags &= ~RV8803_FLAG_AF; |
| 92 | rv8803->ctrl &= ~RV8803_CTRL_AIE; |
| 93 | events |= RTC_AF; |
| 94 | } |
| 95 | |
| 96 | if (flags & RV8803_FLAG_UF) { |
| 97 | flags &= ~RV8803_FLAG_UF; |
| 98 | rv8803->ctrl &= ~RV8803_CTRL_UIE; |
| 99 | events |= RTC_UF; |
| 100 | } |
| 101 | |
| 102 | if (events) { |
| 103 | rtc_update_irq(rv8803->rtc, 1, events); |
| 104 | i2c_smbus_write_byte_data(client, RV8803_FLAG, flags); |
| 105 | i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL, |
| 106 | rv8803->ctrl); |
| 107 | } |
| 108 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 109 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 110 | |
| 111 | return IRQ_HANDLED; |
| 112 | } |
| 113 | |
| 114 | static int rv8803_get_time(struct device *dev, struct rtc_time *tm) |
| 115 | { |
| 116 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 117 | u8 date1[7]; |
| 118 | u8 date2[7]; |
| 119 | u8 *date = date1; |
| 120 | int ret, flags; |
| 121 | |
| 122 | flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG); |
| 123 | if (flags < 0) |
| 124 | return flags; |
| 125 | |
| 126 | if (flags & RV8803_FLAG_V2F) { |
| 127 | dev_warn(dev, "Voltage low, data is invalid.\n"); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC, |
| 132 | 7, date); |
| 133 | if (ret != 7) |
| 134 | return ret < 0 ? ret : -EIO; |
| 135 | |
| 136 | if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { |
| 137 | ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC, |
| 138 | 7, date2); |
| 139 | if (ret != 7) |
| 140 | return ret < 0 ? ret : -EIO; |
| 141 | |
| 142 | if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) |
| 143 | date = date2; |
| 144 | } |
| 145 | |
| 146 | tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); |
| 147 | tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); |
| 148 | tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); |
Benoît Thébaudeau | a1e98e0 | 2016-07-21 12:41:29 +0200 | [diff] [blame^] | 149 | tm->tm_wday = ilog2(date[RV8803_WEEK] & 0x7f); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 150 | tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); |
| 151 | tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; |
| 152 | tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; |
| 153 | |
Benoît Thébaudeau | 96acb25 | 2016-07-21 12:41:28 +0200 | [diff] [blame] | 154 | return 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static int rv8803_set_time(struct device *dev, struct rtc_time *tm) |
| 158 | { |
| 159 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 160 | u8 date[7]; |
| 161 | int flags, ret; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 162 | |
| 163 | if ((tm->tm_year < 100) || (tm->tm_year > 199)) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | date[RV8803_SEC] = bin2bcd(tm->tm_sec); |
| 167 | date[RV8803_MIN] = bin2bcd(tm->tm_min); |
| 168 | date[RV8803_HOUR] = bin2bcd(tm->tm_hour); |
| 169 | date[RV8803_WEEK] = 1 << (tm->tm_wday); |
| 170 | date[RV8803_DAY] = bin2bcd(tm->tm_mday); |
| 171 | date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); |
| 172 | date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); |
| 173 | |
| 174 | ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC, |
| 175 | 7, date); |
| 176 | if (ret < 0) |
| 177 | return ret; |
| 178 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 179 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 180 | |
| 181 | flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG); |
| 182 | if (flags < 0) { |
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 | return flags; |
| 185 | } |
| 186 | |
| 187 | ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, |
| 188 | flags & ~RV8803_FLAG_V2F); |
| 189 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 190 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 191 | |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 196 | { |
| 197 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 198 | struct i2c_client *client = rv8803->client; |
| 199 | u8 alarmvals[3]; |
| 200 | int flags, ret; |
| 201 | |
| 202 | ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN, |
| 203 | 3, alarmvals); |
| 204 | if (ret != 3) |
| 205 | return ret < 0 ? ret : -EIO; |
| 206 | |
| 207 | flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); |
| 208 | if (flags < 0) |
| 209 | return flags; |
| 210 | |
| 211 | alrm->time.tm_sec = 0; |
| 212 | alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); |
| 213 | alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 214 | alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 215 | |
| 216 | alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); |
| 217 | alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 223 | { |
| 224 | struct i2c_client *client = to_i2c_client(dev); |
| 225 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 226 | u8 alarmvals[3]; |
| 227 | u8 ctrl[2]; |
| 228 | int ret, err; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 229 | |
| 230 | /* The alarm has no seconds, round up to nearest minute */ |
| 231 | if (alrm->time.tm_sec) { |
| 232 | time64_t alarm_time = rtc_tm_to_time64(&alrm->time); |
| 233 | |
| 234 | alarm_time += 60 - alrm->time.tm_sec; |
| 235 | rtc_time64_to_tm(alarm_time, &alrm->time); |
| 236 | } |
| 237 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 238 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 239 | |
| 240 | ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl); |
| 241 | if (ret != 2) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 242 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 243 | return ret < 0 ? ret : -EIO; |
| 244 | } |
| 245 | |
| 246 | alarmvals[0] = bin2bcd(alrm->time.tm_min); |
| 247 | alarmvals[1] = bin2bcd(alrm->time.tm_hour); |
| 248 | alarmvals[2] = bin2bcd(alrm->time.tm_mday); |
| 249 | |
| 250 | if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { |
| 251 | rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); |
| 252 | err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL, |
| 253 | rv8803->ctrl); |
| 254 | if (err) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 255 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 256 | return err; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | ctrl[1] &= ~RV8803_FLAG_AF; |
| 261 | err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]); |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 262 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 263 | if (err) |
| 264 | return err; |
| 265 | |
| 266 | err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN, |
| 267 | 3, alarmvals); |
| 268 | if (err) |
| 269 | return err; |
| 270 | |
| 271 | if (alrm->enabled) { |
| 272 | if (rv8803->rtc->uie_rtctimer.enabled) |
| 273 | rv8803->ctrl |= RV8803_CTRL_UIE; |
| 274 | if (rv8803->rtc->aie_timer.enabled) |
| 275 | rv8803->ctrl |= RV8803_CTRL_AIE; |
| 276 | |
| 277 | err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL, |
| 278 | rv8803->ctrl); |
| 279 | if (err) |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 287 | { |
| 288 | struct i2c_client *client = to_i2c_client(dev); |
| 289 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 290 | int ctrl, flags, err; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 291 | |
| 292 | ctrl = rv8803->ctrl; |
| 293 | |
| 294 | if (enabled) { |
| 295 | if (rv8803->rtc->uie_rtctimer.enabled) |
| 296 | ctrl |= RV8803_CTRL_UIE; |
| 297 | if (rv8803->rtc->aie_timer.enabled) |
| 298 | ctrl |= RV8803_CTRL_AIE; |
| 299 | } else { |
| 300 | if (!rv8803->rtc->uie_rtctimer.enabled) |
| 301 | ctrl &= ~RV8803_CTRL_UIE; |
| 302 | if (!rv8803->rtc->aie_timer.enabled) |
| 303 | ctrl &= ~RV8803_CTRL_AIE; |
| 304 | } |
| 305 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 306 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 307 | flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); |
| 308 | if (flags < 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 309 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 310 | return flags; |
| 311 | } |
| 312 | flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); |
| 313 | err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags); |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 314 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 315 | if (err) |
| 316 | return err; |
| 317 | |
| 318 | if (ctrl != rv8803->ctrl) { |
| 319 | rv8803->ctrl = ctrl; |
| 320 | err = i2c_smbus_write_byte_data(client, RV8803_CTRL, |
| 321 | rv8803->ctrl); |
| 322 | if (err) |
| 323 | return err; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 330 | { |
| 331 | struct i2c_client *client = to_i2c_client(dev); |
| 332 | struct rv8803_data *rv8803 = dev_get_drvdata(dev); |
| 333 | int flags, ret = 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 334 | |
| 335 | switch (cmd) { |
| 336 | case RTC_VL_READ: |
| 337 | flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); |
| 338 | if (flags < 0) |
| 339 | return flags; |
| 340 | |
| 341 | if (flags & RV8803_FLAG_V1F) |
| 342 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); |
| 343 | |
| 344 | if (flags & RV8803_FLAG_V2F) |
| 345 | dev_warn(&client->dev, "Voltage low, data loss detected.\n"); |
| 346 | |
| 347 | flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F; |
| 348 | |
| 349 | if (copy_to_user((void __user *)arg, &flags, sizeof(int))) |
| 350 | return -EFAULT; |
| 351 | |
| 352 | return 0; |
| 353 | |
| 354 | case RTC_VL_CLR: |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 355 | mutex_lock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 356 | flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); |
| 357 | if (flags < 0) { |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 358 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 359 | return flags; |
| 360 | } |
| 361 | |
| 362 | flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F); |
| 363 | ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags); |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 364 | mutex_unlock(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 365 | if (ret < 0) |
| 366 | return ret; |
| 367 | |
| 368 | return 0; |
| 369 | |
| 370 | default: |
| 371 | return -ENOIOCTLCMD; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj, |
| 376 | struct bin_attribute *attr, |
| 377 | char *buf, loff_t off, size_t count) |
| 378 | { |
| 379 | struct device *dev = kobj_to_dev(kobj); |
| 380 | struct i2c_client *client = to_i2c_client(dev); |
| 381 | int ret; |
| 382 | |
| 383 | ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]); |
| 384 | if (ret < 0) |
| 385 | return ret; |
| 386 | |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj, |
| 391 | struct bin_attribute *attr, |
| 392 | char *buf, loff_t off, size_t count) |
| 393 | { |
| 394 | struct device *dev = kobj_to_dev(kobj); |
| 395 | struct i2c_client *client = to_i2c_client(dev); |
| 396 | int ret; |
| 397 | |
| 398 | ret = i2c_smbus_read_byte_data(client, RV8803_RAM); |
| 399 | if (ret < 0) |
| 400 | return ret; |
| 401 | |
| 402 | buf[0] = ret; |
| 403 | |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | static struct bin_attribute rv8803_nvram_attr = { |
| 408 | .attr = { |
| 409 | .name = "nvram", |
| 410 | .mode = S_IRUGO | S_IWUSR, |
| 411 | }, |
| 412 | .size = 1, |
| 413 | .read = rv8803_nvram_read, |
| 414 | .write = rv8803_nvram_write, |
| 415 | }; |
| 416 | |
| 417 | static struct rtc_class_ops rv8803_rtc_ops = { |
| 418 | .read_time = rv8803_get_time, |
| 419 | .set_time = rv8803_set_time, |
| 420 | .ioctl = rv8803_ioctl, |
| 421 | }; |
| 422 | |
| 423 | static int rv8803_probe(struct i2c_client *client, |
| 424 | const struct i2c_device_id *id) |
| 425 | { |
| 426 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); |
| 427 | struct rv8803_data *rv8803; |
Alexandre Belloni | 85062c9 | 2016-03-21 15:58:38 +0100 | [diff] [blame] | 428 | int err, flags, try = 0; |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 429 | |
| 430 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 431 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 432 | dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); |
| 433 | return -EIO; |
| 434 | } |
| 435 | |
| 436 | rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), |
| 437 | GFP_KERNEL); |
| 438 | if (!rv8803) |
| 439 | return -ENOMEM; |
| 440 | |
Oleksij Rempel | 9d1fa4c | 2016-02-04 13:45:20 +0100 | [diff] [blame] | 441 | mutex_init(&rv8803->flags_lock); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 442 | rv8803->client = client; |
| 443 | i2c_set_clientdata(client, rv8803); |
| 444 | |
Alexandre Belloni | 85062c9 | 2016-03-21 15:58:38 +0100 | [diff] [blame] | 445 | /* |
| 446 | * There is a 60µs window where the RTC may not reply on the i2c bus in |
| 447 | * that case, the transfer is not ACKed. In that case, ensure there are |
| 448 | * multiple attempts. |
| 449 | */ |
| 450 | do { |
| 451 | flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); |
| 452 | try++; |
Alexandre Belloni | cde0fe2 | 2016-06-07 20:44:05 +0200 | [diff] [blame] | 453 | } while (((flags == -ENXIO) || (flags == -EIO)) && (try < 4)); |
Alexandre Belloni | 85062c9 | 2016-03-21 15:58:38 +0100 | [diff] [blame] | 454 | |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 455 | if (flags < 0) |
| 456 | return flags; |
| 457 | |
| 458 | if (flags & RV8803_FLAG_V1F) |
| 459 | dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); |
| 460 | |
| 461 | if (flags & RV8803_FLAG_V2F) |
| 462 | dev_warn(&client->dev, "Voltage low, data loss detected.\n"); |
| 463 | |
| 464 | if (flags & RV8803_FLAG_AF) |
| 465 | dev_warn(&client->dev, "An alarm maybe have been missed.\n"); |
| 466 | |
| 467 | if (client->irq > 0) { |
| 468 | err = devm_request_threaded_irq(&client->dev, client->irq, |
| 469 | NULL, rv8803_handle_irq, |
| 470 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 471 | "rv8803", client); |
| 472 | if (err) { |
| 473 | dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); |
| 474 | client->irq = 0; |
| 475 | } else { |
| 476 | rv8803_rtc_ops.read_alarm = rv8803_get_alarm; |
| 477 | rv8803_rtc_ops.set_alarm = rv8803_set_alarm; |
| 478 | rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | rv8803->rtc = devm_rtc_device_register(&client->dev, client->name, |
| 483 | &rv8803_rtc_ops, THIS_MODULE); |
| 484 | if (IS_ERR(rv8803->rtc)) { |
| 485 | dev_err(&client->dev, "unable to register the class device\n"); |
| 486 | return PTR_ERR(rv8803->rtc); |
| 487 | } |
| 488 | |
Alexandre Belloni | 85062c9 | 2016-03-21 15:58:38 +0100 | [diff] [blame] | 489 | try = 0; |
| 490 | do { |
| 491 | err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT, |
| 492 | RV8803_EXT_WADA); |
| 493 | try++; |
Alexandre Belloni | cde0fe2 | 2016-06-07 20:44:05 +0200 | [diff] [blame] | 494 | } while (((err == -ENXIO) || (flags == -EIO)) && (try < 4)); |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 495 | if (err) |
| 496 | return err; |
| 497 | |
| 498 | err = device_create_bin_file(&client->dev, &rv8803_nvram_attr); |
| 499 | if (err) |
| 500 | return err; |
| 501 | |
| 502 | rv8803->rtc->max_user_freq = 1; |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int rv8803_remove(struct i2c_client *client) |
| 508 | { |
| 509 | device_remove_bin_file(&client->dev, &rv8803_nvram_attr); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static const struct i2c_device_id rv8803_id[] = { |
| 515 | { "rv8803", 0 }, |
Gregory CLEMENT | 78ef5f2 | 2015-12-11 12:43:05 +0100 | [diff] [blame] | 516 | { "rx8900", 0 }, |
Alexandre Belloni | 1e3929e | 2015-11-02 23:48:32 +0100 | [diff] [blame] | 517 | { } |
| 518 | }; |
| 519 | MODULE_DEVICE_TABLE(i2c, rv8803_id); |
| 520 | |
| 521 | static struct i2c_driver rv8803_driver = { |
| 522 | .driver = { |
| 523 | .name = "rtc-rv8803", |
| 524 | }, |
| 525 | .probe = rv8803_probe, |
| 526 | .remove = rv8803_remove, |
| 527 | .id_table = rv8803_id, |
| 528 | }; |
| 529 | module_i2c_driver(rv8803_driver); |
| 530 | |
| 531 | MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); |
| 532 | MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); |
| 533 | MODULE_LICENSE("GPL v2"); |