Randy Vinson | c124a78 | 2005-06-03 14:36:06 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * drivers/i2c/chips/ds1374.c |
| 3 | * |
| 4 | * I2C client/driver for the Maxim/Dallas DS1374 Real-Time Clock |
| 5 | * |
| 6 | * Author: Randy Vinson <rvinson@mvista.com> |
| 7 | * |
| 8 | * Based on the m41t00.c by Mark Greer <mgreer@mvista.com> |
| 9 | * |
| 10 | * 2005 (c) MontaVista Software, Inc. This file is licensed under |
| 11 | * the terms of the GNU General Public License version 2. This program |
| 12 | * is licensed "as is" without any warranty of any kind, whether express |
| 13 | * or implied. |
| 14 | */ |
| 15 | /* |
| 16 | * This i2c client/driver wedges between the drivers/char/genrtc.c RTC |
| 17 | * interface and the SMBus interface of the i2c subsystem. |
| 18 | * It would be more efficient to use i2c msgs/i2c_transfer directly but, as |
| 19 | * recommened in .../Documentation/i2c/writing-clients section |
| 20 | * "Sending and receiving", using SMBus level communication is preferred. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/rtc.h> |
| 28 | #include <linux/bcd.h> |
| 29 | |
| 30 | #include <asm/time.h> |
| 31 | #include <asm/rtc.h> |
| 32 | |
| 33 | #define DS1374_REG_TOD0 0x00 |
| 34 | #define DS1374_REG_TOD1 0x01 |
| 35 | #define DS1374_REG_TOD2 0x02 |
| 36 | #define DS1374_REG_TOD3 0x03 |
| 37 | #define DS1374_REG_WDALM0 0x04 |
| 38 | #define DS1374_REG_WDALM1 0x05 |
| 39 | #define DS1374_REG_WDALM2 0x06 |
| 40 | #define DS1374_REG_CR 0x07 |
| 41 | #define DS1374_REG_SR 0x08 |
| 42 | #define DS1374_REG_SR_OSF 0x80 |
| 43 | #define DS1374_REG_TCR 0x09 |
| 44 | |
| 45 | #define DS1374_DRV_NAME "ds1374" |
| 46 | |
| 47 | static DECLARE_MUTEX(ds1374_mutex); |
| 48 | |
| 49 | static struct i2c_driver ds1374_driver; |
| 50 | static struct i2c_client *save_client; |
| 51 | |
| 52 | static unsigned short ignore[] = { I2C_CLIENT_END }; |
| 53 | static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END }; |
| 54 | |
| 55 | static struct i2c_client_address_data addr_data = { |
| 56 | .normal_i2c = normal_addr, |
| 57 | .normal_i2c_range = ignore, |
| 58 | .probe = ignore, |
| 59 | .probe_range = ignore, |
| 60 | .ignore = ignore, |
| 61 | .ignore_range = ignore, |
| 62 | .force = ignore, |
| 63 | }; |
| 64 | |
| 65 | static ulong ds1374_read_rtc(void) |
| 66 | { |
| 67 | ulong time = 0; |
| 68 | int reg = DS1374_REG_WDALM0; |
| 69 | |
| 70 | while (reg--) { |
| 71 | s32 tmp; |
| 72 | if ((tmp = i2c_smbus_read_byte_data(save_client, reg)) < 0) { |
| 73 | dev_warn(&save_client->dev, |
| 74 | "can't read from rtc chip\n"); |
| 75 | return 0; |
| 76 | } |
| 77 | time = (time << 8) | (tmp & 0xff); |
| 78 | } |
| 79 | return time; |
| 80 | } |
| 81 | |
| 82 | static void ds1374_write_rtc(ulong time) |
| 83 | { |
| 84 | int reg; |
| 85 | |
| 86 | for (reg = DS1374_REG_TOD0; reg < DS1374_REG_WDALM0; reg++) { |
| 87 | if (i2c_smbus_write_byte_data(save_client, reg, time & 0xff) |
| 88 | < 0) { |
| 89 | dev_warn(&save_client->dev, |
| 90 | "can't write to rtc chip\n"); |
| 91 | break; |
| 92 | } |
| 93 | time = time >> 8; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void ds1374_check_rtc_status(void) |
| 98 | { |
| 99 | s32 tmp; |
| 100 | |
| 101 | tmp = i2c_smbus_read_byte_data(save_client, DS1374_REG_SR); |
| 102 | if (tmp < 0) { |
| 103 | dev_warn(&save_client->dev, |
| 104 | "can't read status from rtc chip\n"); |
| 105 | return; |
| 106 | } |
| 107 | if (tmp & DS1374_REG_SR_OSF) { |
| 108 | dev_warn(&save_client->dev, |
| 109 | "oscillator discontinuity flagged, time unreliable\n"); |
| 110 | tmp &= ~DS1374_REG_SR_OSF; |
| 111 | tmp = i2c_smbus_write_byte_data(save_client, DS1374_REG_SR, |
| 112 | tmp & 0xff); |
| 113 | if (tmp < 0) |
| 114 | dev_warn(&save_client->dev, |
| 115 | "can't clear discontinuity notification\n"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | ulong ds1374_get_rtc_time(void) |
| 120 | { |
| 121 | ulong t1, t2; |
| 122 | int limit = 10; /* arbitrary retry limit */ |
| 123 | |
| 124 | down(&ds1374_mutex); |
| 125 | |
| 126 | /* |
| 127 | * Since the reads are being performed one byte at a time using |
| 128 | * the SMBus vs a 4-byte i2c transfer, there is a chance that a |
| 129 | * carry will occur during the read. To detect this, 2 reads are |
| 130 | * performed and compared. |
| 131 | */ |
| 132 | do { |
| 133 | t1 = ds1374_read_rtc(); |
| 134 | t2 = ds1374_read_rtc(); |
| 135 | } while (t1 != t2 && limit--); |
| 136 | |
| 137 | up(&ds1374_mutex); |
| 138 | |
| 139 | if (t1 != t2) { |
| 140 | dev_warn(&save_client->dev, |
| 141 | "can't get consistent time from rtc chip\n"); |
| 142 | t1 = 0; |
| 143 | } |
| 144 | |
| 145 | return t1; |
| 146 | } |
| 147 | |
| 148 | static void ds1374_set_tlet(ulong arg) |
| 149 | { |
| 150 | ulong t1, t2; |
| 151 | int limit = 10; /* arbitrary retry limit */ |
| 152 | |
| 153 | t1 = *(ulong *) arg; |
| 154 | |
| 155 | down(&ds1374_mutex); |
| 156 | |
| 157 | /* |
| 158 | * Since the writes are being performed one byte at a time using |
| 159 | * the SMBus vs a 4-byte i2c transfer, there is a chance that a |
| 160 | * carry will occur during the write. To detect this, the write |
| 161 | * value is read back and compared. |
| 162 | */ |
| 163 | do { |
| 164 | ds1374_write_rtc(t1); |
| 165 | t2 = ds1374_read_rtc(); |
| 166 | } while (t1 != t2 && limit--); |
| 167 | |
| 168 | up(&ds1374_mutex); |
| 169 | |
| 170 | if (t1 != t2) |
| 171 | dev_warn(&save_client->dev, |
| 172 | "can't confirm time set from rtc chip\n"); |
| 173 | } |
| 174 | |
| 175 | ulong new_time; |
| 176 | |
| 177 | DECLARE_TASKLET_DISABLED(ds1374_tasklet, ds1374_set_tlet, (ulong) & new_time); |
| 178 | |
| 179 | int ds1374_set_rtc_time(ulong nowtime) |
| 180 | { |
| 181 | new_time = nowtime; |
| 182 | |
| 183 | if (in_interrupt()) |
| 184 | tasklet_schedule(&ds1374_tasklet); |
| 185 | else |
| 186 | ds1374_set_tlet((ulong) & new_time); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | ***************************************************************************** |
| 193 | * |
| 194 | * Driver Interface |
| 195 | * |
| 196 | ***************************************************************************** |
| 197 | */ |
| 198 | static int ds1374_probe(struct i2c_adapter *adap, int addr, int kind) |
| 199 | { |
| 200 | struct i2c_client *client; |
| 201 | int rc; |
| 202 | |
| 203 | client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL); |
| 204 | if (!client) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | memset(client, 0, sizeof(struct i2c_client)); |
| 208 | strncpy(client->name, DS1374_DRV_NAME, I2C_NAME_SIZE); |
| 209 | client->flags = I2C_DF_NOTIFY; |
| 210 | client->addr = addr; |
| 211 | client->adapter = adap; |
| 212 | client->driver = &ds1374_driver; |
| 213 | |
| 214 | if ((rc = i2c_attach_client(client)) != 0) { |
| 215 | kfree(client); |
| 216 | return rc; |
| 217 | } |
| 218 | |
| 219 | save_client = client; |
| 220 | |
| 221 | ds1374_check_rtc_status(); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int ds1374_attach(struct i2c_adapter *adap) |
| 227 | { |
| 228 | return i2c_probe(adap, &addr_data, ds1374_probe); |
| 229 | } |
| 230 | |
| 231 | static int ds1374_detach(struct i2c_client *client) |
| 232 | { |
| 233 | int rc; |
| 234 | |
| 235 | if ((rc = i2c_detach_client(client)) == 0) { |
| 236 | kfree(i2c_get_clientdata(client)); |
| 237 | tasklet_kill(&ds1374_tasklet); |
| 238 | } |
| 239 | return rc; |
| 240 | } |
| 241 | |
| 242 | static struct i2c_driver ds1374_driver = { |
| 243 | .owner = THIS_MODULE, |
| 244 | .name = DS1374_DRV_NAME, |
| 245 | .id = I2C_DRIVERID_DS1374, |
| 246 | .flags = I2C_DF_NOTIFY, |
| 247 | .attach_adapter = ds1374_attach, |
| 248 | .detach_client = ds1374_detach, |
| 249 | }; |
| 250 | |
| 251 | static int __init ds1374_init(void) |
| 252 | { |
| 253 | return i2c_add_driver(&ds1374_driver); |
| 254 | } |
| 255 | |
| 256 | static void __exit ds1374_exit(void) |
| 257 | { |
| 258 | i2c_del_driver(&ds1374_driver); |
| 259 | } |
| 260 | |
| 261 | module_init(ds1374_init); |
| 262 | module_exit(ds1374_exit); |
| 263 | |
| 264 | MODULE_AUTHOR("Randy Vinson <rvinson@mvista.com>"); |
| 265 | MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC I2C Client Driver"); |
| 266 | MODULE_LICENSE("GPL"); |