Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/i2c/chips/rtc8564.c |
| 3 | * |
| 4 | * Copyright (C) 2002-2004 Stefan Eletzhofer |
| 5 | * |
| 6 | * based on linux/drivers/acron/char/pcf8583.c |
| 7 | * Copyright (C) 2000 Russell King |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | * |
| 13 | * Driver for system3's EPSON RTC 8564 chip |
| 14 | */ |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/rtc.h> /* get the user-level API */ |
| 21 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #include "rtc8564.h" |
| 24 | |
| 25 | #ifdef DEBUG |
| 26 | # define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_DEBUG"%s: " fmt "\n", __FUNCTION__, ##args); } while(0); |
| 27 | #else |
| 28 | # define _DBG(x, fmt, args...) do { } while(0); |
| 29 | #endif |
| 30 | |
| 31 | #define _DBGRTCTM(x, rtctm) if (debug>=x) printk("%s: secs=%d, mins=%d, hours=%d, mday=%d, " \ |
| 32 | "mon=%d, year=%d, wday=%d VL=%d\n", __FUNCTION__, \ |
| 33 | (rtctm).secs, (rtctm).mins, (rtctm).hours, (rtctm).mday, \ |
| 34 | (rtctm).mon, (rtctm).year, (rtctm).wday, (rtctm).vl); |
| 35 | |
| 36 | struct rtc8564_data { |
| 37 | struct i2c_client client; |
| 38 | u16 ctrl; |
| 39 | }; |
| 40 | |
| 41 | static inline u8 _rtc8564_ctrl1(struct i2c_client *client) |
| 42 | { |
| 43 | struct rtc8564_data *data = i2c_get_clientdata(client); |
| 44 | return data->ctrl & 0xff; |
| 45 | } |
| 46 | static inline u8 _rtc8564_ctrl2(struct i2c_client *client) |
| 47 | { |
| 48 | struct rtc8564_data *data = i2c_get_clientdata(client); |
| 49 | return (data->ctrl & 0xff00) >> 8; |
| 50 | } |
| 51 | |
| 52 | #define CTRL1(c) _rtc8564_ctrl1(c) |
| 53 | #define CTRL2(c) _rtc8564_ctrl2(c) |
| 54 | |
| 55 | #define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10) |
| 56 | #define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10) |
| 57 | |
| 58 | static int debug;; |
| 59 | module_param(debug, int, S_IRUGO | S_IWUSR); |
| 60 | |
| 61 | static struct i2c_driver rtc8564_driver; |
| 62 | |
| 63 | static unsigned short ignore[] = { I2C_CLIENT_END }; |
| 64 | static unsigned short normal_addr[] = { 0x51, I2C_CLIENT_END }; |
| 65 | |
| 66 | static struct i2c_client_address_data addr_data = { |
| 67 | .normal_i2c = normal_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | .probe = ignore, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | .ignore = ignore, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | .force = ignore, |
| 71 | }; |
| 72 | |
| 73 | static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem); |
| 74 | static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem); |
| 75 | |
| 76 | static int rtc8564_read(struct i2c_client *client, unsigned char adr, |
| 77 | unsigned char *buf, unsigned char len) |
| 78 | { |
| 79 | int ret = -EIO; |
| 80 | unsigned char addr[1] = { adr }; |
| 81 | struct i2c_msg msgs[2] = { |
| 82 | {client->addr, 0, 1, addr}, |
| 83 | {client->addr, I2C_M_RD, len, buf} |
| 84 | }; |
| 85 | |
| 86 | _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, buf, len); |
| 87 | |
| 88 | if (!buf) { |
| 89 | ret = -EINVAL; |
| 90 | goto done; |
| 91 | } |
| 92 | |
| 93 | ret = i2c_transfer(client->adapter, msgs, 2); |
| 94 | if (ret == 2) { |
| 95 | ret = 0; |
| 96 | } |
| 97 | |
| 98 | done: |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int rtc8564_write(struct i2c_client *client, unsigned char adr, |
| 103 | unsigned char *data, unsigned char len) |
| 104 | { |
| 105 | int ret = 0; |
| 106 | unsigned char _data[16]; |
| 107 | struct i2c_msg wr; |
| 108 | int i; |
| 109 | |
| 110 | if (!data || len > 15) { |
| 111 | ret = -EINVAL; |
| 112 | goto done; |
| 113 | } |
| 114 | |
| 115 | _DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, data, len); |
| 116 | |
| 117 | _data[0] = adr; |
| 118 | for (i = 0; i < len; i++) { |
| 119 | _data[i + 1] = data[i]; |
| 120 | _DBG(5, "data[%d] = 0x%02x (%d)", i, data[i], data[i]); |
| 121 | } |
| 122 | |
| 123 | wr.addr = client->addr; |
| 124 | wr.flags = 0; |
| 125 | wr.len = len + 1; |
| 126 | wr.buf = _data; |
| 127 | |
| 128 | ret = i2c_transfer(client->adapter, &wr, 1); |
| 129 | if (ret == 1) { |
| 130 | ret = 0; |
| 131 | } |
| 132 | |
| 133 | done: |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static int rtc8564_attach(struct i2c_adapter *adap, int addr, int kind) |
| 138 | { |
| 139 | int ret; |
| 140 | struct i2c_client *new_client; |
| 141 | struct rtc8564_data *d; |
| 142 | unsigned char data[10]; |
| 143 | unsigned char ad[1] = { 0 }; |
| 144 | struct i2c_msg ctrl_wr[1] = { |
| 145 | {addr, 0, 2, data} |
| 146 | }; |
| 147 | struct i2c_msg ctrl_rd[2] = { |
| 148 | {addr, 0, 1, ad}, |
| 149 | {addr, I2C_M_RD, 2, data} |
| 150 | }; |
| 151 | |
| 152 | d = kmalloc(sizeof(struct rtc8564_data), GFP_KERNEL); |
| 153 | if (!d) { |
| 154 | ret = -ENOMEM; |
| 155 | goto done; |
| 156 | } |
| 157 | memset(d, 0, sizeof(struct rtc8564_data)); |
| 158 | new_client = &d->client; |
| 159 | |
| 160 | strlcpy(new_client->name, "RTC8564", I2C_NAME_SIZE); |
| 161 | i2c_set_clientdata(new_client, d); |
| 162 | new_client->flags = I2C_CLIENT_ALLOW_USE | I2C_DF_NOTIFY; |
| 163 | new_client->addr = addr; |
| 164 | new_client->adapter = adap; |
| 165 | new_client->driver = &rtc8564_driver; |
| 166 | |
| 167 | _DBG(1, "client=%p", new_client); |
| 168 | |
| 169 | /* init ctrl1 reg */ |
| 170 | data[0] = 0; |
| 171 | data[1] = 0; |
| 172 | ret = i2c_transfer(new_client->adapter, ctrl_wr, 1); |
| 173 | if (ret != 1) { |
| 174 | printk(KERN_INFO "rtc8564: cant init ctrl1\n"); |
| 175 | ret = -ENODEV; |
| 176 | goto done; |
| 177 | } |
| 178 | |
| 179 | /* read back ctrl1 and ctrl2 */ |
| 180 | ret = i2c_transfer(new_client->adapter, ctrl_rd, 2); |
| 181 | if (ret != 2) { |
| 182 | printk(KERN_INFO "rtc8564: cant read ctrl\n"); |
| 183 | ret = -ENODEV; |
| 184 | goto done; |
| 185 | } |
| 186 | |
| 187 | d->ctrl = data[0] | (data[1] << 8); |
| 188 | |
| 189 | _DBG(1, "RTC8564_REG_CTRL1=%02x, RTC8564_REG_CTRL2=%02x", |
| 190 | data[0], data[1]); |
| 191 | |
| 192 | ret = i2c_attach_client(new_client); |
| 193 | done: |
| 194 | if (ret) { |
| 195 | kfree(d); |
| 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static int rtc8564_probe(struct i2c_adapter *adap) |
| 201 | { |
| 202 | return i2c_probe(adap, &addr_data, rtc8564_attach); |
| 203 | } |
| 204 | |
| 205 | static int rtc8564_detach(struct i2c_client *client) |
| 206 | { |
| 207 | i2c_detach_client(client); |
| 208 | kfree(i2c_get_clientdata(client)); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int rtc8564_get_datetime(struct i2c_client *client, struct rtc_tm *dt) |
| 213 | { |
| 214 | int ret = -EIO; |
| 215 | unsigned char buf[15]; |
| 216 | |
| 217 | _DBG(1, "client=%p, dt=%p", client, dt); |
| 218 | |
| 219 | if (!dt) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | memset(buf, 0, sizeof(buf)); |
| 223 | |
| 224 | ret = rtc8564_read(client, 0, buf, 15); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | /* century stored in minute alarm reg */ |
| 229 | dt->year = BCD_TO_BIN(buf[RTC8564_REG_YEAR]); |
| 230 | dt->year += 100 * BCD_TO_BIN(buf[RTC8564_REG_AL_MIN] & 0x3f); |
| 231 | dt->mday = BCD_TO_BIN(buf[RTC8564_REG_DAY] & 0x3f); |
| 232 | dt->wday = BCD_TO_BIN(buf[RTC8564_REG_WDAY] & 7); |
| 233 | dt->mon = BCD_TO_BIN(buf[RTC8564_REG_MON_CENT] & 0x1f); |
| 234 | |
| 235 | dt->secs = BCD_TO_BIN(buf[RTC8564_REG_SEC] & 0x7f); |
| 236 | dt->vl = (buf[RTC8564_REG_SEC] & 0x80) == 0x80; |
| 237 | dt->mins = BCD_TO_BIN(buf[RTC8564_REG_MIN] & 0x7f); |
| 238 | dt->hours = BCD_TO_BIN(buf[RTC8564_REG_HR] & 0x3f); |
| 239 | |
| 240 | _DBGRTCTM(2, *dt); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int |
| 246 | rtc8564_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo) |
| 247 | { |
| 248 | int ret, len = 5; |
| 249 | unsigned char buf[15]; |
| 250 | |
| 251 | _DBG(1, "client=%p, dt=%p", client, dt); |
| 252 | |
| 253 | if (!dt) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | _DBGRTCTM(2, *dt); |
| 257 | |
| 258 | buf[RTC8564_REG_CTRL1] = CTRL1(client) | RTC8564_CTRL1_STOP; |
| 259 | buf[RTC8564_REG_CTRL2] = CTRL2(client); |
| 260 | buf[RTC8564_REG_SEC] = BIN_TO_BCD(dt->secs); |
| 261 | buf[RTC8564_REG_MIN] = BIN_TO_BCD(dt->mins); |
| 262 | buf[RTC8564_REG_HR] = BIN_TO_BCD(dt->hours); |
| 263 | |
| 264 | if (datetoo) { |
| 265 | len += 5; |
| 266 | buf[RTC8564_REG_DAY] = BIN_TO_BCD(dt->mday); |
| 267 | buf[RTC8564_REG_WDAY] = BIN_TO_BCD(dt->wday); |
| 268 | buf[RTC8564_REG_MON_CENT] = BIN_TO_BCD(dt->mon) & 0x1f; |
| 269 | /* century stored in minute alarm reg */ |
| 270 | buf[RTC8564_REG_YEAR] = BIN_TO_BCD(dt->year % 100); |
| 271 | buf[RTC8564_REG_AL_MIN] = BIN_TO_BCD(dt->year / 100); |
| 272 | } |
| 273 | |
| 274 | ret = rtc8564_write(client, 0, buf, len); |
| 275 | if (ret) { |
| 276 | _DBG(1, "error writing data! %d", ret); |
| 277 | } |
| 278 | |
| 279 | buf[RTC8564_REG_CTRL1] = CTRL1(client); |
| 280 | ret = rtc8564_write(client, 0, buf, 1); |
| 281 | if (ret) { |
| 282 | _DBG(1, "error writing data! %d", ret); |
| 283 | } |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static int rtc8564_get_ctrl(struct i2c_client *client, unsigned int *ctrl) |
| 289 | { |
| 290 | struct rtc8564_data *data = i2c_get_clientdata(client); |
| 291 | |
| 292 | if (!ctrl) |
| 293 | return -1; |
| 294 | |
| 295 | *ctrl = data->ctrl; |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int rtc8564_set_ctrl(struct i2c_client *client, unsigned int *ctrl) |
| 300 | { |
| 301 | struct rtc8564_data *data = i2c_get_clientdata(client); |
| 302 | unsigned char buf[2]; |
| 303 | |
| 304 | if (!ctrl) |
| 305 | return -1; |
| 306 | |
| 307 | buf[0] = *ctrl & 0xff; |
| 308 | buf[1] = (*ctrl & 0xff00) >> 8; |
| 309 | data->ctrl = *ctrl; |
| 310 | |
| 311 | return rtc8564_write(client, 0, buf, 2); |
| 312 | } |
| 313 | |
| 314 | static int rtc8564_read_mem(struct i2c_client *client, struct mem *mem) |
| 315 | { |
| 316 | |
| 317 | if (!mem) |
| 318 | return -EINVAL; |
| 319 | |
| 320 | return rtc8564_read(client, mem->loc, mem->data, mem->nr); |
| 321 | } |
| 322 | |
| 323 | static int rtc8564_write_mem(struct i2c_client *client, struct mem *mem) |
| 324 | { |
| 325 | |
| 326 | if (!mem) |
| 327 | return -EINVAL; |
| 328 | |
| 329 | return rtc8564_write(client, mem->loc, mem->data, mem->nr); |
| 330 | } |
| 331 | |
| 332 | static int |
| 333 | rtc8564_command(struct i2c_client *client, unsigned int cmd, void *arg) |
| 334 | { |
| 335 | |
| 336 | _DBG(1, "cmd=%d", cmd); |
| 337 | |
| 338 | switch (cmd) { |
| 339 | case RTC_GETDATETIME: |
| 340 | return rtc8564_get_datetime(client, arg); |
| 341 | |
| 342 | case RTC_SETTIME: |
| 343 | return rtc8564_set_datetime(client, arg, 0); |
| 344 | |
| 345 | case RTC_SETDATETIME: |
| 346 | return rtc8564_set_datetime(client, arg, 1); |
| 347 | |
| 348 | case RTC_GETCTRL: |
| 349 | return rtc8564_get_ctrl(client, arg); |
| 350 | |
| 351 | case RTC_SETCTRL: |
| 352 | return rtc8564_set_ctrl(client, arg); |
| 353 | |
| 354 | case MEM_READ: |
| 355 | return rtc8564_read_mem(client, arg); |
| 356 | |
| 357 | case MEM_WRITE: |
| 358 | return rtc8564_write_mem(client, arg); |
| 359 | |
| 360 | default: |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | static struct i2c_driver rtc8564_driver = { |
| 366 | .owner = THIS_MODULE, |
| 367 | .name = "RTC8564", |
| 368 | .id = I2C_DRIVERID_RTC8564, |
| 369 | .flags = I2C_DF_NOTIFY, |
| 370 | .attach_adapter = rtc8564_probe, |
| 371 | .detach_client = rtc8564_detach, |
| 372 | .command = rtc8564_command |
| 373 | }; |
| 374 | |
| 375 | static __init int rtc8564_init(void) |
| 376 | { |
| 377 | return i2c_add_driver(&rtc8564_driver); |
| 378 | } |
| 379 | |
| 380 | static __exit void rtc8564_exit(void) |
| 381 | { |
| 382 | i2c_del_driver(&rtc8564_driver); |
| 383 | } |
| 384 | |
| 385 | MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>"); |
| 386 | MODULE_DESCRIPTION("EPSON RTC8564 Driver"); |
| 387 | MODULE_LICENSE("GPL"); |
| 388 | |
| 389 | module_init(rtc8564_init); |
| 390 | module_exit(rtc8564_exit); |