ds1374.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 <linux/mutex.h>
  29. #include <linux/workqueue.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 DEFINE_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. .probe = ignore,
  50. .ignore = ignore,
  51. };
  52. static ulong ds1374_read_rtc(void)
  53. {
  54. ulong time = 0;
  55. int reg = DS1374_REG_WDALM0;
  56. while (reg--) {
  57. s32 tmp;
  58. if ((tmp = i2c_smbus_read_byte_data(save_client, reg)) < 0) {
  59. dev_warn(&save_client->dev,
  60. "can't read from rtc chip\n");
  61. return 0;
  62. }
  63. time = (time << 8) | (tmp & 0xff);
  64. }
  65. return time;
  66. }
  67. static void ds1374_write_rtc(ulong time)
  68. {
  69. int reg;
  70. for (reg = DS1374_REG_TOD0; reg < DS1374_REG_WDALM0; reg++) {
  71. if (i2c_smbus_write_byte_data(save_client, reg, time & 0xff)
  72. < 0) {
  73. dev_warn(&save_client->dev,
  74. "can't write to rtc chip\n");
  75. break;
  76. }
  77. time = time >> 8;
  78. }
  79. }
  80. static void ds1374_check_rtc_status(void)
  81. {
  82. s32 tmp;
  83. tmp = i2c_smbus_read_byte_data(save_client, DS1374_REG_SR);
  84. if (tmp < 0) {
  85. dev_warn(&save_client->dev,
  86. "can't read status from rtc chip\n");
  87. return;
  88. }
  89. if (tmp & DS1374_REG_SR_OSF) {
  90. dev_warn(&save_client->dev,
  91. "oscillator discontinuity flagged, time unreliable\n");
  92. tmp &= ~DS1374_REG_SR_OSF;
  93. tmp = i2c_smbus_write_byte_data(save_client, DS1374_REG_SR,
  94. tmp & 0xff);
  95. if (tmp < 0)
  96. dev_warn(&save_client->dev,
  97. "can't clear discontinuity notification\n");
  98. }
  99. }
  100. ulong ds1374_get_rtc_time(void)
  101. {
  102. ulong t1, t2;
  103. int limit = 10; /* arbitrary retry limit */
  104. mutex_lock(&ds1374_mutex);
  105. /*
  106. * Since the reads are being performed one byte at a time using
  107. * the SMBus vs a 4-byte i2c transfer, there is a chance that a
  108. * carry will occur during the read. To detect this, 2 reads are
  109. * performed and compared.
  110. */
  111. do {
  112. t1 = ds1374_read_rtc();
  113. t2 = ds1374_read_rtc();
  114. } while (t1 != t2 && limit--);
  115. mutex_unlock(&ds1374_mutex);
  116. if (t1 != t2) {
  117. dev_warn(&save_client->dev,
  118. "can't get consistent time from rtc chip\n");
  119. t1 = 0;
  120. }
  121. return t1;
  122. }
  123. static void ds1374_set_work(void *arg)
  124. {
  125. ulong t1, t2;
  126. int limit = 10; /* arbitrary retry limit */
  127. t1 = *(ulong *) arg;
  128. mutex_lock(&ds1374_mutex);
  129. /*
  130. * Since the writes are being performed one byte at a time using
  131. * the SMBus vs a 4-byte i2c transfer, there is a chance that a
  132. * carry will occur during the write. To detect this, the write
  133. * value is read back and compared.
  134. */
  135. do {
  136. ds1374_write_rtc(t1);
  137. t2 = ds1374_read_rtc();
  138. } while (t1 != t2 && limit--);
  139. mutex_unlock(&ds1374_mutex);
  140. if (t1 != t2)
  141. dev_warn(&save_client->dev,
  142. "can't confirm time set from rtc chip\n");
  143. }
  144. static ulong new_time;
  145. static struct workqueue_struct *ds1374_workqueue;
  146. static DECLARE_WORK(ds1374_work, ds1374_set_work, &new_time);
  147. int ds1374_set_rtc_time(ulong nowtime)
  148. {
  149. new_time = nowtime;
  150. if (in_interrupt())
  151. queue_work(ds1374_workqueue, &ds1374_work);
  152. else
  153. ds1374_set_work(&new_time);
  154. return 0;
  155. }
  156. /*
  157. *****************************************************************************
  158. *
  159. * Driver Interface
  160. *
  161. *****************************************************************************
  162. */
  163. static int ds1374_probe(struct i2c_adapter *adap, int addr, int kind)
  164. {
  165. struct i2c_client *client;
  166. int rc;
  167. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  168. if (!client)
  169. return -ENOMEM;
  170. strncpy(client->name, DS1374_DRV_NAME, I2C_NAME_SIZE);
  171. client->addr = addr;
  172. client->adapter = adap;
  173. client->driver = &ds1374_driver;
  174. ds1374_workqueue = create_singlethread_workqueue("ds1374");
  175. if ((rc = i2c_attach_client(client)) != 0) {
  176. kfree(client);
  177. return rc;
  178. }
  179. save_client = client;
  180. ds1374_check_rtc_status();
  181. return 0;
  182. }
  183. static int ds1374_attach(struct i2c_adapter *adap)
  184. {
  185. return i2c_probe(adap, &addr_data, ds1374_probe);
  186. }
  187. static int ds1374_detach(struct i2c_client *client)
  188. {
  189. int rc;
  190. if ((rc = i2c_detach_client(client)) == 0) {
  191. kfree(i2c_get_clientdata(client));
  192. destroy_workqueue(ds1374_workqueue);
  193. }
  194. return rc;
  195. }
  196. static struct i2c_driver ds1374_driver = {
  197. .driver = {
  198. .name = DS1374_DRV_NAME,
  199. },
  200. .id = I2C_DRIVERID_DS1374,
  201. .attach_adapter = ds1374_attach,
  202. .detach_client = ds1374_detach,
  203. };
  204. static int __init ds1374_init(void)
  205. {
  206. return i2c_add_driver(&ds1374_driver);
  207. }
  208. static void __exit ds1374_exit(void)
  209. {
  210. i2c_del_driver(&ds1374_driver);
  211. }
  212. module_init(ds1374_init);
  213. module_exit(ds1374_exit);
  214. MODULE_AUTHOR("Randy Vinson <rvinson@mvista.com>");
  215. MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC I2C Client Driver");
  216. MODULE_LICENSE("GPL");