thermal: armada: Allow to specify an 'inverted readout' sensor
[deliverable/linux.git] / drivers / thermal / armada_thermal.c
CommitLineData
fa0d654c
EG
1/*
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
fa0d654c 27#define THERMAL_VALID_MASK 0x1
fa0d654c
EG
28
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
66fdb7b6 38struct armada_thermal_data;
fa0d654c
EG
39
40/* Marvell EBU Thermal Sensor Dev Structure */
41struct armada_thermal_priv {
42 void __iomem *sensor;
43 void __iomem *control;
66fdb7b6 44 struct armada_thermal_data *data;
fa0d654c
EG
45};
46
66fdb7b6 47struct armada_thermal_data {
fa0d654c 48 /* Initialize the sensor */
04bf3d7e
EG
49 void (*init_sensor)(struct platform_device *pdev,
50 struct armada_thermal_priv *);
fa0d654c
EG
51
52 /* Test for a valid sensor value (optional) */
53 bool (*is_valid)(struct armada_thermal_priv *);
9484bc62
EG
54
55 /* Formula coeficients: temp = (b + m * reg) / div */
56 unsigned long coef_b;
57 unsigned long coef_m;
58 unsigned long coef_div;
fd2c94d5 59 bool inverted;
1fcacca4
EG
60
61 /* Register shift and mask to access the sensor temperature */
62 unsigned int temp_shift;
63 unsigned int temp_mask;
64 unsigned int is_valid_shift;
fa0d654c
EG
65};
66
04bf3d7e
EG
67static void armadaxp_init_sensor(struct platform_device *pdev,
68 struct armada_thermal_priv *priv)
fa0d654c
EG
69{
70 unsigned long reg;
71
72 reg = readl_relaxed(priv->control);
73 reg |= PMU_TDC0_OTF_CAL_MASK;
74 writel(reg, priv->control);
75
76 /* Reference calibration value */
77 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
78 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
79 writel(reg, priv->control);
80
81 /* Reset the sensor */
82 reg = readl_relaxed(priv->control);
83 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
84
85 writel(reg, priv->control);
86
87 /* Enable the sensor */
88 reg = readl_relaxed(priv->sensor);
89 reg &= ~PMU_TM_DISABLE_MASK;
90 writel(reg, priv->sensor);
91}
92
04bf3d7e
EG
93static void armada370_init_sensor(struct platform_device *pdev,
94 struct armada_thermal_priv *priv)
fa0d654c
EG
95{
96 unsigned long reg;
97
98 reg = readl_relaxed(priv->control);
99 reg |= PMU_TDC0_OTF_CAL_MASK;
100 writel(reg, priv->control);
101
102 /* Reference calibration value */
103 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
104 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
105 writel(reg, priv->control);
106
107 reg &= ~PMU_TDC0_START_CAL_MASK;
108 writel(reg, priv->control);
109
110 mdelay(10);
111}
112
113static bool armada_is_valid(struct armada_thermal_priv *priv)
114{
115 unsigned long reg = readl_relaxed(priv->sensor);
116
1fcacca4 117 return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK;
fa0d654c
EG
118}
119
120static int armada_get_temp(struct thermal_zone_device *thermal,
121 unsigned long *temp)
122{
123 struct armada_thermal_priv *priv = thermal->devdata;
124 unsigned long reg;
9484bc62 125 unsigned long m, b, div;
fa0d654c
EG
126
127 /* Valid check */
66fdb7b6 128 if (priv->data->is_valid && !priv->data->is_valid(priv)) {
fa0d654c
EG
129 dev_err(&thermal->device,
130 "Temperature sensor reading not valid\n");
131 return -EIO;
132 }
133
134 reg = readl_relaxed(priv->sensor);
1fcacca4 135 reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
9484bc62
EG
136
137 /* Get formula coeficients */
138 b = priv->data->coef_b;
139 m = priv->data->coef_m;
140 div = priv->data->coef_div;
141
fd2c94d5
EG
142 if (priv->data->inverted)
143 *temp = ((m * reg) - b) / div;
144 else
145 *temp = (b - (m * reg)) / div;
fa0d654c
EG
146 return 0;
147}
148
149static struct thermal_zone_device_ops ops = {
150 .get_temp = armada_get_temp,
151};
152
66fdb7b6 153static const struct armada_thermal_data armadaxp_data = {
fa0d654c 154 .init_sensor = armadaxp_init_sensor,
1fcacca4
EG
155 .temp_shift = 10,
156 .temp_mask = 0x1ff,
9484bc62
EG
157 .coef_b = 3153000000UL,
158 .coef_m = 10000000UL,
159 .coef_div = 13825,
fa0d654c
EG
160};
161
66fdb7b6 162static const struct armada_thermal_data armada370_data = {
fa0d654c
EG
163 .is_valid = armada_is_valid,
164 .init_sensor = armada370_init_sensor,
1fcacca4
EG
165 .is_valid_shift = 9,
166 .temp_shift = 10,
167 .temp_mask = 0x1ff,
9484bc62
EG
168 .coef_b = 3153000000UL,
169 .coef_m = 10000000UL,
170 .coef_div = 13825,
fa0d654c
EG
171};
172
173static const struct of_device_id armada_thermal_id_table[] = {
174 {
175 .compatible = "marvell,armadaxp-thermal",
66fdb7b6 176 .data = &armadaxp_data,
fa0d654c
EG
177 },
178 {
179 .compatible = "marvell,armada370-thermal",
66fdb7b6 180 .data = &armada370_data,
fa0d654c
EG
181 },
182 {
183 /* sentinel */
184 },
185};
186MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
187
188static int armada_thermal_probe(struct platform_device *pdev)
189{
190 struct thermal_zone_device *thermal;
191 const struct of_device_id *match;
192 struct armada_thermal_priv *priv;
193 struct resource *res;
194
195 match = of_match_device(armada_thermal_id_table, &pdev->dev);
196 if (!match)
197 return -ENODEV;
198
199 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
200 if (!priv)
201 return -ENOMEM;
202
203 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
fa0d654c
EG
204 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
205 if (IS_ERR(priv->sensor))
206 return PTR_ERR(priv->sensor);
207
208 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
fa0d654c
EG
209 priv->control = devm_ioremap_resource(&pdev->dev, res);
210 if (IS_ERR(priv->control))
211 return PTR_ERR(priv->control);
212
66fdb7b6 213 priv->data = (struct armada_thermal_data *)match->data;
04bf3d7e 214 priv->data->init_sensor(pdev, priv);
fa0d654c
EG
215
216 thermal = thermal_zone_device_register("armada_thermal", 0, 0,
217 priv, &ops, NULL, 0, 0);
218 if (IS_ERR(thermal)) {
219 dev_err(&pdev->dev,
220 "Failed to register thermal zone device\n");
221 return PTR_ERR(thermal);
222 }
223
224 platform_set_drvdata(pdev, thermal);
225
226 return 0;
227}
228
229static int armada_thermal_exit(struct platform_device *pdev)
230{
231 struct thermal_zone_device *armada_thermal =
232 platform_get_drvdata(pdev);
233
234 thermal_zone_device_unregister(armada_thermal);
fa0d654c
EG
235
236 return 0;
237}
238
239static struct platform_driver armada_thermal_driver = {
240 .probe = armada_thermal_probe,
241 .remove = armada_thermal_exit,
242 .driver = {
243 .name = "armada_thermal",
244 .owner = THIS_MODULE,
1d089e09 245 .of_match_table = armada_thermal_id_table,
fa0d654c
EG
246 },
247};
248
249module_platform_driver(armada_thermal_driver);
250
251MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
252MODULE_DESCRIPTION("Armada 370/XP thermal driver");
253MODULE_LICENSE("GPL v2");
This page took 0.088689 seconds and 5 git commands to generate.