blob: e65c5e442ed582f97487459c6d57ef522e4d0603 [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_Z1_CAL_RESET_LSB 0x8011e214
39#define A375_Z1_CAL_RESET_MSB 0x30a88019
40#define A375_Z1_WORKAROUND_BIT BIT(9)
41
42#define A375_UNIT_CONTROL_SHIFT 27
43#define A375_UNIT_CONTROL_MASK 0x7
44#define A375_READOUT_INVERT BIT(15)
45#define A375_HW_RESETn BIT(8)
46
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030047struct armada_thermal_data;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000048
49/* Marvell EBU Thermal Sensor Dev Structure */
50struct armada_thermal_priv {
51 void __iomem *sensor;
52 void __iomem *control;
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030053 struct armada_thermal_data *data;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000054};
55
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030056struct armada_thermal_data {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000057 /* Initialize the sensor */
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030058 void (*init_sensor)(struct platform_device *pdev,
59 struct armada_thermal_priv *);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000060
61 /* Test for a valid sensor value (optional) */
62 bool (*is_valid)(struct armada_thermal_priv *);
Ezequiel Garcia9484bc62014-05-06 13:59:46 -030063
64 /* Formula coeficients: temp = (b + m * reg) / div */
65 unsigned long coef_b;
66 unsigned long coef_m;
67 unsigned long coef_div;
Ezequiel Garciafd2c94d2014-05-06 13:59:49 -030068 bool inverted;
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -030069
70 /* Register shift and mask to access the sensor temperature */
71 unsigned int temp_shift;
72 unsigned int temp_mask;
73 unsigned int is_valid_shift;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000074};
75
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030076static void armadaxp_init_sensor(struct platform_device *pdev,
77 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000078{
79 unsigned long reg;
80
81 reg = readl_relaxed(priv->control);
82 reg |= PMU_TDC0_OTF_CAL_MASK;
83 writel(reg, priv->control);
84
85 /* Reference calibration value */
86 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
87 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
88 writel(reg, priv->control);
89
90 /* Reset the sensor */
91 reg = readl_relaxed(priv->control);
92 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
93
94 writel(reg, priv->control);
95
96 /* Enable the sensor */
97 reg = readl_relaxed(priv->sensor);
98 reg &= ~PMU_TM_DISABLE_MASK;
99 writel(reg, priv->sensor);
100}
101
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -0300102static void armada370_init_sensor(struct platform_device *pdev,
103 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000104{
105 unsigned long reg;
106
107 reg = readl_relaxed(priv->control);
108 reg |= PMU_TDC0_OTF_CAL_MASK;
109 writel(reg, priv->control);
110
111 /* Reference calibration value */
112 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
113 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
114 writel(reg, priv->control);
115
116 reg &= ~PMU_TDC0_START_CAL_MASK;
117 writel(reg, priv->control);
118
119 mdelay(10);
120}
121
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300122static void armada375_init_sensor(struct platform_device *pdev,
123 struct armada_thermal_priv *priv)
124{
125 unsigned long reg;
126 bool quirk_needed =
127 !!of_device_is_compatible(pdev->dev.of_node,
128 "marvell,armada375-z1-thermal");
129
130 if (quirk_needed) {
131 /* Ensure these registers have the default (reset) values */
132 writel(A375_Z1_CAL_RESET_LSB, priv->control);
133 writel(A375_Z1_CAL_RESET_MSB, priv->control + 0x4);
134 }
135
136 reg = readl(priv->control + 4);
137 reg &= ~(A375_UNIT_CONTROL_MASK << A375_UNIT_CONTROL_SHIFT);
138 reg &= ~A375_READOUT_INVERT;
139 reg &= ~A375_HW_RESETn;
140
141 if (quirk_needed)
142 reg |= A375_Z1_WORKAROUND_BIT;
143
144 writel(reg, priv->control + 4);
145 mdelay(20);
146
147 reg |= A375_HW_RESETn;
148 writel(reg, priv->control + 4);
149 mdelay(50);
150}
151
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000152static bool armada_is_valid(struct armada_thermal_priv *priv)
153{
154 unsigned long reg = readl_relaxed(priv->sensor);
155
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300156 return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000157}
158
159static int armada_get_temp(struct thermal_zone_device *thermal,
160 unsigned long *temp)
161{
162 struct armada_thermal_priv *priv = thermal->devdata;
163 unsigned long reg;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300164 unsigned long m, b, div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000165
166 /* Valid check */
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300167 if (priv->data->is_valid && !priv->data->is_valid(priv)) {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000168 dev_err(&thermal->device,
169 "Temperature sensor reading not valid\n");
170 return -EIO;
171 }
172
173 reg = readl_relaxed(priv->sensor);
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300174 reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300175
176 /* Get formula coeficients */
177 b = priv->data->coef_b;
178 m = priv->data->coef_m;
179 div = priv->data->coef_div;
180
Ezequiel Garciafd2c94d2014-05-06 13:59:49 -0300181 if (priv->data->inverted)
182 *temp = ((m * reg) - b) / div;
183 else
184 *temp = (b - (m * reg)) / div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000185 return 0;
186}
187
188static struct thermal_zone_device_ops ops = {
189 .get_temp = armada_get_temp,
190};
191
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300192static const struct armada_thermal_data armadaxp_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000193 .init_sensor = armadaxp_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300194 .temp_shift = 10,
195 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300196 .coef_b = 3153000000UL,
197 .coef_m = 10000000UL,
198 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000199};
200
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300201static const struct armada_thermal_data armada370_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000202 .is_valid = armada_is_valid,
203 .init_sensor = armada370_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300204 .is_valid_shift = 9,
205 .temp_shift = 10,
206 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300207 .coef_b = 3153000000UL,
208 .coef_m = 10000000UL,
209 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000210};
211
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300212static const struct armada_thermal_data armada375_data = {
213 .is_valid = armada_is_valid,
214 .init_sensor = armada375_init_sensor,
215 .is_valid_shift = 10,
216 .temp_shift = 0,
217 .temp_mask = 0x1ff,
218 .coef_b = 3171900000UL,
219 .coef_m = 10000000UL,
220 .coef_div = 13616,
221};
222
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000223static const struct of_device_id armada_thermal_id_table[] = {
224 {
225 .compatible = "marvell,armadaxp-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300226 .data = &armadaxp_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000227 },
228 {
229 .compatible = "marvell,armada370-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300230 .data = &armada370_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000231 },
232 {
Ezequiel Garciae2d5f052014-05-06 13:59:50 -0300233 .compatible = "marvell,armada375-thermal",
234 .data = &armada375_data,
235 },
236 {
237 .compatible = "marvell,armada375-z1-thermal",
238 .data = &armada375_data,
239 },
240 {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000241 /* sentinel */
242 },
243};
244MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
245
246static int armada_thermal_probe(struct platform_device *pdev)
247{
248 struct thermal_zone_device *thermal;
249 const struct of_device_id *match;
250 struct armada_thermal_priv *priv;
251 struct resource *res;
252
253 match = of_match_device(armada_thermal_id_table, &pdev->dev);
254 if (!match)
255 return -ENODEV;
256
257 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
258 if (!priv)
259 return -ENOMEM;
260
261 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000262 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
263 if (IS_ERR(priv->sensor))
264 return PTR_ERR(priv->sensor);
265
266 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000267 priv->control = devm_ioremap_resource(&pdev->dev, res);
268 if (IS_ERR(priv->control))
269 return PTR_ERR(priv->control);
270
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300271 priv->data = (struct armada_thermal_data *)match->data;
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -0300272 priv->data->init_sensor(pdev, priv);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000273
274 thermal = thermal_zone_device_register("armada_thermal", 0, 0,
275 priv, &ops, NULL, 0, 0);
276 if (IS_ERR(thermal)) {
277 dev_err(&pdev->dev,
278 "Failed to register thermal zone device\n");
279 return PTR_ERR(thermal);
280 }
281
282 platform_set_drvdata(pdev, thermal);
283
284 return 0;
285}
286
287static int armada_thermal_exit(struct platform_device *pdev)
288{
289 struct thermal_zone_device *armada_thermal =
290 platform_get_drvdata(pdev);
291
292 thermal_zone_device_unregister(armada_thermal);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000293
294 return 0;
295}
296
297static struct platform_driver armada_thermal_driver = {
298 .probe = armada_thermal_probe,
299 .remove = armada_thermal_exit,
300 .driver = {
301 .name = "armada_thermal",
302 .owner = THIS_MODULE,
Sachin Kamat1d089e02013-05-16 10:28:08 +0000303 .of_match_table = armada_thermal_id_table,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000304 },
305};
306
307module_platform_driver(armada_thermal_driver);
308
309MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
310MODULE_DESCRIPTION("Armada 370/XP thermal driver");
311MODULE_LICENSE("GPL v2");