Blame view

drivers/thermal/of-thermal.c 27.2 KB
4e5e4705b   Eduardo Valentin   thermal: introduc...
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
  /*
   *  of-thermal.c - Generic Thermal Management device tree support.
   *
   *  Copyright (C) 2013 Texas Instruments
   *  Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
   *
   *
   *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   *
   *  This program is free software; you can redistribute it and/or modify
   *  it under the terms of the GNU General Public License as published by
   *  the Free Software Foundation; version 2 of the License.
   *
   *  This program is distributed in the hope that it will be useful, but
   *  WITHOUT ANY WARRANTY; without even the implied warranty of
   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   *  General Public License for more details.
   *
   *  You should have received a copy of the GNU General Public License along
   *  with this program; if not, write to the Free Software Foundation, Inc.,
   *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
   *
   * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   */
  #include <linux/thermal.h>
  #include <linux/slab.h>
  #include <linux/types.h>
  #include <linux/of_device.h>
  #include <linux/of_platform.h>
  #include <linux/err.h>
  #include <linux/export.h>
  #include <linux/string.h>
2251aef64   Eduardo Valentin   thermal: of: impr...
33
  #include <linux/thermal.h>
4e5e4705b   Eduardo Valentin   thermal: introduc...
34
35
36
37
38
39
  
  #include "thermal_core.h"
  
  /***   Private data structures to represent thermal device tree data ***/
  
  /**
4e5e4705b   Eduardo Valentin   thermal: introduc...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
   * struct __thermal_bind_param - a match between trip and cooling device
   * @cooling_device: a pointer to identify the referred cooling device
   * @trip_id: the trip point index
   * @usage: the percentage (from 0 to 100) of cooling contribution
   * @min: minimum cooling state used at this trip point
   * @max: maximum cooling state used at this trip point
   */
  
  struct __thermal_bind_params {
  	struct device_node *cooling_device;
  	unsigned int trip_id;
  	unsigned int usage;
  	unsigned long min;
  	unsigned long max;
  };
  
  /**
   * struct __thermal_zone - internal representation of a thermal zone
   * @mode: current thermal zone device mode (enabled/disabled)
   * @passive_delay: polling interval while passive cooling is activated
   * @polling_delay: zone polling interval
a46dbae8a   Eduardo Valentin   thermal: of-therm...
61
62
   * @slope: slope of the temperature adjustment curve
   * @offset: offset of the temperature adjustment curve
4e5e4705b   Eduardo Valentin   thermal: introduc...
63
64
65
66
67
   * @ntrips: number of trip points
   * @trips: an array of trip points (0..ntrips - 1)
   * @num_tbps: number of thermal bind params
   * @tbps: an array of thermal bind params (0..num_tbps - 1)
   * @sensor_data: sensor private data used while reading temperature and trend
2251aef64   Eduardo Valentin   thermal: of: impr...
68
   * @ops: set of callbacks to handle the thermal zone based on DT
4e5e4705b   Eduardo Valentin   thermal: introduc...
69
70
71
72
73
74
   */
  
  struct __thermal_zone {
  	enum thermal_device_mode mode;
  	int passive_delay;
  	int polling_delay;
a46dbae8a   Eduardo Valentin   thermal: of-therm...
75
76
  	int slope;
  	int offset;
4e5e4705b   Eduardo Valentin   thermal: introduc...
77
78
79
  
  	/* trip data */
  	int ntrips;
ad9914ac3   Lukasz Majewski   thermal: of: Rena...
80
  	struct thermal_trip *trips;
4e5e4705b   Eduardo Valentin   thermal: introduc...
81
82
83
84
85
86
87
  
  	/* cooling binding data */
  	int num_tbps;
  	struct __thermal_bind_params *tbps;
  
  	/* sensor interface */
  	void *sensor_data;
2251aef64   Eduardo Valentin   thermal: of: impr...
88
  	const struct thermal_zone_of_device_ops *ops;
4e5e4705b   Eduardo Valentin   thermal: introduc...
89
90
91
92
93
  };
  
  /***   DT thermal zone device callbacks   ***/
  
  static int of_thermal_get_temp(struct thermal_zone_device *tz,
17e8351a7   Sascha Hauer   thermal: consiste...
94
  			       int *temp)
4e5e4705b   Eduardo Valentin   thermal: introduc...
95
96
  {
  	struct __thermal_zone *data = tz->devdata;
2251aef64   Eduardo Valentin   thermal: of: impr...
97
  	if (!data->ops->get_temp)
4e5e4705b   Eduardo Valentin   thermal: introduc...
98
  		return -EINVAL;
2251aef64   Eduardo Valentin   thermal: of: impr...
99
  	return data->ops->get_temp(data->sensor_data, temp);
4e5e4705b   Eduardo Valentin   thermal: introduc...
100
  }
826386e73   Sascha Hauer   thermal: of: impl...
101
102
103
104
105
106
107
108
109
110
  static int of_thermal_set_trips(struct thermal_zone_device *tz,
  				int low, int high)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (!data->ops || !data->ops->set_trips)
  		return -EINVAL;
  
  	return data->ops->set_trips(data->sensor_data, low, high);
  }
