m41t00.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <asm/time.h>
  27. #include <asm/rtc.h>
  28. #define M41T00_DRV_NAME "m41t00"
  29. static DECLARE_MUTEX(m41t00_mutex);
  30. static struct i2c_driver m41t00_driver;
  31. static struct i2c_client *save_client;
  32. static unsigned short ignore[] = { I2C_CLIENT_END };
  33. static unsigned short normal_addr[] = { 0x68, I2C_CLIENT_END };
  34. static struct i2c_client_address_data addr_data = {
  35. .normal_i2c = normal_addr,
  36. .probe = ignore,
  37. .ignore = ignore,
  38. };
  39. ulong
  40. m41t00_get_rtc_time(void)
  41. {
  42. s32 sec, min, hour, day, mon, year;
  43. s32 sec1, min1, hour1, day1, mon1, year1;
  44. ulong limit = 10;
  45. sec = min = hour = day = mon = year = 0;
  46. sec1 = min1 = hour1 = day1 = mon1 = year1 = 0;
  47. down(&m41t00_mutex);
  48. do {
  49. if (((sec = i2c_smbus_read_byte_data(save_client, 0)) >= 0)
  50. && ((min = i2c_smbus_read_byte_data(save_client, 1))
  51. >= 0)
  52. && ((hour = i2c_smbus_read_byte_data(save_client, 2))
  53. >= 0)
  54. && ((day = i2c_smbus_read_byte_data(save_client, 4))
  55. >= 0)
  56. && ((mon = i2c_smbus_read_byte_data(save_client, 5))
  57. >= 0)
  58. && ((year = i2c_smbus_read_byte_data(save_client, 6))
  59. >= 0)
  60. && ((sec == sec1) && (min == min1) && (hour == hour1)
  61. && (day == day1) && (mon == mon1)
  62. && (year == year1)))
  63. break;
  64. sec1 = sec;
  65. min1 = min;
  66. hour1 = hour;
  67. day1 = day;
  68. mon1 = mon;
  69. year1 = year;
  70. } while (--limit > 0);
  71. up(&m41t00_mutex);
  72. if (limit == 0) {
  73. dev_warn(&save_client->dev,
  74. "m41t00: can't read rtc chip\n");
  75. sec = min = hour = day = mon = year = 0;
  76. }
  77. sec &= 0x7f;
  78. min &= 0x7f;
  79. hour &= 0x3f;
  80. day &= 0x3f;
  81. mon &= 0x1f;
  82. year &= 0xff;
  83. BCD_TO_BIN(sec);
  84. BCD_TO_BIN(min);
  85. BCD_TO_BIN(hour);
  86. BCD_TO_BIN(day);
  87. BCD_TO_BIN(mon);
  88. BCD_TO_BIN(year);
  89. year += 1900;
  90. if (year < 1970)
  91. year += 100;
  92. return mktime(year, mon, day, hour, min, sec);
  93. }
  94. static void
  95. m41t00_set_tlet(ulong arg)
  96. {
  97. struct rtc_time tm;
  98. ulong nowtime = *(ulong *)arg;
  99. to_tm(nowtime, &tm);
  100. tm.tm_year = (tm.tm_year - 1900) % 100;
  101. BIN_TO_BCD(tm.tm_sec);
  102. BIN_TO_BCD(tm.tm_min);
  103. BIN_TO_BCD(tm.tm_hour);
  104. BIN_TO_BCD(tm.tm_mon);
  105. BIN_TO_BCD(tm.tm_mday);
  106. BIN_TO_BCD(tm.tm_year);
  107. down(&m41t00_mutex);
  108. if ((i2c_smbus_write_byte_data(save_client, 0, tm.tm_sec & 0x7f) < 0)
  109. || (i2c_smbus_write_byte_data(save_client, 1, tm.tm_min & 0x7f)
  110. < 0)
  111. || (i2c_smbus_write_byte_data(save_client, 2, tm.tm_hour & 0x7f)
  112. < 0)
  113. || (i2c_smbus_write_byte_data(save_client, 4, tm.tm_mday & 0x7f)
  114. < 0)
  115. || (i2c_smbus_write_byte_data(save_client, 5, tm.tm_mon & 0x7f)
  116. < 0)
  117. || (i2c_smbus_write_byte_data(save_client, 6, tm.tm_year & 0x7f)
  118. < 0))
  119. dev_warn(&save_client->dev,"m41t00: can't write to rtc chip\n");
  120. up(&m41t00_mutex);
  121. return;
  122. }
  123. static ulong new_time;
  124. DECLARE_TASKLET_DISABLED(m41t00_tasklet, m41t00_set_tlet, (ulong)&new_time);
  125. int
  126. m41t00_set_rtc_time(ulong nowtime)
  127. {
  128. new_time = nowtime;
  129. if (in_interrupt())
  130. tasklet_schedule(&m41t00_tasklet);
  131. else
  132. m41t00_set_tlet((ulong)&new_time);
  133. return 0;
  134. }
  135. /*
  136. *****************************************************************************
  137. *
  138. * Driver Interface
  139. *
  140. *****************************************************************************
  141. */
  142. static int
  143. m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
  144. {
  145. struct i2c_client *client;
  146. int rc;
  147. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  148. if (!client)
  149. return -ENOMEM;
  150. strncpy(client->name, M41T00_DRV_NAME, I2C_NAME_SIZE);
  151. client->addr = addr;
  152. client->adapter = adap;
  153. client->driver = &m41t00_driver;
  154. if ((rc = i2c_attach_client(client)) != 0) {
  155. kfree(client);
  156. return rc;
  157. }
  158. save_client = client;
  159. return 0;
  160. }
  161. static int
  162. m41t00_attach(struct i2c_adapter *adap)
  163. {
  164. return i2c_probe(adap, &addr_data, m41t00_probe);
  165. }
  166. static int
  167. m41t00_detach(struct i2c_client *client)
  168. {
  169. int rc;
  170. if ((rc = i2c_detach_client(client)) == 0) {
  171. kfree(client);
  172. tasklet_kill(&m41t00_tasklet);
  173. }
  174. return rc;
  175. }
  176. static struct i2c_driver m41t00_driver = {
  177. .driver = {
  178. .name = M41T00_DRV_NAME,
  179. },
  180. .id = I2C_DRIVERID_STM41T00,
  181. .attach_adapter = m41t00_attach,
  182. .detach_client = m41t00_detach,
  183. };
  184. static int __init
  185. m41t00_init(void)
  186. {
  187. return i2c_add_driver(&m41t00_driver);
  188. }
  189. static void __exit
  190. m41t00_exit(void)
  191. {
  192. i2c_del_driver(&m41t00_driver);
  193. return;
  194. }
  195. module_init(m41t00_init);
  196. module_exit(m41t00_exit);
  197. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  198. MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
  199. MODULE_LICENSE("GPL");