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 26 #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */ 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 #define REG_OFFSET 0x0e 39 #define REG_OFFSET_MODE BIT(7) 40 41 struct pcf8523 { 42 struct rtc_device *rtc; 43 }; 44 45 static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep) 46 { 47 struct i2c_msg msgs[2]; 48 u8 value = 0; 49 int err; 50 51 msgs[0].addr = client->addr; 52 msgs[0].flags = 0; 53 msgs[0].len = sizeof(reg); 54 msgs[0].buf = ® 55 56 msgs[1].addr = client->addr; 57 msgs[1].flags = I2C_M_RD; 58 msgs[1].len = sizeof(value); 59 msgs[1].buf = &value; 60 61 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 62 if (err < 0) 63 return err; 64 65 *valuep = value; 66 67 return 0; 68 } 69 70 static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value) 71 { 72 u8 buffer[2] = { reg, value }; 73 struct i2c_msg msg; 74 int err; 75 76 msg.addr = client->addr; 77 msg.flags = 0; 78 msg.len = sizeof(buffer); 79 msg.buf = buffer; 80 81 err = i2c_transfer(client->adapter, &msg, 1); 82 if (err < 0) 83 return err; 84 85 return 0; 86 } 87 88 static int pcf8523_voltage_low(struct i2c_client *client) 89 { 90 u8 value; 91 int err; 92 93 err = pcf8523_read(client, REG_CONTROL3, &value); 94 if (err < 0) 95 return err; 96 97 return !!(value & REG_CONTROL3_BLF); 98 } 99 100 static int pcf8523_select_capacitance(struct i2c_client *client, bool high) 101 { 102 u8 value; 103 int err; 104 105 err = pcf8523_read(client, REG_CONTROL1, &value); 106 if (err < 0) 107 return err; 108 109 if (!high) 110 value &= ~REG_CONTROL1_CAP_SEL; 111 else 112 value |= REG_CONTROL1_CAP_SEL; 113 114 err = pcf8523_write(client, REG_CONTROL1, value); 115 if (err < 0) 116 return err; 117 118 return err; 119 } 120 121 static int pcf8523_set_pm(struct i2c_client *client, u8 pm) 122 { 123 u8 value; 124 int err; 125 126 err = pcf8523_read(client, REG_CONTROL3, &value); 127 if (err < 0) 128 return err; 129 130 value = (value & ~REG_CONTROL3_PM_MASK) | pm; 131 132 err = pcf8523_write(client, REG_CONTROL3, value); 133 if (err < 0) 134 return err; 135 136 return 0; 137 } 138 139 static int pcf8523_stop_rtc(struct i2c_client *client) 140 { 141 u8 value; 142 int err; 143 144 err = pcf8523_read(client, REG_CONTROL1, &value); 145 if (err < 0) 146 return err; 147 148 value |= REG_CONTROL1_STOP; 149 150 err = pcf8523_write(client, REG_CONTROL1, value); 151 if (err < 0) 152 return err; 153 154 return 0; 155 } 156 157 static int pcf8523_start_rtc(struct i2c_client *client) 158 { 159 u8 value; 160 int err; 161 162 err = pcf8523_read(client, REG_CONTROL1, &value); 163 if (err < 0) 164 return err; 165 166 value &= ~REG_CONTROL1_STOP; 167 168 err = pcf8523_write(client, REG_CONTROL1, value); 169 if (err < 0) 170 return err; 171 172 return 0; 173 } 174 175 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm) 176 { 177 struct i2c_client *client = to_i2c_client(dev); 178 u8 start = REG_SECONDS, regs[7]; 179 struct i2c_msg msgs[2]; 180 int err; 181 182 err = pcf8523_voltage_low(client); 183 if (err < 0) { 184 return err; 185 } else if (err > 0) { 186 dev_err(dev, "low voltage detected, time is unreliable\n"); 187 return -EINVAL; 188 } 189 190 msgs[0].addr = client->addr; 191 msgs[0].flags = 0; 192 msgs[0].len = 1; 193 msgs[0].buf = &start; 194 195 msgs[1].addr = client->addr; 196 msgs[1].flags = I2C_M_RD; 197 msgs[1].len = sizeof(regs); 198 msgs[1].buf = regs; 199 200 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 201 if (err < 0) 202 return err; 203 204 if (regs[0] & REG_SECONDS_OS) 205 return -EINVAL; 206 207 tm->tm_sec = bcd2bin(regs[0] & 0x7f); 208 tm->tm_min = bcd2bin(regs[1] & 0x7f); 209 tm->tm_hour = bcd2bin(regs[2] & 0x3f); 210 tm->tm_mday = bcd2bin(regs[3] & 0x3f); 211 tm->tm_wday = regs[4] & 0x7; 212 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1; 213 tm->tm_year = bcd2bin(regs[6]) + 100; 214 215 return 0; 216 } 217 218 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm) 219 { 220 struct i2c_client *client = to_i2c_client(dev); 221 struct i2c_msg msg; 222 u8 regs[8]; 223 int err; 224 225 /* 226 * The hardware can only store values between 0 and 99 in it's YEAR 227 * register (with 99 overflowing to 0 on increment). 228 * After 2100-02-28 we could start interpreting the year to be in the 229 * interval [2100, 2199], but there is no path to switch in a smooth way 230 * because the chip handles YEAR=0x00 (and the out-of-spec 231 * YEAR=0xa0) as a leap year, but 2100 isn't. 232 */ 233 if (tm->tm_year < 100 || tm->tm_year >= 200) 234 return -EINVAL; 235 236 err = pcf8523_stop_rtc(client); 237 if (err < 0) 238 return err; 239 240 regs[0] = REG_SECONDS; 241 /* This will purposely overwrite REG_SECONDS_OS */ 242 regs[1] = bin2bcd(tm->tm_sec); 243 regs[2] = bin2bcd(tm->tm_min); 244 regs[3] = bin2bcd(tm->tm_hour); 245 regs[4] = bin2bcd(tm->tm_mday); 246 regs[5] = tm->tm_wday; 247 regs[6] = bin2bcd(tm->tm_mon + 1); 248 regs[7] = bin2bcd(tm->tm_year - 100); 249 250 msg.addr = client->addr; 251 msg.flags = 0; 252 msg.len = sizeof(regs); 253 msg.buf = regs; 254 255 err = i2c_transfer(client->adapter, &msg, 1); 256 if (err < 0) { 257 /* 258 * If the time cannot be set, restart the RTC anyway. Note 259 * that errors are ignored if the RTC cannot be started so 260 * that we have a chance to propagate the original error. 261 */ 262 pcf8523_start_rtc(client); 263 return err; 264 } 265 266 return pcf8523_start_rtc(client); 267 } 268 269 #ifdef CONFIG_RTC_INTF_DEV 270 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd, 271 unsigned long arg) 272 { 273 struct i2c_client *client = to_i2c_client(dev); 274 int ret; 275 276 switch (cmd) { 277 case RTC_VL_READ: 278 ret = pcf8523_voltage_low(client); 279 if (ret < 0) 280 return ret; 281 282 if (copy_to_user((void __user *)arg, &ret, sizeof(int))) 283 return -EFAULT; 284 285 return 0; 286 default: 287 return -ENOIOCTLCMD; 288 } 289 } 290 #else 291 #define pcf8523_rtc_ioctl NULL 292 #endif 293 294 static int pcf8523_rtc_read_offset(struct device *dev, long *offset) 295 { 296 struct i2c_client *client = to_i2c_client(dev); 297 int err; 298 u8 value; 299 s8 val; 300 301 err = pcf8523_read(client, REG_OFFSET, &value); 302 if (err < 0) 303 return err; 304 305 /* sign extend the 7-bit offset value */ 306 val = value << 1; 307 *offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1); 308 309 return 0; 310 } 311 312 static int pcf8523_rtc_set_offset(struct device *dev, long offset) 313 { 314 struct i2c_client *client = to_i2c_client(dev); 315 long reg_m0, reg_m1; 316 u8 value; 317 318 reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L); 319 reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L); 320 321 if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset)) 322 value = reg_m0 & 0x7f; 323 else 324 value = (reg_m1 & 0x7f) | REG_OFFSET_MODE; 325 326 return pcf8523_write(client, REG_OFFSET, value); 327 } 328 329 static const struct rtc_class_ops pcf8523_rtc_ops = { 330 .read_time = pcf8523_rtc_read_time, 331 .set_time = pcf8523_rtc_set_time, 332 .ioctl = pcf8523_rtc_ioctl, 333 .read_offset = pcf8523_rtc_read_offset, 334 .set_offset = pcf8523_rtc_set_offset, 335 }; 336 337 static int pcf8523_probe(struct i2c_client *client, 338 const struct i2c_device_id *id) 339 { 340 struct pcf8523 *pcf; 341 int err; 342 343 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 344 return -ENODEV; 345 346 pcf = devm_kzalloc(&client->dev, sizeof(*pcf), GFP_KERNEL); 347 if (!pcf) 348 return -ENOMEM; 349 350 err = pcf8523_select_capacitance(client, true); 351 if (err < 0) 352 return err; 353 354 err = pcf8523_set_pm(client, 0); 355 if (err < 0) 356 return err; 357 358 pcf->rtc = devm_rtc_device_register(&client->dev, DRIVER_NAME, 359 &pcf8523_rtc_ops, THIS_MODULE); 360 if (IS_ERR(pcf->rtc)) 361 return PTR_ERR(pcf->rtc); 362 363 i2c_set_clientdata(client, pcf); 364 365 return 0; 366 } 367 368 static const struct i2c_device_id pcf8523_id[] = { 369 { "pcf8523", 0 }, 370 { } 371 }; 372 MODULE_DEVICE_TABLE(i2c, pcf8523_id); 373 374 #ifdef CONFIG_OF 375 static const struct of_device_id pcf8523_of_match[] = { 376 { .compatible = "nxp,pcf8523" }, 377 { } 378 }; 379 MODULE_DEVICE_TABLE(of, pcf8523_of_match); 380 #endif 381 382 static struct i2c_driver pcf8523_driver = { 383 .driver = { 384 .name = DRIVER_NAME, 385 .of_match_table = of_match_ptr(pcf8523_of_match), 386 }, 387 .probe = pcf8523_probe, 388 .id_table = pcf8523_id, 389 }; 390 module_i2c_driver(pcf8523_driver); 391 392 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); 393 MODULE_DESCRIPTION("NXP PCF8523 RTC driver"); 394 MODULE_LICENSE("GPL v2"); 395