Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Avionic Design GmbH |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/bcd.h> |
| 10 | #include <linux/i2c.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/rtc.h> |
| 13 | #include <linux/of.h> |
| 14 | |
| 15 | #define DRIVER_NAME "rtc-pcf8523" |
| 16 | |
| 17 | #define REG_CONTROL1 0x00 |
| 18 | #define REG_CONTROL1_CAP_SEL (1 << 7) |
| 19 | #define REG_CONTROL1_STOP (1 << 5) |
| 20 | |
| 21 | #define REG_CONTROL3 0x02 |
| 22 | #define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */ |
| 23 | #define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */ |
| 24 | #define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */ |
| 25 | #define REG_CONTROL3_PM_MASK 0xe0 |
Jesper Nilsson | f32bc70 | 2013-02-21 16:44:27 -0800 | [diff] [blame] | 26 | #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */ |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 27 | |
| 28 | #define REG_SECONDS 0x03 |
| 29 | #define REG_SECONDS_OS (1 << 7) |
| 30 | |
| 31 | #define REG_MINUTES 0x04 |
| 32 | #define REG_HOURS 0x05 |
| 33 | #define REG_DAYS 0x06 |
| 34 | #define REG_WEEKDAYS 0x07 |
| 35 | #define REG_MONTHS 0x08 |
| 36 | #define REG_YEARS 0x09 |
| 37 | |
| 38 | struct pcf8523 { |
| 39 | struct rtc_device *rtc; |
| 40 | }; |
| 41 | |
| 42 | static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep) |
| 43 | { |
| 44 | struct i2c_msg msgs[2]; |
| 45 | u8 value = 0; |
| 46 | int err; |
| 47 | |
| 48 | msgs[0].addr = client->addr; |
| 49 | msgs[0].flags = 0; |
| 50 | msgs[0].len = sizeof(reg); |
| 51 | msgs[0].buf = ® |
| 52 | |
| 53 | msgs[1].addr = client->addr; |
| 54 | msgs[1].flags = I2C_M_RD; |
| 55 | msgs[1].len = sizeof(value); |
| 56 | msgs[1].buf = &value; |
| 57 | |
| 58 | err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 59 | if (err < 0) |
| 60 | return err; |
| 61 | |
| 62 | *valuep = value; |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value) |
| 68 | { |
| 69 | u8 buffer[2] = { reg, value }; |
| 70 | struct i2c_msg msg; |
| 71 | int err; |
| 72 | |
| 73 | msg.addr = client->addr; |
| 74 | msg.flags = 0; |
| 75 | msg.len = sizeof(buffer); |
| 76 | msg.buf = buffer; |
| 77 | |
| 78 | err = i2c_transfer(client->adapter, &msg, 1); |
| 79 | if (err < 0) |
| 80 | return err; |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int pcf8523_select_capacitance(struct i2c_client *client, bool high) |
| 86 | { |
| 87 | u8 value; |
| 88 | int err; |
| 89 | |
| 90 | err = pcf8523_read(client, REG_CONTROL1, &value); |
| 91 | if (err < 0) |
| 92 | return err; |
| 93 | |
| 94 | if (!high) |
| 95 | value &= ~REG_CONTROL1_CAP_SEL; |
| 96 | else |
| 97 | value |= REG_CONTROL1_CAP_SEL; |
| 98 | |
| 99 | err = pcf8523_write(client, REG_CONTROL1, value); |
| 100 | if (err < 0) |
| 101 | return err; |
| 102 | |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | static int pcf8523_set_pm(struct i2c_client *client, u8 pm) |
| 107 | { |
| 108 | u8 value; |
| 109 | int err; |
| 110 | |
| 111 | err = pcf8523_read(client, REG_CONTROL3, &value); |
| 112 | if (err < 0) |
| 113 | return err; |
| 114 | |
| 115 | value = (value & ~REG_CONTROL3_PM_MASK) | pm; |
| 116 | |
| 117 | err = pcf8523_write(client, REG_CONTROL3, value); |
| 118 | if (err < 0) |
| 119 | return err; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int pcf8523_stop_rtc(struct i2c_client *client) |
| 125 | { |
| 126 | u8 value; |
| 127 | int err; |
| 128 | |
| 129 | err = pcf8523_read(client, REG_CONTROL1, &value); |
| 130 | if (err < 0) |
| 131 | return err; |
| 132 | |
| 133 | value |= REG_CONTROL1_STOP; |
| 134 | |
| 135 | err = pcf8523_write(client, REG_CONTROL1, value); |
| 136 | if (err < 0) |
| 137 | return err; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int pcf8523_start_rtc(struct i2c_client *client) |
| 143 | { |
| 144 | u8 value; |
| 145 | int err; |
| 146 | |
| 147 | err = pcf8523_read(client, REG_CONTROL1, &value); |
| 148 | if (err < 0) |
| 149 | return err; |
| 150 | |
| 151 | value &= ~REG_CONTROL1_STOP; |
| 152 | |
| 153 | err = pcf8523_write(client, REG_CONTROL1, value); |
| 154 | if (err < 0) |
| 155 | return err; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 161 | { |
| 162 | struct i2c_client *client = to_i2c_client(dev); |
| 163 | u8 start = REG_SECONDS, regs[7]; |
| 164 | struct i2c_msg msgs[2]; |
| 165 | int err; |
| 166 | |
| 167 | msgs[0].addr = client->addr; |
| 168 | msgs[0].flags = 0; |
| 169 | msgs[0].len = 1; |
| 170 | msgs[0].buf = &start; |
| 171 | |
| 172 | msgs[1].addr = client->addr; |
| 173 | msgs[1].flags = I2C_M_RD; |
| 174 | msgs[1].len = sizeof(regs); |
| 175 | msgs[1].buf = regs; |
| 176 | |
| 177 | err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 178 | if (err < 0) |
| 179 | return err; |
| 180 | |
Alexandre Belloni | ede44c9 | 2016-03-03 09:55:47 +0100 | [diff] [blame] | 181 | if (regs[0] & REG_SECONDS_OS) |
| 182 | return -EINVAL; |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 183 | |
| 184 | tm->tm_sec = bcd2bin(regs[0] & 0x7f); |
| 185 | tm->tm_min = bcd2bin(regs[1] & 0x7f); |
| 186 | tm->tm_hour = bcd2bin(regs[2] & 0x3f); |
| 187 | tm->tm_mday = bcd2bin(regs[3] & 0x3f); |
| 188 | tm->tm_wday = regs[4] & 0x7; |
Chris Cui | 3573839 | 2014-05-06 12:49:58 -0700 | [diff] [blame] | 189 | tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1; |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 190 | tm->tm_year = bcd2bin(regs[6]) + 100; |
| 191 | |
| 192 | return rtc_valid_tm(tm); |
| 193 | } |
| 194 | |
| 195 | static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 196 | { |
| 197 | struct i2c_client *client = to_i2c_client(dev); |
| 198 | struct i2c_msg msg; |
| 199 | u8 regs[8]; |
| 200 | int err; |
| 201 | |
Uwe Kleine-König | fbbf53f | 2015-11-06 17:37:56 +0100 | [diff] [blame] | 202 | /* |
| 203 | * The hardware can only store values between 0 and 99 in it's YEAR |
| 204 | * register (with 99 overflowing to 0 on increment). |
| 205 | * After 2100-02-28 we could start interpreting the year to be in the |
| 206 | * interval [2100, 2199], but there is no path to switch in a smooth way |
| 207 | * because the chip handles YEAR=0x00 (and the out-of-spec |
| 208 | * YEAR=0xa0) as a leap year, but 2100 isn't. |
| 209 | */ |
| 210 | if (tm->tm_year < 100 || tm->tm_year >= 200) |
| 211 | return -EINVAL; |
| 212 | |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 213 | err = pcf8523_stop_rtc(client); |
| 214 | if (err < 0) |
| 215 | return err; |
| 216 | |
| 217 | regs[0] = REG_SECONDS; |
Alexandre Belloni | ede44c9 | 2016-03-03 09:55:47 +0100 | [diff] [blame] | 218 | /* This will purposely overwrite REG_SECONDS_OS */ |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 219 | regs[1] = bin2bcd(tm->tm_sec); |
| 220 | regs[2] = bin2bcd(tm->tm_min); |
| 221 | regs[3] = bin2bcd(tm->tm_hour); |
| 222 | regs[4] = bin2bcd(tm->tm_mday); |
| 223 | regs[5] = tm->tm_wday; |
Chris Cui | 3573839 | 2014-05-06 12:49:58 -0700 | [diff] [blame] | 224 | regs[6] = bin2bcd(tm->tm_mon + 1); |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 225 | regs[7] = bin2bcd(tm->tm_year - 100); |
| 226 | |
| 227 | msg.addr = client->addr; |
| 228 | msg.flags = 0; |
| 229 | msg.len = sizeof(regs); |
| 230 | msg.buf = regs; |
| 231 | |
| 232 | err = i2c_transfer(client->adapter, &msg, 1); |
| 233 | if (err < 0) { |
| 234 | /* |
| 235 | * If the time cannot be set, restart the RTC anyway. Note |
| 236 | * that errors are ignored if the RTC cannot be started so |
| 237 | * that we have a chance to propagate the original error. |
| 238 | */ |
| 239 | pcf8523_start_rtc(client); |
| 240 | return err; |
| 241 | } |
| 242 | |
| 243 | return pcf8523_start_rtc(client); |
| 244 | } |
| 245 | |
Jesper Nilsson | f32bc70 | 2013-02-21 16:44:27 -0800 | [diff] [blame] | 246 | #ifdef CONFIG_RTC_INTF_DEV |
| 247 | static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 248 | unsigned long arg) |
| 249 | { |
| 250 | struct i2c_client *client = to_i2c_client(dev); |
| 251 | u8 value; |
| 252 | int ret = 0, err; |
| 253 | |
| 254 | switch (cmd) { |
| 255 | case RTC_VL_READ: |
| 256 | err = pcf8523_read(client, REG_CONTROL3, &value); |
| 257 | if (err < 0) |
| 258 | return err; |
| 259 | |
| 260 | if (value & REG_CONTROL3_BLF) |
| 261 | ret = 1; |
| 262 | |
| 263 | if (copy_to_user((void __user *)arg, &ret, sizeof(int))) |
| 264 | return -EFAULT; |
| 265 | |
| 266 | return 0; |
| 267 | default: |
| 268 | return -ENOIOCTLCMD; |
| 269 | } |
| 270 | } |
| 271 | #else |
| 272 | #define pcf8523_rtc_ioctl NULL |
| 273 | #endif |
| 274 | |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 275 | static const struct rtc_class_ops pcf8523_rtc_ops = { |
| 276 | .read_time = pcf8523_rtc_read_time, |
| 277 | .set_time = pcf8523_rtc_set_time, |
Jesper Nilsson | f32bc70 | 2013-02-21 16:44:27 -0800 | [diff] [blame] | 278 | .ioctl = pcf8523_rtc_ioctl, |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | static int pcf8523_probe(struct i2c_client *client, |
| 282 | const struct i2c_device_id *id) |
| 283 | { |
| 284 | struct pcf8523 *pcf; |
| 285 | int err; |
| 286 | |
| 287 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 288 | return -ENODEV; |
| 289 | |
| 290 | pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL); |
| 291 | if (!pcf) |
| 292 | return -ENOMEM; |
| 293 | |
| 294 | err = pcf8523_select_capacitance(client, true); |
| 295 | if (err < 0) |
| 296 | return err; |
| 297 | |
| 298 | err = pcf8523_set_pm(client, 0); |
| 299 | if (err < 0) |
| 300 | return err; |
| 301 | |
Jingoo Han | 288031b | 2013-04-29 16:19:11 -0700 | [diff] [blame] | 302 | pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME, |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 303 | &pcf8523_rtc_ops, THIS_MODULE); |
| 304 | if (IS_ERR(pcf->rtc)) |
| 305 | return PTR_ERR(pcf->rtc); |
| 306 | |
| 307 | i2c_set_clientdata(client, pcf); |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 312 | static const struct i2c_device_id pcf8523_id[] = { |
| 313 | { "pcf8523", 0 }, |
| 314 | { } |
| 315 | }; |
| 316 | MODULE_DEVICE_TABLE(i2c, pcf8523_id); |
| 317 | |
| 318 | #ifdef CONFIG_OF |
| 319 | static const struct of_device_id pcf8523_of_match[] = { |
| 320 | { .compatible = "nxp,pcf8523" }, |
| 321 | { } |
| 322 | }; |
| 323 | MODULE_DEVICE_TABLE(of, pcf8523_of_match); |
| 324 | #endif |
| 325 | |
| 326 | static struct i2c_driver pcf8523_driver = { |
| 327 | .driver = { |
| 328 | .name = DRIVER_NAME, |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 329 | .of_match_table = of_match_ptr(pcf8523_of_match), |
| 330 | }, |
| 331 | .probe = pcf8523_probe, |
Thierry Reding | f803f0d | 2012-12-17 16:02:44 -0800 | [diff] [blame] | 332 | .id_table = pcf8523_id, |
| 333 | }; |
| 334 | module_i2c_driver(pcf8523_driver); |
| 335 | |
| 336 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
| 337 | MODULE_DESCRIPTION("NXP PCF8523 RTC driver"); |
| 338 | MODULE_LICENSE("GPL v2"); |