summaryrefslogtreecommitdiff
path: root/gxp-thermal.c
blob: 8a9c24bba0cf17dcfc7a7b6045931c4928a5abf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// SPDX-License-Identifier: GPL-2.0
/*
 * Platform thermal driver for GXP.
 *
 * Copyright (C) 2021 Google LLC
 */

#include <linux/acpm_dvfs.h>
#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/thermal.h>
#include <linux/version.h>

#include <gcip/gcip-pm.h>

/*
 * thermal_cdev_update is moved to drivers/thermal/thermal_core.h in kernel
 * 5.12. The symbol is still exported, manually declare the function prototype
 * to get rid of the implicit declaration compilation error.
 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
void thermal_cdev_update(struct thermal_cooling_device *cdev);
#endif

#include "gxp-internal.h"
#include "gxp-pm.h"
#include "gxp-thermal.h"
#include "gxp-lpm.h"

#if GXP_HAS_MCU
#include "gxp-kci.h"
#include "gxp-mcu.h"
#endif /* GXP_HAS_MCU */

/*
 * Value comes from internal measurement
 * b/229623553
 */
static struct gxp_state_pwr state_pwr_map[] = {
	{ AUR_NOM_RATE, 78 },
	{ AUR_UD_PLUS_RATE, 58 },
	{ AUR_UD_RATE, 40 },
	{ AUR_SUD_PLUS_RATE, 27 },
	{ AUR_SUD_RATE, 20 },
	{ AUR_UUD_PLUS_RATE, 16 },
	{ AUR_UUD_RATE, 13 },
};

static int gxp_get_max_state(struct thermal_cooling_device *cdev,
			     unsigned long *state)
{
	struct gxp_thermal_manager *thermal = cdev->devdata;

	if (!thermal->gxp_num_states)
		return -EIO;

	*state = thermal->gxp_num_states - 1;
	return 0;
}

/*
 * Set cooling state.
 */
static int gxp_set_cur_state(struct thermal_cooling_device *cdev,
				     unsigned long cooling_state)
{
	int ret = 0;
	struct gxp_thermal_manager *thermal = cdev->devdata;
	struct gxp_dev *gxp = thermal->gxp;
	struct device *dev = gxp->dev;
	unsigned long pwr_state;

	if (cooling_state >= thermal->gxp_num_states) {
		dev_err(dev, "%s: invalid cooling state %lu\n", __func__,
			cooling_state);
		return -EINVAL;
	}

	mutex_lock(&thermal->lock);
	cooling_state = max(thermal->sysfs_req, cooling_state);
	if (cooling_state >= ARRAY_SIZE(state_pwr_map)) {
		dev_err(dev, "Unsupported cooling state: %lu\n", cooling_state);
		ret = -EINVAL;
		goto out;
	}
	pwr_state = state_pwr_map[cooling_state].state;
	dev_dbg(dev, "setting state %ld\n", pwr_state);
	if (cooling_state != thermal->cooling_state) {
		if (!gxp_is_direct_mode(gxp)) {
#if GXP_HAS_MCU
			struct gxp_mcu *mcu = gxp_mcu_of(gxp);

			ret = gcip_pm_get_if_powered(mcu->gxp->power_mgr->pm,
						     false);
			if (ret) {
				dev_err(dev,
					"Can't acquire wakelock when powered down: %d\n",
					ret);
				goto out;
			}

			ret = gxp_kci_notify_throttling(&mcu->kci, pwr_state);
			gcip_pm_put(gxp->power_mgr->pm);
#endif /* GXP_HAS_MCU */
		} else {
			ret = gxp_pm_blk_set_rate_acpm(
				gxp,
				max(pwr_state,
				    (unsigned long)
					    aur_power_state2rate[AUR_UUD]));
		}

		if (ret) {
			dev_err(dev, "error setting gxp cooling state: %d\n",
				ret);
			ret = -ENODEV;
			goto out;
		}
		thermal->cooling_state = cooling_state;
		gxp_pm_set_thermal_limit(thermal->gxp, pwr_state);
	} else {
		ret = -EALREADY;
	}

out:
	mutex_unlock(&thermal->lock);
	return ret;
}

static int gxp_get_cur_state(struct thermal_cooling_device *cdev,
			     unsigned long *state)
{
	int ret = 0;
	struct gxp_thermal_manager *thermal = cdev->devdata;

	mutex_lock(&thermal->lock);
	*state = thermal->cooling_state;
	if (*state >= thermal->gxp_num_states) {
		dev_err(thermal->gxp->dev,
			"Unknown cooling state: %lu, resetting\n", *state);
		ret = -EINVAL;
		goto out;
	}
out:
	mutex_unlock(&thermal->lock);
	return ret;
}

static int gxp_state2power_internal(unsigned long state, u32 *power,
				    struct gxp_thermal_manager *thermal)
{
	int i;

	for (i = 0; i < thermal->gxp_num_states; i++) {
		if (state == state_pwr_map[i].state) {
			*power = state_pwr_map[i].power;
			return 0;
		}
	}
	dev_err(thermal->gxp->dev, "Unknown state req for: %lu\n", state);
	*power = 0;
	return -EINVAL;
}

static int gxp_get_requested_power(struct thermal_cooling_device *cdev,
				   u32 *power)
{
	unsigned long power_state;
	struct gxp_thermal_manager *cooling = cdev->devdata;

	power_state = exynos_acpm_get_rate(AUR_DVFS_DOMAIN, 0);
	return gxp_state2power_internal(power_state, power, cooling);
}

