mtk_thermal.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: Hanyi Wu <hanyi.wu@mediatek.com>
  4. * Sascha Hauer <s.hauer@pengutronix.de>
  5. * Dawei Chien <dawei.chien@mediatek.com>
  6. * Louis Yu <louis.yu@mediatek.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/nvmem-consumer.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/io.h>
  29. #include <linux/thermal.h>
  30. #include <linux/reset.h>
  31. #include <linux/types.h>
  32. /* AUXADC Registers */
  33. #define AUXADC_CON1_SET_V 0x008
  34. #define AUXADC_CON1_CLR_V 0x00c
  35. #define AUXADC_CON2_V 0x010
  36. #define AUXADC_DATA(channel) (0x14 + (channel) * 4)
  37. #define APMIXED_SYS_TS_CON1 0x604
  38. /* Thermal Controller Registers */
  39. #define TEMP_MONCTL0 0x000
  40. #define TEMP_MONCTL1 0x004
  41. #define TEMP_MONCTL2 0x008
  42. #define TEMP_MONIDET0 0x014
  43. #define TEMP_MONIDET1 0x018
  44. #define TEMP_MSRCTL0 0x038
  45. #define TEMP_AHBPOLL 0x040
  46. #define TEMP_AHBTO 0x044
  47. #define TEMP_ADCPNP0 0x048
  48. #define TEMP_ADCPNP1 0x04c
  49. #define TEMP_ADCPNP2 0x050
  50. #define TEMP_ADCPNP3 0x0b4
  51. #define TEMP_ADCMUX 0x054
  52. #define TEMP_ADCEN 0x060
  53. #define TEMP_PNPMUXADDR 0x064
  54. #define TEMP_ADCMUXADDR 0x068
  55. #define TEMP_ADCENADDR 0x074
  56. #define TEMP_ADCVALIDADDR 0x078
  57. #define TEMP_ADCVOLTADDR 0x07c
  58. #define TEMP_RDCTRL 0x080
  59. #define TEMP_ADCVALIDMASK 0x084
  60. #define TEMP_ADCVOLTAGESHIFT 0x088
  61. #define TEMP_ADCWRITECTRL 0x08c
  62. #define TEMP_MSR0 0x090
  63. #define TEMP_MSR1 0x094
  64. #define TEMP_MSR2 0x098
  65. #define TEMP_MSR3 0x0B8
  66. #define TEMP_SPARE0 0x0f0
  67. #define PTPCORESEL 0x400
  68. #define TEMP_MONCTL1_PERIOD_UNIT(x) ((x) & 0x3ff)
  69. #define TEMP_MONCTL2_FILTER_INTERVAL(x) (((x) & 0x3ff) << 16)
  70. #define TEMP_MONCTL2_SENSOR_INTERVAL(x) ((x) & 0x3ff)
  71. #define TEMP_AHBPOLL_ADC_POLL_INTERVAL(x) (x)
  72. #define TEMP_ADCWRITECTRL_ADC_PNP_WRITE BIT(0)
  73. #define TEMP_ADCWRITECTRL_ADC_MUX_WRITE BIT(1)
  74. #define TEMP_ADCVALIDMASK_VALID_HIGH BIT(5)
  75. #define TEMP_ADCVALIDMASK_VALID_POS(bit) (bit)
  76. /* MT8173 thermal sensors */
  77. #define MT8173_TS1 0
  78. #define MT8173_TS2 1
  79. #define MT8173_TS3 2
  80. #define MT8173_TS4 3
  81. #define MT8173_TSABB 4
  82. /* AUXADC channel 11 is used for the temperature sensors */
  83. #define MT8173_TEMP_AUXADC_CHANNEL 11
  84. /* The total number of temperature sensors in the MT8173 */
  85. #define MT8173_NUM_SENSORS 5
  86. /* The number of banks in the MT8173 */
  87. #define MT8173_NUM_ZONES 4
  88. /* The number of sensing points per bank */
  89. #define MT8173_NUM_SENSORS_PER_ZONE 4
  90. /*
  91. * Layout of the fuses providing the calibration data
  92. * These macros could be used for MT8173, MT2701, and MT2712.
  93. * MT8173 has 5 sensors and needs 5 VTS calibration data.
  94. * MT2701 has 3 sensors and needs 3 VTS calibration data.
  95. * MT2712 has 4 sensors and needs 4 VTS calibration data.
  96. */
  97. #define MT8173_CALIB_BUF0_VALID BIT(0)
  98. #define MT8173_CALIB_BUF1_ADC_GE(x) (((x) >> 22) & 0x3ff)
  99. #define MT8173_CALIB_BUF0_VTS_TS1(x) (((x) >> 17) & 0x1ff)
  100. #define MT8173_CALIB_BUF0_VTS_TS2(x) (((x) >> 8) & 0x1ff)
  101. #define MT8173_CALIB_BUF1_VTS_TS3(x) (((x) >> 0) & 0x1ff)
  102. #define MT8173_CALIB_BUF2_VTS_TS4(x) (((x) >> 23) & 0x1ff)
  103. #define MT8173_CALIB_BUF2_VTS_TSABB(x) (((x) >> 14) & 0x1ff)
  104. #define MT8173_CALIB_BUF0_DEGC_CALI(x) (((x) >> 1) & 0x3f)
  105. #define MT8173_CALIB_BUF0_O_SLOPE(x) (((x) >> 26) & 0x3f)
  106. #define MT8173_CALIB_BUF0_O_SLOPE_SIGN(x) (((x) >> 7) & 0x1)
  107. #define MT8173_CALIB_BUF1_ID(x) (((x) >> 9) & 0x1)
  108. /* MT2701 thermal sensors */
  109. #define MT2701_TS1 0
  110. #define MT2701_TS2 1
  111. #define MT2701_TSABB 2
  112. /* AUXADC channel 11 is used for the temperature sensors */
  113. #define MT2701_TEMP_AUXADC_CHANNEL 11
  114. /* The total number of temperature sensors in the MT2701 */
  115. #define MT2701_NUM_SENSORS 3
  116. /* The number of sensing points per bank */
  117. #define MT2701_NUM_SENSORS_PER_ZONE 3
  118. /* MT2712 thermal sensors */
  119. #define MT2712_TS1 0
  120. #define MT2712_TS2 1
  121. #define MT2712_TS3 2
  122. #define MT2712_TS4 3
  123. /* AUXADC channel 11 is used for the temperature sensors */
  124. #define MT2712_TEMP_AUXADC_CHANNEL 11
  125. /* The total number of temperature sensors in the MT2712 */
  126. #define MT2712_NUM_SENSORS 4
  127. /* The number of sensing points per bank */
  128. #define MT2712_NUM_SENSORS_PER_ZONE 4
  129. #define MT7622_TEMP_AUXADC_CHANNEL 11
  130. #define MT7622_NUM_SENSORS 1
  131. #define MT7622_NUM_ZONES 1
  132. #define MT7622_NUM_SENSORS_PER_ZONE 1
  133. #define MT7622_TS1 0
  134. struct mtk_thermal;
  135. struct thermal_bank_cfg {
  136. unsigned int num_sensors;
  137. const int *sensors;
  138. };
  139. struct mtk_thermal_bank {
  140. struct mtk_thermal *mt;
  141. int id;
  142. };
  143. struct mtk_thermal_data {
  144. s32 num_banks;
  145. s32 num_sensors;
  146. s32 auxadc_channel;
  147. const int *sensor_mux_values;
  148. const int *msr;
  149. const int *adcpnp;
  150. struct thermal_bank_cfg bank_data[];
  151. };
  152. struct mtk_thermal {
  153. struct device *dev;
  154. void __iomem *thermal_base;
  155. struct clk *clk_peri_therm;
  156. struct clk *clk_auxadc;
  157. /* lock: for getting and putting banks */
  158. struct mutex lock;
  159. /* Calibration values */
  160. s32 adc_ge;
  161. s32 degc_cali;
  162. s32 o_slope;
  163. s32 vts[MT8173_NUM_SENSORS];
  164. const struct mtk_thermal_data *conf;
  165. struct mtk_thermal_bank banks[];
  166. };
  167. /* MT8173 thermal sensor data */
  168. static const int mt8173_bank_data[MT8173_NUM_ZONES][3] = {
  169. { MT8173_TS2, MT8173_TS3 },
  170. { MT8173_TS2, MT8173_TS4 },
  171. { MT8173_TS1, MT8173_TS2, MT8173_TSABB },
  172. { MT8173_TS2 },
  173. };
  174. static const int mt8173_msr[MT8173_NUM_SENSORS_PER_ZONE] = {
  175. TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
  176. };
  177. static const int mt8173_adcpnp[MT8173_NUM_SENSORS_PER_ZONE] = {
  178. TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
  179. };
  180. static const int mt8173_mux_values[MT8173_NUM_SENSORS] = { 0, 1, 2, 3, 16 };
  181. /* MT2701 thermal sensor data */
  182. static const int mt2701_bank_data[MT2701_NUM_SENSORS] = {
  183. MT2701_TS1, MT2701_TS2, MT2701_TSABB
  184. };
  185. static const int mt2701_msr[MT2701_NUM_SENSORS_PER_ZONE] = {
  186. TEMP_MSR0, TEMP_MSR1, TEMP_MSR2
  187. };
  188. static const int mt2701_adcpnp[MT2701_NUM_SENSORS_PER_ZONE] = {
  189. TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2
  190. };
  191. static const int mt2701_mux_values[MT2701_NUM_SENSORS] = { 0, 1, 16 };
  192. /* MT2712 thermal sensor data */
  193. static const int mt2712_bank_data[MT2712_NUM_SENSORS] = {
  194. MT2712_TS1, MT2712_TS2, MT2712_TS3, MT2712_TS4
  195. };
  196. static const int mt2712_msr[MT2712_NUM_SENSORS_PER_ZONE] = {
  197. TEMP_MSR0, TEMP_MSR1, TEMP_MSR2, TEMP_MSR3
  198. };
  199. static const int mt2712_adcpnp[MT2712_NUM_SENSORS_PER_ZONE] = {
  200. TEMP_ADCPNP0, TEMP_ADCPNP1, TEMP_ADCPNP2, TEMP_ADCPNP3
  201. };
  202. static const int mt2712_mux_values[MT2712_NUM_SENSORS] = { 0, 1, 2, 3 };
  203. /* MT7622 thermal sensor data */
  204. static const int mt7622_bank_data[MT7622_NUM_SENSORS] = { MT7622_TS1, };
  205. static const int mt7622_msr[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_MSR0, };
  206. static const int mt7622_adcpnp[MT7622_NUM_SENSORS_PER_ZONE] = { TEMP_ADCPNP0, };
  207. static const int mt7622_mux_values[MT7622_NUM_SENSORS] = { 0, };
  208. /**
  209. * The MT8173 thermal controller has four banks. Each bank can read up to
  210. * four temperature sensors simultaneously. The MT8173 has a total of 5
  211. * temperature sensors. We use each bank to measure a certain area of the
  212. * SoC. Since TS2 is located centrally in the SoC it is influenced by multiple
  213. * areas, hence is used in different banks.
  214. *
  215. * The thermal core only gets the maximum temperature of all banks, so
  216. * the bank concept wouldn't be necessary here. However, the SVS (Smart
  217. * Voltage Scaling) unit makes its decisions based on the same bank
  218. * data, and this indeed needs the temperatures of the individual banks
  219. * for making better decisions.
  220. */
  221. static const struct mtk_thermal_data mt8173_thermal_data = {
  222. .auxadc_channel = MT8173_TEMP_AUXADC_CHANNEL,
  223. .num_banks = MT8173_NUM_ZONES,
  224. .num_sensors = MT8173_NUM_SENSORS,
  225. .bank_data = {
  226. {
  227. .num_sensors = 2,
  228. .sensors = mt8173_bank_data[0],
  229. }, {
  230. .num_sensors = 2,
  231. .sensors = mt8173_bank_data[1],
  232. }, {
  233. .num_sensors = 3,
  234. .sensors = mt8173_bank_data[2],
  235. }, {
  236. .num_sensors = 1,
  237. .sensors = mt8173_bank_data[3],
  238. },
  239. },
  240. .msr = mt8173_msr,
  241. .adcpnp = mt8173_adcpnp,
  242. .sensor_mux_values = mt8173_mux_values,
  243. };
  244. /**
  245. * The MT2701 thermal controller has one bank, which can read up to
  246. * three temperature sensors simultaneously. The MT2701 has a total of 3
  247. * temperature sensors.
  248. *
  249. * The thermal core only gets the maximum temperature of this one bank,
  250. * so the bank concept wouldn't be necessary here. However, the SVS (Smart
  251. * Voltage Scaling) unit makes its decisions based on the same bank
  252. * data.
  253. */
  254. static const struct mtk_thermal_data mt2701_thermal_data = {
  255. .auxadc_channel = MT2701_TEMP_AUXADC_CHANNEL,
  256. .num_banks = 1,
  257. .num_sensors = MT2701_NUM_SENSORS,
  258. .bank_data = {
  259. {
  260. .num_sensors = 3,
  261. .sensors = mt2701_bank_data,
  262. },
  263. },
  264. .msr = mt2701_msr,
  265. .adcpnp = mt2701_adcpnp,
  266. .sensor_mux_values = mt2701_mux_values,
  267. };
  268. /**
  269. * The MT2712 thermal controller has one bank, which can read up to
  270. * four temperature sensors simultaneously. The MT2712 has a total of 4
  271. * temperature sensors.
  272. *
  273. * The thermal core only gets the maximum temperature of this one bank,
  274. * so the bank concept wouldn't be necessary here. However, the SVS (Smart
  275. * Voltage Scaling) unit makes its decisions based on the same bank
  276. * data.
  277. */
  278. static const struct mtk_thermal_data mt2712_thermal_data = {
  279. .auxadc_channel = MT2712_TEMP_AUXADC_CHANNEL,
  280. .num_banks = 1,
  281. .num_sensors = MT2712_NUM_SENSORS,
  282. .bank_data = {
  283. {
  284. .num_sensors = 4,
  285. .sensors = mt2712_bank_data,
  286. },
  287. },
  288. .msr = mt2712_msr,
  289. .adcpnp = mt2712_adcpnp,
  290. .sensor_mux_values = mt2712_mux_values,
  291. };
  292. /*
  293. * MT7622 have only one sensing point which uses AUXADC Channel 11 for raw data
  294. * access.
  295. */
  296. static const struct mtk_thermal_data mt7622_thermal_data = {
  297. .auxadc_channel = MT7622_TEMP_AUXADC_CHANNEL,
  298. .num_banks = MT7622_NUM_ZONES,
  299. .num_sensors = MT7622_NUM_SENSORS,
  300. .bank_data = {
  301. {
  302. .num_sensors = 1,
  303. .sensors = mt7622_bank_data,
  304. },
  305. },
  306. .msr = mt7622_msr,
  307. .adcpnp = mt7622_adcpnp,
  308. .sensor_mux_values = mt7622_mux_values,
  309. };
  310. /**
  311. * raw_to_mcelsius - convert a raw ADC value to mcelsius
  312. * @mt: The thermal controller
  313. * @raw: raw ADC value
  314. *
  315. * This converts the raw ADC value to mcelsius using the SoC specific
  316. * calibration constants
  317. */
  318. static int raw_to_mcelsius(struct mtk_thermal *mt, int sensno, s32 raw)
  319. {
  320. s32 tmp;
  321. raw &= 0xfff;
  322. tmp = 203450520 << 3;
  323. tmp /= 165 + mt->o_slope;
  324. tmp /= 10000 + mt->adc_ge;
  325. tmp *= raw - mt->vts[sensno] - 3350;
  326. tmp >>= 3;
  327. return mt->degc_cali * 500 - tmp;
  328. }
  329. /**
  330. * mtk_thermal_get_bank - get bank
  331. * @bank: The bank
  332. *
  333. * The bank registers are banked, we have to select a bank in the
  334. * PTPCORESEL register to access it.
  335. */
  336. static void mtk_thermal_get_bank(struct mtk_thermal_bank *bank)
  337. {
  338. struct mtk_thermal *mt = bank->mt;
  339. u32 val;
  340. mutex_lock(&mt->lock);
  341. val = readl(mt->thermal_base + PTPCORESEL);
  342. val &= ~0xf;
  343. val |= bank->id;
  344. writel(val, mt->thermal_base + PTPCORESEL);
  345. }
  346. /**
  347. * mtk_thermal_put_bank - release bank
  348. * @bank: The bank
  349. *
  350. * release a bank previously taken with mtk_thermal_get_bank,
  351. */
  352. static void mtk_thermal_put_bank(struct mtk_thermal_bank *bank)
  353. {
  354. struct mtk_thermal *mt = bank->mt;
  355. mutex_unlock(&mt->lock);
  356. }
  357. /**
  358. * mtk_thermal_bank_temperature - get the temperature of a bank
  359. * @bank: The bank
  360. *
  361. * The temperature of a bank is considered the maximum temperature of
  362. * the sensors associated to the bank.
  363. */
  364. static int mtk_thermal_bank_temperature(struct mtk_thermal_bank *bank)
  365. {
  366. struct mtk_thermal *mt = bank->mt;
  367. const struct mtk_thermal_data *conf = mt->conf;
  368. int i, temp = INT_MIN, max = INT_MIN;
  369. u32 raw;
  370. for (i = 0; i < conf->bank_data[bank->id].num_sensors; i++) {
  371. raw = readl(mt->thermal_base +
  372. conf->msr[conf->bank_data[bank->id].sensors[i]]);
  373. temp = raw_to_mcelsius(mt,
  374. conf->bank_data[bank->id].sensors[i],
  375. raw);
  376. /*
  377. * The first read of a sensor often contains very high bogus
  378. * temperature value. Filter these out so that the system does
  379. * not immediately shut down.
  380. */
  381. if (temp > 200000)
  382. temp = 0;
  383. if (temp > max)
  384. max = temp;
  385. }
  386. return max;
  387. }
  388. static int mtk_read_temp(void *data, int *temperature)
  389. {
  390. struct mtk_thermal *mt = data;
  391. int i;
  392. int tempmax = INT_MIN;
  393. for (i = 0; i < mt->conf->num_banks; i++) {
  394. struct mtk_thermal_bank *bank = &mt->banks[i];
  395. mtk_thermal_get_bank(bank);
  396. tempmax = max(tempmax, mtk_thermal_bank_temperature(bank));
  397. mtk_thermal_put_bank(bank);
  398. }
  399. *temperature = tempmax;
  400. return 0;
  401. }
  402. static const struct thermal_zone_of_device_ops mtk_thermal_ops = {
  403. .get_temp = mtk_read_temp,
  404. };
  405. static void mtk_thermal_init_bank(struct mtk_thermal *mt, int num,
  406. u32 apmixed_phys_base, u32 auxadc_phys_base)
  407. {
  408. struct mtk_thermal_bank *bank = &mt->banks[num];
  409. const struct mtk_thermal_data *conf = mt->conf;
  410. int i;
  411. bank->id = num;
  412. bank->mt = mt;
  413. mtk_thermal_get_bank(bank);
  414. /* bus clock 66M counting unit is 12 * 15.15ns * 256 = 46.540us */
  415. writel(TEMP_MONCTL1_PERIOD_UNIT(12), mt->thermal_base + TEMP_MONCTL1);
  416. /*
  417. * filt interval is 1 * 46.540us = 46.54us,
  418. * sen interval is 429 * 46.540us = 19.96ms
  419. */
  420. writel(TEMP_MONCTL2_FILTER_INTERVAL(1) |
  421. TEMP_MONCTL2_SENSOR_INTERVAL(429),
  422. mt->thermal_base + TEMP_MONCTL2);
  423. /* poll is set to 10u */
  424. writel(TEMP_AHBPOLL_ADC_POLL_INTERVAL(768),
  425. mt->thermal_base + TEMP_AHBPOLL);
  426. /* temperature sampling control, 1 sample */
  427. writel(0x0, mt->thermal_base + TEMP_MSRCTL0);
  428. /* exceed this polling time, IRQ would be inserted */
  429. writel(0xffffffff, mt->thermal_base + TEMP_AHBTO);
  430. /* number of interrupts per event, 1 is enough */
  431. writel(0x0, mt->thermal_base + TEMP_MONIDET0);
  432. writel(0x0, mt->thermal_base + TEMP_MONIDET1);
  433. /*
  434. * The MT8173 thermal controller does not have its own ADC. Instead it
  435. * uses AHB bus accesses to control the AUXADC. To do this the thermal
  436. * controller has to be programmed with the physical addresses of the
  437. * AUXADC registers and with the various bit positions in the AUXADC.
  438. * Also the thermal controller controls a mux in the APMIXEDSYS register
  439. * space.
  440. */
  441. /*
  442. * this value will be stored to TEMP_PNPMUXADDR (TEMP_SPARE0)
  443. * automatically by hw
  444. */
  445. writel(BIT(conf->auxadc_channel), mt->thermal_base + TEMP_ADCMUX);
  446. /* AHB address for auxadc mux selection */
  447. writel(auxadc_phys_base + AUXADC_CON1_CLR_V,
  448. mt->thermal_base + TEMP_ADCMUXADDR);
  449. /* AHB address for pnp sensor mux selection */
  450. writel(apmixed_phys_base + APMIXED_SYS_TS_CON1,
  451. mt->thermal_base + TEMP_PNPMUXADDR);
  452. /* AHB value for auxadc enable */
  453. writel(BIT(conf->auxadc_channel), mt->thermal_base + TEMP_ADCEN);
  454. /* AHB address for auxadc enable (channel 0 immediate mode selected) */
  455. writel(auxadc_phys_base + AUXADC_CON1_SET_V,
  456. mt->thermal_base + TEMP_ADCENADDR);
  457. /* AHB address for auxadc valid bit */
  458. writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
  459. mt->thermal_base + TEMP_ADCVALIDADDR);
  460. /* AHB address for auxadc voltage output */
  461. writel(auxadc_phys_base + AUXADC_DATA(conf->auxadc_channel),
  462. mt->thermal_base + TEMP_ADCVOLTADDR);
  463. /* read valid & voltage are at the same register */
  464. writel(0x0, mt->thermal_base + TEMP_RDCTRL);
  465. /* indicate where the valid bit is */
  466. writel(TEMP_ADCVALIDMASK_VALID_HIGH | TEMP_ADCVALIDMASK_VALID_POS(12),
  467. mt->thermal_base + TEMP_ADCVALIDMASK);
  468. /* no shift */
  469. writel(0x0, mt->thermal_base + TEMP_ADCVOLTAGESHIFT);
  470. /* enable auxadc mux write transaction */
  471. writel(TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
  472. mt->thermal_base + TEMP_ADCWRITECTRL);
  473. for (i = 0; i < conf->bank_data[num].num_sensors; i++)
  474. writel(conf->sensor_mux_values[conf->bank_data[num].sensors[i]],
  475. mt->thermal_base +
  476. conf->adcpnp[conf->bank_data[num].sensors[i]]);
  477. writel((1 << conf->bank_data[num].num_sensors) - 1,
  478. mt->thermal_base + TEMP_MONCTL0);
  479. writel(TEMP_ADCWRITECTRL_ADC_PNP_WRITE |
  480. TEMP_ADCWRITECTRL_ADC_MUX_WRITE,
  481. mt->thermal_base + TEMP_ADCWRITECTRL);
  482. mtk_thermal_put_bank(bank);
  483. }
  484. static u64 of_get_phys_base(struct device_node *np)
  485. {
  486. u64 size64;
  487. const __be32 *regaddr_p;
  488. regaddr_p = of_get_address(np, 0, &size64, NULL);
  489. if (!regaddr_p)
  490. return OF_BAD_ADDR;
  491. return of_translate_address(np, regaddr_p);
  492. }
  493. static int mtk_thermal_get_calibration_data(struct device *dev,
  494. struct mtk_thermal *mt)
  495. {
  496. struct nvmem_cell *cell;
  497. u32 *buf;
  498. size_t len;
  499. int i, ret = 0;
  500. /* Start with default values */
  501. mt->adc_ge = 512;
  502. for (i = 0; i < mt->conf->num_sensors; i++)
  503. mt->vts[i] = 260;
  504. mt->degc_cali = 40;
  505. mt->o_slope = 0;
  506. cell = nvmem_cell_get(dev, "calibration-data");
  507. if (IS_ERR(cell)) {
  508. if (PTR_ERR(cell) == -EPROBE_DEFER)
  509. return PTR_ERR(cell);
  510. return 0;
  511. }
  512. buf = (u32 *)nvmem_cell_read(cell, &len);
  513. nvmem_cell_put(cell);
  514. if (IS_ERR(buf))
  515. return PTR_ERR(buf);
  516. if (len < 3 * sizeof(u32)) {
  517. dev_warn(dev, "invalid calibration data\n");
  518. ret = -EINVAL;
  519. goto out;
  520. }
  521. if (buf[0] & MT8173_CALIB_BUF0_VALID) {
  522. mt->adc_ge = MT8173_CALIB_BUF1_ADC_GE(buf[1]);
  523. mt->vts[MT8173_TS1] = MT8173_CALIB_BUF0_VTS_TS1(buf[0]);
  524. mt->vts[MT8173_TS2] = MT8173_CALIB_BUF0_VTS_TS2(buf[0]);
  525. mt->vts[MT8173_TS3] = MT8173_CALIB_BUF1_VTS_TS3(buf[1]);
  526. mt->vts[MT8173_TS4] = MT8173_CALIB_BUF2_VTS_TS4(buf[2]);
  527. mt->vts[MT8173_TSABB] = MT8173_CALIB_BUF2_VTS_TSABB(buf[2]);
  528. mt->degc_cali = MT8173_CALIB_BUF0_DEGC_CALI(buf[0]);
  529. if (MT8173_CALIB_BUF1_ID(buf[1]) &
  530. MT8173_CALIB_BUF0_O_SLOPE_SIGN(buf[0]))
  531. mt->o_slope = -MT8173_CALIB_BUF0_O_SLOPE(buf[0]);
  532. else
  533. mt->o_slope = MT8173_CALIB_BUF0_O_SLOPE(buf[0]);
  534. } else {
  535. dev_info(dev, "Device not calibrated, using default calibration values\n");
  536. }
  537. out:
  538. kfree(buf);
  539. return ret;
  540. }
  541. static const struct of_device_id mtk_thermal_of_match[] = {
  542. {
  543. .compatible = "mediatek,mt8173-thermal",
  544. .data = (void *)&mt8173_thermal_data,
  545. },
  546. {
  547. .compatible = "mediatek,mt2701-thermal",
  548. .data = (void *)&mt2701_thermal_data,
  549. },
  550. {
  551. .compatible = "mediatek,mt2712-thermal",
  552. .data = (void *)&mt2712_thermal_data,
  553. },
  554. {
  555. .compatible = "mediatek,mt7622-thermal",
  556. .data = (void *)&mt7622_thermal_data,
  557. }, {
  558. },
  559. };
  560. MODULE_DEVICE_TABLE(of, mtk_thermal_of_match);
  561. static int mtk_thermal_probe(struct platform_device *pdev)
  562. {
  563. int ret, i;
  564. struct device_node *auxadc, *apmixedsys, *np = pdev->dev.of_node;
  565. struct mtk_thermal *mt;
  566. struct resource *res;
  567. u64 auxadc_phys_base, apmixed_phys_base;
  568. struct thermal_zone_device *tzdev;
  569. mt = devm_kzalloc(&pdev->dev, sizeof(*mt), GFP_KERNEL);
  570. if (!mt)
  571. return -ENOMEM;
  572. mt->conf = of_device_get_match_data(&pdev->dev);
  573. mt->clk_peri_therm = devm_clk_get(&pdev->dev, "therm");
  574. if (IS_ERR(mt->clk_peri_therm))
  575. return PTR_ERR(mt->clk_peri_therm);
  576. mt->clk_auxadc = devm_clk_get(&pdev->dev, "auxadc");
  577. if (IS_ERR(mt->clk_auxadc))
  578. return PTR_ERR(mt->clk_auxadc);
  579. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  580. mt->thermal_base = devm_ioremap_resource(&pdev->dev, res);
  581. if (IS_ERR(mt->thermal_base))
  582. return PTR_ERR(mt->thermal_base);
  583. ret = mtk_thermal_get_calibration_data(&pdev->dev, mt);
  584. if (ret)
  585. return ret;
  586. mutex_init(&mt->lock);
  587. mt->dev = &pdev->dev;
  588. auxadc = of_parse_phandle(np, "mediatek,auxadc", 0);
  589. if (!auxadc) {
  590. dev_err(&pdev->dev, "missing auxadc node\n");
  591. return -ENODEV;
  592. }
  593. auxadc_phys_base = of_get_phys_base(auxadc);
  594. of_node_put(auxadc);
  595. if (auxadc_phys_base == OF_BAD_ADDR) {
  596. dev_err(&pdev->dev, "Can't get auxadc phys address\n");
  597. return -EINVAL;
  598. }
  599. apmixedsys = of_parse_phandle(np, "mediatek,apmixedsys", 0);
  600. if (!apmixedsys) {
  601. dev_err(&pdev->dev, "missing apmixedsys node\n");
  602. return -ENODEV;
  603. }
  604. apmixed_phys_base = of_get_phys_base(apmixedsys);
  605. of_node_put(apmixedsys);
  606. if (apmixed_phys_base == OF_BAD_ADDR) {
  607. dev_err(&pdev->dev, "Can't get auxadc phys address\n");
  608. return -EINVAL;
  609. }
  610. ret = device_reset(&pdev->dev);
  611. if (ret)
  612. return ret;
  613. ret = clk_prepare_enable(mt->clk_auxadc);
  614. if (ret) {
  615. dev_err(&pdev->dev, "Can't enable auxadc clk: %d\n", ret);
  616. return ret;
  617. }
  618. ret = clk_prepare_enable(mt->clk_peri_therm);
  619. if (ret) {
  620. dev_err(&pdev->dev, "Can't enable peri clk: %d\n", ret);
  621. goto err_disable_clk_auxadc;
  622. }
  623. for (i = 0; i < mt->conf->num_banks; i++)
  624. mtk_thermal_init_bank(mt, i, apmixed_phys_base,
  625. auxadc_phys_base);
  626. platform_set_drvdata(pdev, mt);
  627. tzdev = devm_thermal_zone_of_sensor_register(&pdev->dev, 0, mt,
  628. &mtk_thermal_ops);
  629. if (IS_ERR(tzdev)) {
  630. ret = PTR_ERR(tzdev);
  631. goto err_disable_clk_peri_therm;
  632. }
  633. return 0;
  634. err_disable_clk_peri_therm:
  635. clk_disable_unprepare(mt->clk_peri_therm);
  636. err_disable_clk_auxadc:
  637. clk_disable_unprepare(mt->clk_auxadc);
  638. return ret;
  639. }
  640. static int mtk_thermal_remove(struct platform_device *pdev)
  641. {
  642. struct mtk_thermal *mt = platform_get_drvdata(pdev);
  643. clk_disable_unprepare(mt->clk_peri_therm);
  644. clk_disable_unprepare(mt->clk_auxadc);
  645. return 0;
  646. }
  647. static struct platform_driver mtk_thermal_driver = {
  648. .probe = mtk_thermal_probe,
  649. .remove = mtk_thermal_remove,
  650. .driver = {
  651. .name = "mtk-thermal",
  652. .of_match_table = mtk_thermal_of_match,
  653. },
  654. };
  655. module_platform_driver(mtk_thermal_driver);
  656. MODULE_AUTHOR("Louis Yu <louis.yu@mediatek.com>");
  657. MODULE_AUTHOR("Dawei Chien <dawei.chien@mediatek.com>");
  658. MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
  659. MODULE_AUTHOR("Hanyi Wu <hanyi.wu@mediatek.com>");
  660. MODULE_DESCRIPTION("Mediatek thermal driver");
  661. MODULE_LICENSE("GPL v2");