blob: c2556cf5186bc75b71870ed6a304fda430b8521b [file] [log] [blame]
Ezequiel Garciafa0d6542013-04-02 01:37:41 +00001/*
2 * Marvell Armada 370/XP thermal sensor driver
3 *
4 * Copyright (C) 2013 Marvell
5 *
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.
9 *
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.
14 *
15 */
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/kernel.h>
20#include <linux/of.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>
26
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000027#define THERMAL_VALID_MASK 0x1
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000028
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)
37
Ezequiel Garciae2d5f052014-05-06 13:59:50 -030038#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)
Ezequiel Garciae6e0a682014-05-06 13:59:51 -030042#define A380_HW_RESET BIT(8)
Ezequiel Garciae2d5f052014-05-06 13:59:50 -030043
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030044struct armada_thermal_data;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000045
46/* Marvell EBU Thermal Sensor Dev Structure */
47struct armada_thermal_priv {
48 void __iomem *sensor;
49 void __iomem *control;
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030050 struct armada_thermal_data *data;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000051};
52
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030053struct armada_thermal_data {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000054 /* Initialize the sensor */
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030055 void (*init_sensor)(struct platform_device *pdev,
56 struct armada_thermal_priv *);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000057
58 /* Test for a valid sensor value (optional) */
59 bool (*is_valid)(struct armada_thermal_priv *);
Ezequiel Garcia9484bc62014-05-06 13:59:46 -030060
61 /* Formula coeficients: temp = (b + m * reg) / div */
62 unsigned long coef_b;
63 unsigned long coef_m;
64 unsigned long coef_div;
Ezequiel Garciafd2c94d2014-05-06 13:59:49 -030065 bool inverted;
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -030066
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;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000071};
72
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030073static void armadaxp_init_sensor(struct platform_device *pdev,
74 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000075{
76 unsigned long reg;
77
78 reg = readl_relaxed(priv->control);
79 reg |= PMU_TDC0_OTF_CAL_MASK;
80 writel(reg, priv->control);
81
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);
86
87 /* Reset the sensor */
88 reg = readl_relaxed(priv->control);
89 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
90
91 writel(reg, priv->control);
92
93 /* Enable the sensor */
94 reg = readl_relaxed(priv->sensor);
95 reg &= ~PMU_TM_DISABLE_MASK;
96 writel(reg, priv->sensor);
97}
98
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030099static void armada370_init_sensor(struct platform_device *pdev,
100 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000101{
102 unsigned long reg;
103
104 reg = readl_relaxed(priv->control);
105 reg |= PMU_TDC0_OTF_CAL_MASK;
106 writel(reg, priv->control);
107
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);
112
113 reg &= ~PMU_TDC0_START_CAL_MASK;
114 writel(reg, priv->control);
115
116 mdelay(10);
117}
118
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300119static void armada375_init_sensor(struct platform_device *pdev,
120 struct armada_thermal_priv *priv)
121{
122 unsigned long reg;
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300123
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;
128
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300129 writel(reg, priv->control + 4);
130 mdelay(20);
131
132 reg |= A375_HW_RESETn;
133 writel(reg, priv->control + 4);
134 mdelay(50);
135}
136
Ezequiel Garciae6e0a682014-05-06 13:59:51 -0300137static void armada380_init_sensor(struct platform_device *pdev,
138 struct armada_thermal_priv *priv)
139{
140 unsigned long reg = readl_relaxed(priv->control);
141
142 /* Reset hardware once */
143 if (!(reg & A380_HW_RESET)) {
144 reg |= A380_HW_RESET;
145 writel(reg, priv->control);
146 mdelay(10);
147 }
148}
149
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000150static bool armada_is_valid(struct armada_thermal_priv *priv)
151{
152 unsigned long reg = readl_relaxed(priv->sensor);
153
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300154 return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000155}
156
157static int armada_get_temp(struct thermal_zone_device *thermal,
158 unsigned long *temp)
159{
160 struct armada_thermal_priv *priv = thermal->devdata;
161 unsigned long reg;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300162 unsigned long m, b, div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000163
164 /* Valid check */
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300165 if (priv->data->is_valid && !priv->data->is_valid(priv)) {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000166 dev_err(&thermal->device,
167 "Temperature sensor reading not valid\n");
168 return -EIO;
169 }
170
171 reg = readl_relaxed(priv->sensor);
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300172 reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300173
174 /* Get formula coeficients */
175 b = priv->data->coef_b;
176 m = priv->data->coef_m;
177 div = priv->data->coef_div;
178
Ezequiel Garciafd2c94d2014-05-06 13:59:49 -0300179 if (priv->data->inverted)
180 *temp = ((m * reg) - b) / div;
181 else
182 *temp = (b - (m * reg)) / div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000183 return 0;
184}
185
186static struct thermal_zone_device_ops ops = {
187 .get_temp = armada_get_temp,
188};
189
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300190static const struct armada_thermal_data armadaxp_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000191 .init_sensor = armadaxp_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300192 .temp_shift = 10,
193 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300194 .coef_b = 3153000000UL,
195 .coef_m = 10000000UL,
196 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000197};
198
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300199static const struct armada_thermal_data armada370_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000200 .is_valid = armada_is_valid,
201 .init_sensor = armada370_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300202 .is_valid_shift = 9,
203 .temp_shift = 10,
204 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300205 .coef_b = 3153000000UL,
206 .coef_m = 10000000UL,
207 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000208};
209
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300210static const struct armada_thermal_data armada375_data = {
211 .is_valid = armada_is_valid,
212 .init_sensor = armada375_init_sensor,
213 .is_valid_shift = 10,
214 .temp_shift = 0,
215 .temp_mask = 0x1ff,
216 .coef_b = 3171900000UL,
217 .coef_m = 10000000UL,
218 .coef_div = 13616,
219};
220
Ezequiel Garciae6e0a682014-05-06 13:59:51 -0300221static const struct armada_thermal_data armada380_data = {
222 .is_valid = armada_is_valid,
223 .init_sensor = armada380_init_sensor,
224 .is_valid_shift = 10,
225 .temp_shift = 0,
226 .temp_mask = 0x3ff,
227 .coef_b = 1169498786UL,
228 .coef_m = 2000000UL,
229 .coef_div = 4289,
230 .inverted = true,
231};
232
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000233static const struct of_device_id armada_thermal_id_table[] = {
234 {
235 .compatible = "marvell,armadaxp-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300236 .data = &armadaxp_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000237 },
238 {
239 .compatible = "marvell,armada370-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300240 .data = &armada370_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000241 },
242 {
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300243 .compatible = "marvell,armada375-thermal",
244 .data = &armada375_data,
245 },
246 {
Ezequiel Garciae6e0a682014-05-06 13:59:51 -0300247 .compatible = "marvell,armada380-thermal",
248 .data = &armada380_data,
249 },
250 {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000251 /* sentinel */
252 },
253};
254MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
255
256static int armada_thermal_probe(struct platform_device *pdev)
257{
258 struct thermal_zone_device *thermal;
259 const struct of_device_id *match;
260 struct armada_thermal_priv *priv;
261 struct resource *res;
262
263 match = of_match_device(armada_thermal_id_table, &pdev->dev);
264 if (!match)
265 return -ENODEV;
266
267 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
268 if (!priv)
269 return -ENOMEM;
270
271 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000272 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
273 if (IS_ERR(priv->sensor))
274 return PTR_ERR(priv->sensor);
275
276 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000277 priv->control = devm_ioremap_resource(&pdev->dev, res);
278 if (IS_ERR(priv->control))
279 return PTR_ERR(priv->control);
280
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300281 priv->data = (struct armada_thermal_data *)match->data;
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -0300282 priv->data->init_sensor(pdev, priv);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000283
284 thermal = thermal_zone_device_register("armada_thermal", 0, 0,
285 priv, &ops, NULL, 0, 0);
286 if (IS_ERR(thermal)) {
287 dev_err(&pdev->dev,
288 "Failed to register thermal zone device\n");
289 return PTR_ERR(thermal);
290 }
291
292 platform_set_drvdata(pdev, thermal);
293
294 return 0;
295}
296
297static int armada_thermal_exit(struct platform_device *pdev)
298{
299 struct thermal_zone_device *armada_thermal =
300 platform_get_drvdata(pdev);
301
302 thermal_zone_device_unregister(armada_thermal);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000303
304 return 0;
305}
306
307static struct platform_driver armada_thermal_driver = {
308 .probe = armada_thermal_probe,
309 .remove = armada_thermal_exit,
310 .driver = {
311 .name = "armada_thermal",
Sachin Kamat1d089e02013-05-16 10:28:08 +0000312 .of_match_table = armada_thermal_id_table,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000313 },
314};
315
316module_platform_driver(armada_thermal_driver);
317
318MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
319MODULE_DESCRIPTION("Armada 370/XP thermal driver");
320MODULE_LICENSE("GPL v2");