of-thermal.c 24 KB

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