rtc-ac100.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * RTC Driver for X-Powers AC100
  3. *
  4. * Copyright (c) 2016 Chen-Yu Tsai
  5. *
  6. * Chen-Yu Tsai <wens@csie.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. #include <linux/bcd.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mfd/ac100.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/regmap.h>
  28. #include <linux/rtc.h>
  29. #include <linux/types.h>
  30. /* Control register */
  31. #define AC100_RTC_CTRL_24HOUR BIT(0)
  32. /* Clock output register bits */
  33. #define AC100_CLKOUT_PRE_DIV_SHIFT 5
  34. #define AC100_CLKOUT_PRE_DIV_WIDTH 3
  35. #define AC100_CLKOUT_MUX_SHIFT 4
  36. #define AC100_CLKOUT_MUX_WIDTH 1
  37. #define AC100_CLKOUT_DIV_SHIFT 1
  38. #define AC100_CLKOUT_DIV_WIDTH 3
  39. #define AC100_CLKOUT_EN BIT(0)
  40. /* RTC */
  41. #define AC100_RTC_SEC_MASK GENMASK(6, 0)
  42. #define AC100_RTC_MIN_MASK GENMASK(6, 0)
  43. #define AC100_RTC_HOU_MASK GENMASK(5, 0)
  44. #define AC100_RTC_WEE_MASK GENMASK(2, 0)
  45. #define AC100_RTC_DAY_MASK GENMASK(5, 0)
  46. #define AC100_RTC_MON_MASK GENMASK(4, 0)
  47. #define AC100_RTC_YEA_MASK GENMASK(7, 0)
  48. #define AC100_RTC_YEA_LEAP BIT(15)
  49. #define AC100_RTC_UPD_TRIGGER BIT(15)
  50. /* Alarm (wall clock) */
  51. #define AC100_ALM_INT_ENABLE BIT(0)
  52. #define AC100_ALM_SEC_MASK GENMASK(6, 0)
  53. #define AC100_ALM_MIN_MASK GENMASK(6, 0)
  54. #define AC100_ALM_HOU_MASK GENMASK(5, 0)
  55. #define AC100_ALM_WEE_MASK GENMASK(2, 0)
  56. #define AC100_ALM_DAY_MASK GENMASK(5, 0)
  57. #define AC100_ALM_MON_MASK GENMASK(4, 0)
  58. #define AC100_ALM_YEA_MASK GENMASK(7, 0)
  59. #define AC100_ALM_ENABLE_FLAG BIT(15)
  60. #define AC100_ALM_UPD_TRIGGER BIT(15)
  61. /*
  62. * The year parameter passed to the driver is usually an offset relative to
  63. * the year 1900. This macro is used to convert this offset to another one
  64. * relative to the minimum year allowed by the hardware.
  65. *
  66. * The year range is 1970 - 2069. This range is selected to match Allwinner's
  67. * driver.
  68. */
  69. #define AC100_YEAR_MIN 1970
  70. #define AC100_YEAR_MAX 2069
  71. #define AC100_YEAR_OFF (AC100_YEAR_MIN - 1900)
  72. struct ac100_clkout {
  73. struct clk_hw hw;
  74. struct regmap *regmap;
  75. u8 offset;
  76. };
  77. #define to_ac100_clkout(_hw) container_of(_hw, struct ac100_clkout, hw)
  78. #define AC100_RTC_32K_NAME "ac100-rtc-32k"
  79. #define AC100_RTC_32K_RATE 32768
  80. #define AC100_CLKOUT_NUM 3
  81. static const char * const ac100_clkout_names[AC100_CLKOUT_NUM] = {
  82. "ac100-cko1-rtc",
  83. "ac100-cko2-rtc",
  84. "ac100-cko3-rtc",
  85. };
  86. struct ac100_rtc_dev {
  87. struct rtc_device *rtc;
  88. struct device *dev;
  89. struct regmap *regmap;
  90. int irq;
  91. unsigned long alarm;
  92. struct clk_hw *rtc_32k_clk;
  93. struct ac100_clkout clks[AC100_CLKOUT_NUM];
  94. struct clk_hw_onecell_data *clk_data;
  95. };
  96. /**
  97. * Clock controls for 3 clock output pins
  98. */
  99. static const struct clk_div_table ac100_clkout_prediv[] = {
  100. { .val = 0, .div = 1 },
  101. { .val = 1, .div = 2 },
  102. { .val = 2, .div = 4 },
  103. { .val = 3, .div = 8 },
  104. { .val = 4, .div = 16 },
  105. { .val = 5, .div = 32 },
  106. { .val = 6, .div = 64 },
  107. { .val = 7, .div = 122 },
  108. { },
  109. };
  110. /* Abuse the fact that one parent is 32768 Hz, and the other is 4 MHz */
  111. static unsigned long ac100_clkout_recalc_rate(struct clk_hw *hw,
  112. unsigned long prate)
  113. {
  114. struct ac100_clkout *clk = to_ac100_clkout(hw);
  115. unsigned int reg, div;
  116. regmap_read(clk->regmap, clk->offset, &reg);
  117. /* Handle pre-divider first */
  118. if (prate != AC100_RTC_32K_RATE) {
  119. div = (reg >> AC100_CLKOUT_PRE_DIV_SHIFT) &
  120. ((1 << AC100_CLKOUT_PRE_DIV_WIDTH) - 1);
  121. prate = divider_recalc_rate(hw, prate, div,
  122. ac100_clkout_prediv, 0,
  123. AC100_CLKOUT_PRE_DIV_WIDTH);
  124. }
  125. div = (reg >> AC100_CLKOUT_DIV_SHIFT) &
  126. (BIT(AC100_CLKOUT_DIV_WIDTH) - 1);
  127. return divider_recalc_rate(hw, prate, div, NULL,
  128. CLK_DIVIDER_POWER_OF_TWO,
  129. AC100_CLKOUT_DIV_WIDTH);
  130. }
  131. static long ac100_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  132. unsigned long prate)
  133. {
  134. unsigned long best_rate = 0, tmp_rate, tmp_prate;
  135. int i;
  136. if (prate == AC100_RTC_32K_RATE)
  137. return divider_round_rate(hw, rate, &prate, NULL,
  138. AC100_CLKOUT_DIV_WIDTH,
  139. CLK_DIVIDER_POWER_OF_TWO);
  140. for (i = 0; ac100_clkout_prediv[i].div; i++) {
  141. tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[i].val);
  142. tmp_rate = divider_round_rate(hw, rate, &tmp_prate, NULL,
  143. AC100_CLKOUT_DIV_WIDTH,
  144. CLK_DIVIDER_POWER_OF_TWO);
  145. if (tmp_rate > rate)
  146. continue;
  147. if (rate - tmp_rate < best_rate - tmp_rate)
  148. best_rate = tmp_rate;
  149. }
  150. return best_rate;
  151. }
  152. static int ac100_clkout_determine_rate(struct clk_hw *hw,
  153. struct clk_rate_request *req)
  154. {
  155. struct clk_hw *best_parent;
  156. unsigned long best = 0;
  157. int i, num_parents = clk_hw_get_num_parents(hw);
  158. for (i = 0; i < num_parents; i++) {
  159. struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
  160. unsigned long tmp, prate;
  161. /*
  162. * The clock has two parents, one is a fixed clock which is
  163. * internally registered by the ac100 driver. The other parent
  164. * is a clock from the codec side of the chip, which we
  165. * properly declare and reference in the devicetree and is
  166. * not implemented in any driver right now.
  167. * If the clock core looks for the parent of that second
  168. * missing clock, it can't find one that is registered and
  169. * returns NULL.
  170. * So we end up in a situation where clk_hw_get_num_parents
  171. * returns the amount of clocks we can be parented to, but
  172. * clk_hw_get_parent_by_index will not return the orphan
  173. * clocks.
  174. * Thus we need to check if the parent exists before
  175. * we get the parent rate, so we could use the RTC
  176. * without waiting for the codec to be supported.
  177. */
  178. if (!parent)
  179. continue;
  180. prate = clk_hw_get_rate(parent);
  181. tmp = ac100_clkout_round_rate(hw, req->rate, prate);
  182. if (tmp > req->rate)
  183. continue;
  184. if (req->rate - tmp < req->rate - best) {
  185. best = tmp;
  186. best_parent = parent;
  187. }
  188. }
  189. if (!best)
  190. return -EINVAL;
  191. req->best_parent_hw = best_parent;
  192. req->best_parent_rate = best;
  193. req->rate = best;
  194. return 0;
  195. }
  196. static int ac100_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  197. unsigned long prate)
  198. {
  199. struct ac100_clkout *clk = to_ac100_clkout(hw);
  200. int div = 0, pre_div = 0;
  201. do {
  202. div = divider_get_val(rate * ac100_clkout_prediv[pre_div].div,
  203. prate, NULL, AC100_CLKOUT_DIV_WIDTH,
  204. CLK_DIVIDER_POWER_OF_TWO);
  205. if (div >= 0)
  206. break;
  207. } while (prate != AC100_RTC_32K_RATE &&
  208. ac100_clkout_prediv[++pre_div].div);
  209. if (div < 0)
  210. return div;
  211. pre_div = ac100_clkout_prediv[pre_div].val;
  212. regmap_update_bits(clk->regmap, clk->offset,
  213. ((1 << AC100_CLKOUT_DIV_WIDTH) - 1) << AC100_CLKOUT_DIV_SHIFT |
  214. ((1 << AC100_CLKOUT_PRE_DIV_WIDTH) - 1) << AC100_CLKOUT_PRE_DIV_SHIFT,
  215. (div - 1) << AC100_CLKOUT_DIV_SHIFT |
  216. (pre_div - 1) << AC100_CLKOUT_PRE_DIV_SHIFT);
  217. return 0;
  218. }
  219. static int ac100_clkout_prepare(struct clk_hw *hw)
  220. {
  221. struct ac100_clkout *clk = to_ac100_clkout(hw);
  222. return regmap_update_bits(clk->regmap, clk->offset, AC100_CLKOUT_EN,
  223. AC100_CLKOUT_EN);
  224. }
  225. static void ac100_clkout_unprepare(struct clk_hw *hw)
  226. {
  227. struct ac100_clkout *clk = to_ac100_clkout(hw);
  228. regmap_update_bits(clk->regmap, clk->offset, AC100_CLKOUT_EN, 0);
  229. }
  230. static int ac100_clkout_is_prepared(struct clk_hw *hw)
  231. {
  232. struct ac100_clkout *clk = to_ac100_clkout(hw);
  233. unsigned int reg;
  234. regmap_read(clk->regmap, clk->offset, &reg);
  235. return reg & AC100_CLKOUT_EN;
  236. }
  237. static u8 ac100_clkout_get_parent(struct clk_hw *hw)
  238. {
  239. struct ac100_clkout *clk = to_ac100_clkout(hw);
  240. unsigned int reg;
  241. regmap_read(clk->regmap, clk->offset, &reg);
  242. return (reg >> AC100_CLKOUT_MUX_SHIFT) & 0x1;
  243. }
  244. static int ac100_clkout_set_parent(struct clk_hw *hw, u8 index)
  245. {
  246. struct ac100_clkout *clk = to_ac100_clkout(hw);
  247. return regmap_update_bits(clk->regmap, clk->offset,
  248. BIT(AC100_CLKOUT_MUX_SHIFT),
  249. index ? BIT(AC100_CLKOUT_MUX_SHIFT) : 0);
  250. }
  251. static const struct clk_ops ac100_clkout_ops = {
  252. .prepare = ac100_clkout_prepare,
  253. .unprepare = ac100_clkout_unprepare,
  254. .is_prepared = ac100_clkout_is_prepared,
  255. .recalc_rate = ac100_clkout_recalc_rate,
  256. .determine_rate = ac100_clkout_determine_rate,
  257. .get_parent = ac100_clkout_get_parent,
  258. .set_parent = ac100_clkout_set_parent,
  259. .set_rate = ac100_clkout_set_rate,
  260. };
  261. static int ac100_rtc_register_clks(struct ac100_rtc_dev *chip)
  262. {
  263. struct device_node *np = chip->dev->of_node;
  264. const char *parents[2] = {AC100_RTC_32K_NAME};
  265. int i, ret;
  266. chip->clk_data = devm_kzalloc(chip->dev,
  267. struct_size(chip->clk_data, hws,
  268. AC100_CLKOUT_NUM),
  269. GFP_KERNEL);
  270. if (!chip->clk_data)
  271. return -ENOMEM;
  272. chip->rtc_32k_clk = clk_hw_register_fixed_rate(chip->dev,
  273. AC100_RTC_32K_NAME,
  274. NULL, 0,
  275. AC100_RTC_32K_RATE);
  276. if (IS_ERR(chip->rtc_32k_clk)) {
  277. ret = PTR_ERR(chip->rtc_32k_clk);
  278. dev_err(chip->dev, "Failed to register RTC-32k clock: %d\n",
  279. ret);
  280. return ret;
  281. }
  282. parents[1] = of_clk_get_parent_name(np, 0);
  283. if (!parents[1]) {
  284. dev_err(chip->dev, "Failed to get ADDA 4M clock\n");
  285. return -EINVAL;
  286. }
  287. for (i = 0; i < AC100_CLKOUT_NUM; i++) {
  288. struct ac100_clkout *clk = &chip->clks[i];
  289. struct clk_init_data init = {
  290. .name = ac100_clkout_names[i],
  291. .ops = &ac100_clkout_ops,
  292. .parent_names = parents,
  293. .num_parents = ARRAY_SIZE(parents),
  294. .flags = 0,
  295. };
  296. of_property_read_string_index(np, "clock-output-names",
  297. i, &init.name);
  298. clk->regmap = chip->regmap;
  299. clk->offset = AC100_CLKOUT_CTRL1 + i;
  300. clk->hw.init = &init;
  301. ret = devm_clk_hw_register(chip->dev, &clk->hw);
  302. if (ret) {
  303. dev_err(chip->dev, "Failed to register clk '%s': %d\n",
  304. init.name, ret);
  305. goto err_unregister_rtc_32k;
  306. }
  307. chip->clk_data->hws[i] = &clk->hw;
  308. }
  309. chip->clk_data->num = i;
  310. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, chip->clk_data);
  311. if (ret)
  312. goto err_unregister_rtc_32k;
  313. return 0;
  314. err_unregister_rtc_32k:
  315. clk_unregister_fixed_rate(chip->rtc_32k_clk->clk);
  316. return ret;
  317. }
  318. static void ac100_rtc_unregister_clks(struct ac100_rtc_dev *chip)
  319. {
  320. of_clk_del_provider(chip->dev->of_node);
  321. clk_unregister_fixed_rate(chip->rtc_32k_clk->clk);
  322. }
  323. /**
  324. * RTC related bits
  325. */
  326. static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc_tm)
  327. {
  328. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  329. struct regmap *regmap = chip->regmap;
  330. u16 reg[7];
  331. int ret;
  332. ret = regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7);
  333. if (ret)
  334. return ret;
  335. rtc_tm->tm_sec = bcd2bin(reg[0] & AC100_RTC_SEC_MASK);
  336. rtc_tm->tm_min = bcd2bin(reg[1] & AC100_RTC_MIN_MASK);
  337. rtc_tm->tm_hour = bcd2bin(reg[2] & AC100_RTC_HOU_MASK);
  338. rtc_tm->tm_wday = bcd2bin(reg[3] & AC100_RTC_WEE_MASK);
  339. rtc_tm->tm_mday = bcd2bin(reg[4] & AC100_RTC_DAY_MASK);
  340. rtc_tm->tm_mon = bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1;
  341. rtc_tm->tm_year = bcd2bin(reg[6] & AC100_RTC_YEA_MASK) +
  342. AC100_YEAR_OFF;
  343. return 0;
  344. }
  345. static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
  346. {
  347. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  348. struct regmap *regmap = chip->regmap;
  349. int year;
  350. u16 reg[8];
  351. /* our RTC has a limited year range... */
  352. year = rtc_tm->tm_year - AC100_YEAR_OFF;
  353. if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
  354. dev_err(dev, "rtc only supports year in range %d - %d\n",
  355. AC100_YEAR_MIN, AC100_YEAR_MAX);
  356. return -EINVAL;
  357. }
  358. /* convert to BCD */
  359. reg[0] = bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK;
  360. reg[1] = bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK;
  361. reg[2] = bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK;
  362. reg[3] = bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK;
  363. reg[4] = bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK;
  364. reg[5] = bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK;
  365. reg[6] = bin2bcd(year) & AC100_RTC_YEA_MASK;
  366. /* trigger write */
  367. reg[7] = AC100_RTC_UPD_TRIGGER;
  368. /* Is it a leap year? */
  369. if (is_leap_year(year + AC100_YEAR_OFF + 1900))
  370. reg[6] |= AC100_RTC_YEA_LEAP;
  371. return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8);
  372. }
  373. static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
  374. {
  375. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  376. struct regmap *regmap = chip->regmap;
  377. unsigned int val;
  378. val = en ? AC100_ALM_INT_ENABLE : 0;
  379. return regmap_write(regmap, AC100_ALM_INT_ENA, val);
  380. }
  381. static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  382. {
  383. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  384. struct regmap *regmap = chip->regmap;
  385. struct rtc_time *alrm_tm = &alrm->time;
  386. u16 reg[7];
  387. unsigned int val;
  388. int ret;
  389. ret = regmap_read(regmap, AC100_ALM_INT_ENA, &val);
  390. if (ret)
  391. return ret;
  392. alrm->enabled = !!(val & AC100_ALM_INT_ENABLE);
  393. ret = regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7);
  394. if (ret)
  395. return ret;
  396. alrm_tm->tm_sec = bcd2bin(reg[0] & AC100_ALM_SEC_MASK);
  397. alrm_tm->tm_min = bcd2bin(reg[1] & AC100_ALM_MIN_MASK);
  398. alrm_tm->tm_hour = bcd2bin(reg[2] & AC100_ALM_HOU_MASK);
  399. alrm_tm->tm_wday = bcd2bin(reg[3] & AC100_ALM_WEE_MASK);
  400. alrm_tm->tm_mday = bcd2bin(reg[4] & AC100_ALM_DAY_MASK);
  401. alrm_tm->tm_mon = bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1;
  402. alrm_tm->tm_year = bcd2bin(reg[6] & AC100_ALM_YEA_MASK) +
  403. AC100_YEAR_OFF;
  404. return 0;
  405. }
  406. static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  407. {
  408. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  409. struct regmap *regmap = chip->regmap;
  410. struct rtc_time *alrm_tm = &alrm->time;
  411. u16 reg[8];
  412. int year;
  413. int ret;
  414. /* our alarm has a limited year range... */
  415. year = alrm_tm->tm_year - AC100_YEAR_OFF;
  416. if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
  417. dev_err(dev, "alarm only supports year in range %d - %d\n",
  418. AC100_YEAR_MIN, AC100_YEAR_MAX);
  419. return -EINVAL;
  420. }
  421. /* convert to BCD */
  422. reg[0] = (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) |
  423. AC100_ALM_ENABLE_FLAG;
  424. reg[1] = (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) |
  425. AC100_ALM_ENABLE_FLAG;
  426. reg[2] = (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) |
  427. AC100_ALM_ENABLE_FLAG;
  428. /* Do not enable weekday alarm */
  429. reg[3] = bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK;
  430. reg[4] = (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) |
  431. AC100_ALM_ENABLE_FLAG;
  432. reg[5] = (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) |
  433. AC100_ALM_ENABLE_FLAG;
  434. reg[6] = (bin2bcd(year) & AC100_ALM_YEA_MASK) |
  435. AC100_ALM_ENABLE_FLAG;
  436. /* trigger write */
  437. reg[7] = AC100_ALM_UPD_TRIGGER;
  438. ret = regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8);
  439. if (ret)
  440. return ret;
  441. return ac100_rtc_alarm_irq_enable(dev, alrm->enabled);
  442. }
  443. static irqreturn_t ac100_rtc_irq(int irq, void *data)
  444. {
  445. struct ac100_rtc_dev *chip = data;
  446. struct regmap *regmap = chip->regmap;
  447. unsigned int val = 0;
  448. int ret;
  449. mutex_lock(&chip->rtc->ops_lock);
  450. /* read status */
  451. ret = regmap_read(regmap, AC100_ALM_INT_STA, &val);
  452. if (ret)
  453. goto out;
  454. if (val & AC100_ALM_INT_ENABLE) {
  455. /* signal rtc framework */
  456. rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
  457. /* clear status */
  458. ret = regmap_write(regmap, AC100_ALM_INT_STA, val);
  459. if (ret)
  460. goto out;
  461. /* disable interrupt */
  462. ret = ac100_rtc_alarm_irq_enable(chip->dev, 0);
  463. if (ret)
  464. goto out;
  465. }
  466. out:
  467. mutex_unlock(&chip->rtc->ops_lock);
  468. return IRQ_HANDLED;
  469. }
  470. static const struct rtc_class_ops ac100_rtc_ops = {
  471. .read_time = ac100_rtc_get_time,
  472. .set_time = ac100_rtc_set_time,
  473. .read_alarm = ac100_rtc_get_alarm,
  474. .set_alarm = ac100_rtc_set_alarm,
  475. .alarm_irq_enable = ac100_rtc_alarm_irq_enable,
  476. };
  477. static int ac100_rtc_probe(struct platform_device *pdev)
  478. {
  479. struct ac100_dev *ac100 = dev_get_drvdata(pdev->dev.parent);
  480. struct ac100_rtc_dev *chip;
  481. int ret;
  482. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  483. if (!chip)
  484. return -ENOMEM;
  485. platform_set_drvdata(pdev, chip);
  486. chip->dev = &pdev->dev;
  487. chip->regmap = ac100->regmap;
  488. chip->irq = platform_get_irq(pdev, 0);
  489. if (chip->irq < 0) {
  490. dev_err(&pdev->dev, "No IRQ resource\n");
  491. return chip->irq;
  492. }
  493. chip->rtc = devm_rtc_allocate_device(&pdev->dev);
  494. if (IS_ERR(chip->rtc))
  495. return PTR_ERR(chip->rtc);
  496. chip->rtc->ops = &ac100_rtc_ops;
  497. ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
  498. ac100_rtc_irq,
  499. IRQF_SHARED | IRQF_ONESHOT,
  500. dev_name(&pdev->dev), chip);
  501. if (ret) {
  502. dev_err(&pdev->dev, "Could not request IRQ\n");
  503. return ret;
  504. }
  505. /* always use 24 hour mode */
  506. regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR,
  507. AC100_RTC_CTRL_24HOUR);
  508. /* disable counter alarm interrupt */
  509. regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0);
  510. /* clear counter alarm pending interrupts */
  511. regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE);
  512. ret = ac100_rtc_register_clks(chip);
  513. if (ret)
  514. return ret;
  515. ret = rtc_register_device(chip->rtc);
  516. if (ret) {
  517. dev_err(&pdev->dev, "unable to register device\n");
  518. return ret;
  519. }
  520. dev_info(&pdev->dev, "RTC enabled\n");
  521. return 0;
  522. }
  523. static int ac100_rtc_remove(struct platform_device *pdev)
  524. {
  525. struct ac100_rtc_dev *chip = platform_get_drvdata(pdev);
  526. ac100_rtc_unregister_clks(chip);
  527. return 0;
  528. }
  529. static const struct of_device_id ac100_rtc_match[] = {
  530. { .compatible = "x-powers,ac100-rtc" },
  531. { },
  532. };
  533. MODULE_DEVICE_TABLE(of, ac100_rtc_match);
  534. static struct platform_driver ac100_rtc_driver = {
  535. .probe = ac100_rtc_probe,
  536. .remove = ac100_rtc_remove,
  537. .driver = {
  538. .name = "ac100-rtc",
  539. .of_match_table = of_match_ptr(ac100_rtc_match),
  540. },
  541. };
  542. module_platform_driver(ac100_rtc_driver);
  543. MODULE_DESCRIPTION("X-Powers AC100 RTC driver");
  544. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  545. MODULE_LICENSE("GPL v2");