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