cpr4-apss-regulator.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. /*
  2. * Copyright (c) 2015-2018, 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 MSM8953_APSS_FUSE_CORNERS 4
  35. #define SDM632_POWER_APSS_FUSE_CORNERS 4
  36. #define SDM632_PERF_APSS_FUSE_CORNERS 4
  37. /**
  38. * struct cpr4_apss_fuses - APSS specific fuse data
  39. * @ro_sel: Ring oscillator select fuse parameter value for each
  40. * fuse corner
  41. * @init_voltage: Initial (i.e. open-loop) voltage fuse parameter value
  42. * for each fuse corner (raw, not converted to a voltage)
  43. * @target_quot: CPR target quotient fuse parameter value for each fuse
  44. * corner
  45. * @quot_offset: CPR target quotient offset fuse parameter value for each
  46. * fuse corner (raw, not unpacked) used for target quotient
  47. * interpolation
  48. * @speed_bin: Application processor speed bin fuse parameter value for
  49. * the given chip
  50. * @cpr_fusing_rev: CPR fusing revision fuse parameter value
  51. * @foundry_id: Foundry identifier fuse parameter value for the given
  52. * chip
  53. * @boost_cfg: CPR boost configuration fuse parameter value
  54. * @boost_voltage: CPR boost voltage fuse parameter value (raw, not
  55. * converted to a voltage)
  56. * @aging_init_quot_diff: Initial quotient difference between CPR aging
  57. * min and max sensors measured at time of manufacturing
  58. *
  59. * This struct holds the values for all of the fuses read from memory.
  60. */
  61. struct cpr4_apss_fuses {
  62. u64 *ro_sel;
  63. u64 *init_voltage;
  64. u64 *target_quot;
  65. u64 *quot_offset;
  66. u64 speed_bin;
  67. u64 cpr_fusing_rev;
  68. u64 foundry_id;
  69. u64 boost_cfg;
  70. u64 boost_voltage;
  71. u64 misc;
  72. u64 aging_init_quot_diff;
  73. };
  74. /*
  75. * fuse combo = fusing revision + 8 * (speed bin)
  76. * where: fusing revision = 0 - 7 and speed bin = 0 - 7
  77. */
  78. #define CPR4_MSM8953_APSS_FUSE_COMBO_COUNT 64
  79. #define CPR4_SDM632_APSS_FUSE_COMBO_COUNT 64
  80. /*
  81. * Constants which define the name of each fuse corner.
  82. */
  83. enum cpr4_msm8953_apss_fuse_corner {
  84. CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS = 0,
  85. CPR4_MSM8953_APSS_FUSE_CORNER_SVS = 1,
  86. CPR4_MSM8953_APSS_FUSE_CORNER_NOM = 2,
  87. CPR4_MSM8953_APSS_FUSE_CORNER_TURBO_L1 = 3,
  88. };
  89. static const char * const cpr4_msm8953_apss_fuse_corner_name[] = {
  90. [CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS] = "LowSVS",
  91. [CPR4_MSM8953_APSS_FUSE_CORNER_SVS] = "SVS",
  92. [CPR4_MSM8953_APSS_FUSE_CORNER_NOM] = "NOM",
  93. [CPR4_MSM8953_APSS_FUSE_CORNER_TURBO_L1] = "TURBO_L1",
  94. };
  95. enum cpr4_sdm632_power_apss_fuse_corner {
  96. CPR4_SDM632_POWER_APSS_FUSE_CORNER_LOWSVS = 0,
  97. CPR4_SDM632_POWER_APSS_FUSE_CORNER_SVS_L1 = 1,
  98. CPR4_SDM632_POWER_APSS_FUSE_CORNER_NOM = 2,
  99. CPR4_SDM632_POWER_APSS_FUSE_CORNER_TURBO_L1 = 3,
  100. };
  101. static const char * const cpr4_sdm632_power_apss_fuse_corner_name[] = {
  102. [CPR4_SDM632_POWER_APSS_FUSE_CORNER_LOWSVS] = "LowSVS",
  103. [CPR4_SDM632_POWER_APSS_FUSE_CORNER_SVS_L1] = "SVS_L1",
  104. [CPR4_SDM632_POWER_APSS_FUSE_CORNER_NOM] = "NOM",
  105. [CPR4_SDM632_POWER_APSS_FUSE_CORNER_TURBO_L1] = "TURBO_L1",
  106. };
  107. enum cpr4_sdm632_perf_apss_fuse_corner {
  108. CPR4_SDM632_PERF_APSS_FUSE_CORNER_LOWSVS = 0,
  109. CPR4_SDM632_PERF_APSS_FUSE_CORNER_SVS_L1 = 1,
  110. CPR4_SDM632_PERF_APSS_FUSE_CORNER_NOM = 2,
  111. CPR4_SDM632_PERF_APSS_FUSE_CORNER_TURBO_L1 = 3,
  112. };
  113. static const char * const cpr4_sdm632_perf_apss_fuse_corner_name[] = {
  114. [CPR4_SDM632_PERF_APSS_FUSE_CORNER_LOWSVS] = "LowSVS",
  115. [CPR4_SDM632_PERF_APSS_FUSE_CORNER_SVS_L1] = "SVS_L1",
  116. [CPR4_SDM632_PERF_APSS_FUSE_CORNER_NOM] = "NOM",
  117. [CPR4_SDM632_PERF_APSS_FUSE_CORNER_TURBO_L1] = "TURBO_L1",
  118. };
  119. /* APSS cluster thread IDs */
  120. #define CPR4_APSS_POWER_CLUSTER_ID 0
  121. #define CPR4_APSS_PERF_CLUSTER_ID 1
  122. /*
  123. * MSM8953 APSS fuse parameter locations:
  124. *
  125. * Structs are organized with the following dimensions:
  126. * Outer: 0 to 3 for fuse corners from lowest to highest corner
  127. * Inner: large enough to hold the longest set of parameter segments which
  128. * fully defines a fuse parameter, +1 (for NULL termination).
  129. * Each segment corresponds to a contiguous group of bits from a
  130. * single fuse row. These segments are concatentated together in
  131. * order to form the full fuse parameter value. The segments for
  132. * a given parameter may correspond to different fuse rows.
  133. */
  134. static const struct cpr3_fuse_param
  135. msm8953_apss_ro_sel_param[MSM8953_APSS_FUSE_CORNERS][2] = {
  136. {{73, 12, 15}, {} },
  137. {{73, 8, 11}, {} },
  138. {{73, 4, 7}, {} },
  139. {{73, 0, 3}, {} },
  140. };
  141. static const struct cpr3_fuse_param
  142. msm8953_apss_init_voltage_param[MSM8953_APSS_FUSE_CORNERS][2] = {
  143. {{71, 24, 29}, {} },
  144. {{71, 18, 23}, {} },
  145. {{71, 12, 17}, {} },
  146. {{71, 6, 11}, {} },
  147. };
  148. static const struct cpr3_fuse_param
  149. msm8953_apss_target_quot_param[MSM8953_APSS_FUSE_CORNERS][2] = {
  150. {{72, 44, 55}, {} },
  151. {{72, 32, 43}, {} },
  152. {{72, 20, 31}, {} },
  153. {{72, 8, 19}, {} },
  154. };
  155. static const struct cpr3_fuse_param
  156. msm8953_apss_quot_offset_param[MSM8953_APSS_FUSE_CORNERS][2] = {
  157. {{} },
  158. {{71, 46, 52}, {} },
  159. {{71, 39, 45}, {} },
  160. {{71, 32, 38}, {} },
  161. };
  162. static const struct cpr3_fuse_param msm8953_cpr_fusing_rev_param[] = {
  163. {71, 53, 55},
  164. {},
  165. };
  166. static const struct cpr3_fuse_param msm8953_apss_speed_bin_param[] = {
  167. {36, 40, 42},
  168. {},
  169. };
  170. static const struct cpr3_fuse_param msm8953_apss_foundry_id_param[] = {
  171. {37, 40, 42},
  172. {},
  173. };
  174. static const struct cpr3_fuse_param msm8953_cpr_boost_fuse_cfg_param[] = {
  175. {36, 43, 45},
  176. {},
  177. };
  178. static const struct cpr3_fuse_param msm8953_apss_boost_fuse_volt_param[] = {
  179. {71, 0, 5},
  180. {},
  181. };
  182. static const struct cpr3_fuse_param msm8953_misc_fuse_volt_adj_param[] = {
  183. {36, 54, 54},
  184. {},
  185. };
  186. static const struct cpr3_fuse_param msm8953_apss_aging_init_quot_diff_param[]
  187. = {
  188. {72, 0, 7},
  189. {},
  190. };
  191. /*
  192. * SDM632 APSS fuse parameter locations:
  193. *
  194. * Structs are organized with the following dimensions:
  195. * Outer: 0 to 3 for fuse corners from lowest to highest corner
  196. * Inner: large enough to hold the longest set of parameter segments which
  197. * fully defines a fuse parameter, +1 (for NULL termination).
  198. * Each segment corresponds to a contiguous group of bits from a
  199. * single fuse row. These segments are concatentated together in
  200. * order to form the full fuse parameter value. The segments for
  201. * a given parameter may correspond to different fuse rows.
  202. */
  203. static const struct cpr3_fuse_param
  204. sdm632_apss_ro_sel_param[2][SDM632_POWER_APSS_FUSE_CORNERS][2] = {
  205. [CPR4_APSS_POWER_CLUSTER_ID] = {
  206. {{73, 28, 31}, {} },
  207. {{73, 20, 23}, {} },
  208. {{73, 16, 19}, {} },
  209. {{73, 12, 15}, {} },
  210. },
  211. [CPR4_APSS_PERF_CLUSTER_ID] = {
  212. {{73, 28, 31}, {} },
  213. {{73, 8, 11}, {} },
  214. {{73, 4, 7}, {} },
  215. {{73, 0, 3}, {} },
  216. },
  217. };
  218. static const struct cpr3_fuse_param
  219. sdm632_apss_init_voltage_param[2][SDM632_POWER_APSS_FUSE_CORNERS][2] = {
  220. [CPR4_APSS_POWER_CLUSTER_ID] = {
  221. {{74, 18, 23}, {} },
  222. {{71, 24, 29}, {} },
  223. {{74, 6, 11}, {} },
  224. {{74, 0, 5}, {} },
  225. },
  226. [CPR4_APSS_PERF_CLUSTER_ID] = {
  227. {{74, 18, 23}, {} },
  228. {{71, 18, 23}, {} },
  229. {{71, 12, 17}, {} },
  230. {{71, 6, 11}, {} },
  231. },
  232. };
  233. static const struct cpr3_fuse_param
  234. sdm632_apss_target_quot_param[2][SDM632_POWER_APSS_FUSE_CORNERS][2] = {
  235. [CPR4_APSS_POWER_CLUSTER_ID] = {
  236. {{75, 44, 55}, {} },
  237. {{72, 44, 55}, {} },
  238. {{75, 20, 31}, {} },
  239. {{75, 8, 19}, {} },
  240. },
  241. [CPR4_APSS_PERF_CLUSTER_ID] = {
  242. {{75, 44, 55}, {} },
  243. {{72, 32, 43}, {} },
  244. {{72, 20, 31}, {} },
  245. {{72, 8, 19}, {} },
  246. },
  247. };
  248. static const struct cpr3_fuse_param
  249. sdm632_apss_quot_offset_param[2][SDM632_POWER_APSS_FUSE_CORNERS][2] = {
  250. [CPR4_APSS_POWER_CLUSTER_ID] = {
  251. {{} },
  252. {{71, 46, 52}, {} },
  253. {{74, 32, 38}, {} },
  254. {{74, 24, 30}, {} },
  255. },
  256. [CPR4_APSS_PERF_CLUSTER_ID] = {
  257. {{} },
  258. {{74, 39, 45}, {} },
  259. {{71, 39, 45}, {} },
  260. {{71, 32, 38}, {} },
  261. },
  262. };
  263. /*
  264. * The maximum number of fuse combinations possible for the selected fuse
  265. * parameters in fuse combo map logic.
  266. * Here, possible speed-bin values = 8, fuse revision values = 8, and foundry
  267. * identifier values = 8. Total number of combinations = 512 (i.e., 8 * 8 * 8)
  268. */
  269. #define CPR4_APSS_FUSE_COMBO_MAP_MAX_COUNT 512
  270. /*
  271. * The number of possible values for misc fuse is
  272. * 2^(#bits defined for misc fuse)
  273. */
  274. #define MSM8953_MISC_FUSE_VAL_COUNT BIT(1)
  275. /*
  276. * Open loop voltage fuse reference voltages in microvolts for MSM8953
  277. */
  278. static const int msm8953_apss_fuse_ref_volt
  279. [MSM8953_APSS_FUSE_CORNERS] = {
  280. 645000,
  281. 720000,
  282. 865000,
  283. 1065000,
  284. };
  285. /*
  286. * Open loop voltage fuse reference voltages in microvolts for SDM632
  287. */
  288. static const int
  289. sdm632_apss_fuse_ref_volt[2][SDM632_POWER_APSS_FUSE_CORNERS] = {
  290. [CPR4_APSS_POWER_CLUSTER_ID] = {
  291. 645000,
  292. 790000,
  293. 865000,
  294. 1065000,
  295. },
  296. [CPR4_APSS_PERF_CLUSTER_ID] = {
  297. 645000,
  298. 790000,
  299. 865000,
  300. 1065000,
  301. },
  302. };
  303. #define CPR4_APSS_FUSE_STEP_VOLT 10000
  304. #define CPR4_APSS_VOLTAGE_FUSE_SIZE 6
  305. #define CPR4_APSS_QUOT_OFFSET_SCALE 5
  306. #define MSM8953_APSS_CPR_SENSOR_COUNT 13
  307. #define SDM632_APSS_CPR_SENSOR_COUNT 16
  308. #define SDM632_APSS_THREAD0_SENSOR_MIN 0
  309. #define SDM632_APSS_THREAD0_SENSOR_MAX 6
  310. #define SDM632_APSS_THREAD1_SENSOR_MIN 7
  311. #define SDM632_APSS_THREAD1_SENSOR_MAX 15
  312. #define CPR4_APSS_CPR_CLOCK_RATE 19200000
  313. #define MSM8953_APSS_MAX_TEMP_POINTS 3
  314. #define MSM8953_APSS_TEMP_SENSOR_ID_START 4
  315. #define MSM8953_APSS_TEMP_SENSOR_ID_END 13
  316. /*
  317. * Boost voltage fuse reference and ceiling voltages in microvolts for
  318. * MSM8953.
  319. */
  320. #define MSM8953_APSS_BOOST_FUSE_REF_VOLT 1140000
  321. #define MSM8953_APSS_BOOST_CEILING_VOLT 1140000
  322. #define MSM8953_APSS_BOOST_FLOOR_VOLT 900000
  323. #define MAX_BOOST_CONFIG_FUSE_VALUE 8
  324. #define MSM8953_APSS_CPR_SDELTA_CORE_COUNT 15
  325. /*
  326. * Array of integer values mapped to each of the boost config fuse values to
  327. * indicate boost enable/disable status.
  328. */
  329. static bool boost_fuse[MAX_BOOST_CONFIG_FUSE_VALUE] = {0, 1, 1, 1, 1, 1, 1, 1};
  330. /* CPR Aging parameters for msm8953 */
  331. #define MSM8953_APSS_AGING_INIT_QUOT_DIFF_SCALE 1
  332. #define MSM8953_APSS_AGING_INIT_QUOT_DIFF_SIZE 8
  333. #define MSM8953_APSS_AGING_SENSOR_ID 6
  334. /* Use a very high value for max aging margin to be applied */
  335. #define MSM8953_APSS_AGING_MAX_AGE_MARGIN_QUOT (-1000)
  336. /*
  337. * SOC IDs
  338. */
  339. enum soc_id {
  340. MSM8953_SOC_ID = 1,
  341. SDM632_SOC_ID = 2,
  342. };
  343. /**
  344. * cpr4_msm8953_apss_read_fuse_data() - load MSM8953 APSS specific fuse
  345. * parameter values
  346. * @vreg: Pointer to the CPR3 regulator
  347. * @fuse: APSS specific fuse data
  348. *
  349. * This function fills cpr4_apss_fuses struct with values read out of hardware
  350. * fuses.
  351. *
  352. * Return: 0 on success, errno on failure
  353. */
  354. static int cpr4_msm8953_apss_read_fuse_data(struct cpr3_regulator *vreg,
  355. struct cpr4_apss_fuses *fuse)
  356. {
  357. void __iomem *base = vreg->thread->ctrl->fuse_base;
  358. int i, rc;
  359. rc = cpr3_read_fuse_param(base, msm8953_misc_fuse_volt_adj_param,
  360. &fuse->misc);
  361. if (rc) {
  362. cpr3_err(vreg, "Unable to read misc voltage adjustment fuse, rc=%d\n",
  363. rc);
  364. return rc;
  365. }
  366. cpr3_info(vreg, "CPR misc fuse value = %llu\n", fuse->misc);
  367. if (fuse->misc >= MSM8953_MISC_FUSE_VAL_COUNT) {
  368. cpr3_err(vreg, "CPR misc fuse value = %llu, should be < %lu\n",
  369. fuse->misc, MSM8953_MISC_FUSE_VAL_COUNT);
  370. return -EINVAL;
  371. }
  372. rc = cpr3_read_fuse_param(base, msm8953_apss_aging_init_quot_diff_param,
  373. &fuse->aging_init_quot_diff);
  374. if (rc) {
  375. cpr3_err(vreg, "Unable to read aging initial quotient difference fuse, rc=%d\n",
  376. rc);
  377. return rc;
  378. }
  379. for (i = 0; i < MSM8953_APSS_FUSE_CORNERS; i++) {
  380. rc = cpr3_read_fuse_param(base,
  381. msm8953_apss_init_voltage_param[i],
  382. &fuse->init_voltage[i]);
  383. if (rc) {
  384. cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n",
  385. i, rc);
  386. return rc;
  387. }
  388. rc = cpr3_read_fuse_param(base,
  389. msm8953_apss_target_quot_param[i],
  390. &fuse->target_quot[i]);
  391. if (rc) {
  392. cpr3_err(vreg, "Unable to read fuse-corner %d target quotient fuse, rc=%d\n",
  393. i, rc);
  394. return rc;
  395. }
  396. rc = cpr3_read_fuse_param(base,
  397. msm8953_apss_ro_sel_param[i],
  398. &fuse->ro_sel[i]);
  399. if (rc) {
  400. cpr3_err(vreg, "Unable to read fuse-corner %d RO select fuse, rc=%d\n",
  401. i, rc);
  402. return rc;
  403. }
  404. rc = cpr3_read_fuse_param(base,
  405. msm8953_apss_quot_offset_param[i],
  406. &fuse->quot_offset[i]);
  407. if (rc) {
  408. cpr3_err(vreg, "Unable to read fuse-corner %d quotient offset fuse, rc=%d\n",
  409. i, rc);
  410. return rc;
  411. }
  412. }
  413. rc = cpr3_read_fuse_param(base, msm8953_cpr_boost_fuse_cfg_param,
  414. &fuse->boost_cfg);
  415. if (rc) {
  416. cpr3_err(vreg, "Unable to read CPR boost config fuse, rc=%d\n",
  417. rc);
  418. return rc;
  419. }
  420. cpr3_info(vreg, "Voltage boost fuse config = %llu boost = %s\n",
  421. fuse->boost_cfg, boost_fuse[fuse->boost_cfg]
  422. ? "enable" : "disable");
  423. rc = cpr3_read_fuse_param(base,
  424. msm8953_apss_boost_fuse_volt_param,
  425. &fuse->boost_voltage);
  426. if (rc) {
  427. cpr3_err(vreg, "failed to read boost fuse voltage, rc=%d\n",
  428. rc);
  429. return rc;
  430. }
  431. vreg->fuse_combo = fuse->cpr_fusing_rev + 8 * fuse->speed_bin;
  432. if (vreg->fuse_combo >= CPR4_MSM8953_APSS_FUSE_COMBO_COUNT) {
  433. cpr3_err(vreg, "invalid CPR fuse combo = %d found\n",
  434. vreg->fuse_combo);
  435. return -EINVAL;
  436. }
  437. return 0;
  438. }
  439. /**
  440. * cpr4_sdm632_apss_read_fuse_data() - load SDM632 APSS specific fuse
  441. * parameter values
  442. * @vreg: Pointer to the CPR3 regulator
  443. * @fuse: APSS specific fuse data
  444. *
  445. * This function fills cpr4_apss_fuses struct with values read out of hardware
  446. * fuses.
  447. *
  448. * Return: 0 on success, errno on failure
  449. */
  450. static int cpr4_sdm632_apss_read_fuse_data(struct cpr3_regulator *vreg,
  451. struct cpr4_apss_fuses *fuse)
  452. {
  453. void __iomem *base = vreg->thread->ctrl->fuse_base;
  454. int i, id, rc, fuse_corners;
  455. id = vreg->thread->thread_id;
  456. if (id == CPR4_APSS_POWER_CLUSTER_ID)
  457. fuse_corners = SDM632_POWER_APSS_FUSE_CORNERS;
  458. else
  459. fuse_corners = SDM632_PERF_APSS_FUSE_CORNERS;
  460. for (i = 0; i < fuse_corners; i++) {
  461. rc = cpr3_read_fuse_param(base,
  462. sdm632_apss_init_voltage_param[id][i],
  463. &fuse->init_voltage[i]);
  464. if (rc) {
  465. cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n",
  466. i, rc);
  467. return rc;
  468. }
  469. rc = cpr3_read_fuse_param(base,
  470. sdm632_apss_target_quot_param[id][i],
  471. &fuse->target_quot[i]);
  472. if (rc) {
  473. cpr3_err(vreg, "Unable to read fuse-corner %d target quotient fuse, rc=%d\n",
  474. i, rc);
  475. return rc;
  476. }
  477. rc = cpr3_read_fuse_param(base,
  478. sdm632_apss_ro_sel_param[id][i],
  479. &fuse->ro_sel[i]);
  480. if (rc) {
  481. cpr3_err(vreg, "Unable to read fuse-corner %d RO select fuse, rc=%d\n",
  482. i, rc);
  483. return rc;
  484. }
  485. rc = cpr3_read_fuse_param(base,
  486. sdm632_apss_quot_offset_param[id][i],
  487. &fuse->quot_offset[i]);
  488. if (rc) {
  489. cpr3_err(vreg, "Unable to read fuse-corner %d quotient offset fuse, rc=%d\n",
  490. i, rc);
  491. return rc;
  492. }
  493. }
  494. vreg->fuse_combo = fuse->cpr_fusing_rev + (8 * fuse->speed_bin);
  495. if (vreg->fuse_combo >= CPR4_SDM632_APSS_FUSE_COMBO_COUNT) {
  496. cpr3_err(vreg, "invalid CPR fuse combo = %d found\n",
  497. vreg->fuse_combo);
  498. return -EINVAL;
  499. }
  500. return 0;
  501. }
  502. /**
  503. * cpr4_apss_read_fuse_data() - load APSS specific fuse parameter values
  504. * @vreg: Pointer to the CPR3 regulator
  505. *
  506. * This function allocates a cpr4_apss_fuses struct, fills it with
  507. * values read out of hardware fuses, and finally copies common fuse values
  508. * into the CPR3 regulator struct.
  509. *
  510. * Return: 0 on success, errno on failure
  511. */
  512. static int cpr4_apss_read_fuse_data(struct cpr3_regulator *vreg)
  513. {
  514. void __iomem *base = vreg->thread->ctrl->fuse_base;
  515. struct cpr4_apss_fuses *fuse;
  516. int rc, fuse_corners;
  517. enum soc_id soc_revision;
  518. fuse = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*fuse), GFP_KERNEL);
  519. if (!fuse)
  520. return -ENOMEM;
  521. soc_revision = vreg->thread->ctrl->soc_revision;
  522. switch (soc_revision) {
  523. case MSM8953_SOC_ID:
  524. fuse_corners = MSM8953_APSS_FUSE_CORNERS;
  525. break;
  526. case SDM632_SOC_ID:
  527. if (vreg->thread->thread_id == CPR4_APSS_POWER_CLUSTER_ID)
  528. fuse_corners = SDM632_POWER_APSS_FUSE_CORNERS;
  529. else
  530. fuse_corners = SDM632_PERF_APSS_FUSE_CORNERS;
  531. break;
  532. default:
  533. cpr3_err(vreg, "unsupported soc id = %d\n", soc_revision);
  534. return -EINVAL;
  535. }
  536. fuse->ro_sel = devm_kcalloc(vreg->thread->ctrl->dev, fuse_corners,
  537. sizeof(*fuse->ro_sel), GFP_KERNEL);
  538. fuse->init_voltage = devm_kcalloc(vreg->thread->ctrl->dev, fuse_corners,
  539. sizeof(*fuse->init_voltage), GFP_KERNEL);
  540. fuse->target_quot = devm_kcalloc(vreg->thread->ctrl->dev, fuse_corners,
  541. sizeof(*fuse->target_quot), GFP_KERNEL);
  542. fuse->quot_offset = devm_kcalloc(vreg->thread->ctrl->dev, fuse_corners,
  543. sizeof(*fuse->quot_offset), GFP_KERNEL);
  544. if (!fuse->ro_sel || !fuse->init_voltage || !fuse->target_quot
  545. || !fuse->quot_offset)
  546. return -ENOMEM;
  547. rc = cpr3_read_fuse_param(base, msm8953_apss_speed_bin_param,
  548. &fuse->speed_bin);
  549. if (rc) {
  550. cpr3_err(vreg, "Unable to read speed bin fuse, rc=%d\n", rc);
  551. return rc;
  552. }
  553. rc = cpr3_read_fuse_param(base, msm8953_cpr_fusing_rev_param,
  554. &fuse->cpr_fusing_rev);
  555. if (rc) {
  556. cpr3_err(vreg, "Unable to read CPR fusing revision fuse, rc=%d\n",
  557. rc);
  558. return rc;
  559. }
  560. rc = cpr3_read_fuse_param(base, msm8953_apss_foundry_id_param,
  561. &fuse->foundry_id);
  562. if (rc) {
  563. cpr3_err(vreg, "Unable to read foundry id fuse, rc=%d\n", rc);
  564. return rc;
  565. }
  566. cpr3_info(vreg, "speed bin = %llu, CPR fusing revision = %llu, foundry id = %llu\n",
  567. fuse->speed_bin, fuse->cpr_fusing_rev,
  568. fuse->foundry_id);
  569. switch (soc_revision) {
  570. case MSM8953_SOC_ID:
  571. rc = cpr4_msm8953_apss_read_fuse_data(vreg, fuse);
  572. if (rc) {
  573. cpr3_err(vreg, "msm8953 apss fuse data read failed, rc=%d\n",
  574. rc);
  575. return rc;
  576. }
  577. break;
  578. case SDM632_SOC_ID:
  579. rc = cpr4_sdm632_apss_read_fuse_data(vreg, fuse);
  580. if (rc) {
  581. cpr3_err(vreg, "sdm632 apss fuse data read failed, rc=%d\n",
  582. rc);
  583. return rc;
  584. }
  585. break;
  586. default:
  587. cpr3_err(vreg, "unsupported soc id = %d\n", soc_revision);
  588. return -EINVAL;
  589. }
  590. vreg->speed_bin_fuse = fuse->speed_bin;
  591. vreg->cpr_rev_fuse = fuse->cpr_fusing_rev;
  592. vreg->fuse_corner_count = fuse_corners;
  593. vreg->platform_fuses = fuse;
  594. return 0;
  595. }
  596. /**
  597. * cpr4_apss_parse_corner_data() - parse APSS corner data from device tree
  598. * properties of the CPR3 regulator's device node
  599. * @vreg: Pointer to the CPR3 regulator
  600. *
  601. * Return: 0 on success, errno on failure
  602. */
  603. static int cpr4_apss_parse_corner_data(struct cpr3_regulator *vreg)
  604. {
  605. int rc;
  606. rc = cpr3_parse_common_corner_data(vreg);
  607. if (rc) {
  608. cpr3_err(vreg, "error reading corner data, rc=%d\n", rc);
  609. return rc;
  610. }
  611. return rc;
  612. }
  613. /**
  614. * cpr4_apss_parse_misc_fuse_voltage_adjustments() - fill an array from a
  615. * portion of the voltage adjustments specified based on
  616. * miscellaneous fuse bits.
  617. * @vreg: Pointer to the CPR3 regulator
  618. * @volt_adjust: Voltage adjustment output data array which must be
  619. * of size vreg->corner_count
  620. *
  621. * cpr3_parse_common_corner_data() must be called for vreg before this function
  622. * is called so that speed bin size elements are initialized.
  623. *
  624. * Two formats are supported for the device tree property:
  625. * 1. Length == tuple_list_size * vreg->corner_count
  626. * (reading begins at index 0)
  627. * 2. Length == tuple_list_size * vreg->speed_bin_corner_sum
  628. * (reading begins at index tuple_list_size * vreg->speed_bin_offset)
  629. *
  630. * Here, tuple_list_size is the number of possible values for misc fuse.
  631. * All other property lengths are treated as errors.
  632. *
  633. * Return: 0 on success, errno on failure
  634. */
  635. static int cpr4_apss_parse_misc_fuse_voltage_adjustments(
  636. struct cpr3_regulator *vreg, u32 *volt_adjust)
  637. {
  638. struct device_node *node = vreg->of_node;
  639. struct cpr4_apss_fuses *fuse = vreg->platform_fuses;
  640. int tuple_list_size = MSM8953_MISC_FUSE_VAL_COUNT;
  641. int i, offset, rc, len = 0;
  642. const char *prop_name = "qcom,cpr-misc-fuse-voltage-adjustment";
  643. if (!of_find_property(node, prop_name, &len)) {
  644. cpr3_err(vreg, "property %s is missing\n", prop_name);
  645. return -EINVAL;
  646. }
  647. if (len == tuple_list_size * vreg->corner_count * sizeof(u32)) {
  648. offset = 0;
  649. } else if (vreg->speed_bin_corner_sum > 0 &&
  650. len == tuple_list_size * vreg->speed_bin_corner_sum
  651. * sizeof(u32)) {
  652. offset = tuple_list_size * vreg->speed_bin_offset
  653. + fuse->misc * vreg->corner_count;
  654. } else {
  655. if (vreg->speed_bin_corner_sum > 0)
  656. cpr3_err(vreg, "property %s has invalid length=%d, should be %zu or %zu\n",
  657. prop_name, len,
  658. tuple_list_size * vreg->corner_count
  659. * sizeof(u32),
  660. tuple_list_size * vreg->speed_bin_corner_sum
  661. * sizeof(u32));
  662. else
  663. cpr3_err(vreg, "property %s has invalid length=%d, should be %zu\n",
  664. prop_name, len,
  665. tuple_list_size * vreg->corner_count
  666. * sizeof(u32));
  667. return -EINVAL;
  668. }
  669. for (i = 0; i < vreg->corner_count; i++) {
  670. rc = of_property_read_u32_index(node, prop_name, offset + i,
  671. &volt_adjust[i]);
  672. if (rc) {
  673. cpr3_err(vreg, "error reading property %s, rc=%d\n",
  674. prop_name, rc);
  675. return rc;
  676. }
  677. }
  678. return 0;
  679. }
  680. /**
  681. * cpr4_apss_calculate_open_loop_voltages() - calculate the open-loop
  682. * voltage for each corner of a CPR3 regulator
  683. * @vreg: Pointer to the CPR3 regulator
  684. *
  685. * If open-loop voltage interpolation is allowed in device tree, then
  686. * this function calculates the open-loop voltage for a given corner using
  687. * linear interpolation. This interpolation is performed using the processor
  688. * frequencies of the lower and higher Fmax corners along with their fused
  689. * open-loop voltages.
  690. *
  691. * If open-loop voltage interpolation is not allowed, then this function uses
  692. * the Fmax fused open-loop voltage for all of the corners associated with a
  693. * given fuse corner.
  694. *
  695. * Return: 0 on success, errno on failure
  696. */
  697. static int cpr4_apss_calculate_open_loop_voltages(struct cpr3_regulator *vreg)
  698. {
  699. struct device_node *node = vreg->of_node;
  700. struct cpr4_apss_fuses *fuse = vreg->platform_fuses;
  701. int i, j, id, rc = 0;
  702. bool allow_interpolation;
  703. u64 freq_low, volt_low, freq_high, volt_high;
  704. const int *ref_volt;
  705. int *fuse_volt, *misc_adj_volt;
  706. int *fmax_corner;
  707. const char * const *corner_name;
  708. enum soc_id soc_revision;
  709. fuse_volt = kcalloc(vreg->fuse_corner_count, sizeof(*fuse_volt),
  710. GFP_KERNEL);
  711. fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
  712. GFP_KERNEL);
  713. if (!fuse_volt || !fmax_corner) {
  714. rc = -ENOMEM;
  715. goto done;
  716. }
  717. id = vreg->thread->thread_id;
  718. soc_revision = vreg->thread->ctrl->soc_revision;
  719. switch (soc_revision) {
  720. case MSM8953_SOC_ID:
  721. ref_volt = msm8953_apss_fuse_ref_volt;
  722. corner_name = cpr4_msm8953_apss_fuse_corner_name;
  723. break;
  724. case SDM632_SOC_ID:
  725. ref_volt = sdm632_apss_fuse_ref_volt[id];
  726. if (id == CPR4_APSS_POWER_CLUSTER_ID)
  727. corner_name = cpr4_sdm632_power_apss_fuse_corner_name;
  728. else
  729. corner_name = cpr4_sdm632_perf_apss_fuse_corner_name;
  730. break;
  731. default:
  732. cpr3_err(vreg, "unsupported soc id = %d\n", soc_revision);
  733. rc = -EINVAL;
  734. goto done;
  735. }
  736. for (i = 0; i < vreg->fuse_corner_count; i++) {
  737. fuse_volt[i] = cpr3_convert_open_loop_voltage_fuse(ref_volt[i],
  738. CPR4_APSS_FUSE_STEP_VOLT, fuse->init_voltage[i],
  739. CPR4_APSS_VOLTAGE_FUSE_SIZE);
  740. /* Log fused open-loop voltage values for debugging purposes. */
  741. cpr3_info(vreg, "fused %8s: open-loop=%7d uV\n", corner_name[i],
  742. fuse_volt[i]);
  743. }
  744. rc = cpr3_adjust_fused_open_loop_voltages(vreg, fuse_volt);
  745. if (rc) {
  746. cpr3_err(vreg, "fused open-loop voltage adjustment failed, rc=%d\n",
  747. rc);
  748. goto done;
  749. }
  750. allow_interpolation = of_property_read_bool(node,
  751. "qcom,allow-voltage-interpolation");
  752. for (i = 1; i < vreg->fuse_corner_count; i++) {
  753. if (fuse_volt[i] < fuse_volt[i - 1]) {
  754. cpr3_info(vreg, "fuse corner %d voltage=%d uV < fuse corner %d voltage=%d uV; overriding: fuse corner %d voltage=%d\n",
  755. i, fuse_volt[i], i - 1, fuse_volt[i - 1],
  756. i, fuse_volt[i - 1]);
  757. fuse_volt[i] = fuse_volt[i - 1];
  758. }
  759. }
  760. if (!allow_interpolation) {
  761. /* Use fused open-loop voltage for lower frequencies. */
  762. for (i = 0; i < vreg->corner_count; i++)
  763. vreg->corner[i].open_loop_volt
  764. = fuse_volt[vreg->corner[i].cpr_fuse_corner];
  765. goto done;
  766. }
  767. /* Determine highest corner mapped to each fuse corner */
  768. j = vreg->fuse_corner_count - 1;
  769. for (i = vreg->corner_count - 1; i >= 0; i--) {
  770. if (vreg->corner[i].cpr_fuse_corner == j) {
  771. fmax_corner[j] = i;
  772. j--;
  773. }
  774. }
  775. if (j >= 0) {
  776. cpr3_err(vreg, "invalid fuse corner mapping\n");
  777. rc = -EINVAL;
  778. goto done;
  779. }
  780. /*
  781. * Interpolation is not possible for corners mapped to the lowest fuse
  782. * corner so use the fuse corner value directly.
  783. */
  784. for (i = 0; i <= fmax_corner[0]; i++)
  785. vreg->corner[i].open_loop_volt = fuse_volt[0];
  786. /* Interpolate voltages for the higher fuse corners. */
  787. for (i = 1; i < vreg->fuse_corner_count; i++) {
  788. freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
  789. volt_low = fuse_volt[i - 1];
  790. freq_high = vreg->corner[fmax_corner[i]].proc_freq;
  791. volt_high = fuse_volt[i];
  792. for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
  793. vreg->corner[j].open_loop_volt = cpr3_interpolate(
  794. freq_low, volt_low, freq_high, volt_high,
  795. vreg->corner[j].proc_freq);
  796. }
  797. done:
  798. if (rc == 0) {
  799. cpr3_debug(vreg, "unadjusted per-corner open-loop voltages:\n");
  800. for (i = 0; i < vreg->corner_count; i++)
  801. cpr3_debug(vreg, "open-loop[%2d] = %d uV\n", i,
  802. vreg->corner[i].open_loop_volt);
  803. rc = cpr3_adjust_open_loop_voltages(vreg);
  804. if (rc)
  805. cpr3_err(vreg, "open-loop voltage adjustment failed, rc=%d\n",
  806. rc);
  807. if (of_find_property(node,
  808. "qcom,cpr-misc-fuse-voltage-adjustment",
  809. NULL)) {
  810. misc_adj_volt = kcalloc(vreg->corner_count,
  811. sizeof(*misc_adj_volt), GFP_KERNEL);
  812. if (!misc_adj_volt) {
  813. rc = -ENOMEM;
  814. goto _exit;
  815. }
  816. rc = cpr4_apss_parse_misc_fuse_voltage_adjustments(vreg,
  817. misc_adj_volt);
  818. if (rc) {
  819. cpr3_err(vreg, "qcom,cpr-misc-fuse-voltage-adjustment reading failed, rc=%d\n",
  820. rc);
  821. kfree(misc_adj_volt);
  822. goto _exit;
  823. }
  824. for (i = 0; i < vreg->corner_count; i++)
  825. vreg->corner[i].open_loop_volt
  826. += misc_adj_volt[i];
  827. kfree(misc_adj_volt);
  828. }
  829. }
  830. _exit:
  831. kfree(fuse_volt);
  832. kfree(fmax_corner);
  833. return rc;
  834. }
  835. /**
  836. * cpr4_msm8953_apss_set_no_interpolation_quotients() - use the fused target
  837. * quotient values for lower frequencies.
  838. * @vreg: Pointer to the CPR3 regulator
  839. * @volt_adjust: Pointer to array of per-corner closed-loop adjustment
  840. * voltages
  841. * @volt_adjust_fuse: Pointer to array of per-fuse-corner closed-loop
  842. * adjustment voltages
  843. * @ro_scale: Pointer to array of per-fuse-corner RO scaling factor
  844. * values with units of QUOT/V
  845. *
  846. * Return: 0 on success, errno on failure
  847. */
  848. static int cpr4_msm8953_apss_set_no_interpolation_quotients(
  849. struct cpr3_regulator *vreg, int *volt_adjust,
  850. int *volt_adjust_fuse, int *ro_scale)
  851. {
  852. struct cpr4_apss_fuses *fuse = vreg->platform_fuses;
  853. u32 quot, ro;
  854. int quot_adjust;
  855. int i, fuse_corner;
  856. for (i = 0; i < vreg->corner_count; i++) {
  857. fuse_corner = vreg->corner[i].cpr_fuse_corner;
  858. quot = fuse->target_quot[fuse_corner];
  859. quot_adjust = cpr3_quot_adjustment(ro_scale[fuse_corner],
  860. volt_adjust_fuse[fuse_corner] +
  861. volt_adjust[i]);
  862. ro = fuse->ro_sel[fuse_corner];
  863. vreg->corner[i].target_quot[ro] = quot + quot_adjust;
  864. cpr3_debug(vreg, "corner=%d RO=%u target quot=%u\n",
  865. i, ro, quot);
  866. if (quot_adjust)
  867. cpr3_debug(vreg, "adjusted corner %d RO%u target quot: %u --> %u (%d uV)\n",
  868. i, ro, quot, vreg->corner[i].target_quot[ro],
  869. volt_adjust_fuse[fuse_corner] +
  870. volt_adjust[i]);
  871. }
  872. return 0;
  873. }
  874. /**
  875. * cpr4_apss_calculate_target_quotients() - calculate the CPR target
  876. * quotient for each corner of a CPR3 regulator
  877. * @vreg: Pointer to the CPR3 regulator
  878. *
  879. * If target quotient interpolation is allowed in device tree, then this
  880. * function calculates the target quotient for a given corner using linear
  881. * interpolation. This interpolation is performed using the processor
  882. * frequencies of the lower and higher Fmax corners along with the fused
  883. * target quotient and quotient offset of the higher Fmax corner.
  884. *
  885. * If target quotient interpolation is not allowed, then this function uses
  886. * the Fmax fused target quotient for all of the corners associated with a
  887. * given fuse corner.
  888. *
  889. * Return: 0 on success, errno on failure
  890. */
  891. static int cpr4_apss_calculate_target_quotients(struct cpr3_regulator *vreg)
  892. {
  893. struct cpr4_apss_fuses *fuse = vreg->platform_fuses;
  894. int rc;
  895. bool allow_interpolation;
  896. u64 freq_low, freq_high, prev_quot;
  897. u64 *quot_low;
  898. u64 *quot_high;
  899. u32 quot, ro;
  900. int i, j, fuse_corner, quot_adjust;
  901. int *fmax_corner;
  902. int *volt_adjust, *volt_adjust_fuse, *ro_scale;
  903. int *voltage_adj_misc;
  904. int lowest_fuse_corner, highest_fuse_corner;
  905. const char * const *corner_name;
  906. switch (vreg->thread->ctrl->soc_revision) {
  907. case MSM8953_SOC_ID:
  908. corner_name = cpr4_msm8953_apss_fuse_corner_name;
  909. lowest_fuse_corner = CPR4_MSM8953_APSS_FUSE_CORNER_LOWSVS;
  910. highest_fuse_corner = CPR4_MSM8953_APSS_FUSE_CORNER_TURBO_L1;
  911. break;
  912. case SDM632_SOC_ID:
  913. if (vreg->thread->thread_id == CPR4_APSS_POWER_CLUSTER_ID) {
  914. corner_name = cpr4_sdm632_power_apss_fuse_corner_name;
  915. lowest_fuse_corner =
  916. CPR4_SDM632_POWER_APSS_FUSE_CORNER_LOWSVS;
  917. highest_fuse_corner =
  918. CPR4_SDM632_POWER_APSS_FUSE_CORNER_TURBO_L1;
  919. } else {
  920. corner_name = cpr4_sdm632_perf_apss_fuse_corner_name;
  921. lowest_fuse_corner =
  922. CPR4_SDM632_PERF_APSS_FUSE_CORNER_LOWSVS;
  923. highest_fuse_corner =
  924. CPR4_SDM632_PERF_APSS_FUSE_CORNER_TURBO_L1;
  925. }
  926. break;
  927. default:
  928. cpr3_err(vreg, "unsupported soc id = %d\n",
  929. vreg->thread->ctrl->soc_revision);
  930. return -EINVAL;
  931. }
  932. /* Log fused quotient values for debugging purposes. */
  933. cpr3_info(vreg, "fused %8s: quot[%2llu]=%4llu\n",
  934. corner_name[lowest_fuse_corner],
  935. fuse->ro_sel[lowest_fuse_corner],
  936. fuse->target_quot[lowest_fuse_corner]);
  937. for (i = lowest_fuse_corner + 1; i <= highest_fuse_corner; i++)
  938. cpr3_info(vreg, "fused %8s: quot[%2llu]=%4llu, quot_offset[%2llu]=%4llu\n",
  939. corner_name[i], fuse->ro_sel[i], fuse->target_quot[i],
  940. fuse->ro_sel[i], fuse->quot_offset[i] *
  941. CPR4_APSS_QUOT_OFFSET_SCALE);
  942. allow_interpolation = of_property_read_bool(vreg->of_node,
  943. "qcom,allow-quotient-interpolation");
  944. volt_adjust = kcalloc(vreg->corner_count, sizeof(*volt_adjust),
  945. GFP_KERNEL);
  946. volt_adjust_fuse = kcalloc(vreg->fuse_corner_count,
  947. sizeof(*volt_adjust_fuse), GFP_KERNEL);
  948. ro_scale = kcalloc(vreg->fuse_corner_count, sizeof(*ro_scale),
  949. GFP_KERNEL);
  950. fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
  951. GFP_KERNEL);
  952. quot_low = kcalloc(vreg->fuse_corner_count, sizeof(*quot_low),
  953. GFP_KERNEL);
  954. quot_high = kcalloc(vreg->fuse_corner_count, sizeof(*quot_high),
  955. GFP_KERNEL);
  956. if (!volt_adjust || !volt_adjust_fuse || !ro_scale ||
  957. !fmax_corner || !quot_low || !quot_high) {
  958. rc = -ENOMEM;
  959. goto done;
  960. }
  961. rc = cpr3_parse_closed_loop_voltage_adjustments(vreg, &fuse->ro_sel[0],
  962. volt_adjust, volt_adjust_fuse, ro_scale);
  963. if (rc) {
  964. cpr3_err(vreg, "could not load closed-loop voltage adjustments, rc=%d\n",
  965. rc);
  966. goto done;
  967. }
  968. if (of_find_property(vreg->of_node,
  969. "qcom,cpr-misc-fuse-voltage-adjustment", NULL)) {
  970. voltage_adj_misc = kcalloc(vreg->corner_count,
  971. sizeof(*voltage_adj_misc), GFP_KERNEL);
  972. if (!voltage_adj_misc) {
  973. rc = -ENOMEM;
  974. goto done;
  975. }
  976. rc = cpr4_apss_parse_misc_fuse_voltage_adjustments(vreg,
  977. voltage_adj_misc);
  978. if (rc) {
  979. cpr3_err(vreg, "qcom,cpr-misc-fuse-voltage-adjustment reading failed, rc=%d\n",
  980. rc);
  981. kfree(voltage_adj_misc);
  982. goto done;
  983. }
  984. for (i = 0; i < vreg->corner_count; i++)
  985. volt_adjust[i] += voltage_adj_misc[i];
  986. kfree(voltage_adj_misc);
  987. }
  988. if (!allow_interpolation) {
  989. /* Use fused target quotients for lower frequencies. */
  990. return cpr4_msm8953_apss_set_no_interpolation_quotients(
  991. vreg, volt_adjust, volt_adjust_fuse, ro_scale);
  992. }
  993. /* Determine highest corner mapped to each fuse corner */
  994. j = vreg->fuse_corner_count - 1;
  995. for (i = vreg->corner_count - 1; i >= 0; i--) {
  996. if (vreg->corner[i].cpr_fuse_corner == j) {
  997. fmax_corner[j] = i;
  998. j--;
  999. }
  1000. }
  1001. if (j >= 0) {
  1002. cpr3_err(vreg, "invalid fuse corner mapping\n");
  1003. rc = -EINVAL;
  1004. goto done;
  1005. }
  1006. /*
  1007. * Interpolation is not possible for corners mapped to the lowest fuse
  1008. * corner so use the fuse corner value directly.
  1009. */
  1010. i = lowest_fuse_corner;
  1011. quot_adjust = cpr3_quot_adjustment(ro_scale[i], volt_adjust_fuse[i]);
  1012. quot = fuse->target_quot[i] + quot_adjust;
  1013. quot_high[i] = quot_low[i] = quot;
  1014. ro = fuse->ro_sel[i];
  1015. if (quot_adjust)
  1016. cpr3_debug(vreg, "adjusted fuse corner %d RO%u target quot: %llu --> %u (%d uV)\n",
  1017. i, ro, fuse->target_quot[i], quot, volt_adjust_fuse[i]);
  1018. for (i = 0; i <= fmax_corner[lowest_fuse_corner]; i++)
  1019. vreg->corner[i].target_quot[ro] = quot;
  1020. for (i = lowest_fuse_corner + 1; i < vreg->fuse_corner_count; i++) {
  1021. quot_high[i] = fuse->target_quot[i];
  1022. if (fuse->ro_sel[i] == fuse->ro_sel[i - 1])
  1023. quot_low[i] = quot_high[i - 1];
  1024. else
  1025. quot_low[i] = quot_high[i]
  1026. - fuse->quot_offset[i]
  1027. * CPR4_APSS_QUOT_OFFSET_SCALE;
  1028. if (quot_high[i] < quot_low[i]) {
  1029. cpr3_debug(vreg, "quot_high[%d]=%llu < quot_low[%d]=%llu; overriding: quot_high[%d]=%llu\n",
  1030. i, quot_high[i], i, quot_low[i],
  1031. i, quot_low[i]);
  1032. quot_high[i] = quot_low[i];
  1033. }
  1034. }
  1035. /* Perform per-fuse-corner target quotient adjustment */
  1036. for (i = 1; i < vreg->fuse_corner_count; i++) {
  1037. quot_adjust = cpr3_quot_adjustment(ro_scale[i],
  1038. volt_adjust_fuse[i]);
  1039. if (quot_adjust) {
  1040. prev_quot = quot_high[i];
  1041. quot_high[i] += quot_adjust;
  1042. cpr3_debug(vreg, "adjusted fuse corner %d RO%llu target quot: %llu --> %llu (%d uV)\n",
  1043. i, fuse->ro_sel[i], prev_quot, quot_high[i],
  1044. volt_adjust_fuse[i]);
  1045. }
  1046. if (fuse->ro_sel[i] == fuse->ro_sel[i - 1])
  1047. quot_low[i] = quot_high[i - 1];
  1048. else
  1049. quot_low[i] += cpr3_quot_adjustment(ro_scale[i],
  1050. volt_adjust_fuse[i - 1]);
  1051. if (quot_high[i] < quot_low[i]) {
  1052. cpr3_debug(vreg, "quot_high[%d]=%llu < quot_low[%d]=%llu after adjustment; overriding: quot_high[%d]=%llu\n",
  1053. i, quot_high[i], i, quot_low[i],
  1054. i, quot_low[i]);
  1055. quot_high[i] = quot_low[i];
  1056. }
  1057. }
  1058. /* Interpolate voltages for the higher fuse corners. */
  1059. for (i = 1; i < vreg->fuse_corner_count; i++) {
  1060. freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
  1061. freq_high = vreg->corner[fmax_corner[i]].proc_freq;
  1062. ro = fuse->ro_sel[i];
  1063. for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
  1064. vreg->corner[j].target_quot[ro] = cpr3_interpolate(
  1065. freq_low, quot_low[i], freq_high, quot_high[i],
  1066. vreg->corner[j].proc_freq);
  1067. }
  1068. /* Perform per-corner target quotient adjustment */
  1069. for (i = 0; i < vreg->corner_count; i++) {
  1070. fuse_corner = vreg->corner[i].cpr_fuse_corner;
  1071. ro = fuse->ro_sel[fuse_corner];
  1072. quot_adjust = cpr3_quot_adjustment(ro_scale[fuse_corner],
  1073. volt_adjust[i]);
  1074. if (quot_adjust) {
  1075. prev_quot = vreg->corner[i].target_quot[ro];
  1076. vreg->corner[i].target_quot[ro] += quot_adjust;
  1077. cpr3_debug(vreg, "adjusted corner %d RO%u target quot: %llu --> %u (%d uV)\n",
  1078. i, ro, prev_quot,
  1079. vreg->corner[i].target_quot[ro],
  1080. volt_adjust[i]);
  1081. }
  1082. }
  1083. /* Ensure that target quotients increase monotonically */
  1084. for (i = 1; i < vreg->corner_count; i++) {
  1085. ro = fuse->ro_sel[vreg->corner[i].cpr_fuse_corner];
  1086. if (fuse->ro_sel[vreg->corner[i - 1].cpr_fuse_corner] == ro
  1087. && vreg->corner[i].target_quot[ro]
  1088. < vreg->corner[i - 1].target_quot[ro]) {
  1089. cpr3_debug(vreg, "adjusted corner %d RO%u target quot=%u < adjusted corner %d RO%u target quot=%u; overriding: corner %d RO%u target quot=%u\n",
  1090. i, ro, vreg->corner[i].target_quot[ro],
  1091. i - 1, ro, vreg->corner[i - 1].target_quot[ro],
  1092. i, ro, vreg->corner[i - 1].target_quot[ro]);
  1093. vreg->corner[i].target_quot[ro]
  1094. = vreg->corner[i - 1].target_quot[ro];
  1095. }
  1096. }
  1097. done:
  1098. kfree(volt_adjust);
  1099. kfree(volt_adjust_fuse);
  1100. kfree(ro_scale);
  1101. kfree(fmax_corner);
  1102. kfree(quot_low);
  1103. kfree(quot_high);
  1104. return rc;
  1105. }
  1106. /**
  1107. * cpr4_apss_print_settings() - print out APSS CPR configuration settings into
  1108. * the kernel log for debugging purposes
  1109. * @vreg: Pointer to the CPR3 regulator
  1110. */
  1111. static void cpr4_apss_print_settings(struct cpr3_regulator *vreg)
  1112. {
  1113. struct cpr3_corner *corner;
  1114. int i;
  1115. cpr3_debug(vreg, "Corner: Frequency (Hz), Fuse Corner, Floor (uV), Open-Loop (uV), Ceiling (uV)\n");
  1116. for (i = 0; i < vreg->corner_count; i++) {
  1117. corner = &vreg->corner[i];
  1118. cpr3_debug(vreg, "%3d: %10u, %2d, %7d, %7d, %7d\n",
  1119. i, corner->proc_freq, corner->cpr_fuse_corner,
  1120. corner->floor_volt, corner->open_loop_volt,
  1121. corner->ceiling_volt);
  1122. }
  1123. if (vreg->thread->ctrl->apm)
  1124. cpr3_debug(vreg, "APM threshold = %d uV, APM adjust = %d uV\n",
  1125. vreg->thread->ctrl->apm_threshold_volt,
  1126. vreg->thread->ctrl->apm_adj_volt);
  1127. }
  1128. /**
  1129. * cpr4_apss_init_thread() - perform steps necessary to initialize the
  1130. * configuration data for a CPR3 thread
  1131. * @thread: Pointer to the CPR3 thread
  1132. *
  1133. * Return: 0 on success, errno on failure
  1134. */
  1135. static int cpr4_apss_init_thread(struct cpr3_thread *thread)
  1136. {
  1137. int rc;
  1138. rc = cpr3_parse_common_thread_data(thread);
  1139. if (rc) {
  1140. cpr3_err(thread->ctrl, "thread %u unable to read CPR thread data from device tree, rc=%d\n",
  1141. thread->thread_id, rc);
  1142. return rc;
  1143. }
  1144. return 0;
  1145. }
  1146. /**
  1147. * cpr4_apss_parse_temp_adj_properties() - parse temperature based
  1148. * adjustment properties from device tree.
  1149. * @ctrl: Pointer to the CPR3 controller
  1150. *
  1151. * Return: 0 on success, errno on failure
  1152. */
  1153. static int cpr4_apss_parse_temp_adj_properties(struct cpr3_controller *ctrl)
  1154. {
  1155. struct device_node *of_node = ctrl->dev->of_node;
  1156. int rc, i, len, temp_point_count;
  1157. if (!of_find_property(of_node, "qcom,cpr-temp-point-map", &len)) {
  1158. /*
  1159. * Temperature based adjustments are not defined. Single
  1160. * temperature band is still valid for per-online-core
  1161. * adjustments.
  1162. */
  1163. ctrl->temp_band_count = 1;
  1164. return 0;
  1165. }
  1166. temp_point_count = len / sizeof(u32);
  1167. if (temp_point_count <= 0
  1168. || temp_point_count > MSM8953_APSS_MAX_TEMP_POINTS) {
  1169. cpr3_err(ctrl, "invalid number of temperature points %d > %d (max)\n",
  1170. temp_point_count, MSM8953_APSS_MAX_TEMP_POINTS);
  1171. return -EINVAL;
  1172. }
  1173. ctrl->temp_points = devm_kcalloc(ctrl->dev, temp_point_count,
  1174. sizeof(*ctrl->temp_points), GFP_KERNEL);
  1175. if (!ctrl->temp_points)
  1176. return -ENOMEM;
  1177. rc = of_property_read_u32_array(of_node, "qcom,cpr-temp-point-map",
  1178. ctrl->temp_points, temp_point_count);
  1179. if (rc) {
  1180. cpr3_err(ctrl, "error reading property qcom,cpr-temp-point-map, rc=%d\n",
  1181. rc);
  1182. return rc;
  1183. }
  1184. for (i = 0; i < temp_point_count; i++)
  1185. cpr3_debug(ctrl, "Temperature Point %d=%d\n", i,
  1186. ctrl->temp_points[i]);
  1187. /*
  1188. * If t1, t2, and t3 are the temperature points, then the temperature
  1189. * bands are: (-inf, t1], (t1, t2], (t2, t3], and (t3, inf).
  1190. */
  1191. ctrl->temp_band_count = temp_point_count + 1;
  1192. cpr3_debug(ctrl, "Number of temp bands =%d\n", ctrl->temp_band_count);
  1193. rc = of_property_read_u32(of_node, "qcom,cpr-initial-temp-band",
  1194. &ctrl->initial_temp_band);
  1195. if (rc) {
  1196. cpr3_err(ctrl, "error reading qcom,cpr-initial-temp-band, rc=%d\n",
  1197. rc);
  1198. return rc;
  1199. }
  1200. if (ctrl->initial_temp_band >= ctrl->temp_band_count) {
  1201. cpr3_err(ctrl, "Initial temperature band value %d should be in range [0 - %d]\n",
  1202. ctrl->initial_temp_band, ctrl->temp_band_count - 1);
  1203. return -EINVAL;
  1204. }
  1205. ctrl->temp_sensor_id_start = MSM8953_APSS_TEMP_SENSOR_ID_START;
  1206. ctrl->temp_sensor_id_end = MSM8953_APSS_TEMP_SENSOR_ID_END;
  1207. ctrl->allow_temp_adj = true;
  1208. return rc;
  1209. }
  1210. /**
  1211. * cpr4_apss_parse_boost_properties() - parse configuration data for boost
  1212. * voltage adjustment for CPR3 regulator from device tree.
  1213. * @vreg: Pointer to the CPR3 regulator
  1214. *
  1215. * Return: 0 on success, errno on failure
  1216. */
  1217. static int cpr4_apss_parse_boost_properties(struct cpr3_regulator *vreg)
  1218. {
  1219. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  1220. struct cpr4_apss_fuses *fuse = vreg->platform_fuses;
  1221. struct cpr3_corner *corner;
  1222. int i, boost_voltage, final_boost_volt, rc = 0;
  1223. int *boost_table = NULL, *boost_temp_adj = NULL;
  1224. int boost_voltage_adjust = 0, boost_num_cores = 0;
  1225. u32 boost_allowed = 0;
  1226. if (!boost_fuse[fuse->boost_cfg])
  1227. /* Voltage boost is disabled in fuse */
  1228. return 0;
  1229. if (of_find_property(vreg->of_node, "qcom,allow-boost", NULL)) {
  1230. rc = cpr3_parse_array_property(vreg, "qcom,allow-boost", 1,
  1231. &boost_allowed);
  1232. if (rc)
  1233. return rc;
  1234. }
  1235. if (!boost_allowed) {
  1236. /* Voltage boost is not enabled for this regulator */
  1237. return 0;
  1238. }
  1239. boost_voltage = cpr3_convert_open_loop_voltage_fuse(
  1240. MSM8953_APSS_BOOST_FUSE_REF_VOLT,
  1241. CPR4_APSS_FUSE_STEP_VOLT,
  1242. fuse->boost_voltage,
  1243. CPR4_APSS_VOLTAGE_FUSE_SIZE);
  1244. /* Log boost voltage value for debugging purposes. */
  1245. cpr3_info(vreg, "Boost open-loop=%7d uV\n", boost_voltage);
  1246. if (of_find_property(vreg->of_node,
  1247. "qcom,cpr-boost-voltage-fuse-adjustment", NULL)) {
  1248. rc = cpr3_parse_array_property(vreg,
  1249. "qcom,cpr-boost-voltage-fuse-adjustment",
  1250. 1, &boost_voltage_adjust);
  1251. if (rc) {
  1252. cpr3_err(vreg, "qcom,cpr-boost-voltage-fuse-adjustment reading failed, rc=%d\n",
  1253. rc);
  1254. return rc;
  1255. }
  1256. boost_voltage += boost_voltage_adjust;
  1257. /* Log boost voltage value for debugging purposes. */
  1258. cpr3_info(vreg, "Adjusted boost open-loop=%7d uV\n",
  1259. boost_voltage);
  1260. }
  1261. /* Limit boost voltage value between ceiling and floor voltage limits */
  1262. boost_voltage = min(boost_voltage, MSM8953_APSS_BOOST_CEILING_VOLT);
  1263. boost_voltage = max(boost_voltage, MSM8953_APSS_BOOST_FLOOR_VOLT);
  1264. /*
  1265. * The boost feature can only be used for the highest voltage corner.
  1266. * Also, keep core-count adjustments disabled when the boost feature
  1267. * is enabled.
  1268. */
  1269. corner = &vreg->corner[vreg->corner_count - 1];
  1270. if (!corner->sdelta) {
  1271. /*
  1272. * If core-count/temp adjustments are not defined, the cpr4
  1273. * sdelta for this corner will not be allocated. Allocate it
  1274. * here for boost configuration.
  1275. */
  1276. corner->sdelta = devm_kzalloc(ctrl->dev,
  1277. sizeof(*corner->sdelta), GFP_KERNEL);
  1278. if (!corner->sdelta)
  1279. return -ENOMEM;
  1280. }
  1281. corner->sdelta->temp_band_count = ctrl->temp_band_count;
  1282. rc = of_property_read_u32(vreg->of_node, "qcom,cpr-num-boost-cores",
  1283. &boost_num_cores);
  1284. if (rc) {
  1285. cpr3_err(vreg, "qcom,cpr-num-boost-cores reading failed, rc=%d\n",
  1286. rc);
  1287. return rc;
  1288. }
  1289. if (boost_num_cores <= 0
  1290. || boost_num_cores > MSM8953_APSS_CPR_SDELTA_CORE_COUNT) {
  1291. cpr3_err(vreg, "Invalid boost number of cores = %d\n",
  1292. boost_num_cores);
  1293. return -EINVAL;
  1294. }
  1295. corner->sdelta->boost_num_cores = boost_num_cores;
  1296. boost_table = devm_kcalloc(ctrl->dev, corner->sdelta->temp_band_count,
  1297. sizeof(*boost_table), GFP_KERNEL);
  1298. if (!boost_table)
  1299. return -ENOMEM;
  1300. if (of_find_property(vreg->of_node,
  1301. "qcom,cpr-boost-temp-adjustment", NULL)) {
  1302. boost_temp_adj = kcalloc(corner->sdelta->temp_band_count,
  1303. sizeof(*boost_temp_adj), GFP_KERNEL);
  1304. if (!boost_temp_adj)
  1305. return -ENOMEM;
  1306. rc = cpr3_parse_array_property(vreg,
  1307. "qcom,cpr-boost-temp-adjustment",
  1308. corner->sdelta->temp_band_count,
  1309. boost_temp_adj);
  1310. if (rc) {
  1311. cpr3_err(vreg, "qcom,cpr-boost-temp-adjustment reading failed, rc=%d\n",
  1312. rc);
  1313. goto done;
  1314. }
  1315. }
  1316. for (i = 0; i < corner->sdelta->temp_band_count; i++) {
  1317. /* Apply static adjustments to boost voltage */
  1318. final_boost_volt = boost_voltage + (boost_temp_adj == NULL
  1319. ? 0 : boost_temp_adj[i]);
  1320. /*
  1321. * Limit final adjusted boost voltage value between ceiling
  1322. * and floor voltage limits
  1323. */
  1324. final_boost_volt = min(final_boost_volt,
  1325. MSM8953_APSS_BOOST_CEILING_VOLT);
  1326. final_boost_volt = max(final_boost_volt,
  1327. MSM8953_APSS_BOOST_FLOOR_VOLT);
  1328. boost_table[i] = (corner->open_loop_volt - final_boost_volt)
  1329. / ctrl->step_volt;
  1330. cpr3_debug(vreg, "Adjusted boost voltage margin for temp band %d = %d steps\n",
  1331. i, boost_table[i]);
  1332. }
  1333. corner->ceiling_volt = MSM8953_APSS_BOOST_CEILING_VOLT;
  1334. corner->sdelta->boost_table = boost_table;
  1335. corner->sdelta->allow_boost = true;
  1336. corner->sdelta->allow_core_count_adj = false;
  1337. vreg->allow_boost = true;
  1338. ctrl->allow_boost = true;
  1339. done:
  1340. kfree(boost_temp_adj);
  1341. return rc;
  1342. }
  1343. /*
  1344. * Constants which define the selection fuse parameters used in fuse combo map
  1345. * logic.
  1346. */
  1347. enum cpr4_apss_fuse_combo_parameters {
  1348. CPR4_APSS_SPEED_BIN = 0,
  1349. CPR4_APSS_CPR_FUSE_REV,
  1350. CPR4_APSS_FOUNDRY_ID,
  1351. CPR4_APSS_FUSE_COMBO_PARAM_COUNT,
  1352. };
  1353. /**
  1354. * cpr4_parse_fuse_combo_map() - parse APSS fuse combo map data from device tree
  1355. * properties of the CPR3 regulator's device node
  1356. * @vreg: Pointer to the CPR3 regulator
  1357. *
  1358. * Return: 0 on success, errno on failure
  1359. */
  1360. static int cpr4_parse_fuse_combo_map(struct cpr3_regulator *vreg)
  1361. {
  1362. struct cpr4_apss_fuses *fuse = vreg->platform_fuses;
  1363. u64 *fuse_val;
  1364. int rc;
  1365. fuse_val = kcalloc(CPR4_APSS_FUSE_COMBO_PARAM_COUNT,
  1366. sizeof(*fuse_val), GFP_KERNEL);
  1367. if (!fuse_val)
  1368. return -ENOMEM;
  1369. fuse_val[CPR4_APSS_SPEED_BIN] = fuse->speed_bin;
  1370. fuse_val[CPR4_APSS_CPR_FUSE_REV] = fuse->cpr_fusing_rev;
  1371. fuse_val[CPR4_APSS_FOUNDRY_ID] = fuse->foundry_id;
  1372. rc = cpr3_parse_fuse_combo_map(vreg, fuse_val,
  1373. CPR4_APSS_FUSE_COMBO_PARAM_COUNT);
  1374. if (rc == -ENODEV) {
  1375. cpr3_debug(vreg, "using legacy fuse combo logic, rc=%d\n",
  1376. rc);
  1377. rc = 0;
  1378. } else if (rc < 0) {
  1379. cpr3_err(vreg, "error reading fuse combo map data, rc=%d\n",
  1380. rc);
  1381. } else if (vreg->fuse_combo >= CPR4_APSS_FUSE_COMBO_MAP_MAX_COUNT) {
  1382. cpr3_err(vreg, "invalid CPR fuse combo = %d found\n",
  1383. vreg->fuse_combo);
  1384. rc = -EINVAL;
  1385. }
  1386. kfree(fuse_val);
  1387. return rc;
  1388. }
  1389. /**
  1390. * cpr4_apss_init_regulator() - perform all steps necessary to initialize the
  1391. * configuration data for a CPR3 regulator
  1392. * @vreg: Pointer to the CPR3 regulator
  1393. *
  1394. * Return: 0 on success, errno on failure
  1395. */
  1396. static int cpr4_apss_init_regulator(struct cpr3_regulator *vreg)
  1397. {
  1398. struct cpr4_apss_fuses *fuse;
  1399. int rc;
  1400. rc = cpr4_apss_read_fuse_data(vreg);
  1401. if (rc) {
  1402. cpr3_err(vreg, "unable to read CPR fuse data, rc=%d\n", rc);
  1403. return rc;
  1404. }
  1405. fuse = vreg->platform_fuses;
  1406. rc = cpr4_parse_fuse_combo_map(vreg);
  1407. if (rc) {
  1408. cpr3_err(vreg, "error while parsing fuse combo map, rc=%d\n",
  1409. rc);
  1410. return rc;
  1411. }
  1412. rc = cpr4_apss_parse_corner_data(vreg);
  1413. if (rc) {
  1414. cpr3_err(vreg, "unable to read CPR corner data from device tree, rc=%d\n",
  1415. rc);
  1416. return rc;
  1417. }
  1418. rc = cpr3_mem_acc_init(vreg);
  1419. if (rc) {
  1420. if (rc != -EPROBE_DEFER)
  1421. cpr3_err(vreg, "unable to initialize mem-acc regulator settings, rc=%d\n",
  1422. rc);
  1423. return rc;
  1424. }
  1425. rc = cpr4_apss_calculate_open_loop_voltages(vreg);
  1426. if (rc) {
  1427. cpr3_err(vreg, "unable to calculate open-loop voltages, rc=%d\n",
  1428. rc);
  1429. return rc;
  1430. }
  1431. rc = cpr3_limit_open_loop_voltages(vreg);
  1432. if (rc) {
  1433. cpr3_err(vreg, "unable to limit open-loop voltages, rc=%d\n",
  1434. rc);
  1435. return rc;
  1436. }
  1437. cpr3_open_loop_voltage_as_ceiling(vreg);
  1438. rc = cpr3_limit_floor_voltages(vreg);
  1439. if (rc) {
  1440. cpr3_err(vreg, "unable to limit floor voltages, rc=%d\n", rc);
  1441. return rc;
  1442. }
  1443. rc = cpr4_apss_calculate_target_quotients(vreg);
  1444. if (rc) {
  1445. cpr3_err(vreg, "unable to calculate target quotients, rc=%d\n",
  1446. rc);
  1447. return rc;
  1448. }
  1449. rc = cpr4_parse_core_count_temp_voltage_adj(vreg, false);
  1450. if (rc) {
  1451. cpr3_err(vreg, "unable to parse temperature and core count voltage adjustments, rc=%d\n",
  1452. rc);
  1453. return rc;
  1454. }
  1455. if (vreg->allow_core_count_adj && (vreg->max_core_count <= 0
  1456. || vreg->max_core_count >
  1457. MSM8953_APSS_CPR_SDELTA_CORE_COUNT)) {
  1458. cpr3_err(vreg, "qcom,max-core-count has invalid value = %d\n",
  1459. vreg->max_core_count);
  1460. return -EINVAL;
  1461. }
  1462. rc = cpr4_apss_parse_boost_properties(vreg);
  1463. if (rc) {
  1464. cpr3_err(vreg, "unable to parse boost adjustments, rc=%d\n",
  1465. rc);
  1466. return rc;
  1467. }
  1468. cpr4_apss_print_settings(vreg);
  1469. return rc;
  1470. }
  1471. /**
  1472. * cpr4_apss_init_aging() - perform APSS CPR4 controller specific
  1473. * aging initializations
  1474. * @ctrl: Pointer to the CPR3 controller
  1475. *
  1476. * Return: 0 on success, errno on failure
  1477. */
  1478. static int cpr4_apss_init_aging(struct cpr3_controller *ctrl)
  1479. {
  1480. struct cpr4_apss_fuses *fuse = NULL;
  1481. struct cpr3_regulator *vreg = NULL;
  1482. u32 aging_ro_scale;
  1483. int i, j, rc;
  1484. for (i = 0; i < ctrl->thread_count; i++) {
  1485. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  1486. if (ctrl->thread[i].vreg[j].aging_allowed) {
  1487. ctrl->aging_required = true;
  1488. vreg = &ctrl->thread[i].vreg[j];
  1489. fuse = vreg->platform_fuses;
  1490. break;
  1491. }
  1492. }
  1493. }
  1494. if (!ctrl->aging_required || !fuse)
  1495. return 0;
  1496. rc = cpr3_parse_array_property(vreg, "qcom,cpr-aging-ro-scaling-factor",
  1497. 1, &aging_ro_scale);
  1498. if (rc)
  1499. return rc;
  1500. if (aging_ro_scale == 0) {
  1501. cpr3_err(ctrl, "aging RO scaling factor is invalid: %u\n",
  1502. aging_ro_scale);
  1503. return -EINVAL;
  1504. }
  1505. ctrl->aging_vdd_mode = REGULATOR_MODE_NORMAL;
  1506. ctrl->aging_complete_vdd_mode = REGULATOR_MODE_IDLE;
  1507. ctrl->aging_sensor_count = 1;
  1508. ctrl->aging_sensor = kzalloc(sizeof(*ctrl->aging_sensor), GFP_KERNEL);
  1509. if (!ctrl->aging_sensor)
  1510. return -ENOMEM;
  1511. ctrl->aging_sensor->sensor_id = MSM8953_APSS_AGING_SENSOR_ID;
  1512. ctrl->aging_sensor->ro_scale = aging_ro_scale;
  1513. ctrl->aging_sensor->init_quot_diff
  1514. = cpr3_convert_open_loop_voltage_fuse(0,
  1515. MSM8953_APSS_AGING_INIT_QUOT_DIFF_SCALE,
  1516. fuse->aging_init_quot_diff,
  1517. MSM8953_APSS_AGING_INIT_QUOT_DIFF_SIZE);
  1518. if (ctrl->aging_sensor->init_quot_diff == 0) {
  1519. /*
  1520. * Initial quotient difference value '0' has a special meaning
  1521. * in MSM8953 fusing scheme. Use max age margin quotient
  1522. * difference to consider full aging margin of 15 mV.
  1523. */
  1524. ctrl->aging_sensor->init_quot_diff
  1525. = MSM8953_APSS_AGING_MAX_AGE_MARGIN_QUOT;
  1526. cpr3_debug(ctrl, "Init quotient diff = 0, use max age margin quotient\n");
  1527. }
  1528. cpr3_info(ctrl, "sensor %u aging init quotient diff = %d, aging RO scale = %u QUOT/V\n",
  1529. ctrl->aging_sensor->sensor_id,
  1530. ctrl->aging_sensor->init_quot_diff,
  1531. ctrl->aging_sensor->ro_scale);
  1532. return 0;
  1533. }
  1534. /**
  1535. * cpr4_apss_init_controller() - perform APSS CPR4 controller specific
  1536. * initializations
  1537. * @ctrl: Pointer to the CPR3 controller
  1538. *
  1539. * Return: 0 on success, errno on failure
  1540. */
  1541. static int cpr4_apss_init_controller(struct cpr3_controller *ctrl)
  1542. {
  1543. int i, rc;
  1544. rc = cpr3_parse_common_ctrl_data(ctrl);
  1545. if (rc) {
  1546. if (rc != -EPROBE_DEFER)
  1547. cpr3_err(ctrl, "unable to parse common controller data, rc=%d\n",
  1548. rc);
  1549. return rc;
  1550. }
  1551. rc = of_property_read_u32(ctrl->dev->of_node,
  1552. "qcom,cpr-down-error-step-limit",
  1553. &ctrl->down_error_step_limit);
  1554. if (rc) {
  1555. cpr3_err(ctrl, "error reading qcom,cpr-down-error-step-limit, rc=%d\n",
  1556. rc);
  1557. return rc;
  1558. }
  1559. rc = of_property_read_u32(ctrl->dev->of_node,
  1560. "qcom,cpr-up-error-step-limit",
  1561. &ctrl->up_error_step_limit);
  1562. if (rc) {
  1563. cpr3_err(ctrl, "error reading qcom,cpr-up-error-step-limit, rc=%d\n",
  1564. rc);
  1565. return rc;
  1566. }
  1567. /*
  1568. * Use fixed step quotient if specified otherwise use dynamic
  1569. * calculated per RO step quotient
  1570. */
  1571. of_property_read_u32(ctrl->dev->of_node, "qcom,cpr-step-quot-fixed",
  1572. &ctrl->step_quot_fixed);
  1573. ctrl->use_dynamic_step_quot = ctrl->step_quot_fixed ? false : true;
  1574. ctrl->saw_use_unit_mV = of_property_read_bool(ctrl->dev->of_node,
  1575. "qcom,cpr-saw-use-unit-mV");
  1576. of_property_read_u32(ctrl->dev->of_node,
  1577. "qcom,cpr-voltage-settling-time",
  1578. &ctrl->voltage_settling_time);
  1579. ctrl->vdd_limit_regulator = devm_regulator_get(ctrl->dev, "vdd-limit");
  1580. if (IS_ERR(ctrl->vdd_limit_regulator)) {
  1581. rc = PTR_ERR(ctrl->vdd_limit_regulator);
  1582. if (rc != -EPROBE_DEFER)
  1583. cpr3_err(ctrl, "unable to request vdd-limit regulator, rc=%d\n",
  1584. rc);
  1585. return rc;
  1586. }
  1587. rc = cpr3_apm_init(ctrl);
  1588. if (rc) {
  1589. if (rc != -EPROBE_DEFER)
  1590. cpr3_err(ctrl, "unable to initialize APM settings, rc=%d\n",
  1591. rc);
  1592. return rc;
  1593. }
  1594. rc = cpr4_apss_parse_temp_adj_properties(ctrl);
  1595. if (rc) {
  1596. cpr3_err(ctrl, "unable to parse temperature adjustment properties, rc=%d\n",
  1597. rc);
  1598. return rc;
  1599. }
  1600. switch (ctrl->soc_revision) {
  1601. case MSM8953_SOC_ID:
  1602. ctrl->sensor_count = MSM8953_APSS_CPR_SENSOR_COUNT;
  1603. break;
  1604. case SDM632_SOC_ID:
  1605. ctrl->sensor_count = SDM632_APSS_CPR_SENSOR_COUNT;
  1606. break;
  1607. default:
  1608. cpr3_err(ctrl, "unsupported soc id = %d\n", ctrl->soc_revision);
  1609. return -EINVAL;
  1610. }
  1611. /*
  1612. * MSM8953 APSS only has one thread (0) per controller so the zeroed
  1613. * array does not need further modification.
  1614. */
  1615. ctrl->sensor_owner = devm_kcalloc(ctrl->dev, ctrl->sensor_count,
  1616. sizeof(*ctrl->sensor_owner), GFP_KERNEL);
  1617. if (!ctrl->sensor_owner)
  1618. return -ENOMEM;
  1619. /* Specify sensor ownership for SDM632 APSS CPR */
  1620. if (ctrl->soc_revision == SDM632_SOC_ID) {
  1621. for (i = SDM632_APSS_THREAD0_SENSOR_MIN;
  1622. i <= SDM632_APSS_THREAD0_SENSOR_MAX; i++)
  1623. ctrl->sensor_owner[i] = 0;
  1624. for (i = SDM632_APSS_THREAD1_SENSOR_MIN;
  1625. i <= SDM632_APSS_THREAD1_SENSOR_MAX; i++)
  1626. ctrl->sensor_owner[i] = 1;
  1627. }
  1628. ctrl->cpr_clock_rate = CPR4_APSS_CPR_CLOCK_RATE;
  1629. ctrl->ctrl_type = CPR_CTRL_TYPE_CPR4;
  1630. ctrl->supports_hw_closed_loop = true;
  1631. ctrl->use_hw_closed_loop = of_property_read_bool(ctrl->dev->of_node,
  1632. "qcom,cpr-hw-closed-loop");
  1633. return 0;
  1634. }
  1635. #if CONFIG_PM
  1636. static int cpr4_apss_regulator_suspend(struct device *dev)
  1637. {
  1638. struct cpr3_controller *ctrl = dev_get_drvdata(dev);
  1639. return cpr3_regulator_suspend(ctrl);
  1640. }
  1641. static int cpr4_apss_regulator_resume(struct device *dev)
  1642. {
  1643. struct cpr3_controller *ctrl = dev_get_drvdata(dev);
  1644. return cpr3_regulator_resume(ctrl);
  1645. }
  1646. #else
  1647. #define cpr4_apss_regulator_suspend NULL
  1648. #define cpr4_apss_regulator_resume NULL
  1649. #endif
  1650. static const struct dev_pm_ops cpr4_apss_regulator_pm_ops = {
  1651. .suspend = cpr4_apss_regulator_suspend,
  1652. .resume = cpr4_apss_regulator_resume,
  1653. };
  1654. /* Data corresponds to the SoC revision */
  1655. static const struct of_device_id cpr4_regulator_match_table[] = {
  1656. {
  1657. .compatible = "qcom,cpr4-msm8953-apss-regulator",
  1658. .data = (void *)(uintptr_t)MSM8953_SOC_ID,
  1659. },
  1660. {
  1661. .compatible = "qcom,cpr4-sdm632-apss-regulator",
  1662. .data = (void *)(uintptr_t)SDM632_SOC_ID,
  1663. },
  1664. {}
  1665. };
  1666. static int cpr4_apss_regulator_probe(struct platform_device *pdev)
  1667. {
  1668. struct device *dev = &pdev->dev;
  1669. struct cpr3_controller *ctrl;
  1670. struct cpr3_regulator *vreg;
  1671. const struct of_device_id *match;
  1672. int i, j, rc, max_thread_id;
  1673. if (!dev->of_node) {
  1674. dev_err(dev, "Device tree node is missing\n");
  1675. return -EINVAL;
  1676. }
  1677. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  1678. if (!ctrl)
  1679. return -ENOMEM;
  1680. ctrl->dev = dev;
  1681. /* Set to false later if anything precludes CPR operation. */
  1682. ctrl->cpr_allowed_hw = true;
  1683. match = of_match_node(cpr4_regulator_match_table, dev->of_node);
  1684. if (match)
  1685. ctrl->soc_revision = (uintptr_t)match->data;
  1686. else
  1687. cpr3_err(ctrl, "could not find compatible string match\n");
  1688. rc = of_property_read_string(dev->of_node, "qcom,cpr-ctrl-name",
  1689. &ctrl->name);
  1690. if (rc) {
  1691. cpr3_err(ctrl, "unable to read qcom,cpr-ctrl-name, rc=%d\n",
  1692. rc);
  1693. return rc;
  1694. }
  1695. rc = cpr3_map_fuse_base(ctrl, pdev);
  1696. if (rc) {
  1697. cpr3_err(ctrl, "could not map fuse base address\n");
  1698. return rc;
  1699. }
  1700. max_thread_id = 0;
  1701. /* SDM632 uses 2 CPR HW threads */
  1702. if (ctrl->soc_revision == SDM632_SOC_ID)
  1703. max_thread_id = 1;
  1704. rc = cpr3_allocate_threads(ctrl, 0, max_thread_id);
  1705. if (rc) {
  1706. cpr3_err(ctrl, "failed to allocate CPR thread array, rc=%d\n",
  1707. rc);
  1708. return rc;
  1709. }
  1710. if (ctrl->thread_count < 1) {
  1711. cpr3_err(ctrl, "thread nodes are missing\n");
  1712. return -EINVAL;
  1713. }
  1714. rc = cpr4_apss_init_controller(ctrl);
  1715. if (rc) {
  1716. if (rc != -EPROBE_DEFER)
  1717. cpr3_err(ctrl, "failed to initialize CPR controller parameters, rc=%d\n",
  1718. rc);
  1719. return rc;
  1720. }
  1721. for (i = 0; i < ctrl->thread_count; i++) {
  1722. rc = cpr4_apss_init_thread(&ctrl->thread[i]);
  1723. if (rc) {
  1724. cpr3_err(ctrl, "thread %u initialization failed, rc=%d\n",
  1725. ctrl->thread[i].thread_id, rc);
  1726. return rc;
  1727. }
  1728. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  1729. vreg = &ctrl->thread[i].vreg[j];
  1730. rc = cpr4_apss_init_regulator(vreg);
  1731. if (rc) {
  1732. cpr3_err(vreg, "regulator initialization failed, rc=%d\n",
  1733. rc);
  1734. return rc;
  1735. }
  1736. }
  1737. }
  1738. rc = cpr4_apss_init_aging(ctrl);
  1739. if (rc) {
  1740. cpr3_err(ctrl, "failed to initialize aging configurations, rc=%d\n",
  1741. rc);
  1742. return rc;
  1743. }
  1744. platform_set_drvdata(pdev, ctrl);
  1745. return cpr3_regulator_register(pdev, ctrl);
  1746. }
  1747. static int cpr4_apss_regulator_remove(struct platform_device *pdev)
  1748. {
  1749. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  1750. return cpr3_regulator_unregister(ctrl);
  1751. }
  1752. static struct platform_driver cpr4_apss_regulator_driver = {
  1753. .driver = {
  1754. .name = "qcom,cpr4-apss-regulator",
  1755. .of_match_table = cpr4_regulator_match_table,
  1756. .owner = THIS_MODULE,
  1757. .pm = &cpr4_apss_regulator_pm_ops,
  1758. },
  1759. .probe = cpr4_apss_regulator_probe,
  1760. .remove = cpr4_apss_regulator_remove,
  1761. };
  1762. static int cpr4_regulator_init(void)
  1763. {
  1764. return platform_driver_register(&cpr4_apss_regulator_driver);
  1765. }
  1766. static void cpr4_regulator_exit(void)
  1767. {
  1768. platform_driver_unregister(&cpr4_apss_regulator_driver);
  1769. }
  1770. MODULE_DESCRIPTION("CPR4 APSS regulator driver");
  1771. MODULE_LICENSE("GPL v2");
  1772. arch_initcall(cpr4_regulator_init);
  1773. module_exit(cpr4_regulator_exit);