m41t00.c 9.1 KB

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