ds1337.c 9.8 KB

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