of-thermal.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * of-thermal.c - Generic Thermal Management device tree support.
  3. *
  4. * Copyright (C) 2013 Texas Instruments
  5. * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
  6. *
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/thermal.h>
  26. #include <linux/slab.h>
  27. #include <linux/types.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/err.h>
  31. #include <linux/export.h>
  32. #include <linux/string.h>
  33. #include <linux/thermal.h>
  34. #include "thermal_core.h"
  35. /*** Private data structures to represent thermal device tree data ***/
  36. /**
  37. * struct __thermal_bind_param - a match between trip and cooling device
  38. * @cooling_device: a pointer to identify the referred cooling device
  39. * @trip_id: the trip point index
  40. * @usage: the percentage (from 0 to 100) of cooling contribution
  41. * @min: minimum cooling state used at this trip point
  42. * @max: maximum cooling state used at this trip point
  43. */
  44. struct __thermal_bind_params {
  45. struct device_node *cooling_device;
  46. unsigned int trip_id;
  47. unsigned int usage;
  48. unsigned long min;
  49. unsigned long max;
  50. };
  51. /**
  52. * struct __thermal_zone - internal representation of a thermal zone
  53. * @mode: current thermal zone device mode (enabled/disabled)
  54. * @passive_delay: polling interval while passive cooling is activated
  55. * @polling_delay: zone polling interval
  56. * @slope: slope of the temperature adjustment curve
  57. * @offset: offset of the temperature adjustment curve
  58. * @ntrips: number of trip points
  59. * @trips: an array of trip points (0..ntrips - 1)
  60. * @num_tbps: number of thermal bind params
  61. * @tbps: an array of thermal bind params (0..num_tbps - 1)
  62. * @sensor_data: sensor private data used while reading temperature and trend
  63. * @ops: set of callbacks to handle the thermal zone based on DT
  64. */
  65. struct __thermal_zone {
  66. enum thermal_device_mode mode;
  67. int passive_delay;
  68. int polling_delay;
  69. int slope;
  70. int offset;
  71. /* trip data */
  72. int ntrips;
  73. struct thermal_trip *trips;
  74. /* cooling binding data */
  75. int num_tbps;
  76. struct __thermal_bind_params *tbps;
  77. /* sensor interface */
  78. void *sensor_data;
  79. const struct thermal_zone_of_device_ops *ops;
  80. };
  81. /*** DT thermal zone device callbacks ***/
  82. static int of_thermal_get_temp(struct thermal_zone_device *tz,
  83. int *temp)
  84. {
  85. struct __thermal_zone *data = tz->devdata;
  86. if (!data->ops->get_temp)
  87. return -EINVAL;
  88. return data->ops->get_temp(data->sensor_data, temp);
  89. }
  90. /**
  91. * of_thermal_get_ntrips - function to export number of available trip
  92. * points.
  93. * @tz: pointer to a thermal zone
  94. *
  95. * This function is a globally visible wrapper to get number of trip points
  96. * stored in the local struct __thermal_zone
  97. *
  98. * Return: number of available trip points, -ENODEV when data not available
  99. */
  100. int of_thermal_get_ntrips(struct thermal_zone_device *tz)
  101. {
  102. struct __thermal_zone *data = tz->devdata;
  103. if (!data || IS_ERR(data))
  104. return -ENODEV;
  105. return data->ntrips;
  106. }
  107. EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
  108. /**
  109. * of_thermal_is_trip_valid - function to check if trip point is valid
  110. *
  111. * @tz: pointer to a thermal zone
  112. * @trip: trip point to evaluate
  113. *
  114. * This function is responsible for checking if passed trip point is valid
  115. *
  116. * Return: true if trip point is valid, false otherwise
  117. */
  118. bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
  119. {
  120. struct __thermal_zone *data = tz->devdata;
  121. if (!data || trip >= data->ntrips || trip < 0)
  122. return false;
  123. return true;
  124. }
  125. EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
  126. /**
  127. * of_thermal_get_trip_points - function to get access to a globally exported
  128. * trip points
  129. *
  130. * @tz: pointer to a thermal zone
  131. *
  132. * This function provides a pointer to trip points table
  133. *
  134. * Return: pointer to trip points table, NULL otherwise
  135. */
  136. const struct thermal_trip *
  137. of_thermal_get_trip_points(struct thermal_zone_device *tz)
  138. {
  139. struct __thermal_zone *data = tz->devdata;
  140. if (!data)
  141. return NULL;
  142. return data->trips;
  143. }
  144. EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
  145. /**
  146. * of_thermal_set_emul_temp - function to set emulated temperature
  147. *
  148. * @tz: pointer to a thermal zone
  149. * @temp: temperature to set
  150. *
  151. * This function gives the ability to set emulated value of temperature,
  152. * which is handy for debugging
  153. *
  154. * Return: zero on success, error code otherwise
  155. */
  156. static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
  157. int temp)
  158. {
  159. struct __thermal_zone *data = tz->devdata;
  160. if (!data->ops || !data->ops->set_emul_temp)
  161. return -EINVAL;
  162. return data->ops->set_emul_temp(data->sensor_data, temp);
  163. }
  164. static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
  165. enum thermal_trend *trend)
  166. {
  167. struct __thermal_zone *data = tz->devdata;
  168. long dev_trend;
  169. int r;
  170. if (!data->ops->get_trend)
  171. return -EINVAL;
  172. r = data->ops->get_trend(data->sensor_data, &dev_trend);
  173. if (r)
  174. return r;
  175. /* TODO: These intervals might have some thresholds, but in core code */
  176. if (dev_trend > 0)
  177. *trend = THERMAL_TREND_RAISING;
  178. else if (dev_trend < 0)
  179. *trend = THERMAL_TREND_DROPPING;
  180. else
  181. *trend = THERMAL_TREND_STABLE;
  182. return 0;
  183. }
  184. static int of_thermal_bind(struct thermal_zone_device *thermal,
  185. struct thermal_cooling_device *cdev)
  186. {
  187. struct __thermal_zone *data = thermal->devdata;
  188. int i;
  189. if (!data || IS_ERR(data))
  190. return -ENODEV;
  191. /* find where to bind */
  192. for (i = 0; i < data->num_tbps; i++) {
  193. struct __thermal_bind_params *tbp = data->tbps + i;
  194. if (tbp->cooling_device == cdev->np) {
  195. int ret;
  196. ret = thermal_zone_bind_cooling_device(thermal,
  197. tbp->trip_id, cdev,
  198. tbp->max,
  199. tbp->min,
  200. tbp->usage);
  201. if (ret)
  202. return ret;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int of_thermal_unbind(struct thermal_zone_device *thermal,
  208. struct thermal_cooling_device *cdev)
  209. {
  210. struct __thermal_zone *data = thermal->devdata;
  211. int i;
  212. if (!data || IS_ERR(data))
  213. return -ENODEV;
  214. /* find where to unbind */
  215. for (i = 0; i < data->num_tbps; i++) {
  216. struct __thermal_bind_params *tbp = data->tbps + i;
  217. if (tbp->cooling_device == cdev->np) {
  218. int ret;
  219. ret = thermal_zone_unbind_cooling_device(thermal,
  220. tbp->trip_id, cdev);
  221. if (ret)
  222. return ret;
  223. }
  224. }
  225. return 0;
  226. }
  227. static int of_thermal_get_mode(struct thermal_zone_device *tz,
  228. enum thermal_device_mode *mode)
  229. {
  230. struct __thermal_zone *data = tz->devdata;
  231. *mode = data->mode;
  232. return 0;
  233. }
  234. static int of_thermal_set_mode(struct thermal_zone_device *tz,
  235. enum thermal_device_mode mode)
  236. {
  237. struct __thermal_zone *data = tz->devdata;
  238. mutex_lock(&tz->lock);
  239. if (mode == THERMAL_DEVICE_ENABLED)
  240. tz->polling_delay = data->polling_delay;
  241. else
  242. tz->polling_delay = 0;
  243. mutex_unlock(&tz->lock);
  244. data->mode = mode;
  245. thermal_zone_device_update(tz);
  246. return 0;
  247. }
  248. static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
  249. enum thermal_trip_type *type)
  250. {
  251. struct __thermal_zone *data = tz->devdata;
  252. if (trip >= data->ntrips || trip < 0)
  253. return -EDOM;
  254. *type = data->trips[trip].type;
  255. return 0;
  256. }
  257. static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
  258. int *temp)
  259. {
  260. struct __thermal_zone *data = tz->devdata;
  261. if (trip >= data->ntrips || trip < 0)
  262. return -EDOM;
  263. *temp = data->trips[trip].temperature;
  264. return 0;
  265. }
  266. static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
  267. int temp)
  268. {
  269. struct __thermal_zone *data = tz->devdata;
  270. if (trip >= data->ntrips || trip < 0)
  271. return -EDOM;
  272. /* thermal framework should take care of data->mask & (1 << trip) */
  273. data->trips[trip].temperature = temp;
  274. return 0;
  275. }
  276. static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
  277. int *hyst)
  278. {
  279. struct __thermal_zone *data = tz->devdata;
  280. if (trip >= data->ntrips || trip < 0)
  281. return -EDOM;
  282. *hyst = data->trips[trip].hysteresis;
  283. return 0;
  284. }
  285. static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
  286. int hyst)
  287. {
  288. struct __thermal_zone *data = tz->devdata;
  289. if (trip >= data->ntrips || trip < 0)
  290. return -EDOM;
  291. /* thermal framework should take care of data->mask & (1 << trip) */
  292. data->trips[trip].hysteresis = hyst;
  293. return 0;
  294. }
  295. static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
  296. int *temp)
  297. {
  298. struct __thermal_zone *data = tz->devdata;
  299. int i;
  300. for (i = 0; i < data->ntrips; i++)
  301. if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
  302. *temp = data->trips[i].temperature;
  303. return 0;
  304. }
  305. return -EINVAL;
  306. }
  307. static struct thermal_zone_device_ops of_thermal_ops = {
  308. .get_mode = of_thermal_get_mode,
  309. .set_mode = of_thermal_set_mode,
  310. .get_trip_type = of_thermal_get_trip_type,
  311. .get_trip_temp = of_thermal_get_trip_temp,
  312. .set_trip_temp = of_thermal_set_trip_temp,
  313. .get_trip_hyst = of_thermal_get_trip_hyst,
  314. .set_trip_hyst = of_thermal_set_trip_hyst,
  315. .get_crit_temp = of_thermal_get_crit_temp,
  316. .bind = of_thermal_bind,
  317. .unbind = of_thermal_unbind,
  318. };
  319. /*** sensor API ***/
  320. static struct thermal_zone_device *
  321. thermal_zone_of_add_sensor(struct device_node *zone,
  322. struct device_node *sensor, void *data,
  323. const struct thermal_zone_of_device_ops *ops)
  324. {
  325. struct thermal_zone_device *tzd;
  326. struct __thermal_zone *tz;
  327. tzd = thermal_zone_get_zone_by_name(zone->name);
  328. if (IS_ERR(tzd))
  329. return ERR_PTR(-EPROBE_DEFER);
  330. tz = tzd->devdata;
  331. if (!ops)
  332. return ERR_PTR(-EINVAL);
  333. mutex_lock(&tzd->lock);
  334. tz->ops = ops;
  335. tz->sensor_data = data;
  336. tzd->ops->get_temp = of_thermal_get_temp;
  337. tzd->ops->get_trend = of_thermal_get_trend;
  338. tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
  339. mutex_unlock(&tzd->lock);
  340. return tzd;
  341. }
  342. /**
  343. * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
  344. * @dev: a valid struct device pointer of a sensor device. Must contain
  345. * a valid .of_node, for the sensor node.
  346. * @sensor_id: a sensor identifier, in case the sensor IP has more
  347. * than one sensors
  348. * @data: a private pointer (owned by the caller) that will be passed
  349. * back, when a temperature reading is needed.
  350. * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
  351. *
  352. * This function will search the list of thermal zones described in device
  353. * tree and look for the zone that refer to the sensor device pointed by
  354. * @dev->of_node as temperature providers. For the zone pointing to the
  355. * sensor node, the sensor will be added to the DT thermal zone device.
  356. *
  357. * The thermal zone temperature is provided by the @get_temp function
  358. * pointer. When called, it will have the private pointer @data back.
  359. *
  360. * The thermal zone temperature trend is provided by the @get_trend function
  361. * pointer. When called, it will have the private pointer @data back.
  362. *
  363. * TODO:
  364. * 01 - This function must enqueue the new sensor instead of using
  365. * it as the only source of temperature values.
  366. *
  367. * 02 - There must be a way to match the sensor with all thermal zones
  368. * that refer to it.
  369. *
  370. * Return: On success returns a valid struct thermal_zone_device,
  371. * otherwise, it returns a corresponding ERR_PTR(). Caller must
  372. * check the return value with help of IS_ERR() helper.
  373. */
  374. struct thermal_zone_device *
  375. thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
  376. const struct thermal_zone_of_device_ops *ops)
  377. {
  378. struct device_node *np, *child, *sensor_np;
  379. struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
  380. np = of_find_node_by_name(NULL, "thermal-zones");
  381. if (!np)
  382. return ERR_PTR(-ENODEV);
  383. if (!dev || !dev->of_node) {
  384. of_node_put(np);
  385. return ERR_PTR(-EINVAL);
  386. }
  387. sensor_np = of_node_get(dev->of_node);
  388. for_each_available_child_of_node(np, child) {
  389. struct of_phandle_args sensor_specs;
  390. int ret, id;
  391. /* For now, thermal framework supports only 1 sensor per zone */
  392. ret = of_parse_phandle_with_args(child, "thermal-sensors",
  393. "#thermal-sensor-cells",
  394. 0, &sensor_specs);
  395. if (ret)
  396. continue;
  397. if (sensor_specs.args_count >= 1) {
  398. id = sensor_specs.args[0];
  399. WARN(sensor_specs.args_count > 1,
  400. "%s: too many cells in sensor specifier %d\n",
  401. sensor_specs.np->name, sensor_specs.args_count);
  402. } else {
  403. id = 0;
  404. }
  405. if (sensor_specs.np == sensor_np && id == sensor_id) {
  406. tzd = thermal_zone_of_add_sensor(child, sensor_np,
  407. data, ops);
  408. if (!IS_ERR(tzd))
  409. tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
  410. of_node_put(sensor_specs.np);
  411. of_node_put(child);
  412. goto exit;
  413. }
  414. of_node_put(sensor_specs.np);
  415. }
  416. exit:
  417. of_node_put(sensor_np);
  418. of_node_put(np);
  419. return tzd;
  420. }
  421. EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
  422. /**
  423. * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
  424. * @dev: a valid struct device pointer of a sensor device. Must contain
  425. * a valid .of_node, for the sensor node.
  426. * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
  427. *
  428. * This function removes the sensor callbacks and private data from the
  429. * thermal zone device registered with thermal_zone_of_sensor_register()
  430. * API. It will also silent the zone by remove the .get_temp() and .get_trend()
  431. * thermal zone device callbacks.
  432. *
  433. * TODO: When the support to several sensors per zone is added, this
  434. * function must search the sensor list based on @dev parameter.
  435. *
  436. */
  437. void thermal_zone_of_sensor_unregister(struct device *dev,
  438. struct thermal_zone_device *tzd)
  439. {
  440. struct __thermal_zone *tz;
  441. if (!dev || !tzd || !tzd->devdata)
  442. return;
  443. tz = tzd->devdata;
  444. /* no __thermal_zone, nothing to be done */
  445. if (!tz)
  446. return;
  447. mutex_lock(&tzd->lock);
  448. tzd->ops->get_temp = NULL;
  449. tzd->ops->get_trend = NULL;
  450. tzd->ops->set_emul_temp = NULL;
  451. tz->ops = NULL;
  452. tz->sensor_data = NULL;
  453. mutex_unlock(&tzd->lock);
  454. }
  455. EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
  456. static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
  457. {
  458. thermal_zone_of_sensor_unregister(dev,
  459. *(struct thermal_zone_device **)res);
  460. }
  461. static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
  462. void *data)
  463. {
  464. struct thermal_zone_device **r = res;
  465. if (WARN_ON(!r || !*r))
  466. return 0;
  467. return *r == data;
  468. }
  469. /**
  470. * devm_thermal_zone_of_sensor_register - Resource managed version of
  471. * thermal_zone_of_sensor_register()
  472. * @dev: a valid struct device pointer of a sensor device. Must contain
  473. * a valid .of_node, for the sensor node.
  474. * @sensor_id: a sensor identifier, in case the sensor IP has more
  475. * than one sensors
  476. * @data: a private pointer (owned by the caller) that will be passed
  477. * back, when a temperature reading is needed.
  478. * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
  479. *
  480. * Refer thermal_zone_of_sensor_register() for more details.
  481. *
  482. * Return: On success returns a valid struct thermal_zone_device,
  483. * otherwise, it returns a corresponding ERR_PTR(). Caller must
  484. * check the return value with help of IS_ERR() helper.
  485. * Registered hermal_zone_device device will automatically be
  486. * released when device is unbounded.
  487. */
  488. struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
  489. struct device *dev, int sensor_id,
  490. void *data, const struct thermal_zone_of_device_ops *ops)
  491. {
  492. struct thermal_zone_device **ptr, *tzd;
  493. ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
  494. GFP_KERNEL);
  495. if (!ptr)
  496. return ERR_PTR(-ENOMEM);
  497. tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
  498. if (IS_ERR(tzd)) {
  499. devres_free(ptr);
  500. return tzd;
  501. }
  502. *ptr = tzd;
  503. devres_add(dev, ptr);
  504. return tzd;
  505. }
  506. EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
  507. /**
  508. * devm_thermal_zone_of_sensor_unregister - Resource managed version of
  509. * thermal_zone_of_sensor_unregister().
  510. * @dev: Device for which which resource was allocated.
  511. * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
  512. *
  513. * This function removes the sensor callbacks and private data from the
  514. * thermal zone device registered with devm_thermal_zone_of_sensor_register()
  515. * API. It will also silent the zone by remove the .get_temp() and .get_trend()
  516. * thermal zone device callbacks.
  517. * Normally this function will not need to be called and the resource
  518. * management code will ensure that the resource is freed.
  519. */
  520. void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  521. struct thermal_zone_device *tzd)
  522. {
  523. WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
  524. devm_thermal_zone_of_sensor_match, tzd));
  525. }
  526. EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
  527. /*** functions parsing device tree nodes ***/
  528. /**
  529. * thermal_of_populate_bind_params - parse and fill cooling map data
  530. * @np: DT node containing a cooling-map node
  531. * @__tbp: data structure to be filled with cooling map info
  532. * @trips: array of thermal zone trip points
  533. * @ntrips: number of trip points inside trips.
  534. *
  535. * This function parses a cooling-map type of node represented by
  536. * @np parameter and fills the read data into @__tbp data structure.
  537. * It needs the already parsed array of trip points of the thermal zone
  538. * in consideration.
  539. *
  540. * Return: 0 on success, proper error code otherwise
  541. */
  542. static int thermal_of_populate_bind_params(struct device_node *np,
  543. struct __thermal_bind_params *__tbp,
  544. struct thermal_trip *trips,
  545. int ntrips)
  546. {
  547. struct of_phandle_args cooling_spec;
  548. struct device_node *trip;
  549. int ret, i;
  550. u32 prop;
  551. /* Default weight. Usage is optional */
  552. __tbp->usage = THERMAL_WEIGHT_DEFAULT;
  553. ret = of_property_read_u32(np, "contribution", &prop);
  554. if (ret == 0)
  555. __tbp->usage = prop;
  556. trip = of_parse_phandle(np, "trip", 0);
  557. if (!trip) {
  558. pr_err("missing trip property\n");
  559. return -ENODEV;
  560. }
  561. /* match using device_node */
  562. for (i = 0; i < ntrips; i++)
  563. if (trip == trips[i].np) {
  564. __tbp->trip_id = i;
  565. break;
  566. }
  567. if (i == ntrips) {
  568. ret = -ENODEV;
  569. goto end;
  570. }
  571. ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
  572. 0, &cooling_spec);
  573. if (ret < 0) {
  574. pr_err("missing cooling_device property\n");
  575. goto end;
  576. }
  577. __tbp->cooling_device = cooling_spec.np;
  578. if (cooling_spec.args_count >= 2) { /* at least min and max */
  579. __tbp->min = cooling_spec.args[0];
  580. __tbp->max = cooling_spec.args[1];
  581. } else {
  582. pr_err("wrong reference to cooling device, missing limits\n");
  583. }
  584. end:
  585. of_node_put(trip);
  586. return ret;
  587. }
  588. /**
  589. * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
  590. * into the device tree binding of 'trip', property type.
  591. */
  592. static const char * const trip_types[] = {
  593. [THERMAL_TRIP_ACTIVE] = "active",
  594. [THERMAL_TRIP_PASSIVE] = "passive",
  595. [THERMAL_TRIP_HOT] = "hot",
  596. [THERMAL_TRIP_CRITICAL] = "critical",
  597. };
  598. /**
  599. * thermal_of_get_trip_type - Get phy mode for given device_node
  600. * @np: Pointer to the given device_node
  601. * @type: Pointer to resulting trip type
  602. *
  603. * The function gets trip type string from property 'type',
  604. * and store its index in trip_types table in @type,
  605. *
  606. * Return: 0 on success, or errno in error case.
  607. */
  608. static int thermal_of_get_trip_type(struct device_node *np,
  609. enum thermal_trip_type *type)
  610. {
  611. const char *t;
  612. int err, i;
  613. err = of_property_read_string(np, "type", &t);
  614. if (err < 0)
  615. return err;
  616. for (i = 0; i < ARRAY_SIZE(trip_types); i++)
  617. if (!strcasecmp(t, trip_types[i])) {
  618. *type = i;
  619. return 0;
  620. }
  621. return -ENODEV;
  622. }
  623. /**
  624. * thermal_of_populate_trip - parse and fill one trip point data
  625. * @np: DT node containing a trip point node
  626. * @trip: trip point data structure to be filled up
  627. *
  628. * This function parses a trip point type of node represented by
  629. * @np parameter and fills the read data into @trip data structure.
  630. *
  631. * Return: 0 on success, proper error code otherwise
  632. */
  633. static int thermal_of_populate_trip(struct device_node *np,
  634. struct thermal_trip *trip)
  635. {
  636. int prop;
  637. int ret;
  638. ret = of_property_read_u32(np, "temperature", &prop);
  639. if (ret < 0) {
  640. pr_err("missing temperature property\n");
  641. return ret;
  642. }
  643. trip->temperature = prop;
  644. ret = of_property_read_u32(np, "hysteresis", &prop);
  645. if (ret < 0) {
  646. pr_err("missing hysteresis property\n");
  647. return ret;
  648. }
  649. trip->hysteresis = prop;
  650. ret = thermal_of_get_trip_type(np, &trip->type);
  651. if (ret < 0) {
  652. pr_err("wrong trip type property\n");
  653. return ret;
  654. }
  655. /* Required for cooling map matching */
  656. trip->np = np;
  657. of_node_get(np);
  658. return 0;
  659. }
  660. /**
  661. * thermal_of_build_thermal_zone - parse and fill one thermal zone data
  662. * @np: DT node containing a thermal zone node
  663. *
  664. * This function parses a thermal zone type of node represented by
  665. * @np parameter and fills the read data into a __thermal_zone data structure
  666. * and return this pointer.
  667. *
  668. * TODO: Missing properties to parse: thermal-sensor-names
  669. *
  670. * Return: On success returns a valid struct __thermal_zone,
  671. * otherwise, it returns a corresponding ERR_PTR(). Caller must
  672. * check the return value with help of IS_ERR() helper.
  673. */
  674. static struct __thermal_zone *
  675. thermal_of_build_thermal_zone(struct device_node *np)
  676. {
  677. struct device_node *child = NULL, *gchild;
  678. struct __thermal_zone *tz;
  679. int ret, i;
  680. u32 prop, coef[2];
  681. if (!np) {
  682. pr_err("no thermal zone np\n");
  683. return ERR_PTR(-EINVAL);
  684. }
  685. tz = kzalloc(sizeof(*tz), GFP_KERNEL);
  686. if (!tz)
  687. return ERR_PTR(-ENOMEM);
  688. ret = of_property_read_u32(np, "polling-delay-passive", &prop);
  689. if (ret < 0) {
  690. pr_err("missing polling-delay-passive property\n");
  691. goto free_tz;
  692. }
  693. tz->passive_delay = prop;
  694. ret = of_property_read_u32(np, "polling-delay", &prop);
  695. if (ret < 0) {
  696. pr_err("missing polling-delay property\n");
  697. goto free_tz;
  698. }
  699. tz->polling_delay = prop;
  700. /*
  701. * REVIST: for now, the thermal framework supports only
  702. * one sensor per thermal zone. Thus, we are considering
  703. * only the first two values as slope and offset.
  704. */
  705. ret = of_property_read_u32_array(np, "coefficients", coef, 2);
  706. if (ret == 0) {
  707. tz->slope = coef[0];
  708. tz->offset = coef[1];
  709. } else {
  710. tz->slope = 1;
  711. tz->offset = 0;
  712. }
  713. /* trips */
  714. child = of_get_child_by_name(np, "trips");
  715. /* No trips provided */
  716. if (!child)
  717. goto finish;
  718. tz->ntrips = of_get_child_count(child);
  719. if (tz->ntrips == 0) /* must have at least one child */
  720. goto finish;
  721. tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
  722. if (!tz->trips) {
  723. ret = -ENOMEM;
  724. goto free_tz;
  725. }
  726. i = 0;
  727. for_each_child_of_node(child, gchild) {
  728. ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
  729. if (ret)
  730. goto free_trips;
  731. }
  732. of_node_put(child);
  733. /* cooling-maps */
  734. child = of_get_child_by_name(np, "cooling-maps");
  735. /* cooling-maps not provided */
  736. if (!child)
  737. goto finish;
  738. tz->num_tbps = of_get_child_count(child);
  739. if (tz->num_tbps == 0)
  740. goto finish;
  741. tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
  742. if (!tz->tbps) {
  743. ret = -ENOMEM;
  744. goto free_trips;
  745. }
  746. i = 0;
  747. for_each_child_of_node(child, gchild) {
  748. ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
  749. tz->trips, tz->ntrips);
  750. if (ret)
  751. goto free_tbps;
  752. }
  753. finish:
  754. of_node_put(child);
  755. tz->mode = THERMAL_DEVICE_DISABLED;
  756. return tz;
  757. free_tbps:
  758. for (i = 0; i < tz->num_tbps; i++)
  759. of_node_put(tz->tbps[i].cooling_device);
  760. kfree(tz->tbps);
  761. free_trips:
  762. for (i = 0; i < tz->ntrips; i++)
  763. of_node_put(tz->trips[i].np);
  764. kfree(tz->trips);
  765. of_node_put(gchild);
  766. free_tz:
  767. kfree(tz);
  768. of_node_put(child);
  769. return ERR_PTR(ret);
  770. }
  771. static inline void of_thermal_free_zone(struct __thermal_zone *tz)
  772. {
  773. int i;
  774. for (i = 0; i < tz->num_tbps; i++)
  775. of_node_put(tz->tbps[i].cooling_device);
  776. kfree(tz->tbps);
  777. for (i = 0; i < tz->ntrips; i++)
  778. of_node_put(tz->trips[i].np);
  779. kfree(tz->trips);
  780. kfree(tz);
  781. }
  782. /**
  783. * of_parse_thermal_zones - parse device tree thermal data
  784. *
  785. * Initialization function that can be called by machine initialization
  786. * code to parse thermal data and populate the thermal framework
  787. * with hardware thermal zones info. This function only parses thermal zones.
  788. * Cooling devices and sensor devices nodes are supposed to be parsed
  789. * by their respective drivers.
  790. *
  791. * Return: 0 on success, proper error code otherwise
  792. *
  793. */
  794. int __init of_parse_thermal_zones(void)
  795. {
  796. struct device_node *np, *child;
  797. struct __thermal_zone *tz;
  798. struct thermal_zone_device_ops *ops;
  799. np = of_find_node_by_name(NULL, "thermal-zones");
  800. if (!np) {
  801. pr_debug("unable to find thermal zones\n");
  802. return 0; /* Run successfully on systems without thermal DT */
  803. }
  804. for_each_available_child_of_node(np, child) {
  805. struct thermal_zone_device *zone;
  806. struct thermal_zone_params *tzp;
  807. int i, mask = 0;
  808. u32 prop;
  809. tz = thermal_of_build_thermal_zone(child);
  810. if (IS_ERR(tz)) {
  811. pr_err("failed to build thermal zone %s: %ld\n",
  812. child->name,
  813. PTR_ERR(tz));
  814. continue;
  815. }
  816. ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
  817. if (!ops)
  818. goto exit_free;
  819. tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
  820. if (!tzp) {
  821. kfree(ops);
  822. goto exit_free;
  823. }
  824. /* No hwmon because there might be hwmon drivers registering */
  825. tzp->no_hwmon = true;
  826. if (!of_property_read_u32(child, "sustainable-power", &prop))
  827. tzp->sustainable_power = prop;
  828. for (i = 0; i < tz->ntrips; i++)
  829. mask |= 1 << i;
  830. /* these two are left for temperature drivers to use */
  831. tzp->slope = tz->slope;
  832. tzp->offset = tz->offset;
  833. zone = thermal_zone_device_register(child->name, tz->ntrips,
  834. mask, tz,
  835. ops, tzp,
  836. tz->passive_delay,
  837. tz->polling_delay);
  838. if (IS_ERR(zone)) {
  839. pr_err("Failed to build %s zone %ld\n", child->name,
  840. PTR_ERR(zone));
  841. kfree(tzp);
  842. kfree(ops);
  843. of_thermal_free_zone(tz);
  844. /* attempting to build remaining zones still */
  845. }
  846. }
  847. of_node_put(np);
  848. return 0;
  849. exit_free:
  850. of_node_put(child);
  851. of_node_put(np);
  852. of_thermal_free_zone(tz);
  853. /* no memory available, so free what we have built */
  854. of_thermal_destroy_zones();
  855. return -ENOMEM;
  856. }
  857. /**
  858. * of_thermal_destroy_zones - remove all zones parsed and allocated resources
  859. *
  860. * Finds all zones parsed and added to the thermal framework and remove them
  861. * from the system, together with their resources.
  862. *
  863. */
  864. void of_thermal_destroy_zones(void)
  865. {
  866. struct device_node *np, *child;
  867. np = of_find_node_by_name(NULL, "thermal-zones");
  868. if (!np) {
  869. pr_debug("unable to find thermal zones\n");
  870. return;
  871. }
  872. for_each_available_child_of_node(np, child) {
  873. struct thermal_zone_device *zone;
  874. zone = thermal_zone_get_zone_by_name(child->name);
  875. if (IS_ERR(zone))
  876. continue;
  877. thermal_zone_device_unregister(zone);
  878. kfree(zone->tzp);
  879. kfree(zone->ops);
  880. of_thermal_free_zone(zone->devdata);
  881. }
  882. of_node_put(np);
  883. }