mtk-svs.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 MediaTek Inc.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bits.h>
  7. #include <linux/clk.h>
  8. #include <linux/completion.h>
  9. #include <linux/cpuidle.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/device.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kthread.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/nvmem-consumer.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_domain.h>
  24. #include <linux/pm_opp.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/reset.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/slab.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/thermal.h>
  32. /* svs bank 1-line software id */
  33. #define SVSB_CPU_LITTLE BIT(0)
  34. #define SVSB_CPU_BIG BIT(1)
  35. #define SVSB_CCI BIT(2)
  36. #define SVSB_GPU BIT(3)
  37. /* svs bank 2-line type */
  38. #define SVSB_LOW BIT(8)
  39. #define SVSB_HIGH BIT(9)
  40. /* svs bank mode support */
  41. #define SVSB_MODE_ALL_DISABLE 0
  42. #define SVSB_MODE_INIT01 BIT(1)
  43. #define SVSB_MODE_INIT02 BIT(2)
  44. #define SVSB_MODE_MON BIT(3)
  45. /* svs bank volt flags */
  46. #define SVSB_INIT01_PD_REQ BIT(0)
  47. #define SVSB_INIT01_VOLT_IGNORE BIT(1)
  48. #define SVSB_INIT01_VOLT_INC_ONLY BIT(2)
  49. #define SVSB_MON_VOLT_IGNORE BIT(16)
  50. #define SVSB_REMOVE_DVTFIXED_VOLT BIT(24)
  51. /* svs bank register fields and common configuration */
  52. #define SVSB_PTPCONFIG_DETMAX GENMASK(15, 0)
  53. #define SVSB_DET_MAX FIELD_PREP(SVSB_PTPCONFIG_DETMAX, 0xffff)
  54. #define SVSB_DET_WINDOW 0xa28
  55. /* DESCHAR */
  56. #define SVSB_DESCHAR_FLD_MDES GENMASK(7, 0)
  57. #define SVSB_DESCHAR_FLD_BDES GENMASK(15, 8)
  58. /* TEMPCHAR */
  59. #define SVSB_TEMPCHAR_FLD_DVT_FIXED GENMASK(7, 0)
  60. #define SVSB_TEMPCHAR_FLD_MTDES GENMASK(15, 8)
  61. #define SVSB_TEMPCHAR_FLD_VCO GENMASK(23, 16)
  62. /* DETCHAR */
  63. #define SVSB_DETCHAR_FLD_DCMDET GENMASK(7, 0)
  64. #define SVSB_DETCHAR_FLD_DCBDET GENMASK(15, 8)
  65. /* SVSEN (PTPEN) */
  66. #define SVSB_PTPEN_INIT01 BIT(0)
  67. #define SVSB_PTPEN_MON BIT(1)
  68. #define SVSB_PTPEN_INIT02 (SVSB_PTPEN_INIT01 | BIT(2))
  69. #define SVSB_PTPEN_OFF 0x0
  70. /* FREQPCTS */
  71. #define SVSB_FREQPCTS_FLD_PCT0_4 GENMASK(7, 0)
  72. #define SVSB_FREQPCTS_FLD_PCT1_5 GENMASK(15, 8)
  73. #define SVSB_FREQPCTS_FLD_PCT2_6 GENMASK(23, 16)
  74. #define SVSB_FREQPCTS_FLD_PCT3_7 GENMASK(31, 24)
  75. /* INTSTS */
  76. #define SVSB_INTSTS_VAL_CLEAN 0x00ffffff
  77. #define SVSB_INTSTS_F0_COMPLETE BIT(0)
  78. #define SVSB_INTSTS_FLD_MONVOP GENMASK(23, 16)
  79. #define SVSB_RUNCONFIG_DEFAULT 0x80000000
  80. /* LIMITVALS */
  81. #define SVSB_LIMITVALS_FLD_DTLO GENMASK(7, 0)
  82. #define SVSB_LIMITVALS_FLD_DTHI GENMASK(15, 8)
  83. #define SVSB_LIMITVALS_FLD_VMIN GENMASK(23, 16)
  84. #define SVSB_LIMITVALS_FLD_VMAX GENMASK(31, 24)
  85. #define SVSB_VAL_DTHI 0x1
  86. #define SVSB_VAL_DTLO 0xfe
  87. /* INTEN */
  88. #define SVSB_INTEN_F0EN BIT(0)
  89. #define SVSB_INTEN_DACK0UPEN BIT(8)
  90. #define SVSB_INTEN_DC0EN BIT(9)
  91. #define SVSB_INTEN_DC1EN BIT(10)
  92. #define SVSB_INTEN_DACK0LOEN BIT(11)
  93. #define SVSB_INTEN_INITPROD_OVF_EN BIT(12)
  94. #define SVSB_INTEN_INITSUM_OVF_EN BIT(14)
  95. #define SVSB_INTEN_MONVOPEN GENMASK(23, 16)
  96. #define SVSB_INTEN_INIT0x (SVSB_INTEN_F0EN | SVSB_INTEN_DACK0UPEN | \
  97. SVSB_INTEN_DC0EN | SVSB_INTEN_DC1EN | \
  98. SVSB_INTEN_DACK0LOEN | \
  99. SVSB_INTEN_INITPROD_OVF_EN | \
  100. SVSB_INTEN_INITSUM_OVF_EN)
  101. /* TSCALCS */
  102. #define SVSB_TSCALCS_FLD_MTS GENMASK(11, 0)
  103. #define SVSB_TSCALCS_FLD_BTS GENMASK(23, 12)
  104. /* INIT2VALS */
  105. #define SVSB_INIT2VALS_FLD_DCVOFFSETIN GENMASK(15, 0)
  106. #define SVSB_INIT2VALS_FLD_AGEVOFFSETIN GENMASK(31, 16)
  107. /* VOPS */
  108. #define SVSB_VOPS_FLD_VOP0_4 GENMASK(7, 0)
  109. #define SVSB_VOPS_FLD_VOP1_5 GENMASK(15, 8)
  110. #define SVSB_VOPS_FLD_VOP2_6 GENMASK(23, 16)
  111. #define SVSB_VOPS_FLD_VOP3_7 GENMASK(31, 24)
  112. /* svs bank related setting */
  113. #define BITS8 8
  114. #define MAX_OPP_ENTRIES 16
  115. #define REG_BYTES 4
  116. #define SVSB_DC_SIGNED_BIT BIT(15)
  117. #define SVSB_DET_CLK_EN BIT(31)
  118. #define SVSB_TEMP_LOWER_BOUND 0xb2
  119. #define SVSB_TEMP_UPPER_BOUND 0x64
  120. static DEFINE_SPINLOCK(svs_lock);
  121. #ifdef CONFIG_DEBUG_FS
  122. #define debug_fops_ro(name) \
  123. static int svs_##name##_debug_open(struct inode *inode, \
  124. struct file *filp) \
  125. { \
  126. return single_open(filp, svs_##name##_debug_show, \
  127. inode->i_private); \
  128. } \
  129. static const struct file_operations svs_##name##_debug_fops = { \
  130. .owner = THIS_MODULE, \
  131. .open = svs_##name##_debug_open, \
  132. .read = seq_read, \
  133. .llseek = seq_lseek, \
  134. .release = single_release, \
  135. }
  136. #define debug_fops_rw(name) \
  137. static int svs_##name##_debug_open(struct inode *inode, \
  138. struct file *filp) \
  139. { \
  140. return single_open(filp, svs_##name##_debug_show, \
  141. inode->i_private); \
  142. } \
  143. static const struct file_operations svs_##name##_debug_fops = { \
  144. .owner = THIS_MODULE, \
  145. .open = svs_##name##_debug_open, \
  146. .read = seq_read, \
  147. .write = svs_##name##_debug_write, \
  148. .llseek = seq_lseek, \
  149. .release = single_release, \
  150. }
  151. #define svs_dentry_data(name) {__stringify(name), &svs_##name##_debug_fops}
  152. #endif
  153. /**
  154. * enum svsb_phase - svs bank phase enumeration
  155. * @SVSB_PHASE_ERROR: svs bank encounters unexpected condition
  156. * @SVSB_PHASE_INIT01: svs bank basic init for data calibration
  157. * @SVSB_PHASE_INIT02: svs bank can provide voltages to opp table
  158. * @SVSB_PHASE_MON: svs bank can provide voltages with thermal effect
  159. * @SVSB_PHASE_MAX: total number of svs bank phase (debug purpose)
  160. *
  161. * Each svs bank has its own independent phase and we enable each svs bank by
  162. * running their phase orderly. However, when svs bank encounters unexpected
  163. * condition, it will fire an irq (PHASE_ERROR) to inform svs software.
  164. *
  165. * svs bank general phase-enabled order:
  166. * SVSB_PHASE_INIT01 -> SVSB_PHASE_INIT02 -> SVSB_PHASE_MON
  167. */
  168. enum svsb_phase {
  169. SVSB_PHASE_ERROR = 0,
  170. SVSB_PHASE_INIT01,
  171. SVSB_PHASE_INIT02,
  172. SVSB_PHASE_MON,
  173. SVSB_PHASE_MAX,
  174. };
  175. enum svs_reg_index {
  176. DESCHAR = 0,
  177. TEMPCHAR,
  178. DETCHAR,
  179. AGECHAR,
  180. DCCONFIG,
  181. AGECONFIG,
  182. FREQPCT30,
  183. FREQPCT74,
  184. LIMITVALS,
  185. VBOOT,
  186. DETWINDOW,
  187. CONFIG,
  188. TSCALCS,
  189. RUNCONFIG,
  190. SVSEN,
  191. INIT2VALS,
  192. DCVALUES,
  193. AGEVALUES,
  194. VOP30,
  195. VOP74,
  196. TEMP,
  197. INTSTS,
  198. INTSTSRAW,
  199. INTEN,
  200. CHKINT,
  201. CHKSHIFT,
  202. STATUS,
  203. VDESIGN30,
  204. VDESIGN74,
  205. DVT30,
  206. DVT74,
  207. AGECOUNT,
  208. SMSTATE0,
  209. SMSTATE1,
  210. CTL0,
  211. DESDETSEC,
  212. TEMPAGESEC,
  213. CTRLSPARE0,
  214. CTRLSPARE1,
  215. CTRLSPARE2,
  216. CTRLSPARE3,
  217. CORESEL,
  218. THERMINTST,
  219. INTST,
  220. THSTAGE0ST,
  221. THSTAGE1ST,
  222. THSTAGE2ST,
  223. THAHBST0,
  224. THAHBST1,
  225. SPARE0,
  226. SPARE1,
  227. SPARE2,
  228. SPARE3,
  229. THSLPEVEB,
  230. SVS_REG_MAX,
  231. };
  232. static const u32 svs_regs_v2[] = {
  233. [DESCHAR] = 0xc00,
  234. [TEMPCHAR] = 0xc04,
  235. [DETCHAR] = 0xc08,
  236. [AGECHAR] = 0xc0c,
  237. [DCCONFIG] = 0xc10,
  238. [AGECONFIG] = 0xc14,
  239. [FREQPCT30] = 0xc18,
  240. [FREQPCT74] = 0xc1c,
  241. [LIMITVALS] = 0xc20,
  242. [VBOOT] = 0xc24,
  243. [DETWINDOW] = 0xc28,
  244. [CONFIG] = 0xc2c,
  245. [TSCALCS] = 0xc30,
  246. [RUNCONFIG] = 0xc34,
  247. [SVSEN] = 0xc38,
  248. [INIT2VALS] = 0xc3c,
  249. [DCVALUES] = 0xc40,
  250. [AGEVALUES] = 0xc44,
  251. [VOP30] = 0xc48,
  252. [VOP74] = 0xc4c,
  253. [TEMP] = 0xc50,
  254. [INTSTS] = 0xc54,
  255. [INTSTSRAW] = 0xc58,
  256. [INTEN] = 0xc5c,
  257. [CHKINT] = 0xc60,
  258. [CHKSHIFT] = 0xc64,
  259. [STATUS] = 0xc68,
  260. [VDESIGN30] = 0xc6c,
  261. [VDESIGN74] = 0xc70,
  262. [DVT30] = 0xc74,
  263. [DVT74] = 0xc78,
  264. [AGECOUNT] = 0xc7c,
  265. [SMSTATE0] = 0xc80,
  266. [SMSTATE1] = 0xc84,
  267. [CTL0] = 0xc88,
  268. [DESDETSEC] = 0xce0,
  269. [TEMPAGESEC] = 0xce4,
  270. [CTRLSPARE0] = 0xcf0,
  271. [CTRLSPARE1] = 0xcf4,
  272. [CTRLSPARE2] = 0xcf8,
  273. [CTRLSPARE3] = 0xcfc,
  274. [CORESEL] = 0xf00,
  275. [THERMINTST] = 0xf04,
  276. [INTST] = 0xf08,
  277. [THSTAGE0ST] = 0xf0c,
  278. [THSTAGE1ST] = 0xf10,
  279. [THSTAGE2ST] = 0xf14,
  280. [THAHBST0] = 0xf18,
  281. [THAHBST1] = 0xf1c,
  282. [SPARE0] = 0xf20,
  283. [SPARE1] = 0xf24,
  284. [SPARE2] = 0xf28,
  285. [SPARE3] = 0xf2c,
  286. [THSLPEVEB] = 0xf30,
  287. };
  288. /**
  289. * struct svs_platform - svs platform control
  290. * @name: svs platform name
  291. * @base: svs platform register base
  292. * @dev: svs platform device
  293. * @main_clk: main clock for svs bank
  294. * @pbank: svs bank pointer needing to be protected by spin_lock section
  295. * @banks: svs banks that svs platform supports
  296. * @rst: svs platform reset control
  297. * @efuse_parsing: svs platform efuse parsing function pointer
  298. * @probe: svs platform probe function pointer
  299. * @efuse_max: total number of svs efuse
  300. * @tefuse_max: total number of thermal efuse
  301. * @regs: svs platform registers map
  302. * @bank_max: total number of svs banks
  303. * @efuse: svs efuse data received from NVMEM framework
  304. * @tefuse: thermal efuse data received from NVMEM framework
  305. */
  306. struct svs_platform {
  307. char *name;
  308. void __iomem *base;
  309. struct device *dev;
  310. struct clk *main_clk;
  311. struct svs_bank *pbank;
  312. struct svs_bank *banks;
  313. struct reset_control *rst;
  314. bool (*efuse_parsing)(struct svs_platform *svsp);
  315. int (*probe)(struct svs_platform *svsp);
  316. size_t efuse_max;
  317. size_t tefuse_max;
  318. const u32 *regs;
  319. u32 bank_max;
  320. u32 *efuse;
  321. u32 *tefuse;
  322. };
  323. struct svs_platform_data {
  324. char *name;
  325. struct svs_bank *banks;
  326. bool (*efuse_parsing)(struct svs_platform *svsp);
  327. int (*probe)(struct svs_platform *svsp);
  328. const u32 *regs;
  329. u32 bank_max;
  330. };
  331. /**
  332. * struct svs_bank - svs bank representation
  333. * @dev: bank device
  334. * @opp_dev: device for opp table/buck control
  335. * @init_completion: the timeout completion for bank init
  336. * @buck: regulator used by opp_dev
  337. * @tzd: thermal zone device for getting temperature
  338. * @lock: mutex lock to protect voltage update process
  339. * @set_freq_pct: function pointer to set bank frequency percent table
  340. * @get_volts: function pointer to get bank voltages
  341. * @name: bank name
  342. * @buck_name: regulator name
  343. * @tzone_name: thermal zone name
  344. * @phase: bank current phase
  345. * @volt_od: bank voltage overdrive
  346. * @reg_data: bank register data in different phase for debug purpose
  347. * @pm_runtime_enabled_count: bank pm runtime enabled count
  348. * @mode_support: bank mode support.
  349. * @freq_base: reference frequency for bank init
  350. * @turn_freq_base: refenrece frequency for 2-line turn point
  351. * @vboot: voltage request for bank init01 only
  352. * @opp_dfreq: default opp frequency table
  353. * @opp_dvolt: default opp voltage table
  354. * @freq_pct: frequency percent table for bank init
  355. * @volt: bank voltage table
  356. * @volt_step: bank voltage step
  357. * @volt_base: bank voltage base
  358. * @volt_flags: bank voltage flags
  359. * @vmax: bank voltage maximum
  360. * @vmin: bank voltage minimum
  361. * @age_config: bank age configuration
  362. * @age_voffset_in: bank age voltage offset
  363. * @dc_config: bank dc configuration
  364. * @dc_voffset_in: bank dc voltage offset
  365. * @dvt_fixed: bank dvt fixed value
  366. * @vco: bank VCO value
  367. * @chk_shift: bank chicken shift
  368. * @core_sel: bank selection
  369. * @opp_count: bank opp count
  370. * @int_st: bank interrupt identification
  371. * @sw_id: bank software identification
  372. * @cpu_id: cpu core id for SVS CPU bank use only
  373. * @ctl0: TS-x selection
  374. * @temp: bank temperature
  375. * @tzone_htemp: thermal zone high temperature threshold
  376. * @tzone_htemp_voffset: thermal zone high temperature voltage offset
  377. * @tzone_ltemp: thermal zone low temperature threshold
  378. * @tzone_ltemp_voffset: thermal zone low temperature voltage offset
  379. * @bts: svs efuse data
  380. * @mts: svs efuse data
  381. * @bdes: svs efuse data
  382. * @mdes: svs efuse data
  383. * @mtdes: svs efuse data
  384. * @dcbdet: svs efuse data
  385. * @dcmdet: svs efuse data
  386. * @turn_pt: 2-line turn point tells which opp_volt calculated by high/low bank
  387. * @type: bank type to represent it is 2-line (high/low) bank or 1-line bank
  388. *
  389. * Svs bank will generate suitalbe voltages by below general math equation
  390. * and provide these voltages to opp voltage table.
  391. *
  392. * opp_volt[i] = (volt[i] * volt_step) + volt_base;
  393. */
  394. struct svs_bank {
  395. struct device *dev;
  396. struct device *opp_dev;
  397. struct completion init_completion;
  398. struct regulator *buck;
  399. struct thermal_zone_device *tzd;
  400. struct mutex lock; /* lock to protect voltage update process */
  401. void (*set_freq_pct)(struct svs_platform *svsp);
  402. void (*get_volts)(struct svs_platform *svsp);
  403. char *name;
  404. char *buck_name;
  405. char *tzone_name;
  406. enum svsb_phase phase;
  407. s32 volt_od;
  408. u32 reg_data[SVSB_PHASE_MAX][SVS_REG_MAX];
  409. u32 pm_runtime_enabled_count;
  410. u32 mode_support;
  411. u32 freq_base;
  412. u32 turn_freq_base;
  413. u32 vboot;
  414. u32 opp_dfreq[MAX_OPP_ENTRIES];
  415. u32 opp_dvolt[MAX_OPP_ENTRIES];
  416. u32 freq_pct[MAX_OPP_ENTRIES];
  417. u32 volt[MAX_OPP_ENTRIES];
  418. u32 volt_step;
  419. u32 volt_base;
  420. u32 volt_flags;
  421. u32 vmax;
  422. u32 vmin;
  423. u32 age_config;
  424. u32 age_voffset_in;
  425. u32 dc_config;
  426. u32 dc_voffset_in;
  427. u32 dvt_fixed;
  428. u32 vco;
  429. u32 chk_shift;
  430. u32 core_sel;
  431. u32 opp_count;
  432. u32 int_st;
  433. u32 sw_id;
  434. u32 cpu_id;
  435. u32 ctl0;
  436. u32 temp;
  437. u32 tzone_htemp;
  438. u32 tzone_htemp_voffset;
  439. u32 tzone_ltemp;
  440. u32 tzone_ltemp_voffset;
  441. u32 bts;
  442. u32 mts;
  443. u32 bdes;
  444. u32 mdes;
  445. u32 mtdes;
  446. u32 dcbdet;
  447. u32 dcmdet;
  448. u32 turn_pt;
  449. u32 type;
  450. };
  451. static u32 percent(u32 numerator, u32 denominator)
  452. {
  453. /* If not divide 1000, "numerator * 100" will have data overflow. */
  454. numerator /= 1000;
  455. denominator /= 1000;
  456. return DIV_ROUND_UP(numerator * 100, denominator);
  457. }
  458. static u32 svs_readl_relaxed(struct svs_platform *svsp, enum svs_reg_index rg_i)
  459. {
  460. return readl_relaxed(svsp->base + svsp->regs[rg_i]);
  461. }
  462. static void svs_writel_relaxed(struct svs_platform *svsp, u32 val,
  463. enum svs_reg_index rg_i)
  464. {
  465. writel_relaxed(val, svsp->base + svsp->regs[rg_i]);
  466. }
  467. static void svs_switch_bank(struct svs_platform *svsp)
  468. {
  469. struct svs_bank *svsb = svsp->pbank;
  470. svs_writel_relaxed(svsp, svsb->core_sel, CORESEL);
  471. }
  472. static u32 svs_bank_volt_to_opp_volt(u32 svsb_volt, u32 svsb_volt_step,
  473. u32 svsb_volt_base)
  474. {
  475. return (svsb_volt * svsb_volt_step) + svsb_volt_base;
  476. }
  477. static u32 svs_opp_volt_to_bank_volt(u32 opp_u_volt, u32 svsb_volt_step,
  478. u32 svsb_volt_base)
  479. {
  480. return (opp_u_volt - svsb_volt_base) / svsb_volt_step;
  481. }
  482. static int svs_sync_bank_volts_from_opp(struct svs_bank *svsb)
  483. {
  484. struct dev_pm_opp *opp;
  485. u32 i, opp_u_volt;
  486. for (i = 0; i < svsb->opp_count; i++) {
  487. opp = dev_pm_opp_find_freq_exact(svsb->opp_dev,
  488. svsb->opp_dfreq[i],
  489. true);
  490. if (IS_ERR(opp)) {
  491. dev_err(svsb->dev, "cannot find freq = %u (%ld)\n",
  492. svsb->opp_dfreq[i], PTR_ERR(opp));
  493. return PTR_ERR(opp);
  494. }
  495. opp_u_volt = dev_pm_opp_get_voltage(opp);
  496. svsb->volt[i] = svs_opp_volt_to_bank_volt(opp_u_volt,
  497. svsb->volt_step,
  498. svsb->volt_base);
  499. dev_pm_opp_put(opp);
  500. }
  501. return 0;
  502. }
  503. static int svs_adjust_pm_opp_volts(struct svs_bank *svsb)
  504. {
  505. int ret = -EPERM, tzone_temp = 0;
  506. u32 i, svsb_volt, opp_volt, temp_voffset = 0, opp_start, opp_stop;
  507. mutex_lock(&svsb->lock);
  508. /*
  509. * 2-line bank updates its corresponding opp volts.
  510. * 1-line bank updates all opp volts.
  511. */
  512. if (svsb->type == SVSB_HIGH) {
  513. opp_start = 0;
  514. opp_stop = svsb->turn_pt;
  515. } else if (svsb->type == SVSB_LOW) {
  516. opp_start = svsb->turn_pt;
  517. opp_stop = svsb->opp_count;
  518. } else {
  519. opp_start = 0;
  520. opp_stop = svsb->opp_count;
  521. }
  522. /* Get thermal effect */
  523. if (svsb->phase == SVSB_PHASE_MON) {
  524. ret = thermal_zone_get_temp(svsb->tzd, &tzone_temp);
  525. if (ret || (svsb->temp > SVSB_TEMP_UPPER_BOUND &&
  526. svsb->temp < SVSB_TEMP_LOWER_BOUND)) {
  527. dev_err(svsb->dev, "%s: %d (0x%x), run default volts\n",
  528. svsb->tzone_name, ret, svsb->temp);
  529. svsb->phase = SVSB_PHASE_ERROR;
  530. }
  531. if (tzone_temp >= svsb->tzone_htemp)
  532. temp_voffset += svsb->tzone_htemp_voffset;
  533. else if (tzone_temp <= svsb->tzone_ltemp)
  534. temp_voffset += svsb->tzone_ltemp_voffset;
  535. /* 2-line bank update all opp volts when running mon mode */
  536. if (svsb->type == SVSB_HIGH || svsb->type == SVSB_LOW) {
  537. opp_start = 0;
  538. opp_stop = svsb->opp_count;
  539. }
  540. }
  541. /* vmin <= svsb_volt (opp_volt) <= default opp voltage */
  542. for (i = opp_start; i < opp_stop; i++) {
  543. switch (svsb->phase) {
  544. case SVSB_PHASE_ERROR:
  545. opp_volt = svsb->opp_dvolt[i];
  546. break;
  547. case SVSB_PHASE_INIT01:
  548. /* do nothing */
  549. goto unlock_mutex;
  550. case SVSB_PHASE_INIT02:
  551. svsb_volt = max(svsb->volt[i], svsb->vmin);
  552. opp_volt = svs_bank_volt_to_opp_volt(svsb_volt,
  553. svsb->volt_step,
  554. svsb->volt_base);
  555. break;
  556. case SVSB_PHASE_MON:
  557. svsb_volt = max(svsb->volt[i] + temp_voffset, svsb->vmin);
  558. opp_volt = svs_bank_volt_to_opp_volt(svsb_volt,
  559. svsb->volt_step,
  560. svsb->volt_base);
  561. break;
  562. default:
  563. dev_err(svsb->dev, "unknown phase: %u\n", svsb->phase);
  564. ret = -EINVAL;
  565. goto unlock_mutex;
  566. }
  567. opp_volt = min(opp_volt, svsb->opp_dvolt[i]);
  568. ret = dev_pm_opp_adjust_voltage(svsb->opp_dev,
  569. svsb->opp_dfreq[i],
  570. opp_volt, opp_volt,
  571. svsb->opp_dvolt[i]);
  572. if (ret) {
  573. dev_err(svsb->dev, "set %uuV fail: %d\n",
  574. opp_volt, ret);
  575. goto unlock_mutex;
  576. }
  577. }
  578. unlock_mutex:
  579. mutex_unlock(&svsb->lock);
  580. return ret;
  581. }
  582. #ifdef CONFIG_DEBUG_FS
  583. static int svs_dump_debug_show(struct seq_file *m, void *p)
  584. {
  585. struct svs_platform *svsp = (struct svs_platform *)m->private;
  586. struct svs_bank *svsb;
  587. unsigned long svs_reg_addr;
  588. u32 idx, i, j, bank_id;
  589. for (i = 0; i < svsp->efuse_max; i++)
  590. if (svsp->efuse && svsp->efuse[i])
  591. seq_printf(m, "M_HW_RES%d = 0x%08x\n",
  592. i, svsp->efuse[i]);
  593. for (i = 0; i < svsp->tefuse_max; i++)
  594. if (svsp->tefuse)
  595. seq_printf(m, "THERMAL_EFUSE%d = 0x%08x\n",
  596. i, svsp->tefuse[i]);
  597. for (bank_id = 0, idx = 0; idx < svsp->bank_max; idx++, bank_id++) {
  598. svsb = &svsp->banks[idx];
  599. for (i = SVSB_PHASE_INIT01; i <= SVSB_PHASE_MON; i++) {
  600. seq_printf(m, "Bank_number = %u\n", bank_id);
  601. if (i == SVSB_PHASE_INIT01 || i == SVSB_PHASE_INIT02)
  602. seq_printf(m, "mode = init%d\n", i);
  603. else if (i == SVSB_PHASE_MON)
  604. seq_puts(m, "mode = mon\n");
  605. else
  606. seq_puts(m, "mode = error\n");
  607. for (j = DESCHAR; j < SVS_REG_MAX; j++) {
  608. svs_reg_addr = (unsigned long)(svsp->base +
  609. svsp->regs[j]);
  610. seq_printf(m, "0x%08lx = 0x%08x\n",
  611. svs_reg_addr, svsb->reg_data[i][j]);
  612. }
  613. }
  614. }
  615. return 0;
  616. }
  617. debug_fops_ro(dump);
  618. static int svs_enable_debug_show(struct seq_file *m, void *v)
  619. {
  620. struct svs_bank *svsb = (struct svs_bank *)m->private;
  621. switch (svsb->phase) {
  622. case SVSB_PHASE_ERROR:
  623. seq_puts(m, "disabled\n");
  624. break;
  625. case SVSB_PHASE_INIT01:
  626. seq_puts(m, "init1\n");
  627. break;
  628. case SVSB_PHASE_INIT02:
  629. seq_puts(m, "init2\n");
  630. break;
  631. case SVSB_PHASE_MON:
  632. seq_puts(m, "mon mode\n");
  633. break;
  634. default:
  635. seq_puts(m, "unknown\n");
  636. break;
  637. }
  638. return 0;
  639. }
  640. static ssize_t svs_enable_debug_write(struct file *filp,
  641. const char __user *buffer,
  642. size_t count, loff_t *pos)
  643. {
  644. struct svs_bank *svsb = file_inode(filp)->i_private;
  645. struct svs_platform *svsp = dev_get_drvdata(svsb->dev);
  646. unsigned long flags;
  647. int enabled, ret;
  648. char *buf = NULL;
  649. if (count >= PAGE_SIZE)
  650. return -EINVAL;
  651. buf = (char *)memdup_user_nul(buffer, count);
  652. if (IS_ERR(buf))
  653. return PTR_ERR(buf);
  654. ret = kstrtoint(buf, 10, &enabled);
  655. if (ret)
  656. return ret;
  657. if (!enabled) {
  658. spin_lock_irqsave(&svs_lock, flags);
  659. svsp->pbank = svsb;
  660. svsb->mode_support = SVSB_MODE_ALL_DISABLE;
  661. svs_switch_bank(svsp);
  662. svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN);
  663. svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS);
  664. spin_unlock_irqrestore(&svs_lock, flags);
  665. svsb->phase = SVSB_PHASE_ERROR;
  666. svs_adjust_pm_opp_volts(svsb);
  667. }
  668. kfree(buf);
  669. return count;
  670. }
  671. debug_fops_rw(enable);
  672. static int svs_status_debug_show(struct seq_file *m, void *v)
  673. {
  674. struct svs_bank *svsb = (struct svs_bank *)m->private;
  675. struct dev_pm_opp *opp;
  676. int tzone_temp = 0, ret;
  677. u32 i;
  678. ret = thermal_zone_get_temp(svsb->tzd, &tzone_temp);
  679. if (ret)
  680. seq_printf(m, "%s: temperature ignore, turn_pt = %u\n",
  681. svsb->name, svsb->turn_pt);
  682. else
  683. seq_printf(m, "%s: temperature = %d, turn_pt = %u\n",
  684. svsb->name, tzone_temp, svsb->turn_pt);
  685. for (i = 0; i < svsb->opp_count; i++) {
  686. opp = dev_pm_opp_find_freq_exact(svsb->opp_dev,
  687. svsb->opp_dfreq[i], true);
  688. if (IS_ERR(opp)) {
  689. seq_printf(m, "%s: cannot find freq = %u (%ld)\n",
  690. svsb->name, svsb->opp_dfreq[i],
  691. PTR_ERR(opp));
  692. return PTR_ERR(opp);
  693. }
  694. seq_printf(m, "opp_freq[%02u]: %u, opp_volt[%02u]: %lu, ",
  695. i, svsb->opp_dfreq[i], i,
  696. dev_pm_opp_get_voltage(opp));
  697. seq_printf(m, "svsb_volt[%02u]: 0x%x, freq_pct[%02u]: %u\n",
  698. i, svsb->volt[i], i, svsb->freq_pct[i]);
  699. dev_pm_opp_put(opp);
  700. }
  701. return 0;
  702. }
  703. debug_fops_ro(status);
  704. static int svs_create_debug_cmds(struct svs_platform *svsp)
  705. {
  706. struct svs_bank *svsb;
  707. struct dentry *svs_dir, *svsb_dir, *file_entry;
  708. const char *d = "/sys/kernel/debug/svs";
  709. u32 i, idx;
  710. struct svs_dentry {
  711. const char *name;
  712. const struct file_operations *fops;
  713. };
  714. struct svs_dentry svs_entries[] = {
  715. svs_dentry_data(dump),
  716. };
  717. struct svs_dentry svsb_entries[] = {
  718. svs_dentry_data(enable),
  719. svs_dentry_data(status),
  720. };
  721. svs_dir = debugfs_create_dir("svs", NULL);
  722. if (IS_ERR(svs_dir)) {
  723. dev_err(svsp->dev, "cannot create %s: %ld\n",
  724. d, PTR_ERR(svs_dir));
  725. return PTR_ERR(svs_dir);
  726. }
  727. for (i = 0; i < ARRAY_SIZE(svs_entries); i++) {
  728. file_entry = debugfs_create_file(svs_entries[i].name, 0664,
  729. svs_dir, svsp,
  730. svs_entries[i].fops);
  731. if (IS_ERR(file_entry)) {
  732. dev_err(svsp->dev, "cannot create %s/%s: %ld\n",
  733. d, svs_entries[i].name, PTR_ERR(file_entry));
  734. return PTR_ERR(file_entry);
  735. }
  736. }
  737. for (idx = 0; idx < svsp->bank_max; idx++) {
  738. svsb = &svsp->banks[idx];
  739. if (svsb->mode_support == SVSB_MODE_ALL_DISABLE)
  740. continue;
  741. svsb_dir = debugfs_create_dir(svsb->name, svs_dir);
  742. if (IS_ERR(svsb_dir)) {
  743. dev_err(svsp->dev, "cannot create %s/%s: %ld\n",
  744. d, svsb->name, PTR_ERR(svsb_dir));
  745. return PTR_ERR(svsb_dir);
  746. }
  747. for (i = 0; i < ARRAY_SIZE(svsb_entries); i++) {
  748. file_entry = debugfs_create_file(svsb_entries[i].name,
  749. 0664, svsb_dir, svsb,
  750. svsb_entries[i].fops);
  751. if (IS_ERR(file_entry)) {
  752. dev_err(svsp->dev, "no %s/%s/%s?: %ld\n",
  753. d, svsb->name, svsb_entries[i].name,
  754. PTR_ERR(file_entry));
  755. return PTR_ERR(file_entry);
  756. }
  757. }
  758. }
  759. return 0;
  760. }
  761. #endif /* CONFIG_DEBUG_FS */
  762. static u32 interpolate(u32 f0, u32 f1, u32 v0, u32 v1, u32 fx)
  763. {
  764. u32 vx;
  765. if (v0 == v1 || f0 == f1)
  766. return v0;
  767. /* *100 to have decimal fraction factor */
  768. vx = (v0 * 100) - ((((v0 - v1) * 100) / (f0 - f1)) * (f0 - fx));
  769. return DIV_ROUND_UP(vx, 100);
  770. }
  771. static void svs_get_bank_volts_v3(struct svs_platform *svsp)
  772. {
  773. struct svs_bank *svsb = svsp->pbank;
  774. u32 i, j, *vop, vop74, vop30, turn_pt = svsb->turn_pt;
  775. u32 b_sft, shift_byte = 0, opp_start = 0, opp_stop = 0;
  776. u32 middle_index = (svsb->opp_count / 2);
  777. if (svsb->phase == SVSB_PHASE_MON &&
  778. svsb->volt_flags & SVSB_MON_VOLT_IGNORE)
  779. return;
  780. vop74 = svs_readl_relaxed(svsp, VOP74);
  781. vop30 = svs_readl_relaxed(svsp, VOP30);
  782. /* Target is to set svsb->volt[] by algorithm */
  783. if (turn_pt < middle_index) {
  784. if (svsb->type == SVSB_HIGH) {
  785. /* volt[0] ~ volt[turn_pt - 1] */
  786. for (i = 0; i < turn_pt; i++) {
  787. b_sft = BITS8 * (shift_byte % REG_BYTES);
  788. vop = (shift_byte < REG_BYTES) ? &vop30 :
  789. &vop74;
  790. svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0);
  791. shift_byte++;
  792. }
  793. } else if (svsb->type == SVSB_LOW) {
  794. /* volt[turn_pt] + volt[j] ~ volt[opp_count - 1] */
  795. j = svsb->opp_count - 7;
  796. svsb->volt[turn_pt] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, vop30);
  797. shift_byte++;
  798. for (i = j; i < svsb->opp_count; i++) {
  799. b_sft = BITS8 * (shift_byte % REG_BYTES);
  800. vop = (shift_byte < REG_BYTES) ? &vop30 :
  801. &vop74;
  802. svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0);
  803. shift_byte++;
  804. }
  805. /* volt[turn_pt + 1] ~ volt[j - 1] by interpolate */
  806. for (i = turn_pt + 1; i < j; i++)
  807. svsb->volt[i] = interpolate(svsb->freq_pct[turn_pt],
  808. svsb->freq_pct[j],
  809. svsb->volt[turn_pt],
  810. svsb->volt[j],
  811. svsb->freq_pct[i]);
  812. }
  813. } else {
  814. if (svsb->type == SVSB_HIGH) {
  815. /* volt[0] + volt[j] ~ volt[turn_pt - 1] */
  816. j = turn_pt - 7;
  817. svsb->volt[0] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, vop30);
  818. shift_byte++;
  819. for (i = j; i < turn_pt; i++) {
  820. b_sft = BITS8 * (shift_byte % REG_BYTES);
  821. vop = (shift_byte < REG_BYTES) ? &vop30 :
  822. &vop74;
  823. svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0);
  824. shift_byte++;
  825. }
  826. /* volt[1] ~ volt[j - 1] by interpolate */
  827. for (i = 1; i < j; i++)
  828. svsb->volt[i] = interpolate(svsb->freq_pct[0],
  829. svsb->freq_pct[j],
  830. svsb->volt[0],
  831. svsb->volt[j],
  832. svsb->freq_pct[i]);
  833. } else if (svsb->type == SVSB_LOW) {
  834. /* volt[turn_pt] ~ volt[opp_count - 1] */
  835. for (i = turn_pt; i < svsb->opp_count; i++) {
  836. b_sft = BITS8 * (shift_byte % REG_BYTES);
  837. vop = (shift_byte < REG_BYTES) ? &vop30 :
  838. &vop74;
  839. svsb->volt[i] = (*vop >> b_sft) & GENMASK(7, 0);
  840. shift_byte++;
  841. }
  842. }
  843. }
  844. if (svsb->type == SVSB_HIGH) {
  845. opp_start = 0;
  846. opp_stop = svsb->turn_pt;
  847. } else if (svsb->type == SVSB_LOW) {
  848. opp_start = svsb->turn_pt;
  849. opp_stop = svsb->opp_count;
  850. }
  851. for (i = opp_start; i < opp_stop; i++)
  852. if (svsb->volt_flags & SVSB_REMOVE_DVTFIXED_VOLT)
  853. svsb->volt[i] -= svsb->dvt_fixed;
  854. }
  855. static void svs_set_bank_freq_pct_v3(struct svs_platform *svsp)
  856. {
  857. struct svs_bank *svsb = svsp->pbank;
  858. u32 i, j, *freq_pct, freq_pct74 = 0, freq_pct30 = 0;
  859. u32 b_sft, shift_byte = 0, turn_pt;
  860. u32 middle_index = (svsb->opp_count / 2);
  861. for (i = 0; i < svsb->opp_count; i++) {
  862. if (svsb->opp_dfreq[i] <= svsb->turn_freq_base) {
  863. svsb->turn_pt = i;
  864. break;
  865. }
  866. }
  867. turn_pt = svsb->turn_pt;
  868. /* Target is to fill out freq_pct74 / freq_pct30 by algorithm */
  869. if (turn_pt < middle_index) {
  870. if (svsb->type == SVSB_HIGH) {
  871. /*
  872. * If we don't handle this situation,
  873. * SVSB_HIGH's FREQPCT74 / FREQPCT30 would keep "0"
  874. * and this leads SVSB_LOW to work abnormally.
  875. */
  876. if (turn_pt == 0)
  877. freq_pct30 = svsb->freq_pct[0];
  878. /* freq_pct[0] ~ freq_pct[turn_pt - 1] */
  879. for (i = 0; i < turn_pt; i++) {
  880. b_sft = BITS8 * (shift_byte % REG_BYTES);
  881. freq_pct = (shift_byte < REG_BYTES) ?
  882. &freq_pct30 : &freq_pct74;
  883. *freq_pct |= (svsb->freq_pct[i] << b_sft);
  884. shift_byte++;
  885. }
  886. } else if (svsb->type == SVSB_LOW) {
  887. /*
  888. * freq_pct[turn_pt] +
  889. * freq_pct[opp_count - 7] ~ freq_pct[opp_count -1]
  890. */
  891. freq_pct30 = svsb->freq_pct[turn_pt];
  892. shift_byte++;
  893. j = svsb->opp_count - 7;
  894. for (i = j; i < svsb->opp_count; i++) {
  895. b_sft = BITS8 * (shift_byte % REG_BYTES);
  896. freq_pct = (shift_byte < REG_BYTES) ?
  897. &freq_pct30 : &freq_pct74;
  898. *freq_pct |= (svsb->freq_pct[i] << b_sft);
  899. shift_byte++;
  900. }
  901. }
  902. } else {
  903. if (svsb->type == SVSB_HIGH) {
  904. /*
  905. * freq_pct[0] +
  906. * freq_pct[turn_pt - 7] ~ freq_pct[turn_pt - 1]
  907. */
  908. freq_pct30 = svsb->freq_pct[0];
  909. shift_byte++;
  910. j = turn_pt - 7;
  911. for (i = j; i < turn_pt; i++) {
  912. b_sft = BITS8 * (shift_byte % REG_BYTES);
  913. freq_pct = (shift_byte < REG_BYTES) ?
  914. &freq_pct30 : &freq_pct74;
  915. *freq_pct |= (svsb->freq_pct[i] << b_sft);
  916. shift_byte++;
  917. }
  918. } else if (svsb->type == SVSB_LOW) {
  919. /* freq_pct[turn_pt] ~ freq_pct[opp_count - 1] */
  920. for (i = turn_pt; i < svsb->opp_count; i++) {
  921. b_sft = BITS8 * (shift_byte % REG_BYTES);
  922. freq_pct = (shift_byte < REG_BYTES) ?
  923. &freq_pct30 : &freq_pct74;
  924. *freq_pct |= (svsb->freq_pct[i] << b_sft);
  925. shift_byte++;
  926. }
  927. }
  928. }
  929. svs_writel_relaxed(svsp, freq_pct74, FREQPCT74);
  930. svs_writel_relaxed(svsp, freq_pct30, FREQPCT30);
  931. }
  932. static void svs_get_bank_volts_v2(struct svs_platform *svsp)
  933. {
  934. struct svs_bank *svsb = svsp->pbank;
  935. u32 temp, i;
  936. temp = svs_readl_relaxed(svsp, VOP74);
  937. svsb->volt[14] = FIELD_GET(SVSB_VOPS_FLD_VOP3_7, temp);
  938. svsb->volt[12] = FIELD_GET(SVSB_VOPS_FLD_VOP2_6, temp);
  939. svsb->volt[10] = FIELD_GET(SVSB_VOPS_FLD_VOP1_5, temp);
  940. svsb->volt[8] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, temp);
  941. temp = svs_readl_relaxed(svsp, VOP30);
  942. svsb->volt[6] = FIELD_GET(SVSB_VOPS_FLD_VOP3_7, temp);
  943. svsb->volt[4] = FIELD_GET(SVSB_VOPS_FLD_VOP2_6, temp);
  944. svsb->volt[2] = FIELD_GET(SVSB_VOPS_FLD_VOP1_5, temp);
  945. svsb->volt[0] = FIELD_GET(SVSB_VOPS_FLD_VOP0_4, temp);
  946. for (i = 0; i <= 12; i += 2)
  947. svsb->volt[i + 1] = interpolate(svsb->freq_pct[i],
  948. svsb->freq_pct[i + 2],
  949. svsb->volt[i],
  950. svsb->volt[i + 2],
  951. svsb->freq_pct[i + 1]);
  952. svsb->volt[15] = interpolate(svsb->freq_pct[12],
  953. svsb->freq_pct[14],
  954. svsb->volt[12],
  955. svsb->volt[14],
  956. svsb->freq_pct[15]);
  957. for (i = 0; i < svsb->opp_count; i++)
  958. svsb->volt[i] += svsb->volt_od;
  959. }
  960. static void svs_set_bank_freq_pct_v2(struct svs_platform *svsp)
  961. {
  962. struct svs_bank *svsb = svsp->pbank;
  963. u32 freqpct74_val, freqpct30_val;
  964. freqpct74_val = FIELD_PREP(SVSB_FREQPCTS_FLD_PCT0_4, svsb->freq_pct[8]) |
  965. FIELD_PREP(SVSB_FREQPCTS_FLD_PCT1_5, svsb->freq_pct[10]) |
  966. FIELD_PREP(SVSB_FREQPCTS_FLD_PCT2_6, svsb->freq_pct[12]) |
  967. FIELD_PREP(SVSB_FREQPCTS_FLD_PCT3_7, svsb->freq_pct[14]);
  968. freqpct30_val = FIELD_PREP(SVSB_FREQPCTS_FLD_PCT0_4, svsb->freq_pct[0]) |
  969. FIELD_PREP(SVSB_FREQPCTS_FLD_PCT1_5, svsb->freq_pct[2]) |
  970. FIELD_PREP(SVSB_FREQPCTS_FLD_PCT2_6, svsb->freq_pct[4]) |
  971. FIELD_PREP(SVSB_FREQPCTS_FLD_PCT3_7, svsb->freq_pct[6]);
  972. svs_writel_relaxed(svsp, freqpct74_val, FREQPCT74);
  973. svs_writel_relaxed(svsp, freqpct30_val, FREQPCT30);
  974. }
  975. static void svs_set_bank_phase(struct svs_platform *svsp,
  976. enum svsb_phase target_phase)
  977. {
  978. struct svs_bank *svsb = svsp->pbank;
  979. u32 des_char, temp_char, det_char, limit_vals, init2vals, ts_calcs;
  980. svs_switch_bank(svsp);
  981. des_char = FIELD_PREP(SVSB_DESCHAR_FLD_BDES, svsb->bdes) |
  982. FIELD_PREP(SVSB_DESCHAR_FLD_MDES, svsb->mdes);
  983. svs_writel_relaxed(svsp, des_char, DESCHAR);
  984. temp_char = FIELD_PREP(SVSB_TEMPCHAR_FLD_VCO, svsb->vco) |
  985. FIELD_PREP(SVSB_TEMPCHAR_FLD_MTDES, svsb->mtdes) |
  986. FIELD_PREP(SVSB_TEMPCHAR_FLD_DVT_FIXED, svsb->dvt_fixed);
  987. svs_writel_relaxed(svsp, temp_char, TEMPCHAR);
  988. det_char = FIELD_PREP(SVSB_DETCHAR_FLD_DCBDET, svsb->dcbdet) |
  989. FIELD_PREP(SVSB_DETCHAR_FLD_DCMDET, svsb->dcmdet);
  990. svs_writel_relaxed(svsp, det_char, DETCHAR);
  991. svs_writel_relaxed(svsp, svsb->dc_config, DCCONFIG);
  992. svs_writel_relaxed(svsp, svsb->age_config, AGECONFIG);
  993. svs_writel_relaxed(svsp, SVSB_RUNCONFIG_DEFAULT, RUNCONFIG);
  994. svsb->set_freq_pct(svsp);
  995. limit_vals = FIELD_PREP(SVSB_LIMITVALS_FLD_DTLO, SVSB_VAL_DTLO) |
  996. FIELD_PREP(SVSB_LIMITVALS_FLD_DTHI, SVSB_VAL_DTHI) |
  997. FIELD_PREP(SVSB_LIMITVALS_FLD_VMIN, svsb->vmin) |
  998. FIELD_PREP(SVSB_LIMITVALS_FLD_VMAX, svsb->vmax);
  999. svs_writel_relaxed(svsp, limit_vals, LIMITVALS);
  1000. svs_writel_relaxed(svsp, SVSB_DET_WINDOW, DETWINDOW);
  1001. svs_writel_relaxed(svsp, SVSB_DET_MAX, CONFIG);
  1002. svs_writel_relaxed(svsp, svsb->chk_shift, CHKSHIFT);
  1003. svs_writel_relaxed(svsp, svsb->ctl0, CTL0);
  1004. svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS);
  1005. switch (target_phase) {
  1006. case SVSB_PHASE_INIT01:
  1007. svs_writel_relaxed(svsp, svsb->vboot, VBOOT);
  1008. svs_writel_relaxed(svsp, SVSB_INTEN_INIT0x, INTEN);
  1009. svs_writel_relaxed(svsp, SVSB_PTPEN_INIT01, SVSEN);
  1010. break;
  1011. case SVSB_PHASE_INIT02:
  1012. init2vals = FIELD_PREP(SVSB_INIT2VALS_FLD_AGEVOFFSETIN, svsb->age_voffset_in) |
  1013. FIELD_PREP(SVSB_INIT2VALS_FLD_DCVOFFSETIN, svsb->dc_voffset_in);
  1014. svs_writel_relaxed(svsp, SVSB_INTEN_INIT0x, INTEN);
  1015. svs_writel_relaxed(svsp, init2vals, INIT2VALS);
  1016. svs_writel_relaxed(svsp, SVSB_PTPEN_INIT02, SVSEN);
  1017. break;
  1018. case SVSB_PHASE_MON:
  1019. ts_calcs = FIELD_PREP(SVSB_TSCALCS_FLD_BTS, svsb->bts) |
  1020. FIELD_PREP(SVSB_TSCALCS_FLD_MTS, svsb->mts);
  1021. svs_writel_relaxed(svsp, ts_calcs, TSCALCS);
  1022. svs_writel_relaxed(svsp, SVSB_INTEN_MONVOPEN, INTEN);
  1023. svs_writel_relaxed(svsp, SVSB_PTPEN_MON, SVSEN);
  1024. break;
  1025. default:
  1026. dev_err(svsb->dev, "requested unknown target phase: %u\n",
  1027. target_phase);
  1028. break;
  1029. }
  1030. }
  1031. static inline void svs_save_bank_register_data(struct svs_platform *svsp,
  1032. enum svsb_phase phase)
  1033. {
  1034. struct svs_bank *svsb = svsp->pbank;
  1035. enum svs_reg_index rg_i;
  1036. for (rg_i = DESCHAR; rg_i < SVS_REG_MAX; rg_i++)
  1037. svsb->reg_data[phase][rg_i] = svs_readl_relaxed(svsp, rg_i);
  1038. }
  1039. static inline void svs_error_isr_handler(struct svs_platform *svsp)
  1040. {
  1041. struct svs_bank *svsb = svsp->pbank;
  1042. dev_err(svsb->dev, "%s: CORESEL = 0x%08x\n",
  1043. __func__, svs_readl_relaxed(svsp, CORESEL));
  1044. dev_err(svsb->dev, "SVSEN = 0x%08x, INTSTS = 0x%08x\n",
  1045. svs_readl_relaxed(svsp, SVSEN),
  1046. svs_readl_relaxed(svsp, INTSTS));
  1047. dev_err(svsb->dev, "SMSTATE0 = 0x%08x, SMSTATE1 = 0x%08x\n",
  1048. svs_readl_relaxed(svsp, SMSTATE0),
  1049. svs_readl_relaxed(svsp, SMSTATE1));
  1050. dev_err(svsb->dev, "TEMP = 0x%08x\n", svs_readl_relaxed(svsp, TEMP));
  1051. svs_save_bank_register_data(svsp, SVSB_PHASE_ERROR);
  1052. svsb->phase = SVSB_PHASE_ERROR;
  1053. svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN);
  1054. svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS);
  1055. }
  1056. static inline void svs_init01_isr_handler(struct svs_platform *svsp)
  1057. {
  1058. struct svs_bank *svsb = svsp->pbank;
  1059. dev_info(svsb->dev, "%s: VDN74~30:0x%08x~0x%08x, DC:0x%08x\n",
  1060. __func__, svs_readl_relaxed(svsp, VDESIGN74),
  1061. svs_readl_relaxed(svsp, VDESIGN30),
  1062. svs_readl_relaxed(svsp, DCVALUES));
  1063. svs_save_bank_register_data(svsp, SVSB_PHASE_INIT01);
  1064. svsb->phase = SVSB_PHASE_INIT01;
  1065. svsb->dc_voffset_in = ~(svs_readl_relaxed(svsp, DCVALUES) &
  1066. GENMASK(15, 0)) + 1;
  1067. if (svsb->volt_flags & SVSB_INIT01_VOLT_IGNORE ||
  1068. (svsb->dc_voffset_in & SVSB_DC_SIGNED_BIT &&
  1069. svsb->volt_flags & SVSB_INIT01_VOLT_INC_ONLY))
  1070. svsb->dc_voffset_in = 0;
  1071. svsb->age_voffset_in = svs_readl_relaxed(svsp, AGEVALUES) &
  1072. GENMASK(15, 0);
  1073. svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN);
  1074. svs_writel_relaxed(svsp, SVSB_INTSTS_F0_COMPLETE, INTSTS);
  1075. svsb->core_sel &= ~SVSB_DET_CLK_EN;
  1076. }
  1077. static inline void svs_init02_isr_handler(struct svs_platform *svsp)
  1078. {
  1079. struct svs_bank *svsb = svsp->pbank;
  1080. dev_info(svsb->dev, "%s: VOP74~30:0x%08x~0x%08x, DC:0x%08x\n",
  1081. __func__, svs_readl_relaxed(svsp, VOP74),
  1082. svs_readl_relaxed(svsp, VOP30),
  1083. svs_readl_relaxed(svsp, DCVALUES));
  1084. svs_save_bank_register_data(svsp, SVSB_PHASE_INIT02);
  1085. svsb->phase = SVSB_PHASE_INIT02;
  1086. svsb->get_volts(svsp);
  1087. svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN);
  1088. svs_writel_relaxed(svsp, SVSB_INTSTS_F0_COMPLETE, INTSTS);
  1089. }
  1090. static inline void svs_mon_mode_isr_handler(struct svs_platform *svsp)
  1091. {
  1092. struct svs_bank *svsb = svsp->pbank;
  1093. svs_save_bank_register_data(svsp, SVSB_PHASE_MON);
  1094. svsb->phase = SVSB_PHASE_MON;
  1095. svsb->get_volts(svsp);
  1096. svsb->temp = svs_readl_relaxed(svsp, TEMP) & GENMASK(7, 0);
  1097. svs_writel_relaxed(svsp, SVSB_INTSTS_FLD_MONVOP, INTSTS);
  1098. }
  1099. static irqreturn_t svs_isr(int irq, void *data)
  1100. {
  1101. struct svs_platform *svsp = data;
  1102. struct svs_bank *svsb = NULL;
  1103. unsigned long flags;
  1104. u32 idx, int_sts, svs_en;
  1105. for (idx = 0; idx < svsp->bank_max; idx++) {
  1106. svsb = &svsp->banks[idx];
  1107. WARN(!svsb, "%s: svsb(%s) is null", __func__, svsb->name);
  1108. spin_lock_irqsave(&svs_lock, flags);
  1109. svsp->pbank = svsb;
  1110. /* Find out which svs bank fires interrupt */
  1111. if (svsb->int_st & svs_readl_relaxed(svsp, INTST)) {
  1112. spin_unlock_irqrestore(&svs_lock, flags);
  1113. continue;
  1114. }
  1115. svs_switch_bank(svsp);
  1116. int_sts = svs_readl_relaxed(svsp, INTSTS);
  1117. svs_en = svs_readl_relaxed(svsp, SVSEN);
  1118. if (int_sts == SVSB_INTSTS_F0_COMPLETE &&
  1119. svs_en == SVSB_PTPEN_INIT01)
  1120. svs_init01_isr_handler(svsp);
  1121. else if (int_sts == SVSB_INTSTS_F0_COMPLETE &&
  1122. svs_en == SVSB_PTPEN_INIT02)
  1123. svs_init02_isr_handler(svsp);
  1124. else if (int_sts & SVSB_INTSTS_FLD_MONVOP)
  1125. svs_mon_mode_isr_handler(svsp);
  1126. else
  1127. svs_error_isr_handler(svsp);
  1128. spin_unlock_irqrestore(&svs_lock, flags);
  1129. break;
  1130. }
  1131. svs_adjust_pm_opp_volts(svsb);
  1132. if (svsb->phase == SVSB_PHASE_INIT01 ||
  1133. svsb->phase == SVSB_PHASE_INIT02)
  1134. complete(&svsb->init_completion);
  1135. return IRQ_HANDLED;
  1136. }
  1137. static int svs_init01(struct svs_platform *svsp)
  1138. {
  1139. struct svs_bank *svsb;
  1140. unsigned long flags, time_left;
  1141. bool search_done;
  1142. int ret = 0, r;
  1143. u32 opp_freq, opp_vboot, buck_volt, idx, i;
  1144. /* Keep CPUs' core power on for svs_init01 initialization */
  1145. cpuidle_pause_and_lock();
  1146. /* Svs bank init01 preparation - power enable */
  1147. for (idx = 0; idx < svsp->bank_max; idx++) {
  1148. svsb = &svsp->banks[idx];
  1149. if (!(svsb->mode_support & SVSB_MODE_INIT01))
  1150. continue;
  1151. ret = regulator_enable(svsb->buck);
  1152. if (ret) {
  1153. dev_err(svsb->dev, "%s enable fail: %d\n",
  1154. svsb->buck_name, ret);
  1155. goto svs_init01_resume_cpuidle;
  1156. }
  1157. /* Some buck doesn't support mode change. Show fail msg only */
  1158. ret = regulator_set_mode(svsb->buck, REGULATOR_MODE_FAST);
  1159. if (ret)
  1160. dev_notice(svsb->dev, "set fast mode fail: %d\n", ret);
  1161. if (svsb->volt_flags & SVSB_INIT01_PD_REQ) {
  1162. if (!pm_runtime_enabled(svsb->opp_dev)) {
  1163. pm_runtime_enable(svsb->opp_dev);
  1164. svsb->pm_runtime_enabled_count++;
  1165. }
  1166. ret = pm_runtime_resume_and_get(svsb->opp_dev);
  1167. if (ret < 0) {
  1168. dev_err(svsb->dev, "mtcmos on fail: %d\n", ret);
  1169. goto svs_init01_resume_cpuidle;
  1170. }
  1171. }
  1172. }
  1173. /*
  1174. * Svs bank init01 preparation - vboot voltage adjustment
  1175. * Sometimes two svs banks use the same buck. Therefore,
  1176. * we have to set each svs bank to target voltage(vboot) first.
  1177. */
  1178. for (idx = 0; idx < svsp->bank_max; idx++) {
  1179. svsb = &svsp->banks[idx];
  1180. if (!(svsb->mode_support & SVSB_MODE_INIT01))
  1181. continue;
  1182. /*
  1183. * Find the fastest freq that can be run at vboot and
  1184. * fix to that freq until svs_init01 is done.
  1185. */
  1186. search_done = false;
  1187. opp_vboot = svs_bank_volt_to_opp_volt(svsb->vboot,
  1188. svsb->volt_step,
  1189. svsb->volt_base);
  1190. for (i = 0; i < svsb->opp_count; i++) {
  1191. opp_freq = svsb->opp_dfreq[i];
  1192. if (!search_done && svsb->opp_dvolt[i] <= opp_vboot) {
  1193. ret = dev_pm_opp_adjust_voltage(svsb->opp_dev,
  1194. opp_freq,
  1195. opp_vboot,
  1196. opp_vboot,
  1197. opp_vboot);
  1198. if (ret) {
  1199. dev_err(svsb->dev,
  1200. "set opp %uuV vboot fail: %d\n",
  1201. opp_vboot, ret);
  1202. goto svs_init01_finish;
  1203. }
  1204. search_done = true;
  1205. } else {
  1206. ret = dev_pm_opp_disable(svsb->opp_dev,
  1207. svsb->opp_dfreq[i]);
  1208. if (ret) {
  1209. dev_err(svsb->dev,
  1210. "opp %uHz disable fail: %d\n",
  1211. svsb->opp_dfreq[i], ret);
  1212. goto svs_init01_finish;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. /* Svs bank init01 begins */
  1218. for (idx = 0; idx < svsp->bank_max; idx++) {
  1219. svsb = &svsp->banks[idx];
  1220. if (!(svsb->mode_support & SVSB_MODE_INIT01))
  1221. continue;
  1222. opp_vboot = svs_bank_volt_to_opp_volt(svsb->vboot,
  1223. svsb->volt_step,
  1224. svsb->volt_base);
  1225. buck_volt = regulator_get_voltage(svsb->buck);
  1226. if (buck_volt != opp_vboot) {
  1227. dev_err(svsb->dev,
  1228. "buck voltage: %uuV, expected vboot: %uuV\n",
  1229. buck_volt, opp_vboot);
  1230. ret = -EPERM;
  1231. goto svs_init01_finish;
  1232. }
  1233. spin_lock_irqsave(&svs_lock, flags);
  1234. svsp->pbank = svsb;
  1235. svs_set_bank_phase(svsp, SVSB_PHASE_INIT01);
  1236. spin_unlock_irqrestore(&svs_lock, flags);
  1237. time_left = wait_for_completion_timeout(&svsb->init_completion,
  1238. msecs_to_jiffies(5000));
  1239. if (!time_left) {
  1240. dev_err(svsb->dev, "init01 completion timeout\n");
  1241. ret = -EBUSY;
  1242. goto svs_init01_finish;
  1243. }
  1244. }
  1245. svs_init01_finish:
  1246. for (idx = 0; idx < svsp->bank_max; idx++) {
  1247. svsb = &svsp->banks[idx];
  1248. if (!(svsb->mode_support & SVSB_MODE_INIT01))
  1249. continue;
  1250. for (i = 0; i < svsb->opp_count; i++) {
  1251. r = dev_pm_opp_enable(svsb->opp_dev,
  1252. svsb->opp_dfreq[i]);
  1253. if (r)
  1254. dev_err(svsb->dev, "opp %uHz enable fail: %d\n",
  1255. svsb->opp_dfreq[i], r);
  1256. }
  1257. if (svsb->volt_flags & SVSB_INIT01_PD_REQ) {
  1258. r = pm_runtime_put_sync(svsb->opp_dev);
  1259. if (r)
  1260. dev_err(svsb->dev, "mtcmos off fail: %d\n", r);
  1261. if (svsb->pm_runtime_enabled_count > 0) {
  1262. pm_runtime_disable(svsb->opp_dev);
  1263. svsb->pm_runtime_enabled_count--;
  1264. }
  1265. }
  1266. r = regulator_set_mode(svsb->buck, REGULATOR_MODE_NORMAL);
  1267. if (r)
  1268. dev_notice(svsb->dev, "set normal mode fail: %d\n", r);
  1269. r = regulator_disable(svsb->buck);
  1270. if (r)
  1271. dev_err(svsb->dev, "%s disable fail: %d\n",
  1272. svsb->buck_name, r);
  1273. }
  1274. svs_init01_resume_cpuidle:
  1275. cpuidle_resume_and_unlock();
  1276. return ret;
  1277. }
  1278. static int svs_init02(struct svs_platform *svsp)
  1279. {
  1280. struct svs_bank *svsb;
  1281. unsigned long flags, time_left;
  1282. int ret;
  1283. u32 idx;
  1284. for (idx = 0; idx < svsp->bank_max; idx++) {
  1285. svsb = &svsp->banks[idx];
  1286. if (!(svsb->mode_support & SVSB_MODE_INIT02))
  1287. continue;
  1288. reinit_completion(&svsb->init_completion);
  1289. spin_lock_irqsave(&svs_lock, flags);
  1290. svsp->pbank = svsb;
  1291. svs_set_bank_phase(svsp, SVSB_PHASE_INIT02);
  1292. spin_unlock_irqrestore(&svs_lock, flags);
  1293. time_left = wait_for_completion_timeout(&svsb->init_completion,
  1294. msecs_to_jiffies(5000));
  1295. if (!time_left) {
  1296. dev_err(svsb->dev, "init02 completion timeout\n");
  1297. ret = -EBUSY;
  1298. goto out_of_init02;
  1299. }
  1300. }
  1301. /*
  1302. * 2-line high/low bank update its corresponding opp voltages only.
  1303. * Therefore, we sync voltages from opp for high/low bank voltages
  1304. * consistency.
  1305. */
  1306. for (idx = 0; idx < svsp->bank_max; idx++) {
  1307. svsb = &svsp->banks[idx];
  1308. if (!(svsb->mode_support & SVSB_MODE_INIT02))
  1309. continue;
  1310. if (svsb->type == SVSB_HIGH || svsb->type == SVSB_LOW) {
  1311. if (svs_sync_bank_volts_from_opp(svsb)) {
  1312. dev_err(svsb->dev, "sync volt fail\n");
  1313. ret = -EPERM;
  1314. goto out_of_init02;
  1315. }
  1316. }
  1317. }
  1318. return 0;
  1319. out_of_init02:
  1320. for (idx = 0; idx < svsp->bank_max; idx++) {
  1321. svsb = &svsp->banks[idx];
  1322. spin_lock_irqsave(&svs_lock, flags);
  1323. svsp->pbank = svsb;
  1324. svs_switch_bank(svsp);
  1325. svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN);
  1326. svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS);
  1327. spin_unlock_irqrestore(&svs_lock, flags);
  1328. svsb->phase = SVSB_PHASE_ERROR;
  1329. svs_adjust_pm_opp_volts(svsb);
  1330. }
  1331. return ret;
  1332. }
  1333. static void svs_mon_mode(struct svs_platform *svsp)
  1334. {
  1335. struct svs_bank *svsb;
  1336. unsigned long flags;
  1337. u32 idx;
  1338. for (idx = 0; idx < svsp->bank_max; idx++) {
  1339. svsb = &svsp->banks[idx];
  1340. if (!(svsb->mode_support & SVSB_MODE_MON))
  1341. continue;
  1342. spin_lock_irqsave(&svs_lock, flags);
  1343. svsp->pbank = svsb;
  1344. svs_set_bank_phase(svsp, SVSB_PHASE_MON);
  1345. spin_unlock_irqrestore(&svs_lock, flags);
  1346. }
  1347. }
  1348. static int svs_start(struct svs_platform *svsp)
  1349. {
  1350. int ret;
  1351. ret = svs_init01(svsp);
  1352. if (ret)
  1353. return ret;
  1354. ret = svs_init02(svsp);
  1355. if (ret)
  1356. return ret;
  1357. svs_mon_mode(svsp);
  1358. return 0;
  1359. }
  1360. static int svs_suspend(struct device *dev)
  1361. {
  1362. struct svs_platform *svsp = dev_get_drvdata(dev);
  1363. struct svs_bank *svsb;
  1364. unsigned long flags;
  1365. int ret;
  1366. u32 idx;
  1367. for (idx = 0; idx < svsp->bank_max; idx++) {
  1368. svsb = &svsp->banks[idx];
  1369. /* This might wait for svs_isr() process */
  1370. spin_lock_irqsave(&svs_lock, flags);
  1371. svsp->pbank = svsb;
  1372. svs_switch_bank(svsp);
  1373. svs_writel_relaxed(svsp, SVSB_PTPEN_OFF, SVSEN);
  1374. svs_writel_relaxed(svsp, SVSB_INTSTS_VAL_CLEAN, INTSTS);
  1375. spin_unlock_irqrestore(&svs_lock, flags);
  1376. svsb->phase = SVSB_PHASE_ERROR;
  1377. svs_adjust_pm_opp_volts(svsb);
  1378. }
  1379. ret = reset_control_assert(svsp->rst);
  1380. if (ret) {
  1381. dev_err(svsp->dev, "cannot assert reset %d\n", ret);
  1382. return ret;
  1383. }
  1384. clk_disable_unprepare(svsp->main_clk);
  1385. return 0;
  1386. }
  1387. static int svs_resume(struct device *dev)
  1388. {
  1389. struct svs_platform *svsp = dev_get_drvdata(dev);
  1390. int ret;
  1391. ret = clk_prepare_enable(svsp->main_clk);
  1392. if (ret) {
  1393. dev_err(svsp->dev, "cannot enable main_clk, disable svs\n");
  1394. return ret;
  1395. }
  1396. ret = reset_control_deassert(svsp->rst);
  1397. if (ret) {
  1398. dev_err(svsp->dev, "cannot deassert reset %d\n", ret);
  1399. goto out_of_resume;
  1400. }
  1401. ret = svs_init02(svsp);
  1402. if (ret)
  1403. goto svs_resume_reset_assert;
  1404. svs_mon_mode(svsp);
  1405. return 0;
  1406. svs_resume_reset_assert:
  1407. dev_err(svsp->dev, "assert reset: %d\n",
  1408. reset_control_assert(svsp->rst));
  1409. out_of_resume:
  1410. clk_disable_unprepare(svsp->main_clk);
  1411. return ret;
  1412. }
  1413. static int svs_bank_resource_setup(struct svs_platform *svsp)
  1414. {
  1415. struct svs_bank *svsb;
  1416. struct dev_pm_opp *opp;
  1417. unsigned long freq;
  1418. int count, ret;
  1419. u32 idx, i;
  1420. dev_set_drvdata(svsp->dev, svsp);
  1421. for (idx = 0; idx < svsp->bank_max; idx++) {
  1422. svsb = &svsp->banks[idx];
  1423. switch (svsb->sw_id) {
  1424. case SVSB_CPU_LITTLE:
  1425. svsb->name = "SVSB_CPU_LITTLE";
  1426. break;
  1427. case SVSB_CPU_BIG:
  1428. svsb->name = "SVSB_CPU_BIG";
  1429. break;
  1430. case SVSB_CCI:
  1431. svsb->name = "SVSB_CCI";
  1432. break;
  1433. case SVSB_GPU:
  1434. if (svsb->type == SVSB_HIGH)
  1435. svsb->name = "SVSB_GPU_HIGH";
  1436. else if (svsb->type == SVSB_LOW)
  1437. svsb->name = "SVSB_GPU_LOW";
  1438. else
  1439. svsb->name = "SVSB_GPU";
  1440. break;
  1441. default:
  1442. dev_err(svsb->dev, "unknown sw_id: %u\n", svsb->sw_id);
  1443. return -EINVAL;
  1444. }
  1445. svsb->dev = devm_kzalloc(svsp->dev, sizeof(*svsb->dev),
  1446. GFP_KERNEL);
  1447. if (!svsb->dev)
  1448. return -ENOMEM;
  1449. ret = dev_set_name(svsb->dev, "%s", svsb->name);
  1450. if (ret)
  1451. return ret;
  1452. dev_set_drvdata(svsb->dev, svsp);
  1453. ret = devm_pm_opp_of_add_table(svsb->opp_dev);
  1454. if (ret) {
  1455. dev_err(svsb->dev, "add opp table fail: %d\n", ret);
  1456. return ret;
  1457. }
  1458. mutex_init(&svsb->lock);
  1459. init_completion(&svsb->init_completion);
  1460. if (svsb->mode_support & SVSB_MODE_INIT01) {
  1461. svsb->buck = devm_regulator_get_optional(svsb->opp_dev,
  1462. svsb->buck_name);
  1463. if (IS_ERR(svsb->buck)) {
  1464. dev_err(svsb->dev, "cannot get \"%s-supply\"\n",
  1465. svsb->buck_name);
  1466. return PTR_ERR(svsb->buck);
  1467. }
  1468. }
  1469. if (svsb->mode_support & SVSB_MODE_MON) {
  1470. svsb->tzd = thermal_zone_get_zone_by_name(svsb->tzone_name);
  1471. if (IS_ERR(svsb->tzd)) {
  1472. dev_err(svsb->dev, "cannot get \"%s\" thermal zone\n",
  1473. svsb->tzone_name);
  1474. return PTR_ERR(svsb->tzd);
  1475. }
  1476. }
  1477. count = dev_pm_opp_get_opp_count(svsb->opp_dev);
  1478. if (svsb->opp_count != count) {
  1479. dev_err(svsb->dev,
  1480. "opp_count not \"%u\" but get \"%d\"?\n",
  1481. svsb->opp_count, count);
  1482. return count;
  1483. }
  1484. for (i = 0, freq = U32_MAX; i < svsb->opp_count; i++, freq--) {
  1485. opp = dev_pm_opp_find_freq_floor(svsb->opp_dev, &freq);
  1486. if (IS_ERR(opp)) {
  1487. dev_err(svsb->dev, "cannot find freq = %ld\n",
  1488. PTR_ERR(opp));
  1489. return PTR_ERR(opp);
  1490. }
  1491. svsb->opp_dfreq[i] = freq;
  1492. svsb->opp_dvolt[i] = dev_pm_opp_get_voltage(opp);
  1493. svsb->freq_pct[i] = percent(svsb->opp_dfreq[i],
  1494. svsb->freq_base);
  1495. dev_pm_opp_put(opp);
  1496. }
  1497. }
  1498. return 0;
  1499. }
  1500. static int svs_thermal_efuse_get_data(struct svs_platform *svsp)
  1501. {
  1502. struct nvmem_cell *cell;
  1503. /* Thermal efuse parsing */
  1504. cell = nvmem_cell_get(svsp->dev, "t-calibration-data");
  1505. if (IS_ERR_OR_NULL(cell)) {
  1506. dev_err(svsp->dev, "no \"t-calibration-data\"? %ld\n", PTR_ERR(cell));
  1507. return PTR_ERR(cell);
  1508. }
  1509. svsp->tefuse = nvmem_cell_read(cell, &svsp->tefuse_max);
  1510. if (IS_ERR(svsp->tefuse)) {
  1511. dev_err(svsp->dev, "cannot read thermal efuse: %ld\n",
  1512. PTR_ERR(svsp->tefuse));
  1513. nvmem_cell_put(cell);
  1514. return PTR_ERR(svsp->tefuse);
  1515. }
  1516. svsp->tefuse_max /= sizeof(u32);
  1517. nvmem_cell_put(cell);
  1518. return 0;
  1519. }
  1520. static bool svs_mt8192_efuse_parsing(struct svs_platform *svsp)
  1521. {
  1522. struct svs_bank *svsb;
  1523. u32 idx, i, vmin, golden_temp;
  1524. int ret;
  1525. for (i = 0; i < svsp->efuse_max; i++)
  1526. if (svsp->efuse[i])
  1527. dev_info(svsp->dev, "M_HW_RES%d: 0x%08x\n",
  1528. i, svsp->efuse[i]);
  1529. if (!svsp->efuse[9]) {
  1530. dev_notice(svsp->dev, "svs_efuse[9] = 0x0?\n");
  1531. return false;
  1532. }
  1533. /* Svs efuse parsing */
  1534. vmin = (svsp->efuse[19] >> 4) & GENMASK(1, 0);
  1535. for (idx = 0; idx < svsp->bank_max; idx++) {
  1536. svsb = &svsp->banks[idx];
  1537. if (vmin == 0x1)
  1538. svsb->vmin = 0x1e;
  1539. if (svsb->type == SVSB_LOW) {
  1540. svsb->mtdes = svsp->efuse[10] & GENMASK(7, 0);
  1541. svsb->bdes = (svsp->efuse[10] >> 16) & GENMASK(7, 0);
  1542. svsb->mdes = (svsp->efuse[10] >> 24) & GENMASK(7, 0);
  1543. svsb->dcbdet = (svsp->efuse[17]) & GENMASK(7, 0);
  1544. svsb->dcmdet = (svsp->efuse[17] >> 8) & GENMASK(7, 0);
  1545. } else if (svsb->type == SVSB_HIGH) {
  1546. svsb->mtdes = svsp->efuse[9] & GENMASK(7, 0);
  1547. svsb->bdes = (svsp->efuse[9] >> 16) & GENMASK(7, 0);
  1548. svsb->mdes = (svsp->efuse[9] >> 24) & GENMASK(7, 0);
  1549. svsb->dcbdet = (svsp->efuse[17] >> 16) & GENMASK(7, 0);
  1550. svsb->dcmdet = (svsp->efuse[17] >> 24) & GENMASK(7, 0);
  1551. }
  1552. svsb->vmax += svsb->dvt_fixed;
  1553. }
  1554. ret = svs_thermal_efuse_get_data(svsp);
  1555. if (ret)
  1556. return false;
  1557. for (i = 0; i < svsp->tefuse_max; i++)
  1558. if (svsp->tefuse[i] != 0)
  1559. break;
  1560. if (i == svsp->tefuse_max)
  1561. golden_temp = 50; /* All thermal efuse data are 0 */
  1562. else
  1563. golden_temp = (svsp->tefuse[0] >> 24) & GENMASK(7, 0);
  1564. for (idx = 0; idx < svsp->bank_max; idx++) {
  1565. svsb = &svsp->banks[idx];
  1566. svsb->mts = 500;
  1567. svsb->bts = (((500 * golden_temp + 250460) / 1000) - 25) * 4;
  1568. }
  1569. return true;
  1570. }
  1571. static bool svs_mt8183_efuse_parsing(struct svs_platform *svsp)
  1572. {
  1573. struct svs_bank *svsb;
  1574. int format[6], x_roomt[6], o_vtsmcu[5], o_vtsabb, tb_roomt = 0;
  1575. int adc_ge_t, adc_oe_t, ge, oe, gain, degc_cali, adc_cali_en_t;
  1576. int o_slope, o_slope_sign, ts_id;
  1577. u32 idx, i, ft_pgm, mts, temp0, temp1, temp2;
  1578. int ret;
  1579. for (i = 0; i < svsp->efuse_max; i++)
  1580. if (svsp->efuse[i])
  1581. dev_info(svsp->dev, "M_HW_RES%d: 0x%08x\n",
  1582. i, svsp->efuse[i]);
  1583. if (!svsp->efuse[2]) {
  1584. dev_notice(svsp->dev, "svs_efuse[2] = 0x0?\n");
  1585. return false;
  1586. }
  1587. /* Svs efuse parsing */
  1588. ft_pgm = (svsp->efuse[0] >> 4) & GENMASK(3, 0);
  1589. for (idx = 0; idx < svsp->bank_max; idx++) {
  1590. svsb = &svsp->banks[idx];
  1591. if (ft_pgm <= 1)
  1592. svsb->volt_flags |= SVSB_INIT01_VOLT_IGNORE;
  1593. switch (svsb->sw_id) {
  1594. case SVSB_CPU_LITTLE:
  1595. svsb->bdes = svsp->efuse[16] & GENMASK(7, 0);
  1596. svsb->mdes = (svsp->efuse[16] >> 8) & GENMASK(7, 0);
  1597. svsb->dcbdet = (svsp->efuse[16] >> 16) & GENMASK(7, 0);
  1598. svsb->dcmdet = (svsp->efuse[16] >> 24) & GENMASK(7, 0);
  1599. svsb->mtdes = (svsp->efuse[17] >> 16) & GENMASK(7, 0);
  1600. if (ft_pgm <= 3)
  1601. svsb->volt_od += 10;
  1602. else
  1603. svsb->volt_od += 2;
  1604. break;
  1605. case SVSB_CPU_BIG:
  1606. svsb->bdes = svsp->efuse[18] & GENMASK(7, 0);
  1607. svsb->mdes = (svsp->efuse[18] >> 8) & GENMASK(7, 0);
  1608. svsb->dcbdet = (svsp->efuse[18] >> 16) & GENMASK(7, 0);
  1609. svsb->dcmdet = (svsp->efuse[18] >> 24) & GENMASK(7, 0);
  1610. svsb->mtdes = svsp->efuse[17] & GENMASK(7, 0);
  1611. if (ft_pgm <= 3)
  1612. svsb->volt_od += 15;
  1613. else
  1614. svsb->volt_od += 12;
  1615. break;
  1616. case SVSB_CCI:
  1617. svsb->bdes = svsp->efuse[4] & GENMASK(7, 0);
  1618. svsb->mdes = (svsp->efuse[4] >> 8) & GENMASK(7, 0);
  1619. svsb->dcbdet = (svsp->efuse[4] >> 16) & GENMASK(7, 0);
  1620. svsb->dcmdet = (svsp->efuse[4] >> 24) & GENMASK(7, 0);
  1621. svsb->mtdes = (svsp->efuse[5] >> 16) & GENMASK(7, 0);
  1622. if (ft_pgm <= 3)
  1623. svsb->volt_od += 10;
  1624. else
  1625. svsb->volt_od += 2;
  1626. break;
  1627. case SVSB_GPU:
  1628. svsb->bdes = svsp->efuse[6] & GENMASK(7, 0);
  1629. svsb->mdes = (svsp->efuse[6] >> 8) & GENMASK(7, 0);
  1630. svsb->dcbdet = (svsp->efuse[6] >> 16) & GENMASK(7, 0);
  1631. svsb->dcmdet = (svsp->efuse[6] >> 24) & GENMASK(7, 0);
  1632. svsb->mtdes = svsp->efuse[5] & GENMASK(7, 0);
  1633. if (ft_pgm >= 2) {
  1634. svsb->freq_base = 800000000; /* 800MHz */
  1635. svsb->dvt_fixed = 2;
  1636. }
  1637. break;
  1638. default:
  1639. dev_err(svsb->dev, "unknown sw_id: %u\n", svsb->sw_id);
  1640. return false;
  1641. }
  1642. }
  1643. ret = svs_thermal_efuse_get_data(svsp);
  1644. if (ret)
  1645. return false;
  1646. /* Thermal efuse parsing */
  1647. adc_ge_t = (svsp->tefuse[1] >> 22) & GENMASK(9, 0);
  1648. adc_oe_t = (svsp->tefuse[1] >> 12) & GENMASK(9, 0);
  1649. o_vtsmcu[0] = (svsp->tefuse[0] >> 17) & GENMASK(8, 0);
  1650. o_vtsmcu[1] = (svsp->tefuse[0] >> 8) & GENMASK(8, 0);
  1651. o_vtsmcu[2] = svsp->tefuse[1] & GENMASK(8, 0);
  1652. o_vtsmcu[3] = (svsp->tefuse[2] >> 23) & GENMASK(8, 0);
  1653. o_vtsmcu[4] = (svsp->tefuse[2] >> 5) & GENMASK(8, 0);
  1654. o_vtsabb = (svsp->tefuse[2] >> 14) & GENMASK(8, 0);
  1655. degc_cali = (svsp->tefuse[0] >> 1) & GENMASK(5, 0);
  1656. adc_cali_en_t = svsp->tefuse[0] & BIT(0);
  1657. o_slope_sign = (svsp->tefuse[0] >> 7) & BIT(0);
  1658. ts_id = (svsp->tefuse[1] >> 9) & BIT(0);
  1659. o_slope = (svsp->tefuse[0] >> 26) & GENMASK(5, 0);
  1660. if (adc_cali_en_t == 1) {
  1661. if (!ts_id)
  1662. o_slope = 0;
  1663. if (adc_ge_t < 265 || adc_ge_t > 758 ||
  1664. adc_oe_t < 265 || adc_oe_t > 758 ||
  1665. o_vtsmcu[0] < -8 || o_vtsmcu[0] > 484 ||
  1666. o_vtsmcu[1] < -8 || o_vtsmcu[1] > 484 ||
  1667. o_vtsmcu[2] < -8 || o_vtsmcu[2] > 484 ||
  1668. o_vtsmcu[3] < -8 || o_vtsmcu[3] > 484 ||
  1669. o_vtsmcu[4] < -8 || o_vtsmcu[4] > 484 ||
  1670. o_vtsabb < -8 || o_vtsabb > 484 ||
  1671. degc_cali < 1 || degc_cali > 63) {
  1672. dev_err(svsp->dev, "bad thermal efuse, no mon mode\n");
  1673. goto remove_mt8183_svsb_mon_mode;
  1674. }
  1675. } else {
  1676. dev_err(svsp->dev, "no thermal efuse, no mon mode\n");
  1677. goto remove_mt8183_svsb_mon_mode;
  1678. }
  1679. ge = ((adc_ge_t - 512) * 10000) / 4096;
  1680. oe = (adc_oe_t - 512);
  1681. gain = (10000 + ge);
  1682. format[0] = (o_vtsmcu[0] + 3350 - oe);
  1683. format[1] = (o_vtsmcu[1] + 3350 - oe);
  1684. format[2] = (o_vtsmcu[2] + 3350 - oe);
  1685. format[3] = (o_vtsmcu[3] + 3350 - oe);
  1686. format[4] = (o_vtsmcu[4] + 3350 - oe);
  1687. format[5] = (o_vtsabb + 3350 - oe);
  1688. for (i = 0; i < 6; i++)
  1689. x_roomt[i] = (((format[i] * 10000) / 4096) * 10000) / gain;
  1690. temp0 = (10000 * 100000 / gain) * 15 / 18;
  1691. if (!o_slope_sign)
  1692. mts = (temp0 * 10) / (1534 + o_slope * 10);
  1693. else
  1694. mts = (temp0 * 10) / (1534 - o_slope * 10);
  1695. for (idx = 0; idx < svsp->bank_max; idx++) {
  1696. svsb = &svsp->banks[idx];
  1697. svsb->mts = mts;
  1698. switch (svsb->sw_id) {
  1699. case SVSB_CPU_LITTLE:
  1700. tb_roomt = x_roomt[3];
  1701. break;
  1702. case SVSB_CPU_BIG:
  1703. tb_roomt = x_roomt[4];
  1704. break;
  1705. case SVSB_CCI:
  1706. tb_roomt = x_roomt[3];
  1707. break;
  1708. case SVSB_GPU:
  1709. tb_roomt = x_roomt[1];
  1710. break;
  1711. default:
  1712. dev_err(svsb->dev, "unknown sw_id: %u\n", svsb->sw_id);
  1713. goto remove_mt8183_svsb_mon_mode;
  1714. }
  1715. temp0 = (degc_cali * 10 / 2);
  1716. temp1 = ((10000 * 100000 / 4096 / gain) *
  1717. oe + tb_roomt * 10) * 15 / 18;
  1718. if (!o_slope_sign)
  1719. temp2 = temp1 * 100 / (1534 + o_slope * 10);
  1720. else
  1721. temp2 = temp1 * 100 / (1534 - o_slope * 10);
  1722. svsb->bts = (temp0 + temp2 - 250) * 4 / 10;
  1723. }
  1724. return true;
  1725. remove_mt8183_svsb_mon_mode:
  1726. for (idx = 0; idx < svsp->bank_max; idx++) {
  1727. svsb = &svsp->banks[idx];
  1728. svsb->mode_support &= ~SVSB_MODE_MON;
  1729. }
  1730. return true;
  1731. }
  1732. static bool svs_is_efuse_data_correct(struct svs_platform *svsp)
  1733. {
  1734. struct nvmem_cell *cell;
  1735. /* Get svs efuse by nvmem */
  1736. cell = nvmem_cell_get(svsp->dev, "svs-calibration-data");
  1737. if (IS_ERR(cell)) {
  1738. dev_err(svsp->dev, "no \"svs-calibration-data\"? %ld\n",
  1739. PTR_ERR(cell));
  1740. return false;
  1741. }
  1742. svsp->efuse = nvmem_cell_read(cell, &svsp->efuse_max);
  1743. if (IS_ERR(svsp->efuse)) {
  1744. dev_err(svsp->dev, "cannot read svs efuse: %ld\n",
  1745. PTR_ERR(svsp->efuse));
  1746. nvmem_cell_put(cell);
  1747. return false;
  1748. }
  1749. svsp->efuse_max /= sizeof(u32);
  1750. nvmem_cell_put(cell);
  1751. return svsp->efuse_parsing(svsp);
  1752. }
  1753. static struct device *svs_get_subsys_device(struct svs_platform *svsp,
  1754. const char *node_name)
  1755. {
  1756. struct platform_device *pdev;
  1757. struct device_node *np;
  1758. np = of_find_node_by_name(NULL, node_name);
  1759. if (!np) {
  1760. dev_err(svsp->dev, "cannot find %s node\n", node_name);
  1761. return ERR_PTR(-ENODEV);
  1762. }
  1763. pdev = of_find_device_by_node(np);
  1764. if (!pdev) {
  1765. of_node_put(np);
  1766. dev_err(svsp->dev, "cannot find pdev by %s\n", node_name);
  1767. return ERR_PTR(-ENXIO);
  1768. }
  1769. of_node_put(np);
  1770. return &pdev->dev;
  1771. }
  1772. static struct device *svs_add_device_link(struct svs_platform *svsp,
  1773. const char *node_name)
  1774. {
  1775. struct device *dev;
  1776. struct device_link *sup_link;
  1777. if (!node_name) {
  1778. dev_err(svsp->dev, "node name cannot be null\n");
  1779. return ERR_PTR(-EINVAL);
  1780. }
  1781. dev = svs_get_subsys_device(svsp, node_name);
  1782. if (IS_ERR(dev))
  1783. return dev;
  1784. sup_link = device_link_add(svsp->dev, dev,
  1785. DL_FLAG_AUTOREMOVE_CONSUMER);
  1786. if (!sup_link) {
  1787. dev_err(svsp->dev, "sup_link is NULL\n");
  1788. return ERR_PTR(-EINVAL);
  1789. }
  1790. if (sup_link->supplier->links.status != DL_DEV_DRIVER_BOUND)
  1791. return ERR_PTR(-EPROBE_DEFER);
  1792. return dev;
  1793. }
  1794. static int svs_mt8192_platform_probe(struct svs_platform *svsp)
  1795. {
  1796. struct device *dev;
  1797. struct svs_bank *svsb;
  1798. u32 idx;
  1799. svsp->rst = devm_reset_control_get_optional(svsp->dev, "svs_rst");
  1800. if (IS_ERR(svsp->rst))
  1801. return dev_err_probe(svsp->dev, PTR_ERR(svsp->rst),
  1802. "cannot get svs reset control\n");
  1803. dev = svs_add_device_link(svsp, "lvts");
  1804. if (IS_ERR(dev))
  1805. return dev_err_probe(svsp->dev, PTR_ERR(dev),
  1806. "failed to get lvts device\n");
  1807. for (idx = 0; idx < svsp->bank_max; idx++) {
  1808. svsb = &svsp->banks[idx];
  1809. if (svsb->type == SVSB_HIGH)
  1810. svsb->opp_dev = svs_add_device_link(svsp, "gpu");
  1811. else if (svsb->type == SVSB_LOW)
  1812. svsb->opp_dev = svs_get_subsys_device(svsp, "gpu");
  1813. if (IS_ERR(svsb->opp_dev))
  1814. return dev_err_probe(svsp->dev, PTR_ERR(svsb->opp_dev),
  1815. "failed to get OPP device for bank %d\n",
  1816. idx);
  1817. }
  1818. return 0;
  1819. }
  1820. static int svs_mt8183_platform_probe(struct svs_platform *svsp)
  1821. {
  1822. struct device *dev;
  1823. struct svs_bank *svsb;
  1824. u32 idx;
  1825. dev = svs_add_device_link(svsp, "thermal");
  1826. if (IS_ERR(dev))
  1827. return dev_err_probe(svsp->dev, PTR_ERR(dev),
  1828. "failed to get thermal device\n");
  1829. for (idx = 0; idx < svsp->bank_max; idx++) {
  1830. svsb = &svsp->banks[idx];
  1831. switch (svsb->sw_id) {
  1832. case SVSB_CPU_LITTLE:
  1833. case SVSB_CPU_BIG:
  1834. svsb->opp_dev = get_cpu_device(svsb->cpu_id);
  1835. break;
  1836. case SVSB_CCI:
  1837. svsb->opp_dev = svs_add_device_link(svsp, "cci");
  1838. break;
  1839. case SVSB_GPU:
  1840. svsb->opp_dev = svs_add_device_link(svsp, "gpu");
  1841. break;
  1842. default:
  1843. dev_err(svsb->dev, "unknown sw_id: %u\n", svsb->sw_id);
  1844. return -EINVAL;
  1845. }
  1846. if (IS_ERR(svsb->opp_dev))
  1847. return dev_err_probe(svsp->dev, PTR_ERR(svsb->opp_dev),
  1848. "failed to get OPP device for bank %d\n",
  1849. idx);
  1850. }
  1851. return 0;
  1852. }
  1853. static struct svs_bank svs_mt8192_banks[] = {
  1854. {
  1855. .sw_id = SVSB_GPU,
  1856. .type = SVSB_LOW,
  1857. .set_freq_pct = svs_set_bank_freq_pct_v3,
  1858. .get_volts = svs_get_bank_volts_v3,
  1859. .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT,
  1860. .mode_support = SVSB_MODE_INIT02,
  1861. .opp_count = MAX_OPP_ENTRIES,
  1862. .freq_base = 688000000,
  1863. .turn_freq_base = 688000000,
  1864. .volt_step = 6250,
  1865. .volt_base = 400000,
  1866. .vmax = 0x60,
  1867. .vmin = 0x1a,
  1868. .age_config = 0x555555,
  1869. .dc_config = 0x1,
  1870. .dvt_fixed = 0x1,
  1871. .vco = 0x18,
  1872. .chk_shift = 0x87,
  1873. .core_sel = 0x0fff0100,
  1874. .int_st = BIT(0),
  1875. .ctl0 = 0x00540003,
  1876. },
  1877. {
  1878. .sw_id = SVSB_GPU,
  1879. .type = SVSB_HIGH,
  1880. .set_freq_pct = svs_set_bank_freq_pct_v3,
  1881. .get_volts = svs_get_bank_volts_v3,
  1882. .tzone_name = "gpu1",
  1883. .volt_flags = SVSB_REMOVE_DVTFIXED_VOLT |
  1884. SVSB_MON_VOLT_IGNORE,
  1885. .mode_support = SVSB_MODE_INIT02 | SVSB_MODE_MON,
  1886. .opp_count = MAX_OPP_ENTRIES,
  1887. .freq_base = 902000000,
  1888. .turn_freq_base = 688000000,
  1889. .volt_step = 6250,
  1890. .volt_base = 400000,
  1891. .vmax = 0x60,
  1892. .vmin = 0x1a,
  1893. .age_config = 0x555555,
  1894. .dc_config = 0x1,
  1895. .dvt_fixed = 0x6,
  1896. .vco = 0x18,
  1897. .chk_shift = 0x87,
  1898. .core_sel = 0x0fff0101,
  1899. .int_st = BIT(1),
  1900. .ctl0 = 0x00540003,
  1901. .tzone_htemp = 85000,
  1902. .tzone_htemp_voffset = 0,
  1903. .tzone_ltemp = 25000,
  1904. .tzone_ltemp_voffset = 7,
  1905. },
  1906. };
  1907. static struct svs_bank svs_mt8183_banks[] = {
  1908. {
  1909. .sw_id = SVSB_CPU_LITTLE,
  1910. .set_freq_pct = svs_set_bank_freq_pct_v2,
  1911. .get_volts = svs_get_bank_volts_v2,
  1912. .cpu_id = 0,
  1913. .buck_name = "proc",
  1914. .volt_flags = SVSB_INIT01_VOLT_INC_ONLY,
  1915. .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02,
  1916. .opp_count = MAX_OPP_ENTRIES,
  1917. .freq_base = 1989000000,
  1918. .vboot = 0x30,
  1919. .volt_step = 6250,
  1920. .volt_base = 500000,
  1921. .vmax = 0x64,
  1922. .vmin = 0x18,
  1923. .age_config = 0x555555,
  1924. .dc_config = 0x555555,
  1925. .dvt_fixed = 0x7,
  1926. .vco = 0x10,
  1927. .chk_shift = 0x77,
  1928. .core_sel = 0x8fff0000,
  1929. .int_st = BIT(0),
  1930. .ctl0 = 0x00010001,
  1931. },
  1932. {
  1933. .sw_id = SVSB_CPU_BIG,
  1934. .set_freq_pct = svs_set_bank_freq_pct_v2,
  1935. .get_volts = svs_get_bank_volts_v2,
  1936. .cpu_id = 4,
  1937. .buck_name = "proc",
  1938. .volt_flags = SVSB_INIT01_VOLT_INC_ONLY,
  1939. .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02,
  1940. .opp_count = MAX_OPP_ENTRIES,
  1941. .freq_base = 1989000000,
  1942. .vboot = 0x30,
  1943. .volt_step = 6250,
  1944. .volt_base = 500000,
  1945. .vmax = 0x58,
  1946. .vmin = 0x10,
  1947. .age_config = 0x555555,
  1948. .dc_config = 0x555555,
  1949. .dvt_fixed = 0x7,
  1950. .vco = 0x10,
  1951. .chk_shift = 0x77,
  1952. .core_sel = 0x8fff0001,
  1953. .int_st = BIT(1),
  1954. .ctl0 = 0x00000001,
  1955. },
  1956. {
  1957. .sw_id = SVSB_CCI,
  1958. .set_freq_pct = svs_set_bank_freq_pct_v2,
  1959. .get_volts = svs_get_bank_volts_v2,
  1960. .buck_name = "proc",
  1961. .volt_flags = SVSB_INIT01_VOLT_INC_ONLY,
  1962. .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02,
  1963. .opp_count = MAX_OPP_ENTRIES,
  1964. .freq_base = 1196000000,
  1965. .vboot = 0x30,
  1966. .volt_step = 6250,
  1967. .volt_base = 500000,
  1968. .vmax = 0x64,
  1969. .vmin = 0x18,
  1970. .age_config = 0x555555,
  1971. .dc_config = 0x555555,
  1972. .dvt_fixed = 0x7,
  1973. .vco = 0x10,
  1974. .chk_shift = 0x77,
  1975. .core_sel = 0x8fff0002,
  1976. .int_st = BIT(2),
  1977. .ctl0 = 0x00100003,
  1978. },
  1979. {
  1980. .sw_id = SVSB_GPU,
  1981. .set_freq_pct = svs_set_bank_freq_pct_v2,
  1982. .get_volts = svs_get_bank_volts_v2,
  1983. .buck_name = "mali",
  1984. .tzone_name = "tzts2",
  1985. .volt_flags = SVSB_INIT01_PD_REQ |
  1986. SVSB_INIT01_VOLT_INC_ONLY,
  1987. .mode_support = SVSB_MODE_INIT01 | SVSB_MODE_INIT02 |
  1988. SVSB_MODE_MON,
  1989. .opp_count = MAX_OPP_ENTRIES,
  1990. .freq_base = 900000000,
  1991. .vboot = 0x30,
  1992. .volt_step = 6250,
  1993. .volt_base = 500000,
  1994. .vmax = 0x40,
  1995. .vmin = 0x14,
  1996. .age_config = 0x555555,
  1997. .dc_config = 0x555555,
  1998. .dvt_fixed = 0x3,
  1999. .vco = 0x10,
  2000. .chk_shift = 0x77,
  2001. .core_sel = 0x8fff0003,
  2002. .int_st = BIT(3),
  2003. .ctl0 = 0x00050001,
  2004. .tzone_htemp = 85000,
  2005. .tzone_htemp_voffset = 0,
  2006. .tzone_ltemp = 25000,
  2007. .tzone_ltemp_voffset = 3,
  2008. },
  2009. };
  2010. static const struct svs_platform_data svs_mt8192_platform_data = {
  2011. .name = "mt8192-svs",
  2012. .banks = svs_mt8192_banks,
  2013. .efuse_parsing = svs_mt8192_efuse_parsing,
  2014. .probe = svs_mt8192_platform_probe,
  2015. .regs = svs_regs_v2,
  2016. .bank_max = ARRAY_SIZE(svs_mt8192_banks),
  2017. };
  2018. static const struct svs_platform_data svs_mt8183_platform_data = {
  2019. .name = "mt8183-svs",
  2020. .banks = svs_mt8183_banks,
  2021. .efuse_parsing = svs_mt8183_efuse_parsing,
  2022. .probe = svs_mt8183_platform_probe,
  2023. .regs = svs_regs_v2,
  2024. .bank_max = ARRAY_SIZE(svs_mt8183_banks),
  2025. };
  2026. static const struct of_device_id svs_of_match[] = {
  2027. {
  2028. .compatible = "mediatek,mt8192-svs",
  2029. .data = &svs_mt8192_platform_data,
  2030. }, {
  2031. .compatible = "mediatek,mt8183-svs",
  2032. .data = &svs_mt8183_platform_data,
  2033. }, {
  2034. /* Sentinel */
  2035. },
  2036. };
  2037. static struct svs_platform *svs_platform_probe(struct platform_device *pdev)
  2038. {
  2039. struct svs_platform *svsp;
  2040. const struct svs_platform_data *svsp_data;
  2041. int ret;
  2042. svsp_data = of_device_get_match_data(&pdev->dev);
  2043. if (!svsp_data) {
  2044. dev_err(&pdev->dev, "no svs platform data?\n");
  2045. return ERR_PTR(-EPERM);
  2046. }
  2047. svsp = devm_kzalloc(&pdev->dev, sizeof(*svsp), GFP_KERNEL);
  2048. if (!svsp)
  2049. return ERR_PTR(-ENOMEM);
  2050. svsp->dev = &pdev->dev;
  2051. svsp->name = svsp_data->name;
  2052. svsp->banks = svsp_data->banks;
  2053. svsp->efuse_parsing = svsp_data->efuse_parsing;
  2054. svsp->probe = svsp_data->probe;
  2055. svsp->regs = svsp_data->regs;
  2056. svsp->bank_max = svsp_data->bank_max;
  2057. ret = svsp->probe(svsp);
  2058. if (ret)
  2059. return ERR_PTR(ret);
  2060. return svsp;
  2061. }
  2062. static int svs_probe(struct platform_device *pdev)
  2063. {
  2064. struct svs_platform *svsp;
  2065. int svsp_irq, ret;
  2066. svsp = svs_platform_probe(pdev);
  2067. if (IS_ERR(svsp))
  2068. return PTR_ERR(svsp);
  2069. if (!svs_is_efuse_data_correct(svsp)) {
  2070. dev_notice(svsp->dev, "efuse data isn't correct\n");
  2071. ret = -EPERM;
  2072. goto svs_probe_free_resource;
  2073. }
  2074. ret = svs_bank_resource_setup(svsp);
  2075. if (ret) {
  2076. dev_err(svsp->dev, "svs bank resource setup fail: %d\n", ret);
  2077. goto svs_probe_free_resource;
  2078. }
  2079. svsp_irq = platform_get_irq(pdev, 0);
  2080. if (svsp_irq < 0) {
  2081. ret = svsp_irq;
  2082. goto svs_probe_free_resource;
  2083. }
  2084. svsp->main_clk = devm_clk_get(svsp->dev, "main");
  2085. if (IS_ERR(svsp->main_clk)) {
  2086. dev_err(svsp->dev, "failed to get clock: %ld\n",
  2087. PTR_ERR(svsp->main_clk));
  2088. ret = PTR_ERR(svsp->main_clk);
  2089. goto svs_probe_free_resource;
  2090. }
  2091. ret = clk_prepare_enable(svsp->main_clk);
  2092. if (ret) {
  2093. dev_err(svsp->dev, "cannot enable main clk: %d\n", ret);
  2094. goto svs_probe_free_resource;
  2095. }
  2096. svsp->base = of_iomap(svsp->dev->of_node, 0);
  2097. if (IS_ERR_OR_NULL(svsp->base)) {
  2098. dev_err(svsp->dev, "cannot find svs register base\n");
  2099. ret = -EINVAL;
  2100. goto svs_probe_clk_disable;
  2101. }
  2102. ret = devm_request_threaded_irq(svsp->dev, svsp_irq, NULL, svs_isr,
  2103. IRQF_ONESHOT, svsp->name, svsp);
  2104. if (ret) {
  2105. dev_err(svsp->dev, "register irq(%d) failed: %d\n",
  2106. svsp_irq, ret);
  2107. goto svs_probe_iounmap;
  2108. }
  2109. ret = svs_start(svsp);
  2110. if (ret) {
  2111. dev_err(svsp->dev, "svs start fail: %d\n", ret);
  2112. goto svs_probe_iounmap;
  2113. }
  2114. #ifdef CONFIG_DEBUG_FS
  2115. ret = svs_create_debug_cmds(svsp);
  2116. if (ret) {
  2117. dev_err(svsp->dev, "svs create debug cmds fail: %d\n", ret);
  2118. goto svs_probe_iounmap;
  2119. }
  2120. #endif
  2121. return 0;
  2122. svs_probe_iounmap:
  2123. iounmap(svsp->base);
  2124. svs_probe_clk_disable:
  2125. clk_disable_unprepare(svsp->main_clk);
  2126. svs_probe_free_resource:
  2127. if (!IS_ERR_OR_NULL(svsp->efuse))
  2128. kfree(svsp->efuse);
  2129. if (!IS_ERR_OR_NULL(svsp->tefuse))
  2130. kfree(svsp->tefuse);
  2131. return ret;
  2132. }
  2133. static DEFINE_SIMPLE_DEV_PM_OPS(svs_pm_ops, svs_suspend, svs_resume);
  2134. static struct platform_driver svs_driver = {
  2135. .probe = svs_probe,
  2136. .driver = {
  2137. .name = "mtk-svs",
  2138. .pm = &svs_pm_ops,
  2139. .of_match_table = svs_of_match,
  2140. },
  2141. };
  2142. module_platform_driver(svs_driver);
  2143. MODULE_AUTHOR("Roger Lu <[email protected]>");
  2144. MODULE_DESCRIPTION("MediaTek SVS driver");
  2145. MODULE_LICENSE("GPL");