0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <linux/clk-provider.h>
0015 #include <linux/i2c.h>
0016 #include <linux/bcd.h>
0017 #include <linux/rtc.h>
0018 #include <linux/slab.h>
0019 #include <linux/module.h>
0020 #include <linux/of.h>
0021 #include <linux/err.h>
0022
0023 #define PCF8563_REG_ST1 0x00
0024 #define PCF8563_REG_ST2 0x01
0025 #define PCF8563_BIT_AIE BIT(1)
0026 #define PCF8563_BIT_AF BIT(3)
0027 #define PCF8563_BITS_ST2_N (7 << 5)
0028
0029 #define PCF8563_REG_SC 0x02
0030 #define PCF8563_REG_MN 0x03
0031 #define PCF8563_REG_HR 0x04
0032 #define PCF8563_REG_DM 0x05
0033 #define PCF8563_REG_DW 0x06
0034 #define PCF8563_REG_MO 0x07
0035 #define PCF8563_REG_YR 0x08
0036
0037 #define PCF8563_REG_AMN 0x09
0038
0039 #define PCF8563_REG_CLKO 0x0D
0040 #define PCF8563_REG_CLKO_FE 0x80
0041 #define PCF8563_REG_CLKO_F_MASK 0x03
0042 #define PCF8563_REG_CLKO_F_32768HZ 0x00
0043 #define PCF8563_REG_CLKO_F_1024HZ 0x01
0044 #define PCF8563_REG_CLKO_F_32HZ 0x02
0045 #define PCF8563_REG_CLKO_F_1HZ 0x03
0046
0047 #define PCF8563_REG_TMRC 0x0E
0048 #define PCF8563_TMRC_ENABLE BIT(7)
0049 #define PCF8563_TMRC_4096 0
0050 #define PCF8563_TMRC_64 1
0051 #define PCF8563_TMRC_1 2
0052 #define PCF8563_TMRC_1_60 3
0053 #define PCF8563_TMRC_MASK 3
0054
0055 #define PCF8563_REG_TMR 0x0F
0056
0057 #define PCF8563_SC_LV 0x80
0058 #define PCF8563_MO_C 0x80
0059
0060 static struct i2c_driver pcf8563_driver;
0061
0062 struct pcf8563 {
0063 struct rtc_device *rtc;
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 int c_polarity;
0079
0080 struct i2c_client *client;
0081 #ifdef CONFIG_COMMON_CLK
0082 struct clk_hw clkout_hw;
0083 #endif
0084 };
0085
0086 static int pcf8563_read_block_data(struct i2c_client *client, unsigned char reg,
0087 unsigned char length, unsigned char *buf)
0088 {
0089 struct i2c_msg msgs[] = {
0090 {
0091 .addr = client->addr,
0092 .len = 1,
0093 .buf = ®,
0094 },
0095 {
0096 .addr = client->addr,
0097 .flags = I2C_M_RD,
0098 .len = length,
0099 .buf = buf
0100 },
0101 };
0102
0103 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
0104 dev_err(&client->dev, "%s: read error\n", __func__);
0105 return -EIO;
0106 }
0107
0108 return 0;
0109 }
0110
0111 static int pcf8563_write_block_data(struct i2c_client *client,
0112 unsigned char reg, unsigned char length,
0113 unsigned char *buf)
0114 {
0115 int i, err;
0116
0117 for (i = 0; i < length; i++) {
0118 unsigned char data[2] = { reg + i, buf[i] };
0119
0120 err = i2c_master_send(client, data, sizeof(data));
0121 if (err != sizeof(data)) {
0122 dev_err(&client->dev,
0123 "%s: err=%d addr=%02x, data=%02x\n",
0124 __func__, err, data[0], data[1]);
0125 return -EIO;
0126 }
0127 }
0128
0129 return 0;
0130 }
0131
0132 static int pcf8563_set_alarm_mode(struct i2c_client *client, bool on)
0133 {
0134 unsigned char buf;
0135 int err;
0136
0137 err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, &buf);
0138 if (err < 0)
0139 return err;
0140
0141 if (on)
0142 buf |= PCF8563_BIT_AIE;
0143 else
0144 buf &= ~PCF8563_BIT_AIE;
0145
0146 buf &= ~(PCF8563_BIT_AF | PCF8563_BITS_ST2_N);
0147
0148 err = pcf8563_write_block_data(client, PCF8563_REG_ST2, 1, &buf);
0149 if (err < 0) {
0150 dev_err(&client->dev, "%s: write error\n", __func__);
0151 return -EIO;
0152 }
0153
0154 return 0;
0155 }
0156
0157 static int pcf8563_get_alarm_mode(struct i2c_client *client, unsigned char *en,
0158 unsigned char *pen)
0159 {
0160 unsigned char buf;
0161 int err;
0162
0163 err = pcf8563_read_block_data(client, PCF8563_REG_ST2, 1, &buf);
0164 if (err)
0165 return err;
0166
0167 if (en)
0168 *en = !!(buf & PCF8563_BIT_AIE);
0169 if (pen)
0170 *pen = !!(buf & PCF8563_BIT_AF);
0171
0172 return 0;
0173 }
0174
0175 static irqreturn_t pcf8563_irq(int irq, void *dev_id)
0176 {
0177 struct pcf8563 *pcf8563 = i2c_get_clientdata(dev_id);
0178 int err;
0179 char pending;
0180
0181 err = pcf8563_get_alarm_mode(pcf8563->client, NULL, &pending);
0182 if (err)
0183 return IRQ_NONE;
0184
0185 if (pending) {
0186 rtc_update_irq(pcf8563->rtc, 1, RTC_IRQF | RTC_AF);
0187 pcf8563_set_alarm_mode(pcf8563->client, 1);
0188 return IRQ_HANDLED;
0189 }
0190
0191 return IRQ_NONE;
0192 }
0193
0194
0195
0196
0197
0198 static int pcf8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
0199 {
0200 struct i2c_client *client = to_i2c_client(dev);
0201 struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
0202 unsigned char buf[9];
0203 int err;
0204
0205 err = pcf8563_read_block_data(client, PCF8563_REG_ST1, 9, buf);
0206 if (err)
0207 return err;
0208
0209 if (buf[PCF8563_REG_SC] & PCF8563_SC_LV) {
0210 dev_err(&client->dev,
0211 "low voltage detected, date/time is not reliable.\n");
0212 return -EINVAL;
0213 }
0214
0215 dev_dbg(&client->dev,
0216 "%s: raw data is st1=%02x, st2=%02x, sec=%02x, min=%02x, hr=%02x, "
0217 "mday=%02x, wday=%02x, mon=%02x, year=%02x\n",
0218 __func__,
0219 buf[0], buf[1], buf[2], buf[3],
0220 buf[4], buf[5], buf[6], buf[7],
0221 buf[8]);
0222
0223
0224 tm->tm_sec = bcd2bin(buf[PCF8563_REG_SC] & 0x7F);
0225 tm->tm_min = bcd2bin(buf[PCF8563_REG_MN] & 0x7F);
0226 tm->tm_hour = bcd2bin(buf[PCF8563_REG_HR] & 0x3F);
0227 tm->tm_mday = bcd2bin(buf[PCF8563_REG_DM] & 0x3F);
0228 tm->tm_wday = buf[PCF8563_REG_DW] & 0x07;
0229 tm->tm_mon = bcd2bin(buf[PCF8563_REG_MO] & 0x1F) - 1;
0230 tm->tm_year = bcd2bin(buf[PCF8563_REG_YR]) + 100;
0231
0232 pcf8563->c_polarity = (buf[PCF8563_REG_MO] & PCF8563_MO_C) ?
0233 (tm->tm_year >= 100) : (tm->tm_year < 100);
0234
0235 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
0236 "mday=%d, mon=%d, year=%d, wday=%d\n",
0237 __func__,
0238 tm->tm_sec, tm->tm_min, tm->tm_hour,
0239 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
0240
0241 return 0;
0242 }
0243
0244 static int pcf8563_rtc_set_time(struct device *dev, struct rtc_time *tm)
0245 {
0246 struct i2c_client *client = to_i2c_client(dev);
0247 struct pcf8563 *pcf8563 = i2c_get_clientdata(client);
0248 unsigned char buf[9];
0249
0250 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
0251 "mday=%d, mon=%d, year=%d, wday=%d\n",
0252 __func__,
0253 tm->tm_sec, tm->tm_min, tm->tm_hour,
0254 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
0255
0256
0257 buf[PCF8563_REG_SC] = bin2bcd(tm->tm_sec);
0258 buf[PCF8563_REG_MN] = bin2bcd(tm->tm_min);
0259 buf[PCF8563_REG_HR] = bin2bcd(tm->tm_hour);
0260
0261 buf[PCF8563_REG_DM] = bin2bcd(tm->tm_mday);
0262
0263
0264 buf[PCF8563_REG_MO] = bin2bcd(tm->tm_mon + 1);
0265
0266
0267 buf[PCF8563_REG_YR] = bin2bcd(tm->tm_year - 100);
0268 if (pcf8563->c_polarity ? (tm->tm_year >= 100) : (tm->tm_year < 100))
0269 buf[PCF8563_REG_MO] |= PCF8563_MO_C;
0270
0271 buf[PCF8563_REG_DW] = tm->tm_wday & 0x07;
0272
0273 return pcf8563_write_block_data(client, PCF8563_REG_SC,
0274 9 - PCF8563_REG_SC, buf + PCF8563_REG_SC);
0275 }
0276
0277 static int pcf8563_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
0278 {
0279 struct i2c_client *client = to_i2c_client(dev);
0280 int ret;
0281
0282 switch (cmd) {
0283 case RTC_VL_READ:
0284 ret = i2c_smbus_read_byte_data(client, PCF8563_REG_SC);
0285 if (ret < 0)
0286 return ret;
0287
0288 return put_user(ret & PCF8563_SC_LV ? RTC_VL_DATA_INVALID : 0,
0289 (unsigned int __user *)arg);
0290 default:
0291 return -ENOIOCTLCMD;
0292 }
0293 }
0294
0295 static int pcf8563_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *tm)
0296 {
0297 struct i2c_client *client = to_i2c_client(dev);
0298 unsigned char buf[4];
0299 int err;
0300
0301 err = pcf8563_read_block_data(client, PCF8563_REG_AMN, 4, buf);
0302 if (err)
0303 return err;
0304
0305 dev_dbg(&client->dev,
0306 "%s: raw data is min=%02x, hr=%02x, mday=%02x, wday=%02x\n",
0307 __func__, buf[0], buf[1], buf[2], buf[3]);
0308
0309 tm->time.tm_sec = 0;
0310 tm->time.tm_min = bcd2bin(buf[0] & 0x7F);
0311 tm->time.tm_hour = bcd2bin(buf[1] & 0x3F);
0312 tm->time.tm_mday = bcd2bin(buf[2] & 0x3F);
0313 tm->time.tm_wday = bcd2bin(buf[3] & 0x7);
0314
0315 err = pcf8563_get_alarm_mode(client, &tm->enabled, &tm->pending);
0316 if (err < 0)
0317 return err;
0318
0319 dev_dbg(&client->dev, "%s: tm is mins=%d, hours=%d, mday=%d, wday=%d,"
0320 " enabled=%d, pending=%d\n", __func__, tm->time.tm_min,
0321 tm->time.tm_hour, tm->time.tm_mday, tm->time.tm_wday,
0322 tm->enabled, tm->pending);
0323
0324 return 0;
0325 }
0326
0327 static int pcf8563_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm)
0328 {
0329 struct i2c_client *client = to_i2c_client(dev);
0330 unsigned char buf[4];
0331 int err;
0332
0333 buf[0] = bin2bcd(tm->time.tm_min);
0334 buf[1] = bin2bcd(tm->time.tm_hour);
0335 buf[2] = bin2bcd(tm->time.tm_mday);
0336 buf[3] = tm->time.tm_wday & 0x07;
0337
0338 err = pcf8563_write_block_data(client, PCF8563_REG_AMN, 4, buf);
0339 if (err)
0340 return err;
0341
0342 return pcf8563_set_alarm_mode(client, !!tm->enabled);
0343 }
0344
0345 static int pcf8563_irq_enable(struct device *dev, unsigned int enabled)
0346 {
0347 dev_dbg(dev, "%s: en=%d\n", __func__, enabled);
0348 return pcf8563_set_alarm_mode(to_i2c_client(dev), !!enabled);
0349 }
0350
0351 #ifdef CONFIG_COMMON_CLK
0352
0353
0354
0355
0356 #define clkout_hw_to_pcf8563(_hw) container_of(_hw, struct pcf8563, clkout_hw)
0357
0358 static const int clkout_rates[] = {
0359 32768,
0360 1024,
0361 32,
0362 1,
0363 };
0364
0365 static unsigned long pcf8563_clkout_recalc_rate(struct clk_hw *hw,
0366 unsigned long parent_rate)
0367 {
0368 struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
0369 struct i2c_client *client = pcf8563->client;
0370 unsigned char buf;
0371 int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
0372
0373 if (ret < 0)
0374 return 0;
0375
0376 buf &= PCF8563_REG_CLKO_F_MASK;
0377 return clkout_rates[buf];
0378 }
0379
0380 static long pcf8563_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
0381 unsigned long *prate)
0382 {
0383 int i;
0384
0385 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
0386 if (clkout_rates[i] <= rate)
0387 return clkout_rates[i];
0388
0389 return 0;
0390 }
0391
0392 static int pcf8563_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
0393 unsigned long parent_rate)
0394 {
0395 struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
0396 struct i2c_client *client = pcf8563->client;
0397 unsigned char buf;
0398 int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
0399 int i;
0400
0401 if (ret < 0)
0402 return ret;
0403
0404 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
0405 if (clkout_rates[i] == rate) {
0406 buf &= ~PCF8563_REG_CLKO_F_MASK;
0407 buf |= i;
0408 ret = pcf8563_write_block_data(client,
0409 PCF8563_REG_CLKO, 1,
0410 &buf);
0411 return ret;
0412 }
0413
0414 return -EINVAL;
0415 }
0416
0417 static int pcf8563_clkout_control(struct clk_hw *hw, bool enable)
0418 {
0419 struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
0420 struct i2c_client *client = pcf8563->client;
0421 unsigned char buf;
0422 int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
0423
0424 if (ret < 0)
0425 return ret;
0426
0427 if (enable)
0428 buf |= PCF8563_REG_CLKO_FE;
0429 else
0430 buf &= ~PCF8563_REG_CLKO_FE;
0431
0432 ret = pcf8563_write_block_data(client, PCF8563_REG_CLKO, 1, &buf);
0433 return ret;
0434 }
0435
0436 static int pcf8563_clkout_prepare(struct clk_hw *hw)
0437 {
0438 return pcf8563_clkout_control(hw, 1);
0439 }
0440
0441 static void pcf8563_clkout_unprepare(struct clk_hw *hw)
0442 {
0443 pcf8563_clkout_control(hw, 0);
0444 }
0445
0446 static int pcf8563_clkout_is_prepared(struct clk_hw *hw)
0447 {
0448 struct pcf8563 *pcf8563 = clkout_hw_to_pcf8563(hw);
0449 struct i2c_client *client = pcf8563->client;
0450 unsigned char buf;
0451 int ret = pcf8563_read_block_data(client, PCF8563_REG_CLKO, 1, &buf);
0452
0453 if (ret < 0)
0454 return ret;
0455
0456 return !!(buf & PCF8563_REG_CLKO_FE);
0457 }
0458
0459 static const struct clk_ops pcf8563_clkout_ops = {
0460 .prepare = pcf8563_clkout_prepare,
0461 .unprepare = pcf8563_clkout_unprepare,
0462 .is_prepared = pcf8563_clkout_is_prepared,
0463 .recalc_rate = pcf8563_clkout_recalc_rate,
0464 .round_rate = pcf8563_clkout_round_rate,
0465 .set_rate = pcf8563_clkout_set_rate,
0466 };
0467
0468 static struct clk *pcf8563_clkout_register_clk(struct pcf8563 *pcf8563)
0469 {
0470 struct i2c_client *client = pcf8563->client;
0471 struct device_node *node = client->dev.of_node;
0472 struct clk *clk;
0473 struct clk_init_data init;
0474 int ret;
0475 unsigned char buf;
0476
0477
0478 buf = 0;
0479 ret = pcf8563_write_block_data(client, PCF8563_REG_CLKO, 1, &buf);
0480 if (ret < 0)
0481 return ERR_PTR(ret);
0482
0483 init.name = "pcf8563-clkout";
0484 init.ops = &pcf8563_clkout_ops;
0485 init.flags = 0;
0486 init.parent_names = NULL;
0487 init.num_parents = 0;
0488 pcf8563->clkout_hw.init = &init;
0489
0490
0491 of_property_read_string(node, "clock-output-names", &init.name);
0492
0493
0494 clk = devm_clk_register(&client->dev, &pcf8563->clkout_hw);
0495
0496 if (!IS_ERR(clk))
0497 of_clk_add_provider(node, of_clk_src_simple_get, clk);
0498
0499 return clk;
0500 }
0501 #endif
0502
0503 static const struct rtc_class_ops pcf8563_rtc_ops = {
0504 .ioctl = pcf8563_rtc_ioctl,
0505 .read_time = pcf8563_rtc_read_time,
0506 .set_time = pcf8563_rtc_set_time,
0507 .read_alarm = pcf8563_rtc_read_alarm,
0508 .set_alarm = pcf8563_rtc_set_alarm,
0509 .alarm_irq_enable = pcf8563_irq_enable,
0510 };
0511
0512 static int pcf8563_probe(struct i2c_client *client)
0513 {
0514 struct pcf8563 *pcf8563;
0515 int err;
0516 unsigned char buf;
0517
0518 dev_dbg(&client->dev, "%s\n", __func__);
0519
0520 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0521 return -ENODEV;
0522
0523 pcf8563 = devm_kzalloc(&client->dev, sizeof(struct pcf8563),
0524 GFP_KERNEL);
0525 if (!pcf8563)
0526 return -ENOMEM;
0527
0528 i2c_set_clientdata(client, pcf8563);
0529 pcf8563->client = client;
0530 device_set_wakeup_capable(&client->dev, 1);
0531
0532
0533 buf = PCF8563_TMRC_1_60;
0534 err = pcf8563_write_block_data(client, PCF8563_REG_TMRC, 1, &buf);
0535 if (err < 0) {
0536 dev_err(&client->dev, "%s: write error\n", __func__);
0537 return err;
0538 }
0539
0540
0541 buf = 0;
0542 err = pcf8563_write_block_data(client, PCF8563_REG_ST2, 1, &buf);
0543 if (err < 0) {
0544 dev_err(&client->dev, "%s: write error\n", __func__);
0545 return err;
0546 }
0547
0548 pcf8563->rtc = devm_rtc_allocate_device(&client->dev);
0549 if (IS_ERR(pcf8563->rtc))
0550 return PTR_ERR(pcf8563->rtc);
0551
0552 pcf8563->rtc->ops = &pcf8563_rtc_ops;
0553
0554 set_bit(RTC_FEATURE_ALARM_RES_MINUTE, pcf8563->rtc->features);
0555 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, pcf8563->rtc->features);
0556 pcf8563->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
0557 pcf8563->rtc->range_max = RTC_TIMESTAMP_END_2099;
0558 pcf8563->rtc->set_start_time = true;
0559
0560 if (client->irq > 0) {
0561 err = devm_request_threaded_irq(&client->dev, client->irq,
0562 NULL, pcf8563_irq,
0563 IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_LOW,
0564 pcf8563_driver.driver.name, client);
0565 if (err) {
0566 dev_err(&client->dev, "unable to request IRQ %d\n",
0567 client->irq);
0568 return err;
0569 }
0570 }
0571
0572 err = devm_rtc_register_device(pcf8563->rtc);
0573 if (err)
0574 return err;
0575
0576 #ifdef CONFIG_COMMON_CLK
0577
0578 pcf8563_clkout_register_clk(pcf8563);
0579 #endif
0580
0581 return 0;
0582 }
0583
0584 static const struct i2c_device_id pcf8563_id[] = {
0585 { "pcf8563", 0 },
0586 { "rtc8564", 0 },
0587 { "pca8565", 0 },
0588 { }
0589 };
0590 MODULE_DEVICE_TABLE(i2c, pcf8563_id);
0591
0592 #ifdef CONFIG_OF
0593 static const struct of_device_id pcf8563_of_match[] = {
0594 { .compatible = "nxp,pcf8563" },
0595 { .compatible = "epson,rtc8564" },
0596 { .compatible = "microcrystal,rv8564" },
0597 { .compatible = "nxp,pca8565" },
0598 {}
0599 };
0600 MODULE_DEVICE_TABLE(of, pcf8563_of_match);
0601 #endif
0602
0603 static struct i2c_driver pcf8563_driver = {
0604 .driver = {
0605 .name = "rtc-pcf8563",
0606 .of_match_table = of_match_ptr(pcf8563_of_match),
0607 },
0608 .probe_new = pcf8563_probe,
0609 .id_table = pcf8563_id,
0610 };
0611
0612 module_i2c_driver(pcf8563_driver);
0613
0614 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
0615 MODULE_DESCRIPTION("Philips PCF8563/Epson RTC8564 RTC driver");
0616 MODULE_LICENSE("GPL");