soctherm.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014 - 2018, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Author:
  6. * Mikko Perttunen <[email protected]>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/bitops.h>
  20. #include <linux/clk.h>
  21. #include <linux/delay.h>
  22. #include <linux/err.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/reset.h>
  31. #include <linux/thermal.h>
  32. #include <dt-bindings/thermal/tegra124-soctherm.h>
  33. #include "../thermal_core.h"
  34. #include "soctherm.h"
  35. #define SENSOR_CONFIG0 0
  36. #define SENSOR_CONFIG0_STOP BIT(0)
  37. #define SENSOR_CONFIG0_CPTR_OVER BIT(2)
  38. #define SENSOR_CONFIG0_OVER BIT(3)
  39. #define SENSOR_CONFIG0_TCALC_OVER BIT(4)
  40. #define SENSOR_CONFIG0_TALL_MASK (0xfffff << 8)
  41. #define SENSOR_CONFIG0_TALL_SHIFT 8
  42. #define SENSOR_CONFIG1 4
  43. #define SENSOR_CONFIG1_TSAMPLE_MASK 0x3ff
  44. #define SENSOR_CONFIG1_TSAMPLE_SHIFT 0
  45. #define SENSOR_CONFIG1_TIDDQ_EN_MASK (0x3f << 15)
  46. #define SENSOR_CONFIG1_TIDDQ_EN_SHIFT 15
  47. #define SENSOR_CONFIG1_TEN_COUNT_MASK (0x3f << 24)
  48. #define SENSOR_CONFIG1_TEN_COUNT_SHIFT 24
  49. #define SENSOR_CONFIG1_TEMP_ENABLE BIT(31)
  50. /*
  51. * SENSOR_CONFIG2 is defined in soctherm.h
  52. * because, it will be used by tegra_soctherm_fuse.c
  53. */
  54. #define SENSOR_STATUS0 0xc
  55. #define SENSOR_STATUS0_VALID_MASK BIT(31)
  56. #define SENSOR_STATUS0_CAPTURE_MASK 0xffff
  57. #define SENSOR_STATUS1 0x10
  58. #define SENSOR_STATUS1_TEMP_VALID_MASK BIT(31)
  59. #define SENSOR_STATUS1_TEMP_MASK 0xffff
  60. #define READBACK_VALUE_MASK 0xff00
  61. #define READBACK_VALUE_SHIFT 8
  62. #define READBACK_ADD_HALF BIT(7)
  63. #define READBACK_NEGATE BIT(0)
  64. /*
  65. * THERMCTL_LEVEL0_GROUP_CPU is defined in soctherm.h
  66. * because it will be used by tegraxxx_soctherm.c
  67. */
  68. #define THERMCTL_LVL0_CPU0_EN_MASK BIT(8)
  69. #define THERMCTL_LVL0_CPU0_CPU_THROT_MASK (0x3 << 5)
  70. #define THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT 0x1
  71. #define THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY 0x2
  72. #define THERMCTL_LVL0_CPU0_GPU_THROT_MASK (0x3 << 3)
  73. #define THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT 0x1
  74. #define THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY 0x2
  75. #define THERMCTL_LVL0_CPU0_MEM_THROT_MASK BIT(2)
  76. #define THERMCTL_LVL0_CPU0_STATUS_MASK 0x3
  77. #define THERMCTL_LVL0_UP_STATS 0x10
  78. #define THERMCTL_LVL0_DN_STATS 0x14
  79. #define THERMCTL_INTR_STATUS 0x84
  80. #define TH_INTR_MD0_MASK BIT(25)
  81. #define TH_INTR_MU0_MASK BIT(24)
  82. #define TH_INTR_GD0_MASK BIT(17)
  83. #define TH_INTR_GU0_MASK BIT(16)
  84. #define TH_INTR_CD0_MASK BIT(9)
  85. #define TH_INTR_CU0_MASK BIT(8)
  86. #define TH_INTR_PD0_MASK BIT(1)
  87. #define TH_INTR_PU0_MASK BIT(0)
  88. #define TH_INTR_IGNORE_MASK 0xFCFCFCFC
  89. #define THERMCTL_STATS_CTL 0x94
  90. #define STATS_CTL_CLR_DN 0x8
  91. #define STATS_CTL_EN_DN 0x4
  92. #define STATS_CTL_CLR_UP 0x2
  93. #define STATS_CTL_EN_UP 0x1
  94. #define OC1_CFG 0x310
  95. #define OC1_CFG_LONG_LATENCY_MASK BIT(6)
  96. #define OC1_CFG_HW_RESTORE_MASK BIT(5)
  97. #define OC1_CFG_PWR_GOOD_MASK_MASK BIT(4)
  98. #define OC1_CFG_THROTTLE_MODE_MASK (0x3 << 2)
  99. #define OC1_CFG_ALARM_POLARITY_MASK BIT(1)
  100. #define OC1_CFG_EN_THROTTLE_MASK BIT(0)
  101. #define OC1_CNT_THRESHOLD 0x314
  102. #define OC1_THROTTLE_PERIOD 0x318
  103. #define OC1_ALARM_COUNT 0x31c
  104. #define OC1_FILTER 0x320
  105. #define OC1_STATS 0x3a8
  106. #define OC_INTR_STATUS 0x39c
  107. #define OC_INTR_ENABLE 0x3a0
  108. #define OC_INTR_DISABLE 0x3a4
  109. #define OC_STATS_CTL 0x3c4
  110. #define OC_STATS_CTL_CLR_ALL 0x2
  111. #define OC_STATS_CTL_EN_ALL 0x1
  112. #define OC_INTR_OC1_MASK BIT(0)
  113. #define OC_INTR_OC2_MASK BIT(1)
  114. #define OC_INTR_OC3_MASK BIT(2)
  115. #define OC_INTR_OC4_MASK BIT(3)
  116. #define OC_INTR_OC5_MASK BIT(4)
  117. #define THROT_GLOBAL_CFG 0x400
  118. #define THROT_GLOBAL_ENB_MASK BIT(0)
  119. #define CPU_PSKIP_STATUS 0x418
  120. #define XPU_PSKIP_STATUS_M_MASK (0xff << 12)
  121. #define XPU_PSKIP_STATUS_N_MASK (0xff << 4)
  122. #define XPU_PSKIP_STATUS_SW_OVERRIDE_MASK BIT(1)
  123. #define XPU_PSKIP_STATUS_ENABLED_MASK BIT(0)
  124. #define THROT_PRIORITY_LOCK 0x424
  125. #define THROT_PRIORITY_LOCK_PRIORITY_MASK 0xff
  126. #define THROT_STATUS 0x428
  127. #define THROT_STATUS_BREACH_MASK BIT(12)
  128. #define THROT_STATUS_STATE_MASK (0xff << 4)
  129. #define THROT_STATUS_ENABLED_MASK BIT(0)
  130. #define THROT_PSKIP_CTRL_LITE_CPU 0x430
  131. #define THROT_PSKIP_CTRL_ENABLE_MASK BIT(31)
  132. #define THROT_PSKIP_CTRL_DIVIDEND_MASK (0xff << 8)
  133. #define THROT_PSKIP_CTRL_DIVISOR_MASK 0xff
  134. #define THROT_PSKIP_CTRL_VECT_GPU_MASK (0x7 << 16)
  135. #define THROT_PSKIP_CTRL_VECT_CPU_MASK (0x7 << 8)
  136. #define THROT_PSKIP_CTRL_VECT2_CPU_MASK 0x7
  137. #define THROT_VECT_NONE 0x0 /* 3'b000 */
  138. #define THROT_VECT_LOW 0x1 /* 3'b001 */
  139. #define THROT_VECT_MED 0x3 /* 3'b011 */
  140. #define THROT_VECT_HIGH 0x7 /* 3'b111 */
  141. #define THROT_PSKIP_RAMP_LITE_CPU 0x434
  142. #define THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK BIT(31)
  143. #define THROT_PSKIP_RAMP_DURATION_MASK (0xffff << 8)
  144. #define THROT_PSKIP_RAMP_STEP_MASK 0xff
  145. #define THROT_PRIORITY_LITE 0x444
  146. #define THROT_PRIORITY_LITE_PRIO_MASK 0xff
  147. #define THROT_DELAY_LITE 0x448
  148. #define THROT_DELAY_LITE_DELAY_MASK 0xff
  149. /* car register offsets needed for enabling HW throttling */
  150. #define CAR_SUPER_CCLKG_DIVIDER 0x36c
  151. #define CDIVG_USE_THERM_CONTROLS_MASK BIT(30)
  152. /* ccroc register offsets needed for enabling HW throttling for Tegra132 */
  153. #define CCROC_SUPER_CCLKG_DIVIDER 0x024
  154. #define CCROC_GLOBAL_CFG 0x148
  155. #define CCROC_THROT_PSKIP_RAMP_CPU 0x150
  156. #define CCROC_THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK BIT(31)
  157. #define CCROC_THROT_PSKIP_RAMP_DURATION_MASK (0xffff << 8)
  158. #define CCROC_THROT_PSKIP_RAMP_STEP_MASK 0xff
  159. #define CCROC_THROT_PSKIP_CTRL_CPU 0x154
  160. #define CCROC_THROT_PSKIP_CTRL_ENB_MASK BIT(31)
  161. #define CCROC_THROT_PSKIP_CTRL_DIVIDEND_MASK (0xff << 8)
  162. #define CCROC_THROT_PSKIP_CTRL_DIVISOR_MASK 0xff
  163. /* get val from register(r) mask bits(m) */
  164. #define REG_GET_MASK(r, m) (((r) & (m)) >> (ffs(m) - 1))
  165. /* set val(v) to mask bits(m) of register(r) */
  166. #define REG_SET_MASK(r, m, v) (((r) & ~(m)) | \
  167. (((v) & (m >> (ffs(m) - 1))) << (ffs(m) - 1)))
  168. /* get dividend from the depth */
  169. #define THROT_DEPTH_DIVIDEND(depth) ((256 * (100 - (depth)) / 100) - 1)
  170. /* gk20a nv_therm interface N:3 Mapping. Levels defined in tegra124-soctherm.h
  171. * level vector
  172. * NONE 3'b000
  173. * LOW 3'b001
  174. * MED 3'b011
  175. * HIGH 3'b111
  176. */
  177. #define THROT_LEVEL_TO_DEPTH(level) ((0x1 << (level)) - 1)
  178. /* get THROT_PSKIP_xxx offset per LIGHT/HEAVY throt and CPU/GPU dev */
  179. #define THROT_OFFSET 0x30
  180. #define THROT_PSKIP_CTRL(throt, dev) (THROT_PSKIP_CTRL_LITE_CPU + \
  181. (THROT_OFFSET * throt) + (8 * dev))
  182. #define THROT_PSKIP_RAMP(throt, dev) (THROT_PSKIP_RAMP_LITE_CPU + \
  183. (THROT_OFFSET * throt) + (8 * dev))
  184. /* get THROT_xxx_CTRL offset per LIGHT/HEAVY throt */
  185. #define THROT_PRIORITY_CTRL(throt) (THROT_PRIORITY_LITE + \
  186. (THROT_OFFSET * throt))
  187. #define THROT_DELAY_CTRL(throt) (THROT_DELAY_LITE + \
  188. (THROT_OFFSET * throt))
  189. #define ALARM_OFFSET 0x14
  190. #define ALARM_CFG(throt) (OC1_CFG + \
  191. (ALARM_OFFSET * (throt - THROTTLE_OC1)))
  192. #define ALARM_CNT_THRESHOLD(throt) (OC1_CNT_THRESHOLD + \
  193. (ALARM_OFFSET * (throt - THROTTLE_OC1)))
  194. #define ALARM_THROTTLE_PERIOD(throt) (OC1_THROTTLE_PERIOD + \
  195. (ALARM_OFFSET * (throt - THROTTLE_OC1)))
  196. #define ALARM_ALARM_COUNT(throt) (OC1_ALARM_COUNT + \
  197. (ALARM_OFFSET * (throt - THROTTLE_OC1)))
  198. #define ALARM_FILTER(throt) (OC1_FILTER + \
  199. (ALARM_OFFSET * (throt - THROTTLE_OC1)))
  200. #define ALARM_STATS(throt) (OC1_STATS + \
  201. (4 * (throt - THROTTLE_OC1)))
  202. /* get CCROC_THROT_PSKIP_xxx offset per HIGH/MED/LOW vect*/
  203. #define CCROC_THROT_OFFSET 0x0c
  204. #define CCROC_THROT_PSKIP_CTRL_CPU_REG(vect) (CCROC_THROT_PSKIP_CTRL_CPU + \
  205. (CCROC_THROT_OFFSET * vect))
  206. #define CCROC_THROT_PSKIP_RAMP_CPU_REG(vect) (CCROC_THROT_PSKIP_RAMP_CPU + \
  207. (CCROC_THROT_OFFSET * vect))
  208. /* get THERMCTL_LEVELx offset per CPU/GPU/MEM/TSENSE rg and LEVEL0~3 lv */
  209. #define THERMCTL_LVL_REGS_SIZE 0x20
  210. #define THERMCTL_LVL_REG(rg, lv) ((rg) + ((lv) * THERMCTL_LVL_REGS_SIZE))
  211. #define OC_THROTTLE_MODE_DISABLED 0
  212. #define OC_THROTTLE_MODE_BRIEF 2
  213. static const int min_low_temp = -127000;
  214. static const int max_high_temp = 127000;
  215. enum soctherm_throttle_id {
  216. THROTTLE_LIGHT = 0,
  217. THROTTLE_HEAVY,
  218. THROTTLE_OC1,
  219. THROTTLE_OC2,
  220. THROTTLE_OC3,
  221. THROTTLE_OC4,
  222. THROTTLE_OC5, /* OC5 is reserved */
  223. THROTTLE_SIZE,
  224. };
  225. enum soctherm_oc_irq_id {
  226. TEGRA_SOC_OC_IRQ_1,
  227. TEGRA_SOC_OC_IRQ_2,
  228. TEGRA_SOC_OC_IRQ_3,
  229. TEGRA_SOC_OC_IRQ_4,
  230. TEGRA_SOC_OC_IRQ_5,
  231. TEGRA_SOC_OC_IRQ_MAX,
  232. };
  233. enum soctherm_throttle_dev_id {
  234. THROTTLE_DEV_CPU = 0,
  235. THROTTLE_DEV_GPU,
  236. THROTTLE_DEV_SIZE,
  237. };
  238. static const char *const throt_names[] = {
  239. [THROTTLE_LIGHT] = "light",
  240. [THROTTLE_HEAVY] = "heavy",
  241. [THROTTLE_OC1] = "oc1",
  242. [THROTTLE_OC2] = "oc2",
  243. [THROTTLE_OC3] = "oc3",
  244. [THROTTLE_OC4] = "oc4",
  245. [THROTTLE_OC5] = "oc5",
  246. };
  247. struct tegra_soctherm;
  248. struct tegra_thermctl_zone {
  249. void __iomem *reg;
  250. struct device *dev;
  251. struct tegra_soctherm *ts;
  252. struct thermal_zone_device *tz;
  253. const struct tegra_tsensor_group *sg;
  254. };
  255. struct soctherm_oc_cfg {
  256. u32 active_low;
  257. u32 throt_period;
  258. u32 alarm_cnt_thresh;
  259. u32 alarm_filter;
  260. u32 mode;
  261. bool intr_en;
  262. };
  263. struct soctherm_throt_cfg {
  264. const char *name;
  265. unsigned int id;
  266. u8 priority;
  267. u8 cpu_throt_level;
  268. u32 cpu_throt_depth;
  269. u32 gpu_throt_level;
  270. struct soctherm_oc_cfg oc_cfg;
  271. struct thermal_cooling_device *cdev;
  272. bool init;
  273. };
  274. struct tegra_soctherm {
  275. struct reset_control *reset;
  276. struct clk *clock_tsensor;
  277. struct clk *clock_soctherm;
  278. void __iomem *regs;
  279. void __iomem *clk_regs;
  280. void __iomem *ccroc_regs;
  281. int thermal_irq;
  282. int edp_irq;
  283. u32 *calib;
  284. struct thermal_zone_device **thermctl_tzs;
  285. struct tegra_soctherm_soc *soc;
  286. struct soctherm_throt_cfg throt_cfgs[THROTTLE_SIZE];
  287. struct dentry *debugfs_dir;
  288. struct mutex thermctl_lock;
  289. };
  290. struct soctherm_oc_irq_chip_data {
  291. struct mutex irq_lock; /* serialize OC IRQs */
  292. struct irq_chip irq_chip;
  293. struct irq_domain *domain;
  294. int irq_enable;
  295. };
  296. static struct soctherm_oc_irq_chip_data soc_irq_cdata;
  297. /**
  298. * ccroc_writel() - writes a value to a CCROC register
  299. * @ts: pointer to a struct tegra_soctherm
  300. * @value: the value to write
  301. * @reg: the register offset
  302. *
  303. * Writes @v to @reg. No return value.
  304. */
  305. static inline void ccroc_writel(struct tegra_soctherm *ts, u32 value, u32 reg)
  306. {
  307. writel(value, (ts->ccroc_regs + reg));
  308. }
  309. /**
  310. * ccroc_readl() - reads specified register from CCROC IP block
  311. * @ts: pointer to a struct tegra_soctherm
  312. * @reg: register address to be read
  313. *
  314. * Return: the value of the register
  315. */
  316. static inline u32 ccroc_readl(struct tegra_soctherm *ts, u32 reg)
  317. {
  318. return readl(ts->ccroc_regs + reg);
  319. }
  320. static void enable_tsensor(struct tegra_soctherm *tegra, unsigned int i)
  321. {
  322. const struct tegra_tsensor *sensor = &tegra->soc->tsensors[i];
  323. void __iomem *base = tegra->regs + sensor->base;
  324. unsigned int val;
  325. val = sensor->config->tall << SENSOR_CONFIG0_TALL_SHIFT;
  326. writel(val, base + SENSOR_CONFIG0);
  327. val = (sensor->config->tsample - 1) << SENSOR_CONFIG1_TSAMPLE_SHIFT;
  328. val |= sensor->config->tiddq_en << SENSOR_CONFIG1_TIDDQ_EN_SHIFT;
  329. val |= sensor->config->ten_count << SENSOR_CONFIG1_TEN_COUNT_SHIFT;
  330. val |= SENSOR_CONFIG1_TEMP_ENABLE;
  331. writel(val, base + SENSOR_CONFIG1);
  332. writel(tegra->calib[i], base + SENSOR_CONFIG2);
  333. }
  334. /*
  335. * Translate from soctherm readback format to millicelsius.
  336. * The soctherm readback format in bits is as follows:
  337. * TTTTTTTT H______N
  338. * where T's contain the temperature in Celsius,
  339. * H denotes an addition of 0.5 Celsius and N denotes negation
  340. * of the final value.
  341. */
  342. static int translate_temp(u16 val)
  343. {
  344. int t;
  345. t = ((val & READBACK_VALUE_MASK) >> READBACK_VALUE_SHIFT) * 1000;
  346. if (val & READBACK_ADD_HALF)
  347. t += 500;
  348. if (val & READBACK_NEGATE)
  349. t *= -1;
  350. return t;
  351. }
  352. static int tegra_thermctl_get_temp(struct thermal_zone_device *tz, int *out_temp)
  353. {
  354. struct tegra_thermctl_zone *zone = tz->devdata;
  355. u32 val;
  356. val = readl(zone->reg);
  357. val = REG_GET_MASK(val, zone->sg->sensor_temp_mask);
  358. *out_temp = translate_temp(val);
  359. return 0;
  360. }
  361. /**
  362. * enforce_temp_range() - check and enforce temperature range [min, max]
  363. * @dev: struct device * of the SOC_THERM instance
  364. * @trip_temp: the trip temperature to check
  365. *
  366. * Checks and enforces the permitted temperature range that SOC_THERM
  367. * HW can support This is
  368. * done while taking care of precision.
  369. *
  370. * Return: The precision adjusted capped temperature in millicelsius.
  371. */
  372. static int enforce_temp_range(struct device *dev, int trip_temp)
  373. {
  374. int temp;
  375. temp = clamp_val(trip_temp, min_low_temp, max_high_temp);
  376. if (temp != trip_temp)
  377. dev_dbg(dev, "soctherm: trip temperature %d forced to %d\n",
  378. trip_temp, temp);
  379. return temp;
  380. }
  381. /**
  382. * thermtrip_program() - Configures the hardware to shut down the
  383. * system if a given sensor group reaches a given temperature
  384. * @dev: ptr to the struct device for the SOC_THERM IP block
  385. * @sg: pointer to the sensor group to set the thermtrip temperature for
  386. * @trip_temp: the temperature in millicelsius to trigger the thermal trip at
  387. *
  388. * Sets the thermal trip threshold of the given sensor group to be the
  389. * @trip_temp. If this threshold is crossed, the hardware will shut
  390. * down.
  391. *
  392. * Note that, although @trip_temp is specified in millicelsius, the
  393. * hardware is programmed in degrees Celsius.
  394. *
  395. * Return: 0 upon success, or %-EINVAL upon failure.
  396. */
  397. static int thermtrip_program(struct device *dev,
  398. const struct tegra_tsensor_group *sg,
  399. int trip_temp)
  400. {
  401. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  402. int temp;
  403. u32 r;
  404. if (!sg || !sg->thermtrip_threshold_mask)
  405. return -EINVAL;
  406. temp = enforce_temp_range(dev, trip_temp) / ts->soc->thresh_grain;
  407. r = readl(ts->regs + THERMCTL_THERMTRIP_CTL);
  408. r = REG_SET_MASK(r, sg->thermtrip_threshold_mask, temp);
  409. r = REG_SET_MASK(r, sg->thermtrip_enable_mask, 1);
  410. r = REG_SET_MASK(r, sg->thermtrip_any_en_mask, 0);
  411. writel(r, ts->regs + THERMCTL_THERMTRIP_CTL);
  412. return 0;
  413. }
  414. /**
  415. * throttrip_program() - Configures the hardware to throttle the
  416. * pulse if a given sensor group reaches a given temperature
  417. * @dev: ptr to the struct device for the SOC_THERM IP block
  418. * @sg: pointer to the sensor group to set the thermtrip temperature for
  419. * @stc: pointer to the throttle need to be triggered
  420. * @trip_temp: the temperature in millicelsius to trigger the thermal trip at
  421. *
  422. * Sets the thermal trip threshold and throttle event of the given sensor
  423. * group. If this threshold is crossed, the hardware will trigger the
  424. * throttle.
  425. *
  426. * Note that, although @trip_temp is specified in millicelsius, the
  427. * hardware is programmed in degrees Celsius.
  428. *
  429. * Return: 0 upon success, or %-EINVAL upon failure.
  430. */
  431. static int throttrip_program(struct device *dev,
  432. const struct tegra_tsensor_group *sg,
  433. struct soctherm_throt_cfg *stc,
  434. int trip_temp)
  435. {
  436. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  437. int temp, cpu_throt, gpu_throt;
  438. unsigned int throt;
  439. u32 r, reg_off;
  440. if (!sg || !stc || !stc->init)
  441. return -EINVAL;
  442. temp = enforce_temp_range(dev, trip_temp) / ts->soc->thresh_grain;
  443. /* Hardcode LIGHT on LEVEL1 and HEAVY on LEVEL2 */
  444. throt = stc->id;
  445. reg_off = THERMCTL_LVL_REG(sg->thermctl_lvl0_offset, throt + 1);
  446. if (throt == THROTTLE_LIGHT) {
  447. cpu_throt = THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT;
  448. gpu_throt = THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT;
  449. } else {
  450. cpu_throt = THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY;
  451. gpu_throt = THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY;
  452. if (throt != THROTTLE_HEAVY)
  453. dev_warn(dev,
  454. "invalid throt id %d - assuming HEAVY",
  455. throt);
  456. }
  457. r = readl(ts->regs + reg_off);
  458. r = REG_SET_MASK(r, sg->thermctl_lvl0_up_thresh_mask, temp);
  459. r = REG_SET_MASK(r, sg->thermctl_lvl0_dn_thresh_mask, temp);
  460. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_CPU_THROT_MASK, cpu_throt);
  461. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_GPU_THROT_MASK, gpu_throt);
  462. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_EN_MASK, 1);
  463. writel(r, ts->regs + reg_off);
  464. return 0;
  465. }
  466. static struct soctherm_throt_cfg *
  467. find_throttle_cfg_by_name(struct tegra_soctherm *ts, const char *name)
  468. {
  469. unsigned int i;
  470. for (i = 0; ts->throt_cfgs[i].name; i++)
  471. if (!strcmp(ts->throt_cfgs[i].name, name))
  472. return &ts->throt_cfgs[i];
  473. return NULL;
  474. }
  475. static int tsensor_group_thermtrip_get(struct tegra_soctherm *ts, int id)
  476. {
  477. int i, temp = min_low_temp;
  478. struct tsensor_group_thermtrips *tt = ts->soc->thermtrips;
  479. if (id >= TEGRA124_SOCTHERM_SENSOR_NUM)
  480. return temp;
  481. if (tt) {
  482. for (i = 0; i < ts->soc->num_ttgs; i++) {
  483. if (tt[i].id == id)
  484. return tt[i].temp;
  485. }
  486. }
  487. return temp;
  488. }
  489. static int tegra_thermctl_set_trip_temp(struct thermal_zone_device *tz, int trip, int temp)
  490. {
  491. struct tegra_thermctl_zone *zone = tz->devdata;
  492. struct tegra_soctherm *ts = zone->ts;
  493. const struct tegra_tsensor_group *sg = zone->sg;
  494. struct device *dev = zone->dev;
  495. enum thermal_trip_type type;
  496. int ret;
  497. if (!tz)
  498. return -EINVAL;
  499. ret = tz->ops->get_trip_type(tz, trip, &type);
  500. if (ret)
  501. return ret;
  502. if (type == THERMAL_TRIP_CRITICAL) {
  503. /*
  504. * If thermtrips property is set in DT,
  505. * doesn't need to program critical type trip to HW,
  506. * if not, program critical trip to HW.
  507. */
  508. if (min_low_temp == tsensor_group_thermtrip_get(ts, sg->id))
  509. return thermtrip_program(dev, sg, temp);
  510. else
  511. return 0;
  512. } else if (type == THERMAL_TRIP_HOT) {
  513. int i;
  514. for (i = 0; i < THROTTLE_SIZE; i++) {
  515. struct thermal_cooling_device *cdev;
  516. struct soctherm_throt_cfg *stc;
  517. if (!ts->throt_cfgs[i].init)
  518. continue;
  519. cdev = ts->throt_cfgs[i].cdev;
  520. if (get_thermal_instance(tz, cdev, trip))
  521. stc = find_throttle_cfg_by_name(ts, cdev->type);
  522. else
  523. continue;
  524. return throttrip_program(dev, sg, stc, temp);
  525. }
  526. }
  527. return 0;
  528. }
  529. static void thermal_irq_enable(struct tegra_thermctl_zone *zn)
  530. {
  531. u32 r;
  532. /* multiple zones could be handling and setting trips at once */
  533. mutex_lock(&zn->ts->thermctl_lock);
  534. r = readl(zn->ts->regs + THERMCTL_INTR_ENABLE);
  535. r = REG_SET_MASK(r, zn->sg->thermctl_isr_mask, TH_INTR_UP_DN_EN);
  536. writel(r, zn->ts->regs + THERMCTL_INTR_ENABLE);
  537. mutex_unlock(&zn->ts->thermctl_lock);
  538. }
  539. static void thermal_irq_disable(struct tegra_thermctl_zone *zn)
  540. {
  541. u32 r;
  542. /* multiple zones could be handling and setting trips at once */
  543. mutex_lock(&zn->ts->thermctl_lock);
  544. r = readl(zn->ts->regs + THERMCTL_INTR_DISABLE);
  545. r = REG_SET_MASK(r, zn->sg->thermctl_isr_mask, 0);
  546. writel(r, zn->ts->regs + THERMCTL_INTR_DISABLE);
  547. mutex_unlock(&zn->ts->thermctl_lock);
  548. }
  549. static int tegra_thermctl_set_trips(struct thermal_zone_device *tz, int lo, int hi)
  550. {
  551. struct tegra_thermctl_zone *zone = tz->devdata;
  552. u32 r;
  553. thermal_irq_disable(zone);
  554. r = readl(zone->ts->regs + zone->sg->thermctl_lvl0_offset);
  555. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_EN_MASK, 0);
  556. writel(r, zone->ts->regs + zone->sg->thermctl_lvl0_offset);
  557. lo = enforce_temp_range(zone->dev, lo) / zone->ts->soc->thresh_grain;
  558. hi = enforce_temp_range(zone->dev, hi) / zone->ts->soc->thresh_grain;
  559. dev_dbg(zone->dev, "%s hi:%d, lo:%d\n", __func__, hi, lo);
  560. r = REG_SET_MASK(r, zone->sg->thermctl_lvl0_up_thresh_mask, hi);
  561. r = REG_SET_MASK(r, zone->sg->thermctl_lvl0_dn_thresh_mask, lo);
  562. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_EN_MASK, 1);
  563. writel(r, zone->ts->regs + zone->sg->thermctl_lvl0_offset);
  564. thermal_irq_enable(zone);
  565. return 0;
  566. }
  567. static const struct thermal_zone_device_ops tegra_of_thermal_ops = {
  568. .get_temp = tegra_thermctl_get_temp,
  569. .set_trip_temp = tegra_thermctl_set_trip_temp,
  570. .set_trips = tegra_thermctl_set_trips,
  571. };
  572. static int get_hot_temp(struct thermal_zone_device *tz, int *trip, int *temp)
  573. {
  574. int ntrips, i, ret;
  575. enum thermal_trip_type type;
  576. ntrips = of_thermal_get_ntrips(tz);
  577. if (ntrips <= 0)
  578. return -EINVAL;
  579. for (i = 0; i < ntrips; i++) {
  580. ret = tz->ops->get_trip_type(tz, i, &type);
  581. if (ret)
  582. return -EINVAL;
  583. if (type == THERMAL_TRIP_HOT) {
  584. ret = tz->ops->get_trip_temp(tz, i, temp);
  585. if (!ret)
  586. *trip = i;
  587. return ret;
  588. }
  589. }
  590. return -EINVAL;
  591. }
  592. /**
  593. * tegra_soctherm_set_hwtrips() - set HW trip point from DT data
  594. * @dev: struct device * of the SOC_THERM instance
  595. * @sg: pointer to the sensor group to set the thermtrip temperature for
  596. * @tz: struct thermal_zone_device *
  597. *
  598. * Configure the SOC_THERM HW trip points, setting "THERMTRIP"
  599. * "THROTTLE" trip points , using "thermtrips", "critical" or "hot"
  600. * type trip_temp
  601. * from thermal zone.
  602. * After they have been configured, THERMTRIP or THROTTLE will take
  603. * action when the configured SoC thermal sensor group reaches a
  604. * certain temperature.
  605. *
  606. * Return: 0 upon success, or a negative error code on failure.
  607. * "Success" does not mean that trips was enabled; it could also
  608. * mean that no node was found in DT.
  609. * THERMTRIP has been enabled successfully when a message similar to
  610. * this one appears on the serial console:
  611. * "thermtrip: will shut down when sensor group XXX reaches YYYYYY mC"
  612. * THROTTLE has been enabled successfully when a message similar to
  613. * this one appears on the serial console:
  614. * ""throttrip: will throttle when sensor group XXX reaches YYYYYY mC"
  615. */
  616. static int tegra_soctherm_set_hwtrips(struct device *dev,
  617. const struct tegra_tsensor_group *sg,
  618. struct thermal_zone_device *tz)
  619. {
  620. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  621. struct soctherm_throt_cfg *stc;
  622. int i, trip, temperature, ret;
  623. /* Get thermtrips. If missing, try to get critical trips. */
  624. temperature = tsensor_group_thermtrip_get(ts, sg->id);
  625. if (min_low_temp == temperature)
  626. if (tz->ops->get_crit_temp(tz, &temperature))
  627. temperature = max_high_temp;
  628. ret = thermtrip_program(dev, sg, temperature);
  629. if (ret) {
  630. dev_err(dev, "thermtrip: %s: error during enable\n", sg->name);
  631. return ret;
  632. }
  633. dev_info(dev, "thermtrip: will shut down when %s reaches %d mC\n",
  634. sg->name, temperature);
  635. ret = get_hot_temp(tz, &trip, &temperature);
  636. if (ret) {
  637. dev_info(dev, "throttrip: %s: missing hot temperature\n",
  638. sg->name);
  639. return 0;
  640. }
  641. for (i = 0; i < THROTTLE_OC1; i++) {
  642. struct thermal_cooling_device *cdev;
  643. if (!ts->throt_cfgs[i].init)
  644. continue;
  645. cdev = ts->throt_cfgs[i].cdev;
  646. if (get_thermal_instance(tz, cdev, trip))
  647. stc = find_throttle_cfg_by_name(ts, cdev->type);
  648. else
  649. continue;
  650. ret = throttrip_program(dev, sg, stc, temperature);
  651. if (ret) {
  652. dev_err(dev, "throttrip: %s: error during enable\n",
  653. sg->name);
  654. return ret;
  655. }
  656. dev_info(dev,
  657. "throttrip: will throttle when %s reaches %d mC\n",
  658. sg->name, temperature);
  659. break;
  660. }
  661. if (i == THROTTLE_SIZE)
  662. dev_info(dev, "throttrip: %s: missing throttle cdev\n",
  663. sg->name);
  664. return 0;
  665. }
  666. static irqreturn_t soctherm_thermal_isr(int irq, void *dev_id)
  667. {
  668. struct tegra_soctherm *ts = dev_id;
  669. u32 r;
  670. /* Case for no lock:
  671. * Although interrupts are enabled in set_trips, there is still no need
  672. * to lock here because the interrupts are disabled before programming
  673. * new trip points. Hence there cant be a interrupt on the same sensor.
  674. * An interrupt can however occur on a sensor while trips are being
  675. * programmed on a different one. This beign a LEVEL interrupt won't
  676. * cause a new interrupt but this is taken care of by the re-reading of
  677. * the STATUS register in the thread function.
  678. */
  679. r = readl(ts->regs + THERMCTL_INTR_STATUS);
  680. writel(r, ts->regs + THERMCTL_INTR_DISABLE);
  681. return IRQ_WAKE_THREAD;
  682. }
  683. /**
  684. * soctherm_thermal_isr_thread() - Handles a thermal interrupt request
  685. * @irq: The interrupt number being requested; not used
  686. * @dev_id: Opaque pointer to tegra_soctherm;
  687. *
  688. * Clears the interrupt status register if there are expected
  689. * interrupt bits set.
  690. * The interrupt(s) are then handled by updating the corresponding
  691. * thermal zones.
  692. *
  693. * An error is logged if any unexpected interrupt bits are set.
  694. *
  695. * Disabled interrupts are re-enabled.
  696. *
  697. * Return: %IRQ_HANDLED. Interrupt was handled and no further processing
  698. * is needed.
  699. */
  700. static irqreturn_t soctherm_thermal_isr_thread(int irq, void *dev_id)
  701. {
  702. struct tegra_soctherm *ts = dev_id;
  703. struct thermal_zone_device *tz;
  704. u32 st, ex = 0, cp = 0, gp = 0, pl = 0, me = 0;
  705. st = readl(ts->regs + THERMCTL_INTR_STATUS);
  706. /* deliberately clear expected interrupts handled in SW */
  707. cp |= st & TH_INTR_CD0_MASK;
  708. cp |= st & TH_INTR_CU0_MASK;
  709. gp |= st & TH_INTR_GD0_MASK;
  710. gp |= st & TH_INTR_GU0_MASK;
  711. pl |= st & TH_INTR_PD0_MASK;
  712. pl |= st & TH_INTR_PU0_MASK;
  713. me |= st & TH_INTR_MD0_MASK;
  714. me |= st & TH_INTR_MU0_MASK;
  715. ex |= cp | gp | pl | me;
  716. if (ex) {
  717. writel(ex, ts->regs + THERMCTL_INTR_STATUS);
  718. st &= ~ex;
  719. if (cp) {
  720. tz = ts->thermctl_tzs[TEGRA124_SOCTHERM_SENSOR_CPU];
  721. thermal_zone_device_update(tz,
  722. THERMAL_EVENT_UNSPECIFIED);
  723. }
  724. if (gp) {
  725. tz = ts->thermctl_tzs[TEGRA124_SOCTHERM_SENSOR_GPU];
  726. thermal_zone_device_update(tz,
  727. THERMAL_EVENT_UNSPECIFIED);
  728. }
  729. if (pl) {
  730. tz = ts->thermctl_tzs[TEGRA124_SOCTHERM_SENSOR_PLLX];
  731. thermal_zone_device_update(tz,
  732. THERMAL_EVENT_UNSPECIFIED);
  733. }
  734. if (me) {
  735. tz = ts->thermctl_tzs[TEGRA124_SOCTHERM_SENSOR_MEM];
  736. thermal_zone_device_update(tz,
  737. THERMAL_EVENT_UNSPECIFIED);
  738. }
  739. }
  740. /* deliberately ignore expected interrupts NOT handled in SW */
  741. ex |= TH_INTR_IGNORE_MASK;
  742. st &= ~ex;
  743. if (st) {
  744. /* Whine about any other unexpected INTR bits still set */
  745. pr_err("soctherm: Ignored unexpected INTRs 0x%08x\n", st);
  746. writel(st, ts->regs + THERMCTL_INTR_STATUS);
  747. }
  748. return IRQ_HANDLED;
  749. }
  750. /**
  751. * soctherm_oc_intr_enable() - Enables the soctherm over-current interrupt
  752. * @ts: pointer to a struct tegra_soctherm
  753. * @alarm: The soctherm throttle id
  754. * @enable: Flag indicating enable the soctherm over-current
  755. * interrupt or disable it
  756. *
  757. * Enables a specific over-current pins @alarm to raise an interrupt if the flag
  758. * is set and the alarm corresponds to OC1, OC2, OC3, or OC4.
  759. */
  760. static void soctherm_oc_intr_enable(struct tegra_soctherm *ts,
  761. enum soctherm_throttle_id alarm,
  762. bool enable)
  763. {
  764. u32 r;
  765. if (!enable)
  766. return;
  767. r = readl(ts->regs + OC_INTR_ENABLE);
  768. switch (alarm) {
  769. case THROTTLE_OC1:
  770. r = REG_SET_MASK(r, OC_INTR_OC1_MASK, 1);
  771. break;
  772. case THROTTLE_OC2:
  773. r = REG_SET_MASK(r, OC_INTR_OC2_MASK, 1);
  774. break;
  775. case THROTTLE_OC3:
  776. r = REG_SET_MASK(r, OC_INTR_OC3_MASK, 1);
  777. break;
  778. case THROTTLE_OC4:
  779. r = REG_SET_MASK(r, OC_INTR_OC4_MASK, 1);
  780. break;
  781. default:
  782. r = 0;
  783. break;
  784. }
  785. writel(r, ts->regs + OC_INTR_ENABLE);
  786. }
  787. /**
  788. * soctherm_handle_alarm() - Handles soctherm alarms
  789. * @alarm: The soctherm throttle id
  790. *
  791. * "Handles" over-current alarms (OC1, OC2, OC3, and OC4) by printing
  792. * a warning or informative message.
  793. *
  794. * Return: -EINVAL for @alarm = THROTTLE_OC3, otherwise 0 (success).
  795. */
  796. static int soctherm_handle_alarm(enum soctherm_throttle_id alarm)
  797. {
  798. int rv = -EINVAL;
  799. switch (alarm) {
  800. case THROTTLE_OC1:
  801. pr_debug("soctherm: Successfully handled OC1 alarm\n");
  802. rv = 0;
  803. break;
  804. case THROTTLE_OC2:
  805. pr_debug("soctherm: Successfully handled OC2 alarm\n");
  806. rv = 0;
  807. break;
  808. case THROTTLE_OC3:
  809. pr_debug("soctherm: Successfully handled OC3 alarm\n");
  810. rv = 0;
  811. break;
  812. case THROTTLE_OC4:
  813. pr_debug("soctherm: Successfully handled OC4 alarm\n");
  814. rv = 0;
  815. break;
  816. default:
  817. break;
  818. }
  819. if (rv)
  820. pr_err("soctherm: ERROR in handling %s alarm\n",
  821. throt_names[alarm]);
  822. return rv;
  823. }
  824. /**
  825. * soctherm_edp_isr_thread() - log an over-current interrupt request
  826. * @irq: OC irq number. Currently not being used. See description
  827. * @arg: a void pointer for callback, currently not being used
  828. *
  829. * Over-current events are handled in hardware. This function is called to log
  830. * and handle any OC events that happened. Additionally, it checks every
  831. * over-current interrupt registers for registers are set but
  832. * was not expected (i.e. any discrepancy in interrupt status) by the function,
  833. * the discrepancy will logged.
  834. *
  835. * Return: %IRQ_HANDLED
  836. */
  837. static irqreturn_t soctherm_edp_isr_thread(int irq, void *arg)
  838. {
  839. struct tegra_soctherm *ts = arg;
  840. u32 st, ex, oc1, oc2, oc3, oc4;
  841. st = readl(ts->regs + OC_INTR_STATUS);
  842. /* deliberately clear expected interrupts handled in SW */
  843. oc1 = st & OC_INTR_OC1_MASK;
  844. oc2 = st & OC_INTR_OC2_MASK;
  845. oc3 = st & OC_INTR_OC3_MASK;
  846. oc4 = st & OC_INTR_OC4_MASK;
  847. ex = oc1 | oc2 | oc3 | oc4;
  848. pr_err("soctherm: OC ALARM 0x%08x\n", ex);
  849. if (ex) {
  850. writel(st, ts->regs + OC_INTR_STATUS);
  851. st &= ~ex;
  852. if (oc1 && !soctherm_handle_alarm(THROTTLE_OC1))
  853. soctherm_oc_intr_enable(ts, THROTTLE_OC1, true);
  854. if (oc2 && !soctherm_handle_alarm(THROTTLE_OC2))
  855. soctherm_oc_intr_enable(ts, THROTTLE_OC2, true);
  856. if (oc3 && !soctherm_handle_alarm(THROTTLE_OC3))
  857. soctherm_oc_intr_enable(ts, THROTTLE_OC3, true);
  858. if (oc4 && !soctherm_handle_alarm(THROTTLE_OC4))
  859. soctherm_oc_intr_enable(ts, THROTTLE_OC4, true);
  860. if (oc1 && soc_irq_cdata.irq_enable & BIT(0))
  861. handle_nested_irq(
  862. irq_find_mapping(soc_irq_cdata.domain, 0));
  863. if (oc2 && soc_irq_cdata.irq_enable & BIT(1))
  864. handle_nested_irq(
  865. irq_find_mapping(soc_irq_cdata.domain, 1));
  866. if (oc3 && soc_irq_cdata.irq_enable & BIT(2))
  867. handle_nested_irq(
  868. irq_find_mapping(soc_irq_cdata.domain, 2));
  869. if (oc4 && soc_irq_cdata.irq_enable & BIT(3))
  870. handle_nested_irq(
  871. irq_find_mapping(soc_irq_cdata.domain, 3));
  872. }
  873. if (st) {
  874. pr_err("soctherm: Ignored unexpected OC ALARM 0x%08x\n", st);
  875. writel(st, ts->regs + OC_INTR_STATUS);
  876. }
  877. return IRQ_HANDLED;
  878. }
  879. /**
  880. * soctherm_edp_isr() - Disables any active interrupts
  881. * @irq: The interrupt request number
  882. * @arg: Opaque pointer to an argument
  883. *
  884. * Writes to the OC_INTR_DISABLE register the over current interrupt status,
  885. * masking any asserted interrupts. Doing this prevents the same interrupts
  886. * from triggering this isr repeatedly. The thread woken by this isr will
  887. * handle asserted interrupts and subsequently unmask/re-enable them.
  888. *
  889. * The OC_INTR_DISABLE register indicates which OC interrupts
  890. * have been disabled.
  891. *
  892. * Return: %IRQ_WAKE_THREAD, handler requests to wake the handler thread
  893. */
  894. static irqreturn_t soctherm_edp_isr(int irq, void *arg)
  895. {
  896. struct tegra_soctherm *ts = arg;
  897. u32 r;
  898. if (!ts)
  899. return IRQ_NONE;
  900. r = readl(ts->regs + OC_INTR_STATUS);
  901. writel(r, ts->regs + OC_INTR_DISABLE);
  902. return IRQ_WAKE_THREAD;
  903. }
  904. /**
  905. * soctherm_oc_irq_lock() - locks the over-current interrupt request
  906. * @data: Interrupt request data
  907. *
  908. * Looks up the chip data from @data and locks the mutex associated with
  909. * a particular over-current interrupt request.
  910. */
  911. static void soctherm_oc_irq_lock(struct irq_data *data)
  912. {
  913. struct soctherm_oc_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  914. mutex_lock(&d->irq_lock);
  915. }
  916. /**
  917. * soctherm_oc_irq_sync_unlock() - Unlocks the OC interrupt request
  918. * @data: Interrupt request data
  919. *
  920. * Looks up the interrupt request data @data and unlocks the mutex associated
  921. * with a particular over-current interrupt request.
  922. */
  923. static void soctherm_oc_irq_sync_unlock(struct irq_data *data)
  924. {
  925. struct soctherm_oc_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  926. mutex_unlock(&d->irq_lock);
  927. }
  928. /**
  929. * soctherm_oc_irq_enable() - Enables the SOC_THERM over-current interrupt queue
  930. * @data: irq_data structure of the chip
  931. *
  932. * Sets the irq_enable bit of SOC_THERM allowing SOC_THERM
  933. * to respond to over-current interrupts.
  934. *
  935. */
  936. static void soctherm_oc_irq_enable(struct irq_data *data)
  937. {
  938. struct soctherm_oc_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  939. d->irq_enable |= BIT(data->hwirq);
  940. }
  941. /**
  942. * soctherm_oc_irq_disable() - Disables overcurrent interrupt requests
  943. * @data: The interrupt request information
  944. *
  945. * Clears the interrupt request enable bit of the overcurrent
  946. * interrupt request chip data.
  947. *
  948. * Return: Nothing is returned (void)
  949. */
  950. static void soctherm_oc_irq_disable(struct irq_data *data)
  951. {
  952. struct soctherm_oc_irq_chip_data *d = irq_data_get_irq_chip_data(data);
  953. d->irq_enable &= ~BIT(data->hwirq);
  954. }
  955. static int soctherm_oc_irq_set_type(struct irq_data *data, unsigned int type)
  956. {
  957. return 0;
  958. }
  959. /**
  960. * soctherm_oc_irq_map() - SOC_THERM interrupt request domain mapper
  961. * @h: Interrupt request domain
  962. * @virq: Virtual interrupt request number
  963. * @hw: Hardware interrupt request number
  964. *
  965. * Mapping callback function for SOC_THERM's irq_domain. When a SOC_THERM
  966. * interrupt request is called, the irq_domain takes the request's virtual
  967. * request number (much like a virtual memory address) and maps it to a
  968. * physical hardware request number.
  969. *
  970. * When a mapping doesn't already exist for a virtual request number, the
  971. * irq_domain calls this function to associate the virtual request number with
  972. * a hardware request number.
  973. *
  974. * Return: 0
  975. */
  976. static int soctherm_oc_irq_map(struct irq_domain *h, unsigned int virq,
  977. irq_hw_number_t hw)
  978. {
  979. struct soctherm_oc_irq_chip_data *data = h->host_data;
  980. irq_set_chip_data(virq, data);
  981. irq_set_chip(virq, &data->irq_chip);
  982. irq_set_nested_thread(virq, 1);
  983. return 0;
  984. }
  985. /**
  986. * soctherm_irq_domain_xlate_twocell() - xlate for soctherm interrupts
  987. * @d: Interrupt request domain
  988. * @ctrlr: Controller device tree node
  989. * @intspec: Array of u32s from DTs "interrupt" property
  990. * @intsize: Number of values inside the intspec array
  991. * @out_hwirq: HW IRQ value associated with this interrupt
  992. * @out_type: The IRQ SENSE type for this interrupt.
  993. *
  994. * This Device Tree IRQ specifier translation function will translate a
  995. * specific "interrupt" as defined by 2 DT values where the cell values map
  996. * the hwirq number + 1 and linux irq flags. Since the output is the hwirq
  997. * number, this function will subtract 1 from the value listed in DT.
  998. *
  999. * Return: 0
  1000. */
  1001. static int soctherm_irq_domain_xlate_twocell(struct irq_domain *d,
  1002. struct device_node *ctrlr, const u32 *intspec, unsigned int intsize,
  1003. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  1004. {
  1005. if (WARN_ON(intsize < 2))
  1006. return -EINVAL;
  1007. /*
  1008. * The HW value is 1 index less than the DT IRQ values.
  1009. * i.e. OC4 goes to HW index 3.
  1010. */
  1011. *out_hwirq = intspec[0] - 1;
  1012. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  1013. return 0;
  1014. }
  1015. static const struct irq_domain_ops soctherm_oc_domain_ops = {
  1016. .map = soctherm_oc_irq_map,
  1017. .xlate = soctherm_irq_domain_xlate_twocell,
  1018. };
  1019. /**
  1020. * soctherm_oc_int_init() - Initial enabling of the over
  1021. * current interrupts
  1022. * @np: The devicetree node for soctherm
  1023. * @num_irqs: The number of new interrupt requests
  1024. *
  1025. * Sets the over current interrupt request chip data
  1026. *
  1027. * Return: 0 on success or if overcurrent interrupts are not enabled,
  1028. * -ENOMEM (out of memory), or irq_base if the function failed to
  1029. * allocate the irqs
  1030. */
  1031. static int soctherm_oc_int_init(struct device_node *np, int num_irqs)
  1032. {
  1033. if (!num_irqs) {
  1034. pr_info("%s(): OC interrupts are not enabled\n", __func__);
  1035. return 0;
  1036. }
  1037. mutex_init(&soc_irq_cdata.irq_lock);
  1038. soc_irq_cdata.irq_enable = 0;
  1039. soc_irq_cdata.irq_chip.name = "soc_therm_oc";
  1040. soc_irq_cdata.irq_chip.irq_bus_lock = soctherm_oc_irq_lock;
  1041. soc_irq_cdata.irq_chip.irq_bus_sync_unlock =
  1042. soctherm_oc_irq_sync_unlock;
  1043. soc_irq_cdata.irq_chip.irq_disable = soctherm_oc_irq_disable;
  1044. soc_irq_cdata.irq_chip.irq_enable = soctherm_oc_irq_enable;
  1045. soc_irq_cdata.irq_chip.irq_set_type = soctherm_oc_irq_set_type;
  1046. soc_irq_cdata.irq_chip.irq_set_wake = NULL;
  1047. soc_irq_cdata.domain = irq_domain_add_linear(np, num_irqs,
  1048. &soctherm_oc_domain_ops,
  1049. &soc_irq_cdata);
  1050. if (!soc_irq_cdata.domain) {
  1051. pr_err("%s: Failed to create IRQ domain\n", __func__);
  1052. return -ENOMEM;
  1053. }
  1054. pr_debug("%s(): OC interrupts enabled successful\n", __func__);
  1055. return 0;
  1056. }
  1057. #ifdef CONFIG_DEBUG_FS
  1058. static int regs_show(struct seq_file *s, void *data)
  1059. {
  1060. struct platform_device *pdev = s->private;
  1061. struct tegra_soctherm *ts = platform_get_drvdata(pdev);
  1062. const struct tegra_tsensor *tsensors = ts->soc->tsensors;
  1063. const struct tegra_tsensor_group **ttgs = ts->soc->ttgs;
  1064. u32 r, state;
  1065. int i, level;
  1066. seq_puts(s, "-----TSENSE (convert HW)-----\n");
  1067. for (i = 0; i < ts->soc->num_tsensors; i++) {
  1068. r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG1);
  1069. state = REG_GET_MASK(r, SENSOR_CONFIG1_TEMP_ENABLE);
  1070. seq_printf(s, "%s: ", tsensors[i].name);
  1071. seq_printf(s, "En(%d) ", state);
  1072. if (!state) {
  1073. seq_puts(s, "\n");
  1074. continue;
  1075. }
  1076. state = REG_GET_MASK(r, SENSOR_CONFIG1_TIDDQ_EN_MASK);
  1077. seq_printf(s, "tiddq(%d) ", state);
  1078. state = REG_GET_MASK(r, SENSOR_CONFIG1_TEN_COUNT_MASK);
  1079. seq_printf(s, "ten_count(%d) ", state);
  1080. state = REG_GET_MASK(r, SENSOR_CONFIG1_TSAMPLE_MASK);
  1081. seq_printf(s, "tsample(%d) ", state + 1);
  1082. r = readl(ts->regs + tsensors[i].base + SENSOR_STATUS1);
  1083. state = REG_GET_MASK(r, SENSOR_STATUS1_TEMP_VALID_MASK);
  1084. seq_printf(s, "Temp(%d/", state);
  1085. state = REG_GET_MASK(r, SENSOR_STATUS1_TEMP_MASK);
  1086. seq_printf(s, "%d) ", translate_temp(state));
  1087. r = readl(ts->regs + tsensors[i].base + SENSOR_STATUS0);
  1088. state = REG_GET_MASK(r, SENSOR_STATUS0_VALID_MASK);
  1089. seq_printf(s, "Capture(%d/", state);
  1090. state = REG_GET_MASK(r, SENSOR_STATUS0_CAPTURE_MASK);
  1091. seq_printf(s, "%d) ", state);
  1092. r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG0);
  1093. state = REG_GET_MASK(r, SENSOR_CONFIG0_STOP);
  1094. seq_printf(s, "Stop(%d) ", state);
  1095. state = REG_GET_MASK(r, SENSOR_CONFIG0_TALL_MASK);
  1096. seq_printf(s, "Tall(%d) ", state);
  1097. state = REG_GET_MASK(r, SENSOR_CONFIG0_TCALC_OVER);
  1098. seq_printf(s, "Over(%d/", state);
  1099. state = REG_GET_MASK(r, SENSOR_CONFIG0_OVER);
  1100. seq_printf(s, "%d/", state);
  1101. state = REG_GET_MASK(r, SENSOR_CONFIG0_CPTR_OVER);
  1102. seq_printf(s, "%d) ", state);
  1103. r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG2);
  1104. state = REG_GET_MASK(r, SENSOR_CONFIG2_THERMA_MASK);
  1105. seq_printf(s, "Therm_A/B(%d/", state);
  1106. state = REG_GET_MASK(r, SENSOR_CONFIG2_THERMB_MASK);
  1107. seq_printf(s, "%d)\n", (s16)state);
  1108. }
  1109. r = readl(ts->regs + SENSOR_PDIV);
  1110. seq_printf(s, "PDIV: 0x%x\n", r);
  1111. r = readl(ts->regs + SENSOR_HOTSPOT_OFF);
  1112. seq_printf(s, "HOTSPOT: 0x%x\n", r);
  1113. seq_puts(s, "\n");
  1114. seq_puts(s, "-----SOC_THERM-----\n");
  1115. r = readl(ts->regs + SENSOR_TEMP1);
  1116. state = REG_GET_MASK(r, SENSOR_TEMP1_CPU_TEMP_MASK);
  1117. seq_printf(s, "Temperatures: CPU(%d) ", translate_temp(state));
  1118. state = REG_GET_MASK(r, SENSOR_TEMP1_GPU_TEMP_MASK);
  1119. seq_printf(s, " GPU(%d) ", translate_temp(state));
  1120. r = readl(ts->regs + SENSOR_TEMP2);
  1121. state = REG_GET_MASK(r, SENSOR_TEMP2_PLLX_TEMP_MASK);
  1122. seq_printf(s, " PLLX(%d) ", translate_temp(state));
  1123. state = REG_GET_MASK(r, SENSOR_TEMP2_MEM_TEMP_MASK);
  1124. seq_printf(s, " MEM(%d)\n", translate_temp(state));
  1125. for (i = 0; i < ts->soc->num_ttgs; i++) {
  1126. seq_printf(s, "%s:\n", ttgs[i]->name);
  1127. for (level = 0; level < 4; level++) {
  1128. s32 v;
  1129. u32 mask;
  1130. u16 off = ttgs[i]->thermctl_lvl0_offset;
  1131. r = readl(ts->regs + THERMCTL_LVL_REG(off, level));
  1132. mask = ttgs[i]->thermctl_lvl0_up_thresh_mask;
  1133. state = REG_GET_MASK(r, mask);
  1134. v = sign_extend32(state, ts->soc->bptt - 1);
  1135. v *= ts->soc->thresh_grain;
  1136. seq_printf(s, " %d: Up/Dn(%d /", level, v);
  1137. mask = ttgs[i]->thermctl_lvl0_dn_thresh_mask;
  1138. state = REG_GET_MASK(r, mask);
  1139. v = sign_extend32(state, ts->soc->bptt - 1);
  1140. v *= ts->soc->thresh_grain;
  1141. seq_printf(s, "%d ) ", v);
  1142. mask = THERMCTL_LVL0_CPU0_EN_MASK;
  1143. state = REG_GET_MASK(r, mask);
  1144. seq_printf(s, "En(%d) ", state);
  1145. mask = THERMCTL_LVL0_CPU0_CPU_THROT_MASK;
  1146. state = REG_GET_MASK(r, mask);
  1147. seq_puts(s, "CPU Throt");
  1148. if (!state)
  1149. seq_printf(s, "(%s) ", "none");
  1150. else if (state == THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT)
  1151. seq_printf(s, "(%s) ", "L");
  1152. else if (state == THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY)
  1153. seq_printf(s, "(%s) ", "H");
  1154. else
  1155. seq_printf(s, "(%s) ", "H+L");
  1156. mask = THERMCTL_LVL0_CPU0_GPU_THROT_MASK;
  1157. state = REG_GET_MASK(r, mask);
  1158. seq_puts(s, "GPU Throt");
  1159. if (!state)
  1160. seq_printf(s, "(%s) ", "none");
  1161. else if (state == THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT)
  1162. seq_printf(s, "(%s) ", "L");
  1163. else if (state == THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY)
  1164. seq_printf(s, "(%s) ", "H");
  1165. else
  1166. seq_printf(s, "(%s) ", "H+L");
  1167. mask = THERMCTL_LVL0_CPU0_STATUS_MASK;
  1168. state = REG_GET_MASK(r, mask);
  1169. seq_printf(s, "Status(%s)\n",
  1170. state == 0 ? "LO" :
  1171. state == 1 ? "In" :
  1172. state == 2 ? "Res" : "HI");
  1173. }
  1174. }
  1175. r = readl(ts->regs + THERMCTL_STATS_CTL);
  1176. seq_printf(s, "STATS: Up(%s) Dn(%s)\n",
  1177. r & STATS_CTL_EN_UP ? "En" : "--",
  1178. r & STATS_CTL_EN_DN ? "En" : "--");
  1179. for (level = 0; level < 4; level++) {
  1180. u16 off;
  1181. off = THERMCTL_LVL0_UP_STATS;
  1182. r = readl(ts->regs + THERMCTL_LVL_REG(off, level));
  1183. seq_printf(s, " Level_%d Up(%d) ", level, r);
  1184. off = THERMCTL_LVL0_DN_STATS;
  1185. r = readl(ts->regs + THERMCTL_LVL_REG(off, level));
  1186. seq_printf(s, "Dn(%d)\n", r);
  1187. }
  1188. r = readl(ts->regs + THERMCTL_THERMTRIP_CTL);
  1189. state = REG_GET_MASK(r, ttgs[0]->thermtrip_any_en_mask);
  1190. seq_printf(s, "Thermtrip Any En(%d)\n", state);
  1191. for (i = 0; i < ts->soc->num_ttgs; i++) {
  1192. state = REG_GET_MASK(r, ttgs[i]->thermtrip_enable_mask);
  1193. seq_printf(s, " %s En(%d) ", ttgs[i]->name, state);
  1194. state = REG_GET_MASK(r, ttgs[i]->thermtrip_threshold_mask);
  1195. state *= ts->soc->thresh_grain;
  1196. seq_printf(s, "Thresh(%d)\n", state);
  1197. }
  1198. r = readl(ts->regs + THROT_GLOBAL_CFG);
  1199. seq_puts(s, "\n");
  1200. seq_printf(s, "GLOBAL THROTTLE CONFIG: 0x%08x\n", r);
  1201. seq_puts(s, "---------------------------------------------------\n");
  1202. r = readl(ts->regs + THROT_STATUS);
  1203. state = REG_GET_MASK(r, THROT_STATUS_BREACH_MASK);
  1204. seq_printf(s, "THROT STATUS: breach(%d) ", state);
  1205. state = REG_GET_MASK(r, THROT_STATUS_STATE_MASK);
  1206. seq_printf(s, "state(%d) ", state);
  1207. state = REG_GET_MASK(r, THROT_STATUS_ENABLED_MASK);
  1208. seq_printf(s, "enabled(%d)\n", state);
  1209. r = readl(ts->regs + CPU_PSKIP_STATUS);
  1210. if (ts->soc->use_ccroc) {
  1211. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_ENABLED_MASK);
  1212. seq_printf(s, "CPU PSKIP STATUS: enabled(%d)\n", state);
  1213. } else {
  1214. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_M_MASK);
  1215. seq_printf(s, "CPU PSKIP STATUS: M(%d) ", state);
  1216. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_N_MASK);
  1217. seq_printf(s, "N(%d) ", state);
  1218. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_ENABLED_MASK);
  1219. seq_printf(s, "enabled(%d)\n", state);
  1220. }
  1221. return 0;
  1222. }
  1223. DEFINE_SHOW_ATTRIBUTE(regs);
  1224. static void soctherm_debug_init(struct platform_device *pdev)
  1225. {
  1226. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1227. struct dentry *root;
  1228. root = debugfs_create_dir("soctherm", NULL);
  1229. tegra->debugfs_dir = root;
  1230. debugfs_create_file("reg_contents", 0644, root, pdev, &regs_fops);
  1231. }
  1232. #else
  1233. static inline void soctherm_debug_init(struct platform_device *pdev) {}
  1234. #endif
  1235. static int soctherm_clk_enable(struct platform_device *pdev, bool enable)
  1236. {
  1237. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1238. int err;
  1239. if (!tegra->clock_soctherm || !tegra->clock_tsensor)
  1240. return -EINVAL;
  1241. reset_control_assert(tegra->reset);
  1242. if (enable) {
  1243. err = clk_prepare_enable(tegra->clock_soctherm);
  1244. if (err) {
  1245. reset_control_deassert(tegra->reset);
  1246. return err;
  1247. }
  1248. err = clk_prepare_enable(tegra->clock_tsensor);
  1249. if (err) {
  1250. clk_disable_unprepare(tegra->clock_soctherm);
  1251. reset_control_deassert(tegra->reset);
  1252. return err;
  1253. }
  1254. } else {
  1255. clk_disable_unprepare(tegra->clock_tsensor);
  1256. clk_disable_unprepare(tegra->clock_soctherm);
  1257. }
  1258. reset_control_deassert(tegra->reset);
  1259. return 0;
  1260. }
  1261. static int throt_get_cdev_max_state(struct thermal_cooling_device *cdev,
  1262. unsigned long *max_state)
  1263. {
  1264. *max_state = 1;
  1265. return 0;
  1266. }
  1267. static int throt_get_cdev_cur_state(struct thermal_cooling_device *cdev,
  1268. unsigned long *cur_state)
  1269. {
  1270. struct tegra_soctherm *ts = cdev->devdata;
  1271. u32 r;
  1272. r = readl(ts->regs + THROT_STATUS);
  1273. if (REG_GET_MASK(r, THROT_STATUS_STATE_MASK))
  1274. *cur_state = 1;
  1275. else
  1276. *cur_state = 0;
  1277. return 0;
  1278. }
  1279. static int throt_set_cdev_state(struct thermal_cooling_device *cdev,
  1280. unsigned long cur_state)
  1281. {
  1282. return 0;
  1283. }
  1284. static const struct thermal_cooling_device_ops throt_cooling_ops = {
  1285. .get_max_state = throt_get_cdev_max_state,
  1286. .get_cur_state = throt_get_cdev_cur_state,
  1287. .set_cur_state = throt_set_cdev_state,
  1288. };
  1289. static int soctherm_thermtrips_parse(struct platform_device *pdev)
  1290. {
  1291. struct device *dev = &pdev->dev;
  1292. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  1293. struct tsensor_group_thermtrips *tt = ts->soc->thermtrips;
  1294. const int max_num_prop = ts->soc->num_ttgs * 2;
  1295. u32 *tlb;
  1296. int i, j, n, ret;
  1297. if (!tt)
  1298. return -ENOMEM;
  1299. n = of_property_count_u32_elems(dev->of_node, "nvidia,thermtrips");
  1300. if (n <= 0) {
  1301. dev_info(dev,
  1302. "missing thermtrips, will use critical trips as shut down temp\n");
  1303. return n;
  1304. }
  1305. n = min(max_num_prop, n);
  1306. tlb = devm_kcalloc(&pdev->dev, max_num_prop, sizeof(u32), GFP_KERNEL);
  1307. if (!tlb)
  1308. return -ENOMEM;
  1309. ret = of_property_read_u32_array(dev->of_node, "nvidia,thermtrips",
  1310. tlb, n);
  1311. if (ret) {
  1312. dev_err(dev, "invalid num ele: thermtrips:%d\n", ret);
  1313. return ret;
  1314. }
  1315. i = 0;
  1316. for (j = 0; j < n; j = j + 2) {
  1317. if (tlb[j] >= TEGRA124_SOCTHERM_SENSOR_NUM)
  1318. continue;
  1319. tt[i].id = tlb[j];
  1320. tt[i].temp = tlb[j + 1];
  1321. i++;
  1322. }
  1323. return 0;
  1324. }
  1325. static void soctherm_oc_cfg_parse(struct device *dev,
  1326. struct device_node *np_oc,
  1327. struct soctherm_throt_cfg *stc)
  1328. {
  1329. u32 val;
  1330. if (of_property_read_bool(np_oc, "nvidia,polarity-active-low"))
  1331. stc->oc_cfg.active_low = 1;
  1332. else
  1333. stc->oc_cfg.active_low = 0;
  1334. if (!of_property_read_u32(np_oc, "nvidia,count-threshold", &val)) {
  1335. stc->oc_cfg.intr_en = 1;
  1336. stc->oc_cfg.alarm_cnt_thresh = val;
  1337. }
  1338. if (!of_property_read_u32(np_oc, "nvidia,throttle-period-us", &val))
  1339. stc->oc_cfg.throt_period = val;
  1340. if (!of_property_read_u32(np_oc, "nvidia,alarm-filter", &val))
  1341. stc->oc_cfg.alarm_filter = val;
  1342. /* BRIEF throttling by default, do not support STICKY */
  1343. stc->oc_cfg.mode = OC_THROTTLE_MODE_BRIEF;
  1344. }
  1345. static int soctherm_throt_cfg_parse(struct device *dev,
  1346. struct device_node *np,
  1347. struct soctherm_throt_cfg *stc)
  1348. {
  1349. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  1350. int ret;
  1351. u32 val;
  1352. ret = of_property_read_u32(np, "nvidia,priority", &val);
  1353. if (ret) {
  1354. dev_err(dev, "throttle-cfg: %s: invalid priority\n", stc->name);
  1355. return -EINVAL;
  1356. }
  1357. stc->priority = val;
  1358. ret = of_property_read_u32(np, ts->soc->use_ccroc ?
  1359. "nvidia,cpu-throt-level" :
  1360. "nvidia,cpu-throt-percent", &val);
  1361. if (!ret) {
  1362. if (ts->soc->use_ccroc &&
  1363. val <= TEGRA_SOCTHERM_THROT_LEVEL_HIGH)
  1364. stc->cpu_throt_level = val;
  1365. else if (!ts->soc->use_ccroc && val <= 100)
  1366. stc->cpu_throt_depth = val;
  1367. else
  1368. goto err;
  1369. } else {
  1370. goto err;
  1371. }
  1372. ret = of_property_read_u32(np, "nvidia,gpu-throt-level", &val);
  1373. if (!ret && val <= TEGRA_SOCTHERM_THROT_LEVEL_HIGH)
  1374. stc->gpu_throt_level = val;
  1375. else
  1376. goto err;
  1377. return 0;
  1378. err:
  1379. dev_err(dev, "throttle-cfg: %s: no throt prop or invalid prop\n",
  1380. stc->name);
  1381. return -EINVAL;
  1382. }
  1383. /**
  1384. * soctherm_init_hw_throt_cdev() - Parse the HW throttle configurations
  1385. * and register them as cooling devices.
  1386. * @pdev: Pointer to platform_device struct
  1387. */
  1388. static void soctherm_init_hw_throt_cdev(struct platform_device *pdev)
  1389. {
  1390. struct device *dev = &pdev->dev;
  1391. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  1392. struct device_node *np_stc, *np_stcc;
  1393. const char *name;
  1394. int i;
  1395. for (i = 0; i < THROTTLE_SIZE; i++) {
  1396. ts->throt_cfgs[i].name = throt_names[i];
  1397. ts->throt_cfgs[i].id = i;
  1398. ts->throt_cfgs[i].init = false;
  1399. }
  1400. np_stc = of_get_child_by_name(dev->of_node, "throttle-cfgs");
  1401. if (!np_stc) {
  1402. dev_info(dev,
  1403. "throttle-cfg: no throttle-cfgs - not enabling\n");
  1404. return;
  1405. }
  1406. for_each_child_of_node(np_stc, np_stcc) {
  1407. struct soctherm_throt_cfg *stc;
  1408. struct thermal_cooling_device *tcd;
  1409. int err;
  1410. name = np_stcc->name;
  1411. stc = find_throttle_cfg_by_name(ts, name);
  1412. if (!stc) {
  1413. dev_err(dev,
  1414. "throttle-cfg: could not find %s\n", name);
  1415. continue;
  1416. }
  1417. if (stc->init) {
  1418. dev_err(dev, "throttle-cfg: %s: redefined!\n", name);
  1419. of_node_put(np_stcc);
  1420. break;
  1421. }
  1422. err = soctherm_throt_cfg_parse(dev, np_stcc, stc);
  1423. if (err)
  1424. continue;
  1425. if (stc->id >= THROTTLE_OC1) {
  1426. soctherm_oc_cfg_parse(dev, np_stcc, stc);
  1427. stc->init = true;
  1428. } else {
  1429. tcd = thermal_of_cooling_device_register(np_stcc,
  1430. (char *)name, ts,
  1431. &throt_cooling_ops);
  1432. if (IS_ERR_OR_NULL(tcd)) {
  1433. dev_err(dev,
  1434. "throttle-cfg: %s: failed to register cooling device\n",
  1435. name);
  1436. continue;
  1437. }
  1438. stc->cdev = tcd;
  1439. stc->init = true;
  1440. }
  1441. }
  1442. of_node_put(np_stc);
  1443. }
  1444. /**
  1445. * throttlectl_cpu_level_cfg() - programs CCROC NV_THERM level config
  1446. * @ts: pointer to a struct tegra_soctherm
  1447. * @level: describing the level LOW/MED/HIGH of throttling
  1448. *
  1449. * It's necessary to set up the CPU-local CCROC NV_THERM instance with
  1450. * the M/N values desired for each level. This function does this.
  1451. *
  1452. * This function pre-programs the CCROC NV_THERM levels in terms of
  1453. * pre-configured "Low", "Medium" or "Heavy" throttle levels which are
  1454. * mapped to THROT_LEVEL_LOW, THROT_LEVEL_MED and THROT_LEVEL_HVY.
  1455. */
  1456. static void throttlectl_cpu_level_cfg(struct tegra_soctherm *ts, int level)
  1457. {
  1458. u8 depth, dividend;
  1459. u32 r;
  1460. switch (level) {
  1461. case TEGRA_SOCTHERM_THROT_LEVEL_LOW:
  1462. depth = 50;
  1463. break;
  1464. case TEGRA_SOCTHERM_THROT_LEVEL_MED:
  1465. depth = 75;
  1466. break;
  1467. case TEGRA_SOCTHERM_THROT_LEVEL_HIGH:
  1468. depth = 80;
  1469. break;
  1470. case TEGRA_SOCTHERM_THROT_LEVEL_NONE:
  1471. return;
  1472. default:
  1473. return;
  1474. }
  1475. dividend = THROT_DEPTH_DIVIDEND(depth);
  1476. /* setup PSKIP in ccroc nv_therm registers */
  1477. r = ccroc_readl(ts, CCROC_THROT_PSKIP_RAMP_CPU_REG(level));
  1478. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_RAMP_DURATION_MASK, 0xff);
  1479. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_RAMP_STEP_MASK, 0xf);
  1480. ccroc_writel(ts, r, CCROC_THROT_PSKIP_RAMP_CPU_REG(level));
  1481. r = ccroc_readl(ts, CCROC_THROT_PSKIP_CTRL_CPU_REG(level));
  1482. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_ENB_MASK, 1);
  1483. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_DIVIDEND_MASK, dividend);
  1484. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_DIVISOR_MASK, 0xff);
  1485. ccroc_writel(ts, r, CCROC_THROT_PSKIP_CTRL_CPU_REG(level));
  1486. }
  1487. /**
  1488. * throttlectl_cpu_level_select() - program CPU pulse skipper config
  1489. * @ts: pointer to a struct tegra_soctherm
  1490. * @throt: the LIGHT/HEAVY of throttle event id
  1491. *
  1492. * Pulse skippers are used to throttle clock frequencies. This
  1493. * function programs the pulse skippers based on @throt and platform
  1494. * data. This function is used on SoCs which have CPU-local pulse
  1495. * skipper control, such as T13x. It programs soctherm's interface to
  1496. * Denver:CCROC NV_THERM in terms of Low, Medium and HIGH throttling
  1497. * vectors. PSKIP_BYPASS mode is set as required per HW spec.
  1498. */
  1499. static void throttlectl_cpu_level_select(struct tegra_soctherm *ts,
  1500. enum soctherm_throttle_id throt)
  1501. {
  1502. u32 r, throt_vect;
  1503. /* Denver:CCROC NV_THERM interface N:3 Mapping */
  1504. switch (ts->throt_cfgs[throt].cpu_throt_level) {
  1505. case TEGRA_SOCTHERM_THROT_LEVEL_LOW:
  1506. throt_vect = THROT_VECT_LOW;
  1507. break;
  1508. case TEGRA_SOCTHERM_THROT_LEVEL_MED:
  1509. throt_vect = THROT_VECT_MED;
  1510. break;
  1511. case TEGRA_SOCTHERM_THROT_LEVEL_HIGH:
  1512. throt_vect = THROT_VECT_HIGH;
  1513. break;
  1514. default:
  1515. throt_vect = THROT_VECT_NONE;
  1516. break;
  1517. }
  1518. r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  1519. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1);
  1520. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT_CPU_MASK, throt_vect);
  1521. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT2_CPU_MASK, throt_vect);
  1522. writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  1523. /* bypass sequencer in soc_therm as it is programmed in ccroc */
  1524. r = REG_SET_MASK(0, THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK, 1);
  1525. writel(r, ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU));
  1526. }
  1527. /**
  1528. * throttlectl_cpu_mn() - program CPU pulse skipper configuration
  1529. * @ts: pointer to a struct tegra_soctherm
  1530. * @throt: the LIGHT/HEAVY of throttle event id
  1531. *
  1532. * Pulse skippers are used to throttle clock frequencies. This
  1533. * function programs the pulse skippers based on @throt and platform
  1534. * data. This function is used for CPUs that have "remote" pulse
  1535. * skipper control, e.g., the CPU pulse skipper is controlled by the
  1536. * SOC_THERM IP block. (SOC_THERM is located outside the CPU
  1537. * complex.)
  1538. */
  1539. static void throttlectl_cpu_mn(struct tegra_soctherm *ts,
  1540. enum soctherm_throttle_id throt)
  1541. {
  1542. u32 r;
  1543. int depth;
  1544. u8 dividend;
  1545. depth = ts->throt_cfgs[throt].cpu_throt_depth;
  1546. dividend = THROT_DEPTH_DIVIDEND(depth);
  1547. r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  1548. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1);
  1549. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_DIVIDEND_MASK, dividend);
  1550. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_DIVISOR_MASK, 0xff);
  1551. writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  1552. r = readl(ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU));
  1553. r = REG_SET_MASK(r, THROT_PSKIP_RAMP_DURATION_MASK, 0xff);
  1554. r = REG_SET_MASK(r, THROT_PSKIP_RAMP_STEP_MASK, 0xf);
  1555. writel(r, ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU));
  1556. }
  1557. /**
  1558. * throttlectl_gpu_level_select() - selects throttling level for GPU
  1559. * @ts: pointer to a struct tegra_soctherm
  1560. * @throt: the LIGHT/HEAVY of throttle event id
  1561. *
  1562. * This function programs soctherm's interface to GK20a NV_THERM to select
  1563. * pre-configured "Low", "Medium" or "Heavy" throttle levels.
  1564. *
  1565. * Return: boolean true if HW was programmed
  1566. */
  1567. static void throttlectl_gpu_level_select(struct tegra_soctherm *ts,
  1568. enum soctherm_throttle_id throt)
  1569. {
  1570. u32 r, level, throt_vect;
  1571. level = ts->throt_cfgs[throt].gpu_throt_level;
  1572. throt_vect = THROT_LEVEL_TO_DEPTH(level);
  1573. r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_GPU));
  1574. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1);
  1575. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT_GPU_MASK, throt_vect);
  1576. writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_GPU));
  1577. }
  1578. static int soctherm_oc_cfg_program(struct tegra_soctherm *ts,
  1579. enum soctherm_throttle_id throt)
  1580. {
  1581. u32 r;
  1582. struct soctherm_oc_cfg *oc = &ts->throt_cfgs[throt].oc_cfg;
  1583. if (oc->mode == OC_THROTTLE_MODE_DISABLED)
  1584. return -EINVAL;
  1585. r = REG_SET_MASK(0, OC1_CFG_HW_RESTORE_MASK, 1);
  1586. r = REG_SET_MASK(r, OC1_CFG_THROTTLE_MODE_MASK, oc->mode);
  1587. r = REG_SET_MASK(r, OC1_CFG_ALARM_POLARITY_MASK, oc->active_low);
  1588. r = REG_SET_MASK(r, OC1_CFG_EN_THROTTLE_MASK, 1);
  1589. writel(r, ts->regs + ALARM_CFG(throt));
  1590. writel(oc->throt_period, ts->regs + ALARM_THROTTLE_PERIOD(throt));
  1591. writel(oc->alarm_cnt_thresh, ts->regs + ALARM_CNT_THRESHOLD(throt));
  1592. writel(oc->alarm_filter, ts->regs + ALARM_FILTER(throt));
  1593. soctherm_oc_intr_enable(ts, throt, oc->intr_en);
  1594. return 0;
  1595. }
  1596. /**
  1597. * soctherm_throttle_program() - programs pulse skippers' configuration
  1598. * @ts: pointer to a struct tegra_soctherm
  1599. * @throt: the LIGHT/HEAVY of the throttle event id.
  1600. *
  1601. * Pulse skippers are used to throttle clock frequencies.
  1602. * This function programs the pulse skippers.
  1603. */
  1604. static void soctherm_throttle_program(struct tegra_soctherm *ts,
  1605. enum soctherm_throttle_id throt)
  1606. {
  1607. u32 r;
  1608. struct soctherm_throt_cfg stc = ts->throt_cfgs[throt];
  1609. if (!stc.init)
  1610. return;
  1611. if ((throt >= THROTTLE_OC1) && (soctherm_oc_cfg_program(ts, throt)))
  1612. return;
  1613. /* Setup PSKIP parameters */
  1614. if (ts->soc->use_ccroc)
  1615. throttlectl_cpu_level_select(ts, throt);
  1616. else
  1617. throttlectl_cpu_mn(ts, throt);
  1618. throttlectl_gpu_level_select(ts, throt);
  1619. r = REG_SET_MASK(0, THROT_PRIORITY_LITE_PRIO_MASK, stc.priority);
  1620. writel(r, ts->regs + THROT_PRIORITY_CTRL(throt));
  1621. r = REG_SET_MASK(0, THROT_DELAY_LITE_DELAY_MASK, 0);
  1622. writel(r, ts->regs + THROT_DELAY_CTRL(throt));
  1623. r = readl(ts->regs + THROT_PRIORITY_LOCK);
  1624. r = REG_GET_MASK(r, THROT_PRIORITY_LOCK_PRIORITY_MASK);
  1625. if (r >= stc.priority)
  1626. return;
  1627. r = REG_SET_MASK(0, THROT_PRIORITY_LOCK_PRIORITY_MASK,
  1628. stc.priority);
  1629. writel(r, ts->regs + THROT_PRIORITY_LOCK);
  1630. }
  1631. static void tegra_soctherm_throttle(struct device *dev)
  1632. {
  1633. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  1634. u32 v;
  1635. int i;
  1636. /* configure LOW, MED and HIGH levels for CCROC NV_THERM */
  1637. if (ts->soc->use_ccroc) {
  1638. throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_LOW);
  1639. throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_MED);
  1640. throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_HIGH);
  1641. }
  1642. /* Thermal HW throttle programming */
  1643. for (i = 0; i < THROTTLE_SIZE; i++)
  1644. soctherm_throttle_program(ts, i);
  1645. v = REG_SET_MASK(0, THROT_GLOBAL_ENB_MASK, 1);
  1646. if (ts->soc->use_ccroc) {
  1647. ccroc_writel(ts, v, CCROC_GLOBAL_CFG);
  1648. v = ccroc_readl(ts, CCROC_SUPER_CCLKG_DIVIDER);
  1649. v = REG_SET_MASK(v, CDIVG_USE_THERM_CONTROLS_MASK, 1);
  1650. ccroc_writel(ts, v, CCROC_SUPER_CCLKG_DIVIDER);
  1651. } else {
  1652. writel(v, ts->regs + THROT_GLOBAL_CFG);
  1653. v = readl(ts->clk_regs + CAR_SUPER_CCLKG_DIVIDER);
  1654. v = REG_SET_MASK(v, CDIVG_USE_THERM_CONTROLS_MASK, 1);
  1655. writel(v, ts->clk_regs + CAR_SUPER_CCLKG_DIVIDER);
  1656. }
  1657. /* initialize stats collection */
  1658. v = STATS_CTL_CLR_DN | STATS_CTL_EN_DN |
  1659. STATS_CTL_CLR_UP | STATS_CTL_EN_UP;
  1660. writel(v, ts->regs + THERMCTL_STATS_CTL);
  1661. }
  1662. static int soctherm_interrupts_init(struct platform_device *pdev,
  1663. struct tegra_soctherm *tegra)
  1664. {
  1665. struct device_node *np = pdev->dev.of_node;
  1666. int ret;
  1667. ret = soctherm_oc_int_init(np, TEGRA_SOC_OC_IRQ_MAX);
  1668. if (ret < 0) {
  1669. dev_err(&pdev->dev, "soctherm_oc_int_init failed\n");
  1670. return ret;
  1671. }
  1672. tegra->thermal_irq = platform_get_irq(pdev, 0);
  1673. if (tegra->thermal_irq < 0) {
  1674. dev_dbg(&pdev->dev, "get 'thermal_irq' failed.\n");
  1675. return 0;
  1676. }
  1677. tegra->edp_irq = platform_get_irq(pdev, 1);
  1678. if (tegra->edp_irq < 0) {
  1679. dev_dbg(&pdev->dev, "get 'edp_irq' failed.\n");
  1680. return 0;
  1681. }
  1682. ret = devm_request_threaded_irq(&pdev->dev,
  1683. tegra->thermal_irq,
  1684. soctherm_thermal_isr,
  1685. soctherm_thermal_isr_thread,
  1686. IRQF_ONESHOT,
  1687. dev_name(&pdev->dev),
  1688. tegra);
  1689. if (ret < 0) {
  1690. dev_err(&pdev->dev, "request_irq 'thermal_irq' failed.\n");
  1691. return ret;
  1692. }
  1693. ret = devm_request_threaded_irq(&pdev->dev,
  1694. tegra->edp_irq,
  1695. soctherm_edp_isr,
  1696. soctherm_edp_isr_thread,
  1697. IRQF_ONESHOT,
  1698. "soctherm_edp",
  1699. tegra);
  1700. if (ret < 0) {
  1701. dev_err(&pdev->dev, "request_irq 'edp_irq' failed.\n");
  1702. return ret;
  1703. }
  1704. return 0;
  1705. }
  1706. static void soctherm_init(struct platform_device *pdev)
  1707. {
  1708. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1709. const struct tegra_tsensor_group **ttgs = tegra->soc->ttgs;
  1710. int i;
  1711. u32 pdiv, hotspot;
  1712. /* Initialize raw sensors */
  1713. for (i = 0; i < tegra->soc->num_tsensors; ++i)
  1714. enable_tsensor(tegra, i);
  1715. /* program pdiv and hotspot offsets per THERM */
  1716. pdiv = readl(tegra->regs + SENSOR_PDIV);
  1717. hotspot = readl(tegra->regs + SENSOR_HOTSPOT_OFF);
  1718. for (i = 0; i < tegra->soc->num_ttgs; ++i) {
  1719. pdiv = REG_SET_MASK(pdiv, ttgs[i]->pdiv_mask,
  1720. ttgs[i]->pdiv);
  1721. /* hotspot offset from PLLX, doesn't need to configure PLLX */
  1722. if (ttgs[i]->id == TEGRA124_SOCTHERM_SENSOR_PLLX)
  1723. continue;
  1724. hotspot = REG_SET_MASK(hotspot,
  1725. ttgs[i]->pllx_hotspot_mask,
  1726. ttgs[i]->pllx_hotspot_diff);
  1727. }
  1728. writel(pdiv, tegra->regs + SENSOR_PDIV);
  1729. writel(hotspot, tegra->regs + SENSOR_HOTSPOT_OFF);
  1730. /* Configure hw throttle */
  1731. tegra_soctherm_throttle(&pdev->dev);
  1732. }
  1733. static const struct of_device_id tegra_soctherm_of_match[] = {
  1734. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  1735. {
  1736. .compatible = "nvidia,tegra124-soctherm",
  1737. .data = &tegra124_soctherm,
  1738. },
  1739. #endif
  1740. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  1741. {
  1742. .compatible = "nvidia,tegra132-soctherm",
  1743. .data = &tegra132_soctherm,
  1744. },
  1745. #endif
  1746. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  1747. {
  1748. .compatible = "nvidia,tegra210-soctherm",
  1749. .data = &tegra210_soctherm,
  1750. },
  1751. #endif
  1752. { },
  1753. };
  1754. MODULE_DEVICE_TABLE(of, tegra_soctherm_of_match);
  1755. static int tegra_soctherm_probe(struct platform_device *pdev)
  1756. {
  1757. const struct of_device_id *match;
  1758. struct tegra_soctherm *tegra;
  1759. struct thermal_zone_device *z;
  1760. struct tsensor_shared_calib shared_calib;
  1761. struct tegra_soctherm_soc *soc;
  1762. unsigned int i;
  1763. int err;
  1764. match = of_match_node(tegra_soctherm_of_match, pdev->dev.of_node);
  1765. if (!match)
  1766. return -ENODEV;
  1767. soc = (struct tegra_soctherm_soc *)match->data;
  1768. if (soc->num_ttgs > TEGRA124_SOCTHERM_SENSOR_NUM)
  1769. return -EINVAL;
  1770. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  1771. if (!tegra)
  1772. return -ENOMEM;
  1773. mutex_init(&tegra->thermctl_lock);
  1774. dev_set_drvdata(&pdev->dev, tegra);
  1775. tegra->soc = soc;
  1776. tegra->regs = devm_platform_ioremap_resource_byname(pdev, "soctherm-reg");
  1777. if (IS_ERR(tegra->regs)) {
  1778. dev_err(&pdev->dev, "can't get soctherm registers");
  1779. return PTR_ERR(tegra->regs);
  1780. }
  1781. if (!tegra->soc->use_ccroc) {
  1782. tegra->clk_regs = devm_platform_ioremap_resource_byname(pdev, "car-reg");
  1783. if (IS_ERR(tegra->clk_regs)) {
  1784. dev_err(&pdev->dev, "can't get car clk registers");
  1785. return PTR_ERR(tegra->clk_regs);
  1786. }
  1787. } else {
  1788. tegra->ccroc_regs = devm_platform_ioremap_resource_byname(pdev, "ccroc-reg");
  1789. if (IS_ERR(tegra->ccroc_regs)) {
  1790. dev_err(&pdev->dev, "can't get ccroc registers");
  1791. return PTR_ERR(tegra->ccroc_regs);
  1792. }
  1793. }
  1794. tegra->reset = devm_reset_control_get(&pdev->dev, "soctherm");
  1795. if (IS_ERR(tegra->reset)) {
  1796. dev_err(&pdev->dev, "can't get soctherm reset\n");
  1797. return PTR_ERR(tegra->reset);
  1798. }
  1799. tegra->clock_tsensor = devm_clk_get(&pdev->dev, "tsensor");
  1800. if (IS_ERR(tegra->clock_tsensor)) {
  1801. dev_err(&pdev->dev, "can't get tsensor clock\n");
  1802. return PTR_ERR(tegra->clock_tsensor);
  1803. }
  1804. tegra->clock_soctherm = devm_clk_get(&pdev->dev, "soctherm");
  1805. if (IS_ERR(tegra->clock_soctherm)) {
  1806. dev_err(&pdev->dev, "can't get soctherm clock\n");
  1807. return PTR_ERR(tegra->clock_soctherm);
  1808. }
  1809. tegra->calib = devm_kcalloc(&pdev->dev,
  1810. soc->num_tsensors, sizeof(u32),
  1811. GFP_KERNEL);
  1812. if (!tegra->calib)
  1813. return -ENOMEM;
  1814. /* calculate shared calibration data */
  1815. err = tegra_calc_shared_calib(soc->tfuse, &shared_calib);
  1816. if (err)
  1817. return err;
  1818. /* calculate tsensor calibration data */
  1819. for (i = 0; i < soc->num_tsensors; ++i) {
  1820. err = tegra_calc_tsensor_calib(&soc->tsensors[i],
  1821. &shared_calib,
  1822. &tegra->calib[i]);
  1823. if (err)
  1824. return err;
  1825. }
  1826. tegra->thermctl_tzs = devm_kcalloc(&pdev->dev,
  1827. soc->num_ttgs, sizeof(z),
  1828. GFP_KERNEL);
  1829. if (!tegra->thermctl_tzs)
  1830. return -ENOMEM;
  1831. err = soctherm_clk_enable(pdev, true);
  1832. if (err)
  1833. return err;
  1834. soctherm_thermtrips_parse(pdev);
  1835. soctherm_init_hw_throt_cdev(pdev);
  1836. soctherm_init(pdev);
  1837. for (i = 0; i < soc->num_ttgs; ++i) {
  1838. struct tegra_thermctl_zone *zone =
  1839. devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL);
  1840. if (!zone) {
  1841. err = -ENOMEM;
  1842. goto disable_clocks;
  1843. }
  1844. zone->reg = tegra->regs + soc->ttgs[i]->sensor_temp_offset;
  1845. zone->dev = &pdev->dev;
  1846. zone->sg = soc->ttgs[i];
  1847. zone->ts = tegra;
  1848. z = devm_thermal_of_zone_register(&pdev->dev,
  1849. soc->ttgs[i]->id, zone,
  1850. &tegra_of_thermal_ops);
  1851. if (IS_ERR(z)) {
  1852. err = PTR_ERR(z);
  1853. dev_err(&pdev->dev, "failed to register sensor: %d\n",
  1854. err);
  1855. goto disable_clocks;
  1856. }
  1857. zone->tz = z;
  1858. tegra->thermctl_tzs[soc->ttgs[i]->id] = z;
  1859. /* Configure hw trip points */
  1860. err = tegra_soctherm_set_hwtrips(&pdev->dev, soc->ttgs[i], z);
  1861. if (err)
  1862. goto disable_clocks;
  1863. }
  1864. err = soctherm_interrupts_init(pdev, tegra);
  1865. soctherm_debug_init(pdev);
  1866. return 0;
  1867. disable_clocks:
  1868. soctherm_clk_enable(pdev, false);
  1869. return err;
  1870. }
  1871. static int tegra_soctherm_remove(struct platform_device *pdev)
  1872. {
  1873. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1874. debugfs_remove_recursive(tegra->debugfs_dir);
  1875. soctherm_clk_enable(pdev, false);
  1876. return 0;
  1877. }
  1878. static int __maybe_unused soctherm_suspend(struct device *dev)
  1879. {
  1880. struct platform_device *pdev = to_platform_device(dev);
  1881. soctherm_clk_enable(pdev, false);
  1882. return 0;
  1883. }
  1884. static int __maybe_unused soctherm_resume(struct device *dev)
  1885. {
  1886. struct platform_device *pdev = to_platform_device(dev);
  1887. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1888. struct tegra_soctherm_soc *soc = tegra->soc;
  1889. int err, i;
  1890. err = soctherm_clk_enable(pdev, true);
  1891. if (err) {
  1892. dev_err(&pdev->dev,
  1893. "Resume failed: enable clocks failed\n");
  1894. return err;
  1895. }
  1896. soctherm_init(pdev);
  1897. for (i = 0; i < soc->num_ttgs; ++i) {
  1898. struct thermal_zone_device *tz;
  1899. tz = tegra->thermctl_tzs[soc->ttgs[i]->id];
  1900. err = tegra_soctherm_set_hwtrips(dev, soc->ttgs[i], tz);
  1901. if (err) {
  1902. dev_err(&pdev->dev,
  1903. "Resume failed: set hwtrips failed\n");
  1904. return err;
  1905. }
  1906. }
  1907. return 0;
  1908. }
  1909. static SIMPLE_DEV_PM_OPS(tegra_soctherm_pm, soctherm_suspend, soctherm_resume);
  1910. static struct platform_driver tegra_soctherm_driver = {
  1911. .probe = tegra_soctherm_probe,
  1912. .remove = tegra_soctherm_remove,
  1913. .driver = {
  1914. .name = "tegra_soctherm",
  1915. .pm = &tegra_soctherm_pm,
  1916. .of_match_table = tegra_soctherm_of_match,
  1917. },
  1918. };
  1919. module_platform_driver(tegra_soctherm_driver);
  1920. MODULE_AUTHOR("Mikko Perttunen <[email protected]>");
  1921. MODULE_DESCRIPTION("NVIDIA Tegra SOCTHERM thermal management driver");
  1922. MODULE_LICENSE("GPL v2");