thermal_of.c 28 KB

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