of-thermal.c 27 KB

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