0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/bcd.h>
0010 #include <linux/bitops.h>
0011 #include <linux/i2c.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/regmap.h>
0015 #include <linux/rtc.h>
0016
0017 #define RX8010_SEC 0x10
0018 #define RX8010_MIN 0x11
0019 #define RX8010_HOUR 0x12
0020 #define RX8010_WDAY 0x13
0021 #define RX8010_MDAY 0x14
0022 #define RX8010_MONTH 0x15
0023 #define RX8010_YEAR 0x16
0024 #define RX8010_RESV17 0x17
0025 #define RX8010_ALMIN 0x18
0026 #define RX8010_ALHOUR 0x19
0027 #define RX8010_ALWDAY 0x1A
0028 #define RX8010_TCOUNT0 0x1B
0029 #define RX8010_TCOUNT1 0x1C
0030 #define RX8010_EXT 0x1D
0031 #define RX8010_FLAG 0x1E
0032 #define RX8010_CTRL 0x1F
0033
0034 #define RX8010_RESV30 0x30
0035 #define RX8010_RESV31 0x31
0036 #define RX8010_IRQ 0x32
0037
0038 #define RX8010_EXT_WADA BIT(3)
0039
0040 #define RX8010_FLAG_VLF BIT(1)
0041 #define RX8010_FLAG_AF BIT(3)
0042 #define RX8010_FLAG_TF BIT(4)
0043 #define RX8010_FLAG_UF BIT(5)
0044
0045 #define RX8010_CTRL_AIE BIT(3)
0046 #define RX8010_CTRL_UIE BIT(5)
0047 #define RX8010_CTRL_STOP BIT(6)
0048 #define RX8010_CTRL_TEST BIT(7)
0049
0050 #define RX8010_ALARM_AE BIT(7)
0051
0052 static const struct i2c_device_id rx8010_id[] = {
0053 { "rx8010", 0 },
0054 { }
0055 };
0056 MODULE_DEVICE_TABLE(i2c, rx8010_id);
0057
0058 static const __maybe_unused struct of_device_id rx8010_of_match[] = {
0059 { .compatible = "epson,rx8010" },
0060 { }
0061 };
0062 MODULE_DEVICE_TABLE(of, rx8010_of_match);
0063
0064 struct rx8010_data {
0065 struct regmap *regs;
0066 struct rtc_device *rtc;
0067 u8 ctrlreg;
0068 };
0069
0070 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
0071 {
0072 struct i2c_client *client = dev_id;
0073 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
0074 int flagreg, err;
0075
0076 rtc_lock(rx8010->rtc);
0077
0078 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
0079 if (err) {
0080 rtc_unlock(rx8010->rtc);
0081 return IRQ_NONE;
0082 }
0083
0084 if (flagreg & RX8010_FLAG_VLF)
0085 dev_warn(&client->dev, "Frequency stop detected\n");
0086
0087 if (flagreg & RX8010_FLAG_TF) {
0088 flagreg &= ~RX8010_FLAG_TF;
0089 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
0090 }
0091
0092 if (flagreg & RX8010_FLAG_AF) {
0093 flagreg &= ~RX8010_FLAG_AF;
0094 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
0095 }
0096
0097 if (flagreg & RX8010_FLAG_UF) {
0098 flagreg &= ~RX8010_FLAG_UF;
0099 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
0100 }
0101
0102 err = regmap_write(rx8010->regs, RX8010_FLAG, flagreg);
0103 rtc_unlock(rx8010->rtc);
0104 return err ? IRQ_NONE : IRQ_HANDLED;
0105 }
0106
0107 static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
0108 {
0109 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0110 u8 date[RX8010_YEAR - RX8010_SEC + 1];
0111 int flagreg, err;
0112
0113 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
0114 if (err)
0115 return err;
0116
0117 if (flagreg & RX8010_FLAG_VLF) {
0118 dev_warn(dev, "Frequency stop detected\n");
0119 return -EINVAL;
0120 }
0121
0122 err = regmap_bulk_read(rx8010->regs, RX8010_SEC, date, sizeof(date));
0123 if (err)
0124 return err;
0125
0126 dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
0127 dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
0128 dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
0129 dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
0130 dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
0131 dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
0132 dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
0133
0134 return 0;
0135 }
0136
0137 static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
0138 {
0139 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0140 u8 date[RX8010_YEAR - RX8010_SEC + 1];
0141 int err;
0142
0143
0144 err = regmap_set_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP);
0145 if (err)
0146 return err;
0147
0148 date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
0149 date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
0150 date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
0151 date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
0152 date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
0153 date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
0154 date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
0155
0156 err = regmap_bulk_write(rx8010->regs, RX8010_SEC, date, sizeof(date));
0157 if (err)
0158 return err;
0159
0160
0161 err = regmap_clear_bits(rx8010->regs, RX8010_CTRL, RX8010_CTRL_STOP);
0162 if (err)
0163 return err;
0164
0165 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_VLF);
0166 if (err)
0167 return err;
0168
0169 return 0;
0170 }
0171
0172 static int rx8010_init(struct device *dev)
0173 {
0174 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0175 u8 ctrl[2];
0176 int need_clear = 0, err;
0177
0178
0179 err = regmap_write(rx8010->regs, RX8010_RESV17, 0xD8);
0180 if (err)
0181 return err;
0182
0183 err = regmap_write(rx8010->regs, RX8010_RESV30, 0x00);
0184 if (err)
0185 return err;
0186
0187 err = regmap_write(rx8010->regs, RX8010_RESV31, 0x08);
0188 if (err)
0189 return err;
0190
0191 err = regmap_write(rx8010->regs, RX8010_IRQ, 0x00);
0192 if (err)
0193 return err;
0194
0195 err = regmap_bulk_read(rx8010->regs, RX8010_FLAG, ctrl, 2);
0196 if (err)
0197 return err;
0198
0199 if (ctrl[0] & RX8010_FLAG_VLF)
0200 dev_warn(dev, "Frequency stop was detected\n");
0201
0202 if (ctrl[0] & RX8010_FLAG_AF) {
0203 dev_warn(dev, "Alarm was detected\n");
0204 need_clear = 1;
0205 }
0206
0207 if (ctrl[0] & RX8010_FLAG_TF)
0208 need_clear = 1;
0209
0210 if (ctrl[0] & RX8010_FLAG_UF)
0211 need_clear = 1;
0212
0213 if (need_clear) {
0214 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
0215 err = regmap_write(rx8010->regs, RX8010_FLAG, ctrl[0]);
0216 if (err)
0217 return err;
0218 }
0219
0220 rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
0221
0222 return 0;
0223 }
0224
0225 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
0226 {
0227 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0228 u8 alarmvals[3];
0229 int flagreg, err;
0230
0231 err = regmap_bulk_read(rx8010->regs, RX8010_ALMIN, alarmvals, 3);
0232 if (err)
0233 return err;
0234
0235 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
0236 if (err)
0237 return err;
0238
0239 t->time.tm_sec = 0;
0240 t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
0241 t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
0242
0243 if (!(alarmvals[2] & RX8010_ALARM_AE))
0244 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
0245
0246 t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
0247 t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
0248
0249 return 0;
0250 }
0251
0252 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
0253 {
0254 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0255 u8 alarmvals[3];
0256 int err;
0257
0258 if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
0259 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
0260 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
0261 if (err)
0262 return err;
0263 }
0264
0265 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF);
0266 if (err)
0267 return err;
0268
0269 alarmvals[0] = bin2bcd(t->time.tm_min);
0270 alarmvals[1] = bin2bcd(t->time.tm_hour);
0271 alarmvals[2] = bin2bcd(t->time.tm_mday);
0272
0273 err = regmap_bulk_write(rx8010->regs, RX8010_ALMIN, alarmvals, 2);
0274 if (err)
0275 return err;
0276
0277 err = regmap_clear_bits(rx8010->regs, RX8010_EXT, RX8010_EXT_WADA);
0278 if (err)
0279 return err;
0280
0281 if (alarmvals[2] == 0)
0282 alarmvals[2] |= RX8010_ALARM_AE;
0283
0284 err = regmap_write(rx8010->regs, RX8010_ALWDAY, alarmvals[2]);
0285 if (err)
0286 return err;
0287
0288 if (t->enabled) {
0289 if (rx8010->rtc->uie_rtctimer.enabled)
0290 rx8010->ctrlreg |= RX8010_CTRL_UIE;
0291 if (rx8010->rtc->aie_timer.enabled)
0292 rx8010->ctrlreg |=
0293 (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
0294
0295 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
0296 if (err)
0297 return err;
0298 }
0299
0300 return 0;
0301 }
0302
0303 static int rx8010_alarm_irq_enable(struct device *dev,
0304 unsigned int enabled)
0305 {
0306 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0307 int err;
0308 u8 ctrl;
0309
0310 ctrl = rx8010->ctrlreg;
0311
0312 if (enabled) {
0313 if (rx8010->rtc->uie_rtctimer.enabled)
0314 ctrl |= RX8010_CTRL_UIE;
0315 if (rx8010->rtc->aie_timer.enabled)
0316 ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
0317 } else {
0318 if (!rx8010->rtc->uie_rtctimer.enabled)
0319 ctrl &= ~RX8010_CTRL_UIE;
0320 if (!rx8010->rtc->aie_timer.enabled)
0321 ctrl &= ~RX8010_CTRL_AIE;
0322 }
0323
0324 err = regmap_clear_bits(rx8010->regs, RX8010_FLAG, RX8010_FLAG_AF);
0325 if (err)
0326 return err;
0327
0328 if (ctrl != rx8010->ctrlreg) {
0329 rx8010->ctrlreg = ctrl;
0330 err = regmap_write(rx8010->regs, RX8010_CTRL, rx8010->ctrlreg);
0331 if (err)
0332 return err;
0333 }
0334
0335 return 0;
0336 }
0337
0338 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
0339 {
0340 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
0341 int tmp, flagreg, err;
0342
0343 switch (cmd) {
0344 case RTC_VL_READ:
0345 err = regmap_read(rx8010->regs, RX8010_FLAG, &flagreg);
0346 if (err)
0347 return err;
0348
0349 tmp = flagreg & RX8010_FLAG_VLF ? RTC_VL_DATA_INVALID : 0;
0350 return put_user(tmp, (unsigned int __user *)arg);
0351
0352 default:
0353 return -ENOIOCTLCMD;
0354 }
0355 }
0356
0357 static const struct rtc_class_ops rx8010_rtc_ops = {
0358 .read_time = rx8010_get_time,
0359 .set_time = rx8010_set_time,
0360 .ioctl = rx8010_ioctl,
0361 .read_alarm = rx8010_read_alarm,
0362 .set_alarm = rx8010_set_alarm,
0363 .alarm_irq_enable = rx8010_alarm_irq_enable,
0364 };
0365
0366 static const struct regmap_config rx8010_regmap_config = {
0367 .name = "rx8010-rtc",
0368 .reg_bits = 8,
0369 .val_bits = 8,
0370 };
0371
0372 static int rx8010_probe(struct i2c_client *client)
0373 {
0374 struct device *dev = &client->dev;
0375 struct rx8010_data *rx8010;
0376 int err = 0;
0377
0378 rx8010 = devm_kzalloc(dev, sizeof(*rx8010), GFP_KERNEL);
0379 if (!rx8010)
0380 return -ENOMEM;
0381
0382 i2c_set_clientdata(client, rx8010);
0383
0384 rx8010->regs = devm_regmap_init_i2c(client, &rx8010_regmap_config);
0385 if (IS_ERR(rx8010->regs))
0386 return PTR_ERR(rx8010->regs);
0387
0388 err = rx8010_init(dev);
0389 if (err)
0390 return err;
0391
0392 rx8010->rtc = devm_rtc_allocate_device(dev);
0393 if (IS_ERR(rx8010->rtc))
0394 return PTR_ERR(rx8010->rtc);
0395
0396 if (client->irq > 0) {
0397 dev_info(dev, "IRQ %d supplied\n", client->irq);
0398 err = devm_request_threaded_irq(dev, client->irq, NULL,
0399 rx8010_irq_1_handler,
0400 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0401 "rx8010", client);
0402 if (err) {
0403 dev_err(dev, "unable to request IRQ\n");
0404 return err;
0405 }
0406 } else {
0407 clear_bit(RTC_FEATURE_ALARM, rx8010->rtc->features);
0408 }
0409
0410 rx8010->rtc->ops = &rx8010_rtc_ops;
0411 rx8010->rtc->max_user_freq = 1;
0412 rx8010->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
0413 rx8010->rtc->range_max = RTC_TIMESTAMP_END_2099;
0414
0415 return devm_rtc_register_device(rx8010->rtc);
0416 }
0417
0418 static struct i2c_driver rx8010_driver = {
0419 .driver = {
0420 .name = "rtc-rx8010",
0421 .of_match_table = of_match_ptr(rx8010_of_match),
0422 },
0423 .probe_new = rx8010_probe,
0424 .id_table = rx8010_id,
0425 };
0426
0427 module_i2c_driver(rx8010_driver);
0428
0429 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
0430 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
0431 MODULE_LICENSE("GPL v2");