Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/i2c/chips/m41t00.c |
| 3 | * |
| 4 | * I2C client/driver for the ST M41T00 Real-Time Clock chip. |
| 5 | * |
| 6 | * Author: Mark A. Greer <mgreer@mvista.com> |
| 7 | * |
| 8 | * 2005 (c) MontaVista Software, Inc. This file is licensed under |
| 9 | * the terms of the GNU General Public License version 2. This program |
| 10 | * is licensed "as is" without any warranty of any kind, whether express |
| 11 | * or implied. |
| 12 | */ |
| 13 | /* |
| 14 | * This i2c client/driver wedges between the drivers/char/genrtc.c RTC |
| 15 | * interface and the SMBus interface of the i2c subsystem. |
| 16 | * It would be more efficient to use i2c msgs/i2c_transfer directly but, as |
| 17 | * recommened in .../Documentation/i2c/writing-clients section |
| 18 | * "Sending and receiving", using SMBus level communication is preferred. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/i2c.h> |
| 25 | #include <linux/rtc.h> |
| 26 | #include <linux/bcd.h> |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/time.h> |
| 30 | #include <asm/rtc.h> |
| 31 | |
| 32 | #define M41T00_DRV_NAME "m41t00" |
| 33 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 34 | static DEFINE_MUTEX(m41t00_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | static struct i2c_driver m41t00_driver; |
| 37 | static struct i2c_client *save_client; |
| 38 | |
| 39 | static unsigned short ignore[] = { I2C_CLIENT_END }; |
| 40 | static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END }; |
| 41 | |
| 42 | static struct i2c_client_address_data addr_data = { |
| 43 | .normal_i2c = normal_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | .probe = ignore, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | .ignore = ignore, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | ulong |
| 49 | m41t00_get_rtc_time(void) |
| 50 | { |
| 51 | s32 sec, min, hour, day, mon, year; |
| 52 | s32 sec1, min1, hour1, day1, mon1, year1; |
| 53 | ulong limit = 10; |
| 54 | |
| 55 | sec = min = hour = day = mon = year = 0; |
| 56 | sec1 = min1 = hour1 = day1 = mon1 = year1 = 0; |
| 57 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 58 | mutex_lock(&m41t00_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | do { |
| 60 | if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0) |
| 61 | && ((min = i2c_smbus_read_byte_data(save_client, 1)) |
| 62 | >= 0) |
| 63 | && ((hour = i2c_smbus_read_byte_data(save_client, 2)) |
| 64 | >= 0) |
| 65 | && ((day = i2c_smbus_read_byte_data(save_client, 4)) |
| 66 | >= 0) |
| 67 | && ((mon = i2c_smbus_read_byte_data(save_client, 5)) |
| 68 | >= 0) |
| 69 | && ((year = i2c_smbus_read_byte_data(save_client, 6)) |
| 70 | >= 0) |
| 71 | && ((sec == sec1) && (min == min1) && (hour == hour1) |
| 72 | && (day == day1) && (mon == mon1) |
| 73 | && (year == year1))) |
| 74 | |
| 75 | break; |
| 76 | |
| 77 | sec1 = sec; |
| 78 | min1 = min; |
| 79 | hour1 = hour; |
| 80 | day1 = day; |
| 81 | mon1 = mon; |
| 82 | year1 = year; |
| 83 | } while (--limit > 0); |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 84 | mutex_unlock(&m41t00_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | if (limit == 0) { |
| 87 | dev_warn(&save_client->dev, |
| 88 | "m41t00: can't read rtc chip\n"); |
| 89 | sec = min = hour = day = mon = year = 0; |
| 90 | } |
| 91 | |
| 92 | sec &= 0x7f; |
| 93 | min &= 0x7f; |
| 94 | hour &= 0x3f; |
| 95 | day &= 0x3f; |
| 96 | mon &= 0x1f; |
| 97 | year &= 0xff; |
| 98 | |
| 99 | BCD_TO_BIN(sec); |
| 100 | BCD_TO_BIN(min); |
| 101 | BCD_TO_BIN(hour); |
| 102 | BCD_TO_BIN(day); |
| 103 | BCD_TO_BIN(mon); |
| 104 | BCD_TO_BIN(year); |
| 105 | |
| 106 | year += 1900; |
| 107 | if (year < 1970) |
| 108 | year += 100; |
| 109 | |
| 110 | return mktime(year, mon, day, hour, min, sec); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | m41t00_set_tlet(ulong arg) |
| 115 | { |
| 116 | struct rtc_time tm; |
| 117 | ulong nowtime = *(ulong *)arg; |
| 118 | |
| 119 | to_tm(nowtime, &tm); |
| 120 | tm.tm_year = (tm.tm_year - 1900) % 100; |
| 121 | |
| 122 | BIN_TO_BCD(tm.tm_sec); |
| 123 | BIN_TO_BCD(tm.tm_min); |
| 124 | BIN_TO_BCD(tm.tm_hour); |
| 125 | BIN_TO_BCD(tm.tm_mon); |
| 126 | BIN_TO_BCD(tm.tm_mday); |
| 127 | BIN_TO_BCD(tm.tm_year); |
| 128 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 129 | mutex_lock(&m41t00_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0) |
| 131 | || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f) |
| 132 | < 0) |
| 133 | || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x7f) |
| 134 | < 0) |
| 135 | || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x7f) |
| 136 | < 0) |
| 137 | || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x7f) |
| 138 | < 0) |
| 139 | || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0x7f) |
| 140 | < 0)) |
| 141 | |
| 142 | dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n"); |
| 143 | |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 144 | mutex_unlock(&m41t00_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
Mark A. Greer | a44e40b | 2005-09-01 18:09:54 -0700 | [diff] [blame] | 148 | static ulong new_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | DECLARE_TASKLET_DISABLED(m41t00_tasklet, m41t00_set_tlet, (ulong)&new_time); |
| 151 | |
| 152 | int |
| 153 | m41t00_set_rtc_time(ulong nowtime) |
| 154 | { |
| 155 | new_time = nowtime; |
| 156 | |
| 157 | if (in_interrupt()) |
| 158 | tasklet_schedule(&m41t00_tasklet); |
| 159 | else |
| 160 | m41t00_set_tlet((ulong)&new_time); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | ***************************************************************************** |
| 167 | * |
| 168 | * Driver Interface |
| 169 | * |
| 170 | ***************************************************************************** |
| 171 | */ |
| 172 | static int |
| 173 | m41t00_probe(struct i2c_adapter *adap, int addr, int kind) |
| 174 | { |
| 175 | struct i2c_client *client; |
| 176 | int rc; |
| 177 | |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 178 | client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (!client) |
| 180 | return -ENOMEM; |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | client->addr = addr; |
| 184 | client->adapter = adap; |
| 185 | client->driver = &m41t00_driver; |
| 186 | |
| 187 | if ((rc = i2c_attach_client(client)) != 0) { |
| 188 | kfree(client); |
| 189 | return rc; |
| 190 | } |
| 191 | |
| 192 | save_client = client; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int |
| 197 | m41t00_attach(struct i2c_adapter *adap) |
| 198 | { |
| 199 | return i2c_probe(adap, &addr_data, m41t00_probe); |
| 200 | } |
| 201 | |
| 202 | static int |
| 203 | m41t00_detach(struct i2c_client *client) |
| 204 | { |
| 205 | int rc; |
| 206 | |
| 207 | if ((rc = i2c_detach_client(client)) == 0) { |
Jean Delvare | 5da69ba | 2005-07-01 14:28:15 +0200 | [diff] [blame] | 208 | kfree(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | tasklet_kill(&m41t00_tasklet); |
| 210 | } |
| 211 | return rc; |
| 212 | } |
| 213 | |
| 214 | static struct i2c_driver m41t00_driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 215 | .driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 216 | .name = M41T00_DRV_NAME, |
| 217 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | .id = I2C_DRIVERID_STM41T00, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | .attach_adapter = m41t00_attach, |
| 220 | .detach_client = m41t00_detach, |
| 221 | }; |
| 222 | |
| 223 | static int __init |
| 224 | m41t00_init(void) |
| 225 | { |
| 226 | return i2c_add_driver(&m41t00_driver); |
| 227 | } |
| 228 | |
| 229 | static void __exit |
| 230 | m41t00_exit(void) |
| 231 | { |
| 232 | i2c_del_driver(&m41t00_driver); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | module_init(m41t00_init); |
| 237 | module_exit(m41t00_exit); |
| 238 | |
| 239 | MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>"); |
| 240 | MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver"); |
| 241 | MODULE_LICENSE("GPL"); |