2 * Dove thermal sensor driver
4 * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
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/platform_device.h>
23 #include <linux/thermal.h>
25 #define DOVE_THERMAL_TEMP_OFFSET 1
26 #define DOVE_THERMAL_TEMP_MASK 0x1FF
28 /* Dove Thermal Manager Control and Status Register */
29 #define PMU_TM_DISABLE_OFFS 0
30 #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
32 /* Dove Theraml Diode Control 0 Register */
33 #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
34 #define PMU_TDC0_SEL_VCAL_OFFS 5
35 #define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_OFFS)
36 #define PMU_TDC0_REF_CAL_CNT_OFFS 11
37 #define PMU_TDC0_REF_CAL_CNT_MASK (0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
38 #define PMU_TDC0_AVG_NUM_OFFS 25
39 #define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
41 /* Dove Thermal Diode Control 1 Register */
42 #define PMU_TEMP_DIOD_CTRL1_REG 0x04
43 #define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
45 /* Dove Thermal Sensor Dev Structure */
46 struct dove_thermal_priv {
48 void __iomem *control;
51 static int dove_init_sensor(const struct dove_thermal_priv *priv)
56 /* Configure the Diode Control Register #0 */
57 reg = readl_relaxed(priv->control);
59 /* Use average of 2 */
60 reg &= ~PMU_TDC0_AVG_NUM_MASK;
61 reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
63 /* Reference calibration value */
64 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
65 reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
67 /* Set the high level reference for calibration */
68 reg &= ~PMU_TDC0_SEL_VCAL_MASK;
69 reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
70 writel(reg, priv->control);
72 /* Reset the sensor */
73 reg = readl_relaxed(priv->control);
74 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
75 writel(reg, priv->control);
77 /* Enable the sensor */
78 reg = readl_relaxed(priv->sensor);
79 reg &= ~PMU_TM_DISABLE_MASK;
80 writel(reg, priv->sensor);
82 /* Poll the sensor for the first reading */
83 for (i = 0; i < 1000000; i++) {
84 reg = readl_relaxed(priv->sensor);
85 if (reg & DOVE_THERMAL_TEMP_MASK)
95 static int dove_get_temp(struct thermal_zone_device *thermal,
99 struct dove_thermal_priv *priv = thermal->devdata;
102 reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
103 if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
104 dev_err(&thermal->device,
105 "Temperature sensor reading not valid\n");
110 * Calculate temperature. According to Marvell internal
111 * documentation the formula for this is:
112 * Celsius = (322-reg)/1.3625
114 reg = readl_relaxed(priv->sensor);
115 reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
116 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
121 static struct thermal_zone_device_ops ops = {
122 .get_temp = dove_get_temp,
125 static const struct of_device_id dove_thermal_id_table[] = {
126 { .compatible = "marvell,dove-thermal" },
130 static int dove_thermal_probe(struct platform_device *pdev)
132 struct thermal_zone_device *thermal = NULL;
133 struct dove_thermal_priv *priv;
134 struct resource *res;
137 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
143 if (IS_ERR(priv->sensor))
144 return PTR_ERR(priv->sensor);
146 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
147 priv->control = devm_ioremap_resource(&pdev->dev, res);
148 if (IS_ERR(priv->control))
149 return PTR_ERR(priv->control);
151 ret = dove_init_sensor(priv);
153 dev_err(&pdev->dev, "Failed to initialize sensor\n");
157 thermal = thermal_zone_device_register("dove_thermal", 0, 0,
158 priv, &ops, NULL, 0, 0);
159 if (IS_ERR(thermal)) {
161 "Failed to register thermal zone device\n");
162 return PTR_ERR(thermal);
165 platform_set_drvdata(pdev, thermal);
170 static int dove_thermal_exit(struct platform_device *pdev)
172 struct thermal_zone_device *dove_thermal =
173 platform_get_drvdata(pdev);
175 thermal_zone_device_unregister(dove_thermal);
180 MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
182 static struct platform_driver dove_thermal_driver = {
183 .probe = dove_thermal_probe,
184 .remove = dove_thermal_exit,
186 .name = "dove_thermal",
187 .of_match_table = dove_thermal_id_table,
191 module_platform_driver(dove_thermal_driver);
193 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
194 MODULE_DESCRIPTION("Dove thermal driver");
195 MODULE_LICENSE("GPL");