exynos_tmu.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. /*
  2. * exynos_tmu.c - Samsung EXYNOS TMU (Thermal Management Unit)
  3. *
  4. * Copyright (C) 2014 Samsung Electronics
  5. * Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>
  6. * Lukasz Majewski <l.majewski@samsung.com>
  7. *
  8. * Copyright (C) 2011 Samsung Electronics
  9. * Donggeun Kim <dg77.kim@samsung.com>
  10. * Amit Daniel Kachhap <amit.kachhap@linaro.org>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/clk.h>
  28. #include <linux/io.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/module.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/regulator/consumer.h>
  36. #include "exynos_tmu.h"
  37. #include "../thermal_core.h"
  38. /* Exynos generic registers */
  39. #define EXYNOS_TMU_REG_TRIMINFO 0x0
  40. #define EXYNOS_TMU_REG_CONTROL 0x20
  41. #define EXYNOS_TMU_REG_STATUS 0x28
  42. #define EXYNOS_TMU_REG_CURRENT_TEMP 0x40
  43. #define EXYNOS_TMU_REG_INTEN 0x70
  44. #define EXYNOS_TMU_REG_INTSTAT 0x74
  45. #define EXYNOS_TMU_REG_INTCLEAR 0x78
  46. #define EXYNOS_TMU_TEMP_MASK 0xff
  47. #define EXYNOS_TMU_REF_VOLTAGE_SHIFT 24
  48. #define EXYNOS_TMU_REF_VOLTAGE_MASK 0x1f
  49. #define EXYNOS_TMU_BUF_SLOPE_SEL_MASK 0xf
  50. #define EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT 8
  51. #define EXYNOS_TMU_CORE_EN_SHIFT 0
  52. /* Exynos3250 specific registers */
  53. #define EXYNOS_TMU_TRIMINFO_CON1 0x10
  54. /* Exynos4210 specific registers */
  55. #define EXYNOS4210_TMU_REG_THRESHOLD_TEMP 0x44
  56. #define EXYNOS4210_TMU_REG_TRIG_LEVEL0 0x50
  57. /* Exynos5250, Exynos4412, Exynos3250 specific registers */
  58. #define EXYNOS_TMU_TRIMINFO_CON2 0x14
  59. #define EXYNOS_THD_TEMP_RISE 0x50
  60. #define EXYNOS_THD_TEMP_FALL 0x54
  61. #define EXYNOS_EMUL_CON 0x80
  62. #define EXYNOS_TRIMINFO_RELOAD_ENABLE 1
  63. #define EXYNOS_TRIMINFO_25_SHIFT 0
  64. #define EXYNOS_TRIMINFO_85_SHIFT 8
  65. #define EXYNOS_TMU_TRIP_MODE_SHIFT 13
  66. #define EXYNOS_TMU_TRIP_MODE_MASK 0x7
  67. #define EXYNOS_TMU_THERM_TRIP_EN_SHIFT 12
  68. #define EXYNOS_TMU_INTEN_RISE0_SHIFT 0
  69. #define EXYNOS_TMU_INTEN_RISE1_SHIFT 4
  70. #define EXYNOS_TMU_INTEN_RISE2_SHIFT 8
  71. #define EXYNOS_TMU_INTEN_RISE3_SHIFT 12
  72. #define EXYNOS_TMU_INTEN_FALL0_SHIFT 16
  73. #define EXYNOS_EMUL_TIME 0x57F0
  74. #define EXYNOS_EMUL_TIME_MASK 0xffff
  75. #define EXYNOS_EMUL_TIME_SHIFT 16
  76. #define EXYNOS_EMUL_DATA_SHIFT 8
  77. #define EXYNOS_EMUL_DATA_MASK 0xFF
  78. #define EXYNOS_EMUL_ENABLE 0x1
  79. /* Exynos5260 specific */
  80. #define EXYNOS5260_TMU_REG_INTEN 0xC0
  81. #define EXYNOS5260_TMU_REG_INTSTAT 0xC4
  82. #define EXYNOS5260_TMU_REG_INTCLEAR 0xC8
  83. #define EXYNOS5260_EMUL_CON 0x100
  84. /* Exynos4412 specific */
  85. #define EXYNOS4412_MUX_ADDR_VALUE 6
  86. #define EXYNOS4412_MUX_ADDR_SHIFT 20
  87. /* Exynos5433 specific registers */
  88. #define EXYNOS5433_TMU_REG_CONTROL1 0x024
  89. #define EXYNOS5433_TMU_SAMPLING_INTERVAL 0x02c
  90. #define EXYNOS5433_TMU_COUNTER_VALUE0 0x030
  91. #define EXYNOS5433_TMU_COUNTER_VALUE1 0x034
  92. #define EXYNOS5433_TMU_REG_CURRENT_TEMP1 0x044
  93. #define EXYNOS5433_THD_TEMP_RISE3_0 0x050
  94. #define EXYNOS5433_THD_TEMP_RISE7_4 0x054
  95. #define EXYNOS5433_THD_TEMP_FALL3_0 0x060
  96. #define EXYNOS5433_THD_TEMP_FALL7_4 0x064
  97. #define EXYNOS5433_TMU_REG_INTEN 0x0c0
  98. #define EXYNOS5433_TMU_REG_INTPEND 0x0c8
  99. #define EXYNOS5433_TMU_EMUL_CON 0x110
  100. #define EXYNOS5433_TMU_PD_DET_EN 0x130
  101. #define EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT 16
  102. #define EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT 23
  103. #define EXYNOS5433_TRIMINFO_SENSOR_ID_MASK \
  104. (0xf << EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT)
  105. #define EXYNOS5433_TRIMINFO_CALIB_SEL_MASK BIT(23)
  106. #define EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING 0
  107. #define EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING 1
  108. #define EXYNOS5433_PD_DET_EN 1
  109. /*exynos5440 specific registers*/
  110. #define EXYNOS5440_TMU_S0_7_TRIM 0x000
  111. #define EXYNOS5440_TMU_S0_7_CTRL 0x020
  112. #define EXYNOS5440_TMU_S0_7_DEBUG 0x040
  113. #define EXYNOS5440_TMU_S0_7_TEMP 0x0f0
  114. #define EXYNOS5440_TMU_S0_7_TH0 0x110
  115. #define EXYNOS5440_TMU_S0_7_TH1 0x130
  116. #define EXYNOS5440_TMU_S0_7_TH2 0x150
  117. #define EXYNOS5440_TMU_S0_7_IRQEN 0x210
  118. #define EXYNOS5440_TMU_S0_7_IRQ 0x230
  119. /* exynos5440 common registers */
  120. #define EXYNOS5440_TMU_IRQ_STATUS 0x000
  121. #define EXYNOS5440_TMU_PMIN 0x004
  122. #define EXYNOS5440_TMU_INTEN_RISE0_SHIFT 0
  123. #define EXYNOS5440_TMU_INTEN_RISE1_SHIFT 1
  124. #define EXYNOS5440_TMU_INTEN_RISE2_SHIFT 2
  125. #define EXYNOS5440_TMU_INTEN_RISE3_SHIFT 3
  126. #define EXYNOS5440_TMU_INTEN_FALL0_SHIFT 4
  127. #define EXYNOS5440_TMU_TH_RISE4_SHIFT 24
  128. #define EXYNOS5440_EFUSE_SWAP_OFFSET 8
  129. /* Exynos7 specific registers */
  130. #define EXYNOS7_THD_TEMP_RISE7_6 0x50
  131. #define EXYNOS7_THD_TEMP_FALL7_6 0x60
  132. #define EXYNOS7_TMU_REG_INTEN 0x110
  133. #define EXYNOS7_TMU_REG_INTPEND 0x118
  134. #define EXYNOS7_TMU_REG_EMUL_CON 0x160
  135. #define EXYNOS7_TMU_TEMP_MASK 0x1ff
  136. #define EXYNOS7_PD_DET_EN_SHIFT 23
  137. #define EXYNOS7_TMU_INTEN_RISE0_SHIFT 0
  138. #define EXYNOS7_TMU_INTEN_RISE1_SHIFT 1
  139. #define EXYNOS7_TMU_INTEN_RISE2_SHIFT 2
  140. #define EXYNOS7_TMU_INTEN_RISE3_SHIFT 3
  141. #define EXYNOS7_TMU_INTEN_RISE4_SHIFT 4
  142. #define EXYNOS7_TMU_INTEN_RISE5_SHIFT 5
  143. #define EXYNOS7_TMU_INTEN_RISE6_SHIFT 6
  144. #define EXYNOS7_TMU_INTEN_RISE7_SHIFT 7
  145. #define EXYNOS7_EMUL_DATA_SHIFT 7
  146. #define EXYNOS7_EMUL_DATA_MASK 0x1ff
  147. #define MCELSIUS 1000
  148. /**
  149. * struct exynos_tmu_data : A structure to hold the private data of the TMU
  150. driver
  151. * @id: identifier of the one instance of the TMU controller.
  152. * @pdata: pointer to the tmu platform/configuration data
  153. * @base: base address of the single instance of the TMU controller.
  154. * @base_second: base address of the common registers of the TMU controller.
  155. * @irq: irq number of the TMU controller.
  156. * @soc: id of the SOC type.
  157. * @irq_work: pointer to the irq work structure.
  158. * @lock: lock to implement synchronization.
  159. * @clk: pointer to the clock structure.
  160. * @clk_sec: pointer to the clock structure for accessing the base_second.
  161. * @sclk: pointer to the clock structure for accessing the tmu special clk.
  162. * @temp_error1: fused value of the first point trim.
  163. * @temp_error2: fused value of the second point trim.
  164. * @regulator: pointer to the TMU regulator structure.
  165. * @reg_conf: pointer to structure to register with core thermal.
  166. * @ntrip: number of supported trip points.
  167. * @enabled: current status of TMU device
  168. * @tmu_initialize: SoC specific TMU initialization method
  169. * @tmu_control: SoC specific TMU control method
  170. * @tmu_read: SoC specific TMU temperature read method
  171. * @tmu_set_emulation: SoC specific TMU emulation setting method
  172. * @tmu_clear_irqs: SoC specific TMU interrupts clearing method
  173. */
  174. struct exynos_tmu_data {
  175. int id;
  176. struct exynos_tmu_platform_data *pdata;
  177. void __iomem *base;
  178. void __iomem *base_second;
  179. int irq;
  180. enum soc_type soc;
  181. struct work_struct irq_work;
  182. struct mutex lock;
  183. struct clk *clk, *clk_sec, *sclk;
  184. u16 temp_error1, temp_error2;
  185. struct regulator *regulator;
  186. struct thermal_zone_device *tzd;
  187. unsigned int ntrip;
  188. bool enabled;
  189. int (*tmu_initialize)(struct platform_device *pdev);
  190. void (*tmu_control)(struct platform_device *pdev, bool on);
  191. int (*tmu_read)(struct exynos_tmu_data *data);
  192. void (*tmu_set_emulation)(struct exynos_tmu_data *data, int temp);
  193. void (*tmu_clear_irqs)(struct exynos_tmu_data *data);
  194. };
  195. static void exynos_report_trigger(struct exynos_tmu_data *p)
  196. {
  197. char data[10], *envp[] = { data, NULL };
  198. struct thermal_zone_device *tz = p->tzd;
  199. int temp;
  200. unsigned int i;
  201. if (!tz) {
  202. pr_err("No thermal zone device defined\n");
  203. return;
  204. }
  205. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  206. mutex_lock(&tz->lock);
  207. /* Find the level for which trip happened */
  208. for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
  209. tz->ops->get_trip_temp(tz, i, &temp);
  210. if (tz->last_temperature < temp)
  211. break;
  212. }
  213. snprintf(data, sizeof(data), "%u", i);
  214. kobject_uevent_env(&tz->device.kobj, KOBJ_CHANGE, envp);
  215. mutex_unlock(&tz->lock);
  216. }
  217. /*
  218. * TMU treats temperature as a mapped temperature code.
  219. * The temperature is converted differently depending on the calibration type.
  220. */
  221. static int temp_to_code(struct exynos_tmu_data *data, u8 temp)
  222. {
  223. struct exynos_tmu_platform_data *pdata = data->pdata;
  224. int temp_code;
  225. switch (pdata->cal_type) {
  226. case TYPE_TWO_POINT_TRIMMING:
  227. temp_code = (temp - pdata->first_point_trim) *
  228. (data->temp_error2 - data->temp_error1) /
  229. (pdata->second_point_trim - pdata->first_point_trim) +
  230. data->temp_error1;
  231. break;
  232. case TYPE_ONE_POINT_TRIMMING:
  233. temp_code = temp + data->temp_error1 - pdata->first_point_trim;
  234. break;
  235. default:
  236. temp_code = temp + pdata->default_temp_offset;
  237. break;
  238. }
  239. return temp_code;
  240. }
  241. /*
  242. * Calculate a temperature value from a temperature code.
  243. * The unit of the temperature is degree Celsius.
  244. */
  245. static int code_to_temp(struct exynos_tmu_data *data, u16 temp_code)
  246. {
  247. struct exynos_tmu_platform_data *pdata = data->pdata;
  248. int temp;
  249. switch (pdata->cal_type) {
  250. case TYPE_TWO_POINT_TRIMMING:
  251. temp = (temp_code - data->temp_error1) *
  252. (pdata->second_point_trim - pdata->first_point_trim) /
  253. (data->temp_error2 - data->temp_error1) +
  254. pdata->first_point_trim;
  255. break;
  256. case TYPE_ONE_POINT_TRIMMING:
  257. temp = temp_code - data->temp_error1 + pdata->first_point_trim;
  258. break;
  259. default:
  260. temp = temp_code - pdata->default_temp_offset;
  261. break;
  262. }
  263. return temp;
  264. }
  265. static void sanitize_temp_error(struct exynos_tmu_data *data, u32 trim_info)
  266. {
  267. struct exynos_tmu_platform_data *pdata = data->pdata;
  268. data->temp_error1 = trim_info & EXYNOS_TMU_TEMP_MASK;
  269. data->temp_error2 = ((trim_info >> EXYNOS_TRIMINFO_85_SHIFT) &
  270. EXYNOS_TMU_TEMP_MASK);
  271. if (!data->temp_error1 ||
  272. (pdata->min_efuse_value > data->temp_error1) ||
  273. (data->temp_error1 > pdata->max_efuse_value))
  274. data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
  275. if (!data->temp_error2)
  276. data->temp_error2 =
  277. (pdata->efuse_value >> EXYNOS_TRIMINFO_85_SHIFT) &
  278. EXYNOS_TMU_TEMP_MASK;
  279. }
  280. static u32 get_th_reg(struct exynos_tmu_data *data, u32 threshold, bool falling)
  281. {
  282. struct thermal_zone_device *tz = data->tzd;
  283. const struct thermal_trip * const trips =
  284. of_thermal_get_trip_points(tz);
  285. unsigned long temp;
  286. int i;
  287. if (!trips) {
  288. pr_err("%s: Cannot get trip points from of-thermal.c!\n",
  289. __func__);
  290. return 0;
  291. }
  292. for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
  293. if (trips[i].type == THERMAL_TRIP_CRITICAL)
  294. continue;
  295. temp = trips[i].temperature / MCELSIUS;
  296. if (falling)
  297. temp -= (trips[i].hysteresis / MCELSIUS);
  298. else
  299. threshold &= ~(0xff << 8 * i);
  300. threshold |= temp_to_code(data, temp) << 8 * i;
  301. }
  302. return threshold;
  303. }
  304. static int exynos_tmu_initialize(struct platform_device *pdev)
  305. {
  306. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  307. int ret;
  308. if (of_thermal_get_ntrips(data->tzd) > data->ntrip) {
  309. dev_info(&pdev->dev,
  310. "More trip points than supported by this TMU.\n");
  311. dev_info(&pdev->dev,
  312. "%d trip points should be configured in polling mode.\n",
  313. (of_thermal_get_ntrips(data->tzd) - data->ntrip));
  314. }
  315. mutex_lock(&data->lock);
  316. clk_enable(data->clk);
  317. if (!IS_ERR(data->clk_sec))
  318. clk_enable(data->clk_sec);
  319. ret = data->tmu_initialize(pdev);
  320. clk_disable(data->clk);
  321. mutex_unlock(&data->lock);
  322. if (!IS_ERR(data->clk_sec))
  323. clk_disable(data->clk_sec);
  324. return ret;
  325. }
  326. static u32 get_con_reg(struct exynos_tmu_data *data, u32 con)
  327. {
  328. struct exynos_tmu_platform_data *pdata = data->pdata;
  329. if (data->soc == SOC_ARCH_EXYNOS4412 ||
  330. data->soc == SOC_ARCH_EXYNOS3250)
  331. con |= (EXYNOS4412_MUX_ADDR_VALUE << EXYNOS4412_MUX_ADDR_SHIFT);
  332. con &= ~(EXYNOS_TMU_REF_VOLTAGE_MASK << EXYNOS_TMU_REF_VOLTAGE_SHIFT);
  333. con |= pdata->reference_voltage << EXYNOS_TMU_REF_VOLTAGE_SHIFT;
  334. con &= ~(EXYNOS_TMU_BUF_SLOPE_SEL_MASK << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
  335. con |= (pdata->gain << EXYNOS_TMU_BUF_SLOPE_SEL_SHIFT);
  336. if (pdata->noise_cancel_mode) {
  337. con &= ~(EXYNOS_TMU_TRIP_MODE_MASK << EXYNOS_TMU_TRIP_MODE_SHIFT);
  338. con |= (pdata->noise_cancel_mode << EXYNOS_TMU_TRIP_MODE_SHIFT);
  339. }
  340. return con;
  341. }
  342. static void exynos_tmu_control(struct platform_device *pdev, bool on)
  343. {
  344. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  345. mutex_lock(&data->lock);
  346. clk_enable(data->clk);
  347. data->tmu_control(pdev, on);
  348. data->enabled = on;
  349. clk_disable(data->clk);
  350. mutex_unlock(&data->lock);
  351. }
  352. static int exynos4210_tmu_initialize(struct platform_device *pdev)
  353. {
  354. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  355. struct thermal_zone_device *tz = data->tzd;
  356. const struct thermal_trip * const trips =
  357. of_thermal_get_trip_points(tz);
  358. int ret = 0, threshold_code, i;
  359. unsigned long reference, temp;
  360. unsigned int status;
  361. if (!trips) {
  362. pr_err("%s: Cannot get trip points from of-thermal.c!\n",
  363. __func__);
  364. ret = -ENODEV;
  365. goto out;
  366. }
  367. status = readb(data->base + EXYNOS_TMU_REG_STATUS);
  368. if (!status) {
  369. ret = -EBUSY;
  370. goto out;
  371. }
  372. sanitize_temp_error(data, readl(data->base + EXYNOS_TMU_REG_TRIMINFO));
  373. /* Write temperature code for threshold */
  374. reference = trips[0].temperature / MCELSIUS;
  375. threshold_code = temp_to_code(data, reference);
  376. if (threshold_code < 0) {
  377. ret = threshold_code;
  378. goto out;
  379. }
  380. writeb(threshold_code, data->base + EXYNOS4210_TMU_REG_THRESHOLD_TEMP);
  381. for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
  382. temp = trips[i].temperature / MCELSIUS;
  383. writeb(temp - reference, data->base +
  384. EXYNOS4210_TMU_REG_TRIG_LEVEL0 + i * 4);
  385. }
  386. data->tmu_clear_irqs(data);
  387. out:
  388. return ret;
  389. }
  390. static int exynos4412_tmu_initialize(struct platform_device *pdev)
  391. {
  392. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  393. const struct thermal_trip * const trips =
  394. of_thermal_get_trip_points(data->tzd);
  395. unsigned int status, trim_info, con, ctrl, rising_threshold;
  396. int ret = 0, threshold_code, i;
  397. unsigned long crit_temp = 0;
  398. status = readb(data->base + EXYNOS_TMU_REG_STATUS);
  399. if (!status) {
  400. ret = -EBUSY;
  401. goto out;
  402. }
  403. if (data->soc == SOC_ARCH_EXYNOS3250 ||
  404. data->soc == SOC_ARCH_EXYNOS4412 ||
  405. data->soc == SOC_ARCH_EXYNOS5250) {
  406. if (data->soc == SOC_ARCH_EXYNOS3250) {
  407. ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON1);
  408. ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
  409. writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON1);
  410. }
  411. ctrl = readl(data->base + EXYNOS_TMU_TRIMINFO_CON2);
  412. ctrl |= EXYNOS_TRIMINFO_RELOAD_ENABLE;
  413. writel(ctrl, data->base + EXYNOS_TMU_TRIMINFO_CON2);
  414. }
  415. /* On exynos5420 the triminfo register is in the shared space */
  416. if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
  417. trim_info = readl(data->base_second + EXYNOS_TMU_REG_TRIMINFO);
  418. else
  419. trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
  420. sanitize_temp_error(data, trim_info);
  421. /* Write temperature code for rising and falling threshold */
  422. rising_threshold = readl(data->base + EXYNOS_THD_TEMP_RISE);
  423. rising_threshold = get_th_reg(data, rising_threshold, false);
  424. writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
  425. writel(get_th_reg(data, 0, true), data->base + EXYNOS_THD_TEMP_FALL);
  426. data->tmu_clear_irqs(data);
  427. /* if last threshold limit is also present */
  428. for (i = 0; i < of_thermal_get_ntrips(data->tzd); i++) {
  429. if (trips[i].type == THERMAL_TRIP_CRITICAL) {
  430. crit_temp = trips[i].temperature;
  431. break;
  432. }
  433. }
  434. if (i == of_thermal_get_ntrips(data->tzd)) {
  435. pr_err("%s: No CRITICAL trip point defined at of-thermal.c!\n",
  436. __func__);
  437. ret = -EINVAL;
  438. goto out;
  439. }
  440. threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
  441. /* 1-4 level to be assigned in th0 reg */
  442. rising_threshold &= ~(0xff << 8 * i);
  443. rising_threshold |= threshold_code << 8 * i;
  444. writel(rising_threshold, data->base + EXYNOS_THD_TEMP_RISE);
  445. con = readl(data->base + EXYNOS_TMU_REG_CONTROL);
  446. con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
  447. writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
  448. out:
  449. return ret;
  450. }
  451. static int exynos5433_tmu_initialize(struct platform_device *pdev)
  452. {
  453. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  454. struct exynos_tmu_platform_data *pdata = data->pdata;
  455. struct thermal_zone_device *tz = data->tzd;
  456. unsigned int status, trim_info;
  457. unsigned int rising_threshold = 0, falling_threshold = 0;
  458. int temp, temp_hist;
  459. int ret = 0, threshold_code, i, sensor_id, cal_type;
  460. status = readb(data->base + EXYNOS_TMU_REG_STATUS);
  461. if (!status) {
  462. ret = -EBUSY;
  463. goto out;
  464. }
  465. trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
  466. sanitize_temp_error(data, trim_info);
  467. /* Read the temperature sensor id */
  468. sensor_id = (trim_info & EXYNOS5433_TRIMINFO_SENSOR_ID_MASK)
  469. >> EXYNOS5433_TRIMINFO_SENSOR_ID_SHIFT;
  470. dev_info(&pdev->dev, "Temperature sensor ID: 0x%x\n", sensor_id);
  471. /* Read the calibration mode */
  472. writel(trim_info, data->base + EXYNOS_TMU_REG_TRIMINFO);
  473. cal_type = (trim_info & EXYNOS5433_TRIMINFO_CALIB_SEL_MASK)
  474. >> EXYNOS5433_TRIMINFO_CALIB_SEL_SHIFT;
  475. switch (cal_type) {
  476. case EXYNOS5433_TRIMINFO_ONE_POINT_TRIMMING:
  477. pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
  478. break;
  479. case EXYNOS5433_TRIMINFO_TWO_POINT_TRIMMING:
  480. pdata->cal_type = TYPE_TWO_POINT_TRIMMING;
  481. break;
  482. default:
  483. pdata->cal_type = TYPE_ONE_POINT_TRIMMING;
  484. break;
  485. }
  486. dev_info(&pdev->dev, "Calibration type is %d-point calibration\n",
  487. cal_type ? 2 : 1);
  488. /* Write temperature code for rising and falling threshold */
  489. for (i = 0; i < of_thermal_get_ntrips(tz); i++) {
  490. int rising_reg_offset, falling_reg_offset;
  491. int j = 0;
  492. switch (i) {
  493. case 0:
  494. case 1:
  495. case 2:
  496. case 3:
  497. rising_reg_offset = EXYNOS5433_THD_TEMP_RISE3_0;
  498. falling_reg_offset = EXYNOS5433_THD_TEMP_FALL3_0;
  499. j = i;
  500. break;
  501. case 4:
  502. case 5:
  503. case 6:
  504. case 7:
  505. rising_reg_offset = EXYNOS5433_THD_TEMP_RISE7_4;
  506. falling_reg_offset = EXYNOS5433_THD_TEMP_FALL7_4;
  507. j = i - 4;
  508. break;
  509. default:
  510. continue;
  511. }
  512. /* Write temperature code for rising threshold */
  513. tz->ops->get_trip_temp(tz, i, &temp);
  514. temp /= MCELSIUS;
  515. threshold_code = temp_to_code(data, temp);
  516. rising_threshold = readl(data->base + rising_reg_offset);
  517. rising_threshold &= ~(0xff << j * 8);
  518. rising_threshold |= (threshold_code << j * 8);
  519. writel(rising_threshold, data->base + rising_reg_offset);
  520. /* Write temperature code for falling threshold */
  521. tz->ops->get_trip_hyst(tz, i, &temp_hist);
  522. temp_hist = temp - (temp_hist / MCELSIUS);
  523. threshold_code = temp_to_code(data, temp_hist);
  524. falling_threshold = readl(data->base + falling_reg_offset);
  525. falling_threshold &= ~(0xff << j * 8);
  526. falling_threshold |= (threshold_code << j * 8);
  527. writel(falling_threshold, data->base + falling_reg_offset);
  528. }
  529. data->tmu_clear_irqs(data);
  530. out:
  531. return ret;
  532. }
  533. static int exynos5440_tmu_initialize(struct platform_device *pdev)
  534. {
  535. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  536. unsigned int trim_info = 0, con, rising_threshold;
  537. int threshold_code;
  538. int crit_temp = 0;
  539. /*
  540. * For exynos5440 soc triminfo value is swapped between TMU0 and
  541. * TMU2, so the below logic is needed.
  542. */
  543. switch (data->id) {
  544. case 0:
  545. trim_info = readl(data->base + EXYNOS5440_EFUSE_SWAP_OFFSET +
  546. EXYNOS5440_TMU_S0_7_TRIM);
  547. break;
  548. case 1:
  549. trim_info = readl(data->base + EXYNOS5440_TMU_S0_7_TRIM);
  550. break;
  551. case 2:
  552. trim_info = readl(data->base - EXYNOS5440_EFUSE_SWAP_OFFSET +
  553. EXYNOS5440_TMU_S0_7_TRIM);
  554. }
  555. sanitize_temp_error(data, trim_info);
  556. /* Write temperature code for rising and falling threshold */
  557. rising_threshold = readl(data->base + EXYNOS5440_TMU_S0_7_TH0);
  558. rising_threshold = get_th_reg(data, rising_threshold, false);
  559. writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH0);
  560. writel(0, data->base + EXYNOS5440_TMU_S0_7_TH1);
  561. data->tmu_clear_irqs(data);
  562. /* if last threshold limit is also present */
  563. if (!data->tzd->ops->get_crit_temp(data->tzd, &crit_temp)) {
  564. threshold_code = temp_to_code(data, crit_temp / MCELSIUS);
  565. /* 5th level to be assigned in th2 reg */
  566. rising_threshold =
  567. threshold_code << EXYNOS5440_TMU_TH_RISE4_SHIFT;
  568. writel(rising_threshold, data->base + EXYNOS5440_TMU_S0_7_TH2);
  569. con = readl(data->base + EXYNOS5440_TMU_S0_7_CTRL);
  570. con |= (1 << EXYNOS_TMU_THERM_TRIP_EN_SHIFT);
  571. writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
  572. }
  573. /* Clear the PMIN in the common TMU register */
  574. if (!data->id)
  575. writel(0, data->base_second + EXYNOS5440_TMU_PMIN);
  576. return 0;
  577. }
  578. static int exynos7_tmu_initialize(struct platform_device *pdev)
  579. {
  580. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  581. struct thermal_zone_device *tz = data->tzd;
  582. struct exynos_tmu_platform_data *pdata = data->pdata;
  583. unsigned int status, trim_info;
  584. unsigned int rising_threshold = 0, falling_threshold = 0;
  585. int ret = 0, threshold_code, i;
  586. int temp, temp_hist;
  587. unsigned int reg_off, bit_off;
  588. status = readb(data->base + EXYNOS_TMU_REG_STATUS);
  589. if (!status) {
  590. ret = -EBUSY;
  591. goto out;
  592. }
  593. trim_info = readl(data->base + EXYNOS_TMU_REG_TRIMINFO);
  594. data->temp_error1 = trim_info & EXYNOS7_TMU_TEMP_MASK;
  595. if (!data->temp_error1 ||
  596. (pdata->min_efuse_value > data->temp_error1) ||
  597. (data->temp_error1 > pdata->max_efuse_value))
  598. data->temp_error1 = pdata->efuse_value & EXYNOS_TMU_TEMP_MASK;
  599. /* Write temperature code for rising and falling threshold */
  600. for (i = (of_thermal_get_ntrips(tz) - 1); i >= 0; i--) {
  601. /*
  602. * On exynos7 there are 4 rising and 4 falling threshold
  603. * registers (0x50-0x5c and 0x60-0x6c respectively). Each
  604. * register holds the value of two threshold levels (at bit
  605. * offsets 0 and 16). Based on the fact that there are atmost
  606. * eight possible trigger levels, calculate the register and
  607. * bit offsets where the threshold levels are to be written.
  608. *
  609. * e.g. EXYNOS7_THD_TEMP_RISE7_6 (0x50)
  610. * [24:16] - Threshold level 7
  611. * [8:0] - Threshold level 6
  612. * e.g. EXYNOS7_THD_TEMP_RISE5_4 (0x54)
  613. * [24:16] - Threshold level 5
  614. * [8:0] - Threshold level 4
  615. *
  616. * and similarly for falling thresholds.
  617. *
  618. * Based on the above, calculate the register and bit offsets
  619. * for rising/falling threshold levels and populate them.
  620. */
  621. reg_off = ((7 - i) / 2) * 4;
  622. bit_off = ((8 - i) % 2);
  623. tz->ops->get_trip_temp(tz, i, &temp);
  624. temp /= MCELSIUS;
  625. tz->ops->get_trip_hyst(tz, i, &temp_hist);
  626. temp_hist = temp - (temp_hist / MCELSIUS);
  627. /* Set 9-bit temperature code for rising threshold levels */
  628. threshold_code = temp_to_code(data, temp);
  629. rising_threshold = readl(data->base +
  630. EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
  631. rising_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
  632. rising_threshold |= threshold_code << (16 * bit_off);
  633. writel(rising_threshold,
  634. data->base + EXYNOS7_THD_TEMP_RISE7_6 + reg_off);
  635. /* Set 9-bit temperature code for falling threshold levels */
  636. threshold_code = temp_to_code(data, temp_hist);
  637. falling_threshold &= ~(EXYNOS7_TMU_TEMP_MASK << (16 * bit_off));
  638. falling_threshold |= threshold_code << (16 * bit_off);
  639. writel(falling_threshold,
  640. data->base + EXYNOS7_THD_TEMP_FALL7_6 + reg_off);
  641. }
  642. data->tmu_clear_irqs(data);
  643. out:
  644. return ret;
  645. }
  646. static void exynos4210_tmu_control(struct platform_device *pdev, bool on)
  647. {
  648. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  649. struct thermal_zone_device *tz = data->tzd;
  650. unsigned int con, interrupt_en;
  651. con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
  652. if (on) {
  653. con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
  654. interrupt_en =
  655. (of_thermal_is_trip_valid(tz, 3)
  656. << EXYNOS_TMU_INTEN_RISE3_SHIFT) |
  657. (of_thermal_is_trip_valid(tz, 2)
  658. << EXYNOS_TMU_INTEN_RISE2_SHIFT) |
  659. (of_thermal_is_trip_valid(tz, 1)
  660. << EXYNOS_TMU_INTEN_RISE1_SHIFT) |
  661. (of_thermal_is_trip_valid(tz, 0)
  662. << EXYNOS_TMU_INTEN_RISE0_SHIFT);
  663. if (data->soc != SOC_ARCH_EXYNOS4210)
  664. interrupt_en |=
  665. interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
  666. } else {
  667. con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
  668. interrupt_en = 0; /* Disable all interrupts */
  669. }
  670. writel(interrupt_en, data->base + EXYNOS_TMU_REG_INTEN);
  671. writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
  672. }
  673. static void exynos5433_tmu_control(struct platform_device *pdev, bool on)
  674. {
  675. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  676. struct thermal_zone_device *tz = data->tzd;
  677. unsigned int con, interrupt_en, pd_det_en;
  678. con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
  679. if (on) {
  680. con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
  681. interrupt_en =
  682. (of_thermal_is_trip_valid(tz, 7)
  683. << EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
  684. (of_thermal_is_trip_valid(tz, 6)
  685. << EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
  686. (of_thermal_is_trip_valid(tz, 5)
  687. << EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
  688. (of_thermal_is_trip_valid(tz, 4)
  689. << EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
  690. (of_thermal_is_trip_valid(tz, 3)
  691. << EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
  692. (of_thermal_is_trip_valid(tz, 2)
  693. << EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
  694. (of_thermal_is_trip_valid(tz, 1)
  695. << EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
  696. (of_thermal_is_trip_valid(tz, 0)
  697. << EXYNOS7_TMU_INTEN_RISE0_SHIFT);
  698. interrupt_en |=
  699. interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
  700. } else {
  701. con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
  702. interrupt_en = 0; /* Disable all interrupts */
  703. }
  704. pd_det_en = on ? EXYNOS5433_PD_DET_EN : 0;
  705. writel(pd_det_en, data->base + EXYNOS5433_TMU_PD_DET_EN);
  706. writel(interrupt_en, data->base + EXYNOS5433_TMU_REG_INTEN);
  707. writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
  708. }
  709. static void exynos5440_tmu_control(struct platform_device *pdev, bool on)
  710. {
  711. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  712. struct thermal_zone_device *tz = data->tzd;
  713. unsigned int con, interrupt_en;
  714. con = get_con_reg(data, readl(data->base + EXYNOS5440_TMU_S0_7_CTRL));
  715. if (on) {
  716. con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
  717. interrupt_en =
  718. (of_thermal_is_trip_valid(tz, 3)
  719. << EXYNOS5440_TMU_INTEN_RISE3_SHIFT) |
  720. (of_thermal_is_trip_valid(tz, 2)
  721. << EXYNOS5440_TMU_INTEN_RISE2_SHIFT) |
  722. (of_thermal_is_trip_valid(tz, 1)
  723. << EXYNOS5440_TMU_INTEN_RISE1_SHIFT) |
  724. (of_thermal_is_trip_valid(tz, 0)
  725. << EXYNOS5440_TMU_INTEN_RISE0_SHIFT);
  726. interrupt_en |=
  727. interrupt_en << EXYNOS5440_TMU_INTEN_FALL0_SHIFT;
  728. } else {
  729. con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
  730. interrupt_en = 0; /* Disable all interrupts */
  731. }
  732. writel(interrupt_en, data->base + EXYNOS5440_TMU_S0_7_IRQEN);
  733. writel(con, data->base + EXYNOS5440_TMU_S0_7_CTRL);
  734. }
  735. static void exynos7_tmu_control(struct platform_device *pdev, bool on)
  736. {
  737. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  738. struct thermal_zone_device *tz = data->tzd;
  739. unsigned int con, interrupt_en;
  740. con = get_con_reg(data, readl(data->base + EXYNOS_TMU_REG_CONTROL));
  741. if (on) {
  742. con |= (1 << EXYNOS_TMU_CORE_EN_SHIFT);
  743. con |= (1 << EXYNOS7_PD_DET_EN_SHIFT);
  744. interrupt_en =
  745. (of_thermal_is_trip_valid(tz, 7)
  746. << EXYNOS7_TMU_INTEN_RISE7_SHIFT) |
  747. (of_thermal_is_trip_valid(tz, 6)
  748. << EXYNOS7_TMU_INTEN_RISE6_SHIFT) |
  749. (of_thermal_is_trip_valid(tz, 5)
  750. << EXYNOS7_TMU_INTEN_RISE5_SHIFT) |
  751. (of_thermal_is_trip_valid(tz, 4)
  752. << EXYNOS7_TMU_INTEN_RISE4_SHIFT) |
  753. (of_thermal_is_trip_valid(tz, 3)
  754. << EXYNOS7_TMU_INTEN_RISE3_SHIFT) |
  755. (of_thermal_is_trip_valid(tz, 2)
  756. << EXYNOS7_TMU_INTEN_RISE2_SHIFT) |
  757. (of_thermal_is_trip_valid(tz, 1)
  758. << EXYNOS7_TMU_INTEN_RISE1_SHIFT) |
  759. (of_thermal_is_trip_valid(tz, 0)
  760. << EXYNOS7_TMU_INTEN_RISE0_SHIFT);
  761. interrupt_en |=
  762. interrupt_en << EXYNOS_TMU_INTEN_FALL0_SHIFT;
  763. } else {
  764. con &= ~(1 << EXYNOS_TMU_CORE_EN_SHIFT);
  765. con &= ~(1 << EXYNOS7_PD_DET_EN_SHIFT);
  766. interrupt_en = 0; /* Disable all interrupts */
  767. }
  768. writel(interrupt_en, data->base + EXYNOS7_TMU_REG_INTEN);
  769. writel(con, data->base + EXYNOS_TMU_REG_CONTROL);
  770. }
  771. static int exynos_get_temp(void *p, int *temp)
  772. {
  773. struct exynos_tmu_data *data = p;
  774. int value, ret = 0;
  775. if (!data || !data->tmu_read || !data->enabled)
  776. return -EINVAL;
  777. mutex_lock(&data->lock);
  778. clk_enable(data->clk);
  779. value = data->tmu_read(data);
  780. if (value < 0)
  781. ret = value;
  782. else
  783. *temp = code_to_temp(data, value) * MCELSIUS;
  784. clk_disable(data->clk);
  785. mutex_unlock(&data->lock);
  786. return ret;
  787. }
  788. #ifdef CONFIG_THERMAL_EMULATION
  789. static u32 get_emul_con_reg(struct exynos_tmu_data *data, unsigned int val,
  790. int temp)
  791. {
  792. if (temp) {
  793. temp /= MCELSIUS;
  794. if (data->soc != SOC_ARCH_EXYNOS5440) {
  795. val &= ~(EXYNOS_EMUL_TIME_MASK << EXYNOS_EMUL_TIME_SHIFT);
  796. val |= (EXYNOS_EMUL_TIME << EXYNOS_EMUL_TIME_SHIFT);
  797. }
  798. if (data->soc == SOC_ARCH_EXYNOS7) {
  799. val &= ~(EXYNOS7_EMUL_DATA_MASK <<
  800. EXYNOS7_EMUL_DATA_SHIFT);
  801. val |= (temp_to_code(data, temp) <<
  802. EXYNOS7_EMUL_DATA_SHIFT) |
  803. EXYNOS_EMUL_ENABLE;
  804. } else {
  805. val &= ~(EXYNOS_EMUL_DATA_MASK <<
  806. EXYNOS_EMUL_DATA_SHIFT);
  807. val |= (temp_to_code(data, temp) <<
  808. EXYNOS_EMUL_DATA_SHIFT) |
  809. EXYNOS_EMUL_ENABLE;
  810. }
  811. } else {
  812. val &= ~EXYNOS_EMUL_ENABLE;
  813. }
  814. return val;
  815. }
  816. static void exynos4412_tmu_set_emulation(struct exynos_tmu_data *data,
  817. int temp)
  818. {
  819. unsigned int val;
  820. u32 emul_con;
  821. if (data->soc == SOC_ARCH_EXYNOS5260)
  822. emul_con = EXYNOS5260_EMUL_CON;
  823. else if (data->soc == SOC_ARCH_EXYNOS5433)
  824. emul_con = EXYNOS5433_TMU_EMUL_CON;
  825. else if (data->soc == SOC_ARCH_EXYNOS7)
  826. emul_con = EXYNOS7_TMU_REG_EMUL_CON;
  827. else
  828. emul_con = EXYNOS_EMUL_CON;
  829. val = readl(data->base + emul_con);
  830. val = get_emul_con_reg(data, val, temp);
  831. writel(val, data->base + emul_con);
  832. }
  833. static void exynos5440_tmu_set_emulation(struct exynos_tmu_data *data,
  834. int temp)
  835. {
  836. unsigned int val;
  837. val = readl(data->base + EXYNOS5440_TMU_S0_7_DEBUG);
  838. val = get_emul_con_reg(data, val, temp);
  839. writel(val, data->base + EXYNOS5440_TMU_S0_7_DEBUG);
  840. }
  841. static int exynos_tmu_set_emulation(void *drv_data, int temp)
  842. {
  843. struct exynos_tmu_data *data = drv_data;
  844. int ret = -EINVAL;
  845. if (data->soc == SOC_ARCH_EXYNOS4210)
  846. goto out;
  847. if (temp && temp < MCELSIUS)
  848. goto out;
  849. mutex_lock(&data->lock);
  850. clk_enable(data->clk);
  851. data->tmu_set_emulation(data, temp);
  852. clk_disable(data->clk);
  853. mutex_unlock(&data->lock);
  854. return 0;
  855. out:
  856. return ret;
  857. }
  858. #else
  859. #define exynos4412_tmu_set_emulation NULL
  860. #define exynos5440_tmu_set_emulation NULL
  861. static int exynos_tmu_set_emulation(void *drv_data, int temp)
  862. { return -EINVAL; }
  863. #endif /* CONFIG_THERMAL_EMULATION */
  864. static int exynos4210_tmu_read(struct exynos_tmu_data *data)
  865. {
  866. int ret = readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
  867. /* "temp_code" should range between 75 and 175 */
  868. return (ret < 75 || ret > 175) ? -ENODATA : ret;
  869. }
  870. static int exynos4412_tmu_read(struct exynos_tmu_data *data)
  871. {
  872. return readb(data->base + EXYNOS_TMU_REG_CURRENT_TEMP);
  873. }
  874. static int exynos5440_tmu_read(struct exynos_tmu_data *data)
  875. {
  876. return readb(data->base + EXYNOS5440_TMU_S0_7_TEMP);
  877. }
  878. static int exynos7_tmu_read(struct exynos_tmu_data *data)
  879. {
  880. return readw(data->base + EXYNOS_TMU_REG_CURRENT_TEMP) &
  881. EXYNOS7_TMU_TEMP_MASK;
  882. }
  883. static void exynos_tmu_work(struct work_struct *work)
  884. {
  885. struct exynos_tmu_data *data = container_of(work,
  886. struct exynos_tmu_data, irq_work);
  887. unsigned int val_type;
  888. if (!IS_ERR(data->clk_sec))
  889. clk_enable(data->clk_sec);
  890. /* Find which sensor generated this interrupt */
  891. if (data->soc == SOC_ARCH_EXYNOS5440) {
  892. val_type = readl(data->base_second + EXYNOS5440_TMU_IRQ_STATUS);
  893. if (!((val_type >> data->id) & 0x1))
  894. goto out;
  895. }
  896. if (!IS_ERR(data->clk_sec))
  897. clk_disable(data->clk_sec);
  898. exynos_report_trigger(data);
  899. mutex_lock(&data->lock);
  900. clk_enable(data->clk);
  901. /* TODO: take action based on particular interrupt */
  902. data->tmu_clear_irqs(data);
  903. clk_disable(data->clk);
  904. mutex_unlock(&data->lock);
  905. out:
  906. enable_irq(data->irq);
  907. }
  908. static void exynos4210_tmu_clear_irqs(struct exynos_tmu_data *data)
  909. {
  910. unsigned int val_irq;
  911. u32 tmu_intstat, tmu_intclear;
  912. if (data->soc == SOC_ARCH_EXYNOS5260) {
  913. tmu_intstat = EXYNOS5260_TMU_REG_INTSTAT;
  914. tmu_intclear = EXYNOS5260_TMU_REG_INTCLEAR;
  915. } else if (data->soc == SOC_ARCH_EXYNOS7) {
  916. tmu_intstat = EXYNOS7_TMU_REG_INTPEND;
  917. tmu_intclear = EXYNOS7_TMU_REG_INTPEND;
  918. } else if (data->soc == SOC_ARCH_EXYNOS5433) {
  919. tmu_intstat = EXYNOS5433_TMU_REG_INTPEND;
  920. tmu_intclear = EXYNOS5433_TMU_REG_INTPEND;
  921. } else {
  922. tmu_intstat = EXYNOS_TMU_REG_INTSTAT;
  923. tmu_intclear = EXYNOS_TMU_REG_INTCLEAR;
  924. }
  925. val_irq = readl(data->base + tmu_intstat);
  926. /*
  927. * Clear the interrupts. Please note that the documentation for
  928. * Exynos3250, Exynos4412, Exynos5250 and Exynos5260 incorrectly
  929. * states that INTCLEAR register has a different placing of bits
  930. * responsible for FALL IRQs than INTSTAT register. Exynos5420
  931. * and Exynos5440 documentation is correct (Exynos4210 doesn't
  932. * support FALL IRQs at all).
  933. */
  934. writel(val_irq, data->base + tmu_intclear);
  935. }
  936. static void exynos5440_tmu_clear_irqs(struct exynos_tmu_data *data)
  937. {
  938. unsigned int val_irq;
  939. val_irq = readl(data->base + EXYNOS5440_TMU_S0_7_IRQ);
  940. /* clear the interrupts */
  941. writel(val_irq, data->base + EXYNOS5440_TMU_S0_7_IRQ);
  942. }
  943. static irqreturn_t exynos_tmu_irq(int irq, void *id)
  944. {
  945. struct exynos_tmu_data *data = id;
  946. disable_irq_nosync(irq);
  947. schedule_work(&data->irq_work);
  948. return IRQ_HANDLED;
  949. }
  950. static const struct of_device_id exynos_tmu_match[] = {
  951. { .compatible = "samsung,exynos3250-tmu", },
  952. { .compatible = "samsung,exynos4210-tmu", },
  953. { .compatible = "samsung,exynos4412-tmu", },
  954. { .compatible = "samsung,exynos5250-tmu", },
  955. { .compatible = "samsung,exynos5260-tmu", },
  956. { .compatible = "samsung,exynos5420-tmu", },
  957. { .compatible = "samsung,exynos5420-tmu-ext-triminfo", },
  958. { .compatible = "samsung,exynos5433-tmu", },
  959. { .compatible = "samsung,exynos5440-tmu", },
  960. { .compatible = "samsung,exynos7-tmu", },
  961. { /* sentinel */ },
  962. };
  963. MODULE_DEVICE_TABLE(of, exynos_tmu_match);
  964. static int exynos_of_get_soc_type(struct device_node *np)
  965. {
  966. if (of_device_is_compatible(np, "samsung,exynos3250-tmu"))
  967. return SOC_ARCH_EXYNOS3250;
  968. else if (of_device_is_compatible(np, "samsung,exynos4210-tmu"))
  969. return SOC_ARCH_EXYNOS4210;
  970. else if (of_device_is_compatible(np, "samsung,exynos4412-tmu"))
  971. return SOC_ARCH_EXYNOS4412;
  972. else if (of_device_is_compatible(np, "samsung,exynos5250-tmu"))
  973. return SOC_ARCH_EXYNOS5250;
  974. else if (of_device_is_compatible(np, "samsung,exynos5260-tmu"))
  975. return SOC_ARCH_EXYNOS5260;
  976. else if (of_device_is_compatible(np, "samsung,exynos5420-tmu"))
  977. return SOC_ARCH_EXYNOS5420;
  978. else if (of_device_is_compatible(np,
  979. "samsung,exynos5420-tmu-ext-triminfo"))
  980. return SOC_ARCH_EXYNOS5420_TRIMINFO;
  981. else if (of_device_is_compatible(np, "samsung,exynos5433-tmu"))
  982. return SOC_ARCH_EXYNOS5433;
  983. else if (of_device_is_compatible(np, "samsung,exynos5440-tmu"))
  984. return SOC_ARCH_EXYNOS5440;
  985. else if (of_device_is_compatible(np, "samsung,exynos7-tmu"))
  986. return SOC_ARCH_EXYNOS7;
  987. return -EINVAL;
  988. }
  989. static int exynos_of_sensor_conf(struct device_node *np,
  990. struct exynos_tmu_platform_data *pdata)
  991. {
  992. u32 value;
  993. int ret;
  994. of_node_get(np);
  995. ret = of_property_read_u32(np, "samsung,tmu_gain", &value);
  996. pdata->gain = (u8)value;
  997. of_property_read_u32(np, "samsung,tmu_reference_voltage", &value);
  998. pdata->reference_voltage = (u8)value;
  999. of_property_read_u32(np, "samsung,tmu_noise_cancel_mode", &value);
  1000. pdata->noise_cancel_mode = (u8)value;
  1001. of_property_read_u32(np, "samsung,tmu_efuse_value",
  1002. &pdata->efuse_value);
  1003. of_property_read_u32(np, "samsung,tmu_min_efuse_value",
  1004. &pdata->min_efuse_value);
  1005. of_property_read_u32(np, "samsung,tmu_max_efuse_value",
  1006. &pdata->max_efuse_value);
  1007. of_property_read_u32(np, "samsung,tmu_first_point_trim", &value);
  1008. pdata->first_point_trim = (u8)value;
  1009. of_property_read_u32(np, "samsung,tmu_second_point_trim", &value);
  1010. pdata->second_point_trim = (u8)value;
  1011. of_property_read_u32(np, "samsung,tmu_default_temp_offset", &value);
  1012. pdata->default_temp_offset = (u8)value;
  1013. of_property_read_u32(np, "samsung,tmu_cal_type", &pdata->cal_type);
  1014. of_property_read_u32(np, "samsung,tmu_cal_mode", &pdata->cal_mode);
  1015. of_node_put(np);
  1016. return 0;
  1017. }
  1018. static int exynos_map_dt_data(struct platform_device *pdev)
  1019. {
  1020. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  1021. struct exynos_tmu_platform_data *pdata;
  1022. struct resource res;
  1023. if (!data || !pdev->dev.of_node)
  1024. return -ENODEV;
  1025. data->id = of_alias_get_id(pdev->dev.of_node, "tmuctrl");
  1026. if (data->id < 0)
  1027. data->id = 0;
  1028. data->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1029. if (data->irq <= 0) {
  1030. dev_err(&pdev->dev, "failed to get IRQ\n");
  1031. return -ENODEV;
  1032. }
  1033. if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
  1034. dev_err(&pdev->dev, "failed to get Resource 0\n");
  1035. return -ENODEV;
  1036. }
  1037. data->base = devm_ioremap(&pdev->dev, res.start, resource_size(&res));
  1038. if (!data->base) {
  1039. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  1040. return -EADDRNOTAVAIL;
  1041. }
  1042. pdata = devm_kzalloc(&pdev->dev,
  1043. sizeof(struct exynos_tmu_platform_data),
  1044. GFP_KERNEL);
  1045. if (!pdata)
  1046. return -ENOMEM;
  1047. exynos_of_sensor_conf(pdev->dev.of_node, pdata);
  1048. data->pdata = pdata;
  1049. data->soc = exynos_of_get_soc_type(pdev->dev.of_node);
  1050. switch (data->soc) {
  1051. case SOC_ARCH_EXYNOS4210:
  1052. data->tmu_initialize = exynos4210_tmu_initialize;
  1053. data->tmu_control = exynos4210_tmu_control;
  1054. data->tmu_read = exynos4210_tmu_read;
  1055. data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
  1056. data->ntrip = 4;
  1057. break;
  1058. case SOC_ARCH_EXYNOS3250:
  1059. case SOC_ARCH_EXYNOS4412:
  1060. case SOC_ARCH_EXYNOS5250:
  1061. case SOC_ARCH_EXYNOS5260:
  1062. case SOC_ARCH_EXYNOS5420:
  1063. case SOC_ARCH_EXYNOS5420_TRIMINFO:
  1064. data->tmu_initialize = exynos4412_tmu_initialize;
  1065. data->tmu_control = exynos4210_tmu_control;
  1066. data->tmu_read = exynos4412_tmu_read;
  1067. data->tmu_set_emulation = exynos4412_tmu_set_emulation;
  1068. data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
  1069. data->ntrip = 4;
  1070. break;
  1071. case SOC_ARCH_EXYNOS5433:
  1072. data->tmu_initialize = exynos5433_tmu_initialize;
  1073. data->tmu_control = exynos5433_tmu_control;
  1074. data->tmu_read = exynos4412_tmu_read;
  1075. data->tmu_set_emulation = exynos4412_tmu_set_emulation;
  1076. data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
  1077. data->ntrip = 8;
  1078. break;
  1079. case SOC_ARCH_EXYNOS5440:
  1080. data->tmu_initialize = exynos5440_tmu_initialize;
  1081. data->tmu_control = exynos5440_tmu_control;
  1082. data->tmu_read = exynos5440_tmu_read;
  1083. data->tmu_set_emulation = exynos5440_tmu_set_emulation;
  1084. data->tmu_clear_irqs = exynos5440_tmu_clear_irqs;
  1085. data->ntrip = 4;
  1086. break;
  1087. case SOC_ARCH_EXYNOS7:
  1088. data->tmu_initialize = exynos7_tmu_initialize;
  1089. data->tmu_control = exynos7_tmu_control;
  1090. data->tmu_read = exynos7_tmu_read;
  1091. data->tmu_set_emulation = exynos4412_tmu_set_emulation;
  1092. data->tmu_clear_irqs = exynos4210_tmu_clear_irqs;
  1093. data->ntrip = 8;
  1094. break;
  1095. default:
  1096. dev_err(&pdev->dev, "Platform not supported\n");
  1097. return -EINVAL;
  1098. }
  1099. /*
  1100. * Check if the TMU shares some registers and then try to map the
  1101. * memory of common registers.
  1102. */
  1103. if (data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO &&
  1104. data->soc != SOC_ARCH_EXYNOS5440)
  1105. return 0;
  1106. if (of_address_to_resource(pdev->dev.of_node, 1, &res)) {
  1107. dev_err(&pdev->dev, "failed to get Resource 1\n");
  1108. return -ENODEV;
  1109. }
  1110. data->base_second = devm_ioremap(&pdev->dev, res.start,
  1111. resource_size(&res));
  1112. if (!data->base_second) {
  1113. dev_err(&pdev->dev, "Failed to ioremap memory\n");
  1114. return -ENOMEM;
  1115. }
  1116. return 0;
  1117. }
  1118. static struct thermal_zone_of_device_ops exynos_sensor_ops = {
  1119. .get_temp = exynos_get_temp,
  1120. .set_emul_temp = exynos_tmu_set_emulation,
  1121. };
  1122. static int exynos_tmu_probe(struct platform_device *pdev)
  1123. {
  1124. struct exynos_tmu_data *data;
  1125. int ret;
  1126. data = devm_kzalloc(&pdev->dev, sizeof(struct exynos_tmu_data),
  1127. GFP_KERNEL);
  1128. if (!data)
  1129. return -ENOMEM;
  1130. platform_set_drvdata(pdev, data);
  1131. mutex_init(&data->lock);
  1132. /*
  1133. * Try enabling the regulator if found
  1134. * TODO: Add regulator as an SOC feature, so that regulator enable
  1135. * is a compulsory call.
  1136. */
  1137. data->regulator = devm_regulator_get_optional(&pdev->dev, "vtmu");
  1138. if (!IS_ERR(data->regulator)) {
  1139. ret = regulator_enable(data->regulator);
  1140. if (ret) {
  1141. dev_err(&pdev->dev, "failed to enable vtmu\n");
  1142. return ret;
  1143. }
  1144. } else {
  1145. if (PTR_ERR(data->regulator) == -EPROBE_DEFER)
  1146. return -EPROBE_DEFER;
  1147. dev_info(&pdev->dev, "Regulator node (vtmu) not found\n");
  1148. }
  1149. ret = exynos_map_dt_data(pdev);
  1150. if (ret)
  1151. goto err_sensor;
  1152. INIT_WORK(&data->irq_work, exynos_tmu_work);
  1153. data->clk = devm_clk_get(&pdev->dev, "tmu_apbif");
  1154. if (IS_ERR(data->clk)) {
  1155. dev_err(&pdev->dev, "Failed to get clock\n");
  1156. ret = PTR_ERR(data->clk);
  1157. goto err_sensor;
  1158. }
  1159. data->clk_sec = devm_clk_get(&pdev->dev, "tmu_triminfo_apbif");
  1160. if (IS_ERR(data->clk_sec)) {
  1161. if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO) {
  1162. dev_err(&pdev->dev, "Failed to get triminfo clock\n");
  1163. ret = PTR_ERR(data->clk_sec);
  1164. goto err_sensor;
  1165. }
  1166. } else {
  1167. ret = clk_prepare(data->clk_sec);
  1168. if (ret) {
  1169. dev_err(&pdev->dev, "Failed to get clock\n");
  1170. goto err_sensor;
  1171. }
  1172. }
  1173. ret = clk_prepare(data->clk);
  1174. if (ret) {
  1175. dev_err(&pdev->dev, "Failed to get clock\n");
  1176. goto err_clk_sec;
  1177. }
  1178. switch (data->soc) {
  1179. case SOC_ARCH_EXYNOS5433:
  1180. case SOC_ARCH_EXYNOS7:
  1181. data->sclk = devm_clk_get(&pdev->dev, "tmu_sclk");
  1182. if (IS_ERR(data->sclk)) {
  1183. dev_err(&pdev->dev, "Failed to get sclk\n");
  1184. goto err_clk;
  1185. } else {
  1186. ret = clk_prepare_enable(data->sclk);
  1187. if (ret) {
  1188. dev_err(&pdev->dev, "Failed to enable sclk\n");
  1189. goto err_clk;
  1190. }
  1191. }
  1192. break;
  1193. default:
  1194. break;
  1195. }
  1196. /*
  1197. * data->tzd must be registered before calling exynos_tmu_initialize(),
  1198. * requesting irq and calling exynos_tmu_control().
  1199. */
  1200. data->tzd = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
  1201. &exynos_sensor_ops);
  1202. if (IS_ERR(data->tzd)) {
  1203. ret = PTR_ERR(data->tzd);
  1204. dev_err(&pdev->dev, "Failed to register sensor: %d\n", ret);
  1205. goto err_sclk;
  1206. }
  1207. ret = exynos_tmu_initialize(pdev);
  1208. if (ret) {
  1209. dev_err(&pdev->dev, "Failed to initialize TMU\n");
  1210. goto err_thermal;
  1211. }
  1212. ret = devm_request_irq(&pdev->dev, data->irq, exynos_tmu_irq,
  1213. IRQF_TRIGGER_RISING | IRQF_SHARED, dev_name(&pdev->dev), data);
  1214. if (ret) {
  1215. dev_err(&pdev->dev, "Failed to request irq: %d\n", data->irq);
  1216. goto err_thermal;
  1217. }
  1218. exynos_tmu_control(pdev, true);
  1219. return 0;
  1220. err_thermal:
  1221. thermal_zone_of_sensor_unregister(&pdev->dev, data->tzd);
  1222. err_sclk:
  1223. clk_disable_unprepare(data->sclk);
  1224. err_clk:
  1225. clk_unprepare(data->clk);
  1226. err_clk_sec:
  1227. if (!IS_ERR(data->clk_sec))
  1228. clk_unprepare(data->clk_sec);
  1229. err_sensor:
  1230. if (!IS_ERR(data->regulator))
  1231. regulator_disable(data->regulator);
  1232. return ret;
  1233. }
  1234. static int exynos_tmu_remove(struct platform_device *pdev)
  1235. {
  1236. struct exynos_tmu_data *data = platform_get_drvdata(pdev);
  1237. struct thermal_zone_device *tzd = data->tzd;
  1238. thermal_zone_of_sensor_unregister(&pdev->dev, tzd);
  1239. exynos_tmu_control(pdev, false);
  1240. clk_disable_unprepare(data->sclk);
  1241. clk_unprepare(data->clk);
  1242. if (!IS_ERR(data->clk_sec))
  1243. clk_unprepare(data->clk_sec);
  1244. if (!IS_ERR(data->regulator))
  1245. regulator_disable(data->regulator);
  1246. return 0;
  1247. }
  1248. #ifdef CONFIG_PM_SLEEP
  1249. static int exynos_tmu_suspend(struct device *dev)
  1250. {
  1251. exynos_tmu_control(to_platform_device(dev), false);
  1252. return 0;
  1253. }
  1254. static int exynos_tmu_resume(struct device *dev)
  1255. {
  1256. struct platform_device *pdev = to_platform_device(dev);
  1257. exynos_tmu_initialize(pdev);
  1258. exynos_tmu_control(pdev, true);
  1259. return 0;
  1260. }
  1261. static SIMPLE_DEV_PM_OPS(exynos_tmu_pm,
  1262. exynos_tmu_suspend, exynos_tmu_resume);
  1263. #define EXYNOS_TMU_PM (&exynos_tmu_pm)
  1264. #else
  1265. #define EXYNOS_TMU_PM NULL
  1266. #endif
  1267. static struct platform_driver exynos_tmu_driver = {
  1268. .driver = {
  1269. .name = "exynos-tmu",
  1270. .pm = EXYNOS_TMU_PM,
  1271. .of_match_table = exynos_tmu_match,
  1272. },
  1273. .probe = exynos_tmu_probe,
  1274. .remove = exynos_tmu_remove,
  1275. };
  1276. module_platform_driver(exynos_tmu_driver);
  1277. MODULE_DESCRIPTION("EXYNOS TMU Driver");
  1278. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  1279. MODULE_LICENSE("GPL");
  1280. MODULE_ALIAS("platform:exynos-tmu");