G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/rtc/rtc-pcf8583.c |
| 3 | * |
| 4 | * Copyright (C) 2000 Russell King |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 5 | * Copyright (C) 2008 Wolfram Sang & Juergen Beisert, Pengutronix |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * Driver for PCF8583 RTC & RAM chip |
| 12 | * |
| 13 | * Converted to the generic RTC susbsystem by G. Liakhovetski (2006) |
| 14 | */ |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/slab.h> |
David Brownell | 77459b0 | 2008-04-28 02:11:51 -0700 | [diff] [blame] | 18 | #include <linux/rtc.h> |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
Sachin Kamat | ee605e0 | 2013-07-03 15:07:52 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 21 | #include <linux/errno.h> |
| 22 | #include <linux/bcd.h> |
| 23 | |
| 24 | struct rtc_mem { |
| 25 | unsigned int loc; |
| 26 | unsigned int nr; |
| 27 | unsigned char *data; |
| 28 | }; |
| 29 | |
| 30 | struct pcf8583 { |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 31 | struct rtc_device *rtc; |
| 32 | unsigned char ctrl; |
| 33 | }; |
| 34 | |
| 35 | #define CTRL_STOP 0x80 |
| 36 | #define CTRL_HOLD 0x40 |
| 37 | #define CTRL_32KHZ 0x00 |
| 38 | #define CTRL_MASK 0x08 |
| 39 | #define CTRL_ALARMEN 0x04 |
| 40 | #define CTRL_ALARM 0x02 |
| 41 | #define CTRL_TIMER 0x01 |
| 42 | |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 43 | |
| 44 | static struct i2c_driver pcf8583_driver; |
| 45 | |
| 46 | #define get_ctrl(x) ((struct pcf8583 *)i2c_get_clientdata(x))->ctrl |
| 47 | #define set_ctrl(x, v) get_ctrl(x) = v |
| 48 | |
| 49 | #define CMOS_YEAR (64 + 128) |
| 50 | #define CMOS_CHECKSUM (63) |
| 51 | |
| 52 | static int pcf8583_get_datetime(struct i2c_client *client, struct rtc_time *dt) |
| 53 | { |
| 54 | unsigned char buf[8], addr[1] = { 1 }; |
| 55 | struct i2c_msg msgs[2] = { |
| 56 | { |
| 57 | .addr = client->addr, |
| 58 | .flags = 0, |
| 59 | .len = 1, |
| 60 | .buf = addr, |
| 61 | }, { |
| 62 | .addr = client->addr, |
| 63 | .flags = I2C_M_RD, |
| 64 | .len = 6, |
| 65 | .buf = buf, |
| 66 | } |
| 67 | }; |
| 68 | int ret; |
| 69 | |
| 70 | memset(buf, 0, sizeof(buf)); |
| 71 | |
| 72 | ret = i2c_transfer(client->adapter, msgs, 2); |
| 73 | if (ret == 2) { |
| 74 | dt->tm_year = buf[4] >> 6; |
| 75 | dt->tm_wday = buf[5] >> 5; |
| 76 | |
| 77 | buf[4] &= 0x3f; |
| 78 | buf[5] &= 0x1f; |
| 79 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 80 | dt->tm_sec = bcd2bin(buf[1]); |
| 81 | dt->tm_min = bcd2bin(buf[2]); |
| 82 | dt->tm_hour = bcd2bin(buf[3]); |
| 83 | dt->tm_mday = bcd2bin(buf[4]); |
| 84 | dt->tm_mon = bcd2bin(buf[5]) - 1; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return ret == 2 ? 0 : -EIO; |
| 88 | } |
| 89 | |
| 90 | static int pcf8583_set_datetime(struct i2c_client *client, struct rtc_time *dt, int datetoo) |
| 91 | { |
| 92 | unsigned char buf[8]; |
| 93 | int ret, len = 6; |
| 94 | |
| 95 | buf[0] = 0; |
| 96 | buf[1] = get_ctrl(client) | 0x80; |
| 97 | buf[2] = 0; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 98 | buf[3] = bin2bcd(dt->tm_sec); |
| 99 | buf[4] = bin2bcd(dt->tm_min); |
| 100 | buf[5] = bin2bcd(dt->tm_hour); |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 101 | |
| 102 | if (datetoo) { |
| 103 | len = 8; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 104 | buf[6] = bin2bcd(dt->tm_mday) | (dt->tm_year << 6); |
| 105 | buf[7] = bin2bcd(dt->tm_mon + 1) | (dt->tm_wday << 5); |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | ret = i2c_master_send(client, (char *)buf, len); |
| 109 | if (ret != len) |
| 110 | return -EIO; |
| 111 | |
| 112 | buf[1] = get_ctrl(client); |
| 113 | ret = i2c_master_send(client, (char *)buf, 2); |
| 114 | |
| 115 | return ret == 2 ? 0 : -EIO; |
| 116 | } |
| 117 | |
| 118 | static int pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl) |
| 119 | { |
| 120 | *ctrl = get_ctrl(client); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl) |
| 125 | { |
| 126 | unsigned char buf[2]; |
| 127 | |
| 128 | buf[0] = 0; |
| 129 | buf[1] = *ctrl; |
| 130 | set_ctrl(client, *ctrl); |
| 131 | |
| 132 | return i2c_master_send(client, (char *)buf, 2); |
| 133 | } |
| 134 | |
| 135 | static int pcf8583_read_mem(struct i2c_client *client, struct rtc_mem *mem) |
| 136 | { |
| 137 | unsigned char addr[1]; |
| 138 | struct i2c_msg msgs[2] = { |
| 139 | { |
| 140 | .addr = client->addr, |
| 141 | .flags = 0, |
| 142 | .len = 1, |
| 143 | .buf = addr, |
| 144 | }, { |
| 145 | .addr = client->addr, |
| 146 | .flags = I2C_M_RD, |
| 147 | .len = mem->nr, |
| 148 | .buf = mem->data, |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | if (mem->loc < 8) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | addr[0] = mem->loc; |
| 156 | |
| 157 | return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO; |
| 158 | } |
| 159 | |
| 160 | static int pcf8583_write_mem(struct i2c_client *client, struct rtc_mem *mem) |
| 161 | { |
Jean Delvare | 037e291 | 2008-02-06 01:38:41 -0800 | [diff] [blame] | 162 | unsigned char buf[9]; |
| 163 | int ret; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 164 | |
Jean Delvare | 037e291 | 2008-02-06 01:38:41 -0800 | [diff] [blame] | 165 | if (mem->loc < 8 || mem->nr > 8) |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 166 | return -EINVAL; |
| 167 | |
Jean Delvare | 037e291 | 2008-02-06 01:38:41 -0800 | [diff] [blame] | 168 | buf[0] = mem->loc; |
| 169 | memcpy(buf + 1, mem->data, mem->nr); |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 170 | |
Jean Delvare | 037e291 | 2008-02-06 01:38:41 -0800 | [diff] [blame] | 171 | ret = i2c_master_send(client, buf, mem->nr + 1); |
| 172 | return ret == mem->nr + 1 ? 0 : -EIO; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int pcf8583_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 176 | { |
| 177 | struct i2c_client *client = to_i2c_client(dev); |
| 178 | unsigned char ctrl, year[2]; |
Julia Lawall | 473b864 | 2014-10-13 15:52:26 -0700 | [diff] [blame] | 179 | struct rtc_mem mem = { |
| 180 | .loc = CMOS_YEAR, |
| 181 | .nr = sizeof(year), |
| 182 | .data = year |
| 183 | }; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 184 | int real_year, year_offset, err; |
| 185 | |
| 186 | /* |
| 187 | * Ensure that the RTC is running. |
| 188 | */ |
| 189 | pcf8583_get_ctrl(client, &ctrl); |
| 190 | if (ctrl & (CTRL_STOP | CTRL_HOLD)) { |
| 191 | unsigned char new_ctrl = ctrl & ~(CTRL_STOP | CTRL_HOLD); |
| 192 | |
Jingoo Han | 79d1340 | 2013-02-21 16:45:33 -0800 | [diff] [blame] | 193 | dev_warn(dev, "resetting control %02x -> %02x\n", |
| 194 | ctrl, new_ctrl); |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 195 | |
Sachin Kamat | d1bda80 | 2013-07-03 15:06:02 -0700 | [diff] [blame] | 196 | err = pcf8583_set_ctrl(client, &new_ctrl); |
| 197 | if (err < 0) |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 198 | return err; |
| 199 | } |
| 200 | |
| 201 | if (pcf8583_get_datetime(client, tm) || |
| 202 | pcf8583_read_mem(client, &mem)) |
| 203 | return -EIO; |
| 204 | |
| 205 | real_year = year[0]; |
| 206 | |
| 207 | /* |
| 208 | * The RTC year holds the LSB two bits of the current |
| 209 | * year, which should reflect the LSB two bits of the |
| 210 | * CMOS copy of the year. Any difference indicates |
| 211 | * that we have to correct the CMOS version. |
| 212 | */ |
| 213 | year_offset = tm->tm_year - (real_year & 3); |
| 214 | if (year_offset < 0) |
| 215 | /* |
| 216 | * RTC year wrapped. Adjust it appropriately. |
| 217 | */ |
| 218 | year_offset += 4; |
| 219 | |
Russell King | 0ed8f21 | 2007-03-04 20:13:13 +0000 | [diff] [blame] | 220 | tm->tm_year = (real_year + year_offset + year[1] * 100) - 1900; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int pcf8583_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 226 | { |
| 227 | struct i2c_client *client = to_i2c_client(dev); |
| 228 | unsigned char year[2], chk; |
Julia Lawall | 473b864 | 2014-10-13 15:52:26 -0700 | [diff] [blame] | 229 | struct rtc_mem cmos_year = { |
| 230 | .loc = CMOS_YEAR, |
| 231 | .nr = sizeof(year), |
| 232 | .data = year |
| 233 | }; |
| 234 | struct rtc_mem cmos_check = { |
| 235 | .loc = CMOS_CHECKSUM, |
| 236 | .nr = 1, |
| 237 | .data = &chk |
| 238 | }; |
Russell King | 0ed8f21 | 2007-03-04 20:13:13 +0000 | [diff] [blame] | 239 | unsigned int proper_year = tm->tm_year + 1900; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 240 | int ret; |
| 241 | |
| 242 | /* |
| 243 | * The RTC's own 2-bit year must reflect the least |
| 244 | * significant two bits of the CMOS year. |
| 245 | */ |
| 246 | |
| 247 | ret = pcf8583_set_datetime(client, tm, 1); |
| 248 | if (ret) |
| 249 | return ret; |
| 250 | |
| 251 | ret = pcf8583_read_mem(client, &cmos_check); |
| 252 | if (ret) |
| 253 | return ret; |
| 254 | |
| 255 | ret = pcf8583_read_mem(client, &cmos_year); |
| 256 | if (ret) |
| 257 | return ret; |
| 258 | |
| 259 | chk -= year[1] + year[0]; |
| 260 | |
Russell King | 0ed8f21 | 2007-03-04 20:13:13 +0000 | [diff] [blame] | 261 | year[1] = proper_year / 100; |
| 262 | year[0] = proper_year % 100; |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 263 | |
| 264 | chk += year[1] + year[0]; |
| 265 | |
| 266 | ret = pcf8583_write_mem(client, &cmos_year); |
| 267 | |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | |
| 271 | ret = pcf8583_write_mem(client, &cmos_check); |
| 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | |
David Brownell | ff8371a | 2006-09-30 23:28:17 -0700 | [diff] [blame] | 276 | static const struct rtc_class_ops pcf8583_rtc_ops = { |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 277 | .read_time = pcf8583_rtc_read_time, |
| 278 | .set_time = pcf8583_rtc_set_time, |
| 279 | }; |
| 280 | |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 281 | static int pcf8583_probe(struct i2c_client *client, |
| 282 | const struct i2c_device_id *id) |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 283 | { |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 284 | struct pcf8583 *pcf8583; |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 285 | |
| 286 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 287 | return -ENODEV; |
| 288 | |
Jingoo Han | 4ad2118 | 2013-04-29 16:20:50 -0700 | [diff] [blame] | 289 | pcf8583 = devm_kzalloc(&client->dev, sizeof(struct pcf8583), |
| 290 | GFP_KERNEL); |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 291 | if (!pcf8583) |
| 292 | return -ENOMEM; |
| 293 | |
Alessandro Zummo | b74d2ca | 2009-12-15 16:45:53 -0800 | [diff] [blame] | 294 | i2c_set_clientdata(client, pcf8583); |
| 295 | |
Jingoo Han | 4ad2118 | 2013-04-29 16:20:50 -0700 | [diff] [blame] | 296 | pcf8583->rtc = devm_rtc_device_register(&client->dev, |
| 297 | pcf8583_driver.driver.name, |
| 298 | &pcf8583_rtc_ops, THIS_MODULE); |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 299 | |
Sachin Kamat | dac30a9 | 2013-07-15 15:43:32 +0530 | [diff] [blame] | 300 | return PTR_ERR_OR_ZERO(pcf8583->rtc); |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 303 | static const struct i2c_device_id pcf8583_id[] = { |
| 304 | { "pcf8583", 0 }, |
| 305 | { } |
| 306 | }; |
| 307 | MODULE_DEVICE_TABLE(i2c, pcf8583_id); |
| 308 | |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 309 | static struct i2c_driver pcf8583_driver = { |
| 310 | .driver = { |
| 311 | .name = "pcf8583", |
| 312 | }, |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 313 | .probe = pcf8583_probe, |
Wolfram Sang | 02bb584 | 2008-07-23 21:30:39 -0700 | [diff] [blame] | 314 | .id_table = pcf8583_id, |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
Axel Lin | 0abc920 | 2012-03-23 15:02:31 -0700 | [diff] [blame] | 317 | module_i2c_driver(pcf8583_driver); |
G. Liakhovetski | 9c0c570 | 2006-06-25 05:48:18 -0700 | [diff] [blame] | 318 | |
| 319 | MODULE_AUTHOR("Russell King"); |
| 320 | MODULE_DESCRIPTION("PCF8583 I2C RTC driver"); |
| 321 | MODULE_LICENSE("GPL"); |