of-thermal.c 21 KB

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