Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/i2c/chips/ds1337.c |
| 3 | * |
| 4 | * Copyright (C) 2005 James Chapman <jchapman@katalix.com> |
| 5 | * |
Ladislav Michl | 3e9d0ba | 2005-04-08 15:00:21 +0200 | [diff] [blame] | 6 | * based on linux/drivers/acorn/char/pcf8583.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Ladislav Michl | 912b9c0 | 2005-05-10 14:08:04 +0200 | [diff] [blame] | 13 | * Driver for Dallas Semiconductor DS1337 and DS1339 real time clock chip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/i2c-sensor.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/rtc.h> /* get the user-level API */ |
| 23 | #include <linux/bcd.h> |
| 24 | #include <linux/list.h> |
| 25 | |
| 26 | /* Device registers */ |
| 27 | #define DS1337_REG_HOUR 2 |
| 28 | #define DS1337_REG_DAY 3 |
| 29 | #define DS1337_REG_DATE 4 |
| 30 | #define DS1337_REG_MONTH 5 |
| 31 | #define DS1337_REG_CONTROL 14 |
| 32 | #define DS1337_REG_STATUS 15 |
| 33 | |
| 34 | /* FIXME - how do we export these interface constants? */ |
| 35 | #define DS1337_GET_DATE 0 |
| 36 | #define DS1337_SET_DATE 1 |
| 37 | |
| 38 | /* |
| 39 | * Functions declaration |
| 40 | */ |
| 41 | static unsigned short normal_i2c[] = { 0x68, I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | SENSORS_INSMOD_1(ds1337); |
| 44 | |
| 45 | static int ds1337_attach_adapter(struct i2c_adapter *adapter); |
| 46 | static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind); |
| 47 | static void ds1337_init_client(struct i2c_client *client); |
| 48 | static int ds1337_detach_client(struct i2c_client *client); |
| 49 | static int ds1337_command(struct i2c_client *client, unsigned int cmd, |
| 50 | void *arg); |
| 51 | |
| 52 | /* |
| 53 | * Driver data (common to all clients) |
| 54 | */ |
| 55 | static struct i2c_driver ds1337_driver = { |
| 56 | .owner = THIS_MODULE, |
| 57 | .name = "ds1337", |
| 58 | .flags = I2C_DF_NOTIFY, |
| 59 | .attach_adapter = ds1337_attach_adapter, |
| 60 | .detach_client = ds1337_detach_client, |
| 61 | .command = ds1337_command, |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * Client data (each client gets its own) |
| 66 | */ |
| 67 | struct ds1337_data { |
| 68 | struct i2c_client client; |
| 69 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * Internal variables |
| 74 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static LIST_HEAD(ds1337_clients); |
| 76 | |
| 77 | static inline int ds1337_read(struct i2c_client *client, u8 reg, u8 *value) |
| 78 | { |
| 79 | s32 tmp = i2c_smbus_read_byte_data(client, reg); |
| 80 | |
| 81 | if (tmp < 0) |
| 82 | return -EIO; |
| 83 | |
| 84 | *value = tmp; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Chip access functions |
| 91 | */ |
| 92 | static int ds1337_get_datetime(struct i2c_client *client, struct rtc_time *dt) |
| 93 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | int result; |
| 95 | u8 buf[7]; |
| 96 | u8 val; |
| 97 | struct i2c_msg msg[2]; |
| 98 | u8 offs = 0; |
| 99 | |
| 100 | if (!dt) { |
Ladislav Michl | d01b79d | 2005-04-08 15:06:39 +0200 | [diff] [blame] | 101 | dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return -EINVAL; |
| 103 | } |
| 104 | |
| 105 | msg[0].addr = client->addr; |
| 106 | msg[0].flags = 0; |
| 107 | msg[0].len = 1; |
| 108 | msg[0].buf = &offs; |
| 109 | |
| 110 | msg[1].addr = client->addr; |
| 111 | msg[1].flags = I2C_M_RD; |
| 112 | msg[1].len = sizeof(buf); |
| 113 | msg[1].buf = &buf[0]; |
| 114 | |
Ladislav Michl | 3e9d0ba | 2005-04-08 15:00:21 +0200 | [diff] [blame] | 115 | result = i2c_transfer(client->adapter, msg, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Ladislav Michl | d01b79d | 2005-04-08 15:06:39 +0200 | [diff] [blame] | 117 | dev_dbg(&client->dev, "%s: [%d] %02x %02x %02x %02x %02x %02x %02x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | __FUNCTION__, result, buf[0], buf[1], buf[2], buf[3], |
| 119 | buf[4], buf[5], buf[6]); |
| 120 | |
Ladislav Michl | 0058824 | 2005-05-04 08:13:54 +0200 | [diff] [blame] | 121 | if (result == 2) { |
Ladislav Michl | 6069ffd | 2005-04-08 15:02:16 +0200 | [diff] [blame] | 122 | dt->tm_sec = BCD2BIN(buf[0]); |
| 123 | dt->tm_min = BCD2BIN(buf[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | val = buf[2] & 0x3f; |
Ladislav Michl | 6069ffd | 2005-04-08 15:02:16 +0200 | [diff] [blame] | 125 | dt->tm_hour = BCD2BIN(val); |
| 126 | dt->tm_wday = BCD2BIN(buf[3]) - 1; |
| 127 | dt->tm_mday = BCD2BIN(buf[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | val = buf[5] & 0x7f; |
Ladislav Michl | 0b46e33 | 2005-05-04 08:13:13 +0200 | [diff] [blame] | 129 | dt->tm_mon = BCD2BIN(val) - 1; |
| 130 | dt->tm_year = BCD2BIN(buf[6]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (buf[5] & 0x80) |
| 132 | dt->tm_year += 100; |
| 133 | |
Ladislav Michl | d01b79d | 2005-04-08 15:06:39 +0200 | [diff] [blame] | 134 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", |
| 136 | __FUNCTION__, dt->tm_sec, dt->tm_min, |
| 137 | dt->tm_hour, dt->tm_mday, |
| 138 | dt->tm_mon, dt->tm_year, dt->tm_wday); |
Ladislav Michl | 0058824 | 2005-05-04 08:13:54 +0200 | [diff] [blame] | 139 | |
| 140 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Ladislav Michl | 0058824 | 2005-05-04 08:13:54 +0200 | [diff] [blame] | 143 | dev_err(&client->dev, "error reading data! %d\n", result); |
| 144 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int ds1337_set_datetime(struct i2c_client *client, struct rtc_time *dt) |
| 148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | int result; |
| 150 | u8 buf[8]; |
| 151 | u8 val; |
| 152 | struct i2c_msg msg[1]; |
| 153 | |
| 154 | if (!dt) { |
Ladislav Michl | d01b79d | 2005-04-08 15:06:39 +0200 | [diff] [blame] | 155 | dev_dbg(&client->dev, "%s: EINVAL: dt=NULL\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return -EINVAL; |
| 157 | } |
| 158 | |
Ladislav Michl | d01b79d | 2005-04-08 15:06:39 +0200 | [diff] [blame] | 159 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | "mday=%d, mon=%d, year=%d, wday=%d\n", __FUNCTION__, |
| 161 | dt->tm_sec, dt->tm_min, dt->tm_hour, |
| 162 | dt->tm_mday, dt->tm_mon, dt->tm_year, dt->tm_wday); |
| 163 | |
| 164 | buf[0] = 0; /* reg offset */ |
Ladislav Michl | 6069ffd | 2005-04-08 15:02:16 +0200 | [diff] [blame] | 165 | buf[1] = BIN2BCD(dt->tm_sec); |
| 166 | buf[2] = BIN2BCD(dt->tm_min); |
Ladislav Michl | d91e169 | 2005-07-29 12:15:00 -0700 | [diff] [blame] | 167 | buf[3] = BIN2BCD(dt->tm_hour); |
Ladislav Michl | 6069ffd | 2005-04-08 15:02:16 +0200 | [diff] [blame] | 168 | buf[4] = BIN2BCD(dt->tm_wday) + 1; |
| 169 | buf[5] = BIN2BCD(dt->tm_mday); |
Ladislav Michl | 0b46e33 | 2005-05-04 08:13:13 +0200 | [diff] [blame] | 170 | buf[6] = BIN2BCD(dt->tm_mon) + 1; |
| 171 | val = dt->tm_year; |
| 172 | if (val >= 100) { |
| 173 | val -= 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | buf[6] |= (1 << 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
Ladislav Michl | 6069ffd | 2005-04-08 15:02:16 +0200 | [diff] [blame] | 176 | buf[7] = BIN2BCD(val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | msg[0].addr = client->addr; |
| 179 | msg[0].flags = 0; |
| 180 | msg[0].len = sizeof(buf); |
| 181 | msg[0].buf = &buf[0]; |
| 182 | |
Ladislav Michl | 3e9d0ba | 2005-04-08 15:00:21 +0200 | [diff] [blame] | 183 | result = i2c_transfer(client->adapter, msg, 1); |
Ladislav Michl | 0058824 | 2005-05-04 08:13:54 +0200 | [diff] [blame] | 184 | if (result == 1) |
| 185 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Ladislav Michl | 0058824 | 2005-05-04 08:13:54 +0200 | [diff] [blame] | 187 | dev_err(&client->dev, "error writing data! %d\n", result); |
| 188 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int ds1337_command(struct i2c_client *client, unsigned int cmd, |
| 192 | void *arg) |
| 193 | { |
Ladislav Michl | d01b79d | 2005-04-08 15:06:39 +0200 | [diff] [blame] | 194 | dev_dbg(&client->dev, "%s: cmd=%d\n", __FUNCTION__, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | switch (cmd) { |
| 197 | case DS1337_GET_DATE: |
| 198 | return ds1337_get_datetime(client, arg); |
| 199 | |
| 200 | case DS1337_SET_DATE: |
| 201 | return ds1337_set_datetime(client, arg); |
| 202 | |
| 203 | default: |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Public API for access to specific device. Useful for low-level |
| 210 | * RTC access from kernel code. |
| 211 | */ |
Ladislav Michl | 8691983 | 2005-05-04 08:14:38 +0200 | [diff] [blame] | 212 | int ds1337_do_command(int bus, int cmd, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | struct list_head *walk; |
| 215 | struct list_head *tmp; |
| 216 | struct ds1337_data *data; |
| 217 | |
| 218 | list_for_each_safe(walk, tmp, &ds1337_clients) { |
| 219 | data = list_entry(walk, struct ds1337_data, list); |
Ladislav Michl | 8691983 | 2005-05-04 08:14:38 +0200 | [diff] [blame] | 220 | if (data->client.adapter->nr == bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return ds1337_command(&data->client, cmd, arg); |
| 222 | } |
| 223 | |
| 224 | return -ENODEV; |
| 225 | } |
| 226 | |
| 227 | static int ds1337_attach_adapter(struct i2c_adapter *adapter) |
| 228 | { |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame^] | 229 | return i2c_probe(adapter, &addr_data, ds1337_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * The following function does more than just detection. If detection |
| 234 | * succeeds, it also registers the new chip. |
| 235 | */ |
| 236 | static int ds1337_detect(struct i2c_adapter *adapter, int address, int kind) |
| 237 | { |
| 238 | struct i2c_client *new_client; |
| 239 | struct ds1337_data *data; |
| 240 | int err = 0; |
| 241 | const char *name = ""; |
| 242 | |
| 243 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
| 244 | I2C_FUNC_I2C)) |
| 245 | goto exit; |
| 246 | |
| 247 | if (!(data = kmalloc(sizeof(struct ds1337_data), GFP_KERNEL))) { |
| 248 | err = -ENOMEM; |
| 249 | goto exit; |
| 250 | } |
| 251 | memset(data, 0, sizeof(struct ds1337_data)); |
| 252 | INIT_LIST_HEAD(&data->list); |
| 253 | |
| 254 | /* The common I2C client data is placed right before the |
| 255 | * DS1337-specific data. |
| 256 | */ |
| 257 | new_client = &data->client; |
| 258 | i2c_set_clientdata(new_client, data); |
| 259 | new_client->addr = address; |
| 260 | new_client->adapter = adapter; |
| 261 | new_client->driver = &ds1337_driver; |
| 262 | new_client->flags = 0; |
| 263 | |
| 264 | /* |
| 265 | * Now we do the remaining detection. A negative kind means that |
| 266 | * the driver was loaded with no force parameter (default), so we |
| 267 | * must both detect and identify the chip. A zero kind means that |
| 268 | * the driver was loaded with the force parameter, the detection |
| 269 | * step shall be skipped. A positive kind means that the driver |
| 270 | * was loaded with the force parameter and a given kind of chip is |
| 271 | * requested, so both the detection and the identification steps |
| 272 | * are skipped. |
| 273 | * |
| 274 | * For detection, we read registers that are most likely to cause |
| 275 | * detection failure, i.e. those that have more bits with fixed |
| 276 | * or reserved values. |
| 277 | */ |
| 278 | |
| 279 | /* Default to an DS1337 if forced */ |
| 280 | if (kind == 0) |
| 281 | kind = ds1337; |
| 282 | |
| 283 | if (kind < 0) { /* detection and identification */ |
| 284 | u8 data; |
| 285 | |
| 286 | /* Check that status register bits 6-2 are zero */ |
| 287 | if ((ds1337_read(new_client, DS1337_REG_STATUS, &data) < 0) || |
| 288 | (data & 0x7c)) |
| 289 | goto exit_free; |
| 290 | |
| 291 | /* Check for a valid day register value */ |
| 292 | if ((ds1337_read(new_client, DS1337_REG_DAY, &data) < 0) || |
| 293 | (data == 0) || (data & 0xf8)) |
| 294 | goto exit_free; |
| 295 | |
| 296 | /* Check for a valid date register value */ |
| 297 | if ((ds1337_read(new_client, DS1337_REG_DATE, &data) < 0) || |
| 298 | (data == 0) || (data & 0xc0) || ((data & 0x0f) > 9) || |
| 299 | (data >= 0x32)) |
| 300 | goto exit_free; |
| 301 | |
| 302 | /* Check for a valid month register value */ |
| 303 | if ((ds1337_read(new_client, DS1337_REG_MONTH, &data) < 0) || |
| 304 | (data == 0) || (data & 0x60) || ((data & 0x0f) > 9) || |
| 305 | ((data >= 0x13) && (data <= 0x19))) |
| 306 | goto exit_free; |
| 307 | |
| 308 | /* Check that control register bits 6-5 are zero */ |
| 309 | if ((ds1337_read(new_client, DS1337_REG_CONTROL, &data) < 0) || |
| 310 | (data & 0x60)) |
| 311 | goto exit_free; |
| 312 | |
| 313 | kind = ds1337; |
| 314 | } |
| 315 | |
| 316 | if (kind == ds1337) |
| 317 | name = "ds1337"; |
| 318 | |
| 319 | /* We can fill in the remaining client fields */ |
| 320 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
| 321 | |
| 322 | /* Tell the I2C layer a new client has arrived */ |
| 323 | if ((err = i2c_attach_client(new_client))) |
| 324 | goto exit_free; |
| 325 | |
| 326 | /* Initialize the DS1337 chip */ |
| 327 | ds1337_init_client(new_client); |
| 328 | |
| 329 | /* Add client to local list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | list_add(&data->list, &ds1337_clients); |
| 331 | |
| 332 | return 0; |
| 333 | |
| 334 | exit_free: |
| 335 | kfree(data); |
| 336 | exit: |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | static void ds1337_init_client(struct i2c_client *client) |
| 341 | { |
| 342 | s32 val; |
| 343 | |
| 344 | /* Ensure that device is set in 24-hour mode */ |
| 345 | val = i2c_smbus_read_byte_data(client, DS1337_REG_HOUR); |
Ladislav Michl | d91e169 | 2005-07-29 12:15:00 -0700 | [diff] [blame] | 346 | if ((val >= 0) && (val & (1 << 6))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | i2c_smbus_write_byte_data(client, DS1337_REG_HOUR, |
Ladislav Michl | d91e169 | 2005-07-29 12:15:00 -0700 | [diff] [blame] | 348 | val & 0x3f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int ds1337_detach_client(struct i2c_client *client) |
| 352 | { |
| 353 | int err; |
| 354 | struct ds1337_data *data = i2c_get_clientdata(client); |
| 355 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 356 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | list_del(&data->list); |
| 360 | kfree(data); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int __init ds1337_init(void) |
| 365 | { |
| 366 | return i2c_add_driver(&ds1337_driver); |
| 367 | } |
| 368 | |
| 369 | static void __exit ds1337_exit(void) |
| 370 | { |
| 371 | i2c_del_driver(&ds1337_driver); |
| 372 | } |
| 373 | |
| 374 | MODULE_AUTHOR("James Chapman <jchapman@katalix.com>"); |
| 375 | MODULE_DESCRIPTION("DS1337 RTC driver"); |
| 376 | MODULE_LICENSE("GPL"); |
| 377 | |
Ladislav Michl | da17838 | 2005-05-11 10:32:54 +0200 | [diff] [blame] | 378 | EXPORT_SYMBOL_GPL(ds1337_do_command); |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | module_init(ds1337_init); |
| 381 | module_exit(ds1337_exit); |