rockchip_thermal.c 35 KB

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