qpnp-power-on.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/input.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/log2.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/regmap.h>
  23. #include <linux/slab.h>
  24. #include <linux/spmi.h>
  25. #include <linux/suspend.h>
  26. #include <linux/input/qpnp-power-on.h>
  27. #include <linux/regulator/driver.h>
  28. #include <linux/regulator/machine.h>
  29. #include <linux/regulator/of_regulator.h>
  30. #define PMIC_VER_8941 0x01
  31. #define PMIC_VERSION_REG 0x0105
  32. #define PMIC_VERSION_REV4_REG 0x0103
  33. #define PMIC8941_V1_REV4 0x01
  34. #define PMIC8941_V2_REV4 0x02
  35. #define PON_PRIMARY 0x01
  36. #define PON_SECONDARY 0x02
  37. #define PON_1REG 0x03
  38. #define PON_GEN2_PRIMARY 0x04
  39. #define PON_GEN2_SECONDARY 0x05
  40. #define PON_GEN3_PBS 0x08
  41. #define PON_GEN3_HLOS 0x09
  42. enum qpnp_pon_version {
  43. QPNP_PON_GEN1_V1,
  44. QPNP_PON_GEN1_V2,
  45. QPNP_PON_GEN2,
  46. QPNP_PON_GEN3,
  47. };
  48. #define PON_OFFSET(subtype, offset_gen1, offset_gen2) \
  49. (((subtype == PON_PRIMARY) || \
  50. (subtype == PON_SECONDARY) || \
  51. (subtype == PON_1REG)) ? offset_gen1 : offset_gen2)
  52. /* Common PNP register addresses */
  53. #define QPNP_PON_REVISION2(pon) ((pon)->base + 0x01)
  54. #define QPNP_PON_PERPH_SUBTYPE(pon) ((pon)->base + 0x05)
  55. /* PON common register addresses */
  56. #define QPNP_PON_RT_STS(pon) ((pon)->base + 0x10)
  57. #define QPNP_PON_PULL_CTL(pon) ((pon)->base + 0x70)
  58. #define QPNP_PON_DBC_CTL(pon) ((pon)->base + 0x71)
  59. #define QPNP_PON_PBS_DBC_CTL(pon) ((pon)->pbs_base + 0x71)
  60. /* PON/RESET sources register addresses */
  61. #define QPNP_PON_REASON1(pon) \
  62. ((pon)->base + PON_OFFSET((pon)->subtype, 0x8, 0xC0))
  63. #define QPNP_PON_WARM_RESET_REASON1(pon) \
  64. ((pon)->base + PON_OFFSET((pon)->subtype, 0xA, 0xC2))
  65. #define QPNP_POFF_REASON1(pon) \
  66. ((pon)->base + PON_OFFSET((pon)->subtype, 0xC, 0xC5))
  67. #define QPNP_PON_WARM_RESET_REASON2(pon) ((pon)->base + 0xB)
  68. #define QPNP_PON_OFF_REASON(pon) ((pon)->base + 0xC7)
  69. #define QPNP_FAULT_REASON1(pon) ((pon)->base + 0xC8)
  70. #define QPNP_S3_RESET_REASON(pon) ((pon)->base + 0xCA)
  71. #define QPNP_PON_KPDPWR_S1_TIMER(pon) ((pon)->base + 0x40)
  72. #define QPNP_PON_KPDPWR_S2_TIMER(pon) ((pon)->base + 0x41)
  73. #define QPNP_PON_KPDPWR_S2_CNTL(pon) ((pon)->base + 0x42)
  74. #define QPNP_PON_KPDPWR_S2_CNTL2(pon) ((pon)->base + 0x43)
  75. #define QPNP_PON_RESIN_S1_TIMER(pon) ((pon)->base + 0x44)
  76. #define QPNP_PON_RESIN_S2_TIMER(pon) ((pon)->base + 0x45)
  77. #define QPNP_PON_RESIN_S2_CNTL(pon) ((pon)->base + 0x46)
  78. #define QPNP_PON_RESIN_S2_CNTL2(pon) ((pon)->base + 0x47)
  79. #define QPNP_PON_KPDPWR_RESIN_S1_TIMER(pon) ((pon)->base + 0x48)
  80. #define QPNP_PON_KPDPWR_RESIN_S2_TIMER(pon) ((pon)->base + 0x49)
  81. #define QPNP_PON_KPDPWR_RESIN_S2_CNTL(pon) ((pon)->base + 0x4A)
  82. #define QPNP_PON_KPDPWR_RESIN_S2_CNTL2(pon) ((pon)->base + 0x4B)
  83. #define QPNP_PON_PS_HOLD_RST_CTL(pon) \
  84. ((pon)->base + ((pon)->pon_ver != QPNP_PON_GEN3 ? 0x5A : 0x52))
  85. #define QPNP_PON_PS_HOLD_RST_CTL2(pon) \
  86. ((pon)->base + ((pon)->pon_ver != QPNP_PON_GEN3 ? 0x5B : 0x53))
  87. #define QPNP_PON_WD_RST_S2_CTL(pon) ((pon)->base + 0x56)
  88. #define QPNP_PON_WD_RST_S2_CTL2(pon) ((pon)->base + 0x57)
  89. #define QPNP_PON_SW_RST_S2_CTL(pon) ((pon)->base + 0x62)
  90. #define QPNP_PON_SW_RST_S2_CTL2(pon) ((pon)->base + 0x63)
  91. #define QPNP_PON_SW_RST_GO(pon) ((pon)->base + 0x64)
  92. #define QPNP_PON_S3_SRC(pon) ((pon)->base + 0x74)
  93. #define QPNP_PON_S3_DBC_CTL(pon) ((pon)->base + 0x75)
  94. #define QPNP_PON_SMPL_CTL(pon) ((pon)->base + 0x7F)
  95. #define QPNP_PON_TRIGGER_EN(pon) ((pon)->base + 0x80)
  96. #define QPNP_PON_XVDD_RB_SPARE(pon) ((pon)->base + 0x8E)
  97. #define QPNP_PON_SOFT_RB_SPARE(pon) ((pon)->base + 0x8F)
  98. #define QPNP_PON_SEC_ACCESS(pon) ((pon)->base + 0xD0)
  99. #define QPNP_PON_SEC_UNLOCK 0xA5
  100. #define QPNP_PON_SW_RST_GO_VAL 0xA5
  101. #define QPNP_PON_WARM_RESET_TFT BIT(4)
  102. #define QPNP_PON_RESIN_PULL_UP BIT(0)
  103. #define QPNP_PON_KPDPWR_PULL_UP BIT(1)
  104. #define QPNP_PON_CBLPWR_PULL_UP BIT(2)
  105. #define QPNP_PON_FAULT_PULL_UP BIT(4)
  106. #define QPNP_PON_S2_CNTL_EN BIT(7)
  107. #define QPNP_PON_S2_RESET_ENABLE BIT(7)
  108. #define QPNP_PON_DELAY_BIT_SHIFT 6
  109. #define QPNP_PON_GEN2_DELAY_BIT_SHIFT 14
  110. #define QPNP_PON_S1_TIMER_MASK (0xF)
  111. #define QPNP_PON_S2_TIMER_MASK (0x7)
  112. #define QPNP_PON_S2_CNTL_TYPE_MASK (0xF)
  113. #define QPNP_PON_DBC_DELAY_MASK(pon) PON_OFFSET((pon)->subtype, 0x7, 0xF)
  114. #define QPNP_PON_KPDPWR_N_SET BIT(0)
  115. #define QPNP_PON_RESIN_N_SET BIT(1)
  116. #define QPNP_PON_CBLPWR_N_SET BIT(2)
  117. #define QPNP_PON_RESIN_BARK_N_SET BIT(4)
  118. #define QPNP_PON_KPDPWR_RESIN_BARK_N_SET BIT(5)
  119. #define QPNP_PON_GEN3_RESIN_N_SET BIT(6)
  120. #define QPNP_PON_GEN3_KPDPWR_N_SET BIT(7)
  121. #define QPNP_PON_WD_EN BIT(7)
  122. #define QPNP_PON_RESET_EN BIT(7)
  123. #define QPNP_PON_POWER_OFF_MASK 0xF
  124. #define QPNP_GEN2_POFF_SEQ BIT(7)
  125. #define QPNP_GEN2_FAULT_SEQ BIT(6)
  126. #define QPNP_GEN2_S3_RESET_SEQ BIT(5)
  127. #define QPNP_PON_S3_SRC_KPDPWR 0
  128. #define QPNP_PON_S3_SRC_RESIN 1
  129. #define QPNP_PON_S3_SRC_KPDPWR_AND_RESIN 2
  130. #define QPNP_PON_S3_SRC_KPDPWR_OR_RESIN 3
  131. #define QPNP_PON_S3_SRC_MASK 0x3
  132. #define QPNP_PON_HARD_RESET_MASK GENMASK(7, 5)
  133. #define QPNP_PON_UVLO_DLOAD_EN BIT(7)
  134. #define QPNP_PON_SMPL_EN BIT(7)
  135. /* Limits */
  136. #define QPNP_PON_S1_TIMER_MAX 10256
  137. #define QPNP_PON_S2_TIMER_MAX 2000
  138. #define QPNP_PON_S3_TIMER_SECS_MAX 128
  139. #define QPNP_PON_S3_DBC_DELAY_MASK 0x07
  140. #define QPNP_PON_RESET_TYPE_MAX 0xF
  141. #define PON_S1_COUNT_MAX 0xF
  142. #define QPNP_PON_MIN_DBC_US (USEC_PER_SEC / 64)
  143. #define QPNP_PON_MAX_DBC_US (USEC_PER_SEC * 2)
  144. #define QPNP_PON_GEN2_MIN_DBC_US 62
  145. #define QPNP_PON_GEN2_MAX_DBC_US (USEC_PER_SEC / 4)
  146. #define QPNP_KEY_STATUS_DELAY msecs_to_jiffies(250)
  147. #define QPNP_PON_BUFFER_SIZE 9
  148. #define QPNP_POFF_REASON_UVLO 13
  149. enum pon_type {
  150. PON_KPDPWR = PON_POWER_ON_TYPE_KPDPWR,
  151. PON_RESIN = PON_POWER_ON_TYPE_RESIN,
  152. PON_CBLPWR = PON_POWER_ON_TYPE_CBLPWR,
  153. PON_KPDPWR_RESIN = PON_POWER_ON_TYPE_KPDPWR_RESIN,
  154. };
  155. struct pon_reg {
  156. unsigned int val;
  157. u16 addr;
  158. struct list_head list;
  159. };
  160. struct qpnp_pon_config {
  161. u32 pon_type;
  162. u32 support_reset;
  163. u32 key_code;
  164. u32 s1_timer;
  165. u32 s2_timer;
  166. u32 s2_type;
  167. bool pull_up;
  168. int state_irq;
  169. int bark_irq;
  170. u16 s2_cntl_addr;
  171. u16 s2_cntl2_addr;
  172. bool old_state;
  173. bool use_bark;
  174. bool config_reset;
  175. };
  176. struct pon_regulator {
  177. struct qpnp_pon *pon;
  178. struct regulator_dev *rdev;
  179. struct regulator_desc rdesc;
  180. u32 addr;
  181. u32 bit;
  182. bool enabled;
  183. };
  184. struct qpnp_pon {
  185. struct device *dev;
  186. struct regmap *regmap;
  187. struct input_dev *pon_input;
  188. struct qpnp_pon_config *pon_cfg;
  189. struct pon_regulator *pon_reg_cfg;
  190. struct list_head restore_regs;
  191. struct list_head list;
  192. struct mutex restore_lock;
  193. struct delayed_work bark_work;
  194. struct dentry *debugfs;
  195. u16 base;
  196. u16 pbs_base;
  197. u8 subtype;
  198. u8 pon_ver;
  199. u8 warm_reset_reason1;
  200. u8 warm_reset_reason2;
  201. int num_pon_config;
  202. int num_pon_reg;
  203. int pon_trigger_reason;
  204. int pon_power_off_reason;
  205. u32 dbc_time_us;
  206. u32 uvlo;
  207. int warm_reset_poff_type;
  208. int hard_reset_poff_type;
  209. int shutdown_poff_type;
  210. int resin_warm_reset_type;
  211. int resin_hard_reset_type;
  212. int resin_shutdown_type;
  213. bool is_spon;
  214. bool store_hard_reset_reason;
  215. bool resin_hard_reset_disable;
  216. bool resin_shutdown_disable;
  217. bool ps_hold_hard_reset_disable;
  218. bool ps_hold_shutdown_disable;
  219. bool kpdpwr_dbc_enable;
  220. bool resin_pon_reset;
  221. ktime_t kpdpwr_last_release_time;
  222. bool legacy_hard_reset_offset;
  223. };
  224. static struct qpnp_pon *sys_reset_dev;
  225. static struct qpnp_pon *modem_reset_dev;
  226. static DEFINE_SPINLOCK(spon_list_slock);
  227. static LIST_HEAD(spon_dev_list);
  228. static u32 s1_delay[PON_S1_COUNT_MAX + 1] = {
  229. 0, 32, 56, 80, 138, 184, 272, 408, 608, 904, 1352, 2048, 3072, 4480,
  230. 6720, 10256
  231. };
  232. static const char * const qpnp_pon_reason[] = {
  233. [0] = "Triggered from Hard Reset",
  234. [1] = "Triggered from SMPL (Sudden Momentary Power Loss)",
  235. [2] = "Triggered from RTC (RTC Alarm Expiry)",
  236. [3] = "Triggered from DC (DC Charger Insertion)",
  237. [4] = "Triggered from USB (USB Charger Insertion)",
  238. [5] = "Triggered from PON1 (Secondary PMIC)",
  239. [6] = "Triggered from CBL (External Power Supply)",
  240. [7] = "Triggered from KPD (Power Key Press)",
  241. };
  242. #define POFF_REASON_FAULT_OFFSET 16
  243. #define POFF_REASON_S3_RESET_OFFSET 32
  244. static const char * const qpnp_poff_reason[] = {
  245. /* QPNP_PON_GEN1 POFF reasons */
  246. [0] = "Triggered from SOFT (Software)",
  247. [1] = "Triggered from PS_HOLD (PS_HOLD/MSM Controlled Shutdown)",
  248. [2] = "Triggered from PMIC_WD (PMIC Watchdog)",
  249. [3] = "Triggered from GP1 (Keypad_Reset1)",
  250. [4] = "Triggered from GP2 (Keypad_Reset2)",
  251. [5] = "Triggered from KPDPWR_AND_RESIN (Power Key and Reset Line)",
  252. [6] = "Triggered from RESIN_N (Reset Line/Volume Down Key)",
  253. [7] = "Triggered from KPDPWR_N (Long Power Key Hold)",
  254. [8] = "N/A",
  255. [9] = "N/A",
  256. [10] = "N/A",
  257. [11] = "Triggered from CHARGER (Charger ENUM_TIMER, BOOT_DONE)",
  258. [12] = "Triggered from TFT (Thermal Fault Tolerance)",
  259. [13] = "Triggered from UVLO (Under Voltage Lock Out)",
  260. [14] = "Triggered from OTST3 (Over Temperature)",
  261. [15] = "Triggered from STAGE3 (Stage 3 Reset)",
  262. /* QPNP_PON_GEN2 FAULT reasons */
  263. [16] = "Triggered from GP_FAULT0",
  264. [17] = "Triggered from GP_FAULT1",
  265. [18] = "Triggered from GP_FAULT2",
  266. [19] = "Triggered from GP_FAULT3",
  267. [20] = "Triggered from MBG_FAULT",
  268. [21] = "Triggered from OVLO (Over Voltage Lock Out)",
  269. [22] = "Triggered from UVLO (Under Voltage Lock Out)",
  270. [23] = "Triggered from AVDD_RB",
  271. [24] = "N/A",
  272. [25] = "N/A",
  273. [26] = "N/A",
  274. [27] = "Triggered from FAULT_FAULT_N",
  275. [28] = "Triggered from FAULT_PBS_WATCHDOG_TO",
  276. [29] = "Triggered from FAULT_PBS_NACK",
  277. [30] = "Triggered from FAULT_RESTART_PON",
  278. [31] = "Triggered from OTST3 (Over Temperature)",
  279. /* QPNP_PON_GEN2 S3_RESET reasons */
  280. [32] = "N/A",
  281. [33] = "N/A",
  282. [34] = "N/A",
  283. [35] = "N/A",
  284. [36] = "Triggered from S3_RESET_FAULT_N",
  285. [37] = "Triggered from S3_RESET_PBS_WATCHDOG_TO",
  286. [38] = "Triggered from S3_RESET_PBS_NACK",
  287. [39] = "Triggered from S3_RESET_KPDPWR_ANDOR_RESIN",
  288. };
  289. static int qpnp_pon_store_reg(struct qpnp_pon *pon, u16 addr)
  290. {
  291. int rc;
  292. unsigned int val;
  293. struct pon_reg *reg, *pos = NULL;
  294. mutex_lock(&pon->restore_lock);
  295. rc = regmap_read(pon->regmap, addr, &val);
  296. if (rc < 0) {
  297. dev_info(pon->dev, "Register read failed, addr=0x%04X, rc=%d\n",
  298. addr, rc);
  299. } else {
  300. list_for_each_entry(pos, &pon->restore_regs, list) {
  301. if (pos->addr == addr) {
  302. pos->val = val;
  303. goto done;
  304. }
  305. }
  306. reg = devm_kzalloc(pon->dev, sizeof(*reg), GFP_KERNEL);
  307. if (!reg) {
  308. rc = -ENOMEM;
  309. goto done;
  310. }
  311. reg->addr = addr;
  312. reg->val = val;
  313. INIT_LIST_HEAD(&reg->list);
  314. list_add_tail(&reg->list, &pon->restore_regs);
  315. }
  316. done:
  317. mutex_unlock(&pon->restore_lock);
  318. return rc;
  319. }
  320. static int
  321. qpnp_pon_masked_write(struct qpnp_pon *pon, u16 addr, u8 mask, u8 val)
  322. {
  323. int rc;
  324. rc = regmap_update_bits(pon->regmap, addr, mask, val);
  325. if (rc)
  326. dev_err(pon->dev, "Register write failed, addr=0x%04X, rc=%d\n",
  327. addr, rc);
  328. return rc;
  329. }
  330. static int qpnp_pon_write(struct qpnp_pon *pon, u16 addr, u8 val)
  331. {
  332. int rc;
  333. rc = regmap_write(pon->regmap, addr, val);
  334. if (rc)
  335. dev_err(pon->dev, "Register write failed, addr=0x%04X, rc=%d\n",
  336. addr, rc);
  337. return rc;
  338. }
  339. static int
  340. qpnp_pon_masked_write_backup(struct qpnp_pon *pon, u16 addr, u8 mask, u8 val)
  341. {
  342. int rc;
  343. rc = qpnp_pon_masked_write(pon, addr, mask, val);
  344. if (rc < 0)
  345. return rc;
  346. return qpnp_pon_store_reg(pon, addr);
  347. }
  348. static int qpnp_pon_read(struct qpnp_pon *pon, u16 addr, unsigned int *val)
  349. {
  350. int rc;
  351. rc = regmap_read(pon->regmap, addr, val);
  352. if (rc)
  353. dev_err(pon->dev, "Register read failed, addr=0x%04X, rc=%d\n",
  354. addr, rc);
  355. return rc;
  356. }
  357. static bool is_pon_gen1(struct qpnp_pon *pon)
  358. {
  359. return pon->subtype == PON_PRIMARY || pon->subtype == PON_SECONDARY;
  360. }
  361. static bool is_pon_gen2(struct qpnp_pon *pon)
  362. {
  363. return pon->subtype == PON_GEN2_PRIMARY ||
  364. pon->subtype == PON_GEN2_SECONDARY;
  365. }
  366. static bool is_pon_gen3(struct qpnp_pon *pon)
  367. {
  368. return pon->subtype == PON_GEN3_PBS ||
  369. pon->subtype == PON_GEN3_HLOS;
  370. }
  371. /**
  372. * qpnp_pon_set_restart_reason() - Store device restart reason in PMIC register
  373. *
  374. * Returns 0 if PMIC feature is not available or store restart reason
  375. * successfully.
  376. * Returns < 0 for errors
  377. *
  378. * This function is used to store device restart reason in PMIC register.
  379. * It checks here to see if the restart reason register has been specified.
  380. * If it hasn't, this function should immediately return 0
  381. */
  382. int qpnp_pon_set_restart_reason(enum pon_restart_reason reason)
  383. {
  384. int rc = 0;
  385. struct qpnp_pon *pon = sys_reset_dev;
  386. if (!pon)
  387. return 0;
  388. if (!pon->store_hard_reset_reason)
  389. return 0;
  390. if ((is_pon_gen2(pon) || is_pon_gen3(pon)) && !pon->legacy_hard_reset_offset)
  391. rc = qpnp_pon_masked_write(pon, QPNP_PON_SOFT_RB_SPARE(pon),
  392. GENMASK(7, 1), (reason << 1));
  393. else
  394. rc = qpnp_pon_masked_write(pon, QPNP_PON_SOFT_RB_SPARE(pon),
  395. GENMASK(7, 2), (reason << 2));
  396. return rc;
  397. }
  398. EXPORT_SYMBOL(qpnp_pon_set_restart_reason);
  399. /*
  400. * qpnp_pon_check_hard_reset_stored() - Checks if the PMIC need to
  401. * store hard reset reason.
  402. *
  403. * Returns true if reset reason can be stored, false if it cannot be stored
  404. *
  405. */
  406. bool qpnp_pon_check_hard_reset_stored(void)
  407. {
  408. struct qpnp_pon *pon = sys_reset_dev;
  409. if (!pon)
  410. return false;
  411. return pon->store_hard_reset_reason;
  412. }
  413. EXPORT_SYMBOL(qpnp_pon_check_hard_reset_stored);
  414. static int qpnp_pon_set_dbc(struct qpnp_pon *pon, u32 delay)
  415. {
  416. int rc = 0;
  417. u32 val;
  418. if (delay == pon->dbc_time_us)
  419. return 0;
  420. if (pon->pon_input)
  421. mutex_lock(&pon->pon_input->mutex);
  422. if (is_pon_gen2(pon)) {
  423. if (delay < QPNP_PON_GEN2_MIN_DBC_US)
  424. delay = QPNP_PON_GEN2_MIN_DBC_US;
  425. else if (delay > QPNP_PON_GEN2_MAX_DBC_US)
  426. delay = QPNP_PON_GEN2_MAX_DBC_US;
  427. val = (delay << QPNP_PON_GEN2_DELAY_BIT_SHIFT) / USEC_PER_SEC;
  428. } else {
  429. if (delay < QPNP_PON_MIN_DBC_US)
  430. delay = QPNP_PON_MIN_DBC_US;
  431. else if (delay > QPNP_PON_MAX_DBC_US)
  432. delay = QPNP_PON_MAX_DBC_US;
  433. val = (delay << QPNP_PON_DELAY_BIT_SHIFT) / USEC_PER_SEC;
  434. }
  435. val = ilog2(val);
  436. rc = qpnp_pon_masked_write_backup(pon, QPNP_PON_DBC_CTL(pon),
  437. QPNP_PON_DBC_DELAY_MASK(pon), val);
  438. if (!rc)
  439. pon->dbc_time_us = delay;
  440. if (pon->pon_input)
  441. mutex_unlock(&pon->pon_input->mutex);
  442. return rc;
  443. }
  444. static int qpnp_pon_get_dbc(struct qpnp_pon *pon, u32 *delay)
  445. {
  446. int rc;
  447. unsigned int val;
  448. if (is_pon_gen3(pon) && pon->pbs_base)
  449. rc = qpnp_pon_read(pon, QPNP_PON_PBS_DBC_CTL(pon), &val);
  450. else
  451. rc = qpnp_pon_read(pon, QPNP_PON_DBC_CTL(pon), &val);
  452. if (rc)
  453. return rc;
  454. val &= QPNP_PON_DBC_DELAY_MASK(pon);
  455. if (is_pon_gen2(pon) || is_pon_gen3(pon))
  456. *delay = USEC_PER_SEC /
  457. (1 << (QPNP_PON_GEN2_DELAY_BIT_SHIFT - val));
  458. else
  459. *delay = USEC_PER_SEC /
  460. (1 << (QPNP_PON_DELAY_BIT_SHIFT - val));
  461. return rc;
  462. }
  463. static ssize_t debounce_us_show(struct device *dev,
  464. struct device_attribute *attr, char *buf)
  465. {
  466. struct qpnp_pon *pon = dev_get_drvdata(dev);
  467. return scnprintf(buf, QPNP_PON_BUFFER_SIZE, "%d\n", pon->dbc_time_us);
  468. }
  469. static ssize_t debounce_us_store(struct device *dev,
  470. struct device_attribute *attr,
  471. const char *buf, size_t size)
  472. {
  473. struct qpnp_pon *pon = dev_get_drvdata(dev);
  474. u32 value;
  475. int rc;
  476. if (size > QPNP_PON_BUFFER_SIZE)
  477. return -EINVAL;
  478. rc = kstrtou32(buf, 10, &value);
  479. if (rc)
  480. return rc;
  481. rc = qpnp_pon_set_dbc(pon, value);
  482. if (rc < 0)
  483. return rc;
  484. return size;
  485. }
  486. static DEVICE_ATTR_RW(debounce_us);
  487. static int qpnp_pon_reset_config(struct qpnp_pon *pon,
  488. enum pon_power_off_type type)
  489. {
  490. int rc;
  491. bool disable = false;
  492. u16 rst_en_reg;
  493. if (pon->pon_ver == QPNP_PON_GEN1_V1)
  494. rst_en_reg = QPNP_PON_PS_HOLD_RST_CTL(pon);
  495. else
  496. rst_en_reg = QPNP_PON_PS_HOLD_RST_CTL2(pon);
  497. /*
  498. * Based on the power-off type set for a PON device through device tree
  499. * change the type being configured into PS_HOLD_RST_CTL.
  500. */
  501. switch (type) {
  502. case PON_POWER_OFF_WARM_RESET:
  503. if (pon->warm_reset_poff_type != -EINVAL)
  504. type = pon->warm_reset_poff_type;
  505. break;
  506. case PON_POWER_OFF_HARD_RESET:
  507. if (pon->hard_reset_poff_type != -EINVAL)
  508. type = pon->hard_reset_poff_type;
  509. disable = pon->ps_hold_hard_reset_disable;
  510. break;
  511. case PON_POWER_OFF_SHUTDOWN:
  512. if (pon->shutdown_poff_type != -EINVAL)
  513. type = pon->shutdown_poff_type;
  514. disable = pon->ps_hold_shutdown_disable;
  515. break;
  516. default:
  517. break;
  518. }
  519. rc = qpnp_pon_masked_write(pon, rst_en_reg, QPNP_PON_RESET_EN, 0);
  520. if (rc)
  521. return rc;
  522. /*
  523. * Check if ps-hold power off configuration needs to be disabled.
  524. * If yes, then return without configuring.
  525. */
  526. if (disable)
  527. return 0;
  528. /*
  529. * We need 10 sleep clock cycles here. But since the clock is
  530. * internally generated, we need to add 50% tolerance to be
  531. * conservative.
  532. */
  533. udelay(500);
  534. rc = qpnp_pon_masked_write(pon, QPNP_PON_PS_HOLD_RST_CTL(pon),
  535. QPNP_PON_POWER_OFF_MASK, type);
  536. if (rc)
  537. return rc;
  538. rc = qpnp_pon_masked_write(pon, rst_en_reg, QPNP_PON_RESET_EN,
  539. QPNP_PON_RESET_EN);
  540. if (rc)
  541. return rc;
  542. dev_dbg(pon->dev, "ps_hold power off type = 0x%02X\n", type);
  543. return 0;
  544. }
  545. static int qpnp_resin_pon_reset_config(struct qpnp_pon *pon,
  546. enum pon_power_off_type type)
  547. {
  548. int rc;
  549. bool disable = false;
  550. u16 rst_en_reg;
  551. if (pon->pon_ver == QPNP_PON_GEN1_V1)
  552. rst_en_reg = QPNP_PON_RESIN_S2_CNTL(pon);
  553. else
  554. rst_en_reg = QPNP_PON_RESIN_S2_CNTL2(pon);
  555. /*
  556. * Based on the power-off type set for a PON device through device tree
  557. * change the type being configured into PON_RESIN_S2_CTL.
  558. */
  559. switch (type) {
  560. case PON_POWER_OFF_WARM_RESET:
  561. if (pon->resin_warm_reset_type != -EINVAL)
  562. type = pon->resin_warm_reset_type;
  563. break;
  564. case PON_POWER_OFF_HARD_RESET:
  565. if (pon->resin_hard_reset_type != -EINVAL)
  566. type = pon->resin_hard_reset_type;
  567. disable = pon->resin_hard_reset_disable;
  568. break;
  569. case PON_POWER_OFF_SHUTDOWN:
  570. if (pon->resin_shutdown_type != -EINVAL)
  571. type = pon->resin_shutdown_type;
  572. disable = pon->resin_shutdown_disable;
  573. break;
  574. default:
  575. break;
  576. }
  577. rc = qpnp_pon_masked_write(pon, rst_en_reg, QPNP_PON_S2_CNTL_EN, 0);
  578. if (rc)
  579. return rc;
  580. /*
  581. * Check if resin power off configuration needs to be disabled.
  582. * If yes, then return without configuring.
  583. */
  584. if (disable)
  585. return 0;
  586. /*
  587. * We need 10 sleep clock cycles here. But since the clock is
  588. * internally generated, we need to add 50% tolerance to be
  589. * conservative.
  590. */
  591. udelay(500);
  592. rc = qpnp_pon_masked_write(pon, QPNP_PON_RESIN_S2_CNTL(pon),
  593. QPNP_PON_S2_CNTL_TYPE_MASK, type);
  594. if (rc)
  595. return rc;
  596. rc = qpnp_pon_masked_write(pon, rst_en_reg, QPNP_PON_S2_CNTL_EN,
  597. QPNP_PON_S2_CNTL_EN);
  598. if (rc)
  599. return rc;
  600. dev_dbg(pon->dev, "resin power off type = 0x%02X\n", type);
  601. return 0;
  602. }
  603. /**
  604. * qpnp_pon_system_pwr_off() - Configure system-reset PMIC for shutdown or reset
  605. * @type: Determines the type of power off to perform - shutdown, reset, etc
  606. *
  607. * This function supports configuration of multiple PMICs. In some cases, the
  608. * PON of secondary PMICs also needs to be configured, so this supports that
  609. * requirement. Once the system-reset and secondary PMIC is configured properly,
  610. * the MSM can drop PS_HOLD to activate the specified configuration.
  611. *
  612. * Note that this function may be called from atomic context as in the case of
  613. * the panic notifier path and thus it should not rely on function calls that
  614. * may sleep.
  615. */
  616. int qpnp_pon_system_pwr_off(enum pon_power_off_type type)
  617. {
  618. struct qpnp_pon *pon, *tmp;
  619. unsigned long flags;
  620. int rc;
  621. if (!sys_reset_dev)
  622. return -ENODEV;
  623. rc = qpnp_pon_reset_config(sys_reset_dev, type);
  624. if (rc) {
  625. dev_err(sys_reset_dev->dev, "Error configuring main PON, rc=%d\n",
  626. rc);
  627. return rc;
  628. }
  629. /*
  630. * Check if a secondary PON device needs to be configured. If it
  631. * is available, configure that also as per the requested power-off
  632. * type
  633. */
  634. spin_lock_irqsave(&spon_list_slock, flags);
  635. if (list_empty(&spon_dev_list))
  636. goto out;
  637. list_for_each_entry_safe(pon, tmp, &spon_dev_list, list) {
  638. dev_emerg(pon->dev, "PMIC@SID%d: configuring PON for reset\n",
  639. to_spmi_device(pon->dev->parent)->usid);
  640. rc = qpnp_pon_reset_config(pon, type);
  641. if (rc) {
  642. dev_err(pon->dev, "Error configuring secondary PON, rc=%d\n",
  643. rc);
  644. goto out;
  645. }
  646. if (pon->resin_pon_reset) {
  647. rc = qpnp_resin_pon_reset_config(pon, type);
  648. if (rc) {
  649. dev_err(pon->dev, "Error configuring secondary PON resin, rc=%d\n",
  650. rc);
  651. goto out;
  652. }
  653. }
  654. }
  655. out:
  656. spin_unlock_irqrestore(&spon_list_slock, flags);
  657. return rc;
  658. }
  659. EXPORT_SYMBOL(qpnp_pon_system_pwr_off);
  660. /**
  661. * qpnp_pon_modem_pwr_off() - shutdown or reset the modem PMIC
  662. * @type: Determines the type of power off to perform - shutdown, reset, etc
  663. *
  664. * This function causes the immediate shutdown or reset of the primary PMIC
  665. * for an attached modem chip.
  666. *
  667. * Return: 0 for success or < 0 for errors
  668. */
  669. int qpnp_pon_modem_pwr_off(enum pon_power_off_type type)
  670. {
  671. struct qpnp_pon *pon = modem_reset_dev;
  672. int rc;
  673. if (!modem_reset_dev)
  674. return -ENODEV;
  675. rc = qpnp_pon_write(pon, QPNP_PON_SW_RST_S2_CTL2(pon), 0);
  676. if (rc)
  677. return rc;
  678. /* Wait for at least 10 sleep clock cycles. */
  679. udelay(500);
  680. rc = qpnp_pon_write(pon, QPNP_PON_SW_RST_S2_CTL(pon), type);
  681. if (rc)
  682. return rc;
  683. rc = qpnp_pon_write(pon, QPNP_PON_SW_RST_S2_CTL2(pon),
  684. QPNP_PON_RESET_EN);
  685. if (rc)
  686. return rc;
  687. /* Wait for at least 10 sleep clock cycles. */
  688. udelay(500);
  689. rc = qpnp_pon_write(pon, QPNP_PON_SW_RST_GO(pon),
  690. QPNP_PON_SW_RST_GO_VAL);
  691. if (rc)
  692. return rc;
  693. dev_dbg(pon->dev, "modem sw power off type = 0x%02X\n", type);
  694. return 0;
  695. }
  696. EXPORT_SYMBOL(qpnp_pon_modem_pwr_off);
  697. static int _qpnp_pon_is_warm_reset(struct qpnp_pon *pon)
  698. {
  699. if (!pon)
  700. return -ENODEV;
  701. if (is_pon_gen1(pon) || pon->subtype == PON_1REG)
  702. return pon->warm_reset_reason1
  703. || (pon->warm_reset_reason2 & QPNP_PON_WARM_RESET_TFT);
  704. else
  705. return pon->warm_reset_reason1;
  706. }
  707. /**
  708. * qpnp_pon_is_warm_reset - Checks if the PMIC went through a warm reset.
  709. *
  710. * Returns > 0 for warm resets, 0 for not warm reset, < 0 for errors
  711. *
  712. * Note that this function will only return the warm vs not-warm reset status
  713. * of the PMIC that is configured as the system-reset device.
  714. */
  715. int qpnp_pon_is_warm_reset(void)
  716. {
  717. if (!sys_reset_dev)
  718. return -EPROBE_DEFER;
  719. if (is_pon_gen3(sys_reset_dev))
  720. return -EPERM;
  721. return _qpnp_pon_is_warm_reset(sys_reset_dev);
  722. }
  723. EXPORT_SYMBOL(qpnp_pon_is_warm_reset);
  724. /**
  725. * qpnp_pon_wd_config() - configure the watch dog behavior for warm reset
  726. * @enable: to enable or disable the PON watch dog
  727. *
  728. * Return: 0 for success or < 0 for errors
  729. */
  730. int qpnp_pon_wd_config(bool enable)
  731. {
  732. if (!sys_reset_dev)
  733. return -EPROBE_DEFER;
  734. if (is_pon_gen3(sys_reset_dev))
  735. return -EPERM;
  736. return qpnp_pon_masked_write(sys_reset_dev,
  737. QPNP_PON_WD_RST_S2_CTL2(sys_reset_dev),
  738. QPNP_PON_WD_EN, enable ? QPNP_PON_WD_EN : 0);
  739. }
  740. EXPORT_SYMBOL(qpnp_pon_wd_config);
  741. static int qpnp_pon_get_trigger_config(enum pon_trigger_source pon_src,
  742. bool *enabled)
  743. {
  744. struct qpnp_pon *pon = sys_reset_dev;
  745. int val, rc;
  746. u16 addr;
  747. u8 mask;
  748. if (!pon)
  749. return -ENODEV;
  750. if (pon_src < PON_SMPL || pon_src > PON_KPDPWR_N) {
  751. dev_err(pon->dev, "Invalid PON source %d\n", pon_src);
  752. return -EINVAL;
  753. }
  754. addr = QPNP_PON_TRIGGER_EN(pon);
  755. mask = BIT(pon_src);
  756. if (is_pon_gen2(pon) && pon_src == PON_SMPL) {
  757. addr = QPNP_PON_SMPL_CTL(pon);
  758. mask = QPNP_PON_SMPL_EN;
  759. }
  760. rc = qpnp_pon_read(pon, addr, &val);
  761. if (rc)
  762. return rc;
  763. *enabled = !!(val & mask);
  764. return 0;
  765. }
  766. /**
  767. * qpnp_pon_trigger_config() - Configures enable state of the PON trigger source
  768. * @pon_src: PON source to be configured
  769. * @enable: to enable or disable the PON trigger
  770. *
  771. * This function configures the power-on trigger capability of a
  772. * PON source. If a specific PON trigger is disabled it cannot act
  773. * as a power-on source to the PMIC.
  774. */
  775. int qpnp_pon_trigger_config(enum pon_trigger_source pon_src, bool enable)
  776. {
  777. struct qpnp_pon *pon = sys_reset_dev;
  778. int rc;
  779. if (!pon)
  780. return -EPROBE_DEFER;
  781. if (pon_src < PON_SMPL || pon_src > PON_KPDPWR_N) {
  782. dev_err(pon->dev, "Invalid PON source %d\n", pon_src);
  783. return -EINVAL;
  784. }
  785. if (is_pon_gen3(sys_reset_dev))
  786. return -EPERM;
  787. if (is_pon_gen2(pon) && pon_src == PON_SMPL)
  788. rc = qpnp_pon_masked_write_backup(pon, QPNP_PON_SMPL_CTL(pon),
  789. QPNP_PON_SMPL_EN, enable ? QPNP_PON_SMPL_EN : 0);
  790. else
  791. rc = qpnp_pon_masked_write_backup(pon, QPNP_PON_TRIGGER_EN(pon),
  792. BIT(pon_src), enable ? BIT(pon_src) : 0);
  793. return rc;
  794. }
  795. EXPORT_SYMBOL(qpnp_pon_trigger_config);
  796. /*
  797. * This function stores the PMIC warm reset reason register values. It also
  798. * clears these registers if the qcom,clear-warm-reset device tree property
  799. * is specified.
  800. */
  801. static int qpnp_pon_store_and_clear_warm_reset(struct qpnp_pon *pon)
  802. {
  803. int rc;
  804. uint val;
  805. rc = qpnp_pon_read(pon, QPNP_PON_WARM_RESET_REASON1(pon), &val);
  806. if (rc)
  807. return rc;
  808. pon->warm_reset_reason1 = (u8)val;
  809. if (is_pon_gen1(pon) || pon->subtype == PON_1REG) {
  810. rc = qpnp_pon_read(pon, QPNP_PON_WARM_RESET_REASON2(pon), &val);
  811. if (rc)
  812. return rc;
  813. pon->warm_reset_reason2 = (u8)val;
  814. }
  815. if (of_property_read_bool(pon->dev->of_node, "qcom,clear-warm-reset")) {
  816. rc = regmap_write(pon->regmap, QPNP_PON_WARM_RESET_REASON1(pon),
  817. 0);
  818. if (rc) {
  819. dev_err(pon->dev, "Register write failed, addr=0x%04X, rc=%d\n",
  820. QPNP_PON_WARM_RESET_REASON1(pon), rc);
  821. return rc;
  822. }
  823. qpnp_pon_store_reg(pon, QPNP_PON_WARM_RESET_REASON1(pon));
  824. }
  825. return 0;
  826. }
  827. static struct qpnp_pon_config *qpnp_get_cfg(struct qpnp_pon *pon, u32 pon_type)
  828. {
  829. int i;
  830. for (i = 0; i < pon->num_pon_config; i++) {
  831. if (pon_type == pon->pon_cfg[i].pon_type)
  832. return &pon->pon_cfg[i];
  833. }
  834. return NULL;
  835. }
  836. static int qpnp_pon_input_dispatch(struct qpnp_pon *pon, u32 pon_type)
  837. {
  838. struct qpnp_pon_config *cfg = NULL;
  839. u8 pon_rt_bit = 0;
  840. u32 key_status;
  841. uint pon_rt_sts;
  842. u64 elapsed_us;
  843. int rc;
  844. cfg = qpnp_get_cfg(pon, pon_type);
  845. if (!cfg)
  846. return -EINVAL;
  847. /* Check if key reporting is supported */
  848. if (!cfg->key_code)
  849. return 0;
  850. if (pon->kpdpwr_dbc_enable && cfg->pon_type == PON_KPDPWR) {
  851. elapsed_us = ktime_us_delta(ktime_get(),
  852. pon->kpdpwr_last_release_time);
  853. if (elapsed_us < pon->dbc_time_us) {
  854. pr_debug("Ignoring kpdpwr event; within debounce time\n");
  855. return 0;
  856. }
  857. }
  858. /* Check the RT status to get the current status of the line */
  859. rc = qpnp_pon_read(pon, QPNP_PON_RT_STS(pon), &pon_rt_sts);
  860. if (rc)
  861. return rc;
  862. switch (cfg->pon_type) {
  863. case PON_KPDPWR:
  864. pon_rt_bit = is_pon_gen3(pon)
  865. ? QPNP_PON_GEN3_KPDPWR_N_SET
  866. : QPNP_PON_KPDPWR_N_SET;
  867. break;
  868. case PON_RESIN:
  869. pon_rt_bit = is_pon_gen3(pon)
  870. ? QPNP_PON_GEN3_RESIN_N_SET
  871. : QPNP_PON_RESIN_N_SET;
  872. break;
  873. case PON_CBLPWR:
  874. pon_rt_bit = QPNP_PON_CBLPWR_N_SET;
  875. break;
  876. case PON_KPDPWR_RESIN:
  877. pon_rt_bit = QPNP_PON_KPDPWR_RESIN_BARK_N_SET;
  878. break;
  879. default:
  880. return -EINVAL;
  881. }
  882. pr_debug("PMIC input: code=%d, status=0x%02X\n", cfg->key_code,
  883. pon_rt_sts);
  884. key_status = pon_rt_sts & pon_rt_bit;
  885. if (pon->kpdpwr_dbc_enable && cfg->pon_type == PON_KPDPWR) {
  886. if (!key_status)
  887. pon->kpdpwr_last_release_time = ktime_get();
  888. }
  889. /*
  890. * Simulate a press event in case release event occurred without a press
  891. * event
  892. */
  893. if (!cfg->old_state && !key_status) {
  894. input_report_key(pon->pon_input, cfg->key_code, 1);
  895. input_sync(pon->pon_input);
  896. }
  897. input_report_key(pon->pon_input, cfg->key_code, key_status);
  898. input_sync(pon->pon_input);
  899. cfg->old_state = !!key_status;
  900. return 0;
  901. }
  902. static irqreturn_t qpnp_kpdpwr_irq(int irq, void *_pon)
  903. {
  904. int rc;
  905. struct qpnp_pon *pon = _pon;
  906. rc = qpnp_pon_input_dispatch(pon, PON_KPDPWR);
  907. if (rc)
  908. dev_err(pon->dev, "Unable to send input event, rc=%d\n", rc);
  909. return IRQ_HANDLED;
  910. }
  911. static irqreturn_t qpnp_kpdpwr_bark_irq(int irq, void *_pon)
  912. {
  913. return IRQ_HANDLED;
  914. }
  915. static irqreturn_t qpnp_resin_irq(int irq, void *_pon)
  916. {
  917. int rc;
  918. struct qpnp_pon *pon = _pon;
  919. rc = qpnp_pon_input_dispatch(pon, PON_RESIN);
  920. if (rc)
  921. dev_err(pon->dev, "Unable to send input event, rc=%d\n", rc);
  922. return IRQ_HANDLED;
  923. }
  924. static irqreturn_t qpnp_kpdpwr_resin_bark_irq(int irq, void *_pon)
  925. {
  926. return IRQ_HANDLED;
  927. }
  928. static irqreturn_t qpnp_cblpwr_irq(int irq, void *_pon)
  929. {
  930. int rc;
  931. struct qpnp_pon *pon = _pon;
  932. rc = qpnp_pon_input_dispatch(pon, PON_CBLPWR);
  933. if (rc)
  934. dev_err(pon->dev, "Unable to send input event, rc=%d\n", rc);
  935. return IRQ_HANDLED;
  936. }
  937. static void print_pon_reg(struct qpnp_pon *pon, u16 offset)
  938. {
  939. int rc;
  940. u16 addr;
  941. uint reg;
  942. addr = pon->base + offset;
  943. rc = regmap_read(pon->regmap, addr, &reg);
  944. if (rc)
  945. dev_emerg(pon->dev, "Register read failed, addr=0x%04X, rc=%d\n",
  946. addr, rc);
  947. else
  948. dev_emerg(pon->dev, "reg@0x%04X: 0x%02X\n", addr, reg);
  949. }
  950. #define PON_PBL_STATUS 0x7
  951. #define PON_PON_REASON1(subtype) PON_OFFSET(subtype, 0x8, 0xC0)
  952. #define PON_PON_REASON2 0x9
  953. #define PON_WARM_RESET_REASON1(subtype) PON_OFFSET(subtype, 0xA, 0xC2)
  954. #define PON_WARM_RESET_REASON2 0xB
  955. #define PON_POFF_REASON1(subtype) PON_OFFSET(subtype, 0xC, 0xC5)
  956. #define PON_POFF_REASON2 0xD
  957. #define PON_SOFT_RESET_REASON1(subtype) PON_OFFSET(subtype, 0xE, 0xCB)
  958. #define PON_SOFT_RESET_REASON2 0xF
  959. #define PON_FAULT_REASON1 0xC8
  960. #define PON_FAULT_REASON2 0xC9
  961. #define PON_PMIC_WD_RESET_S1_TIMER 0x54
  962. #define PON_PMIC_WD_RESET_S2_TIMER 0x55
  963. static irqreturn_t qpnp_pmic_wd_bark_irq(int irq, void *_pon)
  964. {
  965. struct qpnp_pon *pon = _pon;
  966. print_pon_reg(pon, PON_PBL_STATUS);
  967. print_pon_reg(pon, PON_PON_REASON1(pon->subtype));
  968. print_pon_reg(pon, PON_WARM_RESET_REASON1(pon->subtype));
  969. print_pon_reg(pon, PON_SOFT_RESET_REASON1(pon->subtype));
  970. print_pon_reg(pon, PON_POFF_REASON1(pon->subtype));
  971. if (is_pon_gen1(pon) || pon->subtype == PON_1REG) {
  972. print_pon_reg(pon, PON_PON_REASON2);
  973. print_pon_reg(pon, PON_WARM_RESET_REASON2);
  974. print_pon_reg(pon, PON_POFF_REASON2);
  975. print_pon_reg(pon, PON_SOFT_RESET_REASON2);
  976. } else {
  977. print_pon_reg(pon, PON_FAULT_REASON1);
  978. print_pon_reg(pon, PON_FAULT_REASON2);
  979. }
  980. print_pon_reg(pon, PON_PMIC_WD_RESET_S1_TIMER);
  981. print_pon_reg(pon, PON_PMIC_WD_RESET_S2_TIMER);
  982. panic("PMIC Watch Dog Triggered");
  983. return IRQ_HANDLED;
  984. }
  985. static void bark_work_func(struct work_struct *work)
  986. {
  987. struct qpnp_pon *pon =
  988. container_of(work, struct qpnp_pon, bark_work.work);
  989. uint pon_rt_sts = 0;
  990. struct qpnp_pon_config *cfg;
  991. int rc;
  992. cfg = qpnp_get_cfg(pon, PON_RESIN);
  993. if (!cfg) {
  994. dev_err(pon->dev, "Invalid config pointer\n");
  995. return;
  996. }
  997. /* Enable reset */
  998. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr, QPNP_PON_S2_CNTL_EN,
  999. QPNP_PON_S2_CNTL_EN);
  1000. if (rc)
  1001. return;
  1002. /* Wait for bark RT status update delay */
  1003. msleep(100);
  1004. /* Read the bark RT status */
  1005. rc = qpnp_pon_read(pon, QPNP_PON_RT_STS(pon), &pon_rt_sts);
  1006. if (rc)
  1007. return;
  1008. if (!(pon_rt_sts & QPNP_PON_RESIN_BARK_N_SET)) {
  1009. /* Report the key event and enable the bark IRQ */
  1010. input_report_key(pon->pon_input, cfg->key_code, 0);
  1011. input_sync(pon->pon_input);
  1012. enable_irq(cfg->bark_irq);
  1013. } else {
  1014. /* Disable reset */
  1015. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  1016. QPNP_PON_S2_CNTL_EN, 0);
  1017. if (rc)
  1018. return;
  1019. /* Re-arm the work */
  1020. schedule_delayed_work(&pon->bark_work, QPNP_KEY_STATUS_DELAY);
  1021. }
  1022. }
  1023. static irqreturn_t qpnp_resin_bark_irq(int irq, void *_pon)
  1024. {
  1025. struct qpnp_pon *pon = _pon;
  1026. struct qpnp_pon_config *cfg;
  1027. int rc;
  1028. /* Disable the bark interrupt */
  1029. disable_irq_nosync(irq);
  1030. cfg = qpnp_get_cfg(pon, PON_RESIN);
  1031. if (!cfg) {
  1032. dev_err(pon->dev, "Invalid config pointer\n");
  1033. return IRQ_HANDLED;
  1034. }
  1035. /* Disable reset */
  1036. rc = qpnp_pon_masked_write(pon, cfg->s2_cntl2_addr,
  1037. QPNP_PON_S2_CNTL_EN, 0);
  1038. if (rc)
  1039. return IRQ_HANDLED;
  1040. /* Report the key event */
  1041. input_report_key(pon->pon_input, cfg->key_code, 1);
  1042. input_sync(pon->pon_input);
  1043. /* Schedule work to check the bark status for key-release */
  1044. schedule_delayed_work(&pon->bark_work, QPNP_KEY_STATUS_DELAY);
  1045. return IRQ_HANDLED;
  1046. }
  1047. static int qpnp_config_pull(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  1048. {
  1049. u8 pull_bit;
  1050. if (is_pon_gen3(pon)) {
  1051. /* PON_GEN3 doesn't have pull control */
  1052. return 0;
  1053. }
  1054. switch (cfg->pon_type) {
  1055. case PON_KPDPWR:
  1056. pull_bit = QPNP_PON_KPDPWR_PULL_UP;
  1057. break;
  1058. case PON_RESIN:
  1059. pull_bit = QPNP_PON_RESIN_PULL_UP;
  1060. break;
  1061. case PON_CBLPWR:
  1062. pull_bit = QPNP_PON_CBLPWR_PULL_UP;
  1063. break;
  1064. case PON_KPDPWR_RESIN:
  1065. pull_bit = QPNP_PON_KPDPWR_PULL_UP | QPNP_PON_RESIN_PULL_UP;
  1066. break;
  1067. default:
  1068. return -EINVAL;
  1069. }
  1070. return qpnp_pon_masked_write_backup(pon, QPNP_PON_PULL_CTL(pon),
  1071. pull_bit, cfg->pull_up ? pull_bit : 0);
  1072. }
  1073. static int qpnp_config_reset(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  1074. {
  1075. u16 s1_timer_addr, s2_timer_addr;
  1076. int rc;
  1077. u8 i;
  1078. switch (cfg->pon_type) {
  1079. case PON_KPDPWR:
  1080. s1_timer_addr = QPNP_PON_KPDPWR_S1_TIMER(pon);
  1081. s2_timer_addr = QPNP_PON_KPDPWR_S2_TIMER(pon);
  1082. break;
  1083. case PON_RESIN:
  1084. s1_timer_addr = QPNP_PON_RESIN_S1_TIMER(pon);
  1085. s2_timer_addr = QPNP_PON_RESIN_S2_TIMER(pon);
  1086. break;
  1087. case PON_KPDPWR_RESIN:
  1088. s1_timer_addr = QPNP_PON_KPDPWR_RESIN_S1_TIMER(pon);
  1089. s2_timer_addr = QPNP_PON_KPDPWR_RESIN_S2_TIMER(pon);
  1090. break;
  1091. default:
  1092. return -EINVAL;
  1093. }
  1094. /* Disable S2 reset */
  1095. rc = qpnp_pon_masked_write_backup(pon, cfg->s2_cntl2_addr,
  1096. QPNP_PON_S2_CNTL_EN, 0);
  1097. if (rc)
  1098. return rc;
  1099. usleep_range(100, 120);
  1100. /* configure s1 timer, s2 timer and reset type */
  1101. for (i = 0; i < PON_S1_COUNT_MAX + 1; i++) {
  1102. if (cfg->s1_timer <= s1_delay[i])
  1103. break;
  1104. }
  1105. rc = qpnp_pon_masked_write_backup(pon, s1_timer_addr,
  1106. QPNP_PON_S1_TIMER_MASK, i);
  1107. if (rc)
  1108. return rc;
  1109. i = 0;
  1110. if (cfg->s2_timer) {
  1111. i = cfg->s2_timer / 10;
  1112. i = ilog2(i + 1);
  1113. }
  1114. rc = qpnp_pon_masked_write_backup(pon, s2_timer_addr,
  1115. QPNP_PON_S2_TIMER_MASK, i);
  1116. if (rc)
  1117. return rc;
  1118. rc = qpnp_pon_masked_write_backup(pon, cfg->s2_cntl_addr,
  1119. QPNP_PON_S2_CNTL_TYPE_MASK, (u8)cfg->s2_type);
  1120. if (rc)
  1121. return rc;
  1122. /* Enable S2 reset */
  1123. return qpnp_pon_masked_write_backup(pon, cfg->s2_cntl2_addr,
  1124. QPNP_PON_S2_CNTL_EN, QPNP_PON_S2_CNTL_EN);
  1125. }
  1126. static int
  1127. qpnp_pon_request_irqs(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  1128. {
  1129. int rc;
  1130. switch (cfg->pon_type) {
  1131. case PON_KPDPWR:
  1132. rc = devm_request_irq(pon->dev, cfg->state_irq, qpnp_kpdpwr_irq,
  1133. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1134. "pon_kpdpwr_status", pon);
  1135. if (rc < 0) {
  1136. dev_err(pon->dev, "IRQ %d request failed, rc=%d\n",
  1137. cfg->state_irq, rc);
  1138. return rc;
  1139. }
  1140. if (cfg->use_bark) {
  1141. rc = devm_request_irq(pon->dev, cfg->bark_irq,
  1142. qpnp_kpdpwr_bark_irq,
  1143. IRQF_TRIGGER_RISING,
  1144. "pon_kpdpwr_bark", pon);
  1145. if (rc < 0) {
  1146. dev_err(pon->dev, "IRQ %d request failed, rc=%d\n",
  1147. cfg->bark_irq, rc);
  1148. return rc;
  1149. }
  1150. }
  1151. break;
  1152. case PON_RESIN:
  1153. rc = devm_request_irq(pon->dev, cfg->state_irq, qpnp_resin_irq,
  1154. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1155. "pon_resin_status", pon);
  1156. if (rc < 0) {
  1157. dev_err(pon->dev, "IRQ %d request failed, rc=%d\n",
  1158. cfg->state_irq, rc);
  1159. return rc;
  1160. }
  1161. if (cfg->use_bark) {
  1162. rc = devm_request_irq(pon->dev, cfg->bark_irq,
  1163. qpnp_resin_bark_irq,
  1164. IRQF_TRIGGER_RISING,
  1165. "pon_resin_bark", pon);
  1166. if (rc < 0) {
  1167. dev_err(pon->dev, "IRQ %d request failed, rc=%d\n",
  1168. cfg->bark_irq, rc);
  1169. return rc;
  1170. }
  1171. }
  1172. break;
  1173. case PON_CBLPWR:
  1174. rc = devm_request_irq(pon->dev, cfg->state_irq, qpnp_cblpwr_irq,
  1175. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  1176. "pon_cblpwr_status", pon);
  1177. if (rc < 0) {
  1178. dev_err(pon->dev, "IRQ %d request failed, rc=%d\n",
  1179. cfg->state_irq, rc);
  1180. return rc;
  1181. }
  1182. break;
  1183. case PON_KPDPWR_RESIN:
  1184. if (cfg->use_bark) {
  1185. rc = devm_request_irq(pon->dev, cfg->bark_irq,
  1186. qpnp_kpdpwr_resin_bark_irq,
  1187. IRQF_TRIGGER_RISING,
  1188. "pon_kpdpwr_resin_bark", pon);
  1189. if (rc < 0) {
  1190. dev_err(pon->dev, "IRQ %d request failed, rc=%d\n",
  1191. cfg->bark_irq, rc);
  1192. return rc;
  1193. }
  1194. }
  1195. break;
  1196. default:
  1197. return -EINVAL;
  1198. }
  1199. /* Mark the interrupts wakeable if they support linux-key */
  1200. if (cfg->key_code) {
  1201. enable_irq_wake(cfg->state_irq);
  1202. /* Special handling for RESIN due to a hardware bug */
  1203. if (cfg->pon_type == PON_RESIN && cfg->support_reset)
  1204. enable_irq_wake(cfg->bark_irq);
  1205. }
  1206. return 0;
  1207. }
  1208. static int
  1209. qpnp_pon_free_irqs(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  1210. {
  1211. if (cfg->state_irq > 0)
  1212. devm_free_irq(pon->dev, cfg->state_irq, pon);
  1213. if (cfg->use_bark && cfg->bark_irq > 0)
  1214. devm_free_irq(pon->dev, cfg->bark_irq, pon);
  1215. return 0;
  1216. }
  1217. static int
  1218. qpnp_pon_config_input(struct qpnp_pon *pon, struct qpnp_pon_config *cfg)
  1219. {
  1220. if (!pon->pon_input) {
  1221. pon->pon_input = devm_input_allocate_device(pon->dev);
  1222. if (!pon->pon_input)
  1223. return -ENOMEM;
  1224. pon->pon_input->name = "qpnp_pon";
  1225. pon->pon_input->phys = "qpnp_pon/input0";
  1226. }
  1227. input_set_capability(pon->pon_input, EV_KEY, cfg->key_code);
  1228. return 0;
  1229. }
  1230. static int qpnp_pon_config_kpdpwr_init(struct qpnp_pon *pon,
  1231. struct platform_device *pdev,
  1232. struct qpnp_pon_config *cfg,
  1233. struct device_node *node)
  1234. {
  1235. int rc;
  1236. cfg->state_irq = platform_get_irq_byname(pdev, "kpdpwr");
  1237. if (cfg->state_irq < 0) {
  1238. dev_err(pon->dev, "Unable to get kpdpwr irq, rc=%d\n",
  1239. cfg->state_irq);
  1240. return cfg->state_irq;
  1241. }
  1242. rc = of_property_read_u32(node, "qcom,support-reset",
  1243. &cfg->support_reset);
  1244. if (rc) {
  1245. if (rc != -EINVAL) {
  1246. dev_err(pon->dev, "Unable to read qcom,support-reset, rc=%d\n",
  1247. rc);
  1248. return rc;
  1249. }
  1250. } else {
  1251. cfg->config_reset = true;
  1252. }
  1253. cfg->use_bark = of_property_read_bool(node, "qcom,use-bark");
  1254. if (cfg->use_bark) {
  1255. cfg->bark_irq = platform_get_irq_byname(pdev, "kpdpwr-bark");
  1256. if (cfg->bark_irq < 0) {
  1257. dev_err(pon->dev, "Unable to get kpdpwr-bark irq, rc=%d\n",
  1258. cfg->bark_irq);
  1259. return cfg->bark_irq;
  1260. }
  1261. }
  1262. if (pon->pon_ver == QPNP_PON_GEN1_V1) {
  1263. cfg->s2_cntl_addr = cfg->s2_cntl2_addr =
  1264. QPNP_PON_KPDPWR_S2_CNTL(pon);
  1265. } else {
  1266. cfg->s2_cntl_addr = QPNP_PON_KPDPWR_S2_CNTL(pon);
  1267. cfg->s2_cntl2_addr = QPNP_PON_KPDPWR_S2_CNTL2(pon);
  1268. }
  1269. return 0;
  1270. }
  1271. static int qpnp_pon_config_resin_init(struct qpnp_pon *pon,
  1272. struct platform_device *pdev,
  1273. struct qpnp_pon_config *cfg,
  1274. struct device_node *node)
  1275. {
  1276. unsigned int pmic_type, revid_rev4;
  1277. int rc;
  1278. cfg->state_irq = platform_get_irq_byname(pdev, "resin");
  1279. if (cfg->state_irq < 0) {
  1280. dev_err(pon->dev, "Unable to get resin irq, rc=%d\n",
  1281. cfg->state_irq);
  1282. return cfg->state_irq;
  1283. }
  1284. rc = of_property_read_u32(node, "qcom,support-reset",
  1285. &cfg->support_reset);
  1286. if (rc) {
  1287. if (rc != -EINVAL) {
  1288. dev_err(pon->dev, "Unable to read qcom,support-reset, rc=%d\n",
  1289. rc);
  1290. return rc;
  1291. }
  1292. } else {
  1293. cfg->config_reset = true;
  1294. }
  1295. cfg->use_bark = of_property_read_bool(node, "qcom,use-bark");
  1296. rc = qpnp_pon_read(pon, PMIC_VERSION_REG, &pmic_type);
  1297. if (rc)
  1298. return rc;
  1299. if (pmic_type == PMIC_VER_8941) {
  1300. rc = qpnp_pon_read(pon, PMIC_VERSION_REV4_REG, &revid_rev4);
  1301. if (rc)
  1302. return rc;
  1303. /*
  1304. * PM8941 V3 does not have hardware bug. Hence
  1305. * bark is not required from PMIC versions 3.0.
  1306. */
  1307. if (!(revid_rev4 == PMIC8941_V1_REV4 ||
  1308. revid_rev4 == PMIC8941_V2_REV4)) {
  1309. cfg->support_reset = false;
  1310. cfg->use_bark = false;
  1311. }
  1312. }
  1313. if (cfg->use_bark) {
  1314. cfg->bark_irq = platform_get_irq_byname(pdev, "resin-bark");
  1315. if (cfg->bark_irq < 0) {
  1316. dev_err(pon->dev, "Unable to get resin-bark irq, rc=%d\n",
  1317. cfg->bark_irq);
  1318. return cfg->bark_irq;
  1319. }
  1320. }
  1321. if (pon->pon_ver == QPNP_PON_GEN1_V1) {
  1322. cfg->s2_cntl_addr = cfg->s2_cntl2_addr =
  1323. QPNP_PON_RESIN_S2_CNTL(pon);
  1324. } else {
  1325. cfg->s2_cntl_addr = QPNP_PON_RESIN_S2_CNTL(pon);
  1326. cfg->s2_cntl2_addr = QPNP_PON_RESIN_S2_CNTL2(pon);
  1327. }
  1328. return 0;
  1329. }
  1330. static int qpnp_pon_config_cblpwr_init(struct qpnp_pon *pon,
  1331. struct platform_device *pdev,
  1332. struct qpnp_pon_config *cfg,
  1333. struct device_node *node)
  1334. {
  1335. cfg->state_irq = platform_get_irq_byname(pdev, "cblpwr");
  1336. if (cfg->state_irq < 0) {
  1337. dev_err(pon->dev, "Unable to get cblpwr irq, rc=%d\n",
  1338. cfg->state_irq);
  1339. return cfg->state_irq;
  1340. }
  1341. return 0;
  1342. }
  1343. static int qpnp_pon_config_kpdpwr_resin_init(struct qpnp_pon *pon,
  1344. struct platform_device *pdev,
  1345. struct qpnp_pon_config *cfg,
  1346. struct device_node *node)
  1347. {
  1348. int rc;
  1349. rc = of_property_read_u32(node, "qcom,support-reset",
  1350. &cfg->support_reset);
  1351. if (rc) {
  1352. if (rc != -EINVAL) {
  1353. dev_err(pon->dev, "Unable to read qcom,support-reset, rc=%d\n",
  1354. rc);
  1355. return rc;
  1356. }
  1357. } else {
  1358. cfg->config_reset = true;
  1359. }
  1360. cfg->use_bark = of_property_read_bool(node, "qcom,use-bark");
  1361. if (cfg->use_bark) {
  1362. cfg->bark_irq = platform_get_irq_byname(pdev,
  1363. "kpdpwr-resin-bark");
  1364. if (cfg->bark_irq < 0) {
  1365. dev_err(pon->dev, "Unable to get kpdpwr-resin-bark irq, rc=%d\n",
  1366. cfg->bark_irq);
  1367. return cfg->bark_irq;
  1368. }
  1369. }
  1370. if (pon->pon_ver == QPNP_PON_GEN1_V1) {
  1371. cfg->s2_cntl_addr = cfg->s2_cntl2_addr =
  1372. QPNP_PON_KPDPWR_RESIN_S2_CNTL(pon);
  1373. } else {
  1374. cfg->s2_cntl_addr = QPNP_PON_KPDPWR_RESIN_S2_CNTL(pon);
  1375. cfg->s2_cntl2_addr = QPNP_PON_KPDPWR_RESIN_S2_CNTL2(pon);
  1376. }
  1377. return 0;
  1378. }
  1379. static int qpnp_pon_config_parse_reset_info(struct qpnp_pon *pon,
  1380. struct qpnp_pon_config *cfg,
  1381. struct device_node *node)
  1382. {
  1383. int rc;
  1384. if (!cfg->support_reset)
  1385. return 0;
  1386. /*
  1387. * Get the reset parameters (bark debounce time and
  1388. * reset debounce time) for the reset line.
  1389. */
  1390. rc = of_property_read_u32(node, "qcom,s1-timer", &cfg->s1_timer);
  1391. if (rc) {
  1392. dev_err(pon->dev, "Unable to read s1-timer, rc=%d\n", rc);
  1393. return rc;
  1394. }
  1395. if (cfg->s1_timer > QPNP_PON_S1_TIMER_MAX) {
  1396. dev_err(pon->dev, "Invalid S1 debounce time %u\n",
  1397. cfg->s1_timer);
  1398. return -EINVAL;
  1399. }
  1400. rc = of_property_read_u32(node, "qcom,s2-timer", &cfg->s2_timer);
  1401. if (rc) {
  1402. dev_err(pon->dev, "Unable to read s2-timer, rc=%d\n", rc);
  1403. return rc;
  1404. }
  1405. if (cfg->s2_timer > QPNP_PON_S2_TIMER_MAX) {
  1406. dev_err(pon->dev, "Invalid S2 debounce time %u\n",
  1407. cfg->s2_timer);
  1408. return -EINVAL;
  1409. }
  1410. rc = of_property_read_u32(node, "qcom,s2-type", &cfg->s2_type);
  1411. if (rc) {
  1412. dev_err(pon->dev, "Unable to read s2-type, rc=%d\n", rc);
  1413. return rc;
  1414. }
  1415. if (cfg->s2_type > QPNP_PON_RESET_TYPE_MAX) {
  1416. dev_err(pon->dev, "Invalid reset type specified %u\n",
  1417. cfg->s2_type);
  1418. return -EINVAL;
  1419. }
  1420. return 0;
  1421. }
  1422. static int qpnp_pon_config_init(struct qpnp_pon *pon,
  1423. struct platform_device *pdev)
  1424. {
  1425. int rc = 0, i = 0, pmic_wd_bark_irq;
  1426. struct device_node *cfg_node = NULL;
  1427. struct qpnp_pon_config *cfg;
  1428. if (pon->num_pon_config) {
  1429. pon->pon_cfg = devm_kcalloc(pon->dev, pon->num_pon_config,
  1430. sizeof(*pon->pon_cfg), GFP_KERNEL);
  1431. if (!pon->pon_cfg)
  1432. return -ENOMEM;
  1433. }
  1434. /* Iterate through the list of pon configs */
  1435. for_each_available_child_of_node(pon->dev->of_node, cfg_node) {
  1436. if (!of_find_property(cfg_node, "qcom,pon-type", NULL))
  1437. continue;
  1438. cfg = &pon->pon_cfg[i++];
  1439. rc = of_property_read_u32(cfg_node, "qcom,pon-type",
  1440. &cfg->pon_type);
  1441. if (rc) {
  1442. dev_err(pon->dev, "PON type not specified\n");
  1443. return rc;
  1444. }
  1445. switch (cfg->pon_type) {
  1446. case PON_KPDPWR:
  1447. rc = qpnp_pon_config_kpdpwr_init(pon, pdev, cfg,
  1448. cfg_node);
  1449. if (rc)
  1450. return rc;
  1451. break;
  1452. case PON_RESIN:
  1453. rc = qpnp_pon_config_resin_init(pon, pdev, cfg,
  1454. cfg_node);
  1455. if (rc)
  1456. return rc;
  1457. break;
  1458. case PON_CBLPWR:
  1459. rc = qpnp_pon_config_cblpwr_init(pon, pdev, cfg,
  1460. cfg_node);
  1461. if (rc)
  1462. return rc;
  1463. break;
  1464. case PON_KPDPWR_RESIN:
  1465. rc = qpnp_pon_config_kpdpwr_resin_init(pon, pdev, cfg,
  1466. cfg_node);
  1467. if (rc)
  1468. return rc;
  1469. break;
  1470. default:
  1471. dev_err(pon->dev, "PON RESET %u not supported\n",
  1472. cfg->pon_type);
  1473. return -EINVAL;
  1474. }
  1475. rc = qpnp_pon_config_parse_reset_info(pon, cfg, cfg_node);
  1476. if (rc)
  1477. return rc;
  1478. /*
  1479. * Get the standard key parameters. This might not be
  1480. * specified if there is no key mapping on the reset line.
  1481. */
  1482. of_property_read_u32(cfg_node, "linux,code", &cfg->key_code);
  1483. /* Register key configuration */
  1484. if (cfg->key_code) {
  1485. rc = qpnp_pon_config_input(pon, cfg);
  1486. if (rc < 0)
  1487. return rc;
  1488. }
  1489. /* Get the pull-up configuration */
  1490. cfg->pull_up = of_property_read_bool(cfg_node, "qcom,pull-up");
  1491. }
  1492. pmic_wd_bark_irq = platform_get_irq_byname(pdev, "pmic-wd-bark");
  1493. /* Request the pmic-wd-bark irq only if it is defined */
  1494. if (pmic_wd_bark_irq >= 0) {
  1495. rc = devm_request_irq(pon->dev, pmic_wd_bark_irq,
  1496. qpnp_pmic_wd_bark_irq,
  1497. IRQF_TRIGGER_RISING,
  1498. "qpnp_pmic_wd_bark", pon);
  1499. if (rc < 0) {
  1500. dev_err(pon->dev, "Can't request %d IRQ, rc=%d\n",
  1501. pmic_wd_bark_irq, rc);
  1502. return rc;
  1503. }
  1504. }
  1505. /* Register the input device */
  1506. if (pon->pon_input) {
  1507. rc = input_register_device(pon->pon_input);
  1508. if (rc) {
  1509. dev_err(pon->dev, "Can't register pon key: %d\n", rc);
  1510. return rc;
  1511. }
  1512. }
  1513. for (i = 0; i < pon->num_pon_config; i++) {
  1514. cfg = &pon->pon_cfg[i];
  1515. rc = qpnp_config_pull(pon, cfg);
  1516. if (rc)
  1517. return rc;
  1518. if (cfg->config_reset) {
  1519. if (cfg->support_reset) {
  1520. rc = qpnp_config_reset(pon, cfg);
  1521. if (rc)
  1522. return rc;
  1523. } else if (cfg->pon_type != PON_CBLPWR) {
  1524. /* Disable S2 reset */
  1525. rc = qpnp_pon_masked_write_backup(pon,
  1526. cfg->s2_cntl2_addr,
  1527. QPNP_PON_S2_CNTL_EN, 0);
  1528. if (rc)
  1529. return rc;
  1530. }
  1531. }
  1532. rc = qpnp_pon_request_irqs(pon, cfg);
  1533. if (rc)
  1534. return rc;
  1535. }
  1536. device_init_wakeup(pon->dev, true);
  1537. return 0;
  1538. }
  1539. static int pon_spare_regulator_enable(struct regulator_dev *rdev)
  1540. {
  1541. struct pon_regulator *pon_reg = rdev_get_drvdata(rdev);
  1542. u8 value;
  1543. int rc;
  1544. value = BIT(pon_reg->bit) & 0xFF;
  1545. rc = qpnp_pon_masked_write(pon_reg->pon, pon_reg->pon->base +
  1546. pon_reg->addr, value, value);
  1547. if (rc)
  1548. return rc;
  1549. pon_reg->enabled = true;
  1550. return 0;
  1551. }
  1552. static int pon_spare_regulator_disable(struct regulator_dev *rdev)
  1553. {
  1554. struct pon_regulator *pon_reg = rdev_get_drvdata(rdev);
  1555. u8 mask;
  1556. int rc;
  1557. mask = BIT(pon_reg->bit) & 0xFF;
  1558. rc = qpnp_pon_masked_write(pon_reg->pon, pon_reg->pon->base +
  1559. pon_reg->addr, mask, 0);
  1560. if (rc)
  1561. return rc;
  1562. pon_reg->enabled = false;
  1563. return 0;
  1564. }
  1565. static int pon_spare_regulator_is_enable(struct regulator_dev *rdev)
  1566. {
  1567. struct pon_regulator *pon_reg = rdev_get_drvdata(rdev);
  1568. return pon_reg->enabled;
  1569. }
  1570. static const struct regulator_ops pon_spare_reg_ops = {
  1571. .enable = pon_spare_regulator_enable,
  1572. .disable = pon_spare_regulator_disable,
  1573. .is_enabled = pon_spare_regulator_is_enable,
  1574. };
  1575. static int pon_regulator_init(struct qpnp_pon *pon)
  1576. {
  1577. struct device *dev = pon->dev;
  1578. struct regulator_init_data *init_data;
  1579. struct regulator_config reg_cfg = {};
  1580. struct device_node *node;
  1581. struct pon_regulator *pon_reg;
  1582. int rc, i;
  1583. if (!pon->num_pon_reg)
  1584. return 0;
  1585. pon->pon_reg_cfg = devm_kcalloc(dev, pon->num_pon_reg,
  1586. sizeof(*pon->pon_reg_cfg),
  1587. GFP_KERNEL);
  1588. if (!pon->pon_reg_cfg)
  1589. return -ENOMEM;
  1590. i = 0;
  1591. for_each_available_child_of_node(dev->of_node, node) {
  1592. if (!of_find_property(node, "regulator-name", NULL))
  1593. continue;
  1594. pon_reg = &pon->pon_reg_cfg[i++];
  1595. pon_reg->pon = pon;
  1596. rc = of_property_read_u32(node, "qcom,pon-spare-reg-addr",
  1597. &pon_reg->addr);
  1598. if (rc) {
  1599. dev_err(dev, "Unable to read address for regulator, rc=%d\n",
  1600. rc);
  1601. return rc;
  1602. }
  1603. rc = of_property_read_u32(node, "qcom,pon-spare-reg-bit",
  1604. &pon_reg->bit);
  1605. if (rc) {
  1606. dev_err(dev, "Unable to read bit for regulator, rc=%d\n",
  1607. rc);
  1608. return rc;
  1609. }
  1610. init_data = of_get_regulator_init_data(dev, node,
  1611. &pon_reg->rdesc);
  1612. if (!init_data) {
  1613. dev_err(dev, "regulator init data is missing\n");
  1614. return -ENOMEM;
  1615. }
  1616. if (!init_data->constraints.name) {
  1617. dev_err(dev, "regulator-name is missing\n");
  1618. return -EINVAL;
  1619. }
  1620. pon_reg->rdesc.type = REGULATOR_VOLTAGE;
  1621. pon_reg->rdesc.ops = &pon_spare_reg_ops;
  1622. pon_reg->rdesc.name = init_data->constraints.name;
  1623. reg_cfg.dev = dev;
  1624. reg_cfg.init_data = init_data;
  1625. reg_cfg.driver_data = pon_reg;
  1626. reg_cfg.of_node = node;
  1627. pon_reg->rdev = devm_regulator_register(dev, &pon_reg->rdesc,
  1628. &reg_cfg);
  1629. if (IS_ERR(pon_reg->rdev)) {
  1630. rc = PTR_ERR(pon_reg->rdev);
  1631. pon_reg->rdev = NULL;
  1632. if (rc != -EPROBE_DEFER)
  1633. dev_err(dev, "regulator register failed, rc=%d\n",
  1634. rc);
  1635. return rc;
  1636. }
  1637. }
  1638. return 0;
  1639. }
  1640. static ssize_t pon_smpl_en_show(struct device *dev, struct device_attribute
  1641. *attr, char *buf)
  1642. {
  1643. bool enabled = false;
  1644. int rc;
  1645. rc = qpnp_pon_get_trigger_config(PON_SMPL, &enabled);
  1646. if (rc < 0)
  1647. return rc;
  1648. return scnprintf(buf, QPNP_PON_BUFFER_SIZE, "%d", enabled);
  1649. }
  1650. static ssize_t pon_smpl_en_store(struct device *dev, struct device_attribute
  1651. *attr, const char *buf, size_t count)
  1652. {
  1653. bool store_val;
  1654. if (kstrtobool(buf, &store_val)) {
  1655. pr_err("Unable to set smpl_en\n");
  1656. return -EINVAL;
  1657. }
  1658. return qpnp_pon_trigger_config(PON_SMPL, store_val);
  1659. }
  1660. static DEVICE_ATTR_RW(pon_smpl_en);
  1661. static ssize_t
  1662. pon_uvlo_dload_show(struct device *dev, struct device_attribute
  1663. *attr, char *buf)
  1664. {
  1665. struct qpnp_pon *pon = sys_reset_dev;
  1666. uint reg;
  1667. int rc;
  1668. if (!pon)
  1669. return -ENODEV;
  1670. rc = qpnp_pon_read(pon, QPNP_PON_XVDD_RB_SPARE(pon), &reg);
  1671. if (rc)
  1672. return rc;
  1673. return scnprintf(buf, PAGE_SIZE, "%d",
  1674. !!(QPNP_PON_UVLO_DLOAD_EN & reg));
  1675. }
  1676. static ssize_t
  1677. pon_uvlo_dload_store(struct device *dev, struct device_attribute
  1678. *attr, const char *val, size_t count)
  1679. {
  1680. struct qpnp_pon *pon = sys_reset_dev;
  1681. uint reg;
  1682. bool store_val;
  1683. if (!pon)
  1684. return -ENODEV;
  1685. if (kstrtobool(val, &store_val)) {
  1686. pr_err("Unable to set dload_on_uvlo\n");
  1687. return -EINVAL;
  1688. }
  1689. reg = store_val ? QPNP_PON_UVLO_DLOAD_EN : 0;
  1690. return qpnp_pon_masked_write_backup(pon, QPNP_PON_XVDD_RB_SPARE(pon),
  1691. QPNP_PON_UVLO_DLOAD_EN, reg);
  1692. }
  1693. static DEVICE_ATTR_RW(pon_uvlo_dload);
  1694. static struct attribute *pon_attrs[] = {
  1695. &dev_attr_pon_uvlo_dload.attr,
  1696. &dev_attr_pon_smpl_en.attr,
  1697. NULL,
  1698. };
  1699. ATTRIBUTE_GROUPS(pon);
  1700. #if defined(CONFIG_DEBUG_FS)
  1701. static int qpnp_pon_debugfs_uvlo_get(void *data, u64 *val)
  1702. {
  1703. struct qpnp_pon *pon = data;
  1704. *val = pon->uvlo;
  1705. return 0;
  1706. }
  1707. static int qpnp_pon_debugfs_uvlo_set(void *data, u64 val)
  1708. {
  1709. struct qpnp_pon *pon = data;
  1710. if (pon->pon_trigger_reason == PON_SMPL ||
  1711. pon->pon_power_off_reason == QPNP_POFF_REASON_UVLO)
  1712. panic("UVLO occurred");
  1713. pon->uvlo = val;
  1714. return 0;
  1715. }
  1716. DEFINE_DEBUGFS_ATTRIBUTE(qpnp_pon_debugfs_uvlo_fops, qpnp_pon_debugfs_uvlo_get,
  1717. qpnp_pon_debugfs_uvlo_set, "0x%02llx\n");
  1718. static void qpnp_pon_debugfs_init(struct qpnp_pon *pon)
  1719. {
  1720. struct dentry *ent;
  1721. pon->debugfs = debugfs_create_dir(dev_name(pon->dev), NULL);
  1722. if (!pon->debugfs) {
  1723. dev_err(pon->dev, "Unable to create debugfs directory\n");
  1724. } else {
  1725. ent = debugfs_create_file_unsafe("uvlo_panic", 0644,
  1726. pon->debugfs, pon, &qpnp_pon_debugfs_uvlo_fops);
  1727. if (!ent)
  1728. dev_err(pon->dev, "Unable to create uvlo_panic debugfs file\n");
  1729. }
  1730. }
  1731. static void qpnp_pon_debugfs_remove(struct qpnp_pon *pon)
  1732. {
  1733. debugfs_remove_recursive(pon->debugfs);
  1734. }
  1735. #else
  1736. static void qpnp_pon_debugfs_init(struct qpnp_pon *pon)
  1737. {}
  1738. static void qpnp_pon_debugfs_remove(struct qpnp_pon *pon)
  1739. {}
  1740. #endif
  1741. static int qpnp_pon_read_gen2_pon_off_reason(struct qpnp_pon *pon, u16 *reason,
  1742. int *reason_index_offset)
  1743. {
  1744. unsigned int reg, reg1;
  1745. u8 buf[2];
  1746. int rc;
  1747. rc = qpnp_pon_read(pon, QPNP_PON_OFF_REASON(pon), &reg);
  1748. if (rc)
  1749. return rc;
  1750. if (reg & QPNP_GEN2_POFF_SEQ) {
  1751. rc = qpnp_pon_read(pon, QPNP_POFF_REASON1(pon), &reg1);
  1752. if (rc)
  1753. return rc;
  1754. *reason = (u8)reg1;
  1755. *reason_index_offset = 0;
  1756. } else if (reg & QPNP_GEN2_FAULT_SEQ) {
  1757. rc = regmap_bulk_read(pon->regmap, QPNP_FAULT_REASON1(pon), buf,
  1758. 2);
  1759. if (rc) {
  1760. dev_err(pon->dev, "Register read failed, addr=0x%04X, rc=%d\n",
  1761. QPNP_FAULT_REASON1(pon), rc);
  1762. return rc;
  1763. }
  1764. *reason = buf[0] | (u16)(buf[1] << 8);
  1765. *reason_index_offset = POFF_REASON_FAULT_OFFSET;
  1766. } else if (reg & QPNP_GEN2_S3_RESET_SEQ) {
  1767. rc = qpnp_pon_read(pon, QPNP_S3_RESET_REASON(pon), &reg1);
  1768. if (rc)
  1769. return rc;
  1770. *reason = (u8)reg1;
  1771. *reason_index_offset = POFF_REASON_S3_RESET_OFFSET;
  1772. }
  1773. return 0;
  1774. }
  1775. static int qpnp_pon_configure_s3_reset(struct qpnp_pon *pon)
  1776. {
  1777. struct device *dev = pon->dev;
  1778. const char *src_name;
  1779. u32 debounce;
  1780. u8 src_val;
  1781. int rc;
  1782. /* Program S3 reset debounce time */
  1783. rc = of_property_read_u32(dev->of_node, "qcom,s3-debounce", &debounce);
  1784. if (!rc) {
  1785. if (debounce > QPNP_PON_S3_TIMER_SECS_MAX) {
  1786. dev_err(dev, "S3 debounce time %u greater than max supported %u\n",
  1787. debounce, QPNP_PON_S3_TIMER_SECS_MAX);
  1788. return -EINVAL;
  1789. }
  1790. if (debounce != 0)
  1791. debounce = ilog2(debounce);
  1792. /* S3 debounce is a SEC_ACCESS register */
  1793. rc = qpnp_pon_masked_write_backup(pon, QPNP_PON_SEC_ACCESS(pon),
  1794. 0xFF, QPNP_PON_SEC_UNLOCK);
  1795. if (rc)
  1796. return rc;
  1797. rc = qpnp_pon_masked_write_backup(pon, QPNP_PON_S3_DBC_CTL(pon),
  1798. QPNP_PON_S3_DBC_DELAY_MASK, debounce);
  1799. if (rc)
  1800. return rc;
  1801. }
  1802. /* Program S3 reset source */
  1803. rc = of_property_read_string(dev->of_node, "qcom,s3-src", &src_name);
  1804. if (!rc) {
  1805. if (!strcmp(src_name, "kpdpwr")) {
  1806. src_val = QPNP_PON_S3_SRC_KPDPWR;
  1807. } else if (!strcmp(src_name, "resin")) {
  1808. src_val = QPNP_PON_S3_SRC_RESIN;
  1809. } else if (!strcmp(src_name, "kpdpwr-or-resin")) {
  1810. src_val = QPNP_PON_S3_SRC_KPDPWR_OR_RESIN;
  1811. } else if (!strcmp(src_name, "kpdpwr-and-resin")) {
  1812. src_val = QPNP_PON_S3_SRC_KPDPWR_AND_RESIN;
  1813. } else {
  1814. dev_err(dev, "Unknown S3 reset source: %s\n",
  1815. src_name);
  1816. return -EINVAL;
  1817. }
  1818. /*
  1819. * S3 source is a write once register. If the register has
  1820. * been configured by the bootloader then this operation will
  1821. * not have an effect.
  1822. */
  1823. rc = qpnp_pon_masked_write_backup(pon, QPNP_PON_S3_SRC(pon),
  1824. QPNP_PON_S3_SRC_MASK, src_val);
  1825. if (rc)
  1826. return rc;
  1827. }
  1828. return 0;
  1829. }
  1830. static int qpnp_pon_read_hardware_info(struct qpnp_pon *pon, bool sys_reset)
  1831. {
  1832. struct device *dev = pon->dev;
  1833. unsigned int reg = 0;
  1834. u8 buf[2];
  1835. int reason_index_offset = 0;
  1836. unsigned int pon_sts = 0;
  1837. bool cold_boot;
  1838. u16 poff_sts = 0;
  1839. int rc, index;
  1840. /* Read PON_PERPH_SUBTYPE register to get PON type */
  1841. rc = qpnp_pon_read(pon, QPNP_PON_PERPH_SUBTYPE(pon), &reg);
  1842. if (rc)
  1843. return rc;
  1844. pon->subtype = reg;
  1845. /* Check if it is rev B */
  1846. rc = qpnp_pon_read(pon, QPNP_PON_REVISION2(pon), &reg);
  1847. if (rc)
  1848. return rc;
  1849. pon->pon_ver = reg;
  1850. if (is_pon_gen1(pon)) {
  1851. if (pon->pon_ver == 0)
  1852. pon->pon_ver = QPNP_PON_GEN1_V1;
  1853. else
  1854. pon->pon_ver = QPNP_PON_GEN1_V2;
  1855. } else if (is_pon_gen2(pon)) {
  1856. pon->pon_ver = QPNP_PON_GEN2;
  1857. } else if (is_pon_gen3(pon)) {
  1858. pon->pon_ver = QPNP_PON_GEN3;
  1859. } else if (pon->subtype == PON_1REG) {
  1860. pon->pon_ver = QPNP_PON_GEN1_V2;
  1861. } else {
  1862. dev_err(dev, "Invalid PON_PERPH_SUBTYPE 0x%02X\n",
  1863. pon->subtype);
  1864. return -EINVAL;
  1865. }
  1866. dev_dbg(dev, "pon_subtype=0x%02X, pon_version=0x%02X\n", pon->subtype,
  1867. pon->pon_ver);
  1868. if (is_pon_gen3(pon)) {
  1869. /* PON_GEN3 does not have PON/POFF reason registers */
  1870. return 0;
  1871. }
  1872. rc = qpnp_pon_store_and_clear_warm_reset(pon);
  1873. if (rc)
  1874. return rc;
  1875. /* PON reason */
  1876. rc = qpnp_pon_read(pon, QPNP_PON_REASON1(pon), &pon_sts);
  1877. if (rc)
  1878. return rc;
  1879. index = ffs(pon_sts) - 1;
  1880. cold_boot = sys_reset_dev ? !_qpnp_pon_is_warm_reset(sys_reset_dev)
  1881. : !_qpnp_pon_is_warm_reset(pon);
  1882. if (index >= ARRAY_SIZE(qpnp_pon_reason) || index < 0) {
  1883. dev_info(dev, "PMIC@SID%d Power-on reason: Unknown and '%s' boot\n",
  1884. to_spmi_device(dev->parent)->usid,
  1885. cold_boot ? "cold" : "warm");
  1886. } else {
  1887. pon->pon_trigger_reason = index;
  1888. dev_info(dev, "PMIC@SID%d Power-on reason: %s and '%s' boot\n",
  1889. to_spmi_device(dev->parent)->usid,
  1890. qpnp_pon_reason[index],
  1891. cold_boot ? "cold" : "warm");
  1892. }
  1893. /* POFF reason */
  1894. if (!is_pon_gen1(pon) && pon->subtype != PON_1REG) {
  1895. rc = qpnp_pon_read_gen2_pon_off_reason(pon, &poff_sts,
  1896. &reason_index_offset);
  1897. if (rc)
  1898. return rc;
  1899. } else {
  1900. rc = regmap_bulk_read(pon->regmap, QPNP_POFF_REASON1(pon), buf,
  1901. 2);
  1902. if (rc) {
  1903. dev_err(dev, "Register read failed, addr=0x%04X, rc=%d\n",
  1904. QPNP_POFF_REASON1(pon), rc);
  1905. return rc;
  1906. }
  1907. poff_sts = buf[0] | (u16)(buf[1] << 8);
  1908. }
  1909. index = ffs(poff_sts) - 1 + reason_index_offset;
  1910. if (index >= ARRAY_SIZE(qpnp_poff_reason) || index < 0 ||
  1911. index < reason_index_offset) {
  1912. dev_info(dev, "PMIC@SID%d: Unknown power-off reason\n",
  1913. to_spmi_device(dev->parent)->usid);
  1914. } else {
  1915. pon->pon_power_off_reason = index;
  1916. dev_info(dev, "PMIC@SID%d: Power-off reason: %s\n",
  1917. to_spmi_device(dev->parent)->usid,
  1918. qpnp_poff_reason[index]);
  1919. }
  1920. if ((pon->pon_trigger_reason == PON_SMPL ||
  1921. pon->pon_power_off_reason == QPNP_POFF_REASON_UVLO) &&
  1922. of_property_read_bool(dev->of_node, "qcom,uvlo-panic")) {
  1923. panic("UVLO occurred");
  1924. }
  1925. return 0;
  1926. }
  1927. static int qpnp_pon_parse_power_off_type(struct qpnp_pon *pon,
  1928. const char *prop, int *val)
  1929. {
  1930. u32 type = 0;
  1931. int rc;
  1932. rc = of_property_read_u32(pon->dev->of_node, prop, &type);
  1933. if (rc) {
  1934. if (rc != -EINVAL) {
  1935. dev_err(pon->dev, "Unable to read property %s, rc=%d\n",
  1936. prop, rc);
  1937. return rc;
  1938. }
  1939. *val = -EINVAL;
  1940. return 0;
  1941. }
  1942. if (type >= PON_POWER_OFF_MAX_TYPE) {
  1943. dev_err(pon->dev, "Invalid property value %s=%u\n", prop, type);
  1944. return -EINVAL;
  1945. }
  1946. *val = type;
  1947. return 0;
  1948. }
  1949. static int qpnp_pon_parse_dt_power_off_config(struct qpnp_pon *pon)
  1950. {
  1951. struct device_node *node = pon->dev->of_node;
  1952. int rc;
  1953. rc = qpnp_pon_parse_power_off_type(pon, "qcom,warm-reset-poweroff-type",
  1954. &pon->warm_reset_poff_type);
  1955. if (rc)
  1956. return rc;
  1957. rc = qpnp_pon_parse_power_off_type(pon, "qcom,hard-reset-poweroff-type",
  1958. &pon->hard_reset_poff_type);
  1959. if (rc)
  1960. return rc;
  1961. rc = qpnp_pon_parse_power_off_type(pon, "qcom,shutdown-poweroff-type",
  1962. &pon->shutdown_poff_type);
  1963. if (rc)
  1964. return rc;
  1965. rc = qpnp_pon_parse_power_off_type(pon, "qcom,resin-warm-reset-type",
  1966. &pon->resin_warm_reset_type);
  1967. if (rc)
  1968. return rc;
  1969. rc = qpnp_pon_parse_power_off_type(pon, "qcom,resin-hard-reset-type",
  1970. &pon->resin_hard_reset_type);
  1971. if (rc)
  1972. return rc;
  1973. rc = qpnp_pon_parse_power_off_type(pon, "qcom,resin-shutdown-type",
  1974. &pon->resin_shutdown_type);
  1975. if (rc)
  1976. return rc;
  1977. pon->ps_hold_hard_reset_disable = of_property_read_bool(node,
  1978. "qcom,ps-hold-hard-reset-disable");
  1979. pon->ps_hold_shutdown_disable = of_property_read_bool(node,
  1980. "qcom,ps-hold-shutdown-disable");
  1981. pon->resin_hard_reset_disable = of_property_read_bool(node,
  1982. "qcom,resin-hard-reset-disable");
  1983. pon->resin_shutdown_disable = of_property_read_bool(node,
  1984. "qcom,resin-shutdown-disable");
  1985. pon->resin_pon_reset = of_property_read_bool(node,
  1986. "qcom,resin-pon-reset");
  1987. return 0;
  1988. }
  1989. static int qpnp_pon_probe(struct platform_device *pdev)
  1990. {
  1991. struct device *dev = &pdev->dev;
  1992. struct device_node *node;
  1993. struct qpnp_pon *pon;
  1994. unsigned long flags;
  1995. u32 delay;
  1996. const __be32 *addr;
  1997. bool sys_reset, modem_reset;
  1998. int rc;
  1999. pon = devm_kzalloc(dev, sizeof(*pon), GFP_KERNEL);
  2000. if (!pon)
  2001. return -ENOMEM;
  2002. pon->dev = dev;
  2003. pon->regmap = dev_get_regmap(dev->parent, NULL);
  2004. if (!pon->regmap) {
  2005. dev_err(dev, "Parent regmap missing\n");
  2006. return -ENODEV;
  2007. }
  2008. addr = of_get_address(dev->of_node, 0, NULL, NULL);
  2009. if (!addr) {
  2010. dev_err(dev, "reg property missing\n");
  2011. return -EINVAL;
  2012. }
  2013. pon->base = be32_to_cpu(*addr);
  2014. addr = of_get_address(dev->of_node, 1, NULL, NULL);
  2015. if (addr)
  2016. pon->pbs_base = be32_to_cpu(*addr);
  2017. sys_reset = of_property_read_bool(dev->of_node, "qcom,system-reset");
  2018. if (sys_reset && sys_reset_dev) {
  2019. dev_err(dev, "qcom,system-reset property must only be specified for one PMIC PON device in the system\n");
  2020. return -EINVAL;
  2021. }
  2022. modem_reset = of_property_read_bool(dev->of_node, "qcom,modem-reset");
  2023. if (modem_reset && modem_reset_dev) {
  2024. dev_err(dev, "qcom,modem-reset property must only be specified for one PMIC PON device in the system\n");
  2025. return -EINVAL;
  2026. } else if (modem_reset && sys_reset) {
  2027. dev_err(dev, "qcom,modem-reset and qcom,system-reset properties cannot be supported together for one PMIC PON device\n");
  2028. return -EINVAL;
  2029. }
  2030. INIT_LIST_HEAD(&pon->restore_regs);
  2031. mutex_init(&pon->restore_lock);
  2032. /* Get the total number of pon configurations and regulators */
  2033. for_each_available_child_of_node(dev->of_node, node) {
  2034. if (of_find_property(node, "regulator-name", NULL)) {
  2035. pon->num_pon_reg++;
  2036. } else if (of_find_property(node, "qcom,pon-type", NULL)) {
  2037. pon->num_pon_config++;
  2038. } else {
  2039. dev_err(dev, "Unknown sub-node found %s\n", node->name);
  2040. return -EINVAL;
  2041. }
  2042. }
  2043. dev_dbg(dev, "PON@SID%d: num_pon_config=%d, num_pon_reg=%d\n",
  2044. to_spmi_device(dev->parent)->usid, pon->num_pon_config,
  2045. pon->num_pon_reg);
  2046. rc = qpnp_pon_read_hardware_info(pon, sys_reset);
  2047. if (rc)
  2048. return rc;
  2049. rc = pon_regulator_init(pon);
  2050. if (rc)
  2051. return rc;
  2052. rc = qpnp_pon_configure_s3_reset(pon);
  2053. if (rc)
  2054. return rc;
  2055. dev_set_drvdata(dev, pon);
  2056. INIT_DELAYED_WORK(&pon->bark_work, bark_work_func);
  2057. rc = qpnp_pon_parse_dt_power_off_config(pon);
  2058. if (rc)
  2059. return rc;
  2060. rc = of_property_read_u32(dev->of_node, "qcom,pon-dbc-delay", &delay);
  2061. if (!rc) {
  2062. rc = qpnp_pon_set_dbc(pon, delay);
  2063. if (rc)
  2064. return rc;
  2065. } else if (rc != -EINVAL) {
  2066. dev_err(dev, "Unable to read debounce delay, rc=%d\n", rc);
  2067. return rc;
  2068. }
  2069. rc = qpnp_pon_get_dbc(pon, &pon->dbc_time_us);
  2070. if (rc)
  2071. return rc;
  2072. pon->kpdpwr_dbc_enable = of_property_read_bool(dev->of_node,
  2073. "qcom,kpdpwr-sw-debounce");
  2074. pon->store_hard_reset_reason = of_property_read_bool(dev->of_node,
  2075. "qcom,store-hard-reset-reason");
  2076. pon->legacy_hard_reset_offset = of_property_read_bool(pdev->dev.of_node,
  2077. "qcom,use-legacy-hard-reset-offset");
  2078. if (of_property_read_bool(dev->of_node, "qcom,secondary-pon-reset")) {
  2079. if (sys_reset) {
  2080. dev_err(dev, "qcom,system-reset property shouldn't be used along with qcom,secondary-pon-reset property\n");
  2081. return -EINVAL;
  2082. } else if (modem_reset) {
  2083. dev_err(dev, "qcom,modem-reset property shouldn't be used along with qcom,secondary-pon-reset property\n");
  2084. return -EINVAL;
  2085. }
  2086. spin_lock_irqsave(&spon_list_slock, flags);
  2087. list_add(&pon->list, &spon_dev_list);
  2088. spin_unlock_irqrestore(&spon_list_slock, flags);
  2089. pon->is_spon = true;
  2090. }
  2091. /* Register the PON configurations */
  2092. rc = qpnp_pon_config_init(pon, pdev);
  2093. if (rc)
  2094. return rc;
  2095. rc = device_create_file(dev, &dev_attr_debounce_us);
  2096. if (rc) {
  2097. dev_err(dev, "sysfs debounce file creation failed, rc=%d\n",
  2098. rc);
  2099. return rc;
  2100. }
  2101. if (sys_reset)
  2102. sys_reset_dev = pon;
  2103. if (modem_reset)
  2104. modem_reset_dev = pon;
  2105. qpnp_pon_debugfs_init(pon);
  2106. rc = sysfs_create_groups(&pon->dev->kobj, pon_groups);
  2107. if (rc < 0) {
  2108. pr_err("Failed to create sysfs files rc=%d\n", rc);
  2109. return rc;
  2110. }
  2111. return 0;
  2112. }
  2113. static int qpnp_pon_remove(struct platform_device *pdev)
  2114. {
  2115. struct qpnp_pon *pon = platform_get_drvdata(pdev);
  2116. unsigned long flags;
  2117. device_remove_file(&pdev->dev, &dev_attr_debounce_us);
  2118. cancel_delayed_work_sync(&pon->bark_work);
  2119. qpnp_pon_debugfs_remove(pon);
  2120. if (pon->is_spon) {
  2121. spin_lock_irqsave(&spon_list_slock, flags);
  2122. list_del(&pon->list);
  2123. spin_unlock_irqrestore(&spon_list_slock, flags);
  2124. }
  2125. mutex_destroy(&pon->restore_lock);
  2126. sysfs_remove_groups(&pon->dev->kobj, pon_groups);
  2127. return 0;
  2128. }
  2129. #ifdef CONFIG_PM
  2130. static int qpnp_pon_restore(struct device *dev)
  2131. {
  2132. int i, rc = 0;
  2133. struct qpnp_pon_config *cfg;
  2134. struct qpnp_pon *pon = dev_get_drvdata(dev);
  2135. struct pon_reg *pos = NULL;
  2136. list_for_each_entry(pos, &pon->restore_regs, list) {
  2137. rc = regmap_write(pon->regmap, pos->addr, pos->val);
  2138. if (rc < 0) {
  2139. dev_err(dev, "Failed to restore reg addr=0x%04X rc=%d\n",
  2140. pos->addr, rc);
  2141. return rc;
  2142. }
  2143. }
  2144. for (i = 0; i < pon->num_pon_config; i++) {
  2145. cfg = &pon->pon_cfg[i];
  2146. rc = qpnp_pon_request_irqs(pon, cfg);
  2147. if (rc < 0)
  2148. return rc;
  2149. }
  2150. return rc;
  2151. }
  2152. static int qpnp_pon_freeze(struct device *dev)
  2153. {
  2154. int i, rc = 0;
  2155. struct qpnp_pon_config *cfg;
  2156. struct qpnp_pon *pon = dev_get_drvdata(dev);
  2157. for (i = 0; i < pon->num_pon_config; i++) {
  2158. cfg = &pon->pon_cfg[i];
  2159. rc = qpnp_pon_free_irqs(pon, cfg);
  2160. if (rc < 0)
  2161. return rc;
  2162. }
  2163. return rc;
  2164. }
  2165. static int qpnp_pon_suspend(struct device *dev)
  2166. {
  2167. #ifdef CONFIG_DEEPSLEEP
  2168. if (pm_suspend_via_firmware() == PM_SUSPEND_MEM)
  2169. return qpnp_pon_freeze(dev);
  2170. #endif
  2171. return 0;
  2172. }
  2173. static int qpnp_pon_resume(struct device *dev)
  2174. {
  2175. #ifdef CONFIG_DEEPSLEEP
  2176. if (pm_suspend_via_firmware() == PM_SUSPEND_MEM)
  2177. return qpnp_pon_restore(dev);
  2178. #endif
  2179. return 0;
  2180. }
  2181. static const struct dev_pm_ops qpnp_pon_pm_ops = {
  2182. .freeze = qpnp_pon_freeze,
  2183. .restore = qpnp_pon_restore,
  2184. .suspend = qpnp_pon_suspend,
  2185. .resume = qpnp_pon_resume,
  2186. };
  2187. #endif
  2188. static const struct of_device_id qpnp_pon_match_table[] = {
  2189. { .compatible = "qcom,qpnp-power-on" },
  2190. {}
  2191. };
  2192. static struct platform_driver qpnp_pon_driver = {
  2193. .driver = {
  2194. .name = "qcom,qpnp-power-on",
  2195. .of_match_table = qpnp_pon_match_table,
  2196. #ifdef CONFIG_PM
  2197. .pm = &qpnp_pon_pm_ops,
  2198. #endif
  2199. },
  2200. .probe = qpnp_pon_probe,
  2201. .remove = qpnp_pon_remove,
  2202. };
  2203. static int __init qpnp_pon_init(void)
  2204. {
  2205. return platform_driver_register(&qpnp_pon_driver);
  2206. }
  2207. subsys_initcall(qpnp_pon_init);
  2208. static void __exit qpnp_pon_exit(void)
  2209. {
  2210. return platform_driver_unregister(&qpnp_pon_driver);
  2211. }
  2212. module_exit(qpnp_pon_exit);
  2213. MODULE_DESCRIPTION("QPNP PMIC Power-on driver");
  2214. MODULE_LICENSE("GPL");