Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / thermal / armada_thermal.c
blob5e53212b984fd183f329a0fb7f7b48e778199f12
1 /*
2 * Marvell Armada 370/XP thermal sensor driver
4 * Copyright (C) 2013 Marvell
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.
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.
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>
27 #define THERMAL_VALID_OFFSET 9
28 #define THERMAL_VALID_MASK 0x1
29 #define THERMAL_TEMP_OFFSET 10
30 #define THERMAL_TEMP_MASK 0x1ff
32 /* Thermal Manager Control and Status Register */
33 #define PMU_TDC0_SW_RST_MASK (0x1 << 1)
34 #define PMU_TM_DISABLE_OFFS 0
35 #define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
36 #define PMU_TDC0_REF_CAL_CNT_OFFS 11
37 #define PMU_TDC0_REF_CAL_CNT_MASK (0x1ff << PMU_TDC0_REF_CAL_CNT_OFFS)
38 #define PMU_TDC0_OTF_CAL_MASK (0x1 << 30)
39 #define PMU_TDC0_START_CAL_MASK (0x1 << 25)
41 struct armada_thermal_ops;
43 /* Marvell EBU Thermal Sensor Dev Structure */
44 struct armada_thermal_priv {
45 void __iomem *sensor;
46 void __iomem *control;
47 struct armada_thermal_ops *ops;
50 struct armada_thermal_ops {
51 /* Initialize the sensor */
52 void (*init_sensor)(struct armada_thermal_priv *);
54 /* Test for a valid sensor value (optional) */
55 bool (*is_valid)(struct armada_thermal_priv *);
58 static void armadaxp_init_sensor(struct armada_thermal_priv *priv)
60 unsigned long reg;
62 reg = readl_relaxed(priv->control);
63 reg |= PMU_TDC0_OTF_CAL_MASK;
64 writel(reg, priv->control);
66 /* Reference calibration value */
67 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
68 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
69 writel(reg, priv->control);
71 /* Reset the sensor */
72 reg = readl_relaxed(priv->control);
73 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
75 writel(reg, priv->control);
77 /* Enable the sensor */
78 reg = readl_relaxed(priv->sensor);
79 reg &= ~PMU_TM_DISABLE_MASK;
80 writel(reg, priv->sensor);
83 static void armada370_init_sensor(struct armada_thermal_priv *priv)
85 unsigned long reg;
87 reg = readl_relaxed(priv->control);
88 reg |= PMU_TDC0_OTF_CAL_MASK;
89 writel(reg, priv->control);
91 /* Reference calibration value */
92 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
93 reg |= (0xf1 << PMU_TDC0_REF_CAL_CNT_OFFS);
94 writel(reg, priv->control);
96 reg &= ~PMU_TDC0_START_CAL_MASK;
97 writel(reg, priv->control);
99 mdelay(10);
102 static bool armada_is_valid(struct armada_thermal_priv *priv)
104 unsigned long reg = readl_relaxed(priv->sensor);
106 return (reg >> THERMAL_VALID_OFFSET) & THERMAL_VALID_MASK;
109 static int armada_get_temp(struct thermal_zone_device *thermal,
110 unsigned long *temp)
112 struct armada_thermal_priv *priv = thermal->devdata;
113 unsigned long reg;
115 /* Valid check */
116 if (priv->ops->is_valid && !priv->ops->is_valid(priv)) {
117 dev_err(&thermal->device,
118 "Temperature sensor reading not valid\n");
119 return -EIO;
122 reg = readl_relaxed(priv->sensor);
123 reg = (reg >> THERMAL_TEMP_OFFSET) & THERMAL_TEMP_MASK;
124 *temp = (3153000000UL - (10000000UL*reg)) / 13825;
125 return 0;
128 static struct thermal_zone_device_ops ops = {
129 .get_temp = armada_get_temp,
132 static const struct armada_thermal_ops armadaxp_ops = {
133 .init_sensor = armadaxp_init_sensor,
136 static const struct armada_thermal_ops armada370_ops = {
137 .is_valid = armada_is_valid,
138 .init_sensor = armada370_init_sensor,
141 static const struct of_device_id armada_thermal_id_table[] = {
143 .compatible = "marvell,armadaxp-thermal",
144 .data = &armadaxp_ops,
147 .compatible = "marvell,armada370-thermal",
148 .data = &armada370_ops,
151 /* sentinel */
154 MODULE_DEVICE_TABLE(of, armada_thermal_id_table);
156 static int armada_thermal_probe(struct platform_device *pdev)
158 struct thermal_zone_device *thermal;
159 const struct of_device_id *match;
160 struct armada_thermal_priv *priv;
161 struct resource *res;
163 match = of_match_device(armada_thermal_id_table, &pdev->dev);
164 if (!match)
165 return -ENODEV;
167 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
168 if (!priv)
169 return -ENOMEM;
171 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
173 if (IS_ERR(priv->sensor))
174 return PTR_ERR(priv->sensor);
176 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
177 priv->control = devm_ioremap_resource(&pdev->dev, res);
178 if (IS_ERR(priv->control))
179 return PTR_ERR(priv->control);
181 priv->ops = (struct armada_thermal_ops *)match->data;
182 priv->ops->init_sensor(priv);
184 thermal = thermal_zone_device_register("armada_thermal", 0, 0,
185 priv, &ops, NULL, 0, 0);
186 if (IS_ERR(thermal)) {
187 dev_err(&pdev->dev,
188 "Failed to register thermal zone device\n");
189 return PTR_ERR(thermal);
192 platform_set_drvdata(pdev, thermal);
194 return 0;
197 static int armada_thermal_exit(struct platform_device *pdev)
199 struct thermal_zone_device *armada_thermal =
200 platform_get_drvdata(pdev);
202 thermal_zone_device_unregister(armada_thermal);
204 return 0;
207 static struct platform_driver armada_thermal_driver = {
208 .probe = armada_thermal_probe,
209 .remove = armada_thermal_exit,
210 .driver = {
211 .name = "armada_thermal",
212 .owner = THIS_MODULE,
213 .of_match_table = armada_thermal_id_table,
217 module_platform_driver(armada_thermal_driver);
219 MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@free-electrons.com>");
220 MODULE_DESCRIPTION("Armada 370/XP thermal driver");
221 MODULE_LICENSE("GPL v2");