qcom_spmi-regulator.c 54 KB

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