rtc-m41t80.c 21 KB

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