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