1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ST Thermal Sensor Driver core routines
6 * Copyright (C) 2003-2014 STMicroelectronics (R&D) Limited
10 #include <linux/module.h>
12 #include <linux/of_device.h>
14 #include "st_thermal.h"
16 /* The Thermal Framework expects millidegrees */
17 #define mcelsius(temp) ((temp) * 1000)
20 * Function to allocate regfields which are common
21 * between syscfg and memory mapped based sensors
23 static int st_thermal_alloc_regfields(struct st_thermal_sensor *sensor)
25 struct device *dev = sensor->dev;
26 struct regmap *regmap = sensor->regmap;
27 const struct reg_field *reg_fields = sensor->cdata->reg_fields;
29 sensor->dcorrect = devm_regmap_field_alloc(dev, regmap,
30 reg_fields[DCORRECT]);
32 sensor->overflow = devm_regmap_field_alloc(dev, regmap,
33 reg_fields[OVERFLOW]);
35 sensor->temp_data = devm_regmap_field_alloc(dev, regmap,
38 if (IS_ERR(sensor->dcorrect) ||
39 IS_ERR(sensor->overflow) ||
40 IS_ERR(sensor->temp_data)) {
41 dev_err(dev, "failed to allocate common regfields\n");
45 return sensor->ops->alloc_regfields(sensor);
48 static int st_thermal_sensor_on(struct st_thermal_sensor *sensor)
51 struct device *dev = sensor->dev;
53 ret = clk_prepare_enable(sensor->clk);
55 dev_err(dev, "failed to enable clk\n");
59 ret = sensor->ops->power_ctrl(sensor, POWER_ON);
61 dev_err(dev, "failed to power on sensor\n");
62 clk_disable_unprepare(sensor->clk);
68 static int st_thermal_sensor_off(struct st_thermal_sensor *sensor)
72 ret = sensor->ops->power_ctrl(sensor, POWER_OFF);
76 clk_disable_unprepare(sensor->clk);
81 static int st_thermal_calibration(struct st_thermal_sensor *sensor)
85 struct device *dev = sensor->dev;
87 /* Check if sensor calibration data is already written */
88 ret = regmap_field_read(sensor->dcorrect, &val);
90 dev_err(dev, "failed to read calibration data\n");
96 * Sensor calibration value not set by bootloader,
97 * default calibration data to be used
99 ret = regmap_field_write(sensor->dcorrect,
100 sensor->cdata->calibration_val);
102 dev_err(dev, "failed to set calibration data\n");
108 /* Callback to get temperature from HW*/
109 static int st_thermal_get_temp(struct thermal_zone_device *th, int *temperature)
111 struct st_thermal_sensor *sensor = thermal_zone_device_priv(th);
113 unsigned int overflow;
116 ret = regmap_field_read(sensor->overflow, &overflow);
122 ret = regmap_field_read(sensor->temp_data, &temp);
126 temp += sensor->cdata->temp_adjust_val;
127 temp = mcelsius(temp);
134 static struct thermal_zone_device_ops st_tz_ops = {
135 .get_temp = st_thermal_get_temp,
138 static struct thermal_trip trip;
140 int st_thermal_register(struct platform_device *pdev,
141 const struct of_device_id *st_thermal_of_match)
143 struct st_thermal_sensor *sensor;
144 struct device *dev = &pdev->dev;
145 struct device_node *np = dev->of_node;
146 const struct of_device_id *match;
152 dev_err(dev, "device tree node not found\n");
156 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
162 match = of_match_device(st_thermal_of_match, dev);
163 if (!(match && match->data))
166 sensor->cdata = match->data;
167 if (!sensor->cdata->ops)
170 sensor->ops = sensor->cdata->ops;
172 ret = (sensor->ops->regmap_init)(sensor);
176 ret = st_thermal_alloc_regfields(sensor);
180 sensor->clk = devm_clk_get(dev, "thermal");
181 if (IS_ERR(sensor->clk)) {
182 dev_err(dev, "failed to fetch clock\n");
183 return PTR_ERR(sensor->clk);
186 if (sensor->ops->register_enable_irq) {
187 ret = sensor->ops->register_enable_irq(sensor);
192 ret = st_thermal_sensor_on(sensor);
196 ret = st_thermal_calibration(sensor);
200 polling_delay = sensor->ops->register_enable_irq ? 0 : 1000;
202 trip.temperature = sensor->cdata->crit_temp;
203 trip.type = THERMAL_TRIP_CRITICAL;
205 sensor->thermal_dev =
206 thermal_zone_device_register_with_trips(dev_name(dev), &trip, 1, 0, sensor,
207 &st_tz_ops, NULL, 0, polling_delay);
208 if (IS_ERR(sensor->thermal_dev)) {
209 dev_err(dev, "failed to register thermal zone device\n");
210 ret = PTR_ERR(sensor->thermal_dev);
213 ret = thermal_zone_device_enable(sensor->thermal_dev);
217 platform_set_drvdata(pdev, sensor);
222 thermal_zone_device_unregister(sensor->thermal_dev);
224 st_thermal_sensor_off(sensor);
228 EXPORT_SYMBOL_GPL(st_thermal_register);
230 void st_thermal_unregister(struct platform_device *pdev)
232 struct st_thermal_sensor *sensor = platform_get_drvdata(pdev);
234 st_thermal_sensor_off(sensor);
235 thermal_zone_device_unregister(sensor->thermal_dev);
237 EXPORT_SYMBOL_GPL(st_thermal_unregister);
239 #ifdef CONFIG_PM_SLEEP
240 static int st_thermal_suspend(struct device *dev)
242 struct st_thermal_sensor *sensor = dev_get_drvdata(dev);
244 return st_thermal_sensor_off(sensor);
247 static int st_thermal_resume(struct device *dev)
250 struct st_thermal_sensor *sensor = dev_get_drvdata(dev);
252 ret = st_thermal_sensor_on(sensor);
256 ret = st_thermal_calibration(sensor);
260 if (sensor->ops->enable_irq) {
261 ret = sensor->ops->enable_irq(sensor);
270 SIMPLE_DEV_PM_OPS(st_thermal_pm_ops, st_thermal_suspend, st_thermal_resume);
271 EXPORT_SYMBOL_GPL(st_thermal_pm_ops);
274 MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");
275 MODULE_LICENSE("GPL v2");