Lines Matching +full:temp +full:- +full:sensor
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
18 #define mcelsius(temp) ((temp) * 1000) argument
24 static int st_thermal_alloc_regfields(struct st_thermal_sensor *sensor) in st_thermal_alloc_regfields() argument
26 struct device *dev = sensor->dev; in st_thermal_alloc_regfields()
27 struct regmap *regmap = sensor->regmap; in st_thermal_alloc_regfields()
28 const struct reg_field *reg_fields = sensor->cdata->reg_fields; in st_thermal_alloc_regfields()
30 sensor->dcorrect = devm_regmap_field_alloc(dev, regmap, in st_thermal_alloc_regfields()
33 sensor->overflow = devm_regmap_field_alloc(dev, regmap, in st_thermal_alloc_regfields()
36 sensor->temp_data = devm_regmap_field_alloc(dev, regmap, in st_thermal_alloc_regfields()
39 if (IS_ERR(sensor->dcorrect) || in st_thermal_alloc_regfields()
40 IS_ERR(sensor->overflow) || in st_thermal_alloc_regfields()
41 IS_ERR(sensor->temp_data)) { in st_thermal_alloc_regfields()
43 return -EINVAL; in st_thermal_alloc_regfields()
46 return sensor->ops->alloc_regfields(sensor); in st_thermal_alloc_regfields()
49 static int st_thermal_sensor_on(struct st_thermal_sensor *sensor) in st_thermal_sensor_on() argument
52 struct device *dev = sensor->dev; in st_thermal_sensor_on()
54 ret = clk_prepare_enable(sensor->clk); in st_thermal_sensor_on()
60 ret = sensor->ops->power_ctrl(sensor, POWER_ON); in st_thermal_sensor_on()
62 dev_err(dev, "failed to power on sensor\n"); in st_thermal_sensor_on()
63 clk_disable_unprepare(sensor->clk); in st_thermal_sensor_on()
69 static int st_thermal_sensor_off(struct st_thermal_sensor *sensor) in st_thermal_sensor_off() argument
73 ret = sensor->ops->power_ctrl(sensor, POWER_OFF); in st_thermal_sensor_off()
77 clk_disable_unprepare(sensor->clk); in st_thermal_sensor_off()
82 static int st_thermal_calibration(struct st_thermal_sensor *sensor) in st_thermal_calibration() argument
86 struct device *dev = sensor->dev; in st_thermal_calibration()
88 /* Check if sensor calibration data is already written */ in st_thermal_calibration()
89 ret = regmap_field_read(sensor->dcorrect, &val); in st_thermal_calibration()
97 * Sensor calibration value not set by bootloader, in st_thermal_calibration()
100 ret = regmap_field_write(sensor->dcorrect, in st_thermal_calibration()
101 sensor->cdata->calibration_val); in st_thermal_calibration()
112 struct st_thermal_sensor *sensor = thermal_zone_device_priv(th); in st_thermal_get_temp() local
113 unsigned int temp; in st_thermal_get_temp() local
117 ret = regmap_field_read(sensor->overflow, &overflow); in st_thermal_get_temp()
121 return -EIO; in st_thermal_get_temp()
123 ret = regmap_field_read(sensor->temp_data, &temp); in st_thermal_get_temp()
127 temp += sensor->cdata->temp_adjust_val; in st_thermal_get_temp()
128 temp = mcelsius(temp); in st_thermal_get_temp()
130 *temperature = temp; in st_thermal_get_temp()
142 struct st_thermal_sensor *sensor; in st_thermal_register() local
143 struct device *dev = &pdev->dev; in st_thermal_register()
144 struct device_node *np = dev->of_node; in st_thermal_register()
151 return -EINVAL; in st_thermal_register()
154 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); in st_thermal_register()
155 if (!sensor) in st_thermal_register()
156 return -ENOMEM; in st_thermal_register()
158 sensor->dev = dev; in st_thermal_register()
161 if (!(match && match->data)) in st_thermal_register()
162 return -EINVAL; in st_thermal_register()
164 sensor->cdata = match->data; in st_thermal_register()
165 if (!sensor->cdata->ops) in st_thermal_register()
166 return -EINVAL; in st_thermal_register()
168 sensor->ops = sensor->cdata->ops; in st_thermal_register()
170 ret = (sensor->ops->regmap_init)(sensor); in st_thermal_register()
174 ret = st_thermal_alloc_regfields(sensor); in st_thermal_register()
178 sensor->clk = devm_clk_get(dev, "thermal"); in st_thermal_register()
179 if (IS_ERR(sensor->clk)) { in st_thermal_register()
181 return PTR_ERR(sensor->clk); in st_thermal_register()
184 if (sensor->ops->register_enable_irq) { in st_thermal_register()
185 ret = sensor->ops->register_enable_irq(sensor); in st_thermal_register()
190 ret = st_thermal_sensor_on(sensor); in st_thermal_register()
194 ret = st_thermal_calibration(sensor); in st_thermal_register()
198 sensor->thermal_dev = in st_thermal_register()
199 devm_thermal_of_zone_register(dev, 0, sensor, &st_tz_ops); in st_thermal_register()
200 if (IS_ERR(sensor->thermal_dev)) { in st_thermal_register()
202 ret = PTR_ERR(sensor->thermal_dev); in st_thermal_register()
206 platform_set_drvdata(pdev, sensor); in st_thermal_register()
212 devm_thermal_add_hwmon_sysfs(dev, sensor->thermal_dev); in st_thermal_register()
217 st_thermal_sensor_off(sensor); in st_thermal_register()
225 struct st_thermal_sensor *sensor = platform_get_drvdata(pdev); in st_thermal_unregister() local
227 st_thermal_sensor_off(sensor); in st_thermal_unregister()
228 thermal_remove_hwmon_sysfs(sensor->thermal_dev); in st_thermal_unregister()
229 devm_thermal_of_zone_unregister(sensor->dev, sensor->thermal_dev); in st_thermal_unregister()
235 struct st_thermal_sensor *sensor = dev_get_drvdata(dev); in st_thermal_suspend() local
237 return st_thermal_sensor_off(sensor); in st_thermal_suspend()
243 struct st_thermal_sensor *sensor = dev_get_drvdata(dev); in st_thermal_resume() local
245 ret = st_thermal_sensor_on(sensor); in st_thermal_resume()
249 ret = st_thermal_calibration(sensor); in st_thermal_resume()
253 if (sensor->ops->enable_irq) { in st_thermal_resume()
254 ret = sensor->ops->enable_irq(sensor); in st_thermal_resume()
266 MODULE_DESCRIPTION("STMicroelectronics STi SoC Thermal Sensor Driver");