rtc-m41t80.c 22 KB

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