08dab66ec   Lukasz Majewski   thermal: of: Exte...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  /**
   * of_thermal_get_ntrips - function to export number of available trip
   *			   points.
   * @tz: pointer to a thermal zone
   *
   * This function is a globally visible wrapper to get number of trip points
   * stored in the local struct __thermal_zone
   *
   * Return: number of available trip points, -ENODEV when data not available
   */
  int of_thermal_get_ntrips(struct thermal_zone_device *tz)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (!data || IS_ERR(data))
  		return -ENODEV;
  
  	return data->ntrips;
  }
  EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
a9bf2cc49   Lukasz Majewski   thermal: of: Exte...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  /**
   * of_thermal_is_trip_valid - function to check if trip point is valid
   *
   * @tz:	pointer to a thermal zone
   * @trip:	trip point to evaluate
   *
   * This function is responsible for checking if passed trip point is valid
   *
   * Return: true if trip point is valid, false otherwise
   */
  bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (!data || trip >= data->ntrips || trip < 0)
  		return false;
  
  	return true;
  }
  EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
ce8be7785   Lukasz Majewski   thermal: of: Exte...
151
152
153
154
155
156
157
158
159
160
  /**
   * of_thermal_get_trip_points - function to get access to a globally exported
   *				trip points
   *
   * @tz:	pointer to a thermal zone
   *
   * This function provides a pointer to trip points table
   *
   * Return: pointer to trip points table, NULL otherwise
   */
ebc3193ae   Geert Uytterhoeven   thermal: of: Remo...
161
  const struct thermal_trip *
ce8be7785   Lukasz Majewski   thermal: of: Exte...
162
163
164
165
166
167
168
169
170
171
  of_thermal_get_trip_points(struct thermal_zone_device *tz)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (!data)
  		return NULL;
  
  	return data->trips;
  }
  EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
184a4bf62   Lukasz Majewski   thermal: of: Exte...
172
173
174
175
176
177
178
179
180
181
182
183
  /**
   * of_thermal_set_emul_temp - function to set emulated temperature
   *
   * @tz:	pointer to a thermal zone
   * @temp:	temperature to set
   *
   * This function gives the ability to set emulated value of temperature,
   * which is handy for debugging
   *
   * Return: zero on success, error code otherwise
   */
  static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
17e8351a7   Sascha Hauer   thermal: consiste...
184
  				    int temp)
184a4bf62   Lukasz Majewski   thermal: of: Exte...
185
186
  {
  	struct __thermal_zone *data = tz->devdata;
184a4bf62   Lukasz Majewski   thermal: of: Exte...
187
188
  	return data->ops->set_emul_temp(data->sensor_data, temp);
  }
4e5e4705b   Eduardo Valentin   thermal: introduc...
189
190
191
192
  static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
  				enum thermal_trend *trend)
  {
  	struct __thermal_zone *data = tz->devdata;
4e5e4705b   Eduardo Valentin   thermal: introduc...
193

2251aef64   Eduardo Valentin   thermal: of: impr...
194
  	if (!data->ops->get_trend)
4e5e4705b   Eduardo Valentin   thermal: introduc...
195
  		return -EINVAL;
e78eaf459   Sascha Hauer   thermal: streamli...
196
  	return data->ops->get_trend(data->sensor_data, trip, trend);
4e5e4705b   Eduardo Valentin   thermal: introduc...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  }
  
  static int of_thermal_bind(struct thermal_zone_device *thermal,
  			   struct thermal_cooling_device *cdev)
  {
  	struct __thermal_zone *data = thermal->devdata;
  	int i;
  
  	if (!data || IS_ERR(data))
  		return -ENODEV;
  
  	/* find where to bind */
  	for (i = 0; i < data->num_tbps; i++) {
  		struct __thermal_bind_params *tbp = data->tbps + i;
  
  		if (tbp->cooling_device == cdev->np) {
  			int ret;
  
  			ret = thermal_zone_bind_cooling_device(thermal,
  						tbp->trip_id, cdev,
dd354b84d   Punit Agrawal   thermal: Bind coo...
217
  						tbp->max,
6cd9e9f62   Kapileshwar Singh   thermal: of: fix ...
218
219
  						tbp->min,
  						tbp->usage);
4e5e4705b   Eduardo Valentin   thermal: introduc...
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
  			if (ret)
  				return ret;
  		}
  	}
  
  	return 0;
  }
  
  static int of_thermal_unbind(struct thermal_zone_device *thermal,
  			     struct thermal_cooling_device *cdev)
  {
  	struct __thermal_zone *data = thermal->devdata;
  	int i;
  
  	if (!data || IS_ERR(data))
  		return -ENODEV;
  
  	/* find where to unbind */
  	for (i = 0; i < data->num_tbps; i++) {
  		struct __thermal_bind_params *tbp = data->tbps + i;
  
  		if (tbp->cooling_device == cdev->np) {
  			int ret;
  
  			ret = thermal_zone_unbind_cooling_device(thermal,
  						tbp->trip_id, cdev);
  			if (ret)
  				return ret;
  		}
  	}
  
  	return 0;
  }
  
  static int of_thermal_get_mode(struct thermal_zone_device *tz,
  			       enum thermal_device_mode *mode)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	*mode = data->mode;
  
  	return 0;
  }
  
  static int of_thermal_set_mode(struct thermal_zone_device *tz,
  			       enum thermal_device_mode mode)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	mutex_lock(&tz->lock);
