m41t00.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * I2C client/driver for the ST M41T00 family of i2c rtc chips.
  3. *
  4. * Author: Mark A. Greer <mgreer@mvista.com>
  5. *
  6. * 2005, 2006 (c) MontaVista Software, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. */
  11. /*
  12. * This i2c client/driver wedges between the drivers/char/genrtc.c RTC
  13. * interface and the SMBus interface of the i2c subsystem.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/i2c.h>
  19. #include <linux/rtc.h>
  20. #include <linux/bcd.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/m41t00.h>
  24. #include <asm/time.h>
  25. #include <asm/rtc.h>
  26. static struct i2c_driver m41t00_driver;
  27. static struct i2c_client *save_client;
  28. static unsigned short ignore[] = { I2C_CLIENT_END };
  29. static unsigned short normal_addr[] = { I2C_CLIENT_END, I2C_CLIENT_END };
  30. static struct i2c_client_address_data addr_data = {
  31. .normal_i2c = normal_addr,
  32. .probe = ignore,
  33. .ignore = ignore,
  34. };
  35. struct m41t00_chip_info {
  36. u8 type;
  37. char *name;
  38. u8 read_limit;
  39. u8 sec; /* Offsets for chip regs */
  40. u8 min;
  41. u8 hour;
  42. u8 day;
  43. u8 mon;
  44. u8 year;
  45. u8 alarm_mon;
  46. u8 alarm_hour;
  47. u8 sqw;
  48. u8 sqw_freq;
  49. };
  50. static struct m41t00_chip_info m41t00_chip_info_tbl[] = {
  51. {
  52. .type = M41T00_TYPE_M41T00,
  53. .name = "m41t00",
  54. .read_limit = 5,
  55. .sec = 0,
  56. .min = 1,
  57. .hour = 2,
  58. .day = 4,
  59. .mon = 5,
  60. .year = 6,
  61. },
  62. {
  63. .type = M41T00_TYPE_M41T81,
  64. .name = "m41t81",
  65. .read_limit = 1,
  66. .sec = 1,
  67. .min = 2,
  68. .hour = 3,
  69. .day = 5,
  70. .mon = 6,
  71. .year = 7,
  72. .alarm_mon = 0xa,
  73. .alarm_hour = 0xc,
  74. .sqw = 0x13,
  75. },
  76. {
  77. .type = M41T00_TYPE_M41T85,
  78. .name = "m41t85",
  79. .read_limit = 1,
  80. .sec = 1,
  81. .min = 2,
  82. .hour = 3,
  83. .day = 5,
  84. .mon = 6,
  85. .year = 7,
  86. .alarm_mon = 0xa,
  87. .alarm_hour = 0xc,
  88. .sqw = 0x13,
  89. },
  90. };
  91. static struct m41t00_chip_info *m41t00_chip;
  92. ulong
  93. m41t00_get_rtc_time(void)
  94. {
  95. s32 sec, min, hour, day, mon, year;
  96. s32 sec1, min1, hour1, day1, mon1, year1;
  97. u8 reads = 0;
  98. u8 buf[8], msgbuf[1] = { 0 }; /* offset into rtc's regs */
  99. struct i2c_msg msgs[] = {
  100. {
  101. .addr = save_client->addr,
  102. .flags = 0,
  103. .len = 1,
  104. .buf = msgbuf,
  105. },
  106. {
  107. .addr = save_client->addr,
  108. .flags = I2C_M_RD,
  109. .len = 8,
  110. .buf = buf,
  111. },
  112. };
  113. sec = min = hour = day = mon = year = 0;
  114. do {
  115. if (i2c_transfer(save_client->adapter, msgs, 2) < 0)
  116. goto read_err;
  117. sec1 = sec;
  118. min1 = min;
  119. hour1 = hour;
  120. day1 = day;
  121. mon1 = mon;
  122. year1 = year;
  123. sec = buf[m41t00_chip->sec] & 0x7f;
  124. min = buf[m41t00_chip->min] & 0x7f;
  125. hour = buf[m41t00_chip->hour] & 0x3f;
  126. day = buf[m41t00_chip->day] & 0x3f;
  127. mon = buf[m41t00_chip->mon] & 0x1f;
  128. year = buf[m41t00_chip->year];
  129. } while ((++reads < m41t00_chip->read_limit) && ((sec != sec1)
  130. || (min != min1) || (hour != hour1) || (day != day1)
  131. || (mon != mon1) || (year != year1)));
  132. if ((m41t00_chip->read_limit > 1) && ((sec != sec1) || (min != min1)
  133. || (hour != hour1) || (day != day1) || (mon != mon1)
  134. || (year != year1)))
  135. goto read_err;
  136. sec = BCD2BIN(sec);
  137. min = BCD2BIN(min);
  138. hour = BCD2BIN(hour);
  139. day = BCD2BIN(day);
  140. mon = BCD2BIN(mon);
  141. year = BCD2BIN(year);
  142. year += 1900;
  143. if (year < 1970)
  144. year += 100;
  145. return mktime(year, mon, day, hour, min, sec);
  146. read_err:
  147. dev_err(&save_client->dev, "m41t00_get_rtc_time: Read error\n");
  148. return 0;
  149. }
  150. EXPORT_SYMBOL_GPL(m41t00_get_rtc_time);
  151. static void
  152. m41t00_set(void *arg)
  153. {
  154. struct rtc_time tm;
  155. int nowtime = *(int *)arg;
  156. s32 sec, min, hour, day, mon, year;
  157. u8 wbuf[9], *buf = &wbuf[1], msgbuf[1] = { 0 };
  158. struct i2c_msg msgs[] = {
  159. {
  160. .addr = save_client->addr,
  161. .flags = 0,
  162. .len = 1,
  163. .buf = msgbuf,
  164. },
  165. {
  166. .addr = save_client->addr,
  167. .flags = I2C_M_RD,
  168. .len = 8,
  169. .buf = buf,
  170. },
  171. };
  172. to_tm(nowtime, &tm);
  173. tm.tm_year = (tm.tm_year - 1900) % 100;
  174. sec = BIN2BCD(tm.tm_sec);
  175. min = BIN2BCD(tm.tm_min);
  176. hour = BIN2BCD(tm.tm_hour);
  177. day = BIN2BCD(tm.tm_mday);
  178. mon = BIN2BCD(tm.tm_mon);
  179. year = BIN2BCD(tm.tm_year);
  180. /* Read reg values into buf[0..7]/wbuf[1..8] */
  181. if (i2c_transfer(save_client->adapter, msgs, 2) < 0) {
  182. dev_err(&save_client->dev, "m41t00_set: Read error\n");
  183. return;
  184. }
  185. wbuf[0] = 0; /* offset into rtc's regs */
  186. buf[m41t00_chip->sec] = (buf[m41t00_chip->sec] & ~0x7f) | (sec & 0x7f);
  187. buf[m41t00_chip->min] = (buf[m41t00_chip->min] & ~0x7f) | (min & 0x7f);
  188. buf[m41t00_chip->hour] = (buf[m41t00_chip->hour] & ~0x3f) | (hour& 0x3f);
  189. buf[m41t00_chip->day] = (buf[m41t00_chip->day] & ~0x3f) | (day & 0x3f);
  190. buf[m41t00_chip->mon] = (buf[m41t00_chip->mon] & ~0x1f) | (mon & 0x1f);
  191. if (i2c_master_send(save_client, wbuf, 9) < 0)
  192. dev_err(&save_client->dev, "m41t00_set: Write error\n");
  193. }
  194. static ulong new_time;
  195. static struct workqueue_struct *m41t00_wq;
  196. static DECLARE_WORK(m41t00_work, m41t00_set, &new_time);
  197. int
  198. m41t00_set_rtc_time(ulong nowtime)
  199. {
  200. new_time = nowtime;
  201. if (in_interrupt())
  202. queue_work(m41t00_wq, &m41t00_work);
  203. else
  204. m41t00_set(&new_time);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(m41t00_set_rtc_time);
  208. /*
  209. *****************************************************************************
  210. *
  211. * platform_data Driver Interface
  212. *
  213. *****************************************************************************
  214. */
  215. static int __init
  216. m41t00_platform_probe(struct platform_device *pdev)
  217. {
  218. struct m41t00_platform_data *pdata;
  219. int i;
  220. if (pdev && (pdata = pdev->dev.platform_data)) {
  221. normal_addr[0] = pdata->i2c_addr;
  222. for (i=0; i<ARRAY_SIZE(m41t00_chip_info_tbl); i++)
  223. if (m41t00_chip_info_tbl[i].type == pdata->type) {
  224. m41t00_chip = &m41t00_chip_info_tbl[i];
  225. m41t00_chip->sqw_freq = pdata->sqw_freq;
  226. return 0;
  227. }
  228. }
  229. return -ENODEV;
  230. }
  231. static int __exit
  232. m41t00_platform_remove(struct platform_device *pdev)
  233. {
  234. return 0;
  235. }
  236. static struct platform_driver m41t00_platform_driver = {
  237. .probe = m41t00_platform_probe,
  238. .remove = m41t00_platform_remove,
  239. .driver = {
  240. .owner = THIS_MODULE,
  241. .name = M41T00_DRV_NAME,
  242. },
  243. };
  244. /*
  245. *****************************************************************************
  246. *
  247. * Driver Interface
  248. *
  249. *****************************************************************************
  250. */
  251. static int
  252. m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
  253. {
  254. struct i2c_client *client;
  255. int rc;
  256. if (!i2c_check_functionality(adap, I2C_FUNC_I2C
  257. | I2C_FUNC_SMBUS_BYTE_DATA))
  258. return 0;
  259. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  260. if (!client)
  261. return -ENOMEM;
  262. strlcpy(client->name, m41t00_chip->name, I2C_NAME_SIZE);
  263. client->addr = addr;
  264. client->adapter = adap;
  265. client->driver = &m41t00_driver;
  266. if ((rc = i2c_attach_client(client)))
  267. goto attach_err;
  268. if (m41t00_chip->type != M41T00_TYPE_M41T00) {
  269. /* If asked, disable SQW, set SQW frequency & re-enable */
  270. if (m41t00_chip->sqw_freq)
  271. if (((rc = i2c_smbus_read_byte_data(client,
  272. m41t00_chip->alarm_mon)) < 0)
  273. || ((rc = i2c_smbus_write_byte_data(client,
  274. m41t00_chip->alarm_mon, rc & ~0x40)) <0)
  275. || ((rc = i2c_smbus_write_byte_data(client,
  276. m41t00_chip->sqw,
  277. m41t00_chip->sqw_freq)) < 0)
  278. || ((rc = i2c_smbus_write_byte_data(client,
  279. m41t00_chip->alarm_mon, rc | 0x40)) <0))
  280. goto sqw_err;
  281. /* Make sure HT (Halt Update) bit is cleared */
  282. if ((rc = i2c_smbus_read_byte_data(client,
  283. m41t00_chip->alarm_hour)) < 0)
  284. goto ht_err;
  285. if (rc & 0x40)
  286. if ((rc = i2c_smbus_write_byte_data(client,
  287. m41t00_chip->alarm_hour, rc & ~0x40))<0)
  288. goto ht_err;
  289. }
  290. /* Make sure ST (stop) bit is cleared */
  291. if ((rc = i2c_smbus_read_byte_data(client, m41t00_chip->sec)) < 0)
  292. goto st_err;
  293. if (rc & 0x80)
  294. if ((rc = i2c_smbus_write_byte_data(client, m41t00_chip->sec,
  295. rc & ~0x80)) < 0)
  296. goto st_err;
  297. m41t00_wq = create_singlethread_workqueue(m41t00_chip->name);
  298. save_client = client;
  299. return 0;
  300. st_err:
  301. dev_err(&client->dev, "m41t00_probe: Can't clear ST bit\n");
  302. goto attach_err;
  303. ht_err:
  304. dev_err(&client->dev, "m41t00_probe: Can't clear HT bit\n");
  305. goto attach_err;
  306. sqw_err:
  307. dev_err(&client->dev, "m41t00_probe: Can't set SQW Frequency\n");
  308. attach_err:
  309. kfree(client);
  310. return rc;
  311. }
  312. static int
  313. m41t00_attach(struct i2c_adapter *adap)
  314. {
  315. return i2c_probe(adap, &addr_data, m41t00_probe);
  316. }
  317. static int
  318. m41t00_detach(struct i2c_client *client)
  319. {
  320. int rc;
  321. if ((rc = i2c_detach_client(client)) == 0) {
  322. kfree(client);
  323. destroy_workqueue(m41t00_wq);
  324. }
  325. return rc;
  326. }
  327. static struct i2c_driver m41t00_driver = {
  328. .driver = {
  329. .name = M41T00_DRV_NAME,
  330. },
  331. .id = I2C_DRIVERID_STM41T00,
  332. .attach_adapter = m41t00_attach,
  333. .detach_client = m41t00_detach,
  334. };
  335. static int __init
  336. m41t00_init(void)
  337. {
  338. int rc;
  339. if (!(rc = platform_driver_register(&m41t00_platform_driver)))
  340. rc = i2c_add_driver(&m41t00_driver);
  341. return rc;
  342. }
  343. static void __exit
  344. m41t00_exit(void)
  345. {
  346. i2c_del_driver(&m41t00_driver);
  347. platform_driver_unregister(&m41t00_platform_driver);
  348. }
  349. module_init(m41t00_init);
  350. module_exit(m41t00_exit);
  351. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  352. MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
  353. MODULE_LICENSE("GPL");