rtc-m41t80.c 21 KB

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