083be6fbf   Anson Huang   thermal: of-therm...
270
  	if (mode == THERMAL_DEVICE_ENABLED) {
4e5e4705b   Eduardo Valentin   thermal: introduc...
271
  		tz->polling_delay = data->polling_delay;
083be6fbf   Anson Huang   thermal: of-therm...
272
273
  		tz->passive_delay = data->passive_delay;
  	} else {
4e5e4705b   Eduardo Valentin   thermal: introduc...
274
  		tz->polling_delay = 0;
083be6fbf   Anson Huang   thermal: of-therm...
275
276
  		tz->passive_delay = 0;
  	}
4e5e4705b   Eduardo Valentin   thermal: introduc...
277
278
279
280
  
  	mutex_unlock(&tz->lock);
  
  	data->mode = mode;
0e70f466f   Srinivas Pandruvada   thermal: Enhance ...
281
  	thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
4e5e4705b   Eduardo Valentin   thermal: introduc...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  
  	return 0;
  }
  
  static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
  				    enum thermal_trip_type *type)
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (trip >= data->ntrips || trip < 0)
  		return -EDOM;
  
  	*type = data->trips[trip].type;
  
  	return 0;
  }
  
  static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
17e8351a7   Sascha Hauer   thermal: consiste...
300
  				    int *temp)
4e5e4705b   Eduardo Valentin   thermal: introduc...
301
302
303
304
305
306
307
308
309
310
311
312
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (trip >= data->ntrips || trip < 0)
  		return -EDOM;
  
  	*temp = data->trips[trip].temperature;
  
  	return 0;
  }
  
  static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
17e8351a7   Sascha Hauer   thermal: consiste...
313
  				    int temp)
4e5e4705b   Eduardo Valentin   thermal: introduc...
314
315
316
317
318
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (trip >= data->ntrips || trip < 0)
  		return -EDOM;
c35095215   Wei Ni   thermal: of-therm...
319
320
321
322
323
324
325
  	if (data->ops->set_trip_temp) {
  		int ret;
  
  		ret = data->ops->set_trip_temp(data->sensor_data, trip, temp);
  		if (ret)
  			return ret;
  	}
4e5e4705b   Eduardo Valentin   thermal: introduc...
326
327
328
329
330
331
332
  	/* thermal framework should take care of data->mask & (1 << trip) */
  	data->trips[trip].temperature = temp;
  
  	return 0;
  }
  
  static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
17e8351a7   Sascha Hauer   thermal: consiste...
333
  				    int *hyst)
4e5e4705b   Eduardo Valentin   thermal: introduc...
334
335
336
337
338
339
340
341
342
343
344
345
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (trip >= data->ntrips || trip < 0)
  		return -EDOM;
  
  	*hyst = data->trips[trip].hysteresis;
  
  	return 0;
  }
  
  static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
17e8351a7   Sascha Hauer   thermal: consiste...
346
  				    int hyst)
4e5e4705b   Eduardo Valentin   thermal: introduc...
347
348
349
350
351
352
353
354
355
356
357
358
359
  {
  	struct __thermal_zone *data = tz->devdata;
  
  	if (trip >= data->ntrips || trip < 0)
  		return -EDOM;
  
  	/* thermal framework should take care of data->mask & (1 << trip) */
  	data->trips[trip].hysteresis = hyst;
  
  	return 0;
  }
  
  static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
17e8351a7   Sascha Hauer   thermal: consiste...
360
  				    int *temp)
4e5e4705b   Eduardo Valentin   thermal: introduc...
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  {
  	struct __thermal_zone *data = tz->devdata;
  	int i;
  
  	for (i = 0; i < data->ntrips; i++)
  		if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
  			*temp = data->trips[i].temperature;
  			return 0;
  		}
  
  	return -EINVAL;
  }
  
  static struct thermal_zone_device_ops of_thermal_ops = {
  	.get_mode = of_thermal_get_mode,
  	.set_mode = of_thermal_set_mode,
  
  	.get_trip_type = of_thermal_get_trip_type,
  	.get_trip_temp = of_thermal_get_trip_temp,
  	.set_trip_temp = of_thermal_set_trip_temp,
  	.get_trip_hyst = of_thermal_get_trip_hyst,
  	.set_trip_hyst = of_thermal_set_trip_hyst,
  	.get_crit_temp = of_thermal_get_crit_temp,
  
  	.bind = of_thermal_bind,
  	.unbind = of_thermal_unbind,
  };
  
  /***   sensor API   ***/
  
  static struct thermal_zone_device *
  thermal_zone_of_add_sensor(struct device_node *zone,
  			   struct device_node *sensor, void *data,
2251aef64   Eduardo Valentin   thermal: of: impr...
394
  			   const struct thermal_zone_of_device_ops *ops)
