f35289b1cea98aa0cd9a5b0013394e8aef701c71
[linux-2.6-block.git] / drivers / thermal / armada_thermal.c
1 /*
2  * Marvell Armada 370/XP thermal sensor driver
3  *
4  * Copyright (C) 2013 Marvell
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  */
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include <linux/of.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/of_device.h>
25 #include <linux/thermal.h>
26
27 /* Thermal Manager Control and Status Register */
28 #define PMU_TDC0_SW_RST_MASK            (0x1 << 1)
29 #define PMU_TM_DISABLE_OFFS             0
30 #define PMU_TM_DISABLE_MASK             (0x1 << PMU_TM_DISABLE_OFFS)
31 #define PMU_TDC0_REF_CAL_CNT_OFFS       11
32 #define PMU_TDC0_REF_CAL_CNT_MASK       (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS)
33 #define PMU_TDC0_OTF_CAL_MASK           (0x1 << 30)
34 #define PMU_TDC0_START_CAL_MASK         (0x1 << 25)
35
36 #define A375_UNIT_CONTROL_SHIFT         27
37 #define A375_UNIT_CONTROL_MASK          0x7
38 #define A375_READOUT_INVERT             BIT(15)
39 #define A375_HW_RESETn                  BIT(8)
40 #define A380_HW_RESET                   BIT(8)
41
42 /* Legacy bindings */
43 #define LEGACY_CONTROL_MEM_LEN          0x4
44
45 /* Current bindings with the 2 control registers under the same memory area */
46 #define LEGACY_CONTROL1_OFFSET          0x0
47 #define CONTROL0_OFFSET                 0x0
48 #define CONTROL1_OFFSET                 0x4
49
50 /* TSEN refers to the temperature sensors within the AP */
51 #define CONTROL0_TSEN_START             BIT(0)
52 #define CONTROL0_TSEN_RESET             BIT(1)
53 #define CONTROL0_TSEN_ENABLE            BIT(2)
54
55 struct armada_thermal_data;
56
57 /* Marvell EBU Thermal Sensor Dev Structure */
58 struct armada_thermal_priv {
59         void __iomem *status;
60         void __iomem *control0;
61         void __iomem *control1;
62         struct armada_thermal_data *data;
63 };
64
65 struct armada_thermal_data {
66         /* Initialize the sensor */
67         void (*init_sensor)(struct platform_device *pdev,
68                             struct armada_thermal_priv *);
69
70         /* Test for a valid sensor value (optional) */
71         bool (*is_valid)(struct armada_thermal_priv *);
72
73         /* Formula coeficients: temp = (b - m * reg) / div */
74         s64 coef_b;
75         s64 coef_m;
76         u32 coef_div;
77         bool inverted;
78         bool signed_sample;
79
80         /* Register shift and mask to access the sensor temperature */
81         unsigned int temp_shift;
82         unsigned int temp_mask;
83         u32 is_valid_bit;
84         bool needs_control0;
85 };
86
87 static void armadaxp_init_sensor(struct platform_device *pdev,
88                                  struct armada_thermal_priv *priv)
89 {
90         u32 reg;
91
92         reg = readl_relaxed(priv->control1);
93         reg |= PMU_TDC0_OTF_CAL_MASK;
94         writel(reg, priv->control1);
95
96         /* Reference calibration value */
97         reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
98         reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
99         writel(reg, priv->control1);
100
101         /* Reset the sensor */
102         reg = readl_relaxed(priv->control1);
103         writel((reg | PMU_TDC0_SW_RST_MASK), priv->control1);
104
105         writel(reg, priv->control1);
106
107         /* Enable the sensor */
108         reg = readl_relaxed(priv->status);
109         reg &= ~PMU_TM_DISABLE_MASK;
110         writel(reg, priv->status);
111 }
112
113 static void armada370_init_sensor(struct platform_device *pdev,
114                                   struct armada_thermal_priv *priv)
115 {
116         u32 reg;
117
118         reg = readl_relaxed(priv->control1);
119         reg |= PMU_TDC0_OTF_CAL_MASK;
120         writel(reg, priv->control1);
121
122         /* Reference calibration value */
123         reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
124         reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
125         writel(reg, priv->control1);
126
127         reg &= ~PMU_TDC0_START_CAL_MASK;
128         writel(reg, priv->control1);
129
130         msleep(10);
131 }
132
133 static void armada375_init_sensor(struct platform_device *pdev,
134                                   struct armada_thermal_priv *priv)
135 {
136         u32 reg;
137
138         reg = readl(priv->control1);
139         reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT);
140         reg &= ~A375_READOUT_INVERT;
141         reg &= ~A375_HW_RESETn;
142
143         writel(reg, priv->control1);
144         msleep(20);
145
146         reg |= A375_HW_RESETn;
147         writel(reg, priv->control1);
148         msleep(50);
149 }
150
151 static void armada380_init_sensor(struct platform_device *pdev,
152                                   struct armada_thermal_priv *priv)
153 {
154         u32 reg = readl_relaxed(priv->control1);
155
156         /* Reset hardware once */
157         if (!(reg & A380_HW_RESET)) {
158                 reg |= A380_HW_RESET;
159                 writel(reg, priv->control1);
160                 msleep(10);
161         }
162 }
163
164 static void armada_ap806_init_sensor(struct platform_device *pdev,
165                                      struct armada_thermal_priv *priv)
166 {
167         u32 reg;
168
169         reg = readl_relaxed(priv->control0);
170         reg &= ~CONTROL0_TSEN_RESET;
171         reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_ENABLE;
172         writel(reg, priv->control0);
173         msleep(10);
174 }
175
176 static bool armada_is_valid(struct armada_thermal_priv *priv)
177 {
178         u32 reg = readl_relaxed(priv->status);
179
180         return reg & priv->data->is_valid_bit;
181 }
182
183 static int armada_get_temp(struct thermal_zone_device *thermal,
184                            int *temp)
185 {
186         struct armada_thermal_priv *priv = thermal->devdata;
187         u32 reg, div;
188         s64 sample, b, m;
189
190         /* Valid check */
191         if (priv->data->is_valid && !priv->data->is_valid(priv)) {
192                 dev_err(&thermal->device,
193                         "Temperature sensor reading not valid\n");
194                 return -EIO;
195         }
196
197         reg = readl_relaxed(priv->status);
198         reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
199         if (priv->data->signed_sample)
200                 /* The most significant bit is the sign bit */
201                 sample = sign_extend32(reg, fls(priv->data->temp_mask) - 1);
202         else
203                 sample = reg;
204
205         /* Get formula coeficients */
206         b = priv->data->coef_b;
207         m = priv->data->coef_m;
208         div = priv->data->coef_div;
209
210         if (priv->data->inverted)
211                 *temp = div_s64((m * sample) - b, div);
212         else
213                 *temp = div_s64(b - (m * sample), div);
214
215         return 0;
216 }
217
218 static struct thermal_zone_device_ops ops = {
219         .get_temp = armada_get_temp,
220 };
221
222 static const struct armada_thermal_data armadaxp_data = {
223         .init_sensor = armadaxp_init_sensor,
224         .temp_shift = 10,
225         .temp_mask = 0x1ff,
226         .coef_b = 3153000000ULL,
227         .coef_m = 10000000ULL,
228         .coef_div = 13825,
229 };
230
231 static const struct armada_thermal_data armada370_data = {
232         .is_valid = armada_is_valid,
233         .init_sensor = armada370_init_sensor,
234         .is_valid_bit = BIT(9),
235         .temp_shift = 10,
236         .temp_mask = 0x1ff,
237         .coef_b = 3153000000ULL,
238         .coef_m = 10000000ULL,
239         .coef_div = 13825,
240 };
241
242 static const struct armada_thermal_data armada375_data = {
243         .is_valid = armada_is_valid,
244         .init_sensor = armada375_init_sensor,
245         .is_valid_bit = BIT(10),
246         .temp_shift = 0,
247         .temp_mask = 0x1ff,
248         .coef_b = 3171900000ULL,
249         .coef_m = 10000000ULL,
250         .coef_div = 13616,
251         .needs_control0 = true,
252 };
253
254 static const struct armada_thermal_data armada380_data = {
255         .is_valid = armada_is_valid,
256         .init_sensor = armada380_init_sensor,
257         .is_valid_bit = BIT(10),
258         .temp_shift = 0,
259         .temp_mask = 0x3ff,
260         .coef_b = 1172499100ULL,
261         .coef_m = 2000096ULL,
262         .coef_div = 4201,
263         .inverted = true,
264 };
265
266 static const struct armada_thermal_data armada_ap806_data = {
267         .is_valid = armada_is_valid,
268         .init_sensor = armada_ap806_init_sensor,
269         .is_valid_bit = BIT(16),
270         .temp_shift = 0,
271         .temp_mask = 0x3ff,
272         .coef_b = -150000LL,
273         .coef_m = 423ULL,
274         .coef_div = 1,
275         .inverted = true,
276         .signed_sample = true,
277         .needs_control0 = true,
278 };
279
280 static const struct of_device_id armada_thermal_id_table[] = {
281         {
282                 .compatible = "marvell,armadaxp-thermal",
283                 .data       = &armadaxp_data,
284         },
285         {
286                 .compatible = "marvell,armada370-thermal",
287                 .data       = &armada370_data,
288         },
289         {
290                 .compatible = "marvell,armada375-thermal",
291                 .data       = &armada375_data,
292         },
293         {
294                 .compatible = "marvell,armada380-thermal",
295                 .data       = &armada380_data,
296         },
297         {
298                 .compatible = "marvell,armada-ap806-thermal",
299                 .data       = &armada_ap806_data,
300         },
301         {
302                 /* sentinel */
303         },
304 };
305 MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
306
307 static int armada_thermal_probe(struct platform_device *pdev)
308 {
309         void __iomem *control = NULL;
310         struct thermal_zone_device *thermal;
311         const struct of_device_id *match;
312         struct armada_thermal_priv *priv;
313         struct resource *res;
314
315         match = of_match_device(armada_thermal_id_table, &pdev->dev);
316         if (!match)
317                 return -ENODEV;
318
319         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
320         if (!priv)
321                 return -ENOMEM;
322
323         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
324         priv->status = devm_ioremap_resource(&pdev->dev, res);
325         if (IS_ERR(priv->status))
326                 return PTR_ERR(priv->status);
327
328         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
329         control = devm_ioremap_resource(&pdev->dev, res);
330         if (IS_ERR(control))
331                 return PTR_ERR(control);
332
333         priv->data = (struct armada_thermal_data *)match->data;
334
335         /*
336          * Legacy DT bindings only described "control1" register (also referred
337          * as "control MSB" on old documentation). New bindings cover
338          * "control0/control LSB" and "control1/control MSB" registers within
339          * the same resource, which is then of size 8 instead of 4.
340          */
341         if (resource_size(res) == LEGACY_CONTROL_MEM_LEN) {
342                 /* ->control0 unavailable in this configuration */
343                 if (priv->data->needs_control0) {
344                         dev_err(&pdev->dev, "No access to control0 register\n");
345                         return -EINVAL;
346                 }
347
348                 priv->control1 = control + LEGACY_CONTROL1_OFFSET;
349         } else {
350                 priv->control0 = control + CONTROL0_OFFSET;
351                 priv->control1 = control + CONTROL1_OFFSET;
352         }
353
354         priv->data->init_sensor(pdev, priv);
355
356         thermal = thermal_zone_device_register("armada_thermal", 0, 0,
357                                                priv, &ops, NULL, 0, 0);
358         if (IS_ERR(thermal)) {
359                 dev_err(&pdev->dev,
360                         "Failed to register thermal zone device\n");
361                 return PTR_ERR(thermal);
362         }
363
364         platform_set_drvdata(pdev, thermal);
365
366         return 0;
367 }
368
369 static int armada_thermal_exit(struct platform_device *pdev)
370 {
371         struct thermal_zone_device *armada_thermal =
372                 platform_get_drvdata(pdev);
373
374         thermal_zone_device_unregister(armada_thermal);
375
376         return 0;
377 }
378
379 static struct platform_driver armada_thermal_driver = {
380         .probe = armada_thermal_probe,
381         .remove = armada_thermal_exit,
382         .driver = {
383                 .name = "armada_thermal",
384                 .of_match_table = armada_thermal_id_table,
385         },
386 };
387
388 module_platform_driver(armada_thermal_driver);
389
390 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
391 MODULE_DESCRIPTION("Armada 370/XP thermal driver");
392 MODULE_LICENSE("GPL v2");