rockchip_thermal.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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)(struct chip_tsadc_table table,
  111. int chn, void __iomem *reg, int *temp);
  112. void (*set_alarm_temp)(struct chip_tsadc_table table,
  113. int chn, void __iomem *reg, int temp);
  114. void (*set_tshut_temp)(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. };
  290. static const struct tsadc_table rk3368_code_table[] = {
  291. {0, -40000},
  292. {106, -40000},
  293. {108, -35000},
  294. {110, -30000},
  295. {112, -25000},
  296. {114, -20000},
  297. {116, -15000},
  298. {118, -10000},
  299. {120, -5000},
  300. {122, 0},
  301. {124, 5000},
  302. {126, 10000},
  303. {128, 15000},
  304. {130, 20000},
  305. {132, 25000},
  306. {134, 30000},
  307. {136, 35000},
  308. {138, 40000},
  309. {140, 45000},
  310. {142, 50000},
  311. {144, 55000},
  312. {146, 60000},
  313. {148, 65000},
  314. {150, 70000},
  315. {152, 75000},
  316. {154, 80000},
  317. {156, 85000},
  318. {158, 90000},
  319. {160, 95000},
  320. {162, 100000},
  321. {163, 105000},
  322. {165, 110000},
  323. {167, 115000},
  324. {169, 120000},
  325. {171, 125000},
  326. {TSADCV3_DATA_MASK, 125000},
  327. };
  328. static const struct tsadc_table rk3399_code_table[] = {
  329. {0, -40000},
  330. {402, -40000},
  331. {410, -35000},
  332. {419, -30000},
  333. {427, -25000},
  334. {436, -20000},
  335. {444, -15000},
  336. {453, -10000},
  337. {461, -5000},
  338. {470, 0},
  339. {478, 5000},
  340. {487, 10000},
  341. {496, 15000},
  342. {504, 20000},
  343. {513, 25000},
  344. {521, 30000},
  345. {530, 35000},
  346. {538, 40000},
  347. {547, 45000},
  348. {555, 50000},
  349. {564, 55000},
  350. {573, 60000},
  351. {581, 65000},
  352. {590, 70000},
  353. {599, 75000},
  354. {607, 80000},
  355. {616, 85000},
  356. {624, 90000},
  357. {633, 95000},
  358. {642, 100000},
  359. {650, 105000},
  360. {659, 110000},
  361. {668, 115000},
  362. {677, 120000},
  363. {685, 125000},
  364. {TSADCV3_DATA_MASK, 125000},
  365. };
  366. static u32 rk_tsadcv2_temp_to_code(struct chip_tsadc_table table,
  367. int temp)
  368. {
  369. int high, low, mid;
  370. u32 error = 0;
  371. low = 0;
  372. high = table.length - 1;
  373. mid = (high + low) / 2;
  374. /* Return mask code data when the temp is over table range */
  375. if (temp < table.id[low].temp || temp > table.id[high].temp) {
  376. error = table.data_mask;
  377. goto exit;
  378. }
  379. while (low <= high) {
  380. if (temp == table.id[mid].temp)
  381. return table.id[mid].code;
  382. else if (temp < table.id[mid].temp)
  383. high = mid - 1;
  384. else
  385. low = mid + 1;
  386. mid = (low + high) / 2;
  387. }
  388. exit:
  389. pr_err("Invalid the conversion, error=%d\n", error);
  390. return error;
  391. }
  392. static int rk_tsadcv2_code_to_temp(struct chip_tsadc_table table, u32 code,
  393. int *temp)
  394. {
  395. unsigned int low = 1;
  396. unsigned int high = table.length - 1;
  397. unsigned int mid = (low + high) / 2;
  398. unsigned int num;
  399. unsigned long denom;
  400. WARN_ON(table.length < 2);
  401. switch (table.mode) {
  402. case ADC_DECREMENT:
  403. code &= table.data_mask;
  404. if (code < table.id[high].code)
  405. return -EAGAIN; /* Incorrect reading */
  406. while (low <= high) {
  407. if (code >= table.id[mid].code &&
  408. code < table.id[mid - 1].code)
  409. break;
  410. else if (code < table.id[mid].code)
  411. low = mid + 1;
  412. else
  413. high = mid - 1;
  414. mid = (low + high) / 2;
  415. }
  416. break;
  417. case ADC_INCREMENT:
  418. code &= table.data_mask;
  419. if (code < table.id[low].code)
  420. return -EAGAIN; /* Incorrect reading */
  421. while (low <= high) {
  422. if (code <= table.id[mid].code &&
  423. code > table.id[mid - 1].code)
  424. break;
  425. else if (code > table.id[mid].code)
  426. low = mid + 1;
  427. else
  428. high = mid - 1;
  429. mid = (low + high) / 2;
  430. }
  431. break;
  432. default:
  433. pr_err("Invalid the conversion table\n");
  434. }
  435. /*
  436. * The 5C granularity provided by the table is too much. Let's
  437. * assume that the relationship between sensor readings and
  438. * temperature between 2 table entries is linear and interpolate
  439. * to produce less granular result.
  440. */
  441. num = table.id[mid].temp - table.id[mid - 1].temp;
  442. num *= abs(table.id[mid - 1].code - code);
  443. denom = abs(table.id[mid - 1].code - table.id[mid].code);
  444. *temp = table.id[mid - 1].temp + (num / denom);
  445. return 0;
  446. }
  447. /**
  448. * rk_tsadcv2_initialize - initialize TASDC Controller.
  449. *
  450. * (1) Set TSADC_V2_AUTO_PERIOD:
  451. * Configure the interleave between every two accessing of
  452. * TSADC in normal operation.
  453. *
  454. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  455. * Configure the interleave between every two accessing of
  456. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  457. *
  458. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  459. * If the temperature is higher than COMP_INT or COMP_SHUT for
  460. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  461. */
  462. static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
  463. enum tshut_polarity tshut_polarity)
  464. {
  465. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  466. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  467. regs + TSADCV2_AUTO_CON);
  468. else
  469. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  470. regs + TSADCV2_AUTO_CON);
  471. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
  472. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  473. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  474. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  475. regs + TSADCV2_AUTO_PERIOD_HT);
  476. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  477. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  478. if (IS_ERR(grf)) {
  479. pr_warn("%s: Missing rockchip,grf property\n", __func__);
  480. return;
  481. }
  482. }
  483. /**
  484. * rk_tsadcv3_initialize - initialize TASDC Controller.
  485. *
  486. * (1) The tsadc control power sequence.
  487. *
  488. * (2) Set TSADC_V2_AUTO_PERIOD:
  489. * Configure the interleave between every two accessing of
  490. * TSADC in normal operation.
  491. *
  492. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  493. * Configure the interleave between every two accessing of
  494. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  495. *
  496. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  497. * If the temperature is higher than COMP_INT or COMP_SHUT for
  498. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  499. */
  500. static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
  501. enum tshut_polarity tshut_polarity)
  502. {
  503. /* The tsadc control power sequence */
  504. if (IS_ERR(grf)) {
  505. /* Set interleave value to workround ic time sync issue */
  506. writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
  507. TSADCV2_USER_CON);
  508. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
  509. regs + TSADCV2_AUTO_PERIOD);
  510. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  511. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  512. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  513. regs + TSADCV2_AUTO_PERIOD_HT);
  514. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  515. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  516. } else {
  517. /* Enable the voltage common mode feature */
  518. regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_VCM_EN_L);
  519. regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_VCM_EN_H);
  520. usleep_range(15, 100); /* The spec note says at least 15 us */
  521. regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
  522. regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
  523. usleep_range(90, 200); /* The spec note says at least 90 us */
  524. writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
  525. regs + TSADCV2_AUTO_PERIOD);
  526. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  527. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  528. writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
  529. regs + TSADCV2_AUTO_PERIOD_HT);
  530. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  531. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  532. }
  533. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  534. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  535. regs + TSADCV2_AUTO_CON);
  536. else
  537. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  538. regs + TSADCV2_AUTO_CON);
  539. }
  540. static void rk_tsadcv2_irq_ack(void __iomem *regs)
  541. {
  542. u32 val;
  543. val = readl_relaxed(regs + TSADCV2_INT_PD);
  544. writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  545. }
  546. static void rk_tsadcv3_irq_ack(void __iomem *regs)
  547. {
  548. u32 val;
  549. val = readl_relaxed(regs + TSADCV2_INT_PD);
  550. writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  551. }
  552. static void rk_tsadcv2_control(void __iomem *regs, bool enable)
  553. {
  554. u32 val;
  555. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  556. if (enable)
  557. val |= TSADCV2_AUTO_EN;
  558. else
  559. val &= ~TSADCV2_AUTO_EN;
  560. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  561. }
  562. /**
  563. * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
  564. *
  565. * NOTE: TSADC controller works at auto mode, and some SoCs need set the
  566. * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
  567. * adc value if setting this bit to enable.
  568. */
  569. static void rk_tsadcv3_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 | TSADCV3_AUTO_Q_SEL_EN;
  575. else
  576. val &= ~TSADCV2_AUTO_EN;
  577. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  578. }
  579. static int rk_tsadcv2_get_temp(struct chip_tsadc_table table,
  580. int chn, void __iomem *regs, int *temp)
  581. {
  582. u32 val;
  583. val = readl_relaxed(regs + TSADCV2_DATA(chn));
  584. return rk_tsadcv2_code_to_temp(table, val, temp);
  585. }
  586. static void rk_tsadcv2_alarm_temp(struct chip_tsadc_table table,
  587. int chn, void __iomem *regs, int temp)
  588. {
  589. u32 alarm_value, int_en;
  590. /* Make sure the value is valid */
  591. alarm_value = rk_tsadcv2_temp_to_code(table, temp);
  592. if (alarm_value == table.data_mask)
  593. return;
  594. writel_relaxed(alarm_value & table.data_mask,
  595. regs + TSADCV2_COMP_INT(chn));
  596. int_en = readl_relaxed(regs + TSADCV2_INT_EN);
  597. int_en |= TSADCV2_INT_SRC_EN(chn);
  598. writel_relaxed(int_en, regs + TSADCV2_INT_EN);
  599. }
  600. static void rk_tsadcv2_tshut_temp(struct chip_tsadc_table table,
  601. int chn, void __iomem *regs, int temp)
  602. {
  603. u32 tshut_value, val;
  604. /* Make sure the value is valid */
  605. tshut_value = rk_tsadcv2_temp_to_code(table, temp);
  606. if (tshut_value == table.data_mask)
  607. return;
  608. writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
  609. /* TSHUT will be valid */
  610. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  611. writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
  612. }
  613. static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
  614. enum tshut_mode mode)
  615. {
  616. u32 val;
  617. val = readl_relaxed(regs + TSADCV2_INT_EN);
  618. if (mode == TSHUT_MODE_GPIO) {
  619. val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
  620. val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  621. } else {
  622. val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  623. val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
  624. }
  625. writel_relaxed(val, regs + TSADCV2_INT_EN);
  626. }
  627. static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
  628. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  629. .chn_num = 1, /* one channel for tsadc */
  630. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  631. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  632. .tshut_temp = 95000,
  633. .initialize = rk_tsadcv2_initialize,
  634. .irq_ack = rk_tsadcv3_irq_ack,
  635. .control = rk_tsadcv3_control,
  636. .get_temp = rk_tsadcv2_get_temp,
  637. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  638. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  639. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  640. .table = {
  641. .id = rk3228_code_table,
  642. .length = ARRAY_SIZE(rk3228_code_table),
  643. .data_mask = TSADCV3_DATA_MASK,
  644. .mode = ADC_INCREMENT,
  645. },
  646. };
  647. static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
  648. .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
  649. .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
  650. .chn_num = 2, /* two channels for tsadc */
  651. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  652. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  653. .tshut_temp = 95000,
  654. .initialize = rk_tsadcv2_initialize,
  655. .irq_ack = rk_tsadcv2_irq_ack,
  656. .control = rk_tsadcv2_control,
  657. .get_temp = rk_tsadcv2_get_temp,
  658. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  659. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  660. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  661. .table = {
  662. .id = rk3288_code_table,
  663. .length = ARRAY_SIZE(rk3288_code_table),
  664. .data_mask = TSADCV2_DATA_MASK,
  665. .mode = ADC_DECREMENT,
  666. },
  667. };
  668. static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
  669. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  670. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  671. .chn_num = 2, /* two channels for tsadc */
  672. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  673. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  674. .tshut_temp = 95000,
  675. .initialize = rk_tsadcv3_initialize,
  676. .irq_ack = rk_tsadcv3_irq_ack,
  677. .control = rk_tsadcv3_control,
  678. .get_temp = rk_tsadcv2_get_temp,
  679. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  680. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  681. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  682. .table = {
  683. .id = rk3228_code_table,
  684. .length = ARRAY_SIZE(rk3228_code_table),
  685. .data_mask = TSADCV3_DATA_MASK,
  686. .mode = ADC_INCREMENT,
  687. },
  688. };
  689. static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
  690. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  691. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  692. .chn_num = 2, /* two channels for tsadc */
  693. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  694. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  695. .tshut_temp = 95000,
  696. .initialize = rk_tsadcv2_initialize,
  697. .irq_ack = rk_tsadcv2_irq_ack,
  698. .control = rk_tsadcv2_control,
  699. .get_temp = rk_tsadcv2_get_temp,
  700. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  701. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  702. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  703. .table = {
  704. .id = rk3368_code_table,
  705. .length = ARRAY_SIZE(rk3368_code_table),
  706. .data_mask = TSADCV3_DATA_MASK,
  707. .mode = ADC_INCREMENT,
  708. },
  709. };
  710. static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
  711. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  712. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  713. .chn_num = 2, /* two channels for tsadc */
  714. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  715. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  716. .tshut_temp = 95000,
  717. .initialize = rk_tsadcv3_initialize,
  718. .irq_ack = rk_tsadcv3_irq_ack,
  719. .control = rk_tsadcv3_control,
  720. .get_temp = rk_tsadcv2_get_temp,
  721. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  722. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  723. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  724. .table = {
  725. .id = rk3399_code_table,
  726. .length = ARRAY_SIZE(rk3399_code_table),
  727. .data_mask = TSADCV3_DATA_MASK,
  728. .mode = ADC_INCREMENT,
  729. },
  730. };
  731. static const struct of_device_id of_rockchip_thermal_match[] = {
  732. {
  733. .compatible = "rockchip,rk3228-tsadc",
  734. .data = (void *)&rk3228_tsadc_data,
  735. },
  736. {
  737. .compatible = "rockchip,rk3288-tsadc",
  738. .data = (void *)&rk3288_tsadc_data,
  739. },
  740. {
  741. .compatible = "rockchip,rk3366-tsadc",
  742. .data = (void *)&rk3366_tsadc_data,
  743. },
  744. {
  745. .compatible = "rockchip,rk3368-tsadc",
  746. .data = (void *)&rk3368_tsadc_data,
  747. },
  748. {
  749. .compatible = "rockchip,rk3399-tsadc",
  750. .data = (void *)&rk3399_tsadc_data,
  751. },
  752. { /* end */ },
  753. };
  754. MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
  755. static void
  756. rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
  757. {
  758. struct thermal_zone_device *tzd = sensor->tzd;
  759. tzd->ops->set_mode(tzd,
  760. on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
  761. }
  762. static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
  763. {
  764. struct rockchip_thermal_data *thermal = dev;
  765. int i;
  766. dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
  767. thermal->chip->irq_ack(thermal->regs);
  768. for (i = 0; i < thermal->chip->chn_num; i++)
  769. thermal_zone_device_update(thermal->sensors[i].tzd,
  770. THERMAL_EVENT_UNSPECIFIED);
  771. return IRQ_HANDLED;
  772. }
  773. static int rockchip_thermal_set_trips(void *_sensor, int low, int high)
  774. {
  775. struct rockchip_thermal_sensor *sensor = _sensor;
  776. struct rockchip_thermal_data *thermal = sensor->thermal;
  777. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  778. dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
  779. __func__, sensor->id, low, high);
  780. tsadc->set_alarm_temp(tsadc->table,
  781. sensor->id, thermal->regs, high);
  782. return 0;
  783. }
  784. static int rockchip_thermal_get_temp(void *_sensor, int *out_temp)
  785. {
  786. struct rockchip_thermal_sensor *sensor = _sensor;
  787. struct rockchip_thermal_data *thermal = sensor->thermal;
  788. const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
  789. int retval;
  790. retval = tsadc->get_temp(tsadc->table,
  791. sensor->id, thermal->regs, out_temp);
  792. dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
  793. sensor->id, *out_temp, retval);
  794. return retval;
  795. }
  796. static const struct thermal_zone_of_device_ops rockchip_of_thermal_ops = {
  797. .get_temp = rockchip_thermal_get_temp,
  798. .set_trips = rockchip_thermal_set_trips,
  799. };
  800. static int rockchip_configure_from_dt(struct device *dev,
  801. struct device_node *np,
  802. struct rockchip_thermal_data *thermal)
  803. {
  804. u32 shut_temp, tshut_mode, tshut_polarity;
  805. if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
  806. dev_warn(dev,
  807. "Missing tshut temp property, using default %d\n",
  808. thermal->chip->tshut_temp);
  809. thermal->tshut_temp = thermal->chip->tshut_temp;
  810. } else {
  811. if (shut_temp > INT_MAX) {
  812. dev_err(dev, "Invalid tshut temperature specified: %d\n",
  813. shut_temp);
  814. return -ERANGE;
  815. }
  816. thermal->tshut_temp = shut_temp;
  817. }
  818. if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
  819. dev_warn(dev,
  820. "Missing tshut mode property, using default (%s)\n",
  821. thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
  822. "gpio" : "cru");
  823. thermal->tshut_mode = thermal->chip->tshut_mode;
  824. } else {
  825. thermal->tshut_mode = tshut_mode;
  826. }
  827. if (thermal->tshut_mode > 1) {
  828. dev_err(dev, "Invalid tshut mode specified: %d\n",
  829. thermal->tshut_mode);
  830. return -EINVAL;
  831. }
  832. if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
  833. &tshut_polarity)) {
  834. dev_warn(dev,
  835. "Missing tshut-polarity property, using default (%s)\n",
  836. thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
  837. "low" : "high");
  838. thermal->tshut_polarity = thermal->chip->tshut_polarity;
  839. } else {
  840. thermal->tshut_polarity = tshut_polarity;
  841. }
  842. if (thermal->tshut_polarity > 1) {
  843. dev_err(dev, "Invalid tshut-polarity specified: %d\n",
  844. thermal->tshut_polarity);
  845. return -EINVAL;
  846. }
  847. /* The tsadc wont to handle the error in here since some SoCs didn't
  848. * need this property.
  849. */
  850. thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  851. return 0;
  852. }
  853. static int
  854. rockchip_thermal_register_sensor(struct platform_device *pdev,
  855. struct rockchip_thermal_data *thermal,
  856. struct rockchip_thermal_sensor *sensor,
  857. int id)
  858. {
  859. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  860. int error;
  861. tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
  862. tsadc->set_tshut_temp(tsadc->table, id, thermal->regs,
  863. thermal->tshut_temp);
  864. sensor->thermal = thermal;
  865. sensor->id = id;
  866. sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev, id,
  867. sensor, &rockchip_of_thermal_ops);
  868. if (IS_ERR(sensor->tzd)) {
  869. error = PTR_ERR(sensor->tzd);
  870. dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
  871. id, error);
  872. return error;
  873. }
  874. return 0;
  875. }
  876. /**
  877. * Reset TSADC Controller, reset all tsadc registers.
  878. */
  879. static void rockchip_thermal_reset_controller(struct reset_control *reset)
  880. {
  881. reset_control_assert(reset);
  882. usleep_range(10, 20);
  883. reset_control_deassert(reset);
  884. }
  885. static int rockchip_thermal_probe(struct platform_device *pdev)
  886. {
  887. struct device_node *np = pdev->dev.of_node;
  888. struct rockchip_thermal_data *thermal;
  889. const struct of_device_id *match;
  890. struct resource *res;
  891. int irq;
  892. int i;
  893. int error;
  894. match = of_match_node(of_rockchip_thermal_match, np);
  895. if (!match)
  896. return -ENXIO;
  897. irq = platform_get_irq(pdev, 0);
  898. if (irq < 0) {
  899. dev_err(&pdev->dev, "no irq resource?\n");
  900. return -EINVAL;
  901. }
  902. thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
  903. GFP_KERNEL);
  904. if (!thermal)
  905. return -ENOMEM;
  906. thermal->pdev = pdev;
  907. thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
  908. if (!thermal->chip)
  909. return -EINVAL;
  910. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  911. thermal->regs = devm_ioremap_resource(&pdev->dev, res);
  912. if (IS_ERR(thermal->regs))
  913. return PTR_ERR(thermal->regs);
  914. thermal->reset = devm_reset_control_get(&pdev->dev, "tsadc-apb");
  915. if (IS_ERR(thermal->reset)) {
  916. error = PTR_ERR(thermal->reset);
  917. dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
  918. return error;
  919. }
  920. thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
  921. if (IS_ERR(thermal->clk)) {
  922. error = PTR_ERR(thermal->clk);
  923. dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
  924. return error;
  925. }
  926. thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  927. if (IS_ERR(thermal->pclk)) {
  928. error = PTR_ERR(thermal->pclk);
  929. dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
  930. error);
  931. return error;
  932. }
  933. error = clk_prepare_enable(thermal->clk);
  934. if (error) {
  935. dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
  936. error);
  937. return error;
  938. }
  939. error = clk_prepare_enable(thermal->pclk);
  940. if (error) {
  941. dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
  942. goto err_disable_clk;
  943. }
  944. rockchip_thermal_reset_controller(thermal->reset);
  945. error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
  946. if (error) {
  947. dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
  948. error);
  949. goto err_disable_pclk;
  950. }
  951. thermal->chip->initialize(thermal->grf, thermal->regs,
  952. thermal->tshut_polarity);
  953. for (i = 0; i < thermal->chip->chn_num; i++) {
  954. error = rockchip_thermal_register_sensor(pdev, thermal,
  955. &thermal->sensors[i],
  956. thermal->chip->chn_id[i]);
  957. if (error) {
  958. dev_err(&pdev->dev,
  959. "failed to register sensor[%d] : error = %d\n",
  960. i, error);
  961. goto err_disable_pclk;
  962. }
  963. }
  964. error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  965. &rockchip_thermal_alarm_irq_thread,
  966. IRQF_ONESHOT,
  967. "rockchip_thermal", thermal);
  968. if (error) {
  969. dev_err(&pdev->dev,
  970. "failed to request tsadc irq: %d\n", error);
  971. goto err_disable_pclk;
  972. }
  973. thermal->chip->control(thermal->regs, true);
  974. for (i = 0; i < thermal->chip->chn_num; i++)
  975. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  976. platform_set_drvdata(pdev, thermal);
  977. return 0;
  978. err_disable_pclk:
  979. clk_disable_unprepare(thermal->pclk);
  980. err_disable_clk:
  981. clk_disable_unprepare(thermal->clk);
  982. return error;
  983. }
  984. static int rockchip_thermal_remove(struct platform_device *pdev)
  985. {
  986. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  987. int i;
  988. for (i = 0; i < thermal->chip->chn_num; i++) {
  989. struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
  990. rockchip_thermal_toggle_sensor(sensor, false);
  991. }
  992. thermal->chip->control(thermal->regs, false);
  993. clk_disable_unprepare(thermal->pclk);
  994. clk_disable_unprepare(thermal->clk);
  995. return 0;
  996. }
  997. static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
  998. {
  999. struct platform_device *pdev = to_platform_device(dev);
  1000. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  1001. int i;
  1002. for (i = 0; i < thermal->chip->chn_num; i++)
  1003. rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
  1004. thermal->chip->control(thermal->regs, false);
  1005. clk_disable(thermal->pclk);
  1006. clk_disable(thermal->clk);
  1007. pinctrl_pm_select_sleep_state(dev);
  1008. return 0;
  1009. }
  1010. static int __maybe_unused rockchip_thermal_resume(struct device *dev)
  1011. {
  1012. struct platform_device *pdev = to_platform_device(dev);
  1013. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  1014. int i;
  1015. int error;
  1016. error = clk_enable(thermal->clk);
  1017. if (error)
  1018. return error;
  1019. error = clk_enable(thermal->pclk);
  1020. if (error) {
  1021. clk_disable(thermal->clk);
  1022. return error;
  1023. }
  1024. rockchip_thermal_reset_controller(thermal->reset);
  1025. thermal->chip->initialize(thermal->grf, thermal->regs,
  1026. thermal->tshut_polarity);
  1027. for (i = 0; i < thermal->chip->chn_num; i++) {
  1028. int id = thermal->sensors[i].id;
  1029. thermal->chip->set_tshut_mode(id, thermal->regs,
  1030. thermal->tshut_mode);
  1031. thermal->chip->set_tshut_temp(thermal->chip->table,
  1032. id, thermal->regs,
  1033. thermal->tshut_temp);
  1034. }
  1035. thermal->chip->control(thermal->regs, true);
  1036. for (i = 0; i < thermal->chip->chn_num; i++)
  1037. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  1038. pinctrl_pm_select_default_state(dev);
  1039. return 0;
  1040. }
  1041. static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
  1042. rockchip_thermal_suspend, rockchip_thermal_resume);
  1043. static struct platform_driver rockchip_thermal_driver = {
  1044. .driver = {
  1045. .name = "rockchip-thermal",
  1046. .pm = &rockchip_thermal_pm_ops,
  1047. .of_match_table = of_rockchip_thermal_match,
  1048. },
  1049. .probe = rockchip_thermal_probe,
  1050. .remove = rockchip_thermal_remove,
  1051. };
  1052. module_platform_driver(rockchip_thermal_driver);
  1053. MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
  1054. MODULE_AUTHOR("Rockchip, Inc.");
  1055. MODULE_LICENSE("GPL v2");
  1056. MODULE_ALIAS("platform:rockchip-thermal");