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"
25 /* common data structures */
26 struct ti_thermal_data
{
27 struct cpufreq_policy
*policy
;
28 struct thermal_zone_device
*ti_thermal
;
29 struct thermal_zone_device
*pcb_tz
;
30 struct thermal_cooling_device
*cool_dev
;
31 struct ti_bandgap
*bgp
;
32 enum thermal_device_mode mode
;
33 struct work_struct thermal_wq
;
38 static void ti_thermal_work(struct work_struct
*work
)
40 struct ti_thermal_data
*data
= container_of(work
,
41 struct ti_thermal_data
, thermal_wq
);
43 thermal_zone_device_update(data
->ti_thermal
, THERMAL_EVENT_UNSPECIFIED
);
45 dev_dbg(&data
->ti_thermal
->device
, "updated thermal zone %s\n",
46 data
->ti_thermal
->type
);
50 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
51 * @t: omap sensor temperature
52 * @s: omap sensor slope value
53 * @c: omap sensor const value
55 static inline int ti_thermal_hotspot_temperature(int t
, int s
, int c
)
57 int delta
= t
* s
/ 1000 + c
;
65 /* thermal zone ops */
66 /* Get temperature callback function for thermal zone */
67 static inline int __ti_thermal_get_temp(void *devdata
, int *temp
)
69 struct thermal_zone_device
*pcb_tz
= NULL
;
70 struct ti_thermal_data
*data
= devdata
;
71 struct ti_bandgap
*bgp
;
72 const struct ti_temp_sensor
*s
;
73 int ret
, tmp
, slope
, constant
;
80 s
= &bgp
->conf
->sensors
[data
->sensor_id
];
82 ret
= ti_bandgap_read_temperature(bgp
, data
->sensor_id
, &tmp
);
86 /* Default constants */
87 slope
= thermal_zone_get_slope(data
->ti_thermal
);
88 constant
= thermal_zone_get_offset(data
->ti_thermal
);
90 pcb_tz
= data
->pcb_tz
;
91 /* In case pcb zone is available, use the extrapolation rule with it */
92 if (!IS_ERR(pcb_tz
)) {
93 ret
= thermal_zone_get_temp(pcb_tz
, &pcb_temp
);
95 tmp
-= pcb_temp
; /* got a valid PCB temp */
97 constant
= s
->constant_pcb
;
100 "Failed to read PCB state. Using defaults\n");
104 *temp
= ti_thermal_hotspot_temperature(tmp
, slope
, constant
);
109 static inline int ti_thermal_get_temp(struct thermal_zone_device
*thermal
,
112 struct ti_thermal_data
*data
= thermal
->devdata
;
114 return __ti_thermal_get_temp(data
, temp
);
117 static int __ti_thermal_get_trend(void *p
, int trip
, enum thermal_trend
*trend
)
119 struct ti_thermal_data
*data
= p
;
120 struct ti_bandgap
*bgp
;
124 id
= data
->sensor_id
;
126 ret
= ti_bandgap_get_trend(bgp
, id
, &tr
);
131 *trend
= THERMAL_TREND_RAISING
;
133 *trend
= THERMAL_TREND_DROPPING
;
135 *trend
= THERMAL_TREND_STABLE
;
140 static const struct thermal_zone_of_device_ops ti_of_thermal_ops
= {
141 .get_temp
= __ti_thermal_get_temp
,
142 .get_trend
= __ti_thermal_get_trend
,
145 static struct ti_thermal_data
146 *ti_thermal_build_data(struct ti_bandgap
*bgp
, int id
)
148 struct ti_thermal_data
*data
;
150 data
= devm_kzalloc(bgp
->dev
, sizeof(*data
), GFP_KERNEL
);
152 dev_err(bgp
->dev
, "kzalloc fail\n");
155 data
->sensor_id
= id
;
157 data
->mode
= THERMAL_DEVICE_ENABLED
;
158 /* pcb_tz will be either valid or PTR_ERR() */
159 data
->pcb_tz
= thermal_zone_get_zone_by_name("pcb");
160 INIT_WORK(&data
->thermal_wq
, ti_thermal_work
);
165 int ti_thermal_expose_sensor(struct ti_bandgap
*bgp
, int id
,
168 struct ti_thermal_data
*data
;
170 data
= ti_bandgap_get_sensor_data(bgp
, id
);
172 if (IS_ERR_OR_NULL(data
))
173 data
= ti_thermal_build_data(bgp
, id
);
178 /* in case this is specified by DT */
179 data
->ti_thermal
= devm_thermal_zone_of_sensor_register(bgp
->dev
, id
,
180 data
, &ti_of_thermal_ops
);
181 if (IS_ERR(data
->ti_thermal
)) {
182 dev_err(bgp
->dev
, "thermal zone device is NULL\n");
183 return PTR_ERR(data
->ti_thermal
);
186 ti_bandgap_set_sensor_data(bgp
, id
, data
);
187 ti_bandgap_write_update_interval(bgp
, data
->sensor_id
,
188 data
->ti_thermal
->polling_delay
);
193 int ti_thermal_remove_sensor(struct ti_bandgap
*bgp
, int id
)
195 struct ti_thermal_data
*data
;
197 data
= ti_bandgap_get_sensor_data(bgp
, id
);
199 if (!IS_ERR_OR_NULL(data
) && data
->ti_thermal
) {
201 thermal_zone_device_unregister(data
->ti_thermal
);
207 int ti_thermal_report_sensor_temperature(struct ti_bandgap
*bgp
, int id
)
209 struct ti_thermal_data
*data
;
211 data
= ti_bandgap_get_sensor_data(bgp
, id
);
213 schedule_work(&data
->thermal_wq
);
218 int ti_thermal_register_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
220 struct ti_thermal_data
*data
;
221 struct device_node
*np
= bgp
->dev
->of_node
;
224 * We are assuming here that if one deploys the zone
225 * using DT, then it must be aware that the cooling device
226 * loading has to happen via cpufreq driver.
228 if (of_find_property(np
, "#thermal-sensor-cells", NULL
))
231 data
= ti_bandgap_get_sensor_data(bgp
, id
);
232 if (!data
|| IS_ERR(data
))
233 data
= ti_thermal_build_data(bgp
, id
);
238 data
->policy
= cpufreq_cpu_get(0);
240 pr_debug("%s: CPUFreq policy not found\n", __func__
);
241 return -EPROBE_DEFER
;
244 /* Register cooling device */
245 data
->cool_dev
= cpufreq_cooling_register(data
->policy
);
246 if (IS_ERR(data
->cool_dev
)) {
247 int ret
= PTR_ERR(data
->cool_dev
);
248 dev_err(bgp
->dev
, "Failed to register cpu cooling device %d\n",
250 cpufreq_cpu_put(data
->policy
);
254 ti_bandgap_set_sensor_data(bgp
, id
, data
);
259 int ti_thermal_unregister_cpu_cooling(struct ti_bandgap
*bgp
, int id
)
261 struct ti_thermal_data
*data
;
263 data
= ti_bandgap_get_sensor_data(bgp
, id
);
265 if (!IS_ERR_OR_NULL(data
)) {
266 cpufreq_cooling_unregister(data
->cool_dev
);
268 cpufreq_cpu_put(data
->policy
);