exynos_tmu.c 42 KB

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