1*56573ca7SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0 2a9687aa2SEric Nelson /* 3a9687aa2SEric Nelson * drivers/rtc/rtc-pcf85363.c 4a9687aa2SEric Nelson * 5a9687aa2SEric Nelson * Driver for NXP PCF85363 real-time clock. 6a9687aa2SEric Nelson * 7a9687aa2SEric Nelson * Copyright (C) 2017 Eric Nelson 8a9687aa2SEric Nelson */ 9a9687aa2SEric Nelson #include <linux/module.h> 10a9687aa2SEric Nelson #include <linux/i2c.h> 11a9687aa2SEric Nelson #include <linux/slab.h> 12a9687aa2SEric Nelson #include <linux/rtc.h> 13a9687aa2SEric Nelson #include <linux/init.h> 14a9687aa2SEric Nelson #include <linux/err.h> 15a9687aa2SEric Nelson #include <linux/errno.h> 16a9687aa2SEric Nelson #include <linux/bcd.h> 17a9687aa2SEric Nelson #include <linux/of.h> 18a9687aa2SEric Nelson #include <linux/of_device.h> 19a9687aa2SEric Nelson #include <linux/regmap.h> 20a9687aa2SEric Nelson 21a9687aa2SEric Nelson /* 22a9687aa2SEric Nelson * Date/Time registers 23a9687aa2SEric Nelson */ 24a9687aa2SEric Nelson #define DT_100THS 0x00 25a9687aa2SEric Nelson #define DT_SECS 0x01 26a9687aa2SEric Nelson #define DT_MINUTES 0x02 27a9687aa2SEric Nelson #define DT_HOURS 0x03 28a9687aa2SEric Nelson #define DT_DAYS 0x04 29a9687aa2SEric Nelson #define DT_WEEKDAYS 0x05 30a9687aa2SEric Nelson #define DT_MONTHS 0x06 31a9687aa2SEric Nelson #define DT_YEARS 0x07 32a9687aa2SEric Nelson 33a9687aa2SEric Nelson /* 34a9687aa2SEric Nelson * Alarm registers 35a9687aa2SEric Nelson */ 36a9687aa2SEric Nelson #define DT_SECOND_ALM1 0x08 37a9687aa2SEric Nelson #define DT_MINUTE_ALM1 0x09 38a9687aa2SEric Nelson #define DT_HOUR_ALM1 0x0a 39a9687aa2SEric Nelson #define DT_DAY_ALM1 0x0b 40a9687aa2SEric Nelson #define DT_MONTH_ALM1 0x0c 41a9687aa2SEric Nelson #define DT_MINUTE_ALM2 0x0d 42a9687aa2SEric Nelson #define DT_HOUR_ALM2 0x0e 43a9687aa2SEric Nelson #define DT_WEEKDAY_ALM2 0x0f 44a9687aa2SEric Nelson #define DT_ALARM_EN 0x10 45a9687aa2SEric Nelson 46a9687aa2SEric Nelson /* 47a9687aa2SEric Nelson * Time stamp registers 48a9687aa2SEric Nelson */ 49a9687aa2SEric Nelson #define DT_TIMESTAMP1 0x11 50a9687aa2SEric Nelson #define DT_TIMESTAMP2 0x17 51a9687aa2SEric Nelson #define DT_TIMESTAMP3 0x1d 52a9687aa2SEric Nelson #define DT_TS_MODE 0x23 53a9687aa2SEric Nelson 54a9687aa2SEric Nelson /* 55a9687aa2SEric Nelson * control registers 56a9687aa2SEric Nelson */ 57a9687aa2SEric Nelson #define CTRL_OFFSET 0x24 58a9687aa2SEric Nelson #define CTRL_OSCILLATOR 0x25 59a9687aa2SEric Nelson #define CTRL_BATTERY 0x26 60a9687aa2SEric Nelson #define CTRL_PIN_IO 0x27 61a9687aa2SEric Nelson #define CTRL_FUNCTION 0x28 62a9687aa2SEric Nelson #define CTRL_INTA_EN 0x29 63a9687aa2SEric Nelson #define CTRL_INTB_EN 0x2a 64a9687aa2SEric Nelson #define CTRL_FLAGS 0x2b 65a9687aa2SEric Nelson #define CTRL_RAMBYTE 0x2c 66a9687aa2SEric Nelson #define CTRL_WDOG 0x2d 67a9687aa2SEric Nelson #define CTRL_STOP_EN 0x2e 68a9687aa2SEric Nelson #define CTRL_RESETS 0x2f 69a9687aa2SEric Nelson #define CTRL_RAM 0x40 70a9687aa2SEric Nelson 71e5aac267SAlexandre Belloni #define ALRM_SEC_A1E BIT(0) 72e5aac267SAlexandre Belloni #define ALRM_MIN_A1E BIT(1) 73e5aac267SAlexandre Belloni #define ALRM_HR_A1E BIT(2) 74e5aac267SAlexandre Belloni #define ALRM_DAY_A1E BIT(3) 75e5aac267SAlexandre Belloni #define ALRM_MON_A1E BIT(4) 76e5aac267SAlexandre Belloni #define ALRM_MIN_A2E BIT(5) 77e5aac267SAlexandre Belloni #define ALRM_HR_A2E BIT(6) 78e5aac267SAlexandre Belloni #define ALRM_DAY_A2E BIT(7) 79e5aac267SAlexandre Belloni 80e5aac267SAlexandre Belloni #define INT_WDIE BIT(0) 81e5aac267SAlexandre Belloni #define INT_BSIE BIT(1) 82e5aac267SAlexandre Belloni #define INT_TSRIE BIT(2) 83e5aac267SAlexandre Belloni #define INT_A2IE BIT(3) 84e5aac267SAlexandre Belloni #define INT_A1IE BIT(4) 85e5aac267SAlexandre Belloni #define INT_OIE BIT(5) 86e5aac267SAlexandre Belloni #define INT_PIE BIT(6) 87e5aac267SAlexandre Belloni #define INT_ILP BIT(7) 88e5aac267SAlexandre Belloni 89e5aac267SAlexandre Belloni #define FLAGS_TSR1F BIT(0) 90e5aac267SAlexandre Belloni #define FLAGS_TSR2F BIT(1) 91e5aac267SAlexandre Belloni #define FLAGS_TSR3F BIT(2) 92e5aac267SAlexandre Belloni #define FLAGS_BSF BIT(3) 93e5aac267SAlexandre Belloni #define FLAGS_WDF BIT(4) 94e5aac267SAlexandre Belloni #define FLAGS_A1F BIT(5) 95e5aac267SAlexandre Belloni #define FLAGS_A2F BIT(6) 96e5aac267SAlexandre Belloni #define FLAGS_PIF BIT(7) 97e5aac267SAlexandre Belloni 98e5aac267SAlexandre Belloni #define PIN_IO_INTAPM GENMASK(1, 0) 99e5aac267SAlexandre Belloni #define PIN_IO_INTA_CLK 0 100e5aac267SAlexandre Belloni #define PIN_IO_INTA_BAT 1 101e5aac267SAlexandre Belloni #define PIN_IO_INTA_OUT 2 102e5aac267SAlexandre Belloni #define PIN_IO_INTA_HIZ 3 103e5aac267SAlexandre Belloni 104188306acSAlexandre Belloni #define STOP_EN_STOP BIT(0) 105188306acSAlexandre Belloni 106188306acSAlexandre Belloni #define RESET_CPR 0xa4 107188306acSAlexandre Belloni 108a9687aa2SEric Nelson #define NVRAM_SIZE 0x40 109a9687aa2SEric Nelson 110a9687aa2SEric Nelson struct pcf85363 { 111a9687aa2SEric Nelson struct rtc_device *rtc; 112a9687aa2SEric Nelson struct regmap *regmap; 113a9687aa2SEric Nelson }; 114a9687aa2SEric Nelson 115fc979933SBiju Das struct pcf85x63_config { 116fc979933SBiju Das struct regmap_config regmap; 117fc979933SBiju Das unsigned int num_nvram; 118fc979933SBiju Das }; 119fc979933SBiju Das 120a9687aa2SEric Nelson static int pcf85363_rtc_read_time(struct device *dev, struct rtc_time *tm) 121a9687aa2SEric Nelson { 122a9687aa2SEric Nelson struct pcf85363 *pcf85363 = dev_get_drvdata(dev); 123a9687aa2SEric Nelson unsigned char buf[DT_YEARS + 1]; 124a9687aa2SEric Nelson int ret, len = sizeof(buf); 125a9687aa2SEric Nelson 126a9687aa2SEric Nelson /* read the RTC date and time registers all at once */ 127a9687aa2SEric Nelson ret = regmap_bulk_read(pcf85363->regmap, DT_100THS, buf, len); 128a9687aa2SEric Nelson if (ret) { 129a9687aa2SEric Nelson dev_err(dev, "%s: error %d\n", __func__, ret); 130a9687aa2SEric Nelson return ret; 131a9687aa2SEric Nelson } 132a9687aa2SEric Nelson 133a9687aa2SEric Nelson tm->tm_year = bcd2bin(buf[DT_YEARS]); 134a9687aa2SEric Nelson /* adjust for 1900 base of rtc_time */ 135a9687aa2SEric Nelson tm->tm_year += 100; 136a9687aa2SEric Nelson 137a9687aa2SEric Nelson tm->tm_wday = buf[DT_WEEKDAYS] & 7; 138a9687aa2SEric Nelson buf[DT_SECS] &= 0x7F; 139a9687aa2SEric Nelson tm->tm_sec = bcd2bin(buf[DT_SECS]); 140a9687aa2SEric Nelson buf[DT_MINUTES] &= 0x7F; 141a9687aa2SEric Nelson tm->tm_min = bcd2bin(buf[DT_MINUTES]); 142a9687aa2SEric Nelson tm->tm_hour = bcd2bin(buf[DT_HOURS]); 143a9687aa2SEric Nelson tm->tm_mday = bcd2bin(buf[DT_DAYS]); 144a9687aa2SEric Nelson tm->tm_mon = bcd2bin(buf[DT_MONTHS]) - 1; 145a9687aa2SEric Nelson 146a9687aa2SEric Nelson return 0; 147a9687aa2SEric Nelson } 148a9687aa2SEric Nelson 149a9687aa2SEric Nelson static int pcf85363_rtc_set_time(struct device *dev, struct rtc_time *tm) 150a9687aa2SEric Nelson { 151a9687aa2SEric Nelson struct pcf85363 *pcf85363 = dev_get_drvdata(dev); 152188306acSAlexandre Belloni unsigned char tmp[11]; 153188306acSAlexandre Belloni unsigned char *buf = &tmp[2]; 154188306acSAlexandre Belloni int ret; 155188306acSAlexandre Belloni 156188306acSAlexandre Belloni tmp[0] = STOP_EN_STOP; 157188306acSAlexandre Belloni tmp[1] = RESET_CPR; 158a9687aa2SEric Nelson 159a9687aa2SEric Nelson buf[DT_100THS] = 0; 160a9687aa2SEric Nelson buf[DT_SECS] = bin2bcd(tm->tm_sec); 161a9687aa2SEric Nelson buf[DT_MINUTES] = bin2bcd(tm->tm_min); 162a9687aa2SEric Nelson buf[DT_HOURS] = bin2bcd(tm->tm_hour); 163a9687aa2SEric Nelson buf[DT_DAYS] = bin2bcd(tm->tm_mday); 164a9687aa2SEric Nelson buf[DT_WEEKDAYS] = tm->tm_wday; 165a9687aa2SEric Nelson buf[DT_MONTHS] = bin2bcd(tm->tm_mon + 1); 166a9687aa2SEric Nelson buf[DT_YEARS] = bin2bcd(tm->tm_year % 100); 167a9687aa2SEric Nelson 168188306acSAlexandre Belloni ret = regmap_bulk_write(pcf85363->regmap, CTRL_STOP_EN, 169188306acSAlexandre Belloni tmp, sizeof(tmp)); 170188306acSAlexandre Belloni if (ret) 171188306acSAlexandre Belloni return ret; 172188306acSAlexandre Belloni 173188306acSAlexandre Belloni return regmap_write(pcf85363->regmap, CTRL_STOP_EN, 0); 174a9687aa2SEric Nelson } 175a9687aa2SEric Nelson 176e5aac267SAlexandre Belloni static int pcf85363_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 177e5aac267SAlexandre Belloni { 178e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = dev_get_drvdata(dev); 179e5aac267SAlexandre Belloni unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1]; 180e5aac267SAlexandre Belloni unsigned int val; 181e5aac267SAlexandre Belloni int ret; 182e5aac267SAlexandre Belloni 183e5aac267SAlexandre Belloni ret = regmap_bulk_read(pcf85363->regmap, DT_SECOND_ALM1, buf, 184e5aac267SAlexandre Belloni sizeof(buf)); 185e5aac267SAlexandre Belloni if (ret) 186e5aac267SAlexandre Belloni return ret; 187e5aac267SAlexandre Belloni 188e5aac267SAlexandre Belloni alrm->time.tm_sec = bcd2bin(buf[0]); 189e5aac267SAlexandre Belloni alrm->time.tm_min = bcd2bin(buf[1]); 190e5aac267SAlexandre Belloni alrm->time.tm_hour = bcd2bin(buf[2]); 191e5aac267SAlexandre Belloni alrm->time.tm_mday = bcd2bin(buf[3]); 192e5aac267SAlexandre Belloni alrm->time.tm_mon = bcd2bin(buf[4]) - 1; 193e5aac267SAlexandre Belloni 194e5aac267SAlexandre Belloni ret = regmap_read(pcf85363->regmap, CTRL_INTA_EN, &val); 195e5aac267SAlexandre Belloni if (ret) 196e5aac267SAlexandre Belloni return ret; 197e5aac267SAlexandre Belloni 198e5aac267SAlexandre Belloni alrm->enabled = !!(val & INT_A1IE); 199e5aac267SAlexandre Belloni 200e5aac267SAlexandre Belloni return 0; 201e5aac267SAlexandre Belloni } 202e5aac267SAlexandre Belloni 203e5aac267SAlexandre Belloni static int _pcf85363_rtc_alarm_irq_enable(struct pcf85363 *pcf85363, unsigned 204e5aac267SAlexandre Belloni int enabled) 205e5aac267SAlexandre Belloni { 206e5aac267SAlexandre Belloni unsigned int alarm_flags = ALRM_SEC_A1E | ALRM_MIN_A1E | ALRM_HR_A1E | 207e5aac267SAlexandre Belloni ALRM_DAY_A1E | ALRM_MON_A1E; 208e5aac267SAlexandre Belloni int ret; 209e5aac267SAlexandre Belloni 210e5aac267SAlexandre Belloni ret = regmap_update_bits(pcf85363->regmap, DT_ALARM_EN, alarm_flags, 211e5aac267SAlexandre Belloni enabled ? alarm_flags : 0); 212e5aac267SAlexandre Belloni if (ret) 213e5aac267SAlexandre Belloni return ret; 214e5aac267SAlexandre Belloni 215e5aac267SAlexandre Belloni ret = regmap_update_bits(pcf85363->regmap, CTRL_INTA_EN, 216e5aac267SAlexandre Belloni INT_A1IE, enabled ? INT_A1IE : 0); 217e5aac267SAlexandre Belloni 218e5aac267SAlexandre Belloni if (ret || enabled) 219e5aac267SAlexandre Belloni return ret; 220e5aac267SAlexandre Belloni 221e5aac267SAlexandre Belloni /* clear current flags */ 222e5aac267SAlexandre Belloni return regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0); 223e5aac267SAlexandre Belloni } 224e5aac267SAlexandre Belloni 225e5aac267SAlexandre Belloni static int pcf85363_rtc_alarm_irq_enable(struct device *dev, 226e5aac267SAlexandre Belloni unsigned int enabled) 227e5aac267SAlexandre Belloni { 228e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = dev_get_drvdata(dev); 229e5aac267SAlexandre Belloni 230e5aac267SAlexandre Belloni return _pcf85363_rtc_alarm_irq_enable(pcf85363, enabled); 231e5aac267SAlexandre Belloni } 232e5aac267SAlexandre Belloni 233e5aac267SAlexandre Belloni static int pcf85363_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 234e5aac267SAlexandre Belloni { 235e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = dev_get_drvdata(dev); 236e5aac267SAlexandre Belloni unsigned char buf[DT_MONTH_ALM1 - DT_SECOND_ALM1 + 1]; 237e5aac267SAlexandre Belloni int ret; 238e5aac267SAlexandre Belloni 239e5aac267SAlexandre Belloni buf[0] = bin2bcd(alrm->time.tm_sec); 240e5aac267SAlexandre Belloni buf[1] = bin2bcd(alrm->time.tm_min); 241e5aac267SAlexandre Belloni buf[2] = bin2bcd(alrm->time.tm_hour); 242e5aac267SAlexandre Belloni buf[3] = bin2bcd(alrm->time.tm_mday); 243e5aac267SAlexandre Belloni buf[4] = bin2bcd(alrm->time.tm_mon + 1); 244e5aac267SAlexandre Belloni 245e5aac267SAlexandre Belloni /* 246e5aac267SAlexandre Belloni * Disable the alarm interrupt before changing the value to avoid 247e5aac267SAlexandre Belloni * spurious interrupts 248e5aac267SAlexandre Belloni */ 249e5aac267SAlexandre Belloni ret = _pcf85363_rtc_alarm_irq_enable(pcf85363, 0); 250e5aac267SAlexandre Belloni if (ret) 251e5aac267SAlexandre Belloni return ret; 252e5aac267SAlexandre Belloni 253e5aac267SAlexandre Belloni ret = regmap_bulk_write(pcf85363->regmap, DT_SECOND_ALM1, buf, 254e5aac267SAlexandre Belloni sizeof(buf)); 255e5aac267SAlexandre Belloni if (ret) 256e5aac267SAlexandre Belloni return ret; 257e5aac267SAlexandre Belloni 258e5aac267SAlexandre Belloni return _pcf85363_rtc_alarm_irq_enable(pcf85363, alrm->enabled); 259e5aac267SAlexandre Belloni } 260e5aac267SAlexandre Belloni 261e5aac267SAlexandre Belloni static irqreturn_t pcf85363_rtc_handle_irq(int irq, void *dev_id) 262e5aac267SAlexandre Belloni { 263e5aac267SAlexandre Belloni struct pcf85363 *pcf85363 = i2c_get_clientdata(dev_id); 264e5aac267SAlexandre Belloni unsigned int flags; 265e5aac267SAlexandre Belloni int err; 266e5aac267SAlexandre Belloni 267e5aac267SAlexandre Belloni err = regmap_read(pcf85363->regmap, CTRL_FLAGS, &flags); 268e5aac267SAlexandre Belloni if (err) 269e5aac267SAlexandre Belloni return IRQ_NONE; 270e5aac267SAlexandre Belloni 271e5aac267SAlexandre Belloni if (flags & FLAGS_A1F) { 272e5aac267SAlexandre Belloni rtc_update_irq(pcf85363->rtc, 1, RTC_IRQF | RTC_AF); 273e5aac267SAlexandre Belloni regmap_update_bits(pcf85363->regmap, CTRL_FLAGS, FLAGS_A1F, 0); 274e5aac267SAlexandre Belloni return IRQ_HANDLED; 275e5aac267SAlexandre Belloni } 276e5aac267SAlexandre Belloni 277e5aac267SAlexandre Belloni return IRQ_NONE; 278e5aac267SAlexandre Belloni } 279e5aac267SAlexandre Belloni 280a9687aa2SEric Nelson static const struct rtc_class_ops rtc_ops = { 281a9687aa2SEric Nelson .read_time = pcf85363_rtc_read_time, 282a9687aa2SEric Nelson .set_time = pcf85363_rtc_set_time, 283a9687aa2SEric Nelson }; 284a9687aa2SEric Nelson 285e5aac267SAlexandre Belloni static const struct rtc_class_ops rtc_ops_alarm = { 286e5aac267SAlexandre Belloni .read_time = pcf85363_rtc_read_time, 287e5aac267SAlexandre Belloni .set_time = pcf85363_rtc_set_time, 288e5aac267SAlexandre Belloni .read_alarm = pcf85363_rtc_read_alarm, 289e5aac267SAlexandre Belloni .set_alarm = pcf85363_rtc_set_alarm, 290e5aac267SAlexandre Belloni .alarm_irq_enable = pcf85363_rtc_alarm_irq_enable, 291e5aac267SAlexandre Belloni }; 292e5aac267SAlexandre Belloni 293a9687aa2SEric Nelson static int pcf85363_nvram_read(void *priv, unsigned int offset, void *val, 294a9687aa2SEric Nelson size_t bytes) 295a9687aa2SEric Nelson { 296a9687aa2SEric Nelson struct pcf85363 *pcf85363 = priv; 297a9687aa2SEric Nelson 298a9687aa2SEric Nelson return regmap_bulk_read(pcf85363->regmap, CTRL_RAM + offset, 299a9687aa2SEric Nelson val, bytes); 300a9687aa2SEric Nelson } 301a9687aa2SEric Nelson 302a9687aa2SEric Nelson static int pcf85363_nvram_write(void *priv, unsigned int offset, void *val, 303a9687aa2SEric Nelson size_t bytes) 304a9687aa2SEric Nelson { 305a9687aa2SEric Nelson struct pcf85363 *pcf85363 = priv; 306a9687aa2SEric Nelson 307a9687aa2SEric Nelson return regmap_bulk_write(pcf85363->regmap, CTRL_RAM + offset, 308a9687aa2SEric Nelson val, bytes); 309a9687aa2SEric Nelson } 310a9687aa2SEric Nelson 311fc979933SBiju Das static int pcf85x63_nvram_read(void *priv, unsigned int offset, void *val, 312fc979933SBiju Das size_t bytes) 313fc979933SBiju Das { 314fc979933SBiju Das struct pcf85363 *pcf85363 = priv; 315fc979933SBiju Das unsigned int tmp_val; 316fc979933SBiju Das int ret; 317fc979933SBiju Das 318fc979933SBiju Das ret = regmap_read(pcf85363->regmap, CTRL_RAMBYTE, &tmp_val); 319fc979933SBiju Das (*(unsigned char *) val) = (unsigned char) tmp_val; 320fc979933SBiju Das 321fc979933SBiju Das return ret; 322fc979933SBiju Das } 323fc979933SBiju Das 324fc979933SBiju Das static int pcf85x63_nvram_write(void *priv, unsigned int offset, void *val, 325fc979933SBiju Das size_t bytes) 326fc979933SBiju Das { 327fc979933SBiju Das struct pcf85363 *pcf85363 = priv; 328fc979933SBiju Das unsigned char tmp_val; 329fc979933SBiju Das 330fc979933SBiju Das tmp_val = *((unsigned char *)val); 331fc979933SBiju Das return regmap_write(pcf85363->regmap, CTRL_RAMBYTE, 332fc979933SBiju Das (unsigned int)tmp_val); 333fc979933SBiju Das } 334fc979933SBiju Das 335fc979933SBiju Das static const struct pcf85x63_config pcf_85263_config = { 336fc979933SBiju Das .regmap = { 337fc979933SBiju Das .reg_bits = 8, 338fc979933SBiju Das .val_bits = 8, 339fc979933SBiju Das .max_register = 0x2f, 340fc979933SBiju Das }, 341fc979933SBiju Das .num_nvram = 1 342fc979933SBiju Das }; 343fc979933SBiju Das 344fc979933SBiju Das static const struct pcf85x63_config pcf_85363_config = { 345fc979933SBiju Das .regmap = { 346a9687aa2SEric Nelson .reg_bits = 8, 347a9687aa2SEric Nelson .val_bits = 8, 348c57849ddSAlexandre Belloni .max_register = 0x7f, 349fc979933SBiju Das }, 350fc979933SBiju Das .num_nvram = 2 351a9687aa2SEric Nelson }; 352a9687aa2SEric Nelson 353a9687aa2SEric Nelson static int pcf85363_probe(struct i2c_client *client, 354a9687aa2SEric Nelson const struct i2c_device_id *id) 355a9687aa2SEric Nelson { 356a9687aa2SEric Nelson struct pcf85363 *pcf85363; 357fc979933SBiju Das const struct pcf85x63_config *config = &pcf_85363_config; 358fc979933SBiju Das const void *data = of_device_get_match_data(&client->dev); 359fc979933SBiju Das static struct nvmem_config nvmem_cfg[] = { 360fc979933SBiju Das { 361fc979933SBiju Das .name = "pcf85x63-", 362fc979933SBiju Das .word_size = 1, 363fc979933SBiju Das .stride = 1, 364fc979933SBiju Das .size = 1, 365fc979933SBiju Das .reg_read = pcf85x63_nvram_read, 366fc979933SBiju Das .reg_write = pcf85x63_nvram_write, 367fc979933SBiju Das }, { 3680e7a412fSAlexandre Belloni .name = "pcf85363-", 3690e7a412fSAlexandre Belloni .word_size = 1, 3700e7a412fSAlexandre Belloni .stride = 1, 3710e7a412fSAlexandre Belloni .size = NVRAM_SIZE, 3720e7a412fSAlexandre Belloni .reg_read = pcf85363_nvram_read, 3730e7a412fSAlexandre Belloni .reg_write = pcf85363_nvram_write, 374fc979933SBiju Das }, 3750e7a412fSAlexandre Belloni }; 376fc979933SBiju Das int ret, i; 377fc979933SBiju Das 378fc979933SBiju Das if (data) 379fc979933SBiju Das config = data; 380a9687aa2SEric Nelson 381a9687aa2SEric Nelson pcf85363 = devm_kzalloc(&client->dev, sizeof(struct pcf85363), 382a9687aa2SEric Nelson GFP_KERNEL); 383a9687aa2SEric Nelson if (!pcf85363) 384a9687aa2SEric Nelson return -ENOMEM; 385a9687aa2SEric Nelson 386fc979933SBiju Das pcf85363->regmap = devm_regmap_init_i2c(client, &config->regmap); 387a9687aa2SEric Nelson if (IS_ERR(pcf85363->regmap)) { 388a9687aa2SEric Nelson dev_err(&client->dev, "regmap allocation failed\n"); 389a9687aa2SEric Nelson return PTR_ERR(pcf85363->regmap); 390a9687aa2SEric Nelson } 391a9687aa2SEric Nelson 392a9687aa2SEric Nelson i2c_set_clientdata(client, pcf85363); 393a9687aa2SEric Nelson 3948f7b1d71SAlexandre Belloni pcf85363->rtc = devm_rtc_allocate_device(&client->dev); 395a9687aa2SEric Nelson if (IS_ERR(pcf85363->rtc)) 396a9687aa2SEric Nelson return PTR_ERR(pcf85363->rtc); 397a9687aa2SEric Nelson 398a9687aa2SEric Nelson pcf85363->rtc->ops = &rtc_ops; 399c0ec8319SAlexandre Belloni pcf85363->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 400c0ec8319SAlexandre Belloni pcf85363->rtc->range_max = RTC_TIMESTAMP_END_2099; 401a9687aa2SEric Nelson 402e5aac267SAlexandre Belloni if (client->irq > 0) { 403e5aac267SAlexandre Belloni regmap_write(pcf85363->regmap, CTRL_FLAGS, 0); 404e5aac267SAlexandre Belloni regmap_update_bits(pcf85363->regmap, CTRL_PIN_IO, 405e5aac267SAlexandre Belloni PIN_IO_INTA_OUT, PIN_IO_INTAPM); 4068f7b1d71SAlexandre Belloni ret = devm_request_threaded_irq(&client->dev, client->irq, 407e5aac267SAlexandre Belloni NULL, pcf85363_rtc_handle_irq, 408e5aac267SAlexandre Belloni IRQF_TRIGGER_LOW | IRQF_ONESHOT, 409e5aac267SAlexandre Belloni "pcf85363", client); 410e5aac267SAlexandre Belloni if (ret) 411e5aac267SAlexandre Belloni dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 412e5aac267SAlexandre Belloni else 413e5aac267SAlexandre Belloni pcf85363->rtc->ops = &rtc_ops_alarm; 414e5aac267SAlexandre Belloni } 415e5aac267SAlexandre Belloni 41624849d17SAlexandre Belloni ret = rtc_register_device(pcf85363->rtc); 41724849d17SAlexandre Belloni 418fc979933SBiju Das for (i = 0; i < config->num_nvram; i++) { 419fc979933SBiju Das nvmem_cfg[i].priv = pcf85363; 420fc979933SBiju Das rtc_nvmem_register(pcf85363->rtc, &nvmem_cfg[i]); 421fc979933SBiju Das } 42224849d17SAlexandre Belloni 42324849d17SAlexandre Belloni return ret; 424a9687aa2SEric Nelson } 425a9687aa2SEric Nelson 426a9687aa2SEric Nelson static const struct of_device_id dev_ids[] = { 427fc979933SBiju Das { .compatible = "nxp,pcf85263", .data = &pcf_85263_config }, 428fc979933SBiju Das { .compatible = "nxp,pcf85363", .data = &pcf_85363_config }, 429fc979933SBiju Das { /* sentinel */ } 430a9687aa2SEric Nelson }; 431a9687aa2SEric Nelson MODULE_DEVICE_TABLE(of, dev_ids); 432a9687aa2SEric Nelson 433a9687aa2SEric Nelson static struct i2c_driver pcf85363_driver = { 434a9687aa2SEric Nelson .driver = { 435a9687aa2SEric Nelson .name = "pcf85363", 436a9687aa2SEric Nelson .of_match_table = of_match_ptr(dev_ids), 437a9687aa2SEric Nelson }, 438a9687aa2SEric Nelson .probe = pcf85363_probe, 439a9687aa2SEric Nelson }; 440a9687aa2SEric Nelson 441a9687aa2SEric Nelson module_i2c_driver(pcf85363_driver); 442a9687aa2SEric Nelson 443a9687aa2SEric Nelson MODULE_AUTHOR("Eric Nelson"); 444fc979933SBiju Das MODULE_DESCRIPTION("pcf85263/pcf85363 I2C RTC driver"); 445a9687aa2SEric Nelson MODULE_LICENSE("GPL"); 446