1 // SPDX-License-Identifier: GPL-2.0-only
3 * OMAP thermal driver interface
5 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
7 * Eduardo Valentin <eduardo.valentin@ti.com>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/mutex.h>
13 #include <linux/gfp.h>
14 #include <linux/kernel.h>
15 #include <linux/workqueue.h>
16 #include <linux/thermal.h>
17 #include <linux/cpufreq.h>
18 #include <linux/cpumask.h>
19 #include <linux/cpu_cooling.h>
22 #include "ti-thermal.h"
23 #include "ti-bandgap.h"
24 #include "../thermal_hwmon.h"
26 /* common data structures */
27 struct ti_thermal_data {
28 struct cpufreq_policy *policy;
29 struct thermal_zone_device *ti_thermal;
30 struct thermal_zone_device *pcb_tz;
31 struct thermal_cooling_device *cool_dev;
32 struct ti_bandgap *bgp;
33 enum thermal_device_mode mode;
34 struct work_struct thermal_wq;
39 static void ti_thermal_work(struct work_struct *work)
41 struct ti_thermal_data *data = container_of(work,
42 struct ti_thermal_data, thermal_wq);
44 thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED);
46 dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
47 data->ti_thermal->type);
51 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
52 * @t: omap sensor temperature
53 * @s: omap sensor slope value
54 * @c: omap sensor const value
56 static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
58 int delta = t * s / 1000 + c;
66 /* thermal zone ops */
67 /* Get temperature callback function for thermal zone */
68 static inline int __ti_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
70 struct thermal_zone_device *pcb_tz = NULL;
71 struct ti_thermal_data *data = tz->devdata;
72 struct ti_bandgap *bgp;
73 const struct ti_temp_sensor *s;
74 int ret, tmp, slope, constant;
81 s = &bgp->conf->sensors[data->sensor_id];
83 ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
87 /* Default constants */
88 slope = thermal_zone_get_slope(tz);
89 constant = thermal_zone_get_offset(tz);
91 pcb_tz = data->pcb_tz;
92 /* In case pcb zone is available, use the extrapolation rule with it */
93 if (!IS_ERR(pcb_tz)) {
94 ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
96 tmp -= pcb_temp; /* got a valid PCB temp */
98 constant = s->constant_pcb;
101 "Failed to read PCB state. Using defaults\n");
105 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
110 static int __ti_thermal_get_trend(struct thermal_zone_device *tz, int trip, enum thermal_trend *trend)
112 struct ti_thermal_data *data = tz->devdata;
113 struct ti_bandgap *bgp;
117 id = data->sensor_id;
119 ret = ti_bandgap_get_trend(bgp, id, &tr);
124 *trend = THERMAL_TREND_RAISING;
126 *trend = THERMAL_TREND_DROPPING;
128 *trend = THERMAL_TREND_STABLE;
133 static const struct thermal_zone_device_ops ti_of_thermal_ops = {
134 .get_temp = __ti_thermal_get_temp,
135 .get_trend = __ti_thermal_get_trend,
138 static struct ti_thermal_data
139 *ti_thermal_build_data(struct ti_bandgap *bgp, int id)
141 struct ti_thermal_data *data;
143 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
145 dev_err(bgp->dev, "kzalloc fail\n");
148 data->sensor_id = id;
150 data->mode = THERMAL_DEVICE_ENABLED;
151 /* pcb_tz will be either valid or PTR_ERR() */
152 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
153 INIT_WORK(&data->thermal_wq, ti_thermal_work);
158 int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
161 struct ti_thermal_data *data;
164 data = ti_bandgap_get_sensor_data(bgp, id);
166 if (IS_ERR_OR_NULL(data))
167 data = ti_thermal_build_data(bgp, id);
172 /* in case this is specified by DT */
173 data->ti_thermal = devm_thermal_of_zone_register(bgp->dev, id,
174 data, &ti_of_thermal_ops);
175 if (IS_ERR(data->ti_thermal)) {
176 dev_err(bgp->dev, "thermal zone device is NULL\n");
177 return PTR_ERR(data->ti_thermal);
180 interval = jiffies_to_msecs(data->ti_thermal->polling_delay_jiffies);
182 ti_bandgap_set_sensor_data(bgp, id, data);
183 ti_bandgap_write_update_interval(bgp, data->sensor_id, interval);
185 if (devm_thermal_add_hwmon_sysfs(data->ti_thermal))
186 dev_warn(bgp->dev, "failed to add hwmon sysfs attributes\n");
191 int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
193 struct ti_thermal_data *data;
195 data = ti_bandgap_get_sensor_data(bgp, id);
197 if (!IS_ERR_OR_NULL(data) && data->ti_thermal) {
199 thermal_zone_device_unregister(data->ti_thermal);
205 int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
207 struct ti_thermal_data *data;
209 data = ti_bandgap_get_sensor_data(bgp, id);
211 schedule_work(&data->thermal_wq);
216 int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
218 struct ti_thermal_data *data;
219 struct device_node *np = bgp->dev->of_node;
222 * We are assuming here that if one deploys the zone
223 * using DT, then it must be aware that the cooling device
224 * loading has to happen via cpufreq driver.
226 if (of_find_property(np, "#thermal-sensor-cells", NULL))
229 data = ti_bandgap_get_sensor_data(bgp, id);
230 if (!data || IS_ERR(data))
231 data = ti_thermal_build_data(bgp, id);
236 data->policy = cpufreq_cpu_get(0);
238 pr_debug("%s: CPUFreq policy not found\n", __func__);
239 return -EPROBE_DEFER;
242 /* Register cooling device */
243 data->cool_dev = cpufreq_cooling_register(data->policy);
244 if (IS_ERR(data->cool_dev)) {
245 int ret = PTR_ERR(data->cool_dev);
246 dev_err(bgp->dev, "Failed to register cpu cooling device %d\n",
248 cpufreq_cpu_put(data->policy);
252 ti_bandgap_set_sensor_data(bgp, id, data);
257 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
259 struct ti_thermal_data *data;
261 data = ti_bandgap_get_sensor_data(bgp, id);
263 if (!IS_ERR_OR_NULL(data)) {
264 cpufreq_cooling_unregister(data->cool_dev);
266 cpufreq_cpu_put(data->policy);