rockchip_thermal.c 37 KB

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