rockchip_thermal.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2016, Fuzhou Rockchip Electronics Co., Ltd
  4. * Caesar Wang <[email protected]>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include <linux/reset.h>
  17. #include <linux/thermal.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/pinctrl/consumer.h>
  20. /*
  21. * If the temperature over a period of time High,
  22. * the resulting TSHUT gave CRU module,let it reset the entire chip,
  23. * or via GPIO give PMIC.
  24. */
  25. enum tshut_mode {
  26. TSHUT_MODE_CRU = 0,
  27. TSHUT_MODE_GPIO,
  28. };
  29. /*
  30. * The system Temperature Sensors tshut(tshut) polarity
  31. * the bit 8 is tshut polarity.
  32. * 0: low active, 1: high active
  33. */
  34. enum tshut_polarity {
  35. TSHUT_LOW_ACTIVE = 0,
  36. TSHUT_HIGH_ACTIVE,
  37. };
  38. /*
  39. * The system has two Temperature Sensors.
  40. * sensor0 is for CPU, and sensor1 is for GPU.
  41. */
  42. enum sensor_id {
  43. SENSOR_CPU = 0,
  44. SENSOR_GPU,
  45. };
  46. /*
  47. * The conversion table has the adc value and temperature.
  48. * ADC_DECREMENT: the adc value is of diminishing.(e.g. rk3288_code_table)
  49. * ADC_INCREMENT: the adc value is incremental.(e.g. rk3368_code_table)
  50. */
  51. enum adc_sort_mode {
  52. ADC_DECREMENT = 0,
  53. ADC_INCREMENT,
  54. };
  55. #include "thermal_hwmon.h"
  56. /**
  57. * The max sensors is two in rockchip SoCs.
  58. * Two sensors: CPU and GPU sensor.
  59. */
  60. #define SOC_MAX_SENSORS 2
  61. /**
  62. * struct chip_tsadc_table - hold information about chip-specific differences
  63. * @id: conversion table
  64. * @length: size of conversion table
  65. * @data_mask: mask to apply on data inputs
  66. * @mode: sort mode of this adc variant (incrementing or decrementing)
  67. */
  68. struct chip_tsadc_table {
  69. const struct tsadc_table *id;
  70. unsigned int length;
  71. u32 data_mask;
  72. enum adc_sort_mode mode;
  73. };
  74. /**
  75. * struct rockchip_tsadc_chip - hold the private data of tsadc chip
  76. * @chn_id: array of sensor ids of chip corresponding to the channel
  77. * @chn_num: the channel number of tsadc chip
  78. * @tshut_temp: the hardware-controlled shutdown temperature value
  79. * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
  80. * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
  81. * @initialize: SoC special initialize tsadc controller method
  82. * @irq_ack: clear the interrupt
  83. * @control: enable/disable method for the tsadc controller
  84. * @get_temp: get the temperature
  85. * @set_alarm_temp: set the high temperature interrupt
  86. * @set_tshut_temp: set the hardware-controlled shutdown temperature
  87. * @set_tshut_mode: set the hardware-controlled shutdown mode
  88. * @table: the chip-specific conversion table
  89. */
  90. struct rockchip_tsadc_chip {
  91. /* The sensor id of chip correspond to the ADC channel */
  92. int chn_id[SOC_MAX_SENSORS];
  93. int chn_num;
  94. /* The hardware-controlled tshut property */
  95. int tshut_temp;
  96. enum tshut_mode tshut_mode;
  97. enum tshut_polarity tshut_polarity;
  98. /* Chip-wide methods */
  99. void (*initialize)(struct regmap *grf,
  100. void __iomem *reg, enum tshut_polarity p);
  101. void (*irq_ack)(void __iomem *reg);
  102. void (*control)(void __iomem *reg, bool on);
  103. /* Per-sensor methods */
  104. int (*get_temp)(const struct chip_tsadc_table *table,
  105. int chn, void __iomem *reg, int *temp);
  106. int (*set_alarm_temp)(const struct chip_tsadc_table *table,
  107. int chn, void __iomem *reg, int temp);
  108. int (*set_tshut_temp)(const struct chip_tsadc_table *table,
  109. int chn, void __iomem *reg, int temp);
  110. void (*set_tshut_mode)(int chn, void __iomem *reg, enum tshut_mode m);
  111. /* Per-table methods */
  112. struct chip_tsadc_table table;
  113. };
  114. /**
  115. * struct rockchip_thermal_sensor - hold the information of thermal sensor
  116. * @thermal: pointer to the platform/configuration data
  117. * @tzd: pointer to a thermal zone
  118. * @id: identifier of the thermal sensor
  119. */
  120. struct rockchip_thermal_sensor {
  121. struct rockchip_thermal_data *thermal;
  122. struct thermal_zone_device *tzd;
  123. int id;
  124. };
  125. /**
  126. * struct rockchip_thermal_data - hold the private data of thermal driver
  127. * @chip: pointer to the platform/configuration data
  128. * @pdev: platform device of thermal
  129. * @reset: the reset controller of tsadc
  130. * @sensors: array of thermal sensors
  131. * @clk: the controller clock is divided by the exteral 24MHz
  132. * @pclk: the advanced peripherals bus clock
  133. * @grf: the general register file will be used to do static set by software
  134. * @regs: the base address of tsadc controller
  135. * @tshut_temp: the hardware-controlled shutdown temperature value
  136. * @tshut_mode: the hardware-controlled shutdown mode (0:CRU 1:GPIO)
  137. * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
  138. */
  139. struct rockchip_thermal_data {
  140. const struct rockchip_tsadc_chip *chip;
  141. struct platform_device *pdev;
  142. struct reset_control *reset;
  143. struct rockchip_thermal_sensor sensors[SOC_MAX_SENSORS];
  144. struct clk *clk;
  145. struct clk *pclk;
  146. struct regmap *grf;
  147. void __iomem *regs;
  148. int tshut_temp;
  149. enum tshut_mode tshut_mode;
  150. enum tshut_polarity tshut_polarity;
  151. };
  152. /**
  153. * TSADC Sensor Register description:
  154. *
  155. * TSADCV2_* are used for RK3288 SoCs, the other chips can reuse it.
  156. * TSADCV3_* are used for newer SoCs than RK3288. (e.g: RK3228, RK3399)
  157. *
  158. */
  159. #define TSADCV2_USER_CON 0x00
  160. #define TSADCV2_AUTO_CON 0x04
  161. #define TSADCV2_INT_EN 0x08
  162. #define TSADCV2_INT_PD 0x0c
  163. #define TSADCV2_DATA(chn) (0x20 + (chn) * 0x04)
  164. #define TSADCV2_COMP_INT(chn) (0x30 + (chn) * 0x04)
  165. #define TSADCV2_COMP_SHUT(chn) (0x40 + (chn) * 0x04)
  166. #define TSADCV2_HIGHT_INT_DEBOUNCE 0x60
  167. #define TSADCV2_HIGHT_TSHUT_DEBOUNCE 0x64
  168. #define TSADCV2_AUTO_PERIOD 0x68
  169. #define TSADCV2_AUTO_PERIOD_HT 0x6c
  170. #define TSADCV2_AUTO_EN BIT(0)
  171. #define TSADCV2_AUTO_SRC_EN(chn) BIT(4 + (chn))
  172. #define TSADCV2_AUTO_TSHUT_POLARITY_HIGH BIT(8)
  173. #define TSADCV3_AUTO_Q_SEL_EN BIT(1)
  174. #define TSADCV2_INT_SRC_EN(chn) BIT(chn)
  175. #define TSADCV2_SHUT_2GPIO_SRC_EN(chn) BIT(4 + (chn))
  176. #define TSADCV2_SHUT_2CRU_SRC_EN(chn) BIT(8 + (chn))
  177. #define TSADCV2_INT_PD_CLEAR_MASK ~BIT(8)
  178. #define TSADCV3_INT_PD_CLEAR_MASK ~BIT(16)
  179. #define TSADCV2_DATA_MASK 0xfff
  180. #define TSADCV3_DATA_MASK 0x3ff
  181. #define TSADCV2_HIGHT_INT_DEBOUNCE_COUNT 4
  182. #define TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT 4
  183. #define TSADCV2_AUTO_PERIOD_TIME 250 /* 250ms */
  184. #define TSADCV2_AUTO_PERIOD_HT_TIME 50 /* 50ms */
  185. #define TSADCV3_AUTO_PERIOD_TIME 1875 /* 2.5ms */
  186. #define TSADCV3_AUTO_PERIOD_HT_TIME 1875 /* 2.5ms */
  187. #define TSADCV5_AUTO_PERIOD_TIME 1622 /* 2.5ms */
  188. #define TSADCV5_AUTO_PERIOD_HT_TIME 1622 /* 2.5ms */
  189. #define TSADCV2_USER_INTER_PD_SOC 0x340 /* 13 clocks */
  190. #define TSADCV5_USER_INTER_PD_SOC 0xfc0 /* 97us, at least 90us */
  191. #define GRF_SARADC_TESTBIT 0x0e644
  192. #define GRF_TSADC_TESTBIT_L 0x0e648
  193. #define GRF_TSADC_TESTBIT_H 0x0e64c
  194. #define PX30_GRF_SOC_CON2 0x0408
  195. #define RK3568_GRF_TSADC_CON 0x0600
  196. #define RK3568_GRF_TSADC_ANA_REG0 (0x10001 << 0)
  197. #define RK3568_GRF_TSADC_ANA_REG1 (0x10001 << 1)
  198. #define RK3568_GRF_TSADC_ANA_REG2 (0x10001 << 2)
  199. #define RK3568_GRF_TSADC_TSEN (0x10001 << 8)
  200. #define GRF_SARADC_TESTBIT_ON (0x10001 << 2)
  201. #define GRF_TSADC_TESTBIT_H_ON (0x10001 << 2)
  202. #define GRF_TSADC_VCM_EN_L (0x10001 << 7)
  203. #define GRF_TSADC_VCM_EN_H (0x10001 << 7)
  204. #define GRF_CON_TSADC_CH_INV (0x10001 << 1)
  205. /**
  206. * struct tsadc_table - code to temperature conversion table
  207. * @code: the value of adc channel
  208. * @temp: the temperature
  209. * Note:
  210. * code to temperature mapping of the temperature sensor is a piece wise linear
  211. * curve.Any temperature, code faling between to 2 give temperatures can be
  212. * linearly interpolated.
  213. * Code to Temperature mapping should be updated based on manufacturer results.
  214. */
  215. struct tsadc_table {
  216. u32 code;
  217. int temp;
  218. };
  219. static const struct tsadc_table rv1108_table[] = {
  220. {0, -40000},
  221. {374, -40000},
  222. {382, -35000},
  223. {389, -30000},
  224. {397, -25000},
  225. {405, -20000},
  226. {413, -15000},
  227. {421, -10000},
  228. {429, -5000},
  229. {436, 0},
  230. {444, 5000},
  231. {452, 10000},
  232. {460, 15000},
  233. {468, 20000},
  234. {476, 25000},
  235. {483, 30000},
  236. {491, 35000},
  237. {499, 40000},
  238. {507, 45000},
  239. {515, 50000},
  240. {523, 55000},
  241. {531, 60000},
  242. {539, 65000},
  243. {547, 70000},
  244. {555, 75000},
  245. {562, 80000},
  246. {570, 85000},
  247. {578, 90000},
  248. {586, 95000},
  249. {594, 100000},
  250. {602, 105000},
  251. {610, 110000},
  252. {618, 115000},
  253. {626, 120000},
  254. {634, 125000},
  255. {TSADCV2_DATA_MASK, 125000},
  256. };
  257. static const struct tsadc_table rk3228_code_table[] = {
  258. {0, -40000},
  259. {588, -40000},
  260. {593, -35000},
  261. {598, -30000},
  262. {603, -25000},
  263. {608, -20000},
  264. {613, -15000},
  265. {618, -10000},
  266. {623, -5000},
  267. {629, 0},
  268. {634, 5000},
  269. {639, 10000},
  270. {644, 15000},
  271. {649, 20000},
  272. {654, 25000},
  273. {660, 30000},
  274. {665, 35000},
  275. {670, 40000},
  276. {675, 45000},
  277. {681, 50000},
  278. {686, 55000},
  279. {691, 60000},
  280. {696, 65000},
  281. {702, 70000},
  282. {707, 75000},
  283. {712, 80000},
  284. {717, 85000},
  285. {723, 90000},
  286. {728, 95000},
  287. {733, 100000},
  288. {738, 105000},
  289. {744, 110000},
  290. {749, 115000},
  291. {754, 120000},
  292. {760, 125000},
  293. {TSADCV2_DATA_MASK, 125000},
  294. };
  295. static const struct tsadc_table rk3288_code_table[] = {
  296. {TSADCV2_DATA_MASK, -40000},
  297. {3800, -40000},
  298. {3792, -35000},
  299. {3783, -30000},
  300. {3774, -25000},
  301. {3765, -20000},
  302. {3756, -15000},
  303. {3747, -10000},
  304. {3737, -5000},
  305. {3728, 0},
  306. {3718, 5000},
  307. {3708, 10000},
  308. {3698, 15000},
  309. {3688, 20000},
  310. {3678, 25000},
  311. {3667, 30000},
  312. {3656, 35000},
  313. {3645, 40000},
  314. {3634, 45000},
  315. {3623, 50000},
  316. {3611, 55000},
  317. {3600, 60000},
  318. {3588, 65000},
  319. {3575, 70000},
  320. {3563, 75000},
  321. {3550, 80000},
  322. {3537, 85000},
  323. {3524, 90000},
  324. {3510, 95000},
  325. {3496, 100000},
  326. {3482, 105000},
  327. {3467, 110000},
  328. {3452, 115000},
  329. {3437, 120000},
  330. {3421, 125000},
  331. {0, 125000},
  332. };
  333. static const struct tsadc_table rk3328_code_table[] = {
  334. {0, -40000},
  335. {296, -40000},
  336. {304, -35000},
  337. {313, -30000},
  338. {331, -20000},
  339. {340, -15000},
  340. {349, -10000},
  341. {359, -5000},
  342. {368, 0},
  343. {378, 5000},
  344. {388, 10000},
  345. {398, 15000},
  346. {408, 20000},
  347. {418, 25000},
  348. {429, 30000},
  349. {440, 35000},
  350. {451, 40000},
  351. {462, 45000},
  352. {473, 50000},
  353. {485, 55000},
  354. {496, 60000},
  355. {508, 65000},
  356. {521, 70000},
  357. {533, 75000},
  358. {546, 80000},
  359. {559, 85000},
  360. {572, 90000},
  361. {586, 95000},
  362. {600, 100000},
  363. {614, 105000},
  364. {629, 110000},
  365. {644, 115000},
  366. {659, 120000},
  367. {675, 125000},
  368. {TSADCV2_DATA_MASK, 125000},
  369. };
  370. static const struct tsadc_table rk3368_code_table[] = {
  371. {0, -40000},
  372. {106, -40000},
  373. {108, -35000},
  374. {110, -30000},
  375. {112, -25000},
  376. {114, -20000},
  377. {116, -15000},
  378. {118, -10000},
  379. {120, -5000},
  380. {122, 0},
  381. {124, 5000},
  382. {126, 10000},
  383. {128, 15000},
  384. {130, 20000},
  385. {132, 25000},
  386. {134, 30000},
  387. {136, 35000},
  388. {138, 40000},
  389. {140, 45000},
  390. {142, 50000},
  391. {144, 55000},
  392. {146, 60000},
  393. {148, 65000},
  394. {150, 70000},
  395. {152, 75000},
  396. {154, 80000},
  397. {156, 85000},
  398. {158, 90000},
  399. {160, 95000},
  400. {162, 100000},
  401. {163, 105000},
  402. {165, 110000},
  403. {167, 115000},
  404. {169, 120000},
  405. {171, 125000},
  406. {TSADCV3_DATA_MASK, 125000},
  407. };
  408. static const struct tsadc_table rk3399_code_table[] = {
  409. {0, -40000},
  410. {402, -40000},
  411. {410, -35000},
  412. {419, -30000},
  413. {427, -25000},
  414. {436, -20000},
  415. {444, -15000},
  416. {453, -10000},
  417. {461, -5000},
  418. {470, 0},
  419. {478, 5000},
  420. {487, 10000},
  421. {496, 15000},
  422. {504, 20000},
  423. {513, 25000},
  424. {521, 30000},
  425. {530, 35000},
  426. {538, 40000},
  427. {547, 45000},
  428. {555, 50000},
  429. {564, 55000},
  430. {573, 60000},
  431. {581, 65000},
  432. {590, 70000},
  433. {599, 75000},
  434. {607, 80000},
  435. {616, 85000},
  436. {624, 90000},
  437. {633, 95000},
  438. {642, 100000},
  439. {650, 105000},
  440. {659, 110000},
  441. {668, 115000},
  442. {677, 120000},
  443. {685, 125000},
  444. {TSADCV3_DATA_MASK, 125000},
  445. };
  446. static const struct tsadc_table rk3568_code_table[] = {
  447. {0, -40000},
  448. {1584, -40000},
  449. {1620, -35000},
  450. {1652, -30000},
  451. {1688, -25000},
  452. {1720, -20000},
  453. {1756, -15000},
  454. {1788, -10000},
  455. {1824, -5000},
  456. {1856, 0},
  457. {1892, 5000},
  458. {1924, 10000},
  459. {1956, 15000},
  460. {1992, 20000},
  461. {2024, 25000},
  462. {2060, 30000},
  463. {2092, 35000},
  464. {2128, 40000},
  465. {2160, 45000},
  466. {2196, 50000},
  467. {2228, 55000},
  468. {2264, 60000},
  469. {2300, 65000},
  470. {2332, 70000},
  471. {2368, 75000},
  472. {2400, 80000},
  473. {2436, 85000},
  474. {2468, 90000},
  475. {2500, 95000},
  476. {2536, 100000},
  477. {2572, 105000},
  478. {2604, 110000},
  479. {2636, 115000},
  480. {2672, 120000},
  481. {2704, 125000},
  482. {TSADCV2_DATA_MASK, 125000},
  483. };
  484. static u32 rk_tsadcv2_temp_to_code(const struct chip_tsadc_table *table,
  485. int temp)
  486. {
  487. int high, low, mid;
  488. unsigned long num;
  489. unsigned int denom;
  490. u32 error = table->data_mask;
  491. low = 0;
  492. high = (table->length - 1) - 1; /* ignore the last check for table */
  493. mid = (high + low) / 2;
  494. /* Return mask code data when the temp is over table range */
  495. if (temp < table->id[low].temp || temp > table->id[high].temp)
  496. goto exit;
  497. while (low <= high) {
  498. if (temp == table->id[mid].temp)
  499. return table->id[mid].code;
  500. else if (temp < table->id[mid].temp)
  501. high = mid - 1;
  502. else
  503. low = mid + 1;
  504. mid = (low + high) / 2;
  505. }
  506. /*
  507. * The conversion code granularity provided by the table. Let's
  508. * assume that the relationship between temperature and
  509. * analog value between 2 table entries is linear and interpolate
  510. * to produce less granular result.
  511. */
  512. num = abs(table->id[mid + 1].code - table->id[mid].code);
  513. num *= temp - table->id[mid].temp;
  514. denom = table->id[mid + 1].temp - table->id[mid].temp;
  515. switch (table->mode) {
  516. case ADC_DECREMENT:
  517. return table->id[mid].code - (num / denom);
  518. case ADC_INCREMENT:
  519. return table->id[mid].code + (num / denom);
  520. default:
  521. pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
  522. return error;
  523. }
  524. exit:
  525. pr_err("%s: invalid temperature, temp=%d error=%d\n",
  526. __func__, temp, error);
  527. return error;
  528. }
  529. static int rk_tsadcv2_code_to_temp(const struct chip_tsadc_table *table,
  530. u32 code, int *temp)
  531. {
  532. unsigned int low = 1;
  533. unsigned int high = table->length - 1;
  534. unsigned int mid = (low + high) / 2;
  535. unsigned int num;
  536. unsigned long denom;
  537. WARN_ON(table->length < 2);
  538. switch (table->mode) {
  539. case ADC_DECREMENT:
  540. code &= table->data_mask;
  541. if (code <= table->id[high].code)
  542. return -EAGAIN; /* Incorrect reading */
  543. while (low <= high) {
  544. if (code >= table->id[mid].code &&
  545. code < table->id[mid - 1].code)
  546. break;
  547. else if (code < table->id[mid].code)
  548. low = mid + 1;
  549. else
  550. high = mid - 1;
  551. mid = (low + high) / 2;
  552. }
  553. break;
  554. case ADC_INCREMENT:
  555. code &= table->data_mask;
  556. if (code < table->id[low].code)
  557. return -EAGAIN; /* Incorrect reading */
  558. while (low <= high) {
  559. if (code <= table->id[mid].code &&
  560. code > table->id[mid - 1].code)
  561. break;
  562. else if (code > table->id[mid].code)
  563. low = mid + 1;
  564. else
  565. high = mid - 1;
  566. mid = (low + high) / 2;
  567. }
  568. break;
  569. default:
  570. pr_err("%s: unknown table mode: %d\n", __func__, table->mode);
  571. return -EINVAL;
  572. }
  573. /*
  574. * The 5C granularity provided by the table is too much. Let's
  575. * assume that the relationship between sensor readings and
  576. * temperature between 2 table entries is linear and interpolate
  577. * to produce less granular result.
  578. */
  579. num = table->id[mid].temp - table->id[mid - 1].temp;
  580. num *= abs(table->id[mid - 1].code - code);
  581. denom = abs(table->id[mid - 1].code - table->id[mid].code);
  582. *temp = table->id[mid - 1].temp + (num / denom);
  583. return 0;
  584. }
  585. /**
  586. * rk_tsadcv2_initialize - initialize TASDC Controller.
  587. * @grf: the general register file will be used to do static set by software
  588. * @regs: the base address of tsadc controller
  589. * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
  590. *
  591. * (1) Set TSADC_V2_AUTO_PERIOD:
  592. * Configure the interleave between every two accessing of
  593. * TSADC in normal operation.
  594. *
  595. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  596. * Configure the interleave between every two accessing of
  597. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  598. *
  599. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  600. * If the temperature is higher than COMP_INT or COMP_SHUT for
  601. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  602. */
  603. static void rk_tsadcv2_initialize(struct regmap *grf, void __iomem *regs,
  604. enum tshut_polarity tshut_polarity)
  605. {
  606. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  607. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  608. regs + TSADCV2_AUTO_CON);
  609. else
  610. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  611. regs + TSADCV2_AUTO_CON);
  612. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
  613. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  614. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  615. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  616. regs + TSADCV2_AUTO_PERIOD_HT);
  617. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  618. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  619. }
  620. /**
  621. * rk_tsadcv3_initialize - initialize TASDC Controller.
  622. * @grf: the general register file will be used to do static set by software
  623. * @regs: the base address of tsadc controller
  624. * @tshut_polarity: the hardware-controlled active polarity (0:LOW 1:HIGH)
  625. *
  626. * (1) The tsadc control power sequence.
  627. *
  628. * (2) Set TSADC_V2_AUTO_PERIOD:
  629. * Configure the interleave between every two accessing of
  630. * TSADC in normal operation.
  631. *
  632. * (2) Set TSADCV2_AUTO_PERIOD_HT:
  633. * Configure the interleave between every two accessing of
  634. * TSADC after the temperature is higher than COM_SHUT or COM_INT.
  635. *
  636. * (3) Set TSADCV2_HIGH_INT_DEBOUNCE and TSADC_HIGHT_TSHUT_DEBOUNCE:
  637. * If the temperature is higher than COMP_INT or COMP_SHUT for
  638. * "debounce" times, TSADC controller will generate interrupt or TSHUT.
  639. */
  640. static void rk_tsadcv3_initialize(struct regmap *grf, void __iomem *regs,
  641. enum tshut_polarity tshut_polarity)
  642. {
  643. /* The tsadc control power sequence */
  644. if (IS_ERR(grf)) {
  645. /* Set interleave value to workround ic time sync issue */
  646. writel_relaxed(TSADCV2_USER_INTER_PD_SOC, regs +
  647. TSADCV2_USER_CON);
  648. writel_relaxed(TSADCV2_AUTO_PERIOD_TIME,
  649. regs + TSADCV2_AUTO_PERIOD);
  650. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  651. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  652. writel_relaxed(TSADCV2_AUTO_PERIOD_HT_TIME,
  653. regs + TSADCV2_AUTO_PERIOD_HT);
  654. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  655. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  656. } else {
  657. /* Enable the voltage common mode feature */
  658. regmap_write(grf, GRF_TSADC_TESTBIT_L, GRF_TSADC_VCM_EN_L);
  659. regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_VCM_EN_H);
  660. usleep_range(15, 100); /* The spec note says at least 15 us */
  661. regmap_write(grf, GRF_SARADC_TESTBIT, GRF_SARADC_TESTBIT_ON);
  662. regmap_write(grf, GRF_TSADC_TESTBIT_H, GRF_TSADC_TESTBIT_H_ON);
  663. usleep_range(90, 200); /* The spec note says at least 90 us */
  664. writel_relaxed(TSADCV3_AUTO_PERIOD_TIME,
  665. regs + TSADCV2_AUTO_PERIOD);
  666. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  667. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  668. writel_relaxed(TSADCV3_AUTO_PERIOD_HT_TIME,
  669. regs + TSADCV2_AUTO_PERIOD_HT);
  670. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  671. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  672. }
  673. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  674. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  675. regs + TSADCV2_AUTO_CON);
  676. else
  677. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  678. regs + TSADCV2_AUTO_CON);
  679. }
  680. static void rk_tsadcv4_initialize(struct regmap *grf, void __iomem *regs,
  681. enum tshut_polarity tshut_polarity)
  682. {
  683. rk_tsadcv2_initialize(grf, regs, tshut_polarity);
  684. regmap_write(grf, PX30_GRF_SOC_CON2, GRF_CON_TSADC_CH_INV);
  685. }
  686. static void rk_tsadcv7_initialize(struct regmap *grf, void __iomem *regs,
  687. enum tshut_polarity tshut_polarity)
  688. {
  689. writel_relaxed(TSADCV5_USER_INTER_PD_SOC, regs + TSADCV2_USER_CON);
  690. writel_relaxed(TSADCV5_AUTO_PERIOD_TIME, regs + TSADCV2_AUTO_PERIOD);
  691. writel_relaxed(TSADCV2_HIGHT_INT_DEBOUNCE_COUNT,
  692. regs + TSADCV2_HIGHT_INT_DEBOUNCE);
  693. writel_relaxed(TSADCV5_AUTO_PERIOD_HT_TIME,
  694. regs + TSADCV2_AUTO_PERIOD_HT);
  695. writel_relaxed(TSADCV2_HIGHT_TSHUT_DEBOUNCE_COUNT,
  696. regs + TSADCV2_HIGHT_TSHUT_DEBOUNCE);
  697. if (tshut_polarity == TSHUT_HIGH_ACTIVE)
  698. writel_relaxed(0U | TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  699. regs + TSADCV2_AUTO_CON);
  700. else
  701. writel_relaxed(0U & ~TSADCV2_AUTO_TSHUT_POLARITY_HIGH,
  702. regs + TSADCV2_AUTO_CON);
  703. /*
  704. * The general register file will is optional
  705. * and might not be available.
  706. */
  707. if (!IS_ERR(grf)) {
  708. regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_TSEN);
  709. /*
  710. * RK3568 TRM, section 18.5. requires a delay no less
  711. * than 10us between the rising edge of tsadc_tsen_en
  712. * and the rising edge of tsadc_ana_reg_0/1/2.
  713. */
  714. udelay(15);
  715. regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG0);
  716. regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG1);
  717. regmap_write(grf, RK3568_GRF_TSADC_CON, RK3568_GRF_TSADC_ANA_REG2);
  718. /*
  719. * RK3568 TRM, section 18.5. requires a delay no less
  720. * than 90us after the rising edge of tsadc_ana_reg_0/1/2.
  721. */
  722. usleep_range(100, 200);
  723. }
  724. }
  725. static void rk_tsadcv2_irq_ack(void __iomem *regs)
  726. {
  727. u32 val;
  728. val = readl_relaxed(regs + TSADCV2_INT_PD);
  729. writel_relaxed(val & TSADCV2_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  730. }
  731. static void rk_tsadcv3_irq_ack(void __iomem *regs)
  732. {
  733. u32 val;
  734. val = readl_relaxed(regs + TSADCV2_INT_PD);
  735. writel_relaxed(val & TSADCV3_INT_PD_CLEAR_MASK, regs + TSADCV2_INT_PD);
  736. }
  737. static void rk_tsadcv2_control(void __iomem *regs, bool enable)
  738. {
  739. u32 val;
  740. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  741. if (enable)
  742. val |= TSADCV2_AUTO_EN;
  743. else
  744. val &= ~TSADCV2_AUTO_EN;
  745. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  746. }
  747. /**
  748. * rk_tsadcv3_control - the tsadc controller is enabled or disabled.
  749. * @regs: the base address of tsadc controller
  750. * @enable: boolean flag to enable the controller
  751. *
  752. * NOTE: TSADC controller works at auto mode, and some SoCs need set the
  753. * tsadc_q_sel bit on TSADCV2_AUTO_CON[1]. The (1024 - tsadc_q) as output
  754. * adc value if setting this bit to enable.
  755. */
  756. static void rk_tsadcv3_control(void __iomem *regs, bool enable)
  757. {
  758. u32 val;
  759. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  760. if (enable)
  761. val |= TSADCV2_AUTO_EN | TSADCV3_AUTO_Q_SEL_EN;
  762. else
  763. val &= ~TSADCV2_AUTO_EN;
  764. writel_relaxed(val, regs + TSADCV2_AUTO_CON);
  765. }
  766. static int rk_tsadcv2_get_temp(const struct chip_tsadc_table *table,
  767. int chn, void __iomem *regs, int *temp)
  768. {
  769. u32 val;
  770. val = readl_relaxed(regs + TSADCV2_DATA(chn));
  771. return rk_tsadcv2_code_to_temp(table, val, temp);
  772. }
  773. static int rk_tsadcv2_alarm_temp(const struct chip_tsadc_table *table,
  774. int chn, void __iomem *regs, int temp)
  775. {
  776. u32 alarm_value;
  777. u32 int_en, int_clr;
  778. /*
  779. * In some cases, some sensors didn't need the trip points, the
  780. * set_trips will pass {-INT_MAX, INT_MAX} to trigger tsadc alarm
  781. * in the end, ignore this case and disable the high temperature
  782. * interrupt.
  783. */
  784. if (temp == INT_MAX) {
  785. int_clr = readl_relaxed(regs + TSADCV2_INT_EN);
  786. int_clr &= ~TSADCV2_INT_SRC_EN(chn);
  787. writel_relaxed(int_clr, regs + TSADCV2_INT_EN);
  788. return 0;
  789. }
  790. /* Make sure the value is valid */
  791. alarm_value = rk_tsadcv2_temp_to_code(table, temp);
  792. if (alarm_value == table->data_mask)
  793. return -ERANGE;
  794. writel_relaxed(alarm_value & table->data_mask,
  795. regs + TSADCV2_COMP_INT(chn));
  796. int_en = readl_relaxed(regs + TSADCV2_INT_EN);
  797. int_en |= TSADCV2_INT_SRC_EN(chn);
  798. writel_relaxed(int_en, regs + TSADCV2_INT_EN);
  799. return 0;
  800. }
  801. static int rk_tsadcv2_tshut_temp(const struct chip_tsadc_table *table,
  802. int chn, void __iomem *regs, int temp)
  803. {
  804. u32 tshut_value, val;
  805. /* Make sure the value is valid */
  806. tshut_value = rk_tsadcv2_temp_to_code(table, temp);
  807. if (tshut_value == table->data_mask)
  808. return -ERANGE;
  809. writel_relaxed(tshut_value, regs + TSADCV2_COMP_SHUT(chn));
  810. /* TSHUT will be valid */
  811. val = readl_relaxed(regs + TSADCV2_AUTO_CON);
  812. writel_relaxed(val | TSADCV2_AUTO_SRC_EN(chn), regs + TSADCV2_AUTO_CON);
  813. return 0;
  814. }
  815. static void rk_tsadcv2_tshut_mode(int chn, void __iomem *regs,
  816. enum tshut_mode mode)
  817. {
  818. u32 val;
  819. val = readl_relaxed(regs + TSADCV2_INT_EN);
  820. if (mode == TSHUT_MODE_GPIO) {
  821. val &= ~TSADCV2_SHUT_2CRU_SRC_EN(chn);
  822. val |= TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  823. } else {
  824. val &= ~TSADCV2_SHUT_2GPIO_SRC_EN(chn);
  825. val |= TSADCV2_SHUT_2CRU_SRC_EN(chn);
  826. }
  827. writel_relaxed(val, regs + TSADCV2_INT_EN);
  828. }
  829. static const struct rockchip_tsadc_chip px30_tsadc_data = {
  830. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  831. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  832. .chn_num = 2, /* 2 channels for tsadc */
  833. .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */
  834. .tshut_temp = 95000,
  835. .initialize = rk_tsadcv4_initialize,
  836. .irq_ack = rk_tsadcv3_irq_ack,
  837. .control = rk_tsadcv3_control,
  838. .get_temp = rk_tsadcv2_get_temp,
  839. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  840. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  841. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  842. .table = {
  843. .id = rk3328_code_table,
  844. .length = ARRAY_SIZE(rk3328_code_table),
  845. .data_mask = TSADCV2_DATA_MASK,
  846. .mode = ADC_INCREMENT,
  847. },
  848. };
  849. static const struct rockchip_tsadc_chip rv1108_tsadc_data = {
  850. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  851. .chn_num = 1, /* one channel for tsadc */
  852. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  853. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  854. .tshut_temp = 95000,
  855. .initialize = rk_tsadcv2_initialize,
  856. .irq_ack = rk_tsadcv3_irq_ack,
  857. .control = rk_tsadcv3_control,
  858. .get_temp = rk_tsadcv2_get_temp,
  859. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  860. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  861. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  862. .table = {
  863. .id = rv1108_table,
  864. .length = ARRAY_SIZE(rv1108_table),
  865. .data_mask = TSADCV2_DATA_MASK,
  866. .mode = ADC_INCREMENT,
  867. },
  868. };
  869. static const struct rockchip_tsadc_chip rk3228_tsadc_data = {
  870. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  871. .chn_num = 1, /* one channel for tsadc */
  872. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  873. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  874. .tshut_temp = 95000,
  875. .initialize = rk_tsadcv2_initialize,
  876. .irq_ack = rk_tsadcv3_irq_ack,
  877. .control = rk_tsadcv3_control,
  878. .get_temp = rk_tsadcv2_get_temp,
  879. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  880. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  881. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  882. .table = {
  883. .id = rk3228_code_table,
  884. .length = ARRAY_SIZE(rk3228_code_table),
  885. .data_mask = TSADCV3_DATA_MASK,
  886. .mode = ADC_INCREMENT,
  887. },
  888. };
  889. static const struct rockchip_tsadc_chip rk3288_tsadc_data = {
  890. .chn_id[SENSOR_CPU] = 1, /* cpu sensor is channel 1 */
  891. .chn_id[SENSOR_GPU] = 2, /* gpu sensor is channel 2 */
  892. .chn_num = 2, /* two channels for tsadc */
  893. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  894. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  895. .tshut_temp = 95000,
  896. .initialize = rk_tsadcv2_initialize,
  897. .irq_ack = rk_tsadcv2_irq_ack,
  898. .control = rk_tsadcv2_control,
  899. .get_temp = rk_tsadcv2_get_temp,
  900. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  901. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  902. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  903. .table = {
  904. .id = rk3288_code_table,
  905. .length = ARRAY_SIZE(rk3288_code_table),
  906. .data_mask = TSADCV2_DATA_MASK,
  907. .mode = ADC_DECREMENT,
  908. },
  909. };
  910. static const struct rockchip_tsadc_chip rk3328_tsadc_data = {
  911. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  912. .chn_num = 1, /* one channels for tsadc */
  913. .tshut_mode = TSHUT_MODE_CRU, /* default TSHUT via CRU */
  914. .tshut_temp = 95000,
  915. .initialize = rk_tsadcv2_initialize,
  916. .irq_ack = rk_tsadcv3_irq_ack,
  917. .control = rk_tsadcv3_control,
  918. .get_temp = rk_tsadcv2_get_temp,
  919. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  920. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  921. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  922. .table = {
  923. .id = rk3328_code_table,
  924. .length = ARRAY_SIZE(rk3328_code_table),
  925. .data_mask = TSADCV2_DATA_MASK,
  926. .mode = ADC_INCREMENT,
  927. },
  928. };
  929. static const struct rockchip_tsadc_chip rk3366_tsadc_data = {
  930. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  931. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  932. .chn_num = 2, /* two channels for tsadc */
  933. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  934. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  935. .tshut_temp = 95000,
  936. .initialize = rk_tsadcv3_initialize,
  937. .irq_ack = rk_tsadcv3_irq_ack,
  938. .control = rk_tsadcv3_control,
  939. .get_temp = rk_tsadcv2_get_temp,
  940. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  941. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  942. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  943. .table = {
  944. .id = rk3228_code_table,
  945. .length = ARRAY_SIZE(rk3228_code_table),
  946. .data_mask = TSADCV3_DATA_MASK,
  947. .mode = ADC_INCREMENT,
  948. },
  949. };
  950. static const struct rockchip_tsadc_chip rk3368_tsadc_data = {
  951. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  952. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  953. .chn_num = 2, /* two channels for tsadc */
  954. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  955. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  956. .tshut_temp = 95000,
  957. .initialize = rk_tsadcv2_initialize,
  958. .irq_ack = rk_tsadcv2_irq_ack,
  959. .control = rk_tsadcv2_control,
  960. .get_temp = rk_tsadcv2_get_temp,
  961. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  962. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  963. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  964. .table = {
  965. .id = rk3368_code_table,
  966. .length = ARRAY_SIZE(rk3368_code_table),
  967. .data_mask = TSADCV3_DATA_MASK,
  968. .mode = ADC_INCREMENT,
  969. },
  970. };
  971. static const struct rockchip_tsadc_chip rk3399_tsadc_data = {
  972. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  973. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  974. .chn_num = 2, /* two channels for tsadc */
  975. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  976. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  977. .tshut_temp = 95000,
  978. .initialize = rk_tsadcv3_initialize,
  979. .irq_ack = rk_tsadcv3_irq_ack,
  980. .control = rk_tsadcv3_control,
  981. .get_temp = rk_tsadcv2_get_temp,
  982. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  983. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  984. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  985. .table = {
  986. .id = rk3399_code_table,
  987. .length = ARRAY_SIZE(rk3399_code_table),
  988. .data_mask = TSADCV3_DATA_MASK,
  989. .mode = ADC_INCREMENT,
  990. },
  991. };
  992. static const struct rockchip_tsadc_chip rk3568_tsadc_data = {
  993. .chn_id[SENSOR_CPU] = 0, /* cpu sensor is channel 0 */
  994. .chn_id[SENSOR_GPU] = 1, /* gpu sensor is channel 1 */
  995. .chn_num = 2, /* two channels for tsadc */
  996. .tshut_mode = TSHUT_MODE_GPIO, /* default TSHUT via GPIO give PMIC */
  997. .tshut_polarity = TSHUT_LOW_ACTIVE, /* default TSHUT LOW ACTIVE */
  998. .tshut_temp = 95000,
  999. .initialize = rk_tsadcv7_initialize,
  1000. .irq_ack = rk_tsadcv3_irq_ack,
  1001. .control = rk_tsadcv3_control,
  1002. .get_temp = rk_tsadcv2_get_temp,
  1003. .set_alarm_temp = rk_tsadcv2_alarm_temp,
  1004. .set_tshut_temp = rk_tsadcv2_tshut_temp,
  1005. .set_tshut_mode = rk_tsadcv2_tshut_mode,
  1006. .table = {
  1007. .id = rk3568_code_table,
  1008. .length = ARRAY_SIZE(rk3568_code_table),
  1009. .data_mask = TSADCV2_DATA_MASK,
  1010. .mode = ADC_INCREMENT,
  1011. },
  1012. };
  1013. static const struct of_device_id of_rockchip_thermal_match[] = {
  1014. { .compatible = "rockchip,px30-tsadc",
  1015. .data = (void *)&px30_tsadc_data,
  1016. },
  1017. {
  1018. .compatible = "rockchip,rv1108-tsadc",
  1019. .data = (void *)&rv1108_tsadc_data,
  1020. },
  1021. {
  1022. .compatible = "rockchip,rk3228-tsadc",
  1023. .data = (void *)&rk3228_tsadc_data,
  1024. },
  1025. {
  1026. .compatible = "rockchip,rk3288-tsadc",
  1027. .data = (void *)&rk3288_tsadc_data,
  1028. },
  1029. {
  1030. .compatible = "rockchip,rk3328-tsadc",
  1031. .data = (void *)&rk3328_tsadc_data,
  1032. },
  1033. {
  1034. .compatible = "rockchip,rk3366-tsadc",
  1035. .data = (void *)&rk3366_tsadc_data,
  1036. },
  1037. {
  1038. .compatible = "rockchip,rk3368-tsadc",
  1039. .data = (void *)&rk3368_tsadc_data,
  1040. },
  1041. {
  1042. .compatible = "rockchip,rk3399-tsadc",
  1043. .data = (void *)&rk3399_tsadc_data,
  1044. },
  1045. {
  1046. .compatible = "rockchip,rk3568-tsadc",
  1047. .data = (void *)&rk3568_tsadc_data,
  1048. },
  1049. { /* end */ },
  1050. };
  1051. MODULE_DEVICE_TABLE(of, of_rockchip_thermal_match);
  1052. static void
  1053. rockchip_thermal_toggle_sensor(struct rockchip_thermal_sensor *sensor, bool on)
  1054. {
  1055. struct thermal_zone_device *tzd = sensor->tzd;
  1056. if (on)
  1057. thermal_zone_device_enable(tzd);
  1058. else
  1059. thermal_zone_device_disable(tzd);
  1060. }
  1061. static irqreturn_t rockchip_thermal_alarm_irq_thread(int irq, void *dev)
  1062. {
  1063. struct rockchip_thermal_data *thermal = dev;
  1064. int i;
  1065. dev_dbg(&thermal->pdev->dev, "thermal alarm\n");
  1066. thermal->chip->irq_ack(thermal->regs);
  1067. for (i = 0; i < thermal->chip->chn_num; i++)
  1068. thermal_zone_device_update(thermal->sensors[i].tzd,
  1069. THERMAL_EVENT_UNSPECIFIED);
  1070. return IRQ_HANDLED;
  1071. }
  1072. static int rockchip_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
  1073. {
  1074. struct rockchip_thermal_sensor *sensor = tz->devdata;
  1075. struct rockchip_thermal_data *thermal = sensor->thermal;
  1076. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  1077. dev_dbg(&thermal->pdev->dev, "%s: sensor %d: low: %d, high %d\n",
  1078. __func__, sensor->id, low, high);
  1079. return tsadc->set_alarm_temp(&tsadc->table,
  1080. sensor->id, thermal->regs, high);
  1081. }
  1082. static int rockchip_thermal_get_temp(struct thermal_zone_device *tz, int *out_temp)
  1083. {
  1084. struct rockchip_thermal_sensor *sensor = tz->devdata;
  1085. struct rockchip_thermal_data *thermal = sensor->thermal;
  1086. const struct rockchip_tsadc_chip *tsadc = sensor->thermal->chip;
  1087. int retval;
  1088. retval = tsadc->get_temp(&tsadc->table,
  1089. sensor->id, thermal->regs, out_temp);
  1090. dev_dbg(&thermal->pdev->dev, "sensor %d - temp: %d, retval: %d\n",
  1091. sensor->id, *out_temp, retval);
  1092. return retval;
  1093. }
  1094. static const struct thermal_zone_device_ops rockchip_of_thermal_ops = {
  1095. .get_temp = rockchip_thermal_get_temp,
  1096. .set_trips = rockchip_thermal_set_trips,
  1097. };
  1098. static int rockchip_configure_from_dt(struct device *dev,
  1099. struct device_node *np,
  1100. struct rockchip_thermal_data *thermal)
  1101. {
  1102. u32 shut_temp, tshut_mode, tshut_polarity;
  1103. if (of_property_read_u32(np, "rockchip,hw-tshut-temp", &shut_temp)) {
  1104. dev_warn(dev,
  1105. "Missing tshut temp property, using default %d\n",
  1106. thermal->chip->tshut_temp);
  1107. thermal->tshut_temp = thermal->chip->tshut_temp;
  1108. } else {
  1109. if (shut_temp > INT_MAX) {
  1110. dev_err(dev, "Invalid tshut temperature specified: %d\n",
  1111. shut_temp);
  1112. return -ERANGE;
  1113. }
  1114. thermal->tshut_temp = shut_temp;
  1115. }
  1116. if (of_property_read_u32(np, "rockchip,hw-tshut-mode", &tshut_mode)) {
  1117. dev_warn(dev,
  1118. "Missing tshut mode property, using default (%s)\n",
  1119. thermal->chip->tshut_mode == TSHUT_MODE_GPIO ?
  1120. "gpio" : "cru");
  1121. thermal->tshut_mode = thermal->chip->tshut_mode;
  1122. } else {
  1123. thermal->tshut_mode = tshut_mode;
  1124. }
  1125. if (thermal->tshut_mode > 1) {
  1126. dev_err(dev, "Invalid tshut mode specified: %d\n",
  1127. thermal->tshut_mode);
  1128. return -EINVAL;
  1129. }
  1130. if (of_property_read_u32(np, "rockchip,hw-tshut-polarity",
  1131. &tshut_polarity)) {
  1132. dev_warn(dev,
  1133. "Missing tshut-polarity property, using default (%s)\n",
  1134. thermal->chip->tshut_polarity == TSHUT_LOW_ACTIVE ?
  1135. "low" : "high");
  1136. thermal->tshut_polarity = thermal->chip->tshut_polarity;
  1137. } else {
  1138. thermal->tshut_polarity = tshut_polarity;
  1139. }
  1140. if (thermal->tshut_polarity > 1) {
  1141. dev_err(dev, "Invalid tshut-polarity specified: %d\n",
  1142. thermal->tshut_polarity);
  1143. return -EINVAL;
  1144. }
  1145. /* The tsadc wont to handle the error in here since some SoCs didn't
  1146. * need this property.
  1147. */
  1148. thermal->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  1149. if (IS_ERR(thermal->grf))
  1150. dev_warn(dev, "Missing rockchip,grf property\n");
  1151. return 0;
  1152. }
  1153. static int
  1154. rockchip_thermal_register_sensor(struct platform_device *pdev,
  1155. struct rockchip_thermal_data *thermal,
  1156. struct rockchip_thermal_sensor *sensor,
  1157. int id)
  1158. {
  1159. const struct rockchip_tsadc_chip *tsadc = thermal->chip;
  1160. int error;
  1161. tsadc->set_tshut_mode(id, thermal->regs, thermal->tshut_mode);
  1162. error = tsadc->set_tshut_temp(&tsadc->table, id, thermal->regs,
  1163. thermal->tshut_temp);
  1164. if (error)
  1165. dev_err(&pdev->dev, "%s: invalid tshut=%d, error=%d\n",
  1166. __func__, thermal->tshut_temp, error);
  1167. sensor->thermal = thermal;
  1168. sensor->id = id;
  1169. sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, id, sensor,
  1170. &rockchip_of_thermal_ops);
  1171. if (IS_ERR(sensor->tzd)) {
  1172. error = PTR_ERR(sensor->tzd);
  1173. dev_err(&pdev->dev, "failed to register sensor %d: %d\n",
  1174. id, error);
  1175. return error;
  1176. }
  1177. return 0;
  1178. }
  1179. /**
  1180. * Reset TSADC Controller, reset all tsadc registers.
  1181. * @reset: the reset controller of tsadc
  1182. */
  1183. static void rockchip_thermal_reset_controller(struct reset_control *reset)
  1184. {
  1185. reset_control_assert(reset);
  1186. usleep_range(10, 20);
  1187. reset_control_deassert(reset);
  1188. }
  1189. static int rockchip_thermal_probe(struct platform_device *pdev)
  1190. {
  1191. struct device_node *np = pdev->dev.of_node;
  1192. struct rockchip_thermal_data *thermal;
  1193. const struct of_device_id *match;
  1194. struct resource *res;
  1195. int irq;
  1196. int i;
  1197. int error;
  1198. match = of_match_node(of_rockchip_thermal_match, np);
  1199. if (!match)
  1200. return -ENXIO;
  1201. irq = platform_get_irq(pdev, 0);
  1202. if (irq < 0)
  1203. return -EINVAL;
  1204. thermal = devm_kzalloc(&pdev->dev, sizeof(struct rockchip_thermal_data),
  1205. GFP_KERNEL);
  1206. if (!thermal)
  1207. return -ENOMEM;
  1208. thermal->pdev = pdev;
  1209. thermal->chip = (const struct rockchip_tsadc_chip *)match->data;
  1210. if (!thermal->chip)
  1211. return -EINVAL;
  1212. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1213. thermal->regs = devm_ioremap_resource(&pdev->dev, res);
  1214. if (IS_ERR(thermal->regs))
  1215. return PTR_ERR(thermal->regs);
  1216. thermal->reset = devm_reset_control_array_get(&pdev->dev, false, false);
  1217. if (IS_ERR(thermal->reset)) {
  1218. error = PTR_ERR(thermal->reset);
  1219. dev_err(&pdev->dev, "failed to get tsadc reset: %d\n", error);
  1220. return error;
  1221. }
  1222. thermal->clk = devm_clk_get(&pdev->dev, "tsadc");
  1223. if (IS_ERR(thermal->clk)) {
  1224. error = PTR_ERR(thermal->clk);
  1225. dev_err(&pdev->dev, "failed to get tsadc clock: %d\n", error);
  1226. return error;
  1227. }
  1228. thermal->pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  1229. if (IS_ERR(thermal->pclk)) {
  1230. error = PTR_ERR(thermal->pclk);
  1231. dev_err(&pdev->dev, "failed to get apb_pclk clock: %d\n",
  1232. error);
  1233. return error;
  1234. }
  1235. error = clk_prepare_enable(thermal->clk);
  1236. if (error) {
  1237. dev_err(&pdev->dev, "failed to enable converter clock: %d\n",
  1238. error);
  1239. return error;
  1240. }
  1241. error = clk_prepare_enable(thermal->pclk);
  1242. if (error) {
  1243. dev_err(&pdev->dev, "failed to enable pclk: %d\n", error);
  1244. goto err_disable_clk;
  1245. }
  1246. rockchip_thermal_reset_controller(thermal->reset);
  1247. error = rockchip_configure_from_dt(&pdev->dev, np, thermal);
  1248. if (error) {
  1249. dev_err(&pdev->dev, "failed to parse device tree data: %d\n",
  1250. error);
  1251. goto err_disable_pclk;
  1252. }
  1253. thermal->chip->initialize(thermal->grf, thermal->regs,
  1254. thermal->tshut_polarity);
  1255. for (i = 0; i < thermal->chip->chn_num; i++) {
  1256. error = rockchip_thermal_register_sensor(pdev, thermal,
  1257. &thermal->sensors[i],
  1258. thermal->chip->chn_id[i]);
  1259. if (error) {
  1260. dev_err(&pdev->dev,
  1261. "failed to register sensor[%d] : error = %d\n",
  1262. i, error);
  1263. goto err_disable_pclk;
  1264. }
  1265. }
  1266. error = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  1267. &rockchip_thermal_alarm_irq_thread,
  1268. IRQF_ONESHOT,
  1269. "rockchip_thermal", thermal);
  1270. if (error) {
  1271. dev_err(&pdev->dev,
  1272. "failed to request tsadc irq: %d\n", error);
  1273. goto err_disable_pclk;
  1274. }
  1275. thermal->chip->control(thermal->regs, true);
  1276. for (i = 0; i < thermal->chip->chn_num; i++) {
  1277. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  1278. thermal->sensors[i].tzd->tzp->no_hwmon = false;
  1279. error = thermal_add_hwmon_sysfs(thermal->sensors[i].tzd);
  1280. if (error)
  1281. dev_warn(&pdev->dev,
  1282. "failed to register sensor %d with hwmon: %d\n",
  1283. i, error);
  1284. }
  1285. platform_set_drvdata(pdev, thermal);
  1286. return 0;
  1287. err_disable_pclk:
  1288. clk_disable_unprepare(thermal->pclk);
  1289. err_disable_clk:
  1290. clk_disable_unprepare(thermal->clk);
  1291. return error;
  1292. }
  1293. static int rockchip_thermal_remove(struct platform_device *pdev)
  1294. {
  1295. struct rockchip_thermal_data *thermal = platform_get_drvdata(pdev);
  1296. int i;
  1297. for (i = 0; i < thermal->chip->chn_num; i++) {
  1298. struct rockchip_thermal_sensor *sensor = &thermal->sensors[i];
  1299. thermal_remove_hwmon_sysfs(sensor->tzd);
  1300. rockchip_thermal_toggle_sensor(sensor, false);
  1301. }
  1302. thermal->chip->control(thermal->regs, false);
  1303. clk_disable_unprepare(thermal->pclk);
  1304. clk_disable_unprepare(thermal->clk);
  1305. return 0;
  1306. }
  1307. static int __maybe_unused rockchip_thermal_suspend(struct device *dev)
  1308. {
  1309. struct rockchip_thermal_data *thermal = dev_get_drvdata(dev);
  1310. int i;
  1311. for (i = 0; i < thermal->chip->chn_num; i++)
  1312. rockchip_thermal_toggle_sensor(&thermal->sensors[i], false);
  1313. thermal->chip->control(thermal->regs, false);
  1314. clk_disable(thermal->pclk);
  1315. clk_disable(thermal->clk);
  1316. pinctrl_pm_select_sleep_state(dev);
  1317. return 0;
  1318. }
  1319. static int __maybe_unused rockchip_thermal_resume(struct device *dev)
  1320. {
  1321. struct rockchip_thermal_data *thermal = dev_get_drvdata(dev);
  1322. int i;
  1323. int error;
  1324. error = clk_enable(thermal->clk);
  1325. if (error)
  1326. return error;
  1327. error = clk_enable(thermal->pclk);
  1328. if (error) {
  1329. clk_disable(thermal->clk);
  1330. return error;
  1331. }
  1332. rockchip_thermal_reset_controller(thermal->reset);
  1333. thermal->chip->initialize(thermal->grf, thermal->regs,
  1334. thermal->tshut_polarity);
  1335. for (i = 0; i < thermal->chip->chn_num; i++) {
  1336. int id = thermal->sensors[i].id;
  1337. thermal->chip->set_tshut_mode(id, thermal->regs,
  1338. thermal->tshut_mode);
  1339. error = thermal->chip->set_tshut_temp(&thermal->chip->table,
  1340. id, thermal->regs,
  1341. thermal->tshut_temp);
  1342. if (error)
  1343. dev_err(dev, "%s: invalid tshut=%d, error=%d\n",
  1344. __func__, thermal->tshut_temp, error);
  1345. }
  1346. thermal->chip->control(thermal->regs, true);
  1347. for (i = 0; i < thermal->chip->chn_num; i++)
  1348. rockchip_thermal_toggle_sensor(&thermal->sensors[i], true);
  1349. pinctrl_pm_select_default_state(dev);
  1350. return 0;
  1351. }
  1352. static SIMPLE_DEV_PM_OPS(rockchip_thermal_pm_ops,
  1353. rockchip_thermal_suspend, rockchip_thermal_resume);
  1354. static struct platform_driver rockchip_thermal_driver = {
  1355. .driver = {
  1356. .name = "rockchip-thermal",
  1357. .pm = &rockchip_thermal_pm_ops,
  1358. .of_match_table = of_rockchip_thermal_match,
  1359. },
  1360. .probe = rockchip_thermal_probe,
  1361. .remove = rockchip_thermal_remove,
  1362. };
  1363. module_platform_driver(rockchip_thermal_driver);
  1364. MODULE_DESCRIPTION("ROCKCHIP THERMAL Driver");
  1365. MODULE_AUTHOR("Rockchip, Inc.");
  1366. MODULE_LICENSE("GPL v2");
  1367. MODULE_ALIAS("platform:rockchip-thermal");