| 1 | |
|---|
| 2 | /* |
|---|
| 3 | * linux/drivers/i2c/ds1307.c |
|---|
| 4 | * |
|---|
| 5 | * Author: Abraham van der Merwe <abraham@2d3d.co.za> |
|---|
| 6 | * |
|---|
| 7 | * Linux support for the Dallas Semiconductor DS1307 Serial Real-Time |
|---|
| 8 | * Clock. |
|---|
| 9 | * |
|---|
| 10 | * Based on code from the lm-sensors project which is available |
|---|
| 11 | * at http://www.lm-sensors.nu/ and Russell King's PCF8583 Real-Time |
|---|
| 12 | * Clock driver (linux/drivers/acorn/char/pcf8583.c). |
|---|
| 13 | * |
|---|
| 14 | * This source code is free software; you can redistribute it and/or |
|---|
| 15 | * modify it under the terms of the GNU General Public License |
|---|
| 16 | * version 2 as published by the Free Software Foundation. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <linux/config.h> |
|---|
| 20 | #include <linux/version.h> |
|---|
| 21 | #include <linux/module.h> |
|---|
| 22 | |
|---|
| 23 | #include <linux/kernel.h> |
|---|
| 24 | #include <linux/string.h> |
|---|
| 25 | #include <linux/slab.h> |
|---|
| 26 | #include <linux/init.h> |
|---|
| 27 | #include <linux/i2c.h> |
|---|
| 28 | |
|---|
| 29 | #include <asm/semaphore.h> |
|---|
| 30 | |
|---|
| 31 | #include "ds1307.h" |
|---|
| 32 | |
|---|
| 33 | #define BCD_TO_BIN(x) (((x) & 15) + ((x) >> 4) * 10) |
|---|
| 34 | #define BIN_TO_BCD(x) ((((x) / 10) << 4) + (x) % 10) |
|---|
| 35 | |
|---|
| 36 | static struct i2c_driver ds1307; |
|---|
| 37 | static DECLARE_MUTEX (mutex); |
|---|
| 38 | |
|---|
| 39 | /* |
|---|
| 40 | * The DS1307 Real-Time Clock wants the address in a different |
|---|
| 41 | * message, so we can't use the normal i2c_master_recv() routine |
|---|
| 42 | * for receiving data. |
|---|
| 43 | */ |
|---|
| 44 | static int ds1307_i2c_recv (struct i2c_client *client,char *buf,char addr,int count) |
|---|
| 45 | { |
|---|
| 46 | struct i2c_msg msg[] = { |
|---|
| 47 | { addr: client->addr, flags: 0, len: 1, buf: &addr }, |
|---|
| 48 | { addr: client->addr, flags: I2C_M_RD, len: count, buf: buf } |
|---|
| 49 | }; |
|---|
| 50 | int result = 0; |
|---|
| 51 | |
|---|
| 52 | if (down_interruptible (&mutex)) |
|---|
| 53 | return (-ERESTARTSYS); |
|---|
| 54 | |
|---|
| 55 | if (i2c_transfer (client->adapter,msg,2) != 2) |
|---|
| 56 | result = -EIO; |
|---|
| 57 | |
|---|
| 58 | up (&mutex); |
|---|
| 59 | |
|---|
| 60 | return (result); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /* |
|---|
| 64 | * Would've been nice to specify the address to this as well, but then we |
|---|
| 65 | * would need to copy the buffer twice - not worth it... |
|---|
| 66 | */ |
|---|
| 67 | static int ds1307_i2c_send (struct i2c_client *client,const char *buf,int count) |
|---|
| 68 | { |
|---|
| 69 | int result = 0; |
|---|
| 70 | |
|---|
| 71 | if (down_interruptible (&mutex)) |
|---|
| 72 | return (-ERESTARTSYS); |
|---|
| 73 | |
|---|
| 74 | if (i2c_master_send (client,(const char *) buf,count) != count) |
|---|
| 75 | result = -EIO; |
|---|
| 76 | |
|---|
| 77 | up (&mutex); |
|---|
| 78 | |
|---|
| 79 | return (result); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | static int ds1307_attach (struct i2c_adapter *adapter,int addr,unsigned short flags,int kind) |
|---|
| 83 | { |
|---|
| 84 | struct i2c_client *client; |
|---|
| 85 | int result; |
|---|
| 86 | |
|---|
| 87 | if ((client = (struct i2c_client *) kmalloc (sizeof (struct i2c_client),GFP_KERNEL)) == NULL) |
|---|
| 88 | return (-ENOMEM); |
|---|
| 89 | |
|---|
| 90 | strcpy (client->name,ds1307.name); |
|---|
| 91 | client->id = ds1307.id; |
|---|
| 92 | client->flags = I2C_CLIENT_ALLOW_USE | I2C_CLIENT_ALLOW_MULTIPLE_USE; |
|---|
| 93 | client->addr = addr; |
|---|
| 94 | client->adapter = adapter; |
|---|
| 95 | client->driver = &ds1307; |
|---|
| 96 | client->data = NULL; |
|---|
| 97 | |
|---|
| 98 | if ((result = i2c_attach_client (client))) { |
|---|
| 99 | kfree (client); |
|---|
| 100 | return (result); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return (0); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | static int ds1307_attach_adapter (struct i2c_adapter *adapter) |
|---|
| 107 | { |
|---|
| 108 | static unsigned short ignore[] = { I2C_CLIENT_END }; |
|---|
| 109 | static unsigned short addr[] = { 0x68, I2C_CLIENT_END }; |
|---|
| 110 | static struct i2c_client_address_data ds1307_addr_data = { |
|---|
| 111 | normal_i2c: addr, |
|---|
| 112 | normal_i2c_range: ignore, |
|---|
| 113 | probe: ignore, |
|---|
| 114 | probe_range: ignore, |
|---|
| 115 | ignore: ignore, |
|---|
| 116 | ignore_range: ignore, |
|---|
| 117 | force: ignore |
|---|
| 118 | }; |
|---|
| 119 | |
|---|
| 120 | return (i2c_probe (adapter,&ds1307_addr_data,ds1307_attach)); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | static int ds1307_detach_client (struct i2c_client *client) |
|---|
| 124 | { |
|---|
| 125 | int result; |
|---|
| 126 | |
|---|
| 127 | if ((result = i2c_detach_client (client))) |
|---|
| 128 | return (result); |
|---|
| 129 | |
|---|
| 130 | kfree (client); |
|---|
| 131 | |
|---|
| 132 | return (0); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | static int ds1307_getdate (struct i2c_client *client,void *arg) |
|---|
| 136 | { |
|---|
| 137 | struct ds1307_date *date = (struct ds1307_date *) arg; |
|---|
| 138 | u8 buf[7]; |
|---|
| 139 | |
|---|
| 140 | /* this also enables the oscillator */ |
|---|
| 141 | memset (buf,0,7); |
|---|
| 142 | |
|---|
| 143 | /* enable 24-hour mode */ |
|---|
| 144 | buf[2] = 0x40; |
|---|
| 145 | |
|---|
| 146 | if (ds1307_i2c_recv (client,(char *) buf,0,7) < 0) |
|---|
| 147 | return (-EIO); |
|---|
| 148 | |
|---|
| 149 | date->tm_sec = BCD_TO_BIN (buf[0] & ~0x80); |
|---|
| 150 | date->tm_min = BCD_TO_BIN (buf[1]); |
|---|
| 151 | date->tm_hour = BCD_TO_BIN (buf[2] & 0x3f); |
|---|
| 152 | date->tm_wday = BCD_TO_BIN (buf[3]) - 1; |
|---|
| 153 | date->tm_mday = BCD_TO_BIN (buf[4]); |
|---|
| 154 | date->tm_mon = BCD_TO_BIN (buf[5]) - 1; |
|---|
| 155 | date->tm_year = BCD_TO_BIN (buf[6]) + 100; |
|---|
| 156 | |
|---|
| 157 | return (0); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | static int ds1307_setdate (struct i2c_client *client,void *arg) |
|---|
| 161 | { |
|---|
| 162 | struct ds1307_date *date = (struct ds1307_date *) arg; |
|---|
| 163 | u8 buf[8]; |
|---|
| 164 | |
|---|
| 165 | /* select address 0 */ |
|---|
| 166 | buf[0] = 0; |
|---|
| 167 | |
|---|
| 168 | buf[1] = BIN_TO_BCD (date->tm_sec); |
|---|
| 169 | buf[2] = BIN_TO_BCD (date->tm_min); |
|---|
| 170 | buf[3] = BIN_TO_BCD (date->tm_hour) | 0x40; |
|---|
| 171 | buf[4] = BIN_TO_BCD (date->tm_wday + 1); |
|---|
| 172 | buf[5] = BIN_TO_BCD (date->tm_mday); |
|---|
| 173 | buf[6] = BIN_TO_BCD (date->tm_mon + 1); |
|---|
| 174 | buf[7] = BIN_TO_BCD (date->tm_year - 100); |
|---|
| 175 | |
|---|
| 176 | return (ds1307_i2c_send (client,(const char *) buf,8)); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | static int ds1307_enable (struct i2c_client *client,void *arg) |
|---|
| 180 | { |
|---|
| 181 | u8 buf[2]; |
|---|
| 182 | |
|---|
| 183 | if (ds1307_i2c_recv (client,(char *) buf + 1,0,1) < 0) |
|---|
| 184 | return (-EIO); |
|---|
| 185 | |
|---|
| 186 | if ((buf[1] & 0x80)) { |
|---|
| 187 | buf[0] = 0, buf[1] &= ~0x80; |
|---|
| 188 | return (ds1307_i2c_send (client,(const char *) buf,2)); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | return (0); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | static int ds1307_irqon (struct i2c_client *client,void *arg) |
|---|
| 195 | { |
|---|
| 196 | u8 buf[2]; |
|---|
| 197 | |
|---|
| 198 | if (ds1307_i2c_recv (client,(char *) buf + 1,7,1) < 0) |
|---|
| 199 | return (-EIO); |
|---|
| 200 | |
|---|
| 201 | buf[0] = 7; |
|---|
| 202 | buf[1] |= 0x10; |
|---|
| 203 | |
|---|
| 204 | return (ds1307_i2c_send (client,(const char *) buf,2)); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | static int ds1307_irqoff (struct i2c_client *client,void *arg) |
|---|
| 208 | { |
|---|
| 209 | u8 buf[2]; |
|---|
| 210 | |
|---|
| 211 | if (ds1307_i2c_recv (client,(char *) buf + 1,7,1) < 0) |
|---|
| 212 | return (-EIO); |
|---|
| 213 | |
|---|
| 214 | buf[0] = 7; |
|---|
| 215 | buf[1] &= ~0x10; |
|---|
| 216 | |
|---|
| 217 | return (ds1307_i2c_send (client,(const char *) buf,2)); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | static int ds1307_getfreq (struct i2c_client *client,void *arg) |
|---|
| 221 | { |
|---|
| 222 | u16 *freq = (u16 *) arg; |
|---|
| 223 | u8 buf; |
|---|
| 224 | static const u16 table[] = { |
|---|
| 225 | DS1307_FREQ_1HZ, |
|---|
| 226 | DS1307_FREQ_4KHZ, |
|---|
| 227 | DS1307_FREQ_8KHZ, |
|---|
| 228 | DS1307_FREQ_32KHZ |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | if (ds1307_i2c_recv (client,(char *) &buf,7,1) < 0) |
|---|
| 232 | return (-EIO); |
|---|
| 233 | |
|---|
| 234 | *freq = table[buf & 3]; |
|---|
| 235 | |
|---|
| 236 | return (0); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | static int ds1307_setfreq (struct i2c_client *client,void *arg) |
|---|
| 240 | { |
|---|
| 241 | u16 *freq = (u16 *) arg; |
|---|
| 242 | u8 buf[2]; |
|---|
| 243 | |
|---|
| 244 | /* select address 7 */ |
|---|
| 245 | buf[0] = 7; |
|---|
| 246 | |
|---|
| 247 | /* default to 1HZ */ |
|---|
| 248 | buf[1] = 0; |
|---|
| 249 | |
|---|
| 250 | switch (*freq) { |
|---|
| 251 | case DS1307_FREQ_32KHZ: buf[1]++; |
|---|
| 252 | case DS1307_FREQ_8KHZ: buf[1]++; |
|---|
| 253 | case DS1307_FREQ_4KHZ: buf[1]++; |
|---|
| 254 | case DS1307_FREQ_1HZ: break; |
|---|
| 255 | default: |
|---|
| 256 | return (-EINVAL); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | return (ds1307_i2c_send (client,(const char *) buf,2)); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | static int ds1307_read (struct i2c_client *client,void *arg) |
|---|
| 263 | { |
|---|
| 264 | struct ds1307_memory *mem = (struct ds1307_memory *) arg; |
|---|
| 265 | u8 buf[DS1307_SIZE]; |
|---|
| 266 | |
|---|
| 267 | if (mem->offset >= DS1307_SIZE || mem->offset + mem->length > DS1307_SIZE) |
|---|
| 268 | return (-EINVAL); |
|---|
| 269 | |
|---|
| 270 | if (ds1307_i2c_recv (client,(char *) buf,mem->offset + 8,mem->length) < 0) |
|---|
| 271 | return (-EIO); |
|---|
| 272 | |
|---|
| 273 | memcpy (mem->buf,buf,mem->length); |
|---|
| 274 | |
|---|
| 275 | return (0); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | static int ds1307_write (struct i2c_client *client,void *arg) |
|---|
| 279 | { |
|---|
| 280 | struct ds1307_memory *mem = (struct ds1307_memory *) arg; |
|---|
| 281 | u8 buf[DS1307_SIZE + 1]; |
|---|
| 282 | |
|---|
| 283 | if (mem->offset >= DS1307_SIZE || mem->offset + mem->length > DS1307_SIZE) |
|---|
| 284 | return (-EINVAL); |
|---|
| 285 | |
|---|
| 286 | buf[0] = mem->offset + 8; |
|---|
| 287 | |
|---|
| 288 | memcpy (buf + 1,mem->buf,mem->length); |
|---|
| 289 | |
|---|
| 290 | return (ds1307_i2c_send (client,(const char *) buf,mem->length + 1)); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | static int ds1307_command (struct i2c_client *client,unsigned int cmd,void *arg) |
|---|
| 294 | { |
|---|
| 295 | static const struct { |
|---|
| 296 | int cmd; |
|---|
| 297 | int (*function) (struct i2c_client *,void *arg); |
|---|
| 298 | } ioctl[] = { |
|---|
| 299 | { DS1307_ENABLE, ds1307_enable }, |
|---|
| 300 | { DS1307_GET_DATE, ds1307_getdate }, |
|---|
| 301 | { DS1307_SET_DATE, ds1307_setdate }, |
|---|
| 302 | { DS1307_IRQ_ON, ds1307_irqon }, |
|---|
| 303 | { DS1307_IRQ_OFF, ds1307_irqoff }, |
|---|
| 304 | { DS1307_GET_FREQ, ds1307_getfreq }, |
|---|
| 305 | { DS1307_SET_FREQ, ds1307_setfreq }, |
|---|
| 306 | { DS1307_READ, ds1307_read }, |
|---|
| 307 | { DS1307_WRITE, ds1307_write } |
|---|
| 308 | }; |
|---|
| 309 | int i; |
|---|
| 310 | |
|---|
| 311 | for (i = 0; i < sizeof (ioctl) / sizeof (ioctl[0]); i++) |
|---|
| 312 | if (ioctl[i].cmd == cmd) |
|---|
| 313 | return (ioctl[i].function (client,arg)); |
|---|
| 314 | |
|---|
| 315 | return (-EINVAL); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | static struct i2c_driver ds1307 = { |
|---|
| 320 | .owner = THIS_MODULE, |
|---|
| 321 | .name = "ds1307", |
|---|
| 322 | .id = I2C_DRIVERID_DS1307, |
|---|
| 323 | .flags = I2C_DF_NOTIFY, |
|---|
| 324 | .attach_adapter = ds1307_attach_adapter, |
|---|
| 325 | .detach_client = ds1307_detach_client, |
|---|
| 326 | .command = ds1307_command, |
|---|
| 327 | }; |
|---|
| 328 | |
|---|
| 329 | static int __init sm_ds1307_init(void) |
|---|
| 330 | { |
|---|
| 331 | printk ("Dallas Semiconductor DS1307 Real-Time Clock driver (V0.03)\n"); |
|---|
| 332 | return i2c_add_driver(&ds1307); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | static void __exit sm_ds1307_exit(void) |
|---|
| 336 | { |
|---|
| 337 | i2c_del_driver(&ds1307); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | MODULE_AUTHOR ("Abraham van der Merwe <abraham@2d3d.co.za>"); |
|---|
| 343 | MODULE_DESCRIPTION ("Linux support for DS1307 Real-Time Clock"); |
|---|
| 344 | |
|---|
| 345 | MODULE_LICENSE ("GPL"); |
|---|
| 346 | |
|---|
| 347 | module_init(sm_ds1307_init); |
|---|
| 348 | module_exit(sm_ds1307_exit); |
|---|
| 349 | |
|---|