qpnp-regulator.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437
  1. /*
  2. * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/string.h>
  18. #include <linux/kernel.h>
  19. #include <linux/regmap.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/bitops.h>
  23. #include <linux/slab.h>
  24. #include <linux/spmi.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/ktime.h>
  30. #include <linux/regulator/driver.h>
  31. #include <linux/regulator/of_regulator.h>
  32. #include <linux/regulator/qpnp-regulator.h>
  33. /* Debug Flag Definitions */
  34. enum {
  35. QPNP_VREG_DEBUG_REQUEST = BIT(0), /* Show requests */
  36. QPNP_VREG_DEBUG_DUPLICATE = BIT(1), /* Show duplicate requests */
  37. QPNP_VREG_DEBUG_INIT = BIT(2), /* Show state after probe */
  38. QPNP_VREG_DEBUG_WRITES = BIT(3), /* Show SPMI writes */
  39. QPNP_VREG_DEBUG_READS = BIT(4), /* Show SPMI reads */
  40. QPNP_VREG_DEBUG_OCP = BIT(5), /* Show VS OCP IRQ events */
  41. };
  42. static int qpnp_vreg_debug_mask;
  43. module_param_named(
  44. debug_mask, qpnp_vreg_debug_mask, int, 0600
  45. );
  46. #define vreg_err(vreg, fmt, ...) \
  47. pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
  48. /* These types correspond to unique register layouts. */
  49. enum qpnp_regulator_logical_type {
  50. QPNP_REGULATOR_LOGICAL_TYPE_SMPS,
  51. QPNP_REGULATOR_LOGICAL_TYPE_LDO,
  52. QPNP_REGULATOR_LOGICAL_TYPE_VS,
  53. QPNP_REGULATOR_LOGICAL_TYPE_BOOST,
  54. QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS,
  55. QPNP_REGULATOR_LOGICAL_TYPE_BOOST_BYP,
  56. QPNP_REGULATOR_LOGICAL_TYPE_LN_LDO,
  57. QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS,
  58. QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS,
  59. QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO,
  60. QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2,
  61. };
  62. enum qpnp_regulator_type {
  63. QPNP_REGULATOR_TYPE_BUCK = 0x03,
  64. QPNP_REGULATOR_TYPE_LDO = 0x04,
  65. QPNP_REGULATOR_TYPE_VS = 0x05,
  66. QPNP_REGULATOR_TYPE_BOOST = 0x1B,
  67. QPNP_REGULATOR_TYPE_FTS = 0x1C,
  68. QPNP_REGULATOR_TYPE_BOOST_BYP = 0x1F,
  69. QPNP_REGULATOR_TYPE_ULT_LDO = 0x21,
  70. QPNP_REGULATOR_TYPE_ULT_BUCK = 0x22,
  71. };
  72. enum qpnp_regulator_subtype {
  73. QPNP_REGULATOR_SUBTYPE_GP_CTL = 0x08,
  74. QPNP_REGULATOR_SUBTYPE_RF_CTL = 0x09,
  75. QPNP_REGULATOR_SUBTYPE_N50 = 0x01,
  76. QPNP_REGULATOR_SUBTYPE_N150 = 0x02,
  77. QPNP_REGULATOR_SUBTYPE_N300 = 0x03,
  78. QPNP_REGULATOR_SUBTYPE_N600 = 0x04,
  79. QPNP_REGULATOR_SUBTYPE_N1200 = 0x05,
  80. QPNP_REGULATOR_SUBTYPE_N600_ST = 0x06,
  81. QPNP_REGULATOR_SUBTYPE_N1200_ST = 0x07,
  82. QPNP_REGULATOR_SUBTYPE_N300_ST = 0x15,
  83. QPNP_REGULATOR_SUBTYPE_P50 = 0x08,
  84. QPNP_REGULATOR_SUBTYPE_P150 = 0x09,
  85. QPNP_REGULATOR_SUBTYPE_P300 = 0x0A,
  86. QPNP_REGULATOR_SUBTYPE_P600 = 0x0B,
  87. QPNP_REGULATOR_SUBTYPE_P1200 = 0x0C,
  88. QPNP_REGULATOR_SUBTYPE_LN = 0x10,
  89. QPNP_REGULATOR_SUBTYPE_LV_P50 = 0x28,
  90. QPNP_REGULATOR_SUBTYPE_LV_P150 = 0x29,
  91. QPNP_REGULATOR_SUBTYPE_LV_P300 = 0x2A,
  92. QPNP_REGULATOR_SUBTYPE_LV_P600 = 0x2B,
  93. QPNP_REGULATOR_SUBTYPE_LV_P1200 = 0x2C,
  94. QPNP_REGULATOR_SUBTYPE_LV100 = 0x01,
  95. QPNP_REGULATOR_SUBTYPE_LV300 = 0x02,
  96. QPNP_REGULATOR_SUBTYPE_MV300 = 0x08,
  97. QPNP_REGULATOR_SUBTYPE_MV500 = 0x09,
  98. QPNP_REGULATOR_SUBTYPE_HDMI = 0x10,
  99. QPNP_REGULATOR_SUBTYPE_OTG = 0x11,
  100. QPNP_REGULATOR_SUBTYPE_5V_BOOST = 0x01,
  101. QPNP_REGULATOR_SUBTYPE_FTS_CTL = 0x08,
  102. QPNP_REGULATOR_SUBTYPE_FTS2p5_CTL = 0x09,
  103. QPNP_REGULATOR_SUBTYPE_FTS426 = 0x0A,
  104. QPNP_REGULATOR_SUBTYPE_BB_2A = 0x01,
  105. QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL1 = 0x0D,
  106. QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL2 = 0x0E,
  107. QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL3 = 0x0F,
  108. QPNP_REGULATOR_SUBTYPE_ULT_HF_CTL4 = 0x10,
  109. };
  110. /* First common register layout used by older devices */
  111. enum qpnp_common_regulator_registers {
  112. QPNP_COMMON_REG_DIG_MAJOR_REV = 0x01,
  113. QPNP_COMMON_REG_TYPE = 0x04,
  114. QPNP_COMMON_REG_SUBTYPE = 0x05,
  115. QPNP_COMMON_REG_VOLTAGE_RANGE = 0x40,
  116. QPNP_COMMON_REG_VOLTAGE_SET = 0x41,
  117. QPNP_COMMON_REG_MODE = 0x45,
  118. QPNP_COMMON_REG_ENABLE = 0x46,
  119. QPNP_COMMON_REG_PULL_DOWN = 0x48,
  120. QPNP_COMMON_REG_STEP_CTRL = 0x61,
  121. };
  122. /*
  123. * Second common register layout used by newer devices
  124. * Note that some of the registers from the first common layout remain
  125. * unchanged and their definition is not duplicated.
  126. */
  127. enum qpnp_common2_regulator_registers {
  128. QPNP_COMMON2_REG_VOLTAGE_LSB = 0x40,
  129. QPNP_COMMON2_REG_VOLTAGE_MSB = 0x41,
  130. QPNP_COMMON2_REG_MODE = 0x45,
  131. QPNP_COMMON2_REG_STEP_CTRL = 0x61,
  132. };
  133. enum qpnp_ldo_registers {
  134. QPNP_LDO_REG_SOFT_START = 0x4C,
  135. };
  136. enum qpnp_vs_registers {
  137. QPNP_VS_REG_OCP = 0x4A,
  138. QPNP_VS_REG_SOFT_START = 0x4C,
  139. };
  140. enum qpnp_boost_registers {
  141. QPNP_BOOST_REG_CURRENT_LIMIT = 0x4A,
  142. };
  143. enum qpnp_boost_byp_registers {
  144. QPNP_BOOST_BYP_REG_CURRENT_LIMIT = 0x4B,
  145. };
  146. /* Used for indexing into ctrl_reg. These are offets from 0x40 */
  147. enum qpnp_common_control_register_index {
  148. QPNP_COMMON_IDX_VOLTAGE_RANGE = 0,
  149. QPNP_COMMON_IDX_VOLTAGE_SET = 1,
  150. QPNP_COMMON_IDX_MODE = 5,
  151. QPNP_COMMON_IDX_ENABLE = 6,
  152. };
  153. enum qpnp_common2_control_register_index {
  154. QPNP_COMMON2_IDX_VOLTAGE_LSB = 0,
  155. QPNP_COMMON2_IDX_VOLTAGE_MSB = 1,
  156. QPNP_COMMON2_IDX_MODE = 5,
  157. };
  158. /* Common regulator control register layout */
  159. #define QPNP_COMMON_ENABLE_MASK 0x80
  160. #define QPNP_COMMON_ENABLE 0x80
  161. #define QPNP_COMMON_DISABLE 0x00
  162. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
  163. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
  164. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
  165. #define QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
  166. #define QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0F
  167. /* First common regulator mode register layout */
  168. #define QPNP_COMMON_MODE_HPM_MASK 0x80
  169. #define QPNP_COMMON_MODE_AUTO_MASK 0x40
  170. #define QPNP_COMMON_MODE_BYPASS_MASK 0x20
  171. #define QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
  172. #define QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
  173. #define QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
  174. #define QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
  175. #define QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
  176. #define QPNP_COMMON_MODE_FOLLOW_ALL_MASK 0x1F
  177. /* Second common regulator mode register values */
  178. #define QPNP_COMMON2_MODE_BYPASS 3
  179. #define QPNP_COMMON2_MODE_RETENTION 4
  180. #define QPNP_COMMON2_MODE_LPM 5
  181. #define QPNP_COMMON2_MODE_AUTO 6
  182. #define QPNP_COMMON2_MODE_HPM 7
  183. #define QPNP_COMMON2_MODE_MASK 0x07
  184. /* Common regulator pull down control register layout */
  185. #define QPNP_COMMON_PULL_DOWN_ENABLE_MASK 0x80
  186. /* LDO regulator current limit control register layout */
  187. #define QPNP_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
  188. /* LDO regulator soft start control register layout */
  189. #define QPNP_LDO_SOFT_START_ENABLE_MASK 0x80
  190. /* VS regulator over current protection control register layout */
  191. #define QPNP_VS_OCP_OVERRIDE 0x01
  192. #define QPNP_VS_OCP_NO_OVERRIDE 0x00
  193. /* VS regulator soft start control register layout */
  194. #define QPNP_VS_SOFT_START_ENABLE_MASK 0x80
  195. #define QPNP_VS_SOFT_START_SEL_MASK 0x03
  196. /* Boost regulator current limit control register layout */
  197. #define QPNP_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
  198. #define QPNP_BOOST_CURRENT_LIMIT_MASK 0x07
  199. #define QPNP_VS_OCP_DEFAULT_MAX_RETRIES 10
  200. #define QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
  201. #define QPNP_VS_OCP_FALL_DELAY_US 90
  202. #define QPNP_VS_OCP_FAULT_DELAY_US 20000
  203. #define QPNP_FTSMPS_STEP_CTRL_STEP_MASK 0x18
  204. #define QPNP_FTSMPS_STEP_CTRL_STEP_SHIFT 3
  205. #define QPNP_FTSMPS_STEP_CTRL_DELAY_MASK 0x07
  206. #define QPNP_FTSMPS_STEP_CTRL_DELAY_SHIFT 0
  207. /* Clock rate in kHz of the FTSMPS regulator reference clock. */
  208. #define QPNP_FTSMPS_CLOCK_RATE 19200
  209. /* Minimum voltage stepper delay for each step. */
  210. #define QPNP_FTSMPS_STEP_DELAY 8
  211. /*
  212. * The ratio QPNP_FTSMPS_STEP_MARGIN_NUM/QPNP_FTSMPS_STEP_MARGIN_DEN is used to
  213. * adjust the step rate in order to account for oscillator variance.
  214. */
  215. #define QPNP_FTSMPS_STEP_MARGIN_NUM 4
  216. #define QPNP_FTSMPS_STEP_MARGIN_DEN 5
  217. #define QPNP_FTSMPS2_STEP_CTRL_DELAY_MASK 0x03
  218. #define QPNP_FTSMPS2_STEP_CTRL_DELAY_SHIFT 0
  219. /* Clock rate in kHz of the FTSMPS2 regulator reference clock. */
  220. #define QPNP_FTSMPS2_CLOCK_RATE 4800
  221. /* Minimum voltage stepper delay for each step. */
  222. #define QPNP_FTSMPS2_STEP_DELAY 2
  223. /*
  224. * The ratio QPNP_FTSMPS2_STEP_MARGIN_NUM/QPNP_FTSMPS2_STEP_MARGIN_DEN is used
  225. * to adjust the step rate in order to account for oscillator variance.
  226. */
  227. #define QPNP_FTSMPS2_STEP_MARGIN_NUM 10
  228. #define QPNP_FTSMPS2_STEP_MARGIN_DEN 11
  229. /*
  230. * This voltage in uV is returned by get_voltage functions when there is no way
  231. * to determine the current voltage level. It is needed because the regulator
  232. * framework treats a 0 uV voltage as an error.
  233. */
  234. #define VOLTAGE_UNKNOWN 1
  235. /* VSET value to decide the range of ULT SMPS */
  236. #define ULT_SMPS_RANGE_SPLIT 0x60
  237. /**
  238. * struct qpnp_voltage_range - regulator set point voltage mapping description
  239. * @min_uV: Minimum programmable output voltage resulting from
  240. * set point register value 0x00
  241. * @max_uV: Maximum programmable output voltage
  242. * @step_uV: Output voltage increase resulting from the set point
  243. * register value increasing by 1
  244. * @set_point_min_uV: Minimum allowed voltage
  245. * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order
  246. * to pick which range should be used in the case of
  247. * overlapping set points.
  248. * @n_voltages: Number of preferred voltage set points present in this
  249. * range
  250. * @range_sel: Voltage range register value corresponding to this range
  251. *
  252. * The following relationships must be true for the values used in this struct:
  253. * (max_uV - min_uV) % step_uV == 0
  254. * (set_point_min_uV - min_uV) % step_uV == 0*
  255. * (set_point_max_uV - min_uV) % step_uV == 0*
  256. * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
  257. *
  258. * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
  259. * specify that the voltage range has meaning, but is not preferred.
  260. */
  261. struct qpnp_voltage_range {
  262. int min_uV;
  263. int max_uV;
  264. int step_uV;
  265. int set_point_min_uV;
  266. int set_point_max_uV;
  267. unsigned int n_voltages;
  268. u8 range_sel;
  269. };
  270. /*
  271. * The ranges specified in the qpnp_voltage_set_points struct must be listed
  272. * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
  273. */
  274. struct qpnp_voltage_set_points {
  275. struct qpnp_voltage_range *range;
  276. int count;
  277. unsigned int n_voltages;
  278. };
  279. struct qpnp_regulator_mapping {
  280. enum qpnp_regulator_type type;
  281. enum qpnp_regulator_subtype subtype;
  282. enum qpnp_regulator_logical_type logical_type;
  283. u32 revision_min;
  284. u32 revision_max;
  285. struct regulator_ops *ops;
  286. struct qpnp_voltage_set_points *set_points;
  287. int hpm_min_load;
  288. };
  289. struct qpnp_regulator {
  290. struct regulator_desc rdesc;
  291. struct delayed_work ocp_work;
  292. struct platform_device *pdev;
  293. struct regmap *regmap;
  294. struct regulator_dev *rdev;
  295. struct qpnp_voltage_set_points *set_points;
  296. enum qpnp_regulator_logical_type logical_type;
  297. int enable_time;
  298. int ocp_enable;
  299. int ocp_irq;
  300. int ocp_count;
  301. int ocp_max_retries;
  302. int ocp_retry_delay_ms;
  303. int system_load;
  304. int hpm_min_load;
  305. int slew_rate;
  306. u32 write_count;
  307. u32 prev_write_count;
  308. ktime_t vs_enable_time;
  309. u16 base_addr;
  310. /* ctrl_reg provides a shadow copy of register values 0x40 to 0x47. */
  311. u8 ctrl_reg[8];
  312. u8 init_mode;
  313. };
  314. #define QPNP_VREG_MAP(_type, _subtype, _dig_major_min, _dig_major_max, \
  315. _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
  316. { \
  317. .type = QPNP_REGULATOR_TYPE_##_type, \
  318. .subtype = QPNP_REGULATOR_SUBTYPE_##_subtype, \
  319. .revision_min = _dig_major_min, \
  320. .revision_max = _dig_major_max, \
  321. .logical_type = QPNP_REGULATOR_LOGICAL_TYPE_##_logical_type, \
  322. .ops = &qpnp_##_ops_val##_ops, \
  323. .set_points = &_set_points_val##_set_points, \
  324. .hpm_min_load = _hpm_min_load, \
  325. }
  326. #define VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
  327. _set_point_max_uV, _max_uV, _step_uV) \
  328. { \
  329. .min_uV = _min_uV, \
  330. .max_uV = _max_uV, \
  331. .set_point_min_uV = _set_point_min_uV, \
  332. .set_point_max_uV = _set_point_max_uV, \
  333. .step_uV = _step_uV, \
  334. .range_sel = _range_sel, \
  335. }
  336. #define SET_POINTS(_ranges) \
  337. { \
  338. .range = _ranges, \
  339. .count = ARRAY_SIZE(_ranges), \
  340. }
  341. /*
  342. * These tables contain the physically available PMIC regulator voltage setpoint
  343. * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
  344. * to ensure that the setpoints available to software are monotonically
  345. * increasing and unique. The set_voltage callback functions expect these
  346. * properties to hold.
  347. */
  348. static struct qpnp_voltage_range pldo_ranges[] = {
  349. VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
  350. VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
  351. VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
  352. };
  353. static struct qpnp_voltage_range nldo1_ranges[] = {
  354. VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
  355. };
  356. static struct qpnp_voltage_range nldo2_ranges[] = {
  357. VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500),
  358. VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250),
  359. VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500),
  360. };
  361. static struct qpnp_voltage_range nldo3_ranges[] = {
  362. VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
  363. VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500),
  364. VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500),
  365. };
  366. static struct qpnp_voltage_range ln_ldo_ranges[] = {
  367. VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000),
  368. VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
  369. };
  370. static struct qpnp_voltage_range smps_ranges[] = {
  371. VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
  372. VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
  373. };
  374. static struct qpnp_voltage_range ftsmps_ranges[] = {
  375. VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000),
  376. VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000),
  377. };
  378. static struct qpnp_voltage_range ftsmps2p5_ranges[] = {
  379. VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000),
  380. VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000),
  381. };
  382. static struct qpnp_voltage_range boost_ranges[] = {
  383. VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
  384. };
  385. static struct qpnp_voltage_range boost_byp_ranges[] = {
  386. VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
  387. };
  388. static struct qpnp_voltage_range ult_lo_smps_ranges[] = {
  389. VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
  390. VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000),
  391. };
  392. static struct qpnp_voltage_range ult_ho_smps_ranges[] = {
  393. VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
  394. };
  395. static struct qpnp_voltage_range ult_nldo_ranges[] = {
  396. VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
  397. };
  398. static struct qpnp_voltage_range ult_pldo_ranges[] = {
  399. VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
  400. };
  401. static struct qpnp_voltage_range ftsmps426_ranges[] = {
  402. VOLTAGE_RANGE(0, 0, 320000, 1352000, 1352000, 4000),
  403. };
  404. static struct qpnp_voltage_set_points pldo_set_points = SET_POINTS(pldo_ranges);
  405. static struct qpnp_voltage_set_points nldo1_set_points
  406. = SET_POINTS(nldo1_ranges);
  407. static struct qpnp_voltage_set_points nldo2_set_points
  408. = SET_POINTS(nldo2_ranges);
  409. static struct qpnp_voltage_set_points nldo3_set_points
  410. = SET_POINTS(nldo3_ranges);
  411. static struct qpnp_voltage_set_points ln_ldo_set_points
  412. = SET_POINTS(ln_ldo_ranges);
  413. static struct qpnp_voltage_set_points smps_set_points = SET_POINTS(smps_ranges);
  414. static struct qpnp_voltage_set_points ftsmps_set_points
  415. = SET_POINTS(ftsmps_ranges);
  416. static struct qpnp_voltage_set_points ftsmps2p5_set_points
  417. = SET_POINTS(ftsmps2p5_ranges);
  418. static struct qpnp_voltage_set_points boost_set_points
  419. = SET_POINTS(boost_ranges);
  420. static struct qpnp_voltage_set_points boost_byp_set_points
  421. = SET_POINTS(boost_byp_ranges);
  422. static struct qpnp_voltage_set_points ult_lo_smps_set_points
  423. = SET_POINTS(ult_lo_smps_ranges);
  424. static struct qpnp_voltage_set_points ult_ho_smps_set_points
  425. = SET_POINTS(ult_ho_smps_ranges);
  426. static struct qpnp_voltage_set_points ult_nldo_set_points
  427. = SET_POINTS(ult_nldo_ranges);
  428. static struct qpnp_voltage_set_points ult_pldo_set_points
  429. = SET_POINTS(ult_pldo_ranges);
  430. static struct qpnp_voltage_set_points ftsmps426_set_points
  431. = SET_POINTS(ftsmps426_ranges);
  432. static struct qpnp_voltage_set_points none_set_points;
  433. static struct qpnp_voltage_set_points *all_set_points[] = {
  434. &pldo_set_points,
  435. &nldo1_set_points,
  436. &nldo2_set_points,
  437. &nldo3_set_points,
  438. &ln_ldo_set_points,
  439. &smps_set_points,
  440. &ftsmps_set_points,
  441. &ftsmps2p5_set_points,
  442. &boost_set_points,
  443. &boost_byp_set_points,
  444. &ult_lo_smps_set_points,
  445. &ult_ho_smps_set_points,
  446. &ult_nldo_set_points,
  447. &ult_pldo_set_points,
  448. &ftsmps426_set_points,
  449. };
  450. /* Determines which label to add to a debug print statement. */
  451. enum qpnp_regulator_action {
  452. QPNP_REGULATOR_ACTION_INIT,
  453. QPNP_REGULATOR_ACTION_ENABLE,
  454. QPNP_REGULATOR_ACTION_DISABLE,
  455. QPNP_REGULATOR_ACTION_VOLTAGE,
  456. QPNP_REGULATOR_ACTION_MODE,
  457. };
  458. static void qpnp_vreg_show_state(struct regulator_dev *rdev,
  459. enum qpnp_regulator_action action);
  460. #define DEBUG_PRINT_BUFFER_SIZE 64
  461. static void fill_string(char *str, size_t str_len, u8 *buf, int buf_len)
  462. {
  463. int pos = 0;
  464. int i;
  465. for (i = 0; i < buf_len; i++) {
  466. pos += scnprintf(str + pos, str_len - pos, "0x%02X", buf[i]);
  467. if (i < buf_len - 1)
  468. pos += scnprintf(str + pos, str_len - pos, ", ");
  469. }
  470. }
  471. static inline int qpnp_vreg_read(struct qpnp_regulator *vreg, u16 addr, u8 *buf,
  472. int len)
  473. {
  474. char str[DEBUG_PRINT_BUFFER_SIZE];
  475. int rc = 0;
  476. rc = regmap_bulk_read(vreg->regmap, vreg->base_addr + addr, buf, len);
  477. if (!rc && (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_READS)) {
  478. str[0] = '\0';
  479. fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
  480. pr_info(" %-11s: read(0x%04X), sid=%d, len=%d; %s\n",
  481. vreg->rdesc.name, vreg->base_addr + addr,
  482. to_spmi_device(vreg->pdev->dev.parent)->usid, len,
  483. str);
  484. }
  485. return rc;
  486. }
  487. static inline int qpnp_vreg_write(struct qpnp_regulator *vreg, u16 addr,
  488. u8 *buf, int len)
  489. {
  490. char str[DEBUG_PRINT_BUFFER_SIZE];
  491. int rc = 0;
  492. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_WRITES) {
  493. str[0] = '\0';
  494. fill_string(str, DEBUG_PRINT_BUFFER_SIZE, buf, len);
  495. pr_info("%-11s: write(0x%04X), sid=%d, len=%d; %s\n",
  496. vreg->rdesc.name, vreg->base_addr + addr,
  497. to_spmi_device(vreg->pdev->dev.parent)->usid, len,
  498. str);
  499. }
  500. rc = regmap_bulk_write(vreg->regmap, vreg->base_addr + addr, buf, len);
  501. if (!rc)
  502. vreg->write_count += len;
  503. return rc;
  504. }
  505. /*
  506. * qpnp_vreg_write_optimized - write the minimum sized contiguous subset of buf
  507. * @vreg: qpnp_regulator pointer for this regulator
  508. * @addr: local SPMI address offset from this peripheral's base address
  509. * @buf: new data to write into the SPMI registers
  510. * @buf_save: old data in the registers
  511. * @len: number of bytes to write
  512. *
  513. * This function checks for unchanged register values between buf and buf_save
  514. * starting at both ends of buf. Only the contiguous subset in the middle of
  515. * buf starting and ending with new values is sent.
  516. *
  517. * Consider the following example:
  518. * buf offset: 0 1 2 3 4 5 6 7
  519. * reg state: U U C C U C U U
  520. * (U = unchanged, C = changed)
  521. * In this example registers 2 through 5 will be written with a single
  522. * transaction.
  523. */
  524. static inline int qpnp_vreg_write_optimized(struct qpnp_regulator *vreg,
  525. u16 addr, u8 *buf, u8 *buf_save, int len)
  526. {
  527. int i, rc, start, end;
  528. for (i = 0; i < len; i++)
  529. if (buf[i] != buf_save[i])
  530. break;
  531. start = i;
  532. for (i = len - 1; i >= 0; i--)
  533. if (buf[i] != buf_save[i])
  534. break;
  535. end = i;
  536. if (start > end) {
  537. /* No modified register values present. */
  538. return 0;
  539. }
  540. rc = qpnp_vreg_write(vreg, addr + start, &buf[start], end - start + 1);
  541. if (!rc)
  542. for (i = start; i <= end; i++)
  543. buf_save[i] = buf[i];
  544. return rc;
  545. }
  546. /*
  547. * Perform a masked write to a PMIC register only if the new value differs
  548. * from the last value written to the register. This removes redundant
  549. * register writing.
  550. */
  551. static int qpnp_vreg_masked_write(struct qpnp_regulator *vreg, u16 addr, u8 val,
  552. u8 mask, u8 *reg_save)
  553. {
  554. int rc = 0;
  555. u8 reg;
  556. reg = (*reg_save & ~mask) | (val & mask);
  557. if (reg != *reg_save) {
  558. rc = qpnp_vreg_write(vreg, addr, &reg, 1);
  559. if (rc) {
  560. vreg_err(vreg, "write failed; addr=0x%03X, rc=%d\n",
  561. addr, rc);
  562. } else {
  563. *reg_save = reg;
  564. }
  565. }
  566. return rc;
  567. }
  568. /*
  569. * Perform a masked read-modify-write to a PMIC register only if the new value
  570. * differs from the value currently in the register. This removes redundant
  571. * register writing.
  572. */
  573. static int qpnp_vreg_masked_read_write(struct qpnp_regulator *vreg, u16 addr,
  574. u8 val, u8 mask)
  575. {
  576. int rc;
  577. u8 reg;
  578. rc = qpnp_vreg_read(vreg, addr, &reg, 1);
  579. if (rc) {
  580. vreg_err(vreg, "read failed; addr=0x%03X, rc=%d\n", addr, rc);
  581. return rc;
  582. }
  583. return qpnp_vreg_masked_write(vreg, addr, val, mask, &reg);
  584. }
  585. static int qpnp_regulator_common_is_enabled(struct regulator_dev *rdev)
  586. {
  587. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  588. return (vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]
  589. & QPNP_COMMON_ENABLE_MASK)
  590. == QPNP_COMMON_ENABLE;
  591. }
  592. static int qpnp_regulator_common_enable(struct regulator_dev *rdev)
  593. {
  594. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  595. int rc;
  596. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  597. QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
  598. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  599. if (rc)
  600. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  601. else
  602. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_ENABLE);
  603. return rc;
  604. }
  605. static int qpnp_regulator_vs_enable(struct regulator_dev *rdev)
  606. {
  607. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  608. if (vreg->ocp_irq) {
  609. vreg->ocp_count = 0;
  610. vreg->vs_enable_time = ktime_get();
  611. }
  612. return qpnp_regulator_common_enable(rdev);
  613. }
  614. static int qpnp_regulator_common_disable(struct regulator_dev *rdev)
  615. {
  616. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  617. int rc;
  618. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  619. QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
  620. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  621. if (rc)
  622. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  623. else
  624. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_DISABLE);
  625. return rc;
  626. }
  627. /*
  628. * Returns 1 if the voltage can be set in the current range, 0 if the voltage
  629. * cannot be set in the current range, or errno if an error occurred.
  630. */
  631. static int qpnp_regulator_select_voltage_same_range(struct qpnp_regulator *vreg,
  632. int min_uV, int max_uV, int *range_sel, int *voltage_sel,
  633. unsigned int *selector)
  634. {
  635. struct qpnp_voltage_range *range = NULL;
  636. int uV = min_uV;
  637. int i;
  638. *range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
  639. for (i = 0; i < vreg->set_points->count; i++) {
  640. if (vreg->set_points->range[i].range_sel == *range_sel) {
  641. range = &vreg->set_points->range[i];
  642. break;
  643. }
  644. }
  645. if (!range) {
  646. /* Unknown range */
  647. return 0;
  648. }
  649. if (uV < range->min_uV && max_uV >= range->min_uV)
  650. uV = range->min_uV;
  651. if (uV < range->min_uV || uV > range->max_uV) {
  652. /* Current range doesn't support the requested voltage. */
  653. return 0;
  654. }
  655. /*
  656. * Force uV to be an allowed set point by applying a ceiling function to
  657. * the uV value.
  658. */
  659. *voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
  660. uV = *voltage_sel * range->step_uV + range->min_uV;
  661. if (uV > max_uV) {
  662. /*
  663. * No set point in the current voltage range is within the
  664. * requested min_uV to max_uV range.
  665. */
  666. return 0;
  667. }
  668. *selector = 0;
  669. for (i = 0; i < vreg->set_points->count; i++) {
  670. if (uV >= vreg->set_points->range[i].set_point_min_uV
  671. && uV <= vreg->set_points->range[i].set_point_max_uV) {
  672. *selector +=
  673. (uV - vreg->set_points->range[i].set_point_min_uV)
  674. / vreg->set_points->range[i].step_uV;
  675. break;
  676. }
  677. *selector += vreg->set_points->range[i].n_voltages;
  678. }
  679. if (*selector >= vreg->set_points->n_voltages)
  680. return 0;
  681. return 1;
  682. }
  683. static int qpnp_regulator_select_voltage(struct qpnp_regulator *vreg,
  684. int min_uV, int max_uV, int *range_sel, int *voltage_sel,
  685. unsigned int *selector)
  686. {
  687. struct qpnp_voltage_range *range;
  688. int uV = min_uV;
  689. int lim_min_uV, lim_max_uV, i, range_id, range_max_uV;
  690. /* Check if request voltage is outside of physically settable range. */
  691. lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
  692. lim_max_uV =
  693. vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV;
  694. if (uV < lim_min_uV && max_uV >= lim_min_uV)
  695. uV = lim_min_uV;
  696. if (uV < lim_min_uV || uV > lim_max_uV) {
  697. vreg_err(vreg,
  698. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  699. min_uV, max_uV, lim_min_uV, lim_max_uV);
  700. return -EINVAL;
  701. }
  702. /* Find the range which uV is inside of. */
  703. for (i = vreg->set_points->count - 1; i > 0; i--) {
  704. range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV;
  705. if (uV > range_max_uV && range_max_uV > 0)
  706. break;
  707. }
  708. range_id = i;
  709. range = &vreg->set_points->range[range_id];
  710. *range_sel = range->range_sel;
  711. /*
  712. * Force uV to be an allowed set point by applying a ceiling function to
  713. * the uV value.
  714. */
  715. *voltage_sel = (uV - range->min_uV + range->step_uV - 1)
  716. / range->step_uV;
  717. uV = *voltage_sel * range->step_uV + range->min_uV;
  718. if (uV > max_uV) {
  719. vreg_err(vreg,
  720. "request v=[%d, %d] cannot be met by any set point; "
  721. "next set point: %d\n",
  722. min_uV, max_uV, uV);
  723. return -EINVAL;
  724. }
  725. *selector = 0;
  726. for (i = 0; i < range_id; i++)
  727. *selector += vreg->set_points->range[i].n_voltages;
  728. *selector += (uV - range->set_point_min_uV) / range->step_uV;
  729. return 0;
  730. }
  731. static int qpnp_regulator_delay_for_slewing(struct qpnp_regulator *vreg,
  732. int prev_voltage)
  733. {
  734. int current_voltage;
  735. /* Delay for voltage slewing if a step rate is specified. */
  736. if (vreg->slew_rate && vreg->rdesc.ops->get_voltage) {
  737. current_voltage = vreg->rdesc.ops->get_voltage(vreg->rdev);
  738. if (current_voltage < 0) {
  739. vreg_err(vreg, "could not get new voltage, rc=%d\n",
  740. current_voltage);
  741. return current_voltage;
  742. }
  743. udelay(DIV_ROUND_UP(abs(current_voltage - prev_voltage),
  744. vreg->slew_rate));
  745. }
  746. return 0;
  747. }
  748. static int qpnp_regulator_common_set_voltage(struct regulator_dev *rdev,
  749. int min_uV, int max_uV, unsigned int *selector)
  750. {
  751. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  752. int rc, range_sel, voltage_sel, voltage_old = 0;
  753. u8 buf[2];
  754. if (vreg->slew_rate && vreg->rdesc.ops->get_voltage) {
  755. voltage_old = vreg->rdesc.ops->get_voltage(rdev);
  756. if (voltage_old < 0) {
  757. vreg_err(vreg, "could not get current voltage, rc=%d\n",
  758. voltage_old);
  759. return voltage_old;
  760. }
  761. }
  762. /*
  763. * Favor staying in the current voltage range if possible. This avoids
  764. * voltage spikes that occur when changing the voltage range.
  765. */
  766. rc = qpnp_regulator_select_voltage_same_range(vreg, min_uV, max_uV,
  767. &range_sel, &voltage_sel, selector);
  768. if (rc == 0)
  769. rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV,
  770. &range_sel, &voltage_sel, selector);
  771. if (rc < 0) {
  772. vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
  773. return rc;
  774. }
  775. buf[0] = range_sel;
  776. buf[1] = voltage_sel;
  777. if ((vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] != range_sel)
  778. && (vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] == voltage_sel)) {
  779. /* Handle latched range change. */
  780. rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
  781. buf, 2);
  782. if (!rc) {
  783. vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = buf[0];
  784. vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET] = buf[1];
  785. }
  786. } else {
  787. /* Either write can be optimized away safely. */
  788. rc = qpnp_vreg_write_optimized(vreg,
  789. QPNP_COMMON_REG_VOLTAGE_RANGE, buf,
  790. &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE], 2);
  791. }
  792. if (rc) {
  793. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  794. } else {
  795. rc = qpnp_regulator_delay_for_slewing(vreg, voltage_old);
  796. if (rc)
  797. return rc;
  798. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
  799. }
  800. return rc;
  801. }
  802. static int qpnp_regulator_common_get_voltage(struct regulator_dev *rdev)
  803. {
  804. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  805. struct qpnp_voltage_range *range = NULL;
  806. int range_sel, voltage_sel, i;
  807. range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
  808. voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
  809. for (i = 0; i < vreg->set_points->count; i++) {
  810. if (vreg->set_points->range[i].range_sel == range_sel) {
  811. range = &vreg->set_points->range[i];
  812. break;
  813. }
  814. }
  815. if (!range) {
  816. vreg_err(vreg, "voltage unknown, range %d is invalid\n",
  817. range_sel);
  818. return VOLTAGE_UNKNOWN;
  819. }
  820. return range->step_uV * voltage_sel + range->min_uV;
  821. }
  822. static int qpnp_regulator_single_range_set_voltage(struct regulator_dev *rdev,
  823. int min_uV, int max_uV, unsigned int *selector)
  824. {
  825. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  826. int rc, range_sel, voltage_sel;
  827. rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
  828. &voltage_sel, selector);
  829. if (rc) {
  830. vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
  831. return rc;
  832. }
  833. /*
  834. * Certain types of regulators do not have a range select register so
  835. * only voltage set register needs to be written.
  836. */
  837. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
  838. voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
  839. if (rc)
  840. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  841. else
  842. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
  843. return rc;
  844. }
  845. static int qpnp_regulator_single_range_get_voltage(struct regulator_dev *rdev)
  846. {
  847. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  848. struct qpnp_voltage_range *range = &vreg->set_points->range[0];
  849. int voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
  850. return range->step_uV * voltage_sel + range->min_uV;
  851. }
  852. static int qpnp_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev,
  853. int min_uV, int max_uV, unsigned int *selector)
  854. {
  855. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  856. int rc, range_sel, voltage_sel;
  857. /*
  858. * Favor staying in the current voltage range if possible. This avoids
  859. * voltage spikes that occur when changing the voltage range.
  860. */
  861. rc = qpnp_regulator_select_voltage_same_range(vreg, min_uV, max_uV,
  862. &range_sel, &voltage_sel, selector);
  863. if (rc == 0)
  864. rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV,
  865. &range_sel, &voltage_sel, selector);
  866. if (rc < 0) {
  867. vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
  868. return rc;
  869. }
  870. /*
  871. * Calculate VSET based on range
  872. * In case of range 0: voltage_sel is a 7 bit value, can be written
  873. * witout any modification.
  874. * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
  875. * [011].
  876. */
  877. if (range_sel == 1)
  878. voltage_sel |= ULT_SMPS_RANGE_SPLIT;
  879. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_VOLTAGE_SET,
  880. voltage_sel, 0xFF, &vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]);
  881. if (rc) {
  882. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  883. } else {
  884. vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] = range_sel;
  885. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
  886. }
  887. return rc;
  888. }
  889. static int qpnp_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev)
  890. {
  891. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  892. struct qpnp_voltage_range *range = NULL;
  893. int range_sel, voltage_sel, i;
  894. range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
  895. voltage_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET];
  896. for (i = 0; i < vreg->set_points->count; i++) {
  897. if (vreg->set_points->range[i].range_sel == range_sel) {
  898. range = &vreg->set_points->range[i];
  899. break;
  900. }
  901. }
  902. if (!range) {
  903. vreg_err(vreg, "voltage unknown, range %d is invalid\n",
  904. range_sel);
  905. return VOLTAGE_UNKNOWN;
  906. }
  907. if (range_sel == 1)
  908. voltage_sel &= ~ULT_SMPS_RANGE_SPLIT;
  909. return range->step_uV * voltage_sel + range->min_uV;
  910. }
  911. static int qpnp_regulator_common_list_voltage(struct regulator_dev *rdev,
  912. unsigned int selector)
  913. {
  914. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  915. int uV = 0;
  916. int i;
  917. if (selector >= vreg->set_points->n_voltages)
  918. return 0;
  919. for (i = 0; i < vreg->set_points->count; i++) {
  920. if (selector < vreg->set_points->range[i].n_voltages) {
  921. uV = selector * vreg->set_points->range[i].step_uV
  922. + vreg->set_points->range[i].set_point_min_uV;
  923. break;
  924. }
  925. selector -= vreg->set_points->range[i].n_voltages;
  926. }
  927. return uV;
  928. }
  929. static int qpnp_regulator_common2_set_voltage(struct regulator_dev *rdev,
  930. int min_uV, int max_uV, unsigned int *selector)
  931. {
  932. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  933. int rc, range_sel, voltage_sel, voltage_old = 0;
  934. int voltage_uV, voltage_mV;
  935. u8 buf[2];
  936. if (vreg->slew_rate && vreg->rdesc.ops->get_voltage) {
  937. voltage_old = vreg->rdesc.ops->get_voltage(rdev);
  938. if (voltage_old < 0) {
  939. vreg_err(vreg, "could not get current voltage, rc=%d\n",
  940. voltage_old);
  941. return voltage_old;
  942. }
  943. }
  944. rc = qpnp_regulator_select_voltage(vreg, min_uV, max_uV, &range_sel,
  945. &voltage_sel, selector);
  946. if (rc < 0) {
  947. vreg_err(vreg, "could not set voltage, rc=%d\n", rc);
  948. return rc;
  949. }
  950. voltage_uV = qpnp_regulator_common_list_voltage(rdev, *selector);
  951. voltage_mV = voltage_uV / 1000;
  952. buf[0] = voltage_mV & 0xFF;
  953. buf[1] = (voltage_mV >> 8) & 0xFF;
  954. if (vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_LSB] != buf[0]
  955. || vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_MSB] != buf[1]) {
  956. /* MSB must always be written even if it is unchanged. */
  957. rc = qpnp_vreg_write(vreg, QPNP_COMMON2_REG_VOLTAGE_LSB,
  958. buf, 2);
  959. if (rc) {
  960. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  961. return rc;
  962. }
  963. vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_LSB] = buf[0];
  964. vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_MSB] = buf[1];
  965. rc = qpnp_regulator_delay_for_slewing(vreg, voltage_old);
  966. if (rc)
  967. return rc;
  968. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_VOLTAGE);
  969. }
  970. return rc;
  971. }
  972. static int qpnp_regulator_common2_get_voltage(struct regulator_dev *rdev)
  973. {
  974. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  975. return (((int)vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_MSB] << 8)
  976. | (int)vreg->ctrl_reg[QPNP_COMMON2_IDX_VOLTAGE_LSB]) * 1000;
  977. }
  978. static unsigned int qpnp_regulator_common_get_mode(struct regulator_dev *rdev)
  979. {
  980. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  981. return (vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]
  982. & QPNP_COMMON_MODE_HPM_MASK)
  983. ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
  984. }
  985. static int qpnp_regulator_common_set_mode(struct regulator_dev *rdev,
  986. unsigned int mode)
  987. {
  988. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  989. int rc = 0;
  990. u8 val;
  991. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  992. vreg_err(vreg, "invalid mode: %u\n", mode);
  993. return -EINVAL;
  994. }
  995. val = (mode == REGULATOR_MODE_NORMAL ? QPNP_COMMON_MODE_HPM_MASK : 0);
  996. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_MODE, val,
  997. QPNP_COMMON_MODE_HPM_MASK,
  998. &vreg->ctrl_reg[QPNP_COMMON_IDX_MODE]);
  999. if (rc)
  1000. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  1001. else
  1002. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
  1003. return rc;
  1004. }
  1005. static unsigned int qpnp_regulator_common_get_optimum_mode(
  1006. struct regulator_dev *rdev, int input_uV, int output_uV,
  1007. int load_uA)
  1008. {
  1009. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  1010. unsigned int mode;
  1011. if (load_uA + vreg->system_load >= vreg->hpm_min_load)
  1012. mode = REGULATOR_MODE_NORMAL;
  1013. else
  1014. mode = REGULATOR_MODE_IDLE;
  1015. return mode;
  1016. }
  1017. static unsigned int qpnp_regulator_common2_get_mode(struct regulator_dev *rdev)
  1018. {
  1019. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  1020. return vreg->ctrl_reg[QPNP_COMMON2_IDX_MODE] == QPNP_COMMON2_MODE_HPM
  1021. ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
  1022. }
  1023. static int qpnp_regulator_common2_set_mode(struct regulator_dev *rdev,
  1024. unsigned int mode)
  1025. {
  1026. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  1027. int rc = 0;
  1028. u8 val = QPNP_COMMON2_MODE_HPM;
  1029. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  1030. vreg_err(vreg, "invalid mode: %u\n", mode);
  1031. return -EINVAL;
  1032. }
  1033. /*
  1034. * Use init_mode as the low power mode unless it is equal to HPM. This
  1035. * ensures that AUTO mode is re-asserted after switching away from
  1036. * forced HPM if it was configured initially.
  1037. */
  1038. if (mode == REGULATOR_MODE_NORMAL)
  1039. val = QPNP_COMMON2_MODE_HPM;
  1040. else if (vreg->init_mode == QPNP_COMMON2_MODE_HPM)
  1041. val = QPNP_COMMON2_MODE_LPM;
  1042. else
  1043. val = vreg->init_mode;
  1044. rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON2_REG_MODE, &val,
  1045. &vreg->ctrl_reg[QPNP_COMMON2_IDX_MODE], 1);
  1046. if (rc)
  1047. vreg_err(vreg, "SPMI write failed, rc=%d\n", rc);
  1048. else
  1049. qpnp_vreg_show_state(rdev, QPNP_REGULATOR_ACTION_MODE);
  1050. return rc;
  1051. }
  1052. static int qpnp_regulator_common_enable_time(struct regulator_dev *rdev)
  1053. {
  1054. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  1055. return vreg->enable_time;
  1056. }
  1057. static int qpnp_regulator_vs_clear_ocp(struct qpnp_regulator *vreg)
  1058. {
  1059. int rc;
  1060. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  1061. QPNP_COMMON_DISABLE, QPNP_COMMON_ENABLE_MASK,
  1062. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  1063. if (rc)
  1064. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  1065. vreg->vs_enable_time = ktime_get();
  1066. rc = qpnp_vreg_masked_write(vreg, QPNP_COMMON_REG_ENABLE,
  1067. QPNP_COMMON_ENABLE, QPNP_COMMON_ENABLE_MASK,
  1068. &vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE]);
  1069. if (rc)
  1070. vreg_err(vreg, "qpnp_vreg_masked_write failed, rc=%d\n", rc);
  1071. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
  1072. pr_info("%s: switch state toggled after OCP event\n",
  1073. vreg->rdesc.name);
  1074. }
  1075. return rc;
  1076. }
  1077. static void qpnp_regulator_vs_ocp_work(struct work_struct *work)
  1078. {
  1079. struct delayed_work *dwork
  1080. = container_of(work, struct delayed_work, work);
  1081. struct qpnp_regulator *vreg
  1082. = container_of(dwork, struct qpnp_regulator, ocp_work);
  1083. qpnp_regulator_vs_clear_ocp(vreg);
  1084. }
  1085. static irqreturn_t qpnp_regulator_vs_ocp_isr(int irq, void *data)
  1086. {
  1087. struct qpnp_regulator *vreg = data;
  1088. ktime_t ocp_irq_time;
  1089. s64 ocp_trigger_delay_us;
  1090. ocp_irq_time = ktime_get();
  1091. ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
  1092. vreg->vs_enable_time);
  1093. /*
  1094. * Reset the OCP count if there is a large delay between switch enable
  1095. * and when OCP triggers. This is indicative of a hotplug event as
  1096. * opposed to a fault.
  1097. */
  1098. if (ocp_trigger_delay_us > QPNP_VS_OCP_FAULT_DELAY_US)
  1099. vreg->ocp_count = 0;
  1100. /* Wait for switch output to settle back to 0 V after OCP triggered. */
  1101. udelay(QPNP_VS_OCP_FALL_DELAY_US);
  1102. vreg->ocp_count++;
  1103. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_OCP) {
  1104. pr_info("%s: VS OCP triggered, count = %d, delay = %lld us\n",
  1105. vreg->rdesc.name, vreg->ocp_count,
  1106. ocp_trigger_delay_us);
  1107. }
  1108. if (vreg->ocp_count == 1) {
  1109. /* Immediately clear the over current condition. */
  1110. qpnp_regulator_vs_clear_ocp(vreg);
  1111. } else if (vreg->ocp_count <= vreg->ocp_max_retries) {
  1112. /* Schedule the over current clear task to run later. */
  1113. schedule_delayed_work(&vreg->ocp_work,
  1114. msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
  1115. } else {
  1116. vreg_err(vreg, "OCP triggered %d times; no further retries\n",
  1117. vreg->ocp_count);
  1118. }
  1119. return IRQ_HANDLED;
  1120. }
  1121. static const char * const qpnp_print_actions[] = {
  1122. [QPNP_REGULATOR_ACTION_INIT] = "initial ",
  1123. [QPNP_REGULATOR_ACTION_ENABLE] = "enable ",
  1124. [QPNP_REGULATOR_ACTION_DISABLE] = "disable ",
  1125. [QPNP_REGULATOR_ACTION_VOLTAGE] = "set voltage",
  1126. [QPNP_REGULATOR_ACTION_MODE] = "set mode ",
  1127. };
  1128. static const char * const qpnp_common2_mode_label[] = {
  1129. [0] = "RSV",
  1130. [1] = "RSV",
  1131. [2] = "RSV",
  1132. [QPNP_COMMON2_MODE_BYPASS] = "BYP",
  1133. [QPNP_COMMON2_MODE_RETENTION] = "RET",
  1134. [QPNP_COMMON2_MODE_LPM] = "LPM",
  1135. [QPNP_COMMON2_MODE_AUTO] = "AUTO",
  1136. [QPNP_COMMON2_MODE_HPM] = "HPM",
  1137. };
  1138. static void qpnp_vreg_show_state(struct regulator_dev *rdev,
  1139. enum qpnp_regulator_action action)
  1140. {
  1141. struct qpnp_regulator *vreg = rdev_get_drvdata(rdev);
  1142. const char *action_label = qpnp_print_actions[action];
  1143. unsigned int mode = 0;
  1144. int uV = 0;
  1145. const char *mode_label = "";
  1146. enum qpnp_regulator_logical_type type;
  1147. const char *enable_label = "";
  1148. char pc_enable_label[5] = {'\0'};
  1149. char pc_mode_label[8] = {'\0'};
  1150. bool show_req, show_dupe, show_init, has_changed;
  1151. u8 en_reg, mode_reg;
  1152. /* Do not print unless appropriate flags are set. */
  1153. show_req = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_REQUEST;
  1154. show_dupe = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_DUPLICATE;
  1155. show_init = qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT;
  1156. has_changed = vreg->write_count != vreg->prev_write_count;
  1157. if (!((show_init && action == QPNP_REGULATOR_ACTION_INIT)
  1158. || (show_req && (has_changed || show_dupe)))) {
  1159. return;
  1160. }
  1161. vreg->prev_write_count = vreg->write_count;
  1162. type = vreg->logical_type;
  1163. if (vreg->rdesc.ops->is_enabled)
  1164. enable_label = vreg->rdesc.ops->is_enabled(rdev)
  1165. ? "on " : "off";
  1166. if (vreg->rdesc.ops->get_voltage)
  1167. uV = vreg->rdesc.ops->get_voltage(rdev);
  1168. if (vreg->rdesc.ops->get_mode) {
  1169. mode = vreg->rdesc.ops->get_mode(rdev);
  1170. mode_label = mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM";
  1171. }
  1172. if (type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1173. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1174. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
  1175. en_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_ENABLE];
  1176. pc_enable_label[0] =
  1177. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN3_MASK ? '3' : '_';
  1178. pc_enable_label[1] =
  1179. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN2_MASK ? '2' : '_';
  1180. pc_enable_label[2] =
  1181. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN1_MASK ? '1' : '_';
  1182. pc_enable_label[3] =
  1183. en_reg & QPNP_COMMON_ENABLE_FOLLOW_HW_EN0_MASK ? '0' : '_';
  1184. }
  1185. switch (type) {
  1186. case QPNP_REGULATOR_LOGICAL_TYPE_SMPS:
  1187. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1188. pc_mode_label[0] =
  1189. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  1190. pc_mode_label[1] =
  1191. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  1192. pc_mode_label[2] =
  1193. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
  1194. pc_mode_label[3] =
  1195. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
  1196. pc_mode_label[4] =
  1197. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
  1198. pc_mode_label[5] =
  1199. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
  1200. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, alt_mode=%s\n",
  1201. action_label, vreg->rdesc.name, enable_label, uV,
  1202. mode_label, pc_enable_label, pc_mode_label);
  1203. break;
  1204. case QPNP_REGULATOR_LOGICAL_TYPE_LDO:
  1205. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1206. pc_mode_label[0] =
  1207. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  1208. pc_mode_label[1] =
  1209. mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
  1210. pc_mode_label[2] =
  1211. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  1212. pc_mode_label[3] =
  1213. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN3_MASK ? '3' : '_';
  1214. pc_mode_label[4] =
  1215. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN2_MASK ? '2' : '_';
  1216. pc_mode_label[5] =
  1217. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN1_MASK ? '1' : '_';
  1218. pc_mode_label[6] =
  1219. mode_reg & QPNP_COMMON_MODE_FOLLOW_HW_EN0_MASK ? '0' : '_';
  1220. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, pc_en=%s, alt_mode=%s\n",
  1221. action_label, vreg->rdesc.name, enable_label, uV,
  1222. mode_label, pc_enable_label, pc_mode_label);
  1223. break;
  1224. case QPNP_REGULATOR_LOGICAL_TYPE_LN_LDO:
  1225. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1226. pc_mode_label[0] =
  1227. mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
  1228. pr_info("%s %-11s: %s, v=%7d uV, alt_mode=%s\n",
  1229. action_label, vreg->rdesc.name, enable_label, uV,
  1230. pc_mode_label);
  1231. break;
  1232. case QPNP_REGULATOR_LOGICAL_TYPE_VS:
  1233. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1234. pc_mode_label[0] =
  1235. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  1236. pc_mode_label[1] =
  1237. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  1238. pr_info("%s %-11s: %s, mode=%s, pc_en=%s, alt_mode=%s\n",
  1239. action_label, vreg->rdesc.name, enable_label,
  1240. mode_label, pc_enable_label, pc_mode_label);
  1241. break;
  1242. case QPNP_REGULATOR_LOGICAL_TYPE_BOOST:
  1243. pr_info("%s %-11s: %s, v=%7d uV\n",
  1244. action_label, vreg->rdesc.name, enable_label, uV);
  1245. break;
  1246. case QPNP_REGULATOR_LOGICAL_TYPE_BOOST_BYP:
  1247. pr_info("%s %-11s: %s, v=%7d uV\n",
  1248. action_label, vreg->rdesc.name, enable_label, uV);
  1249. break;
  1250. case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS:
  1251. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1252. pc_mode_label[0] =
  1253. mode_reg & QPNP_COMMON_MODE_AUTO_MASK ? 'A' : '_';
  1254. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
  1255. action_label, vreg->rdesc.name, enable_label, uV,
  1256. mode_label, pc_mode_label);
  1257. break;
  1258. case QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS:
  1259. case QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS:
  1260. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1261. pc_mode_label[0] =
  1262. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  1263. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
  1264. action_label, vreg->rdesc.name, enable_label, uV,
  1265. mode_label, pc_mode_label);
  1266. break;
  1267. case QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO:
  1268. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1269. pc_mode_label[0] =
  1270. mode_reg & QPNP_COMMON_MODE_BYPASS_MASK ? 'B' : '_';
  1271. pc_mode_label[1] =
  1272. mode_reg & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK ? 'W' : '_';
  1273. pr_info("%s %-11s: %s, v=%7d uV, mode=%s, alt_mode=%s\n",
  1274. action_label, vreg->rdesc.name, enable_label, uV,
  1275. mode_label, pc_mode_label);
  1276. break;
  1277. case QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2:
  1278. mode_reg = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1279. mode_label = qpnp_common2_mode_label[mode_reg
  1280. & QPNP_COMMON2_MODE_MASK];
  1281. pr_info("%s %-11s: %s, v=%7d uV, mode=%s\n",
  1282. action_label, vreg->rdesc.name, enable_label, uV,
  1283. mode_label);
  1284. break;
  1285. default:
  1286. break;
  1287. }
  1288. }
  1289. static struct regulator_ops qpnp_smps_ops = {
  1290. .enable = qpnp_regulator_common_enable,
  1291. .disable = qpnp_regulator_common_disable,
  1292. .is_enabled = qpnp_regulator_common_is_enabled,
  1293. .set_voltage = qpnp_regulator_common_set_voltage,
  1294. .get_voltage = qpnp_regulator_common_get_voltage,
  1295. .list_voltage = qpnp_regulator_common_list_voltage,
  1296. .set_mode = qpnp_regulator_common_set_mode,
  1297. .get_mode = qpnp_regulator_common_get_mode,
  1298. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1299. .enable_time = qpnp_regulator_common_enable_time,
  1300. };
  1301. static struct regulator_ops qpnp_ldo_ops = {
  1302. .enable = qpnp_regulator_common_enable,
  1303. .disable = qpnp_regulator_common_disable,
  1304. .is_enabled = qpnp_regulator_common_is_enabled,
  1305. .set_voltage = qpnp_regulator_common_set_voltage,
  1306. .get_voltage = qpnp_regulator_common_get_voltage,
  1307. .list_voltage = qpnp_regulator_common_list_voltage,
  1308. .set_mode = qpnp_regulator_common_set_mode,
  1309. .get_mode = qpnp_regulator_common_get_mode,
  1310. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1311. .enable_time = qpnp_regulator_common_enable_time,
  1312. };
  1313. static struct regulator_ops qpnp_ln_ldo_ops = {
  1314. .enable = qpnp_regulator_common_enable,
  1315. .disable = qpnp_regulator_common_disable,
  1316. .is_enabled = qpnp_regulator_common_is_enabled,
  1317. .set_voltage = qpnp_regulator_common_set_voltage,
  1318. .get_voltage = qpnp_regulator_common_get_voltage,
  1319. .list_voltage = qpnp_regulator_common_list_voltage,
  1320. .enable_time = qpnp_regulator_common_enable_time,
  1321. };
  1322. static struct regulator_ops qpnp_vs_ops = {
  1323. .enable = qpnp_regulator_vs_enable,
  1324. .disable = qpnp_regulator_common_disable,
  1325. .is_enabled = qpnp_regulator_common_is_enabled,
  1326. .enable_time = qpnp_regulator_common_enable_time,
  1327. };
  1328. static struct regulator_ops qpnp_boost_ops = {
  1329. .enable = qpnp_regulator_common_enable,
  1330. .disable = qpnp_regulator_common_disable,
  1331. .is_enabled = qpnp_regulator_common_is_enabled,
  1332. .set_voltage = qpnp_regulator_single_range_set_voltage,
  1333. .get_voltage = qpnp_regulator_single_range_get_voltage,
  1334. .list_voltage = qpnp_regulator_common_list_voltage,
  1335. .enable_time = qpnp_regulator_common_enable_time,
  1336. };
  1337. static struct regulator_ops qpnp_ftsmps_ops = {
  1338. .enable = qpnp_regulator_common_enable,
  1339. .disable = qpnp_regulator_common_disable,
  1340. .is_enabled = qpnp_regulator_common_is_enabled,
  1341. .set_voltage = qpnp_regulator_common_set_voltage,
  1342. .get_voltage = qpnp_regulator_common_get_voltage,
  1343. .list_voltage = qpnp_regulator_common_list_voltage,
  1344. .set_mode = qpnp_regulator_common_set_mode,
  1345. .get_mode = qpnp_regulator_common_get_mode,
  1346. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1347. .enable_time = qpnp_regulator_common_enable_time,
  1348. };
  1349. static struct regulator_ops qpnp_ult_lo_smps_ops = {
  1350. .enable = qpnp_regulator_common_enable,
  1351. .disable = qpnp_regulator_common_disable,
  1352. .is_enabled = qpnp_regulator_common_is_enabled,
  1353. .set_voltage = qpnp_regulator_ult_lo_smps_set_voltage,
  1354. .get_voltage = qpnp_regulator_ult_lo_smps_get_voltage,
  1355. .list_voltage = qpnp_regulator_common_list_voltage,
  1356. .set_mode = qpnp_regulator_common_set_mode,
  1357. .get_mode = qpnp_regulator_common_get_mode,
  1358. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1359. .enable_time = qpnp_regulator_common_enable_time,
  1360. };
  1361. static struct regulator_ops qpnp_ult_ho_smps_ops = {
  1362. .enable = qpnp_regulator_common_enable,
  1363. .disable = qpnp_regulator_common_disable,
  1364. .is_enabled = qpnp_regulator_common_is_enabled,
  1365. .set_voltage = qpnp_regulator_single_range_set_voltage,
  1366. .get_voltage = qpnp_regulator_single_range_get_voltage,
  1367. .list_voltage = qpnp_regulator_common_list_voltage,
  1368. .set_mode = qpnp_regulator_common_set_mode,
  1369. .get_mode = qpnp_regulator_common_get_mode,
  1370. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1371. .enable_time = qpnp_regulator_common_enable_time,
  1372. };
  1373. static struct regulator_ops qpnp_ult_ldo_ops = {
  1374. .enable = qpnp_regulator_common_enable,
  1375. .disable = qpnp_regulator_common_disable,
  1376. .is_enabled = qpnp_regulator_common_is_enabled,
  1377. .set_voltage = qpnp_regulator_single_range_set_voltage,
  1378. .get_voltage = qpnp_regulator_single_range_get_voltage,
  1379. .list_voltage = qpnp_regulator_common_list_voltage,
  1380. .set_mode = qpnp_regulator_common_set_mode,
  1381. .get_mode = qpnp_regulator_common_get_mode,
  1382. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1383. .enable_time = qpnp_regulator_common_enable_time,
  1384. };
  1385. static struct regulator_ops qpnp_ftsmps426_ops = {
  1386. .enable = qpnp_regulator_common_enable,
  1387. .disable = qpnp_regulator_common_disable,
  1388. .is_enabled = qpnp_regulator_common_is_enabled,
  1389. .set_voltage = qpnp_regulator_common2_set_voltage,
  1390. .get_voltage = qpnp_regulator_common2_get_voltage,
  1391. .list_voltage = qpnp_regulator_common_list_voltage,
  1392. .set_mode = qpnp_regulator_common2_set_mode,
  1393. .get_mode = qpnp_regulator_common2_get_mode,
  1394. .get_optimum_mode = qpnp_regulator_common_get_optimum_mode,
  1395. .enable_time = qpnp_regulator_common_enable_time,
  1396. };
  1397. /* Maximum possible digital major revision value */
  1398. #define INF 0xFF
  1399. static const struct qpnp_regulator_mapping supported_regulators[] = {
  1400. /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
  1401. QPNP_VREG_MAP(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
  1402. QPNP_VREG_MAP(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
  1403. QPNP_VREG_MAP(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
  1404. QPNP_VREG_MAP(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
  1405. QPNP_VREG_MAP(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
  1406. QPNP_VREG_MAP(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
  1407. QPNP_VREG_MAP(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
  1408. QPNP_VREG_MAP(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
  1409. QPNP_VREG_MAP(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
  1410. QPNP_VREG_MAP(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
  1411. QPNP_VREG_MAP(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
  1412. QPNP_VREG_MAP(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
  1413. QPNP_VREG_MAP(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
  1414. QPNP_VREG_MAP(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
  1415. QPNP_VREG_MAP(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
  1416. QPNP_VREG_MAP(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0),
  1417. QPNP_VREG_MAP(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
  1418. QPNP_VREG_MAP(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
  1419. QPNP_VREG_MAP(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
  1420. QPNP_VREG_MAP(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
  1421. QPNP_VREG_MAP(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
  1422. QPNP_VREG_MAP(VS, LV100, 0, INF, VS, vs, none, 0),
  1423. QPNP_VREG_MAP(VS, LV300, 0, INF, VS, vs, none, 0),
  1424. QPNP_VREG_MAP(VS, MV300, 0, INF, VS, vs, none, 0),
  1425. QPNP_VREG_MAP(VS, MV500, 0, INF, VS, vs, none, 0),
  1426. QPNP_VREG_MAP(VS, HDMI, 0, INF, VS, vs, none, 0),
  1427. QPNP_VREG_MAP(VS, OTG, 0, INF, VS, vs, none, 0),
  1428. QPNP_VREG_MAP(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
  1429. QPNP_VREG_MAP(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
  1430. QPNP_VREG_MAP(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5,
  1431. 100000),
  1432. QPNP_VREG_MAP(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0),
  1433. QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps,
  1434. ult_lo_smps, 100000),
  1435. QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps,
  1436. ult_lo_smps, 100000),
  1437. QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps,
  1438. ult_lo_smps, 100000),
  1439. QPNP_VREG_MAP(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps,
  1440. ult_ho_smps, 100000),
  1441. QPNP_VREG_MAP(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo,
  1442. 10000),
  1443. QPNP_VREG_MAP(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo,
  1444. 10000),
  1445. QPNP_VREG_MAP(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo,
  1446. 10000),
  1447. QPNP_VREG_MAP(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo,
  1448. 10000),
  1449. QPNP_VREG_MAP(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo,
  1450. 10000),
  1451. QPNP_VREG_MAP(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo,
  1452. 10000),
  1453. QPNP_VREG_MAP(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo,
  1454. 10000),
  1455. QPNP_VREG_MAP(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo,
  1456. 5000),
  1457. QPNP_VREG_MAP(FTS, FTS426, 0, INF, FTSMPS2, ftsmps426, ftsmps426,
  1458. 100000),
  1459. };
  1460. static int qpnp_regulator_match(struct qpnp_regulator *vreg)
  1461. {
  1462. const struct qpnp_regulator_mapping *mapping;
  1463. struct device_node *node = vreg->pdev->dev.of_node;
  1464. int rc, i;
  1465. u32 type_reg[2], dig_major_rev;
  1466. u8 version[QPNP_COMMON_REG_SUBTYPE - QPNP_COMMON_REG_DIG_MAJOR_REV + 1];
  1467. u8 type, subtype;
  1468. rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_DIG_MAJOR_REV, version,
  1469. ARRAY_SIZE(version));
  1470. if (rc) {
  1471. vreg_err(vreg, "could not read version registers, rc=%d\n", rc);
  1472. return rc;
  1473. }
  1474. dig_major_rev = version[QPNP_COMMON_REG_DIG_MAJOR_REV
  1475. - QPNP_COMMON_REG_DIG_MAJOR_REV];
  1476. type = version[QPNP_COMMON_REG_TYPE
  1477. - QPNP_COMMON_REG_DIG_MAJOR_REV];
  1478. subtype = version[QPNP_COMMON_REG_SUBTYPE
  1479. - QPNP_COMMON_REG_DIG_MAJOR_REV];
  1480. /*
  1481. * Override type and subtype register values if qcom,force-type is
  1482. * present in the device tree node.
  1483. */
  1484. rc = of_property_read_u32_array(node, "qcom,force-type", type_reg, 2);
  1485. if (!rc) {
  1486. type = type_reg[0];
  1487. subtype = type_reg[1];
  1488. }
  1489. rc = -ENODEV;
  1490. for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
  1491. mapping = &supported_regulators[i];
  1492. if (mapping->type == type && mapping->subtype == subtype
  1493. && mapping->revision_min <= dig_major_rev
  1494. && mapping->revision_max >= dig_major_rev) {
  1495. vreg->logical_type = mapping->logical_type;
  1496. vreg->set_points = mapping->set_points;
  1497. vreg->hpm_min_load = mapping->hpm_min_load;
  1498. vreg->rdesc.ops = mapping->ops;
  1499. vreg->rdesc.n_voltages
  1500. = mapping->set_points->n_voltages;
  1501. rc = 0;
  1502. break;
  1503. }
  1504. }
  1505. if (rc)
  1506. vreg_err(vreg, "unsupported regulator: type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
  1507. type, subtype, dig_major_rev);
  1508. return rc;
  1509. }
  1510. static int qpnp_regulator_ftsmps_init_slew_rate(struct qpnp_regulator *vreg)
  1511. {
  1512. int rc;
  1513. u8 reg = 0;
  1514. int step = 0, delay, i, range_sel;
  1515. struct qpnp_voltage_range *range = NULL;
  1516. rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_STEP_CTRL, &reg, 1);
  1517. if (rc) {
  1518. vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
  1519. return rc;
  1520. }
  1521. range_sel = vreg->ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE];
  1522. for (i = 0; i < vreg->set_points->count; i++) {
  1523. if (vreg->set_points->range[i].range_sel == range_sel) {
  1524. range = &vreg->set_points->range[i];
  1525. break;
  1526. }
  1527. }
  1528. if (!range) {
  1529. vreg_err(vreg, "range %d is invalid\n", range_sel);
  1530. return -EINVAL;
  1531. }
  1532. step = (reg & QPNP_FTSMPS_STEP_CTRL_STEP_MASK)
  1533. >> QPNP_FTSMPS_STEP_CTRL_STEP_SHIFT;
  1534. delay = (reg & QPNP_FTSMPS_STEP_CTRL_DELAY_MASK)
  1535. >> QPNP_FTSMPS_STEP_CTRL_DELAY_SHIFT;
  1536. /* slew_rate has units of uV/us. */
  1537. vreg->slew_rate = QPNP_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step);
  1538. vreg->slew_rate /= 1000 * (QPNP_FTSMPS_STEP_DELAY << delay);
  1539. vreg->slew_rate = vreg->slew_rate * QPNP_FTSMPS_STEP_MARGIN_NUM
  1540. / QPNP_FTSMPS_STEP_MARGIN_DEN;
  1541. /* Ensure that the slew rate is greater than 0. */
  1542. vreg->slew_rate = max(vreg->slew_rate, 1);
  1543. return rc;
  1544. }
  1545. static int qpnp_regulator_ftsmps2_init_slew_rate(struct qpnp_regulator *vreg)
  1546. {
  1547. struct qpnp_voltage_range *range = NULL;
  1548. int i, rc, delay;
  1549. u8 reg = 0;
  1550. rc = qpnp_vreg_read(vreg, QPNP_COMMON2_REG_STEP_CTRL, &reg, 1);
  1551. if (rc) {
  1552. vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
  1553. return rc;
  1554. }
  1555. /*
  1556. * Regulators using the common #2 register layout do not have a voltage
  1557. * range select register. Choose the lowest possible step size to be
  1558. * conservative in the slew rate calculation.
  1559. */
  1560. for (i = 0; i < vreg->set_points->count; i++) {
  1561. if (!range || vreg->set_points->range[i].step_uV
  1562. < range->step_uV)
  1563. range = &vreg->set_points->range[i];
  1564. }
  1565. if (!range) {
  1566. vreg_err(vreg, "range is invalid\n");
  1567. return -EINVAL;
  1568. }
  1569. delay = (reg & QPNP_FTSMPS2_STEP_CTRL_DELAY_MASK)
  1570. >> QPNP_FTSMPS2_STEP_CTRL_DELAY_SHIFT;
  1571. /* slew_rate has units of uV/us. */
  1572. vreg->slew_rate = QPNP_FTSMPS2_CLOCK_RATE * range->step_uV;
  1573. vreg->slew_rate /= 1000 * (QPNP_FTSMPS2_STEP_DELAY << delay);
  1574. vreg->slew_rate = vreg->slew_rate * QPNP_FTSMPS2_STEP_MARGIN_NUM
  1575. / QPNP_FTSMPS2_STEP_MARGIN_DEN;
  1576. /* Ensure that the slew rate is greater than 0. */
  1577. vreg->slew_rate = max(vreg->slew_rate, 1);
  1578. return rc;
  1579. }
  1580. static int qpnp_regulator_init_registers(struct qpnp_regulator *vreg,
  1581. struct qpnp_regulator_platform_data *pdata)
  1582. {
  1583. int rc, i;
  1584. enum qpnp_regulator_logical_type type;
  1585. u8 ctrl_reg[8], reg, mask;
  1586. type = vreg->logical_type;
  1587. rc = qpnp_vreg_read(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
  1588. vreg->ctrl_reg, 8);
  1589. if (rc) {
  1590. vreg_err(vreg, "spmi read failed, rc=%d\n", rc);
  1591. return rc;
  1592. }
  1593. for (i = 0; i < ARRAY_SIZE(ctrl_reg); i++)
  1594. ctrl_reg[i] = vreg->ctrl_reg[i];
  1595. /* Set up enable pin control. */
  1596. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1597. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1598. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
  1599. && !(pdata->pin_ctrl_enable
  1600. & QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
  1601. ctrl_reg[QPNP_COMMON_IDX_ENABLE] &=
  1602. ~QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
  1603. ctrl_reg[QPNP_COMMON_IDX_ENABLE] |=
  1604. pdata->pin_ctrl_enable & QPNP_COMMON_ENABLE_FOLLOW_ALL_MASK;
  1605. }
  1606. /* Set up HPM control. */
  1607. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1608. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
  1609. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
  1610. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO
  1611. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1612. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
  1613. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
  1614. && (pdata->hpm_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
  1615. ctrl_reg[QPNP_COMMON_IDX_MODE] &= ~QPNP_COMMON_MODE_HPM_MASK;
  1616. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1617. (pdata->hpm_enable ? QPNP_COMMON_MODE_HPM_MASK : 0);
  1618. }
  1619. /* Set up auto mode control. */
  1620. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1621. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1622. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS
  1623. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS)
  1624. && (pdata->auto_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT)) {
  1625. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1626. ~QPNP_COMMON_MODE_AUTO_MASK;
  1627. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1628. (pdata->auto_mode_enable ? QPNP_COMMON_MODE_AUTO_MASK : 0);
  1629. }
  1630. if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2) {
  1631. if (pdata->hpm_enable == QPNP_REGULATOR_ENABLE)
  1632. ctrl_reg[QPNP_COMMON2_IDX_MODE]
  1633. = QPNP_COMMON2_MODE_HPM;
  1634. else if (pdata->auto_mode_enable == QPNP_REGULATOR_ENABLE)
  1635. ctrl_reg[QPNP_COMMON2_IDX_MODE]
  1636. = QPNP_COMMON2_MODE_AUTO;
  1637. else if (pdata->hpm_enable == QPNP_REGULATOR_DISABLE
  1638. && ctrl_reg[QPNP_COMMON2_IDX_MODE]
  1639. == QPNP_COMMON2_MODE_HPM)
  1640. ctrl_reg[QPNP_COMMON2_IDX_MODE]
  1641. = QPNP_COMMON2_MODE_LPM;
  1642. else if (pdata->auto_mode_enable == QPNP_REGULATOR_DISABLE
  1643. && ctrl_reg[QPNP_COMMON2_IDX_MODE]
  1644. == QPNP_COMMON2_MODE_AUTO)
  1645. ctrl_reg[QPNP_COMMON2_IDX_MODE]
  1646. = QPNP_COMMON2_MODE_LPM;
  1647. }
  1648. /* Set up mode pin control. */
  1649. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1650. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO)
  1651. && !(pdata->pin_ctrl_hpm
  1652. & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1653. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1654. ~QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
  1655. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1656. pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_ALL_MASK;
  1657. }
  1658. if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS
  1659. && !(pdata->pin_ctrl_hpm & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1660. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1661. ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1662. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1663. pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1664. }
  1665. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
  1666. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
  1667. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO)
  1668. && !(pdata->pin_ctrl_hpm
  1669. & QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1670. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1671. ~QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1672. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1673. pdata->pin_ctrl_hpm & QPNP_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1674. }
  1675. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1676. || type == QPNP_REGULATOR_LOGICAL_TYPE_LN_LDO
  1677. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO)
  1678. && pdata->bypass_mode_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1679. ctrl_reg[QPNP_COMMON_IDX_MODE] &=
  1680. ~QPNP_COMMON_MODE_BYPASS_MASK;
  1681. ctrl_reg[QPNP_COMMON_IDX_MODE] |=
  1682. (pdata->bypass_mode_enable
  1683. ? QPNP_COMMON_MODE_BYPASS_MASK : 0);
  1684. }
  1685. /* Set boost current limit. */
  1686. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
  1687. || type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST_BYP)
  1688. && pdata->boost_current_limit
  1689. != QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT) {
  1690. reg = pdata->boost_current_limit;
  1691. mask = QPNP_BOOST_CURRENT_LIMIT_MASK;
  1692. rc = qpnp_vreg_masked_read_write(vreg,
  1693. (type == QPNP_REGULATOR_LOGICAL_TYPE_BOOST
  1694. ? QPNP_BOOST_REG_CURRENT_LIMIT
  1695. : QPNP_BOOST_BYP_REG_CURRENT_LIMIT),
  1696. reg, mask);
  1697. if (rc) {
  1698. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1699. return rc;
  1700. }
  1701. }
  1702. /* Write back any control register values that were modified. */
  1703. rc = qpnp_vreg_write_optimized(vreg, QPNP_COMMON_REG_VOLTAGE_RANGE,
  1704. ctrl_reg, vreg->ctrl_reg, 8);
  1705. if (rc) {
  1706. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1707. return rc;
  1708. }
  1709. /* Setup initial range for ULT_LO_SMPS */
  1710. if (type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS) {
  1711. ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_RANGE] =
  1712. (ctrl_reg[QPNP_COMMON_IDX_VOLTAGE_SET]
  1713. < ULT_SMPS_RANGE_SPLIT) ? 0 : 1;
  1714. }
  1715. /* Set pull down. */
  1716. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_SMPS
  1717. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
  1718. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
  1719. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO
  1720. || type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1721. || type == QPNP_REGULATOR_LOGICAL_TYPE_VS)
  1722. && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1723. reg = pdata->pull_down_enable
  1724. ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
  1725. rc = qpnp_vreg_write(vreg, QPNP_COMMON_REG_PULL_DOWN, &reg, 1);
  1726. if (rc) {
  1727. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1728. return rc;
  1729. }
  1730. }
  1731. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS
  1732. || type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2)
  1733. && pdata->pull_down_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1734. /* FTSMPS has other bits in the pull down control register. */
  1735. reg = pdata->pull_down_enable
  1736. ? QPNP_COMMON_PULL_DOWN_ENABLE_MASK : 0;
  1737. rc = qpnp_vreg_masked_read_write(vreg,
  1738. QPNP_COMMON_REG_PULL_DOWN, reg,
  1739. QPNP_COMMON_PULL_DOWN_ENABLE_MASK);
  1740. if (rc) {
  1741. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1742. return rc;
  1743. }
  1744. }
  1745. /* Set soft start for LDO. */
  1746. if ((type == QPNP_REGULATOR_LOGICAL_TYPE_LDO
  1747. || type == QPNP_REGULATOR_LOGICAL_TYPE_ULT_LDO)
  1748. && pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1749. reg = pdata->soft_start_enable
  1750. ? QPNP_LDO_SOFT_START_ENABLE_MASK : 0;
  1751. rc = qpnp_vreg_write(vreg, QPNP_LDO_REG_SOFT_START, &reg, 1);
  1752. if (rc) {
  1753. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1754. return rc;
  1755. }
  1756. }
  1757. /* Set soft start strength and over current protection for VS. */
  1758. if (type == QPNP_REGULATOR_LOGICAL_TYPE_VS) {
  1759. reg = 0;
  1760. mask = 0;
  1761. if (pdata->soft_start_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1762. reg |= pdata->soft_start_enable
  1763. ? QPNP_VS_SOFT_START_ENABLE_MASK : 0;
  1764. mask |= QPNP_VS_SOFT_START_ENABLE_MASK;
  1765. }
  1766. if (pdata->vs_soft_start_strength
  1767. != QPNP_VS_SOFT_START_STR_HW_DEFAULT) {
  1768. reg |= pdata->vs_soft_start_strength
  1769. & QPNP_VS_SOFT_START_SEL_MASK;
  1770. mask |= QPNP_VS_SOFT_START_SEL_MASK;
  1771. }
  1772. rc = qpnp_vreg_masked_read_write(vreg, QPNP_VS_REG_SOFT_START,
  1773. reg, mask);
  1774. if (rc) {
  1775. vreg_err(vreg, "spmi write failed, rc=%d\n", rc);
  1776. return rc;
  1777. }
  1778. if (pdata->ocp_enable != QPNP_REGULATOR_USE_HW_DEFAULT) {
  1779. reg = pdata->ocp_enable ? QPNP_VS_OCP_NO_OVERRIDE
  1780. : QPNP_VS_OCP_OVERRIDE;
  1781. rc = qpnp_vreg_write(vreg, QPNP_VS_REG_OCP, &reg, 1);
  1782. if (rc) {
  1783. vreg_err(vreg, "spmi write failed, rc=%d\n",
  1784. rc);
  1785. return rc;
  1786. }
  1787. }
  1788. }
  1789. /* Calculate the slew rate for FTSMPS regulators. */
  1790. if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS) {
  1791. rc = qpnp_regulator_ftsmps_init_slew_rate(vreg);
  1792. if (rc) {
  1793. vreg_err(vreg, "failed to initialize step rate, rc=%d\n",
  1794. rc);
  1795. return rc;
  1796. }
  1797. }
  1798. /* Calculate the slew rate for FTSMPS2 regulators. */
  1799. if (type == QPNP_REGULATOR_LOGICAL_TYPE_FTSMPS2) {
  1800. rc = qpnp_regulator_ftsmps2_init_slew_rate(vreg);
  1801. if (rc) {
  1802. vreg_err(vreg, "failed to initialize step rate, rc=%d\n",
  1803. rc);
  1804. return rc;
  1805. }
  1806. }
  1807. vreg->init_mode = vreg->ctrl_reg[QPNP_COMMON_IDX_MODE];
  1808. return rc;
  1809. }
  1810. /* Fill in pdata elements based on values found in device tree. */
  1811. static int qpnp_regulator_get_dt_config(struct platform_device *pdev,
  1812. struct qpnp_regulator_platform_data *pdata)
  1813. {
  1814. unsigned int base;
  1815. struct device_node *node = pdev->dev.of_node;
  1816. int rc = 0;
  1817. pdata->init_data.constraints.input_uV
  1818. = pdata->init_data.constraints.max_uV;
  1819. rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
  1820. if (rc < 0) {
  1821. dev_err(&pdev->dev,
  1822. "Couldn't find reg in node = %s rc = %d\n",
  1823. pdev->dev.of_node->full_name, rc);
  1824. return rc;
  1825. }
  1826. pdata->base_addr = base;
  1827. /* OCP IRQ is optional so ignore get errors. */
  1828. pdata->ocp_irq = platform_get_irq_byname(pdev, "ocp");
  1829. if (pdata->ocp_irq < 0)
  1830. pdata->ocp_irq = 0;
  1831. /*
  1832. * Initialize configuration parameters to use hardware default in case
  1833. * no value is specified via device tree.
  1834. */
  1835. pdata->auto_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1836. pdata->bypass_mode_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1837. pdata->ocp_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1838. pdata->pull_down_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1839. pdata->soft_start_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1840. pdata->boost_current_limit = QPNP_BOOST_CURRENT_LIMIT_HW_DEFAULT;
  1841. pdata->pin_ctrl_enable = QPNP_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
  1842. pdata->pin_ctrl_hpm = QPNP_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
  1843. pdata->vs_soft_start_strength = QPNP_VS_SOFT_START_STR_HW_DEFAULT;
  1844. pdata->hpm_enable = QPNP_REGULATOR_USE_HW_DEFAULT;
  1845. /* These bindings are optional, so it is okay if they are not found. */
  1846. of_property_read_u32(node, "qcom,auto-mode-enable",
  1847. &pdata->auto_mode_enable);
  1848. of_property_read_u32(node, "qcom,bypass-mode-enable",
  1849. &pdata->bypass_mode_enable);
  1850. of_property_read_u32(node, "qcom,ocp-enable", &pdata->ocp_enable);
  1851. of_property_read_u32(node, "qcom,ocp-max-retries",
  1852. &pdata->ocp_max_retries);
  1853. of_property_read_u32(node, "qcom,ocp-retry-delay",
  1854. &pdata->ocp_retry_delay_ms);
  1855. of_property_read_u32(node, "qcom,pull-down-enable",
  1856. &pdata->pull_down_enable);
  1857. of_property_read_u32(node, "qcom,soft-start-enable",
  1858. &pdata->soft_start_enable);
  1859. of_property_read_u32(node, "qcom,boost-current-limit",
  1860. &pdata->boost_current_limit);
  1861. of_property_read_u32(node, "qcom,pin-ctrl-enable",
  1862. &pdata->pin_ctrl_enable);
  1863. of_property_read_u32(node, "qcom,pin-ctrl-hpm", &pdata->pin_ctrl_hpm);
  1864. of_property_read_u32(node, "qcom,hpm-enable", &pdata->hpm_enable);
  1865. of_property_read_u32(node, "qcom,vs-soft-start-strength",
  1866. &pdata->vs_soft_start_strength);
  1867. of_property_read_u32(node, "qcom,system-load", &pdata->system_load);
  1868. of_property_read_u32(node, "qcom,enable-time", &pdata->enable_time);
  1869. return rc;
  1870. }
  1871. static const struct of_device_id spmi_match_table[];
  1872. #define MAX_NAME_LEN 127
  1873. static int qpnp_regulator_probe(struct platform_device *pdev)
  1874. {
  1875. struct regulator_config reg_config = {};
  1876. struct qpnp_regulator_platform_data *pdata;
  1877. struct qpnp_regulator *vreg;
  1878. struct regulator_desc *rdesc;
  1879. struct qpnp_regulator_platform_data of_pdata;
  1880. struct regulator_init_data *init_data;
  1881. char *reg_name;
  1882. int rc;
  1883. bool is_dt;
  1884. vreg = kzalloc(sizeof(struct qpnp_regulator), GFP_KERNEL);
  1885. if (!vreg)
  1886. return -ENOMEM;
  1887. vreg->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  1888. if (!vreg->regmap) {
  1889. dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
  1890. kfree(vreg);
  1891. return -EINVAL;
  1892. }
  1893. is_dt = of_match_device(spmi_match_table, &pdev->dev);
  1894. /* Check if device tree is in use. */
  1895. if (is_dt) {
  1896. init_data = of_get_regulator_init_data(&pdev->dev,
  1897. pdev->dev.of_node,
  1898. &vreg->rdesc);
  1899. if (!init_data) {
  1900. dev_err(&pdev->dev, "%s: unable to allocate memory\n",
  1901. __func__);
  1902. kfree(vreg);
  1903. return -ENOMEM;
  1904. }
  1905. memset(&of_pdata, 0,
  1906. sizeof(struct qpnp_regulator_platform_data));
  1907. memcpy(&of_pdata.init_data, init_data,
  1908. sizeof(struct regulator_init_data));
  1909. if (of_get_property(pdev->dev.of_node, "parent-supply", NULL))
  1910. of_pdata.init_data.supply_regulator = "parent";
  1911. rc = qpnp_regulator_get_dt_config(pdev, &of_pdata);
  1912. if (rc) {
  1913. dev_err(&pdev->dev, "%s: DT parsing failed, rc=%d\n",
  1914. __func__, rc);
  1915. kfree(vreg);
  1916. return -ENOMEM;
  1917. }
  1918. pdata = &of_pdata;
  1919. } else {
  1920. pdata = pdev->dev.platform_data;
  1921. }
  1922. if (pdata == NULL) {
  1923. dev_err(&pdev->dev, "%s: no platform data specified\n",
  1924. __func__);
  1925. kfree(vreg);
  1926. return -EINVAL;
  1927. }
  1928. vreg->pdev = pdev;
  1929. vreg->prev_write_count = -1;
  1930. vreg->write_count = 0;
  1931. vreg->base_addr = pdata->base_addr;
  1932. vreg->enable_time = pdata->enable_time;
  1933. vreg->system_load = pdata->system_load;
  1934. vreg->ocp_enable = pdata->ocp_enable;
  1935. vreg->ocp_irq = pdata->ocp_irq;
  1936. vreg->ocp_max_retries = pdata->ocp_max_retries;
  1937. vreg->ocp_retry_delay_ms = pdata->ocp_retry_delay_ms;
  1938. if (vreg->ocp_max_retries == 0)
  1939. vreg->ocp_max_retries = QPNP_VS_OCP_DEFAULT_MAX_RETRIES;
  1940. if (vreg->ocp_retry_delay_ms == 0)
  1941. vreg->ocp_retry_delay_ms = QPNP_VS_OCP_DEFAULT_RETRY_DELAY_MS;
  1942. rdesc = &vreg->rdesc;
  1943. rdesc->id = to_spmi_device(pdev->dev.parent)->ctrl->nr;
  1944. rdesc->owner = THIS_MODULE;
  1945. rdesc->type = REGULATOR_VOLTAGE;
  1946. reg_name = kzalloc(strnlen(pdata->init_data.constraints.name,
  1947. MAX_NAME_LEN) + 1, GFP_KERNEL);
  1948. if (!reg_name) {
  1949. kfree(vreg);
  1950. return -ENOMEM;
  1951. }
  1952. strlcpy(reg_name, pdata->init_data.constraints.name,
  1953. strnlen(pdata->init_data.constraints.name, MAX_NAME_LEN) + 1);
  1954. rdesc->name = reg_name;
  1955. dev_set_drvdata(&pdev->dev, vreg);
  1956. rc = qpnp_regulator_match(vreg);
  1957. if (rc)
  1958. goto bail;
  1959. if (is_dt && rdesc->ops) {
  1960. /* Fill in ops and mode masks when using device tree. */
  1961. if (rdesc->ops->enable)
  1962. pdata->init_data.constraints.valid_ops_mask
  1963. |= REGULATOR_CHANGE_STATUS;
  1964. if (rdesc->ops->get_voltage)
  1965. pdata->init_data.constraints.valid_ops_mask
  1966. |= REGULATOR_CHANGE_VOLTAGE;
  1967. if (rdesc->ops->get_mode) {
  1968. pdata->init_data.constraints.valid_ops_mask
  1969. |= REGULATOR_CHANGE_MODE
  1970. | REGULATOR_CHANGE_DRMS;
  1971. pdata->init_data.constraints.valid_modes_mask
  1972. = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
  1973. }
  1974. }
  1975. rc = qpnp_regulator_init_registers(vreg, pdata);
  1976. if (rc) {
  1977. vreg_err(vreg, "common initialization failed, rc=%d\n", rc);
  1978. goto bail;
  1979. }
  1980. if (vreg->logical_type != QPNP_REGULATOR_LOGICAL_TYPE_VS)
  1981. vreg->ocp_irq = 0;
  1982. if (vreg->ocp_irq) {
  1983. rc = devm_request_irq(&pdev->dev, vreg->ocp_irq,
  1984. qpnp_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
  1985. vreg);
  1986. if (rc < 0) {
  1987. vreg_err(vreg, "failed to request irq %d, rc=%d\n",
  1988. vreg->ocp_irq, rc);
  1989. goto bail;
  1990. }
  1991. INIT_DELAYED_WORK(&vreg->ocp_work, qpnp_regulator_vs_ocp_work);
  1992. }
  1993. reg_config.dev = &pdev->dev;
  1994. reg_config.init_data = &pdata->init_data;
  1995. reg_config.driver_data = vreg;
  1996. reg_config.of_node = pdev->dev.of_node;
  1997. vreg->rdev = regulator_register(rdesc, &reg_config);
  1998. if (IS_ERR(vreg->rdev)) {
  1999. rc = PTR_ERR(vreg->rdev);
  2000. if (rc != -EPROBE_DEFER)
  2001. vreg_err(vreg, "regulator_register failed, rc=%d\n",
  2002. rc);
  2003. goto cancel_ocp_work;
  2004. }
  2005. if (qpnp_vreg_debug_mask & QPNP_VREG_DEBUG_INIT && vreg->slew_rate)
  2006. pr_info("%-11s: step rate=%d uV/us\n", vreg->rdesc.name,
  2007. vreg->slew_rate);
  2008. qpnp_vreg_show_state(vreg->rdev, QPNP_REGULATOR_ACTION_INIT);
  2009. return 0;
  2010. cancel_ocp_work:
  2011. if (vreg->ocp_irq)
  2012. cancel_delayed_work_sync(&vreg->ocp_work);
  2013. bail:
  2014. if (rc && rc != -EPROBE_DEFER)
  2015. vreg_err(vreg, "probe failed, rc=%d\n", rc);
  2016. kfree(vreg->rdesc.name);
  2017. kfree(vreg);
  2018. return rc;
  2019. }
  2020. static int qpnp_regulator_remove(struct platform_device *pdev)
  2021. {
  2022. struct qpnp_regulator *vreg;
  2023. vreg = dev_get_drvdata(&pdev->dev);
  2024. dev_set_drvdata(&pdev->dev, NULL);
  2025. if (vreg) {
  2026. regulator_unregister(vreg->rdev);
  2027. if (vreg->ocp_irq)
  2028. cancel_delayed_work_sync(&vreg->ocp_work);
  2029. kfree(vreg->rdesc.name);
  2030. kfree(vreg);
  2031. }
  2032. return 0;
  2033. }
  2034. static const struct of_device_id spmi_match_table[] = {
  2035. { .compatible = QPNP_REGULATOR_DRIVER_NAME, },
  2036. {}
  2037. };
  2038. static const struct platform_device_id qpnp_regulator_id[] = {
  2039. { QPNP_REGULATOR_DRIVER_NAME, 0 },
  2040. { }
  2041. };
  2042. MODULE_DEVICE_TABLE(spmi, qpnp_regulator_id);
  2043. static struct platform_driver qpnp_regulator_driver = {
  2044. .driver = {
  2045. .name = QPNP_REGULATOR_DRIVER_NAME,
  2046. .of_match_table = spmi_match_table,
  2047. .owner = THIS_MODULE,
  2048. },
  2049. .probe = qpnp_regulator_probe,
  2050. .remove = qpnp_regulator_remove,
  2051. .id_table = qpnp_regulator_id,
  2052. };
  2053. /*
  2054. * Pre-compute the number of set points available for each regulator type to
  2055. * avoid unnecessary calculations later in runtime.
  2056. */
  2057. static void qpnp_regulator_set_point_init(void)
  2058. {
  2059. struct qpnp_voltage_set_points **set_points;
  2060. int i, j, temp;
  2061. set_points = all_set_points;
  2062. for (i = 0; i < ARRAY_SIZE(all_set_points); i++) {
  2063. temp = 0;
  2064. for (j = 0; j < all_set_points[i]->count; j++) {
  2065. all_set_points[i]->range[j].n_voltages
  2066. = (all_set_points[i]->range[j].set_point_max_uV
  2067. - all_set_points[i]->range[j].set_point_min_uV)
  2068. / all_set_points[i]->range[j].step_uV + 1;
  2069. if (all_set_points[i]->range[j].set_point_max_uV == 0)
  2070. all_set_points[i]->range[j].n_voltages = 0;
  2071. temp += all_set_points[i]->range[j].n_voltages;
  2072. }
  2073. all_set_points[i]->n_voltages = temp;
  2074. }
  2075. }
  2076. /**
  2077. * qpnp_regulator_init() - register spmi driver for qpnp-regulator
  2078. *
  2079. * This initialization function should be called in systems in which driver
  2080. * registration ordering must be controlled precisely.
  2081. */
  2082. int __init qpnp_regulator_init(void)
  2083. {
  2084. static bool has_registered;
  2085. if (has_registered)
  2086. return 0;
  2087. has_registered = true;
  2088. qpnp_regulator_set_point_init();
  2089. return platform_driver_register(&qpnp_regulator_driver);
  2090. }
  2091. EXPORT_SYMBOL(qpnp_regulator_init);
  2092. static void __exit qpnp_regulator_exit(void)
  2093. {
  2094. platform_driver_unregister(&qpnp_regulator_driver);
  2095. }
  2096. MODULE_DESCRIPTION("QPNP PMIC regulator driver");
  2097. MODULE_LICENSE("GPL v2");
  2098. arch_initcall(qpnp_regulator_init);
  2099. module_exit(qpnp_regulator_exit);