830-v6.3-05-thermal-drivers-mediatek-Relocate-driver-to-mediatek.patch 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602
  1. From 5e3aac197a74914ccec2732a89c29d960730d28f Mon Sep 17 00:00:00 2001
  2. From: Balsam CHIHI <[email protected]>
  3. Date: Thu, 9 Feb 2023 11:56:23 +0100
  4. Subject: [PATCH 05/42] thermal/drivers/mediatek: Relocate driver to mediatek
  5. folder
  6. Add MediaTek proprietary folder to upstream more thermal zone and cooler
  7. drivers, relocate the original thermal controller driver to it, and rename it
  8. as "auxadc_thermal.c" to show its purpose more clearly.
  9. Signed-off-by: Balsam CHIHI <[email protected]>
  10. Reviewed-by: AngeloGioacchino Del Regno <[email protected]>
  11. Link: https://lore.kernel.org/r/[email protected]
  12. Signed-off-by: Daniel Lezcano <[email protected]>
  13. Signed-off-by: Rafael J. Wysocki <[email protected]>
  14. ---
  15. drivers/thermal/Kconfig | 14 ++++---------
  16. drivers/thermal/Makefile | 2 +-
  17. drivers/thermal/mediatek/Kconfig | 21 +++++++++++++++++++
  18. drivers/thermal/mediatek/Makefile | 1 +
  19. .../auxadc_thermal.c} | 2 +-
  20. 5 files changed, 28 insertions(+), 12 deletions(-)
  21. create mode 100644 drivers/thermal/mediatek/Kconfig
  22. create mode 100644 drivers/thermal/mediatek/Makefile
  23. rename drivers/thermal/{mtk_thermal.c => mediatek/auxadc_thermal.c} (99%)
  24. --- a/drivers/thermal/Kconfig
  25. +++ b/drivers/thermal/Kconfig
  26. @@ -412,16 +412,10 @@ config DA9062_THERMAL
  27. zone.
  28. Compatible with the DA9062 and DA9061 PMICs.
  29. -config MTK_THERMAL
  30. - tristate "Temperature sensor driver for mediatek SoCs"
  31. - depends on ARCH_MEDIATEK || COMPILE_TEST
  32. - depends on HAS_IOMEM
  33. - depends on NVMEM || NVMEM=n
  34. - depends on RESET_CONTROLLER
  35. - default y
  36. - help
  37. - Enable this option if you want to have support for thermal management
  38. - controller present in Mediatek SoCs
  39. +menu "Mediatek thermal drivers"
  40. +depends on ARCH_MEDIATEK || COMPILE_TEST
  41. +source "drivers/thermal/mediatek/Kconfig"
  42. +endmenu
  43. config AMLOGIC_THERMAL
  44. tristate "Amlogic Thermal Support"
  45. --- a/drivers/thermal/Makefile
  46. +++ b/drivers/thermal/Makefile
  47. @@ -55,7 +55,7 @@ obj-y += st/
  48. obj-y += qcom/
  49. obj-y += tegra/
  50. obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
  51. -obj-$(CONFIG_MTK_THERMAL) += mtk_thermal.o
  52. +obj-y += mediatek/
  53. obj-$(CONFIG_GENERIC_ADC_THERMAL) += thermal-generic-adc.o
  54. obj-$(CONFIG_UNIPHIER_THERMAL) += uniphier_thermal.o
  55. obj-$(CONFIG_AMLOGIC_THERMAL) += amlogic_thermal.o
  56. --- /dev/null
  57. +++ b/drivers/thermal/mediatek/Kconfig
  58. @@ -0,0 +1,21 @@
  59. +config MTK_THERMAL
  60. + tristate "MediaTek thermal drivers"
  61. + depends on THERMAL_OF
  62. + help
  63. + This is the option for MediaTek thermal software solutions.
  64. + Please enable corresponding options to get temperature
  65. + information from thermal sensors or turn on throttle
  66. + mechaisms for thermal mitigation.
  67. +
  68. +if MTK_THERMAL
  69. +
  70. +config MTK_SOC_THERMAL
  71. + tristate "AUXADC temperature sensor driver for MediaTek SoCs"
  72. + depends on HAS_IOMEM
  73. + help
  74. + Enable this option if you want to get SoC temperature
  75. + information for MediaTek platforms.
  76. + This driver configures thermal controllers to collect
  77. + temperature via AUXADC interface.
  78. +
  79. +endif
  80. --- /dev/null
  81. +++ b/drivers/thermal/mediatek/Makefile
  82. @@ -0,0 +1 @@
  83. +obj-$(CONFIG_MTK_SOC_THERMAL) += auxadc_thermal.o
  84. --- a/drivers/thermal/mtk_thermal.c
  85. +++ /dev/null
  86. @@ -1,1254 +0,0 @@
  87. -// SPDX-License-Identifier: GPL-2.0-only
  88. -/*
  89. - * Copyright (c) 2015 MediaTek Inc.
  90. - * Author: Hanyi Wu <[email protected]>
  91. - * Sascha Hauer <[email protected]>
  92. - * Dawei Chien <[email protected]>
  93. - * Louis Yu <[email protected]>
  94. - */
  95. -
  96. -#include <linux/clk.h>
  97. -#include <linux/delay.h>
  98. -#include <linux/interrupt.h>
  99. -#include <linux/kernel.h>
  100. -#include <linux/module.h>
  101. -#include <linux/nvmem-consumer.h>
  102. -#include <linux/of.h>
  103. -#include <linux/of_address.h>
  104. -#include <linux/of_device.h>
  105. -#include <linux/platform_device.h>
  106. -#include <linux/slab.h>
  107. -#include <linux/io.h>
  108. -#include <linux/thermal.h>
  109. -#include <linux/reset.h>
  110. -#include <linux/types.h>
  111. -
  112. -#include "thermal_hwmon.h"
  113. -
  114. -/* AUXADC Registers */
  115. -#define AUXADC_CON1_SET_V 0x008
  116. -#define AUXADC_CON1_CLR_V 0x00c
  117. -#define AUXADC_CON2_V 0x010
  118. -#define AUXADC_DATA(channel) (0x14 + (channel) * 4)
  119. -
  120. -#define APMIXED_SYS_TS_CON1 0x604
  121. -
  122. -/* Thermal Controller Registers */
  123. -#define TEMP_MONCTL0 0x000
  124. -#define TEMP_MONCTL1 0x004
  125. -#define TEMP_MONCTL2 0x008
  126. -#define TEMP_MONIDET0 0x014
  127. -#define TEMP_MONIDET1 0x018
  128. -#define TEMP_MSRCTL0 0x038
  129. -#define TEMP_MSRCTL1 0x03c
  130. -#define TEMP_AHBPOLL 0x040
  131. -#define TEMP_AHBTO 0x044
  132. -#define TEMP_ADCPNP0 0x048
  133. -#define TEMP_ADCPNP1 0x04c
  134. -#define TEMP_ADCPNP2 0x050
  135. -#define TEMP_ADCPNP3 0x0b4
  136. -
  137. -#define TEMP_ADCMUX 0x054
  138. -#define TEMP_ADCEN 0x060
  139. -#define TEMP_PNPMUXADDR 0x064
  140. -#define TEMP_ADCMUXADDR 0x068
  141. -#define TEMP_ADCENADDR 0x074
  142. -#define TEMP_ADCVALIDADDR 0x078
  143. -#define TEMP_ADCVOLTADDR 0x07c
  144. -#define TEMP_RDCTRL 0x080
  145. -#define TEMP_ADCVALIDMASK 0x084
  146. -#define TEMP_ADCVOLTAGESHIFT 0x088
  147. -#define TEMP_ADCWRITECTRL 0x08c
  148. -#define TEMP_MSR0 0x090
  149. -#define TEMP_MSR1 0x094
  150. -#define TEMP_MSR2 0x098
  151. -#define TEMP_MSR3 0x0B8
  152. -
  153. -#define TEMP_SPARE0 0x0f0
  154. -
  155. -#define TEMP_ADCPNP0_1 0x148
  156. -#define TEMP_ADCPNP1_1 0x14c
  157. -#define TEMP_ADCPNP2_1 0x150
  158. -#define TEMP_MSR0_1 0x190
  159. -#define TEMP_MSR1_1 0x194
  160. -#define TEMP_MSR2_1 0x198
  161. -#define TEMP_ADCPNP3_1 0x1b4
  162. -#define TEMP_MSR3_1 0x1B8
  163. -
  164. -#define PTPCORESEL 0x400
  165. -
  166. -#define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
  167. -
  168. -#define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
  169. -#define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
  170. -
  171. -#define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
  172. -
  173. -#define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
  174. -#define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
  175. -
  176. -#define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
  177. -#define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
  178. -
  179. -/* MT8173 thermal sensors */
  180. -#define MT8173_TS1 0
  181. -#define MT8173_TS2 1
  182. -#define MT8173_TS3 2
  183. -#define MT8173_TS4 3
  184. -#define MT8173_TSABB 4
  185. -
  186. -/* AUXADC channel 11 is used for the temperature sensors */
  187. -#define MT8173_TEMP_AUXADC_CHANNEL 11
  188. -
  189. -/* The total number of temperature sensors in the MT8173 */
  190. -#define MT8173_NUM_SENSORS 5
  191. -
  192. -/* The number of banks in the MT8173 */
  193. -#define MT8173_NUM_ZONES 4
  194. -
  195. -/* The number of sensing points per bank */
  196. -#define MT8173_NUM_SENSORS_PER_ZONE 4
  197. -
  198. -/* The number of controller in the MT8173 */
  199. -#define MT8173_NUM_CONTROLLER 1
  200. -
  201. -/* The calibration coefficient of sensor */
  202. -#define MT8173_CALIBRATION 165
  203. -
  204. -/*
  205. - * Layout of the fuses providing the calibration data
  206. - * These macros could be used for MT8183, MT8173, MT2701, and MT2712.
  207. - * MT8183 has 6 sensors and needs 6 VTS calibration data.
  208. - * MT8173 has 5 sensors and needs 5 VTS calibration data.
  209. - * MT2701 has 3 sensors and needs 3 VTS calibration data.
  210. - * MT2712 has 4 sensors and needs 4 VTS calibration data.
  211. - */
  212. -#define CALIB_BUF0_VALID_V1 BIT(0)
  213. -#define CALIB_BUF1_ADC_GE_V1(x) (((x) >> 22) & 0x3ff)
  214. -#define CALIB_BUF0_VTS_TS1_V1(x) (((x) >> 17) & 0x1ff)
  215. -#define CALIB_BUF0_VTS_TS2_V1(x) (((x) >> 8) & 0x1ff)
  216. -#define CALIB_BUF1_VTS_TS3_V1(x) (((x) >> 0) & 0x1ff)
  217. -#define CALIB_BUF2_VTS_TS4_V1(x) (((x) >> 23) & 0x1ff)
  218. -#define CALIB_BUF2_VTS_TS5_V1(x) (((x) >> 5) & 0x1ff)
  219. -#define CALIB_BUF2_VTS_TSABB_V1(x) (((x) >> 14) & 0x1ff)
  220. -#define CALIB_BUF0_DEGC_CALI_V1(x) (((x) >> 1) & 0x3f)
  221. -#define CALIB_BUF0_O_SLOPE_V1(x) (((x) >> 26) & 0x3f)
  222. -#define CALIB_BUF0_O_SLOPE_SIGN_V1(x) (((x) >> 7) & 0x1)
  223. -#define CALIB_BUF1_ID_V1(x) (((x) >> 9) & 0x1)
  224. -
  225. -/*
  226. - * Layout of the fuses providing the calibration data
  227. - * These macros could be used for MT7622.
  228. - */
  229. -#define CALIB_BUF0_ADC_OE_V2(x) (((x) >> 22) & 0x3ff)
  230. -#define CALIB_BUF0_ADC_GE_V2(x) (((x) >> 12) & 0x3ff)
  231. -#define CALIB_BUF0_DEGC_CALI_V2(x) (((x) >> 6) & 0x3f)
  232. -#define CALIB_BUF0_O_SLOPE_V2(x) (((x) >> 0) & 0x3f)
  233. -#define CALIB_BUF1_VTS_TS1_V2(x) (((x) >> 23) & 0x1ff)
  234. -#define CALIB_BUF1_VTS_TS2_V2(x) (((x) >> 14) & 0x1ff)
  235. -#define CALIB_BUF1_VTS_TSABB_V2(x) (((x) >> 5) & 0x1ff)
  236. -#define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1)
  237. -#define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1)
  238. -
  239. -/*
  240. - * Layout of the fuses providing the calibration data
  241. - * These macros can be used for MT7981 and MT7986.
  242. - */
  243. -#define CALIB_BUF0_ADC_GE_V3(x) (((x) >> 0) & 0x3ff)
  244. -#define CALIB_BUF0_DEGC_CALI_V3(x) (((x) >> 20) & 0x3f)
  245. -#define CALIB_BUF0_O_SLOPE_V3(x) (((x) >> 26) & 0x3f)
  246. -#define CALIB_BUF1_VTS_TS1_V3(x) (((x) >> 0) & 0x1ff)
  247. -#define CALIB_BUF1_VTS_TS2_V3(x) (((x) >> 21) & 0x1ff)
  248. -#define CALIB_BUF1_VTS_TSABB_V3(x) (((x) >> 9) & 0x1ff)
  249. -#define CALIB_BUF1_VALID_V3(x) (((x) >> 18) & 0x1)
  250. -#define CALIB_BUF1_O_SLOPE_SIGN_V3(x) (((x) >> 19) & 0x1)
  251. -#define CALIB_BUF1_ID_V3(x) (((x) >> 20) & 0x1)
  252. -
  253. -enum {
  254. - VTS1,
  255. - VTS2,
  256. - VTS3,
  257. - VTS4,
  258. - VTS5,
  259. - VTSABB,
  260. - MAX_NUM_VTS,
  261. -};
  262. -
  263. -enum mtk_thermal_version {
  264. - MTK_THERMAL_V1 = 1,
  265. - MTK_THERMAL_V2,
  266. - MTK_THERMAL_V3,
  267. -};
  268. -
  269. -/* MT2701 thermal sensors */
  270. -#define MT2701_TS1 0
  271. -#define MT2701_TS2 1
  272. -#define MT2701_TSABB 2
  273. -
  274. -/* AUXADC channel 11 is used for the temperature sensors */
  275. -#define MT2701_TEMP_AUXADC_CHANNEL 11
  276. -
  277. -/* The total number of temperature sensors in the MT2701 */
  278. -#define MT2701_NUM_SENSORS 3
  279. -
  280. -/* The number of sensing points per bank */
  281. -#define MT2701_NUM_SENSORS_PER_ZONE 3
  282. -
  283. -/* The number of controller in the MT2701 */
  284. -#define MT2701_NUM_CONTROLLER 1
  285. -
  286. -/* The calibration coefficient of sensor */
  287. -#define MT2701_CALIBRATION 165
  288. -
  289. -/* MT2712 thermal sensors */
  290. -#define MT2712_TS1 0
  291. -#define MT2712_TS2 1
  292. -#define MT2712_TS3 2
  293. -#define MT2712_TS4 3
  294. -
  295. -/* AUXADC channel 11 is used for the temperature sensors */
  296. -#define MT2712_TEMP_AUXADC_CHANNEL 11
  297. -
  298. -/* The total number of temperature sensors in the MT2712 */
  299. -#define MT2712_NUM_SENSORS 4
  300. -
  301. -/* The number of sensing points per bank */
  302. -#define MT2712_NUM_SENSORS_PER_ZONE 4
  303. -
  304. -/* The number of controller in the MT2712 */
  305. -#define MT2712_NUM_CONTROLLER 1
  306. -
  307. -/* The calibration coefficient of sensor */
  308. -#define MT2712_CALIBRATION 165
  309. -
  310. -#define MT7622_TEMP_AUXADC_CHANNEL 11
  311. -#define MT7622_NUM_SENSORS 1
  312. -#define MT7622_NUM_ZONES 1
  313. -#define MT7622_NUM_SENSORS_PER_ZONE 1
  314. -#define MT7622_TS1 0
  315. -#define MT7622_NUM_CONTROLLER 1
  316. -
  317. -/* The maximum number of banks */
  318. -#define MAX_NUM_ZONES 8
  319. -
  320. -/* The calibration coefficient of sensor */
  321. -#define MT7622_CALIBRATION 165
  322. -
  323. -/* MT8183 thermal sensors */
  324. -#define MT8183_TS1 0
  325. -#define MT8183_TS2 1
  326. -#define MT8183_TS3 2
  327. -#define MT8183_TS4 3
  328. -#define MT8183_TS5 4
  329. -#define MT8183_TSABB 5
  330. -
  331. -/* AUXADC channel is used for the temperature sensors */
  332. -#define MT8183_TEMP_AUXADC_CHANNEL 11
  333. -
  334. -/* The total number of temperature sensors in the MT8183 */
  335. -#define MT8183_NUM_SENSORS 6
  336. -
  337. -/* The number of banks in the MT8183 */
  338. -#define MT8183_NUM_ZONES 1
  339. -
  340. -/* The number of sensing points per bank */
  341. -#define MT8183_NUM_SENSORS_PER_ZONE 6
  342. -
  343. -/* The number of controller in the MT8183 */
  344. -#define MT8183_NUM_CONTROLLER 2
  345. -
  346. -/* The calibration coefficient of sensor */
  347. -#define MT8183_CALIBRATION 153
  348. -
  349. -/* AUXADC channel 11 is used for the temperature sensors */
  350. -#define MT7986_TEMP_AUXADC_CHANNEL 11
  351. -
  352. -/* The total number of temperature sensors in the MT7986 */
  353. -#define MT7986_NUM_SENSORS 1
  354. -
  355. -/* The number of banks in the MT7986 */
  356. -#define MT7986_NUM_ZONES 1
  357. -
  358. -/* The number of sensing points per bank */
  359. -#define MT7986_NUM_SENSORS_PER_ZONE 1
  360. -
  361. -/* MT7986 thermal sensors */
  362. -#define MT7986_TS1 0
  363. -
  364. -/* The number of controller in the MT7986 */
  365. -#define MT7986_NUM_CONTROLLER 1
  366. -
  367. -/* The calibration coefficient of sensor */
  368. -#define MT7986_CALIBRATION 165
  369. -
  370. -struct mtk_thermal;
  371. -
  372. -struct thermal_bank_cfg {
  373. - unsigned int num_sensors;
  374. - const int *sensors;
  375. -};
  376. -
  377. -struct mtk_thermal_bank {
  378. - struct mtk_thermal *mt;
  379. - int id;
  380. -};
  381. -
  382. -struct mtk_thermal_data {
  383. - s32 num_banks;
  384. - s32 num_sensors;
  385. - s32 auxadc_channel;
  386. - const int *vts_index;
  387. - const int *sensor_mux_values;
  388. - const int *msr;
  389. - const int *adcpnp;
  390. - const int cali_val;
  391. - const int num_controller;
  392. - const int *controller_offset;
  393. - bool need_switch_bank;
  394. - struct thermal_bank_cfg bank_data[MAX_NUM_ZONES];
  395. - enum mtk_thermal_version version;
  396. -};
  397. -
  398. -struct mtk_thermal {
  399. - struct device *dev;
  400. - void __iomem *thermal_base;
  401. -
  402. - struct clk *clk_peri_therm;
  403. - struct clk *clk_auxadc;
  404. - /* lock: for getting and putting banks */
  405. - struct mutex lock;
  406. -
  407. - /* Calibration values */
  408. - s32 adc_ge;
  409. - s32 adc_oe;
  410. - s32 degc_cali;
  411. - s32 o_slope;
  412. - s32 o_slope_sign;
  413. - s32 vts[MAX_NUM_VTS];
  414. -
  415. - const struct mtk_thermal_data *conf;
  416. - struct mtk_thermal_bank banks[MAX_NUM_ZONES];
  417. -
  418. - int (*raw_to_mcelsius)(struct mtk_thermal *mt, int sensno, s32 raw);
  419. -};
  420. -
  421. -/* MT8183 thermal sensor data */
  422. -static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
  423. - MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
  424. -};
  425. -
  426. -static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
  427. - TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
  428. -};
  429. -
  430. -static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
  431. - TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
  432. - TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
  433. -};
  434. -
  435. -static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
  436. -static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
  437. -
  438. -static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
  439. - VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
  440. -};
  441. -
  442. -/* MT8173 thermal sensor data */
  443. -static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
  444. - { MT8173_TS2, MT8173_TS3 },
  445. - { MT8173_TS2, MT8173_TS4 },
  446. - { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
  447. - { MT8173_TS2 },
  448. -};
  449. -
  450. -static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
  451. - TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
  452. -};
  453. -
  454. -static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
  455. - TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
  456. -};
  457. -
  458. -static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
  459. -static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
  460. -
  461. -static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
  462. - VTS1, VTS2, VTS3, VTS4, VTSABB
  463. -};
  464. -
  465. -/* MT2701 thermal sensor data */
  466. -static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
  467. - MT2701_TS1, MT2701_TS2, MT2701_TSABB
  468. -};
  469. -
  470. -static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
  471. - TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
  472. -};
  473. -
  474. -static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
  475. - TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
  476. -};
  477. -
  478. -static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
  479. -static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
  480. -
  481. -static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
  482. - VTS1, VTS2, VTS3
  483. -};
  484. -
  485. -/* MT2712 thermal sensor data */
  486. -static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
  487. - MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
  488. -};
  489. -
  490. -static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
  491. - TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
  492. -};
  493. -
  494. -static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
  495. - TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
  496. -};
  497. -
  498. -static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
  499. -static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
  500. -
  501. -static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
  502. - VTS1, VTS2, VTS3, VTS4
  503. -};
  504. -
  505. -/* MT7622 thermal sensor data */
  506. -static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
  507. -static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
  508. -static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
  509. -static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
  510. -static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
  511. -static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
  512. -
  513. -/* MT7986 thermal sensor data */
  514. -static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, };
  515. -static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
  516. -static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
  517. -static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, };
  518. -static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 };
  519. -static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, };
  520. -
  521. -/*
  522. - * The MT8173 thermal controller has four banks. Each bank can read up to
  523. - * four temperature sensors simultaneously. The MT8173 has a total of 5
  524. - * temperature sensors. We use each bank to measure a certain area of the
  525. - * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
  526. - * areas, hence is used in different banks.
  527. - *
  528. - * The thermal core only gets the maximum temperature of all banks, so
  529. - * the bank concept wouldn't be necessary here. However, the SVS (Smart
  530. - * Voltage Scaling) unit makes its decisions based on the same bank
  531. - * data, and this indeed needs the temperatures of the individual banks
  532. - * for making better decisions.
  533. - */
  534. -static const struct mtk_thermal_data mt8173_thermal_data = {
  535. - .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
  536. - .num_banks = MT8173_NUM_ZONES,
  537. - .num_sensors = MT8173_NUM_SENSORS,
  538. - .vts_index = mt8173_vts_index,
  539. - .cali_val = MT8173_CALIBRATION,
  540. - .num_controller = MT8173_NUM_CONTROLLER,
  541. - .controller_offset = mt8173_tc_offset,
  542. - .need_switch_bank = true,
  543. - .bank_data = {
  544. - {
  545. - .num_sensors = 2,
  546. - .sensors = mt8173_bank_data[0],
  547. - }, {
  548. - .num_sensors = 2,
  549. - .sensors = mt8173_bank_data[1],
  550. - }, {
  551. - .num_sensors = 3,
  552. - .sensors = mt8173_bank_data[2],
  553. - }, {
  554. - .num_sensors = 1,
  555. - .sensors = mt8173_bank_data[3],
  556. - },
  557. - },
  558. - .msr = mt8173_msr,
  559. - .adcpnp = mt8173_adcpnp,
  560. - .sensor_mux_values = mt8173_mux_values,
  561. - .version = MTK_THERMAL_V1,
  562. -};
  563. -
  564. -/*
  565. - * The MT2701 thermal controller has one bank, which can read up to
  566. - * three temperature sensors simultaneously. The MT2701 has a total of 3
  567. - * temperature sensors.
  568. - *
  569. - * The thermal core only gets the maximum temperature of this one bank,
  570. - * so the bank concept wouldn't be necessary here. However, the SVS (Smart
  571. - * Voltage Scaling) unit makes its decisions based on the same bank
  572. - * data.
  573. - */
  574. -static const struct mtk_thermal_data mt2701_thermal_data = {
  575. - .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
  576. - .num_banks = 1,
  577. - .num_sensors = MT2701_NUM_SENSORS,
  578. - .vts_index = mt2701_vts_index,
  579. - .cali_val = MT2701_CALIBRATION,
  580. - .num_controller = MT2701_NUM_CONTROLLER,
  581. - .controller_offset = mt2701_tc_offset,
  582. - .need_switch_bank = true,
  583. - .bank_data = {
  584. - {
  585. - .num_sensors = 3,
  586. - .sensors = mt2701_bank_data,
  587. - },
  588. - },
  589. - .msr = mt2701_msr,
  590. - .adcpnp = mt2701_adcpnp,
  591. - .sensor_mux_values = mt2701_mux_values,
  592. - .version = MTK_THERMAL_V1,
  593. -};
  594. -
  595. -/*
  596. - * The MT2712 thermal controller has one bank, which can read up to
  597. - * four temperature sensors simultaneously. The MT2712 has a total of 4
  598. - * temperature sensors.
  599. - *
  600. - * The thermal core only gets the maximum temperature of this one bank,
  601. - * so the bank concept wouldn't be necessary here. However, the SVS (Smart
  602. - * Voltage Scaling) unit makes its decisions based on the same bank
  603. - * data.
  604. - */
  605. -static const struct mtk_thermal_data mt2712_thermal_data = {
  606. - .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
  607. - .num_banks = 1,
  608. - .num_sensors = MT2712_NUM_SENSORS,
  609. - .vts_index = mt2712_vts_index,
  610. - .cali_val = MT2712_CALIBRATION,
  611. - .num_controller = MT2712_NUM_CONTROLLER,
  612. - .controller_offset = mt2712_tc_offset,
  613. - .need_switch_bank = true,
  614. - .bank_data = {
  615. - {
  616. - .num_sensors = 4,
  617. - .sensors = mt2712_bank_data,
  618. - },
  619. - },
  620. - .msr = mt2712_msr,
  621. - .adcpnp = mt2712_adcpnp,
  622. - .sensor_mux_values = mt2712_mux_values,
  623. - .version = MTK_THERMAL_V1,
  624. -};
  625. -
  626. -/*
  627. - * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
  628. - * access.
  629. - */
  630. -static const struct mtk_thermal_data mt7622_thermal_data = {
  631. - .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
  632. - .num_banks = MT7622_NUM_ZONES,
  633. - .num_sensors = MT7622_NUM_SENSORS,
  634. - .vts_index = mt7622_vts_index,
  635. - .cali_val = MT7622_CALIBRATION,
  636. - .num_controller = MT7622_NUM_CONTROLLER,
  637. - .controller_offset = mt7622_tc_offset,
  638. - .need_switch_bank = true,
  639. - .bank_data = {
  640. - {
  641. - .num_sensors = 1,
  642. - .sensors = mt7622_bank_data,
  643. - },
  644. - },
  645. - .msr = mt7622_msr,
  646. - .adcpnp = mt7622_adcpnp,
  647. - .sensor_mux_values = mt7622_mux_values,
  648. - .version = MTK_THERMAL_V2,
  649. -};
  650. -
  651. -/*
  652. - * The MT8183 thermal controller has one bank for the current SW framework.
  653. - * The MT8183 has a total of 6 temperature sensors.
  654. - * There are two thermal controller to control the six sensor.
  655. - * The first one bind 2 sensor, and the other bind 4 sensors.
  656. - * The thermal core only gets the maximum temperature of all sensor, so
  657. - * the bank concept wouldn't be necessary here. However, the SVS (Smart
  658. - * Voltage Scaling) unit makes its decisions based on the same bank
  659. - * data, and this indeed needs the temperatures of the individual banks
  660. - * for making better decisions.
  661. - */
  662. -static const struct mtk_thermal_data mt8183_thermal_data = {
  663. - .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
  664. - .num_banks = MT8183_NUM_ZONES,
  665. - .num_sensors = MT8183_NUM_SENSORS,
  666. - .vts_index = mt8183_vts_index,
  667. - .cali_val = MT8183_CALIBRATION,
  668. - .num_controller = MT8183_NUM_CONTROLLER,
  669. - .controller_offset = mt8183_tc_offset,
  670. - .need_switch_bank = false,
  671. - .bank_data = {
  672. - {
  673. - .num_sensors = 6,
  674. - .sensors = mt8183_bank_data,
  675. - },
  676. - },
  677. -
  678. - .msr = mt8183_msr,
  679. - .adcpnp = mt8183_adcpnp,
  680. - .sensor_mux_values = mt8183_mux_values,
  681. - .version = MTK_THERMAL_V1,
  682. -};
  683. -
  684. -/*
  685. - * MT7986 uses AUXADC Channel 11 for raw data access.
  686. - */
  687. -static const struct mtk_thermal_data mt7986_thermal_data = {
  688. - .auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL,
  689. - .num_banks = MT7986_NUM_ZONES,
  690. - .num_sensors = MT7986_NUM_SENSORS,
  691. - .vts_index = mt7986_vts_index,
  692. - .cali_val = MT7986_CALIBRATION,
  693. - .num_controller = MT7986_NUM_CONTROLLER,
  694. - .controller_offset = mt7986_tc_offset,
  695. - .need_switch_bank = true,
  696. - .bank_data = {
  697. - {
  698. - .num_sensors = 1,
  699. - .sensors = mt7986_bank_data,
  700. - },
  701. - },
  702. - .msr = mt7986_msr,
  703. - .adcpnp = mt7986_adcpnp,
  704. - .sensor_mux_values = mt7986_mux_values,
  705. - .version = MTK_THERMAL_V3,
  706. -};
  707. -
  708. -/**
  709. - * raw_to_mcelsius_v1 - convert a raw ADC value to mcelsius
  710. - * @mt: The thermal controller
  711. - * @sensno: sensor number
  712. - * @raw: raw ADC value
  713. - *
  714. - * This converts the raw ADC value to mcelsius using the SoC specific
  715. - * calibration constants
  716. - */
  717. -static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw)
  718. -{
  719. - s32 tmp;
  720. -
  721. - raw &= 0xfff;
  722. -
  723. - tmp = 203450520 << 3;
  724. - tmp /= mt->conf->cali_val + mt->o_slope;
  725. - tmp /= 10000 + mt->adc_ge;
  726. - tmp *= raw - mt->vts[sensno] - 3350;
  727. - tmp >>= 3;
  728. -
  729. - return mt->degc_cali * 500 - tmp;
  730. -}
  731. -
  732. -static int raw_to_mcelsius_v2(struct mtk_thermal *mt, int sensno, s32 raw)
  733. -{
  734. - s32 format_1;
  735. - s32 format_2;
  736. - s32 g_oe;
  737. - s32 g_gain;
  738. - s32 g_x_roomt;
  739. - s32 tmp;
  740. -
  741. - if (raw == 0)
  742. - return 0;
  743. -
  744. - raw &= 0xfff;
  745. - g_gain = 10000 + (((mt->adc_ge - 512) * 10000) >> 12);
  746. - g_oe = mt->adc_oe - 512;
  747. - format_1 = mt->vts[VTS2] + 3105 - g_oe;
  748. - format_2 = (mt->degc_cali * 10) >> 1;
  749. - g_x_roomt = (((format_1 * 10000) >> 12) * 10000) / g_gain;
  750. -
  751. - tmp = (((((raw - g_oe) * 10000) >> 12) * 10000) / g_gain) - g_x_roomt;
  752. - tmp = tmp * 10 * 100 / 11;
  753. -
  754. - if (mt->o_slope_sign == 0)
  755. - tmp = tmp / (165 - mt->o_slope);
  756. - else
  757. - tmp = tmp / (165 + mt->o_slope);
  758. -
  759. - return (format_2 - tmp) * 100;
  760. -}
  761. -
  762. -static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw)
  763. -{
  764. - s32 tmp;
  765. -
  766. - if (raw == 0)
  767. - return 0;
  768. -
  769. - raw &= 0xfff;
  770. - tmp = 100000 * 15 / 16 * 10000;
  771. - tmp /= 4096 - 512 + mt->adc_ge;
  772. - tmp /= 1490;
  773. - tmp *= raw - mt->vts[sensno] - 2900;
  774. -
  775. - return mt->degc_cali * 500 - tmp;
  776. -}
  777. -
  778. -/**
  779. - * mtk_thermal_get_bank - get bank
  780. - * @bank: The bank
  781. - *
  782. - * The bank registers are banked, we have to select a bank in the
  783. - * PTPCORESEL register to access it.
  784. - */
  785. -static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
  786. -{
  787. - struct mtk_thermal *mt = bank->mt;
  788. - u32 val;
  789. -
  790. - if (mt->conf->need_switch_bank) {
  791. - mutex_lock(&mt->lock);
  792. -
  793. - val = readl(mt->thermal_base + PTPCORESEL);
  794. - val &= ~0xf;
  795. - val |= bank->id;
  796. - writel(val, mt->thermal_base + PTPCORESEL);
  797. - }
  798. -}
  799. -
  800. -/**
  801. - * mtk_thermal_put_bank - release bank
  802. - * @bank: The bank
  803. - *
  804. - * release a bank previously taken with mtk_thermal_get_bank,
  805. - */
  806. -static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
  807. -{
  808. - struct mtk_thermal *mt = bank->mt;
  809. -
  810. - if (mt->conf->need_switch_bank)
  811. - mutex_unlock(&mt->lock);
  812. -}
  813. -
  814. -/**
  815. - * mtk_thermal_bank_temperature - get the temperature of a bank
  816. - * @bank: The bank
  817. - *
  818. - * The temperature of a bank is considered the maximum temperature of
  819. - * the sensors associated to the bank.
  820. - */
  821. -static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
  822. -{
  823. - struct mtk_thermal *mt = bank->mt;
  824. - const struct mtk_thermal_data *conf = mt->conf;
  825. - int i, temp = INT_MIN, max = INT_MIN;
  826. - u32 raw;
  827. -
  828. - for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
  829. - raw = readl(mt->thermal_base + conf->msr[i]);
  830. -
  831. - temp = mt->raw_to_mcelsius(
  832. - mt, conf->bank_data[bank->id].sensors[i], raw);
  833. -
  834. -
  835. - /*
  836. - * The first read of a sensor often contains very high bogus
  837. - * temperature value. Filter these out so that the system does
  838. - * not immediately shut down.
  839. - */
  840. - if (temp > 200000)
  841. - temp = 0;
  842. -
  843. - if (temp > max)
  844. - max = temp;
  845. - }
  846. -
  847. - return max;
  848. -}
  849. -
  850. -static int mtk_read_temp(struct thermal_zone_device *tz, int *temperature)
  851. -{
  852. - struct mtk_thermal *mt = tz->devdata;
  853. - int i;
  854. - int tempmax = INT_MIN;
  855. -
  856. - for (i = 0; i < mt->conf->num_banks; i++) {
  857. - struct mtk_thermal_bank *bank = &mt->banks[i];
  858. -
  859. - mtk_thermal_get_bank(bank);
  860. -
  861. - tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
  862. -
  863. - mtk_thermal_put_bank(bank);
  864. - }
  865. -
  866. - *temperature = tempmax;
  867. -
  868. - return 0;
  869. -}
  870. -
  871. -static const struct thermal_zone_device_ops mtk_thermal_ops = {
  872. - .get_temp = mtk_read_temp,
  873. -};
  874. -
  875. -static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
  876. - u32 apmixed_phys_base, u32 auxadc_phys_base,
  877. - int ctrl_id)
  878. -{
  879. - struct mtk_thermal_bank *bank = &mt->banks[num];
  880. - const struct mtk_thermal_data *conf = mt->conf;
  881. - int i;
  882. -
  883. - int offset = mt->conf->controller_offset[ctrl_id];
  884. - void __iomem *controller_base = mt->thermal_base + offset;
  885. -
  886. - bank->id = num;
  887. - bank->mt = mt;
  888. -
  889. - mtk_thermal_get_bank(bank);
  890. -
  891. - /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
  892. - writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
  893. -
  894. - /*
  895. - * filt interval is 1 * 46.540us = 46.54us,
  896. - * sen interval is 429 * 46.540us = 19.96ms
  897. - */
  898. - writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
  899. - TEMP_MONCTL2_SENSOR_INTERVAL(429),
  900. - controller_base + TEMP_MONCTL2);
  901. -
  902. - /* poll is set to 10u */
  903. - writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
  904. - controller_base + TEMP_AHBPOLL);
  905. -
  906. - /* temperature sampling control, 1 sample */
  907. - writel(0x0, controller_base + TEMP_MSRCTL0);
  908. -
  909. - /* exceed this polling time, IRQ would be inserted */
  910. - writel(0xffffffff, controller_base + TEMP_AHBTO);
  911. -
  912. - /* number of interrupts per event, 1 is enough */
  913. - writel(0x0, controller_base + TEMP_MONIDET0);
  914. - writel(0x0, controller_base + TEMP_MONIDET1);
  915. -
  916. - /*
  917. - * The MT8173 thermal controller does not have its own ADC. Instead it
  918. - * uses AHB bus accesses to control the AUXADC. To do this the thermal
  919. - * controller has to be programmed with the physical addresses of the
  920. - * AUXADC registers and with the various bit positions in the AUXADC.
  921. - * Also the thermal controller controls a mux in the APMIXEDSYS register
  922. - * space.
  923. - */
  924. -
  925. - /*
  926. - * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
  927. - * automatically by hw
  928. - */
  929. - writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
  930. -
  931. - /* AHB address for auxadc mux selection */
  932. - writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
  933. - controller_base + TEMP_ADCMUXADDR);
  934. -
  935. - if (mt->conf->version == MTK_THERMAL_V1) {
  936. - /* AHB address for pnp sensor mux selection */
  937. - writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
  938. - controller_base + TEMP_PNPMUXADDR);
  939. - }
  940. -
  941. - /* AHB value for auxadc enable */
  942. - writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
  943. -
  944. - /* AHB address for auxadc enable (channel 0 immediate mode selected) */
  945. - writel(auxadc_phys_base + AUXADC_CON1_SET_V,
  946. - controller_base + TEMP_ADCENADDR);
  947. -
  948. - /* AHB address for auxadc valid bit */
  949. - writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
  950. - controller_base + TEMP_ADCVALIDADDR);
  951. -
  952. - /* AHB address for auxadc voltage output */
  953. - writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
  954. - controller_base + TEMP_ADCVOLTADDR);
  955. -
  956. - /* read valid & voltage are at the same register */
  957. - writel(0x0, controller_base + TEMP_RDCTRL);
  958. -
  959. - /* indicate where the valid bit is */
  960. - writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
  961. - controller_base + TEMP_ADCVALIDMASK);
  962. -
  963. - /* no shift */
  964. - writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
  965. -
  966. - /* enable auxadc mux write transaction */
  967. - writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
  968. - controller_base + TEMP_ADCWRITECTRL);
  969. -
  970. - for (i = 0; i < conf->bank_data[num].num_sensors; i++)
  971. - writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
  972. - mt->thermal_base + conf->adcpnp[i]);
  973. -
  974. - writel((1 << conf->bank_data[num].num_sensors) - 1,
  975. - controller_base + TEMP_MONCTL0);
  976. -
  977. - writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
  978. - TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
  979. - controller_base + TEMP_ADCWRITECTRL);
  980. -
  981. - mtk_thermal_put_bank(bank);
  982. -}
  983. -
  984. -static u64 of_get_phys_base(struct device_node *np)
  985. -{
  986. - u64 size64;
  987. - const __be32 *regaddr_p;
  988. -
  989. - regaddr_p = of_get_address(np, 0, &size64, NULL);
  990. - if (!regaddr_p)
  991. - return OF_BAD_ADDR;
  992. -
  993. - return of_translate_address(np, regaddr_p);
  994. -}
  995. -
  996. -static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
  997. -{
  998. - int i;
  999. -
  1000. - if (!(buf[0] & CALIB_BUF0_VALID_V1))
  1001. - return -EINVAL;
  1002. -
  1003. - mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]);
  1004. -
  1005. - for (i = 0; i < mt->conf->num_sensors; i++) {
  1006. - switch (mt->conf->vts_index[i]) {
  1007. - case VTS1:
  1008. - mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]);
  1009. - break;
  1010. - case VTS2:
  1011. - mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]);
  1012. - break;
  1013. - case VTS3:
  1014. - mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]);
  1015. - break;
  1016. - case VTS4:
  1017. - mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]);
  1018. - break;
  1019. - case VTS5:
  1020. - mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]);
  1021. - break;
  1022. - case VTSABB:
  1023. - mt->vts[VTSABB] =
  1024. - CALIB_BUF2_VTS_TSABB_V1(buf[2]);
  1025. - break;
  1026. - default:
  1027. - break;
  1028. - }
  1029. - }
  1030. -
  1031. - mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]);
  1032. - if (CALIB_BUF1_ID_V1(buf[1]) &
  1033. - CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0]))
  1034. - mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]);
  1035. - else
  1036. - mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]);
  1037. -
  1038. - return 0;
  1039. -}
  1040. -
  1041. -static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf)
  1042. -{
  1043. - if (!CALIB_BUF1_VALID_V2(buf[1]))
  1044. - return -EINVAL;
  1045. -
  1046. - mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]);
  1047. - mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]);
  1048. - mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]);
  1049. - mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]);
  1050. - mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]);
  1051. - mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]);
  1052. - mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]);
  1053. - mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]);
  1054. -
  1055. - return 0;
  1056. -}
  1057. -
  1058. -static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
  1059. -{
  1060. - if (!CALIB_BUF1_VALID_V3(buf[1]))
  1061. - return -EINVAL;
  1062. -
  1063. - mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
  1064. - mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
  1065. - mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
  1066. - mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
  1067. - mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
  1068. - mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
  1069. - mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
  1070. -
  1071. - if (CALIB_BUF1_ID_V3(buf[1]) == 0)
  1072. - mt->o_slope = 0;
  1073. -
  1074. - return 0;
  1075. -}
  1076. -
  1077. -static int mtk_thermal_get_calibration_data(struct device *dev,
  1078. - struct mtk_thermal *mt)
  1079. -{
  1080. - struct nvmem_cell *cell;
  1081. - u32 *buf;
  1082. - size_t len;
  1083. - int i, ret = 0;
  1084. -
  1085. - /* Start with default values */
  1086. - mt->adc_ge = 512;
  1087. - mt->adc_oe = 512;
  1088. - for (i = 0; i < mt->conf->num_sensors; i++)
  1089. - mt->vts[i] = 260;
  1090. - mt->degc_cali = 40;
  1091. - mt->o_slope = 0;
  1092. -
  1093. - cell = nvmem_cell_get(dev, "calibration-data");
  1094. - if (IS_ERR(cell)) {
  1095. - if (PTR_ERR(cell) == -EPROBE_DEFER)
  1096. - return PTR_ERR(cell);
  1097. - return 0;
  1098. - }
  1099. -
  1100. - buf = (u32 *)nvmem_cell_read(cell, &len);
  1101. -
  1102. - nvmem_cell_put(cell);
  1103. -
  1104. - if (IS_ERR(buf))
  1105. - return PTR_ERR(buf);
  1106. -
  1107. - if (len < 3 * sizeof(u32)) {
  1108. - dev_warn(dev, "invalid calibration data\n");
  1109. - ret = -EINVAL;
  1110. - goto out;
  1111. - }
  1112. -
  1113. - switch (mt->conf->version) {
  1114. - case MTK_THERMAL_V1:
  1115. - ret = mtk_thermal_extract_efuse_v1(mt, buf);
  1116. - break;
  1117. - case MTK_THERMAL_V2:
  1118. - ret = mtk_thermal_extract_efuse_v2(mt, buf);
  1119. - break;
  1120. - case MTK_THERMAL_V3:
  1121. - ret = mtk_thermal_extract_efuse_v3(mt, buf);
  1122. - break;
  1123. - default:
  1124. - ret = -EINVAL;
  1125. - break;
  1126. - }
  1127. -
  1128. - if (ret) {
  1129. - dev_info(dev, "Device not calibrated, using default calibration values\n");
  1130. - ret = 0;
  1131. - }
  1132. -
  1133. -out:
  1134. - kfree(buf);
  1135. -
  1136. - return ret;
  1137. -}
  1138. -
  1139. -static const struct of_device_id mtk_thermal_of_match[] = {
  1140. - {
  1141. - .compatible = "mediatek,mt8173-thermal",
  1142. - .data = (void *)&mt8173_thermal_data,
  1143. - },
  1144. - {
  1145. - .compatible = "mediatek,mt2701-thermal",
  1146. - .data = (void *)&mt2701_thermal_data,
  1147. - },
  1148. - {
  1149. - .compatible = "mediatek,mt2712-thermal",
  1150. - .data = (void *)&mt2712_thermal_data,
  1151. - },
  1152. - {
  1153. - .compatible = "mediatek,mt7622-thermal",
  1154. - .data = (void *)&mt7622_thermal_data,
  1155. - },
  1156. - {
  1157. - .compatible = "mediatek,mt7986-thermal",
  1158. - .data = (void *)&mt7986_thermal_data,
  1159. - },
  1160. - {
  1161. - .compatible = "mediatek,mt8183-thermal",
  1162. - .data = (void *)&mt8183_thermal_data,
  1163. - }, {
  1164. - },
  1165. -};
  1166. -MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
  1167. -
  1168. -static void mtk_thermal_turn_on_buffer(void __iomem *apmixed_base)
  1169. -{
  1170. - int tmp;
  1171. -
  1172. - tmp = readl(apmixed_base + APMIXED_SYS_TS_CON1);
  1173. - tmp &= ~(0x37);
  1174. - tmp |= 0x1;
  1175. - writel(tmp, apmixed_base + APMIXED_SYS_TS_CON1);
  1176. - udelay(200);
  1177. -}
  1178. -
  1179. -static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt,
  1180. - void __iomem *auxadc_base)
  1181. -{
  1182. - int tmp;
  1183. -
  1184. - writel(0x800, auxadc_base + AUXADC_CON1_SET_V);
  1185. - writel(0x1, mt->thermal_base + TEMP_MONCTL0);
  1186. - tmp = readl(mt->thermal_base + TEMP_MSRCTL1);
  1187. - writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1);
  1188. -}
  1189. -
  1190. -static int mtk_thermal_probe(struct platform_device *pdev)
  1191. -{
  1192. - int ret, i, ctrl_id;
  1193. - struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
  1194. - struct mtk_thermal *mt;
  1195. - u64 auxadc_phys_base, apmixed_phys_base;
  1196. - struct thermal_zone_device *tzdev;
  1197. - void __iomem *apmixed_base, *auxadc_base;
  1198. -
  1199. - mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
  1200. - if (!mt)
  1201. - return -ENOMEM;
  1202. -
  1203. - mt->conf = of_device_get_match_data(&pdev->dev);
  1204. -
  1205. - mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
  1206. - if (IS_ERR(mt->clk_peri_therm))
  1207. - return PTR_ERR(mt->clk_peri_therm);
  1208. -
  1209. - mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
  1210. - if (IS_ERR(mt->clk_auxadc))
  1211. - return PTR_ERR(mt->clk_auxadc);
  1212. -
  1213. - mt->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  1214. - if (IS_ERR(mt->thermal_base))
  1215. - return PTR_ERR(mt->thermal_base);
  1216. -
  1217. - ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
  1218. - if (ret)
  1219. - return ret;
  1220. -
  1221. - mutex_init(&mt->lock);
  1222. -
  1223. - mt->dev = &pdev->dev;
  1224. -
  1225. - auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
  1226. - if (!auxadc) {
  1227. - dev_err(&pdev->dev, "missing auxadc node\n");
  1228. - return -ENODEV;
  1229. - }
  1230. -
  1231. - auxadc_base = of_iomap(auxadc, 0);
  1232. - auxadc_phys_base = of_get_phys_base(auxadc);
  1233. -
  1234. - of_node_put(auxadc);
  1235. -
  1236. - if (auxadc_phys_base == OF_BAD_ADDR) {
  1237. - dev_err(&pdev->dev, "Can't get auxadc phys address\n");
  1238. - return -EINVAL;
  1239. - }
  1240. -
  1241. - apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
  1242. - if (!apmixedsys) {
  1243. - dev_err(&pdev->dev, "missing apmixedsys node\n");
  1244. - return -ENODEV;
  1245. - }
  1246. -
  1247. - apmixed_base = of_iomap(apmixedsys, 0);
  1248. - apmixed_phys_base = of_get_phys_base(apmixedsys);
  1249. -
  1250. - of_node_put(apmixedsys);
  1251. -
  1252. - if (apmixed_phys_base == OF_BAD_ADDR) {
  1253. - dev_err(&pdev->dev, "Can't get auxadc phys address\n");
  1254. - return -EINVAL;
  1255. - }
  1256. -
  1257. - ret = device_reset_optional(&pdev->dev);
  1258. - if (ret)
  1259. - return ret;
  1260. -
  1261. - ret = clk_prepare_enable(mt->clk_auxadc);
  1262. - if (ret) {
  1263. - dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
  1264. - return ret;
  1265. - }
  1266. -
  1267. - ret = clk_prepare_enable(mt->clk_peri_therm);
  1268. - if (ret) {
  1269. - dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
  1270. - goto err_disable_clk_auxadc;
  1271. - }
  1272. -
  1273. - if (mt->conf->version != MTK_THERMAL_V1) {
  1274. - mtk_thermal_turn_on_buffer(apmixed_base);
  1275. - mtk_thermal_release_periodic_ts(mt, auxadc_base);
  1276. - }
  1277. -
  1278. - if (mt->conf->version == MTK_THERMAL_V1)
  1279. - mt->raw_to_mcelsius = raw_to_mcelsius_v1;
  1280. - else if (mt->conf->version == MTK_THERMAL_V2)
  1281. - mt->raw_to_mcelsius = raw_to_mcelsius_v2;
  1282. - else
  1283. - mt->raw_to_mcelsius = raw_to_mcelsius_v3;
  1284. -
  1285. - for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
  1286. - for (i = 0; i < mt->conf->num_banks; i++)
  1287. - mtk_thermal_init_bank(mt, i, apmixed_phys_base,
  1288. - auxadc_phys_base, ctrl_id);
  1289. -
  1290. - platform_set_drvdata(pdev, mt);
  1291. -
  1292. - tzdev = devm_thermal_of_zone_register(&pdev->dev, 0, mt,
  1293. - &mtk_thermal_ops);
  1294. - if (IS_ERR(tzdev)) {
  1295. - ret = PTR_ERR(tzdev);
  1296. - goto err_disable_clk_peri_therm;
  1297. - }
  1298. -
  1299. - ret = devm_thermal_add_hwmon_sysfs(tzdev);
  1300. - if (ret)
  1301. - dev_warn(&pdev->dev, "error in thermal_add_hwmon_sysfs");
  1302. -
  1303. - return 0;
  1304. -
  1305. -err_disable_clk_peri_therm:
  1306. - clk_disable_unprepare(mt->clk_peri_therm);
  1307. -err_disable_clk_auxadc:
  1308. - clk_disable_unprepare(mt->clk_auxadc);
  1309. -
  1310. - return ret;
  1311. -}
  1312. -
  1313. -static int mtk_thermal_remove(struct platform_device *pdev)
  1314. -{
  1315. - struct mtk_thermal *mt = platform_get_drvdata(pdev);
  1316. -
  1317. - clk_disable_unprepare(mt->clk_peri_therm);
  1318. - clk_disable_unprepare(mt->clk_auxadc);
  1319. -
  1320. - return 0;
  1321. -}
  1322. -
  1323. -static struct platform_driver mtk_thermal_driver = {
  1324. - .probe = mtk_thermal_probe,
  1325. - .remove = mtk_thermal_remove,
  1326. - .driver = {
  1327. - .name = "mtk-thermal",
  1328. - .of_match_table = mtk_thermal_of_match,
  1329. - },
  1330. -};
  1331. -
  1332. -module_platform_driver(mtk_thermal_driver);
  1333. -
  1334. -MODULE_AUTHOR("Michael Kao <[email protected]>");
  1335. -MODULE_AUTHOR("Louis Yu <[email protected]>");
  1336. -MODULE_AUTHOR("Dawei Chien <[email protected]>");
  1337. -MODULE_AUTHOR("Sascha Hauer <[email protected]>");
  1338. -MODULE_AUTHOR("Hanyi Wu <[email protected]>");
  1339. -MODULE_DESCRIPTION("Mediatek thermal driver");
  1340. -MODULE_LICENSE("GPL v2");
  1341. --- /dev/null
  1342. +++ b/drivers/thermal/mediatek/auxadc_thermal.c
  1343. @@ -0,0 +1,1254 @@
  1344. +// SPDX-License-Identifier: GPL-2.0-only
  1345. +/*
  1346. + * Copyright (c) 2015 MediaTek Inc.
  1347. + * Author: Hanyi Wu <[email protected]>
  1348. + * Sascha Hauer <[email protected]>
  1349. + * Dawei Chien <[email protected]>
  1350. + * Louis Yu <[email protected]>
  1351. + */
  1352. +
  1353. +#include <linux/clk.h>
  1354. +#include <linux/delay.h>
  1355. +#include <linux/interrupt.h>
  1356. +#include <linux/kernel.h>
  1357. +#include <linux/module.h>
  1358. +#include <linux/nvmem-consumer.h>
  1359. +#include <linux/of.h>
  1360. +#include <linux/of_address.h>
  1361. +#include <linux/of_device.h>
  1362. +#include <linux/platform_device.h>
  1363. +#include <linux/slab.h>
  1364. +#include <linux/io.h>
  1365. +#include <linux/thermal.h>
  1366. +#include <linux/reset.h>
  1367. +#include <linux/types.h>
  1368. +
  1369. +#include "../thermal_hwmon.h"
  1370. +
  1371. +/* AUXADC Registers */
  1372. +#define AUXADC_CON1_SET_V 0x008
  1373. +#define AUXADC_CON1_CLR_V 0x00c
  1374. +#define AUXADC_CON2_V 0x010
  1375. +#define AUXADC_DATA(channel) (0x14 + (channel) * 4)
  1376. +
  1377. +#define APMIXED_SYS_TS_CON1 0x604
  1378. +
  1379. +/* Thermal Controller Registers */
  1380. +#define TEMP_MONCTL0 0x000
  1381. +#define TEMP_MONCTL1 0x004
  1382. +#define TEMP_MONCTL2 0x008
  1383. +#define TEMP_MONIDET0 0x014
  1384. +#define TEMP_MONIDET1 0x018
  1385. +#define TEMP_MSRCTL0 0x038
  1386. +#define TEMP_MSRCTL1 0x03c
  1387. +#define TEMP_AHBPOLL 0x040
  1388. +#define TEMP_AHBTO 0x044
  1389. +#define TEMP_ADCPNP0 0x048
  1390. +#define TEMP_ADCPNP1 0x04c
  1391. +#define TEMP_ADCPNP2 0x050
  1392. +#define TEMP_ADCPNP3 0x0b4
  1393. +
  1394. +#define TEMP_ADCMUX 0x054
  1395. +#define TEMP_ADCEN 0x060
  1396. +#define TEMP_PNPMUXADDR 0x064
  1397. +#define TEMP_ADCMUXADDR 0x068
  1398. +#define TEMP_ADCENADDR 0x074
  1399. +#define TEMP_ADCVALIDADDR 0x078
  1400. +#define TEMP_ADCVOLTADDR 0x07c
  1401. +#define TEMP_RDCTRL 0x080
  1402. +#define TEMP_ADCVALIDMASK 0x084
  1403. +#define TEMP_ADCVOLTAGESHIFT 0x088
  1404. +#define TEMP_ADCWRITECTRL 0x08c
  1405. +#define TEMP_MSR0 0x090
  1406. +#define TEMP_MSR1 0x094
  1407. +#define TEMP_MSR2 0x098
  1408. +#define TEMP_MSR3 0x0B8
  1409. +
  1410. +#define TEMP_SPARE0 0x0f0
  1411. +
  1412. +#define TEMP_ADCPNP0_1 0x148
  1413. +#define TEMP_ADCPNP1_1 0x14c
  1414. +#define TEMP_ADCPNP2_1 0x150
  1415. +#define TEMP_MSR0_1 0x190
  1416. +#define TEMP_MSR1_1 0x194
  1417. +#define TEMP_MSR2_1 0x198
  1418. +#define TEMP_ADCPNP3_1 0x1b4
  1419. +#define TEMP_MSR3_1 0x1B8
  1420. +
  1421. +#define PTPCORESEL 0x400
  1422. +
  1423. +#define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
  1424. +
  1425. +#define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
  1426. +#define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
  1427. +
  1428. +#define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
  1429. +
  1430. +#define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
  1431. +#define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
  1432. +
  1433. +#define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
  1434. +#define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
  1435. +
  1436. +/* MT8173 thermal sensors */
  1437. +#define MT8173_TS1 0
  1438. +#define MT8173_TS2 1
  1439. +#define MT8173_TS3 2
  1440. +#define MT8173_TS4 3
  1441. +#define MT8173_TSABB 4
  1442. +
  1443. +/* AUXADC channel 11 is used for the temperature sensors */
  1444. +#define MT8173_TEMP_AUXADC_CHANNEL 11
  1445. +
  1446. +/* The total number of temperature sensors in the MT8173 */
  1447. +#define MT8173_NUM_SENSORS 5
  1448. +
  1449. +/* The number of banks in the MT8173 */
  1450. +#define MT8173_NUM_ZONES 4
  1451. +
  1452. +/* The number of sensing points per bank */
  1453. +#define MT8173_NUM_SENSORS_PER_ZONE 4
  1454. +
  1455. +/* The number of controller in the MT8173 */
  1456. +#define MT8173_NUM_CONTROLLER 1
  1457. +
  1458. +/* The calibration coefficient of sensor */
  1459. +#define MT8173_CALIBRATION 165
  1460. +
  1461. +/*
  1462. + * Layout of the fuses providing the calibration data
  1463. + * These macros could be used for MT8183, MT8173, MT2701, and MT2712.
  1464. + * MT8183 has 6 sensors and needs 6 VTS calibration data.
  1465. + * MT8173 has 5 sensors and needs 5 VTS calibration data.
  1466. + * MT2701 has 3 sensors and needs 3 VTS calibration data.
  1467. + * MT2712 has 4 sensors and needs 4 VTS calibration data.
  1468. + */
  1469. +#define CALIB_BUF0_VALID_V1 BIT(0)
  1470. +#define CALIB_BUF1_ADC_GE_V1(x) (((x) >> 22) & 0x3ff)
  1471. +#define CALIB_BUF0_VTS_TS1_V1(x) (((x) >> 17) & 0x1ff)
  1472. +#define CALIB_BUF0_VTS_TS2_V1(x) (((x) >> 8) & 0x1ff)
  1473. +#define CALIB_BUF1_VTS_TS3_V1(x) (((x) >> 0) & 0x1ff)
  1474. +#define CALIB_BUF2_VTS_TS4_V1(x) (((x) >> 23) & 0x1ff)
  1475. +#define CALIB_BUF2_VTS_TS5_V1(x) (((x) >> 5) & 0x1ff)
  1476. +#define CALIB_BUF2_VTS_TSABB_V1(x) (((x) >> 14) & 0x1ff)
  1477. +#define CALIB_BUF0_DEGC_CALI_V1(x) (((x) >> 1) & 0x3f)
  1478. +#define CALIB_BUF0_O_SLOPE_V1(x) (((x) >> 26) & 0x3f)
  1479. +#define CALIB_BUF0_O_SLOPE_SIGN_V1(x) (((x) >> 7) & 0x1)
  1480. +#define CALIB_BUF1_ID_V1(x) (((x) >> 9) & 0x1)
  1481. +
  1482. +/*
  1483. + * Layout of the fuses providing the calibration data
  1484. + * These macros could be used for MT7622.
  1485. + */
  1486. +#define CALIB_BUF0_ADC_OE_V2(x) (((x) >> 22) & 0x3ff)
  1487. +#define CALIB_BUF0_ADC_GE_V2(x) (((x) >> 12) & 0x3ff)
  1488. +#define CALIB_BUF0_DEGC_CALI_V2(x) (((x) >> 6) & 0x3f)
  1489. +#define CALIB_BUF0_O_SLOPE_V2(x) (((x) >> 0) & 0x3f)
  1490. +#define CALIB_BUF1_VTS_TS1_V2(x) (((x) >> 23) & 0x1ff)
  1491. +#define CALIB_BUF1_VTS_TS2_V2(x) (((x) >> 14) & 0x1ff)
  1492. +#define CALIB_BUF1_VTS_TSABB_V2(x) (((x) >> 5) & 0x1ff)
  1493. +#define CALIB_BUF1_VALID_V2(x) (((x) >> 4) & 0x1)
  1494. +#define CALIB_BUF1_O_SLOPE_SIGN_V2(x) (((x) >> 3) & 0x1)
  1495. +
  1496. +/*
  1497. + * Layout of the fuses providing the calibration data
  1498. + * These macros can be used for MT7981 and MT7986.
  1499. + */
  1500. +#define CALIB_BUF0_ADC_GE_V3(x) (((x) >> 0) & 0x3ff)
  1501. +#define CALIB_BUF0_DEGC_CALI_V3(x) (((x) >> 20) & 0x3f)
  1502. +#define CALIB_BUF0_O_SLOPE_V3(x) (((x) >> 26) & 0x3f)
  1503. +#define CALIB_BUF1_VTS_TS1_V3(x) (((x) >> 0) & 0x1ff)
  1504. +#define CALIB_BUF1_VTS_TS2_V3(x) (((x) >> 21) & 0x1ff)
  1505. +#define CALIB_BUF1_VTS_TSABB_V3(x) (((x) >> 9) & 0x1ff)
  1506. +#define CALIB_BUF1_VALID_V3(x) (((x) >> 18) & 0x1)
  1507. +#define CALIB_BUF1_O_SLOPE_SIGN_V3(x) (((x) >> 19) & 0x1)
  1508. +#define CALIB_BUF1_ID_V3(x) (((x) >> 20) & 0x1)
  1509. +
  1510. +enum {
  1511. + VTS1,
  1512. + VTS2,
  1513. + VTS3,
  1514. + VTS4,
  1515. + VTS5,
  1516. + VTSABB,
  1517. + MAX_NUM_VTS,
  1518. +};
  1519. +
  1520. +enum mtk_thermal_version {
  1521. + MTK_THERMAL_V1 = 1,
  1522. + MTK_THERMAL_V2,
  1523. + MTK_THERMAL_V3,
  1524. +};
  1525. +
  1526. +/* MT2701 thermal sensors */
  1527. +#define MT2701_TS1 0
  1528. +#define MT2701_TS2 1
  1529. +#define MT2701_TSABB 2
  1530. +
  1531. +/* AUXADC channel 11 is used for the temperature sensors */
  1532. +#define MT2701_TEMP_AUXADC_CHANNEL 11
  1533. +
  1534. +/* The total number of temperature sensors in the MT2701 */
  1535. +#define MT2701_NUM_SENSORS 3
  1536. +
  1537. +/* The number of sensing points per bank */
  1538. +#define MT2701_NUM_SENSORS_PER_ZONE 3
  1539. +
  1540. +/* The number of controller in the MT2701 */
  1541. +#define MT2701_NUM_CONTROLLER 1
  1542. +
  1543. +/* The calibration coefficient of sensor */
  1544. +#define MT2701_CALIBRATION 165
  1545. +
  1546. +/* MT2712 thermal sensors */
  1547. +#define MT2712_TS1 0
  1548. +#define MT2712_TS2 1
  1549. +#define MT2712_TS3 2
  1550. +#define MT2712_TS4 3
  1551. +
  1552. +/* AUXADC channel 11 is used for the temperature sensors */
  1553. +#define MT2712_TEMP_AUXADC_CHANNEL 11
  1554. +
  1555. +/* The total number of temperature sensors in the MT2712 */
  1556. +#define MT2712_NUM_SENSORS 4
  1557. +
  1558. +/* The number of sensing points per bank */
  1559. +#define MT2712_NUM_SENSORS_PER_ZONE 4
  1560. +
  1561. +/* The number of controller in the MT2712 */
  1562. +#define MT2712_NUM_CONTROLLER 1
  1563. +
  1564. +/* The calibration coefficient of sensor */
  1565. +#define MT2712_CALIBRATION 165
  1566. +
  1567. +#define MT7622_TEMP_AUXADC_CHANNEL 11
  1568. +#define MT7622_NUM_SENSORS 1
  1569. +#define MT7622_NUM_ZONES 1
  1570. +#define MT7622_NUM_SENSORS_PER_ZONE 1
  1571. +#define MT7622_TS1 0
  1572. +#define MT7622_NUM_CONTROLLER 1
  1573. +
  1574. +/* The maximum number of banks */
  1575. +#define MAX_NUM_ZONES 8
  1576. +
  1577. +/* The calibration coefficient of sensor */
  1578. +#define MT7622_CALIBRATION 165
  1579. +
  1580. +/* MT8183 thermal sensors */
  1581. +#define MT8183_TS1 0
  1582. +#define MT8183_TS2 1
  1583. +#define MT8183_TS3 2
  1584. +#define MT8183_TS4 3
  1585. +#define MT8183_TS5 4
  1586. +#define MT8183_TSABB 5
  1587. +
  1588. +/* AUXADC channel is used for the temperature sensors */
  1589. +#define MT8183_TEMP_AUXADC_CHANNEL 11
  1590. +
  1591. +/* The total number of temperature sensors in the MT8183 */
  1592. +#define MT8183_NUM_SENSORS 6
  1593. +
  1594. +/* The number of banks in the MT8183 */
  1595. +#define MT8183_NUM_ZONES 1
  1596. +
  1597. +/* The number of sensing points per bank */
  1598. +#define MT8183_NUM_SENSORS_PER_ZONE 6
  1599. +
  1600. +/* The number of controller in the MT8183 */
  1601. +#define MT8183_NUM_CONTROLLER 2
  1602. +
  1603. +/* The calibration coefficient of sensor */
  1604. +#define MT8183_CALIBRATION 153
  1605. +
  1606. +/* AUXADC channel 11 is used for the temperature sensors */
  1607. +#define MT7986_TEMP_AUXADC_CHANNEL 11
  1608. +
  1609. +/* The total number of temperature sensors in the MT7986 */
  1610. +#define MT7986_NUM_SENSORS 1
  1611. +
  1612. +/* The number of banks in the MT7986 */
  1613. +#define MT7986_NUM_ZONES 1
  1614. +
  1615. +/* The number of sensing points per bank */
  1616. +#define MT7986_NUM_SENSORS_PER_ZONE 1
  1617. +
  1618. +/* MT7986 thermal sensors */
  1619. +#define MT7986_TS1 0
  1620. +
  1621. +/* The number of controller in the MT7986 */
  1622. +#define MT7986_NUM_CONTROLLER 1
  1623. +
  1624. +/* The calibration coefficient of sensor */
  1625. +#define MT7986_CALIBRATION 165
  1626. +
  1627. +struct mtk_thermal;
  1628. +
  1629. +struct thermal_bank_cfg {
  1630. + unsigned int num_sensors;
  1631. + const int *sensors;
  1632. +};
  1633. +
  1634. +struct mtk_thermal_bank {
  1635. + struct mtk_thermal *mt;
  1636. + int id;
  1637. +};
  1638. +
  1639. +struct mtk_thermal_data {
  1640. + s32 num_banks;
  1641. + s32 num_sensors;
  1642. + s32 auxadc_channel;
  1643. + const int *vts_index;
  1644. + const int *sensor_mux_values;
  1645. + const int *msr;
  1646. + const int *adcpnp;
  1647. + const int cali_val;
  1648. + const int num_controller;
  1649. + const int *controller_offset;
  1650. + bool need_switch_bank;
  1651. + struct thermal_bank_cfg bank_data[MAX_NUM_ZONES];
  1652. + enum mtk_thermal_version version;
  1653. +};
  1654. +
  1655. +struct mtk_thermal {
  1656. + struct device *dev;
  1657. + void __iomem *thermal_base;
  1658. +
  1659. + struct clk *clk_peri_therm;
  1660. + struct clk *clk_auxadc;
  1661. + /* lock: for getting and putting banks */
  1662. + struct mutex lock;
  1663. +
  1664. + /* Calibration values */
  1665. + s32 adc_ge;
  1666. + s32 adc_oe;
  1667. + s32 degc_cali;
  1668. + s32 o_slope;
  1669. + s32 o_slope_sign;
  1670. + s32 vts[MAX_NUM_VTS];
  1671. +
  1672. + const struct mtk_thermal_data *conf;
  1673. + struct mtk_thermal_bank banks[MAX_NUM_ZONES];
  1674. +
  1675. + int (*raw_to_mcelsius)(struct mtk_thermal *mt, int sensno, s32 raw);
  1676. +};
  1677. +
  1678. +/* MT8183 thermal sensor data */
  1679. +static const int mt8183_bank_data[MT8183_NUM_SENSORS] = {
  1680. + MT8183_TS1, MT8183_TS2, MT8183_TS3, MT8183_TS4, MT8183_TS5, MT8183_TSABB
  1681. +};
  1682. +
  1683. +static const int mt8183_msr[MT8183_NUM_SENSORS_PER_ZONE] = {
  1684. + TEMP_MSR0_1, TEMP_MSR1_1, TEMP_MSR2_1, TEMP_MSR1, TEMP_MSR0, TEMP_MSR3_1
  1685. +};
  1686. +
  1687. +static const int mt8183_adcpnp[MT8183_NUM_SENSORS_PER_ZONE] = {
  1688. + TEMP_ADCPNP0_1, TEMP_ADCPNP1_1, TEMP_ADCPNP2_1,
  1689. + TEMP_ADCPNP1, TEMP_ADCPNP0, TEMP_ADCPNP3_1
  1690. +};
  1691. +
  1692. +static const int mt8183_mux_values[MT8183_NUM_SENSORS] = { 0, 1, 2, 3, 4, 0 };
  1693. +static const int mt8183_tc_offset[MT8183_NUM_CONTROLLER] = {0x0, 0x100};
  1694. +
  1695. +static const int mt8183_vts_index[MT8183_NUM_SENSORS] = {
  1696. + VTS1, VTS2, VTS3, VTS4, VTS5, VTSABB
  1697. +};
  1698. +
  1699. +/* MT8173 thermal sensor data */
  1700. +static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
  1701. + { MT8173_TS2, MT8173_TS3 },
  1702. + { MT8173_TS2, MT8173_TS4 },
  1703. + { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
  1704. + { MT8173_TS2 },
  1705. +};
  1706. +
  1707. +static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
  1708. + TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
  1709. +};
  1710. +
  1711. +static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
  1712. + TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
  1713. +};
  1714. +
  1715. +static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
  1716. +static const int mt8173_tc_offset[MT8173_NUM_CONTROLLER] = { 0x0, };
  1717. +
  1718. +static const int mt8173_vts_index[MT8173_NUM_SENSORS] = {
  1719. + VTS1, VTS2, VTS3, VTS4, VTSABB
  1720. +};
  1721. +
  1722. +/* MT2701 thermal sensor data */
  1723. +static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
  1724. + MT2701_TS1, MT2701_TS2, MT2701_TSABB
  1725. +};
  1726. +
  1727. +static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
  1728. + TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
  1729. +};
  1730. +
  1731. +static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
  1732. + TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
  1733. +};
  1734. +
  1735. +static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
  1736. +static const int mt2701_tc_offset[MT2701_NUM_CONTROLLER] = { 0x0, };
  1737. +
  1738. +static const int mt2701_vts_index[MT2701_NUM_SENSORS] = {
  1739. + VTS1, VTS2, VTS3
  1740. +};
  1741. +
  1742. +/* MT2712 thermal sensor data */
  1743. +static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
  1744. + MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
  1745. +};
  1746. +
  1747. +static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
  1748. + TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
  1749. +};
  1750. +
  1751. +static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
  1752. + TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
  1753. +};
  1754. +
  1755. +static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
  1756. +static const int mt2712_tc_offset[MT2712_NUM_CONTROLLER] = { 0x0, };
  1757. +
  1758. +static const int mt2712_vts_index[MT2712_NUM_SENSORS] = {
  1759. + VTS1, VTS2, VTS3, VTS4
  1760. +};
  1761. +
  1762. +/* MT7622 thermal sensor data */
  1763. +static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
  1764. +static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
  1765. +static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
  1766. +static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
  1767. +static const int mt7622_vts_index[MT7622_NUM_SENSORS] = { VTS1 };
  1768. +static const int mt7622_tc_offset[MT7622_NUM_CONTROLLER] = { 0x0, };
  1769. +
  1770. +/* MT7986 thermal sensor data */
  1771. +static const int mt7986_bank_data[MT7986_NUM_SENSORS] = { MT7986_TS1, };
  1772. +static const int mt7986_msr[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
  1773. +static const int mt7986_adcpnp[MT7986_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
  1774. +static const int mt7986_mux_values[MT7986_NUM_SENSORS] = { 0, };
  1775. +static const int mt7986_vts_index[MT7986_NUM_SENSORS] = { VTS1 };
  1776. +static const int mt7986_tc_offset[MT7986_NUM_CONTROLLER] = { 0x0, };
  1777. +
  1778. +/*
  1779. + * The MT8173 thermal controller has four banks. Each bank can read up to
  1780. + * four temperature sensors simultaneously. The MT8173 has a total of 5
  1781. + * temperature sensors. We use each bank to measure a certain area of the
  1782. + * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
  1783. + * areas, hence is used in different banks.
  1784. + *
  1785. + * The thermal core only gets the maximum temperature of all banks, so
  1786. + * the bank concept wouldn't be necessary here. However, the SVS (Smart
  1787. + * Voltage Scaling) unit makes its decisions based on the same bank
  1788. + * data, and this indeed needs the temperatures of the individual banks
  1789. + * for making better decisions.
  1790. + */
  1791. +static const struct mtk_thermal_data mt8173_thermal_data = {
  1792. + .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
  1793. + .num_banks = MT8173_NUM_ZONES,
  1794. + .num_sensors = MT8173_NUM_SENSORS,
  1795. + .vts_index = mt8173_vts_index,
  1796. + .cali_val = MT8173_CALIBRATION,
  1797. + .num_controller = MT8173_NUM_CONTROLLER,
  1798. + .controller_offset = mt8173_tc_offset,
  1799. + .need_switch_bank = true,
  1800. + .bank_data = {
  1801. + {
  1802. + .num_sensors = 2,
  1803. + .sensors = mt8173_bank_data[0],
  1804. + }, {
  1805. + .num_sensors = 2,
  1806. + .sensors = mt8173_bank_data[1],
  1807. + }, {
  1808. + .num_sensors = 3,
  1809. + .sensors = mt8173_bank_data[2],
  1810. + }, {
  1811. + .num_sensors = 1,
  1812. + .sensors = mt8173_bank_data[3],
  1813. + },
  1814. + },
  1815. + .msr = mt8173_msr,
  1816. + .adcpnp = mt8173_adcpnp,
  1817. + .sensor_mux_values = mt8173_mux_values,
  1818. + .version = MTK_THERMAL_V1,
  1819. +};
  1820. +
  1821. +/*
  1822. + * The MT2701 thermal controller has one bank, which can read up to
  1823. + * three temperature sensors simultaneously. The MT2701 has a total of 3
  1824. + * temperature sensors.
  1825. + *
  1826. + * The thermal core only gets the maximum temperature of this one bank,
  1827. + * so the bank concept wouldn't be necessary here. However, the SVS (Smart
  1828. + * Voltage Scaling) unit makes its decisions based on the same bank
  1829. + * data.
  1830. + */
  1831. +static const struct mtk_thermal_data mt2701_thermal_data = {
  1832. + .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
  1833. + .num_banks = 1,
  1834. + .num_sensors = MT2701_NUM_SENSORS,
  1835. + .vts_index = mt2701_vts_index,
  1836. + .cali_val = MT2701_CALIBRATION,
  1837. + .num_controller = MT2701_NUM_CONTROLLER,
  1838. + .controller_offset = mt2701_tc_offset,
  1839. + .need_switch_bank = true,
  1840. + .bank_data = {
  1841. + {
  1842. + .num_sensors = 3,
  1843. + .sensors = mt2701_bank_data,
  1844. + },
  1845. + },
  1846. + .msr = mt2701_msr,
  1847. + .adcpnp = mt2701_adcpnp,
  1848. + .sensor_mux_values = mt2701_mux_values,
  1849. + .version = MTK_THERMAL_V1,
  1850. +};
  1851. +
  1852. +/*
  1853. + * The MT2712 thermal controller has one bank, which can read up to
  1854. + * four temperature sensors simultaneously. The MT2712 has a total of 4
  1855. + * temperature sensors.
  1856. + *
  1857. + * The thermal core only gets the maximum temperature of this one bank,
  1858. + * so the bank concept wouldn't be necessary here. However, the SVS (Smart
  1859. + * Voltage Scaling) unit makes its decisions based on the same bank
  1860. + * data.
  1861. + */
  1862. +static const struct mtk_thermal_data mt2712_thermal_data = {
  1863. + .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
  1864. + .num_banks = 1,
  1865. + .num_sensors = MT2712_NUM_SENSORS,
  1866. + .vts_index = mt2712_vts_index,
  1867. + .cali_val = MT2712_CALIBRATION,
  1868. + .num_controller = MT2712_NUM_CONTROLLER,
  1869. + .controller_offset = mt2712_tc_offset,
  1870. + .need_switch_bank = true,
  1871. + .bank_data = {
  1872. + {
  1873. + .num_sensors = 4,
  1874. + .sensors = mt2712_bank_data,
  1875. + },
  1876. + },
  1877. + .msr = mt2712_msr,
  1878. + .adcpnp = mt2712_adcpnp,
  1879. + .sensor_mux_values = mt2712_mux_values,
  1880. + .version = MTK_THERMAL_V1,
  1881. +};
  1882. +
  1883. +/*
  1884. + * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
  1885. + * access.
  1886. + */
  1887. +static const struct mtk_thermal_data mt7622_thermal_data = {
  1888. + .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
  1889. + .num_banks = MT7622_NUM_ZONES,
  1890. + .num_sensors = MT7622_NUM_SENSORS,
  1891. + .vts_index = mt7622_vts_index,
  1892. + .cali_val = MT7622_CALIBRATION,
  1893. + .num_controller = MT7622_NUM_CONTROLLER,
  1894. + .controller_offset = mt7622_tc_offset,
  1895. + .need_switch_bank = true,
  1896. + .bank_data = {
  1897. + {
  1898. + .num_sensors = 1,
  1899. + .sensors = mt7622_bank_data,
  1900. + },
  1901. + },
  1902. + .msr = mt7622_msr,
  1903. + .adcpnp = mt7622_adcpnp,
  1904. + .sensor_mux_values = mt7622_mux_values,
  1905. + .version = MTK_THERMAL_V2,
  1906. +};
  1907. +
  1908. +/*
  1909. + * The MT8183 thermal controller has one bank for the current SW framework.
  1910. + * The MT8183 has a total of 6 temperature sensors.
  1911. + * There are two thermal controller to control the six sensor.
  1912. + * The first one bind 2 sensor, and the other bind 4 sensors.
  1913. + * The thermal core only gets the maximum temperature of all sensor, so
  1914. + * the bank concept wouldn't be necessary here. However, the SVS (Smart
  1915. + * Voltage Scaling) unit makes its decisions based on the same bank
  1916. + * data, and this indeed needs the temperatures of the individual banks
  1917. + * for making better decisions.
  1918. + */
  1919. +static const struct mtk_thermal_data mt8183_thermal_data = {
  1920. + .auxadc_channel = MT8183_TEMP_AUXADC_CHANNEL,
  1921. + .num_banks = MT8183_NUM_ZONES,
  1922. + .num_sensors = MT8183_NUM_SENSORS,
  1923. + .vts_index = mt8183_vts_index,
  1924. + .cali_val = MT8183_CALIBRATION,
  1925. + .num_controller = MT8183_NUM_CONTROLLER,
  1926. + .controller_offset = mt8183_tc_offset,
  1927. + .need_switch_bank = false,
  1928. + .bank_data = {
  1929. + {
  1930. + .num_sensors = 6,
  1931. + .sensors = mt8183_bank_data,
  1932. + },
  1933. + },
  1934. +
  1935. + .msr = mt8183_msr,
  1936. + .adcpnp = mt8183_adcpnp,
  1937. + .sensor_mux_values = mt8183_mux_values,
  1938. + .version = MTK_THERMAL_V1,
  1939. +};
  1940. +
  1941. +/*
  1942. + * MT7986 uses AUXADC Channel 11 for raw data access.
  1943. + */
  1944. +static const struct mtk_thermal_data mt7986_thermal_data = {
  1945. + .auxadc_channel = MT7986_TEMP_AUXADC_CHANNEL,
  1946. + .num_banks = MT7986_NUM_ZONES,
  1947. + .num_sensors = MT7986_NUM_SENSORS,
  1948. + .vts_index = mt7986_vts_index,
  1949. + .cali_val = MT7986_CALIBRATION,
  1950. + .num_controller = MT7986_NUM_CONTROLLER,
  1951. + .controller_offset = mt7986_tc_offset,
  1952. + .need_switch_bank = true,
  1953. + .bank_data = {
  1954. + {
  1955. + .num_sensors = 1,
  1956. + .sensors = mt7986_bank_data,
  1957. + },
  1958. + },
  1959. + .msr = mt7986_msr,
  1960. + .adcpnp = mt7986_adcpnp,
  1961. + .sensor_mux_values = mt7986_mux_values,
  1962. + .version = MTK_THERMAL_V3,
  1963. +};
  1964. +
  1965. +/**
  1966. + * raw_to_mcelsius_v1 - convert a raw ADC value to mcelsius
  1967. + * @mt: The thermal controller
  1968. + * @sensno: sensor number
  1969. + * @raw: raw ADC value
  1970. + *
  1971. + * This converts the raw ADC value to mcelsius using the SoC specific
  1972. + * calibration constants
  1973. + */
  1974. +static int raw_to_mcelsius_v1(struct mtk_thermal *mt, int sensno, s32 raw)
  1975. +{
  1976. + s32 tmp;
  1977. +
  1978. + raw &= 0xfff;
  1979. +
  1980. + tmp = 203450520 << 3;
  1981. + tmp /= mt->conf->cali_val + mt->o_slope;
  1982. + tmp /= 10000 + mt->adc_ge;
  1983. + tmp *= raw - mt->vts[sensno] - 3350;
  1984. + tmp >>= 3;
  1985. +
  1986. + return mt->degc_cali * 500 - tmp;
  1987. +}
  1988. +
  1989. +static int raw_to_mcelsius_v2(struct mtk_thermal *mt, int sensno, s32 raw)
  1990. +{
  1991. + s32 format_1;
  1992. + s32 format_2;
  1993. + s32 g_oe;
  1994. + s32 g_gain;
  1995. + s32 g_x_roomt;
  1996. + s32 tmp;
  1997. +
  1998. + if (raw == 0)
  1999. + return 0;
  2000. +
  2001. + raw &= 0xfff;
  2002. + g_gain = 10000 + (((mt->adc_ge - 512) * 10000) >> 12);
  2003. + g_oe = mt->adc_oe - 512;
  2004. + format_1 = mt->vts[VTS2] + 3105 - g_oe;
  2005. + format_2 = (mt->degc_cali * 10) >> 1;
  2006. + g_x_roomt = (((format_1 * 10000) >> 12) * 10000) / g_gain;
  2007. +
  2008. + tmp = (((((raw - g_oe) * 10000) >> 12) * 10000) / g_gain) - g_x_roomt;
  2009. + tmp = tmp * 10 * 100 / 11;
  2010. +
  2011. + if (mt->o_slope_sign == 0)
  2012. + tmp = tmp / (165 - mt->o_slope);
  2013. + else
  2014. + tmp = tmp / (165 + mt->o_slope);
  2015. +
  2016. + return (format_2 - tmp) * 100;
  2017. +}
  2018. +
  2019. +static int raw_to_mcelsius_v3(struct mtk_thermal *mt, int sensno, s32 raw)
  2020. +{
  2021. + s32 tmp;
  2022. +
  2023. + if (raw == 0)
  2024. + return 0;
  2025. +
  2026. + raw &= 0xfff;
  2027. + tmp = 100000 * 15 / 16 * 10000;
  2028. + tmp /= 4096 - 512 + mt->adc_ge;
  2029. + tmp /= 1490;
  2030. + tmp *= raw - mt->vts[sensno] - 2900;
  2031. +
  2032. + return mt->degc_cali * 500 - tmp;
  2033. +}
  2034. +
  2035. +/**
  2036. + * mtk_thermal_get_bank - get bank
  2037. + * @bank: The bank
  2038. + *
  2039. + * The bank registers are banked, we have to select a bank in the
  2040. + * PTPCORESEL register to access it.
  2041. + */
  2042. +static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
  2043. +{
  2044. + struct mtk_thermal *mt = bank->mt;
  2045. + u32 val;
  2046. +
  2047. + if (mt->conf->need_switch_bank) {
  2048. + mutex_lock(&mt->lock);
  2049. +
  2050. + val = readl(mt->thermal_base + PTPCORESEL);
  2051. + val &= ~0xf;
  2052. + val |= bank->id;
  2053. + writel(val, mt->thermal_base + PTPCORESEL);
  2054. + }
  2055. +}
  2056. +
  2057. +/**
  2058. + * mtk_thermal_put_bank - release bank
  2059. + * @bank: The bank
  2060. + *
  2061. + * release a bank previously taken with mtk_thermal_get_bank,
  2062. + */
  2063. +static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
  2064. +{
  2065. + struct mtk_thermal *mt = bank->mt;
  2066. +
  2067. + if (mt->conf->need_switch_bank)
  2068. + mutex_unlock(&mt->lock);
  2069. +}
  2070. +
  2071. +/**
  2072. + * mtk_thermal_bank_temperature - get the temperature of a bank
  2073. + * @bank: The bank
  2074. + *
  2075. + * The temperature of a bank is considered the maximum temperature of
  2076. + * the sensors associated to the bank.
  2077. + */
  2078. +static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
  2079. +{
  2080. + struct mtk_thermal *mt = bank->mt;
  2081. + const struct mtk_thermal_data *conf = mt->conf;
  2082. + int i, temp = INT_MIN, max = INT_MIN;
  2083. + u32 raw;
  2084. +
  2085. + for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
  2086. + raw = readl(mt->thermal_base + conf->msr[i]);
  2087. +
  2088. + temp = mt->raw_to_mcelsius(
  2089. + mt, conf->bank_data[bank->id].sensors[i], raw);
  2090. +
  2091. +
  2092. + /*
  2093. + * The first read of a sensor often contains very high bogus
  2094. + * temperature value. Filter these out so that the system does
  2095. + * not immediately shut down.
  2096. + */
  2097. + if (temp > 200000)
  2098. + temp = 0;
  2099. +
  2100. + if (temp > max)
  2101. + max = temp;
  2102. + }
  2103. +
  2104. + return max;
  2105. +}
  2106. +
  2107. +static int mtk_read_temp(struct thermal_zone_device *tz, int *temperature)
  2108. +{
  2109. + struct mtk_thermal *mt = tz->devdata;
  2110. + int i;
  2111. + int tempmax = INT_MIN;
  2112. +
  2113. + for (i = 0; i < mt->conf->num_banks; i++) {
  2114. + struct mtk_thermal_bank *bank = &mt->banks[i];
  2115. +
  2116. + mtk_thermal_get_bank(bank);
  2117. +
  2118. + tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
  2119. +
  2120. + mtk_thermal_put_bank(bank);
  2121. + }
  2122. +
  2123. + *temperature = tempmax;
  2124. +
  2125. + return 0;
  2126. +}
  2127. +
  2128. +static const struct thermal_zone_device_ops mtk_thermal_ops = {
  2129. + .get_temp = mtk_read_temp,
  2130. +};
  2131. +
  2132. +static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
  2133. + u32 apmixed_phys_base, u32 auxadc_phys_base,
  2134. + int ctrl_id)
  2135. +{
  2136. + struct mtk_thermal_bank *bank = &mt->banks[num];
  2137. + const struct mtk_thermal_data *conf = mt->conf;
  2138. + int i;
  2139. +
  2140. + int offset = mt->conf->controller_offset[ctrl_id];
  2141. + void __iomem *controller_base = mt->thermal_base + offset;
  2142. +
  2143. + bank->id = num;
  2144. + bank->mt = mt;
  2145. +
  2146. + mtk_thermal_get_bank(bank);
  2147. +
  2148. + /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
  2149. + writel(TEMP_MONCTL1_PERIOD_UNIT(12), controller_base + TEMP_MONCTL1);
  2150. +
  2151. + /*
  2152. + * filt interval is 1 * 46.540us = 46.54us,
  2153. + * sen interval is 429 * 46.540us = 19.96ms
  2154. + */
  2155. + writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
  2156. + TEMP_MONCTL2_SENSOR_INTERVAL(429),
  2157. + controller_base + TEMP_MONCTL2);
  2158. +
  2159. + /* poll is set to 10u */
  2160. + writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
  2161. + controller_base + TEMP_AHBPOLL);
  2162. +
  2163. + /* temperature sampling control, 1 sample */
  2164. + writel(0x0, controller_base + TEMP_MSRCTL0);
  2165. +
  2166. + /* exceed this polling time, IRQ would be inserted */
  2167. + writel(0xffffffff, controller_base + TEMP_AHBTO);
  2168. +
  2169. + /* number of interrupts per event, 1 is enough */
  2170. + writel(0x0, controller_base + TEMP_MONIDET0);
  2171. + writel(0x0, controller_base + TEMP_MONIDET1);
  2172. +
  2173. + /*
  2174. + * The MT8173 thermal controller does not have its own ADC. Instead it
  2175. + * uses AHB bus accesses to control the AUXADC. To do this the thermal
  2176. + * controller has to be programmed with the physical addresses of the
  2177. + * AUXADC registers and with the various bit positions in the AUXADC.
  2178. + * Also the thermal controller controls a mux in the APMIXEDSYS register
  2179. + * space.
  2180. + */
  2181. +
  2182. + /*
  2183. + * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
  2184. + * automatically by hw
  2185. + */
  2186. + writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCMUX);
  2187. +
  2188. + /* AHB address for auxadc mux selection */
  2189. + writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
  2190. + controller_base + TEMP_ADCMUXADDR);
  2191. +
  2192. + if (mt->conf->version == MTK_THERMAL_V1) {
  2193. + /* AHB address for pnp sensor mux selection */
  2194. + writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
  2195. + controller_base + TEMP_PNPMUXADDR);
  2196. + }
  2197. +
  2198. + /* AHB value for auxadc enable */
  2199. + writel(BIT(conf->auxadc_channel), controller_base + TEMP_ADCEN);
  2200. +
  2201. + /* AHB address for auxadc enable (channel 0 immediate mode selected) */
  2202. + writel(auxadc_phys_base + AUXADC_CON1_SET_V,
  2203. + controller_base + TEMP_ADCENADDR);
  2204. +
  2205. + /* AHB address for auxadc valid bit */
  2206. + writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
  2207. + controller_base + TEMP_ADCVALIDADDR);
  2208. +
  2209. + /* AHB address for auxadc voltage output */
  2210. + writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
  2211. + controller_base + TEMP_ADCVOLTADDR);
  2212. +
  2213. + /* read valid & voltage are at the same register */
  2214. + writel(0x0, controller_base + TEMP_RDCTRL);
  2215. +
  2216. + /* indicate where the valid bit is */
  2217. + writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
  2218. + controller_base + TEMP_ADCVALIDMASK);
  2219. +
  2220. + /* no shift */
  2221. + writel(0x0, controller_base + TEMP_ADCVOLTAGESHIFT);
  2222. +
  2223. + /* enable auxadc mux write transaction */
  2224. + writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
  2225. + controller_base + TEMP_ADCWRITECTRL);
  2226. +
  2227. + for (i = 0; i < conf->bank_data[num].num_sensors; i++)
  2228. + writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
  2229. + mt->thermal_base + conf->adcpnp[i]);
  2230. +
  2231. + writel((1 << conf->bank_data[num].num_sensors) - 1,
  2232. + controller_base + TEMP_MONCTL0);
  2233. +
  2234. + writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
  2235. + TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
  2236. + controller_base + TEMP_ADCWRITECTRL);
  2237. +
  2238. + mtk_thermal_put_bank(bank);
  2239. +}
  2240. +
  2241. +static u64 of_get_phys_base(struct device_node *np)
  2242. +{
  2243. + u64 size64;
  2244. + const __be32 *regaddr_p;
  2245. +
  2246. + regaddr_p = of_get_address(np, 0, &size64, NULL);
  2247. + if (!regaddr_p)
  2248. + return OF_BAD_ADDR;
  2249. +
  2250. + return of_translate_address(np, regaddr_p);
  2251. +}
  2252. +
  2253. +static int mtk_thermal_extract_efuse_v1(struct mtk_thermal *mt, u32 *buf)
  2254. +{
  2255. + int i;
  2256. +
  2257. + if (!(buf[0] & CALIB_BUF0_VALID_V1))
  2258. + return -EINVAL;
  2259. +
  2260. + mt->adc_ge = CALIB_BUF1_ADC_GE_V1(buf[1]);
  2261. +
  2262. + for (i = 0; i < mt->conf->num_sensors; i++) {
  2263. + switch (mt->conf->vts_index[i]) {
  2264. + case VTS1:
  2265. + mt->vts[VTS1] = CALIB_BUF0_VTS_TS1_V1(buf[0]);
  2266. + break;
  2267. + case VTS2:
  2268. + mt->vts[VTS2] = CALIB_BUF0_VTS_TS2_V1(buf[0]);
  2269. + break;
  2270. + case VTS3:
  2271. + mt->vts[VTS3] = CALIB_BUF1_VTS_TS3_V1(buf[1]);
  2272. + break;
  2273. + case VTS4:
  2274. + mt->vts[VTS4] = CALIB_BUF2_VTS_TS4_V1(buf[2]);
  2275. + break;
  2276. + case VTS5:
  2277. + mt->vts[VTS5] = CALIB_BUF2_VTS_TS5_V1(buf[2]);
  2278. + break;
  2279. + case VTSABB:
  2280. + mt->vts[VTSABB] =
  2281. + CALIB_BUF2_VTS_TSABB_V1(buf[2]);
  2282. + break;
  2283. + default:
  2284. + break;
  2285. + }
  2286. + }
  2287. +
  2288. + mt->degc_cali = CALIB_BUF0_DEGC_CALI_V1(buf[0]);
  2289. + if (CALIB_BUF1_ID_V1(buf[1]) &
  2290. + CALIB_BUF0_O_SLOPE_SIGN_V1(buf[0]))
  2291. + mt->o_slope = -CALIB_BUF0_O_SLOPE_V1(buf[0]);
  2292. + else
  2293. + mt->o_slope = CALIB_BUF0_O_SLOPE_V1(buf[0]);
  2294. +
  2295. + return 0;
  2296. +}
  2297. +
  2298. +static int mtk_thermal_extract_efuse_v2(struct mtk_thermal *mt, u32 *buf)
  2299. +{
  2300. + if (!CALIB_BUF1_VALID_V2(buf[1]))
  2301. + return -EINVAL;
  2302. +
  2303. + mt->adc_oe = CALIB_BUF0_ADC_OE_V2(buf[0]);
  2304. + mt->adc_ge = CALIB_BUF0_ADC_GE_V2(buf[0]);
  2305. + mt->degc_cali = CALIB_BUF0_DEGC_CALI_V2(buf[0]);
  2306. + mt->o_slope = CALIB_BUF0_O_SLOPE_V2(buf[0]);
  2307. + mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V2(buf[1]);
  2308. + mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V2(buf[1]);
  2309. + mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V2(buf[1]);
  2310. + mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V2(buf[1]);
  2311. +
  2312. + return 0;
  2313. +}
  2314. +
  2315. +static int mtk_thermal_extract_efuse_v3(struct mtk_thermal *mt, u32 *buf)
  2316. +{
  2317. + if (!CALIB_BUF1_VALID_V3(buf[1]))
  2318. + return -EINVAL;
  2319. +
  2320. + mt->adc_ge = CALIB_BUF0_ADC_GE_V3(buf[0]);
  2321. + mt->degc_cali = CALIB_BUF0_DEGC_CALI_V3(buf[0]);
  2322. + mt->o_slope = CALIB_BUF0_O_SLOPE_V3(buf[0]);
  2323. + mt->vts[VTS1] = CALIB_BUF1_VTS_TS1_V3(buf[1]);
  2324. + mt->vts[VTS2] = CALIB_BUF1_VTS_TS2_V3(buf[1]);
  2325. + mt->vts[VTSABB] = CALIB_BUF1_VTS_TSABB_V3(buf[1]);
  2326. + mt->o_slope_sign = CALIB_BUF1_O_SLOPE_SIGN_V3(buf[1]);
  2327. +
  2328. + if (CALIB_BUF1_ID_V3(buf[1]) == 0)
  2329. + mt->o_slope = 0;
  2330. +
  2331. + return 0;
  2332. +}
  2333. +
  2334. +static int mtk_thermal_get_calibration_data(struct device *dev,
  2335. + struct mtk_thermal *mt)
  2336. +{
  2337. + struct nvmem_cell *cell;
  2338. + u32 *buf;
  2339. + size_t len;
  2340. + int i, ret = 0;
  2341. +
  2342. + /* Start with default values */
  2343. + mt->adc_ge = 512;
  2344. + mt->adc_oe = 512;
  2345. + for (i = 0; i < mt->conf->num_sensors; i++)
  2346. + mt->vts[i] = 260;
  2347. + mt->degc_cali = 40;
  2348. + mt->o_slope = 0;
  2349. +
  2350. + cell = nvmem_cell_get(dev, "calibration-data");
  2351. + if (IS_ERR(cell)) {
  2352. + if (PTR_ERR(cell) == -EPROBE_DEFER)
  2353. + return PTR_ERR(cell);
  2354. + return 0;
  2355. + }
  2356. +
  2357. + buf = (u32 *)nvmem_cell_read(cell, &len);
  2358. +
  2359. + nvmem_cell_put(cell);
  2360. +
  2361. + if (IS_ERR(buf))
  2362. + return PTR_ERR(buf);
  2363. +
  2364. + if (len < 3 * sizeof(u32)) {
  2365. + dev_warn(dev, "invalid calibration data\n");
  2366. + ret = -EINVAL;
  2367. + goto out;
  2368. + }
  2369. +
  2370. + switch (mt->conf->version) {
  2371. + case MTK_THERMAL_V1:
  2372. + ret = mtk_thermal_extract_efuse_v1(mt, buf);
  2373. + break;
  2374. + case MTK_THERMAL_V2:
  2375. + ret = mtk_thermal_extract_efuse_v2(mt, buf);
  2376. + break;
  2377. + case MTK_THERMAL_V3:
  2378. + ret = mtk_thermal_extract_efuse_v3(mt, buf);
  2379. + break;
  2380. + default:
  2381. + ret = -EINVAL;
  2382. + break;
  2383. + }
  2384. +
  2385. + if (ret) {
  2386. + dev_info(dev, "Device not calibrated, using default calibration values\n");
  2387. + ret = 0;
  2388. + }
  2389. +
  2390. +out:
  2391. + kfree(buf);
  2392. +
  2393. + return ret;
  2394. +}
  2395. +
  2396. +static const struct of_device_id mtk_thermal_of_match[] = {
  2397. + {
  2398. + .compatible = "mediatek,mt8173-thermal",
  2399. + .data = (void *)&mt8173_thermal_data,
  2400. + },
  2401. + {
  2402. + .compatible = "mediatek,mt2701-thermal",
  2403. + .data = (void *)&mt2701_thermal_data,
  2404. + },
  2405. + {
  2406. + .compatible = "mediatek,mt2712-thermal",
  2407. + .data = (void *)&mt2712_thermal_data,
  2408. + },
  2409. + {
  2410. + .compatible = "mediatek,mt7622-thermal",
  2411. + .data = (void *)&mt7622_thermal_data,
  2412. + },
  2413. + {
  2414. + .compatible = "mediatek,mt7986-thermal",
  2415. + .data = (void *)&mt7986_thermal_data,
  2416. + },
  2417. + {
  2418. + .compatible = "mediatek,mt8183-thermal",
  2419. + .data = (void *)&mt8183_thermal_data,
  2420. + }, {
  2421. + },
  2422. +};
  2423. +MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
  2424. +
  2425. +static void mtk_thermal_turn_on_buffer(void __iomem *apmixed_base)
  2426. +{
  2427. + int tmp;
  2428. +
  2429. + tmp = readl(apmixed_base + APMIXED_SYS_TS_CON1);
  2430. + tmp &= ~(0x37);
  2431. + tmp |= 0x1;
  2432. + writel(tmp, apmixed_base + APMIXED_SYS_TS_CON1);
  2433. + udelay(200);
  2434. +}
  2435. +
  2436. +static void mtk_thermal_release_periodic_ts(struct mtk_thermal *mt,
  2437. + void __iomem *auxadc_base)
  2438. +{
  2439. + int tmp;
  2440. +
  2441. + writel(0x800, auxadc_base + AUXADC_CON1_SET_V);
  2442. + writel(0x1, mt->thermal_base + TEMP_MONCTL0);
  2443. + tmp = readl(mt->thermal_base + TEMP_MSRCTL1);
  2444. + writel((tmp & (~0x10e)), mt->thermal_base + TEMP_MSRCTL1);
  2445. +}
  2446. +
  2447. +static int mtk_thermal_probe(struct platform_device *pdev)
  2448. +{
  2449. + int ret, i, ctrl_id;
  2450. + struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
  2451. + struct mtk_thermal *mt;
  2452. + u64 auxadc_phys_base, apmixed_phys_base;
  2453. + struct thermal_zone_device *tzdev;
  2454. + void __iomem *apmixed_base, *auxadc_base;
  2455. +
  2456. + mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
  2457. + if (!mt)
  2458. + return -ENOMEM;
  2459. +
  2460. + mt->conf = of_device_get_match_data(&pdev->dev);
  2461. +
  2462. + mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
  2463. + if (IS_ERR(mt->clk_peri_therm))
  2464. + return PTR_ERR(mt->clk_peri_therm);
  2465. +
  2466. + mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
  2467. + if (IS_ERR(mt->clk_auxadc))
  2468. + return PTR_ERR(mt->clk_auxadc);
  2469. +
  2470. + mt->thermal_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
  2471. + if (IS_ERR(mt->thermal_base))
  2472. + return PTR_ERR(mt->thermal_base);
  2473. +
  2474. + ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
  2475. + if (ret)
  2476. + return ret;
  2477. +
  2478. + mutex_init(&mt->lock);
  2479. +
  2480. + mt->dev = &pdev->dev;
  2481. +
  2482. + auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
  2483. + if (!auxadc) {
  2484. + dev_err(&pdev->dev, "missing auxadc node\n");
  2485. + return -ENODEV;
  2486. + }
  2487. +
  2488. + auxadc_base = of_iomap(auxadc, 0);
  2489. + auxadc_phys_base = of_get_phys_base(auxadc);
  2490. +
  2491. + of_node_put(auxadc);
  2492. +
  2493. + if (auxadc_phys_base == OF_BAD_ADDR) {
  2494. + dev_err(&pdev->dev, "Can't get auxadc phys address\n");
  2495. + return -EINVAL;
  2496. + }
  2497. +
  2498. + apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
  2499. + if (!apmixedsys) {
  2500. + dev_err(&pdev->dev, "missing apmixedsys node\n");
  2501. + return -ENODEV;
  2502. + }
  2503. +
  2504. + apmixed_base = of_iomap(apmixedsys, 0);
  2505. + apmixed_phys_base = of_get_phys_base(apmixedsys);
  2506. +
  2507. + of_node_put(apmixedsys);
  2508. +
  2509. + if (apmixed_phys_base == OF_BAD_ADDR) {
  2510. + dev_err(&pdev->dev, "Can't get auxadc phys address\n");
  2511. + return -EINVAL;
  2512. + }
  2513. +
  2514. + ret = device_reset_optional(&pdev->dev);
  2515. + if (ret)
  2516. + return ret;
  2517. +
  2518. + ret = clk_prepare_enable(mt->clk_auxadc);
  2519. + if (ret) {
  2520. + dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
  2521. + return ret;
  2522. + }
  2523. +
  2524. + ret = clk_prepare_enable(mt->clk_peri_therm);
  2525. + if (ret) {
  2526. + dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
  2527. + goto err_disable_clk_auxadc;
  2528. + }
  2529. +
  2530. + if (mt->conf->version != MTK_THERMAL_V1) {
  2531. + mtk_thermal_turn_on_buffer(apmixed_base);
  2532. + mtk_thermal_release_periodic_ts(mt, auxadc_base);
  2533. + }
  2534. +
  2535. + if (mt->conf->version == MTK_THERMAL_V1)
  2536. + mt->raw_to_mcelsius = raw_to_mcelsius_v1;
  2537. + else if (mt->conf->version == MTK_THERMAL_V2)
  2538. + mt->raw_to_mcelsius = raw_to_mcelsius_v2;
  2539. + else
  2540. + mt->raw_to_mcelsius = raw_to_mcelsius_v3;
  2541. +
  2542. + for (ctrl_id = 0; ctrl_id < mt->conf->num_controller ; ctrl_id++)
  2543. + for (i = 0; i < mt->conf->num_banks; i++)
  2544. + mtk_thermal_init_bank(mt, i, apmixed_phys_base,
  2545. + auxadc_phys_base, ctrl_id);
  2546. +
  2547. + platform_set_drvdata(pdev, mt);
  2548. +
  2549. + tzdev = devm_thermal_of_zone_register(&pdev->dev, 0, mt,
  2550. + &mtk_thermal_ops);
  2551. + if (IS_ERR(tzdev)) {
  2552. + ret = PTR_ERR(tzdev);
  2553. + goto err_disable_clk_peri_therm;
  2554. + }
  2555. +
  2556. + ret = devm_thermal_add_hwmon_sysfs(tzdev);
  2557. + if (ret)
  2558. + dev_warn(&pdev->dev, "error in thermal_add_hwmon_sysfs");
  2559. +
  2560. + return 0;
  2561. +
  2562. +err_disable_clk_peri_therm:
  2563. + clk_disable_unprepare(mt->clk_peri_therm);
  2564. +err_disable_clk_auxadc:
  2565. + clk_disable_unprepare(mt->clk_auxadc);
  2566. +
  2567. + return ret;
  2568. +}
  2569. +
  2570. +static int mtk_thermal_remove(struct platform_device *pdev)
  2571. +{
  2572. + struct mtk_thermal *mt = platform_get_drvdata(pdev);
  2573. +
  2574. + clk_disable_unprepare(mt->clk_peri_therm);
  2575. + clk_disable_unprepare(mt->clk_auxadc);
  2576. +
  2577. + return 0;
  2578. +}
  2579. +
  2580. +static struct platform_driver mtk_thermal_driver = {
  2581. + .probe = mtk_thermal_probe,
  2582. + .remove = mtk_thermal_remove,
  2583. + .driver = {
  2584. + .name = "mtk-thermal",
  2585. + .of_match_table = mtk_thermal_of_match,
  2586. + },
  2587. +};
  2588. +
  2589. +module_platform_driver(mtk_thermal_driver);
  2590. +
  2591. +MODULE_AUTHOR("Michael Kao <[email protected]>");
  2592. +MODULE_AUTHOR("Louis Yu <[email protected]>");
  2593. +MODULE_AUTHOR("Dawei Chien <[email protected]>");
  2594. +MODULE_AUTHOR("Sascha Hauer <[email protected]>");
  2595. +MODULE_AUTHOR("Hanyi Wu <[email protected]>");
  2596. +MODULE_DESCRIPTION("Mediatek thermal driver");
  2597. +MODULE_LICENSE("GPL v2");