1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Avionic Design GmbH 4 */ 5 6 #include <linux/bcd.h> 7 #include <linux/i2c.h> 8 #include <linux/module.h> 9 #include <linux/rtc.h> 10 #include <linux/of.h> 11 12 #define DRIVER_NAME "rtc-pcf8523" 13 14 #define REG_CONTROL1 0x00 15 #define REG_CONTROL1_CAP_SEL BIT(7) 16 #define REG_CONTROL1_STOP BIT(5) 17 18 #define REG_CONTROL3 0x02 19 #define REG_CONTROL3_PM_BLD BIT(7) /* battery low detection disabled */ 20 #define REG_CONTROL3_PM_VDD BIT(6) /* switch-over disabled */ 21 #define REG_CONTROL3_PM_DSM BIT(5) /* direct switching mode */ 22 #define REG_CONTROL3_PM_MASK 0xe0 23 #define REG_CONTROL3_BLF BIT(2) /* battery low bit, read-only */ 24 25 #define REG_SECONDS 0x03 26 #define REG_SECONDS_OS BIT(7) 27 28 #define REG_MINUTES 0x04 29 #define REG_HOURS 0x05 30 #define REG_DAYS 0x06 31 #define REG_WEEKDAYS 0x07 32 #define REG_MONTHS 0x08 33 #define REG_YEARS 0x09 34 35 #define REG_OFFSET 0x0e 36 #define REG_OFFSET_MODE BIT(7) 37 38 static int pcf8523_read(struct i2c_client *client, u8 reg, u8 *valuep) 39 { 40 struct i2c_msg msgs[2]; 41 u8 value = 0; 42 int err; 43 44 msgs[0].addr = client->addr; 45 msgs[0].flags = 0; 46 msgs[0].len = sizeof(reg); 47 msgs[0].buf = ® 48 49 msgs[1].addr = client->addr; 50 msgs[1].flags = I2C_M_RD; 51 msgs[1].len = sizeof(value); 52 msgs[1].buf = &value; 53 54 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 55 if (err < 0) 56 return err; 57 58 *valuep = value; 59 60 return 0; 61 } 62 63 static int pcf8523_write(struct i2c_client *client, u8 reg, u8 value) 64 { 65 u8 buffer[2] = { reg, value }; 66 struct i2c_msg msg; 67 int err; 68 69 msg.addr = client->addr; 70 msg.flags = 0; 71 msg.len = sizeof(buffer); 72 msg.buf = buffer; 73 74 err = i2c_transfer(client->adapter, &msg, 1); 75 if (err < 0) 76 return err; 77 78 return 0; 79 } 80 81 static int pcf8523_voltage_low(struct i2c_client *client) 82 { 83 u8 value; 84 int err; 85 86 err = pcf8523_read(client, REG_CONTROL3, &value); 87 if (err < 0) 88 return err; 89 90 return !!(value & REG_CONTROL3_BLF); 91 } 92 93 static int pcf8523_load_capacitance(struct i2c_client *client) 94 { 95 u32 load; 96 u8 value; 97 int err; 98 99 err = pcf8523_read(client, REG_CONTROL1, &value); 100 if (err < 0) 101 return err; 102 103 load = 12500; 104 of_property_read_u32(client->dev.of_node, "quartz-load-femtofarads", 105 &load); 106 107 switch (load) { 108 default: 109 dev_warn(&client->dev, "Unknown quartz-load-femtofarads value: %d. Assuming 12500", 110 load); 111 fallthrough; 112 case 12500: 113 value |= REG_CONTROL1_CAP_SEL; 114 break; 115 case 7000: 116 value &= ~REG_CONTROL1_CAP_SEL; 117 break; 118 } 119 120 err = pcf8523_write(client, REG_CONTROL1, value); 121 122 return err; 123 } 124 125 static int pcf8523_set_pm(struct i2c_client *client, u8 pm) 126 { 127 u8 value; 128 int err; 129 130 err = pcf8523_read(client, REG_CONTROL3, &value); 131 if (err < 0) 132 return err; 133 134 value = (value & ~REG_CONTROL3_PM_MASK) | pm; 135 136 err = pcf8523_write(client, REG_CONTROL3, value); 137 if (err < 0) 138 return err; 139 140 return 0; 141 } 142 143 static int pcf8523_stop_rtc(struct i2c_client *client) 144 { 145 u8 value; 146 int err; 147 148 err = pcf8523_read(client, REG_CONTROL1, &value); 149 if (err < 0) 150 return err; 151 152 value |= REG_CONTROL1_STOP; 153 154 err = pcf8523_write(client, REG_CONTROL1, value); 155 if (err < 0) 156 return err; 157 158 return 0; 159 } 160 161 static int pcf8523_start_rtc(struct i2c_client *client) 162 { 163 u8 value; 164 int err; 165 166 err = pcf8523_read(client, REG_CONTROL1, &value); 167 if (err < 0) 168 return err; 169 170 value &= ~REG_CONTROL1_STOP; 171 172 err = pcf8523_write(client, REG_CONTROL1, value); 173 if (err < 0) 174 return err; 175 176 return 0; 177 } 178 179 static int pcf8523_rtc_read_time(struct device *dev, struct rtc_time *tm) 180 { 181 struct i2c_client *client = to_i2c_client(dev); 182 u8 start = REG_SECONDS, regs[7]; 183 struct i2c_msg msgs[2]; 184 int err; 185 186 err = pcf8523_voltage_low(client); 187 if (err < 0) { 188 return err; 189 } else if (err > 0) { 190 dev_err(dev, "low voltage detected, time is unreliable\n"); 191 return -EINVAL; 192 } 193 194 msgs[0].addr = client->addr; 195 msgs[0].flags = 0; 196 msgs[0].len = 1; 197 msgs[0].buf = &start; 198 199 msgs[1].addr = client->addr; 200 msgs[1].flags = I2C_M_RD; 201 msgs[1].len = sizeof(regs); 202 msgs[1].buf = regs; 203 204 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 205 if (err < 0) 206 return err; 207 208 if (regs[0] & REG_SECONDS_OS) 209 return -EINVAL; 210 211 tm->tm_sec = bcd2bin(regs[0] & 0x7f); 212 tm->tm_min = bcd2bin(regs[1] & 0x7f); 213 tm->tm_hour = bcd2bin(regs[2] & 0x3f); 214 tm->tm_mday = bcd2bin(regs[3] & 0x3f); 215 tm->tm_wday = regs[4] & 0x7; 216 tm->tm_mon = bcd2bin(regs[5] & 0x1f) - 1; 217 tm->tm_year = bcd2bin(regs[6]) + 100; 218 219 return 0; 220 } 221 222 static int pcf8523_rtc_set_time(struct device *dev, struct rtc_time *tm) 223 { 224 struct i2c_client *client = to_i2c_client(dev); 225 struct i2c_msg msg; 226 u8 regs[8]; 227 int err; 228 229 err = pcf8523_stop_rtc(client); 230 if (err < 0) 231 return err; 232 233 regs[0] = REG_SECONDS; 234 /* This will purposely overwrite REG_SECONDS_OS */ 235 regs[1] = bin2bcd(tm->tm_sec); 236 regs[2] = bin2bcd(tm->tm_min); 237 regs[3] = bin2bcd(tm->tm_hour); 238 regs[4] = bin2bcd(tm->tm_mday); 239 regs[5] = tm->tm_wday; 240 regs[6] = bin2bcd(tm->tm_mon + 1); 241 regs[7] = bin2bcd(tm->tm_year - 100); 242 243 msg.addr = client->addr; 244 msg.flags = 0; 245 msg.len = sizeof(regs); 246 msg.buf = regs; 247 248 err = i2c_transfer(client->adapter, &msg, 1); 249 if (err < 0) { 250 /* 251 * If the time cannot be set, restart the RTC anyway. Note 252 * that errors are ignored if the RTC cannot be started so 253 * that we have a chance to propagate the original error. 254 */ 255 pcf8523_start_rtc(client); 256 return err; 257 } 258 259 return pcf8523_start_rtc(client); 260 } 261 262 #ifdef CONFIG_RTC_INTF_DEV 263 static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd, 264 unsigned long arg) 265 { 266 struct i2c_client *client = to_i2c_client(dev); 267 int ret; 268 269 switch (cmd) { 270 case RTC_VL_READ: 271 ret = pcf8523_voltage_low(client); 272 if (ret < 0) 273 return ret; 274 if (ret) 275 ret = RTC_VL_BACKUP_LOW; 276 277 return put_user(ret, (unsigned int __user *)arg); 278 279 default: 280 return -ENOIOCTLCMD; 281 } 282 } 283 #else 284 #define pcf8523_rtc_ioctl NULL 285 #endif 286 287 static int pcf8523_rtc_read_offset(struct device *dev, long *offset) 288 { 289 struct i2c_client *client = to_i2c_client(dev); 290 int err; 291 u8 value; 292 s8 val; 293 294 err = pcf8523_read(client, REG_OFFSET, &value); 295 if (err < 0) 296 return err; 297 298 /* sign extend the 7-bit offset value */ 299 val = value << 1; 300 *offset = (value & REG_OFFSET_MODE ? 4069 : 4340) * (val >> 1); 301 302 return 0; 303 } 304 305 static int pcf8523_rtc_set_offset(struct device *dev, long offset) 306 { 307 struct i2c_client *client = to_i2c_client(dev); 308 long reg_m0, reg_m1; 309 u8 value; 310 311 reg_m0 = clamp(DIV_ROUND_CLOSEST(offset, 4340), -64L, 63L); 312 reg_m1 = clamp(DIV_ROUND_CLOSEST(offset, 4069), -64L, 63L); 313 314 if (abs(reg_m0 * 4340 - offset) < abs(reg_m1 * 4069 - offset)) 315 value = reg_m0 & 0x7f; 316 else 317 value = (reg_m1 & 0x7f) | REG_OFFSET_MODE; 318 319 return pcf8523_write(client, REG_OFFSET, value); 320 } 321 322 static const struct rtc_class_ops pcf8523_rtc_ops = { 323 .read_time = pcf8523_rtc_read_time, 324 .set_time = pcf8523_rtc_set_time, 325 .ioctl = pcf8523_rtc_ioctl, 326 .read_offset = pcf8523_rtc_read_offset, 327 .set_offset = pcf8523_rtc_set_offset, 328 }; 329 330 static int pcf8523_probe(struct i2c_client *client, 331 const struct i2c_device_id *id) 332 { 333 struct rtc_device *rtc; 334 int err; 335 336 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) 337 return -ENODEV; 338 339 err = pcf8523_load_capacitance(client); 340 if (err < 0) 341 dev_warn(&client->dev, "failed to set xtal load capacitance: %d", 342 err); 343 344 err = pcf8523_set_pm(client, 0); 345 if (err < 0) 346 return err; 347 348 rtc = devm_rtc_allocate_device(&client->dev); 349 if (IS_ERR(rtc)) 350 return PTR_ERR(rtc); 351 352 rtc->ops = &pcf8523_rtc_ops; 353 rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 354 rtc->range_max = RTC_TIMESTAMP_END_2099; 355 356 return devm_rtc_register_device(rtc); 357 } 358 359 static const struct i2c_device_id pcf8523_id[] = { 360 { "pcf8523", 0 }, 361 { } 362 }; 363 MODULE_DEVICE_TABLE(i2c, pcf8523_id); 364 365 #ifdef CONFIG_OF 366 static const struct of_device_id pcf8523_of_match[] = { 367 { .compatible = "nxp,pcf8523" }, 368 { .compatible = "microcrystal,rv8523" }, 369 { } 370 }; 371 MODULE_DEVICE_TABLE(of, pcf8523_of_match); 372 #endif 373 374 static struct i2c_driver pcf8523_driver = { 375 .driver = { 376 .name = DRIVER_NAME, 377 .of_match_table = of_match_ptr(pcf8523_of_match), 378 }, 379 .probe = pcf8523_probe, 380 .id_table = pcf8523_id, 381 }; 382 module_i2c_driver(pcf8523_driver); 383 384 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); 385 MODULE_DESCRIPTION("NXP PCF8523 RTC driver"); 386 MODULE_LICENSE("GPL v2"); 387