ds1337.c 9.7 KB

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