rockchip_thermal.c 51 KB

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