ds1374.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * drivers/i2c/chips/ds1374.c
  3. *
  4. * I2C client/driver for the Maxim/Dallas DS1374 Real-Time Clock
  5. *
  6. * Author: Randy Vinson <rvinson@mvista.com>
  7. *
  8. * Based on the m41t00.c by Mark Greer <mgreer@mvista.com>
  9. *
  10. * 2005 (c) MontaVista Software, Inc. This file is licensed under
  11. * the terms of the GNU General Public License version 2. This program
  12. * is licensed "as is" without any warranty of any kind, whether express
  13. * or implied.
  14. */
  15. /*
  16. * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
  17. * interface and the SMBus interface of the i2c subsystem.
  18. * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
  19. * recommened in .../Documentation/i2c/writing-clients section
  20. * "Sending and receiving", using SMBus level communication is preferred.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/i2c.h>
  26. #include <linux/rtc.h>
  27. #include <linux/bcd.h>
  28. #include <asm/time.h>
  29. #include <asm/rtc.h>
  30. #define DS1374_REG_TOD0 0x00
  31. #define DS1374_REG_TOD1 0x01
  32. #define DS1374_REG_TOD2 0x02
  33. #define DS1374_REG_TOD3 0x03
  34. #define DS1374_REG_WDALM0 0x04
  35. #define DS1374_REG_WDALM1 0x05
  36. #define DS1374_REG_WDALM2 0x06
  37. #define DS1374_REG_CR 0x07
  38. #define DS1374_REG_SR 0x08
  39. #define DS1374_REG_SR_OSF 0x80
  40. #define DS1374_REG_TCR 0x09
  41. #define DS1374_DRV_NAME "ds1374"
  42. static DECLARE_MUTEX(ds1374_mutex);
  43. static struct i2c_driver ds1374_driver;
  44. static struct i2c_client *save_client;
  45. static unsigned short ignore[] = { I2C_CLIENT_END };
  46. static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
  47. static struct i2c_client_address_data addr_data = {
  48. .normal_i2c = normal_addr,
  49. .normal_i2c_range = ignore,
  50. .probe = ignore,
  51. .probe_range = ignore,
  52. .ignore = ignore,
  53. .ignore_range = ignore,
  54. .force = ignore,
  55. };
  56. static ulong ds1374_read_rtc(void)
  57. {
  58. ulong time = 0;
  59. int reg = DS1374_REG_WDALM0;
  60. while (reg--) {
  61. s32 tmp;
  62. if ((tmp = i2c_smbus_read_byte_data(save_client, reg)) < 0) {
  63. dev_warn(&save_client->dev,
  64. "can't read from rtc chip\n");
  65. return 0;
  66. }
  67. time = (time << 8) | (tmp & 0xff);
  68. }
  69. return time;
  70. }
  71. static void ds1374_write_rtc(ulong time)
  72. {
  73. int reg;
  74. for (reg = DS1374_REG_TOD0; reg < DS1374_REG_WDALM0; reg++) {
  75. if (i2c_smbus_write_byte_data(save_client, reg, time & 0xff)
  76. < 0) {
  77. dev_warn(&save_client->dev,
  78. "can't write to rtc chip\n");
  79. break;
  80. }
  81. time = time >> 8;
  82. }
  83. }
  84. static void ds1374_check_rtc_status(void)
  85. {
  86. s32 tmp;
  87. tmp = i2c_smbus_read_byte_data(save_client, DS1374_REG_SR);
  88. if (tmp < 0) {
  89. dev_warn(&save_client->dev,
  90. "can't read status from rtc chip\n");
  91. return;
  92. }
  93. if (tmp & DS1374_REG_SR_OSF) {
  94. dev_warn(&save_client->dev,
  95. "oscillator discontinuity flagged, time unreliable\n");
  96. tmp &= ~DS1374_REG_SR_OSF;
  97. tmp = i2c_smbus_write_byte_data(save_client, DS1374_REG_SR,
  98. tmp & 0xff);
  99. if (tmp < 0)
  100. dev_warn(&save_client->dev,
  101. "can't clear discontinuity notification\n");
  102. }
  103. }
  104. ulong ds1374_get_rtc_time(void)
  105. {
  106. ulong t1, t2;
  107. int limit = 10; /* arbitrary retry limit */
  108. down(&ds1374_mutex);
  109. /*
  110. * Since the reads are being performed one byte at a time using
  111. * the SMBus vs a 4-byte i2c transfer, there is a chance that a
  112. * carry will occur during the read. To detect this, 2 reads are
  113. * performed and compared.
  114. */
  115. do {
  116. t1 = ds1374_read_rtc();
  117. t2 = ds1374_read_rtc();
  118. } while (t1 != t2 && limit--);
  119. up(&ds1374_mutex);
  120. if (t1 != t2) {
  121. dev_warn(&save_client->dev,
  122. "can't get consistent time from rtc chip\n");
  123. t1 = 0;
  124. }
  125. return t1;
  126. }
  127. static void ds1374_set_tlet(ulong arg)
  128. {
  129. ulong t1, t2;
  130. int limit = 10; /* arbitrary retry limit */
  131. t1 = *(ulong *) arg;
  132. down(&ds1374_mutex);
  133. /*
  134. * Since the writes are being performed one byte at a time using
  135. * the SMBus vs a 4-byte i2c transfer, there is a chance that a
  136. * carry will occur during the write. To detect this, the write
  137. * value is read back and compared.
  138. */
  139. do {
  140. ds1374_write_rtc(t1);
  141. t2 = ds1374_read_rtc();
  142. } while (t1 != t2 && limit--);
  143. up(&ds1374_mutex);
  144. if (t1 != t2)
  145. dev_warn(&save_client->dev,
  146. "can't confirm time set from rtc chip\n");
  147. }
  148. ulong new_time;
  149. DECLARE_TASKLET_DISABLED(ds1374_tasklet, ds1374_set_tlet, (ulong) & new_time);
  150. int ds1374_set_rtc_time(ulong nowtime)
  151. {
  152. new_time = nowtime;
  153. if (in_interrupt())
  154. tasklet_schedule(&ds1374_tasklet);
  155. else
  156. ds1374_set_tlet((ulong) & new_time);
  157. return 0;
  158. }
  159. /*
  160. *****************************************************************************
  161. *
  162. * Driver Interface
  163. *
  164. *****************************************************************************
  165. */
  166. static int ds1374_probe(struct i2c_adapter *adap, int addr, int kind)
  167. {
  168. struct i2c_client *client;
  169. int rc;
  170. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  171. if (!client)
  172. return -ENOMEM;
  173. memset(client, 0, sizeof(struct i2c_client));
  174. strncpy(client->name, DS1374_DRV_NAME, I2C_NAME_SIZE);
  175. client->flags = I2C_DF_NOTIFY;
  176. client->addr = addr;
  177. client->adapter = adap;
  178. client->driver = &ds1374_driver;
  179. if ((rc = i2c_attach_client(client)) != 0) {
  180. kfree(client);
  181. return rc;
  182. }
  183. save_client = client;
  184. ds1374_check_rtc_status();
  185. return 0;
  186. }
  187. static int ds1374_attach(struct i2c_adapter *adap)
  188. {
  189. return i2c_probe(adap, &addr_data, ds1374_probe);
  190. }
  191. static int ds1374_detach(struct i2c_client *client)
  192. {
  193. int rc;
  194. if ((rc = i2c_detach_client(client)) == 0) {
  195. kfree(i2c_get_clientdata(client));
  196. tasklet_kill(&ds1374_tasklet);
  197. }
  198. return rc;
  199. }
  200. static struct i2c_driver ds1374_driver = {
  201. .owner = THIS_MODULE,
  202. .name = DS1374_DRV_NAME,
  203. .id = I2C_DRIVERID_DS1374,
  204. .flags = I2C_DF_NOTIFY,
  205. .attach_adapter = ds1374_attach,
  206. .detach_client = ds1374_detach,
  207. };
  208. static int __init ds1374_init(void)
  209. {
  210. return i2c_add_driver(&ds1374_driver);
  211. }
  212. static void __exit ds1374_exit(void)
  213. {
  214. i2c_del_driver(&ds1374_driver);
  215. }
  216. module_init(ds1374_init);
  217. module_exit(ds1374_exit);
  218. MODULE_AUTHOR("Randy Vinson <rvinson@mvista.com>");
  219. MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC I2C Client Driver");
  220. MODULE_LICENSE("GPL");