4e5e4705b   Eduardo Valentin   thermal: introduc...
395
396
397
398
399
400
401
402
403
  {
  	struct thermal_zone_device *tzd;
  	struct __thermal_zone *tz;
  
  	tzd = thermal_zone_get_zone_by_name(zone->name);
  	if (IS_ERR(tzd))
  		return ERR_PTR(-EPROBE_DEFER);
  
  	tz = tzd->devdata;
2251aef64   Eduardo Valentin   thermal: of: impr...
404
405
  	if (!ops)
  		return ERR_PTR(-EINVAL);
4e5e4705b   Eduardo Valentin   thermal: introduc...
406
  	mutex_lock(&tzd->lock);
2251aef64   Eduardo Valentin   thermal: of: impr...
407
  	tz->ops = ops;
4e5e4705b   Eduardo Valentin   thermal: introduc...
408
409
410
411
  	tz->sensor_data = data;
  
  	tzd->ops->get_temp = of_thermal_get_temp;
  	tzd->ops->get_trend = of_thermal_get_trend;
826386e73   Sascha Hauer   thermal: of: impl...
412
413
414
415
416
417
418
  
  	/*
  	 * The thermal zone core will calculate the window if they have set the
  	 * optional set_trips pointer.
  	 */
  	if (ops->set_trips)
  		tzd->ops->set_trips = of_thermal_set_trips;
e2fa74880   Keerthy   thermal: of-therm...
419
420
  	if (ops->set_emul_temp)
  		tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
4e5e4705b   Eduardo Valentin   thermal: introduc...
421
422
423
424
425
426
427
428
429
430
431
432
433
  	mutex_unlock(&tzd->lock);
  
  	return tzd;
  }
  
  /**
   * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
   * @dev: a valid struct device pointer of a sensor device. Must contain
   *       a valid .of_node, for the sensor node.
   * @sensor_id: a sensor identifier, in case the sensor IP has more
   *             than one sensors
   * @data: a private pointer (owned by the caller) that will be passed
   *        back, when a temperature reading is needed.
2251aef64   Eduardo Valentin   thermal: of: impr...
434
   * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
4e5e4705b   Eduardo Valentin   thermal: introduc...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
   *
   * This function will search the list of thermal zones described in device
   * tree and look for the zone that refer to the sensor device pointed by
   * @dev->of_node as temperature providers. For the zone pointing to the
   * sensor node, the sensor will be added to the DT thermal zone device.
   *
   * The thermal zone temperature is provided by the @get_temp function
   * pointer. When called, it will have the private pointer @data back.
   *
   * The thermal zone temperature trend is provided by the @get_trend function
   * pointer. When called, it will have the private pointer @data back.
   *
   * TODO:
   * 01 - This function must enqueue the new sensor instead of using
   * it as the only source of temperature values.
   *
   * 02 - There must be a way to match the sensor with all thermal zones
   * that refer to it.
   *
   * Return: On success returns a valid struct thermal_zone_device,
   * otherwise, it returns a corresponding ERR_PTR(). Caller must
   * check the return value with help of IS_ERR() helper.
   */
  struct thermal_zone_device *
2251aef64   Eduardo Valentin   thermal: of: impr...
459
460
  thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
  				const struct thermal_zone_of_device_ops *ops)
