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_UNIT_CONTROL_SHIFT 27
39 #define A375_UNIT_CONTROL_MASK 0x7
40 #define A375_READOUT_INVERT BIT(15)
41 #define A375_HW_RESETn BIT(8)
42 #define A380_HW_RESET BIT(8)
44 struct armada_thermal_data;
46 /* Marvell EBU Thermal Sensor Dev Structure */
47 struct armada_thermal_priv {
49 void __iomem *control;
50 struct armada_thermal_data *data;
53 struct armada_thermal_data {
54 /* Initialize the sensor */
55 void (*init_sensor)(struct platform_device *pdev,
56 struct armada_thermal_priv *);
58 /* Test for a valid sensor value (optional) */
59 bool (*is_valid)(struct armada_thermal_priv *);
61 /* Formula coeficients: temp = (b + m * reg) / div */
64 unsigned long coef_div;
67 /* Register shift and mask to access the sensor temperature */
68 unsigned int temp_shift;
69 unsigned int temp_mask;
70 unsigned int is_valid_shift;
73 static void armadaxp_init_sensor(struct platform_device *pdev,
74 struct armada_thermal_priv *priv)
78 reg = readl_relaxed(priv->control);
79 reg |= PMU_TDC0_OTF_CAL_MASK;
80 writel(reg, priv->control);
82 /* Reference calibration value */
83 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
84 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
85 writel(reg, priv->control);
87 /* Reset the sensor */
88 reg = readl_relaxed(priv->control);
89 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
91 writel(reg, priv->control);
93 /* Enable the sensor */
94 reg = readl_relaxed(priv->sensor);
95 reg &= ~PMU_TM_DISABLE_MASK;
96 writel(reg, priv->sensor);
99 static void armada370_init_sensor(struct platform_device *pdev,
100 struct armada_thermal_priv *priv)
104 reg = readl_relaxed(priv->control);
105 reg |= PMU_TDC0_OTF_CAL_MASK;
106 writel(reg, priv->control);
108 /* Reference calibration value */
109 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
110 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
111 writel(reg, priv->control);
113 reg &= ~PMU_TDC0_START_CAL_MASK;
114 writel(reg, priv->control);
119 static void armada375_init_sensor(struct platform_device *pdev,
120 struct armada_thermal_priv *priv)
124 reg = readl(priv->control + 4);
125 reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT);
126 reg &= ~A375_READOUT_INVERT;
127 reg &= ~A375_HW_RESETn;
129 writel(reg, priv->control + 4);
132 reg |= A375_HW_RESETn;
133 writel(reg, priv->control + 4);
137 static void armada380_init_sensor(struct platform_device *pdev,
138 struct armada_thermal_priv *priv)
140 unsigned long reg = readl_relaxed(priv->control);
142 /* Reset hardware once */
143 if (!(reg & A380_HW_RESET)) {
144 reg |= A380_HW_RESET;
145 writel(reg, priv->control);
150 static bool armada_is_valid(struct armada_thermal_priv *priv)
152 unsigned long reg = readl_relaxed(priv->sensor);
154 return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK;
157 static int armada_get_temp(struct thermal_zone_device *thermal,
160 struct armada_thermal_priv *priv = thermal->devdata;
162 unsigned long m, b, div;
165 if (priv->data->is_valid && !priv->data->is_valid(priv)) {
166 dev_err(&thermal->device,
167 "Temperature sensor reading not valid\n");
171 reg = readl_relaxed(priv->sensor);
172 reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
174 /* Get formula coeficients */
175 b = priv->data->coef_b;
176 m = priv->data->coef_m;
177 div = priv->data->coef_div;
179 if (priv->data->inverted)
180 *temp = ((m * reg) - b) / div;
182 *temp = (b - (m * reg)) / div;
186 static struct thermal_zone_device_ops ops = {
187 .get_temp = armada_get_temp,
190 static const struct armada_thermal_data armadaxp_data = {
191 .init_sensor = armadaxp_init_sensor,
194 .coef_b = 3153000000UL,
195 .coef_m = 10000000UL,
199 static const struct armada_thermal_data armada370_data = {
200 .is_valid = armada_is_valid,
201 .init_sensor = armada370_init_sensor,
205 .coef_b = 3153000000UL,
206 .coef_m = 10000000UL,
210 static const struct armada_thermal_data armada375_data = {
211 .is_valid = armada_is_valid,
212 .init_sensor = armada375_init_sensor,
213 .is_valid_shift = 10,
216 .coef_b = 3171900000UL,
217 .coef_m = 10000000UL,
221 static const struct armada_thermal_data armada380_data = {
222 .is_valid = armada_is_valid,
223 .init_sensor = armada380_init_sensor,
224 .is_valid_shift = 10,
227 .coef_b = 1172499100UL,
233 static const struct of_device_id armada_thermal_id_table[] = {
235 .compatible = "marvell,armadaxp-thermal",
236 .data = &armadaxp_data,
239 .compatible = "marvell,armada370-thermal",
240 .data = &armada370_data,
243 .compatible = "marvell,armada375-thermal",
244 .data = &armada375_data,
247 .compatible = "marvell,armada380-thermal",
248 .data = &armada380_data,
254 MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
256 static int armada_thermal_probe(struct platform_device *pdev)
258 struct thermal_zone_device *thermal;
259 const struct of_device_id *match;
260 struct armada_thermal_priv *priv;
261 struct resource *res;
263 match = of_match_device(armada_thermal_id_table, &pdev->dev);
267 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
271 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
272 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
273 if (IS_ERR(priv->sensor))
274 return PTR_ERR(priv->sensor);
276 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
277 priv->control = devm_ioremap_resource(&pdev->dev, res);
278 if (IS_ERR(priv->control))
279 return PTR_ERR(priv->control);
281 priv->data = (struct armada_thermal_data *)match->data;
282 priv->data->init_sensor(pdev, priv);
284 thermal = thermal_zone_device_register("armada_thermal", 0, 0,
285 priv, &ops, NULL, 0, 0);
286 if (IS_ERR(thermal)) {
288 "Failed to register thermal zone device\n");
289 return PTR_ERR(thermal);
292 platform_set_drvdata(pdev, thermal);
297 static int armada_thermal_exit(struct platform_device *pdev)
299 struct thermal_zone_device *armada_thermal =
300 platform_get_drvdata(pdev);
302 thermal_zone_device_unregister(armada_thermal);
307 static struct platform_driver armada_thermal_driver = {
308 .probe = armada_thermal_probe,
309 .remove = armada_thermal_exit,
311 .name = "armada_thermal",
312 .of_match_table = armada_thermal_id_table,
316 module_platform_driver(armada_thermal_driver);
318 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
319 MODULE_DESCRIPTION("Armada 370/XP thermal driver");
320 MODULE_LICENSE("GPL v2");