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