blob: 6fd648337e63aa2f0dde2522bcc08e05d800a55d [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 Garcia1fcacca2014-05-06 13:59:47 -030059
60 /* Register shift and mask to access the sensor temperature */
61 unsigned int temp_shift;
62 unsigned int temp_mask;
63 unsigned int is_valid_shift;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000064};
65
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030066static void armadaxp_init_sensor(struct platform_device *pdev,
67 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000068{
69 unsigned long reg;
70
71 reg = readl_relaxed(priv->control);
72 reg |= PMU_TDC0_OTF_CAL_MASK;
73 writel(reg, priv->control);
74
75 /* Reference calibration value */
76 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
77 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
78 writel(reg, priv->control);
79
80 /* Reset the sensor */
81 reg = readl_relaxed(priv->control);
82 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
83
84 writel(reg, priv->control);
85
86 /* Enable the sensor */
87 reg = readl_relaxed(priv->sensor);
88 reg &= ~PMU_TM_DISABLE_MASK;
89 writel(reg, priv->sensor);
90}
91
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -030092static void armada370_init_sensor(struct platform_device *pdev,
93 struct armada_thermal_priv *priv)
Ezequiel Garciafa0d6542013-04-02 01:37:41 +000094{
95 unsigned long reg;
96
97 reg = readl_relaxed(priv->control);
98 reg |= PMU_TDC0_OTF_CAL_MASK;
99 writel(reg, priv->control);
100
101 /* Reference calibration value */
102 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
103 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
104 writel(reg, priv->control);
105
106 reg &= ~PMU_TDC0_START_CAL_MASK;
107 writel(reg, priv->control);
108
109 mdelay(10);
110}
111
112static bool armada_is_valid(struct armada_thermal_priv *priv)
113{
114 unsigned long reg = readl_relaxed(priv->sensor);
115
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300116 return (reg >> priv->data->is_valid_shift) & THERMAL_VALID_MASK;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000117}
118
119static int armada_get_temp(struct thermal_zone_device *thermal,
120 unsigned long *temp)
121{
122 struct armada_thermal_priv *priv = thermal->devdata;
123 unsigned long reg;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300124 unsigned long m, b, div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000125
126 /* Valid check */
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300127 if (priv->data->is_valid && !priv->data->is_valid(priv)) {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000128 dev_err(&thermal->device,
129 "Temperature sensor reading not valid\n");
130 return -EIO;
131 }
132
133 reg = readl_relaxed(priv->sensor);
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300134 reg = (reg >> priv->data->temp_shift) & priv->data->temp_mask;
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300135
136 /* Get formula coeficients */
137 b = priv->data->coef_b;
138 m = priv->data->coef_m;
139 div = priv->data->coef_div;
140
141 *temp = (b - (m * reg)) / div;
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000142 return 0;
143}
144
145static struct thermal_zone_device_ops ops = {
146 .get_temp = armada_get_temp,
147};
148
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300149static const struct armada_thermal_data armadaxp_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000150 .init_sensor = armadaxp_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300151 .temp_shift = 10,
152 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300153 .coef_b = 3153000000UL,
154 .coef_m = 10000000UL,
155 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000156};
157
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300158static const struct armada_thermal_data armada370_data = {
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000159 .is_valid = armada_is_valid,
160 .init_sensor = armada370_init_sensor,
Ezequiel Garcia1fcacca2014-05-06 13:59:47 -0300161 .is_valid_shift = 9,
162 .temp_shift = 10,
163 .temp_mask = 0x1ff,
Ezequiel Garcia9484bc62014-05-06 13:59:46 -0300164 .coef_b = 3153000000UL,
165 .coef_m = 10000000UL,
166 .coef_div = 13825,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000167};
168
169static const struct of_device_id armada_thermal_id_table[] = {
170 {
171 .compatible = "marvell,armadaxp-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300172 .data = &armadaxp_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000173 },
174 {
175 .compatible = "marvell,armada370-thermal",
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300176 .data = &armada370_data,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000177 },
178 {
179 /* sentinel */
180 },
181};
182MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
183
184static int armada_thermal_probe(struct platform_device *pdev)
185{
186 struct thermal_zone_device *thermal;
187 const struct of_device_id *match;
188 struct armada_thermal_priv *priv;
189 struct resource *res;
190
191 match = of_match_device(armada_thermal_id_table, &pdev->dev);
192 if (!match)
193 return -ENODEV;
194
195 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
196 if (!priv)
197 return -ENOMEM;
198
199 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000200 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
201 if (IS_ERR(priv->sensor))
202 return PTR_ERR(priv->sensor);
203
204 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000205 priv->control = devm_ioremap_resource(&pdev->dev, res);
206 if (IS_ERR(priv->control))
207 return PTR_ERR(priv->control);
208
Ezequiel Garcia66fdb7b2014-05-06 13:59:45 -0300209 priv->data = (struct armada_thermal_data *)match->data;
Ezequiel Garcia04bf3d72014-05-06 13:59:48 -0300210 priv->data->init_sensor(pdev, priv);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000211
212 thermal = thermal_zone_device_register("armada_thermal", 0, 0,
213 priv, &ops, NULL, 0, 0);
214 if (IS_ERR(thermal)) {
215 dev_err(&pdev->dev,
216 "Failed to register thermal zone device\n");
217 return PTR_ERR(thermal);
218 }
219
220 platform_set_drvdata(pdev, thermal);
221
222 return 0;
223}
224
225static int armada_thermal_exit(struct platform_device *pdev)
226{
227 struct thermal_zone_device *armada_thermal =
228 platform_get_drvdata(pdev);
229
230 thermal_zone_device_unregister(armada_thermal);
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000231
232 return 0;
233}
234
235static struct platform_driver armada_thermal_driver = {
236 .probe = armada_thermal_probe,
237 .remove = armada_thermal_exit,
238 .driver = {
239 .name = "armada_thermal",
240 .owner = THIS_MODULE,
Sachin Kamat1d089e02013-05-16 10:28:08 +0000241 .of_match_table = armada_thermal_id_table,
Ezequiel Garciafa0d6542013-04-02 01:37:41 +0000242 },
243};
244
245module_platform_driver(armada_thermal_driver);
246
247MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
248MODULE_DESCRIPTION("Armada 370/XP thermal driver");
249MODULE_LICENSE("GPL v2");