qpnp-power-on.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/slab.h>
  17. #include <linux/spmi.h>
  18. #include <linux/delay.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/input.h>
  23. #include <linux/log2.h>
  24. #include <linux/qpnp/power-on.h>
  25. #ifdef CONFIG_SEC_DEBUG
  26. #include <mach/sec_debug.h>
  27. #endif
  28. #define PMIC_VER_8941 0x01
  29. #define PMIC_VERSION_REG 0x0105
  30. #define PMIC_VERSION_REV4_REG 0x0103
  31. #define PMIC8941_V1_REV4 0x01
  32. #define PMIC8941_V2_REV4 0x02
  33. #define PON_REV2_VALUE 0x00
  34. #if defined(CONFIG_SEC_PATEK_PROJECT) || defined(CONFIG_SEC_S_PROJECT)
  35. static int check_pkey_press;
  36. #endif
  37. #ifndef CONFIG_SEC_DEBUG
  38. extern struct class *sec_class;
  39. #endif // !CONFIG_SEC_DEBUG
  40. /* Common PNP defines */
  41. #define QPNP_PON_REVISION2(base) (base + 0x01)
  42. /* PON common register addresses */
  43. #define QPNP_PON_RT_STS(base) (base + 0x10)
  44. #define QPNP_PON_PULL_CTL(base) (base + 0x70)
  45. #define QPNP_PON_DBC_CTL(base) (base + 0x71)
  46. /* PON/RESET sources register addresses */
  47. #define QPNP_PON_REASON1(base) (base + 0x8)
  48. #define QPNP_PON_WARM_RESET_REASON1(base) (base + 0xA)
  49. #define QPNP_PON_WARM_RESET_REASON2(base) (base + 0xB)
  50. #define QPNP_POFF_REASON1(base) (base + 0xC)
  51. #define QPNP_PON_KPDPWR_S1_TIMER(base) (base + 0x40)
  52. #define QPNP_PON_KPDPWR_S2_TIMER(base) (base + 0x41)
  53. #define QPNP_PON_KPDPWR_S2_CNTL(base) (base + 0x42)
  54. #define QPNP_PON_KPDPWR_S2_CNTL2(base) (base + 0x43)
  55. #define QPNP_PON_RESIN_S1_TIMER(base) (base + 0x44)
  56. #define QPNP_PON_RESIN_S2_TIMER(base) (base + 0x45)
  57. #define QPNP_PON_RESIN_S2_CNTL(base) (base + 0x46)
  58. #define QPNP_PON_RESIN_S2_CNTL2(base) (base + 0x47)
  59. #define QPNP_PON_KPDPWR_RESIN_S1_TIMER(base) (base + 0x48)
  60. #define QPNP_PON_KPDPWR_RESIN_S2_TIMER(base) (base + 0x49)
  61. #define QPNP_PON_KPDPWR_RESIN_S2_CNTL(base) (base + 0x4A)
  62. #define QPNP_PON_KPDPWR_RESIN_S2_CNTL2(base) (base + 0x4B)
  63. #define QPNP_PON_PS_HOLD_RST_CTL(base) (base + 0x5A)
  64. #define QPNP_PON_PS_HOLD_RST_CTL2(base) (base + 0x5B)
  65. #define QPNP_PON_WD_RST_S2_CTL(base) (base + 0x56)
  66. #define QPNP_PON_WD_RST_S2_CTL2(base) (base + 0x57)
  67. #define QPNP_PON_S3_SRC(base) (base + 0x74)
  68. #define QPNP_PON_S3_DBC_CTL(base) (base + 0x75)
  69. #define QPNP_PON_TRIGGER_EN(base) (base + 0x80)
  70. #define QPNP_PON_WARM_RESET_TFT BIT(4)
  71. #define QPNP_PON_RESIN_PULL_UP BIT(0)
  72. #define QPNP_PON_KPDPWR_PULL_UP BIT(1)
  73. #define QPNP_PON_CBLPWR_PULL_UP BIT(2)
  74. #define QPNP_PON_S2_CNTL_EN BIT(7)
  75. #define QPNP_PON_S2_RESET_ENABLE BIT(7)
  76. #define QPNP_PON_DELAY_BIT_SHIFT 6
  77. #define QPNP_PON_S1_TIMER_MASK (0xF)
  78. #define QPNP_PON_S2_TIMER_MASK (0x7)
  79. #define QPNP_PON_S2_CNTL_TYPE_MASK (0xF)
  80. #define QPNP_PON_DBC_DELAY_MASK (0x7)
  81. #define QPNP_PON_KPDPWR_N_SET BIT(0)
  82. #define QPNP_PON_RESIN_N_SET BIT(1)
  83. #define QPNP_PON_CBLPWR_N_SET BIT(2)
  84. #define QPNP_PON_RESIN_BARK_N_SET BIT(4)
  85. #define QPNP_PON_KPDPWR_RESIN_BARK_N_SET BIT(5)
  86. #define QPNP_PON_WD_EN BIT(7)
  87. #define QPNP_PON_RESET_EN BIT(7)
  88. #define QPNP_PON_POWER_OFF_MASK 0xF
  89. #define QPNP_PON_S3_SRC_KPDPWR 0
  90. #define QPNP_PON_S3_SRC_RESIN 1
  91. #define QPNP_PON_S3_SRC_KPDPWR_AND_RESIN 2
  92. #define QPNP_PON_S3_SRC_KPDPWR_OR_RESIN 3
  93. #define QPNP_PON_S3_SRC_MASK 0x3
  94. /* Ranges */
  95. #define QPNP_PON_S1_TIMER_MAX 10256
  96. #define QPNP_PON_S2_TIMER_MAX 2000
  97. #define QPNP_PON_S3_TIMER_SECS_MAX 128
  98. #define QPNP_PON_S3_DBC_DELAY_MASK 0x07
  99. #define QPNP_PON_RESET_TYPE_MAX 0xF
  100. #define PON_S1_COUNT_MAX 0xF
  101. #define QPNP_PON_MIN_DBC_US (USEC_PER_SEC / 64)
  102. #define QPNP_PON_MAX_DBC_US (USEC_PER_SEC * 2)
  103. #define QPNP_KEY_STATUS_DELAY msecs_to_jiffies(250)
  104. enum pon_type {
  105. PON_KPDPWR,
  106. PON_RESIN,
  107. PON_CBLPWR,
  108. PON_KPDPWR_RESIN,
  109. };
  110. struct qpnp_pon_config {
  111. u32 pon_type;
  112. u32 support_reset;
  113. #ifdef CONFIG_SEC_PM
  114. u32 disable_reset;
  115. #endif
  116. u32 key_code;
  117. u32 s1_timer;
  118. u32 s2_timer;
  119. u32 s2_type;
  120. u32 pull_up;
  121. u32 state_irq;
  122. u32 bark_irq;
  123. u16 s2_cntl_addr;
  124. u16 s2_cntl2_addr;
  125. bool use_bark;
  126. };
  127. struct qpnp_pon {
  128. struct spmi_device *spmi;
  129. struct input_dev *pon_input;
  130. struct qpnp_pon_config *pon_cfg;
  131. int num_pon_config;
  132. int powerkey_state;
  133. u16 base;
  134. struct delayed_work bark_work;
  135. };
  136. static struct qpnp_pon *sys_reset_dev;
  137. #ifdef CONFIG_SEC_PM_DEBUG
  138. static int wake_enabled;
  139. static int reset_enabled;
  140. #endif
  141. static u32 s1_delay[PON_S1_COUNT_MAX + 1] = {
  142. 0 , 32, 56, 80, 138, 184, 272, 408, 608, 904, 1352, 2048,
  143. 3072, 4480, 6720, 10256
  144. };
  145. static const char * const qpnp_pon_reason[] = {
  146. [0] = "Triggered from Hard Reset",
  147. [1] = "Triggered from SMPL (sudden momentary power loss)",
  148. [2] = "Triggered from RTC (RTC alarm expiry)",
  149. [3] = "Triggered from DC (DC charger insertion)",
  150. [4] = "Triggered from USB (USB charger insertion)",
  151. [5] = "Triggered from PON1 (secondary PMIC)",
  152. [6] = "Triggered from CBL (external power supply)",
  153. [7] = "Triggered from KPD (power key press)",
  154. };
  155. static const char * const qpnp_poff_reason[] = {
  156. [0] = "Triggered from SOFT (Software)",
  157. [1] = "Triggered from PS_HOLD (PS_HOLD/MSM controlled shutdown)",
  158. [2] = "Triggered from PMIC_WD (PMIC watchdog)",
  159. [3] = "Triggered from GP1 (Keypad_Reset1)",
  160. [4] = "Triggered from GP2 (Keypad_Reset2)",
  161. [5] = "Triggered from KPDPWR_AND_RESIN"
  162. "(Simultaneous power key and reset line)",
  163. [6] = "Triggered from RESIN_N (Reset line/Volume Down Key)",
  164. [7] = "Triggered from KPDPWR_N (Long Power Key hold)",
  165. [8] = "N/A",
  166. [9] = "N/A",
  167. [10] = "N/A",
  168. [11] = "Triggered from CHARGER (Charger ENUM_TIMER, BOOT_DONE)",
  169. [12] = "Triggered from TFT (Thermal Fault Tolerance)",
  170. [13] = "Triggered from UVLO (Under Voltage Lock Out)",
  171. [14] = "Triggered from OTST3 (Overtemp)",
  172. [15] = "Triggered from STAGE3 (Stage 3 reset)",
  173. };
  174. static int
  175. qpnp_pon_masked_write(struct qpnp_pon *pon, u16 addr, u8 mask, u8 val)
  176. {
  177. int rc;
  178. u8 reg;
  179. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  180. addr, &reg, 1);
  181. if (rc) {
  182. dev_err(&pon->spmi->dev,
  183. "Unable to read from addr=%x, rc(%d)\n", addr, rc);
  184. return rc;
  185. }
  186. reg &= ~mask;
  187. reg |= val & mask;
  188. rc = spmi_ext_register_writel(pon->spmi->ctrl, pon->spmi->sid,
  189. addr, &reg, 1);
  190. if (rc)
  191. dev_err(&pon->spmi->dev,
  192. "Unable to write to addr=%x, rc(%d)\n", addr, rc);
  193. return rc;
  194. }
  195. /**
  196. * qpnp_pon_system_pwr_off - Configure system-reset PMIC for shutdown or reset
  197. * @type: Determines the type of power off to perform - shutdown, reset, etc
  198. *
  199. * This function will only configure a single PMIC. The other PMICs in the
  200. * system are slaved off of it and require no explicit configuration. Once
  201. * the system-reset PMIC is configured properly, the MSM can drop PS_HOLD to
  202. * activate the specified configuration.
  203. */
  204. int qpnp_pon_system_pwr_off(enum pon_power_off_type type)
  205. {
  206. int rc;
  207. u8 reg;
  208. u16 rst_en_reg;
  209. struct qpnp_pon *pon = sys_reset_dev;
  210. if (!pon)
  211. return -ENODEV;
  212. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  213. QPNP_PON_REVISION2(pon->base), &reg, 1);
  214. if (rc) {
  215. dev_err(&pon->spmi->dev,
  216. "Unable to read addr=%x, rc(%d)\n",
  217. QPNP_PON_REVISION2(pon->base), rc);
  218. return rc;
  219. }
  220. if (reg == PON_REV2_VALUE)
  221. rst_en_reg = QPNP_PON_PS_HOLD_RST_CTL(pon->base);
  222. else
  223. rst_en_reg = QPNP_PON_PS_HOLD_RST_CTL2(pon->base);
  224. rc = qpnp_pon_masked_write(pon, rst_en_reg, QPNP_PON_RESET_EN, 0);
  225. if (rc)
  226. dev_err(&pon->spmi->dev,
  227. "Unable to write to addr=%x, rc(%d)\n", rst_en_reg, rc);
  228. /*
  229. * We need 10 sleep clock cycles here. But since the clock is
  230. * internally generated, we need to add 50% tolerance to be
  231. * conservative.
  232. */
  233. udelay(500);
  234. rc = qpnp_pon_masked_write(pon, QPNP_PON_PS_HOLD_RST_CTL(pon->base),
  235. QPNP_PON_POWER_OFF_MASK, type);
  236. if (rc)
  237. dev_err(&pon->spmi->dev,
  238. "Unable to write to addr=%x, rc(%d)\n",
  239. QPNP_PON_PS_HOLD_RST_CTL(pon->base), rc);
  240. rc = qpnp_pon_masked_write(pon, rst_en_reg, QPNP_PON_RESET_EN,
  241. QPNP_PON_RESET_EN);
  242. if (rc)
  243. dev_err(&pon->spmi->dev,
  244. "Unable to write to addr=%x, rc(%d)\n", rst_en_reg, rc);
  245. dev_dbg(&pon->spmi->dev, "power off type = 0x%02X\n", type);
  246. return rc;
  247. }
  248. EXPORT_SYMBOL(qpnp_pon_system_pwr_off);
  249. /**
  250. * qpnp_pon_is_warm_reset - Checks if the PMIC went through a warm reset.
  251. *
  252. * Returns > 0 for warm resets, 0 for not warm reset, < 0 for errors
  253. *
  254. * Note that this function will only return the warm vs not-warm reset status
  255. * of the PMIC that is configured as the system-reset device.
  256. */
  257. int qpnp_pon_is_warm_reset(void)
  258. {
  259. struct qpnp_pon *pon = sys_reset_dev;
  260. int rc;
  261. u8 reg;
  262. if (!pon)
  263. return -EPROBE_DEFER;
  264. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  265. QPNP_PON_WARM_RESET_REASON1(pon->base), &reg, 1);
  266. if (rc) {
  267. dev_err(&pon->spmi->dev,
  268. "Unable to read addr=%x, rc(%d)\n",
  269. QPNP_PON_WARM_RESET_REASON1(pon->base), rc);
  270. return rc;
  271. }
  272. if (reg)
  273. return 1;
  274. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  275. QPNP_PON_WARM_RESET_REASON2(pon->base), &reg, 1);
  276. if (rc) {
  277. dev_err(&pon->spmi->dev,
  278. "Unable to read addr=%x, rc(%d)\n",
  279. QPNP_PON_WARM_RESET_REASON2(pon->base), rc);
  280. return rc;
  281. }
  282. if (reg & QPNP_PON_WARM_RESET_TFT)
  283. return 1;
  284. return 0;
  285. }
  286. EXPORT_SYMBOL(qpnp_pon_is_warm_reset);
  287. /**
  288. * qpnp_pon_wd_config - Disable the wd in a warm reset.
  289. * @enable: to enable or disable the PON watch dog
  290. *
  291. * Returns = 0 for operate successfully, < 0 for errors
  292. */
  293. int qpnp_pon_wd_config(bool enable)
  294. {
  295. struct qpnp_pon *pon = sys_reset_dev;
  296. int rc = 0;
  297. if (!pon)
  298. return -EPROBE_DEFER;
  299. rc = qpnp_pon_masked_write(pon, QPNP_PON_WD_RST_S2_CTL2(pon->base),
  300. QPNP_PON_WD_EN, enable ? QPNP_PON_WD_EN : 0);
  301. if (rc)
  302. dev_err(&pon->spmi->dev,
  303. "Unable to write to addr=%x, rc(%d)\n",
  304. QPNP_PON_WD_RST_S2_CTL2(pon->base), rc);
  305. return rc;
  306. }
  307. EXPORT_SYMBOL(qpnp_pon_wd_config);
  308. /**
  309. * qpnp_pon_trigger_config - Configures (enable/disable) the PON trigger source
  310. * @pon_src: PON source to be configured
  311. * @enable: to enable or disable the PON trigger
  312. *
  313. * This function configures the power-on trigger capability of a
  314. * PON source. If a specific PON trigger is disabled it cannot act
  315. * as a power-on source to the PMIC.
  316. */
  317. int qpnp_pon_trigger_config(enum pon_trigger_source pon_src, bool enable)
  318. {
  319. struct qpnp_pon *pon = sys_reset_dev;
  320. int rc;
  321. if (!pon)
  322. return -EPROBE_DEFER;
  323. if (pon_src < PON_SMPL || pon_src > PON_KPDPWR_N) {
  324. dev_err(&pon->spmi->dev, "Invalid PON source\n");
  325. return -EINVAL;
  326. }
  327. rc = qpnp_pon_masked_write(pon, QPNP_PON_TRIGGER_EN(pon->base),
  328. BIT(pon_src), enable ? BIT(pon_src) : 0);
  329. if (rc)
  330. dev_err(&pon->spmi->dev, "Unable to write to addr=%x, rc(%d)\n",
  331. QPNP_PON_TRIGGER_EN(pon->base), rc);
  332. return rc;
  333. }
  334. EXPORT_SYMBOL(qpnp_pon_trigger_config);
  335. static struct qpnp_pon_config *
  336. qpnp_get_cfg(struct qpnp_pon *pon, u32 pon_type)
  337. {
  338. int i;
  339. for (i = 0; i < pon->num_pon_config; i++) {
  340. if (pon_type == pon->pon_cfg[i].pon_type)
  341. return &pon->pon_cfg[i];
  342. }
  343. return NULL;
  344. }
  345. static int
  346. qpnp_pon_input_dispatch(struct qpnp_pon *pon, u32 pon_type)
  347. {
  348. int rc;
  349. struct qpnp_pon_config *cfg = NULL;
  350. u8 pon_rt_sts = 0, pon_rt_bit = 0;
  351. cfg = qpnp_get_cfg(pon, pon_type);
  352. if (!cfg)
  353. return -EINVAL;
  354. /* Check if key reporting is supported */
  355. if (!cfg->key_code)
  356. return 0;
  357. /* check the RT status to get the current status of the line */
  358. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  359. QPNP_PON_RT_STS(pon->base), &pon_rt_sts, 1);
  360. if (rc) {
  361. dev_err(&pon->spmi->dev, "Unable to read PON RT status\n");
  362. return rc;
  363. }
  364. switch (cfg->pon_type) {
  365. case PON_KPDPWR:
  366. pon_rt_bit = QPNP_PON_KPDPWR_N_SET;
  367. break;
  368. case PON_RESIN:
  369. pon_rt_bit = QPNP_PON_RESIN_N_SET;
  370. break;
  371. case PON_CBLPWR:
  372. pon_rt_bit = QPNP_PON_CBLPWR_N_SET;
  373. break;
  374. case PON_KPDPWR_RESIN:
  375. pon_rt_bit = QPNP_PON_KPDPWR_RESIN_BARK_N_SET;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. if (cfg->pon_type == PON_RESIN )
  381. printk(KERN_INFO "%s: VOLUME DOWN key is %s\n",
  382. __func__, (pon_rt_sts & pon_rt_bit) ? "pressed" : "released");
  383. else
  384. printk(KERN_INFO "%s: PWR key is %s\n",
  385. __func__, (pon_rt_sts & pon_rt_bit) ? "pressed" : "released");
  386. input_report_key(pon->pon_input, cfg->key_code,
  387. (pon_rt_sts & pon_rt_bit));
  388. input_sync(pon->pon_input);
  389. #ifdef CONFIG_SEC_PATEK_PROJECT
  390. if((cfg->key_code == KEY_END) && (pon_rt_sts & pon_rt_bit)){
  391. pon->powerkey_state = 1;
  392. }else if((cfg->key_code == KEY_END) && !(pon_rt_sts & pon_rt_bit)){
  393. pon->powerkey_state = 0;
  394. }
  395. #else
  396. if((cfg->key_code == 116) && (pon_rt_sts & pon_rt_bit)){
  397. pon->powerkey_state = 1;
  398. }else if((cfg->key_code == 116) && !(pon_rt_sts & pon_rt_bit)){
  399. pon->powerkey_state = 0;
  400. }
  401. #endif
  402. #if defined(CONFIG_SEC_PM)
  403. /* RESIN is used for VOL DOWN key, it should report the keycode for kernel panic */
  404. if((cfg->key_code == 114) && (pon_rt_sts & pon_rt_bit)){
  405. pon->powerkey_state = 1;
  406. }else if((cfg->key_code == 114) && !(pon_rt_sts & pon_rt_bit)){
  407. pon->powerkey_state = 0;
  408. }
  409. #endif
  410. #ifdef CONFIG_SEC_DEBUG
  411. #ifdef CONFIG_SEC_PATEK_PROJECT
  412. sec_debug_check_crash_key(116, pon->powerkey_state);
  413. #else
  414. sec_debug_check_crash_key(cfg->key_code, pon->powerkey_state);
  415. #endif
  416. #endif
  417. #if defined(CONFIG_SEC_PATEK_PROJECT) || defined(CONFIG_SEC_S_PROJECT)
  418. check_pkey_press=pon->powerkey_state;
  419. #endif
  420. return 0;
  421. }
  422. #if defined(CONFIG_SEC_PATEK_PROJECT) || defined(CONFIG_SEC_S_PROJECT)
  423. int check_short_pkey(void)
  424. {
  425. return check_pkey_press;
  426. }
  427. EXPORT_SYMBOL(check_short_pkey);
  428. #endif
  429. static irqreturn_t qpnp_kpdpwr_irq(int irq, void *_pon)
  430. {
  431. int rc;
  432. struct qpnp_pon *pon = _pon;
  433. rc = qpnp_pon_input_dispatch(pon, PON_KPDPWR);
  434. if (rc)
  435. dev_err(&pon->spmi->dev, "Unable to send input event\n");
  436. return IRQ_HANDLED;
  437. }
  438. static irqreturn_t qpnp_kpdpwr_bark_irq(int irq, void *_pon)
  439. {
  440. return IRQ_HANDLED;
  441. }
  442. static irqreturn_t qpnp_resin_irq(int irq, void *_pon)
  443. {
  444. int rc;
  445. struct qpnp_pon *pon = _pon;
  446. rc = qpnp_pon_input_dispatch(pon, PON_RESIN);
  447. if (rc)
  448. dev_err(&pon->spmi->dev, "Unable to send input event\n");
  449. return IRQ_HANDLED;
  450. }
  451. static irqreturn_t qpnp_kpdpwr_resin_bark_irq(int irq, void *_pon)
  452. {
  453. return IRQ_HANDLED;
  454. }
  455. static irqreturn_t qpnp_cblpwr_irq(int irq, void *_pon)
  456. {
  457. int rc;
  458. struct qpnp_pon *pon = _pon;
  459. rc = qpnp_pon_input_dispatch(pon, PON_CBLPWR);
  460. if (rc)
  461. dev_err(&pon->spmi->dev, "Unable to send input event\n");
  462. return IRQ_HANDLED;
  463. }
  464. static void bark_work_func(struct work_struct *work)
  465. {
  466. int rc;
  467. u8 pon_rt_sts = 0;
  468. struct qpnp_pon_config *cfg;
  469. struct qpnp_pon *pon =
  470. container_of(work, struct qpnp_pon, bark_work.work);
  471. cfg = qpnp_get_cfg(pon, PON_RESIN);
  472. if (!cfg) {
  473. dev_err(&pon->spmi->dev, "Invalid config pointer\n");
  474. goto err_return;
  475. }
  476. /* enable reset */
  477. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  478. QPNP_PON_S2_CNTL_EN, QPNP_PON_S2_CNTL_EN);
  479. if (rc) {
  480. dev_err(&pon->spmi->dev, "Unable to configure S2 enable\n");
  481. goto err_return;
  482. }
  483. /* bark RT status update delay */
  484. msleep(100);
  485. /* read the bark RT status */
  486. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  487. QPNP_PON_RT_STS(pon->base), &pon_rt_sts, 1);
  488. if (rc) {
  489. dev_err(&pon->spmi->dev, "Unable to read PON RT status\n");
  490. goto err_return;
  491. }
  492. if (!(pon_rt_sts & QPNP_PON_RESIN_BARK_N_SET)) {
  493. /* report the key event and enable the bark IRQ */
  494. input_report_key(pon->pon_input, cfg->key_code, 0);
  495. input_sync(pon->pon_input);
  496. enable_irq(cfg->bark_irq);
  497. } else {
  498. /* disable reset */
  499. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  500. QPNP_PON_S2_CNTL_EN, 0);
  501. if (rc) {
  502. dev_err(&pon->spmi->dev,
  503. "Unable to configure S2 enable\n");
  504. goto err_return;
  505. }
  506. /* re-arm the work */
  507. schedule_delayed_work(&pon->bark_work, QPNP_KEY_STATUS_DELAY);
  508. }
  509. err_return:
  510. return;
  511. }
  512. static irqreturn_t qpnp_resin_bark_irq(int irq, void *_pon)
  513. {
  514. int rc;
  515. struct qpnp_pon *pon = _pon;
  516. struct qpnp_pon_config *cfg;
  517. /* disable the bark interrupt */
  518. disable_irq_nosync(irq);
  519. cfg = qpnp_get_cfg(pon, PON_RESIN);
  520. if (!cfg) {
  521. dev_err(&pon->spmi->dev, "Invalid config pointer\n");
  522. goto err_exit;
  523. }
  524. /* disable reset */
  525. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  526. QPNP_PON_S2_CNTL_EN, 0);
  527. if (rc) {
  528. dev_err(&pon->spmi->dev, "Unable to configure S2 enable\n");
  529. goto err_exit;
  530. }
  531. /* report the key event */
  532. input_report_key(pon->pon_input, cfg->key_code, 1);
  533. input_sync(pon->pon_input);
  534. /* schedule work to check the bark status for key-release */
  535. schedule_delayed_work(&pon->bark_work, QPNP_KEY_STATUS_DELAY);
  536. err_exit:
  537. return IRQ_HANDLED;
  538. }
  539. static int __devinit
  540. qpnp_config_pull(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  541. {
  542. int rc;
  543. u8 pull_bit;
  544. #if defined(CONFIG_SEC_K_PROJECT) || \
  545. defined(CONFIG_SEC_KACTIVE_PROJECT) || defined(CONFIG_SEC_KSPORTS_PROJECT) || \
  546. defined(CONFIG_SEC_S_PROJECT) || defined(CONFIG_SEC_PATEK_PROJECT)
  547. /* Do nothing in case of KPDPWR_RESIN*/
  548. if (cfg->pon_type == PON_KPDPWR_RESIN)
  549. return 0;
  550. #endif
  551. switch (cfg->pon_type) {
  552. case PON_KPDPWR:
  553. pull_bit = QPNP_PON_KPDPWR_PULL_UP;
  554. break;
  555. case PON_RESIN:
  556. pull_bit = QPNP_PON_RESIN_PULL_UP;
  557. break;
  558. case PON_CBLPWR:
  559. pull_bit = QPNP_PON_CBLPWR_PULL_UP;
  560. break;
  561. case PON_KPDPWR_RESIN:
  562. pull_bit = QPNP_PON_KPDPWR_PULL_UP | QPNP_PON_RESIN_PULL_UP;
  563. break;
  564. default:
  565. return -EINVAL;
  566. }
  567. rc = qpnp_pon_masked_write(pon, QPNP_PON_PULL_CTL(pon->base),
  568. pull_bit, cfg->pull_up ? pull_bit : 0);
  569. if (rc)
  570. dev_err(&pon->spmi->dev, "Unable to config pull-up\n");
  571. return rc;
  572. }
  573. static int __devinit
  574. qpnp_config_reset(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  575. {
  576. int rc;
  577. u8 i;
  578. u16 s1_timer_addr, s2_timer_addr;
  579. switch (cfg->pon_type) {
  580. case PON_KPDPWR:
  581. s1_timer_addr = QPNP_PON_KPDPWR_S1_TIMER(pon->base);
  582. s2_timer_addr = QPNP_PON_KPDPWR_S2_TIMER(pon->base);
  583. break;
  584. case PON_RESIN:
  585. s1_timer_addr = QPNP_PON_RESIN_S1_TIMER(pon->base);
  586. s2_timer_addr = QPNP_PON_RESIN_S2_TIMER(pon->base);
  587. break;
  588. case PON_KPDPWR_RESIN:
  589. s1_timer_addr = QPNP_PON_KPDPWR_RESIN_S1_TIMER(pon->base);
  590. s2_timer_addr = QPNP_PON_KPDPWR_RESIN_S2_TIMER(pon->base);
  591. break;
  592. default:
  593. return -EINVAL;
  594. }
  595. /* disable S2 reset */
  596. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  597. QPNP_PON_S2_CNTL_EN, 0);
  598. if (rc) {
  599. dev_err(&pon->spmi->dev, "Unable to configure S2 enable\n");
  600. return rc;
  601. }
  602. usleep(100);
  603. /* configure s1 timer, s2 timer and reset type */
  604. for (i = 0; i < PON_S1_COUNT_MAX + 1; i++) {
  605. if (cfg->s1_timer <= s1_delay[i])
  606. break;
  607. }
  608. rc = qpnp_pon_masked_write(pon, s1_timer_addr,
  609. QPNP_PON_S1_TIMER_MASK, i);
  610. if (rc) {
  611. dev_err(&pon->spmi->dev, "Unable to configure S1 timer\n");
  612. return rc;
  613. }
  614. i = 0;
  615. if (cfg->s2_timer) {
  616. i = cfg->s2_timer / 10;
  617. i = ilog2(i + 1);
  618. }
  619. rc = qpnp_pon_masked_write(pon, s2_timer_addr,
  620. QPNP_PON_S2_TIMER_MASK, i);
  621. if (rc) {
  622. dev_err(&pon->spmi->dev, "Unable to configure S2 timer\n");
  623. return rc;
  624. }
  625. #ifdef CONFIG_ARCH_MSM8226
  626. #ifdef CONFIG_SEC_DEBUG
  627. /* Configure reset type:
  628. * Debug level MID/HIGH: WARM Reset
  629. * Debug level LOW: HARD Reset
  630. */
  631. if (sec_debug_is_enabled()) {
  632. cfg->s2_type = 1;
  633. } else {
  634. /* For Millet VZW and Mattise VZW models always do warm reset */
  635. #if defined(CONFIG_MACH_MATISSELTE_VZW) || defined(CONFIG_MACH_MILLETLTE_VZW)
  636. cfg->s2_type = 1;
  637. #elif defined(CONFIG_SEC_MILLET_PROJECT) || defined(CONFIG_MACH_MEGA23GEUR_OPEN) || defined(CONFIG_MACH_MEGA2LTE_KTT)
  638. cfg->s2_type = 8; //dVDD hard reset
  639. #elif defined(CONFIG_MACH_MATISSE3G_CHN_OPEN) || defined(CONFIG_MACH_MILLET3G_CHN_OPEN)
  640. cfg->s2_type = 8; //dVDD reset
  641. #else
  642. cfg->s2_type = 7;
  643. #endif
  644. }
  645. #endif
  646. #endif
  647. #ifdef CONFIG_SEC_DEBUG
  648. #if defined(CONFIG_MACH_KANAS3G_CTC) || defined(CONFIG_MACH_CHAGALL_LTE) || defined(CONFIG_MACH_KLIMT_LTE)
  649. if (sec_debug_is_enabled()) {
  650. cfg->s2_type = 1; // warm reset
  651. } else {
  652. cfg->s2_type = 8; // dVdd hard reset
  653. }
  654. #endif
  655. #endif
  656. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl_addr,
  657. QPNP_PON_S2_CNTL_TYPE_MASK, (u8)cfg->s2_type);
  658. if (rc) {
  659. dev_err(&pon->spmi->dev, "Unable to configure S2 reset type\n");
  660. return rc;
  661. }
  662. /* enable S2 reset */
  663. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  664. QPNP_PON_S2_CNTL_EN, QPNP_PON_S2_CNTL_EN);
  665. if (rc) {
  666. dev_err(&pon->spmi->dev, "Unable to configure S2 enable\n");
  667. return rc;
  668. }
  669. return 0;
  670. }
  671. #ifdef CONFIG_SEC_PM
  672. static int
  673. qpnp_control_s2_reset(struct qpnp_pon *pon, struct qpnp_pon_config *cfg, int on)
  674. {
  675. int rc;
  676. /* control S2 reset */
  677. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  678. QPNP_PON_S2_CNTL_EN, on? QPNP_PON_S2_CNTL_EN : 0);
  679. if (rc) {
  680. dev_err(&pon->spmi->dev, "Unable to configure S2 enable\n");
  681. return rc;
  682. }
  683. return 0;
  684. }
  685. #endif
  686. static int __devinit
  687. qpnp_pon_request_irqs(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  688. {
  689. int rc = 0;
  690. switch (cfg->pon_type) {
  691. case PON_KPDPWR:
  692. #ifdef CONFIG_SEC_DEBUG
  693. if (sec_debug_is_enabled()) {
  694. rc = qpnp_pon_input_dispatch(pon, PON_KPDPWR);
  695. if (rc)
  696. dev_err(&pon->spmi->dev, "Fail to first check to send input event\n");
  697. }
  698. #endif
  699. rc = devm_request_irq(&pon->spmi->dev, cfg->state_irq,
  700. qpnp_kpdpwr_irq,
  701. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  702. "qpnp_kpdpwr_status", pon);
  703. if (rc < 0) {
  704. dev_err(&pon->spmi->dev, "Can't request %d IRQ\n",
  705. cfg->state_irq);
  706. return rc;
  707. }
  708. if (cfg->use_bark) {
  709. rc = devm_request_irq(&pon->spmi->dev, cfg->bark_irq,
  710. qpnp_kpdpwr_bark_irq,
  711. IRQF_TRIGGER_RISING,
  712. "qpnp_kpdpwr_bark", pon);
  713. if (rc < 0) {
  714. dev_err(&pon->spmi->dev,
  715. "Can't request %d IRQ\n",
  716. cfg->bark_irq);
  717. return rc;
  718. }
  719. }
  720. break;
  721. case PON_RESIN:
  722. rc = devm_request_irq(&pon->spmi->dev, cfg->state_irq,
  723. qpnp_resin_irq,
  724. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  725. "qpnp_resin_status", pon);
  726. if (rc < 0) {
  727. dev_err(&pon->spmi->dev, "Can't request %d IRQ\n",
  728. cfg->state_irq);
  729. return rc;
  730. }
  731. if (cfg->use_bark) {
  732. rc = devm_request_irq(&pon->spmi->dev, cfg->bark_irq,
  733. qpnp_resin_bark_irq,
  734. IRQF_TRIGGER_RISING,
  735. "qpnp_resin_bark", pon);
  736. if (rc < 0) {
  737. dev_err(&pon->spmi->dev,
  738. "Can't request %d IRQ\n",
  739. cfg->bark_irq);
  740. return rc;
  741. }
  742. }
  743. break;
  744. case PON_CBLPWR:
  745. rc = devm_request_irq(&pon->spmi->dev, cfg->state_irq,
  746. qpnp_cblpwr_irq,
  747. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  748. "qpnp_cblpwr_status", pon);
  749. if (rc < 0) {
  750. dev_err(&pon->spmi->dev, "Can't request %d IRQ\n",
  751. cfg->state_irq);
  752. return rc;
  753. }
  754. break;
  755. case PON_KPDPWR_RESIN:
  756. if (cfg->use_bark) {
  757. rc = devm_request_irq(&pon->spmi->dev, cfg->bark_irq,
  758. qpnp_kpdpwr_resin_bark_irq,
  759. IRQF_TRIGGER_RISING,
  760. "qpnp_kpdpwr_resin_bark", pon);
  761. if (rc < 0) {
  762. dev_err(&pon->spmi->dev,
  763. "Can't request %d IRQ\n",
  764. cfg->bark_irq);
  765. return rc;
  766. }
  767. }
  768. break;
  769. default:
  770. return -EINVAL;
  771. }
  772. /* mark the interrupts wakeable if they support linux-key */
  773. if (cfg->key_code) {
  774. enable_irq_wake(cfg->state_irq);
  775. #ifdef CONFIG_SEC_PM_DEBUG
  776. wake_enabled = true;
  777. #endif
  778. /* special handling for RESIN due to a hardware bug */
  779. if (cfg->pon_type == PON_RESIN && cfg->support_reset)
  780. enable_irq_wake(cfg->bark_irq);
  781. }
  782. return rc;
  783. }
  784. static int __devinit
  785. qpnp_pon_config_input(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  786. {
  787. if (!pon->pon_input) {
  788. pon->pon_input = input_allocate_device();
  789. if (!pon->pon_input) {
  790. dev_err(&pon->spmi->dev,
  791. "Can't allocate pon input device\n");
  792. return -ENOMEM;
  793. }
  794. pon->pon_input->name = "qpnp_pon";
  795. pon->pon_input->phys = "qpnp_pon/input0";
  796. }
  797. /* don't send dummy release event when system resumes */
  798. __set_bit(INPUT_PROP_NO_DUMMY_RELEASE, pon->pon_input->propbit);
  799. input_set_capability(pon->pon_input, EV_KEY, cfg->key_code);
  800. return 0;
  801. }
  802. static int __devinit qpnp_pon_config_init(struct qpnp_pon *pon)
  803. {
  804. int rc = 0, i = 0;
  805. struct device_node *pp = NULL;
  806. struct qpnp_pon_config *cfg;
  807. u8 pon_ver;
  808. u8 pmic_type;
  809. u8 revid_rev4;
  810. /* Check if it is rev B */
  811. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  812. QPNP_PON_REVISION2(pon->base), &pon_ver, 1);
  813. if (rc) {
  814. dev_err(&pon->spmi->dev,
  815. "Unable to read addr=%x, rc(%d)\n",
  816. QPNP_PON_REVISION2(pon->base), rc);
  817. return rc;
  818. }
  819. /* iterate through the list of pon configs */
  820. while ((pp = of_get_next_child(pon->spmi->dev.of_node, pp))) {
  821. cfg = &pon->pon_cfg[i++];
  822. rc = of_property_read_u32(pp, "qcom,pon-type", &cfg->pon_type);
  823. if (rc) {
  824. dev_err(&pon->spmi->dev, "PON type not specified\n");
  825. return rc;
  826. }
  827. switch (cfg->pon_type) {
  828. case PON_KPDPWR:
  829. cfg->state_irq = spmi_get_irq_byname(pon->spmi,
  830. NULL, "kpdpwr");
  831. if (cfg->state_irq < 0) {
  832. dev_err(&pon->spmi->dev,
  833. "Unable to get kpdpwr irq\n");
  834. return cfg->state_irq;
  835. }
  836. rc = of_property_read_u32(pp, "qcom,support-reset",
  837. &cfg->support_reset);
  838. if (rc && rc != -EINVAL) {
  839. dev_err(&pon->spmi->dev,
  840. "Unable to read 'support-reset'\n");
  841. return rc;
  842. }
  843. #ifdef CONFIG_SEC_PM
  844. rc = of_property_read_u32(pp, "qcom,disable-reset",
  845. &cfg->disable_reset);
  846. if (rc && rc != -EINVAL) {
  847. dev_err(&pon->spmi->dev,
  848. "Unable to read 'disable-reset'\n");
  849. return rc;
  850. }
  851. #endif
  852. cfg->use_bark = of_property_read_bool(pp,
  853. "qcom,use-bark");
  854. if (cfg->use_bark) {
  855. cfg->bark_irq = spmi_get_irq_byname(pon->spmi,
  856. NULL, "kpdpwr-bark");
  857. if (cfg->bark_irq < 0) {
  858. dev_err(&pon->spmi->dev,
  859. "Unable to get kpdpwr-bark irq\n");
  860. return cfg->bark_irq;
  861. }
  862. }
  863. /* If the value read from REVISION2 register is 0x00,
  864. * then there is a single register to control s2 reset.
  865. * Otherwise there are separate registers for s2 reset
  866. * type and s2 reset enable
  867. */
  868. if (pon_ver == PON_REV2_VALUE) {
  869. cfg->s2_cntl_addr = cfg->s2_cntl2_addr =
  870. QPNP_PON_KPDPWR_S2_CNTL(pon->base);
  871. } else {
  872. cfg->s2_cntl_addr =
  873. QPNP_PON_KPDPWR_S2_CNTL(pon->base);
  874. cfg->s2_cntl2_addr =
  875. QPNP_PON_KPDPWR_S2_CNTL2(pon->base);
  876. }
  877. break;
  878. case PON_RESIN:
  879. cfg->state_irq = spmi_get_irq_byname(pon->spmi,
  880. NULL, "resin");
  881. if (cfg->state_irq < 0) {
  882. dev_err(&pon->spmi->dev,
  883. "Unable to get resin irq\n");
  884. return cfg->bark_irq;
  885. }
  886. rc = of_property_read_u32(pp, "qcom,support-reset",
  887. &cfg->support_reset);
  888. if (rc && rc != -EINVAL) {
  889. dev_err(&pon->spmi->dev,
  890. "Unable to read 'support-reset'\n");
  891. return rc;
  892. }
  893. #ifdef CONFIG_SEC_PM
  894. rc = of_property_read_u32(pp, "qcom,disable-reset",
  895. &cfg->disable_reset);
  896. if (rc && rc != -EINVAL) {
  897. dev_err(&pon->spmi->dev,
  898. "Unable to read 'disable-reset'\n");
  899. return rc;
  900. }
  901. #endif
  902. cfg->use_bark = of_property_read_bool(pp,
  903. "qcom,use-bark");
  904. rc = spmi_ext_register_readl(pon->spmi->ctrl,
  905. pon->spmi->sid, PMIC_VERSION_REG,
  906. &pmic_type, 1);
  907. if (rc) {
  908. dev_err(&pon->spmi->dev,
  909. "Unable to read PMIC type\n");
  910. return rc;
  911. }
  912. if (pmic_type == PMIC_VER_8941) {
  913. rc = spmi_ext_register_readl(pon->spmi->ctrl,
  914. pon->spmi->sid, PMIC_VERSION_REV4_REG,
  915. &revid_rev4, 1);
  916. if (rc) {
  917. dev_err(&pon->spmi->dev,
  918. "Unable to read PMIC revision ID\n");
  919. return rc;
  920. }
  921. /*PM8941 V3 does not have harware bug. Hence
  922. bark is not required from PMIC versions 3.0*/
  923. if (!(revid_rev4 == PMIC8941_V1_REV4 ||
  924. revid_rev4 == PMIC8941_V2_REV4)) {
  925. cfg->support_reset = false;
  926. cfg->use_bark = false;
  927. }
  928. }
  929. if (cfg->use_bark) {
  930. cfg->bark_irq = spmi_get_irq_byname(pon->spmi,
  931. NULL, "resin-bark");
  932. if (cfg->bark_irq < 0) {
  933. dev_err(&pon->spmi->dev,
  934. "Unable to get resin-bark irq\n");
  935. return cfg->bark_irq;
  936. }
  937. }
  938. if (pon_ver == PON_REV2_VALUE) {
  939. cfg->s2_cntl_addr = cfg->s2_cntl2_addr =
  940. QPNP_PON_RESIN_S2_CNTL(pon->base);
  941. } else {
  942. cfg->s2_cntl_addr =
  943. QPNP_PON_RESIN_S2_CNTL(pon->base);
  944. cfg->s2_cntl2_addr =
  945. QPNP_PON_RESIN_S2_CNTL2(pon->base);
  946. }
  947. break;
  948. case PON_CBLPWR:
  949. cfg->state_irq = spmi_get_irq_byname(pon->spmi,
  950. NULL, "cblpwr");
  951. if (cfg->state_irq < 0) {
  952. dev_err(&pon->spmi->dev,
  953. "Unable to get cblpwr irq\n");
  954. return rc;
  955. }
  956. break;
  957. case PON_KPDPWR_RESIN:
  958. rc = of_property_read_u32(pp, "qcom,support-reset",
  959. &cfg->support_reset);
  960. if (rc && rc != -EINVAL) {
  961. dev_err(&pon->spmi->dev,
  962. "Unable to read 'support-reset'\n");
  963. return rc;
  964. }
  965. #ifdef CONFIG_SEC_PM
  966. rc = of_property_read_u32(pp, "qcom,disable-reset",
  967. &cfg->disable_reset);
  968. if (rc && rc != -EINVAL) {
  969. dev_err(&pon->spmi->dev,
  970. "Unable to read 'disable-reset'\n");
  971. return rc;
  972. }
  973. #endif
  974. cfg->use_bark = of_property_read_bool(pp,
  975. "qcom,use-bark");
  976. if (cfg->use_bark) {
  977. cfg->bark_irq = spmi_get_irq_byname(pon->spmi,
  978. NULL, "kpdpwr-resin-bark");
  979. if (cfg->bark_irq < 0) {
  980. dev_err(&pon->spmi->dev,
  981. "Unable to get kpdpwr-resin-bark irq\n");
  982. return cfg->bark_irq;
  983. }
  984. }
  985. if (pon_ver == PON_REV2_VALUE) {
  986. cfg->s2_cntl_addr = cfg->s2_cntl2_addr =
  987. QPNP_PON_KPDPWR_RESIN_S2_CNTL(pon->base);
  988. } else {
  989. cfg->s2_cntl_addr =
  990. QPNP_PON_KPDPWR_RESIN_S2_CNTL(pon->base);
  991. cfg->s2_cntl2_addr =
  992. QPNP_PON_KPDPWR_RESIN_S2_CNTL2(pon->base);
  993. }
  994. break;
  995. default:
  996. dev_err(&pon->spmi->dev, "PON RESET %d not supported",
  997. cfg->pon_type);
  998. return -EINVAL;
  999. }
  1000. if (cfg->support_reset) {
  1001. /*
  1002. * Get the reset parameters (bark debounce time and
  1003. * reset debounce time) for the reset line.
  1004. */
  1005. #if defined(CONFIG_MACH_KLTE_VZW) || defined(CONFIG_MACH_CHAGALL_VZW)
  1006. rc = of_property_read_u32(pp, "qcom,s1-timer2",
  1007. &cfg->s1_timer);
  1008. #else
  1009. rc = of_property_read_u32(pp, "qcom,s1-timer",
  1010. &cfg->s1_timer);
  1011. #endif
  1012. if (rc) {
  1013. dev_err(&pon->spmi->dev,
  1014. "Unable to read s1-timer\n");
  1015. return rc;
  1016. }
  1017. if (cfg->s1_timer > QPNP_PON_S1_TIMER_MAX) {
  1018. dev_err(&pon->spmi->dev,
  1019. "Incorrect S1 debounce time\n");
  1020. return -EINVAL;
  1021. }
  1022. #if defined(CONFIG_MACH_KLTE_VZW) || defined(CONFIG_MACH_CHAGALL_VZW)
  1023. rc = of_property_read_u32(pp, "qcom,s2-timer2",
  1024. &cfg->s2_timer);
  1025. #else
  1026. rc = of_property_read_u32(pp, "qcom,s2-timer",
  1027. &cfg->s2_timer);
  1028. #endif
  1029. if (rc) {
  1030. dev_err(&pon->spmi->dev,
  1031. "Unable to read s2-timer\n");
  1032. return rc;
  1033. }
  1034. if (cfg->s2_timer > QPNP_PON_S2_TIMER_MAX) {
  1035. dev_err(&pon->spmi->dev,
  1036. "Incorrect S2 debounce time\n");
  1037. return -EINVAL;
  1038. }
  1039. rc = of_property_read_u32(pp, "qcom,s2-type",
  1040. &cfg->s2_type);
  1041. if (rc) {
  1042. dev_err(&pon->spmi->dev,
  1043. "Unable to read s2-type\n");
  1044. return rc;
  1045. }
  1046. if (cfg->s2_type > QPNP_PON_RESET_TYPE_MAX) {
  1047. dev_err(&pon->spmi->dev,
  1048. "Incorrect reset type specified\n");
  1049. return -EINVAL;
  1050. }
  1051. }
  1052. /*
  1053. * Get the standard-key parameters. This might not be
  1054. * specified if there is no key mapping on the reset line.
  1055. */
  1056. rc = of_property_read_u32(pp, "linux,code", &cfg->key_code);
  1057. #ifdef CONFIG_SEC_PATEK_PROJECT
  1058. if(cfg->key_code == 116){
  1059. dev_err(&pon->spmi->dev,
  1060. "patek power key code changed to %d(%d)\n",
  1061. KEY_END, cfg->key_code);
  1062. cfg->key_code = KEY_END;
  1063. }
  1064. #else
  1065. if (rc && rc != -EINVAL) {
  1066. dev_err(&pon->spmi->dev,
  1067. "Unable to read key-code\n");
  1068. return rc;
  1069. }
  1070. #endif
  1071. /* Register key configuration */
  1072. if (cfg->key_code) {
  1073. rc = qpnp_pon_config_input(pon, cfg);
  1074. if (rc < 0)
  1075. return rc;
  1076. }
  1077. /* get the pull-up configuration */
  1078. rc = of_property_read_u32(pp, "qcom,pull-up", &cfg->pull_up);
  1079. if (rc && rc != -EINVAL) {
  1080. dev_err(&pon->spmi->dev, "Unable to read pull-up\n");
  1081. return rc;
  1082. }
  1083. }
  1084. /* register the input device */
  1085. if (pon->pon_input) {
  1086. rc = input_register_device(pon->pon_input);
  1087. if (rc) {
  1088. dev_err(&pon->spmi->dev,
  1089. "Can't register pon key: %d\n", rc);
  1090. goto free_input_dev;
  1091. }
  1092. }
  1093. for (i = 0; i < pon->num_pon_config; i++) {
  1094. cfg = &pon->pon_cfg[i];
  1095. /* Configure the pull-up */
  1096. rc = qpnp_config_pull(pon, cfg);
  1097. if (rc) {
  1098. dev_err(&pon->spmi->dev, "Unable to config pull-up\n");
  1099. goto unreg_input_dev;
  1100. }
  1101. /* Configure the reset-configuration */
  1102. if (cfg->support_reset) {
  1103. rc = qpnp_config_reset(pon, cfg);
  1104. if (rc) {
  1105. dev_err(&pon->spmi->dev,
  1106. "Unable to config pon reset\n");
  1107. goto unreg_input_dev;
  1108. }
  1109. } else {
  1110. /* disable S2 reset */
  1111. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  1112. QPNP_PON_S2_CNTL_EN, 0);
  1113. if (rc) {
  1114. dev_err(&pon->spmi->dev,
  1115. "Unable to disable S2 reset\n");
  1116. goto unreg_input_dev;
  1117. }
  1118. }
  1119. #ifdef CONFIG_SEC_PM
  1120. if (cfg->disable_reset)
  1121. qpnp_control_s2_reset(pon, cfg, !cfg->disable_reset);
  1122. #endif
  1123. #ifdef CONFIG_MACH_HLTEVZW
  1124. /* Disable power key reset on HLTE VZW with debug low by request */
  1125. if (cfg->pon_type == PON_KPDPWR) {
  1126. if (0 == sec_debug_is_enabled())
  1127. qpnp_control_s2_reset(pon, cfg, 0);
  1128. }
  1129. #endif
  1130. rc = qpnp_pon_request_irqs(pon, cfg);
  1131. if (rc) {
  1132. dev_err(&pon->spmi->dev, "Unable to request-irq's\n");
  1133. goto unreg_input_dev;
  1134. }
  1135. }
  1136. device_init_wakeup(&pon->spmi->dev, 1);
  1137. return rc;
  1138. unreg_input_dev:
  1139. if (pon->pon_input)
  1140. input_unregister_device(pon->pon_input);
  1141. free_input_dev:
  1142. if (pon->pon_input)
  1143. input_free_device(pon->pon_input);
  1144. return rc;
  1145. }
  1146. static ssize_t sysfs_powerkey_onoff_show(struct device *dev,
  1147. struct device_attribute *attr, char *buf)
  1148. {
  1149. struct qpnp_pon *pon = dev_get_drvdata(dev);
  1150. printk(KERN_INFO "%s\n",__func__);
  1151. if (pon->powerkey_state == 1) {
  1152. printk(KERN_INFO "powerkey is pressed\n");
  1153. return snprintf(buf, 5, "%d\n", pon->powerkey_state);
  1154. } else {
  1155. printk(KERN_INFO "powerkey is released\n");
  1156. return snprintf(buf, 5, "%d\n", pon->powerkey_state);
  1157. }
  1158. }
  1159. static DEVICE_ATTR(sec_powerkey_pressed, 0664 , sysfs_powerkey_onoff_show, NULL);
  1160. #ifdef CONFIG_SEC_PM_DEBUG
  1161. static int qpnp_wake_enabled(const char *val, const struct kernel_param *kp)
  1162. {
  1163. int ret = 0;
  1164. struct qpnp_pon_config *cfg;
  1165. ret = param_set_bool(val, kp);
  1166. if (ret) {
  1167. pr_err("Unable to set qpnp_wake_enabled: %d\n", ret);
  1168. return ret;
  1169. }
  1170. cfg = qpnp_get_cfg(sys_reset_dev, PON_KPDPWR);
  1171. if (!cfg) {
  1172. pr_err("Invalid config pointer\n");
  1173. return -EFAULT;
  1174. }
  1175. if (!wake_enabled)
  1176. disable_irq_wake(cfg->state_irq);
  1177. else
  1178. enable_irq_wake(cfg->state_irq);
  1179. pr_info("%s: wake_enabled = %d\n", KBUILD_MODNAME, wake_enabled);
  1180. return ret;
  1181. }
  1182. static struct kernel_param_ops module_ops = {
  1183. .set = qpnp_wake_enabled,
  1184. .get = param_get_bool,
  1185. };
  1186. module_param_cb(wake_enabled, &module_ops, &wake_enabled, 0644);
  1187. static int qpnp_reset_enabled(const char *val, const struct kernel_param *kp)
  1188. {
  1189. int ret = 0;
  1190. struct qpnp_pon_config *cfg;
  1191. ret = param_set_bool(val, kp);
  1192. if (ret) {
  1193. pr_err("Unable to set qpnp_reset_enabled: %d\n", ret);
  1194. return ret;
  1195. }
  1196. cfg = qpnp_get_cfg(sys_reset_dev, PON_KPDPWR);
  1197. if (!cfg) {
  1198. pr_err("Invalid config pointer\n");
  1199. return -EFAULT;
  1200. }
  1201. if (!reset_enabled)
  1202. qpnp_control_s2_reset(sys_reset_dev, cfg, 0);
  1203. else
  1204. qpnp_control_s2_reset(sys_reset_dev, cfg, 1);
  1205. pr_info("%s: reset_enabled = %d\n", KBUILD_MODNAME, reset_enabled);
  1206. return ret;
  1207. }
  1208. static struct kernel_param_ops reset_module_ops = {
  1209. .set = qpnp_reset_enabled,
  1210. .get = param_get_bool,
  1211. };
  1212. module_param_cb(reset_enabled, &reset_module_ops, &reset_enabled, 0644);
  1213. #endif
  1214. #ifdef CONFIG_SEC_PM
  1215. int qpnp_pon_set_wd_timer(u8 s1_timer, u8 s2_timer, u8 reset_type)
  1216. {
  1217. struct qpnp_pon *pon = sys_reset_dev;
  1218. u8 data;
  1219. spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid, 0x854, &data, 1);
  1220. pr_debug("%s: 0x854=0x%x\n", __func__, data);
  1221. spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid, 0x855, &data, 1);
  1222. pr_debug("%s: 0x855=0x%x\n", __func__, data);
  1223. spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid, 0x856, &data, 1);
  1224. pr_debug("%s: 0x856=0x%x\n", __func__, data);
  1225. data = s1_timer; /* S1_TIMER */
  1226. spmi_ext_register_writel(pon->spmi->ctrl, pon->spmi->sid, 0x854, &data, 1);
  1227. data = s2_timer; /* S2_TIMER */
  1228. spmi_ext_register_writel(pon->spmi->ctrl, pon->spmi->sid, 0x855, &data, 1);
  1229. data = reset_type; /* RESET_TYPE */
  1230. spmi_ext_register_writel(pon->spmi->ctrl, pon->spmi->sid, 0x856, &data, 1);
  1231. data = 0x1; /* WD_RESET_PET to clear WD timer */
  1232. spmi_ext_register_writel(pon->spmi->ctrl, pon->spmi->sid, 0x858, &data, 1);
  1233. spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid, 0x854, &data, 1);
  1234. pr_info("%s: 0x854=0x%x\n", __func__, data);
  1235. spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid, 0x855, &data, 1);
  1236. pr_info("%s: 0x855=0x%x\n", __func__, data);
  1237. spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid, 0x856, &data, 1);
  1238. pr_info("%s: 0x856=0x%x\n", __func__, data);
  1239. return 0;
  1240. }
  1241. EXPORT_SYMBOL(qpnp_pon_set_wd_timer);
  1242. #endif
  1243. static int __devinit qpnp_pon_probe(struct spmi_device *spmi)
  1244. {
  1245. struct qpnp_pon *pon;
  1246. struct resource *pon_resource;
  1247. struct device_node *itr = NULL;
  1248. u32 delay = 0, s3_debounce = 0;
  1249. int rc, sys_reset, index;
  1250. u8 pon_sts = 0, buf[2];
  1251. const char *s3_src;
  1252. u8 s3_src_reg;
  1253. u16 poff_sts = 0;
  1254. struct device *sec_powerkey;
  1255. int ret;
  1256. pon = devm_kzalloc(&spmi->dev, sizeof(struct qpnp_pon),
  1257. GFP_KERNEL);
  1258. if (!pon) {
  1259. dev_err(&spmi->dev, "Can't allocate qpnp_pon\n");
  1260. return -ENOMEM;
  1261. }
  1262. sys_reset = of_property_read_bool(spmi->dev.of_node,
  1263. "qcom,system-reset");
  1264. if (sys_reset && sys_reset_dev) {
  1265. dev_err(&spmi->dev, "qcom,system-reset property can only be specified for one device on the system\n");
  1266. return -EINVAL;
  1267. } else if (sys_reset) {
  1268. sys_reset_dev = pon;
  1269. }
  1270. pon->spmi = spmi;
  1271. /* get the total number of pon configurations */
  1272. while ((itr = of_get_next_child(spmi->dev.of_node, itr)))
  1273. pon->num_pon_config++;
  1274. if (!pon->num_pon_config) {
  1275. /* No PON config., do not register the driver */
  1276. dev_err(&spmi->dev, "No PON config. specified\n");
  1277. return -EINVAL;
  1278. }
  1279. pon->pon_cfg = devm_kzalloc(&spmi->dev,
  1280. sizeof(struct qpnp_pon_config) * pon->num_pon_config,
  1281. GFP_KERNEL);
  1282. pon_resource = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
  1283. if (!pon_resource) {
  1284. dev_err(&spmi->dev, "Unable to get PON base address\n");
  1285. return -ENXIO;
  1286. }
  1287. pon->base = pon_resource->start;
  1288. /* PON reason */
  1289. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  1290. QPNP_PON_REASON1(pon->base), &pon_sts, 1);
  1291. if (rc) {
  1292. dev_err(&pon->spmi->dev, "Unable to read PON_RESASON1 reg\n");
  1293. return rc;
  1294. }
  1295. boot_reason = ffs(pon_sts);
  1296. index = ffs(pon_sts) - 1;
  1297. cold_boot = !qpnp_pon_is_warm_reset();
  1298. if (index >= ARRAY_SIZE(qpnp_pon_reason) || index < 0)
  1299. dev_info(&pon->spmi->dev,
  1300. "PMIC@SID%d Power-on reason: Unknown and '%s' boot\n",
  1301. pon->spmi->sid, cold_boot ? "cold" : "warm");
  1302. else
  1303. dev_info(&pon->spmi->dev,
  1304. "PMIC@SID%d Power-on reason: %s and '%s' boot\n",
  1305. pon->spmi->sid, qpnp_pon_reason[index],
  1306. cold_boot ? "cold" : "warm");
  1307. /* POFF reason */
  1308. rc = spmi_ext_register_readl(pon->spmi->ctrl, pon->spmi->sid,
  1309. QPNP_POFF_REASON1(pon->base),
  1310. buf, 2);
  1311. if (rc) {
  1312. dev_err(&pon->spmi->dev, "Unable to read POFF_RESASON regs\n");
  1313. return rc;
  1314. }
  1315. poff_sts = buf[0] | (buf[1] << 8);
  1316. index = ffs(poff_sts) - 1;
  1317. if (index >= ARRAY_SIZE(qpnp_poff_reason) || index < 0)
  1318. dev_info(&pon->spmi->dev,
  1319. "PMIC@SID%d: Unknown power-off reason\n",
  1320. pon->spmi->sid);
  1321. else
  1322. dev_info(&pon->spmi->dev,
  1323. "PMIC@SID%d: Power-off reason: %s\n",
  1324. pon->spmi->sid,
  1325. qpnp_poff_reason[index]);
  1326. rc = of_property_read_u32(pon->spmi->dev.of_node,
  1327. "qcom,pon-dbc-delay", &delay);
  1328. if (rc) {
  1329. if (rc != -EINVAL) {
  1330. dev_err(&spmi->dev, "Unable to read debounce delay\n");
  1331. return rc;
  1332. }
  1333. } else {
  1334. delay = (delay << QPNP_PON_DELAY_BIT_SHIFT) / USEC_PER_SEC;
  1335. delay = ilog2(delay);
  1336. rc = qpnp_pon_masked_write(pon, QPNP_PON_DBC_CTL(pon->base),
  1337. QPNP_PON_DBC_DELAY_MASK, delay);
  1338. if (rc) {
  1339. dev_err(&spmi->dev, "Unable to set PON debounce\n");
  1340. return rc;
  1341. }
  1342. }
  1343. /* program s3 debounce */
  1344. rc = of_property_read_u32(pon->spmi->dev.of_node,
  1345. "qcom,s3-debounce", &s3_debounce);
  1346. if (rc) {
  1347. if (rc != -EINVAL) {
  1348. dev_err(&pon->spmi->dev, "Unable to read s3 timer\n");
  1349. return rc;
  1350. }
  1351. } else {
  1352. if (s3_debounce > QPNP_PON_S3_TIMER_SECS_MAX) {
  1353. dev_info(&pon->spmi->dev,
  1354. "Exceeded S3 max value, set it to max\n");
  1355. s3_debounce = QPNP_PON_S3_TIMER_SECS_MAX;
  1356. }
  1357. /* 0 is a special value to indicate instant s3 reset */
  1358. if (s3_debounce != 0)
  1359. s3_debounce = ilog2(s3_debounce);
  1360. rc = qpnp_pon_masked_write(pon, QPNP_PON_S3_DBC_CTL(pon->base),
  1361. QPNP_PON_S3_DBC_DELAY_MASK, s3_debounce);
  1362. if (rc) {
  1363. dev_err(&spmi->dev, "Unable to set S3 debounce\n");
  1364. return rc;
  1365. }
  1366. }
  1367. /* program s3 source */
  1368. s3_src = "kpdpwr-and-resin";
  1369. rc = of_property_read_string(pon->spmi->dev.of_node,
  1370. "qcom,s3-src", &s3_src);
  1371. if (rc && rc != -EINVAL) {
  1372. dev_err(&pon->spmi->dev, "Unable to read s3 timer\n");
  1373. return rc;
  1374. }
  1375. if (!strcmp(s3_src, "kpdpwr"))
  1376. s3_src_reg = QPNP_PON_S3_SRC_KPDPWR;
  1377. else if (!strcmp(s3_src, "resin"))
  1378. s3_src_reg = QPNP_PON_S3_SRC_RESIN;
  1379. else if (!strcmp(s3_src, "kpdpwr-or-resin"))
  1380. s3_src_reg = QPNP_PON_S3_SRC_KPDPWR_OR_RESIN;
  1381. else /* default combination */
  1382. s3_src_reg = QPNP_PON_S3_SRC_KPDPWR_AND_RESIN;
  1383. /* S3 source is a write once register. If the register has
  1384. * been configured by bootloader then this operation will
  1385. * not be effective. */
  1386. rc = qpnp_pon_masked_write(pon, QPNP_PON_S3_SRC(pon->base),
  1387. QPNP_PON_S3_SRC_MASK, s3_src_reg);
  1388. if (rc) {
  1389. dev_err(&spmi->dev,
  1390. "Unable to program s3 source\n");
  1391. return rc;
  1392. }
  1393. dev_set_drvdata(&spmi->dev, pon);
  1394. INIT_DELAYED_WORK(&pon->bark_work, bark_work_func);
  1395. /* register the PON configurations */
  1396. rc = qpnp_pon_config_init(pon);
  1397. if (rc) {
  1398. dev_err(&spmi->dev,
  1399. "Unable to intialize PON configurations\n");
  1400. return rc;
  1401. }
  1402. sec_powerkey = device_create(sec_class, NULL, 0, NULL, "sec_powerkey");
  1403. if (IS_ERR(sec_powerkey))
  1404. pr_err("Failed to create device(sec_powerkey)!\n");
  1405. ret = device_create_file(sec_powerkey, &dev_attr_sec_powerkey_pressed);
  1406. if (ret) {
  1407. pr_err("Failed to create device file in sysfs entries(%s)!\n",
  1408. dev_attr_sec_powerkey_pressed.attr.name);
  1409. }
  1410. dev_set_drvdata(sec_powerkey, pon);
  1411. return rc;
  1412. }
  1413. static int qpnp_pon_remove(struct spmi_device *spmi)
  1414. {
  1415. struct qpnp_pon *pon = dev_get_drvdata(&spmi->dev);
  1416. cancel_delayed_work_sync(&pon->bark_work);
  1417. if (pon->pon_input)
  1418. input_unregister_device(pon->pon_input);
  1419. return 0;
  1420. }
  1421. static struct of_device_id spmi_match_table[] = {
  1422. { .compatible = "qcom,qpnp-power-on", },
  1423. {}
  1424. };
  1425. static struct spmi_driver qpnp_pon_driver = {
  1426. .driver = {
  1427. .name = "qcom,qpnp-power-on",
  1428. .of_match_table = spmi_match_table,
  1429. },
  1430. .probe = qpnp_pon_probe,
  1431. .remove = __devexit_p(qpnp_pon_remove),
  1432. };
  1433. static int __init qpnp_pon_init(void)
  1434. {
  1435. return spmi_driver_register(&qpnp_pon_driver);
  1436. }
  1437. #ifdef CONFIG_ARCH_MSM8226
  1438. subsys_initcall(qpnp_pon_init);
  1439. #else
  1440. module_init(qpnp_pon_init);
  1441. #endif
  1442. static void __exit qpnp_pon_exit(void)
  1443. {
  1444. return spmi_driver_unregister(&qpnp_pon_driver);
  1445. }
  1446. module_exit(qpnp_pon_exit);
  1447. MODULE_DESCRIPTION("QPNP PMIC POWER-ON driver");
  1448. MODULE_LICENSE("GPL v2");