Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Dove thermal sensor driver
4 *
5 * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
6 */
7#include <linux/device.h>
8#include <linux/err.h>
9#include <linux/io.h>
10#include <linux/kernel.h>
11#include <linux/of.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/thermal.h>
15
16#define DOVE_THERMAL_TEMP_OFFSET 1
17#define DOVE_THERMAL_TEMP_MASK 0x1FF
18
19/* Dove Thermal Manager Control and Status Register */
20#define PMU_TM_DISABLE_OFFS 0
21#define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
22
23/* Dove Theraml Diode Control 0 Register */
24#define PMU_TDC0_SW_RST_MASK (0x1 << 1)
25#define PMU_TDC0_SEL_VCAL_OFFS 5
26#define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_OFFS)
27#define PMU_TDC0_REF_CAL_CNT_OFFS 11
28#define PMU_TDC0_REF_CAL_CNT_MASK (0x1FF << PMU_TDC0_REF_CAL_CNT_OFFS)
29#define PMU_TDC0_AVG_NUM_OFFS 25
30#define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
31
32/* Dove Thermal Diode Control 1 Register */
33#define PMU_TEMP_DIOD_CTRL1_REG 0x04
34#define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
35
36/* Dove Thermal Sensor Dev Structure */
37struct dove_thermal_priv {
38 void __iomem *sensor;
39 void __iomem *control;
40};
41
42static int dove_init_sensor(const struct dove_thermal_priv *priv)
43{
44 u32 reg;
45 u32 i;
46
47 /* Configure the Diode Control Register #0 */
48 reg = readl_relaxed(priv->control);
49
50 /* Use average of 2 */
51 reg &= ~PMU_TDC0_AVG_NUM_MASK;
52 reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
53
54 /* Reference calibration value */
55 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
56 reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
57
58 /* Set the high level reference for calibration */
59 reg &= ~PMU_TDC0_SEL_VCAL_MASK;
60 reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
61 writel(reg, priv->control);
62
63 /* Reset the sensor */
64 reg = readl_relaxed(priv->control);
65 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
66 writel(reg, priv->control);
67
68 /* Enable the sensor */
69 reg = readl_relaxed(priv->sensor);
70 reg &= ~PMU_TM_DISABLE_MASK;
71 writel(reg, priv->sensor);
72
73 /* Poll the sensor for the first reading */
74 for (i = 0; i < 1000000; i++) {
75 reg = readl_relaxed(priv->sensor);
76 if (reg & DOVE_THERMAL_TEMP_MASK)
77 break;
78 }
79
80 if (i == 1000000)
81 return -EIO;
82
83 return 0;
84}
85
86static int dove_get_temp(struct thermal_zone_device *thermal,
87 int *temp)
88{
89 unsigned long reg;
90 struct dove_thermal_priv *priv = thermal_zone_device_priv(thermal);
91
92 /* Valid check */
93 reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
94 if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0)
95 return -EIO;
96
97 /*
98 * Calculate temperature. According to Marvell internal
99 * documentation the formula for this is:
100 * Celsius = (322-reg)/1.3625
101 */
102 reg = readl_relaxed(priv->sensor);
103 reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
104 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
105
106 return 0;
107}
108
109static struct thermal_zone_device_ops ops = {
110 .get_temp = dove_get_temp,
111};
112
113static const struct of_device_id dove_thermal_id_table[] = {
114 { .compatible = "marvell,dove-thermal" },
115 {}
116};
117
118static int dove_thermal_probe(struct platform_device *pdev)
119{
120 struct thermal_zone_device *thermal = NULL;
121 struct dove_thermal_priv *priv;
122 int ret;
123
124 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
125 if (!priv)
126 return -ENOMEM;
127
128 priv->sensor = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
129 if (IS_ERR(priv->sensor))
130 return PTR_ERR(priv->sensor);
131
132 priv->control = devm_platform_get_and_ioremap_resource(pdev, 1, NULL);
133 if (IS_ERR(priv->control))
134 return PTR_ERR(priv->control);
135
136 ret = dove_init_sensor(priv);
137 if (ret) {
138 dev_err(&pdev->dev, "Failed to initialize sensor\n");
139 return ret;
140 }
141
142 thermal = thermal_tripless_zone_device_register("dove_thermal", priv,
143 &ops, NULL);
144 if (IS_ERR(thermal)) {
145 dev_err(&pdev->dev,
146 "Failed to register thermal zone device\n");
147 return PTR_ERR(thermal);
148 }
149
150 ret = thermal_zone_device_enable(thermal);
151 if (ret) {
152 thermal_zone_device_unregister(thermal);
153 return ret;
154 }
155
156 platform_set_drvdata(pdev, thermal);
157
158 return 0;
159}
160
161static void dove_thermal_exit(struct platform_device *pdev)
162{
163 struct thermal_zone_device *dove_thermal =
164 platform_get_drvdata(pdev);
165
166 thermal_zone_device_unregister(dove_thermal);
167}
168
169MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
170
171static struct platform_driver dove_thermal_driver = {
172 .probe = dove_thermal_probe,
173 .remove_new = dove_thermal_exit,
174 .driver = {
175 .name = "dove_thermal",
176 .of_match_table = dove_thermal_id_table,
177 },
178};
179
180module_platform_driver(dove_thermal_driver);
181
182MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
183MODULE_DESCRIPTION("Dove thermal driver");
184MODULE_LICENSE("GPL");
1/*
2 * Dove thermal sensor driver
3 *
4 * Copyright (C) 2013 Andrew Lunn <andrew@lunn.ch>
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/platform_device.h>
23#include <linux/thermal.h>
24
25#define DOVE_THERMAL_TEMP_OFFSET 1
26#define DOVE_THERMAL_TEMP_MASK 0x1FF
27
28/* Dove Thermal Manager Control and Status Register */
29#define PMU_TM_DISABLE_OFFS 0
30#define PMU_TM_DISABLE_MASK (0x1 << PMU_TM_DISABLE_OFFS)
31
32/* Dove Theraml Diode Control 0 Register */
33#define PMU_TDC0_SW_RST_MASK (0x1 << 1)
34#define PMU_TDC0_SEL_VCAL_OFFS 5
35#define PMU_TDC0_SEL_VCAL_MASK (0x3 << PMU_TDC0_SEL_VCAL_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_AVG_NUM_OFFS 25
39#define PMU_TDC0_AVG_NUM_MASK (0x7 << PMU_TDC0_AVG_NUM_OFFS)
40
41/* Dove Thermal Diode Control 1 Register */
42#define PMU_TEMP_DIOD_CTRL1_REG 0x04
43#define PMU_TDC1_TEMP_VALID_MASK (0x1 << 10)
44
45/* Dove Thermal Sensor Dev Structure */
46struct dove_thermal_priv {
47 void __iomem *sensor;
48 void __iomem *control;
49};
50
51static int dove_init_sensor(const struct dove_thermal_priv *priv)
52{
53 u32 reg;
54 u32 i;
55
56 /* Configure the Diode Control Register #0 */
57 reg = readl_relaxed(priv->control);
58
59 /* Use average of 2 */
60 reg &= ~PMU_TDC0_AVG_NUM_MASK;
61 reg |= (0x1 << PMU_TDC0_AVG_NUM_OFFS);
62
63 /* Reference calibration value */
64 reg &= ~PMU_TDC0_REF_CAL_CNT_MASK;
65 reg |= (0x0F1 << PMU_TDC0_REF_CAL_CNT_OFFS);
66
67 /* Set the high level reference for calibration */
68 reg &= ~PMU_TDC0_SEL_VCAL_MASK;
69 reg |= (0x2 << PMU_TDC0_SEL_VCAL_OFFS);
70 writel(reg, priv->control);
71
72 /* Reset the sensor */
73 reg = readl_relaxed(priv->control);
74 writel((reg | PMU_TDC0_SW_RST_MASK), priv->control);
75 writel(reg, priv->control);
76
77 /* Enable the sensor */
78 reg = readl_relaxed(priv->sensor);
79 reg &= ~PMU_TM_DISABLE_MASK;
80 writel(reg, priv->sensor);
81
82 /* Poll the sensor for the first reading */
83 for (i = 0; i < 1000000; i++) {
84 reg = readl_relaxed(priv->sensor);
85 if (reg & DOVE_THERMAL_TEMP_MASK)
86 break;
87 }
88
89 if (i == 1000000)
90 return -EIO;
91
92 return 0;
93}
94
95static int dove_get_temp(struct thermal_zone_device *thermal,
96 unsigned long *temp)
97{
98 unsigned long reg;
99 struct dove_thermal_priv *priv = thermal->devdata;
100
101 /* Valid check */
102 reg = readl_relaxed(priv->control + PMU_TEMP_DIOD_CTRL1_REG);
103 if ((reg & PMU_TDC1_TEMP_VALID_MASK) == 0x0) {
104 dev_err(&thermal->device,
105 "Temperature sensor reading not valid\n");
106 return -EIO;
107 }
108
109 /*
110 * Calculate temperature. According to Marvell internal
111 * documentation the formula for this is:
112 * Celsius = (322-reg)/1.3625
113 */
114 reg = readl_relaxed(priv->sensor);
115 reg = (reg >> DOVE_THERMAL_TEMP_OFFSET) & DOVE_THERMAL_TEMP_MASK;
116 *temp = ((3220000000UL - (10000000UL * reg)) / 13625);
117
118 return 0;
119}
120
121static struct thermal_zone_device_ops ops = {
122 .get_temp = dove_get_temp,
123};
124
125static const struct of_device_id dove_thermal_id_table[] = {
126 { .compatible = "marvell,dove-thermal" },
127 {}
128};
129
130static int dove_thermal_probe(struct platform_device *pdev)
131{
132 struct thermal_zone_device *thermal = NULL;
133 struct dove_thermal_priv *priv;
134 struct resource *res;
135 int ret;
136
137 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
138 if (!priv)
139 return -ENOMEM;
140
141 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 priv->sensor = devm_ioremap_resource(&pdev->dev, res);
143 if (IS_ERR(priv->sensor))
144 return PTR_ERR(priv->sensor);
145
146 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
147 priv->control = devm_ioremap_resource(&pdev->dev, res);
148 if (IS_ERR(priv->control))
149 return PTR_ERR(priv->control);
150
151 ret = dove_init_sensor(priv);
152 if (ret) {
153 dev_err(&pdev->dev, "Failed to initialize sensor\n");
154 return ret;
155 }
156
157 thermal = thermal_zone_device_register("dove_thermal", 0, 0,
158 priv, &ops, NULL, 0, 0);
159 if (IS_ERR(thermal)) {
160 dev_err(&pdev->dev,
161 "Failed to register thermal zone device\n");
162 return PTR_ERR(thermal);
163 }
164
165 platform_set_drvdata(pdev, thermal);
166
167 return 0;
168}
169
170static int dove_thermal_exit(struct platform_device *pdev)
171{
172 struct thermal_zone_device *dove_thermal =
173 platform_get_drvdata(pdev);
174
175 thermal_zone_device_unregister(dove_thermal);
176
177 return 0;
178}
179
180MODULE_DEVICE_TABLE(of, dove_thermal_id_table);
181
182static struct platform_driver dove_thermal_driver = {
183 .probe = dove_thermal_probe,
184 .remove = dove_thermal_exit,
185 .driver = {
186 .name = "dove_thermal",
187 .owner = THIS_MODULE,
188 .of_match_table = dove_thermal_id_table,
189 },
190};
191
192module_platform_driver(dove_thermal_driver);
193
194MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
195MODULE_DESCRIPTION("Dove thermal driver");
196MODULE_LICENSE("GPL");