1 // SPDX-License-Identifier: GPL-2.0-only
3 * Dove thermal sensor driver
5 * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
7 #include <linux/device.h>
10 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/thermal.h>
16 #define DOVE_THERMAL_TEMP_OFFSET 1
17 #define DOVE_THERMAL_TEMP_MASK 0x1FF
19 /* Dove Thermal Manager Control and Status Register */
20 #define PMU_TM_DISABLE_OFFS 0
21 #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
23 /* Dove Theraml Diode Control 0 Register */
24 #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
25 #define PMU_TDC0_SEL_VCAL_OFFS 5
26 #define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_OFFS)
27 #define PMU_TDC0_REF_CAL_CNT_OFFS 11
28 #define PMU_TDC0_REF_CAL_CNT_MASK (0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
29 #define PMU_TDC0_AVG_NUM_OFFS 25
30 #define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
32 /* Dove Thermal Diode Control 1 Register */
33 #define PMU_TEMP_DIOD_CTRL1_REG 0x04
34 #define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
36 /* Dove Thermal Sensor Dev Structure */
37 struct dove_thermal_priv
{
39 void __iomem
*control
;
42 static int dove_init_sensor(const struct dove_thermal_priv
*priv
)
47 /* Configure the Diode Control Register #0 */
48 reg
= readl_relaxed(priv
->control
);
50 /* Use average of 2 */
51 reg
&= ~PMU_TDC0_AVG_NUM_MASK
;
52 reg
|= (0x1 << PMU_TDC0_AVG_NUM_OFFS
);
54 /* Reference calibration value */
55 reg
&= ~PMU_TDC0_REF_CAL_CNT_MASK
;
56 reg
|= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS
);
58 /* Set the high level reference for calibration */
59 reg
&= ~PMU_TDC0_SEL_VCAL_MASK
;
60 reg
|= (0x2 << PMU_TDC0_SEL_VCAL_OFFS
);
61 writel(reg
, priv
->control
);
63 /* Reset the sensor */
64 reg
= readl_relaxed(priv
->control
);
65 writel((reg
| PMU_TDC0_SW_RST_MASK
), priv
->control
);
66 writel(reg
, priv
->control
);
68 /* Enable the sensor */
69 reg
= readl_relaxed(priv
->sensor
);
70 reg
&= ~PMU_TM_DISABLE_MASK
;
71 writel(reg
, priv
->sensor
);
73 /* Poll the sensor for the first reading */
74 for (i
= 0; i
< 1000000; i
++) {
75 reg
= readl_relaxed(priv
->sensor
);
76 if (reg
& DOVE_THERMAL_TEMP_MASK
)
86 static int dove_get_temp(struct thermal_zone_device
*thermal
,
90 struct dove_thermal_priv
*priv
= thermal_zone_device_priv(thermal
);
93 reg
= readl_relaxed(priv
->control
+ PMU_TEMP_DIOD_CTRL1_REG
);
94 if ((reg
& PMU_TDC1_TEMP_VALID_MASK
) == 0x0)
98 * Calculate temperature. According to Marvell internal
99 * documentation the formula for this is:
100 * Celsius = (322-reg)/1.3625
102 reg
= readl_relaxed(priv
->sensor
);
103 reg
= (reg
>> DOVE_THERMAL_TEMP_OFFSET
) & DOVE_THERMAL_TEMP_MASK
;
104 *temp
= ((3220000000UL - (10000000UL * reg
)) / 13625);
109 static struct thermal_zone_device_ops ops
= {
110 .get_temp
= dove_get_temp
,
113 static const struct of_device_id dove_thermal_id_table
[] = {
114 { .compatible
= "marvell,dove-thermal" },
118 static int dove_thermal_probe(struct platform_device
*pdev
)
120 struct thermal_zone_device
*thermal
= NULL
;
121 struct dove_thermal_priv
*priv
;
124 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
128 priv
->sensor
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
129 if (IS_ERR(priv
->sensor
))
130 return PTR_ERR(priv
->sensor
);
132 priv
->control
= devm_platform_get_and_ioremap_resource(pdev
, 1, NULL
);
133 if (IS_ERR(priv
->control
))
134 return PTR_ERR(priv
->control
);
136 ret
= dove_init_sensor(priv
);
138 dev_err(&pdev
->dev
, "Failed to initialize sensor\n");
142 thermal
= thermal_tripless_zone_device_register("dove_thermal", priv
,
144 if (IS_ERR(thermal
)) {
146 "Failed to register thermal zone device\n");
147 return PTR_ERR(thermal
);
150 ret
= thermal_zone_device_enable(thermal
);
152 thermal_zone_device_unregister(thermal
);
156 platform_set_drvdata(pdev
, thermal
);
161 static void dove_thermal_exit(struct platform_device
*pdev
)
163 struct thermal_zone_device
*dove_thermal
=
164 platform_get_drvdata(pdev
);
166 thermal_zone_device_unregister(dove_thermal
);
169 MODULE_DEVICE_TABLE(of
, dove_thermal_id_table
);
171 static struct platform_driver dove_thermal_driver
= {
172 .probe
= dove_thermal_probe
,
173 .remove
= dove_thermal_exit
,
175 .name
= "dove_thermal",
176 .of_match_table
= dove_thermal_id_table
,
180 module_platform_driver(dove_thermal_driver
);
182 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
183 MODULE_DESCRIPTION("Dove thermal driver");
184 MODULE_LICENSE("GPL");