1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 218cb6368SRenaud Cerrato /* 3cee2cc21SAkinobu Mita * An I2C and SPI driver for the NXP PCF2127/29 RTC 418cb6368SRenaud Cerrato * Copyright 2013 Til-Technologies 518cb6368SRenaud Cerrato * 618cb6368SRenaud Cerrato * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> 718cb6368SRenaud Cerrato * 818cb6368SRenaud Cerrato * based on the other drivers in this same directory. 918cb6368SRenaud Cerrato * 10cee2cc21SAkinobu Mita * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf 1118cb6368SRenaud Cerrato */ 1218cb6368SRenaud Cerrato 1318cb6368SRenaud Cerrato #include <linux/i2c.h> 149408ec1aSAkinobu Mita #include <linux/spi/spi.h> 1518cb6368SRenaud Cerrato #include <linux/bcd.h> 1618cb6368SRenaud Cerrato #include <linux/rtc.h> 1718cb6368SRenaud Cerrato #include <linux/slab.h> 1818cb6368SRenaud Cerrato #include <linux/module.h> 1918cb6368SRenaud Cerrato #include <linux/of.h> 20907b3262SAkinobu Mita #include <linux/regmap.h> 2118cb6368SRenaud Cerrato 22*bbfe3a7aSBruno Thomsen /* Control register 1 */ 23*bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL1 0x00 24*bbfe3a7aSBruno Thomsen /* Control register 2 */ 25*bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL2 0x01 26*bbfe3a7aSBruno Thomsen /* Control register 3 */ 27*bbfe3a7aSBruno Thomsen #define PCF2127_REG_CTRL3 0x02 28*bbfe3a7aSBruno Thomsen #define PCF2127_BIT_CTRL3_BLF BIT(2) 29*bbfe3a7aSBruno Thomsen /* Time and date registers */ 30*bbfe3a7aSBruno Thomsen #define PCF2127_REG_SC 0x03 31*bbfe3a7aSBruno Thomsen #define PCF2127_BIT_SC_OSF BIT(7) 32*bbfe3a7aSBruno Thomsen #define PCF2127_REG_MN 0x04 33*bbfe3a7aSBruno Thomsen #define PCF2127_REG_HR 0x05 34*bbfe3a7aSBruno Thomsen #define PCF2127_REG_DM 0x06 35*bbfe3a7aSBruno Thomsen #define PCF2127_REG_DW 0x07 36*bbfe3a7aSBruno Thomsen #define PCF2127_REG_MO 0x08 37*bbfe3a7aSBruno Thomsen #define PCF2127_REG_YR 0x09 38*bbfe3a7aSBruno Thomsen /* 39*bbfe3a7aSBruno Thomsen * RAM registers 40*bbfe3a7aSBruno Thomsen * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is 41*bbfe3a7aSBruno Thomsen * battery backed and can survive a power outage. 42*bbfe3a7aSBruno Thomsen * PCF2129 doesn't have this feature. 43*bbfe3a7aSBruno Thomsen */ 44*bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_ADDR_MSB 0x1A 45*bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_WRT_CMD 0x1C 46*bbfe3a7aSBruno Thomsen #define PCF2127_REG_RAM_RD_CMD 0x1D 47f97cfddcSUwe Kleine-König 48653ebd75SAndrea Scian 4918cb6368SRenaud Cerrato struct pcf2127 { 5018cb6368SRenaud Cerrato struct rtc_device *rtc; 51907b3262SAkinobu Mita struct regmap *regmap; 5218cb6368SRenaud Cerrato }; 5318cb6368SRenaud Cerrato 5418cb6368SRenaud Cerrato /* 5518cb6368SRenaud Cerrato * In the routines that deal directly with the pcf2127 hardware, we use 5618cb6368SRenaud Cerrato * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. 5718cb6368SRenaud Cerrato */ 58907b3262SAkinobu Mita static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) 5918cb6368SRenaud Cerrato { 60907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 61907b3262SAkinobu Mita unsigned char buf[10]; 62907b3262SAkinobu Mita int ret; 633769a375SSean Nyekjaer int i; 6418cb6368SRenaud Cerrato 653769a375SSean Nyekjaer for (i = 0; i <= PCF2127_REG_CTRL3; i++) { 663769a375SSean Nyekjaer ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1 + i, 673769a375SSean Nyekjaer (unsigned int *)(buf + i)); 683769a375SSean Nyekjaer if (ret) { 693769a375SSean Nyekjaer dev_err(dev, "%s: read error\n", __func__); 703769a375SSean Nyekjaer return ret; 713769a375SSean Nyekjaer } 723769a375SSean Nyekjaer } 733769a375SSean Nyekjaer 743769a375SSean Nyekjaer ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_SC, 753769a375SSean Nyekjaer (buf + PCF2127_REG_SC), 763769a375SSean Nyekjaer ARRAY_SIZE(buf) - PCF2127_REG_SC); 77907b3262SAkinobu Mita if (ret) { 78907b3262SAkinobu Mita dev_err(dev, "%s: read error\n", __func__); 79907b3262SAkinobu Mita return ret; 8018cb6368SRenaud Cerrato } 8118cb6368SRenaud Cerrato 82*bbfe3a7aSBruno Thomsen if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF) 83907b3262SAkinobu Mita dev_info(dev, 84653ebd75SAndrea Scian "low voltage detected, check/replace RTC battery.\n"); 85653ebd75SAndrea Scian 86*bbfe3a7aSBruno Thomsen /* Clock integrity is not guaranteed when OSF flag is set. */ 87*bbfe3a7aSBruno Thomsen if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) { 88653ebd75SAndrea Scian /* 89653ebd75SAndrea Scian * no need clear the flag here, 90653ebd75SAndrea Scian * it will be cleared once the new date is saved 91653ebd75SAndrea Scian */ 92907b3262SAkinobu Mita dev_warn(dev, 93653ebd75SAndrea Scian "oscillator stop detected, date/time is not reliable\n"); 94653ebd75SAndrea Scian return -EINVAL; 9518cb6368SRenaud Cerrato } 9618cb6368SRenaud Cerrato 97907b3262SAkinobu Mita dev_dbg(dev, 9818cb6368SRenaud Cerrato "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, " 9918cb6368SRenaud Cerrato "sec=%02x, min=%02x, hr=%02x, " 10018cb6368SRenaud Cerrato "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", 10118cb6368SRenaud Cerrato __func__, 10218cb6368SRenaud Cerrato buf[0], buf[1], buf[2], 10318cb6368SRenaud Cerrato buf[3], buf[4], buf[5], 10418cb6368SRenaud Cerrato buf[6], buf[7], buf[8], buf[9]); 10518cb6368SRenaud Cerrato 10618cb6368SRenaud Cerrato 10718cb6368SRenaud Cerrato tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); 10818cb6368SRenaud Cerrato tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); 10918cb6368SRenaud Cerrato tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ 11018cb6368SRenaud Cerrato tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); 11118cb6368SRenaud Cerrato tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; 11218cb6368SRenaud Cerrato tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ 11318cb6368SRenaud Cerrato tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); 11418cb6368SRenaud Cerrato if (tm->tm_year < 70) 11518cb6368SRenaud Cerrato tm->tm_year += 100; /* assume we are in 1970...2069 */ 11618cb6368SRenaud Cerrato 117907b3262SAkinobu Mita dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " 11818cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 11918cb6368SRenaud Cerrato __func__, 12018cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 12118cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 12218cb6368SRenaud Cerrato 12322652ba7SAlexandre Belloni return 0; 12418cb6368SRenaud Cerrato } 12518cb6368SRenaud Cerrato 126907b3262SAkinobu Mita static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) 12718cb6368SRenaud Cerrato { 128907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 129907b3262SAkinobu Mita unsigned char buf[7]; 13018cb6368SRenaud Cerrato int i = 0, err; 13118cb6368SRenaud Cerrato 132907b3262SAkinobu Mita dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " 13318cb6368SRenaud Cerrato "mday=%d, mon=%d, year=%d, wday=%d\n", 13418cb6368SRenaud Cerrato __func__, 13518cb6368SRenaud Cerrato tm->tm_sec, tm->tm_min, tm->tm_hour, 13618cb6368SRenaud Cerrato tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); 13718cb6368SRenaud Cerrato 13818cb6368SRenaud Cerrato /* hours, minutes and seconds */ 139653ebd75SAndrea Scian buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ 14018cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_min); 14118cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_hour); 14218cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mday); 14318cb6368SRenaud Cerrato buf[i++] = tm->tm_wday & 0x07; 14418cb6368SRenaud Cerrato 14518cb6368SRenaud Cerrato /* month, 1 - 12 */ 14618cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_mon + 1); 14718cb6368SRenaud Cerrato 14818cb6368SRenaud Cerrato /* year */ 14918cb6368SRenaud Cerrato buf[i++] = bin2bcd(tm->tm_year % 100); 15018cb6368SRenaud Cerrato 15118cb6368SRenaud Cerrato /* write register's data */ 152907b3262SAkinobu Mita err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); 153907b3262SAkinobu Mita if (err) { 154907b3262SAkinobu Mita dev_err(dev, 15518cb6368SRenaud Cerrato "%s: err=%d", __func__, err); 156907b3262SAkinobu Mita return err; 15718cb6368SRenaud Cerrato } 15818cb6368SRenaud Cerrato 15918cb6368SRenaud Cerrato return 0; 16018cb6368SRenaud Cerrato } 16118cb6368SRenaud Cerrato 16218cb6368SRenaud Cerrato #ifdef CONFIG_RTC_INTF_DEV 16318cb6368SRenaud Cerrato static int pcf2127_rtc_ioctl(struct device *dev, 16418cb6368SRenaud Cerrato unsigned int cmd, unsigned long arg) 16518cb6368SRenaud Cerrato { 166907b3262SAkinobu Mita struct pcf2127 *pcf2127 = dev_get_drvdata(dev); 167f97cfddcSUwe Kleine-König int touser; 168f97cfddcSUwe Kleine-König int ret; 16918cb6368SRenaud Cerrato 17018cb6368SRenaud Cerrato switch (cmd) { 17118cb6368SRenaud Cerrato case RTC_VL_READ: 172907b3262SAkinobu Mita ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser); 173907b3262SAkinobu Mita if (ret) 174f97cfddcSUwe Kleine-König return ret; 17518cb6368SRenaud Cerrato 176*bbfe3a7aSBruno Thomsen touser = touser & PCF2127_BIT_CTRL3_BLF ? 1 : 0; 177f97cfddcSUwe Kleine-König 178f97cfddcSUwe Kleine-König if (copy_to_user((void __user *)arg, &touser, sizeof(int))) 17918cb6368SRenaud Cerrato return -EFAULT; 18018cb6368SRenaud Cerrato return 0; 18118cb6368SRenaud Cerrato default: 18218cb6368SRenaud Cerrato return -ENOIOCTLCMD; 18318cb6368SRenaud Cerrato } 18418cb6368SRenaud Cerrato } 18518cb6368SRenaud Cerrato #else 18618cb6368SRenaud Cerrato #define pcf2127_rtc_ioctl NULL 18718cb6368SRenaud Cerrato #endif 18818cb6368SRenaud Cerrato 18918cb6368SRenaud Cerrato static const struct rtc_class_ops pcf2127_rtc_ops = { 19018cb6368SRenaud Cerrato .ioctl = pcf2127_rtc_ioctl, 19118cb6368SRenaud Cerrato .read_time = pcf2127_rtc_read_time, 19218cb6368SRenaud Cerrato .set_time = pcf2127_rtc_set_time, 19318cb6368SRenaud Cerrato }; 19418cb6368SRenaud Cerrato 195d6c3029fSUwe Kleine-König static int pcf2127_nvmem_read(void *priv, unsigned int offset, 196d6c3029fSUwe Kleine-König void *val, size_t bytes) 197d6c3029fSUwe Kleine-König { 198d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 199d6c3029fSUwe Kleine-König int ret; 200d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 201d6c3029fSUwe Kleine-König 202*bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 203d6c3029fSUwe Kleine-König offsetbuf, 2); 204d6c3029fSUwe Kleine-König if (ret) 205d6c3029fSUwe Kleine-König return ret; 206d6c3029fSUwe Kleine-König 207*bbfe3a7aSBruno Thomsen ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, 208d6c3029fSUwe Kleine-König val, bytes); 209d6c3029fSUwe Kleine-König 210d6c3029fSUwe Kleine-König return ret ?: bytes; 211d6c3029fSUwe Kleine-König } 212d6c3029fSUwe Kleine-König 213d6c3029fSUwe Kleine-König static int pcf2127_nvmem_write(void *priv, unsigned int offset, 214d6c3029fSUwe Kleine-König void *val, size_t bytes) 215d6c3029fSUwe Kleine-König { 216d6c3029fSUwe Kleine-König struct pcf2127 *pcf2127 = priv; 217d6c3029fSUwe Kleine-König int ret; 218d6c3029fSUwe Kleine-König unsigned char offsetbuf[] = { offset >> 8, offset }; 219d6c3029fSUwe Kleine-König 220*bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, 221d6c3029fSUwe Kleine-König offsetbuf, 2); 222d6c3029fSUwe Kleine-König if (ret) 223d6c3029fSUwe Kleine-König return ret; 224d6c3029fSUwe Kleine-König 225*bbfe3a7aSBruno Thomsen ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, 226d6c3029fSUwe Kleine-König val, bytes); 227d6c3029fSUwe Kleine-König 228d6c3029fSUwe Kleine-König return ret ?: bytes; 229d6c3029fSUwe Kleine-König } 230d6c3029fSUwe Kleine-König 231907b3262SAkinobu Mita static int pcf2127_probe(struct device *dev, struct regmap *regmap, 232d6c3029fSUwe Kleine-König const char *name, bool has_nvmem) 23318cb6368SRenaud Cerrato { 23418cb6368SRenaud Cerrato struct pcf2127 *pcf2127; 235d6c3029fSUwe Kleine-König int ret = 0; 23618cb6368SRenaud Cerrato 237907b3262SAkinobu Mita dev_dbg(dev, "%s\n", __func__); 23818cb6368SRenaud Cerrato 239907b3262SAkinobu Mita pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); 24018cb6368SRenaud Cerrato if (!pcf2127) 24118cb6368SRenaud Cerrato return -ENOMEM; 24218cb6368SRenaud Cerrato 243907b3262SAkinobu Mita pcf2127->regmap = regmap; 24418cb6368SRenaud Cerrato 245907b3262SAkinobu Mita dev_set_drvdata(dev, pcf2127); 246907b3262SAkinobu Mita 247e788771cSBruno Thomsen pcf2127->rtc = devm_rtc_allocate_device(dev); 248d6c3029fSUwe Kleine-König if (IS_ERR(pcf2127->rtc)) 249d6c3029fSUwe Kleine-König return PTR_ERR(pcf2127->rtc); 25018cb6368SRenaud Cerrato 251e788771cSBruno Thomsen pcf2127->rtc->ops = &pcf2127_rtc_ops; 252e788771cSBruno Thomsen 253d6c3029fSUwe Kleine-König if (has_nvmem) { 254d6c3029fSUwe Kleine-König struct nvmem_config nvmem_cfg = { 255d6c3029fSUwe Kleine-König .priv = pcf2127, 256d6c3029fSUwe Kleine-König .reg_read = pcf2127_nvmem_read, 257d6c3029fSUwe Kleine-König .reg_write = pcf2127_nvmem_write, 258d6c3029fSUwe Kleine-König .size = 512, 259d6c3029fSUwe Kleine-König }; 260d6c3029fSUwe Kleine-König 261d6c3029fSUwe Kleine-König ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); 262d6c3029fSUwe Kleine-König } 263d6c3029fSUwe Kleine-König 264e788771cSBruno Thomsen return rtc_register_device(pcf2127->rtc); 26518cb6368SRenaud Cerrato } 26618cb6368SRenaud Cerrato 26718cb6368SRenaud Cerrato #ifdef CONFIG_OF 26818cb6368SRenaud Cerrato static const struct of_device_id pcf2127_of_match[] = { 26918cb6368SRenaud Cerrato { .compatible = "nxp,pcf2127" }, 270cee2cc21SAkinobu Mita { .compatible = "nxp,pcf2129" }, 27118cb6368SRenaud Cerrato {} 27218cb6368SRenaud Cerrato }; 27318cb6368SRenaud Cerrato MODULE_DEVICE_TABLE(of, pcf2127_of_match); 27418cb6368SRenaud Cerrato #endif 27518cb6368SRenaud Cerrato 2769408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_I2C) 2779408ec1aSAkinobu Mita 278907b3262SAkinobu Mita static int pcf2127_i2c_write(void *context, const void *data, size_t count) 279907b3262SAkinobu Mita { 280907b3262SAkinobu Mita struct device *dev = context; 281907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 282907b3262SAkinobu Mita int ret; 283907b3262SAkinobu Mita 284907b3262SAkinobu Mita ret = i2c_master_send(client, data, count); 285907b3262SAkinobu Mita if (ret != count) 286907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 287907b3262SAkinobu Mita 288907b3262SAkinobu Mita return 0; 289907b3262SAkinobu Mita } 290907b3262SAkinobu Mita 291907b3262SAkinobu Mita static int pcf2127_i2c_gather_write(void *context, 292907b3262SAkinobu Mita const void *reg, size_t reg_size, 293907b3262SAkinobu Mita const void *val, size_t val_size) 294907b3262SAkinobu Mita { 295907b3262SAkinobu Mita struct device *dev = context; 296907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 297907b3262SAkinobu Mita int ret; 298907b3262SAkinobu Mita void *buf; 299907b3262SAkinobu Mita 300907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 301907b3262SAkinobu Mita return -EINVAL; 302907b3262SAkinobu Mita 303907b3262SAkinobu Mita buf = kmalloc(val_size + 1, GFP_KERNEL); 304907b3262SAkinobu Mita if (!buf) 305907b3262SAkinobu Mita return -ENOMEM; 306907b3262SAkinobu Mita 307907b3262SAkinobu Mita memcpy(buf, reg, 1); 308907b3262SAkinobu Mita memcpy(buf + 1, val, val_size); 309907b3262SAkinobu Mita 310907b3262SAkinobu Mita ret = i2c_master_send(client, buf, val_size + 1); 3119bde0afbSXulin Sun 3129bde0afbSXulin Sun kfree(buf); 3139bde0afbSXulin Sun 314907b3262SAkinobu Mita if (ret != val_size + 1) 315907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 316907b3262SAkinobu Mita 317907b3262SAkinobu Mita return 0; 318907b3262SAkinobu Mita } 319907b3262SAkinobu Mita 320907b3262SAkinobu Mita static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, 321907b3262SAkinobu Mita void *val, size_t val_size) 322907b3262SAkinobu Mita { 323907b3262SAkinobu Mita struct device *dev = context; 324907b3262SAkinobu Mita struct i2c_client *client = to_i2c_client(dev); 325907b3262SAkinobu Mita int ret; 326907b3262SAkinobu Mita 327907b3262SAkinobu Mita if (WARN_ON(reg_size != 1)) 328907b3262SAkinobu Mita return -EINVAL; 329907b3262SAkinobu Mita 330907b3262SAkinobu Mita ret = i2c_master_send(client, reg, 1); 331907b3262SAkinobu Mita if (ret != 1) 332907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 333907b3262SAkinobu Mita 334907b3262SAkinobu Mita ret = i2c_master_recv(client, val, val_size); 335907b3262SAkinobu Mita if (ret != val_size) 336907b3262SAkinobu Mita return ret < 0 ? ret : -EIO; 337907b3262SAkinobu Mita 338907b3262SAkinobu Mita return 0; 339907b3262SAkinobu Mita } 340907b3262SAkinobu Mita 341907b3262SAkinobu Mita /* 342907b3262SAkinobu Mita * The reason we need this custom regmap_bus instead of using regmap_init_i2c() 343907b3262SAkinobu Mita * is that the STOP condition is required between set register address and 344907b3262SAkinobu Mita * read register data when reading from registers. 345907b3262SAkinobu Mita */ 346907b3262SAkinobu Mita static const struct regmap_bus pcf2127_i2c_regmap = { 347907b3262SAkinobu Mita .write = pcf2127_i2c_write, 348907b3262SAkinobu Mita .gather_write = pcf2127_i2c_gather_write, 349907b3262SAkinobu Mita .read = pcf2127_i2c_read, 35018cb6368SRenaud Cerrato }; 35118cb6368SRenaud Cerrato 352907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver; 353907b3262SAkinobu Mita 354907b3262SAkinobu Mita static int pcf2127_i2c_probe(struct i2c_client *client, 355907b3262SAkinobu Mita const struct i2c_device_id *id) 356907b3262SAkinobu Mita { 357907b3262SAkinobu Mita struct regmap *regmap; 358907b3262SAkinobu Mita static const struct regmap_config config = { 359907b3262SAkinobu Mita .reg_bits = 8, 360907b3262SAkinobu Mita .val_bits = 8, 361907b3262SAkinobu Mita }; 362907b3262SAkinobu Mita 363907b3262SAkinobu Mita if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 364907b3262SAkinobu Mita return -ENODEV; 365907b3262SAkinobu Mita 366907b3262SAkinobu Mita regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, 367907b3262SAkinobu Mita &client->dev, &config); 368907b3262SAkinobu Mita if (IS_ERR(regmap)) { 369907b3262SAkinobu Mita dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", 370907b3262SAkinobu Mita __func__, PTR_ERR(regmap)); 371907b3262SAkinobu Mita return PTR_ERR(regmap); 372907b3262SAkinobu Mita } 373907b3262SAkinobu Mita 374907b3262SAkinobu Mita return pcf2127_probe(&client->dev, regmap, 375d6c3029fSUwe Kleine-König pcf2127_i2c_driver.driver.name, id->driver_data); 376907b3262SAkinobu Mita } 377907b3262SAkinobu Mita 378907b3262SAkinobu Mita static const struct i2c_device_id pcf2127_i2c_id[] = { 379d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 380cee2cc21SAkinobu Mita { "pcf2129", 0 }, 381907b3262SAkinobu Mita { } 382907b3262SAkinobu Mita }; 383907b3262SAkinobu Mita MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); 384907b3262SAkinobu Mita 385907b3262SAkinobu Mita static struct i2c_driver pcf2127_i2c_driver = { 386907b3262SAkinobu Mita .driver = { 387907b3262SAkinobu Mita .name = "rtc-pcf2127-i2c", 388907b3262SAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 389907b3262SAkinobu Mita }, 390907b3262SAkinobu Mita .probe = pcf2127_i2c_probe, 391907b3262SAkinobu Mita .id_table = pcf2127_i2c_id, 392907b3262SAkinobu Mita }; 3939408ec1aSAkinobu Mita 3949408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 3959408ec1aSAkinobu Mita { 3969408ec1aSAkinobu Mita return i2c_add_driver(&pcf2127_i2c_driver); 3979408ec1aSAkinobu Mita } 3989408ec1aSAkinobu Mita 3999408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 4009408ec1aSAkinobu Mita { 4019408ec1aSAkinobu Mita i2c_del_driver(&pcf2127_i2c_driver); 4029408ec1aSAkinobu Mita } 4039408ec1aSAkinobu Mita 4049408ec1aSAkinobu Mita #else 4059408ec1aSAkinobu Mita 4069408ec1aSAkinobu Mita static int pcf2127_i2c_register_driver(void) 4079408ec1aSAkinobu Mita { 4089408ec1aSAkinobu Mita return 0; 4099408ec1aSAkinobu Mita } 4109408ec1aSAkinobu Mita 4119408ec1aSAkinobu Mita static void pcf2127_i2c_unregister_driver(void) 4129408ec1aSAkinobu Mita { 4139408ec1aSAkinobu Mita } 4149408ec1aSAkinobu Mita 4159408ec1aSAkinobu Mita #endif 4169408ec1aSAkinobu Mita 4179408ec1aSAkinobu Mita #if IS_ENABLED(CONFIG_SPI_MASTER) 4189408ec1aSAkinobu Mita 4199408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver; 4209408ec1aSAkinobu Mita 4219408ec1aSAkinobu Mita static int pcf2127_spi_probe(struct spi_device *spi) 4229408ec1aSAkinobu Mita { 4239408ec1aSAkinobu Mita static const struct regmap_config config = { 4249408ec1aSAkinobu Mita .reg_bits = 8, 4259408ec1aSAkinobu Mita .val_bits = 8, 4269408ec1aSAkinobu Mita .read_flag_mask = 0xa0, 4279408ec1aSAkinobu Mita .write_flag_mask = 0x20, 4289408ec1aSAkinobu Mita }; 4299408ec1aSAkinobu Mita struct regmap *regmap; 4309408ec1aSAkinobu Mita 4319408ec1aSAkinobu Mita regmap = devm_regmap_init_spi(spi, &config); 4329408ec1aSAkinobu Mita if (IS_ERR(regmap)) { 4339408ec1aSAkinobu Mita dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", 4349408ec1aSAkinobu Mita __func__, PTR_ERR(regmap)); 4359408ec1aSAkinobu Mita return PTR_ERR(regmap); 4369408ec1aSAkinobu Mita } 4379408ec1aSAkinobu Mita 438d6c3029fSUwe Kleine-König return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name, 439d6c3029fSUwe Kleine-König spi_get_device_id(spi)->driver_data); 4409408ec1aSAkinobu Mita } 4419408ec1aSAkinobu Mita 4429408ec1aSAkinobu Mita static const struct spi_device_id pcf2127_spi_id[] = { 443d6c3029fSUwe Kleine-König { "pcf2127", 1 }, 444cee2cc21SAkinobu Mita { "pcf2129", 0 }, 4459408ec1aSAkinobu Mita { } 4469408ec1aSAkinobu Mita }; 4479408ec1aSAkinobu Mita MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); 4489408ec1aSAkinobu Mita 4499408ec1aSAkinobu Mita static struct spi_driver pcf2127_spi_driver = { 4509408ec1aSAkinobu Mita .driver = { 4519408ec1aSAkinobu Mita .name = "rtc-pcf2127-spi", 4529408ec1aSAkinobu Mita .of_match_table = of_match_ptr(pcf2127_of_match), 4539408ec1aSAkinobu Mita }, 4549408ec1aSAkinobu Mita .probe = pcf2127_spi_probe, 4559408ec1aSAkinobu Mita .id_table = pcf2127_spi_id, 4569408ec1aSAkinobu Mita }; 4579408ec1aSAkinobu Mita 4589408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 4599408ec1aSAkinobu Mita { 4609408ec1aSAkinobu Mita return spi_register_driver(&pcf2127_spi_driver); 4619408ec1aSAkinobu Mita } 4629408ec1aSAkinobu Mita 4639408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 4649408ec1aSAkinobu Mita { 4659408ec1aSAkinobu Mita spi_unregister_driver(&pcf2127_spi_driver); 4669408ec1aSAkinobu Mita } 4679408ec1aSAkinobu Mita 4689408ec1aSAkinobu Mita #else 4699408ec1aSAkinobu Mita 4709408ec1aSAkinobu Mita static int pcf2127_spi_register_driver(void) 4719408ec1aSAkinobu Mita { 4729408ec1aSAkinobu Mita return 0; 4739408ec1aSAkinobu Mita } 4749408ec1aSAkinobu Mita 4759408ec1aSAkinobu Mita static void pcf2127_spi_unregister_driver(void) 4769408ec1aSAkinobu Mita { 4779408ec1aSAkinobu Mita } 4789408ec1aSAkinobu Mita 4799408ec1aSAkinobu Mita #endif 4809408ec1aSAkinobu Mita 4819408ec1aSAkinobu Mita static int __init pcf2127_init(void) 4829408ec1aSAkinobu Mita { 4839408ec1aSAkinobu Mita int ret; 4849408ec1aSAkinobu Mita 4859408ec1aSAkinobu Mita ret = pcf2127_i2c_register_driver(); 4869408ec1aSAkinobu Mita if (ret) { 4879408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); 4889408ec1aSAkinobu Mita return ret; 4899408ec1aSAkinobu Mita } 4909408ec1aSAkinobu Mita 4919408ec1aSAkinobu Mita ret = pcf2127_spi_register_driver(); 4929408ec1aSAkinobu Mita if (ret) { 4939408ec1aSAkinobu Mita pr_err("Failed to register pcf2127 spi driver: %d\n", ret); 4949408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 4959408ec1aSAkinobu Mita } 4969408ec1aSAkinobu Mita 4979408ec1aSAkinobu Mita return ret; 4989408ec1aSAkinobu Mita } 4999408ec1aSAkinobu Mita module_init(pcf2127_init) 5009408ec1aSAkinobu Mita 5019408ec1aSAkinobu Mita static void __exit pcf2127_exit(void) 5029408ec1aSAkinobu Mita { 5039408ec1aSAkinobu Mita pcf2127_spi_unregister_driver(); 5049408ec1aSAkinobu Mita pcf2127_i2c_unregister_driver(); 5059408ec1aSAkinobu Mita } 5069408ec1aSAkinobu Mita module_exit(pcf2127_exit) 50718cb6368SRenaud Cerrato 50818cb6368SRenaud Cerrato MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); 509cee2cc21SAkinobu Mita MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver"); 5104d8318bcSUwe Kleine-König MODULE_LICENSE("GPL v2"); 511