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