cpr3-mmss-regulator.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * Copyright (c) 2015-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 "cpr3-regulator.h"
  34. #define MSM8996_MMSS_FUSE_CORNERS 4
  35. /**
  36. * struct cpr3_msm8996_mmss_fuses - MMSS specific fuse data for MSM8996
  37. * @init_voltage: Initial (i.e. open-loop) voltage fuse parameter value
  38. * for each fuse corner (raw, not converted to a voltage)
  39. * @offset_voltage: The closed-loop voltage margin adjustment fuse parameter
  40. * value for each fuse corner (raw, not converted to a
  41. * voltage)
  42. * @speed_bin: Graphics processor speed bin fuse parameter value for
  43. * the given chip
  44. * @cpr_fusing_rev: CPR fusing revision fuse parameter value
  45. * @limitation: CPR limitation select fuse parameter value
  46. * @aging_init_quot_diff: Initial quotient difference between CPR aging
  47. * min and max sensors measured at time of manufacturing
  48. * @force_highest_corner: Flag indicating that all corners must operate
  49. * at the voltage of the highest corner. This is
  50. * applicable to MSM8998 only.
  51. *
  52. * This struct holds the values for all of the fuses read from memory.
  53. */
  54. struct cpr3_msm8996_mmss_fuses {
  55. u64 init_voltage[MSM8996_MMSS_FUSE_CORNERS];
  56. u64 offset_voltage[MSM8996_MMSS_FUSE_CORNERS];
  57. u64 speed_bin;
  58. u64 cpr_fusing_rev;
  59. u64 limitation;
  60. u64 aging_init_quot_diff;
  61. u64 force_highest_corner;
  62. };
  63. /* Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 */
  64. #define CPR3_MSM8996_MMSS_FUSE_COMBO_COUNT 8
  65. /*
  66. * Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 with speed bin fuse = 0.
  67. * Fuse combos 8 - 15 map to CPR fusing revision 0 - 7 with speed bin fuse = 1.
  68. */
  69. #define CPR3_MSM8996PRO_MMSS_FUSE_COMBO_COUNT 16
  70. /* Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 */
  71. #define CPR3_MSM8998_MMSS_FUSE_COMBO_COUNT 8
  72. /*
  73. * MSM8996 MMSS fuse parameter locations:
  74. *
  75. * Structs are organized with the following dimensions:
  76. * Outer: 0 to 3 for fuse corners from lowest to highest corner
  77. * Inner: large enough to hold the longest set of parameter segments which
  78. * fully defines a fuse parameter, +1 (for NULL termination).
  79. * Each segment corresponds to a contiguous group of bits from a
  80. * single fuse row. These segments are concatentated together in
  81. * order to form the full fuse parameter value. The segments for
  82. * a given parameter may correspond to different fuse rows.
  83. */
  84. static const struct cpr3_fuse_param
  85. msm8996_mmss_init_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = {
  86. {{63, 55, 59}, {} },
  87. {{63, 50, 54}, {} },
  88. {{63, 45, 49}, {} },
  89. {{63, 40, 44}, {} },
  90. };
  91. static const struct cpr3_fuse_param msm8996_cpr_fusing_rev_param[] = {
  92. {39, 48, 50},
  93. {},
  94. };
  95. static const struct cpr3_fuse_param msm8996_cpr_limitation_param[] = {
  96. {41, 31, 32},
  97. {},
  98. };
  99. static const struct cpr3_fuse_param
  100. msm8996_mmss_aging_init_quot_diff_param[] = {
  101. {68, 26, 31},
  102. {},
  103. };
  104. /* Offset voltages are defined for SVS and Turbo fuse corners only */
  105. static const struct cpr3_fuse_param
  106. msm8996_mmss_offset_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = {
  107. {{} },
  108. {{66, 42, 44}, {} },
  109. {{} },
  110. {{64, 58, 61}, {} },
  111. };
  112. static const struct cpr3_fuse_param msm8996pro_mmss_speed_bin_param[] = {
  113. {39, 60, 61},
  114. {},
  115. };
  116. /* MSM8998 MMSS fuse parameter locations: */
  117. static const struct cpr3_fuse_param
  118. msm8998_mmss_init_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = {
  119. {{65, 39, 43}, {} },
  120. {{65, 34, 38}, {} },
  121. {{65, 29, 33}, {} },
  122. {{65, 24, 28}, {} },
  123. };
  124. static const struct cpr3_fuse_param msm8998_cpr_fusing_rev_param[] = {
  125. {39, 48, 50},
  126. {},
  127. };
  128. static const struct cpr3_fuse_param msm8998_cpr_limitation_param[] = {
  129. {41, 46, 47},
  130. {},
  131. };
  132. static const struct cpr3_fuse_param
  133. msm8998_mmss_aging_init_quot_diff_param[] = {
  134. {65, 60, 63},
  135. {66, 0, 3},
  136. {},
  137. };
  138. static const struct cpr3_fuse_param
  139. msm8998_mmss_offset_voltage_param[MSM8996_MMSS_FUSE_CORNERS][2] = {
  140. {{65, 56, 59}, {} },
  141. {{65, 52, 55}, {} },
  142. {{65, 48, 51}, {} },
  143. {{65, 44, 47}, {} },
  144. };
  145. static const struct cpr3_fuse_param
  146. msm8998_cpr_force_highest_corner_param[] = {
  147. {100, 45, 45},
  148. {},
  149. };
  150. #define MSM8996PRO_SOC_ID 4
  151. #define MSM8998_V1_SOC_ID 5
  152. #define MSM8998_V2_SOC_ID 6
  153. /*
  154. * Some initial msm8996 parts cannot be used in a meaningful way by software.
  155. * Other parts can only be used when operating with CPR disabled (i.e. at the
  156. * fused open-loop voltage) when no voltage interpolation is applied. A fuse
  157. * parameter is provided so that software can properly handle these limitations.
  158. */
  159. enum msm8996_cpr_limitation {
  160. MSM8996_CPR_LIMITATION_NONE = 0,
  161. MSM8996_CPR_LIMITATION_UNSUPPORTED = 2,
  162. MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION = 3,
  163. };
  164. /* Additional MSM8996 specific data: */
  165. /* Open loop voltage fuse reference voltages in microvolts */
  166. static const int msm8996_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = {
  167. 670000,
  168. 745000,
  169. 905000,
  170. 1015000,
  171. };
  172. static const int msm8996pro_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = {
  173. 670000,
  174. 745000,
  175. 905000,
  176. 1065000,
  177. };
  178. static const int msm8998_v1_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = {
  179. 528000,
  180. 656000,
  181. 812000,
  182. 932000,
  183. };
  184. static const int
  185. msm8998_v1_rev0_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = {
  186. 632000,
  187. 768000,
  188. 896000,
  189. 1032000,
  190. };
  191. static const int msm8998_v2_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = {
  192. 516000,
  193. 628000,
  194. 752000,
  195. 924000,
  196. };
  197. static const int
  198. msm8998_v2_rev0_mmss_fuse_ref_volt[MSM8996_MMSS_FUSE_CORNERS] = {
  199. 616000,
  200. 740000,
  201. 828000,
  202. 1024000,
  203. };
  204. #define MSM8996_MMSS_FUSE_STEP_VOLT 10000
  205. #define MSM8996_MMSS_OFFSET_FUSE_STEP_VOLT 10000
  206. #define MSM8996_MMSS_VOLTAGE_FUSE_SIZE 5
  207. #define MSM8996_MMSS_MIN_VOLTAGE_FUSE_VAL 0x1F
  208. #define MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SCALE 2
  209. #define MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SIZE 6
  210. #define MSM8996_MMSS_CPR_SENSOR_COUNT 35
  211. #define MSM8996_MMSS_CPR_CLOCK_RATE 19200000
  212. #define MSM8996_MMSS_AGING_SENSOR_ID 29
  213. #define MSM8996_MMSS_AGING_BYPASS_MASK0 (GENMASK(23, 0))
  214. #define MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SCALE 1
  215. #define MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SIZE 8
  216. #define MSM8998_MMSS_CPR_SENSOR_COUNT 35
  217. #define MSM8998_MMSS_AGING_SENSOR_ID 29
  218. #define MSM8998_MMSS_AGING_BYPASS_MASK0 (GENMASK(23, 0))
  219. #define MSM8998_MMSS_MAX_TEMP_POINTS 3
  220. #define MSM8998_MMSS_TEMP_SENSOR_ID_START 12
  221. #define MSM8998_MMSS_TEMP_SENSOR_ID_END 13
  222. /*
  223. * Some initial msm8998 parts cannot be operated at low voltages. The
  224. * open-loop voltage fuses are reused to identify these parts so that software
  225. * can properly handle the limitation. 0xF means that the next higher fuse
  226. * corner should be used. 0xE means that the next higher fuse corner which
  227. * does not have a voltage limitation should be used.
  228. */
  229. enum msm8998_cpr_partial_binning {
  230. MSM8998_CPR_PARTIAL_BINNING_NEXT_CORNER = 0xF,
  231. MSM8998_CPR_PARTIAL_BINNING_SAFE_CORNER = 0xE,
  232. };
  233. /*
  234. * The partial binning open-loop voltage fuse values only apply to the lowest
  235. * two fuse corners (0 and 1, i.e. MinSVS and SVS).
  236. */
  237. #define MSM8998_CPR_PARTIAL_BINNING_MAX_FUSE_CORNER 1
  238. static inline bool cpr3_ctrl_is_msm8998(const struct cpr3_controller *ctrl)
  239. {
  240. return ctrl->soc_revision == MSM8998_V1_SOC_ID ||
  241. ctrl->soc_revision == MSM8998_V2_SOC_ID;
  242. }
  243. /**
  244. * cpr3_msm8996_mmss_read_fuse_data() - load MMSS specific fuse parameter values
  245. * @vreg: Pointer to the CPR3 regulator
  246. *
  247. * This function allocates a cpr3_msm8996_mmss_fuses struct, fills it with
  248. * values read out of hardware fuses, and finally copies common fuse values
  249. * into the regulator struct.
  250. *
  251. * Return: 0 on success, errno on failure
  252. */
  253. static int cpr3_msm8996_mmss_read_fuse_data(struct cpr3_regulator *vreg)
  254. {
  255. void __iomem *base = vreg->thread->ctrl->fuse_base;
  256. struct cpr3_msm8996_mmss_fuses *fuse;
  257. int i, rc, combo_max;
  258. fuse = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*fuse), GFP_KERNEL);
  259. if (!fuse)
  260. return -ENOMEM;
  261. if (vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID) {
  262. rc = cpr3_read_fuse_param(base, msm8996pro_mmss_speed_bin_param,
  263. &fuse->speed_bin);
  264. if (rc) {
  265. cpr3_err(vreg, "Unable to read speed bin fuse, rc=%d\n",
  266. rc);
  267. return rc;
  268. }
  269. cpr3_info(vreg, "speed bin = %llu\n", fuse->speed_bin);
  270. }
  271. rc = cpr3_read_fuse_param(base,
  272. cpr3_ctrl_is_msm8998(vreg->thread->ctrl)
  273. ? msm8998_cpr_fusing_rev_param
  274. : msm8996_cpr_fusing_rev_param,
  275. &fuse->cpr_fusing_rev);
  276. if (rc) {
  277. cpr3_err(vreg, "Unable to read CPR fusing revision fuse, rc=%d\n",
  278. rc);
  279. return rc;
  280. }
  281. cpr3_info(vreg, "CPR fusing revision = %llu\n", fuse->cpr_fusing_rev);
  282. rc = cpr3_read_fuse_param(base,
  283. cpr3_ctrl_is_msm8998(vreg->thread->ctrl)
  284. ? msm8998_cpr_limitation_param
  285. : msm8996_cpr_limitation_param,
  286. &fuse->limitation);
  287. if (rc) {
  288. cpr3_err(vreg, "Unable to read CPR limitation fuse, rc=%d\n",
  289. rc);
  290. return rc;
  291. }
  292. cpr3_info(vreg, "CPR limitation = %s\n",
  293. fuse->limitation == MSM8996_CPR_LIMITATION_UNSUPPORTED
  294. ? "unsupported chip" : fuse->limitation
  295. == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION
  296. ? "CPR disabled and no interpolation" : "none");
  297. rc = cpr3_read_fuse_param(base,
  298. cpr3_ctrl_is_msm8998(vreg->thread->ctrl)
  299. ? msm8998_mmss_aging_init_quot_diff_param
  300. : msm8996_mmss_aging_init_quot_diff_param,
  301. &fuse->aging_init_quot_diff);
  302. if (rc) {
  303. cpr3_err(vreg, "Unable to read aging initial quotient difference fuse, rc=%d\n",
  304. rc);
  305. return rc;
  306. }
  307. for (i = 0; i < MSM8996_MMSS_FUSE_CORNERS; i++) {
  308. rc = cpr3_read_fuse_param(base,
  309. cpr3_ctrl_is_msm8998(vreg->thread->ctrl)
  310. ? msm8998_mmss_init_voltage_param[i]
  311. : msm8996_mmss_init_voltage_param[i],
  312. &fuse->init_voltage[i]);
  313. if (rc) {
  314. cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n",
  315. i, rc);
  316. return rc;
  317. }
  318. rc = cpr3_read_fuse_param(base,
  319. cpr3_ctrl_is_msm8998(vreg->thread->ctrl)
  320. ? msm8998_mmss_offset_voltage_param[i]
  321. : msm8996_mmss_offset_voltage_param[i],
  322. &fuse->offset_voltage[i]);
  323. if (rc) {
  324. cpr3_err(vreg, "Unable to read fuse-corner %d offset voltage fuse, rc=%d\n",
  325. i, rc);
  326. return rc;
  327. }
  328. }
  329. if (cpr3_ctrl_is_msm8998(vreg->thread->ctrl)) {
  330. rc = cpr3_read_fuse_param(base,
  331. msm8998_cpr_force_highest_corner_param,
  332. &fuse->force_highest_corner);
  333. if (rc) {
  334. cpr3_err(vreg, "Unable to read CPR force highest corner fuse, rc=%d\n",
  335. rc);
  336. return rc;
  337. }
  338. if (fuse->force_highest_corner)
  339. cpr3_info(vreg, "Fusing requires all operation at the highest corner\n");
  340. }
  341. if (cpr3_ctrl_is_msm8998(vreg->thread->ctrl)) {
  342. combo_max = CPR3_MSM8998_MMSS_FUSE_COMBO_COUNT;
  343. vreg->fuse_combo = fuse->cpr_fusing_rev;
  344. } else if (vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID) {
  345. combo_max = CPR3_MSM8996PRO_MMSS_FUSE_COMBO_COUNT;
  346. vreg->fuse_combo = fuse->cpr_fusing_rev + 8 * fuse->speed_bin;
  347. } else {
  348. combo_max = CPR3_MSM8996_MMSS_FUSE_COMBO_COUNT;
  349. vreg->fuse_combo = fuse->cpr_fusing_rev;
  350. }
  351. if (vreg->fuse_combo >= combo_max) {
  352. cpr3_err(vreg, "invalid CPR fuse combo = %d found, not in range 0 - %d\n",
  353. vreg->fuse_combo, combo_max - 1);
  354. return -EINVAL;
  355. }
  356. vreg->speed_bin_fuse = fuse->speed_bin;
  357. vreg->cpr_rev_fuse = fuse->cpr_fusing_rev;
  358. vreg->fuse_corner_count = MSM8996_MMSS_FUSE_CORNERS;
  359. vreg->platform_fuses = fuse;
  360. return 0;
  361. }
  362. /**
  363. * cpr3_mmss_parse_corner_data() - parse MMSS corner data from device tree
  364. * properties of the regulator's device node
  365. * @vreg: Pointer to the CPR3 regulator
  366. *
  367. * Return: 0 on success, errno on failure
  368. */
  369. static int cpr3_mmss_parse_corner_data(struct cpr3_regulator *vreg)
  370. {
  371. int i, rc;
  372. u32 *temp;
  373. rc = cpr3_parse_common_corner_data(vreg);
  374. if (rc) {
  375. cpr3_err(vreg, "error reading corner data, rc=%d\n", rc);
  376. return rc;
  377. }
  378. temp = kcalloc(vreg->corner_count * CPR3_RO_COUNT, sizeof(*temp),
  379. GFP_KERNEL);
  380. if (!temp)
  381. return -ENOMEM;
  382. rc = cpr3_parse_corner_array_property(vreg, "qcom,cpr-target-quotients",
  383. CPR3_RO_COUNT, temp);
  384. if (rc) {
  385. cpr3_err(vreg, "could not load target quotients, rc=%d\n", rc);
  386. goto done;
  387. }
  388. for (i = 0; i < vreg->corner_count; i++)
  389. memcpy(vreg->corner[i].target_quot, &temp[i * CPR3_RO_COUNT],
  390. sizeof(*temp) * CPR3_RO_COUNT);
  391. done:
  392. kfree(temp);
  393. return rc;
  394. }
  395. /**
  396. * cpr3_msm8996_mmss_adjust_target_quotients() - adjust the target quotients
  397. * for each corner according to device tree values and fuse values
  398. * @vreg: Pointer to the CPR3 regulator
  399. *
  400. * Return: 0 on success, errno on failure
  401. */
  402. static int cpr3_msm8996_mmss_adjust_target_quotients(
  403. struct cpr3_regulator *vreg)
  404. {
  405. struct cpr3_msm8996_mmss_fuses *fuse = vreg->platform_fuses;
  406. const struct cpr3_fuse_param (*offset_param)[2];
  407. int *volt_offset;
  408. int i, fuse_len, rc = 0;
  409. volt_offset = kcalloc(vreg->fuse_corner_count, sizeof(*volt_offset),
  410. GFP_KERNEL);
  411. if (!volt_offset)
  412. return -ENOMEM;
  413. offset_param = cpr3_ctrl_is_msm8998(vreg->thread->ctrl)
  414. ? msm8998_mmss_offset_voltage_param
  415. : msm8996_mmss_offset_voltage_param;
  416. for (i = 0; i < vreg->fuse_corner_count; i++) {
  417. fuse_len = offset_param[i][0].bit_end + 1
  418. - offset_param[i][0].bit_start;
  419. volt_offset[i] = cpr3_convert_open_loop_voltage_fuse(
  420. 0, MSM8996_MMSS_OFFSET_FUSE_STEP_VOLT,
  421. fuse->offset_voltage[i], fuse_len);
  422. if (volt_offset[i])
  423. cpr3_info(vreg, "fuse_corner[%d] offset=%7d uV\n",
  424. i, volt_offset[i]);
  425. }
  426. rc = cpr3_adjust_target_quotients(vreg, volt_offset);
  427. if (rc)
  428. cpr3_err(vreg, "adjust target quotients failed, rc=%d\n", rc);
  429. kfree(volt_offset);
  430. return rc;
  431. }
  432. /**
  433. * cpr3_msm8996_mmss_calculate_open_loop_voltages() - calculate the open-loop
  434. * voltage for each corner of a CPR3 regulator
  435. * @vreg: Pointer to the CPR3 regulator
  436. *
  437. * If open-loop voltage interpolation is allowed in both device tree and in
  438. * hardware fuses, then this function calculates the open-loop voltage for a
  439. * given corner using linear interpolation. This interpolation is performed
  440. * using the processor frequencies of the lower and higher Fmax corners along
  441. * with their fused open-loop voltages.
  442. *
  443. * If open-loop voltage interpolation is not allowed, then this function uses
  444. * the Fmax fused open-loop voltage for all of the corners associated with a
  445. * given fuse corner.
  446. *
  447. * Return: 0 on success, errno on failure
  448. */
  449. static int cpr3_msm8996_mmss_calculate_open_loop_voltages(
  450. struct cpr3_regulator *vreg)
  451. {
  452. struct device_node *node = vreg->of_node;
  453. struct cpr3_msm8996_mmss_fuses *fuse = vreg->platform_fuses;
  454. bool is_msm8998 = cpr3_ctrl_is_msm8998(vreg->thread->ctrl);
  455. int rc = 0;
  456. bool allow_interpolation;
  457. u64 freq_low, volt_low, freq_high, volt_high, volt_init;
  458. int i, j;
  459. const int *ref_volt;
  460. int *fuse_volt;
  461. int *fmax_corner;
  462. fuse_volt = kcalloc(vreg->fuse_corner_count, sizeof(*fuse_volt),
  463. GFP_KERNEL);
  464. fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
  465. GFP_KERNEL);
  466. if (!fuse_volt || !fmax_corner) {
  467. rc = -ENOMEM;
  468. goto done;
  469. }
  470. if (vreg->thread->ctrl->soc_revision == MSM8998_V2_SOC_ID
  471. && fuse->cpr_fusing_rev == 0)
  472. ref_volt = msm8998_v2_rev0_mmss_fuse_ref_volt;
  473. else if (vreg->thread->ctrl->soc_revision == MSM8998_V2_SOC_ID)
  474. ref_volt = msm8998_v2_mmss_fuse_ref_volt;
  475. else if (vreg->thread->ctrl->soc_revision == MSM8998_V1_SOC_ID
  476. && fuse->cpr_fusing_rev == 0)
  477. ref_volt = msm8998_v1_rev0_mmss_fuse_ref_volt;
  478. else if (vreg->thread->ctrl->soc_revision == MSM8998_V1_SOC_ID)
  479. ref_volt = msm8998_v1_mmss_fuse_ref_volt;
  480. else if (vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID)
  481. ref_volt = msm8996pro_mmss_fuse_ref_volt;
  482. else
  483. ref_volt = msm8996_mmss_fuse_ref_volt;
  484. for (i = 0; i < vreg->fuse_corner_count; i++) {
  485. volt_init = fuse->init_voltage[i];
  486. /*
  487. * Handle partial binning on MSM8998 where the initial voltage
  488. * fuse is reused as a flag for partial binning needs. Set the
  489. * open-loop voltage to the minimum possible value so that it
  490. * does not result in higher fuse corners getting forced to
  491. * higher open-loop voltages after monotonicity enforcement.
  492. */
  493. if (is_msm8998 &&
  494. (volt_init == MSM8998_CPR_PARTIAL_BINNING_NEXT_CORNER ||
  495. volt_init == MSM8998_CPR_PARTIAL_BINNING_SAFE_CORNER) &&
  496. i <= MSM8998_CPR_PARTIAL_BINNING_MAX_FUSE_CORNER)
  497. volt_init = MSM8996_MMSS_MIN_VOLTAGE_FUSE_VAL;
  498. fuse_volt[i] = cpr3_convert_open_loop_voltage_fuse(ref_volt[i],
  499. MSM8996_MMSS_FUSE_STEP_VOLT, volt_init,
  500. MSM8996_MMSS_VOLTAGE_FUSE_SIZE);
  501. cpr3_info(vreg, "fuse_corner[%d] open-loop=%7d uV\n",
  502. i, fuse_volt[i]);
  503. }
  504. rc = cpr3_adjust_fused_open_loop_voltages(vreg, fuse_volt);
  505. if (rc) {
  506. cpr3_err(vreg, "fused open-loop voltage adjustment failed, rc=%d\n",
  507. rc);
  508. goto done;
  509. }
  510. allow_interpolation = of_property_read_bool(node,
  511. "qcom,allow-voltage-interpolation");
  512. for (i = 1; i < vreg->fuse_corner_count; i++) {
  513. if (fuse_volt[i] < fuse_volt[i - 1]) {
  514. cpr3_debug(vreg, "fuse corner %d voltage=%d uV < fuse corner %d voltage=%d uV; overriding: fuse corner %d voltage=%d\n",
  515. i, fuse_volt[i], i - 1, fuse_volt[i - 1],
  516. i, fuse_volt[i - 1]);
  517. fuse_volt[i] = fuse_volt[i - 1];
  518. }
  519. }
  520. if (fuse->limitation == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION)
  521. allow_interpolation = false;
  522. if (!allow_interpolation) {
  523. /* Use fused open-loop voltage for lower frequencies. */
  524. for (i = 0; i < vreg->corner_count; i++)
  525. vreg->corner[i].open_loop_volt
  526. = fuse_volt[vreg->corner[i].cpr_fuse_corner];
  527. goto done;
  528. }
  529. /* Determine highest corner mapped to each fuse corner */
  530. j = vreg->fuse_corner_count - 1;
  531. for (i = vreg->corner_count - 1; i >= 0; i--) {
  532. if (vreg->corner[i].cpr_fuse_corner == j) {
  533. fmax_corner[j] = i;
  534. j--;
  535. }
  536. }
  537. if (j >= 0) {
  538. cpr3_err(vreg, "invalid fuse corner mapping\n");
  539. rc = -EINVAL;
  540. goto done;
  541. }
  542. /*
  543. * Interpolation is not possible for corners mapped to the lowest fuse
  544. * corner so use the fuse corner value directly.
  545. */
  546. for (i = 0; i <= fmax_corner[0]; i++)
  547. vreg->corner[i].open_loop_volt = fuse_volt[0];
  548. /* Interpolate voltages for the higher fuse corners. */
  549. for (i = 1; i < vreg->fuse_corner_count; i++) {
  550. freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
  551. volt_low = fuse_volt[i - 1];
  552. freq_high = vreg->corner[fmax_corner[i]].proc_freq;
  553. volt_high = fuse_volt[i];
  554. for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
  555. vreg->corner[j].open_loop_volt = cpr3_interpolate(
  556. freq_low, volt_low, freq_high, volt_high,
  557. vreg->corner[j].proc_freq);
  558. }
  559. done:
  560. if (rc == 0) {
  561. cpr3_debug(vreg, "unadjusted per-corner open-loop voltages:\n");
  562. for (i = 0; i < vreg->corner_count; i++)
  563. cpr3_debug(vreg, "open-loop[%2d] = %d uV\n", i,
  564. vreg->corner[i].open_loop_volt);
  565. rc = cpr3_adjust_open_loop_voltages(vreg);
  566. if (rc)
  567. cpr3_err(vreg, "open-loop voltage adjustment failed, rc=%d\n",
  568. rc);
  569. }
  570. kfree(fuse_volt);
  571. kfree(fmax_corner);
  572. return rc;
  573. }
  574. /**
  575. * cpr3_msm8998_partial_binning_override() - override the voltage and quotient
  576. * settings for low corners based upon the special partial binning
  577. * open-loop voltage fuse values
  578. * @vreg: Pointer to the CPR3 regulator
  579. *
  580. * Some parts are not able to operate at low voltages. The partial binning
  581. * open-loop voltage fuse values specify if a given part has such limitations.
  582. *
  583. * Return: 0 on success, errno on failure
  584. */
  585. static int cpr3_msm8998_partial_binning_override(struct cpr3_regulator *vreg)
  586. {
  587. struct cpr3_msm8996_mmss_fuses *fuse = vreg->platform_fuses;
  588. u64 next = MSM8998_CPR_PARTIAL_BINNING_NEXT_CORNER;
  589. u64 safe = MSM8998_CPR_PARTIAL_BINNING_SAFE_CORNER;
  590. u32 proc_freq;
  591. struct cpr3_corner *corner;
  592. struct cpr3_corner *safe_corner;
  593. int i, j, low, high, safe_fuse_corner, max_fuse_corner;
  594. if (!cpr3_ctrl_is_msm8998(vreg->thread->ctrl))
  595. return 0;
  596. /* Handle the force highest corner fuse. */
  597. if (fuse->force_highest_corner) {
  598. cpr3_info(vreg, "overriding CPR parameters for corners 0 to %d with quotients and voltages of corner %d\n",
  599. vreg->corner_count - 2, vreg->corner_count - 1);
  600. corner = &vreg->corner[vreg->corner_count - 1];
  601. for (i = 0; i < vreg->corner_count - 1; i++) {
  602. proc_freq = vreg->corner[i].proc_freq;
  603. vreg->corner[i] = *corner;
  604. vreg->corner[i].proc_freq = proc_freq;
  605. }
  606. /*
  607. * Return since the potential partial binning fuse values are
  608. * superceded by the force highest corner fuse value.
  609. */
  610. return 0;
  611. }
  612. /*
  613. * Allow up to the max corner which can be fused with partial
  614. * binning values.
  615. */
  616. max_fuse_corner = min(MSM8998_CPR_PARTIAL_BINNING_MAX_FUSE_CORNER,
  617. vreg->fuse_corner_count - 2);
  618. for (i = 0; i <= max_fuse_corner; i++) {
  619. /* Determine which higher corners to override with (if any). */
  620. if (fuse->init_voltage[i] != next
  621. && fuse->init_voltage[i] != safe)
  622. continue;
  623. for (j = i + 1; j <= max_fuse_corner; j++)
  624. if (fuse->init_voltage[j] != next
  625. && fuse->init_voltage[j] != safe)
  626. break;
  627. safe_fuse_corner = j;
  628. j = fuse->init_voltage[i] == next ? i + 1 : safe_fuse_corner;
  629. low = i > 0 ? vreg->fuse_corner_map[i] : 0;
  630. high = vreg->fuse_corner_map[i + 1] - 1;
  631. cpr3_info(vreg, "overriding CPR parameters for corners %d to %d with quotients of corner %d and voltages of corner %d\n",
  632. low, high, vreg->fuse_corner_map[j],
  633. vreg->fuse_corner_map[safe_fuse_corner]);
  634. corner = &vreg->corner[vreg->fuse_corner_map[j]];
  635. safe_corner
  636. = &vreg->corner[vreg->fuse_corner_map[safe_fuse_corner]];
  637. for (j = low; j <= high; j++) {
  638. proc_freq = vreg->corner[j].proc_freq;
  639. vreg->corner[j] = *corner;
  640. vreg->corner[j].proc_freq = proc_freq;
  641. vreg->corner[j].floor_volt
  642. = safe_corner->floor_volt;
  643. vreg->corner[j].ceiling_volt
  644. = safe_corner->ceiling_volt;
  645. vreg->corner[j].open_loop_volt
  646. = safe_corner->open_loop_volt;
  647. vreg->corner[j].abs_ceiling_volt
  648. = safe_corner->abs_ceiling_volt;
  649. }
  650. }
  651. return 0;
  652. }
  653. /**
  654. * cpr3_mmss_print_settings() - print out MMSS CPR configuration settings into
  655. * the kernel log for debugging purposes
  656. * @vreg: Pointer to the CPR3 regulator
  657. */
  658. static void cpr3_mmss_print_settings(struct cpr3_regulator *vreg)
  659. {
  660. struct cpr3_corner *corner;
  661. int i;
  662. cpr3_debug(vreg, "Corner: Frequency (Hz), Fuse Corner, Floor (uV), Open-Loop (uV), Ceiling (uV)\n");
  663. for (i = 0; i < vreg->corner_count; i++) {
  664. corner = &vreg->corner[i];
  665. cpr3_debug(vreg, "%3d: %10u, %2d, %7d, %7d, %7d\n",
  666. i, corner->proc_freq, corner->cpr_fuse_corner,
  667. corner->floor_volt, corner->open_loop_volt,
  668. corner->ceiling_volt);
  669. }
  670. }
  671. /**
  672. * cpr3_mmss_init_aging() - perform MMSS CPR3 controller specific
  673. * aging initializations
  674. * @ctrl: Pointer to the CPR3 controller
  675. *
  676. * Return: 0 on success, errno on failure
  677. */
  678. static int cpr3_mmss_init_aging(struct cpr3_controller *ctrl)
  679. {
  680. struct cpr3_msm8996_mmss_fuses *fuse;
  681. struct cpr3_regulator *vreg;
  682. u32 aging_ro_scale;
  683. int rc;
  684. vreg = &ctrl->thread[0].vreg[0];
  685. ctrl->aging_required = vreg->aging_allowed;
  686. fuse = vreg->platform_fuses;
  687. if (!ctrl->aging_required || !fuse)
  688. return 0;
  689. rc = cpr3_parse_array_property(vreg, "qcom,cpr-aging-ro-scaling-factor",
  690. 1, &aging_ro_scale);
  691. if (rc)
  692. return rc;
  693. if (aging_ro_scale == 0) {
  694. cpr3_err(ctrl, "aging RO scaling factor is invalid: %u\n",
  695. aging_ro_scale);
  696. return -EINVAL;
  697. }
  698. ctrl->aging_vdd_mode = REGULATOR_MODE_NORMAL;
  699. ctrl->aging_complete_vdd_mode = REGULATOR_MODE_IDLE;
  700. ctrl->aging_sensor_count = 1;
  701. ctrl->aging_sensor = kzalloc(sizeof(*ctrl->aging_sensor), GFP_KERNEL);
  702. if (!ctrl->aging_sensor)
  703. return -ENOMEM;
  704. ctrl->aging_sensor->ro_scale = aging_ro_scale;
  705. if (cpr3_ctrl_is_msm8998(ctrl)) {
  706. ctrl->aging_sensor->sensor_id = MSM8998_MMSS_AGING_SENSOR_ID;
  707. ctrl->aging_sensor->bypass_mask[0]
  708. = MSM8998_MMSS_AGING_BYPASS_MASK0;
  709. ctrl->aging_sensor->init_quot_diff
  710. = cpr3_convert_open_loop_voltage_fuse(0,
  711. MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SCALE,
  712. fuse->aging_init_quot_diff,
  713. MSM8998_MMSS_AGING_INIT_QUOT_DIFF_SIZE);
  714. } else {
  715. ctrl->aging_sensor->sensor_id = MSM8996_MMSS_AGING_SENSOR_ID;
  716. ctrl->aging_sensor->bypass_mask[0]
  717. = MSM8996_MMSS_AGING_BYPASS_MASK0;
  718. ctrl->aging_sensor->init_quot_diff
  719. = cpr3_convert_open_loop_voltage_fuse(0,
  720. MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SCALE,
  721. fuse->aging_init_quot_diff,
  722. MSM8996_MMSS_AGING_INIT_QUOT_DIFF_SIZE);
  723. }
  724. cpr3_debug(ctrl, "sensor %u aging init quotient diff = %d, aging RO scale = %u QUOT/V\n",
  725. ctrl->aging_sensor->sensor_id,
  726. ctrl->aging_sensor->init_quot_diff,
  727. ctrl->aging_sensor->ro_scale);
  728. return 0;
  729. }
  730. /**
  731. * cpr3_mmss_init_thread() - perform all steps necessary to initialize the
  732. * configuration data for a CPR3 thread
  733. * @thread: Pointer to the CPR3 thread
  734. *
  735. * Return: 0 on success, errno on failure
  736. */
  737. static int cpr3_mmss_init_thread(struct cpr3_thread *thread)
  738. {
  739. struct cpr3_regulator *vreg = &thread->vreg[0];
  740. struct cpr3_msm8996_mmss_fuses *fuse;
  741. int rc;
  742. rc = cpr3_parse_common_thread_data(thread);
  743. if (rc) {
  744. cpr3_err(vreg, "unable to read CPR thread data from device tree, rc=%d\n",
  745. rc);
  746. return rc;
  747. }
  748. rc = cpr3_msm8996_mmss_read_fuse_data(vreg);
  749. if (rc) {
  750. cpr3_err(vreg, "unable to read CPR fuse data, rc=%d\n", rc);
  751. return rc;
  752. }
  753. fuse = vreg->platform_fuses;
  754. if (fuse->limitation == MSM8996_CPR_LIMITATION_UNSUPPORTED) {
  755. cpr3_err(vreg, "this chip requires an unsupported voltage\n");
  756. return -EPERM;
  757. } else if (fuse->limitation
  758. == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION) {
  759. thread->ctrl->cpr_allowed_hw = false;
  760. }
  761. rc = cpr3_mmss_parse_corner_data(vreg);
  762. if (rc) {
  763. cpr3_err(vreg, "unable to read CPR corner data from device tree, rc=%d\n",
  764. rc);
  765. return rc;
  766. }
  767. rc = cpr3_msm8996_mmss_adjust_target_quotients(vreg);
  768. if (rc) {
  769. cpr3_err(vreg, "unable to adjust target quotients, rc=%d\n",
  770. rc);
  771. return rc;
  772. }
  773. rc = cpr3_msm8996_mmss_calculate_open_loop_voltages(vreg);
  774. if (rc) {
  775. cpr3_err(vreg, "unable to calculate open-loop voltages, rc=%d\n",
  776. rc);
  777. return rc;
  778. }
  779. rc = cpr3_limit_open_loop_voltages(vreg);
  780. if (rc) {
  781. cpr3_err(vreg, "unable to limit open-loop voltages, rc=%d\n",
  782. rc);
  783. return rc;
  784. }
  785. cpr3_open_loop_voltage_as_ceiling(vreg);
  786. rc = cpr3_limit_floor_voltages(vreg);
  787. if (rc) {
  788. cpr3_err(vreg, "unable to limit floor voltages, rc=%d\n", rc);
  789. return rc;
  790. }
  791. if (cpr3_ctrl_is_msm8998(thread->ctrl)) {
  792. rc = cpr4_parse_core_count_temp_voltage_adj(vreg, false);
  793. if (rc) {
  794. cpr3_err(vreg, "unable to parse temperature based voltage adjustments, rc=%d\n",
  795. rc);
  796. return rc;
  797. }
  798. }
  799. rc = cpr3_msm8998_partial_binning_override(vreg);
  800. if (rc) {
  801. cpr3_err(vreg, "unable to override CPR parameters based on partial binning fuse values, rc=%d\n",
  802. rc);
  803. return rc;
  804. }
  805. cpr3_mmss_print_settings(vreg);
  806. return 0;
  807. }
  808. /**
  809. * cpr4_mmss_parse_temp_adj_properties() - parse temperature based
  810. * adjustment properties from device tree
  811. * @ctrl: Pointer to the CPR3 controller
  812. *
  813. * Return: 0 on success, errno on failure
  814. */
  815. static int cpr4_mmss_parse_temp_adj_properties(struct cpr3_controller *ctrl)
  816. {
  817. struct device_node *of_node = ctrl->dev->of_node;
  818. int rc, len, temp_point_count;
  819. if (!of_find_property(of_node, "qcom,cpr-temp-point-map", &len))
  820. return 0;
  821. temp_point_count = len / sizeof(u32);
  822. if (temp_point_count <= 0
  823. || temp_point_count > MSM8998_MMSS_MAX_TEMP_POINTS) {
  824. cpr3_err(ctrl, "invalid number of temperature points %d > %d (max)\n",
  825. temp_point_count, MSM8998_MMSS_MAX_TEMP_POINTS);
  826. return -EINVAL;
  827. }
  828. ctrl->temp_points = devm_kcalloc(ctrl->dev, temp_point_count,
  829. sizeof(*ctrl->temp_points), GFP_KERNEL);
  830. if (!ctrl->temp_points)
  831. return -ENOMEM;
  832. rc = of_property_read_u32_array(of_node, "qcom,cpr-temp-point-map",
  833. ctrl->temp_points, temp_point_count);
  834. if (rc) {
  835. cpr3_err(ctrl, "error reading property qcom,cpr-temp-point-map, rc=%d\n",
  836. rc);
  837. return rc;
  838. }
  839. /*
  840. * If t1, t2, and t3 are the temperature points, then the temperature
  841. * bands are: (-inf, t1], (t1, t2], (t2, t3], and (t3, inf).
  842. */
  843. ctrl->temp_band_count = temp_point_count + 1;
  844. rc = of_property_read_u32(of_node, "qcom,cpr-initial-temp-band",
  845. &ctrl->initial_temp_band);
  846. if (rc) {
  847. cpr3_err(ctrl, "error reading qcom,cpr-initial-temp-band, rc=%d\n",
  848. rc);
  849. return rc;
  850. }
  851. if (ctrl->initial_temp_band >= ctrl->temp_band_count) {
  852. cpr3_err(ctrl, "Initial temperature band value %d should be in range [0 - %d]\n",
  853. ctrl->initial_temp_band, ctrl->temp_band_count - 1);
  854. return -EINVAL;
  855. }
  856. ctrl->temp_sensor_id_start = MSM8998_MMSS_TEMP_SENSOR_ID_START;
  857. ctrl->temp_sensor_id_end = MSM8998_MMSS_TEMP_SENSOR_ID_END;
  858. ctrl->allow_temp_adj = true;
  859. return rc;
  860. }
  861. /**
  862. * cpr3_mmss_init_controller() - perform MMSS CPR3 controller specific
  863. * initializations
  864. * @ctrl: Pointer to the CPR3 controller
  865. *
  866. * Return: 0 on success, errno on failure
  867. */
  868. static int cpr3_mmss_init_controller(struct cpr3_controller *ctrl)
  869. {
  870. int rc;
  871. rc = cpr3_parse_common_ctrl_data(ctrl);
  872. if (rc) {
  873. if (rc != -EPROBE_DEFER)
  874. cpr3_err(ctrl, "unable to parse common controller data, rc=%d\n",
  875. rc);
  876. return rc;
  877. }
  878. if (cpr3_ctrl_is_msm8998(ctrl)) {
  879. rc = cpr4_mmss_parse_temp_adj_properties(ctrl);
  880. if (rc)
  881. return rc;
  882. }
  883. ctrl->sensor_count = cpr3_ctrl_is_msm8998(ctrl)
  884. ? MSM8998_MMSS_CPR_SENSOR_COUNT
  885. : MSM8996_MMSS_CPR_SENSOR_COUNT;
  886. /*
  887. * MMSS only has one thread (0) so the zeroed array does not need
  888. * further modification.
  889. */
  890. ctrl->sensor_owner = devm_kcalloc(ctrl->dev, ctrl->sensor_count,
  891. sizeof(*ctrl->sensor_owner), GFP_KERNEL);
  892. if (!ctrl->sensor_owner)
  893. return -ENOMEM;
  894. ctrl->cpr_clock_rate = MSM8996_MMSS_CPR_CLOCK_RATE;
  895. ctrl->ctrl_type = cpr3_ctrl_is_msm8998(ctrl)
  896. ? CPR_CTRL_TYPE_CPR4 : CPR_CTRL_TYPE_CPR3;
  897. if (ctrl->ctrl_type == CPR_CTRL_TYPE_CPR4) {
  898. /*
  899. * Use fixed step quotient if specified otherwise use dynamic
  900. * calculated per RO step quotient
  901. */
  902. of_property_read_u32(ctrl->dev->of_node,
  903. "qcom,cpr-step-quot-fixed",
  904. &ctrl->step_quot_fixed);
  905. ctrl->use_dynamic_step_quot = !ctrl->step_quot_fixed;
  906. }
  907. ctrl->iface_clk = devm_clk_get(ctrl->dev, "iface_clk");
  908. if (IS_ERR(ctrl->iface_clk)) {
  909. rc = PTR_ERR(ctrl->iface_clk);
  910. if (cpr3_ctrl_is_msm8998(ctrl)) {
  911. /* iface_clk is optional for msm8998 */
  912. ctrl->iface_clk = NULL;
  913. } else if (rc == -EPROBE_DEFER) {
  914. return rc;
  915. } else {
  916. cpr3_err(ctrl, "unable to request interface clock, rc=%d\n",
  917. rc);
  918. return rc;
  919. }
  920. }
  921. ctrl->bus_clk = devm_clk_get(ctrl->dev, "bus_clk");
  922. if (IS_ERR(ctrl->bus_clk)) {
  923. rc = PTR_ERR(ctrl->bus_clk);
  924. if (rc != -EPROBE_DEFER)
  925. cpr3_err(ctrl, "unable request bus clock, rc=%d\n",
  926. rc);
  927. return rc;
  928. }
  929. return 0;
  930. }
  931. static int cpr3_mmss_regulator_suspend(struct platform_device *pdev,
  932. pm_message_t state)
  933. {
  934. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  935. return cpr3_regulator_suspend(ctrl);
  936. }
  937. static int cpr3_mmss_regulator_resume(struct platform_device *pdev)
  938. {
  939. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  940. return cpr3_regulator_resume(ctrl);
  941. }
  942. /* Data corresponds to the SoC revision */
  943. static const struct of_device_id cpr_regulator_match_table[] = {
  944. {
  945. .compatible = "qcom,cpr3-msm8996-v1-mmss-regulator",
  946. .data = (void *)(uintptr_t)1,
  947. },
  948. {
  949. .compatible = "qcom,cpr3-msm8996-v2-mmss-regulator",
  950. .data = (void *)(uintptr_t)2,
  951. },
  952. {
  953. .compatible = "qcom,cpr3-msm8996-v3-mmss-regulator",
  954. .data = (void *)(uintptr_t)3,
  955. },
  956. {
  957. .compatible = "qcom,cpr3-msm8996-mmss-regulator",
  958. .data = (void *)(uintptr_t)3,
  959. },
  960. {
  961. .compatible = "qcom,cpr3-msm8996pro-mmss-regulator",
  962. .data = (void *)(uintptr_t)MSM8996PRO_SOC_ID,
  963. },
  964. {
  965. .compatible = "qcom,cpr4-msm8998-v1-mmss-regulator",
  966. .data = (void *)(uintptr_t)MSM8998_V1_SOC_ID,
  967. },
  968. {
  969. .compatible = "qcom,cpr4-msm8998-v2-mmss-regulator",
  970. .data = (void *)(uintptr_t)MSM8998_V2_SOC_ID,
  971. },
  972. {
  973. .compatible = "qcom,cpr4-msm8998-mmss-regulator",
  974. .data = (void *)(uintptr_t)MSM8998_V2_SOC_ID,
  975. },
  976. {}
  977. };
  978. static int cpr3_mmss_regulator_probe(struct platform_device *pdev)
  979. {
  980. struct device *dev = &pdev->dev;
  981. const struct of_device_id *match;
  982. struct cpr3_controller *ctrl;
  983. int rc;
  984. if (!dev->of_node) {
  985. dev_err(dev, "Device tree node is missing\n");
  986. return -EINVAL;
  987. }
  988. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  989. if (!ctrl)
  990. return -ENOMEM;
  991. ctrl->dev = dev;
  992. /* Set to false later if anything precludes CPR operation. */
  993. ctrl->cpr_allowed_hw = true;
  994. rc = of_property_read_string(dev->of_node, "qcom,cpr-ctrl-name",
  995. &ctrl->name);
  996. if (rc) {
  997. cpr3_err(ctrl, "unable to read qcom,cpr-ctrl-name, rc=%d\n",
  998. rc);
  999. return rc;
  1000. }
  1001. match = of_match_node(cpr_regulator_match_table, dev->of_node);
  1002. if (match)
  1003. ctrl->soc_revision = (uintptr_t)match->data;
  1004. else
  1005. cpr3_err(ctrl, "could not find compatible string match\n");
  1006. rc = cpr3_map_fuse_base(ctrl, pdev);
  1007. if (rc) {
  1008. cpr3_err(ctrl, "could not map fuse base address\n");
  1009. return rc;
  1010. }
  1011. rc = cpr3_allocate_threads(ctrl, 0, 0);
  1012. if (rc) {
  1013. cpr3_err(ctrl, "failed to allocate CPR thread array, rc=%d\n",
  1014. rc);
  1015. return rc;
  1016. }
  1017. if (ctrl->thread_count != 1) {
  1018. cpr3_err(ctrl, "expected 1 thread but found %d\n",
  1019. ctrl->thread_count);
  1020. return -EINVAL;
  1021. } else if (ctrl->thread[0].vreg_count != 1) {
  1022. cpr3_err(ctrl, "expected 1 regulator but found %d\n",
  1023. ctrl->thread[0].vreg_count);
  1024. return -EINVAL;
  1025. }
  1026. rc = cpr3_mmss_init_controller(ctrl);
  1027. if (rc) {
  1028. if (rc != -EPROBE_DEFER)
  1029. cpr3_err(ctrl, "failed to initialize CPR controller parameters, rc=%d\n",
  1030. rc);
  1031. return rc;
  1032. }
  1033. rc = cpr3_mmss_init_thread(&ctrl->thread[0]);
  1034. if (rc) {
  1035. cpr3_err(&ctrl->thread[0].vreg[0], "thread initialization failed, rc=%d\n",
  1036. rc);
  1037. return rc;
  1038. }
  1039. rc = cpr3_mem_acc_init(&ctrl->thread[0].vreg[0]);
  1040. if (rc) {
  1041. cpr3_err(ctrl, "failed to initialize mem-acc configuration, rc=%d\n",
  1042. rc);
  1043. return rc;
  1044. }
  1045. rc = cpr3_mmss_init_aging(ctrl);
  1046. if (rc) {
  1047. cpr3_err(ctrl, "failed to initialize aging configurations, rc=%d\n",
  1048. rc);
  1049. return rc;
  1050. }
  1051. platform_set_drvdata(pdev, ctrl);
  1052. return cpr3_regulator_register(pdev, ctrl);
  1053. }
  1054. static int cpr3_mmss_regulator_remove(struct platform_device *pdev)
  1055. {
  1056. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  1057. return cpr3_regulator_unregister(ctrl);
  1058. }
  1059. static struct platform_driver cpr3_mmss_regulator_driver = {
  1060. .driver = {
  1061. .name = "qcom,cpr3-mmss-regulator",
  1062. .of_match_table = cpr_regulator_match_table,
  1063. .owner = THIS_MODULE,
  1064. },
  1065. .probe = cpr3_mmss_regulator_probe,
  1066. .remove = cpr3_mmss_regulator_remove,
  1067. .suspend = cpr3_mmss_regulator_suspend,
  1068. .resume = cpr3_mmss_regulator_resume,
  1069. };
  1070. static int cpr_regulator_init(void)
  1071. {
  1072. return platform_driver_register(&cpr3_mmss_regulator_driver);
  1073. }
  1074. static void cpr_regulator_exit(void)
  1075. {
  1076. platform_driver_unregister(&cpr3_mmss_regulator_driver);
  1077. }
  1078. MODULE_DESCRIPTION("CPR3 MMSS regulator driver");
  1079. MODULE_LICENSE("GPL v2");
  1080. arch_initcall(cpr_regulator_init);
  1081. module_exit(cpr_regulator_exit);