rtc-m41t80.c 24 KB

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