m41t00.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * drivers/i2c/chips/m41t00.c
  3. *
  4. * I2C client/driver for the ST M41T00 Real-Time Clock chip.
  5. *
  6. * Author: Mark A. Greer <mgreer@mvista.com>
  7. *
  8. * 2005 (c) MontaVista Software, Inc. This file is licensed under
  9. * the terms of the GNU General Public License version 2. This program
  10. * is licensed "as is" without any warranty of any kind, whether express
  11. * or implied.
  12. */
  13. /*
  14. * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
  15. * interface and the SMBus interface of the i2c subsystem.
  16. * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
  17. * recommened in .../Documentation/i2c/writing-clients section
  18. * "Sending and receiving", using SMBus level communication is preferred.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/i2c.h>
  24. #include <linux/rtc.h>
  25. #include <linux/bcd.h>
  26. #include <linux/mutex.h>
  27. #include <linux/workqueue.h>
  28. #include <asm/time.h>
  29. #include <asm/rtc.h>
  30. #define M41T00_DRV_NAME "m41t00"
  31. static DEFINE_MUTEX(m41t00_mutex);
  32. static struct i2c_driver m41t00_driver;
  33. static struct i2c_client *save_client;
  34. static unsigned short ignore[] = { I2C_CLIENT_END };
  35. static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
  36. static struct i2c_client_address_data addr_data = {
  37. .normal_i2c = normal_addr,
  38. .probe = ignore,
  39. .ignore = ignore,
  40. };
  41. ulong
  42. m41t00_get_rtc_time(void)
  43. {
  44. s32 sec, min, hour, day, mon, year;
  45. s32 sec1, min1, hour1, day1, mon1, year1;
  46. ulong limit = 10;
  47. sec = min = hour = day = mon = year = 0;
  48. sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
  49. mutex_lock(&m41t00_mutex);
  50. do {
  51. if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
  52. && ((min = i2c_smbus_read_byte_data(save_client, 1))
  53. >= 0)
  54. && ((hour = i2c_smbus_read_byte_data(save_client, 2))
  55. >= 0)
  56. && ((day = i2c_smbus_read_byte_data(save_client, 4))
  57. >= 0)
  58. && ((mon = i2c_smbus_read_byte_data(save_client, 5))
  59. >= 0)
  60. && ((year = i2c_smbus_read_byte_data(save_client, 6))
  61. >= 0)
  62. && ((sec == sec1) && (min == min1) && (hour == hour1)
  63. && (day == day1) && (mon == mon1)
  64. && (year == year1)))
  65. break;
  66. sec1 = sec;
  67. min1 = min;
  68. hour1 = hour;
  69. day1 = day;
  70. mon1 = mon;
  71. year1 = year;
  72. } while (--limit > 0);
  73. mutex_unlock(&m41t00_mutex);
  74. if (limit == 0) {
  75. dev_warn(&save_client->dev,
  76. "m41t00: can't read rtc chip\n");
  77. sec = min = hour = day = mon = year = 0;
  78. }
  79. sec &= 0x7f;
  80. min &= 0x7f;
  81. hour &= 0x3f;
  82. day &= 0x3f;
  83. mon &= 0x1f;
  84. year &= 0xff;
  85. BCD_TO_BIN(sec);
  86. BCD_TO_BIN(min);
  87. BCD_TO_BIN(hour);
  88. BCD_TO_BIN(day);
  89. BCD_TO_BIN(mon);
  90. BCD_TO_BIN(year);
  91. year += 1900;
  92. if (year < 1970)
  93. year += 100;
  94. return mktime(year, mon, day, hour, min, sec);
  95. }
  96. static void
  97. m41t00_set(void *arg)
  98. {
  99. struct rtc_time tm;
  100. ulong nowtime = *(ulong *)arg;
  101. to_tm(nowtime, &tm);
  102. tm.tm_year = (tm.tm_year - 1900) % 100;
  103. BIN_TO_BCD(tm.tm_sec);
  104. BIN_TO_BCD(tm.tm_min);
  105. BIN_TO_BCD(tm.tm_hour);
  106. BIN_TO_BCD(tm.tm_mon);
  107. BIN_TO_BCD(tm.tm_mday);
  108. BIN_TO_BCD(tm.tm_year);
  109. mutex_lock(&m41t00_mutex);
  110. if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0)
  111. || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f)
  112. < 0)
  113. || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x3f)
  114. < 0)
  115. || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x3f)
  116. < 0)
  117. || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x1f)
  118. < 0)
  119. || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0xff)
  120. < 0))
  121. dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
  122. mutex_unlock(&m41t00_mutex);
  123. return;
  124. }
  125. static ulong new_time;
  126. static struct workqueue_struct *m41t00_wq;
  127. static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
  128. int
  129. m41t00_set_rtc_time(ulong nowtime)
  130. {
  131. new_time = nowtime;
  132. if (in_interrupt())
  133. queue_work(m41t00_wq, &m41t00_work);
  134. else
  135. m41t00_set(&new_time);
  136. return 0;
  137. }
  138. /*
  139. *****************************************************************************
  140. *
  141. * Driver Interface
  142. *
  143. *****************************************************************************
  144. */
  145. static int
  146. m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
  147. {
  148. struct i2c_client *client;
  149. int rc;
  150. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  151. if (!client)
  152. return -ENOMEM;
  153. strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
  154. client->addr = addr;
  155. client->adapter = adap;
  156. client->driver = &m41t00_driver;
  157. if ((rc = i2c_attach_client(client)) != 0) {
  158. kfree(client);
  159. return rc;
  160. }
  161. m41t00_wq = create_singlethread_workqueue("m41t00");
  162. save_client = client;
  163. return 0;
  164. }
  165. static int
  166. m41t00_attach(struct i2c_adapter *adap)
  167. {
  168. return i2c_probe(adap, &addr_data, m41t00_probe);
  169. }
  170. static int
  171. m41t00_detach(struct i2c_client *client)
  172. {
  173. int rc;
  174. if ((rc = i2c_detach_client(client)) == 0) {
  175. kfree(client);
  176. destroy_workqueue(m41t00_wq);
  177. }
  178. return rc;
  179. }
  180. static struct i2c_driver m41t00_driver = {
  181. .driver = {
  182. .name = M41T00_DRV_NAME,
  183. },
  184. .id = I2C_DRIVERID_STM41T00,
  185. .attach_adapter = m41t00_attach,
  186. .detach_client = m41t00_detach,
  187. };
  188. static int __init
  189. m41t00_init(void)
  190. {
  191. return i2c_add_driver(&m41t00_driver);
  192. }
  193. static void __exit
  194. m41t00_exit(void)
  195. {
  196. i2c_del_driver(&m41t00_driver);
  197. return;
  198. }
  199. module_init(m41t00_init);
  200. module_exit(m41t00_exit);
  201. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  202. MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
  203. MODULE_LICENSE("GPL");