of-thermal.c 27 KB

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