qpnp-rtc.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/rtc.h>
  15. #include <linux/pm.h>
  16. #include <linux/slab.h>
  17. #include <linux/idr.h>
  18. #include <linux/of_device.h>
  19. #include <linux/spmi.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/spmi.h>
  22. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  23. #include <linux/qpnp/qpnp-adc.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/battery/sec_charging_common.h>
  26. #endif
  27. #ifdef CONFIG_RTC_AUTO_PWRON
  28. #include <linux/reboot.h>
  29. extern int poweroff_charging;
  30. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  31. #include <linux/sec_param.h>
  32. #include <linux/param.h>
  33. /* for alarm mode */
  34. #define ALARM_MODE_NOMAL (0x6A)
  35. #define ALARM_MODE_BOOT_RTC (0x7B)
  36. #define SAPA_BOOTING_TIME (60*3)
  37. #endif
  38. #endif
  39. #ifdef CONFIG_SEC_PM
  40. #include <linux/wakelock.h>
  41. #endif
  42. /* RTC/ALARM Register offsets */
  43. #define REG_OFFSET_ALARM_RW 0x40
  44. #define REG_OFFSET_ALARM_CTRL1 0x46
  45. #define REG_OFFSET_ALARM_CTRL2 0x48
  46. #define REG_OFFSET_RTC_WRITE 0x40
  47. #define REG_OFFSET_RTC_CTRL 0x46
  48. #define REG_OFFSET_RTC_READ 0x48
  49. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  50. #define INT_ADD 0x10
  51. #define BATT_PRES_IRQ BIT(0)
  52. #endif
  53. #define REG_OFFSET_PERP_SUBTYPE 0x05
  54. /* RTC_CTRL register bit fields */
  55. #define BIT_RTC_ENABLE BIT(7)
  56. #define BIT_RTC_ALARM_ENABLE BIT(7)
  57. #define BIT_RTC_ABORT_ENABLE BIT(0)
  58. #define BIT_RTC_ALARM_CLEAR BIT(0)
  59. /* RTC/ALARM peripheral subtype values */
  60. #define RTC_PERPH_SUBTYPE 0x1
  61. #define ALARM_PERPH_SUBTYPE 0x3
  62. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  63. #define RTC_BATT_PRES_IRQ 0x33
  64. #endif
  65. #define NUM_8_BIT_RTC_REGS 0x4
  66. #define TO_SECS(arr) (arr[0] | (arr[1] << 8) | (arr[2] << 16) | \
  67. (arr[3] << 24))
  68. /* Module parameter to control power-on-alarm */
  69. static bool poweron_alarm;
  70. module_param(poweron_alarm, bool, 0644);
  71. MODULE_PARM_DESC(poweron_alarm, "Enable/Disable power-on alarm");
  72. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  73. static enum power_supply_property pm8926_battery_props[] = {
  74. POWER_SUPPLY_PROP_PRESENT,
  75. };
  76. #endif
  77. /* rtc driver internal structure */
  78. struct qpnp_rtc {
  79. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  80. bool battery_present;
  81. #endif
  82. u8 rtc_ctrl_reg;
  83. u8 alarm_ctrl_reg1;
  84. u16 rtc_base;
  85. u16 alarm_base;
  86. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  87. u16 bat_base;
  88. #endif
  89. u32 rtc_write_enable;
  90. u32 rtc_alarm_powerup;
  91. int rtc_alarm_irq;
  92. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  93. int bat_pres_irq;
  94. struct power_supply psy_pm8926;
  95. #endif
  96. struct device *rtc_dev;
  97. struct rtc_device *rtc;
  98. struct spmi_device *spmi;
  99. spinlock_t alarm_ctrl_lock;
  100. #if defined(CONFIG_RTC_AUTO_PWRON)
  101. bool lpm_mode;
  102. bool alarm_irq_flag;
  103. struct wake_lock alarm_wake_lock;
  104. #endif
  105. };
  106. #if defined(CONFIG_RTC_AUTO_PWRON)
  107. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  108. static struct workqueue_struct* sapa_workq;
  109. static struct workqueue_struct* sapa_check_workq;
  110. static struct delayed_work sapa_load_param_work;
  111. static struct delayed_work sapa_reboot_work;
  112. static struct delayed_work sapa_check_work;
  113. static struct wake_lock sapa_wakelock;
  114. static int kparam_loaded, shutdown_loaded;
  115. #endif
  116. static struct rtc_wkalrm sapa_saved_time;
  117. static int sapa_dev_suspend;
  118. static void print_time(char* str, struct rtc_time *time, unsigned long sec)
  119. {
  120. pr_info("%s: %4d-%02d-%02d %02d:%02d:%02d [%lu]\n", str,
  121. time->tm_year, time->tm_mon, time->tm_mday,
  122. time->tm_hour, time->tm_min, time->tm_sec, sec);
  123. }
  124. #endif
  125. #ifdef CONFIG_SEC_PM
  126. static struct wake_lock resume_wakelock;
  127. #endif
  128. static int qpnp_read_wrapper(struct qpnp_rtc *rtc_dd, u8 *rtc_val,
  129. u16 base, int count)
  130. {
  131. int rc;
  132. struct spmi_device *spmi = rtc_dd->spmi;
  133. rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, rtc_val,
  134. count);
  135. if (rc) {
  136. dev_err(rtc_dd->rtc_dev, "SPMI read failed\n");
  137. return rc;
  138. }
  139. return 0;
  140. }
  141. static int qpnp_write_wrapper(struct qpnp_rtc *rtc_dd, u8 *rtc_val,
  142. u16 base, int count)
  143. {
  144. int rc;
  145. struct spmi_device *spmi = rtc_dd->spmi;
  146. rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, rtc_val,
  147. count);
  148. if (rc) {
  149. dev_err(rtc_dd->rtc_dev, "SPMI write failed\n");
  150. return rc;
  151. }
  152. return 0;
  153. }
  154. static int
  155. qpnp_rtc_set_time(struct device *dev, struct rtc_time *tm)
  156. {
  157. int rc;
  158. unsigned long secs, irq_flags;
  159. u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg;
  160. u8 rtc_disabled = 0, rtc_ctrl_reg;
  161. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  162. rtc_tm_to_time(tm, &secs);
  163. value[0] = secs & 0xFF;
  164. value[1] = (secs >> 8) & 0xFF;
  165. value[2] = (secs >> 16) & 0xFF;
  166. value[3] = (secs >> 24) & 0xFF;
  167. dev_info(dev, "Seconds value to be written to RTC = %lu\n", secs);
  168. spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
  169. ctrl_reg = rtc_dd->alarm_ctrl_reg1;
  170. if (ctrl_reg & BIT_RTC_ALARM_ENABLE) {
  171. alarm_enabled = 1;
  172. ctrl_reg &= ~BIT_RTC_ALARM_ENABLE;
  173. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
  174. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  175. if (rc) {
  176. dev_err(dev, "Write to ALARM ctrl reg failed\n");
  177. goto rtc_rw_fail;
  178. }
  179. } else
  180. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  181. /*
  182. * 32 bit seconds value is coverted to four 8 bit values
  183. * |<------ 32 bit time value in seconds ------>|
  184. * <- 8 bit ->|<- 8 bit ->|<- 8 bit ->|<- 8 bit ->|
  185. * ----------------------------------------------
  186. * | BYTE[3] | BYTE[2] | BYTE[1] | BYTE[0] |
  187. * ----------------------------------------------
  188. *
  189. * RTC has four 8 bit registers for writting time in seconds:
  190. * WDATA[3], WDATA[2], WDATA[1], WDATA[0]
  191. *
  192. * Write to the RTC registers should be done in following order
  193. * Clear WDATA[0] register
  194. *
  195. * Write BYTE[1], BYTE[2] and BYTE[3] of time to
  196. * RTC WDATA[3], WDATA[2], WDATA[1] registers
  197. *
  198. * Write BYTE[0] of time to RTC WDATA[0] register
  199. *
  200. * Clearing BYTE[0] and writting in the end will prevent any
  201. * unintentional overflow from WDATA[0] to higher bytes during the
  202. * write operation
  203. */
  204. /* Disable RTC H/w before writing on RTC register*/
  205. rtc_ctrl_reg = rtc_dd->rtc_ctrl_reg;
  206. if (rtc_ctrl_reg & BIT_RTC_ENABLE) {
  207. rtc_disabled = 1;
  208. rtc_ctrl_reg &= ~BIT_RTC_ENABLE;
  209. rc = qpnp_write_wrapper(rtc_dd, &rtc_ctrl_reg,
  210. rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
  211. if (rc) {
  212. dev_err(dev,
  213. "Disabling of RTC control reg failed"
  214. " with error:%d\n", rc);
  215. goto rtc_rw_fail;
  216. }
  217. rtc_dd->rtc_ctrl_reg = rtc_ctrl_reg;
  218. }
  219. /* Clear WDATA[0] */
  220. reg = 0x0;
  221. rc = qpnp_write_wrapper(rtc_dd, &reg,
  222. rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE, 1);
  223. if (rc) {
  224. dev_err(dev, "Write to RTC reg failed\n");
  225. goto rtc_rw_fail;
  226. }
  227. /* Write to WDATA[3], WDATA[2] and WDATA[1] */
  228. rc = qpnp_write_wrapper(rtc_dd, &value[1],
  229. rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE + 1, 3);
  230. if (rc) {
  231. dev_err(dev, "Write to RTC reg failed\n");
  232. goto rtc_rw_fail;
  233. }
  234. /* Write to WDATA[0] */
  235. rc = qpnp_write_wrapper(rtc_dd, value,
  236. rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE, 1);
  237. if (rc) {
  238. dev_err(dev, "Write to RTC reg failed\n");
  239. goto rtc_rw_fail;
  240. }
  241. /* Enable RTC H/w after writing on RTC register*/
  242. if (rtc_disabled) {
  243. rtc_ctrl_reg |= BIT_RTC_ENABLE;
  244. rc = qpnp_write_wrapper(rtc_dd, &rtc_ctrl_reg,
  245. rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
  246. if (rc) {
  247. dev_err(dev,
  248. "Enabling of RTC control reg failed"
  249. " with error:%d\n", rc);
  250. goto rtc_rw_fail;
  251. }
  252. rtc_dd->rtc_ctrl_reg = rtc_ctrl_reg;
  253. }
  254. if (alarm_enabled) {
  255. ctrl_reg |= BIT_RTC_ALARM_ENABLE;
  256. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
  257. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  258. if (rc) {
  259. dev_err(dev, "Write to ALARM ctrl reg failed\n");
  260. goto rtc_rw_fail;
  261. }
  262. }
  263. rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
  264. #ifdef CONFIG_RTC_AUTO_PWRON
  265. pr_info("%s : secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n", __func__,
  266. secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
  267. tm->tm_mday, tm->tm_mon, tm->tm_year);
  268. #endif
  269. rtc_rw_fail:
  270. if (alarm_enabled)
  271. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  272. return rc;
  273. }
  274. static int
  275. qpnp_rtc_read_time(struct device *dev, struct rtc_time *tm)
  276. {
  277. int rc;
  278. u8 value[4], reg;
  279. unsigned long secs;
  280. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  281. rc = qpnp_read_wrapper(rtc_dd, value,
  282. rtc_dd->rtc_base + REG_OFFSET_RTC_READ,
  283. NUM_8_BIT_RTC_REGS);
  284. if (rc) {
  285. dev_err(dev, "Read from RTC reg failed\n");
  286. return rc;
  287. }
  288. /*
  289. * Read the LSB again and check if there has been a carry over
  290. * If there is, redo the read operation
  291. */
  292. rc = qpnp_read_wrapper(rtc_dd, &reg,
  293. rtc_dd->rtc_base + REG_OFFSET_RTC_READ, 1);
  294. if (rc) {
  295. dev_err(dev, "Read from RTC reg failed\n");
  296. return rc;
  297. }
  298. if (reg < value[0]) {
  299. rc = qpnp_read_wrapper(rtc_dd, value,
  300. rtc_dd->rtc_base + REG_OFFSET_RTC_READ,
  301. NUM_8_BIT_RTC_REGS);
  302. if (rc) {
  303. dev_err(dev, "Read from RTC reg failed\n");
  304. return rc;
  305. }
  306. }
  307. secs = TO_SECS(value);
  308. rtc_time_to_tm(secs, tm);
  309. rc = rtc_valid_tm(tm);
  310. if (rc) {
  311. dev_err(dev, "Invalid time read from RTC\n");
  312. return rc;
  313. }
  314. dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n",
  315. secs, tm->tm_hour, tm->tm_min, tm->tm_sec,
  316. tm->tm_mday, tm->tm_mon, tm->tm_year);
  317. return 0;
  318. }
  319. static int
  320. qpnp_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  321. {
  322. int rc;
  323. u8 value[4], ctrl_reg;
  324. unsigned long secs, secs_rtc, irq_flags;
  325. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  326. struct rtc_time rtc_tm;
  327. rtc_tm_to_time(&alarm->time, &secs);
  328. /*
  329. * Read the current RTC time and verify if the alarm time is in the
  330. * past. If yes, return invalid
  331. */
  332. rc = qpnp_rtc_read_time(dev, &rtc_tm);
  333. if (rc) {
  334. dev_err(dev, "Unable to read RTC time\n");
  335. return -EINVAL;
  336. }
  337. rtc_tm_to_time(&rtc_tm, &secs_rtc);
  338. if (secs < secs_rtc) {
  339. dev_err(dev, "Trying to set alarm in the past\n");
  340. return -EINVAL;
  341. }
  342. #ifdef CONFIG_RTC_AUTO_PWRON
  343. if ( sapa_saved_time.enabled ) {
  344. unsigned long secs_pwron;
  345. /* If there are power on alarm before alarm time, ignore alarm */
  346. rtc_tm_to_time(&sapa_saved_time.time, &secs_pwron);
  347. print_time("[SAPA] rtc ", &rtc_tm, secs_rtc);
  348. print_time("[SAPA] sapa", &sapa_saved_time.time, secs_pwron);
  349. print_time("[SAPA] alrm", &alarm->time, secs);
  350. if ( secs_pwron <= secs_rtc && secs_rtc <= secs_pwron+SAPA_BOOTING_TIME ) {
  351. if ( poweroff_charging ) {
  352. wake_lock(&sapa_wakelock);
  353. rtc_dd->alarm_irq_flag = true;
  354. pr_info("%s [SAPA] Restart(alarm)\n",__func__);
  355. queue_delayed_work(sapa_workq, &sapa_reboot_work, (1*HZ));
  356. return -EINVAL;
  357. }
  358. }
  359. if ( secs_rtc < secs_pwron && secs_pwron < secs ) {
  360. pr_info("[SAPA] override with SAPA\n");
  361. memcpy(alarm, &sapa_saved_time, sizeof(struct rtc_wkalrm));
  362. secs = secs_pwron;
  363. }
  364. }
  365. #endif
  366. value[0] = secs & 0xFF;
  367. value[1] = (secs >> 8) & 0xFF;
  368. value[2] = (secs >> 16) & 0xFF;
  369. value[3] = (secs >> 24) & 0xFF;
  370. spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
  371. rc = qpnp_write_wrapper(rtc_dd, value,
  372. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  373. NUM_8_BIT_RTC_REGS);
  374. if (rc) {
  375. dev_err(dev, "Write to ALARM reg failed\n");
  376. goto rtc_rw_fail;
  377. }
  378. ctrl_reg = (alarm->enabled) ?
  379. (rtc_dd->alarm_ctrl_reg1 | BIT_RTC_ALARM_ENABLE) :
  380. (rtc_dd->alarm_ctrl_reg1 & ~BIT_RTC_ALARM_ENABLE);
  381. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
  382. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  383. if (rc) {
  384. dev_err(dev, "Write to ALARM cntrol reg failed\n");
  385. goto rtc_rw_fail;
  386. }
  387. rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
  388. dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
  389. alarm->time.tm_hour, alarm->time.tm_min,
  390. alarm->time.tm_sec, alarm->time.tm_mday,
  391. alarm->time.tm_mon, alarm->time.tm_year);
  392. rtc_rw_fail:
  393. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  394. return rc;
  395. }
  396. static int
  397. qpnp_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  398. {
  399. int rc;
  400. u8 value[4];
  401. unsigned long secs;
  402. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  403. rc = qpnp_read_wrapper(rtc_dd, value,
  404. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  405. NUM_8_BIT_RTC_REGS);
  406. if (rc) {
  407. dev_err(dev, "Read from ALARM reg failed\n");
  408. return rc;
  409. }
  410. secs = TO_SECS(value);
  411. rtc_time_to_tm(secs, &alarm->time);
  412. rc = rtc_valid_tm(&alarm->time);
  413. if (rc) {
  414. dev_err(dev, "Invalid time read from RTC\n");
  415. return rc;
  416. }
  417. dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n",
  418. alarm->time.tm_hour, alarm->time.tm_min,
  419. alarm->time.tm_sec, alarm->time.tm_mday,
  420. alarm->time.tm_mon, alarm->time.tm_year);
  421. return 0;
  422. }
  423. static int
  424. qpnp_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
  425. {
  426. int rc;
  427. unsigned long irq_flags;
  428. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  429. u8 ctrl_reg;
  430. u8 value[4] = {0};
  431. #ifdef CONFIG_RTC_AUTO_PWRON
  432. pr_info("[SAPA] irq=%d\n", enabled);
  433. #endif
  434. spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
  435. ctrl_reg = rtc_dd->alarm_ctrl_reg1;
  436. ctrl_reg = enabled ? (ctrl_reg | BIT_RTC_ALARM_ENABLE) :
  437. (ctrl_reg & ~BIT_RTC_ALARM_ENABLE);
  438. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
  439. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  440. if (rc) {
  441. dev_err(dev, "Write to ALARM control reg failed\n");
  442. goto rtc_rw_fail;
  443. }
  444. rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
  445. /* Clear Alarm register */
  446. if (!enabled) {
  447. rc = qpnp_write_wrapper(rtc_dd, value,
  448. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  449. NUM_8_BIT_RTC_REGS);
  450. if (rc)
  451. dev_err(dev, "Clear ALARM value reg failed\n");
  452. }
  453. rtc_rw_fail:
  454. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  455. return rc;
  456. }
  457. #ifdef CONFIG_RTC_AUTO_PWRON
  458. static void sapa_reboot(struct work_struct *work)
  459. {
  460. //machine_restart(NULL);
  461. kernel_restart(NULL);
  462. //panic("Test panic");
  463. }
  464. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  465. static struct device * sapa_rtc_dev;
  466. static int qpnp_rtc0_resetbootalarm(struct device *dev);
  467. static void sapa_load_kparam(struct work_struct *work)
  468. {
  469. int temp1, temp2, temp3;
  470. unsigned long pwron_time=(unsigned long)0;
  471. bool rc, kparam_ok = true;
  472. static unsigned int kparam_count = (unsigned int)0;
  473. rc = sec_get_param(param_index_boot_alarm_set, &temp1);
  474. if(!rc)
  475. kparam_ok = false;
  476. rc = sec_get_param(param_index_boot_alarm_value_l, &temp2);
  477. if(!rc)
  478. kparam_ok = false;
  479. rc = sec_get_param(param_index_boot_alarm_value_h, &temp3);
  480. if(!rc)
  481. kparam_ok = false;
  482. if(!kparam_ok) {
  483. if(kparam_count < 3) {
  484. queue_delayed_work(sapa_workq, &sapa_load_param_work, (5*HZ));
  485. kparam_count++;
  486. pr_err("[SAPA] %s fail, count=%d\n", __func__, kparam_count);
  487. return ;
  488. } else {
  489. pr_err("[SAPA] %s final fail, just go on\n", __func__);
  490. }
  491. }
  492. pwron_time = temp3<<4 | temp2;
  493. pr_info("[SAPA] %s %x %lu\n", __func__, temp1, pwron_time);
  494. if ( temp1 == ALARM_MODE_BOOT_RTC )
  495. sapa_saved_time.enabled = 1;
  496. else
  497. sapa_saved_time.enabled = 0;
  498. kparam_loaded = 1;
  499. rtc_time_to_tm( pwron_time, &sapa_saved_time.time );
  500. print_time("[SAPA] saved_time", &sapa_saved_time.time, pwron_time);
  501. /* Bug fix : USB cable or IRQ is disabled in LPM chg */
  502. qpnp_rtc0_resetbootalarm(sapa_rtc_dev);
  503. }
  504. #endif
  505. static void sapa_store_kparam(struct rtc_wkalrm *alarm)
  506. {
  507. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  508. //int temp1, temp2, temp3;
  509. int MSB=0, LSB=0;
  510. int alarm_mode = 0;
  511. unsigned long secs;
  512. if ( alarm == &sapa_saved_time ) {
  513. pr_err("[SAPA] %s: already was written\n", __func__);
  514. return ;
  515. }
  516. if ( alarm->enabled ) {
  517. rtc_tm_to_time(&alarm->time, &secs);
  518. LSB = (int)secs;
  519. MSB = (int)(secs>>4);
  520. alarm_mode = ALARM_MODE_BOOT_RTC;
  521. sec_set_param(param_index_boot_alarm_set, &alarm_mode);
  522. sec_set_param(param_index_boot_alarm_value_l, &LSB);
  523. sec_set_param(param_index_boot_alarm_value_h, &MSB);
  524. pr_info("[SAPA] %s %x/%x/%x\n", __func__, alarm_mode, LSB, MSB);
  525. #if 0 // for debugging
  526. sec_get_param(param_index_boot_alarm_set,&temp1);
  527. sec_get_param(param_index_boot_alarm_value_l, &temp2);
  528. sec_get_param(param_index_boot_alarm_value_h, &temp3);
  529. pr_info( "sec_set_param [%x] [%x] [%x] -- feedback\n", temp1, temp2, temp3);
  530. #endif
  531. }
  532. else {
  533. alarm_mode = ALARM_MODE_NOMAL;
  534. sec_set_param(param_index_boot_alarm_set, &alarm_mode);
  535. pr_info("[SAPA] %s clear\n", __func__);
  536. }
  537. #endif
  538. }
  539. #ifdef CONFIG_RTC_AUTO_PWRON
  540. static void
  541. sapa_check_alarm(struct work_struct *work)
  542. {
  543. struct qpnp_rtc *rtc_dd = dev_get_drvdata(sapa_rtc_dev);
  544. pr_info("%s [SAPA] : lpm_mode:(%d)\n", __func__, rtc_dd->lpm_mode);
  545. if ( poweroff_charging && sapa_saved_time.enabled) {
  546. struct rtc_time now;
  547. struct rtc_wkalrm alarm;
  548. unsigned long curr_time, alarm_time, pwron_time;
  549. /* To wake up rtc device */
  550. wake_lock_timeout(&sapa_wakelock, HZ/2 );
  551. qpnp_rtc_read_time(rtc_dd->rtc_dev, &now);
  552. rtc_tm_to_time(&now, &curr_time);
  553. qpnp_rtc_read_alarm(rtc_dd->rtc_dev, &alarm);
  554. rtc_tm_to_time(&alarm.time, &alarm_time);
  555. rtc_tm_to_time(&sapa_saved_time.time, &pwron_time);
  556. pr_info("%s [SAPA] curr_time: %lu\n",__func__, curr_time);
  557. pr_info("%s [SAPA] pmic_time: %lu\n",__func__, alarm_time);
  558. pr_info("%s [SAPA] pwrontime: %lu [%d]\n",__func__, pwron_time, sapa_saved_time.enabled);
  559. if ( pwron_time <= curr_time && curr_time <= pwron_time+SAPA_BOOTING_TIME ) {
  560. wake_lock(&sapa_wakelock);
  561. rtc_dd->alarm_irq_flag = true;
  562. pr_info("%s [SAPA] Restart since RTC \n",__func__);
  563. queue_delayed_work(sapa_workq, &sapa_reboot_work, (1*HZ));
  564. }
  565. else {
  566. pr_info("%s [SAPA] not power on alarm.\n", __func__);
  567. if (!sapa_dev_suspend) {
  568. qpnp_rtc0_resetbootalarm(rtc_dd->rtc_dev);
  569. queue_delayed_work(sapa_check_workq, &sapa_check_work, (60*HZ));
  570. }
  571. }
  572. }
  573. }
  574. #endif
  575. static int
  576. sapa_rtc_getalarm(struct device *dev, struct rtc_wkalrm *alarm)
  577. {
  578. struct rtc_time b;
  579. int ret = 0;
  580. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  581. unsigned long secs_alrm, secs_rtc;
  582. /* read rtc time */
  583. if ( qpnp_rtc_read_time(dev, &b) ) {
  584. pr_err("%s [SAPA] : read time failed.\n", __func__);
  585. ret = -EINVAL;
  586. }
  587. memcpy(alarm, &sapa_saved_time, sizeof(struct rtc_wkalrm));
  588. if (rtc_dd->alarm_irq_flag)
  589. alarm->enabled = 0x1;
  590. else
  591. alarm->enabled = 0x0;
  592. pr_info("%s [SAPA] : %d, %d\n",__func__,rtc_dd->lpm_mode, alarm->enabled);
  593. if(poweroff_charging && sapa_saved_time.enabled)
  594. {
  595. rtc_tm_to_time(&b, &secs_rtc);
  596. rtc_tm_to_time(&alarm->time, &secs_alrm);
  597. if ( secs_alrm <= secs_rtc && secs_rtc <= secs_alrm+SAPA_BOOTING_TIME )
  598. {
  599. rtc_dd->alarm_irq_flag = true;
  600. pr_info("%s [SAPA] : it will be reboot \n",__func__);
  601. }
  602. }
  603. if ( !ret ) {
  604. pr_info("[SAPA] %s: [ALRM] %d-%d-%d %d:%d:%d \n", __func__,
  605. alarm->time.tm_year, alarm->time.tm_mon, alarm->time.tm_mday,
  606. alarm->time.tm_hour, alarm->time.tm_min, alarm->time.tm_sec);
  607. pr_info("[SAPA] %s: [RTC ] %d-%d-%d %d:%d:%d \n", __func__,
  608. b.tm_year, b.tm_mon, b.tm_mday,
  609. b.tm_hour, b.tm_min, b.tm_sec);
  610. }
  611. return rtc_dd->lpm_mode;
  612. }
  613. static int
  614. sapa_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alarm)
  615. {
  616. int rc;
  617. u8 value[4] = {0,}, ctrl_reg;
  618. unsigned long secs, secs_rtc;//, irq_flags;
  619. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  620. struct rtc_time rtc_tm;
  621. if (!alarm->enabled) {
  622. pr_info("[SAPA] Try to clear : %4d-%02d-%02d %02d:%02d:%02d\n",
  623. alarm->time.tm_year, alarm->time.tm_mon, alarm->time.tm_mday,
  624. alarm->time.tm_hour, alarm->time.tm_min, alarm->time.tm_sec);
  625. if(poweroff_charging && !kparam_loaded && shutdown_loaded){
  626. pr_info("%s [SAPA] without loading kparam, it will be shutdown. No need to reset the alarm!! \n",__func__);
  627. ctrl_reg = (rtc_dd->alarm_ctrl_reg1 | BIT_RTC_ALARM_ENABLE);
  628. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  629. if (rc) {
  630. dev_err(dev, "Write to ALARM cntrol reg failed\n");
  631. goto rtc_rw_fail;
  632. }
  633. return 0;
  634. }
  635. rc = qpnp_write_wrapper(rtc_dd, value,
  636. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  637. NUM_8_BIT_RTC_REGS);
  638. if (rc < 0) {
  639. pr_err("[SAPA] Write to RTC ALARM registers failed\n");
  640. goto rtc_rw_fail;
  641. }
  642. sapa_saved_time.enabled = 0; // disable pwr on alarm to prevent retrying
  643. sapa_store_kparam(alarm);
  644. ctrl_reg = (alarm->enabled) ?
  645. (rtc_dd->alarm_ctrl_reg1 | BIT_RTC_ALARM_ENABLE) :
  646. (rtc_dd->alarm_ctrl_reg1 & ~BIT_RTC_ALARM_ENABLE);
  647. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  648. if (rc) {
  649. dev_err(dev, "Write to ALARM cntrol reg failed\n");
  650. goto rtc_rw_fail;
  651. }
  652. rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
  653. /* read boot alarm */
  654. rc = qpnp_rtc_read_alarm(dev, alarm);
  655. if ( rc < 0 ) {
  656. pr_err("[SAPA] read failed.\n");
  657. return rc;
  658. }
  659. pr_info("[SAPA] -> %4d-%02d-%02d %02d:%02d:%02d\n",
  660. alarm->time.tm_year, alarm->time.tm_mon, alarm->time.tm_mday,
  661. alarm->time.tm_hour, alarm->time.tm_min, alarm->time.tm_sec);
  662. }
  663. else
  664. {
  665. pr_info("[SAPA] <- %4d-%02d-%02d %02d:%02d:%02d\n",
  666. alarm->time.tm_year, alarm->time.tm_mon, alarm->time.tm_mday,
  667. alarm->time.tm_hour, alarm->time.tm_min, alarm->time.tm_sec);
  668. rtc_tm_to_time(&alarm->time, &secs);
  669. /*
  670. * Read the current RTC time and verify if the alarm time is in the
  671. * past. If yes, return invalid.
  672. */
  673. rc = qpnp_rtc_read_time(dev, &rtc_tm);
  674. if (rc < 0) {
  675. pr_err("[SAPA] Unable to read RTC time\n");
  676. return -EINVAL;
  677. }
  678. rtc_tm_to_time(&rtc_tm, &secs_rtc);
  679. if ( secs <= secs_rtc && secs_rtc <= secs+SAPA_BOOTING_TIME ) {
  680. if ( poweroff_charging ) {
  681. wake_lock(&sapa_wakelock);
  682. rtc_dd->alarm_irq_flag = true;
  683. pr_info("%s [SAPA] Restart(alarm)\n",__func__);
  684. queue_delayed_work(sapa_workq, &sapa_reboot_work, (10*HZ));
  685. }
  686. else if (shutdown_loaded) {
  687. pr_info("[SAPA] adjust to rtc+20s\n");
  688. secs = secs_rtc + 10;
  689. }
  690. }
  691. else if ( secs+SAPA_BOOTING_TIME < secs_rtc ) {
  692. pr_err("[SAPA] Trying to set alarm in the past\n");
  693. sapa_saved_time.enabled = 0; // disable pwr on alarm to prevent retrying
  694. sapa_store_kparam(alarm);
  695. return -EINVAL;
  696. }
  697. value[0] = secs & 0xFF;
  698. value[1] = (secs >> 8) & 0xFF;
  699. value[2] = (secs >> 16) & 0xFF;
  700. value[3] = (secs >> 24) & 0xFF;
  701. //spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
  702. rc = qpnp_write_wrapper(rtc_dd, value,
  703. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  704. NUM_8_BIT_RTC_REGS);
  705. if (rc < 0) {
  706. pr_err("[SAPA] Write to RTC ALARM registers failed\n");
  707. goto rtc_rw_fail;
  708. }
  709. ctrl_reg = (alarm->enabled) ?
  710. (rtc_dd->alarm_ctrl_reg1 | BIT_RTC_ALARM_ENABLE) :
  711. (rtc_dd->alarm_ctrl_reg1 & ~BIT_RTC_ALARM_ENABLE);
  712. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  713. if (rc) {
  714. dev_err(dev, "Write to ALARM cntrol reg failed\n");
  715. goto rtc_rw_fail;
  716. }
  717. rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
  718. if ( alarm != &sapa_saved_time ) {
  719. memcpy(&sapa_saved_time, alarm, sizeof(struct rtc_wkalrm));
  720. sapa_store_kparam(alarm);
  721. pr_info("[SAPA] updated\n");
  722. }
  723. }
  724. /* read boot alarm */
  725. rc = qpnp_rtc_read_alarm(dev, alarm);
  726. if ( rc < 0 ) {
  727. pr_err("[SAPA] read failed.\n");
  728. return rc;
  729. }
  730. pr_info("[SAPA] -> %4d-%02d-%02d %02d:%02d:%02d\n",
  731. alarm->time.tm_year, alarm->time.tm_mon, alarm->time.tm_mday,
  732. alarm->time.tm_hour, alarm->time.tm_min, alarm->time.tm_sec);
  733. if ( alarm != &sapa_saved_time )
  734. qpnp_rtc_read_time(dev,&(alarm->time));
  735. rtc_rw_fail:
  736. //spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  737. return rc;
  738. }
  739. static int qpnp_rtc0_resetbootalarm(struct device *dev)
  740. {
  741. pr_info("[SAPA] rewrite [%d]\n", sapa_saved_time.enabled);
  742. return sapa_rtc_setalarm(dev, &sapa_saved_time);
  743. }
  744. #endif /*CONFIG_RTC_AUTO_PWRON*/
  745. static struct rtc_class_ops qpnp_rtc_ops = {
  746. .read_time = qpnp_rtc_read_time,
  747. .set_alarm = qpnp_rtc_set_alarm,
  748. .read_alarm = qpnp_rtc_read_alarm,
  749. #ifdef CONFIG_RTC_AUTO_PWRON
  750. .read_bootalarm = sapa_rtc_getalarm,
  751. .set_bootalarm = sapa_rtc_setalarm,
  752. #endif /*CONFIG_RTC_AUTO_PWRON*/
  753. .alarm_irq_enable = qpnp_rtc_alarm_irq_enable,
  754. };
  755. static irqreturn_t qpnp_alarm_trigger(int irq, void *dev_id)
  756. {
  757. struct qpnp_rtc *rtc_dd = dev_id;
  758. u8 ctrl_reg;
  759. int rc;
  760. unsigned long irq_flags;
  761. rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF);
  762. spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
  763. /* Clear the alarm enable bit */
  764. ctrl_reg = rtc_dd->alarm_ctrl_reg1;
  765. ctrl_reg &= ~BIT_RTC_ALARM_ENABLE;
  766. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
  767. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  768. if (rc) {
  769. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  770. dev_err(rtc_dd->rtc_dev,
  771. "Write to ALARM control reg failed\n");
  772. goto rtc_alarm_handled;
  773. }
  774. rtc_dd->alarm_ctrl_reg1 = ctrl_reg;
  775. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  776. /* Set ALARM_CLR bit */
  777. ctrl_reg = 0x1;
  778. rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg,
  779. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL2, 1);
  780. if (rc)
  781. dev_err(rtc_dd->rtc_dev,
  782. "Write to ALARM control reg failed\n");
  783. #ifdef CONFIG_RTC_AUTO_PWRON
  784. if ( poweroff_charging )
  785. pr_info("%s [SAPA] : irq(%d), lpm_mode\n", __func__, irq);
  786. if ( poweroff_charging && sapa_saved_time.enabled) {
  787. struct rtc_time now;
  788. struct rtc_wkalrm alarm;
  789. unsigned long curr_time, alarm_time, pwron_time;
  790. /* To wake up rtc device */
  791. wake_lock_timeout(&sapa_wakelock, HZ/2 );
  792. qpnp_rtc_read_time(rtc_dd->rtc_dev, &now);
  793. rtc_tm_to_time(&now, &curr_time);
  794. qpnp_rtc_read_alarm(rtc_dd->rtc_dev, &alarm);
  795. rtc_tm_to_time(&alarm.time, &alarm_time);
  796. rtc_tm_to_time(&sapa_saved_time.time, &pwron_time);
  797. pr_info("%s [SAPA] curr_time: %lu\n",__func__, curr_time);
  798. pr_info("%s [SAPA] pmic_time: %lu\n",__func__, alarm_time);
  799. pr_info("%s [SAPA] pwrontime: %lu [%d]\n",__func__, pwron_time, sapa_saved_time.enabled);
  800. if ( pwron_time <= curr_time && curr_time <= pwron_time+SAPA_BOOTING_TIME ) {
  801. wake_lock(&sapa_wakelock);
  802. rtc_dd->alarm_irq_flag = true;
  803. pr_info("%s [SAPA] Restart since RTC \n",__func__);
  804. queue_delayed_work(sapa_workq, &sapa_reboot_work, (1*HZ));
  805. }
  806. else {
  807. pr_info("%s [SAPA] not power on alarm.\n", __func__);
  808. if (!sapa_dev_suspend)
  809. qpnp_rtc0_resetbootalarm(rtc_dd->rtc_dev);
  810. }
  811. }
  812. #endif
  813. rtc_alarm_handled:
  814. return IRQ_HANDLED;
  815. }
  816. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  817. static int pm8926_bat_set_property(struct power_supply *psy,
  818. enum power_supply_property psp,
  819. const union power_supply_propval *val)
  820. {
  821. struct qpnp_rtc *rtc_dd =
  822. container_of(psy, struct qpnp_rtc, psy_pm8926);
  823. switch (psp) {
  824. case POWER_SUPPLY_PROP_PRESENT:
  825. if(val->intval != rtc_dd->battery_present)
  826. pr_debug("Cannot change value battery_present (%d) \n",rtc_dd->battery_present);
  827. default:
  828. return -EINVAL;
  829. }
  830. return 0;
  831. }
  832. static int pm8926_bat_get_property(struct power_supply *psy,
  833. enum power_supply_property psp,
  834. union power_supply_propval *val)
  835. {
  836. struct qpnp_rtc *rtc_dd =
  837. container_of(psy, struct qpnp_rtc, psy_pm8926);
  838. switch (psp) {
  839. case POWER_SUPPLY_PROP_PRESENT:
  840. val->intval = rtc_dd->battery_present;
  841. pr_debug("rtc: battery_present = %d \n",rtc_dd->battery_present);
  842. break;
  843. default:
  844. return -EINVAL;
  845. }
  846. return 0;
  847. }
  848. static irqreturn_t qpnp_batt_pres_irq_handler(int irq, void *dev_id)
  849. {
  850. struct qpnp_rtc *rtc_dd = dev_id;
  851. union power_supply_propval val;
  852. u8 reg;
  853. int rc;
  854. pr_info("##############################\n");
  855. pr_info("%s [SAPA] BAT_PRES TRIGGER\n",__func__);
  856. pr_info("##############################\n");
  857. rc = qpnp_read_wrapper(rtc_dd, &reg,
  858. rtc_dd->bat_base + INT_ADD, 1);
  859. if (rc < 0) {
  860. pr_err("%s qpnp read failed!\n",__func__);
  861. }
  862. reg = reg & BATT_PRES_IRQ;
  863. if(reg){
  864. pr_info("%s [SAPA] BATTERY PRESENT\n",__func__);
  865. rtc_dd->battery_present = 1;
  866. }else{
  867. pr_info("%s [SAPA] BATTERY ABSENT\n",__func__);
  868. rtc_dd->battery_present = 0;
  869. }
  870. val.intval = rtc_dd->battery_present;
  871. psy_do_property("battery", set,
  872. POWER_SUPPLY_PROP_PRESENT, val);
  873. return IRQ_HANDLED;
  874. }
  875. #endif
  876. static int __devinit qpnp_rtc_probe(struct spmi_device *spmi)
  877. {
  878. int rc;
  879. u8 subtype;
  880. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  881. u8 reg;
  882. #endif
  883. struct qpnp_rtc *rtc_dd;
  884. struct resource *resource;
  885. struct spmi_resource *spmi_resource;
  886. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  887. u8 alarm_reg=0;
  888. u8 value[4];
  889. unsigned long rtc_secs, pmic_secs;
  890. #endif
  891. rtc_dd = devm_kzalloc(&spmi->dev, sizeof(*rtc_dd), GFP_KERNEL);
  892. if (rtc_dd == NULL) {
  893. dev_err(&spmi->dev, "Unable to allocate memory!\n");
  894. return -ENOMEM;
  895. }
  896. /* Get the rtc write property */
  897. rc = of_property_read_u32(spmi->dev.of_node, "qcom,qpnp-rtc-write",
  898. &rtc_dd->rtc_write_enable);
  899. if (rc && rc != -EINVAL) {
  900. dev_err(&spmi->dev,
  901. "Error reading rtc_write_enable property %d\n", rc);
  902. return rc;
  903. }
  904. rc = of_property_read_u32(spmi->dev.of_node,
  905. "qcom,qpnp-rtc-alarm-pwrup",
  906. &rtc_dd->rtc_alarm_powerup);
  907. if (rc && rc != -EINVAL) {
  908. dev_err(&spmi->dev,
  909. "Error reading rtc_alarm_powerup property %d\n", rc);
  910. return rc;
  911. }
  912. /* Initialise spinlock to protect RTC control register */
  913. spin_lock_init(&rtc_dd->alarm_ctrl_lock);
  914. rtc_dd->rtc_dev = &(spmi->dev);
  915. rtc_dd->spmi = spmi;
  916. /* Get RTC/ALARM resources */
  917. spmi_for_each_container_dev(spmi_resource, spmi) {
  918. if (!spmi_resource) {
  919. dev_err(&spmi->dev,
  920. "%s: rtc_alarm: spmi resource absent!\n",
  921. __func__);
  922. rc = -ENXIO;
  923. goto fail_rtc_enable;
  924. }
  925. resource = spmi_get_resource(spmi, spmi_resource,
  926. IORESOURCE_MEM, 0);
  927. if (!(resource && resource->start)) {
  928. dev_err(&spmi->dev,
  929. "%s: node %s IO resource absent!\n",
  930. __func__, spmi->dev.of_node->full_name);
  931. rc = -ENXIO;
  932. goto fail_rtc_enable;
  933. }
  934. rc = qpnp_read_wrapper(rtc_dd, &subtype,
  935. resource->start + REG_OFFSET_PERP_SUBTYPE, 1);
  936. if (rc) {
  937. dev_err(&spmi->dev,
  938. "Peripheral subtype read failed\n");
  939. goto fail_rtc_enable;
  940. }
  941. switch (subtype) {
  942. case RTC_PERPH_SUBTYPE:
  943. rtc_dd->rtc_base = resource->start;
  944. break;
  945. case ALARM_PERPH_SUBTYPE:
  946. rtc_dd->alarm_base = resource->start;
  947. rtc_dd->rtc_alarm_irq =
  948. spmi_get_irq(spmi, spmi_resource, 0);
  949. if (rtc_dd->rtc_alarm_irq < 0) {
  950. dev_err(&spmi->dev, "ALARM IRQ absent\n");
  951. rc = -ENXIO;
  952. goto fail_rtc_enable;
  953. }
  954. break;
  955. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  956. case RTC_BATT_PRES_IRQ:
  957. rtc_dd->bat_base = resource->start;
  958. rtc_dd->bat_pres_irq = spmi_get_irq(spmi, spmi_resource, 0);
  959. break;
  960. #endif
  961. default:
  962. dev_err(&spmi->dev, "Invalid peripheral subtype\n");
  963. rc = -EINVAL;
  964. goto fail_rtc_enable;
  965. }
  966. }
  967. rc = qpnp_read_wrapper(rtc_dd, &rtc_dd->rtc_ctrl_reg,
  968. rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
  969. if (rc) {
  970. dev_err(&spmi->dev,
  971. "Read from RTC control reg failed\n");
  972. goto fail_rtc_enable;
  973. }
  974. if (!(rtc_dd->rtc_ctrl_reg & BIT_RTC_ENABLE)) {
  975. dev_err(&spmi->dev,
  976. "RTC h/w disabled, rtc not registered\n");
  977. goto fail_rtc_enable;
  978. }
  979. rc = qpnp_read_wrapper(rtc_dd, &rtc_dd->alarm_ctrl_reg1,
  980. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  981. if (rc) {
  982. dev_err(&spmi->dev,
  983. "Read from Alarm control reg failed\n");
  984. goto fail_rtc_enable;
  985. }
  986. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  987. alarm_reg = rtc_dd->alarm_ctrl_reg1;
  988. #endif
  989. /* Enable abort enable feature */
  990. rtc_dd->alarm_ctrl_reg1 |= BIT_RTC_ABORT_ENABLE;
  991. rc = qpnp_write_wrapper(rtc_dd, &rtc_dd->alarm_ctrl_reg1,
  992. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  993. if (rc) {
  994. dev_err(&spmi->dev, "SPMI write failed!\n");
  995. goto fail_rtc_enable;
  996. }
  997. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  998. rc = qpnp_read_wrapper(rtc_dd, value,
  999. rtc_dd->rtc_base + REG_OFFSET_RTC_READ, NUM_8_BIT_RTC_REGS);
  1000. if (rc) pr_err("Read from RTC reg failed\n");
  1001. rtc_secs = TO_SECS(value);
  1002. rc = qpnp_read_wrapper(rtc_dd, value,
  1003. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW, NUM_8_BIT_RTC_REGS);
  1004. if (rc) pr_err("Read from ALARM reg failed\n");
  1005. pmic_secs = TO_SECS(value);
  1006. pr_info("[SAPA] alarm_reg=%02x, rtc=%lu pmic=%lu\n", alarm_reg, rtc_secs, pmic_secs);
  1007. #endif
  1008. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  1009. reg = 0xFF;
  1010. spmi_ext_register_writel(rtc_dd->spmi->ctrl, rtc_dd->spmi->sid, rtc_dd->bat_base + 0x48, &reg, 1);
  1011. #endif
  1012. if (rtc_dd->rtc_write_enable == true)
  1013. qpnp_rtc_ops.set_time = qpnp_rtc_set_time;
  1014. dev_set_drvdata(&spmi->dev, rtc_dd);
  1015. /* Register the RTC device */
  1016. rtc_dd->rtc = rtc_device_register("qpnp_rtc", &spmi->dev,
  1017. &qpnp_rtc_ops, THIS_MODULE);
  1018. if (IS_ERR(rtc_dd->rtc)) {
  1019. dev_err(&spmi->dev, "%s: RTC registration failed (%ld)\n",
  1020. __func__, PTR_ERR(rtc_dd->rtc));
  1021. rc = PTR_ERR(rtc_dd->rtc);
  1022. goto fail_rtc_enable;
  1023. }
  1024. /* Request the alarm IRQ */
  1025. rc = request_any_context_irq(rtc_dd->rtc_alarm_irq,
  1026. qpnp_alarm_trigger, IRQF_TRIGGER_RISING,
  1027. "qpnp_rtc_alarm", rtc_dd);
  1028. if (rc) {
  1029. dev_err(&spmi->dev, "Request IRQ failed (%d)\n", rc);
  1030. goto fail_req_irq;
  1031. }
  1032. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  1033. rc = request_any_context_irq(rtc_dd->bat_pres_irq,
  1034. qpnp_batt_pres_irq_handler, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
  1035. | IRQF_SHARED | IRQF_ONESHOT,
  1036. "batt-pres", rtc_dd);
  1037. if (rc) {
  1038. dev_err(&spmi->dev, "Request IRQ BATT_PRES failed (%d)\n", rc);
  1039. goto fail_req_irq;
  1040. }
  1041. #endif
  1042. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1043. sapa_rtc_dev = rtc_dd->rtc_dev;
  1044. sapa_workq = create_singlethread_workqueue("pwron_alarm_resume");
  1045. if (sapa_workq == NULL) {
  1046. pr_err("[SAPA] pwron_alarm work creating failed (%d)\n", rc);
  1047. }
  1048. wake_lock_init(&sapa_wakelock, WAKE_LOCK_SUSPEND, "alarm_trigger");
  1049. #endif
  1050. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  1051. /* Initialize battery present */
  1052. rtc_dd->battery_present = 1;
  1053. qpnp_batt_pres_irq_handler(rtc_dd->bat_pres_irq, rtc_dd);
  1054. rtc_dd->psy_pm8926.name = "pm8926",
  1055. rtc_dd->psy_pm8926.type = POWER_SUPPLY_TYPE_BATTERY,
  1056. rtc_dd->psy_pm8926.properties = pm8926_battery_props,
  1057. rtc_dd->psy_pm8926.num_properties = ARRAY_SIZE(pm8926_battery_props),
  1058. rtc_dd->psy_pm8926.get_property = pm8926_bat_get_property,
  1059. rtc_dd->psy_pm8926.set_property = pm8926_bat_set_property,
  1060. rc = power_supply_register(&spmi->dev, &rtc_dd->psy_pm8926);
  1061. if (rc) {
  1062. dev_err(&spmi->dev,
  1063. "%s: Failed to Register psy_pm8926 \n", __func__);
  1064. goto fail_supply_unreg_pm8926;
  1065. }
  1066. dev_dbg(&spmi->dev,"Power-supply pm8926 registered successfully \n");
  1067. #endif
  1068. #ifdef CONFIG_SEC_PM
  1069. wake_lock_init(&resume_wakelock, WAKE_LOCK_SUSPEND, "resume_wakelock");
  1070. #endif
  1071. device_init_wakeup(&spmi->dev, 1);
  1072. enable_irq_wake(rtc_dd->rtc_alarm_irq);
  1073. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  1074. enable_irq_wake(rtc_dd->bat_pres_irq);
  1075. #endif
  1076. dev_err(&spmi->dev, "Probe success !!\n");
  1077. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1078. rtc_dd->lpm_mode = poweroff_charging;
  1079. rtc_dd->alarm_irq_flag = false;
  1080. /* To read saved power on alarm time */
  1081. if ( poweroff_charging ) {
  1082. sapa_check_workq = create_singlethread_workqueue("pwron_alarm_check");
  1083. if (sapa_check_workq == NULL) {
  1084. pr_err("[SAPA] pwron_alarm_check work creating failed (%d)\n", rc);
  1085. }
  1086. INIT_DELAYED_WORK(&sapa_load_param_work, sapa_load_kparam);
  1087. INIT_DELAYED_WORK(&sapa_reboot_work, sapa_reboot);
  1088. INIT_DELAYED_WORK(&sapa_check_work, sapa_check_alarm);
  1089. queue_delayed_work(sapa_workq, &sapa_load_param_work, (5*HZ));
  1090. queue_delayed_work(sapa_check_workq, &sapa_check_work, (60*HZ));
  1091. }
  1092. #endif
  1093. return 0;
  1094. fail_req_irq:
  1095. rtc_device_unregister(rtc_dd->rtc);
  1096. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  1097. fail_supply_unreg_pm8926:
  1098. power_supply_unregister(&rtc_dd->psy_pm8926);
  1099. #endif
  1100. fail_rtc_enable:
  1101. dev_set_drvdata(&spmi->dev, NULL);
  1102. return rc;
  1103. }
  1104. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1105. static int qpnp_rtc_auto_pwron_resume(struct device *dev)
  1106. {
  1107. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  1108. if (device_may_wakeup(dev))
  1109. disable_irq_wake(rtc_dd->rtc_alarm_irq);
  1110. sapa_dev_suspend = 0;
  1111. qpnp_rtc0_resetbootalarm(dev);
  1112. if(rtc_dd->lpm_mode==1)
  1113. queue_delayed_work(sapa_check_workq, &sapa_check_work, (1*HZ));
  1114. pr_info("%s\n",__func__);
  1115. return 0;
  1116. }
  1117. static int qpnp_rtc_auto_pwron_suspend(struct device *dev)
  1118. {
  1119. struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev);
  1120. if (device_may_wakeup(dev))
  1121. enable_irq_wake(rtc_dd->rtc_alarm_irq);
  1122. sapa_dev_suspend = 1;
  1123. if(rtc_dd->lpm_mode==1)
  1124. cancel_delayed_work_sync(&sapa_check_work);
  1125. pr_info("%s\n",__func__);
  1126. return 0;
  1127. }
  1128. #endif
  1129. #ifdef CONFIG_SEC_PM
  1130. static int qpnp_rtc_resume(struct device *dev)
  1131. {
  1132. pr_info("%s\n",__func__);
  1133. wake_lock_timeout(&resume_wakelock, HZ/10);
  1134. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1135. qpnp_rtc_auto_pwron_resume(dev);
  1136. #endif
  1137. return 0;
  1138. }
  1139. static int qpnp_rtc_suspend(struct device *dev)
  1140. {
  1141. pr_info("%s\n",__func__);
  1142. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1143. qpnp_rtc_auto_pwron_suspend(dev);
  1144. #endif
  1145. return 0;
  1146. }
  1147. static const struct dev_pm_ops qpnp_rtc_pm_ops = {
  1148. .suspend = qpnp_rtc_suspend,
  1149. .resume = qpnp_rtc_resume,
  1150. };
  1151. #endif
  1152. static int __devexit qpnp_rtc_remove(struct spmi_device *spmi)
  1153. {
  1154. struct qpnp_rtc *rtc_dd = dev_get_drvdata(&spmi->dev);
  1155. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1156. destroy_workqueue(sapa_workq);
  1157. #endif
  1158. device_init_wakeup(&spmi->dev, 0);
  1159. free_irq(rtc_dd->rtc_alarm_irq, rtc_dd);
  1160. #if defined(CONFIG_PM8926_BATTERY_CHECK_INTERRUPT)
  1161. free_irq(rtc_dd->bat_pres_irq, rtc_dd);
  1162. #endif
  1163. rtc_device_unregister(rtc_dd->rtc);
  1164. dev_set_drvdata(&spmi->dev, NULL);
  1165. return 0;
  1166. }
  1167. #ifdef CONFIG_RTC_AUTO_PWRON
  1168. static void qpnp_rtc_shutdown(struct spmi_device *spmi)
  1169. {
  1170. u8 value[4] = {0};
  1171. unsigned long secs;
  1172. u8 ctrl_reg;
  1173. int rc;
  1174. struct qpnp_rtc *rtc_dd = dev_get_drvdata(&spmi->dev);
  1175. shutdown_loaded = 1;
  1176. qpnp_rtc0_resetbootalarm(&spmi->dev);
  1177. /* Check if the RTC is on, else turn it on */
  1178. rc = qpnp_read_wrapper(rtc_dd, &ctrl_reg,
  1179. rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1);
  1180. if (rc < 0) {
  1181. dev_err(&spmi->dev, "%s qpnp read failed!\n",__func__);
  1182. }
  1183. rc = qpnp_read_wrapper(rtc_dd, value,
  1184. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  1185. NUM_8_BIT_RTC_REGS);
  1186. secs = value[0] | (value[1] << 8) | (value[2] << 16) \
  1187. | (value[3] << 24);
  1188. pr_info("%s : secs = %lu\n", __func__,secs);
  1189. pr_info("%s RTC Register : %d \n", __func__, ctrl_reg);
  1190. #ifdef CONFIG_RTC_AUTO_PWRON_PARAM
  1191. wake_lock_destroy(&sapa_wakelock);
  1192. #endif
  1193. #ifdef CONFIG_SEC_PM
  1194. wake_lock_destroy(&resume_wakelock);
  1195. #endif
  1196. }
  1197. #else
  1198. static void qpnp_rtc_shutdown(struct spmi_device *spmi)
  1199. {
  1200. u8 value[4] = {0};
  1201. u8 reg;
  1202. int rc;
  1203. unsigned long irq_flags;
  1204. struct qpnp_rtc *rtc_dd = dev_get_drvdata(&spmi->dev);
  1205. bool rtc_alarm_powerup = rtc_dd->rtc_alarm_powerup;
  1206. if (!rtc_alarm_powerup && !poweron_alarm) {
  1207. spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags);
  1208. dev_dbg(&spmi->dev, "Disabling alarm interrupts\n");
  1209. /* Disable RTC alarms */
  1210. reg = rtc_dd->alarm_ctrl_reg1;
  1211. reg &= ~BIT_RTC_ALARM_ENABLE;
  1212. rc = qpnp_write_wrapper(rtc_dd, &reg,
  1213. rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1);
  1214. if (rc) {
  1215. dev_err(rtc_dd->rtc_dev, "SPMI write failed\n");
  1216. goto fail_alarm_disable;
  1217. }
  1218. /* Clear Alarm register */
  1219. rc = qpnp_write_wrapper(rtc_dd, value,
  1220. rtc_dd->alarm_base + REG_OFFSET_ALARM_RW,
  1221. NUM_8_BIT_RTC_REGS);
  1222. if (rc)
  1223. dev_err(rtc_dd->rtc_dev, "SPMI write failed\n");
  1224. fail_alarm_disable:
  1225. spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags);
  1226. }
  1227. #ifdef CONFIG_SEC_PM
  1228. wake_lock_destroy(&resume_wakelock);
  1229. #endif
  1230. }
  1231. #endif /* CONFIG_RTC_AUTO_PWRON */
  1232. static struct of_device_id spmi_match_table[] = {
  1233. {
  1234. .compatible = "qcom,qpnp-rtc",
  1235. },
  1236. {}
  1237. };
  1238. static struct spmi_driver qpnp_rtc_driver = {
  1239. .probe = qpnp_rtc_probe,
  1240. .remove = __devexit_p(qpnp_rtc_remove),
  1241. .shutdown = qpnp_rtc_shutdown,
  1242. .driver = {
  1243. .name = "qcom,qpnp-rtc",
  1244. .owner = THIS_MODULE,
  1245. .of_match_table = spmi_match_table,
  1246. #ifdef CONFIG_SEC_PM
  1247. .pm = &qpnp_rtc_pm_ops,
  1248. #endif
  1249. },
  1250. };
  1251. static int __init qpnp_rtc_init(void)
  1252. {
  1253. return spmi_driver_register(&qpnp_rtc_driver);
  1254. }
  1255. module_init(qpnp_rtc_init);
  1256. static void __exit qpnp_rtc_exit(void)
  1257. {
  1258. spmi_driver_unregister(&qpnp_rtc_driver);
  1259. }
  1260. module_exit(qpnp_rtc_exit);
  1261. MODULE_DESCRIPTION("SMPI PMIC RTC driver");
  1262. MODULE_LICENSE("GPL V2");