blob: f84d9f077fe28903a41d8e90a737fc5ebc9c0275 [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 Garcia66fdb7b2014-05-06 13:59:45 -030038struct armada_thermal_data;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000039
40/* Marvell EBU Thermal Sensor Dev Structure */
41struct armada_thermal_priv {
42 void __iomem *sensor;
43 void __iomem *control;
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030044 struct armada_thermal_data *data;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000045};
46
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -030047struct armada_thermal_data {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000048 /* Initialize the sensor */
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030049 void (*init_sensor)(struct platform_device *pdev,
50 struct armada_thermal_priv *);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000051
52 /* Test for a valid sensor value (optional) */
53 bool (*is_valid)(struct armada_thermal_priv *);
Ezequiel Garcia9484bc62014-05-06 13:59:46 -030054
55 /* Formula coeficients: temp = (b + m * reg) / div */
56 unsigned long coef_b;
57 unsigned long coef_m;
58 unsigned long coef_div;
Ezequiel Garciafd2c94d2014-05-06 13:59:49 -030059 bool inverted;
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -030060
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;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000065};
66
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030067static void armadaxp_init_sensor(struct platform_device *pdev,
68 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000069{
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
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030093static void armada370_init_sensor(struct platform_device *pdev,
94 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000095{
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
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300117 return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000118}
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;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300125 unsigned long m, b, div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000126
127 /* Valid check */
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300128 if (priv->data->is_valid && !priv->data->is_valid(priv)) {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000129 dev_err(&thermal->device,
130 "Temperature sensor reading not valid\n");
131 return -EIO;
132 }
133
134 reg = readl_relaxed(priv->sensor);
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300135 reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300136
137 /* Get formula coeficients */
138 b = priv->data->coef_b;
139 m = priv->data->coef_m;
140 div = priv->data->coef_div;
141
Ezequiel Garciafd2c94d2014-05-06 13:59:49 -0300142 if (priv->data->inverted)
143 *temp = ((m * reg) - b) / div;
144 else
145 *temp = (b - (m * reg)) / div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000146 return 0;
147}
148
149static struct thermal_zone_device_ops ops = {
150 .get_temp = armada_get_temp,
151};
152
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300153static const struct armada_thermal_data armadaxp_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000154 .init_sensor = armadaxp_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300155 .temp_shift = 10,
156 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300157 .coef_b = 3153000000UL,
158 .coef_m = 10000000UL,
159 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000160};
161
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300162static const struct armada_thermal_data armada370_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000163 .is_valid = armada_is_valid,
164 .init_sensor = armada370_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300165 .is_valid_shift = 9,
166 .temp_shift = 10,
167 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300168 .coef_b = 3153000000UL,
169 .coef_m = 10000000UL,
170 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000171};
172
173static const struct of_device_id armada_thermal_id_table[] = {
174 {
175 .compatible = "marvell,armadaxp-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300176 .data = &armadaxp_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000177 },
178 {
179 .compatible = "marvell,armada370-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300180 .data = &armada370_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000181 },
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);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000204 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);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000209 priv->control = devm_ioremap_resource(&pdev->dev, res);
210 if (IS_ERR(priv->control))
211 return PTR_ERR(priv->control);
212
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300213 priv->data = (struct armada_thermal_data *)match->data;
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -0300214 priv->data->init_sensor(pdev, priv);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000215
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);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000235
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,
Sachin Kamat1d089e02013-05-16 10:28:08 +0000245 .of_match_table = armada_thermal_id_table,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000246 },
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");