1 /* 2 * RTC driver for the Micro Crystal RV8803 3 * 4 * Copyright (C) 2015 Micro Crystal SA 5 * 6 * Alexandre Belloni <alexandre.belloni@free-electrons.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 * 12 */ 13 14 #include <linux/bcd.h> 15 #include <linux/bitops.h> 16 #include <linux/i2c.h> 17 #include <linux/interrupt.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/rtc.h> 21 22 #define RV8803_SEC 0x00 23 #define RV8803_MIN 0x01 24 #define RV8803_HOUR 0x02 25 #define RV8803_WEEK 0x03 26 #define RV8803_DAY 0x04 27 #define RV8803_MONTH 0x05 28 #define RV8803_YEAR 0x06 29 #define RV8803_RAM 0x07 30 #define RV8803_ALARM_MIN 0x08 31 #define RV8803_ALARM_HOUR 0x09 32 #define RV8803_ALARM_WEEK_OR_DAY 0x0A 33 #define RV8803_EXT 0x0D 34 #define RV8803_FLAG 0x0E 35 #define RV8803_CTRL 0x0F 36 37 #define RV8803_EXT_WADA BIT(6) 38 39 #define RV8803_FLAG_V1F BIT(0) 40 #define RV8803_FLAG_V2F BIT(1) 41 #define RV8803_FLAG_AF BIT(3) 42 #define RV8803_FLAG_TF BIT(4) 43 #define RV8803_FLAG_UF BIT(5) 44 45 #define RV8803_CTRL_RESET BIT(0) 46 47 #define RV8803_CTRL_EIE BIT(2) 48 #define RV8803_CTRL_AIE BIT(3) 49 #define RV8803_CTRL_TIE BIT(4) 50 #define RV8803_CTRL_UIE BIT(5) 51 52 struct rv8803_data { 53 struct i2c_client *client; 54 struct rtc_device *rtc; 55 struct mutex flags_lock; 56 u8 ctrl; 57 }; 58 59 static irqreturn_t rv8803_handle_irq(int irq, void *dev_id) 60 { 61 struct i2c_client *client = dev_id; 62 struct rv8803_data *rv8803 = i2c_get_clientdata(client); 63 unsigned long events = 0; 64 int flags; 65 66 mutex_lock(&rv8803->flags_lock); 67 68 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); 69 if (flags <= 0) { 70 mutex_unlock(&rv8803->flags_lock); 71 return IRQ_NONE; 72 } 73 74 if (flags & RV8803_FLAG_V1F) 75 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 76 77 if (flags & RV8803_FLAG_V2F) 78 dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 79 80 if (flags & RV8803_FLAG_TF) { 81 flags &= ~RV8803_FLAG_TF; 82 rv8803->ctrl &= ~RV8803_CTRL_TIE; 83 events |= RTC_PF; 84 } 85 86 if (flags & RV8803_FLAG_AF) { 87 flags &= ~RV8803_FLAG_AF; 88 rv8803->ctrl &= ~RV8803_CTRL_AIE; 89 events |= RTC_AF; 90 } 91 92 if (flags & RV8803_FLAG_UF) { 93 flags &= ~RV8803_FLAG_UF; 94 rv8803->ctrl &= ~RV8803_CTRL_UIE; 95 events |= RTC_UF; 96 } 97 98 if (events) { 99 rtc_update_irq(rv8803->rtc, 1, events); 100 i2c_smbus_write_byte_data(client, RV8803_FLAG, flags); 101 i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL, 102 rv8803->ctrl); 103 } 104 105 mutex_unlock(&rv8803->flags_lock); 106 107 return IRQ_HANDLED; 108 } 109 110 static int rv8803_get_time(struct device *dev, struct rtc_time *tm) 111 { 112 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 113 u8 date1[7]; 114 u8 date2[7]; 115 u8 *date = date1; 116 int ret, flags; 117 118 flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG); 119 if (flags < 0) 120 return flags; 121 122 if (flags & RV8803_FLAG_V2F) { 123 dev_warn(dev, "Voltage low, data is invalid.\n"); 124 return -EINVAL; 125 } 126 127 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC, 128 7, date); 129 if (ret != 7) 130 return ret < 0 ? ret : -EIO; 131 132 if ((date1[RV8803_SEC] & 0x7f) == bin2bcd(59)) { 133 ret = i2c_smbus_read_i2c_block_data(rv8803->client, RV8803_SEC, 134 7, date2); 135 if (ret != 7) 136 return ret < 0 ? ret : -EIO; 137 138 if ((date2[RV8803_SEC] & 0x7f) != bin2bcd(59)) 139 date = date2; 140 } 141 142 tm->tm_sec = bcd2bin(date[RV8803_SEC] & 0x7f); 143 tm->tm_min = bcd2bin(date[RV8803_MIN] & 0x7f); 144 tm->tm_hour = bcd2bin(date[RV8803_HOUR] & 0x3f); 145 tm->tm_wday = ffs(date[RV8803_WEEK] & 0x7f); 146 tm->tm_mday = bcd2bin(date[RV8803_DAY] & 0x3f); 147 tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1; 148 tm->tm_year = bcd2bin(date[RV8803_YEAR]) + 100; 149 150 return rtc_valid_tm(tm); 151 } 152 153 static int rv8803_set_time(struct device *dev, struct rtc_time *tm) 154 { 155 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 156 u8 date[7]; 157 int flags, ret; 158 159 if ((tm->tm_year < 100) || (tm->tm_year > 199)) 160 return -EINVAL; 161 162 date[RV8803_SEC] = bin2bcd(tm->tm_sec); 163 date[RV8803_MIN] = bin2bcd(tm->tm_min); 164 date[RV8803_HOUR] = bin2bcd(tm->tm_hour); 165 date[RV8803_WEEK] = 1 << (tm->tm_wday); 166 date[RV8803_DAY] = bin2bcd(tm->tm_mday); 167 date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1); 168 date[RV8803_YEAR] = bin2bcd(tm->tm_year - 100); 169 170 ret = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_SEC, 171 7, date); 172 if (ret < 0) 173 return ret; 174 175 mutex_lock(&rv8803->flags_lock); 176 177 flags = i2c_smbus_read_byte_data(rv8803->client, RV8803_FLAG); 178 if (flags < 0) { 179 mutex_unlock(&rv8803->flags_lock); 180 return flags; 181 } 182 183 ret = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, 184 flags & ~RV8803_FLAG_V2F); 185 186 mutex_unlock(&rv8803->flags_lock); 187 188 return ret; 189 } 190 191 static int rv8803_get_alarm(struct device *dev, struct rtc_wkalrm *alrm) 192 { 193 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 194 struct i2c_client *client = rv8803->client; 195 u8 alarmvals[3]; 196 int flags, ret; 197 198 ret = i2c_smbus_read_i2c_block_data(client, RV8803_ALARM_MIN, 199 3, alarmvals); 200 if (ret != 3) 201 return ret < 0 ? ret : -EIO; 202 203 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); 204 if (flags < 0) 205 return flags; 206 207 alrm->time.tm_sec = 0; 208 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f); 209 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f); 210 alrm->time.tm_wday = -1; 211 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f); 212 alrm->time.tm_mon = -1; 213 alrm->time.tm_year = -1; 214 215 alrm->enabled = !!(rv8803->ctrl & RV8803_CTRL_AIE); 216 alrm->pending = (flags & RV8803_FLAG_AF) && alrm->enabled; 217 218 return 0; 219 } 220 221 static int rv8803_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 222 { 223 struct i2c_client *client = to_i2c_client(dev); 224 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 225 u8 alarmvals[3]; 226 u8 ctrl[2]; 227 int ret, err; 228 229 /* The alarm has no seconds, round up to nearest minute */ 230 if (alrm->time.tm_sec) { 231 time64_t alarm_time = rtc_tm_to_time64(&alrm->time); 232 233 alarm_time += 60 - alrm->time.tm_sec; 234 rtc_time64_to_tm(alarm_time, &alrm->time); 235 } 236 237 mutex_lock(&rv8803->flags_lock); 238 239 ret = i2c_smbus_read_i2c_block_data(client, RV8803_FLAG, 2, ctrl); 240 if (ret != 2) { 241 mutex_unlock(&rv8803->flags_lock); 242 return ret < 0 ? ret : -EIO; 243 } 244 245 alarmvals[0] = bin2bcd(alrm->time.tm_min); 246 alarmvals[1] = bin2bcd(alrm->time.tm_hour); 247 alarmvals[2] = bin2bcd(alrm->time.tm_mday); 248 249 if (rv8803->ctrl & (RV8803_CTRL_AIE | RV8803_CTRL_UIE)) { 250 rv8803->ctrl &= ~(RV8803_CTRL_AIE | RV8803_CTRL_UIE); 251 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL, 252 rv8803->ctrl); 253 if (err) { 254 mutex_unlock(&rv8803->flags_lock); 255 return err; 256 } 257 } 258 259 ctrl[1] &= ~RV8803_FLAG_AF; 260 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_FLAG, ctrl[1]); 261 mutex_unlock(&rv8803->flags_lock); 262 if (err) 263 return err; 264 265 err = i2c_smbus_write_i2c_block_data(rv8803->client, RV8803_ALARM_MIN, 266 3, alarmvals); 267 if (err) 268 return err; 269 270 if (alrm->enabled) { 271 if (rv8803->rtc->uie_rtctimer.enabled) 272 rv8803->ctrl |= RV8803_CTRL_UIE; 273 if (rv8803->rtc->aie_timer.enabled) 274 rv8803->ctrl |= RV8803_CTRL_AIE; 275 276 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_CTRL, 277 rv8803->ctrl); 278 if (err) 279 return err; 280 } 281 282 return 0; 283 } 284 285 static int rv8803_alarm_irq_enable(struct device *dev, unsigned int enabled) 286 { 287 struct i2c_client *client = to_i2c_client(dev); 288 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 289 int ctrl, flags, err; 290 291 ctrl = rv8803->ctrl; 292 293 if (enabled) { 294 if (rv8803->rtc->uie_rtctimer.enabled) 295 ctrl |= RV8803_CTRL_UIE; 296 if (rv8803->rtc->aie_timer.enabled) 297 ctrl |= RV8803_CTRL_AIE; 298 } else { 299 if (!rv8803->rtc->uie_rtctimer.enabled) 300 ctrl &= ~RV8803_CTRL_UIE; 301 if (!rv8803->rtc->aie_timer.enabled) 302 ctrl &= ~RV8803_CTRL_AIE; 303 } 304 305 mutex_lock(&rv8803->flags_lock); 306 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); 307 if (flags < 0) { 308 mutex_unlock(&rv8803->flags_lock); 309 return flags; 310 } 311 flags &= ~(RV8803_FLAG_AF | RV8803_FLAG_UF); 312 err = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags); 313 mutex_unlock(&rv8803->flags_lock); 314 if (err) 315 return err; 316 317 if (ctrl != rv8803->ctrl) { 318 rv8803->ctrl = ctrl; 319 err = i2c_smbus_write_byte_data(client, RV8803_CTRL, 320 rv8803->ctrl); 321 if (err) 322 return err; 323 } 324 325 return 0; 326 } 327 328 static int rv8803_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) 329 { 330 struct i2c_client *client = to_i2c_client(dev); 331 struct rv8803_data *rv8803 = dev_get_drvdata(dev); 332 int flags, ret = 0; 333 334 switch (cmd) { 335 case RTC_VL_READ: 336 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); 337 if (flags < 0) 338 return flags; 339 340 if (flags & RV8803_FLAG_V1F) 341 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 342 343 if (flags & RV8803_FLAG_V2F) 344 dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 345 346 flags &= RV8803_FLAG_V1F | RV8803_FLAG_V2F; 347 348 if (copy_to_user((void __user *)arg, &flags, sizeof(int))) 349 return -EFAULT; 350 351 return 0; 352 353 case RTC_VL_CLR: 354 mutex_lock(&rv8803->flags_lock); 355 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); 356 if (flags < 0) { 357 mutex_unlock(&rv8803->flags_lock); 358 return flags; 359 } 360 361 flags &= ~(RV8803_FLAG_V1F | RV8803_FLAG_V2F); 362 ret = i2c_smbus_write_byte_data(client, RV8803_FLAG, flags); 363 mutex_unlock(&rv8803->flags_lock); 364 if (ret < 0) 365 return ret; 366 367 return 0; 368 369 default: 370 return -ENOIOCTLCMD; 371 } 372 } 373 374 static ssize_t rv8803_nvram_write(struct file *filp, struct kobject *kobj, 375 struct bin_attribute *attr, 376 char *buf, loff_t off, size_t count) 377 { 378 struct device *dev = kobj_to_dev(kobj); 379 struct i2c_client *client = to_i2c_client(dev); 380 int ret; 381 382 ret = i2c_smbus_write_byte_data(client, RV8803_RAM, buf[0]); 383 if (ret < 0) 384 return ret; 385 386 return 1; 387 } 388 389 static ssize_t rv8803_nvram_read(struct file *filp, struct kobject *kobj, 390 struct bin_attribute *attr, 391 char *buf, loff_t off, size_t count) 392 { 393 struct device *dev = kobj_to_dev(kobj); 394 struct i2c_client *client = to_i2c_client(dev); 395 int ret; 396 397 ret = i2c_smbus_read_byte_data(client, RV8803_RAM); 398 if (ret < 0) 399 return ret; 400 401 buf[0] = ret; 402 403 return 1; 404 } 405 406 static struct bin_attribute rv8803_nvram_attr = { 407 .attr = { 408 .name = "nvram", 409 .mode = S_IRUGO | S_IWUSR, 410 }, 411 .size = 1, 412 .read = rv8803_nvram_read, 413 .write = rv8803_nvram_write, 414 }; 415 416 static struct rtc_class_ops rv8803_rtc_ops = { 417 .read_time = rv8803_get_time, 418 .set_time = rv8803_set_time, 419 .ioctl = rv8803_ioctl, 420 }; 421 422 static int rv8803_probe(struct i2c_client *client, 423 const struct i2c_device_id *id) 424 { 425 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); 426 struct rv8803_data *rv8803; 427 int err, flags; 428 429 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 430 I2C_FUNC_SMBUS_I2C_BLOCK)) { 431 dev_err(&adapter->dev, "doesn't support I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_I2C_BLOCK\n"); 432 return -EIO; 433 } 434 435 rv8803 = devm_kzalloc(&client->dev, sizeof(struct rv8803_data), 436 GFP_KERNEL); 437 if (!rv8803) 438 return -ENOMEM; 439 440 mutex_init(&rv8803->flags_lock); 441 rv8803->client = client; 442 i2c_set_clientdata(client, rv8803); 443 444 flags = i2c_smbus_read_byte_data(client, RV8803_FLAG); 445 if (flags < 0) 446 return flags; 447 448 if (flags & RV8803_FLAG_V1F) 449 dev_warn(&client->dev, "Voltage low, temperature compensation stopped.\n"); 450 451 if (flags & RV8803_FLAG_V2F) 452 dev_warn(&client->dev, "Voltage low, data loss detected.\n"); 453 454 if (flags & RV8803_FLAG_AF) 455 dev_warn(&client->dev, "An alarm maybe have been missed.\n"); 456 457 if (client->irq > 0) { 458 err = devm_request_threaded_irq(&client->dev, client->irq, 459 NULL, rv8803_handle_irq, 460 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 461 "rv8803", client); 462 if (err) { 463 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n"); 464 client->irq = 0; 465 } else { 466 rv8803_rtc_ops.read_alarm = rv8803_get_alarm; 467 rv8803_rtc_ops.set_alarm = rv8803_set_alarm; 468 rv8803_rtc_ops.alarm_irq_enable = rv8803_alarm_irq_enable; 469 } 470 } 471 472 rv8803->rtc = devm_rtc_device_register(&client->dev, client->name, 473 &rv8803_rtc_ops, THIS_MODULE); 474 if (IS_ERR(rv8803->rtc)) { 475 dev_err(&client->dev, "unable to register the class device\n"); 476 return PTR_ERR(rv8803->rtc); 477 } 478 479 err = i2c_smbus_write_byte_data(rv8803->client, RV8803_EXT, 480 RV8803_EXT_WADA); 481 if (err) 482 return err; 483 484 err = device_create_bin_file(&client->dev, &rv8803_nvram_attr); 485 if (err) 486 return err; 487 488 rv8803->rtc->max_user_freq = 1; 489 490 return 0; 491 } 492 493 static int rv8803_remove(struct i2c_client *client) 494 { 495 device_remove_bin_file(&client->dev, &rv8803_nvram_attr); 496 497 return 0; 498 } 499 500 static const struct i2c_device_id rv8803_id[] = { 501 { "rv8803", 0 }, 502 { "rx8900", 0 }, 503 { } 504 }; 505 MODULE_DEVICE_TABLE(i2c, rv8803_id); 506 507 static struct i2c_driver rv8803_driver = { 508 .driver = { 509 .name = "rtc-rv8803", 510 }, 511 .probe = rv8803_probe, 512 .remove = rv8803_remove, 513 .id_table = rv8803_id, 514 }; 515 module_i2c_driver(rv8803_driver); 516 517 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>"); 518 MODULE_DESCRIPTION("Micro Crystal RV8803 RTC driver"); 519 MODULE_LICENSE("GPL v2"); 520