of-thermal.c 27 KB

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