/* TODO(b/213272324): Move state2power table to dts */
static int gxp_state2power(struct thermal_cooling_device *cdev,
			       unsigned long state, u32 *power)
{
	struct gxp_thermal_manager *thermal = cdev->devdata;

	if (state >= thermal->gxp_num_states) {
		dev_err(thermal->gxp->dev, "%s: invalid state: %lu\n", __func__,
			state);
		return -EINVAL;
	}

	return gxp_state2power_internal(state_pwr_map[state].state, power,
					thermal);
}

static int gxp_power2state(struct thermal_cooling_device *cdev,
			       u32 power, unsigned long *state)
{
	int i, penultimate_throttle_state;
	struct gxp_thermal_manager *thermal = cdev->devdata;

	*state = 0;
	/* Less than 2 state means we cannot really throttle */
	if (thermal->gxp_num_states < 2)
		return thermal->gxp_num_states == 1 ? 0 : -EIO;

	penultimate_throttle_state = thermal->gxp_num_states - 2;
	/*
	 * argument "power" is the maximum allowed power consumption in mW as
	 * defined by the PID control loop. Check for the first state that is
	 * less than or equal to the current allowed power. state_pwr_map is
	 * descending, so lowest power consumption is last value in the array
	 * return lowest state even if it consumes more power than allowed as
	 * not all platforms can handle throttling below an active state
	 */
	for (i = penultimate_throttle_state; i >= 0; --i) {
		if (power < state_pwr_map[i].power) {
			*state = i + 1;
			break;
		}
	}
	return 0;
}

static struct thermal_cooling_device_ops gxp_cooling_ops = {
	.get_max_state = gxp_get_max_state,
	.get_cur_state = gxp_get_cur_state,
	.set_cur_state = gxp_set_cur_state,
	.get_requested_power = gxp_get_requested_power,
	.state2power = gxp_state2power,
	.power2state = gxp_power2state,
};

static void gxp_thermal_exit(struct gxp_thermal_manager *thermal)
{
	if (!IS_ERR_OR_NULL(thermal->cdev))
		thermal_cooling_device_unregister(thermal->cdev);
}

static void devm_gxp_thermal_release(struct device *dev, void *res)
{
	struct gxp_thermal_manager *thermal = res;

	gxp_thermal_exit(thermal);
}

static ssize_t
user_vote_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct thermal_cooling_device *cdev =
			container_of(dev, struct thermal_cooling_device,
				     device);
	struct gxp_thermal_manager *cooling = cdev->devdata;

	if (!cooling)
		return -ENODEV;

	return sysfs_emit(buf, "%lu\n", cooling->sysfs_req);
}

static ssize_t user_vote_store(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t count)
{
	struct thermal_cooling_device *cdev =
			container_of(dev, struct thermal_cooling_device,
				     device);
	struct gxp_thermal_manager *cooling = cdev->devdata;
	int ret;
	unsigned long state;

	if (!cooling)
		return -ENODEV;

	ret = kstrtoul(buf, 0, &state);
	if (ret)
		return ret;

	if (state >= cooling->gxp_num_states)
		return -EINVAL;

	mutex_lock(&cdev->lock);
	cooling->sysfs_req = state;
	cdev->updated = false;
	mutex_unlock(&cdev->lock);
	thermal_cdev_update(cdev);
	return count;
}

static DEVICE_ATTR_RW(user_vote);

static int
gxp_thermal_cooling_register(struct gxp_thermal_manager *thermal, char *type)
{
	struct device_node *cooling_node = NULL;

	thermal->op_data = NULL;
	thermal->gxp_num_states = ARRAY_SIZE(state_pwr_map);

	mutex_init(&thermal->lock);
	cooling_node = of_find_node_by_name(NULL, GXP_COOLING_NAME);

	/* TODO: Change this to fatal error once dts change is merged */
	if (!cooling_node)
		dev_warn(thermal->gxp->dev, "failed to find cooling node\n");
	/* Initialize the cooling state as 0, means "no cooling" */
	thermal->cooling_state = 0;
	thermal->cdev = thermal_of_cooling_device_register(
		cooling_node, type, thermal, &gxp_cooling_ops);
	if (IS_ERR(thermal->cdev))
		return PTR_ERR(thermal->cdev);

	return device_create_file(&thermal->cdev->device, &dev_attr_user_vote);
}

static int cooling_init(struct gxp_thermal_manager *thermal, struct device *dev)
{
	int err;
	struct dentry *d;

	d = debugfs_create_dir("cooling", thermal->gxp->d_entry);
	/* don't let debugfs creation failure abort the init procedure */
	if (IS_ERR_OR_NULL(d))
		dev_warn(dev, "failed to create debug fs for cooling");
	thermal->cooling_root = d;

	err = gxp_thermal_cooling_register(thermal, GXP_COOLING_NAME);
	if (err) {
		dev_err(dev, "failed to initialize external cooling\n");
		gxp_thermal_exit(thermal);
		return err;
	}
	return 0;
}

struct gxp_thermal_manager
*gxp_thermal_init(struct gxp_dev *gxp)
{
	struct device *dev = gxp->dev;
	struct gxp_thermal_manager *thermal;
	int err;

	thermal = devres_alloc(devm_gxp_thermal_release, sizeof(*thermal),
				GFP_KERNEL);
	if (!thermal)
		return ERR_PTR(-ENOMEM);

	thermal->gxp = gxp;
	err = cooling_init(thermal, dev);
	if (err) {
		devres_free(thermal);
		return ERR_PTR(err);
	}

	devres_add(dev, thermal);
	return thermal;
}