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