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