cpr4-mmss-ldo-regulator.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/bitops.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_opp.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/regulator/driver.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/regulator/of_regulator.h>
  33. #include <linux/regulator/msm-ldo-regulator.h>
  34. #include "cpr3-regulator.h"
  35. #define SDM660_MMSS_FUSE_CORNERS 6
  36. /**
  37. * struct cpr4_sdm660_mmss_fuses - MMSS specific fuse data for SDM660
  38. * @init_voltage: Initial (i.e. open-loop) voltage fuse parameter value
  39. * for each fuse corner (raw, not converted to a voltage)
  40. * @offset_voltage: The closed-loop voltage margin adjustment fuse parameter
  41. * value for each fuse corner (raw, not converted to a
  42. * voltage)
  43. * @cpr_fusing_rev: CPR fusing revision fuse parameter value
  44. * @ldo_enable: The ldo enable fuse parameter for each fuse corner
  45. * indicates that VDD_GFX can be configured to LDO mode in
  46. * the corresponding fuse corner.
  47. * @ldo_cpr_cl_enable: A fuse parameter indicates that GFX CPR can be
  48. * configured to operate in closed-loop mode when VDD_GFX
  49. * is configured for LDO sub-regulated mode.
  50. *
  51. * This struct holds the values for all of the fuses read from memory.
  52. */
  53. struct cpr4_sdm660_mmss_fuses {
  54. u64 init_voltage[SDM660_MMSS_FUSE_CORNERS];
  55. u64 offset_voltage[SDM660_MMSS_FUSE_CORNERS];
  56. u64 cpr_fusing_rev;
  57. u64 ldo_enable[SDM660_MMSS_FUSE_CORNERS];
  58. u64 ldo_cpr_cl_enable;
  59. };
  60. /* Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 */
  61. #define CPR4_SDM660_MMSS_FUSE_COMBO_COUNT 8
  62. /*
  63. * SDM660 MMSS fuse parameter locations:
  64. *
  65. * Structs are organized with the following dimensions:
  66. * Outer: 0 to 3 for fuse corners from lowest to highest corner
  67. * Inner: large enough to hold the longest set of parameter segments which
  68. * fully defines a fuse parameter, +1 (for NULL termination).
  69. * Each segment corresponds to a contiguous group of bits from a
  70. * single fuse row. These segments are concatentated together in
  71. * order to form the full fuse parameter value. The segments for
  72. * a given parameter may correspond to different fuse rows.
  73. */
  74. static const struct cpr3_fuse_param
  75. sdm660_mmss_init_voltage_param[SDM660_MMSS_FUSE_CORNERS][2] = {
  76. {{65, 39, 43}, {} },
  77. {{65, 39, 43}, {} },
  78. {{65, 34, 38}, {} },
  79. {{65, 34, 38}, {} },
  80. {{65, 29, 33}, {} },
  81. {{65, 24, 28}, {} },
  82. };
  83. static const struct cpr3_fuse_param sdm660_cpr_fusing_rev_param[] = {
  84. {71, 34, 36},
  85. {},
  86. };
  87. static const struct cpr3_fuse_param
  88. sdm660_mmss_offset_voltage_param[SDM660_MMSS_FUSE_CORNERS][2] = {
  89. {{} },
  90. {{} },
  91. {{} },
  92. {{65, 52, 55}, {} },
  93. {{65, 48, 51}, {} },
  94. {{65, 44, 47}, {} },
  95. };
  96. static const struct cpr3_fuse_param
  97. sdm660_mmss_ldo_enable_param[SDM660_MMSS_FUSE_CORNERS][2] = {
  98. {{73, 62, 62}, {} },
  99. {{73, 61, 61}, {} },
  100. {{73, 60, 60}, {} },
  101. {{73, 59, 59}, {} },
  102. {{73, 58, 58}, {} },
  103. {{73, 57, 57}, {} },
  104. };
  105. static const struct cpr3_fuse_param sdm660_ldo_cpr_cl_enable_param[] = {
  106. {71, 38, 38},
  107. {},
  108. };
  109. /* Additional SDM660 specific data: */
  110. /* Open loop voltage fuse reference voltages in microvolts */
  111. static const int sdm660_mmss_fuse_ref_volt[SDM660_MMSS_FUSE_CORNERS] = {
  112. 585000,
  113. 645000,
  114. 725000,
  115. 790000,
  116. 870000,
  117. 925000,
  118. };
  119. #define SDM660_MMSS_FUSE_STEP_VOLT 10000
  120. #define SDM660_MMSS_OFFSET_FUSE_STEP_VOLT 10000
  121. #define SDM660_MMSS_VOLTAGE_FUSE_SIZE 5
  122. #define SDM660_MMSS_CPR_SENSOR_COUNT 11
  123. #define SDM660_MMSS_CPR_CLOCK_RATE 19200000
  124. /**
  125. * cpr4_sdm660_mmss_read_fuse_data() - load MMSS specific fuse parameter
  126. * values
  127. * @vreg: Pointer to the CPR3 regulator
  128. *
  129. * This function allocates a cpr4_sdm660_mmss_fuses struct, fills it with
  130. * values read out of hardware fuses, and finally copies common fuse values
  131. * into the regulator struct.
  132. *
  133. * Return: 0 on success, errno on failure
  134. */
  135. static int cpr4_sdm660_mmss_read_fuse_data(struct cpr3_regulator *vreg)
  136. {
  137. void __iomem *base = vreg->thread->ctrl->fuse_base;
  138. struct cpr4_sdm660_mmss_fuses *fuse;
  139. int i, rc;
  140. fuse = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*fuse), GFP_KERNEL);
  141. if (!fuse)
  142. return -ENOMEM;
  143. rc = cpr3_read_fuse_param(base, sdm660_cpr_fusing_rev_param,
  144. &fuse->cpr_fusing_rev);
  145. if (rc) {
  146. cpr3_err(vreg, "Unable to read CPR fusing revision fuse, rc=%d\n",
  147. rc);
  148. return rc;
  149. }
  150. cpr3_info(vreg, "CPR fusing revision = %llu\n", fuse->cpr_fusing_rev);
  151. rc = cpr3_read_fuse_param(base, sdm660_ldo_cpr_cl_enable_param,
  152. &fuse->ldo_cpr_cl_enable);
  153. if (rc) {
  154. cpr3_err(vreg, "Unable to read ldo cpr closed-loop enable fuse, rc=%d\n",
  155. rc);
  156. return rc;
  157. }
  158. for (i = 0; i < SDM660_MMSS_FUSE_CORNERS; i++) {
  159. rc = cpr3_read_fuse_param(base,
  160. sdm660_mmss_init_voltage_param[i],
  161. &fuse->init_voltage[i]);
  162. if (rc) {
  163. cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n",
  164. i, rc);
  165. return rc;
  166. }
  167. rc = cpr3_read_fuse_param(base,
  168. sdm660_mmss_offset_voltage_param[i],
  169. &fuse->offset_voltage[i]);
  170. if (rc) {
  171. cpr3_err(vreg, "Unable to read fuse-corner %d offset voltage fuse, rc=%d\n",
  172. i, rc);
  173. return rc;
  174. }
  175. rc = cpr3_read_fuse_param(base,
  176. sdm660_mmss_ldo_enable_param[i],
  177. &fuse->ldo_enable[i]);
  178. if (rc) {
  179. cpr3_err(vreg, "Unable to read fuse-corner %d ldo enable fuse, rc=%d\n",
  180. i, rc);
  181. return rc;
  182. }
  183. }
  184. vreg->fuse_combo = fuse->cpr_fusing_rev;
  185. if (vreg->fuse_combo >= CPR4_SDM660_MMSS_FUSE_COMBO_COUNT) {
  186. cpr3_err(vreg, "invalid CPR fuse combo = %d found, not in range 0 - %d\n",
  187. vreg->fuse_combo,
  188. CPR4_SDM660_MMSS_FUSE_COMBO_COUNT - 1);
  189. return -EINVAL;
  190. }
  191. vreg->cpr_rev_fuse = fuse->cpr_fusing_rev;
  192. vreg->fuse_corner_count = SDM660_MMSS_FUSE_CORNERS;
  193. vreg->platform_fuses = fuse;
  194. return 0;
  195. }
  196. /**
  197. * cpr3_sdm660_mmss_calculate_open_loop_voltages() - calculate the open-loop
  198. * voltage for each corner of a CPR3 regulator
  199. * @vreg: Pointer to the CPR3 regulator
  200. *
  201. * Return: 0 on success, errno on failure
  202. */
  203. static int cpr4_sdm660_mmss_calculate_open_loop_voltages(
  204. struct cpr3_regulator *vreg)
  205. {
  206. struct cpr4_sdm660_mmss_fuses *fuse = vreg->platform_fuses;
  207. int i, rc = 0;
  208. const int *ref_volt;
  209. int *fuse_volt;
  210. fuse_volt = kcalloc(vreg->fuse_corner_count, sizeof(*fuse_volt),
  211. GFP_KERNEL);
  212. if (!fuse_volt)
  213. return -ENOMEM;
  214. ref_volt = sdm660_mmss_fuse_ref_volt;
  215. for (i = 0; i < vreg->fuse_corner_count; i++) {
  216. fuse_volt[i] = cpr3_convert_open_loop_voltage_fuse(ref_volt[i],
  217. SDM660_MMSS_FUSE_STEP_VOLT, fuse->init_voltage[i],
  218. SDM660_MMSS_VOLTAGE_FUSE_SIZE);
  219. cpr3_info(vreg, "fuse_corner[%d] open-loop=%7d uV\n",
  220. i, fuse_volt[i]);
  221. }
  222. rc = cpr3_adjust_fused_open_loop_voltages(vreg, fuse_volt);
  223. if (rc) {
  224. cpr3_err(vreg, "fused open-loop voltage adjustment failed, rc=%d\n",
  225. rc);
  226. goto done;
  227. }
  228. for (i = 1; i < vreg->fuse_corner_count; i++) {
  229. if (fuse_volt[i] < fuse_volt[i - 1]) {
  230. cpr3_debug(vreg, "fuse corner %d voltage=%d uV < fuse corner %d voltage=%d uV; overriding: fuse corner %d voltage=%d\n",
  231. i, fuse_volt[i], i - 1, fuse_volt[i - 1],
  232. i, fuse_volt[i - 1]);
  233. fuse_volt[i] = fuse_volt[i - 1];
  234. }
  235. }
  236. for (i = 0; i < vreg->corner_count; i++)
  237. vreg->corner[i].open_loop_volt
  238. = fuse_volt[vreg->corner[i].cpr_fuse_corner];
  239. cpr3_debug(vreg, "unadjusted per-corner open-loop voltages:\n");
  240. for (i = 0; i < vreg->corner_count; i++)
  241. cpr3_debug(vreg, "open-loop[%2d] = %d uV\n", i,
  242. vreg->corner[i].open_loop_volt);
  243. rc = cpr3_adjust_open_loop_voltages(vreg);
  244. if (rc)
  245. cpr3_err(vreg, "open-loop voltage adjustment failed, rc=%d\n",
  246. rc);
  247. done:
  248. kfree(fuse_volt);
  249. return rc;
  250. }
  251. /**
  252. * cpr4_mmss_parse_ldo_mode_data() - Parse the LDO mode enable state for each
  253. * corner of a CPR3 regulator
  254. * @vreg: Pointer to the CPR3 regulator
  255. *
  256. * This function considers 2 sets of data: one set from device node and other
  257. * set from fuses and applies set intersection to decide the final LDO mode
  258. * enable state of each corner. If the device node configuration is not
  259. * specified, then the function applies LDO mode disable for all corners.
  260. *
  261. * Return: 0 on success, errno on failure
  262. */
  263. static int cpr4_mmss_parse_ldo_mode_data(struct cpr3_regulator *vreg)
  264. {
  265. struct cpr4_sdm660_mmss_fuses *fuse = vreg->platform_fuses;
  266. int i, rc = 0;
  267. u32 *ldo_allowed;
  268. char *prop_str = "qcom,cpr-corner-allow-ldo-mode";
  269. if (!of_find_property(vreg->of_node, prop_str, NULL)) {
  270. cpr3_debug(vreg, "%s property is missing. LDO mode is disabled for all corners\n",
  271. prop_str);
  272. return 0;
  273. }
  274. ldo_allowed = kcalloc(vreg->corner_count, sizeof(*ldo_allowed),
  275. GFP_KERNEL);
  276. if (!ldo_allowed)
  277. return -ENOMEM;
  278. rc = cpr3_parse_corner_array_property(vreg, prop_str, 1, ldo_allowed);
  279. if (rc) {
  280. cpr3_err(vreg, "%s read failed, rc=%d\n", prop_str, rc);
  281. goto done;
  282. }
  283. for (i = 0; i < vreg->corner_count; i++)
  284. vreg->corner[i].ldo_mode_allowed
  285. = (ldo_allowed[i] && fuse->ldo_enable[i]);
  286. done:
  287. kfree(ldo_allowed);
  288. return rc;
  289. }
  290. /**
  291. * cpr4_mmss_parse_corner_operating_mode() - Parse the CPR closed-loop operation
  292. * enable state for each corner of a CPR3 regulator
  293. * @vreg: Pointer to the CPR3 regulator
  294. *
  295. * This function ensures that closed-loop operation is enabled only for LDO
  296. * mode allowed corners.
  297. *
  298. * Return: 0 on success, errno on failure
  299. */
  300. static int cpr4_mmss_parse_corner_operating_mode(struct cpr3_regulator *vreg)
  301. {
  302. struct cpr4_sdm660_mmss_fuses *fuse = vreg->platform_fuses;
  303. int i, rc = 0;
  304. u32 *use_closed_loop;
  305. char *prop_str = "qcom,cpr-corner-allow-closed-loop";
  306. if (!of_find_property(vreg->of_node, prop_str, NULL)) {
  307. cpr3_debug(vreg, "%s property is missing. Use open-loop for all corners\n",
  308. prop_str);
  309. for (i = 0; i < vreg->corner_count; i++)
  310. vreg->corner[i].use_open_loop = true;
  311. return 0;
  312. }
  313. use_closed_loop = kcalloc(vreg->corner_count, sizeof(*use_closed_loop),
  314. GFP_KERNEL);
  315. if (!use_closed_loop)
  316. return -ENOMEM;
  317. rc = cpr3_parse_corner_array_property(vreg, prop_str, 1,
  318. use_closed_loop);
  319. if (rc) {
  320. cpr3_err(vreg, "%s read failed, rc=%d\n", prop_str, rc);
  321. goto done;
  322. }
  323. for (i = 0; i < vreg->corner_count; i++)
  324. vreg->corner[i].use_open_loop
  325. = !(fuse->ldo_cpr_cl_enable && use_closed_loop[i]
  326. && vreg->corner[i].ldo_mode_allowed);
  327. done:
  328. kfree(use_closed_loop);
  329. return rc;
  330. }
  331. /**
  332. * cpr4_mmss_parse_corner_data() - parse MMSS corner data from device tree
  333. * properties of the regulator's device node
  334. * @vreg: Pointer to the CPR3 regulator
  335. *
  336. * Return: 0 on success, errno on failure
  337. */
  338. static int cpr4_mmss_parse_corner_data(struct cpr3_regulator *vreg)
  339. {
  340. int i, rc;
  341. u32 *temp;
  342. rc = cpr3_parse_common_corner_data(vreg);
  343. if (rc) {
  344. cpr3_err(vreg, "error reading corner data, rc=%d\n", rc);
  345. return rc;
  346. }
  347. temp = kcalloc(vreg->corner_count * CPR3_RO_COUNT, sizeof(*temp),
  348. GFP_KERNEL);
  349. if (!temp)
  350. return -ENOMEM;
  351. rc = cpr3_parse_corner_array_property(vreg, "qcom,cpr-target-quotients",
  352. CPR3_RO_COUNT, temp);
  353. if (rc) {
  354. cpr3_err(vreg, "could not load target quotients, rc=%d\n", rc);
  355. goto done;
  356. }
  357. for (i = 0; i < vreg->corner_count; i++)
  358. memcpy(vreg->corner[i].target_quot, &temp[i * CPR3_RO_COUNT],
  359. sizeof(*temp) * CPR3_RO_COUNT);
  360. done:
  361. kfree(temp);
  362. return rc;
  363. }
  364. /**
  365. * cpr4_sdm660_mmss_adjust_target_quotients() - adjust the target quotients for
  366. * each corner according to device tree values and fuse values
  367. * @vreg: Pointer to the CPR3 regulator
  368. *
  369. * Return: 0 on success, errno on failure
  370. */
  371. static int cpr4_sdm660_mmss_adjust_target_quotients(struct cpr3_regulator *vreg)
  372. {
  373. struct cpr4_sdm660_mmss_fuses *fuse = vreg->platform_fuses;
  374. const struct cpr3_fuse_param (*offset_param)[2];
  375. int *volt_offset;
  376. int i, fuse_len, rc = 0;
  377. volt_offset = kcalloc(vreg->fuse_corner_count, sizeof(*volt_offset),
  378. GFP_KERNEL);
  379. if (!volt_offset)
  380. return -ENOMEM;
  381. offset_param = sdm660_mmss_offset_voltage_param;
  382. for (i = 0; i < vreg->fuse_corner_count; i++) {
  383. fuse_len = offset_param[i][0].bit_end + 1
  384. - offset_param[i][0].bit_start;
  385. volt_offset[i] = cpr3_convert_open_loop_voltage_fuse(
  386. 0, SDM660_MMSS_OFFSET_FUSE_STEP_VOLT,
  387. fuse->offset_voltage[i], fuse_len);
  388. if (volt_offset[i])
  389. cpr3_info(vreg, "fuse_corner[%d] offset=%7d uV\n",
  390. i, volt_offset[i]);
  391. }
  392. rc = cpr3_adjust_target_quotients(vreg, volt_offset);
  393. if (rc)
  394. cpr3_err(vreg, "adjust target quotients failed, rc=%d\n", rc);
  395. kfree(volt_offset);
  396. return rc;
  397. }
  398. /**
  399. * cpr4_mmss_print_settings() - print out MMSS CPR configuration settings into
  400. * the kernel log for debugging purposes
  401. * @vreg: Pointer to the CPR3 regulator
  402. */
  403. static void cpr4_mmss_print_settings(struct cpr3_regulator *vreg)
  404. {
  405. struct cpr3_corner *corner;
  406. int i;
  407. cpr3_debug(vreg, "Corner: Frequency (Hz), Fuse Corner, Floor (uV), Open-Loop (uV), Ceiling (uV)\n");
  408. for (i = 0; i < vreg->corner_count; i++) {
  409. corner = &vreg->corner[i];
  410. cpr3_debug(vreg, "%3d: %10u, %2d, %7d, %7d, %7d\n",
  411. i, corner->proc_freq, corner->cpr_fuse_corner,
  412. corner->floor_volt, corner->open_loop_volt,
  413. corner->ceiling_volt);
  414. }
  415. }
  416. /**
  417. * cpr4_mmss_init_thread() - perform all steps necessary to initialize the
  418. * configuration data for a CPR3 thread
  419. * @thread: Pointer to the CPR3 thread
  420. *
  421. * Return: 0 on success, errno on failure
  422. */
  423. static int cpr4_mmss_init_thread(struct cpr3_thread *thread)
  424. {
  425. struct cpr3_controller *ctrl = thread->ctrl;
  426. struct cpr3_regulator *vreg = &thread->vreg[0];
  427. int rc;
  428. rc = cpr3_parse_common_thread_data(thread);
  429. if (rc) {
  430. cpr3_err(vreg, "unable to read CPR thread data from device tree, rc=%d\n",
  431. rc);
  432. return rc;
  433. }
  434. if (!of_find_property(ctrl->dev->of_node, "vdd-thread0-ldo-supply",
  435. NULL)) {
  436. cpr3_err(vreg, "ldo supply regulator is not specified\n");
  437. return -EINVAL;
  438. }
  439. vreg->ldo_regulator = devm_regulator_get(ctrl->dev, "vdd-thread0-ldo");
  440. if (IS_ERR(vreg->ldo_regulator)) {
  441. rc = PTR_ERR(vreg->ldo_regulator);
  442. if (rc != -EPROBE_DEFER)
  443. cpr3_err(vreg, "unable to request vdd-thread0-ldo regulator, rc=%d\n",
  444. rc);
  445. return rc;
  446. }
  447. vreg->ldo_mode_allowed = !of_property_read_bool(vreg->of_node,
  448. "qcom,ldo-disable");
  449. vreg->ldo_regulator_bypass = BHS_MODE;
  450. vreg->ldo_type = CPR3_LDO300;
  451. rc = cpr4_sdm660_mmss_read_fuse_data(vreg);
  452. if (rc) {
  453. cpr3_err(vreg, "unable to read CPR fuse data, rc=%d\n", rc);
  454. return rc;
  455. }
  456. rc = cpr4_mmss_parse_corner_data(vreg);
  457. if (rc) {
  458. cpr3_err(vreg, "unable to read CPR corner data from device tree, rc=%d\n",
  459. rc);
  460. return rc;
  461. }
  462. rc = cpr4_sdm660_mmss_adjust_target_quotients(vreg);
  463. if (rc) {
  464. cpr3_err(vreg, "unable to adjust target quotients, rc=%d\n",
  465. rc);
  466. return rc;
  467. }
  468. rc = cpr4_sdm660_mmss_calculate_open_loop_voltages(vreg);
  469. if (rc) {
  470. cpr3_err(vreg, "unable to calculate open-loop voltages, rc=%d\n",
  471. rc);
  472. return rc;
  473. }
  474. rc = cpr3_limit_open_loop_voltages(vreg);
  475. if (rc) {
  476. cpr3_err(vreg, "unable to limit open-loop voltages, rc=%d\n",
  477. rc);
  478. return rc;
  479. }
  480. cpr3_open_loop_voltage_as_ceiling(vreg);
  481. rc = cpr3_limit_floor_voltages(vreg);
  482. if (rc) {
  483. cpr3_err(vreg, "unable to limit floor voltages, rc=%d\n", rc);
  484. return rc;
  485. }
  486. rc = cpr4_mmss_parse_ldo_mode_data(vreg);
  487. if (rc) {
  488. cpr3_err(vreg, "unable to parse ldo mode data, rc=%d\n", rc);
  489. return rc;
  490. }
  491. rc = cpr4_mmss_parse_corner_operating_mode(vreg);
  492. if (rc) {
  493. cpr3_err(vreg, "unable to parse closed-loop operating mode data, rc=%d\n",
  494. rc);
  495. return rc;
  496. }
  497. cpr4_mmss_print_settings(vreg);
  498. return 0;
  499. }
  500. /**
  501. * cpr4_mmss_init_controller() - perform MMSS CPR4 controller specific
  502. * initializations
  503. * @ctrl: Pointer to the CPR3 controller
  504. *
  505. * Return: 0 on success, errno on failure
  506. */
  507. static int cpr4_mmss_init_controller(struct cpr3_controller *ctrl)
  508. {
  509. int rc;
  510. rc = cpr3_parse_common_ctrl_data(ctrl);
  511. if (rc) {
  512. if (rc != -EPROBE_DEFER)
  513. cpr3_err(ctrl, "unable to parse common controller data, rc=%d\n",
  514. rc);
  515. return rc;
  516. }
  517. ctrl->sensor_count = SDM660_MMSS_CPR_SENSOR_COUNT;
  518. /*
  519. * MMSS only has one thread (0) so the zeroed array does not need
  520. * further modification.
  521. */
  522. ctrl->sensor_owner = devm_kcalloc(ctrl->dev, ctrl->sensor_count,
  523. sizeof(*ctrl->sensor_owner), GFP_KERNEL);
  524. if (!ctrl->sensor_owner)
  525. return -ENOMEM;
  526. ctrl->cpr_clock_rate = SDM660_MMSS_CPR_CLOCK_RATE;
  527. ctrl->ctrl_type = CPR_CTRL_TYPE_CPR4;
  528. ctrl->support_ldo300_vreg = true;
  529. /*
  530. * Use fixed step quotient if specified otherwise use dynamic
  531. * calculated per RO step quotient
  532. */
  533. of_property_read_u32(ctrl->dev->of_node,
  534. "qcom,cpr-step-quot-fixed",
  535. &ctrl->step_quot_fixed);
  536. ctrl->use_dynamic_step_quot = !ctrl->step_quot_fixed;
  537. /* iface_clk is optional for sdm660 */
  538. ctrl->iface_clk = NULL;
  539. ctrl->bus_clk = devm_clk_get(ctrl->dev, "bus_clk");
  540. if (IS_ERR(ctrl->bus_clk)) {
  541. rc = PTR_ERR(ctrl->bus_clk);
  542. if (rc != -EPROBE_DEFER)
  543. cpr3_err(ctrl, "unable request bus clock, rc=%d\n",
  544. rc);
  545. return rc;
  546. }
  547. return 0;
  548. }
  549. static int cpr4_mmss_regulator_probe(struct platform_device *pdev)
  550. {
  551. struct device *dev = &pdev->dev;
  552. struct cpr3_controller *ctrl;
  553. int rc;
  554. if (!dev->of_node) {
  555. dev_err(dev, "Device tree node is missing\n");
  556. return -EINVAL;
  557. }
  558. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  559. if (!ctrl)
  560. return -ENOMEM;
  561. ctrl->dev = dev;
  562. /* Set to false later if anything precludes CPR operation. */
  563. ctrl->cpr_allowed_hw = true;
  564. rc = of_property_read_string(dev->of_node, "qcom,cpr-ctrl-name",
  565. &ctrl->name);
  566. if (rc) {
  567. cpr3_err(ctrl, "unable to read qcom,cpr-ctrl-name, rc=%d\n",
  568. rc);
  569. return rc;
  570. }
  571. rc = cpr3_map_fuse_base(ctrl, pdev);
  572. if (rc) {
  573. cpr3_err(ctrl, "could not map fuse base address\n");
  574. return rc;
  575. }
  576. rc = cpr3_allocate_threads(ctrl, 0, 0);
  577. if (rc) {
  578. cpr3_err(ctrl, "failed to allocate CPR thread array, rc=%d\n",
  579. rc);
  580. return rc;
  581. }
  582. if (ctrl->thread_count != 1) {
  583. cpr3_err(ctrl, "expected 1 thread but found %d\n",
  584. ctrl->thread_count);
  585. return -EINVAL;
  586. } else if (ctrl->thread[0].vreg_count != 1) {
  587. cpr3_err(ctrl, "expected 1 regulator but found %d\n",
  588. ctrl->thread[0].vreg_count);
  589. return -EINVAL;
  590. }
  591. rc = cpr4_mmss_init_controller(ctrl);
  592. if (rc) {
  593. if (rc != -EPROBE_DEFER)
  594. cpr3_err(ctrl, "failed to initialize CPR controller parameters, rc=%d\n",
  595. rc);
  596. return rc;
  597. }
  598. rc = cpr4_mmss_init_thread(&ctrl->thread[0]);
  599. if (rc) {
  600. cpr3_err(&ctrl->thread[0].vreg[0], "thread initialization failed, rc=%d\n",
  601. rc);
  602. return rc;
  603. }
  604. rc = cpr3_mem_acc_init(&ctrl->thread[0].vreg[0]);
  605. if (rc) {
  606. cpr3_err(ctrl, "failed to initialize mem-acc configuration, rc=%d\n",
  607. rc);
  608. return rc;
  609. }
  610. platform_set_drvdata(pdev, ctrl);
  611. return cpr3_regulator_register(pdev, ctrl);
  612. }
  613. static int cpr4_mmss_regulator_remove(struct platform_device *pdev)
  614. {
  615. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  616. return cpr3_regulator_unregister(ctrl);
  617. }
  618. static int cpr4_mmss_regulator_suspend(struct platform_device *pdev,
  619. pm_message_t state)
  620. {
  621. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  622. return cpr3_regulator_suspend(ctrl);
  623. }
  624. static int cpr4_mmss_regulator_resume(struct platform_device *pdev)
  625. {
  626. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  627. return cpr3_regulator_resume(ctrl);
  628. }
  629. /* Data corresponds to the SoC revision */
  630. static const struct of_device_id cpr4_mmss_regulator_match_table[] = {
  631. {
  632. .compatible = "qcom,cpr4-sdm660-mmss-ldo-regulator",
  633. .data = (void *)NULL,
  634. },
  635. { },
  636. };
  637. static struct platform_driver cpr4_mmss_regulator_driver = {
  638. .driver = {
  639. .name = "qcom,cpr4-mmss-ldo-regulator",
  640. .of_match_table = cpr4_mmss_regulator_match_table,
  641. .owner = THIS_MODULE,
  642. },
  643. .probe = cpr4_mmss_regulator_probe,
  644. .remove = cpr4_mmss_regulator_remove,
  645. .suspend = cpr4_mmss_regulator_suspend,
  646. .resume = cpr4_mmss_regulator_resume,
  647. };
  648. static int cpr_regulator_init(void)
  649. {
  650. return platform_driver_register(&cpr4_mmss_regulator_driver);
  651. }
  652. static void cpr_regulator_exit(void)
  653. {
  654. platform_driver_unregister(&cpr4_mmss_regulator_driver);
  655. }
  656. MODULE_DESCRIPTION("CPR4 MMSS LDO regulator driver");
  657. MODULE_LICENSE("GPL v2");
  658. arch_initcall(cpr_regulator_init);
  659. module_exit(cpr_regulator_exit);