of-thermal.c 27 KB

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