Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 1 | /* |
Akinobu Mita | cee2cc2 | 2016-03-14 23:45:01 +0900 | [diff] [blame] | 2 | * An I2C and SPI driver for the NXP PCF2127/29 RTC |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 3 | * Copyright 2013 Til-Technologies |
| 4 | * |
| 5 | * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> |
| 6 | * |
| 7 | * based on the other drivers in this same directory. |
| 8 | * |
Akinobu Mita | cee2cc2 | 2016-03-14 23:45:01 +0900 | [diff] [blame] | 9 | * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/i2c.h> |
Akinobu Mita | 9408ec1 | 2016-03-14 23:45:00 +0900 | [diff] [blame] | 17 | #include <linux/spi/spi.h> |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 18 | #include <linux/bcd.h> |
| 19 | #include <linux/rtc.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 23 | #include <linux/regmap.h> |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 24 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 25 | #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ |
| 26 | #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 27 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 28 | #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 29 | #define PCF2127_REG_CTRL3_BLF BIT(2) |
| 30 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 31 | #define PCF2127_REG_SC (0x03) /* datetime */ |
| 32 | #define PCF2127_REG_MN (0x04) |
| 33 | #define PCF2127_REG_HR (0x05) |
| 34 | #define PCF2127_REG_DM (0x06) |
| 35 | #define PCF2127_REG_DW (0x07) |
| 36 | #define PCF2127_REG_MO (0x08) |
| 37 | #define PCF2127_REG_YR (0x09) |
| 38 | |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 39 | /* the pcf2127 has 512 bytes nvmem, pcf2129 doesn't */ |
| 40 | #define PCF2127_REG_RAM_addr_MSB 0x1a |
| 41 | #define PCF2127_REG_RAM_wrt_cmd 0x1c |
| 42 | #define PCF2127_REG_RAM_rd_cmd 0x1d |
| 43 | |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 44 | #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ |
| 45 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 46 | struct pcf2127 { |
| 47 | struct rtc_device *rtc; |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 48 | struct regmap *regmap; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * In the routines that deal directly with the pcf2127 hardware, we use |
| 53 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. |
| 54 | */ |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 55 | static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 56 | { |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 57 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
| 58 | unsigned char buf[10]; |
| 59 | int ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 60 | |
Bruno Thomsen | 630c20f | 2019-08-22 15:19:34 +0200 | [diff] [blame] | 61 | /* |
| 62 | * Avoid reading CTRL2 register as it causes WD_VAL register |
| 63 | * value to reset to 0 which means watchdog is stopped. |
| 64 | */ |
| 65 | ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3, |
| 66 | (buf + PCF2127_REG_CTRL3), |
| 67 | ARRAY_SIZE(buf) - PCF2127_REG_CTRL3); |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 68 | if (ret) { |
| 69 | dev_err(dev, "%s: read error\n", __func__); |
| 70 | return ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 73 | if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF) |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 74 | dev_info(dev, |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 75 | "low voltage detected, check/replace RTC battery.\n"); |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 76 | |
| 77 | if (buf[PCF2127_REG_SC] & PCF2127_OSF) { |
| 78 | /* |
| 79 | * no need clear the flag here, |
| 80 | * it will be cleared once the new date is saved |
| 81 | */ |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 82 | dev_warn(dev, |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 83 | "oscillator stop detected, date/time is not reliable\n"); |
| 84 | return -EINVAL; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 87 | dev_dbg(dev, |
Bruno Thomsen | 630c20f | 2019-08-22 15:19:34 +0200 | [diff] [blame] | 88 | "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, " |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 89 | "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", |
Bruno Thomsen | 630c20f | 2019-08-22 15:19:34 +0200 | [diff] [blame] | 90 | __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC], |
| 91 | buf[PCF2127_REG_MN], buf[PCF2127_REG_HR], |
| 92 | buf[PCF2127_REG_DM], buf[PCF2127_REG_DW], |
| 93 | buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 94 | |
| 95 | tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); |
| 96 | tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); |
| 97 | tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ |
| 98 | tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); |
| 99 | tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; |
| 100 | tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ |
| 101 | tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); |
| 102 | if (tm->tm_year < 70) |
| 103 | tm->tm_year += 100; /* assume we are in 1970...2069 */ |
| 104 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 105 | dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, " |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 106 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
| 107 | __func__, |
| 108 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
| 109 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); |
| 110 | |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 111 | return 0; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 114 | static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 115 | { |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 116 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
| 117 | unsigned char buf[7]; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 118 | int i = 0, err; |
| 119 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 120 | dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, " |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 121 | "mday=%d, mon=%d, year=%d, wday=%d\n", |
| 122 | __func__, |
| 123 | tm->tm_sec, tm->tm_min, tm->tm_hour, |
| 124 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); |
| 125 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 126 | /* hours, minutes and seconds */ |
Andrea Scian | 653ebd7 | 2015-06-16 11:39:47 +0200 | [diff] [blame] | 127 | buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 128 | buf[i++] = bin2bcd(tm->tm_min); |
| 129 | buf[i++] = bin2bcd(tm->tm_hour); |
| 130 | buf[i++] = bin2bcd(tm->tm_mday); |
| 131 | buf[i++] = tm->tm_wday & 0x07; |
| 132 | |
| 133 | /* month, 1 - 12 */ |
| 134 | buf[i++] = bin2bcd(tm->tm_mon + 1); |
| 135 | |
| 136 | /* year */ |
| 137 | buf[i++] = bin2bcd(tm->tm_year % 100); |
| 138 | |
| 139 | /* write register's data */ |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 140 | err = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_SC, buf, i); |
| 141 | if (err) { |
| 142 | dev_err(dev, |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 143 | "%s: err=%d", __func__, err); |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 144 | return err; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | #ifdef CONFIG_RTC_INTF_DEV |
| 151 | static int pcf2127_rtc_ioctl(struct device *dev, |
| 152 | unsigned int cmd, unsigned long arg) |
| 153 | { |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 154 | struct pcf2127 *pcf2127 = dev_get_drvdata(dev); |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 155 | int touser; |
| 156 | int ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 157 | |
| 158 | switch (cmd) { |
| 159 | case RTC_VL_READ: |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 160 | ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL3, &touser); |
| 161 | if (ret) |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 162 | return ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 163 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 164 | touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0; |
Uwe Kleine-König | f97cfdd | 2015-10-02 11:17:19 +0200 | [diff] [blame] | 165 | |
| 166 | if (copy_to_user((void __user *)arg, &touser, sizeof(int))) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 167 | return -EFAULT; |
| 168 | return 0; |
| 169 | default: |
| 170 | return -ENOIOCTLCMD; |
| 171 | } |
| 172 | } |
| 173 | #else |
| 174 | #define pcf2127_rtc_ioctl NULL |
| 175 | #endif |
| 176 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 177 | static const struct rtc_class_ops pcf2127_rtc_ops = { |
| 178 | .ioctl = pcf2127_rtc_ioctl, |
| 179 | .read_time = pcf2127_rtc_read_time, |
| 180 | .set_time = pcf2127_rtc_set_time, |
| 181 | }; |
| 182 | |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 183 | static int pcf2127_nvmem_read(void *priv, unsigned int offset, |
| 184 | void *val, size_t bytes) |
| 185 | { |
| 186 | struct pcf2127 *pcf2127 = priv; |
| 187 | int ret; |
| 188 | unsigned char offsetbuf[] = { offset >> 8, offset }; |
| 189 | |
| 190 | ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB, |
| 191 | offsetbuf, 2); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | |
| 195 | ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_rd_cmd, |
| 196 | val, bytes); |
| 197 | |
| 198 | return ret ?: bytes; |
| 199 | } |
| 200 | |
| 201 | static int pcf2127_nvmem_write(void *priv, unsigned int offset, |
| 202 | void *val, size_t bytes) |
| 203 | { |
| 204 | struct pcf2127 *pcf2127 = priv; |
| 205 | int ret; |
| 206 | unsigned char offsetbuf[] = { offset >> 8, offset }; |
| 207 | |
| 208 | ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB, |
| 209 | offsetbuf, 2); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | |
| 213 | ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_wrt_cmd, |
| 214 | val, bytes); |
| 215 | |
| 216 | return ret ?: bytes; |
| 217 | } |
| 218 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 219 | static int pcf2127_probe(struct device *dev, struct regmap *regmap, |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 220 | const char *name, bool has_nvmem) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 221 | { |
| 222 | struct pcf2127 *pcf2127; |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 223 | int ret = 0; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 224 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 225 | dev_dbg(dev, "%s\n", __func__); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 226 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 227 | pcf2127 = devm_kzalloc(dev, sizeof(*pcf2127), GFP_KERNEL); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 228 | if (!pcf2127) |
| 229 | return -ENOMEM; |
| 230 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 231 | pcf2127->regmap = regmap; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 232 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 233 | dev_set_drvdata(dev, pcf2127); |
| 234 | |
| 235 | pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops, |
| 236 | THIS_MODULE); |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 237 | if (IS_ERR(pcf2127->rtc)) |
| 238 | return PTR_ERR(pcf2127->rtc); |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 239 | |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 240 | if (has_nvmem) { |
| 241 | struct nvmem_config nvmem_cfg = { |
| 242 | .priv = pcf2127, |
| 243 | .reg_read = pcf2127_nvmem_read, |
| 244 | .reg_write = pcf2127_nvmem_write, |
| 245 | .size = 512, |
| 246 | }; |
| 247 | |
| 248 | ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); |
| 249 | } |
| 250 | |
| 251 | return ret; |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 254 | #ifdef CONFIG_OF |
| 255 | static const struct of_device_id pcf2127_of_match[] = { |
| 256 | { .compatible = "nxp,pcf2127" }, |
Akinobu Mita | cee2cc2 | 2016-03-14 23:45:01 +0900 | [diff] [blame] | 257 | { .compatible = "nxp,pcf2129" }, |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 258 | {} |
| 259 | }; |
| 260 | MODULE_DEVICE_TABLE(of, pcf2127_of_match); |
| 261 | #endif |
| 262 | |
Akinobu Mita | 9408ec1 | 2016-03-14 23:45:00 +0900 | [diff] [blame] | 263 | #if IS_ENABLED(CONFIG_I2C) |
| 264 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 265 | static int pcf2127_i2c_write(void *context, const void *data, size_t count) |
| 266 | { |
| 267 | struct device *dev = context; |
| 268 | struct i2c_client *client = to_i2c_client(dev); |
| 269 | int ret; |
| 270 | |
| 271 | ret = i2c_master_send(client, data, count); |
| 272 | if (ret != count) |
| 273 | return ret < 0 ? ret : -EIO; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int pcf2127_i2c_gather_write(void *context, |
| 279 | const void *reg, size_t reg_size, |
| 280 | const void *val, size_t val_size) |
| 281 | { |
| 282 | struct device *dev = context; |
| 283 | struct i2c_client *client = to_i2c_client(dev); |
| 284 | int ret; |
| 285 | void *buf; |
| 286 | |
| 287 | if (WARN_ON(reg_size != 1)) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | buf = kmalloc(val_size + 1, GFP_KERNEL); |
| 291 | if (!buf) |
| 292 | return -ENOMEM; |
| 293 | |
| 294 | memcpy(buf, reg, 1); |
| 295 | memcpy(buf + 1, val, val_size); |
| 296 | |
| 297 | ret = i2c_master_send(client, buf, val_size + 1); |
Xulin Sun | 49bcb04 | 2018-11-06 16:42:19 +0800 | [diff] [blame] | 298 | |
| 299 | kfree(buf); |
| 300 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 301 | if (ret != val_size + 1) |
| 302 | return ret < 0 ? ret : -EIO; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int pcf2127_i2c_read(void *context, const void *reg, size_t reg_size, |
| 308 | void *val, size_t val_size) |
| 309 | { |
| 310 | struct device *dev = context; |
| 311 | struct i2c_client *client = to_i2c_client(dev); |
| 312 | int ret; |
| 313 | |
| 314 | if (WARN_ON(reg_size != 1)) |
| 315 | return -EINVAL; |
| 316 | |
| 317 | ret = i2c_master_send(client, reg, 1); |
| 318 | if (ret != 1) |
| 319 | return ret < 0 ? ret : -EIO; |
| 320 | |
| 321 | ret = i2c_master_recv(client, val, val_size); |
| 322 | if (ret != val_size) |
| 323 | return ret < 0 ? ret : -EIO; |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * The reason we need this custom regmap_bus instead of using regmap_init_i2c() |
| 330 | * is that the STOP condition is required between set register address and |
| 331 | * read register data when reading from registers. |
| 332 | */ |
| 333 | static const struct regmap_bus pcf2127_i2c_regmap = { |
| 334 | .write = pcf2127_i2c_write, |
| 335 | .gather_write = pcf2127_i2c_gather_write, |
| 336 | .read = pcf2127_i2c_read, |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 339 | static struct i2c_driver pcf2127_i2c_driver; |
| 340 | |
| 341 | static int pcf2127_i2c_probe(struct i2c_client *client, |
| 342 | const struct i2c_device_id *id) |
| 343 | { |
| 344 | struct regmap *regmap; |
| 345 | static const struct regmap_config config = { |
| 346 | .reg_bits = 8, |
| 347 | .val_bits = 8, |
| 348 | }; |
| 349 | |
| 350 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 351 | return -ENODEV; |
| 352 | |
| 353 | regmap = devm_regmap_init(&client->dev, &pcf2127_i2c_regmap, |
| 354 | &client->dev, &config); |
| 355 | if (IS_ERR(regmap)) { |
| 356 | dev_err(&client->dev, "%s: regmap allocation failed: %ld\n", |
| 357 | __func__, PTR_ERR(regmap)); |
| 358 | return PTR_ERR(regmap); |
| 359 | } |
| 360 | |
| 361 | return pcf2127_probe(&client->dev, regmap, |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 362 | pcf2127_i2c_driver.driver.name, id->driver_data); |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static const struct i2c_device_id pcf2127_i2c_id[] = { |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 366 | { "pcf2127", 1 }, |
Akinobu Mita | cee2cc2 | 2016-03-14 23:45:01 +0900 | [diff] [blame] | 367 | { "pcf2129", 0 }, |
Akinobu Mita | 907b326 | 2016-03-14 23:44:59 +0900 | [diff] [blame] | 368 | { } |
| 369 | }; |
| 370 | MODULE_DEVICE_TABLE(i2c, pcf2127_i2c_id); |
| 371 | |
| 372 | static struct i2c_driver pcf2127_i2c_driver = { |
| 373 | .driver = { |
| 374 | .name = "rtc-pcf2127-i2c", |
| 375 | .of_match_table = of_match_ptr(pcf2127_of_match), |
| 376 | }, |
| 377 | .probe = pcf2127_i2c_probe, |
| 378 | .id_table = pcf2127_i2c_id, |
| 379 | }; |
Akinobu Mita | 9408ec1 | 2016-03-14 23:45:00 +0900 | [diff] [blame] | 380 | |
| 381 | static int pcf2127_i2c_register_driver(void) |
| 382 | { |
| 383 | return i2c_add_driver(&pcf2127_i2c_driver); |
| 384 | } |
| 385 | |
| 386 | static void pcf2127_i2c_unregister_driver(void) |
| 387 | { |
| 388 | i2c_del_driver(&pcf2127_i2c_driver); |
| 389 | } |
| 390 | |
| 391 | #else |
| 392 | |
| 393 | static int pcf2127_i2c_register_driver(void) |
| 394 | { |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static void pcf2127_i2c_unregister_driver(void) |
| 399 | { |
| 400 | } |
| 401 | |
| 402 | #endif |
| 403 | |
| 404 | #if IS_ENABLED(CONFIG_SPI_MASTER) |
| 405 | |
| 406 | static struct spi_driver pcf2127_spi_driver; |
| 407 | |
| 408 | static int pcf2127_spi_probe(struct spi_device *spi) |
| 409 | { |
| 410 | static const struct regmap_config config = { |
| 411 | .reg_bits = 8, |
| 412 | .val_bits = 8, |
| 413 | .read_flag_mask = 0xa0, |
| 414 | .write_flag_mask = 0x20, |
| 415 | }; |
| 416 | struct regmap *regmap; |
| 417 | |
| 418 | regmap = devm_regmap_init_spi(spi, &config); |
| 419 | if (IS_ERR(regmap)) { |
| 420 | dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n", |
| 421 | __func__, PTR_ERR(regmap)); |
| 422 | return PTR_ERR(regmap); |
| 423 | } |
| 424 | |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 425 | return pcf2127_probe(&spi->dev, regmap, pcf2127_spi_driver.driver.name, |
| 426 | spi_get_device_id(spi)->driver_data); |
Akinobu Mita | 9408ec1 | 2016-03-14 23:45:00 +0900 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static const struct spi_device_id pcf2127_spi_id[] = { |
Uwe Kleine-König | d6c3029 | 2018-05-20 15:37:23 +0200 | [diff] [blame] | 430 | { "pcf2127", 1 }, |
Akinobu Mita | cee2cc2 | 2016-03-14 23:45:01 +0900 | [diff] [blame] | 431 | { "pcf2129", 0 }, |
Akinobu Mita | 9408ec1 | 2016-03-14 23:45:00 +0900 | [diff] [blame] | 432 | { } |
| 433 | }; |
| 434 | MODULE_DEVICE_TABLE(spi, pcf2127_spi_id); |
| 435 | |
| 436 | static struct spi_driver pcf2127_spi_driver = { |
| 437 | .driver = { |
| 438 | .name = "rtc-pcf2127-spi", |
| 439 | .of_match_table = of_match_ptr(pcf2127_of_match), |
| 440 | }, |
| 441 | .probe = pcf2127_spi_probe, |
| 442 | .id_table = pcf2127_spi_id, |
| 443 | }; |
| 444 | |
| 445 | static int pcf2127_spi_register_driver(void) |
| 446 | { |
| 447 | return spi_register_driver(&pcf2127_spi_driver); |
| 448 | } |
| 449 | |
| 450 | static void pcf2127_spi_unregister_driver(void) |
| 451 | { |
| 452 | spi_unregister_driver(&pcf2127_spi_driver); |
| 453 | } |
| 454 | |
| 455 | #else |
| 456 | |
| 457 | static int pcf2127_spi_register_driver(void) |
| 458 | { |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static void pcf2127_spi_unregister_driver(void) |
| 463 | { |
| 464 | } |
| 465 | |
| 466 | #endif |
| 467 | |
| 468 | static int __init pcf2127_init(void) |
| 469 | { |
| 470 | int ret; |
| 471 | |
| 472 | ret = pcf2127_i2c_register_driver(); |
| 473 | if (ret) { |
| 474 | pr_err("Failed to register pcf2127 i2c driver: %d\n", ret); |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | ret = pcf2127_spi_register_driver(); |
| 479 | if (ret) { |
| 480 | pr_err("Failed to register pcf2127 spi driver: %d\n", ret); |
| 481 | pcf2127_i2c_unregister_driver(); |
| 482 | } |
| 483 | |
| 484 | return ret; |
| 485 | } |
| 486 | module_init(pcf2127_init) |
| 487 | |
| 488 | static void __exit pcf2127_exit(void) |
| 489 | { |
| 490 | pcf2127_spi_unregister_driver(); |
| 491 | pcf2127_i2c_unregister_driver(); |
| 492 | } |
| 493 | module_exit(pcf2127_exit) |
Renaud Cerrato | 18cb636 | 2013-07-03 15:08:01 -0700 | [diff] [blame] | 494 | |
| 495 | MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); |
Akinobu Mita | cee2cc2 | 2016-03-14 23:45:01 +0900 | [diff] [blame] | 496 | MODULE_DESCRIPTION("NXP PCF2127/29 RTC driver"); |
Uwe Kleine-König | 4d8318b | 2015-09-09 11:29:10 +0200 | [diff] [blame] | 497 | MODULE_LICENSE("GPL v2"); |