of-thermal.c 24 KB

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