exynos_tmu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * Donggeun Kim <dg77.kim@samsung.com>
  6. * Amit Daniel Kachhap <amit.kachhap@linaro.org>
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/io.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/regulator/consumer.h>
  32. #include "exynos_thermal_common.h"
  33. #include "exynos_tmu.h"
  34. #include "exynos_tmu_data.h"
  35. /**
  36. * struct exynos_tmu_data : A structure to hold the private data of the TMU
  37. driver
  38. * @id: identifier of the one instance of the TMU controller.
  39. * @pdata: pointer to the tmu platform/configuration data
  40. * @base: base address of the single instance of the TMU controller.
  41. * @base_second: base address of the common registers of the TMU controller.
  42. * @irq: irq number of the TMU controller.
  43. * @soc: id of the SOC type.
  44. * @irq_work: pointer to the irq work structure.
  45. * @lock: lock to implement synchronization.
  46. * @clk: pointer to the clock structure.
  47. * @clk_sec: pointer to the clock structure for accessing the base_second.
  48. * @temp_error1: fused value of the first point trim.
  49. * @temp_error2: fused value of the second point trim.
  50. * @regulator: pointer to the TMU regulator structure.
  51. * @reg_conf: pointer to structure to register with core thermal.
  52. */
  53. struct exynos_tmu_data {
  54. int id;
  55. struct exynos_tmu_platform_data *pdata;
  56. void __iomem *base;
  57. void __iomem *base_second;
  58. int irq;
  59. enum soc_type soc;
  60. struct work_struct irq_work;
  61. struct mutex lock;
  62. struct clk *clk, *clk_sec;
  63. u8 temp_error1, temp_error2;
  64. struct regulator *regulator;
  65. struct thermal_sensor_conf *reg_conf;
  66. };
  67. /*
  68. * TMU treats temperature as a mapped temperature code.
  69. * The temperature is converted differently depending on the calibration type.
  70. */
  71. static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
  72. {
  73. struct exynos_tmu_platform_data *pdata = data->pdata;
  74. int temp_code;
  75. switch (pdata->cal_type) {
  76. case TYPE_TWO_POINT_TRIMMING:
  77. temp_code = (temp - pdata->first_point_trim) *
  78. (data->temp_error2 - data->temp_error1) /
  79. (pdata->second_point_trim - pdata->first_point_trim) +
  80. data->temp_error1;
  81. break;
  82. case TYPE_ONE_POINT_TRIMMING:
  83. temp_code = temp + data->temp_error1 - pdata->first_point_trim;
  84. break;
  85. default:
  86. temp_code = temp + pdata->default_temp_offset;
  87. break;
  88. }
  89. return temp_code;
  90. }
  91. /*
  92. * Calculate a temperature value from a temperature code.
  93. * The unit of the temperature is degree Celsius.
  94. */
  95. static int code_to_temp(struct exynos_tmu_data *data, u8 temp_code)
  96. {
  97. struct exynos_tmu_platform_data *pdata = data->pdata;
  98. int temp;
  99. switch (pdata->cal_type) {
  100. case TYPE_TWO_POINT_TRIMMING:
  101. temp = (temp_code - data->temp_error1) *
  102. (pdata->second_point_trim - pdata->first_point_trim) /
  103. (data->temp_error2 - data->temp_error1) +
  104. pdata->first_point_trim;
  105. break;
  106. case TYPE_ONE_POINT_TRIMMING:
  107. temp = temp_code - data->temp_error1 + pdata->first_point_trim;
  108. break;
  109. default:
  110. temp = temp_code - pdata->default_temp_offset;
  111. break;
  112. }
  113. return temp;
  114. }
  115. static void exynos_tmu_clear_irqs(struct exynos_tmu_data *data)
  116. {
  117. const struct exynos_tmu_registers *reg = data->pdata->registers;
  118. unsigned int val_irq;
  119. val_irq = readl(data->base + reg->tmu_intstat);
  120. /*
  121. * Clear the interrupts. Please note that the documentation for
  122. * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
  123. * states that INTCLEAR register has a different placing of bits
  124. * responsible for FALL IRQs than INTSTAT register. Exynos5420
  125. * and Exynos5440 documentation is correct (Exynos4210 doesn't
  126. * support FALL IRQs at all).
  127. */
  128. writel(val_irq, data->base + reg->tmu_intclear);
  129. }
  130. static int exynos_tmu_initialize(struct platform_device *pdev)
  131. {
  132. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  133. struct exynos_tmu_platform_data *pdata = data->pdata;
  134. const struct exynos_tmu_registers *reg = pdata->registers;
  135. unsigned int status, trim_info = 0, con, ctrl;
  136. unsigned int rising_threshold = 0, falling_threshold = 0;
  137. int ret = 0, threshold_code, i;
  138. mutex_lock(&data->lock);
  139. clk_enable(data->clk);
  140. if (!IS_ERR(data->clk_sec))
  141. clk_enable(data->clk_sec);
  142. if (TMU_SUPPORTS(pdata, READY_STATUS)) {
  143. status = readb(data->base + reg->tmu_status);
  144. if (!status) {
  145. ret = -EBUSY;
  146. goto out;
  147. }
  148. }
  149. if (TMU_SUPPORTS(pdata, TRIM_RELOAD)) {
  150. for (i = 0; i < reg->triminfo_ctrl_count; i++) {
  151. if (pdata->triminfo_reload[i]) {
  152. ctrl = readl(data->base +
  153. reg->triminfo_ctrl[i]);
  154. ctrl |= pdata->triminfo_reload[i];
  155. writel(ctrl, data->base +
  156. reg->triminfo_ctrl[i]);
  157. }
  158. }
  159. }
  160. /* Save trimming info in order to perform calibration */
  161. if (data->soc == SOC_ARCH_EXYNOS5440) {
  162. /*
  163. * For exynos5440 soc triminfo value is swapped between TMU0 and
  164. * TMU2, so the below logic is needed.
  165. */
  166. switch (data->id) {
  167. case 0:
  168. trim_info = readl(data->base +
  169. EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
  170. break;
  171. case 1:
  172. trim_info = readl(data->base + reg->triminfo_data);
  173. break;
  174. case 2:
  175. trim_info = readl(data->base -
  176. EXYNOS5440_EFUSE_SWAP_OFFSET + reg->triminfo_data);
  177. }
  178. } else {
  179. /* On exynos5420 the triminfo register is in the shared space */
  180. if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
  181. trim_info = readl(data->base_second +
  182. reg->triminfo_data);
  183. else
  184. trim_info = readl(data->base + reg->triminfo_data);
  185. }
  186. data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
  187. data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
  188. EXYNOS_TMU_TEMP_MASK);
  189. if (!data->temp_error1 ||
  190. (pdata->min_efuse_value > data->temp_error1) ||
  191. (data->temp_error1 > pdata->max_efuse_value))
  192. data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
  193. if (!data->temp_error2)
  194. data->temp_error2 =
  195. (pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
  196. EXYNOS_TMU_TEMP_MASK;
  197. rising_threshold = readl(data->base + reg->threshold_th0);
  198. if (data->soc == SOC_ARCH_EXYNOS4210) {
  199. /* Write temperature code for threshold */
  200. threshold_code = temp_to_code(data, pdata->threshold);
  201. writeb(threshold_code,
  202. data->base + reg->threshold_temp);
  203. for (i = 0; i < pdata->non_hw_trigger_levels; i++)
  204. writeb(pdata->trigger_levels[i], data->base +
  205. reg->threshold_th0 + i * sizeof(reg->threshold_th0));
  206. exynos_tmu_clear_irqs(data);
  207. } else {
  208. /* Write temperature code for rising and falling threshold */
  209. for (i = 0; i < pdata->non_hw_trigger_levels; i++) {
  210. threshold_code = temp_to_code(data,
  211. pdata->trigger_levels[i]);
  212. rising_threshold &= ~(0xff << 8 * i);
  213. rising_threshold |= threshold_code << 8 * i;
  214. if (pdata->threshold_falling) {
  215. threshold_code = temp_to_code(data,
  216. pdata->trigger_levels[i] -
  217. pdata->threshold_falling);
  218. falling_threshold |= threshold_code << 8 * i;
  219. }
  220. }
  221. writel(rising_threshold,
  222. data->base + reg->threshold_th0);
  223. writel(falling_threshold,
  224. data->base + reg->threshold_th1);
  225. exynos_tmu_clear_irqs(data);
  226. /* if last threshold limit is also present */
  227. i = pdata->max_trigger_level - 1;
  228. if (pdata->trigger_levels[i] &&
  229. (pdata->trigger_type[i] == HW_TRIP)) {
  230. threshold_code = temp_to_code(data,
  231. pdata->trigger_levels[i]);
  232. if (i == EXYNOS_MAX_TRIGGER_PER_REG - 1) {
  233. /* 1-4 level to be assigned in th0 reg */
  234. rising_threshold &= ~(0xff << 8 * i);
  235. rising_threshold |= threshold_code << 8 * i;
  236. writel(rising_threshold,
  237. data->base + reg->threshold_th0);
  238. } else if (i == EXYNOS_MAX_TRIGGER_PER_REG) {
  239. /* 5th level to be assigned in th2 reg */
  240. rising_threshold =
  241. threshold_code << reg->threshold_th3_l0_shift;
  242. writel(rising_threshold,
  243. data->base + reg->threshold_th2);
  244. }
  245. con = readl(data->base + reg->tmu_ctrl);
  246. con |= (1 << reg->therm_trip_en_shift);
  247. writel(con, data->base + reg->tmu_ctrl);
  248. }
  249. }
  250. /*Clear the PMIN in the common TMU register*/
  251. if (reg->tmu_pmin && !data->id)
  252. writel(0, data->base_second + reg->tmu_pmin);
  253. out:
  254. clk_disable(data->clk);
  255. mutex_unlock(&data->lock);
  256. if (!IS_ERR(data->clk_sec))
  257. clk_disable(data->clk_sec);
  258. return ret;
  259. }
  260. static void exynos_tmu_control(struct platform_device *pdev, bool on)
  261. {
  262. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  263. struct exynos_tmu_platform_data *pdata = data->pdata;
  264. const struct exynos_tmu_registers *reg = pdata->registers;
  265. unsigned int con, interrupt_en;
  266. mutex_lock(&data->lock);
  267. clk_enable(data->clk);
  268. con = readl(data->base + reg->tmu_ctrl);
  269. if (pdata->test_mux)
  270. con |= (pdata->test_mux << reg->test_mux_addr_shift);
  271. con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
  272. con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
  273. con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
  274. con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
  275. if (pdata->noise_cancel_mode) {
  276. con &= ~(reg->therm_trip_mode_mask <<
  277. reg->therm_trip_mode_shift);
  278. con |= (pdata->noise_cancel_mode << reg->therm_trip_mode_shift);
  279. }
  280. if (on) {
  281. con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
  282. interrupt_en =
  283. pdata->trigger_enable[3] << reg->inten_rise3_shift |
  284. pdata->trigger_enable[2] << reg->inten_rise2_shift |
  285. pdata->trigger_enable[1] << reg->inten_rise1_shift |
  286. pdata->trigger_enable[0] << reg->inten_rise0_shift;
  287. if (TMU_SUPPORTS(pdata, FALLING_TRIP))
  288. interrupt_en |=
  289. interrupt_en << reg->inten_fall0_shift;
  290. } else {
  291. con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
  292. interrupt_en = 0; /* Disable all interrupts */
  293. }
  294. writel(interrupt_en, data->base + reg->tmu_inten);
  295. writel(con, data->base + reg->tmu_ctrl);
  296. clk_disable(data->clk);
  297. mutex_unlock(&data->lock);
  298. }
  299. static int exynos_tmu_read(struct exynos_tmu_data *data)
  300. {
  301. struct exynos_tmu_platform_data *pdata = data->pdata;
  302. const struct exynos_tmu_registers *reg = pdata->registers;
  303. u8 temp_code;
  304. int temp;
  305. mutex_lock(&data->lock);
  306. clk_enable(data->clk);
  307. temp_code = readb(data->base + reg->tmu_cur_temp);
  308. if (data->soc == SOC_ARCH_EXYNOS4210)
  309. /* temp_code should range between 75 and 175 */
  310. if (temp_code < 75 || temp_code > 175) {
  311. temp = -ENODATA;
  312. goto out;
  313. }
  314. temp = code_to_temp(data, temp_code);
  315. out:
  316. clk_disable(data->clk);
  317. mutex_unlock(&data->lock);
  318. return temp;
  319. }
  320. #ifdef CONFIG_THERMAL_EMULATION
  321. static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
  322. {
  323. struct exynos_tmu_data *data = drv_data;
  324. struct exynos_tmu_platform_data *pdata = data->pdata;
  325. const struct exynos_tmu_registers *reg = pdata->registers;
  326. unsigned int val;
  327. int ret = -EINVAL;
  328. if (!TMU_SUPPORTS(pdata, EMULATION))
  329. goto out;
  330. if (temp && temp < MCELSIUS)
  331. goto out;
  332. mutex_lock(&data->lock);
  333. clk_enable(data->clk);
  334. val = readl(data->base + reg->emul_con);
  335. if (temp) {
  336. temp /= MCELSIUS;
  337. if (TMU_SUPPORTS(pdata, EMUL_TIME)) {
  338. val &= ~(EXYNOS_EMUL_TIME_MASK << reg->emul_time_shift);
  339. val |= (EXYNOS_EMUL_TIME << reg->emul_time_shift);
  340. }
  341. val &= ~(EXYNOS_EMUL_DATA_MASK << reg->emul_temp_shift);
  342. val |= (temp_to_code(data, temp) << reg->emul_temp_shift) |
  343. EXYNOS_EMUL_ENABLE;
  344. } else {
  345. val &= ~EXYNOS_EMUL_ENABLE;
  346. }
  347. writel(val, data->base + reg->emul_con);
  348. clk_disable(data->clk);
  349. mutex_unlock(&data->lock);
  350. return 0;
  351. out:
  352. return ret;
  353. }
  354. #else
  355. static int exynos_tmu_set_emulation(void *drv_data, unsigned long temp)
  356. { return -EINVAL; }
  357. #endif/*CONFIG_THERMAL_EMULATION*/
  358. static void exynos_tmu_work(struct work_struct *work)
  359. {
  360. struct exynos_tmu_data *data = container_of(work,
  361. struct exynos_tmu_data, irq_work);
  362. struct exynos_tmu_platform_data *pdata = data->pdata;
  363. const struct exynos_tmu_registers *reg = pdata->registers;
  364. unsigned int val_type;
  365. if (!IS_ERR(data->clk_sec))
  366. clk_enable(data->clk_sec);
  367. /* Find which sensor generated this interrupt */
  368. if (reg->tmu_irqstatus) {
  369. val_type = readl(data->base_second + reg->tmu_irqstatus);
  370. if (!((val_type >> data->id) & 0x1))
  371. goto out;
  372. }
  373. if (!IS_ERR(data->clk_sec))
  374. clk_disable(data->clk_sec);
  375. exynos_report_trigger(data->reg_conf);
  376. mutex_lock(&data->lock);
  377. clk_enable(data->clk);
  378. /* TODO: take action based on particular interrupt */
  379. exynos_tmu_clear_irqs(data);
  380. clk_disable(data->clk);
  381. mutex_unlock(&data->lock);
  382. out:
  383. enable_irq(data->irq);
  384. }
  385. static irqreturn_t exynos_tmu_irq(int irq, void *id)
  386. {
  387. struct exynos_tmu_data *data = id;
  388. disable_irq_nosync(irq);
  389. schedule_work(&data->irq_work);
  390. return IRQ_HANDLED;
  391. }
  392. static const struct of_device_id exynos_tmu_match[] = {
  393. {
  394. .compatible = "samsung,exynos3250-tmu",
  395. .data = (void *)EXYNOS3250_TMU_DRV_DATA,
  396. },
  397. {
  398. .compatible = "samsung,exynos4210-tmu",
  399. .data = (void *)EXYNOS4210_TMU_DRV_DATA,
  400. },
  401. {
  402. .compatible = "samsung,exynos4412-tmu",
  403. .data = (void *)EXYNOS4412_TMU_DRV_DATA,
  404. },
  405. {
  406. .compatible = "samsung,exynos5250-tmu",
  407. .data = (void *)EXYNOS5250_TMU_DRV_DATA,
  408. },
  409. {
  410. .compatible = "samsung,exynos5260-tmu",
  411. .data = (void *)EXYNOS5260_TMU_DRV_DATA,
  412. },
  413. {
  414. .compatible = "samsung,exynos5420-tmu",
  415. .data = (void *)EXYNOS5420_TMU_DRV_DATA,
  416. },
  417. {
  418. .compatible = "samsung,exynos5420-tmu-ext-triminfo",
  419. .data = (void *)EXYNOS5420_TMU_DRV_DATA,
  420. },
  421. {
  422. .compatible = "samsung,exynos5440-tmu",
  423. .data = (void *)EXYNOS5440_TMU_DRV_DATA,
  424. },
  425. {},
  426. };
  427. MODULE_DEVICE_TABLE(of, exynos_tmu_match);
  428. static inline struct exynos_tmu_platform_data *exynos_get_driver_data(
  429. struct platform_device *pdev, int id)
  430. {
  431. struct exynos_tmu_init_data *data_table;
  432. struct exynos_tmu_platform_data *tmu_data;
  433. const struct of_device_id *match;
  434. match = of_match_node(exynos_tmu_match, pdev->dev.of_node);
  435. if (!match)
  436. return NULL;
  437. data_table = (struct exynos_tmu_init_data *) match->data;
  438. if (!data_table || id >= data_table->tmu_count)
  439. return NULL;
  440. tmu_data = data_table->tmu_data;
  441. return (struct exynos_tmu_platform_data *) (tmu_data + id);
  442. }
  443. static int exynos_map_dt_data(struct platform_device *pdev)
  444. {
  445. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  446. struct exynos_tmu_platform_data *pdata;
  447. struct resource res;
  448. int ret;
  449. if (!data || !pdev->dev.of_node)
  450. return -ENODEV;
  451. /*
  452. * Try enabling the regulator if found
  453. * TODO: Add regulator as an SOC feature, so that regulator enable
  454. * is a compulsory call.
  455. */
  456. data->regulator = devm_regulator_get(&pdev->dev, "vtmu");
  457. if (!IS_ERR(data->regulator)) {
  458. ret = regulator_enable(data->regulator);
  459. if (ret) {
  460. dev_err(&pdev->dev, "failed to enable vtmu\n");
  461. return ret;
  462. }
  463. } else {
  464. dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
  465. }
  466. data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
  467. if (data->id < 0)
  468. data->id = 0;
  469. data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  470. if (data->irq <= 0) {
  471. dev_err(&pdev->dev, "failed to get IRQ\n");
  472. return -ENODEV;
  473. }
  474. if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
  475. dev_err(&pdev->dev, "failed to get Resource 0\n");
  476. return -ENODEV;
  477. }
  478. data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
  479. if (!data->base) {
  480. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  481. return -EADDRNOTAVAIL;
  482. }
  483. pdata = exynos_get_driver_data(pdev, data->id);
  484. if (!pdata) {
  485. dev_err(&pdev->dev, "No platform init data supplied.\n");
  486. return -ENODEV;
  487. }
  488. data->pdata = pdata;
  489. /*
  490. * Check if the TMU shares some registers and then try to map the
  491. * memory of common registers.
  492. */
  493. if (!TMU_SUPPORTS(pdata, ADDRESS_MULTIPLE))
  494. return 0;
  495. if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
  496. dev_err(&pdev->dev, "failed to get Resource 1\n");
  497. return -ENODEV;
  498. }
  499. data->base_second = devm_ioremap(&pdev->dev, res.start,
  500. resource_size(&res));
  501. if (!data->base_second) {
  502. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  503. return -ENOMEM;
  504. }
  505. return 0;
  506. }
  507. static int exynos_tmu_probe(struct platform_device *pdev)
  508. {
  509. struct exynos_tmu_data *data;
  510. struct exynos_tmu_platform_data *pdata;
  511. struct thermal_sensor_conf *sensor_conf;
  512. int ret, i;
  513. data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
  514. GFP_KERNEL);
  515. if (!data)
  516. return -ENOMEM;
  517. platform_set_drvdata(pdev, data);
  518. mutex_init(&data->lock);
  519. ret = exynos_map_dt_data(pdev);
  520. if (ret)
  521. return ret;
  522. pdata = data->pdata;
  523. INIT_WORK(&data->irq_work, exynos_tmu_work);
  524. data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
  525. if (IS_ERR(data->clk)) {
  526. dev_err(&pdev->dev, "Failed to get clock\n");
  527. return PTR_ERR(data->clk);
  528. }
  529. data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
  530. if (IS_ERR(data->clk_sec)) {
  531. if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
  532. dev_err(&pdev->dev, "Failed to get triminfo clock\n");
  533. return PTR_ERR(data->clk_sec);
  534. }
  535. } else {
  536. ret = clk_prepare(data->clk_sec);
  537. if (ret) {
  538. dev_err(&pdev->dev, "Failed to get clock\n");
  539. return ret;
  540. }
  541. }
  542. ret = clk_prepare(data->clk);
  543. if (ret) {
  544. dev_err(&pdev->dev, "Failed to get clock\n");
  545. goto err_clk_sec;
  546. }
  547. if (pdata->type == SOC_ARCH_EXYNOS3250 ||
  548. pdata->type == SOC_ARCH_EXYNOS4210 ||
  549. pdata->type == SOC_ARCH_EXYNOS4412 ||
  550. pdata->type == SOC_ARCH_EXYNOS5250 ||
  551. pdata->type == SOC_ARCH_EXYNOS5260 ||
  552. pdata->type == SOC_ARCH_EXYNOS5420_TRIMINFO ||
  553. pdata->type == SOC_ARCH_EXYNOS5440)
  554. data->soc = pdata->type;
  555. else {
  556. ret = -EINVAL;
  557. dev_err(&pdev->dev, "Platform not supported\n");
  558. goto err_clk;
  559. }
  560. ret = exynos_tmu_initialize(pdev);
  561. if (ret) {
  562. dev_err(&pdev->dev, "Failed to initialize TMU\n");
  563. goto err_clk;
  564. }
  565. exynos_tmu_control(pdev, true);
  566. /* Allocate a structure to register with the exynos core thermal */
  567. sensor_conf = devm_kzalloc(&pdev->dev,
  568. sizeof(struct thermal_sensor_conf), GFP_KERNEL);
  569. if (!sensor_conf) {
  570. ret = -ENOMEM;
  571. goto err_clk;
  572. }
  573. sprintf(sensor_conf->name, "therm_zone%d", data->id);
  574. sensor_conf->read_temperature = (int (*)(void *))exynos_tmu_read;
  575. sensor_conf->write_emul_temp =
  576. (int (*)(void *, unsigned long))exynos_tmu_set_emulation;
  577. sensor_conf->driver_data = data;
  578. sensor_conf->trip_data.trip_count = pdata->trigger_enable[0] +
  579. pdata->trigger_enable[1] + pdata->trigger_enable[2]+
  580. pdata->trigger_enable[3];
  581. for (i = 0; i < sensor_conf->trip_data.trip_count; i++) {
  582. sensor_conf->trip_data.trip_val[i] =
  583. pdata->threshold + pdata->trigger_levels[i];
  584. sensor_conf->trip_data.trip_type[i] =
  585. pdata->trigger_type[i];
  586. }
  587. sensor_conf->trip_data.trigger_falling = pdata->threshold_falling;
  588. sensor_conf->cooling_data.freq_clip_count = pdata->freq_tab_count;
  589. for (i = 0; i < pdata->freq_tab_count; i++) {
  590. sensor_conf->cooling_data.freq_data[i].freq_clip_max =
  591. pdata->freq_tab[i].freq_clip_max;
  592. sensor_conf->cooling_data.freq_data[i].temp_level =
  593. pdata->freq_tab[i].temp_level;
  594. }
  595. sensor_conf->dev = &pdev->dev;
  596. /* Register the sensor with thermal management interface */
  597. ret = exynos_register_thermal(sensor_conf);
  598. if (ret) {
  599. dev_err(&pdev->dev, "Failed to register thermal interface\n");
  600. goto err_clk;
  601. }
  602. data->reg_conf = sensor_conf;
  603. ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
  604. IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
  605. if (ret) {
  606. dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
  607. goto err_clk;
  608. }
  609. return 0;
  610. err_clk:
  611. clk_unprepare(data->clk);
  612. err_clk_sec:
  613. if (!IS_ERR(data->clk_sec))
  614. clk_unprepare(data->clk_sec);
  615. return ret;
  616. }
  617. static int exynos_tmu_remove(struct platform_device *pdev)
  618. {
  619. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  620. exynos_unregister_thermal(data->reg_conf);
  621. exynos_tmu_control(pdev, false);
  622. clk_unprepare(data->clk);
  623. if (!IS_ERR(data->clk_sec))
  624. clk_unprepare(data->clk_sec);
  625. if (!IS_ERR(data->regulator))
  626. regulator_disable(data->regulator);
  627. return 0;
  628. }
  629. #ifdef CONFIG_PM_SLEEP
  630. static int exynos_tmu_suspend(struct device *dev)
  631. {
  632. exynos_tmu_control(to_platform_device(dev), false);
  633. return 0;
  634. }
  635. static int exynos_tmu_resume(struct device *dev)
  636. {
  637. struct platform_device *pdev = to_platform_device(dev);
  638. exynos_tmu_initialize(pdev);
  639. exynos_tmu_control(pdev, true);
  640. return 0;
  641. }
  642. static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
  643. exynos_tmu_suspend, exynos_tmu_resume);
  644. #define EXYNOS_TMU_PM (&exynos_tmu_pm)
  645. #else
  646. #define EXYNOS_TMU_PM NULL
  647. #endif
  648. static struct platform_driver exynos_tmu_driver = {
  649. .driver = {
  650. .name = "exynos-tmu",
  651. .owner = THIS_MODULE,
  652. .pm = EXYNOS_TMU_PM,
  653. .of_match_table = exynos_tmu_match,
  654. },
  655. .probe = exynos_tmu_probe,
  656. .remove = exynos_tmu_remove,
  657. };
  658. module_platform_driver(exynos_tmu_driver);
  659. MODULE_DESCRIPTION("EXYNOS TMU Driver");
  660. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  661. MODULE_LICENSE("GPL");
  662. MODULE_ALIAS("platform:exynos-tmu");