rtc-s5m.c 21 KB

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