rtc-m41t80.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. if (tm->tm_year < 100 || tm->tm_year > 199) {
  166. dev_err(&client->dev, "Year must be between 2000 and 2099. It's %d.\n",
  167. tm->tm_year + 1900);
  168. return -EINVAL;
  169. }
  170. buf[M41T80_REG_YEAR] = bin2bcd(tm->tm_year % 100);
  171. if (i2c_transfer(client->adapter, msgs, 1) != 1) {
  172. dev_err(&client->dev, "write error\n");
  173. return -EIO;
  174. }
  175. return 0;
  176. }
  177. #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
  178. static int m41t80_rtc_proc(struct device *dev, struct seq_file *seq)
  179. {
  180. struct i2c_client *client = to_i2c_client(dev);
  181. struct m41t80_data *clientdata = i2c_get_clientdata(client);
  182. u8 reg;
  183. if (clientdata->features & M41T80_FEATURE_BL) {
  184. reg = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
  185. seq_printf(seq, "battery\t\t: %s\n",
  186. (reg & M41T80_FLAGS_BATT_LOW) ? "exhausted" : "ok");
  187. }
  188. return 0;
  189. }
  190. #else
  191. #define m41t80_rtc_proc NULL
  192. #endif
  193. static int m41t80_rtc_read_time(struct device *dev, struct rtc_time *tm)
  194. {
  195. return m41t80_get_datetime(to_i2c_client(dev), tm);
  196. }
  197. static int m41t80_rtc_set_time(struct device *dev, struct rtc_time *tm)
  198. {
  199. return m41t80_set_datetime(to_i2c_client(dev), tm);
  200. }
  201. /*
  202. * XXX - m41t80 alarm functionality is reported broken.
  203. * until it is fixed, don't register alarm functions.
  204. */
  205. static struct rtc_class_ops m41t80_rtc_ops = {
  206. .read_time = m41t80_rtc_read_time,
  207. .set_time = m41t80_rtc_set_time,
  208. .proc = m41t80_rtc_proc,
  209. };
  210. #if defined(CONFIG_RTC_INTF_SYSFS) || defined(CONFIG_RTC_INTF_SYSFS_MODULE)
  211. static ssize_t m41t80_sysfs_show_flags(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. struct i2c_client *client = to_i2c_client(dev);
  215. int val;
  216. val = i2c_smbus_read_byte_data(client, M41T80_REG_FLAGS);
  217. if (val < 0)
  218. return val;
  219. return sprintf(buf, "%#x\n", val);
  220. }
  221. static DEVICE_ATTR(flags, S_IRUGO, m41t80_sysfs_show_flags, NULL);
  222. static ssize_t m41t80_sysfs_show_sqwfreq(struct device *dev,
  223. struct device_attribute *attr, char *buf)
  224. {
  225. struct i2c_client *client = to_i2c_client(dev);
  226. struct m41t80_data *clientdata = i2c_get_clientdata(client);
  227. int val, reg_sqw;
  228. if (!(clientdata->features & M41T80_FEATURE_SQ))
  229. return -EINVAL;
  230. reg_sqw = M41T80_REG_SQW;
  231. if (clientdata->features & M41T80_FEATURE_SQ_ALT)
  232. reg_sqw = M41T80_REG_WDAY;
  233. val = i2c_smbus_read_byte_data(client, reg_sqw);
  234. if (val < 0)
  235. return val;
  236. val = (val >> 4) & 0xf;
  237. switch (val) {
  238. case 0:
  239. break;
  240. case 1:
  241. val = 32768;
  242. break;
  243. default:
  244. val = 32768 >> val;
  245. }
  246. return sprintf(buf, "%d\n", val);
  247. }
  248. static ssize_t m41t80_sysfs_set_sqwfreq(struct device *dev,
  249. struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct i2c_client *client = to_i2c_client(dev);
  253. struct m41t80_data *clientdata = i2c_get_clientdata(client);
  254. int almon, sqw, reg_sqw, rc;
  255. int val = simple_strtoul(buf, NULL, 0);
  256. if (!(clientdata->features & M41T80_FEATURE_SQ))
  257. return -EINVAL;
  258. if (val) {
  259. if (!is_power_of_2(val))
  260. return -EINVAL;
  261. val = ilog2(val);
  262. if (val == 15)
  263. val = 1;
  264. else if (val < 14)
  265. val = 15 - val;
  266. else
  267. return -EINVAL;
  268. }
  269. /* disable SQW, set SQW frequency & re-enable */
  270. almon = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_MON);
  271. if (almon < 0)
  272. return almon;
  273. reg_sqw = M41T80_REG_SQW;
  274. if (clientdata->features & M41T80_FEATURE_SQ_ALT)
  275. reg_sqw = M41T80_REG_WDAY;
  276. sqw = i2c_smbus_read_byte_data(client, reg_sqw);
  277. if (sqw < 0)
  278. return sqw;
  279. sqw = (sqw & 0x0f) | (val << 4);
  280. rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
  281. almon & ~M41T80_ALMON_SQWE);
  282. if (rc < 0)
  283. return rc;
  284. if (val) {
  285. rc = i2c_smbus_write_byte_data(client, reg_sqw, sqw);
  286. if (rc < 0)
  287. return rc;
  288. rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_MON,
  289. almon | M41T80_ALMON_SQWE);
  290. if (rc <0)
  291. return rc;
  292. }
  293. return count;
  294. }
  295. static DEVICE_ATTR(sqwfreq, S_IRUGO | S_IWUSR,
  296. m41t80_sysfs_show_sqwfreq, m41t80_sysfs_set_sqwfreq);
  297. static struct attribute *attrs[] = {
  298. &dev_attr_flags.attr,
  299. &dev_attr_sqwfreq.attr,
  300. NULL,
  301. };
  302. static struct attribute_group attr_group = {
  303. .attrs = attrs,
  304. };
  305. static int m41t80_sysfs_register(struct device *dev)
  306. {
  307. return sysfs_create_group(&dev->kobj, &attr_group);
  308. }
  309. #else
  310. static int m41t80_sysfs_register(struct device *dev)
  311. {
  312. return 0;
  313. }
  314. #endif
  315. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  316. /*
  317. *****************************************************************************
  318. *
  319. * Watchdog Driver
  320. *
  321. *****************************************************************************
  322. */
  323. static struct i2c_client *save_client;
  324. /* Default margin */
  325. #define WD_TIMO 60 /* 1..31 seconds */
  326. static int wdt_margin = WD_TIMO;
  327. module_param(wdt_margin, int, 0);
  328. MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 60s)");
  329. static unsigned long wdt_is_open;
  330. static int boot_flag;
  331. /**
  332. * wdt_ping:
  333. *
  334. * Reload counter one with the watchdog timeout. We don't bother reloading
  335. * the cascade counter.
  336. */
  337. static void wdt_ping(void)
  338. {
  339. unsigned char i2c_data[2];
  340. struct i2c_msg msgs1[1] = {
  341. {
  342. .addr = save_client->addr,
  343. .flags = 0,
  344. .len = 2,
  345. .buf = i2c_data,
  346. },
  347. };
  348. struct m41t80_data *clientdata = i2c_get_clientdata(save_client);
  349. i2c_data[0] = 0x09; /* watchdog register */
  350. if (wdt_margin > 31)
  351. i2c_data[1] = (wdt_margin & 0xFC) | 0x83; /* resolution = 4s */
  352. else
  353. /*
  354. * WDS = 1 (0x80), mulitplier = WD_TIMO, resolution = 1s (0x02)
  355. */
  356. i2c_data[1] = wdt_margin<<2 | 0x82;
  357. /*
  358. * M41T65 has three bits for watchdog resolution. Don't set bit 7, as
  359. * that would be an invalid resolution.
  360. */
  361. if (clientdata->features & M41T80_FEATURE_WD)
  362. i2c_data[1] &= ~M41T80_WATCHDOG_RB2;
  363. i2c_transfer(save_client->adapter, msgs1, 1);
  364. }
  365. /**
  366. * wdt_disable:
  367. *
  368. * disables watchdog.
  369. */
  370. static void wdt_disable(void)
  371. {
  372. unsigned char i2c_data[2], i2c_buf[0x10];
  373. struct i2c_msg msgs0[2] = {
  374. {
  375. .addr = save_client->addr,
  376. .flags = 0,
  377. .len = 1,
  378. .buf = i2c_data,
  379. },
  380. {
  381. .addr = save_client->addr,
  382. .flags = I2C_M_RD,
  383. .len = 1,
  384. .buf = i2c_buf,
  385. },
  386. };
  387. struct i2c_msg msgs1[1] = {
  388. {
  389. .addr = save_client->addr,
  390. .flags = 0,
  391. .len = 2,
  392. .buf = i2c_data,
  393. },
  394. };
  395. i2c_data[0] = 0x09;
  396. i2c_transfer(save_client->adapter, msgs0, 2);
  397. i2c_data[0] = 0x09;
  398. i2c_data[1] = 0x00;
  399. i2c_transfer(save_client->adapter, msgs1, 1);
  400. }
  401. /**
  402. * wdt_write:
  403. * @file: file handle to the watchdog
  404. * @buf: buffer to write (unused as data does not matter here
  405. * @count: count of bytes
  406. * @ppos: pointer to the position to write. No seeks allowed
  407. *
  408. * A write to a watchdog device is defined as a keepalive signal. Any
  409. * write of data will do, as we we don't define content meaning.
  410. */
  411. static ssize_t wdt_write(struct file *file, const char __user *buf,
  412. size_t count, loff_t *ppos)
  413. {
  414. if (count) {
  415. wdt_ping();
  416. return 1;
  417. }
  418. return 0;
  419. }
  420. static ssize_t wdt_read(struct file *file, char __user *buf,
  421. size_t count, loff_t *ppos)
  422. {
  423. return 0;
  424. }
  425. /**
  426. * wdt_ioctl:
  427. * @inode: inode of the device
  428. * @file: file handle to the device
  429. * @cmd: watchdog command
  430. * @arg: argument pointer
  431. *
  432. * The watchdog API defines a common set of functions for all watchdogs
  433. * according to their available features. We only actually usefully support
  434. * querying capabilities and current status.
  435. */
  436. static int wdt_ioctl(struct file *file, unsigned int cmd,
  437. unsigned long arg)
  438. {
  439. int new_margin, rv;
  440. static struct watchdog_info ident = {
  441. .options = WDIOF_POWERUNDER | WDIOF_KEEPALIVEPING |
  442. WDIOF_SETTIMEOUT,
  443. .firmware_version = 1,
  444. .identity = "M41T80 WTD"
  445. };
  446. switch (cmd) {
  447. case WDIOC_GETSUPPORT:
  448. return copy_to_user((struct watchdog_info __user *)arg, &ident,
  449. sizeof(ident)) ? -EFAULT : 0;
  450. case WDIOC_GETSTATUS:
  451. case WDIOC_GETBOOTSTATUS:
  452. return put_user(boot_flag, (int __user *)arg);
  453. case WDIOC_KEEPALIVE:
  454. wdt_ping();
  455. return 0;
  456. case WDIOC_SETTIMEOUT:
  457. if (get_user(new_margin, (int __user *)arg))
  458. return -EFAULT;
  459. /* Arbitrary, can't find the card's limits */
  460. if (new_margin < 1 || new_margin > 124)
  461. return -EINVAL;
  462. wdt_margin = new_margin;
  463. wdt_ping();
  464. /* Fall */
  465. case WDIOC_GETTIMEOUT:
  466. return put_user(wdt_margin, (int __user *)arg);
  467. case WDIOC_SETOPTIONS:
  468. if (copy_from_user(&rv, (int __user *)arg, sizeof(int)))
  469. return -EFAULT;
  470. if (rv & WDIOS_DISABLECARD) {
  471. pr_info("disable watchdog\n");
  472. wdt_disable();
  473. }
  474. if (rv & WDIOS_ENABLECARD) {
  475. pr_info("enable watchdog\n");
  476. wdt_ping();
  477. }
  478. return -EINVAL;
  479. }
  480. return -ENOTTY;
  481. }
  482. static long wdt_unlocked_ioctl(struct file *file, unsigned int cmd,
  483. unsigned long arg)
  484. {
  485. int ret;
  486. mutex_lock(&m41t80_rtc_mutex);
  487. ret = wdt_ioctl(file, cmd, arg);
  488. mutex_unlock(&m41t80_rtc_mutex);
  489. return ret;
  490. }
  491. /**
  492. * wdt_open:
  493. * @inode: inode of device
  494. * @file: file handle to device
  495. *
  496. */
  497. static int wdt_open(struct inode *inode, struct file *file)
  498. {
  499. if (MINOR(inode->i_rdev) == WATCHDOG_MINOR) {
  500. mutex_lock(&m41t80_rtc_mutex);
  501. if (test_and_set_bit(0, &wdt_is_open)) {
  502. mutex_unlock(&m41t80_rtc_mutex);
  503. return -EBUSY;
  504. }
  505. /*
  506. * Activate
  507. */
  508. wdt_is_open = 1;
  509. mutex_unlock(&m41t80_rtc_mutex);
  510. return nonseekable_open(inode, file);
  511. }
  512. return -ENODEV;
  513. }
  514. /**
  515. * wdt_close:
  516. * @inode: inode to board
  517. * @file: file handle to board
  518. *
  519. */
  520. static int wdt_release(struct inode *inode, struct file *file)
  521. {
  522. if (MINOR(inode->i_rdev) == WATCHDOG_MINOR)
  523. clear_bit(0, &wdt_is_open);
  524. return 0;
  525. }
  526. /**
  527. * notify_sys:
  528. * @this: our notifier block
  529. * @code: the event being reported
  530. * @unused: unused
  531. *
  532. * Our notifier is called on system shutdowns. We want to turn the card
  533. * off at reboot otherwise the machine will reboot again during memory
  534. * test or worse yet during the following fsck. This would suck, in fact
  535. * trust me - if it happens it does suck.
  536. */
  537. static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
  538. void *unused)
  539. {
  540. if (code == SYS_DOWN || code == SYS_HALT)
  541. /* Disable Watchdog */
  542. wdt_disable();
  543. return NOTIFY_DONE;
  544. }
  545. static const struct file_operations wdt_fops = {
  546. .owner = THIS_MODULE,
  547. .read = wdt_read,
  548. .unlocked_ioctl = wdt_unlocked_ioctl,
  549. .write = wdt_write,
  550. .open = wdt_open,
  551. .release = wdt_release,
  552. .llseek = no_llseek,
  553. };
  554. static struct miscdevice wdt_dev = {
  555. .minor = WATCHDOG_MINOR,
  556. .name = "watchdog",
  557. .fops = &wdt_fops,
  558. };
  559. /*
  560. * The WDT card needs to learn about soft shutdowns in order to
  561. * turn the timebomb registers off.
  562. */
  563. static struct notifier_block wdt_notifier = {
  564. .notifier_call = wdt_notify_sys,
  565. };
  566. #endif /* CONFIG_RTC_DRV_M41T80_WDT */
  567. /*
  568. *****************************************************************************
  569. *
  570. * Driver Interface
  571. *
  572. *****************************************************************************
  573. */
  574. static int m41t80_probe(struct i2c_client *client,
  575. const struct i2c_device_id *id)
  576. {
  577. int rc = 0;
  578. struct rtc_device *rtc = NULL;
  579. struct rtc_time tm;
  580. struct m41t80_data *clientdata = NULL;
  581. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C
  582. | I2C_FUNC_SMBUS_BYTE_DATA))
  583. return -ENODEV;
  584. clientdata = devm_kzalloc(&client->dev, sizeof(*clientdata),
  585. GFP_KERNEL);
  586. if (!clientdata)
  587. return -ENOMEM;
  588. clientdata->features = id->driver_data;
  589. i2c_set_clientdata(client, clientdata);
  590. rtc = devm_rtc_device_register(&client->dev, client->name,
  591. &m41t80_rtc_ops, THIS_MODULE);
  592. if (IS_ERR(rtc))
  593. return PTR_ERR(rtc);
  594. clientdata->rtc = rtc;
  595. /* Make sure HT (Halt Update) bit is cleared */
  596. rc = i2c_smbus_read_byte_data(client, M41T80_REG_ALARM_HOUR);
  597. if (rc >= 0 && rc & M41T80_ALHOUR_HT) {
  598. if (clientdata->features & M41T80_FEATURE_HT) {
  599. m41t80_get_datetime(client, &tm);
  600. dev_info(&client->dev, "HT bit was set!\n");
  601. dev_info(&client->dev,
  602. "Power Down at "
  603. "%04i-%02i-%02i %02i:%02i:%02i\n",
  604. tm.tm_year + 1900,
  605. tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
  606. tm.tm_min, tm.tm_sec);
  607. }
  608. rc = i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_HOUR,
  609. rc & ~M41T80_ALHOUR_HT);
  610. }
  611. if (rc < 0) {
  612. dev_err(&client->dev, "Can't clear HT bit\n");
  613. return rc;
  614. }
  615. /* Make sure ST (stop) bit is cleared */
  616. rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC);
  617. if (rc >= 0 && rc & M41T80_SEC_ST)
  618. rc = i2c_smbus_write_byte_data(client, M41T80_REG_SEC,
  619. rc & ~M41T80_SEC_ST);
  620. if (rc < 0) {
  621. dev_err(&client->dev, "Can't clear ST bit\n");
  622. return rc;
  623. }
  624. rc = m41t80_sysfs_register(&client->dev);
  625. if (rc)
  626. return rc;
  627. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  628. if (clientdata->features & M41T80_FEATURE_HT) {
  629. save_client = client;
  630. rc = misc_register(&wdt_dev);
  631. if (rc)
  632. return rc;
  633. rc = register_reboot_notifier(&wdt_notifier);
  634. if (rc) {
  635. misc_deregister(&wdt_dev);
  636. return rc;
  637. }
  638. }
  639. #endif
  640. return 0;
  641. }
  642. static int m41t80_remove(struct i2c_client *client)
  643. {
  644. #ifdef CONFIG_RTC_DRV_M41T80_WDT
  645. struct m41t80_data *clientdata = i2c_get_clientdata(client);
  646. if (clientdata->features & M41T80_FEATURE_HT) {
  647. misc_deregister(&wdt_dev);
  648. unregister_reboot_notifier(&wdt_notifier);
  649. }
  650. #endif
  651. return 0;
  652. }
  653. static struct i2c_driver m41t80_driver = {
  654. .driver = {
  655. .name = "rtc-m41t80",
  656. },
  657. .probe = m41t80_probe,
  658. .remove = m41t80_remove,
  659. .id_table = m41t80_id,
  660. };
  661. module_i2c_driver(m41t80_driver);
  662. MODULE_AUTHOR("Alexander Bigga <ab@mycable.de>");
  663. MODULE_DESCRIPTION("ST Microelectronics M41T80 series RTC I2C Client Driver");
  664. MODULE_LICENSE("GPL");