of-thermal.c 24 KB

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