/drivers/i2c/chips/m41t00.c

https://bitbucket.org/evzijst/gittest · C · 246 lines · 179 code · 41 blank · 26 comment · 34 complexity · 287e3e269cd50b2d37254d6d8b67b84b MD5 · raw file

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