2 * Marvell Armada 370/XP thermal sensor driver
4 * Copyright (C) 2013 Marvell
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.
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.
16 #include <linux/device.h>
17 #include <linux/err.h>
19 #include <linux/kernel.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>
27 #define THERMAL_VALID_MASK 0x1
29 /* Thermal Manager Control and Status Register */
30 #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
31 #define PMU_TM_DISABLE_OFFS 0
32 #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
33 #define PMU_TDC0_REF_CAL_CNT_OFFS 11
34 #define PMU_TDC0_REF_CAL_CNT_MASK (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS)
35 #define PMU_TDC0_OTF_CAL_MASK (0x1 << 30)
36 #define PMU_TDC0_START_CAL_MASK (0x1 << 25)
38 #define A375_Z1_CAL_RESET_LSB 0x8011e214
39 #define A375_Z1_CAL_RESET_MSB 0x30a88019
40 #define A375_Z1_WORKAROUND_BIT BIT(9)
42 #define A375_UNIT_CONTROL_SHIFT 27
43 #define A375_UNIT_CONTROL_MASK 0x7
44 #define A375_READOUT_INVERT BIT(15)
45 #define A375_HW_RESETn BIT(8)
46 #define A380_HW_RESET BIT(8)
48 struct armada_thermal_data
;
50 /* Marvell EBU Thermal Sensor Dev Structure */
51 struct armada_thermal_priv
{
53 void __iomem
*control
;
54 struct armada_thermal_data
*data
;
57 struct armada_thermal_data
{
58 /* Initialize the sensor */
59 void (*init_sensor
)(struct platform_device
*pdev
,
60 struct armada_thermal_priv
*);
62 /* Test for a valid sensor value (optional) */
63 bool (*is_valid
)(struct armada_thermal_priv
*);
65 /* Formula coeficients: temp = (b + m * reg) / div */
68 unsigned long coef_div
;
71 /* Register shift and mask to access the sensor temperature */
72 unsigned int temp_shift
;
73 unsigned int temp_mask
;
74 unsigned int is_valid_shift
;
77 static void armadaxp_init_sensor(struct platform_device
*pdev
,
78 struct armada_thermal_priv
*priv
)
82 reg
= readl_relaxed(priv
->control
);
83 reg
|= PMU_TDC0_OTF_CAL_MASK
;
84 writel(reg
, priv
->control
);
86 /* Reference calibration value */
87 reg
&= ~PMU_TDC0_REF_CAL_CNT_MASK
;
88 reg
|= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS
);
89 writel(reg
, priv
->control
);
91 /* Reset the sensor */
92 reg
= readl_relaxed(priv
->control
);
93 writel((reg
| PMU_TDC0_SW_RST_MASK
), priv
->control
);
95 writel(reg
, priv
->control
);
97 /* Enable the sensor */
98 reg
= readl_relaxed(priv
->sensor
);
99 reg
&= ~PMU_TM_DISABLE_MASK
;
100 writel(reg
, priv
->sensor
);
103 static void armada370_init_sensor(struct platform_device
*pdev
,
104 struct armada_thermal_priv
*priv
)
108 reg
= readl_relaxed(priv
->control
);
109 reg
|= PMU_TDC0_OTF_CAL_MASK
;
110 writel(reg
, priv
->control
);
112 /* Reference calibration value */
113 reg
&= ~PMU_TDC0_REF_CAL_CNT_MASK
;
114 reg
|= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS
);
115 writel(reg
, priv
->control
);
117 reg
&= ~PMU_TDC0_START_CAL_MASK
;
118 writel(reg
, priv
->control
);
123 static void armada375_init_sensor(struct platform_device
*pdev
,
124 struct armada_thermal_priv
*priv
)
128 !!of_device_is_compatible(pdev
->dev
.of_node
,
129 "marvell,armada375-z1-thermal");
132 /* Ensure these registers have the default (reset) values */
133 writel(A375_Z1_CAL_RESET_LSB
, priv
->control
);
134 writel(A375_Z1_CAL_RESET_MSB
, priv
->control
+ 0x4);
137 reg
= readl(priv
->control
+ 4);
138 reg
&= ~(A375_UNIT_CONTROL_MASK
<< A375_UNIT_CONTROL_SHIFT
);
139 reg
&= ~A375_READOUT_INVERT
;
140 reg
&= ~A375_HW_RESETn
;
143 reg
|= A375_Z1_WORKAROUND_BIT
;
145 writel(reg
, priv
->control
+ 4);
148 reg
|= A375_HW_RESETn
;
149 writel(reg
, priv
->control
+ 4);
153 static void armada380_init_sensor(struct platform_device
*pdev
,
154 struct armada_thermal_priv
*priv
)
156 unsigned long reg
= readl_relaxed(priv
->control
);
158 /* Reset hardware once */
159 if (!(reg
& A380_HW_RESET
)) {
160 reg
|= A380_HW_RESET
;
161 writel(reg
, priv
->control
);
166 static bool armada_is_valid(struct armada_thermal_priv
*priv
)
168 unsigned long reg
= readl_relaxed(priv
->sensor
);
170 return (reg
>> priv
->data
->is_valid_shift
) & THERMAL_VALID_MASK
;
173 static int armada_get_temp(struct thermal_zone_device
*thermal
,
176 struct armada_thermal_priv
*priv
= thermal
->devdata
;
178 unsigned long m
, b
, div
;
181 if (priv
->data
->is_valid
&& !priv
->data
->is_valid(priv
)) {
182 dev_err(&thermal
->device
,
183 "Temperature sensor reading not valid\n");
187 reg
= readl_relaxed(priv
->sensor
);
188 reg
= (reg
>> priv
->data
->temp_shift
) & priv
->data
->temp_mask
;
190 /* Get formula coeficients */
191 b
= priv
->data
->coef_b
;
192 m
= priv
->data
->coef_m
;
193 div
= priv
->data
->coef_div
;
195 if (priv
->data
->inverted
)
196 *temp
= ((m
* reg
) - b
) / div
;
198 *temp
= (b
- (m
* reg
)) / div
;
202 static struct thermal_zone_device_ops ops
= {
203 .get_temp
= armada_get_temp
,
206 static const struct armada_thermal_data armadaxp_data
= {
207 .init_sensor
= armadaxp_init_sensor
,
210 .coef_b
= 3153000000UL,
211 .coef_m
= 10000000UL,
215 static const struct armada_thermal_data armada370_data
= {
216 .is_valid
= armada_is_valid
,
217 .init_sensor
= armada370_init_sensor
,
221 .coef_b
= 3153000000UL,
222 .coef_m
= 10000000UL,
226 static const struct armada_thermal_data armada375_data
= {
227 .is_valid
= armada_is_valid
,
228 .init_sensor
= armada375_init_sensor
,
229 .is_valid_shift
= 10,
232 .coef_b
= 3171900000UL,
233 .coef_m
= 10000000UL,
237 static const struct armada_thermal_data armada380_data
= {
238 .is_valid
= armada_is_valid
,
239 .init_sensor
= armada380_init_sensor
,
240 .is_valid_shift
= 10,
243 .coef_b
= 1169498786UL,
249 static const struct of_device_id armada_thermal_id_table
[] = {
251 .compatible
= "marvell,armadaxp-thermal",
252 .data
= &armadaxp_data
,
255 .compatible
= "marvell,armada370-thermal",
256 .data
= &armada370_data
,
259 .compatible
= "marvell,armada375-thermal",
260 .data
= &armada375_data
,
263 .compatible
= "marvell,armada375-z1-thermal",
264 .data
= &armada375_data
,
267 .compatible
= "marvell,armada380-thermal",
268 .data
= &armada380_data
,
274 MODULE_DEVICE_TABLE(of
, armada_thermal_id_table
);
276 static int armada_thermal_probe(struct platform_device
*pdev
)
278 struct thermal_zone_device
*thermal
;
279 const struct of_device_id
*match
;
280 struct armada_thermal_priv
*priv
;
281 struct resource
*res
;
283 match
= of_match_device(armada_thermal_id_table
, &pdev
->dev
);
287 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
291 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
292 priv
->sensor
= devm_ioremap_resource(&pdev
->dev
, res
);
293 if (IS_ERR(priv
->sensor
))
294 return PTR_ERR(priv
->sensor
);
296 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
297 priv
->control
= devm_ioremap_resource(&pdev
->dev
, res
);
298 if (IS_ERR(priv
->control
))
299 return PTR_ERR(priv
->control
);
301 priv
->data
= (struct armada_thermal_data
*)match
->data
;
302 priv
->data
->init_sensor(pdev
, priv
);
304 thermal
= thermal_zone_device_register("armada_thermal", 0, 0,
305 priv
, &ops
, NULL
, 0, 0);
306 if (IS_ERR(thermal
)) {
308 "Failed to register thermal zone device\n");
309 return PTR_ERR(thermal
);
312 platform_set_drvdata(pdev
, thermal
);
317 static int armada_thermal_exit(struct platform_device
*pdev
)
319 struct thermal_zone_device
*armada_thermal
=
320 platform_get_drvdata(pdev
);
322 thermal_zone_device_unregister(armada_thermal
);
327 static struct platform_driver armada_thermal_driver
= {
328 .probe
= armada_thermal_probe
,
329 .remove
= armada_thermal_exit
,
331 .name
= "armada_thermal",
332 .owner
= THIS_MODULE
,
333 .of_match_table
= armada_thermal_id_table
,
337 module_platform_driver(armada_thermal_driver
);
339 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
340 MODULE_DESCRIPTION("Armada 370/XP thermal driver");
341 MODULE_LICENSE("GPL v2");