Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 2 | /* |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 3 | * Micro Crystal RV-3029 / RV-3049 rtc class driver |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 4 | * |
| 5 | * Author: Gregory Hermant <gregory.hermant@calao-systems.com> |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 6 | * Michael Buesch <m@bues.ch> |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 7 | * |
| 8 | * based on previously existing rtc class drivers |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/i2c.h> |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 13 | #include <linux/spi/spi.h> |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 14 | #include <linux/bcd.h> |
| 15 | #include <linux/rtc.h> |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 16 | #include <linux/delay.h> |
| 17 | #include <linux/of.h> |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 18 | #include <linux/hwmon.h> |
| 19 | #include <linux/hwmon-sysfs.h> |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 20 | #include <linux/regmap.h> |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 21 | |
| 22 | /* Register map */ |
| 23 | /* control section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 24 | #define RV3029_ONOFF_CTRL 0x00 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 25 | #define RV3029_ONOFF_CTRL_WE BIT(0) |
| 26 | #define RV3029_ONOFF_CTRL_TE BIT(1) |
| 27 | #define RV3029_ONOFF_CTRL_TAR BIT(2) |
| 28 | #define RV3029_ONOFF_CTRL_EERE BIT(3) |
| 29 | #define RV3029_ONOFF_CTRL_SRON BIT(4) |
| 30 | #define RV3029_ONOFF_CTRL_TD0 BIT(5) |
| 31 | #define RV3029_ONOFF_CTRL_TD1 BIT(6) |
| 32 | #define RV3029_ONOFF_CTRL_CLKINT BIT(7) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 33 | #define RV3029_IRQ_CTRL 0x01 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 34 | #define RV3029_IRQ_CTRL_AIE BIT(0) |
| 35 | #define RV3029_IRQ_CTRL_TIE BIT(1) |
| 36 | #define RV3029_IRQ_CTRL_V1IE BIT(2) |
| 37 | #define RV3029_IRQ_CTRL_V2IE BIT(3) |
| 38 | #define RV3029_IRQ_CTRL_SRIE BIT(4) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 39 | #define RV3029_IRQ_FLAGS 0x02 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 40 | #define RV3029_IRQ_FLAGS_AF BIT(0) |
| 41 | #define RV3029_IRQ_FLAGS_TF BIT(1) |
| 42 | #define RV3029_IRQ_FLAGS_V1IF BIT(2) |
| 43 | #define RV3029_IRQ_FLAGS_V2IF BIT(3) |
| 44 | #define RV3029_IRQ_FLAGS_SRF BIT(4) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 45 | #define RV3029_STATUS 0x03 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 46 | #define RV3029_STATUS_VLOW1 BIT(2) |
| 47 | #define RV3029_STATUS_VLOW2 BIT(3) |
| 48 | #define RV3029_STATUS_SR BIT(4) |
| 49 | #define RV3029_STATUS_PON BIT(5) |
| 50 | #define RV3029_STATUS_EEBUSY BIT(7) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 51 | #define RV3029_RST_CTRL 0x04 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 52 | #define RV3029_RST_CTRL_SYSR BIT(4) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 53 | #define RV3029_CONTROL_SECTION_LEN 0x05 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 54 | |
| 55 | /* watch section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 56 | #define RV3029_W_SEC 0x08 |
| 57 | #define RV3029_W_MINUTES 0x09 |
| 58 | #define RV3029_W_HOURS 0x0A |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 59 | #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */ |
| 60 | #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] | 61 | #define RV3029_W_DATE 0x0B |
| 62 | #define RV3029_W_DAYS 0x0C |
| 63 | #define RV3029_W_MONTHS 0x0D |
| 64 | #define RV3029_W_YEARS 0x0E |
| 65 | #define RV3029_WATCH_SECTION_LEN 0x07 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 66 | |
| 67 | /* alarm section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 68 | #define RV3029_A_SC 0x10 |
| 69 | #define RV3029_A_MN 0x11 |
| 70 | #define RV3029_A_HR 0x12 |
| 71 | #define RV3029_A_DT 0x13 |
| 72 | #define RV3029_A_DW 0x14 |
| 73 | #define RV3029_A_MO 0x15 |
| 74 | #define RV3029_A_YR 0x16 |
Mylène Josserand | dc492e8 | 2016-05-03 11:54:36 +0200 | [diff] [blame] | 75 | #define RV3029_A_AE_X BIT(7) |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 76 | #define RV3029_ALARM_SECTION_LEN 0x07 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 77 | |
| 78 | /* timer section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 79 | #define RV3029_TIMER_LOW 0x18 |
| 80 | #define RV3029_TIMER_HIGH 0x19 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 81 | |
| 82 | /* temperature section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 83 | #define RV3029_TEMP_PAGE 0x20 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 84 | |
| 85 | /* eeprom data section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 86 | #define RV3029_E2P_EEDATA1 0x28 |
| 87 | #define RV3029_E2P_EEDATA2 0x29 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 88 | #define RV3029_E2PDATA_SECTION_LEN 0x02 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 89 | |
| 90 | /* eeprom control section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 91 | #define RV3029_CONTROL_E2P_EECTRL 0x30 |
Michael Büsch | 7697de3 | 2016-03-04 22:39:49 +0100 | [diff] [blame] | 92 | #define RV3029_EECTRL_THP BIT(0) /* temp scan interval */ |
| 93 | #define RV3029_EECTRL_THE BIT(1) /* thermometer enable */ |
| 94 | #define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */ |
| 95 | #define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */ |
| 96 | #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */ |
| 97 | #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */ |
| 98 | #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */ |
| 99 | #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */ |
| 100 | #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\ |
| 101 | RV3029_TRICKLE_5K |\ |
| 102 | RV3029_TRICKLE_20K |\ |
| 103 | RV3029_TRICKLE_80K) |
| 104 | #define RV3029_TRICKLE_SHIFT 4 |
| 105 | #define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */ |
| 106 | #define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */ |
| 107 | #define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */ |
| 108 | #define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */ |
| 109 | #define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */ |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 110 | |
| 111 | /* user ram section */ |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 112 | #define RV3029_USR1_RAM_PAGE 0x38 |
| 113 | #define RV3029_USR1_SECTION_LEN 0x04 |
| 114 | #define RV3029_USR2_RAM_PAGE 0x3C |
| 115 | #define RV3029_USR2_SECTION_LEN 0x04 |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 116 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 117 | struct rv3029_data { |
| 118 | struct device *dev; |
| 119 | struct rtc_device *rtc; |
| 120 | struct regmap *regmap; |
| 121 | int irq; |
| 122 | }; |
| 123 | |
| 124 | static int rv3029_read_regs(struct device *dev, u8 reg, u8 *buf, |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 125 | unsigned int len) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 126 | { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 127 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 128 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 129 | return regmap_bulk_read(rv3029->regmap, reg, buf, len); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 132 | static int rv3029_write_regs(struct device *dev, u8 reg, u8 const buf[], |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 133 | unsigned int len) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 134 | { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 135 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
| 136 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 137 | return regmap_bulk_write(rv3029->regmap, reg, buf, len); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 140 | static int rv3029_eeprom_busywait(struct rv3029_data *rv3029) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 141 | { |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 142 | unsigned int sr; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 143 | int i, ret; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 144 | |
| 145 | for (i = 100; i > 0; i--) { |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 146 | ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 147 | if (ret < 0) |
| 148 | break; |
| 149 | if (!(sr & RV3029_STATUS_EEBUSY)) |
| 150 | break; |
| 151 | usleep_range(1000, 10000); |
| 152 | } |
| 153 | if (i <= 0) { |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 154 | dev_err(rv3029->dev, "EEPROM busy wait timeout.\n"); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 155 | return -ETIMEDOUT; |
| 156 | } |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 161 | static int rv3029_eeprom_exit(struct device *dev) |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 162 | { |
Alexandre Belloni | 609e97f | 2019-12-14 23:10:09 +0100 | [diff] [blame] | 163 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
| 164 | |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 165 | /* Re-enable eeprom refresh */ |
Alexandre Belloni | 609e97f | 2019-12-14 23:10:09 +0100 | [diff] [blame] | 166 | return regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL, |
Mylène Josserand | 4e7f1a6 | 2016-05-03 11:54:32 +0200 | [diff] [blame] | 167 | RV3029_ONOFF_CTRL_EERE, |
| 168 | RV3029_ONOFF_CTRL_EERE); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 171 | static int rv3029_eeprom_enter(struct device *dev) |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 172 | { |
Alexandre Belloni | 609e97f | 2019-12-14 23:10:09 +0100 | [diff] [blame] | 173 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 174 | unsigned int sr; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 175 | int ret; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 176 | |
| 177 | /* Check whether we are in the allowed voltage range. */ |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 178 | ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 179 | if (ret < 0) |
| 180 | return ret; |
| 181 | if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { |
| 182 | /* We clear the bits and retry once just in case |
| 183 | * we had a brown out in early startup. |
| 184 | */ |
Alexandre Belloni | 54c5970 | 2019-12-14 23:10:10 +0100 | [diff] [blame] | 185 | ret = regmap_update_bits(rv3029->regmap, RV3029_STATUS, |
| 186 | RV3029_STATUS_VLOW1 | |
| 187 | RV3029_STATUS_VLOW2, 0); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 188 | if (ret < 0) |
| 189 | return ret; |
| 190 | usleep_range(1000, 10000); |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 191 | ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 192 | if (ret < 0) |
| 193 | return ret; |
| 194 | if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 195 | dev_err(dev, |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 196 | "Supply voltage is too low to safely access the EEPROM.\n"); |
| 197 | return -ENODEV; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* Disable eeprom refresh. */ |
Alexandre Belloni | 609e97f | 2019-12-14 23:10:09 +0100 | [diff] [blame] | 202 | ret = regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL, |
| 203 | RV3029_ONOFF_CTRL_EERE, 0); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 204 | if (ret < 0) |
| 205 | return ret; |
| 206 | |
| 207 | /* Wait for any previous eeprom accesses to finish. */ |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 208 | ret = rv3029_eeprom_busywait(rv3029); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 209 | if (ret < 0) |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 210 | rv3029_eeprom_exit(dev); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 215 | static int rv3029_eeprom_read(struct device *dev, u8 reg, |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 216 | u8 buf[], size_t len) |
| 217 | { |
| 218 | int ret, err; |
| 219 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 220 | err = rv3029_eeprom_enter(dev); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 221 | if (err < 0) |
| 222 | return err; |
| 223 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 224 | ret = rv3029_read_regs(dev, reg, buf, len); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 225 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 226 | err = rv3029_eeprom_exit(dev); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 227 | if (err < 0) |
| 228 | return err; |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 233 | static int rv3029_eeprom_write(struct device *dev, u8 reg, |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 234 | u8 const buf[], size_t len) |
| 235 | { |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 236 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
Dan Carpenter | a6f2660 | 2019-08-17 09:56:04 +0300 | [diff] [blame] | 237 | int ret, err; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 238 | size_t i; |
| 239 | u8 tmp; |
| 240 | |
Dan Carpenter | a6f2660 | 2019-08-17 09:56:04 +0300 | [diff] [blame] | 241 | err = rv3029_eeprom_enter(dev); |
| 242 | if (err < 0) |
| 243 | return err; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 244 | |
| 245 | for (i = 0; i < len; i++, reg++) { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 246 | ret = rv3029_read_regs(dev, reg, &tmp, 1); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 247 | if (ret < 0) |
| 248 | break; |
| 249 | if (tmp != buf[i]) { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 250 | ret = rv3029_write_regs(dev, reg, &buf[i], 1); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 251 | if (ret < 0) |
| 252 | break; |
| 253 | } |
Alexandre Belloni | bb72dbb | 2019-12-14 23:10:12 +0100 | [diff] [blame] | 254 | ret = rv3029_eeprom_busywait(rv3029); |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 255 | if (ret < 0) |
| 256 | break; |
| 257 | } |
| 258 | |
Dan Carpenter | a6f2660 | 2019-08-17 09:56:04 +0300 | [diff] [blame] | 259 | err = rv3029_eeprom_exit(dev); |
| 260 | if (err < 0) |
| 261 | return err; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 262 | |
Dan Carpenter | a6f2660 | 2019-08-17 09:56:04 +0300 | [diff] [blame] | 263 | return ret; |
Michael Büsch | a7f6e28 | 2016-03-04 22:40:55 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 266 | static int rv3029_eeprom_update_bits(struct device *dev, |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 267 | u8 reg, u8 mask, u8 set) |
| 268 | { |
| 269 | u8 buf; |
| 270 | int ret; |
| 271 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 272 | ret = rv3029_eeprom_read(dev, reg, &buf, 1); |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | buf &= ~mask; |
| 276 | buf |= set & mask; |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 277 | ret = rv3029_eeprom_write(dev, reg, &buf, 1); |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 278 | if (ret < 0) |
| 279 | return ret; |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 284 | static irqreturn_t rv3029_handle_irq(int irq, void *dev_id) |
| 285 | { |
| 286 | struct device *dev = dev_id; |
| 287 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
| 288 | struct mutex *lock = &rv3029->rtc->ops_lock; |
| 289 | unsigned long events = 0; |
| 290 | u8 flags, controls; |
| 291 | int ret; |
| 292 | |
| 293 | mutex_lock(lock); |
| 294 | |
| 295 | ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1); |
| 296 | if (ret) { |
| 297 | dev_warn(dev, "Read IRQ Control Register error %d\n", ret); |
| 298 | mutex_unlock(lock); |
| 299 | return IRQ_NONE; |
| 300 | } |
| 301 | |
| 302 | ret = rv3029_read_regs(dev, RV3029_IRQ_FLAGS, &flags, 1); |
| 303 | if (ret) { |
| 304 | dev_warn(dev, "Read IRQ Flags Register error %d\n", ret); |
| 305 | mutex_unlock(lock); |
| 306 | return IRQ_NONE; |
| 307 | } |
| 308 | |
| 309 | if (flags & RV3029_IRQ_FLAGS_AF) { |
| 310 | flags &= ~RV3029_IRQ_FLAGS_AF; |
| 311 | controls &= ~RV3029_IRQ_CTRL_AIE; |
| 312 | events |= RTC_AF; |
| 313 | } |
| 314 | |
| 315 | if (events) { |
| 316 | rtc_update_irq(rv3029->rtc, 1, events); |
| 317 | rv3029_write_regs(dev, RV3029_IRQ_FLAGS, &flags, 1); |
| 318 | rv3029_write_regs(dev, RV3029_IRQ_CTRL, &controls, 1); |
| 319 | } |
| 320 | mutex_unlock(lock); |
| 321 | |
| 322 | return IRQ_HANDLED; |
| 323 | } |
| 324 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 325 | static int rv3029_read_time(struct device *dev, struct rtc_time *tm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 326 | { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 327 | int ret; |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 328 | u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, }; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 329 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 330 | ret = rv3029_read_regs(dev, RV3029_W_SEC, regs, |
Mylène Josserand | 4e7f1a6 | 2016-05-03 11:54:32 +0200 | [diff] [blame] | 331 | RV3029_WATCH_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 332 | if (ret < 0) { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 333 | dev_err(dev, "%s: reading RTC section failed\n", __func__); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 334 | return ret; |
| 335 | } |
| 336 | |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 337 | tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]); |
| 338 | tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 339 | |
| 340 | /* HR field has a more complex interpretation */ |
| 341 | { |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 342 | const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC]; |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 343 | |
| 344 | if (_hr & RV3029_REG_HR_12_24) { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 345 | /* 12h format */ |
| 346 | tm->tm_hour = bcd2bin(_hr & 0x1f); |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 347 | if (_hr & RV3029_REG_HR_PM) /* PM flag set */ |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 348 | tm->tm_hour += 12; |
| 349 | } else /* 24h format */ |
| 350 | tm->tm_hour = bcd2bin(_hr & 0x3f); |
| 351 | } |
| 352 | |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 353 | tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]); |
| 354 | tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1; |
| 355 | tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 100; |
| 356 | tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 361 | static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 362 | { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 363 | struct rtc_time *const tm = &alarm->time; |
| 364 | int ret; |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 365 | u8 regs[8], controls, flags; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 366 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 367 | ret = rv3029_read_regs(dev, RV3029_A_SC, regs, |
Mylène Josserand | 4e7f1a6 | 2016-05-03 11:54:32 +0200 | [diff] [blame] | 368 | RV3029_ALARM_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 369 | |
| 370 | if (ret < 0) { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 371 | dev_err(dev, "%s: reading alarm section failed\n", __func__); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 372 | return ret; |
| 373 | } |
| 374 | |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 375 | ret = rv3029_read_regs(dev, RV3029_IRQ_CTRL, &controls, 1); |
| 376 | if (ret) { |
| 377 | dev_err(dev, "Read IRQ Control Register error %d\n", ret); |
| 378 | return ret; |
| 379 | } |
| 380 | ret = rv3029_read_regs(dev, RV3029_IRQ_FLAGS, &flags, 1); |
| 381 | if (ret < 0) { |
| 382 | dev_err(dev, "Read IRQ Flags Register error %d\n", ret); |
| 383 | return ret; |
| 384 | } |
| 385 | |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 386 | tm->tm_sec = bcd2bin(regs[RV3029_A_SC - RV3029_A_SC] & 0x7f); |
| 387 | tm->tm_min = bcd2bin(regs[RV3029_A_MN - RV3029_A_SC] & 0x7f); |
| 388 | tm->tm_hour = bcd2bin(regs[RV3029_A_HR - RV3029_A_SC] & 0x3f); |
| 389 | tm->tm_mday = bcd2bin(regs[RV3029_A_DT - RV3029_A_SC] & 0x3f); |
| 390 | tm->tm_mon = bcd2bin(regs[RV3029_A_MO - RV3029_A_SC] & 0x1f) - 1; |
| 391 | tm->tm_year = bcd2bin(regs[RV3029_A_YR - RV3029_A_SC] & 0x7f) + 100; |
| 392 | 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] | 393 | |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 394 | alarm->enabled = !!(controls & RV3029_IRQ_CTRL_AIE); |
| 395 | alarm->pending = (flags & RV3029_IRQ_FLAGS_AF) && alarm->enabled; |
| 396 | |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 400 | static int rv3029_alarm_irq_enable(struct device *dev, unsigned int enable) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 401 | { |
Alexandre Belloni | 38ce8e3 | 2019-12-14 23:10:13 +0100 | [diff] [blame] | 402 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 403 | |
Alexandre Belloni | 38ce8e3 | 2019-12-14 23:10:13 +0100 | [diff] [blame] | 404 | return regmap_update_bits(rv3029->regmap, RV3029_IRQ_CTRL, |
| 405 | RV3029_IRQ_CTRL_AIE, |
| 406 | enable ? RV3029_IRQ_CTRL_AIE : 0); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 409 | static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 410 | { |
| 411 | struct rtc_time *const tm = &alarm->time; |
| 412 | int ret; |
| 413 | u8 regs[8]; |
| 414 | |
| 415 | /* |
| 416 | * The clock has an 8 bit wide bcd-coded register (they never learn) |
| 417 | * for the year. tm_year is an offset from 1900 and we are interested |
| 418 | * in the 2000-2099 range, so any value less than 100 is invalid. |
| 419 | */ |
| 420 | if (tm->tm_year < 100) |
| 421 | return -EINVAL; |
| 422 | |
Mylène Josserand | dc492e8 | 2016-05-03 11:54:36 +0200 | [diff] [blame] | 423 | /* Activate all the alarms with AE_x bit */ |
| 424 | regs[RV3029_A_SC - RV3029_A_SC] = bin2bcd(tm->tm_sec) | RV3029_A_AE_X; |
| 425 | regs[RV3029_A_MN - RV3029_A_SC] = bin2bcd(tm->tm_min) | RV3029_A_AE_X; |
| 426 | regs[RV3029_A_HR - RV3029_A_SC] = (bin2bcd(tm->tm_hour) & 0x3f) |
| 427 | | RV3029_A_AE_X; |
| 428 | regs[RV3029_A_DT - RV3029_A_SC] = (bin2bcd(tm->tm_mday) & 0x3f) |
| 429 | | RV3029_A_AE_X; |
| 430 | regs[RV3029_A_MO - RV3029_A_SC] = (bin2bcd(tm->tm_mon + 1) & 0x1f) |
| 431 | | RV3029_A_AE_X; |
| 432 | regs[RV3029_A_DW - RV3029_A_SC] = (bin2bcd(tm->tm_wday + 1) & 0x7) |
| 433 | | RV3029_A_AE_X; |
| 434 | regs[RV3029_A_YR - RV3029_A_SC] = (bin2bcd(tm->tm_year - 100)) |
| 435 | | RV3029_A_AE_X; |
| 436 | |
| 437 | /* Write the alarm */ |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 438 | ret = rv3029_write_regs(dev, RV3029_A_SC, regs, |
Mylène Josserand | 4e7f1a6 | 2016-05-03 11:54:32 +0200 | [diff] [blame] | 439 | RV3029_ALARM_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 440 | if (ret < 0) |
| 441 | return ret; |
| 442 | |
Alexandre Belloni | 8fd3d60 | 2019-12-14 23:10:14 +0100 | [diff] [blame^] | 443 | return rv3029_alarm_irq_enable(dev, alarm->enabled); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 446 | static int rv3029_set_time(struct device *dev, struct rtc_time *tm) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 447 | { |
Alexandre Belloni | 54c5970 | 2019-12-14 23:10:10 +0100 | [diff] [blame] | 448 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 449 | u8 regs[8]; |
| 450 | int ret; |
| 451 | |
| 452 | /* |
| 453 | * The clock has an 8 bit wide bcd-coded register (they never learn) |
| 454 | * for the year. tm_year is an offset from 1900 and we are interested |
| 455 | * in the 2000-2099 range, so any value less than 100 is invalid. |
| 456 | */ |
| 457 | if (tm->tm_year < 100) |
| 458 | return -EINVAL; |
| 459 | |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 460 | regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec); |
| 461 | regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min); |
| 462 | regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour); |
| 463 | regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday); |
| 464 | regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1); |
Mylène Josserand | 38201ca | 2016-05-03 11:54:37 +0200 | [diff] [blame] | 465 | regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7; |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 466 | regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 100); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 467 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 468 | ret = rv3029_write_regs(dev, RV3029_W_SEC, regs, |
Mylène Josserand | 4e7f1a6 | 2016-05-03 11:54:32 +0200 | [diff] [blame] | 469 | RV3029_WATCH_SECTION_LEN); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 470 | if (ret < 0) |
| 471 | return ret; |
| 472 | |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 473 | /* clear PON bit */ |
Alexandre Belloni | 54c5970 | 2019-12-14 23:10:10 +0100 | [diff] [blame] | 474 | return regmap_update_bits(rv3029->regmap, RV3029_STATUS, |
| 475 | RV3029_STATUS_PON, 0); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 476 | } |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 477 | |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 478 | static const struct rv3029_trickle_tab_elem { |
| 479 | u32 r; /* resistance in ohms */ |
| 480 | u8 conf; /* trickle config bits */ |
| 481 | } rv3029_trickle_tab[] = { |
| 482 | { |
| 483 | .r = 1076, |
| 484 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | |
| 485 | RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, |
| 486 | }, { |
| 487 | .r = 1091, |
| 488 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | |
| 489 | RV3029_TRICKLE_20K, |
| 490 | }, { |
| 491 | .r = 1137, |
| 492 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | |
| 493 | RV3029_TRICKLE_80K, |
| 494 | }, { |
| 495 | .r = 1154, |
| 496 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K, |
| 497 | }, { |
| 498 | .r = 1371, |
| 499 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K | |
| 500 | RV3029_TRICKLE_80K, |
| 501 | }, { |
| 502 | .r = 1395, |
| 503 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K, |
| 504 | }, { |
| 505 | .r = 1472, |
| 506 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K, |
| 507 | }, { |
| 508 | .r = 1500, |
| 509 | .conf = RV3029_TRICKLE_1K, |
| 510 | }, { |
| 511 | .r = 3810, |
| 512 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K | |
| 513 | RV3029_TRICKLE_80K, |
| 514 | }, { |
| 515 | .r = 4000, |
| 516 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K, |
| 517 | }, { |
| 518 | .r = 4706, |
| 519 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K, |
| 520 | }, { |
| 521 | .r = 5000, |
| 522 | .conf = RV3029_TRICKLE_5K, |
| 523 | }, { |
| 524 | .r = 16000, |
| 525 | .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, |
| 526 | }, { |
| 527 | .r = 20000, |
| 528 | .conf = RV3029_TRICKLE_20K, |
| 529 | }, { |
| 530 | .r = 80000, |
| 531 | .conf = RV3029_TRICKLE_80K, |
| 532 | }, |
| 533 | }; |
| 534 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 535 | static void rv3029_trickle_config(struct device *dev) |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 536 | { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 537 | struct device_node *of_node = dev->of_node; |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 538 | const struct rv3029_trickle_tab_elem *elem; |
| 539 | int i, err; |
| 540 | u32 ohms; |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 541 | u8 trickle_set_bits; |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 542 | |
| 543 | if (!of_node) |
| 544 | return; |
| 545 | |
| 546 | /* Configure the trickle charger. */ |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 547 | err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms); |
| 548 | if (err) { |
| 549 | /* Disable trickle charger. */ |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 550 | trickle_set_bits = 0; |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 551 | } else { |
| 552 | /* Enable trickle charger. */ |
| 553 | for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) { |
| 554 | elem = &rv3029_trickle_tab[i]; |
| 555 | if (elem->r >= ohms) |
| 556 | break; |
| 557 | } |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 558 | trickle_set_bits = elem->conf; |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 559 | dev_info(dev, |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 560 | "Trickle charger enabled at %d ohms resistance.\n", |
| 561 | elem->r); |
| 562 | } |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 563 | err = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL, |
Michael Büsch | 39387dc2 | 2016-03-10 18:34:23 +0100 | [diff] [blame] | 564 | RV3029_TRICKLE_MASK, |
| 565 | trickle_set_bits); |
Mylène Josserand | abe2f55 | 2016-05-03 11:54:35 +0200 | [diff] [blame] | 566 | if (err < 0) |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 567 | dev_err(dev, "Failed to update trickle charger config\n"); |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 570 | #ifdef CONFIG_RTC_DRV_RV3029_HWMON |
| 571 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 572 | static int rv3029_read_temp(struct device *dev, int *temp_mC) |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 573 | { |
| 574 | int ret; |
| 575 | u8 temp; |
| 576 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 577 | ret = rv3029_read_regs(dev, RV3029_TEMP_PAGE, &temp, 1); |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 578 | if (ret < 0) |
| 579 | return ret; |
| 580 | |
| 581 | *temp_mC = ((int)temp - 60) * 1000; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static ssize_t rv3029_hwmon_show_temp(struct device *dev, |
| 587 | struct device_attribute *attr, |
| 588 | char *buf) |
| 589 | { |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 590 | int ret, temp_mC; |
| 591 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 592 | ret = rv3029_read_temp(dev, &temp_mC); |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | |
| 596 | return sprintf(buf, "%d\n", temp_mC); |
| 597 | } |
| 598 | |
| 599 | static ssize_t rv3029_hwmon_set_update_interval(struct device *dev, |
| 600 | struct device_attribute *attr, |
| 601 | const char *buf, |
| 602 | size_t count) |
| 603 | { |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 604 | unsigned long interval_ms; |
| 605 | int ret; |
| 606 | u8 th_set_bits = 0; |
| 607 | |
| 608 | ret = kstrtoul(buf, 10, &interval_ms); |
| 609 | if (ret < 0) |
| 610 | return ret; |
| 611 | |
| 612 | if (interval_ms != 0) { |
| 613 | th_set_bits |= RV3029_EECTRL_THE; |
| 614 | if (interval_ms >= 16000) |
| 615 | th_set_bits |= RV3029_EECTRL_THP; |
| 616 | } |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 617 | ret = rv3029_eeprom_update_bits(dev, RV3029_CONTROL_E2P_EECTRL, |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 618 | RV3029_EECTRL_THE | RV3029_EECTRL_THP, |
| 619 | th_set_bits); |
| 620 | if (ret < 0) |
| 621 | return ret; |
| 622 | |
| 623 | return count; |
| 624 | } |
| 625 | |
| 626 | static ssize_t rv3029_hwmon_show_update_interval(struct device *dev, |
| 627 | struct device_attribute *attr, |
| 628 | char *buf) |
| 629 | { |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 630 | int ret, interval_ms; |
| 631 | u8 eectrl; |
| 632 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 633 | ret = rv3029_eeprom_read(dev, RV3029_CONTROL_E2P_EECTRL, |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 634 | &eectrl, 1); |
| 635 | if (ret < 0) |
| 636 | return ret; |
| 637 | |
| 638 | if (eectrl & RV3029_EECTRL_THE) { |
| 639 | if (eectrl & RV3029_EECTRL_THP) |
| 640 | interval_ms = 16000; |
| 641 | else |
| 642 | interval_ms = 1000; |
| 643 | } else { |
| 644 | interval_ms = 0; |
| 645 | } |
| 646 | |
| 647 | return sprintf(buf, "%d\n", interval_ms); |
| 648 | } |
| 649 | |
| 650 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp, |
| 651 | NULL, 0); |
| 652 | static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, |
| 653 | rv3029_hwmon_show_update_interval, |
| 654 | rv3029_hwmon_set_update_interval, 0); |
| 655 | |
| 656 | static struct attribute *rv3029_hwmon_attrs[] = { |
| 657 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 658 | &sensor_dev_attr_update_interval.dev_attr.attr, |
| 659 | NULL, |
| 660 | }; |
| 661 | ATTRIBUTE_GROUPS(rv3029_hwmon); |
| 662 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 663 | static void rv3029_hwmon_register(struct device *dev, const char *name) |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 664 | { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 665 | struct rv3029_data *rv3029 = dev_get_drvdata(dev); |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 666 | struct device *hwmon_dev; |
| 667 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 668 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029, |
| 669 | rv3029_hwmon_groups); |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 670 | if (IS_ERR(hwmon_dev)) { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 671 | dev_warn(dev, "unable to register hwmon device %ld\n", |
Mylène Josserand | 4e7f1a6 | 2016-05-03 11:54:32 +0200 | [diff] [blame] | 672 | PTR_ERR(hwmon_dev)); |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | #else /* CONFIG_RTC_DRV_RV3029_HWMON */ |
| 677 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 678 | static void rv3029_hwmon_register(struct device *dev, const char *name) |
Michael Büsch | a696b31 | 2016-03-10 18:34:46 +0100 | [diff] [blame] | 679 | { |
| 680 | } |
| 681 | |
| 682 | #endif /* CONFIG_RTC_DRV_RV3029_HWMON */ |
| 683 | |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 684 | static struct rtc_class_ops rv3029_rtc_ops = { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 685 | .read_time = rv3029_read_time, |
| 686 | .set_time = rv3029_set_time, |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 687 | }; |
| 688 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 689 | static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq, |
| 690 | const char *name) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 691 | { |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 692 | struct rv3029_data *rv3029; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 693 | int rc = 0; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 694 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 695 | rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL); |
| 696 | if (!rv3029) |
| 697 | return -ENOMEM; |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 698 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 699 | rv3029->regmap = regmap; |
| 700 | rv3029->irq = irq; |
| 701 | rv3029->dev = dev; |
| 702 | dev_set_drvdata(dev, rv3029); |
| 703 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 704 | rv3029_trickle_config(dev); |
| 705 | rv3029_hwmon_register(dev, name); |
Michael Büsch | e27e216 | 2016-03-04 22:41:19 +0100 | [diff] [blame] | 706 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 707 | rv3029->rtc = devm_rtc_device_register(dev, name, &rv3029_rtc_ops, |
| 708 | THIS_MODULE); |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 709 | if (IS_ERR(rv3029->rtc)) { |
| 710 | dev_err(dev, "unable to register the class device\n"); |
| 711 | return PTR_ERR(rv3029->rtc); |
| 712 | } |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 713 | |
Mylène Josserand | 0ddc5b8 | 2016-05-03 11:54:38 +0200 | [diff] [blame] | 714 | if (rv3029->irq > 0) { |
| 715 | rc = devm_request_threaded_irq(dev, rv3029->irq, |
| 716 | NULL, rv3029_handle_irq, |
| 717 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 718 | "rv3029", dev); |
| 719 | if (rc) { |
| 720 | dev_warn(dev, "unable to request IRQ, alarms disabled\n"); |
| 721 | rv3029->irq = 0; |
| 722 | } else { |
| 723 | rv3029_rtc_ops.read_alarm = rv3029_read_alarm; |
| 724 | rv3029_rtc_ops.set_alarm = rv3029_set_alarm; |
| 725 | rv3029_rtc_ops.alarm_irq_enable = rv3029_alarm_irq_enable; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | return 0; |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 730 | } |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 731 | |
Alexandre Belloni | c509e43 | 2019-12-14 23:10:08 +0100 | [diff] [blame] | 732 | static const struct regmap_range rv3029_holes_range[] = { |
| 733 | regmap_reg_range(0x05, 0x07), |
| 734 | regmap_reg_range(0x0f, 0x0f), |
| 735 | regmap_reg_range(0x17, 0x17), |
| 736 | regmap_reg_range(0x1a, 0x1f), |
| 737 | regmap_reg_range(0x21, 0x27), |
| 738 | regmap_reg_range(0x34, 0x37), |
| 739 | }; |
| 740 | |
| 741 | static const struct regmap_access_table rv3029_regs = { |
| 742 | .no_ranges = rv3029_holes_range, |
| 743 | .n_no_ranges = ARRAY_SIZE(rv3029_holes_range), |
| 744 | }; |
| 745 | |
| 746 | static const struct regmap_config config = { |
| 747 | .reg_bits = 8, |
| 748 | .val_bits = 8, |
| 749 | .rd_table = &rv3029_regs, |
| 750 | .wr_table = &rv3029_regs, |
| 751 | .max_register = 0x3f, |
| 752 | }; |
| 753 | |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 754 | #if IS_ENABLED(CONFIG_I2C) |
| 755 | |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 756 | static int rv3029_i2c_probe(struct i2c_client *client, |
| 757 | const struct i2c_device_id *id) |
| 758 | { |
| 759 | struct regmap *regmap; |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 760 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK | |
| 761 | I2C_FUNC_SMBUS_BYTE)) { |
| 762 | dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n"); |
| 763 | return -ENODEV; |
| 764 | } |
| 765 | |
| 766 | regmap = devm_regmap_init_i2c(client, &config); |
| 767 | if (IS_ERR(regmap)) { |
| 768 | dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", |
| 769 | __func__, PTR_ERR(regmap)); |
| 770 | return PTR_ERR(regmap); |
| 771 | } |
| 772 | |
| 773 | return rv3029_probe(&client->dev, regmap, client->irq, client->name); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Arvind Yadav | 45a6351 | 2017-08-20 00:37:55 +0530 | [diff] [blame] | 776 | static const struct i2c_device_id rv3029_id[] = { |
Arnd Bergmann | 814db2b | 2016-05-04 11:50:02 +0200 | [diff] [blame] | 777 | { "rv3029", 0 }, |
| 778 | { "rv3029c2", 0 }, |
| 779 | { } |
| 780 | }; |
| 781 | MODULE_DEVICE_TABLE(i2c, rv3029_id); |
| 782 | |
Javier Martinez Canillas | e696a1d | 2017-03-03 11:29:13 -0300 | [diff] [blame] | 783 | static const struct of_device_id rv3029_of_match[] = { |
Alexandre Belloni | 45b611c | 2017-09-15 04:00:02 +0200 | [diff] [blame] | 784 | { .compatible = "microcrystal,rv3029" }, |
| 785 | /* Backward compatibility only, do not use compatibles below: */ |
Javier Martinez Canillas | e696a1d | 2017-03-03 11:29:13 -0300 | [diff] [blame] | 786 | { .compatible = "rv3029" }, |
| 787 | { .compatible = "rv3029c2" }, |
| 788 | { .compatible = "mc,rv3029c2" }, |
| 789 | { } |
| 790 | }; |
| 791 | MODULE_DEVICE_TABLE(of, rv3029_of_match); |
| 792 | |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 793 | static struct i2c_driver rv3029_driver = { |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 794 | .driver = { |
Alexandre Belloni | 9b45ef9 | 2019-12-14 23:10:07 +0100 | [diff] [blame] | 795 | .name = "rv3029", |
Javier Martinez Canillas | e696a1d | 2017-03-03 11:29:13 -0300 | [diff] [blame] | 796 | .of_match_table = of_match_ptr(rv3029_of_match), |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 797 | }, |
Mylène Josserand | e6e3808 | 2016-05-03 11:54:33 +0200 | [diff] [blame] | 798 | .probe = rv3029_i2c_probe, |
Michael Büsch | aba39d2 | 2016-03-04 22:38:45 +0100 | [diff] [blame] | 799 | .id_table = rv3029_id, |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 800 | }; |
| 801 | |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 802 | static int rv3029_register_driver(void) |
| 803 | { |
| 804 | return i2c_add_driver(&rv3029_driver); |
| 805 | } |
| 806 | |
| 807 | static void rv3029_unregister_driver(void) |
| 808 | { |
| 809 | i2c_del_driver(&rv3029_driver); |
| 810 | } |
| 811 | |
| 812 | #else |
| 813 | |
| 814 | static int rv3029_register_driver(void) |
| 815 | { |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static void rv3029_unregister_driver(void) |
| 820 | { |
| 821 | } |
| 822 | |
| 823 | #endif |
| 824 | |
| 825 | #if IS_ENABLED(CONFIG_SPI_MASTER) |
| 826 | |
| 827 | static int rv3049_probe(struct spi_device *spi) |
| 828 | { |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 829 | struct regmap *regmap; |
| 830 | |
| 831 | regmap = devm_regmap_init_spi(spi, &config); |
| 832 | if (IS_ERR(regmap)) { |
| 833 | dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", |
| 834 | __func__, PTR_ERR(regmap)); |
| 835 | return PTR_ERR(regmap); |
| 836 | } |
| 837 | |
| 838 | return rv3029_probe(&spi->dev, regmap, spi->irq, "rv3049"); |
| 839 | } |
| 840 | |
| 841 | static struct spi_driver rv3049_driver = { |
| 842 | .driver = { |
| 843 | .name = "rv3049", |
| 844 | }, |
| 845 | .probe = rv3049_probe, |
| 846 | }; |
| 847 | |
| 848 | static int rv3049_register_driver(void) |
| 849 | { |
| 850 | return spi_register_driver(&rv3049_driver); |
| 851 | } |
| 852 | |
| 853 | static void rv3049_unregister_driver(void) |
| 854 | { |
| 855 | spi_unregister_driver(&rv3049_driver); |
| 856 | } |
| 857 | |
| 858 | #else |
| 859 | |
| 860 | static int rv3049_register_driver(void) |
| 861 | { |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | static void rv3049_unregister_driver(void) |
| 866 | { |
| 867 | } |
| 868 | |
| 869 | #endif |
| 870 | |
| 871 | static int __init rv30x9_init(void) |
| 872 | { |
| 873 | int ret; |
| 874 | |
| 875 | ret = rv3029_register_driver(); |
| 876 | if (ret) { |
| 877 | pr_err("Failed to register rv3029 driver: %d\n", ret); |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | ret = rv3049_register_driver(); |
| 882 | if (ret) { |
| 883 | pr_err("Failed to register rv3049 driver: %d\n", ret); |
| 884 | rv3029_unregister_driver(); |
| 885 | } |
| 886 | |
| 887 | return ret; |
| 888 | } |
| 889 | module_init(rv30x9_init) |
| 890 | |
| 891 | static void __exit rv30x9_exit(void) |
| 892 | { |
| 893 | rv3049_unregister_driver(); |
| 894 | rv3029_unregister_driver(); |
| 895 | } |
| 896 | module_exit(rv30x9_exit) |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 897 | |
| 898 | MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>"); |
Michael Büsch | 2dca3d9 | 2016-03-04 22:40:30 +0100 | [diff] [blame] | 899 | MODULE_AUTHOR("Michael Buesch <m@bues.ch>"); |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 900 | MODULE_DESCRIPTION("Micro Crystal RV3029/RV3049 RTC driver"); |
Heiko Schocher | 5236523 | 2011-05-26 16:25:05 -0700 | [diff] [blame] | 901 | MODULE_LICENSE("GPL"); |
Mylène Josserand | c2a1c14 | 2016-05-03 11:54:34 +0200 | [diff] [blame] | 902 | MODULE_ALIAS("spi:rv3049"); |