rockchip_thermal.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
  3. * Caesar Wang <wxt@rock-chips.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/reset.h>
  25. #include <linux/thermal.h>
  26. #include <linux/mfd/syscon.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. /**
  82. * struct rockchip_tsadc_chip - hold the private data of tsadc chip
  83. * @chn_id[SOC_MAX_SENSORS]: the sensor id of chip correspond to the channel
  84. * @chn_num: the channel number of tsadc chip
  85. * @tshut_temp: the hardware-controlled shutdown temperature value
  86. * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
  87. * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
  88. * @initialize: SoC special initialize tsadc controller method
  89. * @irq_ack: clear the interrupt
  90. * @get_temp: get the temperature
  91. * @set_alarm_temp: set the high temperature interrupt
  92. * @set_tshut_temp: set the hardware-controlled shutdown temperature
  93. * @set_tshut_mode: set the hardware-controlled shutdown mode
  94. * @table: the chip-specific conversion table
  95. */
  96. struct rockchip_tsadc_chip {
  97. /* The sensor id of chip correspond to the ADC channel */
  98. int chn_id[SOC_MAX_SENSORS];
  99. int chn_num;
  100. /* The hardware-controlled tshut property */
  101. int tshut_temp;
  102. enum tshut_mode tshut_mode;
  103. enum tshut_polarity tshut_polarity;
  104. /* Chip-wide methods */
  105. void (*initialize)(struct regmap *grf,
  106. void __iomem *reg, enum tshut_polarity p);
  107. void (*irq_ack)(void __iomem *reg);
  108. void (*control)(void __iomem *reg, bool on);
  109. /* Per-sensor methods */
  110. int (*get_temp)(const struct chip_tsadc_table *table,
  111. int chn, void __iomem *reg, int *temp);
  112. int (*set_alarm_temp)(const struct chip_tsadc_table *table,
  113. int chn, void __iomem *reg, int temp);
  114. int (*set_tshut_temp)(const struct chip_tsadc_table *table,
  115. int chn, void __iomem *reg, int temp);
  116. void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
  117. /* Per-table methods */
  118. struct chip_tsadc_table table;
  119. };
  120. /**
  121. * struct rockchip_thermal_sensor - hold the information of thermal sensor
  122. * @thermal: pointer to the platform/configuration data
  123. * @tzd: pointer to a thermal zone
  124. * @id: identifier of the thermal sensor
  125. */
  126. struct rockchip_thermal_sensor {
  127. struct rockchip_thermal_data *thermal;
  128. struct thermal_zone_device *tzd;
  129. int id;
  130. };
  131. /**
  132. * struct rockchip_thermal_data - hold the private data of thermal driver
  133. * @chip: pointer to the platform/configuration data
  134. * @pdev: platform device of thermal
  135. * @reset: the reset controller of tsadc
  136. * @sensors[SOC_MAX_SENSORS]: the thermal sensor
  137. * @clk: the controller clock is divided by the exteral 24MHz
  138. * @pclk: the advanced peripherals bus clock
  139. * @grf: the general register file will be used to do static set by software
  140. * @regs: the base address of tsadc controller
  141. * @tshut_temp: the hardware-controlled shutdown temperature value
  142. * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
  143. * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
  144. */
  145. struct rockchip_thermal_data {
  146. const struct rockchip_tsadc_chip *chip;
  147. struct platform_device *pdev;
  148. struct reset_control *reset;
  149. struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
  150. struct clk *clk;
  151. struct clk *pclk;
  152. struct regmap *grf;
  153. void __iomem *regs;
  154. int tshut_temp;
  155. enum tshut_mode tshut_mode;
  156. enum tshut_polarity tshut_polarity;
  157. };
  158. /**
  159. * TSADC Sensor Register description:
  160. *
  161. * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
  162. * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
  163. *
  164. */
  165. #define TSADCV2_USER_CON 0x00
  166. #define TSADCV2_AUTO_CON 0x04
  167. #define TSADCV2_INT_EN 0x08
  168. #define TSADCV2_INT_PD 0x0c
  169. #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
  170. #define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04)
  171. #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
  172. #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
  173. #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
  174. #define TSADCV2_AUTO_PERIOD 0x68
  175. #define TSADCV2_AUTO_PERIOD_HT 0x6c
  176. #define TSADCV2_AUTO_EN BIT(0)
  177. #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
  178. #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
  179. #define TSADCV3_AUTO_Q_SEL_EN BIT(1)
  180. #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
  181. #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
  182. #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
  183. #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
  184. #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
  185. #define TSADCV2_DATA_MASK 0xfff
  186. #define TSADCV3_DATA_MASK 0x3ff
  187. #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
  188. #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
  189. #define TSADCV2_AUTO_PERIOD_TIME 250 /* 250ms */
  190. #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* 50ms */
  191. #define TSADCV3_AUTO_PERIOD_TIME 1875 /* 2.5ms */
  192. #define TSADCV3_AUTO_PERIOD_HT_TIME 1875 /* 2.5ms */
  193. #define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */
  194. #define GRF_SARADC_TESTBIT 0x0e644
  195. #define GRF_TSADC_TESTBIT_L 0x0e648
  196. #define GRF_TSADC_TESTBIT_H 0x0e64c
  197. #define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
  198. #define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
  199. #define GRF_TSADC_VCM_EN_L (0x10001 << 7)
  200. #define GRF_TSADC_VCM_EN_H (0x10001 << 7)
  201. /**
  202. * struct tsadc_table - code to temperature conversion table
  203. * @code: the value of adc channel
  204. * @temp: the temperature
  205. * Note:
  206. * code to temperature mapping of the temperature sensor is a piece wise linear
  207. * curve.Any temperature, code faling between to 2 give temperatures can be
  208. * linearly interpolated.
  209. * Code to Temperature mapping should be updated based on manufacturer results.
  210. */
  211. struct tsadc_table {
  212. u32 code;
  213. int temp;
  214. };
  215. static const struct tsadc_table rk3228_code_table[] = {
  216. {0, -40000},
  217. {588, -40000},
  218. {593, -35000},
  219. {598, -30000},
  220. {603, -25000},
  221. {608, -20000},
  222. {613, -15000},
  223. {618, -10000},
  224. {623, -5000},
  225. {629, 0},
  226. {634, 5000},
  227. {639, 10000},
  228. {644, 15000},
  229. {649, 20000},
  230. {654, 25000},
  231. {660, 30000},
  232. {665, 35000},
  233. {670, 40000},
  234. {675, 45000},
  235. {681, 50000},
  236. {686, 55000},
  237. {691, 60000},
  238. {696, 65000},
  239. {702, 70000},
  240. {707, 75000},
  241. {712, 80000},
  242. {717, 85000},
  243. {723, 90000},
  244. {728, 95000},
  245. {733, 100000},
  246. {738, 105000},
  247. {744, 110000},
  248. {749, 115000},
  249. {754, 120000},
  250. {760, 125000},
  251. {TSADCV2_DATA_MASK, 125000},
  252. };
  253. static const struct tsadc_table rk3288_code_table[] = {
  254. {TSADCV2_DATA_MASK, -40000},
  255. {3800, -40000},
  256. {3792, -35000},
  257. {3783, -30000},
  258. {3774, -25000},
  259. {3765, -20000},
  260. {3756, -15000},
  261. {3747, -10000},
  262. {3737, -5000},
  263. {3728, 0},
  264. {3718, 5000},
  265. {3708, 10000},
  266. {3698, 15000},
  267. {3688, 20000},
  268. {3678, 25000},
  269. {3667, 30000},
  270. {3656, 35000},
  271. {3645, 40000},
  272. {3634, 45000},
  273. {3623, 50000},
  274. {3611, 55000},
  275. {3600, 60000},
  276. {3588, 65000},
  277. {3575, 70000},
  278. {3563, 75000},
  279. {3550, 80000},
  280. {3537, 85000},
  281. {3524, 90000},
  282. {3510, 95000},
  283. {3496, 100000},
  284. {3482, 105000},
  285. {3467, 110000},
  286. {3452, 115000},
  287. {3437, 120000},
  288. {3421, 125000},
  289. {0, 125000},
  290. };
  291. static const struct tsadc_table rk3368_code_table[] = {
  292. {0, -40000},
  293. {106, -40000},
  294. {108, -35000},
  295. {110, -30000},
  296. {112, -25000},
  297. {114, -20000},
  298. {116, -15000},
  299. {118, -10000},
  300. {120, -5000},
  301. {122, 0},
  302. {124, 5000},
  303. {126, 10000},
  304. {128, 15000},
  305. {130, 20000},
  306. {132, 25000},
  307. {134, 30000},
  308. {136, 35000},
  309. {138, 40000},
  310. {140, 45000},
  311. {142, 50000},
  312. {144, 55000},
  313. {146, 60000},
  314. {148, 65000},
  315. {150, 70000},
  316. {152, 75000},
  317. {154, 80000},
  318. {156, 85000},
  319. {158, 90000},
  320. {160, 95000},
  321. {162, 100000},
  322. {163, 105000},
  323. {165, 110000},
  324. {167, 115000},
  325. {169, 120000},
  326. {171, 125000},
  327. {TSADCV3_DATA_MASK, 125000},
  328. };
  329. static const struct tsadc_table rk3399_code_table[] = {
  330. {0, -40000},
  331. {402, -40000},
  332. {410, -35000},
  333. {419, -30000},
  334. {427, -25000},
  335. {436, -20000},
  336. {444, -15000},
  337. {453, -10000},
  338. {461, -5000},
  339. {470, 0},
  340. {478, 5000},
  341. {487, 10000},
  342. {496, 15000},
  343. {504, 20000},
  344. {513, 25000},
  345. {521, 30000},
  346. {530, 35000},
  347. {538, 40000},
  348. {547, 45000},
  349. {555, 50000},
  350. {564, 55000},
  351. {573, 60000},
  352. {581, 65000},
  353. {590, 70000},
  354. {599, 75000},
  355. {607, 80000},
  356. {616, 85000},
  357. {624, 90000},
  358. {633, 95000},
  359. {642, 100000},
  360. {650, 105000},
  361. {659, 110000},
  362. {668, 115000},
  363. {677, 120000},
  364. {685, 125000},
  365. {TSADCV3_DATA_MASK, 125000},
  366. };
  367. static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table,
  368. int temp)
  369. {
  370. int high, low, mid;
  371. unsigned long num;
  372. unsigned int denom;
  373. u32 error = table->data_mask;
  374. low = 0;
  375. high = (table->length - 1) - 1; /* ignore the last check for table */
  376. mid = (high + low) / 2;
  377. /* Return mask code data when the temp is over table range */
  378. if (temp < table->id[low].temp || temp > table->id[high].temp)
  379. goto exit;
  380. while (low <= high) {
  381. if (temp == table->id[mid].temp)
  382. return table->id[mid].code;
  383. else if (temp < table->id[mid].temp)
  384. high = mid - 1;
  385. else
  386. low = mid + 1;
  387. mid = (low + high) / 2;
  388. }
  389. /*
  390. * The conversion code granularity provided by the table. Let's
  391. * assume that the relationship between temperature and
  392. * analog value between 2 table entries is linear and interpolate
  393. * to produce less granular result.
  394. */
  395. num = abs(table->id[mid + 1].code - table->id[mid].code);
  396. num *= temp - table->id[mid].temp;
  397. denom = table->id[mid + 1].temp - table->id[mid].temp;
  398. switch (table->mode) {
  399. case ADC_DECREMENT:
  400. return table->id[mid].code - (num / denom);
  401. case ADC_INCREMENT:
  402. return table->id[mid].code + (num / denom);
  403. default:
  404. pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
  405. return error;
  406. }
  407. exit:
  408. pr_err("%s: invalid temperature, temp=%d error=%d\n",
  409. __func__, temp, error);
  410. return error;
  411. }
  412. static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table *table,
  413. u32 code, int *temp)
  414. {
  415. unsigned int low = 1;
  416. unsigned int high = table->length - 1;
  417. unsigned int mid = (low + high) / 2;
  418. unsigned int num;
  419. unsigned long denom;
  420. WARN_ON(table->length < 2);
  421. switch (table->mode) {
  422. case ADC_DECREMENT:
  423. code &= table->data_mask;
  424. if (code <= table->id[high].code)
  425. return -EAGAIN; /* Incorrect reading */
  426. while (low <= high) {
  427. if (code >= table->id[mid].code &&
  428. code < table->id[mid - 1].code)
  429. break;
  430. else if (code < table->id[mid].code)
  431. low = mid + 1;
  432. else
  433. high = mid - 1;
  434. mid = (low + high) / 2;
  435. }
  436. break;
  437. case ADC_INCREMENT:
  438. code &= table->data_mask;
  439. if (code < table->id[low].code)
  440. return -EAGAIN; /* Incorrect reading */
  441. while (low <= high) {
  442. if (code <= table->id[mid].code &&
  443. code > table->id[mid - 1].code)
  444. break;
  445. else if (code > table->id[mid].code)
  446. low = mid + 1;
  447. else
  448. high = mid - 1;
  449. mid = (low + high) / 2;
  450. }
  451. break;
  452. default:
  453. pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
  454. return -EINVAL;
  455. }
  456. /*
  457. * The 5C granularity provided by the table is too much. Let's
  458. * assume that the relationship between sensor readings and
  459. * temperature between 2 table entries is linear and interpolate
  460. * to produce less granular result.
  461. */
  462. num = table->id[mid].temp - table->id[mid - 1].temp;
  463. num *= abs(table->id[mid - 1].code - code);
  464. denom = abs(table->id[mid - 1].code - table->id[mid].code);
  465. *temp = table->id[mid - 1].temp + (num / denom);
  466. return 0;
  467. }
  468. /**
  469. * rk_tsadcv2_initialize - initialize TASDC Controller.
  470. *
  471. * (1) Set TSADC_V2_AUTO_PERIOD:
  472. * Configure the interleave between every two accessing of
  473. * TSADC in normal operation.
  474. *
  475. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  476. * Configure the interleave between every two accessing of
  477. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  478. *
  479. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  480. * If the temperature is higher than COMP_INT or COMP_SHUT for
  481. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  482. */
  483. static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
  484. enum tshut_polarity tshut_polarity)
  485. {
  486. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  487. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  488. regs + TSADCV2_AUTO_CON);
  489. else
  490. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  491. regs + TSADCV2_AUTO_CON);
  492. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
  493. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  494. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  495. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  496. regs + TSADCV2_AUTO_PERIOD_HT);
  497. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  498. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  499. }
  500. /**
  501. * rk_tsadcv3_initialize - initialize TASDC Controller.
  502. *
  503. * (1) The tsadc control power sequence.
  504. *
  505. * (2) Set TSADC_V2_AUTO_PERIOD:
  506. * Configure the interleave between every two accessing of
  507. * TSADC in normal operation.
  508. *
  509. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  510. * Configure the interleave between every two accessing of
  511. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  512. *
  513. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  514. * If the temperature is higher than COMP_INT or COMP_SHUT for
  515. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  516. */
  517. static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
  518. enum tshut_polarity tshut_polarity)
  519. {
  520. /* The tsadc control power sequence */
  521. if (IS_ERR(grf)) {
  522. /* Set interleave value to workround ic time sync issue */
  523. writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
  524. TSADCV2_USER_CON);
  525. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
  526. regs + TSADCV2_AUTO_PERIOD);
  527. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  528. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  529. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  530. regs + TSADCV2_AUTO_PERIOD_HT);
  531. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  532. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  533. } else {
  534. /* Enable the voltage common mode feature */
  535. regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_VCM_EN_L);
  536. regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_VCM_EN_H);
  537. usleep_range(15, 100); /* The spec note says at least 15 us */
  538. regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
  539. regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
  540. usleep_range(90, 200); /* The spec note says at least 90 us */
  541. writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
  542. regs + TSADCV2_AUTO_PERIOD);
  543. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  544. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  545. writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
  546. regs + TSADCV2_AUTO_PERIOD_HT);
  547. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  548. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  549. }
  550. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  551. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  552. regs + TSADCV2_AUTO_CON);
  553. else
  554. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  555. regs + TSADCV2_AUTO_CON);
  556. }
  557. static void rk_tsadcv2_irq_ack(void __iomem *regs)
  558. {
  559. u32 val;
  560. val = readl_relaxed(regs + TSADCV2_INT_PD);
  561. writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  562. }
  563. static void rk_tsadcv3_irq_ack(void __iomem *regs)
  564. {
  565. u32 val;
  566. val = readl_relaxed(regs + TSADCV2_INT_PD);
  567. writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  568. }
  569. static void rk_tsadcv2_control(void __iomem *regs, bool enable)
  570. {
  571. u32 val;
  572. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  573. if (enable)
  574. val |= TSADCV2_AUTO_EN;
  575. else
  576. val &= ~TSADCV2_AUTO_EN;
  577. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  578. }
  579. /**
  580. * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
  581. *
  582. * NOTE: TSADC controller works at auto mode, and some SoCs need set the
  583. * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
  584. * adc value if setting this bit to enable.
  585. */
  586. static void rk_tsadcv3_control(void __iomem *regs, bool enable)
  587. {
  588. u32 val;
  589. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  590. if (enable)
  591. val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
  592. else
  593. val &= ~TSADCV2_AUTO_EN;
  594. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  595. }
  596. static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table,
  597. int chn, void __iomem *regs, int *temp)
  598. {
  599. u32 val;
  600. val = readl_relaxed(regs + TSADCV2_DATA(chn));
  601. return rk_tsadcv2_code_to_temp(table, val, temp);
  602. }
  603. static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
  604. int chn, void __iomem *regs, int temp)
  605. {
  606. u32 alarm_value;
  607. u32 int_en, int_clr;
  608. /*
  609. * In some cases, some sensors didn't need the trip points, the
  610. * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm
  611. * in the end, ignore this case and disable the high temperature
  612. * interrupt.
  613. */
  614. if (temp == INT_MAX) {
  615. int_clr = readl_relaxed(regs + TSADCV2_INT_EN);
  616. int_clr &= ~TSADCV2_INT_SRC_EN(chn);
  617. writel_relaxed(int_clr, regs + TSADCV2_INT_EN);
  618. return 0;
  619. }
  620. /* Make sure the value is valid */
  621. alarm_value = rk_tsadcv2_temp_to_code(table, temp);
  622. if (alarm_value == table->data_mask)
  623. return -ERANGE;
  624. writel_relaxed(alarm_value & table->data_mask,
  625. regs + TSADCV2_COMP_INT(chn));
  626. int_en = readl_relaxed(regs + TSADCV2_INT_EN);
  627. int_en |= TSADCV2_INT_SRC_EN(chn);
  628. writel_relaxed(int_en, regs + TSADCV2_INT_EN);
  629. return 0;
  630. }
  631. static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table,
  632. int chn, void __iomem *regs, int temp)
  633. {
  634. u32 tshut_value, val;
  635. /* Make sure the value is valid */
  636. tshut_value = rk_tsadcv2_temp_to_code(table, temp);
  637. if (tshut_value == table->data_mask)
  638. return -ERANGE;
  639. writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
  640. /* TSHUT will be valid */
  641. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  642. writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
  643. return 0;
  644. }
  645. static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
  646. enum tshut_mode mode)
  647. {
  648. u32 val;
  649. val = readl_relaxed(regs + TSADCV2_INT_EN);
  650. if (mode == TSHUT_MODE_GPIO) {
  651. val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
  652. val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  653. } else {
  654. val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  655. val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
  656. }
  657. writel_relaxed(val, regs + TSADCV2_INT_EN);
  658. }
  659. static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
  660. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  661. .chn_num = 1, /* one channel for tsadc */
  662. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  663. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  664. .tshut_temp = 95000,
  665. .initialize = rk_tsadcv2_initialize,
  666. .irq_ack = rk_tsadcv3_irq_ack,
  667. .control = rk_tsadcv3_control,
  668. .get_temp = rk_tsadcv2_get_temp,
  669. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  670. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  671. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  672. .table = {
  673. .id = rk3228_code_table,
  674. .length = ARRAY_SIZE(rk3228_code_table),
  675. .data_mask = TSADCV3_DATA_MASK,
  676. .mode = ADC_INCREMENT,
  677. },
  678. };
  679. static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
  680. .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
  681. .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
  682. .chn_num = 2, /* two channels for tsadc */
  683. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  684. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  685. .tshut_temp = 95000,
  686. .initialize = rk_tsadcv2_initialize,
  687. .irq_ack = rk_tsadcv2_irq_ack,
  688. .control = rk_tsadcv2_control,
  689. .get_temp = rk_tsadcv2_get_temp,
  690. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  691. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  692. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  693. .table = {
  694. .id = rk3288_code_table,
  695. .length = ARRAY_SIZE(rk3288_code_table),
  696. .data_mask = TSADCV2_DATA_MASK,
  697. .mode = ADC_DECREMENT,
  698. },
  699. };
  700. static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
  701. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  702. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  703. .chn_num = 2, /* two channels for tsadc */
  704. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  705. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  706. .tshut_temp = 95000,
  707. .initialize = rk_tsadcv3_initialize,
  708. .irq_ack = rk_tsadcv3_irq_ack,
  709. .control = rk_tsadcv3_control,
  710. .get_temp = rk_tsadcv2_get_temp,
  711. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  712. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  713. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  714. .table = {
  715. .id = rk3228_code_table,
  716. .length = ARRAY_SIZE(rk3228_code_table),
  717. .data_mask = TSADCV3_DATA_MASK,
  718. .mode = ADC_INCREMENT,
  719. },
  720. };
  721. static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
  722. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  723. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  724. .chn_num = 2, /* two channels for tsadc */
  725. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  726. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  727. .tshut_temp = 95000,
  728. .initialize = rk_tsadcv2_initialize,
  729. .irq_ack = rk_tsadcv2_irq_ack,
  730. .control = rk_tsadcv2_control,
  731. .get_temp = rk_tsadcv2_get_temp,
  732. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  733. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  734. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  735. .table = {
  736. .id = rk3368_code_table,
  737. .length = ARRAY_SIZE(rk3368_code_table),
  738. .data_mask = TSADCV3_DATA_MASK,
  739. .mode = ADC_INCREMENT,
  740. },
  741. };
  742. static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
  743. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  744. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  745. .chn_num = 2, /* two channels for tsadc */
  746. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  747. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  748. .tshut_temp = 95000,
  749. .initialize = rk_tsadcv3_initialize,
  750. .irq_ack = rk_tsadcv3_irq_ack,
  751. .control = rk_tsadcv3_control,
  752. .get_temp = rk_tsadcv2_get_temp,
  753. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  754. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  755. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  756. .table = {
  757. .id = rk3399_code_table,
  758. .length = ARRAY_SIZE(rk3399_code_table),
  759. .data_mask = TSADCV3_DATA_MASK,
  760. .mode = ADC_INCREMENT,
  761. },
  762. };
  763. static const struct of_device_id of_rockchip_thermal_match[] = {
  764. {
  765. .compatible = "rockchip,rk3228-tsadc",
  766. .data = (void *)&rk3228_tsadc_data,
  767. },
  768. {
  769. .compatible = "rockchip,rk3288-tsadc",
  770. .data = (void *)&rk3288_tsadc_data,
  771. },
  772. {
  773. .compatible = "rockchip,rk3366-tsadc",
  774. .data = (void *)&rk3366_tsadc_data,
  775. },
  776. {
  777. .compatible = "rockchip,rk3368-tsadc",
  778. .data = (void *)&rk3368_tsadc_data,
  779. },
  780. {
  781. .compatible = "rockchip,rk3399-tsadc",
  782. .data = (void *)&rk3399_tsadc_data,
  783. },
  784. { /* end */ },
  785. };
  786. MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
  787. static void
  788. rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
  789. {
  790. struct thermal_zone_device *tzd = sensor->tzd;
  791. tzd->ops->set_mode(tzd,
  792. on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
  793. }
  794. static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
  795. {
  796. struct rockchip_thermal_data *thermal = dev;
  797. int i;
  798. dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
  799. thermal->chip->irq_ack(thermal->regs);
  800. for (i = 0; i < thermal->chip->chn_num; i++)
  801. thermal_zone_device_update(thermal->sensors[i].tzd,
  802. THERMAL_EVENT_UNSPECIFIED);
  803. return IRQ_HANDLED;
  804. }
  805. static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
  806. {
  807. struct rockchip_thermal_sensor *sensor = _sensor;
  808. struct rockchip_thermal_data *thermal = sensor->thermal;
  809. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  810. dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
  811. __func__, sensor->id, low, high);
  812. return tsadc->set_alarm_temp(&tsadc->table,
  813. sensor->id, thermal->regs, high);
  814. }
  815. static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
  816. {
  817. struct rockchip_thermal_sensor *sensor = _sensor;
  818. struct rockchip_thermal_data *thermal = sensor->thermal;
  819. const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
  820. int retval;
  821. retval = tsadc->get_temp(&tsadc->table,
  822. sensor->id, thermal->regs, out_temp);
  823. dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
  824. sensor->id, *out_temp, retval);
  825. return retval;
  826. }
  827. static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
  828. .get_temp = rockchip_thermal_get_temp,
  829. .set_trips = rockchip_thermal_set_trips,
  830. };
  831. static int rockchip_configure_from_dt(struct device *dev,
  832. struct device_node *np,
  833. struct rockchip_thermal_data *thermal)
  834. {
  835. u32 shut_temp, tshut_mode, tshut_polarity;
  836. if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
  837. dev_warn(dev,
  838. "Missing tshut temp property, using default %d\n",
  839. thermal->chip->tshut_temp);
  840. thermal->tshut_temp = thermal->chip->tshut_temp;
  841. } else {
  842. if (shut_temp > INT_MAX) {
  843. dev_err(dev, "Invalid tshut temperature specified: %d\n",
  844. shut_temp);
  845. return -ERANGE;
  846. }
  847. thermal->tshut_temp = shut_temp;
  848. }
  849. if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
  850. dev_warn(dev,
  851. "Missing tshut mode property, using default (%s)\n",
  852. thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
  853. "gpio" : "cru");
  854. thermal->tshut_mode = thermal->chip->tshut_mode;
  855. } else {
  856. thermal->tshut_mode = tshut_mode;
  857. }
  858. if (thermal->tshut_mode > 1) {
  859. dev_err(dev, "Invalid tshut mode specified: %d\n",
  860. thermal->tshut_mode);
  861. return -EINVAL;
  862. }
  863. if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
  864. &tshut_polarity)) {
  865. dev_warn(dev,
  866. "Missing tshut-polarity property, using default (%s)\n",
  867. thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
  868. "low" : "high");
  869. thermal->tshut_polarity = thermal->chip->tshut_polarity;
  870. } else {
  871. thermal->tshut_polarity = tshut_polarity;
  872. }
  873. if (thermal->tshut_polarity > 1) {
  874. dev_err(dev, "Invalid tshut-polarity specified: %d\n",
  875. thermal->tshut_polarity);
  876. return -EINVAL;
  877. }
  878. /* The tsadc wont to handle the error in here since some SoCs didn't
  879. * need this property.
  880. */
  881. thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  882. if (IS_ERR(thermal->grf))
  883. dev_warn(dev, "Missing rockchip,grf property\n");
  884. return 0;
  885. }
  886. static int
  887. rockchip_thermal_register_sensor(struct platform_device *pdev,
  888. struct rockchip_thermal_data *thermal,
  889. struct rockchip_thermal_sensor *sensor,
  890. int id)
  891. {
  892. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  893. int error;
  894. tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
  895. error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs,
  896. thermal->tshut_temp);
  897. if (error)
  898. dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
  899. __func__, thermal->tshut_temp, error);
  900. sensor->thermal = thermal;
  901. sensor->id = id;
  902. sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
  903. sensor, &rockchip_of_thermal_ops);
  904. if (IS_ERR(sensor->tzd)) {
  905. error = PTR_ERR(sensor->tzd);
  906. dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
  907. id, error);
  908. return error;
  909. }
  910. return 0;
  911. }
  912. /**
  913. * Reset TSADC Controller, reset all tsadc registers.
  914. */
  915. static void rockchip_thermal_reset_controller(struct reset_control *reset)
  916. {
  917. reset_control_assert(reset);
  918. usleep_range(10, 20);
  919. reset_control_deassert(reset);
  920. }
  921. static int rockchip_thermal_probe(struct platform_device *pdev)
  922. {
  923. struct device_node *np = pdev->dev.of_node;
  924. struct rockchip_thermal_data *thermal;
  925. const struct of_device_id *match;
  926. struct resource *res;
  927. int irq;
  928. int i;
  929. int error;
  930. match = of_match_node(of_rockchip_thermal_match, np);
  931. if (!match)
  932. return -ENXIO;
  933. irq = platform_get_irq(pdev, 0);
  934. if (irq < 0) {
  935. dev_err(&pdev->dev, "no irq resource?\n");
  936. return -EINVAL;
  937. }
  938. thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
  939. GFP_KERNEL);
  940. if (!thermal)
  941. return -ENOMEM;
  942. thermal->pdev = pdev;
  943. thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
  944. if (!thermal->chip)
  945. return -EINVAL;
  946. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  947. thermal->regs = devm_ioremap_resource(&pdev->dev, res);
  948. if (IS_ERR(thermal->regs))
  949. return PTR_ERR(thermal->regs);
  950. thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
  951. if (IS_ERR(thermal->reset)) {
  952. error = PTR_ERR(thermal->reset);
  953. dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
  954. return error;
  955. }
  956. thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
  957. if (IS_ERR(thermal->clk)) {
  958. error = PTR_ERR(thermal->clk);
  959. dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
  960. return error;
  961. }
  962. thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  963. if (IS_ERR(thermal->pclk)) {
  964. error = PTR_ERR(thermal->pclk);
  965. dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
  966. error);
  967. return error;
  968. }
  969. error = clk_prepare_enable(thermal->clk);
  970. if (error) {
  971. dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
  972. error);
  973. return error;
  974. }
  975. error = clk_prepare_enable(thermal->pclk);
  976. if (error) {
  977. dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
  978. goto err_disable_clk;
  979. }
  980. rockchip_thermal_reset_controller(thermal->reset);
  981. error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
  982. if (error) {
  983. dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
  984. error);
  985. goto err_disable_pclk;
  986. }
  987. thermal->chip->initialize(thermal->grf, thermal->regs,
  988. thermal->tshut_polarity);
  989. for (i = 0; i < thermal->chip->chn_num; i++) {
  990. error = rockchip_thermal_register_sensor(pdev, thermal,
  991. &thermal->sensors[i],
  992. thermal->chip->chn_id[i]);
  993. if (error) {
  994. dev_err(&pdev->dev,
  995. "failed to register sensor[%d] : error = %d\n",
  996. i, error);
  997. goto err_disable_pclk;
  998. }
  999. }
  1000. error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  1001. &rockchip_thermal_alarm_irq_thread,
  1002. IRQF_ONESHOT,
  1003. "rockchip_thermal", thermal);
  1004. if (error) {
  1005. dev_err(&pdev->dev,
  1006. "failed to request tsadc irq: %d\n", error);
  1007. goto err_disable_pclk;
  1008. }
  1009. thermal->chip->control(thermal->regs, true);
  1010. for (i = 0; i < thermal->chip->chn_num; i++)
  1011. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  1012. platform_set_drvdata(pdev, thermal);
  1013. return 0;
  1014. err_disable_pclk:
  1015. clk_disable_unprepare(thermal->pclk);
  1016. err_disable_clk:
  1017. clk_disable_unprepare(thermal->clk);
  1018. return error;
  1019. }
  1020. static int rockchip_thermal_remove(struct platform_device *pdev)
  1021. {
  1022. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  1023. int i;
  1024. for (i = 0; i < thermal->chip->chn_num; i++) {
  1025. struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
  1026. rockchip_thermal_toggle_sensor(sensor, false);
  1027. }
  1028. thermal->chip->control(thermal->regs, false);
  1029. clk_disable_unprepare(thermal->pclk);
  1030. clk_disable_unprepare(thermal->clk);
  1031. return 0;
  1032. }
  1033. static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
  1034. {
  1035. struct platform_device *pdev = to_platform_device(dev);
  1036. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  1037. int i;
  1038. for (i = 0; i < thermal->chip->chn_num; i++)
  1039. rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
  1040. thermal->chip->control(thermal->regs, false);
  1041. clk_disable(thermal->pclk);
  1042. clk_disable(thermal->clk);
  1043. pinctrl_pm_select_sleep_state(dev);
  1044. return 0;
  1045. }
  1046. static int __maybe_unused rockchip_thermal_resume(struct device *dev)
  1047. {
  1048. struct platform_device *pdev = to_platform_device(dev);
  1049. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  1050. int i;
  1051. int error;
  1052. error = clk_enable(thermal->clk);
  1053. if (error)
  1054. return error;
  1055. error = clk_enable(thermal->pclk);
  1056. if (error) {
  1057. clk_disable(thermal->clk);
  1058. return error;
  1059. }
  1060. rockchip_thermal_reset_controller(thermal->reset);
  1061. thermal->chip->initialize(thermal->grf, thermal->regs,
  1062. thermal->tshut_polarity);
  1063. for (i = 0; i < thermal->chip->chn_num; i++) {
  1064. int id = thermal->sensors[i].id;
  1065. thermal->chip->set_tshut_mode(id, thermal->regs,
  1066. thermal->tshut_mode);
  1067. error = thermal->chip->set_tshut_temp(&thermal->chip->table,
  1068. id, thermal->regs,
  1069. thermal->tshut_temp);
  1070. if (error)
  1071. dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
  1072. __func__, thermal->tshut_temp, error);
  1073. }
  1074. thermal->chip->control(thermal->regs, true);
  1075. for (i = 0; i < thermal->chip->chn_num; i++)
  1076. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  1077. pinctrl_pm_select_default_state(dev);
  1078. return 0;
  1079. }
  1080. static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
  1081. rockchip_thermal_suspend, rockchip_thermal_resume);
  1082. static struct platform_driver rockchip_thermal_driver = {
  1083. .driver = {
  1084. .name = "rockchip-thermal",
  1085. .pm = &rockchip_thermal_pm_ops,
  1086. .of_match_table = of_rockchip_thermal_match,
  1087. },
  1088. .probe = rockchip_thermal_probe,
  1089. .remove = rockchip_thermal_remove,
  1090. };
  1091. module_platform_driver(rockchip_thermal_driver);
  1092. MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
  1093. MODULE_AUTHOR("Rockchip, Inc.");
  1094. MODULE_LICENSE("GPL v2");
  1095. MODULE_ALIAS("platform:rockchip-thermal");