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