of-thermal.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. /*
  2. * of-thermal.c - Generic Thermal Management device tree support.
  3. *
  4. * Copyright (C) 2013 Texas Instruments
  5. * Copyright (C) 2013 Eduardo Valentin <[email protected]>
  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 <linux/list.h>
  35. #define CREATE_TRACE_POINTS
  36. #include <trace/events/thermal_virtual.h>
  37. #include "thermal_core.h"
  38. /*** Private data structures to represent thermal device tree data ***/
  39. /**
  40. * struct __thermal_bind_param - a match between trip and cooling device
  41. * @cooling_device: a pointer to identify the referred cooling device
  42. * @trip_id: the trip point index
  43. * @usage: the percentage (from 0 to 100) of cooling contribution
  44. * @min: minimum cooling state used at this trip point
  45. * @max: maximum cooling state used at this trip point
  46. */
  47. struct __thermal_bind_params {
  48. struct device_node *cooling_device;
  49. unsigned int trip_id;
  50. unsigned int usage;
  51. unsigned long min;
  52. unsigned long max;
  53. };
  54. /**
  55. * struct __sensor_param - Holds individual sensor data
  56. * @sensor_data: sensor driver private data passed as input argument
  57. * @ops: sensor driver ops
  58. * @trip_high: last trip high value programmed in the sensor driver
  59. * @trip_low: last trip low value programmed in the sensor driver
  60. * @lock: mutex lock acquired before updating the trip temperatures
  61. * @first_tz: list head pointing the first thermal zone
  62. */
  63. struct __sensor_param {
  64. void *sensor_data;
  65. const struct thermal_zone_of_device_ops *ops;
  66. int trip_high, trip_low;
  67. struct mutex lock;
  68. struct list_head first_tz;
  69. };
  70. /**
  71. * struct __thermal_zone - internal representation of a thermal zone
  72. * @mode: current thermal zone device mode (enabled/disabled)
  73. * @passive_delay: polling interval while passive cooling is activated
  74. * @polling_delay: zone polling interval
  75. * @slope: slope of the temperature adjustment curve
  76. * @offset: offset of the temperature adjustment curve
  77. * @default_disable: Keep the thermal zone disabled by default
  78. * @is_wakeable: Ignore post suspend thermal zone re-evaluation
  79. * @tzd: thermal zone device pointer for this sensor
  80. * @ntrips: number of trip points
  81. * @trips: an array of trip points (0..ntrips - 1)
  82. * @num_tbps: number of thermal bind params
  83. * @tbps: an array of thermal bind params (0..num_tbps - 1)
  84. * @list: sibling thermal zone pointer
  85. * @senps: sensor related parameters
  86. */
  87. struct __thermal_zone {
  88. enum thermal_device_mode mode;
  89. int passive_delay;
  90. int polling_delay;
  91. int slope;
  92. int offset;
  93. struct thermal_zone_device *tzd;
  94. bool default_disable;
  95. bool is_wakeable;
  96. /* trip data */
  97. int ntrips;
  98. struct thermal_trip *trips;
  99. /* cooling binding data */
  100. int num_tbps;
  101. struct __thermal_bind_params *tbps;
  102. struct list_head list;
  103. /* sensor interface */
  104. struct __sensor_param *senps;
  105. };
  106. /**
  107. * struct virtual_sensor - internal representation of a virtual thermal zone
  108. * @num_sensors - number of sensors this virtual sensor will reference to
  109. * estimate temperature
  110. * @tz - Array of thermal zones of the sensors this virtual sensor will use
  111. * to estimate temperature
  112. * @virt_tz - Virtual thermal zone pointer
  113. * @logic - aggregation logic to be used to estimate the temperature
  114. * @last_reading - last estimated temperature
  115. * @coefficients - array of coefficients to be used for weighted aggregation
  116. * logic
  117. * @avg_offset - offset value to be used for the weighted aggregation logic
  118. * @avg_denominator - denominator value to be used for the weighted aggregation
  119. * logic
  120. */
  121. struct virtual_sensor {
  122. int num_sensors;
  123. struct thermal_zone_device *tz[THERMAL_MAX_VIRT_SENSORS];
  124. struct thermal_zone_device *virt_tz;
  125. enum aggregation_logic logic;
  126. int last_reading;
  127. int coefficients[THERMAL_MAX_VIRT_SENSORS];
  128. int avg_offset;
  129. int avg_denominator;
  130. };
  131. static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
  132. unsigned int trip_type_mask, int *low, int *high);
  133. /*** DT thermal zone device callbacks ***/
  134. static int virt_sensor_read_temp(void *data, int *val)
  135. {
  136. struct virtual_sensor *sens = data;
  137. int idx, temp = 0, ret = 0;
  138. for (idx = 0; idx < sens->num_sensors; idx++) {
  139. int sens_temp = 0;
  140. ret = thermal_zone_get_temp(sens->tz[idx], &sens_temp);
  141. if (ret) {
  142. pr_err("virt zone: sensor[%s] read error:%d\n",
  143. sens->tz[idx]->type, ret);
  144. return ret;
  145. }
  146. switch (sens->logic) {
  147. case VIRT_WEIGHTED_AVG:
  148. temp += sens_temp * sens->coefficients[idx];
  149. if (idx == (sens->num_sensors - 1))
  150. temp = (temp + sens->avg_offset)
  151. / sens->avg_denominator;
  152. break;
  153. case VIRT_MAXIMUM:
  154. if (idx == 0)
  155. temp = INT_MIN;
  156. if (sens_temp > temp)
  157. temp = sens_temp;
  158. break;
  159. case VIRT_MINIMUM:
  160. if (idx == 0)
  161. temp = INT_MAX;
  162. if (sens_temp < temp)
  163. temp = sens_temp;
  164. break;
  165. default:
  166. break;
  167. }
  168. trace_virtual_temperature(sens->virt_tz, sens->tz[idx],
  169. sens_temp, temp);
  170. }
  171. sens->last_reading = *val = temp;
  172. return 0;
  173. }
  174. static int of_thermal_get_temp(struct thermal_zone_device *tz,
  175. int *temp)
  176. {
  177. struct __thermal_zone *data = tz->devdata;
  178. if (!data->senps || !data->senps->ops->get_temp)
  179. return -EINVAL;
  180. if (data->mode == THERMAL_DEVICE_DISABLED) {
  181. *temp = tz->tzp->tracks_low ?
  182. THERMAL_TEMP_INVALID_LOW :
  183. THERMAL_TEMP_INVALID;
  184. return 0;
  185. }
  186. return data->senps->ops->get_temp(data->senps->sensor_data, temp);
  187. }
  188. static int of_thermal_set_trips(struct thermal_zone_device *tz,
  189. int inp_low, int inp_high)
  190. {
  191. struct __thermal_zone *data = tz->devdata;
  192. int high = INT_MAX, low = INT_MIN, ret = 0;
  193. if (!data->senps || !data->senps->ops->set_trips)
  194. return -EINVAL;
  195. mutex_lock(&data->senps->lock);
  196. of_thermal_aggregate_trip_types(tz, GENMASK(THERMAL_TRIP_CRITICAL, 0),
  197. &low, &high);
  198. data->senps->trip_low = low;
  199. data->senps->trip_high = high;
  200. ret = data->senps->ops->set_trips(data->senps->sensor_data,
  201. low, high);
  202. mutex_unlock(&data->senps->lock);
  203. return ret;
  204. }
  205. /**
  206. * of_thermal_get_ntrips - function to export number of available trip
  207. * points.
  208. * @tz: pointer to a thermal zone
  209. *
  210. * This function is a globally visible wrapper to get number of trip points
  211. * stored in the local struct __thermal_zone
  212. *
  213. * Return: number of available trip points, -ENODEV when data not available
  214. */
  215. int of_thermal_get_ntrips(struct thermal_zone_device *tz)
  216. {
  217. struct __thermal_zone *data = tz->devdata;
  218. if (!data || IS_ERR(data))
  219. return -ENODEV;
  220. return data->ntrips;
  221. }
  222. EXPORT_SYMBOL_GPL(of_thermal_get_ntrips);
  223. /**
  224. * of_thermal_is_trip_valid - function to check if trip point is valid
  225. *
  226. * @tz: pointer to a thermal zone
  227. * @trip: trip point to evaluate
  228. *
  229. * This function is responsible for checking if passed trip point is valid
  230. *
  231. * Return: true if trip point is valid, false otherwise
  232. */
  233. bool of_thermal_is_trip_valid(struct thermal_zone_device *tz, int trip)
  234. {
  235. struct __thermal_zone *data = tz->devdata;
  236. if (!data || trip >= data->ntrips || trip < 0)
  237. return false;
  238. return true;
  239. }
  240. EXPORT_SYMBOL_GPL(of_thermal_is_trip_valid);
  241. /**
  242. * of_thermal_get_trip_points - function to get access to a globally exported
  243. * trip points
  244. *
  245. * @tz: pointer to a thermal zone
  246. *
  247. * This function provides a pointer to trip points table
  248. *
  249. * Return: pointer to trip points table, NULL otherwise
  250. */
  251. const struct thermal_trip *
  252. of_thermal_get_trip_points(struct thermal_zone_device *tz)
  253. {
  254. struct __thermal_zone *data = tz->devdata;
  255. if (!data)
  256. return NULL;
  257. return data->trips;
  258. }
  259. EXPORT_SYMBOL_GPL(of_thermal_get_trip_points);
  260. /**
  261. * of_thermal_set_emul_temp - function to set emulated temperature
  262. *
  263. * @tz: pointer to a thermal zone
  264. * @temp: temperature to set
  265. *
  266. * This function gives the ability to set emulated value of temperature,
  267. * which is handy for debugging
  268. *
  269. * Return: zero on success, error code otherwise
  270. */
  271. static int of_thermal_set_emul_temp(struct thermal_zone_device *tz,
  272. int temp)
  273. {
  274. struct __thermal_zone *data = tz->devdata;
  275. if (!data->senps || !data->senps->ops->set_emul_temp)
  276. return -EINVAL;
  277. return data->senps->ops->set_emul_temp(data->senps->sensor_data, temp);
  278. }
  279. static int of_thermal_get_trend(struct thermal_zone_device *tz, int trip,
  280. enum thermal_trend *trend)
  281. {
  282. struct __thermal_zone *data = tz->devdata;
  283. if (!data->senps || !data->senps->ops->get_trend)
  284. return -EINVAL;
  285. return data->senps->ops->get_trend(data->senps->sensor_data,
  286. trip, trend);
  287. }
  288. static int of_thermal_bind(struct thermal_zone_device *thermal,
  289. struct thermal_cooling_device *cdev)
  290. {
  291. struct __thermal_zone *data = thermal->devdata;
  292. int i;
  293. if (!data || IS_ERR(data))
  294. return -ENODEV;
  295. /* find where to bind */
  296. for (i = 0; i < data->num_tbps; i++) {
  297. struct __thermal_bind_params *tbp = data->tbps + i;
  298. if (tbp->cooling_device == cdev->np) {
  299. int ret;
  300. ret = thermal_zone_bind_cooling_device(thermal,
  301. tbp->trip_id, cdev,
  302. tbp->max,
  303. tbp->min,
  304. tbp->usage);
  305. if (ret)
  306. return ret;
  307. }
  308. }
  309. return 0;
  310. }
  311. static int of_thermal_unbind(struct thermal_zone_device *thermal,
  312. struct thermal_cooling_device *cdev)
  313. {
  314. struct __thermal_zone *data = thermal->devdata;
  315. int i;
  316. if (!data || IS_ERR(data))
  317. return -ENODEV;
  318. /* find where to unbind */
  319. for (i = 0; i < data->num_tbps; i++) {
  320. struct __thermal_bind_params *tbp = data->tbps + i;
  321. if (tbp->cooling_device == cdev->np) {
  322. int ret;
  323. ret = thermal_zone_unbind_cooling_device(thermal,
  324. tbp->trip_id, cdev);
  325. if (ret)
  326. return ret;
  327. }
  328. }
  329. return 0;
  330. }
  331. static int of_thermal_get_mode(struct thermal_zone_device *tz,
  332. enum thermal_device_mode *mode)
  333. {
  334. struct __thermal_zone *data = tz->devdata;
  335. *mode = data->mode;
  336. return 0;
  337. }
  338. static int of_thermal_set_mode(struct thermal_zone_device *tz,
  339. enum thermal_device_mode mode)
  340. {
  341. struct __thermal_zone *data = tz->devdata;
  342. mutex_lock(&tz->lock);
  343. if (mode == THERMAL_DEVICE_ENABLED) {
  344. tz->polling_delay = data->polling_delay;
  345. tz->passive_delay = data->passive_delay;
  346. } else {
  347. tz->polling_delay = 0;
  348. tz->passive_delay = 0;
  349. }
  350. mutex_unlock(&tz->lock);
  351. data->mode = mode;
  352. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  353. return 0;
  354. }
  355. static int of_thermal_get_trip_type(struct thermal_zone_device *tz, int trip,
  356. enum thermal_trip_type *type)
  357. {
  358. struct __thermal_zone *data = tz->devdata;
  359. if (trip >= data->ntrips || trip < 0)
  360. return -EDOM;
  361. *type = data->trips[trip].type;
  362. return 0;
  363. }
  364. static int of_thermal_get_trip_temp(struct thermal_zone_device *tz, int trip,
  365. int *temp)
  366. {
  367. struct __thermal_zone *data = tz->devdata;
  368. if (trip >= data->ntrips || trip < 0)
  369. return -EDOM;
  370. if (data->senps && data->senps->ops->get_trip_temp) {
  371. int ret;
  372. ret = data->senps->ops->get_trip_temp(data->senps->sensor_data,
  373. trip, temp);
  374. if (ret)
  375. return ret;
  376. } else {
  377. *temp = data->trips[trip].temperature;
  378. }
  379. return 0;
  380. }
  381. static int of_thermal_set_trip_temp(struct thermal_zone_device *tz, int trip,
  382. int temp)
  383. {
  384. struct __thermal_zone *data = tz->devdata;
  385. if (trip >= data->ntrips || trip < 0)
  386. return -EDOM;
  387. if (data->senps && data->senps->ops->set_trip_temp) {
  388. int ret;
  389. ret = data->senps->ops->set_trip_temp(data->senps->sensor_data,
  390. trip, temp);
  391. if (ret)
  392. return ret;
  393. }
  394. /* thermal framework should take care of data->mask & (1 << trip) */
  395. data->trips[trip].temperature = temp;
  396. return 0;
  397. }
  398. static int of_thermal_get_trip_hyst(struct thermal_zone_device *tz, int trip,
  399. int *hyst)
  400. {
  401. struct __thermal_zone *data = tz->devdata;
  402. if (trip >= data->ntrips || trip < 0)
  403. return -EDOM;
  404. *hyst = data->trips[trip].hysteresis;
  405. return 0;
  406. }
  407. static int of_thermal_set_trip_hyst(struct thermal_zone_device *tz, int trip,
  408. int hyst)
  409. {
  410. struct __thermal_zone *data = tz->devdata;
  411. if (trip >= data->ntrips || trip < 0)
  412. return -EDOM;
  413. /* thermal framework should take care of data->mask & (1 << trip) */
  414. data->trips[trip].hysteresis = hyst;
  415. return 0;
  416. }
  417. static int of_thermal_get_crit_temp(struct thermal_zone_device *tz,
  418. int *temp)
  419. {
  420. struct __thermal_zone *data = tz->devdata;
  421. int i;
  422. for (i = 0; i < data->ntrips; i++)
  423. if (data->trips[i].type == THERMAL_TRIP_CRITICAL) {
  424. *temp = data->trips[i].temperature;
  425. return 0;
  426. }
  427. return -EINVAL;
  428. }
  429. static bool of_thermal_is_wakeable(struct thermal_zone_device *tz)
  430. {
  431. struct __thermal_zone *data = tz->devdata;
  432. return data->is_wakeable;
  433. }
  434. static int of_thermal_aggregate_trip_types(struct thermal_zone_device *tz,
  435. unsigned int trip_type_mask, int *low, int *high)
  436. {
  437. int min = INT_MIN;
  438. int max = INT_MAX;
  439. int tt, th, trip;
  440. int temp = tz->temperature;
  441. struct thermal_zone_device *zone = NULL;
  442. struct __thermal_zone *data = tz->devdata;
  443. struct list_head *head;
  444. enum thermal_trip_type type = 0;
  445. head = &data->senps->first_tz;
  446. list_for_each_entry(data, head, list) {
  447. zone = data->tzd;
  448. if (data->mode == THERMAL_DEVICE_DISABLED)
  449. continue;
  450. for (trip = 0; trip < data->ntrips; trip++) {
  451. of_thermal_get_trip_type(zone, trip, &type);
  452. if (!(BIT(type) & trip_type_mask))
  453. continue;
  454. if (!zone->tzp->tracks_low) {
  455. tt = data->trips[trip].temperature;
  456. if (tt > temp && tt < max)
  457. max = tt;
  458. th = tt - data->trips[trip].hysteresis;
  459. if (th < temp && th > min)
  460. min = th;
  461. } else {
  462. tt = data->trips[trip].temperature;
  463. if (tt < temp && tt > min)
  464. min = tt;
  465. th = tt + data->trips[trip].hysteresis;
  466. if (th > temp && th < max)
  467. max = th;
  468. }
  469. }
  470. }
  471. *high = max;
  472. *low = min;
  473. return 0;
  474. }
  475. /*
  476. * of_thermal_aggregate_trip - aggregate trip temperatures across sibling
  477. * thermal zones.
  478. * @tz: pointer to the primary thermal zone.
  479. * @type: the thermal trip type to be aggregated upon
  480. * @low: the low trip threshold which the most lesser than the @temp
  481. * @high: the high trip threshold which is the least greater than the @temp
  482. */
  483. int of_thermal_aggregate_trip(struct thermal_zone_device *tz,
  484. enum thermal_trip_type type,
  485. int *low, int *high)
  486. {
  487. if (type <= THERMAL_TRIP_CRITICAL)
  488. return of_thermal_aggregate_trip_types(tz, BIT(type), low,
  489. high);
  490. return -EINVAL;
  491. }
  492. EXPORT_SYMBOL(of_thermal_aggregate_trip);
  493. static void handle_thermal_trip(struct thermal_zone_device *tz,
  494. bool temp_valid, int trip_temp)
  495. {
  496. struct thermal_zone_device *zone;
  497. struct __thermal_zone *data = tz->devdata;
  498. struct list_head *head;
  499. head = &data->senps->first_tz;
  500. list_for_each_entry(data, head, list) {
  501. zone = data->tzd;
  502. if (data->mode == THERMAL_DEVICE_DISABLED)
  503. continue;
  504. if (!temp_valid) {
  505. thermal_zone_device_update(zone,
  506. THERMAL_EVENT_UNSPECIFIED);
  507. } else {
  508. thermal_zone_device_update_temp(zone,
  509. THERMAL_EVENT_UNSPECIFIED, trip_temp);
  510. }
  511. }
  512. }
  513. /*
  514. * of_thermal_handle_trip_temp - Handle thermal trip from sensors
  515. *
  516. * @tz: pointer to the primary thermal zone.
  517. * @trip_temp: The temperature
  518. */
  519. void of_thermal_handle_trip_temp(struct thermal_zone_device *tz,
  520. int trip_temp)
  521. {
  522. return handle_thermal_trip(tz, true, trip_temp);
  523. }
  524. EXPORT_SYMBOL(of_thermal_handle_trip_temp);
  525. /*
  526. * of_thermal_handle_trip - Handle thermal trip from sensors
  527. *
  528. * @tz: pointer to the primary thermal zone.
  529. */
  530. void of_thermal_handle_trip(struct thermal_zone_device *tz)
  531. {
  532. return handle_thermal_trip(tz, false, 0);
  533. }
  534. EXPORT_SYMBOL(of_thermal_handle_trip);
  535. static struct thermal_zone_device_ops of_thermal_ops = {
  536. .get_mode = of_thermal_get_mode,
  537. .set_mode = of_thermal_set_mode,
  538. .get_trip_type = of_thermal_get_trip_type,
  539. .get_trip_temp = of_thermal_get_trip_temp,
  540. .set_trip_temp = of_thermal_set_trip_temp,
  541. .get_trip_hyst = of_thermal_get_trip_hyst,
  542. .set_trip_hyst = of_thermal_set_trip_hyst,
  543. .get_crit_temp = of_thermal_get_crit_temp,
  544. .bind = of_thermal_bind,
  545. .unbind = of_thermal_unbind,
  546. .is_wakeable = of_thermal_is_wakeable,
  547. };
  548. static struct thermal_zone_of_device_ops of_virt_ops = {
  549. .get_temp = virt_sensor_read_temp,
  550. };
  551. /*** sensor API ***/
  552. static struct thermal_zone_device *
  553. thermal_zone_of_add_sensor(struct device_node *zone,
  554. struct device_node *sensor,
  555. struct __sensor_param *sens_param)
  556. {
  557. struct thermal_zone_device *tzd;
  558. struct __thermal_zone *tz;
  559. tzd = thermal_zone_get_zone_by_name(zone->name);
  560. if (IS_ERR(tzd))
  561. return ERR_PTR(-EPROBE_DEFER);
  562. tz = tzd->devdata;
  563. if (!sens_param->ops)
  564. return ERR_PTR(-EINVAL);
  565. mutex_lock(&tzd->lock);
  566. tz->senps = sens_param;
  567. tzd->ops->get_temp = of_thermal_get_temp;
  568. tzd->ops->get_trend = of_thermal_get_trend;
  569. /*
  570. * The thermal zone core will calculate the window if they have set the
  571. * optional set_trips pointer.
  572. */
  573. if (sens_param->ops->set_trips)
  574. tzd->ops->set_trips = of_thermal_set_trips;
  575. if (sens_param->ops->set_emul_temp)
  576. tzd->ops->set_emul_temp = of_thermal_set_emul_temp;
  577. list_add_tail(&tz->list, &sens_param->first_tz);
  578. mutex_unlock(&tzd->lock);
  579. return tzd;
  580. }
  581. /**
  582. * thermal_zone_of_sensor_register - registers a sensor to a DT thermal zone
  583. * @dev: a valid struct device pointer of a sensor device. Must contain
  584. * a valid .of_node, for the sensor node.
  585. * @sensor_id: a sensor identifier, in case the sensor IP has more
  586. * than one sensors
  587. * @data: a private pointer (owned by the caller) that will be passed
  588. * back, when a temperature reading is needed.
  589. * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
  590. *
  591. * This function will search the list of thermal zones described in device
  592. * tree and look for the zone that refer to the sensor device pointed by
  593. * @dev->of_node as temperature providers. For the zone pointing to the
  594. * sensor node, the sensor will be added to the DT thermal zone device.
  595. *
  596. * The thermal zone temperature is provided by the @get_temp function
  597. * pointer. When called, it will have the private pointer @data back.
  598. *
  599. * The thermal zone temperature trend is provided by the @get_trend function
  600. * pointer. When called, it will have the private pointer @data back.
  601. *
  602. * TODO:
  603. * 01 - This function must enqueue the new sensor instead of using
  604. * it as the only source of temperature values.
  605. *
  606. * Return: On success returns a valid struct thermal_zone_device,
  607. * otherwise, it returns a corresponding ERR_PTR(). Incase there are multiple
  608. * thermal zones referencing the same sensor, the return value will be
  609. * thermal_zone_device pointer of the first thermal zone. Caller must
  610. * check the return value with help of IS_ERR() helper.
  611. */
  612. struct thermal_zone_device *
  613. thermal_zone_of_sensor_register(struct device *dev, int sensor_id, void *data,
  614. const struct thermal_zone_of_device_ops *ops)
  615. {
  616. struct device_node *np, *child, *sensor_np;
  617. struct thermal_zone_device *tzd = ERR_PTR(-ENODEV);
  618. struct thermal_zone_device *first_tzd = NULL;
  619. struct __sensor_param *sens_param = NULL;
  620. np = of_find_node_by_name(NULL, "thermal-zones");
  621. if (!np)
  622. return ERR_PTR(-ENODEV);
  623. if (!dev || !dev->of_node) {
  624. of_node_put(np);
  625. return ERR_PTR(-EINVAL);
  626. }
  627. sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
  628. if (!sens_param) {
  629. of_node_put(np);
  630. return ERR_PTR(-ENOMEM);
  631. }
  632. sens_param->sensor_data = data;
  633. sens_param->ops = ops;
  634. INIT_LIST_HEAD(&sens_param->first_tz);
  635. sens_param->trip_high = INT_MAX;
  636. sens_param->trip_low = INT_MIN;
  637. mutex_init(&sens_param->lock);
  638. sensor_np = of_node_get(dev->of_node);
  639. for_each_available_child_of_node(np, child) {
  640. struct of_phandle_args sensor_specs;
  641. int ret, id;
  642. struct __thermal_zone *tz;
  643. /* For now, thermal framework supports only 1 sensor per zone */
  644. ret = of_parse_phandle_with_args(child, "thermal-sensors",
  645. "#thermal-sensor-cells",
  646. 0, &sensor_specs);
  647. if (ret)
  648. continue;
  649. if (sensor_specs.args_count >= 1) {
  650. id = sensor_specs.args[0];
  651. WARN(sensor_specs.args_count > 1,
  652. "%s: too many cells in sensor specifier %d\n",
  653. sensor_specs.np->name, sensor_specs.args_count);
  654. } else {
  655. id = 0;
  656. }
  657. if (sensor_specs.np == sensor_np && id == sensor_id) {
  658. tzd = thermal_zone_of_add_sensor(child, sensor_np,
  659. sens_param);
  660. if (!IS_ERR(tzd)) {
  661. if (!first_tzd)
  662. first_tzd = tzd;
  663. tz = tzd->devdata;
  664. if (!tz->default_disable)
  665. tzd->ops->set_mode(tzd,
  666. THERMAL_DEVICE_ENABLED);
  667. }
  668. }
  669. of_node_put(sensor_specs.np);
  670. }
  671. of_node_put(sensor_np);
  672. of_node_put(np);
  673. if (!first_tzd) {
  674. first_tzd = ERR_PTR(-ENODEV);
  675. kfree(sens_param);
  676. }
  677. return first_tzd;
  678. }
  679. EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_register);
  680. /**
  681. * thermal_zone_of_sensor_unregister - unregisters a sensor from a DT thermal zone
  682. * @dev: a valid struct device pointer of a sensor device. Must contain
  683. * a valid .of_node, for the sensor node.
  684. * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
  685. *
  686. * This function removes the sensor callbacks and private data from the
  687. * thermal zone device registered with thermal_zone_of_sensor_register()
  688. * API. It will also silent the zone by remove the .get_temp() and .get_trend()
  689. * thermal zone device callbacks.
  690. *
  691. * TODO: When the support to several sensors per zone is added, this
  692. * function must search the sensor list based on @dev parameter.
  693. *
  694. */
  695. void thermal_zone_of_sensor_unregister(struct device *dev,
  696. struct thermal_zone_device *tzd)
  697. {
  698. struct __thermal_zone *tz, *next;
  699. struct thermal_zone_device *pos;
  700. struct list_head *head;
  701. if (!dev || !tzd || !tzd->devdata)
  702. return;
  703. tz = tzd->devdata;
  704. /* no __thermal_zone, nothing to be done */
  705. if (!tz)
  706. return;
  707. head = &tz->senps->first_tz;
  708. list_for_each_entry_safe(tz, next, head, list) {
  709. pos = tz->tzd;
  710. mutex_lock(&pos->lock);
  711. pos->ops->get_temp = NULL;
  712. pos->ops->get_trend = NULL;
  713. pos->ops->set_emul_temp = NULL;
  714. list_del(&tz->list);
  715. if (list_empty(&tz->senps->first_tz))
  716. kfree(tz->senps);
  717. tz->senps = NULL;
  718. mutex_unlock(&pos->lock);
  719. }
  720. }
  721. EXPORT_SYMBOL_GPL(thermal_zone_of_sensor_unregister);
  722. static void devm_thermal_zone_of_sensor_release(struct device *dev, void *res)
  723. {
  724. thermal_zone_of_sensor_unregister(dev,
  725. *(struct thermal_zone_device **)res);
  726. }
  727. static int devm_thermal_zone_of_sensor_match(struct device *dev, void *res,
  728. void *data)
  729. {
  730. struct thermal_zone_device **r = res;
  731. if (WARN_ON(!r || !*r))
  732. return 0;
  733. return *r == data;
  734. }
  735. /**
  736. * devm_thermal_of_virtual_sensor_register - Register a virtual sensor.
  737. * Three types of virtual sensors are supported.
  738. * 1. Weighted aggregation type:
  739. * Virtual sensor of this type calculates the weighted aggregation
  740. * of sensor temperatures using the below formula,
  741. * temp = (sensor_1_temp * coeff_1 + ... + sensor_n_temp * coeff_n)
  742. * + avg_offset / avg_denominator
  743. * So the sensor drivers has to specify n+2 coefficients.
  744. * 2. Maximum type:
  745. * Virtual sensors of this type will report the maximum of all
  746. * sensor temperatures.
  747. * 3. Minimum type:
  748. * Virtual sensors of this type will report the minimum of all
  749. * sensor temperatures.
  750. *
  751. * @input arguments:
  752. * @dev: Virtual sensor driver device pointer.
  753. * @sensor_data: Virtual sensor data supported for the device.
  754. *
  755. * @return: Returns a virtual thermal zone pointer. Returns error if thermal
  756. * zone is not created. Returns -EAGAIN, if the sensor that is required for
  757. * this virtual sensor temperature estimation is not registered yet. The
  758. * sensor driver can try again later.
  759. */
  760. struct thermal_zone_device *devm_thermal_of_virtual_sensor_register(
  761. struct device *dev,
  762. const struct virtual_sensor_data *sensor_data)
  763. {
  764. int sens_idx = 0;
  765. struct virtual_sensor *sens;
  766. struct __thermal_zone *tz;
  767. struct thermal_zone_device **ptr;
  768. struct thermal_zone_device *tzd;
  769. struct __sensor_param *sens_param = NULL;
  770. enum thermal_device_mode mode;
  771. if (!dev || !sensor_data)
  772. return ERR_PTR(-EINVAL);
  773. tzd = thermal_zone_get_zone_by_name(
  774. sensor_data->virt_zone_name);
  775. if (IS_ERR(tzd)) {
  776. dev_dbg(dev, "sens:%s not available err: %ld\n",
  777. sensor_data->virt_zone_name,
  778. PTR_ERR(tzd));
  779. return tzd;
  780. }
  781. mutex_lock(&tzd->lock);
  782. /*
  783. * Check if the virtual zone is registered and enabled.
  784. * If so return the registered thermal zone.
  785. */
  786. tzd->ops->get_mode(tzd, &mode);
  787. mutex_unlock(&tzd->lock);
  788. if (mode == THERMAL_DEVICE_ENABLED)
  789. return tzd;
  790. sens = devm_kzalloc(dev, sizeof(*sens), GFP_KERNEL);
  791. if (!sens)
  792. return ERR_PTR(-ENOMEM);
  793. sens->virt_tz = tzd;
  794. sens->logic = sensor_data->logic;
  795. sens->num_sensors = sensor_data->num_sensors;
  796. if (sens->logic == VIRT_WEIGHTED_AVG) {
  797. int coeff_ct = sensor_data->coefficient_ct;
  798. /*
  799. * For weighted aggregation, sensor drivers has to specify
  800. * n+2 coefficients.
  801. */
  802. if (coeff_ct != sens->num_sensors) {
  803. dev_err(dev, "sens:%s Invalid coefficient\n",
  804. sensor_data->virt_zone_name);
  805. return ERR_PTR(-EINVAL);
  806. }
  807. memcpy(sens->coefficients, sensor_data->coefficients,
  808. coeff_ct * sizeof(*sens->coefficients));
  809. sens->avg_offset = sensor_data->avg_offset;
  810. sens->avg_denominator = sensor_data->avg_denominator;
  811. }
  812. for (sens_idx = 0; sens_idx < sens->num_sensors; sens_idx++) {
  813. sens->tz[sens_idx] = thermal_zone_get_zone_by_name(
  814. sensor_data->sensor_names[sens_idx]);
  815. if (IS_ERR(sens->tz[sens_idx])) {
  816. dev_err(dev, "sens:%s sensor[%s] fetch err:%ld\n",
  817. sensor_data->virt_zone_name,
  818. sensor_data->sensor_names[sens_idx],
  819. PTR_ERR(sens->tz[sens_idx]));
  820. break;
  821. }
  822. }
  823. if (sens->num_sensors != sens_idx)
  824. return ERR_PTR(-EAGAIN);
  825. sens_param = kzalloc(sizeof(*sens_param), GFP_KERNEL);
  826. if (!sens_param)
  827. return ERR_PTR(-ENOMEM);
  828. sens_param->sensor_data = sens;
  829. sens_param->ops = &of_virt_ops;
  830. INIT_LIST_HEAD(&sens_param->first_tz);
  831. sens_param->trip_high = INT_MAX;
  832. sens_param->trip_low = INT_MIN;
  833. mutex_init(&sens_param->lock);
  834. mutex_lock(&tzd->lock);
  835. tz = tzd->devdata;
  836. tz->senps = sens_param;
  837. tzd->ops->get_temp = of_thermal_get_temp;
  838. list_add_tail(&tz->list, &sens_param->first_tz);
  839. mutex_unlock(&tzd->lock);
  840. ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
  841. GFP_KERNEL);
  842. if (!ptr)
  843. return ERR_PTR(-ENOMEM);
  844. *ptr = tzd;
  845. devres_add(dev, ptr);
  846. if (!tz->default_disable)
  847. tzd->ops->set_mode(tzd, THERMAL_DEVICE_ENABLED);
  848. return tzd;
  849. }
  850. EXPORT_SYMBOL(devm_thermal_of_virtual_sensor_register);
  851. /**
  852. * devm_thermal_zone_of_sensor_register - Resource managed version of
  853. * thermal_zone_of_sensor_register()
  854. * @dev: a valid struct device pointer of a sensor device. Must contain
  855. * a valid .of_node, for the sensor node.
  856. * @sensor_id: a sensor identifier, in case the sensor IP has more
  857. * than one sensors
  858. * @data: a private pointer (owned by the caller) that will be passed
  859. * back, when a temperature reading is needed.
  860. * @ops: struct thermal_zone_of_device_ops *. Must contain at least .get_temp.
  861. *
  862. * Refer thermal_zone_of_sensor_register() for more details.
  863. *
  864. * Return: On success returns a valid struct thermal_zone_device,
  865. * otherwise, it returns a corresponding ERR_PTR(). Caller must
  866. * check the return value with help of IS_ERR() helper.
  867. * Registered thermal_zone_device device will automatically be
  868. * released when device is unbounded.
  869. */
  870. struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
  871. struct device *dev, int sensor_id,
  872. void *data, const struct thermal_zone_of_device_ops *ops)
  873. {
  874. struct thermal_zone_device **ptr, *tzd;
  875. ptr = devres_alloc(devm_thermal_zone_of_sensor_release, sizeof(*ptr),
  876. GFP_KERNEL);
  877. if (!ptr)
  878. return ERR_PTR(-ENOMEM);
  879. tzd = thermal_zone_of_sensor_register(dev, sensor_id, data, ops);
  880. if (IS_ERR(tzd)) {
  881. devres_free(ptr);
  882. return tzd;
  883. }
  884. *ptr = tzd;
  885. devres_add(dev, ptr);
  886. return tzd;
  887. }
  888. EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_register);
  889. /**
  890. * devm_thermal_zone_of_sensor_unregister - Resource managed version of
  891. * thermal_zone_of_sensor_unregister().
  892. * @dev: Device for which which resource was allocated.
  893. * @tzd: a pointer to struct thermal_zone_device where the sensor is registered.
  894. *
  895. * This function removes the sensor callbacks and private data from the
  896. * thermal zone device registered with devm_thermal_zone_of_sensor_register()
  897. * API. It will also silent the zone by remove the .get_temp() and .get_trend()
  898. * thermal zone device callbacks.
  899. * Normally this function will not need to be called and the resource
  900. * management code will ensure that the resource is freed.
  901. */
  902. void devm_thermal_zone_of_sensor_unregister(struct device *dev,
  903. struct thermal_zone_device *tzd)
  904. {
  905. WARN_ON(devres_release(dev, devm_thermal_zone_of_sensor_release,
  906. devm_thermal_zone_of_sensor_match, tzd));
  907. }
  908. EXPORT_SYMBOL_GPL(devm_thermal_zone_of_sensor_unregister);
  909. /*** functions parsing device tree nodes ***/
  910. /**
  911. * thermal_of_populate_bind_params - parse and fill cooling map data
  912. * @np: DT node containing a cooling-map node
  913. * @__tbp: data structure to be filled with cooling map info
  914. * @trips: array of thermal zone trip points
  915. * @ntrips: number of trip points inside trips.
  916. *
  917. * This function parses a cooling-map type of node represented by
  918. * @np parameter and fills the read data into @__tbp data structure.
  919. * It needs the already parsed array of trip points of the thermal zone
  920. * in consideration.
  921. *
  922. * Return: 0 on success, proper error code otherwise
  923. */
  924. static int thermal_of_populate_bind_params(struct device_node *np,
  925. struct __thermal_bind_params *__tbp,
  926. struct thermal_trip *trips,
  927. int ntrips)
  928. {
  929. struct of_phandle_args cooling_spec;
  930. struct device_node *trip;
  931. int ret, i;
  932. u32 prop;
  933. /* Default weight. Usage is optional */
  934. __tbp->usage = THERMAL_WEIGHT_DEFAULT;
  935. ret = of_property_read_u32(np, "contribution", &prop);
  936. if (ret == 0)
  937. __tbp->usage = prop;
  938. trip = of_parse_phandle(np, "trip", 0);
  939. if (!trip) {
  940. pr_err("missing trip property\n");
  941. return -ENODEV;
  942. }
  943. /* match using device_node */
  944. for (i = 0; i < ntrips; i++)
  945. if (trip == trips[i].np) {
  946. __tbp->trip_id = i;
  947. break;
  948. }
  949. if (i == ntrips) {
  950. ret = -ENODEV;
  951. goto end;
  952. }
  953. ret = of_parse_phandle_with_args(np, "cooling-device", "#cooling-cells",
  954. 0, &cooling_spec);
  955. if (ret < 0) {
  956. pr_err("missing cooling_device property\n");
  957. goto end;
  958. }
  959. __tbp->cooling_device = cooling_spec.np;
  960. if (cooling_spec.args_count >= 2) { /* at least min and max */
  961. __tbp->min = cooling_spec.args[0];
  962. __tbp->max = cooling_spec.args[1];
  963. } else {
  964. pr_err("wrong reference to cooling device, missing limits\n");
  965. }
  966. end:
  967. of_node_put(trip);
  968. return ret;
  969. }
  970. /**
  971. * It maps 'enum thermal_trip_type' found in include/linux/thermal.h
  972. * into the device tree binding of 'trip', property type.
  973. */
  974. static const char * const trip_types[] = {
  975. [THERMAL_TRIP_ACTIVE] = "active",
  976. [THERMAL_TRIP_PASSIVE] = "passive",
  977. [THERMAL_TRIP_HOT] = "hot",
  978. [THERMAL_TRIP_CRITICAL] = "critical",
  979. };
  980. /**
  981. * thermal_of_get_trip_type - Get phy mode for given device_node
  982. * @np: Pointer to the given device_node
  983. * @type: Pointer to resulting trip type
  984. *
  985. * The function gets trip type string from property 'type',
  986. * and store its index in trip_types table in @type,
  987. *
  988. * Return: 0 on success, or errno in error case.
  989. */
  990. static int thermal_of_get_trip_type(struct device_node *np,
  991. enum thermal_trip_type *type)
  992. {
  993. const char *t;
  994. int err, i;
  995. err = of_property_read_string(np, "type", &t);
  996. if (err < 0)
  997. return err;
  998. for (i = 0; i < ARRAY_SIZE(trip_types); i++)
  999. if (!strcasecmp(t, trip_types[i])) {
  1000. *type = i;
  1001. return 0;
  1002. }
  1003. return -ENODEV;
  1004. }
  1005. /**
  1006. * thermal_of_populate_trip - parse and fill one trip point data
  1007. * @np: DT node containing a trip point node
  1008. * @trip: trip point data structure to be filled up
  1009. *
  1010. * This function parses a trip point type of node represented by
  1011. * @np parameter and fills the read data into @trip data structure.
  1012. *
  1013. * Return: 0 on success, proper error code otherwise
  1014. */
  1015. static int thermal_of_populate_trip(struct device_node *np,
  1016. struct thermal_trip *trip)
  1017. {
  1018. int prop;
  1019. int ret;
  1020. ret = of_property_read_u32(np, "temperature", &prop);
  1021. if (ret < 0) {
  1022. pr_err("missing temperature property\n");
  1023. return ret;
  1024. }
  1025. trip->temperature = prop;
  1026. ret = of_property_read_u32(np, "hysteresis", &prop);
  1027. if (ret < 0) {
  1028. pr_err("missing hysteresis property\n");
  1029. return ret;
  1030. }
  1031. trip->hysteresis = prop;
  1032. ret = thermal_of_get_trip_type(np, &trip->type);
  1033. if (ret < 0) {
  1034. pr_err("wrong trip type property\n");
  1035. return ret;
  1036. }
  1037. /* Required for cooling map matching */
  1038. trip->np = np;
  1039. of_node_get(np);
  1040. return 0;
  1041. }
  1042. /**
  1043. * thermal_of_build_thermal_zone - parse and fill one thermal zone data
  1044. * @np: DT node containing a thermal zone node
  1045. *
  1046. * This function parses a thermal zone type of node represented by
  1047. * @np parameter and fills the read data into a __thermal_zone data structure
  1048. * and return this pointer.
  1049. *
  1050. * TODO: Missing properties to parse: thermal-sensor-names
  1051. *
  1052. * Return: On success returns a valid struct __thermal_zone,
  1053. * otherwise, it returns a corresponding ERR_PTR(). Caller must
  1054. * check the return value with help of IS_ERR() helper.
  1055. */
  1056. static struct __thermal_zone
  1057. __init *thermal_of_build_thermal_zone(struct device_node *np)
  1058. {
  1059. struct device_node *child = NULL, *gchild;
  1060. struct __thermal_zone *tz;
  1061. int ret, i;
  1062. u32 prop, coef[2];
  1063. if (!np) {
  1064. pr_err("no thermal zone np\n");
  1065. return ERR_PTR(-EINVAL);
  1066. }
  1067. tz = kzalloc(sizeof(*tz), GFP_KERNEL);
  1068. if (!tz)
  1069. return ERR_PTR(-ENOMEM);
  1070. INIT_LIST_HEAD(&tz->list);
  1071. ret = of_property_read_u32(np, "polling-delay-passive", &prop);
  1072. if (ret < 0) {
  1073. pr_err("missing polling-delay-passive property\n");
  1074. goto free_tz;
  1075. }
  1076. tz->passive_delay = prop;
  1077. ret = of_property_read_u32(np, "polling-delay", &prop);
  1078. if (ret < 0) {
  1079. pr_err("missing polling-delay property\n");
  1080. goto free_tz;
  1081. }
  1082. tz->polling_delay = prop;
  1083. tz->default_disable = of_property_read_bool(np,
  1084. "disable-thermal-zone");
  1085. tz->is_wakeable = of_property_read_bool(np,
  1086. "wake-capable-sensor");
  1087. /*
  1088. * REVIST: for now, the thermal framework supports only
  1089. * one sensor per thermal zone. Thus, we are considering
  1090. * only the first two values as slope and offset.
  1091. */
  1092. ret = of_property_read_u32_array(np, "coefficients", coef, 2);
  1093. if (ret == 0) {
  1094. tz->slope = coef[0];
  1095. tz->offset = coef[1];
  1096. } else {
  1097. tz->slope = 1;
  1098. tz->offset = 0;
  1099. }
  1100. /* trips */
  1101. child = of_get_child_by_name(np, "trips");
  1102. /* No trips provided */
  1103. if (!child)
  1104. goto finish;
  1105. tz->ntrips = of_get_child_count(child);
  1106. if (tz->ntrips == 0) /* must have at least one child */
  1107. goto finish;
  1108. tz->trips = kzalloc(tz->ntrips * sizeof(*tz->trips), GFP_KERNEL);
  1109. if (!tz->trips) {
  1110. ret = -ENOMEM;
  1111. goto free_tz;
  1112. }
  1113. i = 0;
  1114. for_each_child_of_node(child, gchild) {
  1115. ret = thermal_of_populate_trip(gchild, &tz->trips[i++]);
  1116. if (ret)
  1117. goto free_trips;
  1118. }
  1119. of_node_put(child);
  1120. /* cooling-maps */
  1121. child = of_get_child_by_name(np, "cooling-maps");
  1122. /* cooling-maps not provided */
  1123. if (!child)
  1124. goto finish;
  1125. tz->num_tbps = of_get_child_count(child);
  1126. if (tz->num_tbps == 0)
  1127. goto finish;
  1128. tz->tbps = kzalloc(tz->num_tbps * sizeof(*tz->tbps), GFP_KERNEL);
  1129. if (!tz->tbps) {
  1130. ret = -ENOMEM;
  1131. goto free_trips;
  1132. }
  1133. i = 0;
  1134. for_each_child_of_node(child, gchild) {
  1135. ret = thermal_of_populate_bind_params(gchild, &tz->tbps[i++],
  1136. tz->trips, tz->ntrips);
  1137. if (ret)
  1138. goto free_tbps;
  1139. }
  1140. finish:
  1141. of_node_put(child);
  1142. tz->mode = THERMAL_DEVICE_DISABLED;
  1143. return tz;
  1144. free_tbps:
  1145. for (i = i - 1; i >= 0; i--)
  1146. of_node_put(tz->tbps[i].cooling_device);
  1147. kfree(tz->tbps);
  1148. free_trips:
  1149. for (i = 0; i < tz->ntrips; i++)
  1150. of_node_put(tz->trips[i].np);
  1151. kfree(tz->trips);
  1152. of_node_put(gchild);
  1153. free_tz:
  1154. kfree(tz);
  1155. of_node_put(child);
  1156. return ERR_PTR(ret);
  1157. }
  1158. static inline void of_thermal_free_zone(struct __thermal_zone *tz)
  1159. {
  1160. int i;
  1161. for (i = 0; i < tz->num_tbps; i++)
  1162. of_node_put(tz->tbps[i].cooling_device);
  1163. kfree(tz->tbps);
  1164. for (i = 0; i < tz->ntrips; i++)
  1165. of_node_put(tz->trips[i].np);
  1166. kfree(tz->trips);
  1167. kfree(tz);
  1168. }
  1169. /**
  1170. * of_parse_thermal_zones - parse device tree thermal data
  1171. *
  1172. * Initialization function that can be called by machine initialization
  1173. * code to parse thermal data and populate the thermal framework
  1174. * with hardware thermal zones info. This function only parses thermal zones.
  1175. * Cooling devices and sensor devices nodes are supposed to be parsed
  1176. * by their respective drivers.
  1177. *
  1178. * Return: 0 on success, proper error code otherwise
  1179. *
  1180. */
  1181. int __init of_parse_thermal_zones(void)
  1182. {
  1183. struct device_node *np, *child;
  1184. struct __thermal_zone *tz;
  1185. struct thermal_zone_device_ops *ops;
  1186. np = of_find_node_by_name(NULL, "thermal-zones");
  1187. if (!np) {
  1188. pr_debug("unable to find thermal zones\n");
  1189. return 0; /* Run successfully on systems without thermal DT */
  1190. }
  1191. for_each_available_child_of_node(np, child) {
  1192. struct thermal_zone_device *zone;
  1193. struct thermal_zone_params *tzp;
  1194. int i, mask = 0;
  1195. u32 prop;
  1196. const char *governor_name;
  1197. tz = thermal_of_build_thermal_zone(child);
  1198. if (IS_ERR(tz)) {
  1199. pr_err("failed to build thermal zone %s: %ld\n",
  1200. child->name,
  1201. PTR_ERR(tz));
  1202. continue;
  1203. }
  1204. ops = kmemdup(&of_thermal_ops, sizeof(*ops), GFP_KERNEL);
  1205. if (!ops)
  1206. goto exit_free;
  1207. tzp = kzalloc(sizeof(*tzp), GFP_KERNEL);
  1208. if (!tzp) {
  1209. kfree(ops);
  1210. goto exit_free;
  1211. }
  1212. /* No hwmon because there might be hwmon drivers registering */
  1213. tzp->no_hwmon = true;
  1214. if (!of_property_read_string(child, "thermal-governor",
  1215. &governor_name))
  1216. strlcpy(tzp->governor_name, governor_name,
  1217. THERMAL_NAME_LENGTH);
  1218. if (!of_property_read_u32(child, "sustainable-power", &prop))
  1219. tzp->sustainable_power = prop;
  1220. for (i = 0; i < tz->ntrips; i++)
  1221. mask |= 1 << i;
  1222. /* these two are left for temperature drivers to use */
  1223. tzp->slope = tz->slope;
  1224. tzp->offset = tz->offset;
  1225. if (of_property_read_bool(child, "tracks-low"))
  1226. tzp->tracks_low = true;
  1227. zone = thermal_zone_device_register(child->name, tz->ntrips,
  1228. mask, tz,
  1229. ops, tzp,
  1230. tz->passive_delay,
  1231. tz->polling_delay);
  1232. if (IS_ERR(zone)) {
  1233. pr_err("Failed to build %s zone %ld\n", child->name,
  1234. PTR_ERR(zone));
  1235. kfree(tzp);
  1236. kfree(ops);
  1237. of_thermal_free_zone(tz);
  1238. /* attempting to build remaining zones still */
  1239. continue;
  1240. }
  1241. tz->tzd = zone;
  1242. }
  1243. of_node_put(np);
  1244. return 0;
  1245. exit_free:
  1246. of_node_put(child);
  1247. of_node_put(np);
  1248. of_thermal_free_zone(tz);
  1249. /* no memory available, so free what we have built */
  1250. of_thermal_destroy_zones();
  1251. return -ENOMEM;
  1252. }
  1253. /**
  1254. * of_thermal_destroy_zones - remove all zones parsed and allocated resources
  1255. *
  1256. * Finds all zones parsed and added to the thermal framework and remove them
  1257. * from the system, together with their resources.
  1258. *
  1259. */
  1260. void of_thermal_destroy_zones(void)
  1261. {
  1262. struct device_node *np, *child;
  1263. np = of_find_node_by_name(NULL, "thermal-zones");
  1264. if (!np) {
  1265. pr_debug("unable to find thermal zones\n");
  1266. return;
  1267. }
  1268. for_each_available_child_of_node(np, child) {
  1269. struct thermal_zone_device *zone;
  1270. zone = thermal_zone_get_zone_by_name(child->name);
  1271. if (IS_ERR(zone))
  1272. continue;
  1273. thermal_zone_device_unregister(zone);
  1274. kfree(zone->tzp);
  1275. kfree(zone->ops);
  1276. of_thermal_free_zone(zone->devdata);
  1277. }
  1278. of_node_put(np);
  1279. }