rockchip_thermal.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  3. *
  4. * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
  5. * Caesar Wang <wxt@rock-chips.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/reset.h>
  26. #include <linux/thermal.h>
  27. #include <linux/pinctrl/consumer.h>
  28. /**
  29. * If the temperature over a period of time High,
  30. * the resulting TSHUT gave CRU module,let it reset the entire chip,
  31. * or via GPIO give PMIC.
  32. */
  33. enum tshut_mode {
  34. TSHUT_MODE_CRU = 0,
  35. TSHUT_MODE_GPIO,
  36. };
  37. /**
  38. * The system Temperature Sensors tshut(tshut) polarity
  39. * the bit 8 is tshut polarity.
  40. * 0: low active, 1: high active
  41. */
  42. enum tshut_polarity {
  43. TSHUT_LOW_ACTIVE = 0,
  44. TSHUT_HIGH_ACTIVE,
  45. };
  46. /**
  47. * The system has two Temperature Sensors.
  48. * sensor0 is for CPU, and sensor1 is for GPU.
  49. */
  50. enum sensor_id {
  51. SENSOR_CPU = 0,
  52. SENSOR_GPU,
  53. };
  54. /**
  55. * The conversion table has the adc value and temperature.
  56. * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
  57. * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
  58. */
  59. enum adc_sort_mode {
  60. ADC_DECREMENT = 0,
  61. ADC_INCREMENT,
  62. };
  63. /**
  64. * The max sensors is two in rockchip SoCs.
  65. * Two sensors: CPU and GPU sensor.
  66. */
  67. #define SOC_MAX_SENSORS 2
  68. /**
  69. * struct chip_tsadc_table: hold information about chip-specific differences
  70. * @id: conversion table
  71. * @length: size of conversion table
  72. * @data_mask: mask to apply on data inputs
  73. * @mode: sort mode of this adc variant (incrementing or decrementing)
  74. */
  75. struct chip_tsadc_table {
  76. const struct tsadc_table *id;
  77. unsigned int length;
  78. u32 data_mask;
  79. enum adc_sort_mode mode;
  80. };
  81. struct rockchip_tsadc_chip {
  82. /* The sensor id of chip correspond to the ADC channel */
  83. int chn_id[SOC_MAX_SENSORS];
  84. int chn_num;
  85. /* The hardware-controlled tshut property */
  86. int tshut_temp;
  87. enum tshut_mode tshut_mode;
  88. enum tshut_polarity tshut_polarity;
  89. /* Chip-wide methods */
  90. void (*initialize)(void __iomem *reg, enum tshut_polarity p);
  91. void (*irq_ack)(void __iomem *reg);
  92. void (*control)(void __iomem *reg, bool on);
  93. /* Per-sensor methods */
  94. int (*get_temp)(struct chip_tsadc_table table,
  95. int chn, void __iomem *reg, int *temp);
  96. void (*set_tshut_temp)(struct chip_tsadc_table table,
  97. int chn, void __iomem *reg, int temp);
  98. void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
  99. /* Per-table methods */
  100. struct chip_tsadc_table table;
  101. };
  102. struct rockchip_thermal_sensor {
  103. struct rockchip_thermal_data *thermal;
  104. struct thermal_zone_device *tzd;
  105. int id;
  106. };
  107. struct rockchip_thermal_data {
  108. const struct rockchip_tsadc_chip *chip;
  109. struct platform_device *pdev;
  110. struct reset_control *reset;
  111. struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
  112. struct clk *clk;
  113. struct clk *pclk;
  114. void __iomem *regs;
  115. int tshut_temp;
  116. enum tshut_mode tshut_mode;
  117. enum tshut_polarity tshut_polarity;
  118. };
  119. /**
  120. * TSADC Sensor Register description:
  121. *
  122. * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
  123. * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
  124. *
  125. */
  126. #define TSADCV2_AUTO_CON 0x04
  127. #define TSADCV2_INT_EN 0x08
  128. #define TSADCV2_INT_PD 0x0c
  129. #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
  130. #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
  131. #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
  132. #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
  133. #define TSADCV2_AUTO_PERIOD 0x68
  134. #define TSADCV2_AUTO_PERIOD_HT 0x6c
  135. #define TSADCV2_AUTO_EN BIT(0)
  136. #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
  137. #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
  138. /**
  139. * TSADCV1_AUTO_Q_SEL_EN:
  140. * whether select (1024 - tsadc_q) as output
  141. * 1'b0:use tsadc_q as output(temperature-code is rising sequence)
  142. * 1'b1:use(1024 - tsadc_q) as output (temperature-code is falling sequence)
  143. */
  144. #define TSADCV3_AUTO_Q_SEL_EN BIT(1)
  145. #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
  146. #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
  147. #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
  148. #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
  149. #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
  150. #define TSADCV2_DATA_MASK 0xfff
  151. #define TSADCV3_DATA_MASK 0x3ff
  152. #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
  153. #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
  154. #define TSADCV2_AUTO_PERIOD_TIME 250 /* msec */
  155. #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* msec */
  156. struct tsadc_table {
  157. u32 code;
  158. int temp;
  159. };
  160. /**
  161. * Note:
  162. * Code to Temperature mapping of the Temperature sensor is a piece wise linear
  163. * curve.Any temperature, code faling between to 2 give temperatures can be
  164. * linearly interpolated.
  165. * Code to Temperature mapping should be updated based on sillcon results.
  166. */
  167. static const struct tsadc_table rk3228_code_table[] = {
  168. {0, -40000},
  169. {588, -40000},
  170. {593, -35000},
  171. {598, -30000},
  172. {603, -25000},
  173. {608, -20000},
  174. {613, -15000},
  175. {618, -10000},
  176. {623, -5000},
  177. {629, 0},
  178. {634, 5000},
  179. {639, 10000},
  180. {644, 15000},
  181. {649, 20000},
  182. {654, 25000},
  183. {660, 30000},
  184. {665, 35000},
  185. {670, 40000},
  186. {675, 45000},
  187. {681, 50000},
  188. {686, 55000},
  189. {691, 60000},
  190. {696, 65000},
  191. {702, 70000},
  192. {707, 75000},
  193. {712, 80000},
  194. {717, 85000},
  195. {723, 90000},
  196. {728, 95000},
  197. {733, 100000},
  198. {738, 105000},
  199. {744, 110000},
  200. {749, 115000},
  201. {754, 120000},
  202. {760, 125000},
  203. {TSADCV2_DATA_MASK, 125000},
  204. };
  205. static const struct tsadc_table rk3288_code_table[] = {
  206. {TSADCV2_DATA_MASK, -40000},
  207. {3800, -40000},
  208. {3792, -35000},
  209. {3783, -30000},
  210. {3774, -25000},
  211. {3765, -20000},
  212. {3756, -15000},
  213. {3747, -10000},
  214. {3737, -5000},
  215. {3728, 0},
  216. {3718, 5000},
  217. {3708, 10000},
  218. {3698, 15000},
  219. {3688, 20000},
  220. {3678, 25000},
  221. {3667, 30000},
  222. {3656, 35000},
  223. {3645, 40000},
  224. {3634, 45000},
  225. {3623, 50000},
  226. {3611, 55000},
  227. {3600, 60000},
  228. {3588, 65000},
  229. {3575, 70000},
  230. {3563, 75000},
  231. {3550, 80000},
  232. {3537, 85000},
  233. {3524, 90000},
  234. {3510, 95000},
  235. {3496, 100000},
  236. {3482, 105000},
  237. {3467, 110000},
  238. {3452, 115000},
  239. {3437, 120000},
  240. {3421, 125000},
  241. };
  242. static const struct tsadc_table rk3368_code_table[] = {
  243. {0, -40000},
  244. {106, -40000},
  245. {108, -35000},
  246. {110, -30000},
  247. {112, -25000},
  248. {114, -20000},
  249. {116, -15000},
  250. {118, -10000},
  251. {120, -5000},
  252. {122, 0},
  253. {124, 5000},
  254. {126, 10000},
  255. {128, 15000},
  256. {130, 20000},
  257. {132, 25000},
  258. {134, 30000},
  259. {136, 35000},
  260. {138, 40000},
  261. {140, 45000},
  262. {142, 50000},
  263. {144, 55000},
  264. {146, 60000},
  265. {148, 65000},
  266. {150, 70000},
  267. {152, 75000},
  268. {154, 80000},
  269. {156, 85000},
  270. {158, 90000},
  271. {160, 95000},
  272. {162, 100000},
  273. {163, 105000},
  274. {165, 110000},
  275. {167, 115000},
  276. {169, 120000},
  277. {171, 125000},
  278. {TSADCV3_DATA_MASK, 125000},
  279. };
  280. static const struct tsadc_table rk3399_code_table[] = {
  281. {0, -40000},
  282. {593, -40000},
  283. {598, -35000},
  284. {603, -30000},
  285. {609, -25000},
  286. {614, -20000},
  287. {619, -15000},
  288. {625, -10000},
  289. {630, -5000},
  290. {635, 0},
  291. {641, 5000},
  292. {646, 10000},
  293. {651, 15000},
  294. {657, 20000},
  295. {662, 25000},
  296. {667, 30000},
  297. {673, 35000},
  298. {678, 40000},
  299. {684, 45000},
  300. {689, 50000},
  301. {694, 55000},
  302. {700, 60000},
  303. {705, 65000},
  304. {711, 70000},
  305. {716, 75000},
  306. {722, 80000},
  307. {727, 85000},
  308. {733, 90000},
  309. {738, 95000},
  310. {743, 100000},
  311. {749, 105000},
  312. {754, 110000},
  313. {760, 115000},
  314. {765, 120000},
  315. {771, 125000},
  316. {TSADCV3_DATA_MASK, 125000},
  317. };
  318. static u32 rk_tsadcv2_temp_to_code(struct chip_tsadc_table table,
  319. int temp)
  320. {
  321. int high, low, mid;
  322. low = 0;
  323. high = table.length - 1;
  324. mid = (high + low) / 2;
  325. if (temp < table.id[low].temp || temp > table.id[high].temp)
  326. return 0;
  327. while (low <= high) {
  328. if (temp == table.id[mid].temp)
  329. return table.id[mid].code;
  330. else if (temp < table.id[mid].temp)
  331. high = mid - 1;
  332. else
  333. low = mid + 1;
  334. mid = (low + high) / 2;
  335. }
  336. return 0;
  337. }
  338. static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table, u32 code,
  339. int *temp)
  340. {
  341. unsigned int low = 1;
  342. unsigned int high = table.length - 1;
  343. unsigned int mid = (low + high) / 2;
  344. unsigned int num;
  345. unsigned long denom;
  346. WARN_ON(table.length < 2);
  347. switch (table.mode) {
  348. case ADC_DECREMENT:
  349. code &= table.data_mask;
  350. if (code < table.id[high].code)
  351. return -EAGAIN; /* Incorrect reading */
  352. while (low <= high) {
  353. if (code >= table.id[mid].code &&
  354. code < table.id[mid - 1].code)
  355. break;
  356. else if (code < table.id[mid].code)
  357. low = mid + 1;
  358. else
  359. high = mid - 1;
  360. mid = (low + high) / 2;
  361. }
  362. break;
  363. case ADC_INCREMENT:
  364. code &= table.data_mask;
  365. if (code < table.id[low].code)
  366. return -EAGAIN; /* Incorrect reading */
  367. while (low <= high) {
  368. if (code >= table.id[mid - 1].code &&
  369. code < table.id[mid].code)
  370. break;
  371. else if (code > table.id[mid].code)
  372. low = mid + 1;
  373. else
  374. high = mid - 1;
  375. mid = (low + high) / 2;
  376. }
  377. break;
  378. default:
  379. pr_err("Invalid the conversion table\n");
  380. }
  381. /*
  382. * The 5C granularity provided by the table is too much. Let's
  383. * assume that the relationship between sensor readings and
  384. * temperature between 2 table entries is linear and interpolate
  385. * to produce less granular result.
  386. */
  387. num = table.id[mid].temp - table.id[mid - 1].temp;
  388. num *= abs(table.id[mid - 1].code - code);
  389. denom = abs(table.id[mid - 1].code - table.id[mid].code);
  390. *temp = table.id[mid - 1].temp + (num / denom);
  391. return 0;
  392. }
  393. /**
  394. * rk_tsadcv2_initialize - initialize TASDC Controller.
  395. *
  396. * (1) Set TSADC_V2_AUTO_PERIOD:
  397. * Configure the interleave between every two accessing of
  398. * TSADC in normal operation.
  399. *
  400. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  401. * Configure the interleave between every two accessing of
  402. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  403. *
  404. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  405. * If the temperature is higher than COMP_INT or COMP_SHUT for
  406. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  407. */
  408. static void rk_tsadcv2_initialize(void __iomem *regs,
  409. enum tshut_polarity tshut_polarity)
  410. {
  411. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  412. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  413. regs + TSADCV2_AUTO_CON);
  414. else
  415. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  416. regs + TSADCV2_AUTO_CON);
  417. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
  418. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  419. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  420. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  421. regs + TSADCV2_AUTO_PERIOD_HT);
  422. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  423. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  424. }
  425. static void rk_tsadcv2_irq_ack(void __iomem *regs)
  426. {
  427. u32 val;
  428. val = readl_relaxed(regs + TSADCV2_INT_PD);
  429. writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  430. }
  431. static void rk_tsadcv3_irq_ack(void __iomem *regs)
  432. {
  433. u32 val;
  434. val = readl_relaxed(regs + TSADCV2_INT_PD);
  435. writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  436. }
  437. static void rk_tsadcv2_control(void __iomem *regs, bool enable)
  438. {
  439. u32 val;
  440. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  441. if (enable)
  442. val |= TSADCV2_AUTO_EN;
  443. else
  444. val &= ~TSADCV2_AUTO_EN;
  445. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  446. }
  447. /**
  448. * @rk_tsadcv3_control:
  449. * TSADC controller works at auto mode, and some SoCs need set the tsadc_q_sel
  450. * bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output adc value if
  451. * setting this bit to enable.
  452. */
  453. static void rk_tsadcv3_control(void __iomem *regs, bool enable)
  454. {
  455. u32 val;
  456. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  457. if (enable)
  458. val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
  459. else
  460. val &= ~TSADCV2_AUTO_EN;
  461. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  462. }
  463. static int rk_tsadcv2_get_temp(struct chip_tsadc_table table,
  464. int chn, void __iomem *regs, int *temp)
  465. {
  466. u32 val;
  467. val = readl_relaxed(regs + TSADCV2_DATA(chn));
  468. return rk_tsadcv2_code_to_temp(table, val, temp);
  469. }
  470. static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table,
  471. int chn, void __iomem *regs, int temp)
  472. {
  473. u32 tshut_value, val;
  474. tshut_value = rk_tsadcv2_temp_to_code(table, temp);
  475. writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
  476. /* TSHUT will be valid */
  477. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  478. writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
  479. }
  480. static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
  481. enum tshut_mode mode)
  482. {
  483. u32 val;
  484. val = readl_relaxed(regs + TSADCV2_INT_EN);
  485. if (mode == TSHUT_MODE_GPIO) {
  486. val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
  487. val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  488. } else {
  489. val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  490. val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
  491. }
  492. writel_relaxed(val, regs + TSADCV2_INT_EN);
  493. }
  494. static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
  495. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  496. .chn_num = 1, /* one channel for tsadc */
  497. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  498. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  499. .tshut_temp = 95000,
  500. .initialize = rk_tsadcv2_initialize,
  501. .irq_ack = rk_tsadcv3_irq_ack,
  502. .control = rk_tsadcv3_control,
  503. .get_temp = rk_tsadcv2_get_temp,
  504. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  505. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  506. .table = {
  507. .id = rk3228_code_table,
  508. .length = ARRAY_SIZE(rk3228_code_table),
  509. .data_mask = TSADCV3_DATA_MASK,
  510. .mode = ADC_INCREMENT,
  511. },
  512. };
  513. static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
  514. .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
  515. .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
  516. .chn_num = 2, /* two channels for tsadc */
  517. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  518. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  519. .tshut_temp = 95000,
  520. .initialize = rk_tsadcv2_initialize,
  521. .irq_ack = rk_tsadcv2_irq_ack,
  522. .control = rk_tsadcv2_control,
  523. .get_temp = rk_tsadcv2_get_temp,
  524. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  525. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  526. .table = {
  527. .id = rk3288_code_table,
  528. .length = ARRAY_SIZE(rk3288_code_table),
  529. .data_mask = TSADCV2_DATA_MASK,
  530. .mode = ADC_DECREMENT,
  531. },
  532. };
  533. static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
  534. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  535. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  536. .chn_num = 2, /* two channels for tsadc */
  537. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  538. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  539. .tshut_temp = 95000,
  540. .initialize = rk_tsadcv2_initialize,
  541. .irq_ack = rk_tsadcv2_irq_ack,
  542. .control = rk_tsadcv2_control,
  543. .get_temp = rk_tsadcv2_get_temp,
  544. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  545. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  546. .table = {
  547. .id = rk3368_code_table,
  548. .length = ARRAY_SIZE(rk3368_code_table),
  549. .data_mask = TSADCV3_DATA_MASK,
  550. .mode = ADC_INCREMENT,
  551. },
  552. };
  553. static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
  554. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  555. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  556. .chn_num = 2, /* two channels for tsadc */
  557. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  558. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  559. .tshut_temp = 95000,
  560. .initialize = rk_tsadcv2_initialize,
  561. .irq_ack = rk_tsadcv3_irq_ack,
  562. .control = rk_tsadcv3_control,
  563. .get_temp = rk_tsadcv2_get_temp,
  564. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  565. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  566. .table = {
  567. .id = rk3399_code_table,
  568. .length = ARRAY_SIZE(rk3399_code_table),
  569. .data_mask = TSADCV3_DATA_MASK,
  570. .mode = ADC_INCREMENT,
  571. },
  572. };
  573. static const struct of_device_id of_rockchip_thermal_match[] = {
  574. {
  575. .compatible = "rockchip,rk3228-tsadc",
  576. .data = (void *)&rk3228_tsadc_data,
  577. },
  578. {
  579. .compatible = "rockchip,rk3288-tsadc",
  580. .data = (void *)&rk3288_tsadc_data,
  581. },
  582. {
  583. .compatible = "rockchip,rk3368-tsadc",
  584. .data = (void *)&rk3368_tsadc_data,
  585. },
  586. {
  587. .compatible = "rockchip,rk3399-tsadc",
  588. .data = (void *)&rk3399_tsadc_data,
  589. },
  590. { /* end */ },
  591. };
  592. MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
  593. static void
  594. rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
  595. {
  596. struct thermal_zone_device *tzd = sensor->tzd;
  597. tzd->ops->set_mode(tzd,
  598. on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
  599. }
  600. static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
  601. {
  602. struct rockchip_thermal_data *thermal = dev;
  603. int i;
  604. dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
  605. thermal->chip->irq_ack(thermal->regs);
  606. for (i = 0; i < thermal->chip->chn_num; i++)
  607. thermal_zone_device_update(thermal->sensors[i].tzd);
  608. return IRQ_HANDLED;
  609. }
  610. static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
  611. {
  612. struct rockchip_thermal_sensor *sensor = _sensor;
  613. struct rockchip_thermal_data *thermal = sensor->thermal;
  614. const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
  615. int retval;
  616. retval = tsadc->get_temp(tsadc->table,
  617. sensor->id, thermal->regs, out_temp);
  618. dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
  619. sensor->id, *out_temp, retval);
  620. return retval;
  621. }
  622. static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
  623. .get_temp = rockchip_thermal_get_temp,
  624. };
  625. static int rockchip_configure_from_dt(struct device *dev,
  626. struct device_node *np,
  627. struct rockchip_thermal_data *thermal)
  628. {
  629. u32 shut_temp, tshut_mode, tshut_polarity;
  630. if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
  631. dev_warn(dev,
  632. "Missing tshut temp property, using default %d\n",
  633. thermal->chip->tshut_temp);
  634. thermal->tshut_temp = thermal->chip->tshut_temp;
  635. } else {
  636. if (shut_temp > INT_MAX) {
  637. dev_err(dev, "Invalid tshut temperature specified: %d\n",
  638. shut_temp);
  639. return -ERANGE;
  640. }
  641. thermal->tshut_temp = shut_temp;
  642. }
  643. if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
  644. dev_warn(dev,
  645. "Missing tshut mode property, using default (%s)\n",
  646. thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
  647. "gpio" : "cru");
  648. thermal->tshut_mode = thermal->chip->tshut_mode;
  649. } else {
  650. thermal->tshut_mode = tshut_mode;
  651. }
  652. if (thermal->tshut_mode > 1) {
  653. dev_err(dev, "Invalid tshut mode specified: %d\n",
  654. thermal->tshut_mode);
  655. return -EINVAL;
  656. }
  657. if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
  658. &tshut_polarity)) {
  659. dev_warn(dev,
  660. "Missing tshut-polarity property, using default (%s)\n",
  661. thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
  662. "low" : "high");
  663. thermal->tshut_polarity = thermal->chip->tshut_polarity;
  664. } else {
  665. thermal->tshut_polarity = tshut_polarity;
  666. }
  667. if (thermal->tshut_polarity > 1) {
  668. dev_err(dev, "Invalid tshut-polarity specified: %d\n",
  669. thermal->tshut_polarity);
  670. return -EINVAL;
  671. }
  672. return 0;
  673. }
  674. static int
  675. rockchip_thermal_register_sensor(struct platform_device *pdev,
  676. struct rockchip_thermal_data *thermal,
  677. struct rockchip_thermal_sensor *sensor,
  678. int id)
  679. {
  680. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  681. int error;
  682. tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
  683. tsadc->set_tshut_temp(tsadc->table, id, thermal->regs,
  684. thermal->tshut_temp);
  685. sensor->thermal = thermal;
  686. sensor->id = id;
  687. sensor->tzd = thermal_zone_of_sensor_register(&pdev->dev, id, sensor,
  688. &rockchip_of_thermal_ops);
  689. if (IS_ERR(sensor->tzd)) {
  690. error = PTR_ERR(sensor->tzd);
  691. dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
  692. id, error);
  693. return error;
  694. }
  695. return 0;
  696. }
  697. /**
  698. * Reset TSADC Controller, reset all tsadc registers.
  699. */
  700. static void rockchip_thermal_reset_controller(struct reset_control *reset)
  701. {
  702. reset_control_assert(reset);
  703. usleep_range(10, 20);
  704. reset_control_deassert(reset);
  705. }
  706. static int rockchip_thermal_probe(struct platform_device *pdev)
  707. {
  708. struct device_node *np = pdev->dev.of_node;
  709. struct rockchip_thermal_data *thermal;
  710. const struct of_device_id *match;
  711. struct resource *res;
  712. int irq;
  713. int i, j;
  714. int error;
  715. match = of_match_node(of_rockchip_thermal_match, np);
  716. if (!match)
  717. return -ENXIO;
  718. irq = platform_get_irq(pdev, 0);
  719. if (irq < 0) {
  720. dev_err(&pdev->dev, "no irq resource?\n");
  721. return -EINVAL;
  722. }
  723. thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
  724. GFP_KERNEL);
  725. if (!thermal)
  726. return -ENOMEM;
  727. thermal->pdev = pdev;
  728. thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
  729. if (!thermal->chip)
  730. return -EINVAL;
  731. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  732. thermal->regs = devm_ioremap_resource(&pdev->dev, res);
  733. if (IS_ERR(thermal->regs))
  734. return PTR_ERR(thermal->regs);
  735. thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
  736. if (IS_ERR(thermal->reset)) {
  737. error = PTR_ERR(thermal->reset);
  738. dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
  739. return error;
  740. }
  741. thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
  742. if (IS_ERR(thermal->clk)) {
  743. error = PTR_ERR(thermal->clk);
  744. dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
  745. return error;
  746. }
  747. thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  748. if (IS_ERR(thermal->pclk)) {
  749. error = PTR_ERR(thermal->pclk);
  750. dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
  751. error);
  752. return error;
  753. }
  754. error = clk_prepare_enable(thermal->clk);
  755. if (error) {
  756. dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
  757. error);
  758. return error;
  759. }
  760. error = clk_prepare_enable(thermal->pclk);
  761. if (error) {
  762. dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
  763. goto err_disable_clk;
  764. }
  765. rockchip_thermal_reset_controller(thermal->reset);
  766. error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
  767. if (error) {
  768. dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
  769. error);
  770. goto err_disable_pclk;
  771. }
  772. thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
  773. for (i = 0; i < thermal->chip->chn_num; i++) {
  774. error = rockchip_thermal_register_sensor(pdev, thermal,
  775. &thermal->sensors[i],
  776. thermal->chip->chn_id[i]);
  777. if (error) {
  778. dev_err(&pdev->dev,
  779. "failed to register sensor[%d] : error = %d\n",
  780. i, error);
  781. for (j = 0; j < i; j++)
  782. thermal_zone_of_sensor_unregister(&pdev->dev,
  783. thermal->sensors[j].tzd);
  784. goto err_disable_pclk;
  785. }
  786. }
  787. error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  788. &rockchip_thermal_alarm_irq_thread,
  789. IRQF_ONESHOT,
  790. "rockchip_thermal", thermal);
  791. if (error) {
  792. dev_err(&pdev->dev,
  793. "failed to request tsadc irq: %d\n", error);
  794. goto err_unregister_sensor;
  795. }
  796. thermal->chip->control(thermal->regs, true);
  797. for (i = 0; i < thermal->chip->chn_num; i++)
  798. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  799. platform_set_drvdata(pdev, thermal);
  800. return 0;
  801. err_unregister_sensor:
  802. while (i--)
  803. thermal_zone_of_sensor_unregister(&pdev->dev,
  804. thermal->sensors[i].tzd);
  805. err_disable_pclk:
  806. clk_disable_unprepare(thermal->pclk);
  807. err_disable_clk:
  808. clk_disable_unprepare(thermal->clk);
  809. return error;
  810. }
  811. static int rockchip_thermal_remove(struct platform_device *pdev)
  812. {
  813. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  814. int i;
  815. for (i = 0; i < thermal->chip->chn_num; i++) {
  816. struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
  817. rockchip_thermal_toggle_sensor(sensor, false);
  818. thermal_zone_of_sensor_unregister(&pdev->dev, sensor->tzd);
  819. }
  820. thermal->chip->control(thermal->regs, false);
  821. clk_disable_unprepare(thermal->pclk);
  822. clk_disable_unprepare(thermal->clk);
  823. return 0;
  824. }
  825. static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
  826. {
  827. struct platform_device *pdev = to_platform_device(dev);
  828. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  829. int i;
  830. for (i = 0; i < thermal->chip->chn_num; i++)
  831. rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
  832. thermal->chip->control(thermal->regs, false);
  833. clk_disable(thermal->pclk);
  834. clk_disable(thermal->clk);
  835. pinctrl_pm_select_sleep_state(dev);
  836. return 0;
  837. }
  838. static int __maybe_unused rockchip_thermal_resume(struct device *dev)
  839. {
  840. struct platform_device *pdev = to_platform_device(dev);
  841. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  842. int i;
  843. int error;
  844. error = clk_enable(thermal->clk);
  845. if (error)
  846. return error;
  847. error = clk_enable(thermal->pclk);
  848. if (error)
  849. return error;
  850. rockchip_thermal_reset_controller(thermal->reset);
  851. thermal->chip->initialize(thermal->regs, thermal->tshut_polarity);
  852. for (i = 0; i < thermal->chip->chn_num; i++) {
  853. int id = thermal->sensors[i].id;
  854. thermal->chip->set_tshut_mode(id, thermal->regs,
  855. thermal->tshut_mode);
  856. thermal->chip->set_tshut_temp(thermal->chip->table,
  857. id, thermal->regs,
  858. thermal->tshut_temp);
  859. }
  860. thermal->chip->control(thermal->regs, true);
  861. for (i = 0; i < thermal->chip->chn_num; i++)
  862. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  863. pinctrl_pm_select_default_state(dev);
  864. return 0;
  865. }
  866. static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
  867. rockchip_thermal_suspend, rockchip_thermal_resume);
  868. static struct platform_driver rockchip_thermal_driver = {
  869. .driver = {
  870. .name = "rockchip-thermal",
  871. .pm = &rockchip_thermal_pm_ops,
  872. .of_match_table = of_rockchip_thermal_match,
  873. },
  874. .probe = rockchip_thermal_probe,
  875. .remove = rockchip_thermal_remove,
  876. };
  877. module_platform_driver(rockchip_thermal_driver);
  878. MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
  879. MODULE_AUTHOR("Rockchip, Inc.");
  880. MODULE_LICENSE("GPL v2");
  881. MODULE_ALIAS("platform:rockchip-thermal");