rtc-m41t80.c 22 KB

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