Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * A driver for the I2C members of the Abracon AB x8xx RTC family, |
| 3 | * and compatible: AB 1805 and AB 0805 |
| 4 | * |
| 5 | * Copyright 2014-2015 Macq S.A. |
| 6 | * |
| 7 | * Author: Philippe De Muyter <phdm@macqel.be> |
| 8 | * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/bcd.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/rtc.h> |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 20 | #include <linux/watchdog.h> |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 21 | |
| 22 | #define ABX8XX_REG_HTH 0x00 |
| 23 | #define ABX8XX_REG_SC 0x01 |
| 24 | #define ABX8XX_REG_MN 0x02 |
| 25 | #define ABX8XX_REG_HR 0x03 |
| 26 | #define ABX8XX_REG_DA 0x04 |
| 27 | #define ABX8XX_REG_MO 0x05 |
| 28 | #define ABX8XX_REG_YR 0x06 |
| 29 | #define ABX8XX_REG_WD 0x07 |
| 30 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 31 | #define ABX8XX_REG_AHTH 0x08 |
| 32 | #define ABX8XX_REG_ASC 0x09 |
| 33 | #define ABX8XX_REG_AMN 0x0a |
| 34 | #define ABX8XX_REG_AHR 0x0b |
| 35 | #define ABX8XX_REG_ADA 0x0c |
| 36 | #define ABX8XX_REG_AMO 0x0d |
| 37 | #define ABX8XX_REG_AWD 0x0e |
| 38 | |
| 39 | #define ABX8XX_REG_STATUS 0x0f |
| 40 | #define ABX8XX_STATUS_AF BIT(2) |
Marek Vasut | ffe1c5a | 2018-12-07 18:40:53 +0100 | [diff] [blame^] | 41 | #define ABX8XX_STATUS_BLF BIT(4) |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 42 | #define ABX8XX_STATUS_WDT BIT(6) |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 43 | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 44 | #define ABX8XX_REG_CTRL1 0x10 |
Mitja Spes | 5f1b2f7 | 2015-09-02 10:02:29 +0200 | [diff] [blame] | 45 | #define ABX8XX_CTRL_WRITE BIT(0) |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 46 | #define ABX8XX_CTRL_ARST BIT(2) |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 47 | #define ABX8XX_CTRL_12_24 BIT(6) |
| 48 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 49 | #define ABX8XX_REG_IRQ 0x12 |
| 50 | #define ABX8XX_IRQ_AIE BIT(2) |
| 51 | #define ABX8XX_IRQ_IM_1_4 (0x3 << 5) |
| 52 | |
| 53 | #define ABX8XX_REG_CD_TIMER_CTL 0x18 |
| 54 | |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 55 | #define ABX8XX_REG_OSC 0x1c |
| 56 | #define ABX8XX_OSC_FOS BIT(3) |
| 57 | #define ABX8XX_OSC_BOS BIT(4) |
| 58 | #define ABX8XX_OSC_ACAL_512 BIT(5) |
| 59 | #define ABX8XX_OSC_ACAL_1024 BIT(6) |
| 60 | |
| 61 | #define ABX8XX_OSC_OSEL BIT(7) |
| 62 | |
| 63 | #define ABX8XX_REG_OSS 0x1d |
Mylène Josserand | ee08774 | 2016-03-21 18:06:10 +0100 | [diff] [blame] | 64 | #define ABX8XX_OSS_OF BIT(1) |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 65 | #define ABX8XX_OSS_OMODE BIT(4) |
| 66 | |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 67 | #define ABX8XX_REG_WDT 0x1b |
| 68 | #define ABX8XX_WDT_WDS BIT(7) |
| 69 | #define ABX8XX_WDT_BMB_MASK 0x7c |
| 70 | #define ABX8XX_WDT_BMB_SHIFT 2 |
| 71 | #define ABX8XX_WDT_MAX_TIME (ABX8XX_WDT_BMB_MASK >> ABX8XX_WDT_BMB_SHIFT) |
| 72 | #define ABX8XX_WDT_WRB_MASK 0x03 |
| 73 | #define ABX8XX_WDT_WRB_1HZ 0x02 |
| 74 | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 75 | #define ABX8XX_REG_CFG_KEY 0x1f |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 76 | #define ABX8XX_CFG_KEY_OSC 0xa1 |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 77 | #define ABX8XX_CFG_KEY_MISC 0x9d |
| 78 | |
| 79 | #define ABX8XX_REG_ID0 0x28 |
| 80 | |
| 81 | #define ABX8XX_REG_TRICKLE 0x20 |
| 82 | #define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0 |
| 83 | #define ABX8XX_TRICKLE_STANDARD_DIODE 0x8 |
| 84 | #define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4 |
| 85 | |
| 86 | static u8 trickle_resistors[] = {0, 3, 6, 11}; |
| 87 | |
| 88 | enum abx80x_chip {AB0801, AB0803, AB0804, AB0805, |
| 89 | AB1801, AB1803, AB1804, AB1805, ABX80X}; |
| 90 | |
| 91 | struct abx80x_cap { |
| 92 | u16 pn; |
| 93 | bool has_tc; |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 94 | bool has_wdog; |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static struct abx80x_cap abx80x_caps[] = { |
| 98 | [AB0801] = {.pn = 0x0801}, |
| 99 | [AB0803] = {.pn = 0x0803}, |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 100 | [AB0804] = {.pn = 0x0804, .has_tc = true, .has_wdog = true}, |
| 101 | [AB0805] = {.pn = 0x0805, .has_tc = true, .has_wdog = true}, |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 102 | [AB1801] = {.pn = 0x1801}, |
| 103 | [AB1803] = {.pn = 0x1803}, |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 104 | [AB1804] = {.pn = 0x1804, .has_tc = true, .has_wdog = true}, |
| 105 | [AB1805] = {.pn = 0x1805, .has_tc = true, .has_wdog = true}, |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 106 | [ABX80X] = {.pn = 0} |
| 107 | }; |
| 108 | |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 109 | struct abx80x_priv { |
| 110 | struct rtc_device *rtc; |
| 111 | struct i2c_client *client; |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 112 | struct watchdog_device wdog; |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 113 | }; |
| 114 | |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 115 | static int abx80x_is_rc_mode(struct i2c_client *client) |
| 116 | { |
| 117 | int flags = 0; |
| 118 | |
| 119 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); |
| 120 | if (flags < 0) { |
| 121 | dev_err(&client->dev, |
| 122 | "Failed to read autocalibration attribute\n"); |
| 123 | return flags; |
| 124 | } |
| 125 | |
| 126 | return (flags & ABX8XX_OSS_OMODE) ? 1 : 0; |
| 127 | } |
| 128 | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 129 | static int abx80x_enable_trickle_charger(struct i2c_client *client, |
| 130 | u8 trickle_cfg) |
| 131 | { |
| 132 | int err; |
| 133 | |
| 134 | /* |
| 135 | * Write the configuration key register to enable access to the Trickle |
| 136 | * register |
| 137 | */ |
| 138 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, |
| 139 | ABX8XX_CFG_KEY_MISC); |
| 140 | if (err < 0) { |
| 141 | dev_err(&client->dev, "Unable to write configuration key\n"); |
| 142 | return -EIO; |
| 143 | } |
| 144 | |
| 145 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE, |
| 146 | ABX8XX_TRICKLE_CHARGE_ENABLE | |
| 147 | trickle_cfg); |
| 148 | if (err < 0) { |
| 149 | dev_err(&client->dev, "Unable to write trickle register\n"); |
| 150 | return -EIO; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 157 | { |
| 158 | struct i2c_client *client = to_i2c_client(dev); |
| 159 | unsigned char buf[8]; |
Mylène Josserand | ee08774 | 2016-03-21 18:06:10 +0100 | [diff] [blame] | 160 | int err, flags, rc_mode = 0; |
| 161 | |
| 162 | /* Read the Oscillator Failure only in XT mode */ |
| 163 | rc_mode = abx80x_is_rc_mode(client); |
| 164 | if (rc_mode < 0) |
| 165 | return rc_mode; |
| 166 | |
| 167 | if (!rc_mode) { |
| 168 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); |
| 169 | if (flags < 0) |
| 170 | return flags; |
| 171 | |
| 172 | if (flags & ABX8XX_OSS_OF) { |
| 173 | dev_err(dev, "Oscillator failure, data is invalid.\n"); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | } |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 177 | |
| 178 | err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH, |
| 179 | sizeof(buf), buf); |
| 180 | if (err < 0) { |
| 181 | dev_err(&client->dev, "Unable to read date\n"); |
| 182 | return -EIO; |
| 183 | } |
| 184 | |
| 185 | tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F); |
| 186 | tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F); |
| 187 | tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F); |
| 188 | tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7; |
| 189 | tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F); |
| 190 | tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1; |
| 191 | tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100; |
| 192 | |
Alexandre Belloni | fbfd36f | 2018-02-20 23:42:27 +0100 | [diff] [blame] | 193 | return 0; |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 197 | { |
| 198 | struct i2c_client *client = to_i2c_client(dev); |
| 199 | unsigned char buf[8]; |
Mylène Josserand | ee08774 | 2016-03-21 18:06:10 +0100 | [diff] [blame] | 200 | int err, flags; |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 201 | |
| 202 | if (tm->tm_year < 100) |
| 203 | return -EINVAL; |
| 204 | |
| 205 | buf[ABX8XX_REG_HTH] = 0; |
| 206 | buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec); |
| 207 | buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min); |
| 208 | buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour); |
| 209 | buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday); |
| 210 | buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1); |
| 211 | buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100); |
| 212 | buf[ABX8XX_REG_WD] = tm->tm_wday; |
| 213 | |
| 214 | err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH, |
| 215 | sizeof(buf), buf); |
| 216 | if (err < 0) { |
| 217 | dev_err(&client->dev, "Unable to write to date registers\n"); |
| 218 | return -EIO; |
| 219 | } |
| 220 | |
Mylène Josserand | ee08774 | 2016-03-21 18:06:10 +0100 | [diff] [blame] | 221 | /* Clear the OF bit of Oscillator Status Register */ |
| 222 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSS); |
| 223 | if (flags < 0) |
| 224 | return flags; |
| 225 | |
| 226 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSS, |
| 227 | flags & ~ABX8XX_OSS_OF); |
| 228 | if (err < 0) { |
| 229 | dev_err(&client->dev, "Unable to write oscillator status register\n"); |
| 230 | return err; |
| 231 | } |
| 232 | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 236 | static irqreturn_t abx80x_handle_irq(int irq, void *dev_id) |
| 237 | { |
| 238 | struct i2c_client *client = dev_id; |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 239 | struct abx80x_priv *priv = i2c_get_clientdata(client); |
| 240 | struct rtc_device *rtc = priv->rtc; |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 241 | int status; |
| 242 | |
| 243 | status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); |
| 244 | if (status < 0) |
| 245 | return IRQ_NONE; |
| 246 | |
| 247 | if (status & ABX8XX_STATUS_AF) |
| 248 | rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF); |
| 249 | |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 250 | /* |
| 251 | * It is unclear if we'll get an interrupt before the external |
| 252 | * reset kicks in. |
| 253 | */ |
| 254 | if (status & ABX8XX_STATUS_WDT) |
| 255 | dev_alert(&client->dev, "watchdog timeout interrupt.\n"); |
| 256 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 257 | i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); |
| 258 | |
| 259 | return IRQ_HANDLED; |
| 260 | } |
| 261 | |
| 262 | static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 263 | { |
| 264 | struct i2c_client *client = to_i2c_client(dev); |
| 265 | unsigned char buf[7]; |
| 266 | |
| 267 | int irq_mask, err; |
| 268 | |
| 269 | if (client->irq <= 0) |
| 270 | return -EINVAL; |
| 271 | |
| 272 | err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC, |
| 273 | sizeof(buf), buf); |
| 274 | if (err) |
| 275 | return err; |
| 276 | |
| 277 | irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ); |
| 278 | if (irq_mask < 0) |
| 279 | return irq_mask; |
| 280 | |
| 281 | t->time.tm_sec = bcd2bin(buf[0] & 0x7F); |
| 282 | t->time.tm_min = bcd2bin(buf[1] & 0x7F); |
| 283 | t->time.tm_hour = bcd2bin(buf[2] & 0x3F); |
| 284 | t->time.tm_mday = bcd2bin(buf[3] & 0x3F); |
| 285 | t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1; |
| 286 | t->time.tm_wday = buf[5] & 0x7; |
| 287 | |
| 288 | t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE); |
| 289 | t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled; |
| 290 | |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
| 295 | { |
| 296 | struct i2c_client *client = to_i2c_client(dev); |
| 297 | u8 alarm[6]; |
| 298 | int err; |
| 299 | |
| 300 | if (client->irq <= 0) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | alarm[0] = 0x0; |
| 304 | alarm[1] = bin2bcd(t->time.tm_sec); |
| 305 | alarm[2] = bin2bcd(t->time.tm_min); |
| 306 | alarm[3] = bin2bcd(t->time.tm_hour); |
| 307 | alarm[4] = bin2bcd(t->time.tm_mday); |
| 308 | alarm[5] = bin2bcd(t->time.tm_mon + 1); |
| 309 | |
| 310 | err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH, |
| 311 | sizeof(alarm), alarm); |
| 312 | if (err < 0) { |
| 313 | dev_err(&client->dev, "Unable to write alarm registers\n"); |
| 314 | return -EIO; |
| 315 | } |
| 316 | |
| 317 | if (t->enabled) { |
| 318 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, |
| 319 | (ABX8XX_IRQ_IM_1_4 | |
| 320 | ABX8XX_IRQ_AIE)); |
| 321 | if (err) |
| 322 | return err; |
| 323 | } |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 328 | static int abx80x_rtc_set_autocalibration(struct device *dev, |
| 329 | int autocalibration) |
| 330 | { |
| 331 | struct i2c_client *client = to_i2c_client(dev); |
| 332 | int retval, flags = 0; |
| 333 | |
| 334 | if ((autocalibration != 0) && (autocalibration != 1024) && |
| 335 | (autocalibration != 512)) { |
| 336 | dev_err(dev, "autocalibration value outside permitted range\n"); |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); |
| 341 | if (flags < 0) |
| 342 | return flags; |
| 343 | |
| 344 | if (autocalibration == 0) { |
| 345 | flags &= ~(ABX8XX_OSC_ACAL_512 | ABX8XX_OSC_ACAL_1024); |
| 346 | } else if (autocalibration == 1024) { |
| 347 | /* 1024 autocalibration is 0x10 */ |
| 348 | flags |= ABX8XX_OSC_ACAL_1024; |
| 349 | flags &= ~(ABX8XX_OSC_ACAL_512); |
| 350 | } else { |
| 351 | /* 512 autocalibration is 0x11 */ |
| 352 | flags |= (ABX8XX_OSC_ACAL_1024 | ABX8XX_OSC_ACAL_512); |
| 353 | } |
| 354 | |
| 355 | /* Unlock write access to Oscillator Control Register */ |
| 356 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, |
| 357 | ABX8XX_CFG_KEY_OSC); |
| 358 | if (retval < 0) { |
| 359 | dev_err(dev, "Failed to write CONFIG_KEY register\n"); |
| 360 | return retval; |
| 361 | } |
| 362 | |
| 363 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); |
| 364 | |
| 365 | return retval; |
| 366 | } |
| 367 | |
| 368 | static int abx80x_rtc_get_autocalibration(struct device *dev) |
| 369 | { |
| 370 | struct i2c_client *client = to_i2c_client(dev); |
| 371 | int flags = 0, autocalibration; |
| 372 | |
| 373 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); |
| 374 | if (flags < 0) |
| 375 | return flags; |
| 376 | |
| 377 | if (flags & ABX8XX_OSC_ACAL_512) |
| 378 | autocalibration = 512; |
| 379 | else if (flags & ABX8XX_OSC_ACAL_1024) |
| 380 | autocalibration = 1024; |
| 381 | else |
| 382 | autocalibration = 0; |
| 383 | |
| 384 | return autocalibration; |
| 385 | } |
| 386 | |
| 387 | static ssize_t autocalibration_store(struct device *dev, |
| 388 | struct device_attribute *attr, |
| 389 | const char *buf, size_t count) |
| 390 | { |
| 391 | int retval; |
| 392 | unsigned long autocalibration = 0; |
| 393 | |
| 394 | retval = kstrtoul(buf, 10, &autocalibration); |
| 395 | if (retval < 0) { |
| 396 | dev_err(dev, "Failed to store RTC autocalibration attribute\n"); |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | retval = abx80x_rtc_set_autocalibration(dev, autocalibration); |
| 401 | |
| 402 | return retval ? retval : count; |
| 403 | } |
| 404 | |
| 405 | static ssize_t autocalibration_show(struct device *dev, |
| 406 | struct device_attribute *attr, char *buf) |
| 407 | { |
| 408 | int autocalibration = 0; |
| 409 | |
| 410 | autocalibration = abx80x_rtc_get_autocalibration(dev); |
| 411 | if (autocalibration < 0) { |
| 412 | dev_err(dev, "Failed to read RTC autocalibration\n"); |
| 413 | sprintf(buf, "0\n"); |
| 414 | return autocalibration; |
| 415 | } |
| 416 | |
| 417 | return sprintf(buf, "%d\n", autocalibration); |
| 418 | } |
| 419 | |
| 420 | static DEVICE_ATTR_RW(autocalibration); |
| 421 | |
| 422 | static ssize_t oscillator_store(struct device *dev, |
| 423 | struct device_attribute *attr, |
| 424 | const char *buf, size_t count) |
| 425 | { |
| 426 | struct i2c_client *client = to_i2c_client(dev); |
| 427 | int retval, flags, rc_mode = 0; |
| 428 | |
| 429 | if (strncmp(buf, "rc", 2) == 0) { |
| 430 | rc_mode = 1; |
| 431 | } else if (strncmp(buf, "xtal", 4) == 0) { |
| 432 | rc_mode = 0; |
| 433 | } else { |
| 434 | dev_err(dev, "Oscillator selection value outside permitted ones\n"); |
| 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | flags = i2c_smbus_read_byte_data(client, ABX8XX_REG_OSC); |
| 439 | if (flags < 0) |
| 440 | return flags; |
| 441 | |
| 442 | if (rc_mode == 0) |
| 443 | flags &= ~(ABX8XX_OSC_OSEL); |
| 444 | else |
| 445 | flags |= (ABX8XX_OSC_OSEL); |
| 446 | |
| 447 | /* Unlock write access on Oscillator Control register */ |
| 448 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY, |
| 449 | ABX8XX_CFG_KEY_OSC); |
| 450 | if (retval < 0) { |
| 451 | dev_err(dev, "Failed to write CONFIG_KEY register\n"); |
| 452 | return retval; |
| 453 | } |
| 454 | |
| 455 | retval = i2c_smbus_write_byte_data(client, ABX8XX_REG_OSC, flags); |
| 456 | if (retval < 0) { |
| 457 | dev_err(dev, "Failed to write Oscillator Control register\n"); |
| 458 | return retval; |
| 459 | } |
| 460 | |
| 461 | return retval ? retval : count; |
| 462 | } |
| 463 | |
| 464 | static ssize_t oscillator_show(struct device *dev, |
| 465 | struct device_attribute *attr, char *buf) |
| 466 | { |
| 467 | int rc_mode = 0; |
| 468 | struct i2c_client *client = to_i2c_client(dev); |
| 469 | |
| 470 | rc_mode = abx80x_is_rc_mode(client); |
| 471 | |
| 472 | if (rc_mode < 0) { |
| 473 | dev_err(dev, "Failed to read RTC oscillator selection\n"); |
| 474 | sprintf(buf, "\n"); |
| 475 | return rc_mode; |
| 476 | } |
| 477 | |
| 478 | if (rc_mode) |
| 479 | return sprintf(buf, "rc\n"); |
| 480 | else |
| 481 | return sprintf(buf, "xtal\n"); |
| 482 | } |
| 483 | |
| 484 | static DEVICE_ATTR_RW(oscillator); |
| 485 | |
| 486 | static struct attribute *rtc_calib_attrs[] = { |
| 487 | &dev_attr_autocalibration.attr, |
| 488 | &dev_attr_oscillator.attr, |
| 489 | NULL, |
| 490 | }; |
| 491 | |
| 492 | static const struct attribute_group rtc_calib_attr_group = { |
| 493 | .attrs = rtc_calib_attrs, |
| 494 | }; |
| 495 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 496 | static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 497 | { |
| 498 | struct i2c_client *client = to_i2c_client(dev); |
| 499 | int err; |
| 500 | |
| 501 | if (enabled) |
| 502 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, |
| 503 | (ABX8XX_IRQ_IM_1_4 | |
| 504 | ABX8XX_IRQ_AIE)); |
| 505 | else |
| 506 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ, |
| 507 | ABX8XX_IRQ_IM_1_4); |
| 508 | return err; |
| 509 | } |
| 510 | |
Marek Vasut | ffe1c5a | 2018-12-07 18:40:53 +0100 | [diff] [blame^] | 511 | static int abx80x_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) |
| 512 | { |
| 513 | struct i2c_client *client = to_i2c_client(dev); |
| 514 | int status, tmp; |
| 515 | |
| 516 | switch (cmd) { |
| 517 | case RTC_VL_READ: |
| 518 | status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); |
| 519 | if (status < 0) |
| 520 | return status; |
| 521 | |
| 522 | tmp = !!(status & ABX8XX_STATUS_BLF); |
| 523 | |
| 524 | if (copy_to_user((void __user *)arg, &tmp, sizeof(int))) |
| 525 | return -EFAULT; |
| 526 | |
| 527 | return 0; |
| 528 | |
| 529 | case RTC_VL_CLR: |
| 530 | status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS); |
| 531 | if (status < 0) |
| 532 | return status; |
| 533 | |
| 534 | status &= ~ABX8XX_STATUS_BLF; |
| 535 | |
| 536 | tmp = i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0); |
| 537 | if (tmp < 0) |
| 538 | return tmp; |
| 539 | |
| 540 | return 0; |
| 541 | |
| 542 | default: |
| 543 | return -ENOIOCTLCMD; |
| 544 | } |
| 545 | } |
| 546 | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 547 | static const struct rtc_class_ops abx80x_rtc_ops = { |
| 548 | .read_time = abx80x_rtc_read_time, |
| 549 | .set_time = abx80x_rtc_set_time, |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 550 | .read_alarm = abx80x_read_alarm, |
| 551 | .set_alarm = abx80x_set_alarm, |
| 552 | .alarm_irq_enable = abx80x_alarm_irq_enable, |
Marek Vasut | ffe1c5a | 2018-12-07 18:40:53 +0100 | [diff] [blame^] | 553 | .ioctl = abx80x_ioctl, |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 554 | }; |
| 555 | |
| 556 | static int abx80x_dt_trickle_cfg(struct device_node *np) |
| 557 | { |
| 558 | const char *diode; |
| 559 | int trickle_cfg = 0; |
| 560 | int i, ret; |
| 561 | u32 tmp; |
| 562 | |
| 563 | ret = of_property_read_string(np, "abracon,tc-diode", &diode); |
| 564 | if (ret) |
| 565 | return ret; |
| 566 | |
| 567 | if (!strcmp(diode, "standard")) |
| 568 | trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE; |
| 569 | else if (!strcmp(diode, "schottky")) |
| 570 | trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE; |
| 571 | else |
| 572 | return -EINVAL; |
| 573 | |
| 574 | ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp); |
| 575 | if (ret) |
| 576 | return ret; |
| 577 | |
| 578 | for (i = 0; i < sizeof(trickle_resistors); i++) |
| 579 | if (trickle_resistors[i] == tmp) |
| 580 | break; |
| 581 | |
| 582 | if (i == sizeof(trickle_resistors)) |
| 583 | return -EINVAL; |
| 584 | |
| 585 | return (trickle_cfg | i); |
| 586 | } |
| 587 | |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 588 | static void rtc_calib_remove_sysfs_group(void *_dev) |
| 589 | { |
| 590 | struct device *dev = _dev; |
| 591 | |
| 592 | sysfs_remove_group(&dev->kobj, &rtc_calib_attr_group); |
| 593 | } |
| 594 | |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 595 | #ifdef CONFIG_WATCHDOG |
| 596 | |
| 597 | static inline u8 timeout_bits(unsigned int timeout) |
| 598 | { |
| 599 | return ((timeout << ABX8XX_WDT_BMB_SHIFT) & ABX8XX_WDT_BMB_MASK) | |
| 600 | ABX8XX_WDT_WRB_1HZ; |
| 601 | } |
| 602 | |
| 603 | static int __abx80x_wdog_set_timeout(struct watchdog_device *wdog, |
| 604 | unsigned int timeout) |
| 605 | { |
| 606 | struct abx80x_priv *priv = watchdog_get_drvdata(wdog); |
| 607 | u8 val = ABX8XX_WDT_WDS | timeout_bits(timeout); |
| 608 | |
| 609 | /* |
| 610 | * Writing any timeout to the WDT register resets the watchdog timer. |
| 611 | * Writing 0 disables it. |
| 612 | */ |
| 613 | return i2c_smbus_write_byte_data(priv->client, ABX8XX_REG_WDT, val); |
| 614 | } |
| 615 | |
| 616 | static int abx80x_wdog_set_timeout(struct watchdog_device *wdog, |
| 617 | unsigned int new_timeout) |
| 618 | { |
| 619 | int err = 0; |
| 620 | |
| 621 | if (watchdog_hw_running(wdog)) |
| 622 | err = __abx80x_wdog_set_timeout(wdog, new_timeout); |
| 623 | |
| 624 | if (err == 0) |
| 625 | wdog->timeout = new_timeout; |
| 626 | |
| 627 | return err; |
| 628 | } |
| 629 | |
| 630 | static int abx80x_wdog_ping(struct watchdog_device *wdog) |
| 631 | { |
| 632 | return __abx80x_wdog_set_timeout(wdog, wdog->timeout); |
| 633 | } |
| 634 | |
| 635 | static int abx80x_wdog_start(struct watchdog_device *wdog) |
| 636 | { |
| 637 | return __abx80x_wdog_set_timeout(wdog, wdog->timeout); |
| 638 | } |
| 639 | |
| 640 | static int abx80x_wdog_stop(struct watchdog_device *wdog) |
| 641 | { |
| 642 | return __abx80x_wdog_set_timeout(wdog, 0); |
| 643 | } |
| 644 | |
| 645 | static const struct watchdog_info abx80x_wdog_info = { |
| 646 | .identity = "abx80x watchdog", |
| 647 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, |
| 648 | }; |
| 649 | |
| 650 | static const struct watchdog_ops abx80x_wdog_ops = { |
| 651 | .owner = THIS_MODULE, |
| 652 | .start = abx80x_wdog_start, |
| 653 | .stop = abx80x_wdog_stop, |
| 654 | .ping = abx80x_wdog_ping, |
| 655 | .set_timeout = abx80x_wdog_set_timeout, |
| 656 | }; |
| 657 | |
| 658 | static int abx80x_setup_watchdog(struct abx80x_priv *priv) |
| 659 | { |
| 660 | priv->wdog.parent = &priv->client->dev; |
| 661 | priv->wdog.ops = &abx80x_wdog_ops; |
| 662 | priv->wdog.info = &abx80x_wdog_info; |
| 663 | priv->wdog.min_timeout = 1; |
| 664 | priv->wdog.max_timeout = ABX8XX_WDT_MAX_TIME; |
| 665 | priv->wdog.timeout = ABX8XX_WDT_MAX_TIME; |
| 666 | |
| 667 | watchdog_set_drvdata(&priv->wdog, priv); |
| 668 | |
| 669 | return devm_watchdog_register_device(&priv->client->dev, &priv->wdog); |
| 670 | } |
| 671 | #else |
| 672 | static int abx80x_setup_watchdog(struct abx80x_priv *priv) |
| 673 | { |
| 674 | return 0; |
| 675 | } |
| 676 | #endif |
| 677 | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 678 | static int abx80x_probe(struct i2c_client *client, |
| 679 | const struct i2c_device_id *id) |
| 680 | { |
| 681 | struct device_node *np = client->dev.of_node; |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 682 | struct abx80x_priv *priv; |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 683 | int i, data, err, trickle_cfg = -EINVAL; |
| 684 | char buf[7]; |
| 685 | unsigned int part = id->driver_data; |
| 686 | unsigned int partnumber; |
| 687 | unsigned int majrev, minrev; |
| 688 | unsigned int lot; |
| 689 | unsigned int wafer; |
| 690 | unsigned int uid; |
| 691 | |
| 692 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 693 | return -ENODEV; |
| 694 | |
| 695 | err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0, |
| 696 | sizeof(buf), buf); |
| 697 | if (err < 0) { |
| 698 | dev_err(&client->dev, "Unable to read partnumber\n"); |
| 699 | return -EIO; |
| 700 | } |
| 701 | |
| 702 | partnumber = (buf[0] << 8) | buf[1]; |
| 703 | majrev = buf[2] >> 3; |
| 704 | minrev = buf[2] & 0x7; |
| 705 | lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3]; |
| 706 | uid = ((buf[4] & 0x7f) << 8) | buf[5]; |
| 707 | wafer = (buf[6] & 0x7c) >> 2; |
| 708 | dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n", |
| 709 | partnumber, majrev, minrev, lot, wafer, uid); |
| 710 | |
| 711 | data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1); |
| 712 | if (data < 0) { |
| 713 | dev_err(&client->dev, "Unable to read control register\n"); |
| 714 | return -EIO; |
| 715 | } |
| 716 | |
| 717 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1, |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 718 | ((data & ~(ABX8XX_CTRL_12_24 | |
| 719 | ABX8XX_CTRL_ARST)) | |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 720 | ABX8XX_CTRL_WRITE)); |
| 721 | if (err < 0) { |
| 722 | dev_err(&client->dev, "Unable to write control register\n"); |
| 723 | return -EIO; |
| 724 | } |
| 725 | |
| 726 | /* part autodetection */ |
| 727 | if (part == ABX80X) { |
| 728 | for (i = 0; abx80x_caps[i].pn; i++) |
| 729 | if (partnumber == abx80x_caps[i].pn) |
| 730 | break; |
| 731 | if (abx80x_caps[i].pn == 0) { |
| 732 | dev_err(&client->dev, "Unknown part: %04x\n", |
| 733 | partnumber); |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | part = i; |
| 737 | } |
| 738 | |
| 739 | if (partnumber != abx80x_caps[part].pn) { |
| 740 | dev_err(&client->dev, "partnumber mismatch %04x != %04x\n", |
| 741 | partnumber, abx80x_caps[part].pn); |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | |
| 745 | if (np && abx80x_caps[part].has_tc) |
| 746 | trickle_cfg = abx80x_dt_trickle_cfg(np); |
| 747 | |
| 748 | if (trickle_cfg > 0) { |
| 749 | dev_info(&client->dev, "Enabling trickle charger: %02x\n", |
| 750 | trickle_cfg); |
| 751 | abx80x_enable_trickle_charger(client, trickle_cfg); |
| 752 | } |
| 753 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 754 | err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL, |
| 755 | BIT(2)); |
| 756 | if (err) |
| 757 | return err; |
| 758 | |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 759 | priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); |
| 760 | if (priv == NULL) |
| 761 | return -ENOMEM; |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 762 | |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 763 | priv->rtc = devm_rtc_allocate_device(&client->dev); |
| 764 | if (IS_ERR(priv->rtc)) |
| 765 | return PTR_ERR(priv->rtc); |
Alexandre Belloni | 9360a6a | 2017-10-13 00:04:46 +0200 | [diff] [blame] | 766 | |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 767 | priv->rtc->ops = &abx80x_rtc_ops; |
| 768 | priv->client = client; |
| 769 | |
| 770 | i2c_set_clientdata(client, priv); |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 771 | |
Jeremy Gebben | 749e36d | 2018-09-11 11:28:26 -0600 | [diff] [blame] | 772 | if (abx80x_caps[part].has_wdog) { |
| 773 | err = abx80x_setup_watchdog(priv); |
| 774 | if (err) |
| 775 | return err; |
| 776 | } |
| 777 | |
Alexandre Belloni | 718a820 | 2015-12-17 00:36:22 +0100 | [diff] [blame] | 778 | if (client->irq > 0) { |
| 779 | dev_info(&client->dev, "IRQ %d supplied\n", client->irq); |
| 780 | err = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 781 | abx80x_handle_irq, |
| 782 | IRQF_SHARED | IRQF_ONESHOT, |
| 783 | "abx8xx", |
| 784 | client); |
| 785 | if (err) { |
| 786 | dev_err(&client->dev, "unable to request IRQ, alarms disabled\n"); |
| 787 | client->irq = 0; |
| 788 | } |
| 789 | } |
| 790 | |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 791 | /* Export sysfs entries */ |
| 792 | err = sysfs_create_group(&(&client->dev)->kobj, &rtc_calib_attr_group); |
| 793 | if (err) { |
| 794 | dev_err(&client->dev, "Failed to create sysfs group: %d\n", |
| 795 | err); |
| 796 | return err; |
| 797 | } |
| 798 | |
Sudip Mukherjee | d8cac8d | 2016-07-03 21:32:22 +0100 | [diff] [blame] | 799 | err = devm_add_action_or_reset(&client->dev, |
| 800 | rtc_calib_remove_sysfs_group, |
| 801 | &client->dev); |
Alexandre Belloni | 9da32ba | 2017-10-13 00:04:47 +0200 | [diff] [blame] | 802 | if (err) { |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 803 | dev_err(&client->dev, |
| 804 | "Failed to add sysfs cleanup action: %d\n", |
| 805 | err); |
Alexandre Belloni | 9da32ba | 2017-10-13 00:04:47 +0200 | [diff] [blame] | 806 | return err; |
| 807 | } |
| 808 | |
Jeremy Gebben | af69f9a | 2018-09-11 11:28:25 -0600 | [diff] [blame] | 809 | err = rtc_register_device(priv->rtc); |
Mylène Josserand | 59a8383 | 2016-03-21 18:06:09 +0100 | [diff] [blame] | 810 | |
Sudip Mukherjee | d8cac8d | 2016-07-03 21:32:22 +0100 | [diff] [blame] | 811 | return err; |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | static int abx80x_remove(struct i2c_client *client) |
| 815 | { |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static const struct i2c_device_id abx80x_id[] = { |
| 820 | { "abx80x", ABX80X }, |
| 821 | { "ab0801", AB0801 }, |
| 822 | { "ab0803", AB0803 }, |
| 823 | { "ab0804", AB0804 }, |
| 824 | { "ab0805", AB0805 }, |
| 825 | { "ab1801", AB1801 }, |
| 826 | { "ab1803", AB1803 }, |
| 827 | { "ab1804", AB1804 }, |
| 828 | { "ab1805", AB1805 }, |
Alexandre Belloni | fca733a | 2015-12-17 00:36:21 +0100 | [diff] [blame] | 829 | { "rv1805", AB1805 }, |
Philippe De Muyter | 4d61ff6 | 2015-05-05 16:23:44 -0700 | [diff] [blame] | 830 | { } |
| 831 | }; |
| 832 | MODULE_DEVICE_TABLE(i2c, abx80x_id); |
| 833 | |
| 834 | static struct i2c_driver abx80x_driver = { |
| 835 | .driver = { |
| 836 | .name = "rtc-abx80x", |
| 837 | }, |
| 838 | .probe = abx80x_probe, |
| 839 | .remove = abx80x_remove, |
| 840 | .id_table = abx80x_id, |
| 841 | }; |
| 842 | |
| 843 | module_i2c_driver(abx80x_driver); |
| 844 | |
| 845 | MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>"); |
| 846 | MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); |
| 847 | MODULE_DESCRIPTION("Abracon ABX80X RTC driver"); |
| 848 | MODULE_LICENSE("GPL v2"); |