4e5e4705b   Eduardo Valentin   thermal: introduc...
461
462
  {
  	struct device_node *np, *child, *sensor_np;
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
463
  	struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
4e5e4705b   Eduardo Valentin   thermal: introduc...
464
465
466
467
  
  	np = of_find_node_by_name(NULL, "thermal-zones");
  	if (!np)
  		return ERR_PTR(-ENODEV);
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
468
469
  	if (!dev || !dev->of_node) {
  		of_node_put(np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
470
  		return ERR_PTR(-EINVAL);
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
471
  	}
4e5e4705b   Eduardo Valentin   thermal: introduc...
472

c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
473
  	sensor_np = of_node_get(dev->of_node);
4e5e4705b   Eduardo Valentin   thermal: introduc...
474

42bbe400f   Laxman Dewangan   thermal: of: use ...
475
  	for_each_available_child_of_node(np, child) {
4e5e4705b   Eduardo Valentin   thermal: introduc...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  		struct of_phandle_args sensor_specs;
  		int ret, id;
  
  		/* For now, thermal framework supports only 1 sensor per zone */
  		ret = of_parse_phandle_with_args(child, "thermal-sensors",
  						 "#thermal-sensor-cells",
  						 0, &sensor_specs);
  		if (ret)
  			continue;
  
  		if (sensor_specs.args_count >= 1) {
  			id = sensor_specs.args[0];
  			WARN(sensor_specs.args_count > 1,
  			     "%s: too many cells in sensor specifier %d
  ",
  			     sensor_specs.np->name, sensor_specs.args_count);
  		} else {
  			id = 0;
  		}
  
  		if (sensor_specs.np == sensor_np && id == sensor_id) {
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
497
  			tzd = thermal_zone_of_add_sensor(child, sensor_np,
2251aef64   Eduardo Valentin   thermal: of: impr...
498
  							 data, ops);
528012c1f   Lukasz Majewski   thermal: of: Enab...
499
500
  			if (!IS_ERR(tzd))
  				tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
501
502
503
  			of_node_put(sensor_specs.np);
  			of_node_put(child);
  			goto exit;
4e5e4705b   Eduardo Valentin   thermal: introduc...
504
  		}
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
505
  		of_node_put(sensor_specs.np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
506
  	}
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
507
508
  exit:
  	of_node_put(sensor_np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
509
  	of_node_put(np);
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
510
  	return tzd;
4e5e4705b   Eduardo Valentin   thermal: introduc...
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
  }
  EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
  
  /**
   * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
   * @dev: a valid struct device pointer of a sensor device. Must contain
   *       a valid .of_node, for the sensor node.
   * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
   *
   * This function removes the sensor callbacks and private data from the
   * thermal zone device registered with thermal_zone_of_sensor_register()
   * API. It will also silent the zone by remove the .get_temp() and .get_trend()
   * thermal zone device callbacks.
   *
   * TODO: When the support to several sensors per zone is added, this
   * function must search the sensor list based on @dev parameter.
   *
   */
  void thermal_zone_of_sensor_unregister(struct device *dev,
  				       struct thermal_zone_device *tzd)
  {
  	struct __thermal_zone *tz;
  
  	if (!dev || !tzd || !tzd->devdata)
  		return;
  
  	tz = tzd->devdata;
  
  	/* no __thermal_zone, nothing to be done */
  	if (!tz)
  		return;
  
  	mutex_lock(&tzd->lock);
  	tzd->ops->get_temp = NULL;
  	tzd->ops->get_trend = NULL;
184a4bf62   Lukasz Majewski   thermal: of: Exte...
546
  	tzd->ops->set_emul_temp = NULL;
4e5e4705b   Eduardo Valentin   thermal: introduc...
547

2251aef64   Eduardo Valentin   thermal: of: impr...
548
  	tz->ops = NULL;
4e5e4705b   Eduardo Valentin   thermal: introduc...
549
550
551
552
  	tz->sensor_data = NULL;
  	mutex_unlock(&tzd->lock);
  }
  EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
e498b4984   Laxman Dewangan   thermal: of-therm...
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
  static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
  {
  	thermal_zone_of_sensor_unregister(dev,
  					  *(struct thermal_zone_device **)res);
  }
  
  static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
  					     void *data)
  {
  	struct thermal_zone_device **r = res;
  
  	if (WARN_ON(!r || !*r))
  		return 0;
  
  	return *r == data;
  }
  
  /**
   * devm_thermal_zone_of_sensor_register - Resource managed version of
   *				thermal_zone_of_sensor_register()
   * @dev: a valid struct device pointer of a sensor device. Must contain
   *       a valid .of_node, for the sensor node.
   * @sensor_id: a sensor identifier, in case the sensor IP has more
   *	       than one sensors
   * @data: a private pointer (owned by the caller) that will be passed
   *	  back, when a temperature reading is needed.
   * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
   *
   * Refer thermal_zone_of_sensor_register() for more details.
   *
   * Return: On success returns a valid struct thermal_zone_device,
   * otherwise, it returns a corresponding ERR_PTR(). Caller must
   * check the return value with help of IS_ERR() helper.
7b5c4a0cd   Zhang Rui   Thermal: of therm...
586
   * Registered thermal_zone_device device will automatically be
e498b4984   Laxman Dewangan   thermal: of-therm...
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
   * released when device is unbounded.
   */
  struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
  	struct device *dev, int sensor_id,
  	void *data, const struct thermal_zone_of_device_ops *ops)
  {
  	struct thermal_zone_device **ptr, *tzd;
  
  	ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
  			   GFP_KERNEL);
  	if (!ptr)
  		return ERR_PTR(-ENOMEM);
  
  	tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
  	if (IS_ERR(tzd)) {
  		devres_free(ptr);
  		return tzd;
  	}
  
  	*ptr = tzd;
  	devres_add(dev, ptr);
  
  	return tzd;
  }
  EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
  
  /**
   * devm_thermal_zone_of_sensor_unregister - Resource managed version of
   *				thermal_zone_of_sensor_unregister().
   * @dev: Device for which which resource was allocated.
   * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
   *
   * This function removes the sensor callbacks and private data from the
   * thermal zone device registered with devm_thermal_zone_of_sensor_register()
   * API. It will also silent the zone by remove the .get_temp() and .get_trend()
   * thermal zone device callbacks.
   * Normally this function will not need to be called and the resource
   * management code will ensure that the resource is freed.
   */
  void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  					    struct thermal_zone_device *tzd)
  {
  	WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
  			       devm_thermal_zone_of_sensor_match, tzd));
  }
  EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
