123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #include <linux/i2c.h>
- #include <linux/bcd.h>
- #include <linux/rtc.h>
- #include <linux/module.h>
- #define PCF85063_REG_CTRL1 0x00
- #define PCF85063_REG_CTRL1_STOP BIT(5)
- #define PCF85063_REG_CTRL2 0x01
- #define PCF85063_REG_SC 0x04
- #define PCF85063_REG_SC_OS 0x80
- #define PCF85063_REG_MN 0x05
- #define PCF85063_REG_HR 0x06
- #define PCF85063_REG_DM 0x07
- #define PCF85063_REG_DW 0x08
- #define PCF85063_REG_MO 0x09
- #define PCF85063_REG_YR 0x0A
- static struct i2c_driver pcf85063_driver;
- static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
- {
- s32 ret;
- ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
- if (ret < 0) {
- dev_err(&client->dev, "Failing to stop the clock\n");
- return -EIO;
- }
-
- ret |= PCF85063_REG_CTRL1_STOP;
- ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
- if (ret < 0) {
- dev_err(&client->dev, "Failing to stop the clock\n");
- return -EIO;
- }
- *ctrl1 = ret;
- return 0;
- }
- static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
- {
- s32 ret;
-
- ctrl1 &= PCF85063_REG_CTRL1_STOP;
- ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
- if (ret < 0) {
- dev_err(&client->dev, "Failing to start the clock\n");
- return -EIO;
- }
- return 0;
- }
- static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
- {
- int rc;
- u8 regs[7];
-
- rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
- sizeof(regs), regs);
- if (rc != sizeof(regs)) {
- dev_err(&client->dev, "date/time register read error\n");
- return -EIO;
- }
-
-
- tm->tm_sec = bcd2bin(regs[0] & 0x7F);
- tm->tm_min = bcd2bin(regs[1] & 0x7F);
- tm->tm_hour = bcd2bin(regs[2] & 0x3F);
- tm->tm_mday = bcd2bin(regs[3] & 0x3F);
- tm->tm_wday = regs[4] & 0x07;
- tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1;
- tm->tm_year = bcd2bin(regs[6]);
- tm->tm_year += 100;
- return rtc_valid_tm(tm);
- }
- static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
- {
- int rc;
- u8 regs[7];
- u8 ctrl1;
- if ((tm->tm_year < 100) || (tm->tm_year > 199))
- return -EINVAL;
-
- rc = pcf85063_stop_clock(client, &ctrl1);
- if (rc != 0)
- return rc;
-
- regs[0] = bin2bcd(tm->tm_sec) & 0x7F;
- regs[1] = bin2bcd(tm->tm_min);
- regs[2] = bin2bcd(tm->tm_hour);
-
- regs[3] = bin2bcd(tm->tm_mday);
-
- regs[4] = tm->tm_wday & 0x07;
-
- regs[5] = bin2bcd(tm->tm_mon + 1);
-
- regs[6] = bin2bcd(tm->tm_year - 100);
-
- rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
- sizeof(regs), regs);
- if (rc < 0) {
- dev_err(&client->dev, "date/time register write error\n");
- return rc;
- }
-
- rc = pcf85063_start_clock(client, ctrl1);
- if (rc != 0)
- return rc;
- return 0;
- }
- static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
- {
- return pcf85063_get_datetime(to_i2c_client(dev), tm);
- }
- static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
- {
- return pcf85063_set_datetime(to_i2c_client(dev), tm);
- }
- static const struct rtc_class_ops pcf85063_rtc_ops = {
- .read_time = pcf85063_rtc_read_time,
- .set_time = pcf85063_rtc_set_time
- };
- static int pcf85063_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct rtc_device *rtc;
- dev_dbg(&client->dev, "%s\n", __func__);
- if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
- return -ENODEV;
- rtc = devm_rtc_device_register(&client->dev,
- pcf85063_driver.driver.name,
- &pcf85063_rtc_ops, THIS_MODULE);
- return PTR_ERR_OR_ZERO(rtc);
- }
- static const struct i2c_device_id pcf85063_id[] = {
- { "pcf85063", 0 },
- { }
- };
- MODULE_DEVICE_TABLE(i2c, pcf85063_id);
- #ifdef CONFIG_OF
- static const struct of_device_id pcf85063_of_match[] = {
- { .compatible = "nxp,pcf85063" },
- {}
- };
- MODULE_DEVICE_TABLE(of, pcf85063_of_match);
- #endif
- static struct i2c_driver pcf85063_driver = {
- .driver = {
- .name = "rtc-pcf85063",
- .of_match_table = of_match_ptr(pcf85063_of_match),
- },
- .probe = pcf85063_probe,
- .id_table = pcf85063_id,
- };
- module_i2c_driver(pcf85063_driver);
- MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
- MODULE_DESCRIPTION("PCF85063 RTC driver");
- MODULE_LICENSE("GPL");
|