thermal/core: Use the thermal zone 'devdata' accessor in thermal located drivers
[linux-block.git] / drivers / thermal / ti-soc-thermal / ti-thermal-common.c
CommitLineData
2b27bdcc 1// SPDX-License-Identifier: GPL-2.0-only
445eaf87
EV
2/*
3 * OMAP thermal driver interface
4 *
5 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
6 * Contact:
7 * Eduardo Valentin <eduardo.valentin@ti.com>
445eaf87
EV
8 */
9
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>
4d753aa7 17#include <linux/cpufreq.h>
5035d48d 18#include <linux/cpumask.h>
445eaf87 19#include <linux/cpu_cooling.h>
26d9cc65 20#include <linux/of.h>
445eaf87 21
7372add4
EV
22#include "ti-thermal.h"
23#include "ti-bandgap.h"
3a9abd6c 24#include "../thermal_hwmon.h"
445eaf87
EV
25
26/* common data structures */
03e859d3 27struct ti_thermal_data {
4d753aa7 28 struct cpufreq_policy *policy;
03e859d3 29 struct thermal_zone_device *ti_thermal;
359836e1 30 struct thermal_zone_device *pcb_tz;
445eaf87 31 struct thermal_cooling_device *cool_dev;
03e859d3 32 struct ti_bandgap *bgp;
445eaf87
EV
33 enum thermal_device_mode mode;
34 struct work_struct thermal_wq;
35 int sensor_id;
26d9cc65 36 bool our_zone;
445eaf87
EV
37};
38
03e859d3 39static void ti_thermal_work(struct work_struct *work)
445eaf87 40{
03e859d3
EV
41 struct ti_thermal_data *data = container_of(work,
42 struct ti_thermal_data, thermal_wq);
445eaf87 43
0e70f466 44 thermal_zone_device_update(data->ti_thermal, THERMAL_EVENT_UNSPECIFIED);
445eaf87 45
03e859d3
EV
46 dev_dbg(&data->ti_thermal->device, "updated thermal zone %s\n",
47 data->ti_thermal->type);
445eaf87
EV
48}
49
50/**
03e859d3 51 * ti_thermal_hotspot_temperature - returns sensor extrapolated temperature
445eaf87
EV
52 * @t: omap sensor temperature
53 * @s: omap sensor slope value
54 * @c: omap sensor const value
55 */
03e859d3 56static inline int ti_thermal_hotspot_temperature(int t, int s, int c)
445eaf87
EV
57{
58 int delta = t * s / 1000 + c;
59
60 if (delta < 0)
61 delta = 0;
62
63 return t + delta;
64}
65
66/* thermal zone ops */
e34238bf 67/* Get temperature callback function for thermal zone */
2cf3c72a 68static inline int __ti_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
445eaf87 69{
359836e1 70 struct thermal_zone_device *pcb_tz = NULL;
5f68d078 71 struct ti_thermal_data *data = thermal_zone_device_priv(tz);
03e859d3 72 struct ti_bandgap *bgp;
9879b2c4 73 const struct ti_temp_sensor *s;
359836e1 74 int ret, tmp, slope, constant;
17e8351a 75 int pcb_temp;
445eaf87 76
04a4d10d
EV
77 if (!data)
78 return 0;
79
d7f080e6
EV
80 bgp = data->bgp;
81 s = &bgp->conf->sensors[data->sensor_id];
04a4d10d 82
03e859d3 83 ret = ti_bandgap_read_temperature(bgp, data->sensor_id, &tmp);
445eaf87
EV
84 if (ret)
85 return ret;
86
359836e1 87 /* Default constants */
2cf3c72a
DL
88 slope = thermal_zone_get_slope(tz);
89 constant = thermal_zone_get_offset(tz);
359836e1
EV
90
91 pcb_tz = data->pcb_tz;
445eaf87 92 /* In case pcb zone is available, use the extrapolation rule with it */
0c12b5ac 93 if (!IS_ERR(pcb_tz)) {
359836e1
EV
94 ret = thermal_zone_get_temp(pcb_tz, &pcb_temp);
95 if (!ret) {
96 tmp -= pcb_temp; /* got a valid PCB temp */
97 slope = s->slope_pcb;
98 constant = s->constant_pcb;
99 } else {
100 dev_err(bgp->dev,
101 "Failed to read PCB state. Using defaults\n");
df8f1347 102 ret = 0;
359836e1 103 }
445eaf87 104 }
03e859d3 105 *temp = ti_thermal_hotspot_temperature(tmp, slope, constant);
445eaf87
EV
106
107 return ret;
108}
109
2cf3c72a 110static int __ti_thermal_get_trend(struct thermal_zone_device *tz, int trip, enum thermal_trend *trend)
03b7f67b 111{
5f68d078 112 struct ti_thermal_data *data = thermal_zone_device_priv(tz);
03b7f67b
K
113 struct ti_bandgap *bgp;
114 int id, tr, ret = 0;
115
116 bgp = data->bgp;
117 id = data->sensor_id;
118
119 ret = ti_bandgap_get_trend(bgp, id, &tr);
120 if (ret)
121 return ret;
122
123 if (tr > 0)
124 *trend = THERMAL_TREND_RAISING;
125 else if (tr < 0)
126 *trend = THERMAL_TREND_DROPPING;
127 else
128 *trend = THERMAL_TREND_STABLE;
129
130 return 0;
131}
132
2cf3c72a 133static const struct thermal_zone_device_ops ti_of_thermal_ops = {
2251aef6
EV
134 .get_temp = __ti_thermal_get_temp,
135 .get_trend = __ti_thermal_get_trend,
136};
137
03e859d3
EV
138static struct ti_thermal_data
139*ti_thermal_build_data(struct ti_bandgap *bgp, int id)
445eaf87 140{
03e859d3 141 struct ti_thermal_data *data;
445eaf87 142
d7f080e6 143 data = devm_kzalloc(bgp->dev, sizeof(*data), GFP_KERNEL);
445eaf87 144 if (!data) {
d7f080e6 145 dev_err(bgp->dev, "kzalloc fail\n");
04a4d10d 146 return NULL;
445eaf87
EV
147 }
148 data->sensor_id = id;
d7f080e6 149 data->bgp = bgp;
445eaf87 150 data->mode = THERMAL_DEVICE_ENABLED;
0c12b5ac 151 /* pcb_tz will be either valid or PTR_ERR() */
359836e1 152 data->pcb_tz = thermal_zone_get_zone_by_name("pcb");
03e859d3 153 INIT_WORK(&data->thermal_wq, ti_thermal_work);
445eaf87 154
04a4d10d
EV
155 return data;
156}
157
03e859d3
EV
158int ti_thermal_expose_sensor(struct ti_bandgap *bgp, int id,
159 char *domain)
04a4d10d 160{
03e859d3 161 struct ti_thermal_data *data;
b39d2dd5 162 int interval;
04a4d10d 163
03e859d3 164 data = ti_bandgap_get_sensor_data(bgp, id);
04a4d10d 165
0f348db0 166 if (IS_ERR_OR_NULL(data))
03e859d3 167 data = ti_thermal_build_data(bgp, id);
04a4d10d
EV
168
169 if (!data)
170 return -EINVAL;
171
26d9cc65 172 /* in case this is specified by DT */
2cf3c72a 173 data->ti_thermal = devm_thermal_of_zone_register(bgp->dev, id,
2251aef6 174 data, &ti_of_thermal_ops);
26d9cc65 175 if (IS_ERR(data->ti_thermal)) {
b263b473
K
176 dev_err(bgp->dev, "thermal zone device is NULL\n");
177 return PTR_ERR(data->ti_thermal);
445eaf87 178 }
b263b473 179
b39d2dd5
DL
180 interval = jiffies_to_msecs(data->ti_thermal->polling_delay_jiffies);
181
03e859d3 182 ti_bandgap_set_sensor_data(bgp, id, data);
b39d2dd5 183 ti_bandgap_write_update_interval(bgp, data->sensor_id, interval);
445eaf87 184
3a9abd6c
RN
185 if (devm_thermal_add_hwmon_sysfs(data->ti_thermal))
186 dev_warn(bgp->dev, "failed to add hwmon sysfs attributes\n");
187
445eaf87
EV
188 return 0;
189}
190
03e859d3 191int ti_thermal_remove_sensor(struct ti_bandgap *bgp, int id)
445eaf87 192{
03e859d3 193 struct ti_thermal_data *data;
445eaf87 194
03e859d3 195 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87 196
7440f518 197 if (!IS_ERR_OR_NULL(data) && data->ti_thermal) {
26d9cc65
EV
198 if (data->our_zone)
199 thermal_zone_device_unregister(data->ti_thermal);
26d9cc65 200 }
445eaf87
EV
201
202 return 0;
203}
204
03e859d3 205int ti_thermal_report_sensor_temperature(struct ti_bandgap *bgp, int id)
445eaf87 206{
03e859d3 207 struct ti_thermal_data *data;
445eaf87 208
03e859d3 209 data = ti_bandgap_get_sensor_data(bgp, id);
445eaf87
EV
210
211 schedule_work(&data->thermal_wq);
212
213 return 0;
214}
215
03e859d3 216int ti_thermal_register_cpu_cooling(struct ti_bandgap *bgp, int id)
445eaf87 217{
03e859d3 218 struct ti_thermal_data *data;
26d9cc65
EV
219 struct device_node *np = bgp->dev->of_node;
220
221 /*
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.
225 */
226 if (of_find_property(np, "#thermal-sensor-cells", NULL))
227 return 0;
445eaf87 228
03e859d3 229 data = ti_bandgap_get_sensor_data(bgp, id);
0c12b5ac 230 if (!data || IS_ERR(data))
03e859d3 231 data = ti_thermal_build_data(bgp, id);
04a4d10d
EV
232
233 if (!data)
234 return -EINVAL;
445eaf87 235
4d753aa7
VK
236 data->policy = cpufreq_cpu_get(0);
237 if (!data->policy) {
238 pr_debug("%s: CPUFreq policy not found\n", __func__);
239 return -EPROBE_DEFER;
240 }
241
445eaf87 242 /* Register cooling device */
4d753aa7 243 data->cool_dev = cpufreq_cooling_register(data->policy);
0c12b5ac 244 if (IS_ERR(data->cool_dev)) {
cffafc32 245 int ret = PTR_ERR(data->cool_dev);
4d753aa7
VK
246 dev_err(bgp->dev, "Failed to register cpu cooling device %d\n",
247 ret);
248 cpufreq_cpu_put(data->policy);
cffafc32
EV
249
250 return ret;
445eaf87 251 }
03e859d3 252 ti_bandgap_set_sensor_data(bgp, id, data);
445eaf87
EV
253
254 return 0;
255}
256
03e859d3 257int ti_thermal_unregister_cpu_cooling(struct ti_bandgap *bgp, int id)
445eaf87 258{
03e859d3 259 struct ti_thermal_data *data;
445eaf87 260
03e859d3 261 data = ti_bandgap_get_sensor_data(bgp, id);
26d9cc65 262
7440f518 263 if (!IS_ERR_OR_NULL(data)) {
26d9cc65 264 cpufreq_cooling_unregister(data->cool_dev);
ba817a8c
TL
265 if (data->policy)
266 cpufreq_cpu_put(data->policy);
4d753aa7 267 }
445eaf87
EV
268
269 return 0;
270}