rtc-m41t80.c 18 KB

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