soctherm.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. /*
  2. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Author:
  5. * Mikko Perttunen <mperttunen@nvidia.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/debugfs.h>
  18. #include <linux/bitops.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/reset.h>
  28. #include <linux/thermal.h>
  29. #include <dt-bindings/thermal/tegra124-soctherm.h>
  30. #include "../thermal_core.h"
  31. #include "soctherm.h"
  32. #define SENSOR_CONFIG0 0
  33. #define SENSOR_CONFIG0_STOP BIT(0)
  34. #define SENSOR_CONFIG0_CPTR_OVER BIT(2)
  35. #define SENSOR_CONFIG0_OVER BIT(3)
  36. #define SENSOR_CONFIG0_TCALC_OVER BIT(4)
  37. #define SENSOR_CONFIG0_TALL_MASK (0xfffff << 8)
  38. #define SENSOR_CONFIG0_TALL_SHIFT 8
  39. #define SENSOR_CONFIG1 4
  40. #define SENSOR_CONFIG1_TSAMPLE_MASK 0x3ff
  41. #define SENSOR_CONFIG1_TSAMPLE_SHIFT 0
  42. #define SENSOR_CONFIG1_TIDDQ_EN_MASK (0x3f << 15)
  43. #define SENSOR_CONFIG1_TIDDQ_EN_SHIFT 15
  44. #define SENSOR_CONFIG1_TEN_COUNT_MASK (0x3f << 24)
  45. #define SENSOR_CONFIG1_TEN_COUNT_SHIFT 24
  46. #define SENSOR_CONFIG1_TEMP_ENABLE BIT(31)
  47. /*
  48. * SENSOR_CONFIG2 is defined in soctherm.h
  49. * because, it will be used by tegra_soctherm_fuse.c
  50. */
  51. #define SENSOR_STATUS0 0xc
  52. #define SENSOR_STATUS0_VALID_MASK BIT(31)
  53. #define SENSOR_STATUS0_CAPTURE_MASK 0xffff
  54. #define SENSOR_STATUS1 0x10
  55. #define SENSOR_STATUS1_TEMP_VALID_MASK BIT(31)
  56. #define SENSOR_STATUS1_TEMP_MASK 0xffff
  57. #define READBACK_VALUE_MASK 0xff00
  58. #define READBACK_VALUE_SHIFT 8
  59. #define READBACK_ADD_HALF BIT(7)
  60. #define READBACK_NEGATE BIT(0)
  61. /*
  62. * THERMCTL_LEVEL0_GROUP_CPU is defined in soctherm.h
  63. * because it will be used by tegraxxx_soctherm.c
  64. */
  65. #define THERMCTL_LVL0_CPU0_EN_MASK BIT(8)
  66. #define THERMCTL_LVL0_CPU0_CPU_THROT_MASK (0x3 << 5)
  67. #define THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT 0x1
  68. #define THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY 0x2
  69. #define THERMCTL_LVL0_CPU0_GPU_THROT_MASK (0x3 << 3)
  70. #define THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT 0x1
  71. #define THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY 0x2
  72. #define THERMCTL_LVL0_CPU0_MEM_THROT_MASK BIT(2)
  73. #define THERMCTL_LVL0_CPU0_STATUS_MASK 0x3
  74. #define THERMCTL_LVL0_UP_STATS 0x10
  75. #define THERMCTL_LVL0_DN_STATS 0x14
  76. #define THERMCTL_STATS_CTL 0x94
  77. #define STATS_CTL_CLR_DN 0x8
  78. #define STATS_CTL_EN_DN 0x4
  79. #define STATS_CTL_CLR_UP 0x2
  80. #define STATS_CTL_EN_UP 0x1
  81. #define THROT_GLOBAL_CFG 0x400
  82. #define THROT_GLOBAL_ENB_MASK BIT(0)
  83. #define CPU_PSKIP_STATUS 0x418
  84. #define XPU_PSKIP_STATUS_M_MASK (0xff << 12)
  85. #define XPU_PSKIP_STATUS_N_MASK (0xff << 4)
  86. #define XPU_PSKIP_STATUS_SW_OVERRIDE_MASK BIT(1)
  87. #define XPU_PSKIP_STATUS_ENABLED_MASK BIT(0)
  88. #define THROT_PRIORITY_LOCK 0x424
  89. #define THROT_PRIORITY_LOCK_PRIORITY_MASK 0xff
  90. #define THROT_STATUS 0x428
  91. #define THROT_STATUS_BREACH_MASK BIT(12)
  92. #define THROT_STATUS_STATE_MASK (0xff << 4)
  93. #define THROT_STATUS_ENABLED_MASK BIT(0)
  94. #define THROT_PSKIP_CTRL_LITE_CPU 0x430
  95. #define THROT_PSKIP_CTRL_ENABLE_MASK BIT(31)
  96. #define THROT_PSKIP_CTRL_DIVIDEND_MASK (0xff << 8)
  97. #define THROT_PSKIP_CTRL_DIVISOR_MASK 0xff
  98. #define THROT_PSKIP_CTRL_VECT_GPU_MASK (0x7 << 16)
  99. #define THROT_PSKIP_CTRL_VECT_CPU_MASK (0x7 << 8)
  100. #define THROT_PSKIP_CTRL_VECT2_CPU_MASK 0x7
  101. #define THROT_VECT_NONE 0x0 /* 3'b000 */
  102. #define THROT_VECT_LOW 0x1 /* 3'b001 */
  103. #define THROT_VECT_MED 0x3 /* 3'b011 */
  104. #define THROT_VECT_HIGH 0x7 /* 3'b111 */
  105. #define THROT_PSKIP_RAMP_LITE_CPU 0x434
  106. #define THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK BIT(31)
  107. #define THROT_PSKIP_RAMP_DURATION_MASK (0xffff << 8)
  108. #define THROT_PSKIP_RAMP_STEP_MASK 0xff
  109. #define THROT_PRIORITY_LITE 0x444
  110. #define THROT_PRIORITY_LITE_PRIO_MASK 0xff
  111. #define THROT_DELAY_LITE 0x448
  112. #define THROT_DELAY_LITE_DELAY_MASK 0xff
  113. /* car register offsets needed for enabling HW throttling */
  114. #define CAR_SUPER_CCLKG_DIVIDER 0x36c
  115. #define CDIVG_USE_THERM_CONTROLS_MASK BIT(30)
  116. /* ccroc register offsets needed for enabling HW throttling for Tegra132 */
  117. #define CCROC_SUPER_CCLKG_DIVIDER 0x024
  118. #define CCROC_GLOBAL_CFG 0x148
  119. #define CCROC_THROT_PSKIP_RAMP_CPU 0x150
  120. #define CCROC_THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK BIT(31)
  121. #define CCROC_THROT_PSKIP_RAMP_DURATION_MASK (0xffff << 8)
  122. #define CCROC_THROT_PSKIP_RAMP_STEP_MASK 0xff
  123. #define CCROC_THROT_PSKIP_CTRL_CPU 0x154
  124. #define CCROC_THROT_PSKIP_CTRL_ENB_MASK BIT(31)
  125. #define CCROC_THROT_PSKIP_CTRL_DIVIDEND_MASK (0xff << 8)
  126. #define CCROC_THROT_PSKIP_CTRL_DIVISOR_MASK 0xff
  127. /* get val from register(r) mask bits(m) */
  128. #define REG_GET_MASK(r, m) (((r) & (m)) >> (ffs(m) - 1))
  129. /* set val(v) to mask bits(m) of register(r) */
  130. #define REG_SET_MASK(r, m, v) (((r) & ~(m)) | \
  131. (((v) & (m >> (ffs(m) - 1))) << (ffs(m) - 1)))
  132. /* get dividend from the depth */
  133. #define THROT_DEPTH_DIVIDEND(depth) ((256 * (100 - (depth)) / 100) - 1)
  134. /* get THROT_PSKIP_xxx offset per LIGHT/HEAVY throt and CPU/GPU dev */
  135. #define THROT_OFFSET 0x30
  136. #define THROT_PSKIP_CTRL(throt, dev) (THROT_PSKIP_CTRL_LITE_CPU + \
  137. (THROT_OFFSET * throt) + (8 * dev))
  138. #define THROT_PSKIP_RAMP(throt, dev) (THROT_PSKIP_RAMP_LITE_CPU + \
  139. (THROT_OFFSET * throt) + (8 * dev))
  140. /* get THROT_xxx_CTRL offset per LIGHT/HEAVY throt */
  141. #define THROT_PRIORITY_CTRL(throt) (THROT_PRIORITY_LITE + \
  142. (THROT_OFFSET * throt))
  143. #define THROT_DELAY_CTRL(throt) (THROT_DELAY_LITE + \
  144. (THROT_OFFSET * throt))
  145. /* get CCROC_THROT_PSKIP_xxx offset per HIGH/MED/LOW vect*/
  146. #define CCROC_THROT_OFFSET 0x0c
  147. #define CCROC_THROT_PSKIP_CTRL_CPU_REG(vect) (CCROC_THROT_PSKIP_CTRL_CPU + \
  148. (CCROC_THROT_OFFSET * vect))
  149. #define CCROC_THROT_PSKIP_RAMP_CPU_REG(vect) (CCROC_THROT_PSKIP_RAMP_CPU + \
  150. (CCROC_THROT_OFFSET * vect))
  151. /* get THERMCTL_LEVELx offset per CPU/GPU/MEM/TSENSE rg and LEVEL0~3 lv */
  152. #define THERMCTL_LVL_REGS_SIZE 0x20
  153. #define THERMCTL_LVL_REG(rg, lv) ((rg) + ((lv) * THERMCTL_LVL_REGS_SIZE))
  154. static const int min_low_temp = -127000;
  155. static const int max_high_temp = 127000;
  156. enum soctherm_throttle_id {
  157. THROTTLE_LIGHT = 0,
  158. THROTTLE_HEAVY,
  159. THROTTLE_SIZE,
  160. };
  161. enum soctherm_throttle_dev_id {
  162. THROTTLE_DEV_CPU = 0,
  163. THROTTLE_DEV_GPU,
  164. THROTTLE_DEV_SIZE,
  165. };
  166. static const char *const throt_names[] = {
  167. [THROTTLE_LIGHT] = "light",
  168. [THROTTLE_HEAVY] = "heavy",
  169. };
  170. struct tegra_soctherm;
  171. struct tegra_thermctl_zone {
  172. void __iomem *reg;
  173. struct device *dev;
  174. struct tegra_soctherm *ts;
  175. struct thermal_zone_device *tz;
  176. const struct tegra_tsensor_group *sg;
  177. };
  178. struct soctherm_throt_cfg {
  179. const char *name;
  180. unsigned int id;
  181. u8 priority;
  182. u8 cpu_throt_level;
  183. u32 cpu_throt_depth;
  184. struct thermal_cooling_device *cdev;
  185. bool init;
  186. };
  187. struct tegra_soctherm {
  188. struct reset_control *reset;
  189. struct clk *clock_tsensor;
  190. struct clk *clock_soctherm;
  191. void __iomem *regs;
  192. void __iomem *clk_regs;
  193. void __iomem *ccroc_regs;
  194. u32 *calib;
  195. struct thermal_zone_device **thermctl_tzs;
  196. struct tegra_soctherm_soc *soc;
  197. struct soctherm_throt_cfg throt_cfgs[THROTTLE_SIZE];
  198. struct dentry *debugfs_dir;
  199. };
  200. /**
  201. * ccroc_writel() - writes a value to a CCROC register
  202. * @ts: pointer to a struct tegra_soctherm
  203. * @v: the value to write
  204. * @reg: the register offset
  205. *
  206. * Writes @v to @reg. No return value.
  207. */
  208. static inline void ccroc_writel(struct tegra_soctherm *ts, u32 value, u32 reg)
  209. {
  210. writel(value, (ts->ccroc_regs + reg));
  211. }
  212. /**
  213. * ccroc_readl() - reads specified register from CCROC IP block
  214. * @ts: pointer to a struct tegra_soctherm
  215. * @reg: register address to be read
  216. *
  217. * Return: the value of the register
  218. */
  219. static inline u32 ccroc_readl(struct tegra_soctherm *ts, u32 reg)
  220. {
  221. return readl(ts->ccroc_regs + reg);
  222. }
  223. static void enable_tsensor(struct tegra_soctherm *tegra, unsigned int i)
  224. {
  225. const struct tegra_tsensor *sensor = &tegra->soc->tsensors[i];
  226. void __iomem *base = tegra->regs + sensor->base;
  227. unsigned int val;
  228. val = sensor->config->tall << SENSOR_CONFIG0_TALL_SHIFT;
  229. writel(val, base + SENSOR_CONFIG0);
  230. val = (sensor->config->tsample - 1) << SENSOR_CONFIG1_TSAMPLE_SHIFT;
  231. val |= sensor->config->tiddq_en << SENSOR_CONFIG1_TIDDQ_EN_SHIFT;
  232. val |= sensor->config->ten_count << SENSOR_CONFIG1_TEN_COUNT_SHIFT;
  233. val |= SENSOR_CONFIG1_TEMP_ENABLE;
  234. writel(val, base + SENSOR_CONFIG1);
  235. writel(tegra->calib[i], base + SENSOR_CONFIG2);
  236. }
  237. /*
  238. * Translate from soctherm readback format to millicelsius.
  239. * The soctherm readback format in bits is as follows:
  240. * TTTTTTTT H______N
  241. * where T's contain the temperature in Celsius,
  242. * H denotes an addition of 0.5 Celsius and N denotes negation
  243. * of the final value.
  244. */
  245. static int translate_temp(u16 val)
  246. {
  247. int t;
  248. t = ((val & READBACK_VALUE_MASK) >> READBACK_VALUE_SHIFT) * 1000;
  249. if (val & READBACK_ADD_HALF)
  250. t += 500;
  251. if (val & READBACK_NEGATE)
  252. t *= -1;
  253. return t;
  254. }
  255. static int tegra_thermctl_get_temp(void *data, int *out_temp)
  256. {
  257. struct tegra_thermctl_zone *zone = data;
  258. u32 val;
  259. val = readl(zone->reg);
  260. val = REG_GET_MASK(val, zone->sg->sensor_temp_mask);
  261. *out_temp = translate_temp(val);
  262. return 0;
  263. }
  264. /**
  265. * enforce_temp_range() - check and enforce temperature range [min, max]
  266. * @trip_temp: the trip temperature to check
  267. *
  268. * Checks and enforces the permitted temperature range that SOC_THERM
  269. * HW can support This is
  270. * done while taking care of precision.
  271. *
  272. * Return: The precision adjusted capped temperature in millicelsius.
  273. */
  274. static int enforce_temp_range(struct device *dev, int trip_temp)
  275. {
  276. int temp;
  277. temp = clamp_val(trip_temp, min_low_temp, max_high_temp);
  278. if (temp != trip_temp)
  279. dev_info(dev, "soctherm: trip temperature %d forced to %d\n",
  280. trip_temp, temp);
  281. return temp;
  282. }
  283. /**
  284. * thermtrip_program() - Configures the hardware to shut down the
  285. * system if a given sensor group reaches a given temperature
  286. * @dev: ptr to the struct device for the SOC_THERM IP block
  287. * @sg: pointer to the sensor group to set the thermtrip temperature for
  288. * @trip_temp: the temperature in millicelsius to trigger the thermal trip at
  289. *
  290. * Sets the thermal trip threshold of the given sensor group to be the
  291. * @trip_temp. If this threshold is crossed, the hardware will shut
  292. * down.
  293. *
  294. * Note that, although @trip_temp is specified in millicelsius, the
  295. * hardware is programmed in degrees Celsius.
  296. *
  297. * Return: 0 upon success, or %-EINVAL upon failure.
  298. */
  299. static int thermtrip_program(struct device *dev,
  300. const struct tegra_tsensor_group *sg,
  301. int trip_temp)
  302. {
  303. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  304. int temp;
  305. u32 r;
  306. if (!sg || !sg->thermtrip_threshold_mask)
  307. return -EINVAL;
  308. temp = enforce_temp_range(dev, trip_temp) / ts->soc->thresh_grain;
  309. r = readl(ts->regs + THERMCTL_THERMTRIP_CTL);
  310. r = REG_SET_MASK(r, sg->thermtrip_threshold_mask, temp);
  311. r = REG_SET_MASK(r, sg->thermtrip_enable_mask, 1);
  312. r = REG_SET_MASK(r, sg->thermtrip_any_en_mask, 0);
  313. writel(r, ts->regs + THERMCTL_THERMTRIP_CTL);
  314. return 0;
  315. }
  316. /**
  317. * throttrip_program() - Configures the hardware to throttle the
  318. * pulse if a given sensor group reaches a given temperature
  319. * @dev: ptr to the struct device for the SOC_THERM IP block
  320. * @sg: pointer to the sensor group to set the thermtrip temperature for
  321. * @stc: pointer to the throttle need to be triggered
  322. * @trip_temp: the temperature in millicelsius to trigger the thermal trip at
  323. *
  324. * Sets the thermal trip threshold and throttle event of the given sensor
  325. * group. If this threshold is crossed, the hardware will trigger the
  326. * throttle.
  327. *
  328. * Note that, although @trip_temp is specified in millicelsius, the
  329. * hardware is programmed in degrees Celsius.
  330. *
  331. * Return: 0 upon success, or %-EINVAL upon failure.
  332. */
  333. static int throttrip_program(struct device *dev,
  334. const struct tegra_tsensor_group *sg,
  335. struct soctherm_throt_cfg *stc,
  336. int trip_temp)
  337. {
  338. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  339. int temp, cpu_throt, gpu_throt;
  340. unsigned int throt;
  341. u32 r, reg_off;
  342. if (!sg || !stc || !stc->init)
  343. return -EINVAL;
  344. temp = enforce_temp_range(dev, trip_temp) / ts->soc->thresh_grain;
  345. /* Hardcode LIGHT on LEVEL1 and HEAVY on LEVEL2 */
  346. throt = stc->id;
  347. reg_off = THERMCTL_LVL_REG(sg->thermctl_lvl0_offset, throt + 1);
  348. if (throt == THROTTLE_LIGHT) {
  349. cpu_throt = THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT;
  350. gpu_throt = THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT;
  351. } else {
  352. cpu_throt = THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY;
  353. gpu_throt = THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY;
  354. if (throt != THROTTLE_HEAVY)
  355. dev_warn(dev,
  356. "invalid throt id %d - assuming HEAVY",
  357. throt);
  358. }
  359. r = readl(ts->regs + reg_off);
  360. r = REG_SET_MASK(r, sg->thermctl_lvl0_up_thresh_mask, temp);
  361. r = REG_SET_MASK(r, sg->thermctl_lvl0_dn_thresh_mask, temp);
  362. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_CPU_THROT_MASK, cpu_throt);
  363. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_GPU_THROT_MASK, gpu_throt);
  364. r = REG_SET_MASK(r, THERMCTL_LVL0_CPU0_EN_MASK, 1);
  365. writel(r, ts->regs + reg_off);
  366. return 0;
  367. }
  368. static struct soctherm_throt_cfg *
  369. find_throttle_cfg_by_name(struct tegra_soctherm *ts, const char *name)
  370. {
  371. unsigned int i;
  372. for (i = 0; ts->throt_cfgs[i].name; i++)
  373. if (!strcmp(ts->throt_cfgs[i].name, name))
  374. return &ts->throt_cfgs[i];
  375. return NULL;
  376. }
  377. static int tegra_thermctl_set_trip_temp(void *data, int trip, int temp)
  378. {
  379. struct tegra_thermctl_zone *zone = data;
  380. struct thermal_zone_device *tz = zone->tz;
  381. struct tegra_soctherm *ts = zone->ts;
  382. const struct tegra_tsensor_group *sg = zone->sg;
  383. struct device *dev = zone->dev;
  384. enum thermal_trip_type type;
  385. int ret;
  386. if (!tz)
  387. return -EINVAL;
  388. ret = tz->ops->get_trip_type(tz, trip, &type);
  389. if (ret)
  390. return ret;
  391. if (type == THERMAL_TRIP_CRITICAL) {
  392. return thermtrip_program(dev, sg, temp);
  393. } else if (type == THERMAL_TRIP_HOT) {
  394. int i;
  395. for (i = 0; i < THROTTLE_SIZE; i++) {
  396. struct thermal_cooling_device *cdev;
  397. struct soctherm_throt_cfg *stc;
  398. if (!ts->throt_cfgs[i].init)
  399. continue;
  400. cdev = ts->throt_cfgs[i].cdev;
  401. if (get_thermal_instance(tz, cdev, trip))
  402. stc = find_throttle_cfg_by_name(ts, cdev->type);
  403. else
  404. continue;
  405. return throttrip_program(dev, sg, stc, temp);
  406. }
  407. }
  408. return 0;
  409. }
  410. static const struct thermal_zone_of_device_ops tegra_of_thermal_ops = {
  411. .get_temp = tegra_thermctl_get_temp,
  412. .set_trip_temp = tegra_thermctl_set_trip_temp,
  413. };
  414. static int get_hot_temp(struct thermal_zone_device *tz, int *trip, int *temp)
  415. {
  416. int ntrips, i, ret;
  417. enum thermal_trip_type type;
  418. ntrips = of_thermal_get_ntrips(tz);
  419. if (ntrips <= 0)
  420. return -EINVAL;
  421. for (i = 0; i < ntrips; i++) {
  422. ret = tz->ops->get_trip_type(tz, i, &type);
  423. if (ret)
  424. return -EINVAL;
  425. if (type == THERMAL_TRIP_HOT) {
  426. ret = tz->ops->get_trip_temp(tz, i, temp);
  427. if (!ret)
  428. *trip = i;
  429. return ret;
  430. }
  431. }
  432. return -EINVAL;
  433. }
  434. /**
  435. * tegra_soctherm_set_hwtrips() - set HW trip point from DT data
  436. * @dev: struct device * of the SOC_THERM instance
  437. *
  438. * Configure the SOC_THERM HW trip points, setting "THERMTRIP"
  439. * "THROTTLE" trip points , using "critical" or "hot" type trip_temp
  440. * from thermal zone.
  441. * After they have been configured, THERMTRIP or THROTTLE will take
  442. * action when the configured SoC thermal sensor group reaches a
  443. * certain temperature.
  444. *
  445. * Return: 0 upon success, or a negative error code on failure.
  446. * "Success" does not mean that trips was enabled; it could also
  447. * mean that no node was found in DT.
  448. * THERMTRIP has been enabled successfully when a message similar to
  449. * this one appears on the serial console:
  450. * "thermtrip: will shut down when sensor group XXX reaches YYYYYY mC"
  451. * THROTTLE has been enabled successfully when a message similar to
  452. * this one appears on the serial console:
  453. * ""throttrip: will throttle when sensor group XXX reaches YYYYYY mC"
  454. */
  455. static int tegra_soctherm_set_hwtrips(struct device *dev,
  456. const struct tegra_tsensor_group *sg,
  457. struct thermal_zone_device *tz)
  458. {
  459. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  460. struct soctherm_throt_cfg *stc;
  461. int i, trip, temperature;
  462. int ret;
  463. ret = tz->ops->get_crit_temp(tz, &temperature);
  464. if (ret) {
  465. dev_warn(dev, "thermtrip: %s: missing critical temperature\n",
  466. sg->name);
  467. goto set_throttle;
  468. }
  469. ret = thermtrip_program(dev, sg, temperature);
  470. if (ret) {
  471. dev_err(dev, "thermtrip: %s: error during enable\n",
  472. sg->name);
  473. return ret;
  474. }
  475. dev_info(dev,
  476. "thermtrip: will shut down when %s reaches %d mC\n",
  477. sg->name, temperature);
  478. set_throttle:
  479. ret = get_hot_temp(tz, &trip, &temperature);
  480. if (ret) {
  481. dev_warn(dev, "throttrip: %s: missing hot temperature\n",
  482. sg->name);
  483. return 0;
  484. }
  485. for (i = 0; i < THROTTLE_SIZE; i++) {
  486. struct thermal_cooling_device *cdev;
  487. if (!ts->throt_cfgs[i].init)
  488. continue;
  489. cdev = ts->throt_cfgs[i].cdev;
  490. if (get_thermal_instance(tz, cdev, trip))
  491. stc = find_throttle_cfg_by_name(ts, cdev->type);
  492. else
  493. continue;
  494. ret = throttrip_program(dev, sg, stc, temperature);
  495. if (ret) {
  496. dev_err(dev, "throttrip: %s: error during enable\n",
  497. sg->name);
  498. return ret;
  499. }
  500. dev_info(dev,
  501. "throttrip: will throttle when %s reaches %d mC\n",
  502. sg->name, temperature);
  503. break;
  504. }
  505. if (i == THROTTLE_SIZE)
  506. dev_warn(dev, "throttrip: %s: missing throttle cdev\n",
  507. sg->name);
  508. return 0;
  509. }
  510. #ifdef CONFIG_DEBUG_FS
  511. static int regs_show(struct seq_file *s, void *data)
  512. {
  513. struct platform_device *pdev = s->private;
  514. struct tegra_soctherm *ts = platform_get_drvdata(pdev);
  515. const struct tegra_tsensor *tsensors = ts->soc->tsensors;
  516. const struct tegra_tsensor_group **ttgs = ts->soc->ttgs;
  517. u32 r, state;
  518. int i, level;
  519. seq_puts(s, "-----TSENSE (convert HW)-----\n");
  520. for (i = 0; i < ts->soc->num_tsensors; i++) {
  521. r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG1);
  522. state = REG_GET_MASK(r, SENSOR_CONFIG1_TEMP_ENABLE);
  523. seq_printf(s, "%s: ", tsensors[i].name);
  524. seq_printf(s, "En(%d) ", state);
  525. if (!state) {
  526. seq_puts(s, "\n");
  527. continue;
  528. }
  529. state = REG_GET_MASK(r, SENSOR_CONFIG1_TIDDQ_EN_MASK);
  530. seq_printf(s, "tiddq(%d) ", state);
  531. state = REG_GET_MASK(r, SENSOR_CONFIG1_TEN_COUNT_MASK);
  532. seq_printf(s, "ten_count(%d) ", state);
  533. state = REG_GET_MASK(r, SENSOR_CONFIG1_TSAMPLE_MASK);
  534. seq_printf(s, "tsample(%d) ", state + 1);
  535. r = readl(ts->regs + tsensors[i].base + SENSOR_STATUS1);
  536. state = REG_GET_MASK(r, SENSOR_STATUS1_TEMP_VALID_MASK);
  537. seq_printf(s, "Temp(%d/", state);
  538. state = REG_GET_MASK(r, SENSOR_STATUS1_TEMP_MASK);
  539. seq_printf(s, "%d) ", translate_temp(state));
  540. r = readl(ts->regs + tsensors[i].base + SENSOR_STATUS0);
  541. state = REG_GET_MASK(r, SENSOR_STATUS0_VALID_MASK);
  542. seq_printf(s, "Capture(%d/", state);
  543. state = REG_GET_MASK(r, SENSOR_STATUS0_CAPTURE_MASK);
  544. seq_printf(s, "%d) ", state);
  545. r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG0);
  546. state = REG_GET_MASK(r, SENSOR_CONFIG0_STOP);
  547. seq_printf(s, "Stop(%d) ", state);
  548. state = REG_GET_MASK(r, SENSOR_CONFIG0_TALL_MASK);
  549. seq_printf(s, "Tall(%d) ", state);
  550. state = REG_GET_MASK(r, SENSOR_CONFIG0_TCALC_OVER);
  551. seq_printf(s, "Over(%d/", state);
  552. state = REG_GET_MASK(r, SENSOR_CONFIG0_OVER);
  553. seq_printf(s, "%d/", state);
  554. state = REG_GET_MASK(r, SENSOR_CONFIG0_CPTR_OVER);
  555. seq_printf(s, "%d) ", state);
  556. r = readl(ts->regs + tsensors[i].base + SENSOR_CONFIG2);
  557. state = REG_GET_MASK(r, SENSOR_CONFIG2_THERMA_MASK);
  558. seq_printf(s, "Therm_A/B(%d/", state);
  559. state = REG_GET_MASK(r, SENSOR_CONFIG2_THERMB_MASK);
  560. seq_printf(s, "%d)\n", (s16)state);
  561. }
  562. r = readl(ts->regs + SENSOR_PDIV);
  563. seq_printf(s, "PDIV: 0x%x\n", r);
  564. r = readl(ts->regs + SENSOR_HOTSPOT_OFF);
  565. seq_printf(s, "HOTSPOT: 0x%x\n", r);
  566. seq_puts(s, "\n");
  567. seq_puts(s, "-----SOC_THERM-----\n");
  568. r = readl(ts->regs + SENSOR_TEMP1);
  569. state = REG_GET_MASK(r, SENSOR_TEMP1_CPU_TEMP_MASK);
  570. seq_printf(s, "Temperatures: CPU(%d) ", translate_temp(state));
  571. state = REG_GET_MASK(r, SENSOR_TEMP1_GPU_TEMP_MASK);
  572. seq_printf(s, " GPU(%d) ", translate_temp(state));
  573. r = readl(ts->regs + SENSOR_TEMP2);
  574. state = REG_GET_MASK(r, SENSOR_TEMP2_PLLX_TEMP_MASK);
  575. seq_printf(s, " PLLX(%d) ", translate_temp(state));
  576. state = REG_GET_MASK(r, SENSOR_TEMP2_MEM_TEMP_MASK);
  577. seq_printf(s, " MEM(%d)\n", translate_temp(state));
  578. for (i = 0; i < ts->soc->num_ttgs; i++) {
  579. seq_printf(s, "%s:\n", ttgs[i]->name);
  580. for (level = 0; level < 4; level++) {
  581. s32 v;
  582. u32 mask;
  583. u16 off = ttgs[i]->thermctl_lvl0_offset;
  584. r = readl(ts->regs + THERMCTL_LVL_REG(off, level));
  585. mask = ttgs[i]->thermctl_lvl0_up_thresh_mask;
  586. state = REG_GET_MASK(r, mask);
  587. v = sign_extend32(state, ts->soc->bptt - 1);
  588. v *= ts->soc->thresh_grain;
  589. seq_printf(s, " %d: Up/Dn(%d /", level, v);
  590. mask = ttgs[i]->thermctl_lvl0_dn_thresh_mask;
  591. state = REG_GET_MASK(r, mask);
  592. v = sign_extend32(state, ts->soc->bptt - 1);
  593. v *= ts->soc->thresh_grain;
  594. seq_printf(s, "%d ) ", v);
  595. mask = THERMCTL_LVL0_CPU0_EN_MASK;
  596. state = REG_GET_MASK(r, mask);
  597. seq_printf(s, "En(%d) ", state);
  598. mask = THERMCTL_LVL0_CPU0_CPU_THROT_MASK;
  599. state = REG_GET_MASK(r, mask);
  600. seq_puts(s, "CPU Throt");
  601. if (!state)
  602. seq_printf(s, "(%s) ", "none");
  603. else if (state == THERMCTL_LVL0_CPU0_CPU_THROT_LIGHT)
  604. seq_printf(s, "(%s) ", "L");
  605. else if (state == THERMCTL_LVL0_CPU0_CPU_THROT_HEAVY)
  606. seq_printf(s, "(%s) ", "H");
  607. else
  608. seq_printf(s, "(%s) ", "H+L");
  609. mask = THERMCTL_LVL0_CPU0_GPU_THROT_MASK;
  610. state = REG_GET_MASK(r, mask);
  611. seq_puts(s, "GPU Throt");
  612. if (!state)
  613. seq_printf(s, "(%s) ", "none");
  614. else if (state == THERMCTL_LVL0_CPU0_GPU_THROT_LIGHT)
  615. seq_printf(s, "(%s) ", "L");
  616. else if (state == THERMCTL_LVL0_CPU0_GPU_THROT_HEAVY)
  617. seq_printf(s, "(%s) ", "H");
  618. else
  619. seq_printf(s, "(%s) ", "H+L");
  620. mask = THERMCTL_LVL0_CPU0_STATUS_MASK;
  621. state = REG_GET_MASK(r, mask);
  622. seq_printf(s, "Status(%s)\n",
  623. state == 0 ? "LO" :
  624. state == 1 ? "In" :
  625. state == 2 ? "Res" : "HI");
  626. }
  627. }
  628. r = readl(ts->regs + THERMCTL_STATS_CTL);
  629. seq_printf(s, "STATS: Up(%s) Dn(%s)\n",
  630. r & STATS_CTL_EN_UP ? "En" : "--",
  631. r & STATS_CTL_EN_DN ? "En" : "--");
  632. for (level = 0; level < 4; level++) {
  633. u16 off;
  634. off = THERMCTL_LVL0_UP_STATS;
  635. r = readl(ts->regs + THERMCTL_LVL_REG(off, level));
  636. seq_printf(s, " Level_%d Up(%d) ", level, r);
  637. off = THERMCTL_LVL0_DN_STATS;
  638. r = readl(ts->regs + THERMCTL_LVL_REG(off, level));
  639. seq_printf(s, "Dn(%d)\n", r);
  640. }
  641. r = readl(ts->regs + THERMCTL_THERMTRIP_CTL);
  642. state = REG_GET_MASK(r, ttgs[0]->thermtrip_any_en_mask);
  643. seq_printf(s, "Thermtrip Any En(%d)\n", state);
  644. for (i = 0; i < ts->soc->num_ttgs; i++) {
  645. state = REG_GET_MASK(r, ttgs[i]->thermtrip_enable_mask);
  646. seq_printf(s, " %s En(%d) ", ttgs[i]->name, state);
  647. state = REG_GET_MASK(r, ttgs[i]->thermtrip_threshold_mask);
  648. state *= ts->soc->thresh_grain;
  649. seq_printf(s, "Thresh(%d)\n", state);
  650. }
  651. r = readl(ts->regs + THROT_GLOBAL_CFG);
  652. seq_puts(s, "\n");
  653. seq_printf(s, "GLOBAL THROTTLE CONFIG: 0x%08x\n", r);
  654. seq_puts(s, "---------------------------------------------------\n");
  655. r = readl(ts->regs + THROT_STATUS);
  656. state = REG_GET_MASK(r, THROT_STATUS_BREACH_MASK);
  657. seq_printf(s, "THROT STATUS: breach(%d) ", state);
  658. state = REG_GET_MASK(r, THROT_STATUS_STATE_MASK);
  659. seq_printf(s, "state(%d) ", state);
  660. state = REG_GET_MASK(r, THROT_STATUS_ENABLED_MASK);
  661. seq_printf(s, "enabled(%d)\n", state);
  662. r = readl(ts->regs + CPU_PSKIP_STATUS);
  663. if (ts->soc->use_ccroc) {
  664. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_ENABLED_MASK);
  665. seq_printf(s, "CPU PSKIP STATUS: enabled(%d)\n", state);
  666. } else {
  667. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_M_MASK);
  668. seq_printf(s, "CPU PSKIP STATUS: M(%d) ", state);
  669. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_N_MASK);
  670. seq_printf(s, "N(%d) ", state);
  671. state = REG_GET_MASK(r, XPU_PSKIP_STATUS_ENABLED_MASK);
  672. seq_printf(s, "enabled(%d)\n", state);
  673. }
  674. return 0;
  675. }
  676. static int regs_open(struct inode *inode, struct file *file)
  677. {
  678. return single_open(file, regs_show, inode->i_private);
  679. }
  680. static const struct file_operations regs_fops = {
  681. .open = regs_open,
  682. .read = seq_read,
  683. .llseek = seq_lseek,
  684. .release = single_release,
  685. };
  686. static void soctherm_debug_init(struct platform_device *pdev)
  687. {
  688. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  689. struct dentry *root, *file;
  690. root = debugfs_create_dir("soctherm", NULL);
  691. if (!root) {
  692. dev_err(&pdev->dev, "failed to create debugfs directory\n");
  693. return;
  694. }
  695. tegra->debugfs_dir = root;
  696. file = debugfs_create_file("reg_contents", 0644, root,
  697. pdev, &regs_fops);
  698. if (!file) {
  699. dev_err(&pdev->dev, "failed to create debugfs file\n");
  700. debugfs_remove_recursive(tegra->debugfs_dir);
  701. tegra->debugfs_dir = NULL;
  702. }
  703. }
  704. #else
  705. static inline void soctherm_debug_init(struct platform_device *pdev) {}
  706. #endif
  707. static int soctherm_clk_enable(struct platform_device *pdev, bool enable)
  708. {
  709. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  710. int err;
  711. if (!tegra->clock_soctherm || !tegra->clock_tsensor)
  712. return -EINVAL;
  713. reset_control_assert(tegra->reset);
  714. if (enable) {
  715. err = clk_prepare_enable(tegra->clock_soctherm);
  716. if (err) {
  717. reset_control_deassert(tegra->reset);
  718. return err;
  719. }
  720. err = clk_prepare_enable(tegra->clock_tsensor);
  721. if (err) {
  722. clk_disable_unprepare(tegra->clock_soctherm);
  723. reset_control_deassert(tegra->reset);
  724. return err;
  725. }
  726. } else {
  727. clk_disable_unprepare(tegra->clock_tsensor);
  728. clk_disable_unprepare(tegra->clock_soctherm);
  729. }
  730. reset_control_deassert(tegra->reset);
  731. return 0;
  732. }
  733. static int throt_get_cdev_max_state(struct thermal_cooling_device *cdev,
  734. unsigned long *max_state)
  735. {
  736. *max_state = 1;
  737. return 0;
  738. }
  739. static int throt_get_cdev_cur_state(struct thermal_cooling_device *cdev,
  740. unsigned long *cur_state)
  741. {
  742. struct tegra_soctherm *ts = cdev->devdata;
  743. u32 r;
  744. r = readl(ts->regs + THROT_STATUS);
  745. if (REG_GET_MASK(r, THROT_STATUS_STATE_MASK))
  746. *cur_state = 1;
  747. else
  748. *cur_state = 0;
  749. return 0;
  750. }
  751. static int throt_set_cdev_state(struct thermal_cooling_device *cdev,
  752. unsigned long cur_state)
  753. {
  754. return 0;
  755. }
  756. static const struct thermal_cooling_device_ops throt_cooling_ops = {
  757. .get_max_state = throt_get_cdev_max_state,
  758. .get_cur_state = throt_get_cdev_cur_state,
  759. .set_cur_state = throt_set_cdev_state,
  760. };
  761. /**
  762. * soctherm_init_hw_throt_cdev() - Parse the HW throttle configurations
  763. * and register them as cooling devices.
  764. */
  765. static void soctherm_init_hw_throt_cdev(struct platform_device *pdev)
  766. {
  767. struct device *dev = &pdev->dev;
  768. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  769. struct device_node *np_stc, *np_stcc;
  770. const char *name;
  771. u32 val;
  772. int i, r;
  773. for (i = 0; i < THROTTLE_SIZE; i++) {
  774. ts->throt_cfgs[i].name = throt_names[i];
  775. ts->throt_cfgs[i].id = i;
  776. ts->throt_cfgs[i].init = false;
  777. }
  778. np_stc = of_get_child_by_name(dev->of_node, "throttle-cfgs");
  779. if (!np_stc) {
  780. dev_info(dev,
  781. "throttle-cfg: no throttle-cfgs - not enabling\n");
  782. return;
  783. }
  784. for_each_child_of_node(np_stc, np_stcc) {
  785. struct soctherm_throt_cfg *stc;
  786. struct thermal_cooling_device *tcd;
  787. name = np_stcc->name;
  788. stc = find_throttle_cfg_by_name(ts, name);
  789. if (!stc) {
  790. dev_err(dev,
  791. "throttle-cfg: could not find %s\n", name);
  792. continue;
  793. }
  794. r = of_property_read_u32(np_stcc, "nvidia,priority", &val);
  795. if (r) {
  796. dev_info(dev,
  797. "throttle-cfg: %s: missing priority\n", name);
  798. continue;
  799. }
  800. stc->priority = val;
  801. if (ts->soc->use_ccroc) {
  802. r = of_property_read_u32(np_stcc,
  803. "nvidia,cpu-throt-level",
  804. &val);
  805. if (r) {
  806. dev_info(dev,
  807. "throttle-cfg: %s: missing cpu-throt-level\n",
  808. name);
  809. continue;
  810. }
  811. stc->cpu_throt_level = val;
  812. } else {
  813. r = of_property_read_u32(np_stcc,
  814. "nvidia,cpu-throt-percent",
  815. &val);
  816. if (r) {
  817. dev_info(dev,
  818. "throttle-cfg: %s: missing cpu-throt-percent\n",
  819. name);
  820. continue;
  821. }
  822. stc->cpu_throt_depth = val;
  823. }
  824. tcd = thermal_of_cooling_device_register(np_stcc,
  825. (char *)name, ts,
  826. &throt_cooling_ops);
  827. of_node_put(np_stcc);
  828. if (IS_ERR_OR_NULL(tcd)) {
  829. dev_err(dev,
  830. "throttle-cfg: %s: failed to register cooling device\n",
  831. name);
  832. continue;
  833. }
  834. stc->cdev = tcd;
  835. stc->init = true;
  836. }
  837. of_node_put(np_stc);
  838. }
  839. /**
  840. * throttlectl_cpu_level_cfg() - programs CCROC NV_THERM level config
  841. * @level: describing the level LOW/MED/HIGH of throttling
  842. *
  843. * It's necessary to set up the CPU-local CCROC NV_THERM instance with
  844. * the M/N values desired for each level. This function does this.
  845. *
  846. * This function pre-programs the CCROC NV_THERM levels in terms of
  847. * pre-configured "Low", "Medium" or "Heavy" throttle levels which are
  848. * mapped to THROT_LEVEL_LOW, THROT_LEVEL_MED and THROT_LEVEL_HVY.
  849. */
  850. static void throttlectl_cpu_level_cfg(struct tegra_soctherm *ts, int level)
  851. {
  852. u8 depth, dividend;
  853. u32 r;
  854. switch (level) {
  855. case TEGRA_SOCTHERM_THROT_LEVEL_LOW:
  856. depth = 50;
  857. break;
  858. case TEGRA_SOCTHERM_THROT_LEVEL_MED:
  859. depth = 75;
  860. break;
  861. case TEGRA_SOCTHERM_THROT_LEVEL_HIGH:
  862. depth = 80;
  863. break;
  864. case TEGRA_SOCTHERM_THROT_LEVEL_NONE:
  865. return;
  866. default:
  867. return;
  868. }
  869. dividend = THROT_DEPTH_DIVIDEND(depth);
  870. /* setup PSKIP in ccroc nv_therm registers */
  871. r = ccroc_readl(ts, CCROC_THROT_PSKIP_RAMP_CPU_REG(level));
  872. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_RAMP_DURATION_MASK, 0xff);
  873. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_RAMP_STEP_MASK, 0xf);
  874. ccroc_writel(ts, r, CCROC_THROT_PSKIP_RAMP_CPU_REG(level));
  875. r = ccroc_readl(ts, CCROC_THROT_PSKIP_CTRL_CPU_REG(level));
  876. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_ENB_MASK, 1);
  877. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_DIVIDEND_MASK, dividend);
  878. r = REG_SET_MASK(r, CCROC_THROT_PSKIP_CTRL_DIVISOR_MASK, 0xff);
  879. ccroc_writel(ts, r, CCROC_THROT_PSKIP_CTRL_CPU_REG(level));
  880. }
  881. /**
  882. * throttlectl_cpu_level_select() - program CPU pulse skipper config
  883. * @throt: the LIGHT/HEAVY of throttle event id
  884. *
  885. * Pulse skippers are used to throttle clock frequencies. This
  886. * function programs the pulse skippers based on @throt and platform
  887. * data. This function is used on SoCs which have CPU-local pulse
  888. * skipper control, such as T13x. It programs soctherm's interface to
  889. * Denver:CCROC NV_THERM in terms of Low, Medium and HIGH throttling
  890. * vectors. PSKIP_BYPASS mode is set as required per HW spec.
  891. */
  892. static void throttlectl_cpu_level_select(struct tegra_soctherm *ts,
  893. enum soctherm_throttle_id throt)
  894. {
  895. u32 r, throt_vect;
  896. /* Denver:CCROC NV_THERM interface N:3 Mapping */
  897. switch (ts->throt_cfgs[throt].cpu_throt_level) {
  898. case TEGRA_SOCTHERM_THROT_LEVEL_LOW:
  899. throt_vect = THROT_VECT_LOW;
  900. break;
  901. case TEGRA_SOCTHERM_THROT_LEVEL_MED:
  902. throt_vect = THROT_VECT_MED;
  903. break;
  904. case TEGRA_SOCTHERM_THROT_LEVEL_HIGH:
  905. throt_vect = THROT_VECT_HIGH;
  906. break;
  907. default:
  908. throt_vect = THROT_VECT_NONE;
  909. break;
  910. }
  911. r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  912. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1);
  913. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT_CPU_MASK, throt_vect);
  914. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_VECT2_CPU_MASK, throt_vect);
  915. writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  916. /* bypass sequencer in soc_therm as it is programmed in ccroc */
  917. r = REG_SET_MASK(0, THROT_PSKIP_RAMP_SEQ_BYPASS_MODE_MASK, 1);
  918. writel(r, ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU));
  919. }
  920. /**
  921. * throttlectl_cpu_mn() - program CPU pulse skipper configuration
  922. * @throt: the LIGHT/HEAVY of throttle event id
  923. *
  924. * Pulse skippers are used to throttle clock frequencies. This
  925. * function programs the pulse skippers based on @throt and platform
  926. * data. This function is used for CPUs that have "remote" pulse
  927. * skipper control, e.g., the CPU pulse skipper is controlled by the
  928. * SOC_THERM IP block. (SOC_THERM is located outside the CPU
  929. * complex.)
  930. */
  931. static void throttlectl_cpu_mn(struct tegra_soctherm *ts,
  932. enum soctherm_throttle_id throt)
  933. {
  934. u32 r;
  935. int depth;
  936. u8 dividend;
  937. depth = ts->throt_cfgs[throt].cpu_throt_depth;
  938. dividend = THROT_DEPTH_DIVIDEND(depth);
  939. r = readl(ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  940. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_ENABLE_MASK, 1);
  941. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_DIVIDEND_MASK, dividend);
  942. r = REG_SET_MASK(r, THROT_PSKIP_CTRL_DIVISOR_MASK, 0xff);
  943. writel(r, ts->regs + THROT_PSKIP_CTRL(throt, THROTTLE_DEV_CPU));
  944. r = readl(ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU));
  945. r = REG_SET_MASK(r, THROT_PSKIP_RAMP_DURATION_MASK, 0xff);
  946. r = REG_SET_MASK(r, THROT_PSKIP_RAMP_STEP_MASK, 0xf);
  947. writel(r, ts->regs + THROT_PSKIP_RAMP(throt, THROTTLE_DEV_CPU));
  948. }
  949. /**
  950. * soctherm_throttle_program() - programs pulse skippers' configuration
  951. * @throt: the LIGHT/HEAVY of the throttle event id.
  952. *
  953. * Pulse skippers are used to throttle clock frequencies.
  954. * This function programs the pulse skippers.
  955. */
  956. static void soctherm_throttle_program(struct tegra_soctherm *ts,
  957. enum soctherm_throttle_id throt)
  958. {
  959. u32 r;
  960. struct soctherm_throt_cfg stc = ts->throt_cfgs[throt];
  961. if (!stc.init)
  962. return;
  963. /* Setup PSKIP parameters */
  964. if (ts->soc->use_ccroc)
  965. throttlectl_cpu_level_select(ts, throt);
  966. else
  967. throttlectl_cpu_mn(ts, throt);
  968. r = REG_SET_MASK(0, THROT_PRIORITY_LITE_PRIO_MASK, stc.priority);
  969. writel(r, ts->regs + THROT_PRIORITY_CTRL(throt));
  970. r = REG_SET_MASK(0, THROT_DELAY_LITE_DELAY_MASK, 0);
  971. writel(r, ts->regs + THROT_DELAY_CTRL(throt));
  972. r = readl(ts->regs + THROT_PRIORITY_LOCK);
  973. r = REG_GET_MASK(r, THROT_PRIORITY_LOCK_PRIORITY_MASK);
  974. if (r >= stc.priority)
  975. return;
  976. r = REG_SET_MASK(0, THROT_PRIORITY_LOCK_PRIORITY_MASK,
  977. stc.priority);
  978. writel(r, ts->regs + THROT_PRIORITY_LOCK);
  979. }
  980. static void tegra_soctherm_throttle(struct device *dev)
  981. {
  982. struct tegra_soctherm *ts = dev_get_drvdata(dev);
  983. u32 v;
  984. int i;
  985. /* configure LOW, MED and HIGH levels for CCROC NV_THERM */
  986. if (ts->soc->use_ccroc) {
  987. throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_LOW);
  988. throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_MED);
  989. throttlectl_cpu_level_cfg(ts, TEGRA_SOCTHERM_THROT_LEVEL_HIGH);
  990. }
  991. /* Thermal HW throttle programming */
  992. for (i = 0; i < THROTTLE_SIZE; i++)
  993. soctherm_throttle_program(ts, i);
  994. v = REG_SET_MASK(0, THROT_GLOBAL_ENB_MASK, 1);
  995. if (ts->soc->use_ccroc) {
  996. ccroc_writel(ts, v, CCROC_GLOBAL_CFG);
  997. v = ccroc_readl(ts, CCROC_SUPER_CCLKG_DIVIDER);
  998. v = REG_SET_MASK(v, CDIVG_USE_THERM_CONTROLS_MASK, 1);
  999. ccroc_writel(ts, v, CCROC_SUPER_CCLKG_DIVIDER);
  1000. } else {
  1001. writel(v, ts->regs + THROT_GLOBAL_CFG);
  1002. v = readl(ts->clk_regs + CAR_SUPER_CCLKG_DIVIDER);
  1003. v = REG_SET_MASK(v, CDIVG_USE_THERM_CONTROLS_MASK, 1);
  1004. writel(v, ts->clk_regs + CAR_SUPER_CCLKG_DIVIDER);
  1005. }
  1006. /* initialize stats collection */
  1007. v = STATS_CTL_CLR_DN | STATS_CTL_EN_DN |
  1008. STATS_CTL_CLR_UP | STATS_CTL_EN_UP;
  1009. writel(v, ts->regs + THERMCTL_STATS_CTL);
  1010. }
  1011. static void soctherm_init(struct platform_device *pdev)
  1012. {
  1013. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1014. const struct tegra_tsensor_group **ttgs = tegra->soc->ttgs;
  1015. int i;
  1016. u32 pdiv, hotspot;
  1017. /* Initialize raw sensors */
  1018. for (i = 0; i < tegra->soc->num_tsensors; ++i)
  1019. enable_tsensor(tegra, i);
  1020. /* program pdiv and hotspot offsets per THERM */
  1021. pdiv = readl(tegra->regs + SENSOR_PDIV);
  1022. hotspot = readl(tegra->regs + SENSOR_HOTSPOT_OFF);
  1023. for (i = 0; i < tegra->soc->num_ttgs; ++i) {
  1024. pdiv = REG_SET_MASK(pdiv, ttgs[i]->pdiv_mask,
  1025. ttgs[i]->pdiv);
  1026. /* hotspot offset from PLLX, doesn't need to configure PLLX */
  1027. if (ttgs[i]->id == TEGRA124_SOCTHERM_SENSOR_PLLX)
  1028. continue;
  1029. hotspot = REG_SET_MASK(hotspot,
  1030. ttgs[i]->pllx_hotspot_mask,
  1031. ttgs[i]->pllx_hotspot_diff);
  1032. }
  1033. writel(pdiv, tegra->regs + SENSOR_PDIV);
  1034. writel(hotspot, tegra->regs + SENSOR_HOTSPOT_OFF);
  1035. /* Configure hw throttle */
  1036. tegra_soctherm_throttle(&pdev->dev);
  1037. }
  1038. static const struct of_device_id tegra_soctherm_of_match[] = {
  1039. #ifdef CONFIG_ARCH_TEGRA_124_SOC
  1040. {
  1041. .compatible = "nvidia,tegra124-soctherm",
  1042. .data = &tegra124_soctherm,
  1043. },
  1044. #endif
  1045. #ifdef CONFIG_ARCH_TEGRA_132_SOC
  1046. {
  1047. .compatible = "nvidia,tegra132-soctherm",
  1048. .data = &tegra132_soctherm,
  1049. },
  1050. #endif
  1051. #ifdef CONFIG_ARCH_TEGRA_210_SOC
  1052. {
  1053. .compatible = "nvidia,tegra210-soctherm",
  1054. .data = &tegra210_soctherm,
  1055. },
  1056. #endif
  1057. { },
  1058. };
  1059. MODULE_DEVICE_TABLE(of, tegra_soctherm_of_match);
  1060. static int tegra_soctherm_probe(struct platform_device *pdev)
  1061. {
  1062. const struct of_device_id *match;
  1063. struct tegra_soctherm *tegra;
  1064. struct thermal_zone_device *z;
  1065. struct tsensor_shared_calib shared_calib;
  1066. struct resource *res;
  1067. struct tegra_soctherm_soc *soc;
  1068. unsigned int i;
  1069. int err;
  1070. match = of_match_node(tegra_soctherm_of_match, pdev->dev.of_node);
  1071. if (!match)
  1072. return -ENODEV;
  1073. soc = (struct tegra_soctherm_soc *)match->data;
  1074. if (soc->num_ttgs > TEGRA124_SOCTHERM_SENSOR_NUM)
  1075. return -EINVAL;
  1076. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  1077. if (!tegra)
  1078. return -ENOMEM;
  1079. dev_set_drvdata(&pdev->dev, tegra);
  1080. tegra->soc = soc;
  1081. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1082. "soctherm-reg");
  1083. tegra->regs = devm_ioremap_resource(&pdev->dev, res);
  1084. if (IS_ERR(tegra->regs)) {
  1085. dev_err(&pdev->dev, "can't get soctherm registers");
  1086. return PTR_ERR(tegra->regs);
  1087. }
  1088. if (!tegra->soc->use_ccroc) {
  1089. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1090. "car-reg");
  1091. tegra->clk_regs = devm_ioremap_resource(&pdev->dev, res);
  1092. if (IS_ERR(tegra->clk_regs)) {
  1093. dev_err(&pdev->dev, "can't get car clk registers");
  1094. return PTR_ERR(tegra->clk_regs);
  1095. }
  1096. } else {
  1097. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1098. "ccroc-reg");
  1099. tegra->ccroc_regs = devm_ioremap_resource(&pdev->dev, res);
  1100. if (IS_ERR(tegra->ccroc_regs)) {
  1101. dev_err(&pdev->dev, "can't get ccroc registers");
  1102. return PTR_ERR(tegra->ccroc_regs);
  1103. }
  1104. }
  1105. tegra->reset = devm_reset_control_get(&pdev->dev, "soctherm");
  1106. if (IS_ERR(tegra->reset)) {
  1107. dev_err(&pdev->dev, "can't get soctherm reset\n");
  1108. return PTR_ERR(tegra->reset);
  1109. }
  1110. tegra->clock_tsensor = devm_clk_get(&pdev->dev, "tsensor");
  1111. if (IS_ERR(tegra->clock_tsensor)) {
  1112. dev_err(&pdev->dev, "can't get tsensor clock\n");
  1113. return PTR_ERR(tegra->clock_tsensor);
  1114. }
  1115. tegra->clock_soctherm = devm_clk_get(&pdev->dev, "soctherm");
  1116. if (IS_ERR(tegra->clock_soctherm)) {
  1117. dev_err(&pdev->dev, "can't get soctherm clock\n");
  1118. return PTR_ERR(tegra->clock_soctherm);
  1119. }
  1120. tegra->calib = devm_kcalloc(&pdev->dev,
  1121. soc->num_tsensors, sizeof(u32),
  1122. GFP_KERNEL);
  1123. if (!tegra->calib)
  1124. return -ENOMEM;
  1125. /* calculate shared calibration data */
  1126. err = tegra_calc_shared_calib(soc->tfuse, &shared_calib);
  1127. if (err)
  1128. return err;
  1129. /* calculate tsensor calibaration data */
  1130. for (i = 0; i < soc->num_tsensors; ++i) {
  1131. err = tegra_calc_tsensor_calib(&soc->tsensors[i],
  1132. &shared_calib,
  1133. &tegra->calib[i]);
  1134. if (err)
  1135. return err;
  1136. }
  1137. tegra->thermctl_tzs = devm_kcalloc(&pdev->dev,
  1138. soc->num_ttgs, sizeof(*z),
  1139. GFP_KERNEL);
  1140. if (!tegra->thermctl_tzs)
  1141. return -ENOMEM;
  1142. err = soctherm_clk_enable(pdev, true);
  1143. if (err)
  1144. return err;
  1145. soctherm_init_hw_throt_cdev(pdev);
  1146. soctherm_init(pdev);
  1147. for (i = 0; i < soc->num_ttgs; ++i) {
  1148. struct tegra_thermctl_zone *zone =
  1149. devm_kzalloc(&pdev->dev, sizeof(*zone), GFP_KERNEL);
  1150. if (!zone) {
  1151. err = -ENOMEM;
  1152. goto disable_clocks;
  1153. }
  1154. zone->reg = tegra->regs + soc->ttgs[i]->sensor_temp_offset;
  1155. zone->dev = &pdev->dev;
  1156. zone->sg = soc->ttgs[i];
  1157. zone->ts = tegra;
  1158. z = devm_thermal_zone_of_sensor_register(&pdev->dev,
  1159. soc->ttgs[i]->id, zone,
  1160. &tegra_of_thermal_ops);
  1161. if (IS_ERR(z)) {
  1162. err = PTR_ERR(z);
  1163. dev_err(&pdev->dev, "failed to register sensor: %d\n",
  1164. err);
  1165. goto disable_clocks;
  1166. }
  1167. zone->tz = z;
  1168. tegra->thermctl_tzs[soc->ttgs[i]->id] = z;
  1169. /* Configure hw trip points */
  1170. err = tegra_soctherm_set_hwtrips(&pdev->dev, soc->ttgs[i], z);
  1171. if (err)
  1172. goto disable_clocks;
  1173. }
  1174. soctherm_debug_init(pdev);
  1175. return 0;
  1176. disable_clocks:
  1177. soctherm_clk_enable(pdev, false);
  1178. return err;
  1179. }
  1180. static int tegra_soctherm_remove(struct platform_device *pdev)
  1181. {
  1182. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1183. debugfs_remove_recursive(tegra->debugfs_dir);
  1184. soctherm_clk_enable(pdev, false);
  1185. return 0;
  1186. }
  1187. static int __maybe_unused soctherm_suspend(struct device *dev)
  1188. {
  1189. struct platform_device *pdev = to_platform_device(dev);
  1190. soctherm_clk_enable(pdev, false);
  1191. return 0;
  1192. }
  1193. static int __maybe_unused soctherm_resume(struct device *dev)
  1194. {
  1195. struct platform_device *pdev = to_platform_device(dev);
  1196. struct tegra_soctherm *tegra = platform_get_drvdata(pdev);
  1197. struct tegra_soctherm_soc *soc = tegra->soc;
  1198. int err, i;
  1199. err = soctherm_clk_enable(pdev, true);
  1200. if (err) {
  1201. dev_err(&pdev->dev,
  1202. "Resume failed: enable clocks failed\n");
  1203. return err;
  1204. }
  1205. soctherm_init(pdev);
  1206. for (i = 0; i < soc->num_ttgs; ++i) {
  1207. struct thermal_zone_device *tz;
  1208. tz = tegra->thermctl_tzs[soc->ttgs[i]->id];
  1209. err = tegra_soctherm_set_hwtrips(dev, soc->ttgs[i], tz);
  1210. if (err) {
  1211. dev_err(&pdev->dev,
  1212. "Resume failed: set hwtrips failed\n");
  1213. return err;
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. static SIMPLE_DEV_PM_OPS(tegra_soctherm_pm, soctherm_suspend, soctherm_resume);
  1219. static struct platform_driver tegra_soctherm_driver = {
  1220. .probe = tegra_soctherm_probe,
  1221. .remove = tegra_soctherm_remove,
  1222. .driver = {
  1223. .name = "tegra_soctherm",
  1224. .pm = &tegra_soctherm_pm,
  1225. .of_match_table = tegra_soctherm_of_match,
  1226. },
  1227. };
  1228. module_platform_driver(tegra_soctherm_driver);
  1229. MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
  1230. MODULE_DESCRIPTION("NVIDIA Tegra SOCTHERM thermal management driver");
  1231. MODULE_LICENSE("GPL v2");