mtk_thermal.c 18 KB

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