qcom_spmi-regulator.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/kernel.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/bitops.h>
  11. #include <linux/slab.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/ktime.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regmap.h>
  18. #include <linux/list.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/io.h>
  21. /* Pin control enable input pins. */
  22. #define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE 0x00
  23. #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0 0x01
  24. #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1 0x02
  25. #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2 0x04
  26. #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3 0x08
  27. #define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT 0x10
  28. /* Pin control high power mode input pins. */
  29. #define SPMI_REGULATOR_PIN_CTRL_HPM_NONE 0x00
  30. #define SPMI_REGULATOR_PIN_CTRL_HPM_EN0 0x01
  31. #define SPMI_REGULATOR_PIN_CTRL_HPM_EN1 0x02
  32. #define SPMI_REGULATOR_PIN_CTRL_HPM_EN2 0x04
  33. #define SPMI_REGULATOR_PIN_CTRL_HPM_EN3 0x08
  34. #define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B 0x10
  35. #define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT 0x20
  36. /*
  37. * Used with enable parameters to specify that hardware default register values
  38. * should be left unaltered.
  39. */
  40. #define SPMI_REGULATOR_USE_HW_DEFAULT 2
  41. /* Soft start strength of a voltage switch type regulator */
  42. enum spmi_vs_soft_start_str {
  43. SPMI_VS_SOFT_START_STR_0P05_UA = 0,
  44. SPMI_VS_SOFT_START_STR_0P25_UA,
  45. SPMI_VS_SOFT_START_STR_0P55_UA,
  46. SPMI_VS_SOFT_START_STR_0P75_UA,
  47. SPMI_VS_SOFT_START_STR_HW_DEFAULT,
  48. };
  49. /**
  50. * struct spmi_regulator_init_data - spmi-regulator initialization data
  51. * @pin_ctrl_enable: Bit mask specifying which hardware pins should be
  52. * used to enable the regulator, if any
  53. * Value should be an ORing of
  54. * SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants. If
  55. * the bit specified by
  56. * SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is
  57. * set, then pin control enable hardware registers
  58. * will not be modified.
  59. * @pin_ctrl_hpm: Bit mask specifying which hardware pins should be
  60. * used to force the regulator into high power
  61. * mode, if any
  62. * Value should be an ORing of
  63. * SPMI_REGULATOR_PIN_CTRL_HPM_* constants. If
  64. * the bit specified by
  65. * SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is
  66. * set, then pin control mode hardware registers
  67. * will not be modified.
  68. * @vs_soft_start_strength: This parameter sets the soft start strength for
  69. * voltage switch type regulators. Its value
  70. * should be one of SPMI_VS_SOFT_START_STR_*. If
  71. * its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT,
  72. * then the soft start strength will be left at its
  73. * default hardware value.
  74. */
  75. struct spmi_regulator_init_data {
  76. unsigned pin_ctrl_enable;
  77. unsigned pin_ctrl_hpm;
  78. enum spmi_vs_soft_start_str vs_soft_start_strength;
  79. };
  80. /* These types correspond to unique register layouts. */
  81. enum spmi_regulator_logical_type {
  82. SPMI_REGULATOR_LOGICAL_TYPE_SMPS,
  83. SPMI_REGULATOR_LOGICAL_TYPE_LDO,
  84. SPMI_REGULATOR_LOGICAL_TYPE_VS,
  85. SPMI_REGULATOR_LOGICAL_TYPE_BOOST,
  86. SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS,
  87. SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP,
  88. SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO,
  89. SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS,
  90. SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS,
  91. SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO,
  92. SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426,
  93. SPMI_REGULATOR_LOGICAL_TYPE_HFS430,
  94. };
  95. enum spmi_regulator_type {
  96. SPMI_REGULATOR_TYPE_BUCK = 0x03,
  97. SPMI_REGULATOR_TYPE_LDO = 0x04,
  98. SPMI_REGULATOR_TYPE_VS = 0x05,
  99. SPMI_REGULATOR_TYPE_BOOST = 0x1b,
  100. SPMI_REGULATOR_TYPE_FTS = 0x1c,
  101. SPMI_REGULATOR_TYPE_BOOST_BYP = 0x1f,
  102. SPMI_REGULATOR_TYPE_ULT_LDO = 0x21,
  103. SPMI_REGULATOR_TYPE_ULT_BUCK = 0x22,
  104. };
  105. enum spmi_regulator_subtype {
  106. SPMI_REGULATOR_SUBTYPE_GP_CTL = 0x08,
  107. SPMI_REGULATOR_SUBTYPE_RF_CTL = 0x09,
  108. SPMI_REGULATOR_SUBTYPE_N50 = 0x01,
  109. SPMI_REGULATOR_SUBTYPE_N150 = 0x02,
  110. SPMI_REGULATOR_SUBTYPE_N300 = 0x03,
  111. SPMI_REGULATOR_SUBTYPE_N600 = 0x04,
  112. SPMI_REGULATOR_SUBTYPE_N1200 = 0x05,
  113. SPMI_REGULATOR_SUBTYPE_N600_ST = 0x06,
  114. SPMI_REGULATOR_SUBTYPE_N1200_ST = 0x07,
  115. SPMI_REGULATOR_SUBTYPE_N900_ST = 0x14,
  116. SPMI_REGULATOR_SUBTYPE_N300_ST = 0x15,
  117. SPMI_REGULATOR_SUBTYPE_P50 = 0x08,
  118. SPMI_REGULATOR_SUBTYPE_P150 = 0x09,
  119. SPMI_REGULATOR_SUBTYPE_P300 = 0x0a,
  120. SPMI_REGULATOR_SUBTYPE_P600 = 0x0b,
  121. SPMI_REGULATOR_SUBTYPE_P1200 = 0x0c,
  122. SPMI_REGULATOR_SUBTYPE_LN = 0x10,
  123. SPMI_REGULATOR_SUBTYPE_LV_P50 = 0x28,
  124. SPMI_REGULATOR_SUBTYPE_LV_P150 = 0x29,
  125. SPMI_REGULATOR_SUBTYPE_LV_P300 = 0x2a,
  126. SPMI_REGULATOR_SUBTYPE_LV_P600 = 0x2b,
  127. SPMI_REGULATOR_SUBTYPE_LV_P1200 = 0x2c,
  128. SPMI_REGULATOR_SUBTYPE_LV_P450 = 0x2d,
  129. SPMI_REGULATOR_SUBTYPE_LV100 = 0x01,
  130. SPMI_REGULATOR_SUBTYPE_LV300 = 0x02,
  131. SPMI_REGULATOR_SUBTYPE_MV300 = 0x08,
  132. SPMI_REGULATOR_SUBTYPE_MV500 = 0x09,
  133. SPMI_REGULATOR_SUBTYPE_HDMI = 0x10,
  134. SPMI_REGULATOR_SUBTYPE_OTG = 0x11,
  135. SPMI_REGULATOR_SUBTYPE_5V_BOOST = 0x01,
  136. SPMI_REGULATOR_SUBTYPE_FTS_CTL = 0x08,
  137. SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL = 0x09,
  138. SPMI_REGULATOR_SUBTYPE_FTS426_CTL = 0x0a,
  139. SPMI_REGULATOR_SUBTYPE_BB_2A = 0x01,
  140. SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1 = 0x0d,
  141. SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2 = 0x0e,
  142. SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3 = 0x0f,
  143. SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4 = 0x10,
  144. SPMI_REGULATOR_SUBTYPE_HFS430 = 0x0a,
  145. };
  146. enum spmi_common_regulator_registers {
  147. SPMI_COMMON_REG_DIG_MAJOR_REV = 0x01,
  148. SPMI_COMMON_REG_TYPE = 0x04,
  149. SPMI_COMMON_REG_SUBTYPE = 0x05,
  150. SPMI_COMMON_REG_VOLTAGE_RANGE = 0x40,
  151. SPMI_COMMON_REG_VOLTAGE_SET = 0x41,
  152. SPMI_COMMON_REG_MODE = 0x45,
  153. SPMI_COMMON_REG_ENABLE = 0x46,
  154. SPMI_COMMON_REG_PULL_DOWN = 0x48,
  155. SPMI_COMMON_REG_SOFT_START = 0x4c,
  156. SPMI_COMMON_REG_STEP_CTRL = 0x61,
  157. };
  158. /*
  159. * Second common register layout used by newer devices starting with ftsmps426
  160. * Note that some of the registers from the first common layout remain
  161. * unchanged and their definition is not duplicated.
  162. */
  163. enum spmi_ftsmps426_regulator_registers {
  164. SPMI_FTSMPS426_REG_VOLTAGE_LSB = 0x40,
  165. SPMI_FTSMPS426_REG_VOLTAGE_MSB = 0x41,
  166. SPMI_FTSMPS426_REG_VOLTAGE_ULS_LSB = 0x68,
  167. SPMI_FTSMPS426_REG_VOLTAGE_ULS_MSB = 0x69,
  168. };
  169. enum spmi_vs_registers {
  170. SPMI_VS_REG_OCP = 0x4a,
  171. SPMI_VS_REG_SOFT_START = 0x4c,
  172. };
  173. enum spmi_boost_registers {
  174. SPMI_BOOST_REG_CURRENT_LIMIT = 0x4a,
  175. };
  176. enum spmi_boost_byp_registers {
  177. SPMI_BOOST_BYP_REG_CURRENT_LIMIT = 0x4b,
  178. };
  179. enum spmi_saw3_registers {
  180. SAW3_SECURE = 0x00,
  181. SAW3_ID = 0x04,
  182. SAW3_SPM_STS = 0x0C,
  183. SAW3_AVS_STS = 0x10,
  184. SAW3_PMIC_STS = 0x14,
  185. SAW3_RST = 0x18,
  186. SAW3_VCTL = 0x1C,
  187. SAW3_AVS_CTL = 0x20,
  188. SAW3_AVS_LIMIT = 0x24,
  189. SAW3_AVS_DLY = 0x28,
  190. SAW3_AVS_HYSTERESIS = 0x2C,
  191. SAW3_SPM_STS2 = 0x38,
  192. SAW3_SPM_PMIC_DATA_3 = 0x4C,
  193. SAW3_VERSION = 0xFD0,
  194. };
  195. /* Used for indexing into ctrl_reg. These are offets from 0x40 */
  196. enum spmi_common_control_register_index {
  197. SPMI_COMMON_IDX_VOLTAGE_RANGE = 0,
  198. SPMI_COMMON_IDX_VOLTAGE_SET = 1,
  199. SPMI_COMMON_IDX_MODE = 5,
  200. SPMI_COMMON_IDX_ENABLE = 6,
  201. };
  202. /* Common regulator control register layout */
  203. #define SPMI_COMMON_ENABLE_MASK 0x80
  204. #define SPMI_COMMON_ENABLE 0x80
  205. #define SPMI_COMMON_DISABLE 0x00
  206. #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08
  207. #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04
  208. #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02
  209. #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01
  210. #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0f
  211. /* Common regulator mode register layout */
  212. #define SPMI_COMMON_MODE_HPM_MASK 0x80
  213. #define SPMI_COMMON_MODE_AUTO_MASK 0x40
  214. #define SPMI_COMMON_MODE_BYPASS_MASK 0x20
  215. #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10
  216. #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08
  217. #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04
  218. #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02
  219. #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01
  220. #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK 0x1f
  221. #define SPMI_FTSMPS426_MODE_BYPASS_MASK 3
  222. #define SPMI_FTSMPS426_MODE_RETENTION_MASK 4
  223. #define SPMI_FTSMPS426_MODE_LPM_MASK 5
  224. #define SPMI_FTSMPS426_MODE_AUTO_MASK 6
  225. #define SPMI_FTSMPS426_MODE_HPM_MASK 7
  226. #define SPMI_FTSMPS426_MODE_MASK 0x07
  227. /* Common regulator pull down control register layout */
  228. #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK 0x80
  229. /* LDO regulator current limit control register layout */
  230. #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80
  231. /* LDO regulator soft start control register layout */
  232. #define SPMI_LDO_SOFT_START_ENABLE_MASK 0x80
  233. /* VS regulator over current protection control register layout */
  234. #define SPMI_VS_OCP_OVERRIDE 0x01
  235. #define SPMI_VS_OCP_NO_OVERRIDE 0x00
  236. /* VS regulator soft start control register layout */
  237. #define SPMI_VS_SOFT_START_ENABLE_MASK 0x80
  238. #define SPMI_VS_SOFT_START_SEL_MASK 0x03
  239. /* Boost regulator current limit control register layout */
  240. #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80
  241. #define SPMI_BOOST_CURRENT_LIMIT_MASK 0x07
  242. #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES 10
  243. #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS 30
  244. #define SPMI_VS_OCP_FALL_DELAY_US 90
  245. #define SPMI_VS_OCP_FAULT_DELAY_US 20000
  246. #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK 0x18
  247. #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT 3
  248. #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK 0x07
  249. #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT 0
  250. /* Clock rate in kHz of the FTSMPS regulator reference clock. */
  251. #define SPMI_FTSMPS_CLOCK_RATE 19200
  252. /* Minimum voltage stepper delay for each step. */
  253. #define SPMI_FTSMPS_STEP_DELAY 8
  254. #define SPMI_DEFAULT_STEP_DELAY 20
  255. /*
  256. * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to
  257. * adjust the step rate in order to account for oscillator variance.
  258. */
  259. #define SPMI_FTSMPS_STEP_MARGIN_NUM 4
  260. #define SPMI_FTSMPS_STEP_MARGIN_DEN 5
  261. #define SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK 0x03
  262. #define SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT 0
  263. /* Clock rate in kHz of the FTSMPS426 regulator reference clock. */
  264. #define SPMI_FTSMPS426_CLOCK_RATE 4800
  265. #define SPMI_HFS430_CLOCK_RATE 1600
  266. /* Minimum voltage stepper delay for each step. */
  267. #define SPMI_FTSMPS426_STEP_DELAY 2
  268. /*
  269. * The ratio SPMI_FTSMPS426_STEP_MARGIN_NUM/SPMI_FTSMPS426_STEP_MARGIN_DEN is
  270. * used to adjust the step rate in order to account for oscillator variance.
  271. */
  272. #define SPMI_FTSMPS426_STEP_MARGIN_NUM 10
  273. #define SPMI_FTSMPS426_STEP_MARGIN_DEN 11
  274. /* VSET value to decide the range of ULT SMPS */
  275. #define ULT_SMPS_RANGE_SPLIT 0x60
  276. /**
  277. * struct spmi_voltage_range - regulator set point voltage mapping description
  278. * @min_uV: Minimum programmable output voltage resulting from
  279. * set point register value 0x00
  280. * @max_uV: Maximum programmable output voltage
  281. * @step_uV: Output voltage increase resulting from the set point
  282. * register value increasing by 1
  283. * @set_point_min_uV: Minimum allowed voltage
  284. * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order
  285. * to pick which range should be used in the case of
  286. * overlapping set points.
  287. * @n_voltages: Number of preferred voltage set points present in this
  288. * range
  289. * @range_sel: Voltage range register value corresponding to this range
  290. *
  291. * The following relationships must be true for the values used in this struct:
  292. * (max_uV - min_uV) % step_uV == 0
  293. * (set_point_min_uV - min_uV) % step_uV == 0*
  294. * (set_point_max_uV - min_uV) % step_uV == 0*
  295. * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1
  296. *
  297. * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to
  298. * specify that the voltage range has meaning, but is not preferred.
  299. */
  300. struct spmi_voltage_range {
  301. int min_uV;
  302. int max_uV;
  303. int step_uV;
  304. int set_point_min_uV;
  305. int set_point_max_uV;
  306. unsigned n_voltages;
  307. u8 range_sel;
  308. };
  309. /*
  310. * The ranges specified in the spmi_voltage_set_points struct must be listed
  311. * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV.
  312. */
  313. struct spmi_voltage_set_points {
  314. struct spmi_voltage_range *range;
  315. int count;
  316. unsigned n_voltages;
  317. };
  318. struct spmi_regulator {
  319. struct regulator_desc desc;
  320. struct device *dev;
  321. struct delayed_work ocp_work;
  322. struct regmap *regmap;
  323. struct spmi_voltage_set_points *set_points;
  324. enum spmi_regulator_logical_type logical_type;
  325. int ocp_irq;
  326. int ocp_count;
  327. int ocp_max_retries;
  328. int ocp_retry_delay_ms;
  329. int hpm_min_load;
  330. int slew_rate;
  331. ktime_t vs_enable_time;
  332. u16 base;
  333. struct list_head node;
  334. };
  335. struct spmi_regulator_mapping {
  336. enum spmi_regulator_type type;
  337. enum spmi_regulator_subtype subtype;
  338. enum spmi_regulator_logical_type logical_type;
  339. u32 revision_min;
  340. u32 revision_max;
  341. struct regulator_ops *ops;
  342. struct spmi_voltage_set_points *set_points;
  343. int hpm_min_load;
  344. };
  345. struct spmi_regulator_data {
  346. const char *name;
  347. u16 base;
  348. const char *supply;
  349. const char *ocp;
  350. u16 force_type;
  351. };
  352. #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \
  353. _logical_type, _ops_val, _set_points_val, _hpm_min_load) \
  354. { \
  355. .type = SPMI_REGULATOR_TYPE_##_type, \
  356. .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
  357. .revision_min = _dig_major_min, \
  358. .revision_max = _dig_major_max, \
  359. .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \
  360. .ops = &spmi_##_ops_val##_ops, \
  361. .set_points = &_set_points_val##_set_points, \
  362. .hpm_min_load = _hpm_min_load, \
  363. }
  364. #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \
  365. { \
  366. .type = SPMI_REGULATOR_TYPE_VS, \
  367. .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \
  368. .revision_min = _dig_major_min, \
  369. .revision_max = _dig_major_max, \
  370. .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_VS, \
  371. .ops = &spmi_vs_ops, \
  372. }
  373. #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \
  374. _set_point_max_uV, _max_uV, _step_uV) \
  375. { \
  376. .min_uV = _min_uV, \
  377. .max_uV = _max_uV, \
  378. .set_point_min_uV = _set_point_min_uV, \
  379. .set_point_max_uV = _set_point_max_uV, \
  380. .step_uV = _step_uV, \
  381. .range_sel = _range_sel, \
  382. }
  383. #define DEFINE_SPMI_SET_POINTS(name) \
  384. struct spmi_voltage_set_points name##_set_points = { \
  385. .range = name##_ranges, \
  386. .count = ARRAY_SIZE(name##_ranges), \
  387. }
  388. /*
  389. * These tables contain the physically available PMIC regulator voltage setpoint
  390. * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed
  391. * to ensure that the setpoints available to software are monotonically
  392. * increasing and unique. The set_voltage callback functions expect these
  393. * properties to hold.
  394. */
  395. static struct spmi_voltage_range pldo_ranges[] = {
  396. SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
  397. SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000),
  398. SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000),
  399. };
  400. static struct spmi_voltage_range nldo1_ranges[] = {
  401. SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500),
  402. };
  403. static struct spmi_voltage_range nldo2_ranges[] = {
  404. SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500),
  405. SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250),
  406. SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500),
  407. };
  408. static struct spmi_voltage_range nldo3_ranges[] = {
  409. SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
  410. SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500),
  411. SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500),
  412. };
  413. static struct spmi_voltage_range ln_ldo_ranges[] = {
  414. SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000),
  415. SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000),
  416. };
  417. static struct spmi_voltage_range smps_ranges[] = {
  418. SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
  419. SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000),
  420. };
  421. static struct spmi_voltage_range ftsmps_ranges[] = {
  422. SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000),
  423. SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000),
  424. };
  425. static struct spmi_voltage_range ftsmps2p5_ranges[] = {
  426. SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000),
  427. SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000),
  428. };
  429. static struct spmi_voltage_range ftsmps426_ranges[] = {
  430. SPMI_VOLTAGE_RANGE(0, 0, 320000, 1352000, 1352000, 4000),
  431. };
  432. static struct spmi_voltage_range boost_ranges[] = {
  433. SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000),
  434. };
  435. static struct spmi_voltage_range boost_byp_ranges[] = {
  436. SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000),
  437. };
  438. static struct spmi_voltage_range ult_lo_smps_ranges[] = {
  439. SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500),
  440. SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000),
  441. };
  442. static struct spmi_voltage_range ult_ho_smps_ranges[] = {
  443. SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000),
  444. };
  445. static struct spmi_voltage_range ult_nldo_ranges[] = {
  446. SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500),
  447. };
  448. static struct spmi_voltage_range ult_pldo_ranges[] = {
  449. SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500),
  450. };
  451. static struct spmi_voltage_range hfs430_ranges[] = {
  452. SPMI_VOLTAGE_RANGE(0, 320000, 320000, 2040000, 2040000, 8000),
  453. };
  454. static DEFINE_SPMI_SET_POINTS(pldo);
  455. static DEFINE_SPMI_SET_POINTS(nldo1);
  456. static DEFINE_SPMI_SET_POINTS(nldo2);
  457. static DEFINE_SPMI_SET_POINTS(nldo3);
  458. static DEFINE_SPMI_SET_POINTS(ln_ldo);
  459. static DEFINE_SPMI_SET_POINTS(smps);
  460. static DEFINE_SPMI_SET_POINTS(ftsmps);
  461. static DEFINE_SPMI_SET_POINTS(ftsmps2p5);
  462. static DEFINE_SPMI_SET_POINTS(ftsmps426);
  463. static DEFINE_SPMI_SET_POINTS(boost);
  464. static DEFINE_SPMI_SET_POINTS(boost_byp);
  465. static DEFINE_SPMI_SET_POINTS(ult_lo_smps);
  466. static DEFINE_SPMI_SET_POINTS(ult_ho_smps);
  467. static DEFINE_SPMI_SET_POINTS(ult_nldo);
  468. static DEFINE_SPMI_SET_POINTS(ult_pldo);
  469. static DEFINE_SPMI_SET_POINTS(hfs430);
  470. static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf,
  471. int len)
  472. {
  473. return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len);
  474. }
  475. static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr,
  476. u8 *buf, int len)
  477. {
  478. return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len);
  479. }
  480. static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val,
  481. u8 mask)
  482. {
  483. return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val);
  484. }
  485. static int spmi_regulator_vs_enable(struct regulator_dev *rdev)
  486. {
  487. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  488. if (vreg->ocp_irq) {
  489. vreg->ocp_count = 0;
  490. vreg->vs_enable_time = ktime_get();
  491. }
  492. return regulator_enable_regmap(rdev);
  493. }
  494. static int spmi_regulator_vs_ocp(struct regulator_dev *rdev)
  495. {
  496. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  497. u8 reg = SPMI_VS_OCP_OVERRIDE;
  498. return spmi_vreg_write(vreg, SPMI_VS_REG_OCP, &reg, 1);
  499. }
  500. static int spmi_regulator_select_voltage(struct spmi_regulator *vreg,
  501. int min_uV, int max_uV)
  502. {
  503. const struct spmi_voltage_range *range;
  504. int uV = min_uV;
  505. int lim_min_uV, lim_max_uV, i, range_id, range_max_uV;
  506. int selector, voltage_sel;
  507. /* Check if request voltage is outside of physically settable range. */
  508. lim_min_uV = vreg->set_points->range[0].set_point_min_uV;
  509. lim_max_uV =
  510. vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV;
  511. if (uV < lim_min_uV && max_uV >= lim_min_uV)
  512. uV = lim_min_uV;
  513. if (uV < lim_min_uV || uV > lim_max_uV) {
  514. dev_err(vreg->dev,
  515. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  516. min_uV, max_uV, lim_min_uV, lim_max_uV);
  517. return -EINVAL;
  518. }
  519. /* Find the range which uV is inside of. */
  520. for (i = vreg->set_points->count - 1; i > 0; i--) {
  521. range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV;
  522. if (uV > range_max_uV && range_max_uV > 0)
  523. break;
  524. }
  525. range_id = i;
  526. range = &vreg->set_points->range[range_id];
  527. /*
  528. * Force uV to be an allowed set point by applying a ceiling function to
  529. * the uV value.
  530. */
  531. voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
  532. uV = voltage_sel * range->step_uV + range->min_uV;
  533. if (uV > max_uV) {
  534. dev_err(vreg->dev,
  535. "request v=[%d, %d] cannot be met by any set point; "
  536. "next set point: %d\n",
  537. min_uV, max_uV, uV);
  538. return -EINVAL;
  539. }
  540. selector = 0;
  541. for (i = 0; i < range_id; i++)
  542. selector += vreg->set_points->range[i].n_voltages;
  543. selector += (uV - range->set_point_min_uV) / range->step_uV;
  544. return selector;
  545. }
  546. static int spmi_sw_selector_to_hw(struct spmi_regulator *vreg,
  547. unsigned selector, u8 *range_sel,
  548. u8 *voltage_sel)
  549. {
  550. const struct spmi_voltage_range *range, *end;
  551. unsigned offset;
  552. range = vreg->set_points->range;
  553. end = range + vreg->set_points->count;
  554. for (; range < end; range++) {
  555. if (selector < range->n_voltages) {
  556. /*
  557. * hardware selectors between set point min and real
  558. * min are invalid so we ignore them
  559. */
  560. offset = range->set_point_min_uV - range->min_uV;
  561. offset /= range->step_uV;
  562. *voltage_sel = selector + offset;
  563. *range_sel = range->range_sel;
  564. return 0;
  565. }
  566. selector -= range->n_voltages;
  567. }
  568. return -EINVAL;
  569. }
  570. static int spmi_hw_selector_to_sw(struct spmi_regulator *vreg, u8 hw_sel,
  571. const struct spmi_voltage_range *range)
  572. {
  573. unsigned sw_sel = 0;
  574. unsigned offset, max_hw_sel;
  575. const struct spmi_voltage_range *r = vreg->set_points->range;
  576. const struct spmi_voltage_range *end = r + vreg->set_points->count;
  577. for (; r < end; r++) {
  578. if (r == range && range->n_voltages) {
  579. /*
  580. * hardware selectors between set point min and real
  581. * min and between set point max and real max are
  582. * invalid so we return an error if they're
  583. * programmed into the hardware
  584. */
  585. offset = range->set_point_min_uV - range->min_uV;
  586. offset /= range->step_uV;
  587. if (hw_sel < offset)
  588. return -EINVAL;
  589. max_hw_sel = range->set_point_max_uV - range->min_uV;
  590. max_hw_sel /= range->step_uV;
  591. if (hw_sel > max_hw_sel)
  592. return -EINVAL;
  593. return sw_sel + hw_sel - offset;
  594. }
  595. sw_sel += r->n_voltages;
  596. }
  597. return -EINVAL;
  598. }
  599. static const struct spmi_voltage_range *
  600. spmi_regulator_find_range(struct spmi_regulator *vreg)
  601. {
  602. u8 range_sel;
  603. const struct spmi_voltage_range *range, *end;
  604. range = vreg->set_points->range;
  605. end = range + vreg->set_points->count;
  606. spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1);
  607. for (; range < end; range++)
  608. if (range->range_sel == range_sel)
  609. return range;
  610. return NULL;
  611. }
  612. static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg,
  613. int min_uV, int max_uV)
  614. {
  615. const struct spmi_voltage_range *range;
  616. int uV = min_uV;
  617. int i, selector;
  618. range = spmi_regulator_find_range(vreg);
  619. if (!range)
  620. goto different_range;
  621. if (uV < range->min_uV && max_uV >= range->min_uV)
  622. uV = range->min_uV;
  623. if (uV < range->min_uV || uV > range->max_uV) {
  624. /* Current range doesn't support the requested voltage. */
  625. goto different_range;
  626. }
  627. /*
  628. * Force uV to be an allowed set point by applying a ceiling function to
  629. * the uV value.
  630. */
  631. uV = DIV_ROUND_UP(uV - range->min_uV, range->step_uV);
  632. uV = uV * range->step_uV + range->min_uV;
  633. if (uV > max_uV) {
  634. /*
  635. * No set point in the current voltage range is within the
  636. * requested min_uV to max_uV range.
  637. */
  638. goto different_range;
  639. }
  640. selector = 0;
  641. for (i = 0; i < vreg->set_points->count; i++) {
  642. if (uV >= vreg->set_points->range[i].set_point_min_uV
  643. && uV <= vreg->set_points->range[i].set_point_max_uV) {
  644. selector +=
  645. (uV - vreg->set_points->range[i].set_point_min_uV)
  646. / vreg->set_points->range[i].step_uV;
  647. break;
  648. }
  649. selector += vreg->set_points->range[i].n_voltages;
  650. }
  651. if (selector >= vreg->set_points->n_voltages)
  652. goto different_range;
  653. return selector;
  654. different_range:
  655. return spmi_regulator_select_voltage(vreg, min_uV, max_uV);
  656. }
  657. static int spmi_regulator_common_map_voltage(struct regulator_dev *rdev,
  658. int min_uV, int max_uV)
  659. {
  660. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  661. /*
  662. * Favor staying in the current voltage range if possible. This avoids
  663. * voltage spikes that occur when changing the voltage range.
  664. */
  665. return spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV);
  666. }
  667. static int
  668. spmi_regulator_common_set_voltage(struct regulator_dev *rdev, unsigned selector)
  669. {
  670. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  671. int ret;
  672. u8 buf[2];
  673. u8 range_sel, voltage_sel;
  674. ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
  675. if (ret)
  676. return ret;
  677. buf[0] = range_sel;
  678. buf[1] = voltage_sel;
  679. return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2);
  680. }
  681. static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev,
  682. unsigned selector);
  683. static int spmi_regulator_ftsmps426_set_voltage(struct regulator_dev *rdev,
  684. unsigned selector)
  685. {
  686. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  687. u8 buf[2];
  688. int mV;
  689. mV = spmi_regulator_common_list_voltage(rdev, selector) / 1000;
  690. buf[0] = mV & 0xff;
  691. buf[1] = mV >> 8;
  692. return spmi_vreg_write(vreg, SPMI_FTSMPS426_REG_VOLTAGE_LSB, buf, 2);
  693. }
  694. static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
  695. unsigned int old_selector, unsigned int new_selector)
  696. {
  697. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  698. int diff_uV;
  699. diff_uV = abs(spmi_regulator_common_list_voltage(rdev, new_selector) -
  700. spmi_regulator_common_list_voltage(rdev, old_selector));
  701. return DIV_ROUND_UP(diff_uV, vreg->slew_rate);
  702. }
  703. static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev)
  704. {
  705. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  706. const struct spmi_voltage_range *range;
  707. u8 voltage_sel;
  708. spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
  709. range = spmi_regulator_find_range(vreg);
  710. if (!range)
  711. return -EINVAL;
  712. return spmi_hw_selector_to_sw(vreg, voltage_sel, range);
  713. }
  714. static int spmi_regulator_ftsmps426_get_voltage(struct regulator_dev *rdev)
  715. {
  716. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  717. const struct spmi_voltage_range *range;
  718. u8 buf[2];
  719. int uV;
  720. spmi_vreg_read(vreg, SPMI_FTSMPS426_REG_VOLTAGE_LSB, buf, 2);
  721. uV = (((unsigned int)buf[1] << 8) | (unsigned int)buf[0]) * 1000;
  722. range = vreg->set_points->range;
  723. return (uV - range->set_point_min_uV) / range->step_uV;
  724. }
  725. static int spmi_regulator_single_map_voltage(struct regulator_dev *rdev,
  726. int min_uV, int max_uV)
  727. {
  728. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  729. return spmi_regulator_select_voltage(vreg, min_uV, max_uV);
  730. }
  731. static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev,
  732. unsigned selector)
  733. {
  734. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  735. u8 sel = selector;
  736. /*
  737. * Certain types of regulators do not have a range select register so
  738. * only voltage set register needs to be written.
  739. */
  740. return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1);
  741. }
  742. static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev)
  743. {
  744. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  745. u8 selector;
  746. int ret;
  747. ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &selector, 1);
  748. if (ret)
  749. return ret;
  750. return selector;
  751. }
  752. static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev,
  753. unsigned selector)
  754. {
  755. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  756. int ret;
  757. u8 range_sel, voltage_sel;
  758. ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
  759. if (ret)
  760. return ret;
  761. /*
  762. * Calculate VSET based on range
  763. * In case of range 0: voltage_sel is a 7 bit value, can be written
  764. * witout any modification.
  765. * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to
  766. * [011].
  767. */
  768. if (range_sel == 1)
  769. voltage_sel |= ULT_SMPS_RANGE_SPLIT;
  770. return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET,
  771. voltage_sel, 0xff);
  772. }
  773. static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev)
  774. {
  775. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  776. const struct spmi_voltage_range *range;
  777. u8 voltage_sel;
  778. spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1);
  779. range = spmi_regulator_find_range(vreg);
  780. if (!range)
  781. return -EINVAL;
  782. if (range->range_sel == 1)
  783. voltage_sel &= ~ULT_SMPS_RANGE_SPLIT;
  784. return spmi_hw_selector_to_sw(vreg, voltage_sel, range);
  785. }
  786. static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev,
  787. unsigned selector)
  788. {
  789. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  790. int uV = 0;
  791. int i;
  792. if (selector >= vreg->set_points->n_voltages)
  793. return 0;
  794. for (i = 0; i < vreg->set_points->count; i++) {
  795. if (selector < vreg->set_points->range[i].n_voltages) {
  796. uV = selector * vreg->set_points->range[i].step_uV
  797. + vreg->set_points->range[i].set_point_min_uV;
  798. break;
  799. }
  800. selector -= vreg->set_points->range[i].n_voltages;
  801. }
  802. return uV;
  803. }
  804. static int
  805. spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable)
  806. {
  807. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  808. u8 mask = SPMI_COMMON_MODE_BYPASS_MASK;
  809. u8 val = 0;
  810. if (enable)
  811. val = mask;
  812. return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
  813. }
  814. static int
  815. spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable)
  816. {
  817. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  818. u8 val;
  819. int ret;
  820. ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1);
  821. *enable = val & SPMI_COMMON_MODE_BYPASS_MASK;
  822. return ret;
  823. }
  824. static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev)
  825. {
  826. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  827. u8 reg;
  828. spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &reg, 1);
  829. reg &= SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK;
  830. switch (reg) {
  831. case SPMI_COMMON_MODE_HPM_MASK:
  832. return REGULATOR_MODE_NORMAL;
  833. case SPMI_COMMON_MODE_AUTO_MASK:
  834. return REGULATOR_MODE_FAST;
  835. default:
  836. return REGULATOR_MODE_IDLE;
  837. }
  838. }
  839. static unsigned int spmi_regulator_ftsmps426_get_mode(struct regulator_dev *rdev)
  840. {
  841. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  842. u8 reg;
  843. spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &reg, 1);
  844. switch (reg) {
  845. case SPMI_FTSMPS426_MODE_HPM_MASK:
  846. return REGULATOR_MODE_NORMAL;
  847. case SPMI_FTSMPS426_MODE_AUTO_MASK:
  848. return REGULATOR_MODE_FAST;
  849. default:
  850. return REGULATOR_MODE_IDLE;
  851. }
  852. }
  853. static int
  854. spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode)
  855. {
  856. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  857. u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK;
  858. u8 val;
  859. switch (mode) {
  860. case REGULATOR_MODE_NORMAL:
  861. val = SPMI_COMMON_MODE_HPM_MASK;
  862. break;
  863. case REGULATOR_MODE_FAST:
  864. val = SPMI_COMMON_MODE_AUTO_MASK;
  865. break;
  866. default:
  867. val = 0;
  868. break;
  869. }
  870. return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
  871. }
  872. static int
  873. spmi_regulator_ftsmps426_set_mode(struct regulator_dev *rdev, unsigned int mode)
  874. {
  875. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  876. u8 mask = SPMI_FTSMPS426_MODE_MASK;
  877. u8 val;
  878. switch (mode) {
  879. case REGULATOR_MODE_NORMAL:
  880. val = SPMI_FTSMPS426_MODE_HPM_MASK;
  881. break;
  882. case REGULATOR_MODE_FAST:
  883. val = SPMI_FTSMPS426_MODE_AUTO_MASK;
  884. break;
  885. case REGULATOR_MODE_IDLE:
  886. val = SPMI_FTSMPS426_MODE_LPM_MASK;
  887. break;
  888. default:
  889. return -EINVAL;
  890. }
  891. return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask);
  892. }
  893. static int
  894. spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA)
  895. {
  896. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  897. unsigned int mode;
  898. if (load_uA >= vreg->hpm_min_load)
  899. mode = REGULATOR_MODE_NORMAL;
  900. else
  901. mode = REGULATOR_MODE_IDLE;
  902. return spmi_regulator_common_set_mode(rdev, mode);
  903. }
  904. static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev)
  905. {
  906. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  907. unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK;
  908. return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN,
  909. mask, mask);
  910. }
  911. static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev)
  912. {
  913. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  914. unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK;
  915. return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START,
  916. mask, mask);
  917. }
  918. static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA)
  919. {
  920. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  921. enum spmi_regulator_logical_type type = vreg->logical_type;
  922. unsigned int current_reg;
  923. u8 reg;
  924. u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK |
  925. SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
  926. int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500;
  927. if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST)
  928. current_reg = SPMI_BOOST_REG_CURRENT_LIMIT;
  929. else
  930. current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT;
  931. if (ilim_uA > max || ilim_uA <= 0)
  932. return -EINVAL;
  933. reg = (ilim_uA - 1) / 500;
  934. reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK;
  935. return spmi_vreg_update_bits(vreg, current_reg, reg, mask);
  936. }
  937. static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg)
  938. {
  939. int ret;
  940. ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
  941. SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK);
  942. vreg->vs_enable_time = ktime_get();
  943. ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE,
  944. SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK);
  945. return ret;
  946. }
  947. static void spmi_regulator_vs_ocp_work(struct work_struct *work)
  948. {
  949. struct delayed_work *dwork = to_delayed_work(work);
  950. struct spmi_regulator *vreg
  951. = container_of(dwork, struct spmi_regulator, ocp_work);
  952. spmi_regulator_vs_clear_ocp(vreg);
  953. }
  954. static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data)
  955. {
  956. struct spmi_regulator *vreg = data;
  957. ktime_t ocp_irq_time;
  958. s64 ocp_trigger_delay_us;
  959. ocp_irq_time = ktime_get();
  960. ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time,
  961. vreg->vs_enable_time);
  962. /*
  963. * Reset the OCP count if there is a large delay between switch enable
  964. * and when OCP triggers. This is indicative of a hotplug event as
  965. * opposed to a fault.
  966. */
  967. if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US)
  968. vreg->ocp_count = 0;
  969. /* Wait for switch output to settle back to 0 V after OCP triggered. */
  970. udelay(SPMI_VS_OCP_FALL_DELAY_US);
  971. vreg->ocp_count++;
  972. if (vreg->ocp_count == 1) {
  973. /* Immediately clear the over current condition. */
  974. spmi_regulator_vs_clear_ocp(vreg);
  975. } else if (vreg->ocp_count <= vreg->ocp_max_retries) {
  976. /* Schedule the over current clear task to run later. */
  977. schedule_delayed_work(&vreg->ocp_work,
  978. msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1);
  979. } else {
  980. dev_err(vreg->dev,
  981. "OCP triggered %d times; no further retries\n",
  982. vreg->ocp_count);
  983. }
  984. return IRQ_HANDLED;
  985. }
  986. #define SAW3_VCTL_DATA_MASK 0xFF
  987. #define SAW3_VCTL_CLEAR_MASK 0x700FF
  988. #define SAW3_AVS_CTL_EN_MASK 0x1
  989. #define SAW3_AVS_CTL_TGGL_MASK 0x8000000
  990. #define SAW3_AVS_CTL_CLEAR_MASK 0x7efc00
  991. static struct regmap *saw_regmap;
  992. static void spmi_saw_set_vdd(void *data)
  993. {
  994. u32 vctl, data3, avs_ctl, pmic_sts;
  995. bool avs_enabled = false;
  996. unsigned long timeout;
  997. u8 voltage_sel = *(u8 *)data;
  998. regmap_read(saw_regmap, SAW3_AVS_CTL, &avs_ctl);
  999. regmap_read(saw_regmap, SAW3_VCTL, &vctl);
  1000. regmap_read(saw_regmap, SAW3_SPM_PMIC_DATA_3, &data3);
  1001. /* select the band */
  1002. vctl &= ~SAW3_VCTL_CLEAR_MASK;
  1003. vctl |= (u32)voltage_sel;
  1004. data3 &= ~SAW3_VCTL_CLEAR_MASK;
  1005. data3 |= (u32)voltage_sel;
  1006. /* If AVS is enabled, switch it off during the voltage change */
  1007. avs_enabled = SAW3_AVS_CTL_EN_MASK & avs_ctl;
  1008. if (avs_enabled) {
  1009. avs_ctl &= ~SAW3_AVS_CTL_TGGL_MASK;
  1010. regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl);
  1011. }
  1012. regmap_write(saw_regmap, SAW3_RST, 1);
  1013. regmap_write(saw_regmap, SAW3_VCTL, vctl);
  1014. regmap_write(saw_regmap, SAW3_SPM_PMIC_DATA_3, data3);
  1015. timeout = jiffies + usecs_to_jiffies(100);
  1016. do {
  1017. regmap_read(saw_regmap, SAW3_PMIC_STS, &pmic_sts);
  1018. pmic_sts &= SAW3_VCTL_DATA_MASK;
  1019. if (pmic_sts == (u32)voltage_sel)
  1020. break;
  1021. cpu_relax();
  1022. } while (time_before(jiffies, timeout));
  1023. /* After successful voltage change, switch the AVS back on */
  1024. if (avs_enabled) {
  1025. pmic_sts &= 0x3f;
  1026. avs_ctl &= ~SAW3_AVS_CTL_CLEAR_MASK;
  1027. avs_ctl |= ((pmic_sts - 4) << 10);
  1028. avs_ctl |= (pmic_sts << 17);
  1029. avs_ctl |= SAW3_AVS_CTL_TGGL_MASK;
  1030. regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl);
  1031. }
  1032. }
  1033. static int
  1034. spmi_regulator_saw_set_voltage(struct regulator_dev *rdev, unsigned selector)
  1035. {
  1036. struct spmi_regulator *vreg = rdev_get_drvdata(rdev);
  1037. int ret;
  1038. u8 range_sel, voltage_sel;
  1039. ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel);
  1040. if (ret)
  1041. return ret;
  1042. if (0 != range_sel) {
  1043. dev_dbg(&rdev->dev, "range_sel = %02X voltage_sel = %02X", \
  1044. range_sel, voltage_sel);
  1045. return -EINVAL;
  1046. }
  1047. /* Always do the SAW register writes on the first CPU */
  1048. return smp_call_function_single(0, spmi_saw_set_vdd, \
  1049. &voltage_sel, true);
  1050. }
  1051. static struct regulator_ops spmi_saw_ops = {};
  1052. static struct regulator_ops spmi_smps_ops = {
  1053. .enable = regulator_enable_regmap,
  1054. .disable = regulator_disable_regmap,
  1055. .is_enabled = regulator_is_enabled_regmap,
  1056. .set_voltage_sel = spmi_regulator_common_set_voltage,
  1057. .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
  1058. .get_voltage_sel = spmi_regulator_common_get_voltage,
  1059. .map_voltage = spmi_regulator_common_map_voltage,
  1060. .list_voltage = spmi_regulator_common_list_voltage,
  1061. .set_mode = spmi_regulator_common_set_mode,
  1062. .get_mode = spmi_regulator_common_get_mode,
  1063. .set_load = spmi_regulator_common_set_load,
  1064. .set_pull_down = spmi_regulator_common_set_pull_down,
  1065. };
  1066. static struct regulator_ops spmi_ldo_ops = {
  1067. .enable = regulator_enable_regmap,
  1068. .disable = regulator_disable_regmap,
  1069. .is_enabled = regulator_is_enabled_regmap,
  1070. .set_voltage_sel = spmi_regulator_common_set_voltage,
  1071. .get_voltage_sel = spmi_regulator_common_get_voltage,
  1072. .map_voltage = spmi_regulator_common_map_voltage,
  1073. .list_voltage = spmi_regulator_common_list_voltage,
  1074. .set_mode = spmi_regulator_common_set_mode,
  1075. .get_mode = spmi_regulator_common_get_mode,
  1076. .set_load = spmi_regulator_common_set_load,
  1077. .set_bypass = spmi_regulator_common_set_bypass,
  1078. .get_bypass = spmi_regulator_common_get_bypass,
  1079. .set_pull_down = spmi_regulator_common_set_pull_down,
  1080. .set_soft_start = spmi_regulator_common_set_soft_start,
  1081. };
  1082. static struct regulator_ops spmi_ln_ldo_ops = {
  1083. .enable = regulator_enable_regmap,
  1084. .disable = regulator_disable_regmap,
  1085. .is_enabled = regulator_is_enabled_regmap,
  1086. .set_voltage_sel = spmi_regulator_common_set_voltage,
  1087. .get_voltage_sel = spmi_regulator_common_get_voltage,
  1088. .map_voltage = spmi_regulator_common_map_voltage,
  1089. .list_voltage = spmi_regulator_common_list_voltage,
  1090. .set_bypass = spmi_regulator_common_set_bypass,
  1091. .get_bypass = spmi_regulator_common_get_bypass,
  1092. };
  1093. static struct regulator_ops spmi_vs_ops = {
  1094. .enable = spmi_regulator_vs_enable,
  1095. .disable = regulator_disable_regmap,
  1096. .is_enabled = regulator_is_enabled_regmap,
  1097. .set_pull_down = spmi_regulator_common_set_pull_down,
  1098. .set_soft_start = spmi_regulator_common_set_soft_start,
  1099. .set_over_current_protection = spmi_regulator_vs_ocp,
  1100. .set_mode = spmi_regulator_common_set_mode,
  1101. .get_mode = spmi_regulator_common_get_mode,
  1102. };
  1103. static struct regulator_ops spmi_boost_ops = {
  1104. .enable = regulator_enable_regmap,
  1105. .disable = regulator_disable_regmap,
  1106. .is_enabled = regulator_is_enabled_regmap,
  1107. .set_voltage_sel = spmi_regulator_single_range_set_voltage,
  1108. .get_voltage_sel = spmi_regulator_single_range_get_voltage,
  1109. .map_voltage = spmi_regulator_single_map_voltage,
  1110. .list_voltage = spmi_regulator_common_list_voltage,
  1111. .set_input_current_limit = spmi_regulator_set_ilim,
  1112. };
  1113. static struct regulator_ops spmi_ftsmps_ops = {
  1114. .enable = regulator_enable_regmap,
  1115. .disable = regulator_disable_regmap,
  1116. .is_enabled = regulator_is_enabled_regmap,
  1117. .set_voltage_sel = spmi_regulator_common_set_voltage,
  1118. .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
  1119. .get_voltage_sel = spmi_regulator_common_get_voltage,
  1120. .map_voltage = spmi_regulator_common_map_voltage,
  1121. .list_voltage = spmi_regulator_common_list_voltage,
  1122. .set_mode = spmi_regulator_common_set_mode,
  1123. .get_mode = spmi_regulator_common_get_mode,
  1124. .set_load = spmi_regulator_common_set_load,
  1125. .set_pull_down = spmi_regulator_common_set_pull_down,
  1126. };
  1127. static struct regulator_ops spmi_ult_lo_smps_ops = {
  1128. .enable = regulator_enable_regmap,
  1129. .disable = regulator_disable_regmap,
  1130. .is_enabled = regulator_is_enabled_regmap,
  1131. .set_voltage_sel = spmi_regulator_ult_lo_smps_set_voltage,
  1132. .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
  1133. .get_voltage_sel = spmi_regulator_ult_lo_smps_get_voltage,
  1134. .list_voltage = spmi_regulator_common_list_voltage,
  1135. .set_mode = spmi_regulator_common_set_mode,
  1136. .get_mode = spmi_regulator_common_get_mode,
  1137. .set_load = spmi_regulator_common_set_load,
  1138. .set_pull_down = spmi_regulator_common_set_pull_down,
  1139. };
  1140. static struct regulator_ops spmi_ult_ho_smps_ops = {
  1141. .enable = regulator_enable_regmap,
  1142. .disable = regulator_disable_regmap,
  1143. .is_enabled = regulator_is_enabled_regmap,
  1144. .set_voltage_sel = spmi_regulator_single_range_set_voltage,
  1145. .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
  1146. .get_voltage_sel = spmi_regulator_single_range_get_voltage,
  1147. .map_voltage = spmi_regulator_single_map_voltage,
  1148. .list_voltage = spmi_regulator_common_list_voltage,
  1149. .set_mode = spmi_regulator_common_set_mode,
  1150. .get_mode = spmi_regulator_common_get_mode,
  1151. .set_load = spmi_regulator_common_set_load,
  1152. .set_pull_down = spmi_regulator_common_set_pull_down,
  1153. };
  1154. static struct regulator_ops spmi_ult_ldo_ops = {
  1155. .enable = regulator_enable_regmap,
  1156. .disable = regulator_disable_regmap,
  1157. .is_enabled = regulator_is_enabled_regmap,
  1158. .set_voltage_sel = spmi_regulator_single_range_set_voltage,
  1159. .get_voltage_sel = spmi_regulator_single_range_get_voltage,
  1160. .map_voltage = spmi_regulator_single_map_voltage,
  1161. .list_voltage = spmi_regulator_common_list_voltage,
  1162. .set_mode = spmi_regulator_common_set_mode,
  1163. .get_mode = spmi_regulator_common_get_mode,
  1164. .set_load = spmi_regulator_common_set_load,
  1165. .set_bypass = spmi_regulator_common_set_bypass,
  1166. .get_bypass = spmi_regulator_common_get_bypass,
  1167. .set_pull_down = spmi_regulator_common_set_pull_down,
  1168. .set_soft_start = spmi_regulator_common_set_soft_start,
  1169. };
  1170. static struct regulator_ops spmi_ftsmps426_ops = {
  1171. .enable = regulator_enable_regmap,
  1172. .disable = regulator_disable_regmap,
  1173. .is_enabled = regulator_is_enabled_regmap,
  1174. .set_voltage_sel = spmi_regulator_ftsmps426_set_voltage,
  1175. .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
  1176. .get_voltage_sel = spmi_regulator_ftsmps426_get_voltage,
  1177. .map_voltage = spmi_regulator_single_map_voltage,
  1178. .list_voltage = spmi_regulator_common_list_voltage,
  1179. .set_mode = spmi_regulator_ftsmps426_set_mode,
  1180. .get_mode = spmi_regulator_ftsmps426_get_mode,
  1181. .set_load = spmi_regulator_common_set_load,
  1182. .set_pull_down = spmi_regulator_common_set_pull_down,
  1183. };
  1184. static struct regulator_ops spmi_hfs430_ops = {
  1185. .enable = regulator_enable_regmap,
  1186. .disable = regulator_disable_regmap,
  1187. .is_enabled = regulator_is_enabled_regmap,
  1188. .set_voltage_sel = spmi_regulator_ftsmps426_set_voltage,
  1189. .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel,
  1190. .get_voltage_sel = spmi_regulator_ftsmps426_get_voltage,
  1191. .map_voltage = spmi_regulator_single_map_voltage,
  1192. .list_voltage = spmi_regulator_common_list_voltage,
  1193. .set_mode = spmi_regulator_ftsmps426_set_mode,
  1194. .get_mode = spmi_regulator_ftsmps426_get_mode,
  1195. };
  1196. /* Maximum possible digital major revision value */
  1197. #define INF 0xFF
  1198. static const struct spmi_regulator_mapping supported_regulators[] = {
  1199. /* type subtype dig_min dig_max ltype ops setpoints hpm_min */
  1200. SPMI_VREG(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000),
  1201. SPMI_VREG(BUCK, HFS430, 0, INF, HFS430, hfs430, hfs430, 10000),
  1202. SPMI_VREG(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000),
  1203. SPMI_VREG(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000),
  1204. SPMI_VREG(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000),
  1205. SPMI_VREG(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000),
  1206. SPMI_VREG(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000),
  1207. SPMI_VREG(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000),
  1208. SPMI_VREG(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000),
  1209. SPMI_VREG(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000),
  1210. SPMI_VREG(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000),
  1211. SPMI_VREG(LDO, P50, 0, INF, LDO, ldo, pldo, 5000),
  1212. SPMI_VREG(LDO, P150, 0, INF, LDO, ldo, pldo, 10000),
  1213. SPMI_VREG(LDO, P300, 0, INF, LDO, ldo, pldo, 10000),
  1214. SPMI_VREG(LDO, P600, 0, INF, LDO, ldo, pldo, 10000),
  1215. SPMI_VREG(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000),
  1216. SPMI_VREG(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0),
  1217. SPMI_VREG(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000),
  1218. SPMI_VREG(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000),
  1219. SPMI_VREG(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000),
  1220. SPMI_VREG(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000),
  1221. SPMI_VREG(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000),
  1222. SPMI_VREG_VS(LV100, 0, INF),
  1223. SPMI_VREG_VS(LV300, 0, INF),
  1224. SPMI_VREG_VS(MV300, 0, INF),
  1225. SPMI_VREG_VS(MV500, 0, INF),
  1226. SPMI_VREG_VS(HDMI, 0, INF),
  1227. SPMI_VREG_VS(OTG, 0, INF),
  1228. SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0),
  1229. SPMI_VREG(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000),
  1230. SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000),
  1231. SPMI_VREG(FTS, FTS426_CTL, 0, INF, FTSMPS426, ftsmps426, ftsmps426, 100000),
  1232. SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0),
  1233. SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps,
  1234. ult_lo_smps, 100000),
  1235. SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps,
  1236. ult_lo_smps, 100000),
  1237. SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps,
  1238. ult_lo_smps, 100000),
  1239. SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps,
  1240. ult_ho_smps, 100000),
  1241. SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
  1242. SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
  1243. SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
  1244. SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000),
  1245. SPMI_VREG(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
  1246. SPMI_VREG(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
  1247. SPMI_VREG(ULT_LDO, LV_P450, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
  1248. SPMI_VREG(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
  1249. SPMI_VREG(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000),
  1250. SPMI_VREG(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000),
  1251. };
  1252. static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points)
  1253. {
  1254. unsigned int n;
  1255. struct spmi_voltage_range *range = points->range;
  1256. for (; range < points->range + points->count; range++) {
  1257. n = 0;
  1258. if (range->set_point_max_uV) {
  1259. n = range->set_point_max_uV - range->set_point_min_uV;
  1260. n = (n / range->step_uV) + 1;
  1261. }
  1262. range->n_voltages = n;
  1263. points->n_voltages += n;
  1264. }
  1265. }
  1266. static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type)
  1267. {
  1268. const struct spmi_regulator_mapping *mapping;
  1269. int ret, i;
  1270. u32 dig_major_rev;
  1271. u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1];
  1272. u8 type, subtype;
  1273. ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version,
  1274. ARRAY_SIZE(version));
  1275. if (ret) {
  1276. dev_dbg(vreg->dev, "could not read version registers\n");
  1277. return ret;
  1278. }
  1279. dig_major_rev = version[SPMI_COMMON_REG_DIG_MAJOR_REV
  1280. - SPMI_COMMON_REG_DIG_MAJOR_REV];
  1281. if (!force_type) {
  1282. type = version[SPMI_COMMON_REG_TYPE -
  1283. SPMI_COMMON_REG_DIG_MAJOR_REV];
  1284. subtype = version[SPMI_COMMON_REG_SUBTYPE -
  1285. SPMI_COMMON_REG_DIG_MAJOR_REV];
  1286. } else {
  1287. type = force_type >> 8;
  1288. subtype = force_type;
  1289. }
  1290. for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) {
  1291. mapping = &supported_regulators[i];
  1292. if (mapping->type == type && mapping->subtype == subtype
  1293. && mapping->revision_min <= dig_major_rev
  1294. && mapping->revision_max >= dig_major_rev)
  1295. goto found;
  1296. }
  1297. dev_err(vreg->dev,
  1298. "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n",
  1299. vreg->desc.name, type, subtype, dig_major_rev);
  1300. return -ENODEV;
  1301. found:
  1302. vreg->logical_type = mapping->logical_type;
  1303. vreg->set_points = mapping->set_points;
  1304. vreg->hpm_min_load = mapping->hpm_min_load;
  1305. vreg->desc.ops = mapping->ops;
  1306. if (mapping->set_points) {
  1307. if (!mapping->set_points->n_voltages)
  1308. spmi_calculate_num_voltages(mapping->set_points);
  1309. vreg->desc.n_voltages = mapping->set_points->n_voltages;
  1310. }
  1311. return 0;
  1312. }
  1313. static int spmi_regulator_init_slew_rate(struct spmi_regulator *vreg)
  1314. {
  1315. int ret;
  1316. u8 reg = 0;
  1317. int step, delay, slew_rate, step_delay;
  1318. const struct spmi_voltage_range *range;
  1319. ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, &reg, 1);
  1320. if (ret) {
  1321. dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret);
  1322. return ret;
  1323. }
  1324. range = spmi_regulator_find_range(vreg);
  1325. if (!range)
  1326. return -EINVAL;
  1327. switch (vreg->logical_type) {
  1328. case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
  1329. step_delay = SPMI_FTSMPS_STEP_DELAY;
  1330. break;
  1331. default:
  1332. step_delay = SPMI_DEFAULT_STEP_DELAY;
  1333. break;
  1334. }
  1335. step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK;
  1336. step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT;
  1337. delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK;
  1338. delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT;
  1339. /* slew_rate has units of uV/us */
  1340. slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step);
  1341. slew_rate /= 1000 * (step_delay << delay);
  1342. slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM;
  1343. slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN;
  1344. /* Ensure that the slew rate is greater than 0 */
  1345. vreg->slew_rate = max(slew_rate, 1);
  1346. return ret;
  1347. }
  1348. static int spmi_regulator_init_slew_rate_ftsmps426(struct spmi_regulator *vreg,
  1349. int clock_rate)
  1350. {
  1351. int ret;
  1352. u8 reg = 0;
  1353. int delay, slew_rate;
  1354. const struct spmi_voltage_range *range = &vreg->set_points->range[0];
  1355. ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, &reg, 1);
  1356. if (ret) {
  1357. dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret);
  1358. return ret;
  1359. }
  1360. delay = reg & SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK;
  1361. delay >>= SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT;
  1362. /* slew_rate has units of uV/us */
  1363. slew_rate = clock_rate * range->step_uV;
  1364. slew_rate /= 1000 * (SPMI_FTSMPS426_STEP_DELAY << delay);
  1365. slew_rate *= SPMI_FTSMPS426_STEP_MARGIN_NUM;
  1366. slew_rate /= SPMI_FTSMPS426_STEP_MARGIN_DEN;
  1367. /* Ensure that the slew rate is greater than 0 */
  1368. vreg->slew_rate = max(slew_rate, 1);
  1369. return ret;
  1370. }
  1371. static int spmi_regulator_init_registers(struct spmi_regulator *vreg,
  1372. const struct spmi_regulator_init_data *data)
  1373. {
  1374. int ret;
  1375. enum spmi_regulator_logical_type type;
  1376. u8 ctrl_reg[8], reg, mask;
  1377. type = vreg->logical_type;
  1378. ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
  1379. if (ret)
  1380. return ret;
  1381. /* Set up enable pin control. */
  1382. if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
  1383. || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO
  1384. || type == SPMI_REGULATOR_LOGICAL_TYPE_VS)
  1385. && !(data->pin_ctrl_enable
  1386. & SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) {
  1387. ctrl_reg[SPMI_COMMON_IDX_ENABLE] &=
  1388. ~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
  1389. ctrl_reg[SPMI_COMMON_IDX_ENABLE] |=
  1390. data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK;
  1391. }
  1392. /* Set up mode pin control. */
  1393. if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS
  1394. || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO)
  1395. && !(data->pin_ctrl_hpm
  1396. & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1397. ctrl_reg[SPMI_COMMON_IDX_MODE] &=
  1398. ~SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
  1399. ctrl_reg[SPMI_COMMON_IDX_MODE] |=
  1400. data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK;
  1401. }
  1402. if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS
  1403. && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1404. ctrl_reg[SPMI_COMMON_IDX_MODE] &=
  1405. ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1406. ctrl_reg[SPMI_COMMON_IDX_MODE] |=
  1407. data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1408. }
  1409. if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS
  1410. || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS
  1411. || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO)
  1412. && !(data->pin_ctrl_hpm
  1413. & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) {
  1414. ctrl_reg[SPMI_COMMON_IDX_MODE] &=
  1415. ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1416. ctrl_reg[SPMI_COMMON_IDX_MODE] |=
  1417. data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK;
  1418. }
  1419. /* Write back any control register values that were modified. */
  1420. ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8);
  1421. if (ret)
  1422. return ret;
  1423. /* Set soft start strength and over current protection for VS. */
  1424. if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) {
  1425. if (data->vs_soft_start_strength
  1426. != SPMI_VS_SOFT_START_STR_HW_DEFAULT) {
  1427. reg = data->vs_soft_start_strength
  1428. & SPMI_VS_SOFT_START_SEL_MASK;
  1429. mask = SPMI_VS_SOFT_START_SEL_MASK;
  1430. return spmi_vreg_update_bits(vreg,
  1431. SPMI_VS_REG_SOFT_START,
  1432. reg, mask);
  1433. }
  1434. }
  1435. return 0;
  1436. }
  1437. static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg,
  1438. struct device_node *node, struct spmi_regulator_init_data *data)
  1439. {
  1440. /*
  1441. * Initialize configuration parameters to use hardware default in case
  1442. * no value is specified via device tree.
  1443. */
  1444. data->pin_ctrl_enable = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT;
  1445. data->pin_ctrl_hpm = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT;
  1446. data->vs_soft_start_strength = SPMI_VS_SOFT_START_STR_HW_DEFAULT;
  1447. /* These bindings are optional, so it is okay if they aren't found. */
  1448. of_property_read_u32(node, "qcom,ocp-max-retries",
  1449. &vreg->ocp_max_retries);
  1450. of_property_read_u32(node, "qcom,ocp-retry-delay",
  1451. &vreg->ocp_retry_delay_ms);
  1452. of_property_read_u32(node, "qcom,pin-ctrl-enable",
  1453. &data->pin_ctrl_enable);
  1454. of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm);
  1455. of_property_read_u32(node, "qcom,vs-soft-start-strength",
  1456. &data->vs_soft_start_strength);
  1457. }
  1458. static unsigned int spmi_regulator_of_map_mode(unsigned int mode)
  1459. {
  1460. if (mode == 1)
  1461. return REGULATOR_MODE_NORMAL;
  1462. if (mode == 2)
  1463. return REGULATOR_MODE_FAST;
  1464. return REGULATOR_MODE_IDLE;
  1465. }
  1466. static int spmi_regulator_of_parse(struct device_node *node,
  1467. const struct regulator_desc *desc,
  1468. struct regulator_config *config)
  1469. {
  1470. struct spmi_regulator_init_data data = { };
  1471. struct spmi_regulator *vreg = config->driver_data;
  1472. struct device *dev = config->dev;
  1473. int ret;
  1474. spmi_regulator_get_dt_config(vreg, node, &data);
  1475. if (!vreg->ocp_max_retries)
  1476. vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES;
  1477. if (!vreg->ocp_retry_delay_ms)
  1478. vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS;
  1479. ret = spmi_regulator_init_registers(vreg, &data);
  1480. if (ret) {
  1481. dev_err(dev, "common initialization failed, ret=%d\n", ret);
  1482. return ret;
  1483. }
  1484. switch (vreg->logical_type) {
  1485. case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS:
  1486. case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS:
  1487. case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS:
  1488. case SPMI_REGULATOR_LOGICAL_TYPE_SMPS:
  1489. ret = spmi_regulator_init_slew_rate(vreg);
  1490. if (ret)
  1491. return ret;
  1492. break;
  1493. case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426:
  1494. ret = spmi_regulator_init_slew_rate_ftsmps426(vreg,
  1495. SPMI_FTSMPS426_CLOCK_RATE);
  1496. if (ret)
  1497. return ret;
  1498. break;
  1499. case SPMI_REGULATOR_LOGICAL_TYPE_HFS430:
  1500. ret = spmi_regulator_init_slew_rate_ftsmps426(vreg,
  1501. SPMI_HFS430_CLOCK_RATE);
  1502. if (ret)
  1503. return ret;
  1504. break;
  1505. default:
  1506. break;
  1507. }
  1508. if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS)
  1509. vreg->ocp_irq = 0;
  1510. if (vreg->ocp_irq) {
  1511. ret = devm_request_irq(dev, vreg->ocp_irq,
  1512. spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp",
  1513. vreg);
  1514. if (ret < 0) {
  1515. dev_err(dev, "failed to request irq %d, ret=%d\n",
  1516. vreg->ocp_irq, ret);
  1517. return ret;
  1518. }
  1519. INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work);
  1520. }
  1521. return 0;
  1522. }
  1523. static const struct spmi_regulator_data pm8941_regulators[] = {
  1524. { "s1", 0x1400, "vdd_s1", },
  1525. { "s2", 0x1700, "vdd_s2", },
  1526. { "s3", 0x1a00, "vdd_s3", },
  1527. { "s4", 0xa000, },
  1528. { "l1", 0x4000, "vdd_l1_l3", },
  1529. { "l2", 0x4100, "vdd_l2_lvs_1_2_3", },
  1530. { "l3", 0x4200, "vdd_l1_l3", },
  1531. { "l4", 0x4300, "vdd_l4_l11", },
  1532. { "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 },
  1533. { "l6", 0x4500, "vdd_l6_l12_l14_l15", },
  1534. { "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 },
  1535. { "l8", 0x4700, "vdd_l8_l16_l18_19", },
  1536. { "l9", 0x4800, "vdd_l9_l10_l17_l22", },
  1537. { "l10", 0x4900, "vdd_l9_l10_l17_l22", },
  1538. { "l11", 0x4a00, "vdd_l4_l11", },
  1539. { "l12", 0x4b00, "vdd_l6_l12_l14_l15", },
  1540. { "l13", 0x4c00, "vdd_l13_l20_l23_l24", },
  1541. { "l14", 0x4d00, "vdd_l6_l12_l14_l15", },
  1542. { "l15", 0x4e00, "vdd_l6_l12_l14_l15", },
  1543. { "l16", 0x4f00, "vdd_l8_l16_l18_19", },
  1544. { "l17", 0x5000, "vdd_l9_l10_l17_l22", },
  1545. { "l18", 0x5100, "vdd_l8_l16_l18_19", },
  1546. { "l19", 0x5200, "vdd_l8_l16_l18_19", },
  1547. { "l20", 0x5300, "vdd_l13_l20_l23_l24", },
  1548. { "l21", 0x5400, "vdd_l21", },
  1549. { "l22", 0x5500, "vdd_l9_l10_l17_l22", },
  1550. { "l23", 0x5600, "vdd_l13_l20_l23_l24", },
  1551. { "l24", 0x5700, "vdd_l13_l20_l23_l24", },
  1552. { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", },
  1553. { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", },
  1554. { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", },
  1555. { "5vs1", 0x8300, "vin_5vs", "ocp-5vs1", },
  1556. { "5vs2", 0x8400, "vin_5vs", "ocp-5vs2", },
  1557. { }
  1558. };
  1559. static const struct spmi_regulator_data pm8841_regulators[] = {
  1560. { "s1", 0x1400, "vdd_s1", },
  1561. { "s2", 0x1700, "vdd_s2", NULL, 0x1c08 },
  1562. { "s3", 0x1a00, "vdd_s3", },
  1563. { "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 },
  1564. { "s5", 0x2000, "vdd_s5", NULL, 0x1c08 },
  1565. { "s6", 0x2300, "vdd_s6", NULL, 0x1c08 },
  1566. { "s7", 0x2600, "vdd_s7", NULL, 0x1c08 },
  1567. { "s8", 0x2900, "vdd_s8", NULL, 0x1c08 },
  1568. { }
  1569. };
  1570. static const struct spmi_regulator_data pm8916_regulators[] = {
  1571. { "s1", 0x1400, "vdd_s1", },
  1572. { "s2", 0x1700, "vdd_s2", },
  1573. { "s3", 0x1a00, "vdd_s3", },
  1574. { "s4", 0x1d00, "vdd_s4", },
  1575. { "l1", 0x4000, "vdd_l1_l3", },
  1576. { "l2", 0x4100, "vdd_l2", },
  1577. { "l3", 0x4200, "vdd_l1_l3", },
  1578. { "l4", 0x4300, "vdd_l4_l5_l6", },
  1579. { "l5", 0x4400, "vdd_l4_l5_l6", },
  1580. { "l6", 0x4500, "vdd_l4_l5_l6", },
  1581. { "l7", 0x4600, "vdd_l7", },
  1582. { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", },
  1583. { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", },
  1584. { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", },
  1585. { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", },
  1586. { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", },
  1587. { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", },
  1588. { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", },
  1589. { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", },
  1590. { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", },
  1591. { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", },
  1592. { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", },
  1593. { }
  1594. };
  1595. static const struct spmi_regulator_data pm8994_regulators[] = {
  1596. { "s1", 0x1400, "vdd_s1", },
  1597. { "s2", 0x1700, "vdd_s2", },
  1598. { "s3", 0x1a00, "vdd_s3", },
  1599. { "s4", 0x1d00, "vdd_s4", },
  1600. { "s5", 0x2000, "vdd_s5", },
  1601. { "s6", 0x2300, "vdd_s6", },
  1602. { "s7", 0x2600, "vdd_s7", },
  1603. { "s8", 0x2900, "vdd_s8", },
  1604. { "s9", 0x2c00, "vdd_s9", },
  1605. { "s10", 0x2f00, "vdd_s10", },
  1606. { "s11", 0x3200, "vdd_s11", },
  1607. { "s12", 0x3500, "vdd_s12", },
  1608. { "l1", 0x4000, "vdd_l1", },
  1609. { "l2", 0x4100, "vdd_l2_l26_l28", },
  1610. { "l3", 0x4200, "vdd_l3_l11", },
  1611. { "l4", 0x4300, "vdd_l4_l27_l31", },
  1612. { "l5", 0x4400, "vdd_l5_l7", },
  1613. { "l6", 0x4500, "vdd_l6_l12_l32", },
  1614. { "l7", 0x4600, "vdd_l5_l7", },
  1615. { "l8", 0x4700, "vdd_l8_l16_l30", },
  1616. { "l9", 0x4800, "vdd_l9_l10_l18_l22", },
  1617. { "l10", 0x4900, "vdd_l9_l10_l18_l22", },
  1618. { "l11", 0x4a00, "vdd_l3_l11", },
  1619. { "l12", 0x4b00, "vdd_l6_l12_l32", },
  1620. { "l13", 0x4c00, "vdd_l13_l19_l23_l24", },
  1621. { "l14", 0x4d00, "vdd_l14_l15", },
  1622. { "l15", 0x4e00, "vdd_l14_l15", },
  1623. { "l16", 0x4f00, "vdd_l8_l16_l30", },
  1624. { "l17", 0x5000, "vdd_l17_l29", },
  1625. { "l18", 0x5100, "vdd_l9_l10_l18_l22", },
  1626. { "l19", 0x5200, "vdd_l13_l19_l23_l24", },
  1627. { "l20", 0x5300, "vdd_l20_l21", },
  1628. { "l21", 0x5400, "vdd_l20_l21", },
  1629. { "l22", 0x5500, "vdd_l9_l10_l18_l22", },
  1630. { "l23", 0x5600, "vdd_l13_l19_l23_l24", },
  1631. { "l24", 0x5700, "vdd_l13_l19_l23_l24", },
  1632. { "l25", 0x5800, "vdd_l25", },
  1633. { "l26", 0x5900, "vdd_l2_l26_l28", },
  1634. { "l27", 0x5a00, "vdd_l4_l27_l31", },
  1635. { "l28", 0x5b00, "vdd_l2_l26_l28", },
  1636. { "l29", 0x5c00, "vdd_l17_l29", },
  1637. { "l30", 0x5d00, "vdd_l8_l16_l30", },
  1638. { "l31", 0x5e00, "vdd_l4_l27_l31", },
  1639. { "l32", 0x5f00, "vdd_l6_l12_l32", },
  1640. { "lvs1", 0x8000, "vdd_lvs_1_2", },
  1641. { "lvs2", 0x8100, "vdd_lvs_1_2", },
  1642. { }
  1643. };
  1644. static const struct spmi_regulator_data pmi8994_regulators[] = {
  1645. { "s1", 0x1400, "vdd_s1", },
  1646. { "s2", 0x1700, "vdd_s2", },
  1647. { "s3", 0x1a00, "vdd_s3", },
  1648. { "l1", 0x4000, "vdd_l1", },
  1649. { }
  1650. };
  1651. static const struct spmi_regulator_data pm8005_regulators[] = {
  1652. { "s1", 0x1400, "vdd_s1", },
  1653. { "s2", 0x1700, "vdd_s2", },
  1654. { "s3", 0x1a00, "vdd_s3", },
  1655. { "s4", 0x1d00, "vdd_s4", },
  1656. { }
  1657. };
  1658. static const struct spmi_regulator_data pms405_regulators[] = {
  1659. { "s3", 0x1a00, "vdd_s3"},
  1660. { }
  1661. };
  1662. static const struct of_device_id qcom_spmi_regulator_match[] = {
  1663. { .compatible = "qcom,pm8005-regulators", .data = &pm8005_regulators },
  1664. { .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators },
  1665. { .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators },
  1666. { .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators },
  1667. { .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators },
  1668. { .compatible = "qcom,pmi8994-regulators", .data = &pmi8994_regulators },
  1669. { .compatible = "qcom,pms405-regulators", .data = &pms405_regulators },
  1670. { }
  1671. };
  1672. MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match);
  1673. static int qcom_spmi_regulator_probe(struct platform_device *pdev)
  1674. {
  1675. const struct spmi_regulator_data *reg;
  1676. const struct spmi_voltage_range *range;
  1677. const struct of_device_id *match;
  1678. struct regulator_config config = { };
  1679. struct regulator_dev *rdev;
  1680. struct spmi_regulator *vreg;
  1681. struct regmap *regmap;
  1682. const char *name;
  1683. struct device *dev = &pdev->dev;
  1684. struct device_node *node = pdev->dev.of_node;
  1685. struct device_node *syscon, *reg_node;
  1686. struct property *reg_prop;
  1687. int ret, lenp;
  1688. struct list_head *vreg_list;
  1689. vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL);
  1690. if (!vreg_list)
  1691. return -ENOMEM;
  1692. INIT_LIST_HEAD(vreg_list);
  1693. platform_set_drvdata(pdev, vreg_list);
  1694. regmap = dev_get_regmap(dev->parent, NULL);
  1695. if (!regmap)
  1696. return -ENODEV;
  1697. match = of_match_device(qcom_spmi_regulator_match, &pdev->dev);
  1698. if (!match)
  1699. return -ENODEV;
  1700. if (of_find_property(node, "qcom,saw-reg", &lenp)) {
  1701. syscon = of_parse_phandle(node, "qcom,saw-reg", 0);
  1702. saw_regmap = syscon_node_to_regmap(syscon);
  1703. of_node_put(syscon);
  1704. if (IS_ERR(saw_regmap))
  1705. dev_err(dev, "ERROR reading SAW regmap\n");
  1706. }
  1707. for (reg = match->data; reg->name; reg++) {
  1708. if (saw_regmap) {
  1709. reg_node = of_get_child_by_name(node, reg->name);
  1710. reg_prop = of_find_property(reg_node, "qcom,saw-slave",
  1711. &lenp);
  1712. of_node_put(reg_node);
  1713. if (reg_prop)
  1714. continue;
  1715. }
  1716. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  1717. if (!vreg)
  1718. return -ENOMEM;
  1719. vreg->dev = dev;
  1720. vreg->base = reg->base;
  1721. vreg->regmap = regmap;
  1722. if (reg->ocp) {
  1723. vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp);
  1724. if (vreg->ocp_irq < 0) {
  1725. ret = vreg->ocp_irq;
  1726. goto err;
  1727. }
  1728. }
  1729. vreg->desc.id = -1;
  1730. vreg->desc.owner = THIS_MODULE;
  1731. vreg->desc.type = REGULATOR_VOLTAGE;
  1732. vreg->desc.enable_reg = reg->base + SPMI_COMMON_REG_ENABLE;
  1733. vreg->desc.enable_mask = SPMI_COMMON_ENABLE_MASK;
  1734. vreg->desc.enable_val = SPMI_COMMON_ENABLE;
  1735. vreg->desc.name = name = reg->name;
  1736. vreg->desc.supply_name = reg->supply;
  1737. vreg->desc.of_match = reg->name;
  1738. vreg->desc.of_parse_cb = spmi_regulator_of_parse;
  1739. vreg->desc.of_map_mode = spmi_regulator_of_map_mode;
  1740. ret = spmi_regulator_match(vreg, reg->force_type);
  1741. if (ret)
  1742. continue;
  1743. if (saw_regmap) {
  1744. reg_node = of_get_child_by_name(node, reg->name);
  1745. reg_prop = of_find_property(reg_node, "qcom,saw-leader",
  1746. &lenp);
  1747. of_node_put(reg_node);
  1748. if (reg_prop) {
  1749. spmi_saw_ops = *(vreg->desc.ops);
  1750. spmi_saw_ops.set_voltage_sel =
  1751. spmi_regulator_saw_set_voltage;
  1752. vreg->desc.ops = &spmi_saw_ops;
  1753. }
  1754. }
  1755. if (vreg->set_points && vreg->set_points->count == 1) {
  1756. /* since there is only one range */
  1757. range = vreg->set_points->range;
  1758. vreg->desc.uV_step = range->step_uV;
  1759. }
  1760. config.dev = dev;
  1761. config.driver_data = vreg;
  1762. config.regmap = regmap;
  1763. rdev = devm_regulator_register(dev, &vreg->desc, &config);
  1764. if (IS_ERR(rdev)) {
  1765. dev_err(dev, "failed to register %s\n", name);
  1766. ret = PTR_ERR(rdev);
  1767. goto err;
  1768. }
  1769. INIT_LIST_HEAD(&vreg->node);
  1770. list_add(&vreg->node, vreg_list);
  1771. }
  1772. return 0;
  1773. err:
  1774. list_for_each_entry(vreg, vreg_list, node)
  1775. if (vreg->ocp_irq)
  1776. cancel_delayed_work_sync(&vreg->ocp_work);
  1777. return ret;
  1778. }
  1779. static int qcom_spmi_regulator_remove(struct platform_device *pdev)
  1780. {
  1781. struct spmi_regulator *vreg;
  1782. struct list_head *vreg_list = platform_get_drvdata(pdev);
  1783. list_for_each_entry(vreg, vreg_list, node)
  1784. if (vreg->ocp_irq)
  1785. cancel_delayed_work_sync(&vreg->ocp_work);
  1786. return 0;
  1787. }
  1788. static struct platform_driver qcom_spmi_regulator_driver = {
  1789. .driver = {
  1790. .name = "qcom-spmi-regulator",
  1791. .of_match_table = qcom_spmi_regulator_match,
  1792. },
  1793. .probe = qcom_spmi_regulator_probe,
  1794. .remove = qcom_spmi_regulator_remove,
  1795. };
  1796. module_platform_driver(qcom_spmi_regulator_driver);
  1797. MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver");
  1798. MODULE_LICENSE("GPL v2");
  1799. MODULE_ALIAS("platform:qcom-spmi-regulator");