Alexandre Belloni | de311aa | 2019-04-01 18:33:49 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * An I2C driver for the PCF85063 RTC |
| 4 | * Copyright 2014 Rose Technology |
| 5 | * |
| 6 | * Author: Søren Andersen <san@rosetechnology.dk> |
| 7 | * Maintainers: http://www.nslu2-linux.org/ |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/i2c.h> |
| 10 | #include <linux/bcd.h> |
| 11 | #include <linux/rtc.h> |
| 12 | #include <linux/module.h> |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 13 | #include <linux/of_device.h> |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 14 | #include <linux/pm_wakeirq.h> |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 15 | #include <linux/regmap.h> |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 16 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 17 | /* |
| 18 | * Information for this driver was pulled from the following datasheets. |
| 19 | * |
| 20 | * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf |
| 21 | * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf |
| 22 | * |
| 23 | * PCF85063A -- Rev. 6 — 18 November 2015 |
| 24 | * PCF85063TP -- Rev. 4 — 6 May 2015 |
| 25 | */ |
| 26 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 27 | #define PCF85063_REG_CTRL1 0x00 /* status */ |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 28 | #define PCF85063_REG_CTRL1_CAP_SEL BIT(0) |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 29 | #define PCF85063_REG_CTRL1_STOP BIT(5) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 30 | |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 31 | #define PCF85063_REG_CTRL2 0x01 |
| 32 | #define PCF85063_CTRL2_AF BIT(6) |
| 33 | #define PCF85063_CTRL2_AIE BIT(7) |
| 34 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 35 | #define PCF85063_REG_SC 0x04 /* datetime */ |
Juergen Borleis | 6cc4c8b | 2016-02-09 11:57:26 +0100 | [diff] [blame] | 36 | #define PCF85063_REG_SC_OS 0x80 |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 37 | |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 38 | #define PCF85063_REG_ALM_S 0x0b |
| 39 | #define PCF85063_AEN BIT(7) |
| 40 | |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 41 | struct pcf85063_config { |
| 42 | struct regmap_config regmap; |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 43 | unsigned has_alarms:1; |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 46 | struct pcf85063 { |
| 47 | struct rtc_device *rtc; |
| 48 | struct regmap *regmap; |
| 49 | }; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 50 | |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 51 | static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 52 | { |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 53 | struct pcf85063 *pcf85063 = dev_get_drvdata(dev); |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 54 | int rc; |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 55 | u8 regs[7]; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 56 | |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 57 | /* |
| 58 | * while reading, the time/date registers are blocked and not updated |
| 59 | * anymore until the access is finished. To not lose a second |
| 60 | * event, the access must be finished within one second. So, read all |
| 61 | * time/date registers in one turn. |
| 62 | */ |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 63 | rc = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_SC, regs, |
| 64 | sizeof(regs)); |
| 65 | if (rc) |
| 66 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 67 | |
Juergen Borleis | 6cc4c8b | 2016-02-09 11:57:26 +0100 | [diff] [blame] | 68 | /* if the clock has lost its power it makes no sense to use its time */ |
| 69 | if (regs[0] & PCF85063_REG_SC_OS) { |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 70 | dev_warn(&pcf85063->rtc->dev, "Power loss detected, invalid time\n"); |
Juergen Borleis | 6cc4c8b | 2016-02-09 11:57:26 +0100 | [diff] [blame] | 71 | return -EINVAL; |
| 72 | } |
| 73 | |
Juergen Borleis | 7b57684 | 2016-02-09 11:57:25 +0100 | [diff] [blame] | 74 | tm->tm_sec = bcd2bin(regs[0] & 0x7F); |
| 75 | tm->tm_min = bcd2bin(regs[1] & 0x7F); |
| 76 | tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */ |
| 77 | tm->tm_mday = bcd2bin(regs[3] & 0x3F); |
| 78 | tm->tm_wday = regs[4] & 0x07; |
| 79 | tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */ |
| 80 | tm->tm_year = bcd2bin(regs[6]); |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 81 | tm->tm_year += 100; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 82 | |
Alexandre Belloni | 0a6b888 | 2018-02-21 16:07:34 +0100 | [diff] [blame] | 83 | return 0; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alexandre Belloni | 965271d | 2018-02-21 16:09:27 +0100 | [diff] [blame] | 86 | static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 87 | { |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 88 | struct pcf85063 *pcf85063 = dev_get_drvdata(dev); |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 89 | int rc; |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 90 | u8 regs[7]; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 91 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 92 | /* |
| 93 | * to accurately set the time, reset the divider chain and keep it in |
| 94 | * reset state until all time/date registers are written |
| 95 | */ |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 96 | rc = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1, |
| 97 | PCF85063_REG_CTRL1_STOP, |
| 98 | PCF85063_REG_CTRL1_STOP); |
| 99 | if (rc) |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 100 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 101 | |
| 102 | /* hours, minutes and seconds */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 103 | regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */ |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 104 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 105 | regs[1] = bin2bcd(tm->tm_min); |
| 106 | regs[2] = bin2bcd(tm->tm_hour); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 107 | |
| 108 | /* Day of month, 1 - 31 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 109 | regs[3] = bin2bcd(tm->tm_mday); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 110 | |
| 111 | /* Day, 0 - 6 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 112 | regs[4] = tm->tm_wday & 0x07; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 113 | |
| 114 | /* month, 1 - 12 */ |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 115 | regs[5] = bin2bcd(tm->tm_mon + 1); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 116 | |
| 117 | /* year and century */ |
Alexandre Belloni | c421ce7 | 2016-07-18 11:08:59 +0200 | [diff] [blame] | 118 | regs[6] = bin2bcd(tm->tm_year - 100); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 119 | |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 120 | /* write all registers at once */ |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 121 | rc = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_SC, |
| 122 | regs, sizeof(regs)); |
| 123 | if (rc) |
Juergen Borleis | 31d4d33 | 2016-02-09 11:57:27 +0100 | [diff] [blame] | 124 | return rc; |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 125 | |
Chris DeBruin | 0d981f8 | 2016-07-12 17:15:46 -0400 | [diff] [blame] | 126 | /* |
| 127 | * Write the control register as a separate action since the size of |
| 128 | * the register space is different between the PCF85063TP and |
| 129 | * PCF85063A devices. The rollover point can not be used. |
| 130 | */ |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 131 | return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1, |
| 132 | PCF85063_REG_CTRL1_STOP, 0); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 135 | static int pcf85063_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 136 | { |
| 137 | struct pcf85063 *pcf85063 = dev_get_drvdata(dev); |
| 138 | u8 buf[4]; |
| 139 | unsigned int val; |
| 140 | int ret; |
| 141 | |
| 142 | ret = regmap_bulk_read(pcf85063->regmap, PCF85063_REG_ALM_S, |
| 143 | buf, sizeof(buf)); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | alrm->time.tm_sec = bcd2bin(buf[0]); |
| 148 | alrm->time.tm_min = bcd2bin(buf[1]); |
| 149 | alrm->time.tm_hour = bcd2bin(buf[2]); |
| 150 | alrm->time.tm_mday = bcd2bin(buf[3]); |
| 151 | |
| 152 | ret = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL2, &val); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | alrm->enabled = !!(val & PCF85063_CTRL2_AIE); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int pcf85063_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 162 | { |
| 163 | struct pcf85063 *pcf85063 = dev_get_drvdata(dev); |
| 164 | u8 buf[5]; |
| 165 | int ret; |
| 166 | |
| 167 | buf[0] = bin2bcd(alrm->time.tm_sec); |
| 168 | buf[1] = bin2bcd(alrm->time.tm_min); |
| 169 | buf[2] = bin2bcd(alrm->time.tm_hour); |
| 170 | buf[3] = bin2bcd(alrm->time.tm_mday); |
| 171 | buf[4] = PCF85063_AEN; /* Do not match on week day */ |
| 172 | |
| 173 | ret = regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2, |
| 174 | PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF, 0); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | |
| 178 | ret = regmap_bulk_write(pcf85063->regmap, PCF85063_REG_ALM_S, |
| 179 | buf, sizeof(buf)); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2, |
| 184 | PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF, |
| 185 | alrm->enabled ? PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF : PCF85063_CTRL2_AF); |
| 186 | } |
| 187 | |
| 188 | static int pcf85063_rtc_alarm_irq_enable(struct device *dev, |
| 189 | unsigned int enabled) |
| 190 | { |
| 191 | struct pcf85063 *pcf85063 = dev_get_drvdata(dev); |
| 192 | |
| 193 | return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2, |
| 194 | PCF85063_CTRL2_AIE, |
| 195 | enabled ? PCF85063_CTRL2_AIE : 0); |
| 196 | } |
| 197 | |
| 198 | static irqreturn_t pcf85063_rtc_handle_irq(int irq, void *dev_id) |
| 199 | { |
| 200 | struct pcf85063 *pcf85063 = dev_id; |
| 201 | unsigned int val; |
| 202 | int err; |
| 203 | |
| 204 | err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL2, &val); |
| 205 | if (err) |
| 206 | return IRQ_NONE; |
| 207 | |
| 208 | if (val & PCF85063_CTRL2_AF) { |
| 209 | rtc_update_irq(pcf85063->rtc, 1, RTC_IRQF | RTC_AF); |
| 210 | regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL2, |
| 211 | PCF85063_CTRL2_AIE | PCF85063_CTRL2_AF, |
| 212 | 0); |
| 213 | return IRQ_HANDLED; |
| 214 | } |
| 215 | |
| 216 | return IRQ_NONE; |
| 217 | } |
| 218 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 219 | static const struct rtc_class_ops pcf85063_rtc_ops = { |
| 220 | .read_time = pcf85063_rtc_read_time, |
| 221 | .set_time = pcf85063_rtc_set_time |
| 222 | }; |
| 223 | |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 224 | static const struct rtc_class_ops pcf85063_rtc_ops_alarm = { |
| 225 | .read_time = pcf85063_rtc_read_time, |
| 226 | .set_time = pcf85063_rtc_set_time, |
| 227 | .read_alarm = pcf85063_rtc_read_alarm, |
| 228 | .set_alarm = pcf85063_rtc_set_alarm, |
| 229 | .alarm_irq_enable = pcf85063_rtc_alarm_irq_enable, |
| 230 | }; |
| 231 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 232 | static int pcf85063_load_capacitance(struct pcf85063 *pcf85063, |
| 233 | const struct device_node *np) |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 234 | { |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 235 | u32 load = 7000; |
| 236 | u8 reg = 0; |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 237 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 238 | of_property_read_u32(np, "quartz-load-femtofarads", &load); |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 239 | switch (load) { |
| 240 | default: |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 241 | dev_warn(&pcf85063->rtc->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 7000", |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 242 | load); |
| 243 | /* fall through */ |
| 244 | case 7000: |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 245 | break; |
| 246 | case 12500: |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 247 | reg = PCF85063_REG_CTRL1_CAP_SEL; |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 251 | return regmap_update_bits(pcf85063->regmap, PCF85063_REG_CTRL1, |
| 252 | PCF85063_REG_CTRL1_CAP_SEL, reg); |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 255 | static const struct pcf85063_config pcf85063a_config = { |
| 256 | .regmap = { |
| 257 | .reg_bits = 8, |
| 258 | .val_bits = 8, |
| 259 | .max_register = 0x11, |
| 260 | }, |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 261 | .has_alarms = 1, |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | static const struct pcf85063_config pcf85063tp_config = { |
| 265 | .regmap = { |
| 266 | .reg_bits = 8, |
| 267 | .val_bits = 8, |
| 268 | .max_register = 0x0a, |
| 269 | }, |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 270 | }; |
| 271 | |
Alexandre Belloni | 0f21700 | 2019-04-01 18:08:05 +0200 | [diff] [blame] | 272 | static int pcf85063_probe(struct i2c_client *client) |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 273 | { |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 274 | struct pcf85063 *pcf85063; |
| 275 | unsigned int tmp; |
Mirza Krak | c18b4c5 | 2016-10-17 15:53:31 +0200 | [diff] [blame] | 276 | int err; |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 277 | const struct pcf85063_config *config = &pcf85063tp_config; |
| 278 | const void *data = of_device_get_match_data(&client->dev); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 279 | |
| 280 | dev_dbg(&client->dev, "%s\n", __func__); |
| 281 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 282 | pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063), |
| 283 | GFP_KERNEL); |
| 284 | if (!pcf85063) |
| 285 | return -ENOMEM; |
| 286 | |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 287 | if (data) |
| 288 | config = data; |
| 289 | |
| 290 | pcf85063->regmap = devm_regmap_init_i2c(client, &config->regmap); |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 291 | if (IS_ERR(pcf85063->regmap)) |
| 292 | return PTR_ERR(pcf85063->regmap); |
| 293 | |
| 294 | i2c_set_clientdata(client, pcf85063); |
| 295 | |
| 296 | err = regmap_read(pcf85063->regmap, PCF85063_REG_CTRL1, &tmp); |
| 297 | if (err) { |
Mirza Krak | c18b4c5 | 2016-10-17 15:53:31 +0200 | [diff] [blame] | 298 | dev_err(&client->dev, "RTC chip is not present\n"); |
| 299 | return err; |
| 300 | } |
| 301 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 302 | pcf85063->rtc = devm_rtc_allocate_device(&client->dev); |
| 303 | if (IS_ERR(pcf85063->rtc)) |
| 304 | return PTR_ERR(pcf85063->rtc); |
| 305 | |
| 306 | err = pcf85063_load_capacitance(pcf85063, client->dev.of_node); |
Sam Ravnborg | bbb4383 | 2019-01-19 10:00:31 +0100 | [diff] [blame] | 307 | if (err < 0) |
| 308 | dev_warn(&client->dev, "failed to set xtal load capacitance: %d", |
| 309 | err); |
| 310 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 311 | pcf85063->rtc->ops = &pcf85063_rtc_ops; |
| 312 | pcf85063->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; |
| 313 | pcf85063->rtc->range_max = RTC_TIMESTAMP_END_2099; |
Alexandre Belloni | 05cb3a5 | 2019-04-01 18:08:12 +0200 | [diff] [blame^] | 314 | pcf85063->rtc->uie_unsupported = 1; |
| 315 | |
| 316 | if (config->has_alarms && client->irq > 0) { |
| 317 | err = devm_request_threaded_irq(&client->dev, client->irq, |
| 318 | NULL, pcf85063_rtc_handle_irq, |
| 319 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 320 | "pcf85063", pcf85063); |
| 321 | if (err) { |
| 322 | dev_warn(&pcf85063->rtc->dev, |
| 323 | "unable to request IRQ, alarms disabled\n"); |
| 324 | } else { |
| 325 | pcf85063->rtc->ops = &pcf85063_rtc_ops_alarm; |
| 326 | device_init_wakeup(&client->dev, true); |
| 327 | err = dev_pm_set_wake_irq(&client->dev, client->irq); |
| 328 | if (err) |
| 329 | dev_err(&pcf85063->rtc->dev, |
| 330 | "failed to enable irq wake\n"); |
| 331 | } |
| 332 | } |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 333 | |
Alexandre Belloni | e89b60d | 2019-04-01 18:08:10 +0200 | [diff] [blame] | 334 | return rtc_register_device(pcf85063->rtc); |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 337 | #ifdef CONFIG_OF |
| 338 | static const struct of_device_id pcf85063_of_match[] = { |
Alexandre Belloni | 0e2e877 | 2019-04-01 18:08:11 +0200 | [diff] [blame] | 339 | { .compatible = "nxp,pcf85063", .data = &pcf85063tp_config }, |
| 340 | { .compatible = "nxp,pcf85063tp", .data = &pcf85063tp_config }, |
| 341 | { .compatible = "nxp,pcf85063a", .data = &pcf85063a_config }, |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 342 | {} |
| 343 | }; |
| 344 | MODULE_DEVICE_TABLE(of, pcf85063_of_match); |
| 345 | #endif |
| 346 | |
| 347 | static struct i2c_driver pcf85063_driver = { |
| 348 | .driver = { |
| 349 | .name = "rtc-pcf85063", |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 350 | .of_match_table = of_match_ptr(pcf85063_of_match), |
| 351 | }, |
Alexandre Belloni | 0f21700 | 2019-04-01 18:08:05 +0200 | [diff] [blame] | 352 | .probe_new = pcf85063_probe, |
Søren Andersen | 796b7ab | 2014-08-08 14:20:22 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | module_i2c_driver(pcf85063_driver); |
| 356 | |
| 357 | MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>"); |
| 358 | MODULE_DESCRIPTION("PCF85063 RTC driver"); |
| 359 | MODULE_LICENSE("GPL"); |