mtk_thermal.c 21 KB

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