rockchip_thermal.c 45 KB

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