1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the Epson RTC module RX-8010 SJ 4 * 5 * Copyright(C) Timesys Corporation 2015 6 * Copyright(C) General Electric Company 2015 7 */ 8 9 #include <linux/bcd.h> 10 #include <linux/bitops.h> 11 #include <linux/i2c.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/regmap.h> 15 #include <linux/rtc.h> 16 17 #define RX8010_SEC 0x10 18 #define RX8010_MIN 0x11 19 #define RX8010_HOUR 0x12 20 #define RX8010_WDAY 0x13 21 #define RX8010_MDAY 0x14 22 #define RX8010_MONTH 0x15 23 #define RX8010_YEAR 0x16 24 #define RX8010_RESV17 0x17 25 #define RX8010_ALMIN 0x18 26 #define RX8010_ALHOUR 0x19 27 #define RX8010_ALWDAY 0x1A 28 #define RX8010_TCOUNT0 0x1B 29 #define RX8010_TCOUNT1 0x1C 30 #define RX8010_EXT 0x1D 31 #define RX8010_FLAG 0x1E 32 #define RX8010_CTRL 0x1F 33 /* 0x20 to 0x2F are user registers */ 34 #define RX8010_RESV30 0x30 35 #define RX8010_RESV31 0x31 36 #define RX8010_IRQ 0x32 37 38 #define RX8010_EXT_WADA BIT(3) 39 40 #define RX8010_FLAG_VLF BIT(1) 41 #define RX8010_FLAG_AF BIT(3) 42 #define RX8010_FLAG_TF BIT(4) 43 #define RX8010_FLAG_UF BIT(5) 44 45 #define RX8010_CTRL_AIE BIT(3) 46 #define RX8010_CTRL_UIE BIT(5) 47 #define RX8010_CTRL_STOP BIT(6) 48 #define RX8010_CTRL_TEST BIT(7) 49 50 #define RX8010_ALARM_AE BIT(7) 51 52 static const struct i2c_device_id rx8010_id[] = { 53 { .name = "rx8010" }, 54 { } 55 }; 56 MODULE_DEVICE_TABLE(i2c, rx8010_id); 57 58 static const __maybe_unused struct of_device_id rx8010_of_match[] = { 59 { .compatible = "epson,rx8010" }, 60 { } 61 }; 62 MODULE_DEVICE_TABLE(of, rx8010_of_match); 63 64 struct rx8010_data { 65 struct regmap *regs; 66 struct rtc_device *rtc; 67 u8 ctrlreg; 68 }; 69 70 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id) 71 { 72 struct i2c_client *client = dev_id; 73 struct rx8010_data *rx8010 = i2c_get_clientdata(client); 74 int flagreg, err; 75 76 guard(rtc_ops)(rx8010->rtc); 77 78 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); 79 if (err) { 80 return IRQ_NONE; 81 } 82 83 if (flagreg & RX8010_FLAG_VLF) 84 dev_warn(&client->dev, "Frequency stop detected\n"); 85 86 if (flagreg & RX8010_FLAG_TF) { 87 flagreg &= ~RX8010_FLAG_TF; 88 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF); 89 } 90 91 if (flagreg & RX8010_FLAG_AF) { 92 flagreg &= ~RX8010_FLAG_AF; 93 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF); 94 } 95 96 if (flagreg & RX8010_FLAG_UF) { 97 flagreg &= ~RX8010_FLAG_UF; 98 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF); 99 } 100 101 err = regmap_write(rx8010->regs, RX8010_FLAG, flagreg); 102 return err ? IRQ_NONE : IRQ_HANDLED; 103 } 104 105 static int rx8010_get_time(struct device *dev, struct rtc_time *dt) 106 { 107 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 108 u8 date[RX8010_YEAR - RX8010_SEC + 1]; 109 int flagreg, err; 110 111 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); 112 if (err) 113 return err; 114 115 if (flagreg & RX8010_FLAG_VLF) { 116 dev_warn(dev, "Frequency stop detected\n"); 117 return -EINVAL; 118 } 119 120 err = regmap_bulk_read(rx8010->regs, RX8010_SEC, date, sizeof(date)); 121 if (err) 122 return err; 123 124 dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f); 125 dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f); 126 dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f); 127 dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f); 128 dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1; 129 dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100; 130 dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f); 131 132 return 0; 133 } 134 135 static int rx8010_set_time(struct device *dev, struct rtc_time *dt) 136 { 137 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 138 u8 date[RX8010_YEAR - RX8010_SEC + 1]; 139 int err; 140 141 /* set STOP bit before changing clock/calendar */ 142 err = regmap_set_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP); 143 if (err) 144 return err; 145 146 date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec); 147 date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min); 148 date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour); 149 date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday); 150 date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1); 151 date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100); 152 date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday); 153 154 err = regmap_bulk_write(rx8010->regs, RX8010_SEC, date, sizeof(date)); 155 if (err) 156 return err; 157 158 /* clear STOP bit after changing clock/calendar */ 159 err = regmap_clear_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP); 160 if (err) 161 return err; 162 163 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_VLF); 164 if (err) 165 return err; 166 167 return 0; 168 } 169 170 static int rx8010_init(struct device *dev) 171 { 172 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 173 u8 ctrl[2]; 174 int need_clear = 0, err; 175 176 /* Initialize reserved registers as specified in datasheet */ 177 err = regmap_write(rx8010->regs, RX8010_RESV17, 0xD8); 178 if (err) 179 return err; 180 181 err = regmap_write(rx8010->regs, RX8010_RESV30, 0x00); 182 if (err) 183 return err; 184 185 err = regmap_write(rx8010->regs, RX8010_RESV31, 0x08); 186 if (err) 187 return err; 188 189 err = regmap_write(rx8010->regs, RX8010_IRQ, 0x00); 190 if (err) 191 return err; 192 193 err = regmap_bulk_read(rx8010->regs, RX8010_FLAG, ctrl, 2); 194 if (err) 195 return err; 196 197 if (ctrl[0] & RX8010_FLAG_VLF) 198 dev_warn(dev, "Frequency stop was detected\n"); 199 200 if (ctrl[0] & RX8010_FLAG_AF) { 201 dev_warn(dev, "Alarm was detected\n"); 202 need_clear = 1; 203 } 204 205 if (ctrl[0] & RX8010_FLAG_TF) 206 need_clear = 1; 207 208 if (ctrl[0] & RX8010_FLAG_UF) 209 need_clear = 1; 210 211 if (need_clear) { 212 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF); 213 err = regmap_write(rx8010->regs, RX8010_FLAG, ctrl[0]); 214 if (err) 215 return err; 216 } 217 218 rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST); 219 220 return 0; 221 } 222 223 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t) 224 { 225 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 226 u8 alarmvals[3]; 227 int flagreg, err; 228 229 err = regmap_bulk_read(rx8010->regs, RX8010_ALMIN, alarmvals, 3); 230 if (err) 231 return err; 232 233 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); 234 if (err) 235 return err; 236 237 t->time.tm_sec = 0; 238 t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 239 t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 240 241 if (!(alarmvals[2] & RX8010_ALARM_AE)) 242 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f); 243 244 t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE); 245 t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled; 246 247 return 0; 248 } 249 250 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t) 251 { 252 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 253 u8 alarmvals[3]; 254 int err; 255 256 if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) { 257 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE); 258 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg); 259 if (err) 260 return err; 261 } 262 263 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF); 264 if (err) 265 return err; 266 267 alarmvals[0] = bin2bcd(t->time.tm_min); 268 alarmvals[1] = bin2bcd(t->time.tm_hour); 269 alarmvals[2] = bin2bcd(t->time.tm_mday); 270 271 err = regmap_bulk_write(rx8010->regs, RX8010_ALMIN, alarmvals, 2); 272 if (err) 273 return err; 274 275 err = regmap_clear_bits(rx8010->regs, RX8010_EXT, RX8010_EXT_WADA); 276 if (err) 277 return err; 278 279 if (alarmvals[2] == 0) 280 alarmvals[2] |= RX8010_ALARM_AE; 281 282 err = regmap_write(rx8010->regs, RX8010_ALWDAY, alarmvals[2]); 283 if (err) 284 return err; 285 286 if (t->enabled) { 287 if (rx8010->rtc->uie_rtctimer.enabled) 288 rx8010->ctrlreg |= RX8010_CTRL_UIE; 289 if (rx8010->rtc->aie_timer.enabled) 290 rx8010->ctrlreg |= 291 (RX8010_CTRL_AIE | RX8010_CTRL_UIE); 292 293 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg); 294 if (err) 295 return err; 296 } 297 298 return 0; 299 } 300 301 static int rx8010_alarm_irq_enable(struct device *dev, 302 unsigned int enabled) 303 { 304 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 305 int err; 306 u8 ctrl; 307 308 ctrl = rx8010->ctrlreg; 309 310 if (enabled) { 311 if (rx8010->rtc->uie_rtctimer.enabled) 312 ctrl |= RX8010_CTRL_UIE; 313 if (rx8010->rtc->aie_timer.enabled) 314 ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE); 315 } else { 316 if (!rx8010->rtc->uie_rtctimer.enabled) 317 ctrl &= ~RX8010_CTRL_UIE; 318 if (!rx8010->rtc->aie_timer.enabled) 319 ctrl &= ~RX8010_CTRL_AIE; 320 } 321 322 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF); 323 if (err) 324 return err; 325 326 if (ctrl != rx8010->ctrlreg) { 327 rx8010->ctrlreg = ctrl; 328 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg); 329 if (err) 330 return err; 331 } 332 333 return 0; 334 } 335 336 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 337 { 338 struct rx8010_data *rx8010 = dev_get_drvdata(dev); 339 int tmp, flagreg, err; 340 341 switch (cmd) { 342 case RTC_VL_READ: 343 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg); 344 if (err) 345 return err; 346 347 tmp = flagreg & RX8010_FLAG_VLF ? RTC_VL_DATA_INVALID : 0; 348 return put_user(tmp, (unsigned int __user *)arg); 349 350 default: 351 return -ENOIOCTLCMD; 352 } 353 } 354 355 static const struct rtc_class_ops rx8010_rtc_ops = { 356 .read_time = rx8010_get_time, 357 .set_time = rx8010_set_time, 358 .ioctl = rx8010_ioctl, 359 .read_alarm = rx8010_read_alarm, 360 .set_alarm = rx8010_set_alarm, 361 .alarm_irq_enable = rx8010_alarm_irq_enable, 362 }; 363 364 static const struct regmap_config rx8010_regmap_config = { 365 .name = "rx8010-rtc", 366 .reg_bits = 8, 367 .val_bits = 8, 368 }; 369 370 static int rx8010_probe(struct i2c_client *client) 371 { 372 struct device *dev = &client->dev; 373 struct rx8010_data *rx8010; 374 int err = 0; 375 376 rx8010 = devm_kzalloc_obj(dev, *rx8010); 377 if (!rx8010) 378 return -ENOMEM; 379 380 i2c_set_clientdata(client, rx8010); 381 382 rx8010->regs = devm_regmap_init_i2c(client, &rx8010_regmap_config); 383 if (IS_ERR(rx8010->regs)) 384 return PTR_ERR(rx8010->regs); 385 386 err = rx8010_init(dev); 387 if (err) 388 return err; 389 390 rx8010->rtc = devm_rtc_allocate_device(dev); 391 if (IS_ERR(rx8010->rtc)) 392 return PTR_ERR(rx8010->rtc); 393 394 if (client->irq > 0) { 395 unsigned long irqflags = IRQF_TRIGGER_LOW; 396 397 if (dev_fwnode(&client->dev)) 398 irqflags = 0; 399 400 err = devm_request_threaded_irq(dev, client->irq, NULL, 401 rx8010_irq_1_handler, 402 irqflags | IRQF_ONESHOT, 403 "rx8010", client); 404 if (err) { 405 dev_err(dev, "unable to request IRQ\n"); 406 return err; 407 } 408 } else { 409 clear_bit(RTC_FEATURE_ALARM, rx8010->rtc->features); 410 } 411 412 rx8010->rtc->ops = &rx8010_rtc_ops; 413 rx8010->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; 414 rx8010->rtc->range_max = RTC_TIMESTAMP_END_2099; 415 416 return devm_rtc_register_device(rx8010->rtc); 417 } 418 419 static struct i2c_driver rx8010_driver = { 420 .driver = { 421 .name = "rtc-rx8010", 422 .of_match_table = of_match_ptr(rx8010_of_match), 423 }, 424 .probe = rx8010_probe, 425 .id_table = rx8010_id, 426 }; 427 428 module_i2c_driver(rx8010_driver); 429 430 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>"); 431 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver"); 432 MODULE_LICENSE("GPL v2"); 433