rtc-m41t80.c 23 KB

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