Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 1 | /* |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 2 | * Micro Crystal RV-3029 rtc class driver |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 3 | * |
| 4 | * Author: Gregory Hermant <gregory.hermant@calao-systems.com> |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 5 | * Michael Buesch <m@bues.ch> |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 6 | * |
| 7 | * based on previously existing rtc class drivers |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/bcd.h> |
| 18 | #include <linux/rtc.h> |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 19 | #include <linux/delay.h> |
| 20 | #include <linux/of.h> |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 21 | #include <linux/hwmon.h> |
| 22 | #include <linux/hwmon-sysfs.h> |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 23 | |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 24 | |
| 25 | /* Register map */ |
| 26 | /* control section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 27 | #define RV3029_ONOFF_CTRL 0x00 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 28 | #define RV3029_ONOFF_CTRL_WE BIT(0) |
| 29 | #define RV3029_ONOFF_CTRL_TE BIT(1) |
| 30 | #define RV3029_ONOFF_CTRL_TAR BIT(2) |
| 31 | #define RV3029_ONOFF_CTRL_EERE BIT(3) |
| 32 | #define RV3029_ONOFF_CTRL_SRON BIT(4) |
| 33 | #define RV3029_ONOFF_CTRL_TD0 BIT(5) |
| 34 | #define RV3029_ONOFF_CTRL_TD1 BIT(6) |
| 35 | #define RV3029_ONOFF_CTRL_CLKINT BIT(7) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 36 | #define RV3029_IRQ_CTRL 0x01 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 37 | #define RV3029_IRQ_CTRL_AIE BIT(0) |
| 38 | #define RV3029_IRQ_CTRL_TIE BIT(1) |
| 39 | #define RV3029_IRQ_CTRL_V1IE BIT(2) |
| 40 | #define RV3029_IRQ_CTRL_V2IE BIT(3) |
| 41 | #define RV3029_IRQ_CTRL_SRIE BIT(4) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 42 | #define RV3029_IRQ_FLAGS 0x02 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 43 | #define RV3029_IRQ_FLAGS_AF BIT(0) |
| 44 | #define RV3029_IRQ_FLAGS_TF BIT(1) |
| 45 | #define RV3029_IRQ_FLAGS_V1IF BIT(2) |
| 46 | #define RV3029_IRQ_FLAGS_V2IF BIT(3) |
| 47 | #define RV3029_IRQ_FLAGS_SRF BIT(4) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 48 | #define RV3029_STATUS 0x03 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 49 | #define RV3029_STATUS_VLOW1 BIT(2) |
| 50 | #define RV3029_STATUS_VLOW2 BIT(3) |
| 51 | #define RV3029_STATUS_SR BIT(4) |
| 52 | #define RV3029_STATUS_PON BIT(5) |
| 53 | #define RV3029_STATUS_EEBUSY BIT(7) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 54 | #define RV3029_RST_CTRL 0x04 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 55 | #define RV3029_RST_CTRL_SYSR BIT(4) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 56 | #define RV3029_CONTROL_SECTION_LEN 0x05 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 57 | |
| 58 | /* watch section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 59 | #define RV3029_W_SEC 0x08 |
| 60 | #define RV3029_W_MINUTES 0x09 |
| 61 | #define RV3029_W_HOURS 0x0A |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 62 | #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */ |
| 63 | #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 64 | #define RV3029_W_DATE 0x0B |
| 65 | #define RV3029_W_DAYS 0x0C |
| 66 | #define RV3029_W_MONTHS 0x0D |
| 67 | #define RV3029_W_YEARS 0x0E |
| 68 | #define RV3029_WATCH_SECTION_LEN 0x07 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 69 | |
| 70 | /* alarm section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 71 | #define RV3029_A_SC 0x10 |
| 72 | #define RV3029_A_MN 0x11 |
| 73 | #define RV3029_A_HR 0x12 |
| 74 | #define RV3029_A_DT 0x13 |
| 75 | #define RV3029_A_DW 0x14 |
| 76 | #define RV3029_A_MO 0x15 |
| 77 | #define RV3029_A_YR 0x16 |
| 78 | #define RV3029_ALARM_SECTION_LEN 0x07 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 79 | |
| 80 | /* timer section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 81 | #define RV3029_TIMER_LOW 0x18 |
| 82 | #define RV3029_TIMER_HIGH 0x19 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 83 | |
| 84 | /* temperature section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 85 | #define RV3029_TEMP_PAGE 0x20 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 86 | |
| 87 | /* eeprom data section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 88 | #define RV3029_E2P_EEDATA1 0x28 |
| 89 | #define RV3029_E2P_EEDATA2 0x29 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 90 | #define RV3029_E2PDATA_SECTION_LEN 0x02 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 91 | |
| 92 | /* eeprom control section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 93 | #define RV3029_CONTROL_E2P_EECTRL 0x30 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 94 | #define RV3029_EECTRL_THP BIT(0) /* temp scan interval */ |
| 95 | #define RV3029_EECTRL_THE BIT(1) /* thermometer enable */ |
| 96 | #define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */ |
| 97 | #define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */ |
| 98 | #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */ |
| 99 | #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */ |
| 100 | #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */ |
| 101 | #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */ |
| 102 | #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\ |
| 103 | RV3029_TRICKLE_5K |\ |
| 104 | RV3029_TRICKLE_20K |\ |
| 105 | RV3029_TRICKLE_80K) |
| 106 | #define RV3029_TRICKLE_SHIFT 4 |
| 107 | #define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */ |
| 108 | #define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */ |
| 109 | #define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */ |
| 110 | #define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */ |
| 111 | #define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */ |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 112 | |
| 113 | /* user ram section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 114 | #define RV3029_USR1_RAM_PAGE 0x38 |
| 115 | #define RV3029_USR1_SECTION_LEN 0x04 |
| 116 | #define RV3029_USR2_RAM_PAGE 0x3C |
| 117 | #define RV3029_USR2_SECTION_LEN 0x04 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 118 | |
| 119 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 120 | rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf, |
| 121 | unsigned len) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 122 | { |
| 123 | int ret; |
| 124 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 125 | if ((reg > RV3029_USR1_RAM_PAGE + 7) || |
| 126 | (reg + len > RV3029_USR1_RAM_PAGE + 8)) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 127 | return -EINVAL; |
| 128 | |
| 129 | ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf); |
| 130 | if (ret < 0) |
| 131 | return ret; |
| 132 | if (ret < len) |
| 133 | return -EIO; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 138 | rv3029_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], |
| 139 | unsigned len) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 140 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 141 | if ((reg > RV3029_USR1_RAM_PAGE + 7) || |
| 142 | (reg + len > RV3029_USR1_RAM_PAGE + 8)) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
| 145 | return i2c_smbus_write_i2c_block_data(client, reg, len, buf); |
| 146 | } |
| 147 | |
| 148 | static int |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 149 | rv3029_i2c_update_bits(struct i2c_client *client, u8 reg, u8 mask, u8 set) |
| 150 | { |
| 151 | u8 buf; |
| 152 | int ret; |
| 153 | |
| 154 | ret = rv3029_i2c_read_regs(client, reg, &buf, 1); |
| 155 | if (ret < 0) |
| 156 | return ret; |
| 157 | buf &= ~mask; |
| 158 | buf |= set & mask; |
| 159 | ret = rv3029_i2c_write_regs(client, reg, &buf, 1); |
| 160 | if (ret < 0) |
| 161 | return ret; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 167 | rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 168 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 169 | int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 170 | |
| 171 | if (ret < 0) |
| 172 | return -EIO; |
| 173 | dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 178 | rv3029_i2c_set_sr(struct i2c_client *client, u8 val) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 179 | { |
| 180 | u8 buf[1]; |
| 181 | int sr; |
| 182 | |
| 183 | buf[0] = val; |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 184 | sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 185 | dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); |
| 186 | if (sr < 0) |
| 187 | return -EIO; |
| 188 | return 0; |
| 189 | } |
| 190 | |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 191 | static int rv3029_eeprom_busywait(struct i2c_client *client) |
| 192 | { |
| 193 | int i, ret; |
| 194 | u8 sr; |
| 195 | |
| 196 | for (i = 100; i > 0; i--) { |
| 197 | ret = rv3029_i2c_get_sr(client, &sr); |
| 198 | if (ret < 0) |
| 199 | break; |
| 200 | if (!(sr & RV3029_STATUS_EEBUSY)) |
| 201 | break; |
| 202 | usleep_range(1000, 10000); |
| 203 | } |
| 204 | if (i <= 0) { |
| 205 | dev_err(&client->dev, "EEPROM busy wait timeout.\n"); |
| 206 | return -ETIMEDOUT; |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static int rv3029_eeprom_exit(struct i2c_client *client) |
| 213 | { |
| 214 | /* Re-enable eeprom refresh */ |
| 215 | return rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL, |
| 216 | RV3029_ONOFF_CTRL_EERE, |
| 217 | RV3029_ONOFF_CTRL_EERE); |
| 218 | } |
| 219 | |
| 220 | static int rv3029_eeprom_enter(struct i2c_client *client) |
| 221 | { |
| 222 | int ret; |
| 223 | u8 sr; |
| 224 | |
| 225 | /* Check whether we are in the allowed voltage range. */ |
| 226 | ret = rv3029_i2c_get_sr(client, &sr); |
| 227 | if (ret < 0) |
| 228 | return ret; |
| 229 | if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { |
| 230 | /* We clear the bits and retry once just in case |
| 231 | * we had a brown out in early startup. |
| 232 | */ |
| 233 | sr &= ~RV3029_STATUS_VLOW1; |
| 234 | sr &= ~RV3029_STATUS_VLOW2; |
| 235 | ret = rv3029_i2c_set_sr(client, sr); |
| 236 | if (ret < 0) |
| 237 | return ret; |
| 238 | usleep_range(1000, 10000); |
| 239 | ret = rv3029_i2c_get_sr(client, &sr); |
| 240 | if (ret < 0) |
| 241 | return ret; |
| 242 | if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { |
| 243 | dev_err(&client->dev, |
| 244 | "Supply voltage is too low to safely access the EEPROM.\n"); |
| 245 | return -ENODEV; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* Disable eeprom refresh. */ |
| 250 | ret = rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL, |
| 251 | RV3029_ONOFF_CTRL_EERE, 0); |
| 252 | if (ret < 0) |
| 253 | return ret; |
| 254 | |
| 255 | /* Wait for any previous eeprom accesses to finish. */ |
| 256 | ret = rv3029_eeprom_busywait(client); |
| 257 | if (ret < 0) |
| 258 | rv3029_eeprom_exit(client); |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | static int rv3029_eeprom_read(struct i2c_client *client, u8 reg, |
| 264 | u8 buf[], size_t len) |
| 265 | { |
| 266 | int ret, err; |
| 267 | |
| 268 | err = rv3029_eeprom_enter(client); |
| 269 | if (err < 0) |
| 270 | return err; |
| 271 | |
| 272 | ret = rv3029_i2c_read_regs(client, reg, buf, len); |
| 273 | |
| 274 | err = rv3029_eeprom_exit(client); |
| 275 | if (err < 0) |
| 276 | return err; |
| 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int rv3029_eeprom_write(struct i2c_client *client, u8 reg, |
| 282 | u8 const buf[], size_t len) |
| 283 | { |
| 284 | int ret, err; |
| 285 | size_t i; |
| 286 | u8 tmp; |
| 287 | |
| 288 | err = rv3029_eeprom_enter(client); |
| 289 | if (err < 0) |
| 290 | return err; |
| 291 | |
| 292 | for (i = 0; i < len; i++, reg++) { |
| 293 | ret = rv3029_i2c_read_regs(client, reg, &tmp, 1); |
| 294 | if (ret < 0) |
| 295 | break; |
| 296 | if (tmp != buf[i]) { |
| 297 | ret = rv3029_i2c_write_regs(client, reg, &buf[i], 1); |
| 298 | if (ret < 0) |
| 299 | break; |
| 300 | } |
| 301 | ret = rv3029_eeprom_busywait(client); |
| 302 | if (ret < 0) |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | err = rv3029_eeprom_exit(client); |
| 307 | if (err < 0) |
| 308 | return err; |
| 309 | |
| 310 | return ret; |
| 311 | } |
| 312 | |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 313 | static int rv3029_eeprom_update_bits(struct i2c_client *client, |
| 314 | u8 reg, u8 mask, u8 set) |
| 315 | { |
| 316 | u8 buf; |
| 317 | int ret; |
| 318 | |
| 319 | ret = rv3029_eeprom_read(client, reg, &buf, 1); |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | buf &= ~mask; |
| 323 | buf |= set & mask; |
| 324 | ret = rv3029_eeprom_write(client, reg, &buf, 1); |
| 325 | if (ret < 0) |
| 326 | return ret; |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 331 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 332 | rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 333 | { |
| 334 | u8 buf[1]; |
| 335 | int ret; |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 336 | u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, }; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 337 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 338 | ret = rv3029_i2c_get_sr(client, buf); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 339 | if (ret < 0) { |
| 340 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); |
| 341 | return -EIO; |
| 342 | } |
| 343 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 344 | ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs, |
| 345 | RV3029_WATCH_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 346 | if (ret < 0) { |
| 347 | dev_err(&client->dev, "%s: reading RTC section failed\n", |
| 348 | __func__); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 352 | tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]); |
| 353 | tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 354 | |
| 355 | /* HR field has a more complex interpretation */ |
| 356 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 357 | const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC]; |
| 358 | |
| 359 | if (_hr & RV3029_REG_HR_12_24) { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 360 | /* 12h format */ |
| 361 | tm->tm_hour = bcd2bin(_hr & 0x1f); |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 362 | if (_hr & RV3029_REG_HR_PM) /* PM flag set */ |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 363 | tm->tm_hour += 12; |
| 364 | } else /* 24h format */ |
| 365 | tm->tm_hour = bcd2bin(_hr & 0x3f); |
| 366 | } |
| 367 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 368 | tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]); |
| 369 | tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1; |
| 370 | tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100; |
| 371 | tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 376 | static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 377 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 378 | return rv3029_i2c_read_time(to_i2c_client(dev), tm); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 382 | rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 383 | { |
| 384 | struct rtc_time *const tm = &alarm->time; |
| 385 | int ret; |
| 386 | u8 regs[8]; |
| 387 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 388 | ret = rv3029_i2c_get_sr(client, regs); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 389 | if (ret < 0) { |
| 390 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); |
| 391 | return -EIO; |
| 392 | } |
| 393 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 394 | ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs, |
| 395 | RV3029_ALARM_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 396 | |
| 397 | if (ret < 0) { |
| 398 | dev_err(&client->dev, "%s: reading alarm section failed\n", |
| 399 | __func__); |
| 400 | return ret; |
| 401 | } |
| 402 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 403 | tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f); |
| 404 | tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f); |
| 405 | tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f); |
| 406 | tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f); |
| 407 | tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1; |
| 408 | tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100; |
| 409 | tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 415 | rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 416 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 417 | return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 420 | static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client, |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 421 | int enable) |
| 422 | { |
| 423 | int ret; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 424 | |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 425 | /* enable/disable AIE irq */ |
| 426 | ret = rv3029_i2c_update_bits(client, RV3029_IRQ_CTRL, |
| 427 | RV3029_IRQ_CTRL_AIE, |
| 428 | (enable ? RV3029_IRQ_CTRL_AIE : 0)); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 429 | if (ret < 0) { |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 430 | dev_err(&client->dev, "can't update INT reg\n"); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 437 | static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client, |
| 438 | struct rtc_wkalrm *alarm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 439 | { |
| 440 | struct rtc_time *const tm = &alarm->time; |
| 441 | int ret; |
| 442 | u8 regs[8]; |
| 443 | |
| 444 | /* |
| 445 | * The clock has an 8 bit wide bcd-coded register (they never learn) |
| 446 | * for the year. tm_year is an offset from 1900 and we are interested |
| 447 | * in the 2000-2099 range, so any value less than 100 is invalid. |
| 448 | */ |
| 449 | if (tm->tm_year < 100) |
| 450 | return -EINVAL; |
| 451 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 452 | ret = rv3029_i2c_get_sr(client, regs); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 453 | if (ret < 0) { |
| 454 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); |
| 455 | return -EIO; |
| 456 | } |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 457 | regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f); |
| 458 | regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f); |
| 459 | regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f); |
| 460 | regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f); |
| 461 | regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1); |
| 462 | regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1); |
| 463 | regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 464 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 465 | ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs, |
| 466 | RV3029_ALARM_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 467 | if (ret < 0) |
| 468 | return ret; |
| 469 | |
| 470 | if (alarm->enabled) { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 471 | /* clear AF flag */ |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 472 | ret = rv3029_i2c_update_bits(client, RV3029_IRQ_FLAGS, |
| 473 | RV3029_IRQ_FLAGS_AF, 0); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 474 | if (ret < 0) { |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 475 | dev_err(&client->dev, "can't clear alarm flag\n"); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 476 | return ret; |
| 477 | } |
| 478 | /* enable AIE irq */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 479 | ret = rv3029_rtc_i2c_alarm_set_irq(client, 1); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 480 | if (ret) |
| 481 | return ret; |
| 482 | |
| 483 | dev_dbg(&client->dev, "alarm IRQ armed\n"); |
| 484 | } else { |
| 485 | /* disable AIE irq */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 486 | ret = rv3029_rtc_i2c_alarm_set_irq(client, 0); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 487 | if (ret) |
| 488 | return ret; |
| 489 | |
| 490 | dev_dbg(&client->dev, "alarm IRQ disabled\n"); |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 496 | static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 497 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 498 | return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static int |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 502 | rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 503 | { |
| 504 | u8 regs[8]; |
| 505 | int ret; |
| 506 | |
| 507 | /* |
| 508 | * The clock has an 8 bit wide bcd-coded register (they never learn) |
| 509 | * for the year. tm_year is an offset from 1900 and we are interested |
| 510 | * in the 2000-2099 range, so any value less than 100 is invalid. |
| 511 | */ |
| 512 | if (tm->tm_year < 100) |
| 513 | return -EINVAL; |
| 514 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 515 | regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec); |
| 516 | regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min); |
| 517 | regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour); |
| 518 | regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday); |
| 519 | regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1); |
| 520 | regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1); |
| 521 | regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 522 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 523 | ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs, |
| 524 | RV3029_WATCH_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 525 | if (ret < 0) |
| 526 | return ret; |
| 527 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 528 | ret = rv3029_i2c_get_sr(client, regs); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 529 | if (ret < 0) { |
| 530 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); |
| 531 | return ret; |
| 532 | } |
| 533 | /* clear PON bit */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 534 | ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON)); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 535 | if (ret < 0) { |
| 536 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 543 | static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 544 | { |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 545 | return rv3029_i2c_set_time(to_i2c_client(dev), tm); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 548 | static const struct rv3029_trickle_tab_elem { |
| 549 | u32 r; /* resistance in ohms */ |
| 550 | u8 conf; /* trickle config bits */ |
| 551 | } rv3029_trickle_tab[] = { |
| 552 | { |
| 553 | .r = 1076, |
| 554 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | |
| 555 | RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, |
| 556 | }, { |
| 557 | .r = 1091, |
| 558 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | |
| 559 | RV3029_TRICKLE_20K, |
| 560 | }, { |
| 561 | .r = 1137, |
| 562 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | |
| 563 | RV3029_TRICKLE_80K, |
| 564 | }, { |
| 565 | .r = 1154, |
| 566 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K, |
| 567 | }, { |
| 568 | .r = 1371, |
| 569 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K | |
| 570 | RV3029_TRICKLE_80K, |
| 571 | }, { |
| 572 | .r = 1395, |
| 573 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K, |
| 574 | }, { |
| 575 | .r = 1472, |
| 576 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K, |
| 577 | }, { |
| 578 | .r = 1500, |
| 579 | .conf = RV3029_TRICKLE_1K, |
| 580 | }, { |
| 581 | .r = 3810, |
| 582 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K | |
| 583 | RV3029_TRICKLE_80K, |
| 584 | }, { |
| 585 | .r = 4000, |
| 586 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K, |
| 587 | }, { |
| 588 | .r = 4706, |
| 589 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K, |
| 590 | }, { |
| 591 | .r = 5000, |
| 592 | .conf = RV3029_TRICKLE_5K, |
| 593 | }, { |
| 594 | .r = 16000, |
| 595 | .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, |
| 596 | }, { |
| 597 | .r = 20000, |
| 598 | .conf = RV3029_TRICKLE_20K, |
| 599 | }, { |
| 600 | .r = 80000, |
| 601 | .conf = RV3029_TRICKLE_80K, |
| 602 | }, |
| 603 | }; |
| 604 | |
| 605 | static void rv3029_trickle_config(struct i2c_client *client) |
| 606 | { |
| 607 | struct device_node *of_node = client->dev.of_node; |
| 608 | const struct rv3029_trickle_tab_elem *elem; |
| 609 | int i, err; |
| 610 | u32 ohms; |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 611 | u8 trickle_set_bits; |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 612 | |
| 613 | if (!of_node) |
| 614 | return; |
| 615 | |
| 616 | /* Configure the trickle charger. */ |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 617 | err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms); |
| 618 | if (err) { |
| 619 | /* Disable trickle charger. */ |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 620 | trickle_set_bits = 0; |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 621 | } else { |
| 622 | /* Enable trickle charger. */ |
| 623 | for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) { |
| 624 | elem = &rv3029_trickle_tab[i]; |
| 625 | if (elem->r >= ohms) |
| 626 | break; |
| 627 | } |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 628 | trickle_set_bits = elem->conf; |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 629 | dev_info(&client->dev, |
| 630 | "Trickle charger enabled at %d ohms resistance.\n", |
| 631 | elem->r); |
| 632 | } |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 633 | err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, |
| 634 | RV3029_TRICKLE_MASK, |
| 635 | trickle_set_bits); |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 636 | if (err < 0) { |
| 637 | dev_err(&client->dev, |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 638 | "Failed to update trickle charger config\n"); |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 642 | #ifdef CONFIG_RTC_DRV_RV3029_HWMON |
| 643 | |
| 644 | static int rv3029_read_temp(struct i2c_client *client, int *temp_mC) |
| 645 | { |
| 646 | int ret; |
| 647 | u8 temp; |
| 648 | |
| 649 | ret = rv3029_i2c_read_regs(client, RV3029_TEMP_PAGE, &temp, 1); |
| 650 | if (ret < 0) |
| 651 | return ret; |
| 652 | |
| 653 | *temp_mC = ((int)temp - 60) * 1000; |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static ssize_t rv3029_hwmon_show_temp(struct device *dev, |
| 659 | struct device_attribute *attr, |
| 660 | char *buf) |
| 661 | { |
| 662 | struct i2c_client *client = dev_get_drvdata(dev); |
| 663 | int ret, temp_mC; |
| 664 | |
| 665 | ret = rv3029_read_temp(client, &temp_mC); |
| 666 | if (ret < 0) |
| 667 | return ret; |
| 668 | |
| 669 | return sprintf(buf, "%d\n", temp_mC); |
| 670 | } |
| 671 | |
| 672 | static ssize_t rv3029_hwmon_set_update_interval(struct device *dev, |
| 673 | struct device_attribute *attr, |
| 674 | const char *buf, |
| 675 | size_t count) |
| 676 | { |
| 677 | struct i2c_client *client = dev_get_drvdata(dev); |
| 678 | unsigned long interval_ms; |
| 679 | int ret; |
| 680 | u8 th_set_bits = 0; |
| 681 | |
| 682 | ret = kstrtoul(buf, 10, &interval_ms); |
| 683 | if (ret < 0) |
| 684 | return ret; |
| 685 | |
| 686 | if (interval_ms != 0) { |
| 687 | th_set_bits |= RV3029_EECTRL_THE; |
| 688 | if (interval_ms >= 16000) |
| 689 | th_set_bits |= RV3029_EECTRL_THP; |
| 690 | } |
| 691 | ret = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, |
| 692 | RV3029_EECTRL_THE | RV3029_EECTRL_THP, |
| 693 | th_set_bits); |
| 694 | if (ret < 0) |
| 695 | return ret; |
| 696 | |
| 697 | return count; |
| 698 | } |
| 699 | |
| 700 | static ssize_t rv3029_hwmon_show_update_interval(struct device *dev, |
| 701 | struct device_attribute *attr, |
| 702 | char *buf) |
| 703 | { |
| 704 | struct i2c_client *client = dev_get_drvdata(dev); |
| 705 | int ret, interval_ms; |
| 706 | u8 eectrl; |
| 707 | |
| 708 | ret = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL, |
| 709 | &eectrl, 1); |
| 710 | if (ret < 0) |
| 711 | return ret; |
| 712 | |
| 713 | if (eectrl & RV3029_EECTRL_THE) { |
| 714 | if (eectrl & RV3029_EECTRL_THP) |
| 715 | interval_ms = 16000; |
| 716 | else |
| 717 | interval_ms = 1000; |
| 718 | } else { |
| 719 | interval_ms = 0; |
| 720 | } |
| 721 | |
| 722 | return sprintf(buf, "%d\n", interval_ms); |
| 723 | } |
| 724 | |
| 725 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp, |
| 726 | NULL, 0); |
| 727 | static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, |
| 728 | rv3029_hwmon_show_update_interval, |
| 729 | rv3029_hwmon_set_update_interval, 0); |
| 730 | |
| 731 | static struct attribute *rv3029_hwmon_attrs[] = { |
| 732 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 733 | &sensor_dev_attr_update_interval.dev_attr.attr, |
| 734 | NULL, |
| 735 | }; |
| 736 | ATTRIBUTE_GROUPS(rv3029_hwmon); |
| 737 | |
| 738 | static void rv3029_hwmon_register(struct i2c_client *client) |
| 739 | { |
| 740 | struct device *hwmon_dev; |
| 741 | |
| 742 | hwmon_dev = devm_hwmon_device_register_with_groups( |
| 743 | &client->dev, client->name, client, rv3029_hwmon_groups); |
| 744 | if (IS_ERR(hwmon_dev)) { |
| 745 | dev_warn(&client->dev, |
| 746 | "unable to register hwmon device %ld\n", |
| 747 | PTR_ERR(hwmon_dev)); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | #else /* CONFIG_RTC_DRV_RV3029_HWMON */ |
| 752 | |
| 753 | static void rv3029_hwmon_register(struct i2c_client *client) |
| 754 | { |
| 755 | } |
| 756 | |
| 757 | #endif /* CONFIG_RTC_DRV_RV3029_HWMON */ |
| 758 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 759 | static const struct rtc_class_ops rv3029_rtc_ops = { |
| 760 | .read_time = rv3029_rtc_read_time, |
| 761 | .set_time = rv3029_rtc_set_time, |
| 762 | .read_alarm = rv3029_rtc_read_alarm, |
| 763 | .set_alarm = rv3029_rtc_set_alarm, |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 764 | }; |
| 765 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 766 | static struct i2c_device_id rv3029_id[] = { |
Michael Büsch | baba623 | 2016-03-04 22:39:17 +0100 | [diff] [blame] | 767 | { "rv3029", 0 }, |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 768 | { "rv3029c2", 0 }, |
| 769 | { } |
| 770 | }; |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 771 | MODULE_DEVICE_TABLE(i2c, rv3029_id); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 772 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 773 | static int rv3029_probe(struct i2c_client *client, |
| 774 | const struct i2c_device_id *id) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 775 | { |
| 776 | struct rtc_device *rtc; |
| 777 | int rc = 0; |
| 778 | u8 buf[1]; |
| 779 | |
| 780 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL)) |
| 781 | return -ENODEV; |
| 782 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 783 | rc = rv3029_i2c_get_sr(client, buf); |
Gregory Hermant | 67ab244 | 2014-04-03 14:50:17 -0700 | [diff] [blame] | 784 | if (rc < 0) { |
| 785 | dev_err(&client->dev, "reading status failed\n"); |
| 786 | return rc; |
| 787 | } |
| 788 | |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 789 | rv3029_trickle_config(client); |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 790 | rv3029_hwmon_register(client); |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 791 | |
Jingoo Han | 3d7068c | 2013-04-29 16:19:48 -0700 | [diff] [blame] | 792 | rtc = devm_rtc_device_register(&client->dev, client->name, |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 793 | &rv3029_rtc_ops, THIS_MODULE); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 794 | |
| 795 | if (IS_ERR(rtc)) |
| 796 | return PTR_ERR(rtc); |
| 797 | |
| 798 | i2c_set_clientdata(client, rtc); |
| 799 | |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 800 | return 0; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 803 | static struct i2c_driver rv3029_driver = { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 804 | .driver = { |
| 805 | .name = "rtc-rv3029c2", |
| 806 | }, |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 807 | .probe = rv3029_probe, |
| 808 | .id_table = rv3029_id, |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 809 | }; |
| 810 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 811 | module_i2c_driver(rv3029_driver); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 812 | |
| 813 | MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>"); |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 814 | MODULE_AUTHOR("Michael Buesch <m@bues.ch>"); |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 815 | MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver"); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 816 | MODULE_LICENSE("GPL"); |