cpr3-hmss-regulator.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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 <linux/regulator/msm-ldo-regulator.h>
  34. #include "cpr3-regulator.h"
  35. #define MSM8996_HMSS_FUSE_CORNERS 5
  36. /**
  37. * struct cpr3_msm8996_hmss_fuses - HMSS specific fuse data for MSM8996
  38. * @ro_sel: Ring oscillator select fuse parameter value for each
  39. * fuse corner
  40. * @init_voltage: Initial (i.e. open-loop) voltage fuse parameter value
  41. * for each fuse corner (raw, not converted to a voltage)
  42. * @target_quot: CPR target quotient fuse parameter value for each fuse
  43. * corner
  44. * @quot_offset: CPR target quotient offset fuse parameter value for each
  45. * fuse corner (raw, not unpacked) used for target quotient
  46. * interpolation
  47. * @speed_bin: Application processor speed bin fuse parameter value for
  48. * the given chip
  49. * @cbf_voltage_offset: Voltage margin offset for the CBF regulator used on
  50. * MSM8996-Pro chips.
  51. * @cpr_fusing_rev: CPR fusing revision fuse parameter value
  52. * @redundant_fusing: Redundant fusing select fuse parameter value
  53. * @limitation: CPR limitation select fuse parameter value
  54. * @partial_binning: Chip partial binning fuse parameter value which defines
  55. * limitations found on a given chip
  56. * @vdd_mx_ret_fuse: Defines the logic retention voltage of VDD_MX
  57. * @vdd_apcc_ret_fuse: Defines the logic retention voltage of VDD_APCC
  58. * @aging_init_quot_diff: Initial quotient difference between CPR aging
  59. * min and max sensors measured at time of manufacturing
  60. *
  61. * This struct holds the values for all of the fuses read from memory. The
  62. * values for ro_sel, init_voltage, target_quot, and quot_offset come from
  63. * either the primary or redundant fuse locations depending upon the value of
  64. * redundant_fusing.
  65. */
  66. struct cpr3_msm8996_hmss_fuses {
  67. u64 ro_sel[MSM8996_HMSS_FUSE_CORNERS];
  68. u64 init_voltage[MSM8996_HMSS_FUSE_CORNERS];
  69. u64 target_quot[MSM8996_HMSS_FUSE_CORNERS];
  70. u64 quot_offset[MSM8996_HMSS_FUSE_CORNERS];
  71. u64 cbf_voltage_offset[MSM8996_HMSS_FUSE_CORNERS];
  72. u64 speed_bin;
  73. u64 cpr_fusing_rev;
  74. u64 redundant_fusing;
  75. u64 limitation;
  76. u64 partial_binning;
  77. u64 vdd_mx_ret_fuse;
  78. u64 vdd_apcc_ret_fuse;
  79. u64 aging_init_quot_diff;
  80. };
  81. /*
  82. * Fuse combos 0 - 7 map to CPR fusing revision 0 - 7 with speed bin fuse = 0.
  83. * Fuse combos 8 - 15 map to CPR fusing revision 0 - 7 with speed bin fuse = 1.
  84. */
  85. #define CPR3_MSM8996_HMSS_FUSE_COMBO_COUNT 16
  86. /*
  87. * Constants which define the name of each fuse corner. Note that no actual
  88. * fuses are defined for LowSVS. However, a mapping from corner to LowSVS
  89. * is required in order to perform target quotient interpolation properly.
  90. */
  91. enum cpr3_msm8996_hmss_fuse_corner {
  92. CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS = 0,
  93. CPR3_MSM8996_HMSS_FUSE_CORNER_LOWSVS = 1,
  94. CPR3_MSM8996_HMSS_FUSE_CORNER_SVS = 2,
  95. CPR3_MSM8996_HMSS_FUSE_CORNER_NOM = 3,
  96. CPR3_MSM8996_HMSS_FUSE_CORNER_TURBO = 4,
  97. };
  98. static const char * const cpr3_msm8996_hmss_fuse_corner_name[] = {
  99. [CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS] = "MinSVS",
  100. [CPR3_MSM8996_HMSS_FUSE_CORNER_LOWSVS] = "LowSVS",
  101. [CPR3_MSM8996_HMSS_FUSE_CORNER_SVS] = "SVS",
  102. [CPR3_MSM8996_HMSS_FUSE_CORNER_NOM] = "NOM",
  103. [CPR3_MSM8996_HMSS_FUSE_CORNER_TURBO] = "TURBO",
  104. };
  105. /* CPR3 hardware thread IDs */
  106. #define MSM8996_HMSS_POWER_CLUSTER_THREAD_ID 0
  107. #define MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID 1
  108. /*
  109. * MSM8996 HMSS fuse parameter locations:
  110. *
  111. * Structs are organized with the following dimensions:
  112. * Outer: 0 or 1 for power or performance cluster
  113. * Middle: 0 to 3 for fuse corners from lowest to highest corner
  114. * Inner: large enough to hold the longest set of parameter segments which
  115. * fully defines a fuse parameter, +1 (for NULL termination).
  116. * Each segment corresponds to a contiguous group of bits from a
  117. * single fuse row. These segments are concatentated together in
  118. * order to form the full fuse parameter value. The segments for
  119. * a given parameter may correspond to different fuse rows.
  120. *
  121. * Note that there are only physically 4 sets of fuse parameters which
  122. * correspond to the MinSVS, SVS, NOM, and TURBO fuse corners. However, the SVS
  123. * quotient offset fuse is used to define the target quotient for the LowSVS
  124. * fuse corner. In order to utilize LowSVS, it must be treated as if it were a
  125. * real fully defined fuse corner. Thus, LowSVS fuse parameter locations are
  126. * specified. These locations duplicate the SVS values in order to simplify
  127. * interpolation logic.
  128. */
  129. static const struct cpr3_fuse_param
  130. msm8996_hmss_ro_sel_param[2][MSM8996_HMSS_FUSE_CORNERS][2] = {
  131. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  132. {{66, 38, 41}, {} },
  133. {{66, 38, 41}, {} },
  134. {{66, 38, 41}, {} },
  135. {{66, 34, 37}, {} },
  136. {{66, 30, 33}, {} },
  137. },
  138. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  139. {{64, 54, 57}, {} },
  140. {{64, 54, 57}, {} },
  141. {{64, 54, 57}, {} },
  142. {{64, 50, 53}, {} },
  143. {{64, 46, 49}, {} },
  144. },
  145. };
  146. static const struct cpr3_fuse_param
  147. msm8996_hmss_init_voltage_param[2][MSM8996_HMSS_FUSE_CORNERS][3] = {
  148. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  149. {{67, 0, 5}, {} },
  150. {{66, 58, 63}, {} },
  151. {{66, 58, 63}, {} },
  152. {{66, 52, 57}, {} },
  153. {{66, 46, 51}, {} },
  154. },
  155. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  156. {{65, 16, 21}, {} },
  157. {{65, 10, 15}, {} },
  158. {{65, 10, 15}, {} },
  159. {{65, 4, 9}, {} },
  160. {{64, 62, 63}, {65, 0, 3}, {} },
  161. },
  162. };
  163. static const struct cpr3_fuse_param
  164. msm8996_hmss_target_quot_param[2][MSM8996_HMSS_FUSE_CORNERS][3] = {
  165. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  166. {{67, 42, 53}, {} },
  167. {{67, 30, 41}, {} },
  168. {{67, 30, 41}, {} },
  169. {{67, 18, 29}, {} },
  170. {{67, 6, 17}, {} },
  171. },
  172. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  173. {{65, 58, 63}, {66, 0, 5}, {} },
  174. {{65, 46, 57}, {} },
  175. {{65, 46, 57}, {} },
  176. {{65, 34, 45}, {} },
  177. {{65, 22, 33}, {} },
  178. },
  179. };
  180. static const struct cpr3_fuse_param
  181. msm8996_hmss_quot_offset_param[2][MSM8996_HMSS_FUSE_CORNERS][3] = {
  182. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  183. {{} },
  184. {{} },
  185. {{68, 6, 13}, {} },
  186. {{67, 62, 63}, {68, 0, 5}, {} },
  187. {{67, 54, 61}, {} },
  188. },
  189. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  190. {{} },
  191. {{} },
  192. {{66, 22, 29}, {} },
  193. {{66, 14, 21}, {} },
  194. {{66, 6, 13}, {} },
  195. },
  196. };
  197. /*
  198. * This fuse is used to define if the redundant set of fuses should be used for
  199. * any particular feature. CPR is one such feature. The redundant CPR fuses
  200. * should be used if this fuse parameter has a value of 1.
  201. */
  202. static const struct cpr3_fuse_param msm8996_redundant_fusing_param[] = {
  203. {73, 61, 63},
  204. {},
  205. };
  206. #define MSM8996_CPR_REDUNDANT_FUSING 1
  207. static const struct cpr3_fuse_param
  208. msm8996_hmss_redun_ro_sel_param[2][MSM8996_HMSS_FUSE_CORNERS][2] = {
  209. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  210. {{76, 36, 39}, {} },
  211. {{76, 32, 35}, {} },
  212. {{76, 32, 35}, {} },
  213. {{76, 28, 31}, {} },
  214. {{76, 24, 27}, {} },
  215. },
  216. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  217. {{74, 52, 55}, {} },
  218. {{74, 48, 51}, {} },
  219. {{74, 48, 51}, {} },
  220. {{74, 44, 47}, {} },
  221. {{74, 40, 43}, {} },
  222. },
  223. };
  224. static const struct cpr3_fuse_param
  225. msm8996_hmss_redun_init_voltage_param[2][MSM8996_HMSS_FUSE_CORNERS][3] = {
  226. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  227. {{76, 58, 63}, {} },
  228. {{76, 52, 57}, {} },
  229. {{76, 52, 57}, {} },
  230. {{76, 46, 51}, {} },
  231. {{76, 40, 45}, {} },
  232. },
  233. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  234. {{75, 10, 15}, {} },
  235. {{75, 4, 9}, {} },
  236. {{75, 4, 9}, {} },
  237. {{74, 62, 63}, {75, 0, 3}, {} },
  238. {{74, 56, 61}, {} },
  239. },
  240. };
  241. static const struct cpr3_fuse_param
  242. msm8996_hmss_redun_target_quot_param[2][MSM8996_HMSS_FUSE_CORNERS][2] = {
  243. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  244. {{77, 36, 47}, {} },
  245. {{77, 24, 35}, {} },
  246. {{77, 24, 35}, {} },
  247. {{77, 12, 23}, {} },
  248. {{77, 0, 11}, {} },
  249. },
  250. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  251. {{75, 52, 63}, {} },
  252. {{75, 40, 51}, {} },
  253. {{75, 40, 51}, {} },
  254. {{75, 28, 39}, {} },
  255. {{75, 16, 27}, {} },
  256. },
  257. };
  258. static const struct cpr3_fuse_param
  259. msm8996_hmss_redun_quot_offset_param[2][MSM8996_HMSS_FUSE_CORNERS][2] = {
  260. [MSM8996_HMSS_POWER_CLUSTER_THREAD_ID] = {
  261. {{} },
  262. {{} },
  263. {{68, 11, 18}, {} },
  264. {{77, 56, 63}, {} },
  265. {{77, 48, 55}, {} },
  266. },
  267. [MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID] = {
  268. {{} },
  269. {{} },
  270. {{76, 16, 23}, {} },
  271. {{76, 8, 15}, {} },
  272. {{76, 0, 7}, {} },
  273. },
  274. };
  275. static const struct cpr3_fuse_param msm8996_cpr_fusing_rev_param[] = {
  276. {39, 51, 53},
  277. {},
  278. };
  279. static const struct cpr3_fuse_param msm8996_hmss_speed_bin_param[] = {
  280. {38, 29, 31},
  281. {},
  282. };
  283. static const struct cpr3_fuse_param msm8996_cpr_limitation_param[] = {
  284. {41, 31, 32},
  285. {},
  286. };
  287. static const struct cpr3_fuse_param msm8996_vdd_mx_ret_param[] = {
  288. {41, 2, 4},
  289. {},
  290. };
  291. static const struct cpr3_fuse_param msm8996_vdd_apcc_ret_param[] = {
  292. {41, 52, 54},
  293. {},
  294. };
  295. static const struct cpr3_fuse_param msm8996_cpr_partial_binning_param[] = {
  296. {39, 55, 59},
  297. {},
  298. };
  299. static const struct cpr3_fuse_param
  300. msm8996_hmss_aging_init_quot_diff_param[] = {
  301. {68, 14, 19},
  302. {},
  303. };
  304. static const struct cpr3_fuse_param
  305. msm8996pro_hmss_voltage_offset_param[MSM8996_HMSS_FUSE_CORNERS][4] = {
  306. {{68, 50, 52}, {41, 63, 63}, {} },
  307. {{62, 30, 31}, {62, 63, 63}, {66, 45, 45}, {} },
  308. {{61, 35, 36}, {61, 62, 63}, {} },
  309. {{61, 26, 26}, {61, 32, 34}, {} },
  310. {{61, 22, 25}, {} },
  311. };
  312. #define MSM8996PRO_SOC_ID 4
  313. /*
  314. * Some initial msm8996 parts cannot be used in a meaningful way by software.
  315. * Other parts can only be used when operating with CPR disabled (i.e. at the
  316. * fused open-loop voltage) when no voltage interpolation is applied. A fuse
  317. * parameter is provided so that software can properly handle these limitations.
  318. */
  319. enum msm8996_cpr_limitation {
  320. MSM8996_CPR_LIMITATION_NONE = 0,
  321. MSM8996_CPR_LIMITATION_UNSUPPORTED = 2,
  322. MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION = 3,
  323. };
  324. /*
  325. * Some initial msm8996 parts cannot be operated at low voltages. A fuse
  326. * parameter is provided so that software can properly handle these limitations.
  327. */
  328. enum msm8996_cpr_partial_binning {
  329. MSM8996_CPR_PARTIAL_BINNING_SVS = 11,
  330. MSM8996_CPR_PARTIAL_BINNING_NOM = 12,
  331. };
  332. /* Additional MSM8996 specific data: */
  333. /* Open loop voltage fuse reference voltages in microvolts for MSM8996 v1/v2 */
  334. static const int msm8996_v1_v2_hmss_fuse_ref_volt[MSM8996_HMSS_FUSE_CORNERS] = {
  335. 605000,
  336. 745000, /* Place holder entry for LowSVS */
  337. 745000,
  338. 905000,
  339. 1015000,
  340. };
  341. /* Open loop voltage fuse reference voltages in microvolts for MSM8996 v3 */
  342. static const int msm8996_v3_hmss_fuse_ref_volt[MSM8996_HMSS_FUSE_CORNERS] = {
  343. 605000,
  344. 745000, /* Place holder entry for LowSVS */
  345. 745000,
  346. 905000,
  347. 1140000,
  348. };
  349. /*
  350. * Open loop voltage fuse reference voltages in microvolts for MSM8996 v3 with
  351. * speed_bin == 1 and cpr_fusing_rev >= 5.
  352. */
  353. static const int msm8996_v3_speed_bin1_rev5_hmss_fuse_ref_volt[
  354. MSM8996_HMSS_FUSE_CORNERS] = {
  355. 605000,
  356. 745000, /* Place holder entry for LowSVS */
  357. 745000,
  358. 905000,
  359. 1040000,
  360. };
  361. /* Defines mapping from retention fuse values to voltages in microvolts */
  362. static const int msm8996_vdd_apcc_fuse_ret_volt[] = {
  363. 600000, 550000, 500000, 450000, 400000, 350000, 300000, 600000,
  364. };
  365. static const int msm8996_vdd_mx_fuse_ret_volt[] = {
  366. 700000, 650000, 580000, 550000, 490000, 490000, 490000, 490000,
  367. };
  368. #define MSM8996_HMSS_FUSE_STEP_VOLT 10000
  369. #define MSM8996_HMSS_VOLTAGE_FUSE_SIZE 6
  370. #define MSM8996PRO_HMSS_CBF_FUSE_STEP_VOLT 10000
  371. #define MSM8996PRO_HMSS_CBF_VOLTAGE_FUSE_SIZE 4
  372. #define MSM8996_HMSS_QUOT_OFFSET_SCALE 5
  373. #define MSM8996_HMSS_AGING_INIT_QUOT_DIFF_SCALE 2
  374. #define MSM8996_HMSS_AGING_INIT_QUOT_DIFF_SIZE 6
  375. #define MSM8996_HMSS_CPR_SENSOR_COUNT 25
  376. #define MSM8996_HMSS_THREAD0_SENSOR_MIN 0
  377. #define MSM8996_HMSS_THREAD0_SENSOR_MAX 14
  378. #define MSM8996_HMSS_THREAD1_SENSOR_MIN 15
  379. #define MSM8996_HMSS_THREAD1_SENSOR_MAX 24
  380. #define MSM8996_HMSS_CPR_CLOCK_RATE 19200000
  381. #define MSM8996_HMSS_AGING_SENSOR_ID 11
  382. #define MSM8996_HMSS_AGING_BYPASS_MASK0 (GENMASK(7, 0) & ~BIT(3))
  383. /**
  384. * cpr3_msm8996_hmss_use_voltage_offset_fuse() - return if this part utilizes
  385. * voltage offset fuses or not
  386. * @vreg: Pointer to the CPR3 regulator
  387. *
  388. * Return: true if this part utilizes voltage offset fuses, else false
  389. */
  390. static inline bool cpr3_msm8996_hmss_use_voltage_offset_fuse(
  391. struct cpr3_regulator *vreg)
  392. {
  393. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  394. return vreg->thread->ctrl->soc_revision == MSM8996PRO_SOC_ID
  395. && fuse->cpr_fusing_rev >= 1
  396. && of_property_read_bool(vreg->of_node, "qcom,is-cbf-regulator");
  397. }
  398. /**
  399. * cpr3_msm8996_hmss_read_fuse_data() - load HMSS specific fuse parameter values
  400. * @vreg: Pointer to the CPR3 regulator
  401. *
  402. * This function allocates a cpr3_msm8996_hmss_fuses struct, fills it with
  403. * values read out of hardware fuses, and finally copies common fuse values
  404. * into the CPR3 regulator struct.
  405. *
  406. * Return: 0 on success, errno on failure
  407. */
  408. static int cpr3_msm8996_hmss_read_fuse_data(struct cpr3_regulator *vreg)
  409. {
  410. void __iomem *base = vreg->thread->ctrl->fuse_base;
  411. struct cpr3_msm8996_hmss_fuses *fuse;
  412. bool redundant;
  413. int i, id, rc;
  414. fuse = devm_kzalloc(vreg->thread->ctrl->dev, sizeof(*fuse), GFP_KERNEL);
  415. if (!fuse)
  416. return -ENOMEM;
  417. rc = cpr3_read_fuse_param(base, msm8996_hmss_speed_bin_param,
  418. &fuse->speed_bin);
  419. if (rc) {
  420. cpr3_err(vreg, "Unable to read speed bin fuse, rc=%d\n", rc);
  421. return rc;
  422. }
  423. cpr3_info(vreg, "speed bin = %llu\n", fuse->speed_bin);
  424. rc = cpr3_read_fuse_param(base, msm8996_cpr_fusing_rev_param,
  425. &fuse->cpr_fusing_rev);
  426. if (rc) {
  427. cpr3_err(vreg, "Unable to read CPR fusing revision fuse, rc=%d\n",
  428. rc);
  429. return rc;
  430. }
  431. cpr3_info(vreg, "CPR fusing revision = %llu\n", fuse->cpr_fusing_rev);
  432. rc = cpr3_read_fuse_param(base, msm8996_redundant_fusing_param,
  433. &fuse->redundant_fusing);
  434. if (rc) {
  435. cpr3_err(vreg, "Unable to read redundant fusing config fuse, rc=%d\n",
  436. rc);
  437. return rc;
  438. }
  439. redundant = (fuse->redundant_fusing == MSM8996_CPR_REDUNDANT_FUSING);
  440. cpr3_info(vreg, "using redundant fuses = %c\n",
  441. redundant ? 'Y' : 'N');
  442. rc = cpr3_read_fuse_param(base, msm8996_cpr_limitation_param,
  443. &fuse->limitation);
  444. if (rc) {
  445. cpr3_err(vreg, "Unable to read CPR limitation fuse, rc=%d\n",
  446. rc);
  447. return rc;
  448. }
  449. cpr3_info(vreg, "CPR limitation = %s\n",
  450. fuse->limitation == MSM8996_CPR_LIMITATION_UNSUPPORTED
  451. ? "unsupported chip" : fuse->limitation
  452. == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION
  453. ? "CPR disabled and no interpolation" : "none");
  454. rc = cpr3_read_fuse_param(base, msm8996_cpr_partial_binning_param,
  455. &fuse->partial_binning);
  456. if (rc) {
  457. cpr3_err(vreg, "Unable to read partial binning fuse, rc=%d\n",
  458. rc);
  459. return rc;
  460. }
  461. cpr3_info(vreg, "CPR partial binning limitation = %s\n",
  462. fuse->partial_binning == MSM8996_CPR_PARTIAL_BINNING_SVS
  463. ? "SVS min voltage"
  464. : fuse->partial_binning == MSM8996_CPR_PARTIAL_BINNING_NOM
  465. ? "NOM min voltage"
  466. : "none");
  467. rc = cpr3_read_fuse_param(base, msm8996_vdd_mx_ret_param,
  468. &fuse->vdd_mx_ret_fuse);
  469. if (rc) {
  470. cpr3_err(vreg, "Unable to read VDD_MX retention fuse, rc=%d\n",
  471. rc);
  472. return rc;
  473. }
  474. rc = cpr3_read_fuse_param(base, msm8996_vdd_apcc_ret_param,
  475. &fuse->vdd_apcc_ret_fuse);
  476. if (rc) {
  477. cpr3_err(vreg, "Unable to read VDD_APCC retention fuse, rc=%d\n",
  478. rc);
  479. return rc;
  480. }
  481. cpr3_info(vreg, "Retention voltage fuses: VDD_MX = %llu, VDD_APCC = %llu\n",
  482. fuse->vdd_mx_ret_fuse, fuse->vdd_apcc_ret_fuse);
  483. rc = cpr3_read_fuse_param(base, msm8996_hmss_aging_init_quot_diff_param,
  484. &fuse->aging_init_quot_diff);
  485. if (rc) {
  486. cpr3_err(vreg, "Unable to read aging initial quotient difference fuse, rc=%d\n",
  487. rc);
  488. return rc;
  489. }
  490. id = vreg->thread->thread_id;
  491. for (i = 0; i < MSM8996_HMSS_FUSE_CORNERS; i++) {
  492. rc = cpr3_read_fuse_param(base,
  493. redundant
  494. ? msm8996_hmss_redun_init_voltage_param[id][i]
  495. : msm8996_hmss_init_voltage_param[id][i],
  496. &fuse->init_voltage[i]);
  497. if (rc) {
  498. cpr3_err(vreg, "Unable to read fuse-corner %d initial voltage fuse, rc=%d\n",
  499. i, rc);
  500. return rc;
  501. }
  502. rc = cpr3_read_fuse_param(base,
  503. redundant
  504. ? msm8996_hmss_redun_target_quot_param[id][i]
  505. : msm8996_hmss_target_quot_param[id][i],
  506. &fuse->target_quot[i]);
  507. if (rc) {
  508. cpr3_err(vreg, "Unable to read fuse-corner %d target quotient fuse, rc=%d\n",
  509. i, rc);
  510. return rc;
  511. }
  512. rc = cpr3_read_fuse_param(base,
  513. redundant
  514. ? msm8996_hmss_redun_ro_sel_param[id][i]
  515. : msm8996_hmss_ro_sel_param[id][i],
  516. &fuse->ro_sel[i]);
  517. if (rc) {
  518. cpr3_err(vreg, "Unable to read fuse-corner %d RO select fuse, rc=%d\n",
  519. i, rc);
  520. return rc;
  521. }
  522. rc = cpr3_read_fuse_param(base,
  523. redundant
  524. ? msm8996_hmss_redun_quot_offset_param[id][i]
  525. : msm8996_hmss_quot_offset_param[id][i],
  526. &fuse->quot_offset[i]);
  527. if (rc) {
  528. cpr3_err(vreg, "Unable to read fuse-corner %d quotient offset fuse, rc=%d\n",
  529. i, rc);
  530. return rc;
  531. }
  532. }
  533. vreg->fuse_combo = fuse->cpr_fusing_rev + 8 * fuse->speed_bin;
  534. if (vreg->fuse_combo >= CPR3_MSM8996_HMSS_FUSE_COMBO_COUNT) {
  535. cpr3_err(vreg, "invalid CPR fuse combo = %d found\n",
  536. vreg->fuse_combo);
  537. return -EINVAL;
  538. }
  539. vreg->speed_bin_fuse = fuse->speed_bin;
  540. vreg->cpr_rev_fuse = fuse->cpr_fusing_rev;
  541. vreg->fuse_corner_count = MSM8996_HMSS_FUSE_CORNERS;
  542. vreg->platform_fuses = fuse;
  543. if (cpr3_msm8996_hmss_use_voltage_offset_fuse(vreg)) {
  544. for (i = 0; i < MSM8996_HMSS_FUSE_CORNERS; i++) {
  545. rc = cpr3_read_fuse_param(base,
  546. msm8996pro_hmss_voltage_offset_param[i],
  547. &fuse->cbf_voltage_offset[i]);
  548. if (rc) {
  549. cpr3_err(vreg, "Unable to read fuse-corner %d CBF voltage offset fuse, rc=%d\n",
  550. i, rc);
  551. return rc;
  552. }
  553. }
  554. }
  555. return 0;
  556. }
  557. /**
  558. * cpr3_hmss_apply_fused_voltage_offset() - adjust the fused voltages for each
  559. * fuse corner according to voltage offset fuse values
  560. * @vreg: Pointer to the CPR3 regulator
  561. * @fuse_volt: Pointer to an array of the fused voltage values; must
  562. * have length equal to vreg->fuse_corner_count
  563. *
  564. * Voltage values in fuse_volt are modified in place.
  565. *
  566. * Return: 0 on success, errno on failure
  567. */
  568. static int cpr3_hmss_apply_fused_voltage_offset(struct cpr3_regulator *vreg,
  569. int *fuse_volt)
  570. {
  571. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  572. int i;
  573. if (!cpr3_msm8996_hmss_use_voltage_offset_fuse(vreg))
  574. return 0;
  575. for (i = 0; i < vreg->fuse_corner_count; i++)
  576. fuse_volt[i] += cpr3_convert_open_loop_voltage_fuse(
  577. 0,
  578. MSM8996PRO_HMSS_CBF_FUSE_STEP_VOLT,
  579. fuse->cbf_voltage_offset[i],
  580. MSM8996PRO_HMSS_CBF_VOLTAGE_FUSE_SIZE);
  581. return 0;
  582. }
  583. /**
  584. * cpr3_hmss_parse_corner_data() - parse HMSS corner data from device tree
  585. * properties of the CPR3 regulator's device node
  586. * @vreg: Pointer to the CPR3 regulator
  587. *
  588. * Return: 0 on success, errno on failure
  589. */
  590. static int cpr3_hmss_parse_corner_data(struct cpr3_regulator *vreg)
  591. {
  592. int rc;
  593. rc = cpr3_parse_common_corner_data(vreg);
  594. if (rc) {
  595. cpr3_err(vreg, "error reading corner data, rc=%d\n", rc);
  596. return rc;
  597. }
  598. return rc;
  599. }
  600. /**
  601. * cpr3_msm8996_hmss_calculate_open_loop_voltages() - calculate the open-loop
  602. * voltage for each corner of a CPR3 regulator
  603. * @vreg: Pointer to the CPR3 regulator
  604. *
  605. * If open-loop voltage interpolation is allowed in both device tree and in
  606. * hardware fuses, then this function calculates the open-loop voltage for a
  607. * given corner using linear interpolation. This interpolation is performed
  608. * using the processor frequencies of the lower and higher Fmax corners along
  609. * with their fused open-loop voltages.
  610. *
  611. * If open-loop voltage interpolation is not allowed, then this function uses
  612. * the Fmax fused open-loop voltage for all of the corners associated with a
  613. * given fuse corner.
  614. *
  615. * Return: 0 on success, errno on failure
  616. */
  617. static int cpr3_msm8996_hmss_calculate_open_loop_voltages(
  618. struct cpr3_regulator *vreg)
  619. {
  620. struct device_node *node = vreg->of_node;
  621. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  622. int rc = 0;
  623. bool allow_interpolation;
  624. u64 freq_low, volt_low, freq_high, volt_high;
  625. int i, j, soc_revision;
  626. const int *ref_volt;
  627. int *fuse_volt;
  628. int *fmax_corner;
  629. fuse_volt = kcalloc(vreg->fuse_corner_count, sizeof(*fuse_volt),
  630. GFP_KERNEL);
  631. fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
  632. GFP_KERNEL);
  633. if (!fuse_volt || !fmax_corner) {
  634. rc = -ENOMEM;
  635. goto done;
  636. }
  637. soc_revision = vreg->thread->ctrl->soc_revision;
  638. if (soc_revision == 1 || soc_revision == 2)
  639. ref_volt = msm8996_v1_v2_hmss_fuse_ref_volt;
  640. else if (soc_revision == 3 && fuse->speed_bin == 1
  641. && fuse->cpr_fusing_rev >= 5)
  642. ref_volt = msm8996_v3_speed_bin1_rev5_hmss_fuse_ref_volt;
  643. else
  644. ref_volt = msm8996_v3_hmss_fuse_ref_volt;
  645. for (i = 0; i < vreg->fuse_corner_count; i++) {
  646. fuse_volt[i] = cpr3_convert_open_loop_voltage_fuse(
  647. ref_volt[i],
  648. MSM8996_HMSS_FUSE_STEP_VOLT, fuse->init_voltage[i],
  649. MSM8996_HMSS_VOLTAGE_FUSE_SIZE);
  650. /* Log fused open-loop voltage values for debugging purposes. */
  651. if (i != CPR3_MSM8996_HMSS_FUSE_CORNER_LOWSVS)
  652. cpr3_info(vreg, "fused %6s: open-loop=%7d uV\n",
  653. cpr3_msm8996_hmss_fuse_corner_name[i],
  654. fuse_volt[i]);
  655. }
  656. if (cpr3_msm8996_hmss_use_voltage_offset_fuse(vreg)) {
  657. rc = cpr3_hmss_apply_fused_voltage_offset(vreg, fuse_volt);
  658. if (rc) {
  659. cpr3_err(vreg, "could not apply CBF voltage offsets, rc=%d\n",
  660. rc);
  661. goto done;
  662. }
  663. for (i = 0; i < vreg->fuse_corner_count; i++)
  664. cpr3_info(vreg, "fused %6s: CBF offset open-loop=%7d uV\n",
  665. cpr3_msm8996_hmss_fuse_corner_name[i],
  666. fuse_volt[i]);
  667. }
  668. rc = cpr3_adjust_fused_open_loop_voltages(vreg, fuse_volt);
  669. if (rc) {
  670. cpr3_err(vreg, "fused open-loop voltage adjustment failed, rc=%d\n",
  671. rc);
  672. goto done;
  673. }
  674. allow_interpolation = of_property_read_bool(node,
  675. "qcom,allow-voltage-interpolation");
  676. /*
  677. * No LowSVS open-loop voltage fuse exists. Instead, intermediate
  678. * voltages are interpolated between MinSVS and SVS. Set the LowSVS
  679. * voltage to be equal to the adjusted SVS voltage in order to avoid
  680. * triggering an incorrect condition violation in the following loop.
  681. */
  682. fuse_volt[CPR3_MSM8996_HMSS_FUSE_CORNER_LOWSVS]
  683. = fuse_volt[CPR3_MSM8996_HMSS_FUSE_CORNER_SVS];
  684. for (i = 1; i < vreg->fuse_corner_count; i++) {
  685. if (fuse_volt[i] < fuse_volt[i - 1]) {
  686. cpr3_debug(vreg, "fuse corner %d voltage=%d uV < fuse corner %d voltage=%d uV; overriding: fuse corner %d voltage=%d\n",
  687. i, fuse_volt[i], i - 1, fuse_volt[i - 1],
  688. i, fuse_volt[i - 1]);
  689. fuse_volt[i] = fuse_volt[i - 1];
  690. }
  691. }
  692. if (fuse->limitation == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION)
  693. allow_interpolation = false;
  694. if (!allow_interpolation) {
  695. /* Use fused open-loop voltage for lower frequencies. */
  696. for (i = 0; i < vreg->corner_count; i++)
  697. vreg->corner[i].open_loop_volt
  698. = fuse_volt[vreg->corner[i].cpr_fuse_corner];
  699. goto done;
  700. }
  701. for (i = 0; i < vreg->fuse_corner_count; i++)
  702. fmax_corner[i] = vreg->fuse_corner_map[i];
  703. /*
  704. * Interpolation is not possible for corners mapped to the lowest fuse
  705. * corner so use the fuse corner value directly.
  706. */
  707. for (i = 0; i <= fmax_corner[0]; i++)
  708. vreg->corner[i].open_loop_volt = fuse_volt[0];
  709. /*
  710. * Interpolation is not possible for corners mapped above the highest
  711. * fuse corner so use the fuse corner value directly.
  712. */
  713. j = vreg->fuse_corner_count - 1;
  714. for (i = fmax_corner[j] + 1; i < vreg->corner_count; i++)
  715. vreg->corner[i].open_loop_volt = fuse_volt[j];
  716. /*
  717. * Corner LowSVS should be skipped for voltage interpolation
  718. * since no fuse exists for it. Instead, the lowest interpolation
  719. * should be between MinSVS and SVS.
  720. */
  721. for (i = CPR3_MSM8996_HMSS_FUSE_CORNER_LOWSVS;
  722. i < vreg->fuse_corner_count - 1; i++) {
  723. fmax_corner[i] = fmax_corner[i + 1];
  724. fuse_volt[i] = fuse_volt[i + 1];
  725. }
  726. /* Interpolate voltages for the higher fuse corners. */
  727. for (i = 1; i < vreg->fuse_corner_count - 1; i++) {
  728. freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
  729. volt_low = fuse_volt[i - 1];
  730. freq_high = vreg->corner[fmax_corner[i]].proc_freq;
  731. volt_high = fuse_volt[i];
  732. for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
  733. vreg->corner[j].open_loop_volt = cpr3_interpolate(
  734. freq_low, volt_low, freq_high, volt_high,
  735. vreg->corner[j].proc_freq);
  736. }
  737. done:
  738. if (rc == 0) {
  739. cpr3_debug(vreg, "unadjusted per-corner open-loop voltages:\n");
  740. for (i = 0; i < vreg->corner_count; i++)
  741. cpr3_debug(vreg, "open-loop[%2d] = %d uV\n", i,
  742. vreg->corner[i].open_loop_volt);
  743. rc = cpr3_adjust_open_loop_voltages(vreg);
  744. if (rc)
  745. cpr3_err(vreg, "open-loop voltage adjustment failed, rc=%d\n",
  746. rc);
  747. }
  748. kfree(fuse_volt);
  749. kfree(fmax_corner);
  750. return rc;
  751. }
  752. /**
  753. * cpr3_msm8996_hmss_set_no_interpolation_quotients() - use the fused target
  754. * quotient values for lower frequencies.
  755. * @vreg: Pointer to the CPR3 regulator
  756. * @volt_adjust: Pointer to array of per-corner closed-loop adjustment
  757. * voltages
  758. * @volt_adjust_fuse: Pointer to array of per-fuse-corner closed-loop
  759. * adjustment voltages
  760. * @ro_scale: Pointer to array of per-fuse-corner RO scaling factor
  761. * values with units of QUOT/V
  762. *
  763. * Return: 0 on success, errno on failure
  764. */
  765. static int cpr3_msm8996_hmss_set_no_interpolation_quotients(
  766. struct cpr3_regulator *vreg, int *volt_adjust,
  767. int *volt_adjust_fuse, int *ro_scale)
  768. {
  769. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  770. u32 quot, ro;
  771. int quot_adjust;
  772. int i, fuse_corner;
  773. for (i = 0; i < vreg->corner_count; i++) {
  774. fuse_corner = vreg->corner[i].cpr_fuse_corner;
  775. quot = fuse->target_quot[fuse_corner];
  776. quot_adjust = cpr3_quot_adjustment(ro_scale[fuse_corner],
  777. volt_adjust_fuse[fuse_corner] + volt_adjust[i]);
  778. ro = fuse->ro_sel[fuse_corner];
  779. vreg->corner[i].target_quot[ro] = quot + quot_adjust;
  780. if (quot_adjust)
  781. cpr3_debug(vreg, "adjusted corner %d RO%u target quot: %u --> %u (%d uV)\n",
  782. i, ro, quot, vreg->corner[i].target_quot[ro],
  783. volt_adjust_fuse[fuse_corner] + volt_adjust[i]);
  784. }
  785. return 0;
  786. }
  787. /**
  788. * cpr3_msm8996_hmss_calculate_target_quotients() - calculate the CPR target
  789. * quotient for each corner of a CPR3 regulator
  790. * @vreg: Pointer to the CPR3 regulator
  791. *
  792. * If target quotient interpolation is allowed in both device tree and in
  793. * hardware fuses, then this function calculates the target quotient for a
  794. * given corner using linear interpolation. This interpolation is performed
  795. * using the processor frequencies of the lower and higher Fmax corners along
  796. * with the fused target quotient and quotient offset of the higher Fmax corner.
  797. *
  798. * If target quotient interpolation is not allowed, then this function uses
  799. * the Fmax fused target quotient for all of the corners associated with a
  800. * given fuse corner.
  801. *
  802. * Return: 0 on success, errno on failure
  803. */
  804. static int cpr3_msm8996_hmss_calculate_target_quotients(
  805. struct cpr3_regulator *vreg)
  806. {
  807. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  808. int rc;
  809. bool allow_interpolation;
  810. u64 freq_low, freq_high, prev_quot;
  811. u64 *quot_low;
  812. u64 *quot_high;
  813. u32 quot, ro;
  814. int i, j, fuse_corner, quot_adjust;
  815. int *fmax_corner;
  816. int *volt_adjust, *volt_adjust_fuse, *ro_scale;
  817. /* Log fused quotient values for debugging purposes. */
  818. cpr3_info(vreg, "fused MinSVS: quot[%2llu]=%4llu\n",
  819. fuse->ro_sel[CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS],
  820. fuse->target_quot[CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS]);
  821. for (i = CPR3_MSM8996_HMSS_FUSE_CORNER_SVS;
  822. i <= CPR3_MSM8996_HMSS_FUSE_CORNER_TURBO; i++)
  823. cpr3_info(vreg, "fused %6s: quot[%2llu]=%4llu, quot_offset[%2llu]=%4llu\n",
  824. cpr3_msm8996_hmss_fuse_corner_name[i],
  825. fuse->ro_sel[i], fuse->target_quot[i], fuse->ro_sel[i],
  826. fuse->quot_offset[i] * MSM8996_HMSS_QUOT_OFFSET_SCALE);
  827. allow_interpolation = of_property_read_bool(vreg->of_node,
  828. "qcom,allow-quotient-interpolation");
  829. if (fuse->limitation == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION)
  830. allow_interpolation = false;
  831. volt_adjust = kcalloc(vreg->corner_count, sizeof(*volt_adjust),
  832. GFP_KERNEL);
  833. volt_adjust_fuse = kcalloc(vreg->fuse_corner_count,
  834. sizeof(*volt_adjust_fuse), GFP_KERNEL);
  835. ro_scale = kcalloc(vreg->fuse_corner_count, sizeof(*ro_scale),
  836. GFP_KERNEL);
  837. fmax_corner = kcalloc(vreg->fuse_corner_count, sizeof(*fmax_corner),
  838. GFP_KERNEL);
  839. quot_low = kcalloc(vreg->fuse_corner_count, sizeof(*quot_low),
  840. GFP_KERNEL);
  841. quot_high = kcalloc(vreg->fuse_corner_count, sizeof(*quot_high),
  842. GFP_KERNEL);
  843. if (!volt_adjust || !volt_adjust_fuse || !ro_scale ||
  844. !fmax_corner || !quot_low || !quot_high) {
  845. rc = -ENOMEM;
  846. goto done;
  847. }
  848. rc = cpr3_parse_closed_loop_voltage_adjustments(vreg, &fuse->ro_sel[0],
  849. volt_adjust, volt_adjust_fuse, ro_scale);
  850. if (rc) {
  851. cpr3_err(vreg, "could not load closed-loop voltage adjustments, rc=%d\n",
  852. rc);
  853. goto done;
  854. }
  855. rc = cpr3_hmss_apply_fused_voltage_offset(vreg, volt_adjust_fuse);
  856. if (rc) {
  857. cpr3_err(vreg, "could not apply CBF voltage offsets, rc=%d\n",
  858. rc);
  859. goto done;
  860. }
  861. if (!allow_interpolation) {
  862. /* Use fused target quotients for lower frequencies. */
  863. return cpr3_msm8996_hmss_set_no_interpolation_quotients(vreg,
  864. volt_adjust, volt_adjust_fuse, ro_scale);
  865. }
  866. for (i = 0; i < vreg->fuse_corner_count; i++)
  867. fmax_corner[i] = vreg->fuse_corner_map[i];
  868. /*
  869. * Interpolation is not possible for corners mapped to the lowest fuse
  870. * corner so use the fuse corner value directly.
  871. */
  872. i = CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS;
  873. quot_adjust = cpr3_quot_adjustment(ro_scale[i], volt_adjust_fuse[i]);
  874. quot = fuse->target_quot[i] + quot_adjust;
  875. quot_high[i] = quot;
  876. ro = fuse->ro_sel[i];
  877. if (quot_adjust)
  878. cpr3_debug(vreg, "adjusted fuse corner %d RO%u target quot: %llu --> %u (%d uV)\n",
  879. i, ro, fuse->target_quot[i], quot, volt_adjust_fuse[i]);
  880. for (i = 0; i <= fmax_corner[CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS]; i++)
  881. vreg->corner[i].target_quot[ro] = quot;
  882. /*
  883. * Interpolation is not possible for corners mapped above the highest
  884. * fuse corner so use the fuse corner value directly.
  885. */
  886. j = vreg->fuse_corner_count - 1;
  887. quot_adjust = cpr3_quot_adjustment(ro_scale[j], volt_adjust_fuse[j]);
  888. quot = fuse->target_quot[j] + quot_adjust;
  889. ro = fuse->ro_sel[j];
  890. for (i = fmax_corner[j] + 1; i < vreg->corner_count; i++)
  891. vreg->corner[i].target_quot[ro] = quot;
  892. /*
  893. * The LowSVS target quotient is defined as:
  894. * (SVS target quotient) - (the unpacked SVS quotient offset)
  895. * MinSVS, LowSVS, and SVS fuse corners all share the same RO so it is
  896. * possible to interpolate between their target quotient values.
  897. */
  898. i = CPR3_MSM8996_HMSS_FUSE_CORNER_LOWSVS;
  899. quot_high[i] = fuse->target_quot[CPR3_MSM8996_HMSS_FUSE_CORNER_SVS]
  900. - fuse->quot_offset[CPR3_MSM8996_HMSS_FUSE_CORNER_SVS]
  901. * MSM8996_HMSS_QUOT_OFFSET_SCALE;
  902. quot_low[i] = fuse->target_quot[CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS];
  903. if (quot_high[i] < quot_low[i]) {
  904. cpr3_info(vreg, "quot_lowsvs=%llu < quot_minsvs=%llu; overriding: quot_lowsvs=%llu\n",
  905. quot_high[i], quot_low[i], quot_low[i]);
  906. quot_high[i] = quot_low[i];
  907. }
  908. if (fuse->ro_sel[CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS]
  909. != fuse->ro_sel[CPR3_MSM8996_HMSS_FUSE_CORNER_SVS]) {
  910. cpr3_info(vreg, "MinSVS RO=%llu != SVS RO=%llu; disabling LowSVS interpolation\n",
  911. fuse->ro_sel[CPR3_MSM8996_HMSS_FUSE_CORNER_MINSVS],
  912. fuse->ro_sel[CPR3_MSM8996_HMSS_FUSE_CORNER_SVS]);
  913. quot_low[i] = quot_high[i];
  914. }
  915. for (i = CPR3_MSM8996_HMSS_FUSE_CORNER_SVS;
  916. i < vreg->fuse_corner_count; i++) {
  917. quot_high[i] = fuse->target_quot[i];
  918. if (fuse->ro_sel[i] == fuse->ro_sel[i - 1])
  919. quot_low[i] = quot_high[i - 1];
  920. else
  921. quot_low[i] = quot_high[i]
  922. - fuse->quot_offset[i]
  923. * MSM8996_HMSS_QUOT_OFFSET_SCALE;
  924. if (quot_high[i] < quot_low[i]) {
  925. cpr3_debug(vreg, "quot_high[%d]=%llu < quot_low[%d]=%llu; overriding: quot_high[%d]=%llu\n",
  926. i, quot_high[i], i, quot_low[i],
  927. i, quot_low[i]);
  928. quot_high[i] = quot_low[i];
  929. }
  930. }
  931. /* Perform per-fuse-corner target quotient adjustment */
  932. for (i = 1; i < vreg->fuse_corner_count; i++) {
  933. quot_adjust = cpr3_quot_adjustment(ro_scale[i],
  934. volt_adjust_fuse[i]);
  935. if (quot_adjust) {
  936. prev_quot = quot_high[i];
  937. quot_high[i] += quot_adjust;
  938. cpr3_debug(vreg, "adjusted fuse corner %d RO%llu target quot: %llu --> %llu (%d uV)\n",
  939. i, fuse->ro_sel[i], prev_quot, quot_high[i],
  940. volt_adjust_fuse[i]);
  941. }
  942. if (fuse->ro_sel[i] == fuse->ro_sel[i - 1])
  943. quot_low[i] = quot_high[i - 1];
  944. else
  945. quot_low[i] += cpr3_quot_adjustment(ro_scale[i],
  946. volt_adjust_fuse[i - 1]);
  947. if (quot_high[i] < quot_low[i]) {
  948. cpr3_debug(vreg, "quot_high[%d]=%llu < quot_low[%d]=%llu after adjustment; overriding: quot_high[%d]=%llu\n",
  949. i, quot_high[i], i, quot_low[i],
  950. i, quot_low[i]);
  951. quot_high[i] = quot_low[i];
  952. }
  953. }
  954. /* Interpolate voltages for the higher fuse corners. */
  955. for (i = 1; i < vreg->fuse_corner_count; i++) {
  956. freq_low = vreg->corner[fmax_corner[i - 1]].proc_freq;
  957. freq_high = vreg->corner[fmax_corner[i]].proc_freq;
  958. ro = fuse->ro_sel[i];
  959. for (j = fmax_corner[i - 1] + 1; j <= fmax_corner[i]; j++)
  960. vreg->corner[j].target_quot[ro] = cpr3_interpolate(
  961. freq_low, quot_low[i], freq_high, quot_high[i],
  962. vreg->corner[j].proc_freq);
  963. }
  964. /* Perform per-corner target quotient adjustment */
  965. for (i = 0; i < vreg->corner_count; i++) {
  966. fuse_corner = vreg->corner[i].cpr_fuse_corner;
  967. ro = fuse->ro_sel[fuse_corner];
  968. quot_adjust = cpr3_quot_adjustment(ro_scale[fuse_corner],
  969. volt_adjust[i]);
  970. if (quot_adjust) {
  971. prev_quot = vreg->corner[i].target_quot[ro];
  972. vreg->corner[i].target_quot[ro] += quot_adjust;
  973. cpr3_debug(vreg, "adjusted corner %d RO%u target quot: %llu --> %u (%d uV)\n",
  974. i, ro, prev_quot,
  975. vreg->corner[i].target_quot[ro],
  976. volt_adjust[i]);
  977. }
  978. }
  979. /* Ensure that target quotients increase monotonically */
  980. for (i = 1; i < vreg->corner_count; i++) {
  981. ro = fuse->ro_sel[vreg->corner[i].cpr_fuse_corner];
  982. if (fuse->ro_sel[vreg->corner[i - 1].cpr_fuse_corner] == ro
  983. && vreg->corner[i].target_quot[ro]
  984. < vreg->corner[i - 1].target_quot[ro]) {
  985. 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",
  986. i, ro, vreg->corner[i].target_quot[ro],
  987. i - 1, ro, vreg->corner[i - 1].target_quot[ro],
  988. i, ro, vreg->corner[i - 1].target_quot[ro]);
  989. vreg->corner[i].target_quot[ro]
  990. = vreg->corner[i - 1].target_quot[ro];
  991. }
  992. }
  993. done:
  994. kfree(volt_adjust);
  995. kfree(volt_adjust_fuse);
  996. kfree(ro_scale);
  997. kfree(fmax_corner);
  998. kfree(quot_low);
  999. kfree(quot_high);
  1000. return rc;
  1001. }
  1002. /**
  1003. * cpr3_msm8996_partial_binning_override() - override the voltage and quotient
  1004. * settings for low corners based upon the value of the partial
  1005. * binning fuse
  1006. * @vreg: Pointer to the CPR3 regulator
  1007. *
  1008. * Some parts are not able to operate at low voltages. The partial binning
  1009. * fuse specifies if a given part has such limitations.
  1010. *
  1011. * Return: 0 on success, errno on failure
  1012. */
  1013. static int cpr3_msm8996_partial_binning_override(struct cpr3_regulator *vreg)
  1014. {
  1015. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  1016. int i, fuse_corner, fmax_corner;
  1017. if (fuse->partial_binning == MSM8996_CPR_PARTIAL_BINNING_SVS)
  1018. fuse_corner = CPR3_MSM8996_HMSS_FUSE_CORNER_SVS;
  1019. else if (fuse->partial_binning == MSM8996_CPR_PARTIAL_BINNING_NOM)
  1020. fuse_corner = CPR3_MSM8996_HMSS_FUSE_CORNER_NOM;
  1021. else
  1022. return 0;
  1023. cpr3_info(vreg, "overriding voltages and quotients for all corners below %s Fmax\n",
  1024. cpr3_msm8996_hmss_fuse_corner_name[fuse_corner]);
  1025. fmax_corner = -1;
  1026. for (i = vreg->corner_count - 1; i >= 0; i--) {
  1027. if (vreg->corner[i].cpr_fuse_corner == fuse_corner) {
  1028. fmax_corner = i;
  1029. break;
  1030. }
  1031. }
  1032. if (fmax_corner < 0) {
  1033. cpr3_err(vreg, "could not find %s Fmax corner\n",
  1034. cpr3_msm8996_hmss_fuse_corner_name[fuse_corner]);
  1035. return -EINVAL;
  1036. }
  1037. for (i = 0; i < fmax_corner; i++)
  1038. vreg->corner[i] = vreg->corner[fmax_corner];
  1039. return 0;
  1040. }
  1041. /**
  1042. * cpr3_hmss_print_settings() - print out HMSS CPR configuration settings into
  1043. * the kernel log for debugging purposes
  1044. * @vreg: Pointer to the CPR3 regulator
  1045. */
  1046. static void cpr3_hmss_print_settings(struct cpr3_regulator *vreg)
  1047. {
  1048. struct cpr3_corner *corner;
  1049. int i;
  1050. cpr3_debug(vreg, "Corner: Frequency (Hz), Fuse Corner, Floor (uV), Open-Loop (uV), Ceiling (uV)\n");
  1051. for (i = 0; i < vreg->corner_count; i++) {
  1052. corner = &vreg->corner[i];
  1053. cpr3_debug(vreg, "%3d: %10u, %2d, %7d, %7d, %7d\n",
  1054. i, corner->proc_freq, corner->cpr_fuse_corner,
  1055. corner->floor_volt, corner->open_loop_volt,
  1056. corner->ceiling_volt);
  1057. }
  1058. if (vreg->thread->ctrl->apm)
  1059. cpr3_debug(vreg, "APM threshold = %d uV, APM adjust = %d uV\n",
  1060. vreg->thread->ctrl->apm_threshold_volt,
  1061. vreg->thread->ctrl->apm_adj_volt);
  1062. }
  1063. /**
  1064. * cpr3_hmss_init_thread() - perform steps necessary to initialize the
  1065. * configuration data for a CPR3 thread
  1066. * @thread: Pointer to the CPR3 thread
  1067. *
  1068. * Return: 0 on success, errno on failure
  1069. */
  1070. static int cpr3_hmss_init_thread(struct cpr3_thread *thread)
  1071. {
  1072. int rc;
  1073. rc = cpr3_parse_common_thread_data(thread);
  1074. if (rc) {
  1075. cpr3_err(thread->ctrl, "thread %u unable to read CPR thread data from device tree, rc=%d\n",
  1076. thread->thread_id, rc);
  1077. return rc;
  1078. }
  1079. return 0;
  1080. }
  1081. #define MAX_VREG_NAME_SIZE 25
  1082. /**
  1083. * cpr3_hmss_kvreg_init() - initialize HMSS Kryo Regulator data for a CPR3
  1084. * regulator
  1085. * @vreg: Pointer to the CPR3 regulator
  1086. *
  1087. * This function loads Kryo Regulator data from device tree if it is present
  1088. * and requests a handle to the appropriate Kryo regulator device. In addition,
  1089. * it initializes Kryo Regulator data originating from hardware fuses, such as
  1090. * the LDO retention voltage, and requests the Kryo retention regulator to
  1091. * be configured to that value.
  1092. *
  1093. * Return: 0 on success, errno on failure
  1094. */
  1095. static int cpr3_hmss_kvreg_init(struct cpr3_regulator *vreg)
  1096. {
  1097. struct cpr3_msm8996_hmss_fuses *fuse = vreg->platform_fuses;
  1098. struct device_node *node = vreg->of_node;
  1099. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  1100. int id = vreg->thread->thread_id;
  1101. char kvreg_name_buf[MAX_VREG_NAME_SIZE];
  1102. int rc;
  1103. scnprintf(kvreg_name_buf, MAX_VREG_NAME_SIZE,
  1104. "vdd-thread%d-ldo-supply", id);
  1105. if (!of_find_property(ctrl->dev->of_node, kvreg_name_buf, NULL))
  1106. return 0;
  1107. else if (!of_find_property(node, "qcom,ldo-min-headroom-voltage", NULL))
  1108. return 0;
  1109. scnprintf(kvreg_name_buf, MAX_VREG_NAME_SIZE, "vdd-thread%d-ldo", id);
  1110. vreg->ldo_regulator = devm_regulator_get(ctrl->dev, kvreg_name_buf);
  1111. if (IS_ERR(vreg->ldo_regulator)) {
  1112. rc = PTR_ERR(vreg->ldo_regulator);
  1113. if (rc != -EPROBE_DEFER)
  1114. cpr3_err(vreg, "unable to request %s regulator, rc=%d\n",
  1115. kvreg_name_buf, rc);
  1116. return rc;
  1117. }
  1118. vreg->ldo_regulator_bypass = BHS_MODE;
  1119. scnprintf(kvreg_name_buf, MAX_VREG_NAME_SIZE, "vdd-thread%d-ldo-ret",
  1120. id);
  1121. vreg->ldo_ret_regulator = devm_regulator_get(ctrl->dev, kvreg_name_buf);
  1122. if (IS_ERR(vreg->ldo_ret_regulator)) {
  1123. rc = PTR_ERR(vreg->ldo_ret_regulator);
  1124. if (rc != -EPROBE_DEFER)
  1125. cpr3_err(vreg, "unable to request %s regulator, rc=%d\n",
  1126. kvreg_name_buf, rc);
  1127. return rc;
  1128. }
  1129. if (!ctrl->system_supply_max_volt) {
  1130. cpr3_err(ctrl, "system-supply max voltage must be specified\n");
  1131. return -EINVAL;
  1132. }
  1133. rc = of_property_read_u32(node, "qcom,ldo-min-headroom-voltage",
  1134. &vreg->ldo_min_headroom_volt);
  1135. if (rc) {
  1136. cpr3_err(vreg, "error reading qcom,ldo-min-headroom-voltage, rc=%d\n",
  1137. rc);
  1138. return rc;
  1139. }
  1140. rc = of_property_read_u32(node, "qcom,ldo-max-headroom-voltage",
  1141. &vreg->ldo_max_headroom_volt);
  1142. if (rc) {
  1143. cpr3_err(vreg, "error reading qcom,ldo-max-headroom-voltage, rc=%d\n",
  1144. rc);
  1145. return rc;
  1146. }
  1147. rc = of_property_read_u32(node, "qcom,ldo-max-voltage",
  1148. &vreg->ldo_max_volt);
  1149. if (rc) {
  1150. cpr3_err(vreg, "error reading qcom,ldo-max-voltage, rc=%d\n",
  1151. rc);
  1152. return rc;
  1153. }
  1154. /* Determine the CPU retention voltage based on fused data */
  1155. vreg->ldo_ret_volt =
  1156. max(msm8996_vdd_apcc_fuse_ret_volt[fuse->vdd_apcc_ret_fuse],
  1157. msm8996_vdd_mx_fuse_ret_volt[fuse->vdd_mx_ret_fuse]);
  1158. rc = regulator_set_voltage(vreg->ldo_ret_regulator, vreg->ldo_ret_volt,
  1159. INT_MAX);
  1160. if (rc < 0) {
  1161. cpr3_err(vreg, "regulator_set_voltage(ldo_ret) == %d failed, rc=%d\n",
  1162. vreg->ldo_ret_volt, rc);
  1163. return rc;
  1164. }
  1165. /* optional properties, do not error out if missing */
  1166. of_property_read_u32(node, "qcom,ldo-adjust-voltage",
  1167. &vreg->ldo_adjust_volt);
  1168. vreg->ldo_mode_allowed = !of_property_read_bool(node,
  1169. "qcom,ldo-disable");
  1170. cpr3_info(vreg, "LDO min headroom=%d uV, LDO max headroom=%d uV, LDO adj=%d uV, LDO mode=%s, LDO retention=%d uV\n",
  1171. vreg->ldo_min_headroom_volt,
  1172. vreg->ldo_max_headroom_volt,
  1173. vreg->ldo_adjust_volt,
  1174. vreg->ldo_mode_allowed ? "allowed" : "disallowed",
  1175. vreg->ldo_ret_volt);
  1176. return 0;
  1177. }
  1178. /**
  1179. * cpr3_hmss_mem_acc_init() - initialize mem-acc regulator data for
  1180. * a CPR3 regulator
  1181. * @vreg: Pointer to the CPR3 regulator
  1182. *
  1183. * This function loads mem-acc data from device tree to enable
  1184. * the control of mem-acc settings based upon the CPR3 regulator
  1185. * output voltage.
  1186. *
  1187. * Return: 0 on success, errno on failure
  1188. */
  1189. static int cpr3_hmss_mem_acc_init(struct cpr3_regulator *vreg)
  1190. {
  1191. struct cpr3_controller *ctrl = vreg->thread->ctrl;
  1192. int id = vreg->thread->thread_id;
  1193. char mem_acc_vreg_name_buf[MAX_VREG_NAME_SIZE];
  1194. int rc;
  1195. scnprintf(mem_acc_vreg_name_buf, MAX_VREG_NAME_SIZE,
  1196. "mem-acc-thread%d-supply", id);
  1197. if (!of_find_property(ctrl->dev->of_node, mem_acc_vreg_name_buf,
  1198. NULL)) {
  1199. cpr3_debug(vreg, "not using memory accelerator regulator\n");
  1200. return 0;
  1201. } else if (!of_property_read_bool(vreg->of_node, "qcom,uses-mem-acc")) {
  1202. return 0;
  1203. }
  1204. scnprintf(mem_acc_vreg_name_buf, MAX_VREG_NAME_SIZE,
  1205. "mem-acc-thread%d", id);
  1206. vreg->mem_acc_regulator = devm_regulator_get(ctrl->dev,
  1207. mem_acc_vreg_name_buf);
  1208. if (IS_ERR(vreg->mem_acc_regulator)) {
  1209. rc = PTR_ERR(vreg->mem_acc_regulator);
  1210. if (rc != -EPROBE_DEFER)
  1211. cpr3_err(vreg, "unable to request %s regulator, rc=%d\n",
  1212. mem_acc_vreg_name_buf, rc);
  1213. return rc;
  1214. }
  1215. return 0;
  1216. }
  1217. /**
  1218. * cpr3_hmss_init_regulator() - perform all steps necessary to initialize the
  1219. * configuration data for a CPR3 regulator
  1220. * @vreg: Pointer to the CPR3 regulator
  1221. *
  1222. * Return: 0 on success, errno on failure
  1223. */
  1224. static int cpr3_hmss_init_regulator(struct cpr3_regulator *vreg)
  1225. {
  1226. struct cpr3_msm8996_hmss_fuses *fuse;
  1227. int rc;
  1228. rc = cpr3_msm8996_hmss_read_fuse_data(vreg);
  1229. if (rc) {
  1230. cpr3_err(vreg, "unable to read CPR fuse data, rc=%d\n", rc);
  1231. return rc;
  1232. }
  1233. rc = cpr3_hmss_kvreg_init(vreg);
  1234. if (rc) {
  1235. if (rc != -EPROBE_DEFER)
  1236. cpr3_err(vreg, "unable to initialize Kryo Regulator settings, rc=%d\n",
  1237. rc);
  1238. return rc;
  1239. }
  1240. rc = cpr3_hmss_mem_acc_init(vreg);
  1241. if (rc) {
  1242. if (rc != -EPROBE_DEFER)
  1243. cpr3_err(vreg, "unable to initialize mem-acc regulator settings, rc=%d\n",
  1244. rc);
  1245. return rc;
  1246. }
  1247. fuse = vreg->platform_fuses;
  1248. if (fuse->limitation == MSM8996_CPR_LIMITATION_UNSUPPORTED) {
  1249. cpr3_err(vreg, "this chip requires an unsupported voltage\n");
  1250. return -EPERM;
  1251. } else if (fuse->limitation
  1252. == MSM8996_CPR_LIMITATION_NO_CPR_OR_INTERPOLATION) {
  1253. vreg->thread->ctrl->cpr_allowed_hw = false;
  1254. }
  1255. rc = of_property_read_u32(vreg->of_node, "qcom,cpr-pd-bypass-mask",
  1256. &vreg->pd_bypass_mask);
  1257. if (rc) {
  1258. cpr3_err(vreg, "error reading qcom,cpr-pd-bypass-mask, rc=%d\n",
  1259. rc);
  1260. return rc;
  1261. }
  1262. rc = cpr3_hmss_parse_corner_data(vreg);
  1263. if (rc) {
  1264. cpr3_err(vreg, "unable to read CPR corner data from device tree, rc=%d\n",
  1265. rc);
  1266. return rc;
  1267. }
  1268. if (of_find_property(vreg->of_node, "qcom,cpr-dynamic-floor-corner",
  1269. NULL)) {
  1270. rc = cpr3_parse_array_property(vreg,
  1271. "qcom,cpr-dynamic-floor-corner",
  1272. 1, &vreg->dynamic_floor_corner);
  1273. if (rc) {
  1274. cpr3_err(vreg, "error reading qcom,cpr-dynamic-floor-corner, rc=%d\n",
  1275. rc);
  1276. return rc;
  1277. }
  1278. if (vreg->dynamic_floor_corner <= 0) {
  1279. vreg->uses_dynamic_floor = false;
  1280. } else if (vreg->dynamic_floor_corner < CPR3_CORNER_OFFSET
  1281. || vreg->dynamic_floor_corner
  1282. > vreg->corner_count - 1 + CPR3_CORNER_OFFSET) {
  1283. cpr3_err(vreg, "dynamic floor corner=%d not in range [%d, %d]\n",
  1284. vreg->dynamic_floor_corner, CPR3_CORNER_OFFSET,
  1285. vreg->corner_count - 1 + CPR3_CORNER_OFFSET);
  1286. return -EINVAL;
  1287. }
  1288. vreg->dynamic_floor_corner -= CPR3_CORNER_OFFSET;
  1289. vreg->uses_dynamic_floor = true;
  1290. }
  1291. rc = cpr3_msm8996_hmss_calculate_open_loop_voltages(vreg);
  1292. if (rc) {
  1293. cpr3_err(vreg, "unable to calculate open-loop voltages, rc=%d\n",
  1294. rc);
  1295. return rc;
  1296. }
  1297. rc = cpr3_limit_open_loop_voltages(vreg);
  1298. if (rc) {
  1299. cpr3_err(vreg, "unable to limit open-loop voltages, rc=%d\n",
  1300. rc);
  1301. return rc;
  1302. }
  1303. cpr3_open_loop_voltage_as_ceiling(vreg);
  1304. rc = cpr3_limit_floor_voltages(vreg);
  1305. if (rc) {
  1306. cpr3_err(vreg, "unable to limit floor voltages, rc=%d\n", rc);
  1307. return rc;
  1308. }
  1309. rc = cpr3_msm8996_hmss_calculate_target_quotients(vreg);
  1310. if (rc) {
  1311. cpr3_err(vreg, "unable to calculate target quotients, rc=%d\n",
  1312. rc);
  1313. return rc;
  1314. }
  1315. rc = cpr3_msm8996_partial_binning_override(vreg);
  1316. if (rc) {
  1317. cpr3_err(vreg, "unable to override voltages and quotients based on partial binning fuse, rc=%d\n",
  1318. rc);
  1319. return rc;
  1320. }
  1321. cpr3_hmss_print_settings(vreg);
  1322. return 0;
  1323. }
  1324. /**
  1325. * cpr3_hmss_init_aging() - perform HMSS CPR3 controller specific
  1326. * aging initializations
  1327. * @ctrl: Pointer to the CPR3 controller
  1328. *
  1329. * Return: 0 on success, errno on failure
  1330. */
  1331. static int cpr3_hmss_init_aging(struct cpr3_controller *ctrl)
  1332. {
  1333. struct cpr3_msm8996_hmss_fuses *fuse = NULL;
  1334. struct cpr3_regulator *vreg;
  1335. u32 aging_ro_scale;
  1336. int i, j, rc;
  1337. for (i = 0; i < ctrl->thread_count; i++) {
  1338. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  1339. if (ctrl->thread[i].vreg[j].aging_allowed) {
  1340. ctrl->aging_required = true;
  1341. vreg = &ctrl->thread[i].vreg[j];
  1342. fuse = vreg->platform_fuses;
  1343. break;
  1344. }
  1345. }
  1346. }
  1347. if (!ctrl->aging_required || !fuse || !vreg)
  1348. return 0;
  1349. rc = cpr3_parse_array_property(vreg, "qcom,cpr-aging-ro-scaling-factor",
  1350. 1, &aging_ro_scale);
  1351. if (rc)
  1352. return rc;
  1353. if (aging_ro_scale == 0) {
  1354. cpr3_err(ctrl, "aging RO scaling factor is invalid: %u\n",
  1355. aging_ro_scale);
  1356. return -EINVAL;
  1357. }
  1358. ctrl->aging_vdd_mode = REGULATOR_MODE_NORMAL;
  1359. ctrl->aging_complete_vdd_mode = REGULATOR_MODE_IDLE;
  1360. ctrl->aging_sensor_count = 1;
  1361. ctrl->aging_sensor = kzalloc(sizeof(*ctrl->aging_sensor), GFP_KERNEL);
  1362. if (!ctrl->aging_sensor)
  1363. return -ENOMEM;
  1364. ctrl->aging_sensor->sensor_id = MSM8996_HMSS_AGING_SENSOR_ID;
  1365. ctrl->aging_sensor->bypass_mask[0] = MSM8996_HMSS_AGING_BYPASS_MASK0;
  1366. ctrl->aging_sensor->ro_scale = aging_ro_scale;
  1367. ctrl->aging_sensor->init_quot_diff
  1368. = cpr3_convert_open_loop_voltage_fuse(0,
  1369. MSM8996_HMSS_AGING_INIT_QUOT_DIFF_SCALE,
  1370. fuse->aging_init_quot_diff,
  1371. MSM8996_HMSS_AGING_INIT_QUOT_DIFF_SIZE);
  1372. cpr3_debug(ctrl, "sensor %u aging init quotient diff = %d, aging RO scale = %u QUOT/V\n",
  1373. ctrl->aging_sensor->sensor_id,
  1374. ctrl->aging_sensor->init_quot_diff,
  1375. ctrl->aging_sensor->ro_scale);
  1376. return 0;
  1377. }
  1378. /**
  1379. * cpr3_hmss_init_controller() - perform HMSS CPR3 controller specific
  1380. * initializations
  1381. * @ctrl: Pointer to the CPR3 controller
  1382. *
  1383. * Return: 0 on success, errno on failure
  1384. */
  1385. static int cpr3_hmss_init_controller(struct cpr3_controller *ctrl)
  1386. {
  1387. int i, rc;
  1388. rc = cpr3_parse_common_ctrl_data(ctrl);
  1389. if (rc) {
  1390. if (rc != -EPROBE_DEFER)
  1391. cpr3_err(ctrl, "unable to parse common controller data, rc=%d\n",
  1392. rc);
  1393. return rc;
  1394. }
  1395. ctrl->vdd_limit_regulator = devm_regulator_get(ctrl->dev, "vdd-limit");
  1396. if (IS_ERR(ctrl->vdd_limit_regulator)) {
  1397. rc = PTR_ERR(ctrl->vdd_limit_regulator);
  1398. if (rc != -EPROBE_DEFER)
  1399. cpr3_err(ctrl, "unable to request vdd-supply regulator, rc=%d\n",
  1400. rc);
  1401. return rc;
  1402. }
  1403. rc = of_property_read_u32(ctrl->dev->of_node,
  1404. "qcom,cpr-up-down-delay-time",
  1405. &ctrl->up_down_delay_time);
  1406. if (rc) {
  1407. cpr3_err(ctrl, "error reading property qcom,cpr-up-down-delay-time, rc=%d\n",
  1408. rc);
  1409. return rc;
  1410. }
  1411. /* No error check since this is an optional property. */
  1412. of_property_read_u32(ctrl->dev->of_node,
  1413. "qcom,system-supply-max-voltage",
  1414. &ctrl->system_supply_max_volt);
  1415. /* No error check since this is an optional property. */
  1416. of_property_read_u32(ctrl->dev->of_node, "qcom,cpr-clock-throttling",
  1417. &ctrl->proc_clock_throttle);
  1418. rc = cpr3_apm_init(ctrl);
  1419. if (rc) {
  1420. if (rc != -EPROBE_DEFER)
  1421. cpr3_err(ctrl, "unable to initialize APM settings, rc=%d\n",
  1422. rc);
  1423. return rc;
  1424. }
  1425. ctrl->sensor_count = MSM8996_HMSS_CPR_SENSOR_COUNT;
  1426. ctrl->sensor_owner = devm_kcalloc(ctrl->dev, ctrl->sensor_count,
  1427. sizeof(*ctrl->sensor_owner), GFP_KERNEL);
  1428. if (!ctrl->sensor_owner)
  1429. return -ENOMEM;
  1430. /* Specify sensor ownership */
  1431. for (i = MSM8996_HMSS_THREAD0_SENSOR_MIN;
  1432. i <= MSM8996_HMSS_THREAD0_SENSOR_MAX; i++)
  1433. ctrl->sensor_owner[i] = 0;
  1434. for (i = MSM8996_HMSS_THREAD1_SENSOR_MIN;
  1435. i <= MSM8996_HMSS_THREAD1_SENSOR_MAX; i++)
  1436. ctrl->sensor_owner[i] = 1;
  1437. ctrl->cpr_clock_rate = MSM8996_HMSS_CPR_CLOCK_RATE;
  1438. ctrl->ctrl_type = CPR_CTRL_TYPE_CPR3;
  1439. ctrl->supports_hw_closed_loop = true;
  1440. ctrl->use_hw_closed_loop = of_property_read_bool(ctrl->dev->of_node,
  1441. "qcom,cpr-hw-closed-loop");
  1442. if (ctrl->mem_acc_regulator) {
  1443. rc = of_property_read_u32(ctrl->dev->of_node,
  1444. "qcom,mem-acc-supply-threshold-voltage",
  1445. &ctrl->mem_acc_threshold_volt);
  1446. if (rc) {
  1447. cpr3_err(ctrl, "error reading property qcom,mem-acc-supply-threshold-voltage, rc=%d\n",
  1448. rc);
  1449. return rc;
  1450. }
  1451. ctrl->mem_acc_threshold_volt =
  1452. CPR3_ROUND(ctrl->mem_acc_threshold_volt,
  1453. ctrl->step_volt);
  1454. rc = of_property_read_u32_array(ctrl->dev->of_node,
  1455. "qcom,mem-acc-supply-corner-map",
  1456. &ctrl->mem_acc_corner_map[CPR3_MEM_ACC_LOW_CORNER],
  1457. CPR3_MEM_ACC_CORNERS);
  1458. if (rc) {
  1459. cpr3_err(ctrl, "error reading qcom,mem-acc-supply-corner-map, rc=%d\n",
  1460. rc);
  1461. return rc;
  1462. }
  1463. }
  1464. return 0;
  1465. }
  1466. static int cpr3_hmss_regulator_suspend(struct platform_device *pdev,
  1467. pm_message_t state)
  1468. {
  1469. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  1470. return cpr3_regulator_suspend(ctrl);
  1471. }
  1472. static int cpr3_hmss_regulator_resume(struct platform_device *pdev)
  1473. {
  1474. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  1475. return cpr3_regulator_resume(ctrl);
  1476. }
  1477. /* Data corresponds to the SoC revision */
  1478. static const struct of_device_id cpr_regulator_match_table[] = {
  1479. {
  1480. .compatible = "qcom,cpr3-msm8996-v1-hmss-regulator",
  1481. .data = (void *)(uintptr_t)1
  1482. },
  1483. {
  1484. .compatible = "qcom,cpr3-msm8996-v2-hmss-regulator",
  1485. .data = (void *)(uintptr_t)2
  1486. },
  1487. {
  1488. .compatible = "qcom,cpr3-msm8996-v3-hmss-regulator",
  1489. .data = (void *)(uintptr_t)3
  1490. },
  1491. {
  1492. .compatible = "qcom,cpr3-msm8996-hmss-regulator",
  1493. .data = (void *)(uintptr_t)3
  1494. },
  1495. {
  1496. .compatible = "qcom,cpr3-msm8996pro-hmss-regulator",
  1497. .data = (void *)(uintptr_t)MSM8996PRO_SOC_ID,
  1498. },
  1499. {}
  1500. };
  1501. static int cpr3_hmss_regulator_probe(struct platform_device *pdev)
  1502. {
  1503. struct device *dev = &pdev->dev;
  1504. const struct of_device_id *match;
  1505. struct cpr3_controller *ctrl;
  1506. struct cpr3_regulator *vreg;
  1507. int i, j, rc;
  1508. if (!dev->of_node) {
  1509. dev_err(dev, "Device tree node is missing\n");
  1510. return -EINVAL;
  1511. }
  1512. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  1513. if (!ctrl)
  1514. return -ENOMEM;
  1515. ctrl->dev = dev;
  1516. /* Set to false later if anything precludes CPR operation. */
  1517. ctrl->cpr_allowed_hw = true;
  1518. rc = of_property_read_string(dev->of_node, "qcom,cpr-ctrl-name",
  1519. &ctrl->name);
  1520. if (rc) {
  1521. cpr3_err(ctrl, "unable to read qcom,cpr-ctrl-name, rc=%d\n",
  1522. rc);
  1523. return rc;
  1524. }
  1525. match = of_match_node(cpr_regulator_match_table, dev->of_node);
  1526. if (match)
  1527. ctrl->soc_revision = (uintptr_t)match->data;
  1528. else
  1529. cpr3_err(ctrl, "could not find compatible string match\n");
  1530. rc = cpr3_map_fuse_base(ctrl, pdev);
  1531. if (rc) {
  1532. cpr3_err(ctrl, "could not map fuse base address\n");
  1533. return rc;
  1534. }
  1535. rc = cpr3_allocate_threads(ctrl, MSM8996_HMSS_POWER_CLUSTER_THREAD_ID,
  1536. MSM8996_HMSS_PERFORMANCE_CLUSTER_THREAD_ID);
  1537. if (rc) {
  1538. cpr3_err(ctrl, "failed to allocate CPR thread array, rc=%d\n",
  1539. rc);
  1540. return rc;
  1541. }
  1542. if (ctrl->thread_count < 1) {
  1543. cpr3_err(ctrl, "thread nodes are missing\n");
  1544. return -EINVAL;
  1545. }
  1546. rc = cpr3_hmss_init_controller(ctrl);
  1547. if (rc) {
  1548. if (rc != -EPROBE_DEFER)
  1549. cpr3_err(ctrl, "failed to initialize CPR controller parameters, rc=%d\n",
  1550. rc);
  1551. return rc;
  1552. }
  1553. for (i = 0; i < ctrl->thread_count; i++) {
  1554. rc = cpr3_hmss_init_thread(&ctrl->thread[i]);
  1555. if (rc) {
  1556. cpr3_err(ctrl, "thread %u initialization failed, rc=%d\n",
  1557. ctrl->thread[i].thread_id, rc);
  1558. return rc;
  1559. }
  1560. for (j = 0; j < ctrl->thread[i].vreg_count; j++) {
  1561. vreg = &ctrl->thread[i].vreg[j];
  1562. rc = cpr3_hmss_init_regulator(vreg);
  1563. if (rc) {
  1564. cpr3_err(vreg, "regulator initialization failed, rc=%d\n",
  1565. rc);
  1566. return rc;
  1567. }
  1568. }
  1569. }
  1570. rc = cpr3_hmss_init_aging(ctrl);
  1571. if (rc) {
  1572. cpr3_err(ctrl, "failed to initialize aging configurations, rc=%d\n",
  1573. rc);
  1574. return rc;
  1575. }
  1576. platform_set_drvdata(pdev, ctrl);
  1577. return cpr3_regulator_register(pdev, ctrl);
  1578. }
  1579. static int cpr3_hmss_regulator_remove(struct platform_device *pdev)
  1580. {
  1581. struct cpr3_controller *ctrl = platform_get_drvdata(pdev);
  1582. return cpr3_regulator_unregister(ctrl);
  1583. }
  1584. static struct platform_driver cpr3_hmss_regulator_driver = {
  1585. .driver = {
  1586. .name = "qcom,cpr3-hmss-regulator",
  1587. .of_match_table = cpr_regulator_match_table,
  1588. .owner = THIS_MODULE,
  1589. },
  1590. .probe = cpr3_hmss_regulator_probe,
  1591. .remove = cpr3_hmss_regulator_remove,
  1592. .suspend = cpr3_hmss_regulator_suspend,
  1593. .resume = cpr3_hmss_regulator_resume,
  1594. };
  1595. static int cpr_regulator_init(void)
  1596. {
  1597. return platform_driver_register(&cpr3_hmss_regulator_driver);
  1598. }
  1599. static void cpr_regulator_exit(void)
  1600. {
  1601. platform_driver_unregister(&cpr3_hmss_regulator_driver);
  1602. }
  1603. MODULE_DESCRIPTION("CPR3 HMSS regulator driver");
  1604. MODULE_LICENSE("GPL v2");
  1605. arch_initcall(cpr_regulator_init);
  1606. module_exit(cpr_regulator_exit);