4e5e4705b   Eduardo Valentin   thermal: introduc...
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
  /***   functions parsing device tree nodes   ***/
  
  /**
   * thermal_of_populate_bind_params - parse and fill cooling map data
   * @np: DT node containing a cooling-map node
   * @__tbp: data structure to be filled with cooling map info
   * @trips: array of thermal zone trip points
   * @ntrips: number of trip points inside trips.
   *
   * This function parses a cooling-map type of node represented by
   * @np parameter and fills the read data into @__tbp data structure.
   * It needs the already parsed array of trip points of the thermal zone
   * in consideration.
   *
   * Return: 0 on success, proper error code otherwise
   */
  static int thermal_of_populate_bind_params(struct device_node *np,
  					   struct __thermal_bind_params *__tbp,
ad9914ac3   Lukasz Majewski   thermal: of: Rena...
651
  					   struct thermal_trip *trips,
4e5e4705b   Eduardo Valentin   thermal: introduc...
652
653
654
655
656
657
658
659
  					   int ntrips)
  {
  	struct of_phandle_args cooling_spec;
  	struct device_node *trip;
  	int ret, i;
  	u32 prop;
  
  	/* Default weight. Usage is optional */
6cd9e9f62   Kapileshwar Singh   thermal: of: fix ...
660
  	__tbp->usage = THERMAL_WEIGHT_DEFAULT;
4e5e4705b   Eduardo Valentin   thermal: introduc...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  	ret = of_property_read_u32(np, "contribution", &prop);
  	if (ret == 0)
  		__tbp->usage = prop;
  
  	trip = of_parse_phandle(np, "trip", 0);
  	if (!trip) {
  		pr_err("missing trip property
  ");
  		return -ENODEV;
  	}
  
  	/* match using device_node */
  	for (i = 0; i < ntrips; i++)
  		if (trip == trips[i].np) {
  			__tbp->trip_id = i;
  			break;
  		}
  
  	if (i == ntrips) {
  		ret = -ENODEV;
  		goto end;
  	}
  
  	ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
  					 0, &cooling_spec);
  	if (ret < 0) {
  		pr_err("missing cooling_device property
  ");
  		goto end;
  	}
  	__tbp->cooling_device = cooling_spec.np;
  	if (cooling_spec.args_count >= 2) { /* at least min and max */
  		__tbp->min = cooling_spec.args[0];
  		__tbp->max = cooling_spec.args[1];
  	} else {
  		pr_err("wrong reference to cooling device, missing limits
  ");
  	}
  
  end:
  	of_node_put(trip);
  
  	return ret;
  }
  
  /**
   * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
   * into the device tree binding of 'trip', property type.
   */
  static const char * const trip_types[] = {
  	[THERMAL_TRIP_ACTIVE]	= "active",
  	[THERMAL_TRIP_PASSIVE]	= "passive",
  	[THERMAL_TRIP_HOT]	= "hot",
  	[THERMAL_TRIP_CRITICAL]	= "critical",
  };
  
  /**
   * thermal_of_get_trip_type - Get phy mode for given device_node
   * @np:	Pointer to the given device_node
   * @type: Pointer to resulting trip type
   *
   * The function gets trip type string from property 'type',
   * and store its index in trip_types table in @type,
   *
   * Return: 0 on success, or errno in error case.
   */
  static int thermal_of_get_trip_type(struct device_node *np,
  				    enum thermal_trip_type *type)
  {
  	const char *t;
  	int err, i;
  
  	err = of_property_read_string(np, "type", &t);
  	if (err < 0)
  		return err;
  
  	for (i = 0; i < ARRAY_SIZE(trip_types); i++)
  		if (!strcasecmp(t, trip_types[i])) {
  			*type = i;
  			return 0;
  		}
  
  	return -ENODEV;
  }
  
  /**
   * thermal_of_populate_trip - parse and fill one trip point data
   * @np: DT node containing a trip point node
   * @trip: trip point data structure to be filled up
   *
   * This function parses a trip point type of node represented by
   * @np parameter and fills the read data into @trip data structure.
   *
   * Return: 0 on success, proper error code otherwise
   */
  static int thermal_of_populate_trip(struct device_node *np,
ad9914ac3   Lukasz Majewski   thermal: of: Rena...
757
  				    struct thermal_trip *trip)
4e5e4705b   Eduardo Valentin   thermal: introduc...
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
  {
  	int prop;
  	int ret;
  
  	ret = of_property_read_u32(np, "temperature", &prop);
  	if (ret < 0) {
  		pr_err("missing temperature property
  ");
  		return ret;
  	}
  	trip->temperature = prop;
  
  	ret = of_property_read_u32(np, "hysteresis", &prop);
  	if (ret < 0) {
  		pr_err("missing hysteresis property
  ");
  		return ret;
  	}
  	trip->hysteresis = prop;
  
  	ret = thermal_of_get_trip_type(np, &trip->type);
  	if (ret < 0) {
  		pr_err("wrong trip type property
  ");
  		return ret;
  	}
  
  	/* Required for cooling map matching */
  	trip->np = np;
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
787
  	of_node_get(np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
788
789
790
791
792
793
794
795
796
797
798
799
  
  	return 0;
  }
  
  /**
   * thermal_of_build_thermal_zone - parse and fill one thermal zone data
   * @np: DT node containing a thermal zone node
   *
   * This function parses a thermal zone type of node represented by
   * @np parameter and fills the read data into a __thermal_zone data structure
   * and return this pointer.
   *
a46dbae8a   Eduardo Valentin   thermal: of-therm...
800
   * TODO: Missing properties to parse: thermal-sensor-names
4e5e4705b   Eduardo Valentin   thermal: introduc...
801
802
803
804
805
   *
   * Return: On success returns a valid struct __thermal_zone,
   * otherwise, it returns a corresponding ERR_PTR(). Caller must
   * check the return value with help of IS_ERR() helper.
   */
c0ff8aaae   Julia Lawall   thermal: of: add ...
806
807
  static struct __thermal_zone
  __init *thermal_of_build_thermal_zone(struct device_node *np)
4e5e4705b   Eduardo Valentin   thermal: introduc...
808
809
810
811
  {
  	struct device_node *child = NULL, *gchild;
  	struct __thermal_zone *tz;
  	int ret, i;
a46dbae8a   Eduardo Valentin   thermal: of-therm...
812
  	u32 prop, coef[2];
4e5e4705b   Eduardo Valentin   thermal: introduc...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
  
  	if (!np) {
  		pr_err("no thermal zone np
  ");
  		return ERR_PTR(-EINVAL);
  	}
  
  	tz = kzalloc(sizeof(*tz), GFP_KERNEL);
  	if (!tz)
  		return ERR_PTR(-ENOMEM);
  
  	ret = of_property_read_u32(np, "polling-delay-passive", &prop);
  	if (ret < 0) {
  		pr_err("missing polling-delay-passive property
  ");
  		goto free_tz;
  	}
  	tz->passive_delay = prop;
  
  	ret = of_property_read_u32(np, "polling-delay", &prop);
  	if (ret < 0) {
  		pr_err("missing polling-delay property
  ");
  		goto free_tz;
  	}
  	tz->polling_delay = prop;
a46dbae8a   Eduardo Valentin   thermal: of-therm...
839
840
841
842
843
844
845
846
847
848
849
850
851
  	/*
  	 * REVIST: for now, the thermal framework supports only
  	 * one sensor per thermal zone. Thus, we are considering
  	 * only the first two values as slope and offset.
  	 */
  	ret = of_property_read_u32_array(np, "coefficients", coef, 2);
  	if (ret == 0) {
  		tz->slope = coef[0];
  		tz->offset = coef[1];
  	} else {
  		tz->slope = 1;
  		tz->offset = 0;
  	}
4e5e4705b   Eduardo Valentin   thermal: introduc...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
  	/* trips */
  	child = of_get_child_by_name(np, "trips");
  
  	/* No trips provided */
  	if (!child)
  		goto finish;
  
  	tz->ntrips = of_get_child_count(child);
  	if (tz->ntrips == 0) /* must have at least one child */
  		goto finish;
  
  	tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
  	if (!tz->trips) {
  		ret = -ENOMEM;
  		goto free_tz;
  	}
  
  	i = 0;
  	for_each_child_of_node(child, gchild) {
  		ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
  		if (ret)
  			goto free_trips;
  	}
  
  	of_node_put(child);
  
  	/* cooling-maps */
  	child = of_get_child_by_name(np, "cooling-maps");
  
  	/* cooling-maps not provided */
  	if (!child)
  		goto finish;
  
  	tz->num_tbps = of_get_child_count(child);
  	if (tz->num_tbps == 0)
  		goto finish;
  
  	tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
  	if (!tz->tbps) {
  		ret = -ENOMEM;
  		goto free_trips;
  	}
  
  	i = 0;
ca9521b77   Stephen Boyd   thermal: Add brac...
896
  	for_each_child_of_node(child, gchild) {
4e5e4705b   Eduardo Valentin   thermal: introduc...
897
898
899
900
  		ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
  						      tz->trips, tz->ntrips);
  		if (ret)
  			goto free_tbps;
ca9521b77   Stephen Boyd   thermal: Add brac...
901
  	}
4e5e4705b   Eduardo Valentin   thermal: introduc...
902
903
904
905
906
907
908
909
  
  finish:
  	of_node_put(child);
  	tz->mode = THERMAL_DEVICE_DISABLED;
  
  	return tz;
  
  free_tbps:
1cd91c182   Ulises Brindis   thermal: of: fix ...
910
  	for (i = i - 1; i >= 0; i--)
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
911
  		of_node_put(tz->tbps[i].cooling_device);
4e5e4705b   Eduardo Valentin   thermal: introduc...
912
913
  	kfree(tz->tbps);
  free_trips:
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
914
915
  	for (i = 0; i < tz->ntrips; i++)
  		of_node_put(tz->trips[i].np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
916
  	kfree(tz->trips);
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
917
  	of_node_put(gchild);
4e5e4705b   Eduardo Valentin   thermal: introduc...
918
919
920
921
922
923
924
925
926
  free_tz:
  	kfree(tz);
  	of_node_put(child);
  
  	return ERR_PTR(ret);
  }
  
  static inline void of_thermal_free_zone(struct __thermal_zone *tz)
  {
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
927
928
929
930
  	int i;
  
  	for (i = 0; i < tz->num_tbps; i++)
  		of_node_put(tz->tbps[i].cooling_device);
4e5e4705b   Eduardo Valentin   thermal: introduc...
931
  	kfree(tz->tbps);
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
932
933
  	for (i = 0; i < tz->ntrips; i++)
  		of_node_put(tz->trips[i].np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
  	kfree(tz->trips);
  	kfree(tz);
  }
  
  /**
   * of_parse_thermal_zones - parse device tree thermal data
   *
   * Initialization function that can be called by machine initialization
   * code to parse thermal data and populate the thermal framework
   * with hardware thermal zones info. This function only parses thermal zones.
   * Cooling devices and sensor devices nodes are supposed to be parsed
   * by their respective drivers.
   *
   * Return: 0 on success, proper error code otherwise
   *
   */
  int __init of_parse_thermal_zones(void)
  {
  	struct device_node *np, *child;
  	struct __thermal_zone *tz;
  	struct thermal_zone_device_ops *ops;
  
  	np = of_find_node_by_name(NULL, "thermal-zones");
  	if (!np) {
  		pr_debug("unable to find thermal zones
  ");
  		return 0; /* Run successfully on systems without thermal DT */
  	}
42bbe400f   Laxman Dewangan   thermal: of: use ...
962
  	for_each_available_child_of_node(np, child) {
4e5e4705b   Eduardo Valentin   thermal: introduc...
963
964
  		struct thermal_zone_device *zone;
  		struct thermal_zone_params *tzp;
76af5495a   Punit Agrawal   thermal: Default ...
965
  		int i, mask = 0;
647f99255   Punit Agrawal   of: thermal: Intr...
966
  		u32 prop;
4e5e4705b   Eduardo Valentin   thermal: introduc...
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
  
  		tz = thermal_of_build_thermal_zone(child);
  		if (IS_ERR(tz)) {
  			pr_err("failed to build thermal zone %s: %ld
  ",
  			       child->name,
  			       PTR_ERR(tz));
  			continue;
  		}
  
  		ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
  		if (!ops)
  			goto exit_free;
  
  		tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
  		if (!tzp) {
  			kfree(ops);
  			goto exit_free;
  		}
  
  		/* No hwmon because there might be hwmon drivers registering */
  		tzp->no_hwmon = true;
647f99255   Punit Agrawal   of: thermal: Intr...
989
990
  		if (!of_property_read_u32(child, "sustainable-power", &prop))
  			tzp->sustainable_power = prop;
76af5495a   Punit Agrawal   thermal: Default ...
991
992
  		for (i = 0; i < tz->ntrips; i++)
  			mask |= 1 << i;
a46dbae8a   Eduardo Valentin   thermal: of-therm...
993
994
995
  		/* these two are left for temperature drivers to use */
  		tzp->slope = tz->slope;
  		tzp->offset = tz->offset;
4e5e4705b   Eduardo Valentin   thermal: introduc...
996
  		zone = thermal_zone_device_register(child->name, tz->ntrips,
76af5495a   Punit Agrawal   thermal: Default ...
997
  						    mask, tz,
4e5e4705b   Eduardo Valentin   thermal: introduc...
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
  						    ops, tzp,
  						    tz->passive_delay,
  						    tz->polling_delay);
  		if (IS_ERR(zone)) {
  			pr_err("Failed to build %s zone %ld
  ", child->name,
  			       PTR_ERR(zone));
  			kfree(tzp);
  			kfree(ops);
  			of_thermal_free_zone(tz);
  			/* attempting to build remaining zones still */
  		}
  	}
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
1011
  	of_node_put(np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
1012
1013
1014
1015
  
  	return 0;
  
  exit_free:
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
1016
1017
  	of_node_put(child);
  	of_node_put(np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  	of_thermal_free_zone(tz);
  
  	/* no memory available, so free what we have built */
  	of_thermal_destroy_zones();
  
  	return -ENOMEM;
  }
  
  /**
   * of_thermal_destroy_zones - remove all zones parsed and allocated resources
   *
   * Finds all zones parsed and added to the thermal framework and remove them
   * from the system, together with their resources.
   *
   */
  void of_thermal_destroy_zones(void)
  {
  	struct device_node *np, *child;
  
  	np = of_find_node_by_name(NULL, "thermal-zones");
  	if (!np) {
285249884   Jiada Wang   thermal: of-therm...
1039
1040
  		pr_debug("unable to find thermal zones
  ");
4e5e4705b   Eduardo Valentin   thermal: introduc...
1041
1042
  		return;
  	}
42bbe400f   Laxman Dewangan   thermal: of: use ...
1043
  	for_each_available_child_of_node(np, child) {
4e5e4705b   Eduardo Valentin   thermal: introduc...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  		struct thermal_zone_device *zone;
  
  		zone = thermal_zone_get_zone_by_name(child->name);
  		if (IS_ERR(zone))
  			continue;
  
  		thermal_zone_device_unregister(zone);
  		kfree(zone->tzp);
  		kfree(zone->ops);
  		of_thermal_free_zone(zone->devdata);
  	}
c2aad93c7   Vladimir Zapolskiy   thermal: fix mult...
1055
  	of_node_put(np);
4e5e4705b   Eduardo Valentin   thermal: introduc...
1056
  }