rtc-m41t80.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * I2C client/driver for the ST M41T80 family of i2c rtc chips.
  3. *
  4. * Author: Alexander Bigga <ab@mycable.de>
  5. *
  6. * Based on m41t00.c by Mark A. Greer <mgreer@mvista.com>
  7. *
  8. * 2006 (c) mycable GmbH
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/i2c.h>
  20. #include <linux/rtc.h>
  21. #include <linux/bcd.h>
  22. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  23. #include <linux/miscdevice.h>
  24. #include <linux/watchdog.h>
  25. #include <linux/reboot.h>
  26. #include <linux/fs.h>
  27. #include <linux/ioctl.h>
  28. #endif
  29. #define M41T80_REG_SSEC 0
  30. #define M41T80_REG_SEC 1
  31. #define M41T80_REG_MIN 2
  32. #define M41T80_REG_HOUR 3
  33. #define M41T80_REG_WDAY 4
  34. #define M41T80_REG_DAY 5
  35. #define M41T80_REG_MON 6
  36. #define M41T80_REG_YEAR 7
  37. #define M41T80_REG_ALARM_MON 0xa
  38. #define M41T80_REG_ALARM_DAY 0xb
  39. #define M41T80_REG_ALARM_HOUR 0xc
  40. #define M41T80_REG_ALARM_MIN 0xd
  41. #define M41T80_REG_ALARM_SEC 0xe
  42. #define M41T80_REG_FLAGS 0xf
  43. #define M41T80_REG_SQW 0x13
  44. #define M41T80_DATETIME_REG_SIZE (M41T80_REG_YEAR + 1)
  45. #define M41T80_ALARM_REG_SIZE \
  46. (M41T80_REG_ALARM_SEC + 1 - M41T80_REG_ALARM_MON)
  47. #define M41T80_SEC_ST (1 << 7) /* ST: Stop Bit */
  48. #define M41T80_ALMON_AFE (1 << 7) /* AFE: AF Enable Bit */
  49. #define M41T80_ALMON_SQWE (1 << 6) /* SQWE: SQW Enable Bit */
  50. #define M41T80_ALHOUR_HT (1 << 6) /* HT: Halt Update Bit */
  51. #define M41T80_FLAGS_AF (1 << 6) /* AF: Alarm Flag Bit */
  52. #define M41T80_FLAGS_BATT_LOW (1 << 4) /* BL: Battery Low Bit */
  53. #define M41T80_FEATURE_HT (1 << 0)
  54. #define M41T80_FEATURE_BL (1 << 1)
  55. #define DRV_VERSION "0.05"
  56. struct m41t80_chip_info {
  57. const char *name;
  58. u8 features;
  59. };
  60. static const struct m41t80_chip_info m41t80_chip_info_tbl[] = {
  61. {
  62. .name = "m41t80",
  63. .features = 0,
  64. },
  65. {
  66. .name = "m41t81",
  67. .features = M41T80_FEATURE_HT,
  68. },
  69. {
  70. .name = "m41t81s",
  71. .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
  72. },
  73. {
  74. .name = "m41t82",
  75. .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
  76. },
  77. {
  78. .name = "m41t83",
  79. .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
  80. },
  81. {
  82. .name = "m41st84",
  83. .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
  84. },
  85. {
  86. .name = "m41st85",
  87. .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
  88. },
  89. {
  90. .name = "m41st87",
  91. .features = M41T80_FEATURE_HT | M41T80_FEATURE_BL,
  92. },
  93. };
  94. struct m41t80_data {
  95. const struct m41t80_chip_info *chip;
  96. struct rtc_device *rtc;
  97. };
  98. static int m41t80_get_datetime(struct i2c_client *client,
  99. struct rtc_time *tm)
  100. {
  101. u8 buf[M41T80_DATETIME_REG_SIZE], dt_addr[1] = { M41T80_REG_SEC };
  102. struct i2c_msg msgs[] = {
  103. {
  104. .addr = client->addr,
  105. .flags = 0,
  106. .len = 1,
  107. .buf = dt_addr,
  108. },
  109. {
  110. .addr = client->addr,
  111. .flags = I2C_M_RD,
  112. .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
  113. .buf = buf + M41T80_REG_SEC,
  114. },
  115. };
  116. if (i2c_transfer(client->adapter, msgs, 2) < 0) {
  117. dev_err(&client->dev, "read error\n");
  118. return -EIO;
  119. }
  120. tm->tm_sec = BCD2BIN(buf[M41T80_REG_SEC] & 0x7f);
  121. tm->tm_min = BCD2BIN(buf[M41T80_REG_MIN] & 0x7f);
  122. tm->tm_hour = BCD2BIN(buf[M41T80_REG_HOUR] & 0x3f);
  123. tm->tm_mday = BCD2BIN(buf[M41T80_REG_DAY] & 0x3f);
  124. tm->tm_wday = buf[M41T80_REG_WDAY] & 0x07;
  125. tm->tm_mon = BCD2BIN(buf[M41T80_REG_MON] & 0x1f) - 1;
  126. /* assume 20YY not 19YY, and ignore the Century Bit */
  127. tm->tm_year = BCD2BIN(buf[M41T80_REG_YEAR]) + 100;
  128. return 0;
  129. }
  130. /* Sets the given date and time to the real time clock. */
  131. static int m41t80_set_datetime(struct i2c_client *client, struct rtc_time *tm)
  132. {
  133. u8 wbuf[1 + M41T80_DATETIME_REG_SIZE];
  134. u8 *buf = &wbuf[1];
  135. u8 dt_addr[1] = { M41T80_REG_SEC };
  136. struct i2c_msg msgs_in[] = {
  137. {
  138. .addr = client->addr,
  139. .flags = 0,
  140. .len = 1,
  141. .buf = dt_addr,
  142. },
  143. {
  144. .addr = client->addr,
  145. .flags = I2C_M_RD,
  146. .len = M41T80_DATETIME_REG_SIZE - M41T80_REG_SEC,
  147. .buf = buf + M41T80_REG_SEC,
  148. },
  149. };
  150. struct i2c_msg msgs[] = {
  151. {
  152. .addr = client->addr,
  153. .flags = 0,
  154. .len = 1 + M41T80_DATETIME_REG_SIZE,
  155. .buf = wbuf,
  156. },
  157. };
  158. /* Read current reg values into buf[1..7] */
  159. if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
  160. dev_err(&client->dev, "read error\n");
  161. return -EIO;
  162. }
  163. wbuf[0] = 0; /* offset into rtc's regs */
  164. /* Merge time-data and register flags into buf[0..7] */
  165. buf[M41T80_REG_SSEC] = 0;
  166. buf[M41T80_REG_SEC] =
  167. BIN2BCD(tm->tm_sec) | (buf[M41T80_REG_SEC] & ~0x7f);
  168. buf[M41T80_REG_MIN] =
  169. BIN2BCD(tm->tm_min) | (buf[M41T80_REG_MIN] & ~0x7f);
  170. buf[M41T80_REG_HOUR] =
  171. BIN2BCD(tm->tm_hour) | (buf[M41T80_REG_HOUR] & ~0x3f) ;
  172. buf[M41T80_REG_WDAY] =
  173. (tm->tm_wday & 0x07) | (buf[M41T80_REG_WDAY] & ~0x07);
  174. buf[M41T80_REG_DAY] =
  175. BIN2BCD(tm->tm_mday) | (buf[M41T80_REG_DAY] & ~0x3f);
  176. buf[M41T80_REG_MON] =
  177. BIN2BCD(tm->tm_mon + 1) | (buf[M41T80_REG_MON] & ~0x1f);
  178. /* assume 20YY not 19YY */
  179. buf[M41T80_REG_YEAR] = BIN2BCD(tm->tm_year % 100);
  180. if (i2c_transfer(client->adapter, msgs, 1) != 1) {
  181. dev_err(&client->dev, "write error\n");
  182. return -EIO;
  183. }
  184. return 0;
  185. }
  186. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  187. static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
  188. {
  189. struct i2c_client *client = to_i2c_client(dev);
  190. struct m41t80_data *clientdata = i2c_get_clientdata(client);
  191. u8 reg;
  192. if (clientdata->chip->features & M41T80_FEATURE_BL) {
  193. reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
  194. seq_printf(seq, "battery\t\t: %s\n",
  195. (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
  196. }
  197. return 0;
  198. }
  199. #else
  200. #define m41t80_rtc_proc NULL
  201. #endif
  202. static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
  203. {
  204. return m41t80_get_datetime(to_i2c_client(dev), tm);
  205. }
  206. static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
  207. {
  208. return m41t80_set_datetime(to_i2c_client(dev), tm);
  209. }
  210. #if defined(CONFIG_RTC_INTF_DEV) || defined(CONFIG_RTC_INTF_DEV_MODULE)
  211. static int
  212. m41t80_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
  213. {
  214. struct i2c_client *client = to_i2c_client(dev);
  215. int rc;
  216. switch (cmd) {
  217. case RTC_AIE_OFF:
  218. case RTC_AIE_ON:
  219. break;
  220. default:
  221. return -ENOIOCTLCMD;
  222. }
  223. rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
  224. if (rc < 0)
  225. goto err;
  226. switch (cmd) {
  227. case RTC_AIE_OFF:
  228. rc &= ~M41T80_ALMON_AFE;
  229. break;
  230. case RTC_AIE_ON:
  231. rc |= M41T80_ALMON_AFE;
  232. break;
  233. }
  234. if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON, rc) < 0)
  235. goto err;
  236. return 0;
  237. err:
  238. return -EIO;
  239. }
  240. #else
  241. #define m41t80_rtc_ioctl NULL
  242. #endif
  243. static int m41t80_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t)
  244. {
  245. struct i2c_client *client = to_i2c_client(dev);
  246. u8 wbuf[1 + M41T80_ALARM_REG_SIZE];
  247. u8 *buf = &wbuf[1];
  248. u8 *reg = buf - M41T80_REG_ALARM_MON;
  249. u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
  250. struct i2c_msg msgs_in[] = {
  251. {
  252. .addr = client->addr,
  253. .flags = 0,
  254. .len = 1,
  255. .buf = dt_addr,
  256. },
  257. {
  258. .addr = client->addr,
  259. .flags = I2C_M_RD,
  260. .len = M41T80_ALARM_REG_SIZE,
  261. .buf = buf,
  262. },
  263. };
  264. struct i2c_msg msgs[] = {
  265. {
  266. .addr = client->addr,
  267. .flags = 0,
  268. .len = 1 + M41T80_ALARM_REG_SIZE,
  269. .buf = wbuf,
  270. },
  271. };
  272. if (i2c_transfer(client->adapter, msgs_in, 2) < 0) {
  273. dev_err(&client->dev, "read error\n");
  274. return -EIO;
  275. }
  276. reg[M41T80_REG_ALARM_MON] &= ~(0x1f | M41T80_ALMON_AFE);
  277. reg[M41T80_REG_ALARM_DAY] = 0;
  278. reg[M41T80_REG_ALARM_HOUR] &= ~(0x3f | 0x80);
  279. reg[M41T80_REG_ALARM_MIN] = 0;
  280. reg[M41T80_REG_ALARM_SEC] = 0;
  281. wbuf[0] = M41T80_REG_ALARM_MON; /* offset into rtc's regs */
  282. reg[M41T80_REG_ALARM_SEC] |= t->time.tm_sec >= 0 ?
  283. BIN2BCD(t->time.tm_sec) : 0x80;
  284. reg[M41T80_REG_ALARM_MIN] |= t->time.tm_min >= 0 ?
  285. BIN2BCD(t->time.tm_min) : 0x80;
  286. reg[M41T80_REG_ALARM_HOUR] |= t->time.tm_hour >= 0 ?
  287. BIN2BCD(t->time.tm_hour) : 0x80;
  288. reg[M41T80_REG_ALARM_DAY] |= t->time.tm_mday >= 0 ?
  289. BIN2BCD(t->time.tm_mday) : 0x80;
  290. if (t->time.tm_mon >= 0)
  291. reg[M41T80_REG_ALARM_MON] |= BIN2BCD(t->time.tm_mon + 1);
  292. else
  293. reg[M41T80_REG_ALARM_DAY] |= 0x40;
  294. if (i2c_transfer(client->adapter, msgs, 1) != 1) {
  295. dev_err(&client->dev, "write error\n");
  296. return -EIO;
  297. }
  298. if (t->enabled) {
  299. reg[M41T80_REG_ALARM_MON] |= M41T80_ALMON_AFE;
  300. if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
  301. reg[M41T80_REG_ALARM_MON]) < 0) {
  302. dev_err(&client->dev, "write error\n");
  303. return -EIO;
  304. }
  305. }
  306. return 0;
  307. }
  308. static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t)
  309. {
  310. struct i2c_client *client = to_i2c_client(dev);
  311. u8 buf[M41T80_ALARM_REG_SIZE + 1]; /* all alarm regs and flags */
  312. u8 dt_addr[1] = { M41T80_REG_ALARM_MON };
  313. u8 *reg = buf - M41T80_REG_ALARM_MON;
  314. struct i2c_msg msgs[] = {
  315. {
  316. .addr = client->addr,
  317. .flags = 0,
  318. .len = 1,
  319. .buf = dt_addr,
  320. },
  321. {
  322. .addr = client->addr,
  323. .flags = I2C_M_RD,
  324. .len = M41T80_ALARM_REG_SIZE + 1,
  325. .buf = buf,
  326. },
  327. };
  328. if (i2c_transfer(client->adapter, msgs, 2) < 0) {
  329. dev_err(&client->dev, "read error\n");
  330. return -EIO;
  331. }
  332. t->time.tm_sec = -1;
  333. t->time.tm_min = -1;
  334. t->time.tm_hour = -1;
  335. t->time.tm_mday = -1;
  336. t->time.tm_mon = -1;
  337. if (!(reg[M41T80_REG_ALARM_SEC] & 0x80))
  338. t->time.tm_sec = BCD2BIN(reg[M41T80_REG_ALARM_SEC] & 0x7f);
  339. if (!(reg[M41T80_REG_ALARM_MIN] & 0x80))
  340. t->time.tm_min = BCD2BIN(reg[M41T80_REG_ALARM_MIN] & 0x7f);
  341. if (!(reg[M41T80_REG_ALARM_HOUR] & 0x80))
  342. t->time.tm_hour = BCD2BIN(reg[M41T80_REG_ALARM_HOUR] & 0x3f);
  343. if (!(reg[M41T80_REG_ALARM_DAY] & 0x80))
  344. t->time.tm_mday = BCD2BIN(reg[M41T80_REG_ALARM_DAY] & 0x3f);
  345. if (!(reg[M41T80_REG_ALARM_DAY] & 0x40))
  346. t->time.tm_mon = BCD2BIN(reg[M41T80_REG_ALARM_MON] & 0x1f) - 1;
  347. t->time.tm_year = -1;
  348. t->time.tm_wday = -1;
  349. t->time.tm_yday = -1;
  350. t->time.tm_isdst = -1;
  351. t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE);
  352. t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF);
  353. return 0;
  354. }
  355. static struct rtc_class_ops m41t80_rtc_ops = {
  356. .read_time = m41t80_rtc_read_time,
  357. .set_time = m41t80_rtc_set_time,
  358. .read_alarm = m41t80_rtc_read_alarm,
  359. .set_alarm = m41t80_rtc_set_alarm,
  360. .proc = m41t80_rtc_proc,
  361. .ioctl = m41t80_rtc_ioctl,
  362. };
  363. #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
  364. static ssize_t m41t80_sysfs_show_flags(struct device *dev,
  365. struct device_attribute *attr, char *buf)
  366. {
  367. struct i2c_client *client = to_i2c_client(dev);
  368. int val;
  369. val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
  370. if (val < 0)
  371. return -EIO;
  372. return sprintf(buf, "%#x\n", val);
  373. }
  374. static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL);
  375. static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
  376. struct device_attribute *attr, char *buf)
  377. {
  378. struct i2c_client *client = to_i2c_client(dev);
  379. int val;
  380. val = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
  381. if (val < 0)
  382. return -EIO;
  383. val = (val >> 4) & 0xf;
  384. switch (val) {
  385. case 0:
  386. break;
  387. case 1:
  388. val = 32768;
  389. break;
  390. default:
  391. val = 32768 >> val;
  392. }
  393. return sprintf(buf, "%d\n", val);
  394. }
  395. static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
  396. struct device_attribute *attr,
  397. const char *buf, size_t count)
  398. {
  399. struct i2c_client *client = to_i2c_client(dev);
  400. int almon, sqw;
  401. int val = simple_strtoul(buf, NULL, 0);
  402. if (val) {
  403. if (!is_power_of_2(val))
  404. return -EINVAL;
  405. val = ilog2(val);
  406. if (val == 15)
  407. val = 1;
  408. else if (val < 14)
  409. val = 15 - val;
  410. else
  411. return -EINVAL;
  412. }
  413. /* disable SQW, set SQW frequency & re-enable */
  414. almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
  415. if (almon < 0)
  416. return -EIO;
  417. sqw = i2c_smbus_read_byte_data(client, M41T80_REG_SQW);
  418. if (sqw < 0)
  419. return -EIO;
  420. sqw = (sqw & 0x0f) | (val << 4);
  421. if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
  422. almon & ~M41T80_ALMON_SQWE) < 0 ||
  423. i2c_smbus_write_byte_data(client, M41T80_REG_SQW, sqw) < 0)
  424. return -EIO;
  425. if (val && i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
  426. almon | M41T80_ALMON_SQWE) < 0)
  427. return -EIO;
  428. return count;
  429. }
  430. static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR,
  431. m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
  432. static struct attribute *attrs[] = {
  433. &dev_attr_flags.attr,
  434. &dev_attr_sqwfreq.attr,
  435. NULL,
  436. };
  437. static struct attribute_group attr_group = {
  438. .attrs = attrs,
  439. };
  440. static int m41t80_sysfs_register(struct device *dev)
  441. {
  442. return sysfs_create_group(&dev->kobj, &attr_group);
  443. }
  444. #else
  445. static int m41t80_sysfs_register(struct device *dev)
  446. {
  447. return 0;
  448. }
  449. #endif
  450. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  451. /*
  452. *****************************************************************************
  453. *
  454. * Watchdog Driver
  455. *
  456. *****************************************************************************
  457. */
  458. static struct i2c_client *save_client;
  459. /* Default margin */
  460. #define WD_TIMO 60 /* 1..31 seconds */
  461. static int wdt_margin = WD_TIMO;
  462. module_param(wdt_margin, int, 0);
  463. MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
  464. static unsigned long wdt_is_open;
  465. static int boot_flag;
  466. /**
  467. * wdt_ping:
  468. *
  469. * Reload counter one with the watchdog timeout. We don't bother reloading
  470. * the cascade counter.
  471. */
  472. static void wdt_ping(void)
  473. {
  474. unsigned char i2c_data[2];
  475. struct i2c_msg msgs1[1] = {
  476. {
  477. .addr = save_client->addr,
  478. .flags = 0,
  479. .len = 2,
  480. .buf = i2c_data,
  481. },
  482. };
  483. i2c_data[0] = 0x09; /* watchdog register */
  484. if (wdt_margin > 31)
  485. i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
  486. else
  487. /*
  488. * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
  489. */
  490. i2c_data[1] = wdt_margin<<2 | 0x82;
  491. i2c_transfer(save_client->adapter, msgs1, 1);
  492. }
  493. /**
  494. * wdt_disable:
  495. *
  496. * disables watchdog.
  497. */
  498. static void wdt_disable(void)
  499. {
  500. unsigned char i2c_data[2], i2c_buf[0x10];
  501. struct i2c_msg msgs0[2] = {
  502. {
  503. .addr = save_client->addr,
  504. .flags = 0,
  505. .len = 1,
  506. .buf = i2c_data,
  507. },
  508. {
  509. .addr = save_client->addr,
  510. .flags = I2C_M_RD,
  511. .len = 1,
  512. .buf = i2c_buf,
  513. },
  514. };
  515. struct i2c_msg msgs1[1] = {
  516. {
  517. .addr = save_client->addr,
  518. .flags = 0,
  519. .len = 2,
  520. .buf = i2c_data,
  521. },
  522. };
  523. i2c_data[0] = 0x09;
  524. i2c_transfer(save_client->adapter, msgs0, 2);
  525. i2c_data[0] = 0x09;
  526. i2c_data[1] = 0x00;
  527. i2c_transfer(save_client->adapter, msgs1, 1);
  528. }
  529. /**
  530. * wdt_write:
  531. * @file: file handle to the watchdog
  532. * @buf: buffer to write (unused as data does not matter here
  533. * @count: count of bytes
  534. * @ppos: pointer to the position to write. No seeks allowed
  535. *
  536. * A write to a watchdog device is defined as a keepalive signal. Any
  537. * write of data will do, as we we don't define content meaning.
  538. */
  539. static ssize_t wdt_write(struct file *file, const char __user *buf,
  540. size_t count, loff_t *ppos)
  541. {
  542. /* Can't seek (pwrite) on this device
  543. if (ppos != &file->f_pos)
  544. return -ESPIPE;
  545. */
  546. if (count) {
  547. wdt_ping();
  548. return 1;
  549. }
  550. return 0;
  551. }
  552. static ssize_t wdt_read(struct file *file, char __user *buf,
  553. size_t count, loff_t *ppos)
  554. {
  555. return 0;
  556. }
  557. /**
  558. * wdt_ioctl:
  559. * @inode: inode of the device
  560. * @file: file handle to the device
  561. * @cmd: watchdog command
  562. * @arg: argument pointer
  563. *
  564. * The watchdog API defines a common set of functions for all watchdogs
  565. * according to their available features. We only actually usefully support
  566. * querying capabilities and current status.
  567. */
  568. static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  569. unsigned long arg)
  570. {
  571. int new_margin, rv;
  572. static struct watchdog_info ident = {
  573. .options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
  574. WDIOF_SETTIMEOUT,
  575. .firmware_version = 1,
  576. .identity = "M41T80 WTD"
  577. };
  578. switch (cmd) {
  579. case WDIOC_GETSUPPORT:
  580. return copy_to_user((struct watchdog_info __user *)arg, &ident,
  581. sizeof(ident)) ? -EFAULT : 0;
  582. case WDIOC_GETSTATUS:
  583. case WDIOC_GETBOOTSTATUS:
  584. return put_user(boot_flag, (int __user *)arg);
  585. case WDIOC_KEEPALIVE:
  586. wdt_ping();
  587. return 0;
  588. case WDIOC_SETTIMEOUT:
  589. if (get_user(new_margin, (int __user *)arg))
  590. return -EFAULT;
  591. /* Arbitrary, can't find the card's limits */
  592. if (new_margin < 1 || new_margin > 124)
  593. return -EINVAL;
  594. wdt_margin = new_margin;
  595. wdt_ping();
  596. /* Fall */
  597. case WDIOC_GETTIMEOUT:
  598. return put_user(wdt_margin, (int __user *)arg);
  599. case WDIOC_SETOPTIONS:
  600. if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
  601. return -EFAULT;
  602. if (rv & WDIOS_DISABLECARD) {
  603. printk(KERN_INFO
  604. "rtc-m41t80: disable watchdog\n");
  605. wdt_disable();
  606. }
  607. if (rv & WDIOS_ENABLECARD) {
  608. printk(KERN_INFO
  609. "rtc-m41t80: enable watchdog\n");
  610. wdt_ping();
  611. }
  612. return -EINVAL;
  613. }
  614. return -ENOTTY;
  615. }
  616. /**
  617. * wdt_open:
  618. * @inode: inode of device
  619. * @file: file handle to device
  620. *
  621. */
  622. static int wdt_open(struct inode *inode, struct file *file)
  623. {
  624. if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
  625. if (test_and_set_bit(0, &wdt_is_open))
  626. return -EBUSY;
  627. /*
  628. * Activate
  629. */
  630. wdt_is_open = 1;
  631. return 0;
  632. }
  633. return -ENODEV;
  634. }
  635. /**
  636. * wdt_close:
  637. * @inode: inode to board
  638. * @file: file handle to board
  639. *
  640. */
  641. static int wdt_release(struct inode *inode, struct file *file)
  642. {
  643. if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
  644. clear_bit(0, &wdt_is_open);
  645. return 0;
  646. }
  647. /**
  648. * notify_sys:
  649. * @this: our notifier block
  650. * @code: the event being reported
  651. * @unused: unused
  652. *
  653. * Our notifier is called on system shutdowns. We want to turn the card
  654. * off at reboot otherwise the machine will reboot again during memory
  655. * test or worse yet during the following fsck. This would suck, in fact
  656. * trust me - if it happens it does suck.
  657. */
  658. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  659. void *unused)
  660. {
  661. if (code == SYS_DOWN || code == SYS_HALT)
  662. /* Disable Watchdog */
  663. wdt_disable();
  664. return NOTIFY_DONE;
  665. }
  666. static const struct file_operations wdt_fops = {
  667. .owner = THIS_MODULE,
  668. .read = wdt_read,
  669. .ioctl = wdt_ioctl,
  670. .write = wdt_write,
  671. .open = wdt_open,
  672. .release = wdt_release,
  673. };
  674. static struct miscdevice wdt_dev = {
  675. .minor = WATCHDOG_MINOR,
  676. .name = "watchdog",
  677. .fops = &wdt_fops,
  678. };
  679. /*
  680. * The WDT card needs to learn about soft shutdowns in order to
  681. * turn the timebomb registers off.
  682. */
  683. static struct notifier_block wdt_notifier = {
  684. .notifier_call = wdt_notify_sys,
  685. };
  686. #endif /* CONFIG_RTC_DRV_M41T80_WDT */
  687. /*
  688. *****************************************************************************
  689. *
  690. * Driver Interface
  691. *
  692. *****************************************************************************
  693. */
  694. static int m41t80_probe(struct i2c_client *client)
  695. {
  696. int i, rc = 0;
  697. struct rtc_device *rtc = NULL;
  698. struct rtc_time tm;
  699. const struct m41t80_chip_info *chip;
  700. struct m41t80_data *clientdata = NULL;
  701. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
  702. | I2C_FUNC_SMBUS_BYTE_DATA)) {
  703. rc = -ENODEV;
  704. goto exit;
  705. }
  706. dev_info(&client->dev,
  707. "chip found, driver version " DRV_VERSION "\n");
  708. chip = NULL;
  709. for (i = 0; i < ARRAY_SIZE(m41t80_chip_info_tbl); i++) {
  710. if (!strcmp(m41t80_chip_info_tbl[i].name, client->name)) {
  711. chip = &m41t80_chip_info_tbl[i];
  712. break;
  713. }
  714. }
  715. if (!chip) {
  716. dev_err(&client->dev, "%s is not supported\n", client->name);
  717. rc = -ENODEV;
  718. goto exit;
  719. }
  720. clientdata = kzalloc(sizeof(*clientdata), GFP_KERNEL);
  721. if (!clientdata) {
  722. rc = -ENOMEM;
  723. goto exit;
  724. }
  725. rtc = rtc_device_register(client->name, &client->dev,
  726. &m41t80_rtc_ops, THIS_MODULE);
  727. if (IS_ERR(rtc)) {
  728. rc = PTR_ERR(rtc);
  729. rtc = NULL;
  730. goto exit;
  731. }
  732. clientdata->rtc = rtc;
  733. clientdata->chip = chip;
  734. i2c_set_clientdata(client, clientdata);
  735. /* Make sure HT (Halt Update) bit is cleared */
  736. rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
  737. if (rc < 0)
  738. goto ht_err;
  739. if (rc & M41T80_ALHOUR_HT) {
  740. if (chip->features & M41T80_FEATURE_HT) {
  741. m41t80_get_datetime(client, &tm);
  742. dev_info(&client->dev, "HT bit was set!\n");
  743. dev_info(&client->dev,
  744. "Power Down at "
  745. "%04i-%02i-%02i %02i:%02i:%02i\n",
  746. tm.tm_year + 1900,
  747. tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
  748. tm.tm_min, tm.tm_sec);
  749. }
  750. if (i2c_smbus_write_byte_data(client,
  751. M41T80_REG_ALARM_HOUR,
  752. rc & ~M41T80_ALHOUR_HT) < 0)
  753. goto ht_err;
  754. }
  755. /* Make sure ST (stop) bit is cleared */
  756. rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
  757. if (rc < 0)
  758. goto st_err;
  759. if (rc & M41T80_SEC_ST) {
  760. if (i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
  761. rc & ~M41T80_SEC_ST) < 0)
  762. goto st_err;
  763. }
  764. rc = m41t80_sysfs_register(&client->dev);
  765. if (rc)
  766. goto exit;
  767. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  768. if (chip->features & M41T80_FEATURE_HT) {
  769. rc = misc_register(&wdt_dev);
  770. if (rc)
  771. goto exit;
  772. rc = register_reboot_notifier(&wdt_notifier);
  773. if (rc) {
  774. misc_deregister(&wdt_dev);
  775. goto exit;
  776. }
  777. save_client = client;
  778. }
  779. #endif
  780. return 0;
  781. st_err:
  782. rc = -EIO;
  783. dev_err(&client->dev, "Can't clear ST bit\n");
  784. goto exit;
  785. ht_err:
  786. rc = -EIO;
  787. dev_err(&client->dev, "Can't clear HT bit\n");
  788. goto exit;
  789. exit:
  790. if (rtc)
  791. rtc_device_unregister(rtc);
  792. kfree(clientdata);
  793. return rc;
  794. }
  795. static int m41t80_remove(struct i2c_client *client)
  796. {
  797. struct m41t80_data *clientdata = i2c_get_clientdata(client);
  798. struct rtc_device *rtc = clientdata->rtc;
  799. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  800. if (clientdata->chip->features & M41T80_FEATURE_HT) {
  801. misc_deregister(&wdt_dev);
  802. unregister_reboot_notifier(&wdt_notifier);
  803. }
  804. #endif
  805. if (rtc)
  806. rtc_device_unregister(rtc);
  807. kfree(clientdata);
  808. return 0;
  809. }
  810. static struct i2c_driver m41t80_driver = {
  811. .driver = {
  812. .name = "rtc-m41t80",
  813. },
  814. .probe = m41t80_probe,
  815. .remove = m41t80_remove,
  816. };
  817. static int __init m41t80_rtc_init(void)
  818. {
  819. return i2c_add_driver(&m41t80_driver);
  820. }
  821. static void __exit m41t80_rtc_exit(void)
  822. {
  823. i2c_del_driver(&m41t80_driver);
  824. }
  825. MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
  826. MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
  827. MODULE_LICENSE("GPL");
  828. MODULE_VERSION(DRV_VERSION);
  829. module_init(m41t80_rtc_init);
  830. module_exit(m41t80_rtc_exit);