rtc-m41t80.c 21 KB

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