of-thermal.c 28 KB

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