0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/i2c.h>
0012 #include <linux/rtc.h>
0013 #include <linux/bcd.h>
0014 #include <linux/module.h>
0015 #include <linux/of.h>
0016
0017
0018 #define EM3027_REG_ON_OFF_CTRL 0x00
0019 #define EM3027_REG_IRQ_CTRL 0x01
0020 #define EM3027_REG_IRQ_FLAGS 0x02
0021 #define EM3027_REG_STATUS 0x03
0022 #define EM3027_REG_RST_CTRL 0x04
0023
0024 #define EM3027_REG_WATCH_SEC 0x08
0025 #define EM3027_REG_WATCH_MIN 0x09
0026 #define EM3027_REG_WATCH_HOUR 0x0a
0027 #define EM3027_REG_WATCH_DATE 0x0b
0028 #define EM3027_REG_WATCH_DAY 0x0c
0029 #define EM3027_REG_WATCH_MON 0x0d
0030 #define EM3027_REG_WATCH_YEAR 0x0e
0031
0032 #define EM3027_REG_ALARM_SEC 0x10
0033 #define EM3027_REG_ALARM_MIN 0x11
0034 #define EM3027_REG_ALARM_HOUR 0x12
0035 #define EM3027_REG_ALARM_DATE 0x13
0036 #define EM3027_REG_ALARM_DAY 0x14
0037 #define EM3027_REG_ALARM_MON 0x15
0038 #define EM3027_REG_ALARM_YEAR 0x16
0039
0040 static struct i2c_driver em3027_driver;
0041
0042 static int em3027_get_time(struct device *dev, struct rtc_time *tm)
0043 {
0044 struct i2c_client *client = to_i2c_client(dev);
0045
0046 unsigned char addr = EM3027_REG_WATCH_SEC;
0047 unsigned char buf[7];
0048
0049 struct i2c_msg msgs[] = {
0050 {
0051 .addr = client->addr,
0052 .len = 1,
0053 .buf = &addr
0054 },
0055 {
0056 .addr = client->addr,
0057 .flags = I2C_M_RD,
0058 .len = 7,
0059 .buf = buf
0060 },
0061 };
0062
0063
0064 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
0065 dev_err(&client->dev, "%s: read error\n", __func__);
0066 return -EIO;
0067 }
0068
0069 tm->tm_sec = bcd2bin(buf[0]);
0070 tm->tm_min = bcd2bin(buf[1]);
0071 tm->tm_hour = bcd2bin(buf[2]);
0072 tm->tm_mday = bcd2bin(buf[3]);
0073 tm->tm_wday = bcd2bin(buf[4]);
0074 tm->tm_mon = bcd2bin(buf[5]) - 1;
0075 tm->tm_year = bcd2bin(buf[6]) + 100;
0076
0077 return 0;
0078 }
0079
0080 static int em3027_set_time(struct device *dev, struct rtc_time *tm)
0081 {
0082 struct i2c_client *client = to_i2c_client(dev);
0083 unsigned char buf[8];
0084
0085 struct i2c_msg msg = {
0086 .addr = client->addr,
0087 .len = 8,
0088 .buf = buf,
0089 };
0090
0091 buf[0] = EM3027_REG_WATCH_SEC;
0092 buf[1] = bin2bcd(tm->tm_sec);
0093 buf[2] = bin2bcd(tm->tm_min);
0094 buf[3] = bin2bcd(tm->tm_hour);
0095 buf[4] = bin2bcd(tm->tm_mday);
0096 buf[5] = bin2bcd(tm->tm_wday);
0097 buf[6] = bin2bcd(tm->tm_mon + 1);
0098 buf[7] = bin2bcd(tm->tm_year % 100);
0099
0100
0101 if ((i2c_transfer(client->adapter, &msg, 1)) != 1) {
0102 dev_err(&client->dev, "%s: write error\n", __func__);
0103 return -EIO;
0104 }
0105
0106 return 0;
0107 }
0108
0109 static const struct rtc_class_ops em3027_rtc_ops = {
0110 .read_time = em3027_get_time,
0111 .set_time = em3027_set_time,
0112 };
0113
0114 static int em3027_probe(struct i2c_client *client)
0115 {
0116 struct rtc_device *rtc;
0117
0118 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0119 return -ENODEV;
0120
0121 rtc = devm_rtc_device_register(&client->dev, em3027_driver.driver.name,
0122 &em3027_rtc_ops, THIS_MODULE);
0123 if (IS_ERR(rtc))
0124 return PTR_ERR(rtc);
0125
0126 i2c_set_clientdata(client, rtc);
0127
0128 return 0;
0129 }
0130
0131 static const struct i2c_device_id em3027_id[] = {
0132 { "em3027", 0 },
0133 { }
0134 };
0135 MODULE_DEVICE_TABLE(i2c, em3027_id);
0136
0137 #ifdef CONFIG_OF
0138 static const struct of_device_id em3027_of_match[] = {
0139 { .compatible = "emmicro,em3027", },
0140 {}
0141 };
0142 MODULE_DEVICE_TABLE(of, em3027_of_match);
0143 #endif
0144
0145 static struct i2c_driver em3027_driver = {
0146 .driver = {
0147 .name = "rtc-em3027",
0148 .of_match_table = of_match_ptr(em3027_of_match),
0149 },
0150 .probe_new = em3027_probe,
0151 .id_table = em3027_id,
0152 };
0153
0154 module_i2c_driver(em3027_driver);
0155
0156 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
0157 MODULE_DESCRIPTION("EM Microelectronic EM3027 RTC driver");
0158 MODULE_LICENSE("GPL");