of-thermal.c 27 KB

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