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