rtc-s5m.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (c) 2013-2014 Samsung Electronics Co., Ltd
  3. * http://www.samsung.com
  4. *
  5. * Copyright (C) 2013 Google, Inc
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/i2c.h>
  20. #include <linux/bcd.h>
  21. #include <linux/regmap.h>
  22. #include <linux/rtc.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/mfd/samsung/core.h>
  25. #include <linux/mfd/samsung/irq.h>
  26. #include <linux/mfd/samsung/rtc.h>
  27. #include <linux/mfd/samsung/s2mps14.h>
  28. /*
  29. * Maximum number of retries for checking changes in UDR field
  30. * of S5M_RTC_UDR_CON register (to limit possible endless loop).
  31. *
  32. * After writing to RTC registers (setting time or alarm) read the UDR field
  33. * in S5M_RTC_UDR_CON register. UDR is auto-cleared when data have
  34. * been transferred.
  35. */
  36. #define UDR_READ_RETRY_CNT 5
  37. /*
  38. * Registers used by the driver which are different between chipsets.
  39. *
  40. * Operations like read time and write alarm/time require updating
  41. * specific fields in UDR register. These fields usually are auto-cleared
  42. * (with some exceptions).
  43. *
  44. * Table of operations per device:
  45. *
  46. * Device | Write time | Read time | Write alarm
  47. * =================================================
  48. * S5M8767 | UDR + TIME | | UDR
  49. * S2MPS11/14 | WUDR | RUDR | WUDR + RUDR
  50. * S2MPS13 | WUDR | RUDR | WUDR + AUDR
  51. * S2MPS15 | WUDR | RUDR | AUDR
  52. */
  53. struct s5m_rtc_reg_config {
  54. /* Number of registers used for setting time/alarm0/alarm1 */
  55. unsigned int regs_count;
  56. /* First register for time, seconds */
  57. unsigned int time;
  58. /* RTC control register */
  59. unsigned int ctrl;
  60. /* First register for alarm 0, seconds */
  61. unsigned int alarm0;
  62. /* First register for alarm 1, seconds */
  63. unsigned int alarm1;
  64. /*
  65. * Register for update flag (UDR). Typically setting UDR field to 1
  66. * will enable update of time or alarm register. Then it will be
  67. * auto-cleared after successful update.
  68. */
  69. unsigned int udr_update;
  70. /* Auto-cleared mask in UDR field for writing time and alarm */
  71. unsigned int autoclear_udr_mask;
  72. /*
  73. * Masks in UDR field for time and alarm operations.
  74. * The read time mask can be 0. Rest should not.
  75. */
  76. unsigned int read_time_udr_mask;
  77. unsigned int write_time_udr_mask;
  78. unsigned int write_alarm_udr_mask;
  79. };
  80. /* Register map for S5M8763 and S5M8767 */
  81. static const struct s5m_rtc_reg_config s5m_rtc_regs = {
  82. .regs_count = 8,
  83. .time = S5M_RTC_SEC,
  84. .ctrl = S5M_ALARM1_CONF,
  85. .alarm0 = S5M_ALARM0_SEC,
  86. .alarm1 = S5M_ALARM1_SEC,
  87. .udr_update = S5M_RTC_UDR_CON,
  88. .autoclear_udr_mask = S5M_RTC_UDR_MASK,
  89. .read_time_udr_mask = 0, /* Not needed */
  90. .write_time_udr_mask = S5M_RTC_UDR_MASK | S5M_RTC_TIME_EN_MASK,
  91. .write_alarm_udr_mask = S5M_RTC_UDR_MASK,
  92. };
  93. /* Register map for S2MPS13 */
  94. static const struct s5m_rtc_reg_config s2mps13_rtc_regs = {
  95. .regs_count = 7,
  96. .time = S2MPS_RTC_SEC,
  97. .ctrl = S2MPS_RTC_CTRL,
  98. .alarm0 = S2MPS_ALARM0_SEC,
  99. .alarm1 = S2MPS_ALARM1_SEC,
  100. .udr_update = S2MPS_RTC_UDR_CON,
  101. .autoclear_udr_mask = S2MPS_RTC_WUDR_MASK,
  102. .read_time_udr_mask = S2MPS_RTC_RUDR_MASK,
  103. .write_time_udr_mask = S2MPS_RTC_WUDR_MASK,
  104. .write_alarm_udr_mask = S2MPS_RTC_WUDR_MASK | S2MPS13_RTC_AUDR_MASK,
  105. };
  106. /* Register map for S2MPS11/14 */
  107. static const struct s5m_rtc_reg_config s2mps14_rtc_regs = {
  108. .regs_count = 7,
  109. .time = S2MPS_RTC_SEC,
  110. .ctrl = S2MPS_RTC_CTRL,
  111. .alarm0 = S2MPS_ALARM0_SEC,
  112. .alarm1 = S2MPS_ALARM1_SEC,
  113. .udr_update = S2MPS_RTC_UDR_CON,
  114. .autoclear_udr_mask = S2MPS_RTC_WUDR_MASK,
  115. .read_time_udr_mask = S2MPS_RTC_RUDR_MASK,
  116. .write_time_udr_mask = S2MPS_RTC_WUDR_MASK,
  117. .write_alarm_udr_mask = S2MPS_RTC_WUDR_MASK | S2MPS_RTC_RUDR_MASK,
  118. };
  119. /*
  120. * Register map for S2MPS15 - in comparison to S2MPS14 the WUDR and AUDR bits
  121. * are swapped.
  122. */
  123. static const struct s5m_rtc_reg_config s2mps15_rtc_regs = {
  124. .regs_count = 7,
  125. .time = S2MPS_RTC_SEC,
  126. .ctrl = S2MPS_RTC_CTRL,
  127. .alarm0 = S2MPS_ALARM0_SEC,
  128. .alarm1 = S2MPS_ALARM1_SEC,
  129. .udr_update = S2MPS_RTC_UDR_CON,
  130. .autoclear_udr_mask = S2MPS_RTC_WUDR_MASK,
  131. .read_time_udr_mask = S2MPS_RTC_RUDR_MASK,
  132. .write_time_udr_mask = S2MPS15_RTC_WUDR_MASK,
  133. .write_alarm_udr_mask = S2MPS15_RTC_AUDR_MASK,
  134. };
  135. struct s5m_rtc_info {
  136. struct device *dev;
  137. struct i2c_client *i2c;
  138. struct sec_pmic_dev *s5m87xx;
  139. struct regmap *regmap;
  140. struct rtc_device *rtc_dev;
  141. int irq;
  142. enum sec_device_type device_type;
  143. int rtc_24hr_mode;
  144. const struct s5m_rtc_reg_config *regs;
  145. };
  146. static const struct regmap_config s5m_rtc_regmap_config = {
  147. .reg_bits = 8,
  148. .val_bits = 8,
  149. .max_register = S5M_RTC_REG_MAX,
  150. };
  151. static const struct regmap_config s2mps14_rtc_regmap_config = {
  152. .reg_bits = 8,
  153. .val_bits = 8,
  154. .max_register = S2MPS_RTC_REG_MAX,
  155. };
  156. static void s5m8767_data_to_tm(u8 *data, struct rtc_time *tm,
  157. int rtc_24hr_mode)
  158. {
  159. tm->tm_sec = data[RTC_SEC] & 0x7f;
  160. tm->tm_min = data[RTC_MIN] & 0x7f;
  161. if (rtc_24hr_mode) {
  162. tm->tm_hour = data[RTC_HOUR] & 0x1f;
  163. } else {
  164. tm->tm_hour = data[RTC_HOUR] & 0x0f;
  165. if (data[RTC_HOUR] & HOUR_PM_MASK)
  166. tm->tm_hour += 12;
  167. }
  168. tm->tm_wday = ffs(data[RTC_WEEKDAY] & 0x7f);
  169. tm->tm_mday = data[RTC_DATE] & 0x1f;
  170. tm->tm_mon = (data[RTC_MONTH] & 0x0f) - 1;
  171. tm->tm_year = (data[RTC_YEAR1] & 0x7f) + 100;
  172. tm->tm_yday = 0;
  173. tm->tm_isdst = 0;
  174. }
  175. static int s5m8767_tm_to_data(struct rtc_time *tm, u8 *data)
  176. {
  177. data[RTC_SEC] = tm->tm_sec;
  178. data[RTC_MIN] = tm->tm_min;
  179. if (tm->tm_hour >= 12)
  180. data[RTC_HOUR] = tm->tm_hour | HOUR_PM_MASK;
  181. else
  182. data[RTC_HOUR] = tm->tm_hour & ~HOUR_PM_MASK;
  183. data[RTC_WEEKDAY] = 1 << tm->tm_wday;
  184. data[RTC_DATE] = tm->tm_mday;
  185. data[RTC_MONTH] = tm->tm_mon + 1;
  186. data[RTC_YEAR1] = tm->tm_year > 100 ? (tm->tm_year - 100) : 0;
  187. if (tm->tm_year < 100) {
  188. pr_err("RTC cannot handle the year %d\n",
  189. 1900 + tm->tm_year);
  190. return -EINVAL;
  191. } else {
  192. return 0;
  193. }
  194. }
  195. /*
  196. * Read RTC_UDR_CON register and wait till UDR field is cleared.
  197. * This indicates that time/alarm update ended.
  198. */
  199. static int s5m8767_wait_for_udr_update(struct s5m_rtc_info *info)
  200. {
  201. int ret, retry = UDR_READ_RETRY_CNT;
  202. unsigned int data;
  203. do {
  204. ret = regmap_read(info->regmap, info->regs->udr_update, &data);
  205. } while (--retry && (data & info->regs->autoclear_udr_mask) && !ret);
  206. if (!retry)
  207. dev_err(info->dev, "waiting for UDR update, reached max number of retries\n");
  208. return ret;
  209. }
  210. static int s5m_check_peding_alarm_interrupt(struct s5m_rtc_info *info,
  211. struct rtc_wkalrm *alarm)
  212. {
  213. int ret;
  214. unsigned int val;
  215. switch (info->device_type) {
  216. case S5M8767X:
  217. case S5M8763X:
  218. ret = regmap_read(info->regmap, S5M_RTC_STATUS, &val);
  219. val &= S5M_ALARM0_STATUS;
  220. break;
  221. case S2MPS15X:
  222. case S2MPS14X:
  223. case S2MPS13X:
  224. ret = regmap_read(info->s5m87xx->regmap_pmic, S2MPS14_REG_ST2,
  225. &val);
  226. val &= S2MPS_ALARM0_STATUS;
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. if (ret < 0)
  232. return ret;
  233. if (val)
  234. alarm->pending = 1;
  235. else
  236. alarm->pending = 0;
  237. return 0;
  238. }
  239. static int s5m8767_rtc_set_time_reg(struct s5m_rtc_info *info)
  240. {
  241. int ret;
  242. unsigned int data;
  243. ret = regmap_read(info->regmap, info->regs->udr_update, &data);
  244. if (ret < 0) {
  245. dev_err(info->dev, "failed to read update reg(%d)\n", ret);
  246. return ret;
  247. }
  248. data |= info->regs->write_time_udr_mask;
  249. ret = regmap_write(info->regmap, info->regs->udr_update, data);
  250. if (ret < 0) {
  251. dev_err(info->dev, "failed to write update reg(%d)\n", ret);
  252. return ret;
  253. }
  254. ret = s5m8767_wait_for_udr_update(info);
  255. return ret;
  256. }
  257. static int s5m8767_rtc_set_alarm_reg(struct s5m_rtc_info *info)
  258. {
  259. int ret;
  260. unsigned int data;
  261. ret = regmap_read(info->regmap, info->regs->udr_update, &data);
  262. if (ret < 0) {
  263. dev_err(info->dev, "%s: fail to read update reg(%d)\n",
  264. __func__, ret);
  265. return ret;
  266. }
  267. data |= info->regs->write_alarm_udr_mask;
  268. switch (info->device_type) {
  269. case S5M8763X:
  270. case S5M8767X:
  271. data &= ~S5M_RTC_TIME_EN_MASK;
  272. break;
  273. case S2MPS15X:
  274. case S2MPS14X:
  275. case S2MPS13X:
  276. /* No exceptions needed */
  277. break;
  278. default:
  279. return -EINVAL;
  280. }
  281. ret = regmap_write(info->regmap, info->regs->udr_update, data);
  282. if (ret < 0) {
  283. dev_err(info->dev, "%s: fail to write update reg(%d)\n",
  284. __func__, ret);
  285. return ret;
  286. }
  287. ret = s5m8767_wait_for_udr_update(info);
  288. /* On S2MPS13 the AUDR is not auto-cleared */
  289. if (info->device_type == S2MPS13X)
  290. regmap_update_bits(info->regmap, info->regs->udr_update,
  291. S2MPS13_RTC_AUDR_MASK, 0);
  292. return ret;
  293. }
  294. static void s5m8763_data_to_tm(u8 *data, struct rtc_time *tm)
  295. {
  296. tm->tm_sec = bcd2bin(data[RTC_SEC]);
  297. tm->tm_min = bcd2bin(data[RTC_MIN]);
  298. if (data[RTC_HOUR] & HOUR_12) {
  299. tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x1f);
  300. if (data[RTC_HOUR] & HOUR_PM)
  301. tm->tm_hour += 12;
  302. } else {
  303. tm->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3f);
  304. }
  305. tm->tm_wday = data[RTC_WEEKDAY] & 0x07;
  306. tm->tm_mday = bcd2bin(data[RTC_DATE]);
  307. tm->tm_mon = bcd2bin(data[RTC_MONTH]);
  308. tm->tm_year = bcd2bin(data[RTC_YEAR1]) + bcd2bin(data[RTC_YEAR2]) * 100;
  309. tm->tm_year -= 1900;
  310. }
  311. static void s5m8763_tm_to_data(struct rtc_time *tm, u8 *data)
  312. {
  313. data[RTC_SEC] = bin2bcd(tm->tm_sec);
  314. data[RTC_MIN] = bin2bcd(tm->tm_min);
  315. data[RTC_HOUR] = bin2bcd(tm->tm_hour);
  316. data[RTC_WEEKDAY] = tm->tm_wday;
  317. data[RTC_DATE] = bin2bcd(tm->tm_mday);
  318. data[RTC_MONTH] = bin2bcd(tm->tm_mon);
  319. data[RTC_YEAR1] = bin2bcd(tm->tm_year % 100);
  320. data[RTC_YEAR2] = bin2bcd((tm->tm_year + 1900) / 100);
  321. }
  322. static int s5m_rtc_read_time(struct device *dev, struct rtc_time *tm)
  323. {
  324. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  325. u8 data[info->regs->regs_count];
  326. int ret;
  327. if (info->regs->read_time_udr_mask) {
  328. ret = regmap_update_bits(info->regmap,
  329. info->regs->udr_update,
  330. info->regs->read_time_udr_mask,
  331. info->regs->read_time_udr_mask);
  332. if (ret) {
  333. dev_err(dev,
  334. "Failed to prepare registers for time reading: %d\n",
  335. ret);
  336. return ret;
  337. }
  338. }
  339. ret = regmap_bulk_read(info->regmap, info->regs->time, data,
  340. info->regs->regs_count);
  341. if (ret < 0)
  342. return ret;
  343. switch (info->device_type) {
  344. case S5M8763X:
  345. s5m8763_data_to_tm(data, tm);
  346. break;
  347. case S5M8767X:
  348. case S2MPS15X:
  349. case S2MPS14X:
  350. case S2MPS13X:
  351. s5m8767_data_to_tm(data, tm, info->rtc_24hr_mode);
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
  357. 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
  358. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
  359. return rtc_valid_tm(tm);
  360. }
  361. static int s5m_rtc_set_time(struct device *dev, struct rtc_time *tm)
  362. {
  363. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  364. u8 data[info->regs->regs_count];
  365. int ret = 0;
  366. switch (info->device_type) {
  367. case S5M8763X:
  368. s5m8763_tm_to_data(tm, data);
  369. break;
  370. case S5M8767X:
  371. case S2MPS15X:
  372. case S2MPS14X:
  373. case S2MPS13X:
  374. ret = s5m8767_tm_to_data(tm, data);
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. if (ret < 0)
  380. return ret;
  381. dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
  382. 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday,
  383. tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_wday);
  384. ret = regmap_raw_write(info->regmap, info->regs->time, data,
  385. info->regs->regs_count);
  386. if (ret < 0)
  387. return ret;
  388. ret = s5m8767_rtc_set_time_reg(info);
  389. return ret;
  390. }
  391. static int s5m_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  392. {
  393. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  394. u8 data[info->regs->regs_count];
  395. unsigned int val;
  396. int ret, i;
  397. ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
  398. info->regs->regs_count);
  399. if (ret < 0)
  400. return ret;
  401. switch (info->device_type) {
  402. case S5M8763X:
  403. s5m8763_data_to_tm(data, &alrm->time);
  404. ret = regmap_read(info->regmap, S5M_ALARM0_CONF, &val);
  405. if (ret < 0)
  406. return ret;
  407. alrm->enabled = !!val;
  408. break;
  409. case S5M8767X:
  410. case S2MPS15X:
  411. case S2MPS14X:
  412. case S2MPS13X:
  413. s5m8767_data_to_tm(data, &alrm->time, info->rtc_24hr_mode);
  414. alrm->enabled = 0;
  415. for (i = 0; i < info->regs->regs_count; i++) {
  416. if (data[i] & ALARM_ENABLE_MASK) {
  417. alrm->enabled = 1;
  418. break;
  419. }
  420. }
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
  426. 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
  427. alrm->time.tm_mday, alrm->time.tm_hour,
  428. alrm->time.tm_min, alrm->time.tm_sec,
  429. alrm->time.tm_wday);
  430. ret = s5m_check_peding_alarm_interrupt(info, alrm);
  431. return 0;
  432. }
  433. static int s5m_rtc_stop_alarm(struct s5m_rtc_info *info)
  434. {
  435. u8 data[info->regs->regs_count];
  436. int ret, i;
  437. struct rtc_time tm;
  438. ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
  439. info->regs->regs_count);
  440. if (ret < 0)
  441. return ret;
  442. s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
  443. dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
  444. 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
  445. tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
  446. switch (info->device_type) {
  447. case S5M8763X:
  448. ret = regmap_write(info->regmap, S5M_ALARM0_CONF, 0);
  449. break;
  450. case S5M8767X:
  451. case S2MPS15X:
  452. case S2MPS14X:
  453. case S2MPS13X:
  454. for (i = 0; i < info->regs->regs_count; i++)
  455. data[i] &= ~ALARM_ENABLE_MASK;
  456. ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
  457. info->regs->regs_count);
  458. if (ret < 0)
  459. return ret;
  460. ret = s5m8767_rtc_set_alarm_reg(info);
  461. break;
  462. default:
  463. return -EINVAL;
  464. }
  465. return ret;
  466. }
  467. static int s5m_rtc_start_alarm(struct s5m_rtc_info *info)
  468. {
  469. int ret;
  470. u8 data[info->regs->regs_count];
  471. u8 alarm0_conf;
  472. struct rtc_time tm;
  473. ret = regmap_bulk_read(info->regmap, info->regs->alarm0, data,
  474. info->regs->regs_count);
  475. if (ret < 0)
  476. return ret;
  477. s5m8767_data_to_tm(data, &tm, info->rtc_24hr_mode);
  478. dev_dbg(info->dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
  479. 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday,
  480. tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_wday);
  481. switch (info->device_type) {
  482. case S5M8763X:
  483. alarm0_conf = 0x77;
  484. ret = regmap_write(info->regmap, S5M_ALARM0_CONF, alarm0_conf);
  485. break;
  486. case S5M8767X:
  487. case S2MPS15X:
  488. case S2MPS14X:
  489. case S2MPS13X:
  490. data[RTC_SEC] |= ALARM_ENABLE_MASK;
  491. data[RTC_MIN] |= ALARM_ENABLE_MASK;
  492. data[RTC_HOUR] |= ALARM_ENABLE_MASK;
  493. data[RTC_WEEKDAY] &= ~ALARM_ENABLE_MASK;
  494. if (data[RTC_DATE] & 0x1f)
  495. data[RTC_DATE] |= ALARM_ENABLE_MASK;
  496. if (data[RTC_MONTH] & 0xf)
  497. data[RTC_MONTH] |= ALARM_ENABLE_MASK;
  498. if (data[RTC_YEAR1] & 0x7f)
  499. data[RTC_YEAR1] |= ALARM_ENABLE_MASK;
  500. ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
  501. info->regs->regs_count);
  502. if (ret < 0)
  503. return ret;
  504. ret = s5m8767_rtc_set_alarm_reg(info);
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. return ret;
  510. }
  511. static int s5m_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  512. {
  513. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  514. u8 data[info->regs->regs_count];
  515. int ret;
  516. switch (info->device_type) {
  517. case S5M8763X:
  518. s5m8763_tm_to_data(&alrm->time, data);
  519. break;
  520. case S5M8767X:
  521. case S2MPS15X:
  522. case S2MPS14X:
  523. case S2MPS13X:
  524. s5m8767_tm_to_data(&alrm->time, data);
  525. break;
  526. default:
  527. return -EINVAL;
  528. }
  529. dev_dbg(dev, "%s: %d/%d/%d %d:%d:%d(%d)\n", __func__,
  530. 1900 + alrm->time.tm_year, 1 + alrm->time.tm_mon,
  531. alrm->time.tm_mday, alrm->time.tm_hour, alrm->time.tm_min,
  532. alrm->time.tm_sec, alrm->time.tm_wday);
  533. ret = s5m_rtc_stop_alarm(info);
  534. if (ret < 0)
  535. return ret;
  536. ret = regmap_raw_write(info->regmap, info->regs->alarm0, data,
  537. info->regs->regs_count);
  538. if (ret < 0)
  539. return ret;
  540. ret = s5m8767_rtc_set_alarm_reg(info);
  541. if (ret < 0)
  542. return ret;
  543. if (alrm->enabled)
  544. ret = s5m_rtc_start_alarm(info);
  545. return ret;
  546. }
  547. static int s5m_rtc_alarm_irq_enable(struct device *dev,
  548. unsigned int enabled)
  549. {
  550. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  551. if (enabled)
  552. return s5m_rtc_start_alarm(info);
  553. else
  554. return s5m_rtc_stop_alarm(info);
  555. }
  556. static irqreturn_t s5m_rtc_alarm_irq(int irq, void *data)
  557. {
  558. struct s5m_rtc_info *info = data;
  559. rtc_update_irq(info->rtc_dev, 1, RTC_IRQF | RTC_AF);
  560. return IRQ_HANDLED;
  561. }
  562. static const struct rtc_class_ops s5m_rtc_ops = {
  563. .read_time = s5m_rtc_read_time,
  564. .set_time = s5m_rtc_set_time,
  565. .read_alarm = s5m_rtc_read_alarm,
  566. .set_alarm = s5m_rtc_set_alarm,
  567. .alarm_irq_enable = s5m_rtc_alarm_irq_enable,
  568. };
  569. static int s5m8767_rtc_init_reg(struct s5m_rtc_info *info)
  570. {
  571. u8 data[2];
  572. int ret;
  573. switch (info->device_type) {
  574. case S5M8763X:
  575. case S5M8767X:
  576. /* UDR update time. Default of 7.32 ms is too long. */
  577. ret = regmap_update_bits(info->regmap, S5M_RTC_UDR_CON,
  578. S5M_RTC_UDR_T_MASK, S5M_RTC_UDR_T_450_US);
  579. if (ret < 0)
  580. dev_err(info->dev, "%s: fail to change UDR time: %d\n",
  581. __func__, ret);
  582. /* Set RTC control register : Binary mode, 24hour mode */
  583. data[0] = (1 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  584. data[1] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  585. ret = regmap_raw_write(info->regmap, S5M_ALARM0_CONF, data, 2);
  586. break;
  587. case S2MPS15X:
  588. case S2MPS14X:
  589. case S2MPS13X:
  590. data[0] = (0 << BCD_EN_SHIFT) | (1 << MODEL24_SHIFT);
  591. ret = regmap_write(info->regmap, info->regs->ctrl, data[0]);
  592. if (ret < 0)
  593. break;
  594. /*
  595. * Should set WUDR & (RUDR or AUDR) bits to high after writing
  596. * RTC_CTRL register like writing Alarm registers. We can't find
  597. * the description from datasheet but vendor code does that
  598. * really.
  599. */
  600. ret = s5m8767_rtc_set_alarm_reg(info);
  601. break;
  602. default:
  603. return -EINVAL;
  604. }
  605. info->rtc_24hr_mode = 1;
  606. if (ret < 0) {
  607. dev_err(info->dev, "%s: fail to write controlm reg(%d)\n",
  608. __func__, ret);
  609. return ret;
  610. }
  611. return ret;
  612. }
  613. static int s5m_rtc_probe(struct platform_device *pdev)
  614. {
  615. struct sec_pmic_dev *s5m87xx = dev_get_drvdata(pdev->dev.parent);
  616. struct sec_platform_data *pdata = s5m87xx->pdata;
  617. struct s5m_rtc_info *info;
  618. const struct regmap_config *regmap_cfg;
  619. int ret, alarm_irq;
  620. if (!pdata) {
  621. dev_err(pdev->dev.parent, "Platform data not supplied\n");
  622. return -ENODEV;
  623. }
  624. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  625. if (!info)
  626. return -ENOMEM;
  627. switch (platform_get_device_id(pdev)->driver_data) {
  628. case S2MPS15X:
  629. regmap_cfg = &s2mps14_rtc_regmap_config;
  630. info->regs = &s2mps15_rtc_regs;
  631. alarm_irq = S2MPS14_IRQ_RTCA0;
  632. break;
  633. case S2MPS14X:
  634. regmap_cfg = &s2mps14_rtc_regmap_config;
  635. info->regs = &s2mps14_rtc_regs;
  636. alarm_irq = S2MPS14_IRQ_RTCA0;
  637. break;
  638. case S2MPS13X:
  639. regmap_cfg = &s2mps14_rtc_regmap_config;
  640. info->regs = &s2mps13_rtc_regs;
  641. alarm_irq = S2MPS14_IRQ_RTCA0;
  642. break;
  643. case S5M8763X:
  644. regmap_cfg = &s5m_rtc_regmap_config;
  645. info->regs = &s5m_rtc_regs;
  646. alarm_irq = S5M8763_IRQ_ALARM0;
  647. break;
  648. case S5M8767X:
  649. regmap_cfg = &s5m_rtc_regmap_config;
  650. info->regs = &s5m_rtc_regs;
  651. alarm_irq = S5M8767_IRQ_RTCA1;
  652. break;
  653. default:
  654. dev_err(&pdev->dev,
  655. "Device type %lu is not supported by RTC driver\n",
  656. platform_get_device_id(pdev)->driver_data);
  657. return -ENODEV;
  658. }
  659. info->i2c = i2c_new_dummy(s5m87xx->i2c->adapter, RTC_I2C_ADDR);
  660. if (!info->i2c) {
  661. dev_err(&pdev->dev, "Failed to allocate I2C for RTC\n");
  662. return -ENODEV;
  663. }
  664. info->regmap = devm_regmap_init_i2c(info->i2c, regmap_cfg);
  665. if (IS_ERR(info->regmap)) {
  666. ret = PTR_ERR(info->regmap);
  667. dev_err(&pdev->dev, "Failed to allocate RTC register map: %d\n",
  668. ret);
  669. goto err;
  670. }
  671. info->dev = &pdev->dev;
  672. info->s5m87xx = s5m87xx;
  673. info->device_type = platform_get_device_id(pdev)->driver_data;
  674. if (s5m87xx->irq_data) {
  675. info->irq = regmap_irq_get_virq(s5m87xx->irq_data, alarm_irq);
  676. if (info->irq <= 0) {
  677. ret = -EINVAL;
  678. dev_err(&pdev->dev, "Failed to get virtual IRQ %d\n",
  679. alarm_irq);
  680. goto err;
  681. }
  682. }
  683. platform_set_drvdata(pdev, info);
  684. ret = s5m8767_rtc_init_reg(info);
  685. device_init_wakeup(&pdev->dev, 1);
  686. info->rtc_dev = devm_rtc_device_register(&pdev->dev, "s5m-rtc",
  687. &s5m_rtc_ops, THIS_MODULE);
  688. if (IS_ERR(info->rtc_dev)) {
  689. ret = PTR_ERR(info->rtc_dev);
  690. goto err;
  691. }
  692. if (!info->irq) {
  693. dev_info(&pdev->dev, "Alarm IRQ not available\n");
  694. return 0;
  695. }
  696. ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
  697. s5m_rtc_alarm_irq, 0, "rtc-alarm0",
  698. info);
  699. if (ret < 0) {
  700. dev_err(&pdev->dev, "Failed to request alarm IRQ: %d: %d\n",
  701. info->irq, ret);
  702. goto err;
  703. }
  704. return 0;
  705. err:
  706. i2c_unregister_device(info->i2c);
  707. return ret;
  708. }
  709. static int s5m_rtc_remove(struct platform_device *pdev)
  710. {
  711. struct s5m_rtc_info *info = platform_get_drvdata(pdev);
  712. i2c_unregister_device(info->i2c);
  713. return 0;
  714. }
  715. #ifdef CONFIG_PM_SLEEP
  716. static int s5m_rtc_resume(struct device *dev)
  717. {
  718. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  719. int ret = 0;
  720. if (info->irq && device_may_wakeup(dev))
  721. ret = disable_irq_wake(info->irq);
  722. return ret;
  723. }
  724. static int s5m_rtc_suspend(struct device *dev)
  725. {
  726. struct s5m_rtc_info *info = dev_get_drvdata(dev);
  727. int ret = 0;
  728. if (info->irq && device_may_wakeup(dev))
  729. ret = enable_irq_wake(info->irq);
  730. return ret;
  731. }
  732. #endif /* CONFIG_PM_SLEEP */
  733. static SIMPLE_DEV_PM_OPS(s5m_rtc_pm_ops, s5m_rtc_suspend, s5m_rtc_resume);
  734. static const struct platform_device_id s5m_rtc_id[] = {
  735. { "s5m-rtc", S5M8767X },
  736. { "s2mps13-rtc", S2MPS13X },
  737. { "s2mps14-rtc", S2MPS14X },
  738. { "s2mps15-rtc", S2MPS15X },
  739. { },
  740. };
  741. MODULE_DEVICE_TABLE(platform, s5m_rtc_id);
  742. static struct platform_driver s5m_rtc_driver = {
  743. .driver = {
  744. .name = "s5m-rtc",
  745. .pm = &s5m_rtc_pm_ops,
  746. },
  747. .probe = s5m_rtc_probe,
  748. .remove = s5m_rtc_remove,
  749. .id_table = s5m_rtc_id,
  750. };
  751. module_platform_driver(s5m_rtc_driver);
  752. /* Module information */
  753. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  754. MODULE_DESCRIPTION("Samsung S5M/S2MPS14 RTC driver");
  755. MODULE_LICENSE("GPL");
  756. MODULE_ALIAS("platform:s5m-rtc");