ds1337.c 9.3 KB

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