m41t00.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /* well, isn't this API just _lovely_? */
  196. static void
  197. m41t00_barf(struct work_struct *unusable)
  198. {
  199. m41t00_set(&new_time);
  200. }
  201. static struct workqueue_struct *m41t00_wq;
  202. static DECLARE_WORK(m41t00_work, m41t00_barf);
  203. int
  204. m41t00_set_rtc_time(ulong nowtime)
  205. {
  206. new_time = nowtime;
  207. if (in_interrupt())
  208. queue_work(m41t00_wq, &m41t00_work);
  209. else
  210. m41t00_set(&new_time);
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(m41t00_set_rtc_time);
  214. /*
  215. *****************************************************************************
  216. *
  217. * platform_data Driver Interface
  218. *
  219. *****************************************************************************
  220. */
  221. static int __init
  222. m41t00_platform_probe(struct platform_device *pdev)
  223. {
  224. struct m41t00_platform_data *pdata;
  225. int i;
  226. if (pdev && (pdata = pdev->dev.platform_data)) {
  227. normal_addr[0] = pdata->i2c_addr;
  228. for (i=0; i<ARRAY_SIZE(m41t00_chip_info_tbl); i++)
  229. if (m41t00_chip_info_tbl[i].type == pdata->type) {
  230. m41t00_chip = &m41t00_chip_info_tbl[i];
  231. m41t00_chip->sqw_freq = pdata->sqw_freq;
  232. return 0;
  233. }
  234. }
  235. return -ENODEV;
  236. }
  237. static int __exit
  238. m41t00_platform_remove(struct platform_device *pdev)
  239. {
  240. return 0;
  241. }
  242. static struct platform_driver m41t00_platform_driver = {
  243. .probe = m41t00_platform_probe,
  244. .remove = m41t00_platform_remove,
  245. .driver = {
  246. .owner = THIS_MODULE,
  247. .name = M41T00_DRV_NAME,
  248. },
  249. };
  250. /*
  251. *****************************************************************************
  252. *
  253. * Driver Interface
  254. *
  255. *****************************************************************************
  256. */
  257. static int
  258. m41t00_probe(struct i2c_adapter *adap, int addr, int kind)
  259. {
  260. struct i2c_client *client;
  261. int rc;
  262. if (!i2c_check_functionality(adap, I2C_FUNC_I2C
  263. | I2C_FUNC_SMBUS_BYTE_DATA))
  264. return 0;
  265. client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  266. if (!client)
  267. return -ENOMEM;
  268. strlcpy(client->name, m41t00_chip->name, I2C_NAME_SIZE);
  269. client->addr = addr;
  270. client->adapter = adap;
  271. client->driver = &m41t00_driver;
  272. if ((rc = i2c_attach_client(client)))
  273. goto attach_err;
  274. if (m41t00_chip->type != M41T00_TYPE_M41T00) {
  275. /* If asked, disable SQW, set SQW frequency & re-enable */
  276. if (m41t00_chip->sqw_freq)
  277. if (((rc = i2c_smbus_read_byte_data(client,
  278. m41t00_chip->alarm_mon)) < 0)
  279. || ((rc = i2c_smbus_write_byte_data(client,
  280. m41t00_chip->alarm_mon, rc & ~0x40)) <0)
  281. || ((rc = i2c_smbus_write_byte_data(client,
  282. m41t00_chip->sqw,
  283. m41t00_chip->sqw_freq)) < 0)
  284. || ((rc = i2c_smbus_write_byte_data(client,
  285. m41t00_chip->alarm_mon, rc | 0x40)) <0))
  286. goto sqw_err;
  287. /* Make sure HT (Halt Update) bit is cleared */
  288. if ((rc = i2c_smbus_read_byte_data(client,
  289. m41t00_chip->alarm_hour)) < 0)
  290. goto ht_err;
  291. if (rc & 0x40)
  292. if ((rc = i2c_smbus_write_byte_data(client,
  293. m41t00_chip->alarm_hour, rc & ~0x40))<0)
  294. goto ht_err;
  295. }
  296. /* Make sure ST (stop) bit is cleared */
  297. if ((rc = i2c_smbus_read_byte_data(client, m41t00_chip->sec)) < 0)
  298. goto st_err;
  299. if (rc & 0x80)
  300. if ((rc = i2c_smbus_write_byte_data(client, m41t00_chip->sec,
  301. rc & ~0x80)) < 0)
  302. goto st_err;
  303. m41t00_wq = create_singlethread_workqueue(m41t00_chip->name);
  304. save_client = client;
  305. return 0;
  306. st_err:
  307. dev_err(&client->dev, "m41t00_probe: Can't clear ST bit\n");
  308. goto attach_err;
  309. ht_err:
  310. dev_err(&client->dev, "m41t00_probe: Can't clear HT bit\n");
  311. goto attach_err;
  312. sqw_err:
  313. dev_err(&client->dev, "m41t00_probe: Can't set SQW Frequency\n");
  314. attach_err:
  315. kfree(client);
  316. return rc;
  317. }
  318. static int
  319. m41t00_attach(struct i2c_adapter *adap)
  320. {
  321. return i2c_probe(adap, &addr_data, m41t00_probe);
  322. }
  323. static int
  324. m41t00_detach(struct i2c_client *client)
  325. {
  326. int rc;
  327. if ((rc = i2c_detach_client(client)) == 0) {
  328. kfree(client);
  329. destroy_workqueue(m41t00_wq);
  330. }
  331. return rc;
  332. }
  333. static struct i2c_driver m41t00_driver = {
  334. .driver = {
  335. .name = M41T00_DRV_NAME,
  336. },
  337. .id = I2C_DRIVERID_STM41T00,
  338. .attach_adapter = m41t00_attach,
  339. .detach_client = m41t00_detach,
  340. };
  341. static int __init
  342. m41t00_init(void)
  343. {
  344. int rc;
  345. if (!(rc = platform_driver_register(&m41t00_platform_driver)))
  346. rc = i2c_add_driver(&m41t00_driver);
  347. return rc;
  348. }
  349. static void __exit
  350. m41t00_exit(void)
  351. {
  352. i2c_del_driver(&m41t00_driver);
  353. platform_driver_unregister(&m41t00_platform_driver);
  354. }
  355. module_init(m41t00_init);
  356. module_exit(m41t00_exit);
  357. MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
  358. MODULE_DESCRIPTION("ST Microelectronics M41T00 RTC I2C Client Driver");
  359. MODULE_LICENSE("GPL");