Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/rtc/rtc-pcf85363.c |
| 3 | * |
| 4 | * Driver for NXP PCF85363 real-time clock. |
| 5 | * |
| 6 | * Copyright (C) 2017 Eric Nelson |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * Based loosely on rtc-8583 by Russell King, Wolfram Sang and Juergen Beisert |
| 13 | */ |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/rtc.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/bcd.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_device.h> |
| 24 | #include <linux/regmap.h> |
| 25 | |
| 26 | /* |
| 27 | * Date/Time registers |
| 28 | */ |
| 29 | #define DT_100THS 0x00 |
| 30 | #define DT_SECS 0x01 |
| 31 | #define DT_MINUTES 0x02 |
| 32 | #define DT_HOURS 0x03 |
| 33 | #define DT_DAYS 0x04 |
| 34 | #define DT_WEEKDAYS 0x05 |
| 35 | #define DT_MONTHS 0x06 |
| 36 | #define DT_YEARS 0x07 |
| 37 | |
| 38 | /* |
| 39 | * Alarm registers |
| 40 | */ |
| 41 | #define DT_SECOND_ALM1 0x08 |
| 42 | #define DT_MINUTE_ALM1 0x09 |
| 43 | #define DT_HOUR_ALM1 0x0a |
| 44 | #define DT_DAY_ALM1 0x0b |
| 45 | #define DT_MONTH_ALM1 0x0c |
| 46 | #define DT_MINUTE_ALM2 0x0d |
| 47 | #define DT_HOUR_ALM2 0x0e |
| 48 | #define DT_WEEKDAY_ALM2 0x0f |
| 49 | #define DT_ALARM_EN 0x10 |
| 50 | |
| 51 | /* |
| 52 | * Time stamp registers |
| 53 | */ |
| 54 | #define DT_TIMESTAMP1 0x11 |
| 55 | #define DT_TIMESTAMP2 0x17 |
| 56 | #define DT_TIMESTAMP3 0x1d |
| 57 | #define DT_TS_MODE 0x23 |
| 58 | |
| 59 | /* |
| 60 | * control registers |
| 61 | */ |
| 62 | #define CTRL_OFFSET 0x24 |
| 63 | #define CTRL_OSCILLATOR 0x25 |
| 64 | #define CTRL_BATTERY 0x26 |
| 65 | #define CTRL_PIN_IO 0x27 |
| 66 | #define CTRL_FUNCTION 0x28 |
| 67 | #define CTRL_INTA_EN 0x29 |
| 68 | #define CTRL_INTB_EN 0x2a |
| 69 | #define CTRL_FLAGS 0x2b |
| 70 | #define CTRL_RAMBYTE 0x2c |
| 71 | #define CTRL_WDOG 0x2d |
| 72 | #define CTRL_STOP_EN 0x2e |
| 73 | #define CTRL_RESETS 0x2f |
| 74 | #define CTRL_RAM 0x40 |
| 75 | |
Alexandre Belloni | e5aac26 | 2018-03-01 01:42:19 +0100 | [diff] [blame^] | 76 | #define ALRM_SEC_A1E BIT(0) |
| 77 | #define ALRM_MIN_A1E BIT(1) |
| 78 | #define ALRM_HR_A1E BIT(2) |
| 79 | #define ALRM_DAY_A1E BIT(3) |
| 80 | #define ALRM_MON_A1E BIT(4) |
| 81 | #define ALRM_MIN_A2E BIT(5) |
| 82 | #define ALRM_HR_A2E BIT(6) |
| 83 | #define ALRM_DAY_A2E BIT(7) |
| 84 | |
| 85 | #define INT_WDIE BIT(0) |
| 86 | #define INT_BSIE BIT(1) |
| 87 | #define INT_TSRIE BIT(2) |
| 88 | #define INT_A2IE BIT(3) |
| 89 | #define INT_A1IE BIT(4) |
| 90 | #define INT_OIE BIT(5) |
| 91 | #define INT_PIE BIT(6) |
| 92 | #define INT_ILP BIT(7) |
| 93 | |
| 94 | #define FLAGS_TSR1F BIT(0) |
| 95 | #define FLAGS_TSR2F BIT(1) |
| 96 | #define FLAGS_TSR3F BIT(2) |
| 97 | #define FLAGS_BSF BIT(3) |
| 98 | #define FLAGS_WDF BIT(4) |
| 99 | #define FLAGS_A1F BIT(5) |
| 100 | #define FLAGS_A2F BIT(6) |
| 101 | #define FLAGS_PIF BIT(7) |
| 102 | |
| 103 | #define PIN_IO_INTAPM GENMASK(1, 0) |
| 104 | #define PIN_IO_INTA_CLK 0 |
| 105 | #define PIN_IO_INTA_BAT 1 |
| 106 | #define PIN_IO_INTA_OUT 2 |
| 107 | #define PIN_IO_INTA_HIZ 3 |
| 108 | |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 109 | #define NVRAM_SIZE 0x40 |
| 110 | |
| 111 | static struct i2c_driver pcf85363_driver; |
| 112 | |
| 113 | struct pcf85363 { |
| 114 | struct device *dev; |
| 115 | struct rtc_device *rtc; |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 116 | struct regmap *regmap; |
| 117 | }; |
| 118 | |
| 119 | static int pcf85363_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 120 | { |
| 121 | struct pcf85363 *pcf85363 = dev_get_drvdata(dev); |
| 122 | unsigned char buf[DT_YEARS + 1]; |
| 123 | int ret, len = sizeof(buf); |
| 124 | |
| 125 | /* read the RTC date and time registers all at once */ |
| 126 | ret = regmap_bulk_read(pcf85363->regmap, DT_100THS, buf, len); |
| 127 | if (ret) { |
| 128 | dev_err(dev, "%s: error %d\n", __func__, ret); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | tm->tm_year = bcd2bin(buf[DT_YEARS]); |
| 133 | /* adjust for 1900 base of rtc_time */ |
| 134 | tm->tm_year += 100; |
| 135 | |
| 136 | tm->tm_wday = buf[DT_WEEKDAYS] & 7; |
| 137 | buf[DT_SECS] &= 0x7F; |
| 138 | tm->tm_sec = bcd2bin(buf[DT_SECS]); |
| 139 | buf[DT_MINUTES] &= 0x7F; |
| 140 | tm->tm_min = bcd2bin(buf[DT_MINUTES]); |
| 141 | tm->tm_hour = bcd2bin(buf[DT_HOURS]); |
| 142 | tm->tm_mday = bcd2bin(buf[DT_DAYS]); |
| 143 | tm->tm_mon = bcd2bin(buf[DT_MONTHS]) - 1; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int pcf85363_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 149 | { |
| 150 | struct pcf85363 *pcf85363 = dev_get_drvdata(dev); |
| 151 | unsigned char buf[DT_YEARS + 1]; |
| 152 | int len = sizeof(buf); |
| 153 | |
| 154 | buf[DT_100THS] = 0; |
| 155 | buf[DT_SECS] = bin2bcd(tm->tm_sec); |
| 156 | buf[DT_MINUTES] = bin2bcd(tm->tm_min); |
| 157 | buf[DT_HOURS] = bin2bcd(tm->tm_hour); |
| 158 | buf[DT_DAYS] = bin2bcd(tm->tm_mday); |
| 159 | buf[DT_WEEKDAYS] = tm->tm_wday; |
| 160 | buf[DT_MONTHS] = bin2bcd(tm->tm_mon + 1); |
| 161 | buf[DT_YEARS] = bin2bcd(tm->tm_year % 100); |
| 162 | |
| 163 | return regmap_bulk_write(pcf85363->regmap, DT_100THS, |
| 164 | buf, len); |
| 165 | } |
| 166 | |
Alexandre Belloni | e5aac26 | 2018-03-01 01:42:19 +0100 | [diff] [blame^] | 167 | static int pcf85363_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 168 | { |
| 169 | struct pcf85363 *pcf85363 = dev_get_drvdata(dev); |
| 170 | unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1]; |
| 171 | unsigned int val; |
| 172 | int ret; |
| 173 | |
| 174 | ret = regmap_bulk_read(pcf85363->regmap, DT_SECOND_ALM1, buf, |
| 175 | sizeof(buf)); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | alrm->time.tm_sec = bcd2bin(buf[0]); |
| 180 | alrm->time.tm_min = bcd2bin(buf[1]); |
| 181 | alrm->time.tm_hour = bcd2bin(buf[2]); |
| 182 | alrm->time.tm_mday = bcd2bin(buf[3]); |
| 183 | alrm->time.tm_mon = bcd2bin(buf[4]) - 1; |
| 184 | |
| 185 | ret = regmap_read(pcf85363->regmap, CTRL_INTA_EN, &val); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | alrm->enabled = !!(val & INT_A1IE); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int _pcf85363_rtc_alarm_irq_enable(struct pcf85363 *pcf85363, unsigned |
| 195 | int enabled) |
| 196 | { |
| 197 | unsigned int alarm_flags = ALRM_SEC_A1E | ALRM_MIN_A1E | ALRM_HR_A1E | |
| 198 | ALRM_DAY_A1E | ALRM_MON_A1E; |
| 199 | int ret; |
| 200 | |
| 201 | ret = regmap_update_bits(pcf85363->regmap, DT_ALARM_EN, alarm_flags, |
| 202 | enabled ? alarm_flags : 0); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | |
| 206 | ret = regmap_update_bits(pcf85363->regmap, CTRL_INTA_EN, |
| 207 | INT_A1IE, enabled ? INT_A1IE : 0); |
| 208 | |
| 209 | if (ret || enabled) |
| 210 | return ret; |
| 211 | |
| 212 | /* clear current flags */ |
| 213 | return regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0); |
| 214 | } |
| 215 | |
| 216 | static int pcf85363_rtc_alarm_irq_enable(struct device *dev, |
| 217 | unsigned int enabled) |
| 218 | { |
| 219 | struct pcf85363 *pcf85363 = dev_get_drvdata(dev); |
| 220 | |
| 221 | return _pcf85363_rtc_alarm_irq_enable(pcf85363, enabled); |
| 222 | } |
| 223 | |
| 224 | static int pcf85363_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 225 | { |
| 226 | struct pcf85363 *pcf85363 = dev_get_drvdata(dev); |
| 227 | unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1]; |
| 228 | int ret; |
| 229 | |
| 230 | buf[0] = bin2bcd(alrm->time.tm_sec); |
| 231 | buf[1] = bin2bcd(alrm->time.tm_min); |
| 232 | buf[2] = bin2bcd(alrm->time.tm_hour); |
| 233 | buf[3] = bin2bcd(alrm->time.tm_mday); |
| 234 | buf[4] = bin2bcd(alrm->time.tm_mon + 1); |
| 235 | |
| 236 | /* |
| 237 | * Disable the alarm interrupt before changing the value to avoid |
| 238 | * spurious interrupts |
| 239 | */ |
| 240 | ret = _pcf85363_rtc_alarm_irq_enable(pcf85363, 0); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | |
| 244 | ret = regmap_bulk_write(pcf85363->regmap, DT_SECOND_ALM1, buf, |
| 245 | sizeof(buf)); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
| 249 | return _pcf85363_rtc_alarm_irq_enable(pcf85363, alrm->enabled); |
| 250 | } |
| 251 | |
| 252 | static irqreturn_t pcf85363_rtc_handle_irq(int irq, void *dev_id) |
| 253 | { |
| 254 | struct pcf85363 *pcf85363 = i2c_get_clientdata(dev_id); |
| 255 | unsigned int flags; |
| 256 | int err; |
| 257 | |
| 258 | err = regmap_read(pcf85363->regmap, CTRL_FLAGS, &flags); |
| 259 | if (err) |
| 260 | return IRQ_NONE; |
| 261 | |
| 262 | if (flags & FLAGS_A1F) { |
| 263 | rtc_update_irq(pcf85363->rtc, 1, RTC_IRQF | RTC_AF); |
| 264 | regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0); |
| 265 | return IRQ_HANDLED; |
| 266 | } |
| 267 | |
| 268 | return IRQ_NONE; |
| 269 | } |
| 270 | |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 271 | static const struct rtc_class_ops rtc_ops = { |
| 272 | .read_time = pcf85363_rtc_read_time, |
| 273 | .set_time = pcf85363_rtc_set_time, |
| 274 | }; |
| 275 | |
Alexandre Belloni | e5aac26 | 2018-03-01 01:42:19 +0100 | [diff] [blame^] | 276 | static const struct rtc_class_ops rtc_ops_alarm = { |
| 277 | .read_time = pcf85363_rtc_read_time, |
| 278 | .set_time = pcf85363_rtc_set_time, |
| 279 | .read_alarm = pcf85363_rtc_read_alarm, |
| 280 | .set_alarm = pcf85363_rtc_set_alarm, |
| 281 | .alarm_irq_enable = pcf85363_rtc_alarm_irq_enable, |
| 282 | }; |
| 283 | |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 284 | static int pcf85363_nvram_read(void *priv, unsigned int offset, void *val, |
| 285 | size_t bytes) |
| 286 | { |
| 287 | struct pcf85363 *pcf85363 = priv; |
| 288 | |
| 289 | return regmap_bulk_read(pcf85363->regmap, CTRL_RAM + offset, |
| 290 | val, bytes); |
| 291 | } |
| 292 | |
| 293 | static int pcf85363_nvram_write(void *priv, unsigned int offset, void *val, |
| 294 | size_t bytes) |
| 295 | { |
| 296 | struct pcf85363 *pcf85363 = priv; |
| 297 | |
| 298 | return regmap_bulk_write(pcf85363->regmap, CTRL_RAM + offset, |
| 299 | val, bytes); |
| 300 | } |
| 301 | |
| 302 | static const struct regmap_config regmap_config = { |
| 303 | .reg_bits = 8, |
| 304 | .val_bits = 8, |
Alexandre Belloni | c57849d | 2018-02-24 01:08:50 +0100 | [diff] [blame] | 305 | .max_register = 0x7f, |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 306 | }; |
| 307 | |
| 308 | static int pcf85363_probe(struct i2c_client *client, |
| 309 | const struct i2c_device_id *id) |
| 310 | { |
| 311 | struct pcf85363 *pcf85363; |
Alexandre Belloni | 0e7a412 | 2018-02-12 23:47:30 +0100 | [diff] [blame] | 312 | struct nvmem_config nvmem_cfg = { |
| 313 | .name = "pcf85363-", |
| 314 | .word_size = 1, |
| 315 | .stride = 1, |
| 316 | .size = NVRAM_SIZE, |
| 317 | .reg_read = pcf85363_nvram_read, |
| 318 | .reg_write = pcf85363_nvram_write, |
| 319 | }; |
Alexandre Belloni | 24849d1 | 2018-02-12 23:47:29 +0100 | [diff] [blame] | 320 | int ret; |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 321 | |
| 322 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 323 | return -ENODEV; |
| 324 | |
| 325 | pcf85363 = devm_kzalloc(&client->dev, sizeof(struct pcf85363), |
| 326 | GFP_KERNEL); |
| 327 | if (!pcf85363) |
| 328 | return -ENOMEM; |
| 329 | |
| 330 | pcf85363->regmap = devm_regmap_init_i2c(client, ®map_config); |
| 331 | if (IS_ERR(pcf85363->regmap)) { |
| 332 | dev_err(&client->dev, "regmap allocation failed\n"); |
| 333 | return PTR_ERR(pcf85363->regmap); |
| 334 | } |
| 335 | |
| 336 | pcf85363->dev = &client->dev; |
| 337 | i2c_set_clientdata(client, pcf85363); |
| 338 | |
| 339 | pcf85363->rtc = devm_rtc_allocate_device(pcf85363->dev); |
| 340 | if (IS_ERR(pcf85363->rtc)) |
| 341 | return PTR_ERR(pcf85363->rtc); |
| 342 | |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 343 | pcf85363->rtc->ops = &rtc_ops; |
| 344 | |
Alexandre Belloni | e5aac26 | 2018-03-01 01:42:19 +0100 | [diff] [blame^] | 345 | if (client->irq > 0) { |
| 346 | regmap_write(pcf85363->regmap, CTRL_FLAGS, 0); |
| 347 | regmap_update_bits(pcf85363->regmap, CTRL_PIN_IO, |
| 348 | PIN_IO_INTA_OUT, PIN_IO_INTAPM); |
| 349 | ret = devm_request_threaded_irq(pcf85363->dev, client->irq, |
| 350 | NULL, pcf85363_rtc_handle_irq, |
| 351 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 352 | "pcf85363", client); |
| 353 | if (ret) |
| 354 | dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); |
| 355 | else |
| 356 | pcf85363->rtc->ops = &rtc_ops_alarm; |
| 357 | } |
| 358 | |
Alexandre Belloni | 24849d1 | 2018-02-12 23:47:29 +0100 | [diff] [blame] | 359 | ret = rtc_register_device(pcf85363->rtc); |
| 360 | |
Alexandre Belloni | 0e7a412 | 2018-02-12 23:47:30 +0100 | [diff] [blame] | 361 | nvmem_cfg.priv = pcf85363; |
| 362 | rtc_nvmem_register(pcf85363->rtc, &nvmem_cfg); |
Alexandre Belloni | 24849d1 | 2018-02-12 23:47:29 +0100 | [diff] [blame] | 363 | |
| 364 | return ret; |
Eric Nelson | a9687aa | 2017-11-01 08:01:20 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static const struct of_device_id dev_ids[] = { |
| 368 | { .compatible = "nxp,pcf85363" }, |
| 369 | {} |
| 370 | }; |
| 371 | MODULE_DEVICE_TABLE(of, dev_ids); |
| 372 | |
| 373 | static struct i2c_driver pcf85363_driver = { |
| 374 | .driver = { |
| 375 | .name = "pcf85363", |
| 376 | .of_match_table = of_match_ptr(dev_ids), |
| 377 | }, |
| 378 | .probe = pcf85363_probe, |
| 379 | }; |
| 380 | |
| 381 | module_i2c_driver(pcf85363_driver); |
| 382 | |
| 383 | MODULE_AUTHOR("Eric Nelson"); |
| 384 | MODULE_DESCRIPTION("pcf85363 I2C RTC driver"); |
| 385 | MODULE_LICENSE("GPL"); |