rtc-ac100.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. }
  124. div = (reg >> AC100_CLKOUT_DIV_SHIFT) &
  125. (BIT(AC100_CLKOUT_DIV_WIDTH) - 1);
  126. return divider_recalc_rate(hw, prate, div, NULL,
  127. CLK_DIVIDER_POWER_OF_TWO);
  128. }
  129. static long ac100_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
  130. unsigned long prate)
  131. {
  132. unsigned long best_rate = 0, tmp_rate, tmp_prate;
  133. int i;
  134. if (prate == AC100_RTC_32K_RATE)
  135. return divider_round_rate(hw, rate, &prate, NULL,
  136. AC100_CLKOUT_DIV_WIDTH,
  137. CLK_DIVIDER_POWER_OF_TWO);
  138. for (i = 0; ac100_clkout_prediv[i].div; i++) {
  139. tmp_prate = DIV_ROUND_UP(prate, ac100_clkout_prediv[i].val);
  140. tmp_rate = divider_round_rate(hw, rate, &tmp_prate, NULL,
  141. AC100_CLKOUT_DIV_WIDTH,
  142. CLK_DIVIDER_POWER_OF_TWO);
  143. if (tmp_rate > rate)
  144. continue;
  145. if (rate - tmp_rate < best_rate - tmp_rate)
  146. best_rate = tmp_rate;
  147. }
  148. return best_rate;
  149. }
  150. static int ac100_clkout_determine_rate(struct clk_hw *hw,
  151. struct clk_rate_request *req)
  152. {
  153. struct clk_hw *best_parent;
  154. unsigned long best = 0;
  155. int i, num_parents = clk_hw_get_num_parents(hw);
  156. for (i = 0; i < num_parents; i++) {
  157. struct clk_hw *parent = clk_hw_get_parent_by_index(hw, i);
  158. unsigned long tmp, prate = clk_hw_get_rate(parent);
  159. tmp = ac100_clkout_round_rate(hw, req->rate, prate);
  160. if (tmp > req->rate)
  161. continue;
  162. if (req->rate - tmp < req->rate - best) {
  163. best = tmp;
  164. best_parent = parent;
  165. }
  166. }
  167. if (!best)
  168. return -EINVAL;
  169. req->best_parent_hw = best_parent;
  170. req->best_parent_rate = best;
  171. req->rate = best;
  172. return 0;
  173. }
  174. static int ac100_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
  175. unsigned long prate)
  176. {
  177. struct ac100_clkout *clk = to_ac100_clkout(hw);
  178. int div = 0, pre_div = 0;
  179. do {
  180. div = divider_get_val(rate * ac100_clkout_prediv[pre_div].div,
  181. prate, NULL, AC100_CLKOUT_DIV_WIDTH,
  182. CLK_DIVIDER_POWER_OF_TWO);
  183. if (div >= 0)
  184. break;
  185. } while (prate != AC100_RTC_32K_RATE &&
  186. ac100_clkout_prediv[++pre_div].div);
  187. if (div < 0)
  188. return div;
  189. pre_div = ac100_clkout_prediv[pre_div].val;
  190. regmap_update_bits(clk->regmap, clk->offset,
  191. ((1 << AC100_CLKOUT_DIV_WIDTH) - 1) << AC100_CLKOUT_DIV_SHIFT |
  192. ((1 << AC100_CLKOUT_PRE_DIV_WIDTH) - 1) << AC100_CLKOUT_PRE_DIV_SHIFT,
  193. (div - 1) << AC100_CLKOUT_DIV_SHIFT |
  194. (pre_div - 1) << AC100_CLKOUT_PRE_DIV_SHIFT);
  195. return 0;
  196. }
  197. static int ac100_clkout_prepare(struct clk_hw *hw)
  198. {
  199. struct ac100_clkout *clk = to_ac100_clkout(hw);
  200. return regmap_update_bits(clk->regmap, clk->offset, AC100_CLKOUT_EN,
  201. AC100_CLKOUT_EN);
  202. }
  203. static void ac100_clkout_unprepare(struct clk_hw *hw)
  204. {
  205. struct ac100_clkout *clk = to_ac100_clkout(hw);
  206. regmap_update_bits(clk->regmap, clk->offset, AC100_CLKOUT_EN, 0);
  207. }
  208. static int ac100_clkout_is_prepared(struct clk_hw *hw)
  209. {
  210. struct ac100_clkout *clk = to_ac100_clkout(hw);
  211. unsigned int reg;
  212. regmap_read(clk->regmap, clk->offset, &reg);
  213. return reg & AC100_CLKOUT_EN;
  214. }
  215. static u8 ac100_clkout_get_parent(struct clk_hw *hw)
  216. {
  217. struct ac100_clkout *clk = to_ac100_clkout(hw);
  218. unsigned int reg;
  219. regmap_read(clk->regmap, clk->offset, &reg);
  220. return (reg >> AC100_CLKOUT_MUX_SHIFT) & 0x1;
  221. }
  222. static int ac100_clkout_set_parent(struct clk_hw *hw, u8 index)
  223. {
  224. struct ac100_clkout *clk = to_ac100_clkout(hw);
  225. return regmap_update_bits(clk->regmap, clk->offset,
  226. BIT(AC100_CLKOUT_MUX_SHIFT),
  227. index ? BIT(AC100_CLKOUT_MUX_SHIFT) : 0);
  228. }
  229. static const struct clk_ops ac100_clkout_ops = {
  230. .prepare = ac100_clkout_prepare,
  231. .unprepare = ac100_clkout_unprepare,
  232. .is_prepared = ac100_clkout_is_prepared,
  233. .recalc_rate = ac100_clkout_recalc_rate,
  234. .determine_rate = ac100_clkout_determine_rate,
  235. .get_parent = ac100_clkout_get_parent,
  236. .set_parent = ac100_clkout_set_parent,
  237. .set_rate = ac100_clkout_set_rate,
  238. };
  239. static int ac100_rtc_register_clks(struct ac100_rtc_dev *chip)
  240. {
  241. struct device_node *np = chip->dev->of_node;
  242. const char *parents[2] = {AC100_RTC_32K_NAME};
  243. int i, ret;
  244. chip->clk_data = devm_kzalloc(chip->dev, sizeof(*chip->clk_data) +
  245. sizeof(*chip->clk_data->hws) *
  246. AC100_CLKOUT_NUM,
  247. GFP_KERNEL);
  248. if (!chip->clk_data)
  249. return -ENOMEM;
  250. chip->rtc_32k_clk = clk_hw_register_fixed_rate(chip->dev,
  251. AC100_RTC_32K_NAME,
  252. NULL, 0,
  253. AC100_RTC_32K_RATE);
  254. if (IS_ERR(chip->rtc_32k_clk)) {
  255. ret = PTR_ERR(chip->rtc_32k_clk);
  256. dev_err(chip->dev, "Failed to register RTC-32k clock: %d\n",
  257. ret);
  258. return ret;
  259. }
  260. parents[1] = of_clk_get_parent_name(np, 0);
  261. if (!parents[1]) {
  262. dev_err(chip->dev, "Failed to get ADDA 4M clock\n");
  263. return -EINVAL;
  264. }
  265. for (i = 0; i < AC100_CLKOUT_NUM; i++) {
  266. struct ac100_clkout *clk = &chip->clks[i];
  267. struct clk_init_data init = {
  268. .name = ac100_clkout_names[i],
  269. .ops = &ac100_clkout_ops,
  270. .parent_names = parents,
  271. .num_parents = ARRAY_SIZE(parents),
  272. .flags = 0,
  273. };
  274. of_property_read_string_index(np, "clock-output-names",
  275. i, &init.name);
  276. clk->regmap = chip->regmap;
  277. clk->offset = AC100_CLKOUT_CTRL1 + i;
  278. clk->hw.init = &init;
  279. ret = devm_clk_hw_register(chip->dev, &clk->hw);
  280. if (ret) {
  281. dev_err(chip->dev, "Failed to register clk '%s': %d\n",
  282. init.name, ret);
  283. goto err_unregister_rtc_32k;
  284. }
  285. chip->clk_data->hws[i] = &clk->hw;
  286. }
  287. chip->clk_data->num = i;
  288. ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, chip->clk_data);
  289. if (ret)
  290. goto err_unregister_rtc_32k;
  291. return 0;
  292. err_unregister_rtc_32k:
  293. clk_unregister_fixed_rate(chip->rtc_32k_clk->clk);
  294. return ret;
  295. }
  296. static void ac100_rtc_unregister_clks(struct ac100_rtc_dev *chip)
  297. {
  298. of_clk_del_provider(chip->dev->of_node);
  299. clk_unregister_fixed_rate(chip->rtc_32k_clk->clk);
  300. }
  301. /**
  302. * RTC related bits
  303. */
  304. static int ac100_rtc_get_time(struct device *dev, struct rtc_time *rtc_tm)
  305. {
  306. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  307. struct regmap *regmap = chip->regmap;
  308. u16 reg[7];
  309. int ret;
  310. ret = regmap_bulk_read(regmap, AC100_RTC_SEC, reg, 7);
  311. if (ret)
  312. return ret;
  313. rtc_tm->tm_sec = bcd2bin(reg[0] & AC100_RTC_SEC_MASK);
  314. rtc_tm->tm_min = bcd2bin(reg[1] & AC100_RTC_MIN_MASK);
  315. rtc_tm->tm_hour = bcd2bin(reg[2] & AC100_RTC_HOU_MASK);
  316. rtc_tm->tm_wday = bcd2bin(reg[3] & AC100_RTC_WEE_MASK);
  317. rtc_tm->tm_mday = bcd2bin(reg[4] & AC100_RTC_DAY_MASK);
  318. rtc_tm->tm_mon = bcd2bin(reg[5] & AC100_RTC_MON_MASK) - 1;
  319. rtc_tm->tm_year = bcd2bin(reg[6] & AC100_RTC_YEA_MASK) +
  320. AC100_YEAR_OFF;
  321. return rtc_valid_tm(rtc_tm);
  322. }
  323. static int ac100_rtc_set_time(struct device *dev, struct rtc_time *rtc_tm)
  324. {
  325. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  326. struct regmap *regmap = chip->regmap;
  327. int year;
  328. u16 reg[8];
  329. /* our RTC has a limited year range... */
  330. year = rtc_tm->tm_year - AC100_YEAR_OFF;
  331. if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
  332. dev_err(dev, "rtc only supports year in range %d - %d\n",
  333. AC100_YEAR_MIN, AC100_YEAR_MAX);
  334. return -EINVAL;
  335. }
  336. /* convert to BCD */
  337. reg[0] = bin2bcd(rtc_tm->tm_sec) & AC100_RTC_SEC_MASK;
  338. reg[1] = bin2bcd(rtc_tm->tm_min) & AC100_RTC_MIN_MASK;
  339. reg[2] = bin2bcd(rtc_tm->tm_hour) & AC100_RTC_HOU_MASK;
  340. reg[3] = bin2bcd(rtc_tm->tm_wday) & AC100_RTC_WEE_MASK;
  341. reg[4] = bin2bcd(rtc_tm->tm_mday) & AC100_RTC_DAY_MASK;
  342. reg[5] = bin2bcd(rtc_tm->tm_mon + 1) & AC100_RTC_MON_MASK;
  343. reg[6] = bin2bcd(year) & AC100_RTC_YEA_MASK;
  344. /* trigger write */
  345. reg[7] = AC100_RTC_UPD_TRIGGER;
  346. /* Is it a leap year? */
  347. if (is_leap_year(year + AC100_YEAR_OFF + 1900))
  348. reg[6] |= AC100_RTC_YEA_LEAP;
  349. return regmap_bulk_write(regmap, AC100_RTC_SEC, reg, 8);
  350. }
  351. static int ac100_rtc_alarm_irq_enable(struct device *dev, unsigned int en)
  352. {
  353. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  354. struct regmap *regmap = chip->regmap;
  355. unsigned int val;
  356. val = en ? AC100_ALM_INT_ENABLE : 0;
  357. return regmap_write(regmap, AC100_ALM_INT_ENA, val);
  358. }
  359. static int ac100_rtc_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  360. {
  361. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  362. struct regmap *regmap = chip->regmap;
  363. struct rtc_time *alrm_tm = &alrm->time;
  364. u16 reg[7];
  365. unsigned int val;
  366. int ret;
  367. ret = regmap_read(regmap, AC100_ALM_INT_ENA, &val);
  368. if (ret)
  369. return ret;
  370. alrm->enabled = !!(val & AC100_ALM_INT_ENABLE);
  371. ret = regmap_bulk_read(regmap, AC100_ALM_SEC, reg, 7);
  372. if (ret)
  373. return ret;
  374. alrm_tm->tm_sec = bcd2bin(reg[0] & AC100_ALM_SEC_MASK);
  375. alrm_tm->tm_min = bcd2bin(reg[1] & AC100_ALM_MIN_MASK);
  376. alrm_tm->tm_hour = bcd2bin(reg[2] & AC100_ALM_HOU_MASK);
  377. alrm_tm->tm_wday = bcd2bin(reg[3] & AC100_ALM_WEE_MASK);
  378. alrm_tm->tm_mday = bcd2bin(reg[4] & AC100_ALM_DAY_MASK);
  379. alrm_tm->tm_mon = bcd2bin(reg[5] & AC100_ALM_MON_MASK) - 1;
  380. alrm_tm->tm_year = bcd2bin(reg[6] & AC100_ALM_YEA_MASK) +
  381. AC100_YEAR_OFF;
  382. return 0;
  383. }
  384. static int ac100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  385. {
  386. struct ac100_rtc_dev *chip = dev_get_drvdata(dev);
  387. struct regmap *regmap = chip->regmap;
  388. struct rtc_time *alrm_tm = &alrm->time;
  389. u16 reg[8];
  390. int year;
  391. int ret;
  392. /* our alarm has a limited year range... */
  393. year = alrm_tm->tm_year - AC100_YEAR_OFF;
  394. if (year < 0 || year > (AC100_YEAR_MAX - 1900)) {
  395. dev_err(dev, "alarm only supports year in range %d - %d\n",
  396. AC100_YEAR_MIN, AC100_YEAR_MAX);
  397. return -EINVAL;
  398. }
  399. /* convert to BCD */
  400. reg[0] = (bin2bcd(alrm_tm->tm_sec) & AC100_ALM_SEC_MASK) |
  401. AC100_ALM_ENABLE_FLAG;
  402. reg[1] = (bin2bcd(alrm_tm->tm_min) & AC100_ALM_MIN_MASK) |
  403. AC100_ALM_ENABLE_FLAG;
  404. reg[2] = (bin2bcd(alrm_tm->tm_hour) & AC100_ALM_HOU_MASK) |
  405. AC100_ALM_ENABLE_FLAG;
  406. /* Do not enable weekday alarm */
  407. reg[3] = bin2bcd(alrm_tm->tm_wday) & AC100_ALM_WEE_MASK;
  408. reg[4] = (bin2bcd(alrm_tm->tm_mday) & AC100_ALM_DAY_MASK) |
  409. AC100_ALM_ENABLE_FLAG;
  410. reg[5] = (bin2bcd(alrm_tm->tm_mon + 1) & AC100_ALM_MON_MASK) |
  411. AC100_ALM_ENABLE_FLAG;
  412. reg[6] = (bin2bcd(year) & AC100_ALM_YEA_MASK) |
  413. AC100_ALM_ENABLE_FLAG;
  414. /* trigger write */
  415. reg[7] = AC100_ALM_UPD_TRIGGER;
  416. ret = regmap_bulk_write(regmap, AC100_ALM_SEC, reg, 8);
  417. if (ret)
  418. return ret;
  419. return ac100_rtc_alarm_irq_enable(dev, alrm->enabled);
  420. }
  421. static irqreturn_t ac100_rtc_irq(int irq, void *data)
  422. {
  423. struct ac100_rtc_dev *chip = data;
  424. struct regmap *regmap = chip->regmap;
  425. unsigned int val = 0;
  426. int ret;
  427. mutex_lock(&chip->rtc->ops_lock);
  428. /* read status */
  429. ret = regmap_read(regmap, AC100_ALM_INT_STA, &val);
  430. if (ret)
  431. goto out;
  432. if (val & AC100_ALM_INT_ENABLE) {
  433. /* signal rtc framework */
  434. rtc_update_irq(chip->rtc, 1, RTC_AF | RTC_IRQF);
  435. /* clear status */
  436. ret = regmap_write(regmap, AC100_ALM_INT_STA, val);
  437. if (ret)
  438. goto out;
  439. /* disable interrupt */
  440. ret = ac100_rtc_alarm_irq_enable(chip->dev, 0);
  441. if (ret)
  442. goto out;
  443. }
  444. out:
  445. mutex_unlock(&chip->rtc->ops_lock);
  446. return IRQ_HANDLED;
  447. }
  448. static const struct rtc_class_ops ac100_rtc_ops = {
  449. .read_time = ac100_rtc_get_time,
  450. .set_time = ac100_rtc_set_time,
  451. .read_alarm = ac100_rtc_get_alarm,
  452. .set_alarm = ac100_rtc_set_alarm,
  453. .alarm_irq_enable = ac100_rtc_alarm_irq_enable,
  454. };
  455. static int ac100_rtc_probe(struct platform_device *pdev)
  456. {
  457. struct ac100_dev *ac100 = dev_get_drvdata(pdev->dev.parent);
  458. struct ac100_rtc_dev *chip;
  459. int ret;
  460. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  461. if (!chip)
  462. return -ENOMEM;
  463. platform_set_drvdata(pdev, chip);
  464. chip->dev = &pdev->dev;
  465. chip->regmap = ac100->regmap;
  466. chip->irq = platform_get_irq(pdev, 0);
  467. if (chip->irq < 0) {
  468. dev_err(&pdev->dev, "No IRQ resource\n");
  469. return chip->irq;
  470. }
  471. ret = devm_request_threaded_irq(&pdev->dev, chip->irq, NULL,
  472. ac100_rtc_irq,
  473. IRQF_SHARED | IRQF_ONESHOT,
  474. dev_name(&pdev->dev), chip);
  475. if (ret) {
  476. dev_err(&pdev->dev, "Could not request IRQ\n");
  477. return ret;
  478. }
  479. /* always use 24 hour mode */
  480. regmap_write_bits(chip->regmap, AC100_RTC_CTRL, AC100_RTC_CTRL_24HOUR,
  481. AC100_RTC_CTRL_24HOUR);
  482. /* disable counter alarm interrupt */
  483. regmap_write(chip->regmap, AC100_ALM_INT_ENA, 0);
  484. /* clear counter alarm pending interrupts */
  485. regmap_write(chip->regmap, AC100_ALM_INT_STA, AC100_ALM_INT_ENABLE);
  486. chip->rtc = devm_rtc_device_register(&pdev->dev, "rtc-ac100",
  487. &ac100_rtc_ops, THIS_MODULE);
  488. if (IS_ERR(chip->rtc)) {
  489. dev_err(&pdev->dev, "unable to register device\n");
  490. return PTR_ERR(chip->rtc);
  491. }
  492. ret = ac100_rtc_register_clks(chip);
  493. if (ret)
  494. return ret;
  495. dev_info(&pdev->dev, "RTC enabled\n");
  496. return 0;
  497. }
  498. static int ac100_rtc_remove(struct platform_device *pdev)
  499. {
  500. struct ac100_rtc_dev *chip = platform_get_drvdata(pdev);
  501. ac100_rtc_unregister_clks(chip);
  502. return 0;
  503. }
  504. static const struct of_device_id ac100_rtc_match[] = {
  505. { .compatible = "x-powers,ac100-rtc" },
  506. { },
  507. };
  508. MODULE_DEVICE_TABLE(of, ac100_rtc_match);
  509. static struct platform_driver ac100_rtc_driver = {
  510. .probe = ac100_rtc_probe,
  511. .remove = ac100_rtc_remove,
  512. .driver = {
  513. .name = "ac100-rtc",
  514. .of_match_table = of_match_ptr(ac100_rtc_match),
  515. },
  516. };
  517. module_platform_driver(ac100_rtc_driver);
  518. MODULE_DESCRIPTION("X-Powers AC100 RTC driver");
  519. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  520. MODULE_LICENSE("GPL v2");