spm-regulator.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "%s: " fmt, __func__
  13. #include <linux/arm-smccc.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/regmap.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/spmi.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/string.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/regulator/spm-regulator.h>
  30. #include <soc/qcom/spm.h>
  31. #include <linux/arm-smccc.h>
  32. #if defined(CONFIG_ARM64) || (defined(CONFIG_ARM) && defined(CONFIG_ARM_PSCI))
  33. #else
  34. #define __invoke_psci_fn_smc(a, b, c, d) 0
  35. #endif
  36. #define SPM_REGULATOR_DRIVER_NAME "qcom,spm-regulator"
  37. struct voltage_range {
  38. int min_uV;
  39. int set_point_min_uV;
  40. int max_uV;
  41. int step_uV;
  42. };
  43. enum qpnp_regulator_uniq_type {
  44. QPNP_TYPE_HF,
  45. QPNP_TYPE_FTS2,
  46. QPNP_TYPE_FTS2p5,
  47. QPNP_TYPE_FTS426,
  48. QPNP_TYPE_ULT_HF,
  49. };
  50. enum qpnp_regulator_type {
  51. QPNP_HF_TYPE = 0x03,
  52. QPNP_FTS2_TYPE = 0x1C,
  53. QPNP_FTS2p5_TYPE = 0x1C,
  54. QPNP_FTS426_TYPE = 0x1C,
  55. QPNP_ULT_HF_TYPE = 0x22,
  56. };
  57. enum qpnp_regulator_subtype {
  58. QPNP_FTS2_SUBTYPE = 0x08,
  59. QPNP_HF_SUBTYPE = 0x08,
  60. QPNP_FTS2p5_SUBTYPE = 0x09,
  61. QPNP_FTS426_SUBTYPE = 0x0A,
  62. QPNP_ULT_HF_SUBTYPE = 0x0D,
  63. };
  64. enum qpnp_logical_mode {
  65. QPNP_LOGICAL_MODE_AUTO,
  66. QPNP_LOGICAL_MODE_PWM,
  67. };
  68. static const struct voltage_range fts2_range0 = {0, 350000, 1275000, 5000};
  69. static const struct voltage_range fts2_range1 = {0, 700000, 2040000, 10000};
  70. static const struct voltage_range fts2p5_range0
  71. = { 80000, 350000, 1355000, 5000};
  72. static const struct voltage_range fts2p5_range1
  73. = {160000, 700000, 2200000, 10000};
  74. static const struct voltage_range fts426_range = {0, 320000, 1352000, 4000};
  75. static const struct voltage_range ult_hf_range0 = {375000, 375000, 1562500,
  76. 12500};
  77. static const struct voltage_range ult_hf_range1 = {750000, 750000, 1525000,
  78. 25000};
  79. static const struct voltage_range hf_range0 = {375000, 375000, 1562500, 12500};
  80. static const struct voltage_range hf_range1 = {1550000, 1550000, 3125000,
  81. 25000};
  82. #define QPNP_SMPS_REG_TYPE 0x04
  83. #define QPNP_SMPS_REG_SUBTYPE 0x05
  84. #define QPNP_SMPS_REG_VOLTAGE_RANGE 0x40
  85. #define QPNP_SMPS_REG_VOLTAGE_SETPOINT 0x41
  86. #define QPNP_SMPS_REG_MODE 0x45
  87. #define QPNP_SMPS_REG_STEP_CTRL 0x61
  88. #define QPNP_SMPS_REG_UL_LL_CTRL 0x68
  89. /* FTS426 voltage control registers */
  90. #define QPNP_FTS426_REG_VOLTAGE_LB 0x40
  91. #define QPNP_FTS426_REG_VOLTAGE_UB 0x41
  92. #define QPNP_FTS426_REG_VOLTAGE_VALID_LB 0x42
  93. #define QPNP_FTS426_REG_VOLTAGE_VALID_UB 0x43
  94. /* HF voltage limit registers */
  95. #define QPNP_HF_REG_VOLTAGE_ULS 0x69
  96. #define QPNP_HF_REG_VOLTAGE_LLS 0x6B
  97. /* FTS voltage limit registers */
  98. #define QPNP_FTS_REG_VOLTAGE_ULS_VALID 0x6A
  99. #define QPNP_FTS_REG_VOLTAGE_LLS_VALID 0x6C
  100. /* FTS426 voltage limit registers */
  101. #define QPNP_FTS426_REG_VOLTAGE_ULS_LB 0x68
  102. #define QPNP_FTS426_REG_VOLTAGE_ULS_UB 0x69
  103. /* Common regulator UL & LL limits control register layout */
  104. #define QPNP_COMMON_UL_EN_MASK 0x80
  105. #define QPNP_COMMON_LL_EN_MASK 0x40
  106. #define QPNP_SMPS_MODE_PWM 0x80
  107. #define QPNP_SMPS_MODE_AUTO 0x40
  108. #define QPNP_FTS426_MODE_PWM 0x07
  109. #define QPNP_FTS426_MODE_AUTO 0x06
  110. #define QPNP_SMPS_STEP_CTRL_STEP_MASK 0x18
  111. #define QPNP_SMPS_STEP_CTRL_STEP_SHIFT 3
  112. #define QPNP_SMPS_STEP_CTRL_DELAY_MASK 0x07
  113. #define QPNP_SMPS_STEP_CTRL_DELAY_SHIFT 0
  114. #define QPNP_FTS426_STEP_CTRL_DELAY_MASK 0x03
  115. #define QPNP_FTS426_STEP_CTRL_DELAY_SHIFT 0
  116. /* Clock rate in kHz of the FTS2 regulator reference clock. */
  117. #define QPNP_SMPS_CLOCK_RATE 19200
  118. #define QPNP_FTS426_CLOCK_RATE 4800
  119. /* Time to delay in us to ensure that a mode change has completed. */
  120. #define QPNP_FTS2_MODE_CHANGE_DELAY 50
  121. /* Minimum time in us that it takes to complete a single SPMI write. */
  122. #define QPNP_SPMI_WRITE_MIN_DELAY 8
  123. /* Minimum voltage stepper delay for each step. */
  124. #define QPNP_FTS2_STEP_DELAY 8
  125. #define QPNP_HF_STEP_DELAY 20
  126. #define QPNP_FTS426_STEP_DELAY 2
  127. /* Arbitrarily large max step size used to avoid possible numerical overflow */
  128. #define SPM_REGULATOR_MAX_STEP_UV 10000000
  129. /*
  130. * The ratio QPNP_FTS2_STEP_MARGIN_NUM/QPNP_FTS2_STEP_MARGIN_DEN is use to
  131. * adjust the step rate in order to account for oscillator variance.
  132. */
  133. #define QPNP_FTS2_STEP_MARGIN_NUM 4
  134. #define QPNP_FTS2_STEP_MARGIN_DEN 5
  135. #define QPNP_FTS426_STEP_MARGIN_NUM 10
  136. #define QPNP_FTS426_STEP_MARGIN_DEN 11
  137. /*
  138. * Settling delay for FTS2.5
  139. * Warm-up=20uS, 0-10% & 90-100% non-linear V-ramp delay = 50uS
  140. */
  141. #define FTS2P5_SETTLING_DELAY_US 70
  142. /* VSET value to decide the range of ULT SMPS */
  143. #define ULT_SMPS_RANGE_SPLIT 0x60
  144. struct spm_vreg {
  145. struct regulator_desc rdesc;
  146. struct regulator_dev *rdev;
  147. struct platform_device *pdev;
  148. struct regmap *regmap;
  149. const struct voltage_range *range;
  150. int uV;
  151. int last_set_uV;
  152. unsigned vlevel;
  153. unsigned last_set_vlevel;
  154. u32 max_step_uV;
  155. bool online;
  156. u16 spmi_base_addr;
  157. enum qpnp_logical_mode init_mode;
  158. enum qpnp_logical_mode mode;
  159. int step_rate;
  160. enum qpnp_regulator_uniq_type regulator_type;
  161. u32 cpu_num;
  162. bool bypass_spm;
  163. struct regulator_desc avs_rdesc;
  164. struct regulator_dev *avs_rdev;
  165. int avs_min_uV;
  166. int avs_max_uV;
  167. bool avs_enabled;
  168. u32 recal_cluster_mask;
  169. };
  170. static inline bool spm_regulator_using_avs(struct spm_vreg *vreg)
  171. {
  172. return vreg->avs_rdev && !vreg->bypass_spm;
  173. }
  174. static int spm_regulator_uv_to_vlevel(struct spm_vreg *vreg, int uV)
  175. {
  176. int vlevel;
  177. if (vreg->regulator_type == QPNP_TYPE_FTS426)
  178. return roundup(uV, vreg->range->step_uV) / 1000;
  179. vlevel = DIV_ROUND_UP(uV - vreg->range->min_uV, vreg->range->step_uV);
  180. /* Fix VSET for ULT HF Buck */
  181. if (vreg->regulator_type == QPNP_TYPE_ULT_HF
  182. && vreg->range == &ult_hf_range1) {
  183. vlevel &= 0x1F;
  184. vlevel |= ULT_SMPS_RANGE_SPLIT;
  185. }
  186. return vlevel;
  187. }
  188. static int spm_regulator_vlevel_to_uv(struct spm_vreg *vreg, int vlevel)
  189. {
  190. if (vreg->regulator_type == QPNP_TYPE_FTS426)
  191. return vlevel * 1000;
  192. /*
  193. * Calculate ULT HF buck VSET based on range:
  194. * In case of range 0: VSET is a 7 bit value.
  195. * In case of range 1: VSET is a 5 bit value.
  196. */
  197. if (vreg->regulator_type == QPNP_TYPE_ULT_HF
  198. && vreg->range == &ult_hf_range1)
  199. vlevel &= ~ULT_SMPS_RANGE_SPLIT;
  200. return vlevel * vreg->range->step_uV + vreg->range->min_uV;
  201. }
  202. static unsigned spm_regulator_vlevel_to_selector(struct spm_vreg *vreg,
  203. unsigned vlevel)
  204. {
  205. /* Fix VSET for ULT HF Buck */
  206. if (vreg->regulator_type == QPNP_TYPE_ULT_HF
  207. && vreg->range == &ult_hf_range1)
  208. vlevel &= ~ULT_SMPS_RANGE_SPLIT;
  209. return vlevel - (vreg->range->set_point_min_uV - vreg->range->min_uV)
  210. / vreg->range->step_uV;
  211. }
  212. static int qpnp_smps_read_voltage(struct spm_vreg *vreg)
  213. {
  214. int rc;
  215. u8 val[2] = {0};
  216. if (vreg->regulator_type == QPNP_TYPE_FTS426) {
  217. rc = regmap_bulk_read(vreg->regmap,
  218. vreg->spmi_base_addr + QPNP_FTS426_REG_VOLTAGE_VALID_LB,
  219. val, 2);
  220. if (rc) {
  221. dev_err(&vreg->pdev->dev, "%s: could not read voltage setpoint registers, rc=%d\n",
  222. __func__, rc);
  223. return rc;
  224. }
  225. vreg->last_set_vlevel = ((unsigned)val[1] << 8) | val[0];
  226. } else {
  227. rc = regmap_bulk_read(vreg->regmap,
  228. vreg->spmi_base_addr + QPNP_SMPS_REG_VOLTAGE_SETPOINT,
  229. val, 1);
  230. if (rc) {
  231. dev_err(&vreg->pdev->dev, "%s: could not read voltage setpoint register, rc=%d\n",
  232. __func__, rc);
  233. return rc;
  234. }
  235. vreg->last_set_vlevel = val[0];
  236. }
  237. vreg->last_set_uV = spm_regulator_vlevel_to_uv(vreg,
  238. vreg->last_set_vlevel);
  239. return rc;
  240. }
  241. static int qpnp_smps_write_voltage(struct spm_vreg *vreg, unsigned vlevel)
  242. {
  243. int rc = 0;
  244. u8 reg[2];
  245. /* Set voltage control registers via SPMI. */
  246. reg[0] = vlevel & 0xFF;
  247. reg[1] = (vlevel >> 8) & 0xFF;
  248. if (vreg->regulator_type == QPNP_TYPE_FTS426) {
  249. rc = regmap_bulk_write(vreg->regmap,
  250. vreg->spmi_base_addr + QPNP_FTS426_REG_VOLTAGE_LB,
  251. reg, 2);
  252. } else {
  253. rc = regmap_write(vreg->regmap,
  254. vreg->spmi_base_addr + QPNP_SMPS_REG_VOLTAGE_SETPOINT,
  255. reg[0]);
  256. }
  257. if (rc)
  258. pr_err("%s: regmap_write failed, rc=%d\n",
  259. vreg->rdesc.name, rc);
  260. return rc;
  261. }
  262. static inline enum qpnp_logical_mode qpnp_regval_to_mode(struct spm_vreg *vreg,
  263. u8 regval)
  264. {
  265. if (vreg->regulator_type == QPNP_TYPE_FTS426)
  266. return (regval == QPNP_FTS426_MODE_PWM)
  267. ? QPNP_LOGICAL_MODE_PWM : QPNP_LOGICAL_MODE_AUTO;
  268. else
  269. return (regval & QPNP_SMPS_MODE_PWM)
  270. ? QPNP_LOGICAL_MODE_PWM : QPNP_LOGICAL_MODE_AUTO;
  271. }
  272. static inline u8 qpnp_mode_to_regval(struct spm_vreg *vreg,
  273. enum qpnp_logical_mode mode)
  274. {
  275. if (vreg->regulator_type == QPNP_TYPE_FTS426)
  276. return (mode == QPNP_LOGICAL_MODE_PWM)
  277. ? QPNP_FTS426_MODE_PWM : QPNP_FTS426_MODE_AUTO;
  278. else
  279. return (mode == QPNP_LOGICAL_MODE_PWM)
  280. ? QPNP_SMPS_MODE_PWM : QPNP_SMPS_MODE_AUTO;
  281. }
  282. static int qpnp_smps_set_mode(struct spm_vreg *vreg, u8 mode)
  283. {
  284. int rc;
  285. rc = regmap_write(vreg->regmap,
  286. vreg->spmi_base_addr + QPNP_SMPS_REG_MODE,
  287. qpnp_mode_to_regval(vreg, mode));
  288. if (rc)
  289. dev_err(&vreg->pdev->dev,
  290. "%s: could not write to mode register, rc=%d\n",
  291. __func__, rc);
  292. return rc;
  293. }
  294. static int spm_regulator_get_voltage(struct regulator_dev *rdev)
  295. {
  296. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  297. int vlevel, rc;
  298. if (spm_regulator_using_avs(vreg)) {
  299. vlevel = msm_spm_get_vdd(vreg->cpu_num);
  300. if (vlevel < 0) {
  301. pr_debug("%s: msm_spm_get_vdd failed, rc=%d; falling back on SPMI read\n",
  302. vreg->rdesc.name, vlevel);
  303. rc = qpnp_smps_read_voltage(vreg);
  304. if (rc) {
  305. pr_err("%s: voltage read failed, rc=%d\n",
  306. vreg->rdesc.name, rc);
  307. return rc;
  308. }
  309. return vreg->last_set_uV;
  310. }
  311. vreg->last_set_vlevel = vlevel;
  312. vreg->last_set_uV = spm_regulator_vlevel_to_uv(vreg, vlevel);
  313. return vreg->last_set_uV;
  314. } else {
  315. return vreg->uV;
  316. }
  317. };
  318. static int spm_regulator_write_voltage(struct spm_vreg *vreg, int uV)
  319. {
  320. unsigned vlevel = spm_regulator_uv_to_vlevel(vreg, uV);
  321. bool spm_failed = false;
  322. int rc = 0;
  323. u32 slew_delay;
  324. if (likely(!vreg->bypass_spm)) {
  325. /* Set voltage control register via SPM. */
  326. rc = msm_spm_set_vdd(vreg->cpu_num, vlevel);
  327. if (rc) {
  328. pr_debug("%s: msm_spm_set_vdd failed, rc=%d; falling back on SPMI write\n",
  329. vreg->rdesc.name, rc);
  330. spm_failed = true;
  331. }
  332. }
  333. if (unlikely(vreg->bypass_spm || spm_failed)) {
  334. rc = qpnp_smps_write_voltage(vreg, vlevel);
  335. if (rc) {
  336. pr_err("%s: voltage write failed, rc=%d\n",
  337. vreg->rdesc.name, rc);
  338. return rc;
  339. }
  340. }
  341. if (uV > vreg->last_set_uV) {
  342. /* Wait for voltage stepping to complete. */
  343. slew_delay = DIV_ROUND_UP(uV - vreg->last_set_uV,
  344. vreg->step_rate);
  345. if (vreg->regulator_type == QPNP_TYPE_FTS2p5)
  346. slew_delay += FTS2P5_SETTLING_DELAY_US;
  347. udelay(slew_delay);
  348. } else if (vreg->regulator_type == QPNP_TYPE_FTS2p5) {
  349. /* add the ramp-down delay */
  350. slew_delay = DIV_ROUND_UP(vreg->last_set_uV - uV,
  351. vreg->step_rate) + FTS2P5_SETTLING_DELAY_US;
  352. udelay(slew_delay);
  353. }
  354. vreg->last_set_uV = uV;
  355. vreg->last_set_vlevel = vlevel;
  356. return rc;
  357. }
  358. static int spm_regulator_recalibrate(struct spm_vreg *vreg)
  359. {
  360. struct arm_smccc_res res;
  361. if (!vreg->recal_cluster_mask)
  362. return 0;
  363. arm_smccc_smc(0xC4000020, vreg->recal_cluster_mask, 2, 0, 0, 0, 0, 0, &res);
  364. if (res.a0)
  365. pr_err("%s: recalibration failed, rc=%ld\n", vreg->rdesc.name,
  366. res.a0);
  367. return res.a0;
  368. }
  369. static int _spm_regulator_set_voltage(struct regulator_dev *rdev)
  370. {
  371. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  372. bool pwm_required;
  373. int rc = 0;
  374. int uV;
  375. rc = spm_regulator_get_voltage(rdev);
  376. if (rc < 0)
  377. return rc;
  378. if (vreg->vlevel == vreg->last_set_vlevel)
  379. return 0;
  380. pwm_required = (vreg->regulator_type == QPNP_TYPE_FTS2)
  381. && (vreg->init_mode != QPNP_LOGICAL_MODE_PWM)
  382. && vreg->uV > vreg->last_set_uV;
  383. if (pwm_required) {
  384. /* Switch to PWM mode so that voltage ramping is fast. */
  385. rc = qpnp_smps_set_mode(vreg, QPNP_LOGICAL_MODE_PWM);
  386. if (rc)
  387. return rc;
  388. }
  389. do {
  390. uV = vreg->uV > vreg->last_set_uV
  391. ? min(vreg->uV, vreg->last_set_uV + (int)vreg->max_step_uV)
  392. : max(vreg->uV, vreg->last_set_uV - (int)vreg->max_step_uV);
  393. rc = spm_regulator_write_voltage(vreg, uV);
  394. if (rc)
  395. return rc;
  396. } while (vreg->last_set_uV != vreg->uV);
  397. if (pwm_required) {
  398. /* Wait for mode transition to complete. */
  399. udelay(QPNP_FTS2_MODE_CHANGE_DELAY - QPNP_SPMI_WRITE_MIN_DELAY);
  400. /* Switch to AUTO mode so that power consumption is lowered. */
  401. rc = qpnp_smps_set_mode(vreg, QPNP_LOGICAL_MODE_AUTO);
  402. if (rc)
  403. return rc;
  404. }
  405. rc = spm_regulator_recalibrate(vreg);
  406. return rc;
  407. }
  408. static int spm_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  409. int max_uV, unsigned *selector)
  410. {
  411. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  412. const struct voltage_range *range = vreg->range;
  413. int uV = min_uV;
  414. unsigned vlevel;
  415. if (uV < range->set_point_min_uV && max_uV >= range->set_point_min_uV)
  416. uV = range->set_point_min_uV;
  417. if (uV < range->set_point_min_uV || uV > range->max_uV) {
  418. pr_err("%s: request v=[%d, %d] is outside possible v=[%d, %d]\n",
  419. vreg->rdesc.name, min_uV, max_uV,
  420. range->set_point_min_uV, range->max_uV);
  421. return -EINVAL;
  422. }
  423. vlevel = spm_regulator_uv_to_vlevel(vreg, uV);
  424. uV = spm_regulator_vlevel_to_uv(vreg, vlevel);
  425. if (uV > max_uV) {
  426. pr_err("%s: request v=[%d, %d] cannot be met by any set point\n",
  427. vreg->rdesc.name, min_uV, max_uV);
  428. return -EINVAL;
  429. }
  430. *selector = spm_regulator_vlevel_to_selector(vreg, vlevel);
  431. vreg->vlevel = vlevel;
  432. vreg->uV = uV;
  433. if (!vreg->online)
  434. return 0;
  435. return _spm_regulator_set_voltage(rdev);
  436. }
  437. static int spm_regulator_list_voltage(struct regulator_dev *rdev,
  438. unsigned selector)
  439. {
  440. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  441. if (selector >= vreg->rdesc.n_voltages)
  442. return 0;
  443. return selector * vreg->range->step_uV + vreg->range->set_point_min_uV;
  444. }
  445. static int spm_regulator_enable(struct regulator_dev *rdev)
  446. {
  447. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  448. int rc;
  449. rc = _spm_regulator_set_voltage(rdev);
  450. if (!rc)
  451. vreg->online = true;
  452. return rc;
  453. }
  454. static int spm_regulator_disable(struct regulator_dev *rdev)
  455. {
  456. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  457. vreg->online = false;
  458. return 0;
  459. }
  460. static int spm_regulator_is_enabled(struct regulator_dev *rdev)
  461. {
  462. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  463. return vreg->online;
  464. }
  465. static unsigned int spm_regulator_get_mode(struct regulator_dev *rdev)
  466. {
  467. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  468. return vreg->mode == QPNP_LOGICAL_MODE_PWM
  469. ? REGULATOR_MODE_NORMAL : REGULATOR_MODE_IDLE;
  470. }
  471. static int spm_regulator_set_mode(struct regulator_dev *rdev, unsigned int mode)
  472. {
  473. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  474. /*
  475. * Map REGULATOR_MODE_NORMAL to PWM mode and REGULATOR_MODE_IDLE to
  476. * init_mode. This ensures that the regulator always stays in PWM mode
  477. * in the case that qcom,mode has been specified as "pwm" in device
  478. * tree.
  479. */
  480. vreg->mode = (mode == REGULATOR_MODE_NORMAL) ? QPNP_LOGICAL_MODE_PWM
  481. : vreg->init_mode;
  482. return qpnp_smps_set_mode(vreg, vreg->mode);
  483. }
  484. static struct regulator_ops spm_regulator_ops = {
  485. .get_voltage = spm_regulator_get_voltage,
  486. .set_voltage = spm_regulator_set_voltage,
  487. .list_voltage = spm_regulator_list_voltage,
  488. .get_mode = spm_regulator_get_mode,
  489. .set_mode = spm_regulator_set_mode,
  490. .enable = spm_regulator_enable,
  491. .disable = spm_regulator_disable,
  492. .is_enabled = spm_regulator_is_enabled,
  493. };
  494. static int spm_regulator_avs_set_voltage(struct regulator_dev *rdev, int min_uV,
  495. int max_uV, unsigned *selector)
  496. {
  497. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  498. const struct voltage_range *range = vreg->range;
  499. unsigned vlevel_min, vlevel_max;
  500. int uV, avs_min_uV, avs_max_uV, rc;
  501. uV = min_uV;
  502. if (uV < range->set_point_min_uV && max_uV >= range->set_point_min_uV)
  503. uV = range->set_point_min_uV;
  504. if (uV < range->set_point_min_uV || uV > range->max_uV) {
  505. pr_err("%s: request v=[%d, %d] is outside possible v=[%d, %d]\n",
  506. vreg->avs_rdesc.name, min_uV, max_uV,
  507. range->set_point_min_uV, range->max_uV);
  508. return -EINVAL;
  509. }
  510. vlevel_min = spm_regulator_uv_to_vlevel(vreg, uV);
  511. avs_min_uV = spm_regulator_vlevel_to_uv(vreg, vlevel_min);
  512. if (avs_min_uV > max_uV) {
  513. pr_err("%s: request v=[%d, %d] cannot be met by any set point\n",
  514. vreg->avs_rdesc.name, min_uV, max_uV);
  515. return -EINVAL;
  516. }
  517. uV = max_uV;
  518. if (uV > range->max_uV && min_uV <= range->max_uV)
  519. uV = range->max_uV;
  520. if (uV < range->set_point_min_uV || uV > range->max_uV) {
  521. pr_err("%s: request v=[%d, %d] is outside possible v=[%d, %d]\n",
  522. vreg->avs_rdesc.name, min_uV, max_uV,
  523. range->set_point_min_uV, range->max_uV);
  524. return -EINVAL;
  525. }
  526. vlevel_max = spm_regulator_uv_to_vlevel(vreg, uV);
  527. avs_max_uV = spm_regulator_vlevel_to_uv(vreg, vlevel_max);
  528. if (avs_max_uV < min_uV) {
  529. pr_err("%s: request v=[%d, %d] cannot be met by any set point\n",
  530. vreg->avs_rdesc.name, min_uV, max_uV);
  531. return -EINVAL;
  532. }
  533. if (likely(!vreg->bypass_spm)) {
  534. rc = msm_spm_avs_set_limit(vreg->cpu_num, vlevel_min,
  535. vlevel_max);
  536. if (rc) {
  537. pr_err("%s: AVS limit setting failed, rc=%d\n",
  538. vreg->avs_rdesc.name, rc);
  539. return rc;
  540. }
  541. }
  542. *selector = spm_regulator_vlevel_to_selector(vreg, vlevel_min);
  543. vreg->avs_min_uV = avs_min_uV;
  544. vreg->avs_max_uV = avs_max_uV;
  545. return 0;
  546. }
  547. static int spm_regulator_avs_get_voltage(struct regulator_dev *rdev)
  548. {
  549. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  550. return vreg->avs_min_uV;
  551. }
  552. static int spm_regulator_avs_enable(struct regulator_dev *rdev)
  553. {
  554. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  555. int rc;
  556. if (likely(!vreg->bypass_spm)) {
  557. rc = msm_spm_avs_enable(vreg->cpu_num);
  558. if (rc) {
  559. pr_err("%s: AVS enable failed, rc=%d\n",
  560. vreg->avs_rdesc.name, rc);
  561. return rc;
  562. }
  563. }
  564. vreg->avs_enabled = true;
  565. return 0;
  566. }
  567. static int spm_regulator_avs_disable(struct regulator_dev *rdev)
  568. {
  569. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  570. int rc;
  571. if (likely(!vreg->bypass_spm)) {
  572. rc = msm_spm_avs_disable(vreg->cpu_num);
  573. if (rc) {
  574. pr_err("%s: AVS disable failed, rc=%d\n",
  575. vreg->avs_rdesc.name, rc);
  576. return rc;
  577. }
  578. }
  579. vreg->avs_enabled = false;
  580. return 0;
  581. }
  582. static int spm_regulator_avs_is_enabled(struct regulator_dev *rdev)
  583. {
  584. struct spm_vreg *vreg = rdev_get_drvdata(rdev);
  585. return vreg->avs_enabled;
  586. }
  587. static struct regulator_ops spm_regulator_avs_ops = {
  588. .get_voltage = spm_regulator_avs_get_voltage,
  589. .set_voltage = spm_regulator_avs_set_voltage,
  590. .list_voltage = spm_regulator_list_voltage,
  591. .enable = spm_regulator_avs_enable,
  592. .disable = spm_regulator_avs_disable,
  593. .is_enabled = spm_regulator_avs_is_enabled,
  594. };
  595. static int qpnp_smps_check_type(struct spm_vreg *vreg)
  596. {
  597. int rc;
  598. u8 type[2];
  599. rc = regmap_bulk_read(vreg->regmap,
  600. vreg->spmi_base_addr + QPNP_SMPS_REG_TYPE,
  601. type,
  602. 2);
  603. if (rc) {
  604. dev_err(&vreg->pdev->dev,
  605. "%s: could not read type register, rc=%d\n",
  606. __func__, rc);
  607. return rc;
  608. }
  609. if (type[0] == QPNP_FTS2_TYPE && type[1] == QPNP_FTS2_SUBTYPE) {
  610. vreg->regulator_type = QPNP_TYPE_FTS2;
  611. } else if (type[0] == QPNP_FTS2p5_TYPE
  612. && type[1] == QPNP_FTS2p5_SUBTYPE) {
  613. vreg->regulator_type = QPNP_TYPE_FTS2p5;
  614. } else if (type[0] == QPNP_FTS426_TYPE
  615. && type[1] == QPNP_FTS426_SUBTYPE) {
  616. vreg->regulator_type = QPNP_TYPE_FTS426;
  617. } else if (type[0] == QPNP_ULT_HF_TYPE
  618. && type[1] == QPNP_ULT_HF_SUBTYPE) {
  619. vreg->regulator_type = QPNP_TYPE_ULT_HF;
  620. } else if (type[0] == QPNP_HF_TYPE
  621. && type[1] == QPNP_HF_SUBTYPE) {
  622. vreg->regulator_type = QPNP_TYPE_HF;
  623. } else {
  624. dev_err(&vreg->pdev->dev,
  625. "%s: invalid type=0x%02X, subtype=0x%02X register pair\n",
  626. __func__, type[0], type[1]);
  627. return -ENODEV;
  628. };
  629. return rc;
  630. }
  631. static int qpnp_smps_init_range(struct spm_vreg *vreg,
  632. const struct voltage_range *range0, const struct voltage_range *range1)
  633. {
  634. int rc;
  635. u8 reg = 0;
  636. uint val;
  637. rc = regmap_read(vreg->regmap,
  638. vreg->spmi_base_addr + QPNP_SMPS_REG_VOLTAGE_RANGE,
  639. &val);
  640. if (rc) {
  641. dev_err(&vreg->pdev->dev,
  642. "%s: could not read voltage range register, rc=%d\n",
  643. __func__, rc);
  644. return rc;
  645. }
  646. reg = (u8)val;
  647. if (reg == 0x00) {
  648. vreg->range = range0;
  649. } else if (reg == 0x01) {
  650. vreg->range = range1;
  651. } else {
  652. dev_err(&vreg->pdev->dev, "%s: voltage range=%d is invalid\n",
  653. __func__, reg);
  654. rc = -EINVAL;
  655. }
  656. return rc;
  657. }
  658. static int qpnp_ult_hf_init_range(struct spm_vreg *vreg)
  659. {
  660. int rc;
  661. u8 reg = 0;
  662. uint val;
  663. rc = regmap_read(vreg->regmap,
  664. vreg->spmi_base_addr + QPNP_SMPS_REG_VOLTAGE_SETPOINT,
  665. &val);
  666. if (rc) {
  667. dev_err(&vreg->pdev->dev,
  668. "%s: could not read voltage range register, rc=%d\n",
  669. __func__, rc);
  670. return rc;
  671. }
  672. reg = (u8)val;
  673. vreg->range = (reg < ULT_SMPS_RANGE_SPLIT) ? &ult_hf_range0 :
  674. &ult_hf_range1;
  675. return rc;
  676. }
  677. static int qpnp_smps_init_voltage(struct spm_vreg *vreg)
  678. {
  679. int rc;
  680. rc = qpnp_smps_read_voltage(vreg);
  681. if (rc) {
  682. pr_err("%s: voltage read failed, rc=%d\n", vreg->rdesc.name,
  683. rc);
  684. return rc;
  685. }
  686. vreg->vlevel = vreg->last_set_vlevel;
  687. vreg->uV = vreg->last_set_uV;
  688. /* Initialize SAW voltage control register */
  689. if (!vreg->bypass_spm) {
  690. rc = msm_spm_set_vdd(vreg->cpu_num, vreg->vlevel);
  691. if (rc)
  692. pr_err("%s: msm_spm_set_vdd failed, rc=%d\n",
  693. vreg->rdesc.name, rc);
  694. }
  695. return 0;
  696. }
  697. static int qpnp_smps_init_mode(struct spm_vreg *vreg)
  698. {
  699. const char *mode_name;
  700. int rc;
  701. uint val;
  702. rc = of_property_read_string(vreg->pdev->dev.of_node, "qcom,mode",
  703. &mode_name);
  704. if (!rc) {
  705. if (strcmp("pwm", mode_name) == 0) {
  706. vreg->init_mode = QPNP_LOGICAL_MODE_PWM;
  707. } else if ((strcmp("auto", mode_name) == 0) &&
  708. (vreg->regulator_type != QPNP_TYPE_ULT_HF)) {
  709. vreg->init_mode = QPNP_LOGICAL_MODE_AUTO;
  710. } else {
  711. dev_err(&vreg->pdev->dev,
  712. "%s: unknown regulator mode: %s\n",
  713. __func__, mode_name);
  714. return -EINVAL;
  715. }
  716. rc = qpnp_smps_set_mode(vreg, vreg->init_mode);
  717. if (rc)
  718. return rc;
  719. } else {
  720. rc = regmap_read(vreg->regmap,
  721. vreg->spmi_base_addr + QPNP_SMPS_REG_MODE,
  722. &val);
  723. if (rc)
  724. dev_err(&vreg->pdev->dev,
  725. "%s: could not read mode register, rc=%d\n",
  726. __func__, rc);
  727. vreg->init_mode = qpnp_regval_to_mode(vreg, val);
  728. }
  729. vreg->mode = vreg->init_mode;
  730. return rc;
  731. }
  732. static int qpnp_smps_init_step_rate(struct spm_vreg *vreg)
  733. {
  734. int rc;
  735. u8 reg = 0;
  736. int step = 0, delay;
  737. uint val;
  738. rc = regmap_read(vreg->regmap,
  739. vreg->spmi_base_addr + QPNP_SMPS_REG_STEP_CTRL, &val);
  740. if (rc) {
  741. dev_err(&vreg->pdev->dev,
  742. "%s: could not read stepping control register, rc=%d\n",
  743. __func__, rc);
  744. return rc;
  745. }
  746. reg = (u8)val;
  747. /* ULT and FTS426 bucks do not support steps */
  748. if (vreg->regulator_type != QPNP_TYPE_ULT_HF && vreg->regulator_type !=
  749. QPNP_TYPE_FTS426)
  750. step = (reg & QPNP_SMPS_STEP_CTRL_STEP_MASK)
  751. >> QPNP_SMPS_STEP_CTRL_STEP_SHIFT;
  752. if (vreg->regulator_type == QPNP_TYPE_FTS426) {
  753. delay = (reg & QPNP_FTS426_STEP_CTRL_DELAY_MASK)
  754. >> QPNP_FTS426_STEP_CTRL_DELAY_SHIFT;
  755. /* step_rate has units of uV/us. */
  756. vreg->step_rate = QPNP_FTS426_CLOCK_RATE * vreg->range->step_uV;
  757. } else {
  758. delay = (reg & QPNP_SMPS_STEP_CTRL_DELAY_MASK)
  759. >> QPNP_SMPS_STEP_CTRL_DELAY_SHIFT;
  760. /* step_rate has units of uV/us. */
  761. vreg->step_rate = QPNP_SMPS_CLOCK_RATE * vreg->range->step_uV
  762. * (1 << step);
  763. }
  764. if ((vreg->regulator_type == QPNP_TYPE_ULT_HF)
  765. || (vreg->regulator_type == QPNP_TYPE_HF))
  766. vreg->step_rate /= 1000 * (QPNP_HF_STEP_DELAY << delay);
  767. else if (vreg->regulator_type == QPNP_TYPE_FTS426)
  768. vreg->step_rate /= 1000 * (QPNP_FTS426_STEP_DELAY << delay);
  769. else
  770. vreg->step_rate /= 1000 * (QPNP_FTS2_STEP_DELAY << delay);
  771. if (vreg->regulator_type == QPNP_TYPE_FTS426)
  772. vreg->step_rate = vreg->step_rate * QPNP_FTS426_STEP_MARGIN_NUM
  773. / QPNP_FTS426_STEP_MARGIN_DEN;
  774. else
  775. vreg->step_rate = vreg->step_rate * QPNP_FTS2_STEP_MARGIN_NUM
  776. / QPNP_FTS2_STEP_MARGIN_DEN;
  777. /* Ensure that the stepping rate is greater than 0. */
  778. vreg->step_rate = max(vreg->step_rate, 1);
  779. return rc;
  780. }
  781. static int qpnp_smps_check_constraints(struct spm_vreg *vreg,
  782. struct regulator_init_data *init_data)
  783. {
  784. int rc = 0, limit_min_uV, limit_max_uV;
  785. u16 ul_reg, ll_reg;
  786. u8 reg[2];
  787. limit_min_uV = 0;
  788. limit_max_uV = INT_MAX;
  789. ul_reg = QPNP_FTS_REG_VOLTAGE_ULS_VALID;
  790. ll_reg = QPNP_FTS_REG_VOLTAGE_LLS_VALID;
  791. switch (vreg->regulator_type) {
  792. case QPNP_TYPE_HF:
  793. ul_reg = QPNP_HF_REG_VOLTAGE_ULS;
  794. ll_reg = QPNP_HF_REG_VOLTAGE_LLS;
  795. case QPNP_TYPE_FTS2:
  796. case QPNP_TYPE_FTS2p5:
  797. rc = regmap_bulk_read(vreg->regmap, vreg->spmi_base_addr
  798. + QPNP_SMPS_REG_UL_LL_CTRL, reg, 1);
  799. if (rc) {
  800. dev_err(&vreg->pdev->dev, "%s: UL_LL register read failed, rc=%d\n",
  801. __func__, rc);
  802. return rc;
  803. }
  804. if (reg[0] & QPNP_COMMON_UL_EN_MASK) {
  805. rc = regmap_bulk_read(vreg->regmap, vreg->spmi_base_addr
  806. + ul_reg, &reg[1], 1);
  807. if (rc) {
  808. dev_err(&vreg->pdev->dev, "%s: ULS register read failed, rc=%d\n",
  809. __func__, rc);
  810. return rc;
  811. }
  812. limit_max_uV = spm_regulator_vlevel_to_uv(vreg, reg[1]);
  813. }
  814. if (reg[0] & QPNP_COMMON_LL_EN_MASK) {
  815. rc = regmap_bulk_read(vreg->regmap, vreg->spmi_base_addr
  816. + ll_reg, &reg[1], 1);
  817. if (rc) {
  818. dev_err(&vreg->pdev->dev, "%s: LLS register read failed, rc=%d\n",
  819. __func__, rc);
  820. return rc;
  821. }
  822. limit_min_uV = spm_regulator_vlevel_to_uv(vreg, reg[1]);
  823. }
  824. break;
  825. case QPNP_TYPE_FTS426:
  826. rc = regmap_bulk_read(vreg->regmap, vreg->spmi_base_addr
  827. + QPNP_FTS426_REG_VOLTAGE_ULS_LB,
  828. reg, 2);
  829. if (rc) {
  830. dev_err(&vreg->pdev->dev, "%s: could not read voltage limit registers, rc=%d\n",
  831. __func__, rc);
  832. return rc;
  833. }
  834. limit_max_uV = spm_regulator_vlevel_to_uv(vreg,
  835. ((unsigned)reg[1] << 8) | reg[0]);
  836. break;
  837. case QPNP_TYPE_ULT_HF:
  838. /* no HW voltage limit configuration */
  839. break;
  840. }
  841. if (init_data->constraints.min_uV < limit_min_uV
  842. || init_data->constraints.max_uV > limit_max_uV) {
  843. dev_err(&vreg->pdev->dev, "regulator min/max(%d/%d) constraints do not fit within HW configured min/max(%d/%d) constraints\n",
  844. init_data->constraints.min_uV,
  845. init_data->constraints.max_uV, limit_min_uV,
  846. limit_max_uV);
  847. return -EINVAL;
  848. }
  849. return rc;
  850. }
  851. static bool spm_regulator_using_range0(struct spm_vreg *vreg)
  852. {
  853. return vreg->range == &fts2_range0 || vreg->range == &fts2p5_range0
  854. || vreg->range == &ult_hf_range0 || vreg->range == &hf_range0
  855. || vreg->range == &fts426_range;
  856. }
  857. /* Register a regulator to enable/disable AVS and set AVS min/max limits. */
  858. static int spm_regulator_avs_register(struct spm_vreg *vreg,
  859. struct device *dev, struct device_node *node)
  860. {
  861. struct regulator_config reg_config = {};
  862. struct device_node *avs_node = NULL;
  863. struct device_node *child_node;
  864. struct regulator_init_data *init_data;
  865. int rc;
  866. /*
  867. * Find the first available child node (if any). It corresponds to an
  868. * AVS limits regulator.
  869. */
  870. for_each_available_child_of_node(node, child_node) {
  871. avs_node = child_node;
  872. break;
  873. }
  874. if (!avs_node)
  875. return 0;
  876. init_data = of_get_regulator_init_data(dev, avs_node, &vreg->avs_rdesc);
  877. if (!init_data) {
  878. dev_err(dev, "%s: unable to allocate memory\n", __func__);
  879. return -ENOMEM;
  880. }
  881. init_data->constraints.input_uV = init_data->constraints.max_uV;
  882. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_STATUS
  883. | REGULATOR_CHANGE_VOLTAGE;
  884. if (!init_data->constraints.name) {
  885. dev_err(dev, "%s: AVS node is missing regulator name\n",
  886. __func__);
  887. return -EINVAL;
  888. }
  889. vreg->avs_rdesc.name = init_data->constraints.name;
  890. vreg->avs_rdesc.type = REGULATOR_VOLTAGE;
  891. vreg->avs_rdesc.owner = THIS_MODULE;
  892. vreg->avs_rdesc.ops = &spm_regulator_avs_ops;
  893. vreg->avs_rdesc.n_voltages
  894. = (vreg->range->max_uV - vreg->range->set_point_min_uV)
  895. / vreg->range->step_uV + 1;
  896. reg_config.dev = dev;
  897. reg_config.init_data = init_data;
  898. reg_config.driver_data = vreg;
  899. reg_config.of_node = avs_node;
  900. vreg->avs_rdev = regulator_register(&vreg->avs_rdesc, &reg_config);
  901. if (IS_ERR(vreg->avs_rdev)) {
  902. rc = PTR_ERR(vreg->avs_rdev);
  903. dev_err(dev, "%s: AVS regulator_register failed, rc=%d\n",
  904. __func__, rc);
  905. return rc;
  906. }
  907. if (vreg->bypass_spm)
  908. pr_debug("%s: SPM bypassed so AVS regulator calls are no-ops\n",
  909. vreg->avs_rdesc.name);
  910. return 0;
  911. }
  912. static int spm_regulator_probe(struct platform_device *pdev)
  913. {
  914. struct regulator_config reg_config = {};
  915. struct device_node *node = pdev->dev.of_node;
  916. struct regulator_init_data *init_data;
  917. struct spm_vreg *vreg;
  918. unsigned int base;
  919. bool bypass_spm;
  920. int rc;
  921. if (!node) {
  922. dev_err(&pdev->dev, "%s: device node missing\n", __func__);
  923. return -ENODEV;
  924. }
  925. bypass_spm = of_property_read_bool(node, "qcom,bypass-spm");
  926. if (!bypass_spm) {
  927. rc = msm_spm_probe_done();
  928. if (rc) {
  929. if (rc != -EPROBE_DEFER)
  930. dev_err(&pdev->dev,
  931. "%s: spm unavailable, rc=%d\n",
  932. __func__, rc);
  933. return rc;
  934. }
  935. }
  936. vreg = devm_kzalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
  937. if (!vreg) {
  938. pr_err("allocation failed.\n");
  939. return -ENOMEM;
  940. }
  941. vreg->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  942. if (!vreg->regmap) {
  943. dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
  944. return -EINVAL;
  945. }
  946. vreg->pdev = pdev;
  947. vreg->bypass_spm = bypass_spm;
  948. rc = of_property_read_u32(pdev->dev.of_node, "reg", &base);
  949. if (rc < 0) {
  950. dev_err(&pdev->dev,
  951. "Couldn't find reg in node = %s rc = %d\n",
  952. pdev->dev.of_node->full_name, rc);
  953. return rc;
  954. }
  955. vreg->spmi_base_addr = base;
  956. rc = qpnp_smps_check_type(vreg);
  957. if (rc)
  958. return rc;
  959. /* Specify CPU 0 as default in order to handle shared regulator case. */
  960. vreg->cpu_num = 0;
  961. of_property_read_u32(vreg->pdev->dev.of_node, "qcom,cpu-num",
  962. &vreg->cpu_num);
  963. of_property_read_u32(vreg->pdev->dev.of_node, "qcom,recal-mask",
  964. &vreg->recal_cluster_mask);
  965. /*
  966. * The regulator must be initialized to range 0 or range 1 during
  967. * PMIC power on sequence. Once it is set, it cannot be changed
  968. * dynamically.
  969. */
  970. if (vreg->regulator_type == QPNP_TYPE_FTS2)
  971. rc = qpnp_smps_init_range(vreg, &fts2_range0, &fts2_range1);
  972. else if (vreg->regulator_type == QPNP_TYPE_FTS2p5)
  973. rc = qpnp_smps_init_range(vreg, &fts2p5_range0, &fts2p5_range1);
  974. else if (vreg->regulator_type == QPNP_TYPE_FTS426)
  975. vreg->range = &fts426_range;
  976. else if (vreg->regulator_type == QPNP_TYPE_HF)
  977. rc = qpnp_smps_init_range(vreg, &hf_range0, &hf_range1);
  978. else if (vreg->regulator_type == QPNP_TYPE_ULT_HF)
  979. rc = qpnp_ult_hf_init_range(vreg);
  980. if (rc)
  981. return rc;
  982. rc = qpnp_smps_init_voltage(vreg);
  983. if (rc)
  984. return rc;
  985. rc = qpnp_smps_init_mode(vreg);
  986. if (rc)
  987. return rc;
  988. rc = qpnp_smps_init_step_rate(vreg);
  989. if (rc)
  990. return rc;
  991. init_data = of_get_regulator_init_data(&pdev->dev, node, &vreg->rdesc);
  992. if (!init_data) {
  993. dev_err(&pdev->dev, "%s: unable to allocate memory\n",
  994. __func__);
  995. return -ENOMEM;
  996. }
  997. init_data->constraints.input_uV = init_data->constraints.max_uV;
  998. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_STATUS
  999. | REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_MODE;
  1000. init_data->constraints.valid_modes_mask
  1001. = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
  1002. if (!init_data->constraints.name) {
  1003. dev_err(&pdev->dev, "%s: node is missing regulator name\n",
  1004. __func__);
  1005. return -EINVAL;
  1006. }
  1007. rc = qpnp_smps_check_constraints(vreg, init_data);
  1008. if (rc) {
  1009. dev_err(&pdev->dev, "%s: regulator constraints check failed, rc=%d\n",
  1010. __func__, rc);
  1011. return rc;
  1012. }
  1013. vreg->rdesc.name = init_data->constraints.name;
  1014. vreg->rdesc.type = REGULATOR_VOLTAGE;
  1015. vreg->rdesc.owner = THIS_MODULE;
  1016. vreg->rdesc.ops = &spm_regulator_ops;
  1017. vreg->rdesc.n_voltages
  1018. = (vreg->range->max_uV - vreg->range->set_point_min_uV)
  1019. / vreg->range->step_uV + 1;
  1020. vreg->max_step_uV = SPM_REGULATOR_MAX_STEP_UV;
  1021. of_property_read_u32(vreg->pdev->dev.of_node,
  1022. "qcom,max-voltage-step", &vreg->max_step_uV);
  1023. if (vreg->max_step_uV > SPM_REGULATOR_MAX_STEP_UV)
  1024. vreg->max_step_uV = SPM_REGULATOR_MAX_STEP_UV;
  1025. vreg->max_step_uV = rounddown(vreg->max_step_uV, vreg->range->step_uV);
  1026. pr_debug("%s: max single voltage step size=%u uV\n",
  1027. vreg->rdesc.name, vreg->max_step_uV);
  1028. reg_config.dev = &pdev->dev;
  1029. reg_config.init_data = init_data;
  1030. reg_config.driver_data = vreg;
  1031. reg_config.of_node = node;
  1032. vreg->rdev = regulator_register(&vreg->rdesc, &reg_config);
  1033. if (IS_ERR(vreg->rdev)) {
  1034. rc = PTR_ERR(vreg->rdev);
  1035. dev_err(&pdev->dev, "%s: regulator_register failed, rc=%d\n",
  1036. __func__, rc);
  1037. return rc;
  1038. }
  1039. rc = spm_regulator_avs_register(vreg, &pdev->dev, node);
  1040. if (rc) {
  1041. regulator_unregister(vreg->rdev);
  1042. return rc;
  1043. }
  1044. dev_set_drvdata(&pdev->dev, vreg);
  1045. pr_info("name=%s, range=%s, voltage=%d uV, mode=%s, step rate=%d uV/us\n",
  1046. vreg->rdesc.name,
  1047. spm_regulator_using_range0(vreg) ? "LV" : "MV",
  1048. vreg->uV,
  1049. vreg->init_mode == QPNP_LOGICAL_MODE_PWM ? "PWM" :
  1050. (vreg->init_mode == QPNP_LOGICAL_MODE_AUTO ? "AUTO" : "PFM"),
  1051. vreg->step_rate);
  1052. return rc;
  1053. }
  1054. static int spm_regulator_remove(struct platform_device *pdev)
  1055. {
  1056. struct spm_vreg *vreg = dev_get_drvdata(&pdev->dev);
  1057. if (vreg->avs_rdev)
  1058. regulator_unregister(vreg->avs_rdev);
  1059. regulator_unregister(vreg->rdev);
  1060. return 0;
  1061. }
  1062. static struct of_device_id spm_regulator_match_table[] = {
  1063. { .compatible = SPM_REGULATOR_DRIVER_NAME, },
  1064. {}
  1065. };
  1066. static const struct platform_device_id spm_regulator_id[] = {
  1067. { SPM_REGULATOR_DRIVER_NAME, 0 },
  1068. {}
  1069. };
  1070. MODULE_DEVICE_TABLE(spmi, spm_regulator_id);
  1071. static struct platform_driver spm_regulator_driver = {
  1072. .driver = {
  1073. .name = SPM_REGULATOR_DRIVER_NAME,
  1074. .of_match_table = spm_regulator_match_table,
  1075. .owner = THIS_MODULE,
  1076. },
  1077. .probe = spm_regulator_probe,
  1078. .remove = spm_regulator_remove,
  1079. .id_table = spm_regulator_id,
  1080. };
  1081. /**
  1082. * spm_regulator_init() - register spmi driver for spm-regulator
  1083. *
  1084. * This initialization function should be called in systems in which driver
  1085. * registration ordering must be controlled precisely.
  1086. *
  1087. * Returns 0 on success or errno on failure.
  1088. */
  1089. int __init spm_regulator_init(void)
  1090. {
  1091. static bool has_registered;
  1092. if (has_registered)
  1093. return 0;
  1094. else
  1095. has_registered = true;
  1096. return platform_driver_register(&spm_regulator_driver);
  1097. }
  1098. EXPORT_SYMBOL(spm_regulator_init);
  1099. static void __exit spm_regulator_exit(void)
  1100. {
  1101. platform_driver_unregister(&spm_regulator_driver);
  1102. }
  1103. arch_initcall(spm_regulator_init);
  1104. module_exit(spm_regulator_exit);
  1105. MODULE_LICENSE("GPL v2");
  1106. MODULE_DESCRIPTION("SPM regulator driver");
  1107. MODULE_ALIAS("platform:spm-regulator");