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->devdata;
93 reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
94 if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
95 dev_err(&thermal->device,
96 "Temperature sensor reading not valid\n");
101 * Calculate temperature. According to Marvell internal
102 * documentation the formula for this is:
103 * Celsius = (322-reg)/1.3625
105 reg = readl_relaxed(priv->sensor);
106 reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
107 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
112 static struct thermal_zone_device_ops ops = {
113 .get_temp = dove_get_temp,
116 static const struct of_device_id dove_thermal_id_table[] = {
117 { .compatible = "marvell,dove-thermal" },
121 static int dove_thermal_probe(struct platform_device *pdev)
123 struct thermal_zone_device *thermal = NULL;
124 struct dove_thermal_priv *priv;
125 struct resource *res;
128 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
134 if (IS_ERR(priv->sensor))
135 return PTR_ERR(priv->sensor);
137 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
138 priv->control = devm_ioremap_resource(&pdev->dev, res);
139 if (IS_ERR(priv->control))
140 return PTR_ERR(priv->control);
142 ret = dove_init_sensor(priv);
144 dev_err(&pdev->dev, "Failed to initialize sensor\n");
148 thermal = thermal_zone_device_register("dove_thermal", 0, 0,
149 priv, &ops, NULL, 0, 0);
150 if (IS_ERR(thermal)) {
152 "Failed to register thermal zone device\n");
153 return PTR_ERR(thermal);
156 ret = thermal_zone_device_enable(thermal);
158 thermal_zone_device_unregister(thermal);
162 platform_set_drvdata(pdev, thermal);
167 static int dove_thermal_exit(struct platform_device *pdev)
169 struct thermal_zone_device *dove_thermal =
170 platform_get_drvdata(pdev);
172 thermal_zone_device_unregister(dove_thermal);
177 MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
179 static struct platform_driver dove_thermal_driver = {
180 .probe = dove_thermal_probe,
181 .remove = dove_thermal_exit,
183 .name = "dove_thermal",
184 .of_match_table = dove_thermal_id_table,
188 module_platform_driver(dove_thermal_driver);
190 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
191 MODULE_DESCRIPTION("Dove thermal driver");
192 MODULE_LICENSE("GPL");