rtc-m41t80.c 27 KB

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