thermal_sys.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /*
  2. * thermal.c - Generic Thermal Management Sysfs support.
  3. *
  4. * Copyright (C) 2008 Intel Corp
  5. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  6. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/kdev_t.h>
  31. #include <linux/idr.h>
  32. #include <linux/thermal.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/reboot.h>
  35. #include <net/netlink.h>
  36. #include <net/genetlink.h>
  37. #include "thermal_core.h"
  38. MODULE_AUTHOR("Zhang Rui");
  39. MODULE_DESCRIPTION("Generic thermal management sysfs support");
  40. MODULE_LICENSE("GPL");
  41. static DEFINE_IDR(thermal_tz_idr);
  42. static DEFINE_IDR(thermal_cdev_idr);
  43. static DEFINE_MUTEX(thermal_idr_lock);
  44. static LIST_HEAD(thermal_tz_list);
  45. static LIST_HEAD(thermal_cdev_list);
  46. static LIST_HEAD(thermal_governor_list);
  47. static DEFINE_MUTEX(thermal_list_lock);
  48. static DEFINE_MUTEX(thermal_governor_lock);
  49. static struct thermal_governor *__find_governor(const char *name)
  50. {
  51. struct thermal_governor *pos;
  52. list_for_each_entry(pos, &thermal_governor_list, governor_list)
  53. if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
  54. return pos;
  55. return NULL;
  56. }
  57. int thermal_register_governor(struct thermal_governor *governor)
  58. {
  59. int err;
  60. const char *name;
  61. struct thermal_zone_device *pos;
  62. if (!governor)
  63. return -EINVAL;
  64. mutex_lock(&thermal_governor_lock);
  65. err = -EBUSY;
  66. if (__find_governor(governor->name) == NULL) {
  67. err = 0;
  68. list_add(&governor->governor_list, &thermal_governor_list);
  69. }
  70. mutex_lock(&thermal_list_lock);
  71. list_for_each_entry(pos, &thermal_tz_list, node) {
  72. if (pos->governor)
  73. continue;
  74. if (pos->tzp)
  75. name = pos->tzp->governor_name;
  76. else
  77. name = DEFAULT_THERMAL_GOVERNOR;
  78. if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
  79. pos->governor = governor;
  80. }
  81. mutex_unlock(&thermal_list_lock);
  82. mutex_unlock(&thermal_governor_lock);
  83. return err;
  84. }
  85. EXPORT_SYMBOL_GPL(thermal_register_governor);
  86. void thermal_unregister_governor(struct thermal_governor *governor)
  87. {
  88. struct thermal_zone_device *pos;
  89. if (!governor)
  90. return;
  91. mutex_lock(&thermal_governor_lock);
  92. if (__find_governor(governor->name) == NULL)
  93. goto exit;
  94. mutex_lock(&thermal_list_lock);
  95. list_for_each_entry(pos, &thermal_tz_list, node) {
  96. if (!strnicmp(pos->governor->name, governor->name,
  97. THERMAL_NAME_LENGTH))
  98. pos->governor = NULL;
  99. }
  100. mutex_unlock(&thermal_list_lock);
  101. list_del(&governor->governor_list);
  102. exit:
  103. mutex_unlock(&thermal_governor_lock);
  104. return;
  105. }
  106. EXPORT_SYMBOL_GPL(thermal_unregister_governor);
  107. static int get_idr(struct idr *idr, struct mutex *lock, int *id)
  108. {
  109. int err;
  110. again:
  111. if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
  112. return -ENOMEM;
  113. if (lock)
  114. mutex_lock(lock);
  115. err = idr_get_new(idr, NULL, id);
  116. if (lock)
  117. mutex_unlock(lock);
  118. if (unlikely(err == -EAGAIN))
  119. goto again;
  120. else if (unlikely(err))
  121. return err;
  122. *id = *id & MAX_IDR_MASK;
  123. return 0;
  124. }
  125. static void release_idr(struct idr *idr, struct mutex *lock, int id)
  126. {
  127. if (lock)
  128. mutex_lock(lock);
  129. idr_remove(idr, id);
  130. if (lock)
  131. mutex_unlock(lock);
  132. }
  133. int get_tz_trend(struct thermal_zone_device *tz, int trip)
  134. {
  135. enum thermal_trend trend;
  136. if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
  137. if (tz->temperature > tz->last_temperature)
  138. trend = THERMAL_TREND_RAISING;
  139. else if (tz->temperature < tz->last_temperature)
  140. trend = THERMAL_TREND_DROPPING;
  141. else
  142. trend = THERMAL_TREND_STABLE;
  143. }
  144. return trend;
  145. }
  146. EXPORT_SYMBOL(get_tz_trend);
  147. struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
  148. struct thermal_cooling_device *cdev, int trip)
  149. {
  150. struct thermal_instance *pos = NULL;
  151. struct thermal_instance *target_instance = NULL;
  152. mutex_lock(&tz->lock);
  153. mutex_lock(&cdev->lock);
  154. list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
  155. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  156. target_instance = pos;
  157. break;
  158. }
  159. }
  160. mutex_unlock(&cdev->lock);
  161. mutex_unlock(&tz->lock);
  162. return target_instance;
  163. }
  164. EXPORT_SYMBOL(get_thermal_instance);
  165. static void print_bind_err_msg(struct thermal_zone_device *tz,
  166. struct thermal_cooling_device *cdev, int ret)
  167. {
  168. dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
  169. tz->type, cdev->type, ret);
  170. }
  171. static void __bind(struct thermal_zone_device *tz, int mask,
  172. struct thermal_cooling_device *cdev)
  173. {
  174. int i, ret;
  175. for (i = 0; i < tz->trips; i++) {
  176. if (mask & (1 << i)) {
  177. ret = thermal_zone_bind_cooling_device(tz, i, cdev,
  178. THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
  179. if (ret)
  180. print_bind_err_msg(tz, cdev, ret);
  181. }
  182. }
  183. }
  184. static void __unbind(struct thermal_zone_device *tz, int mask,
  185. struct thermal_cooling_device *cdev)
  186. {
  187. int i;
  188. for (i = 0; i < tz->trips; i++)
  189. if (mask & (1 << i))
  190. thermal_zone_unbind_cooling_device(tz, i, cdev);
  191. }
  192. static void bind_cdev(struct thermal_cooling_device *cdev)
  193. {
  194. int i, ret;
  195. const struct thermal_zone_params *tzp;
  196. struct thermal_zone_device *pos = NULL;
  197. mutex_lock(&thermal_list_lock);
  198. list_for_each_entry(pos, &thermal_tz_list, node) {
  199. if (!pos->tzp && !pos->ops->bind)
  200. continue;
  201. if (!pos->tzp && pos->ops->bind) {
  202. ret = pos->ops->bind(pos, cdev);
  203. if (ret)
  204. print_bind_err_msg(pos, cdev, ret);
  205. }
  206. tzp = pos->tzp;
  207. if (!tzp->tbp)
  208. return;
  209. for (i = 0; i < tzp->num_tbps; i++) {
  210. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  211. continue;
  212. if (tzp->tbp[i].match(pos, cdev))
  213. continue;
  214. tzp->tbp[i].cdev = cdev;
  215. __bind(pos, tzp->tbp[i].trip_mask, cdev);
  216. }
  217. }
  218. mutex_unlock(&thermal_list_lock);
  219. }
  220. static void bind_tz(struct thermal_zone_device *tz)
  221. {
  222. int i, ret;
  223. struct thermal_cooling_device *pos = NULL;
  224. const struct thermal_zone_params *tzp = tz->tzp;
  225. if (!tzp && !tz->ops->bind)
  226. return;
  227. mutex_lock(&thermal_list_lock);
  228. /* If there is no platform data, try to use ops->bind */
  229. if (!tzp && tz->ops->bind) {
  230. list_for_each_entry(pos, &thermal_cdev_list, node) {
  231. ret = tz->ops->bind(tz, pos);
  232. if (ret)
  233. print_bind_err_msg(tz, pos, ret);
  234. }
  235. goto exit;
  236. }
  237. if (!tzp->tbp)
  238. goto exit;
  239. list_for_each_entry(pos, &thermal_cdev_list, node) {
  240. for (i = 0; i < tzp->num_tbps; i++) {
  241. if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
  242. continue;
  243. if (tzp->tbp[i].match(tz, pos))
  244. continue;
  245. tzp->tbp[i].cdev = pos;
  246. __bind(tz, tzp->tbp[i].trip_mask, pos);
  247. }
  248. }
  249. exit:
  250. mutex_unlock(&thermal_list_lock);
  251. }
  252. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  253. int delay)
  254. {
  255. if (delay > 1000)
  256. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  257. round_jiffies(msecs_to_jiffies(delay)));
  258. else if (delay)
  259. mod_delayed_work(system_freezable_wq, &tz->poll_queue,
  260. msecs_to_jiffies(delay));
  261. else
  262. cancel_delayed_work(&tz->poll_queue);
  263. }
  264. static void monitor_thermal_zone(struct thermal_zone_device *tz)
  265. {
  266. mutex_lock(&tz->lock);
  267. if (tz->passive)
  268. thermal_zone_device_set_polling(tz, tz->passive_delay);
  269. else if (tz->polling_delay)
  270. thermal_zone_device_set_polling(tz, tz->polling_delay);
  271. else
  272. thermal_zone_device_set_polling(tz, 0);
  273. mutex_unlock(&tz->lock);
  274. }
  275. static void handle_non_critical_trips(struct thermal_zone_device *tz,
  276. int trip, enum thermal_trip_type trip_type)
  277. {
  278. tz->governor->throttle(tz, trip);
  279. }
  280. static void handle_critical_trips(struct thermal_zone_device *tz,
  281. int trip, enum thermal_trip_type trip_type)
  282. {
  283. long trip_temp;
  284. tz->ops->get_trip_temp(tz, trip, &trip_temp);
  285. /* If we have not crossed the trip_temp, we do not care. */
  286. if (tz->temperature < trip_temp)
  287. return;
  288. if (tz->ops->notify)
  289. tz->ops->notify(tz, trip, trip_type);
  290. if (trip_type == THERMAL_TRIP_CRITICAL) {
  291. pr_emerg("Critical temperature reached(%d C),shutting down\n",
  292. tz->temperature / 1000);
  293. orderly_poweroff(true);
  294. }
  295. }
  296. static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
  297. {
  298. enum thermal_trip_type type;
  299. tz->ops->get_trip_type(tz, trip, &type);
  300. if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
  301. handle_critical_trips(tz, trip, type);
  302. else
  303. handle_non_critical_trips(tz, trip, type);
  304. /*
  305. * Alright, we handled this trip successfully.
  306. * So, start monitoring again.
  307. */
  308. monitor_thermal_zone(tz);
  309. }
  310. static void update_temperature(struct thermal_zone_device *tz)
  311. {
  312. long temp;
  313. int ret;
  314. mutex_lock(&tz->lock);
  315. ret = tz->ops->get_temp(tz, &temp);
  316. if (ret) {
  317. pr_warn("failed to read out thermal zone %d\n", tz->id);
  318. return;
  319. }
  320. tz->last_temperature = tz->temperature;
  321. tz->temperature = temp;
  322. mutex_unlock(&tz->lock);
  323. }
  324. void thermal_zone_device_update(struct thermal_zone_device *tz)
  325. {
  326. int count;
  327. update_temperature(tz);
  328. for (count = 0; count < tz->trips; count++)
  329. handle_thermal_trip(tz, count);
  330. }
  331. EXPORT_SYMBOL(thermal_zone_device_update);
  332. static void thermal_zone_device_check(struct work_struct *work)
  333. {
  334. struct thermal_zone_device *tz = container_of(work, struct
  335. thermal_zone_device,
  336. poll_queue.work);
  337. thermal_zone_device_update(tz);
  338. }
  339. /* sys I/F for thermal zone */
  340. #define to_thermal_zone(_dev) \
  341. container_of(_dev, struct thermal_zone_device, device)
  342. static ssize_t
  343. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  344. {
  345. struct thermal_zone_device *tz = to_thermal_zone(dev);
  346. return sprintf(buf, "%s\n", tz->type);
  347. }
  348. static ssize_t
  349. temp_show(struct device *dev, struct device_attribute *attr, char *buf)
  350. {
  351. struct thermal_zone_device *tz = to_thermal_zone(dev);
  352. long temperature;
  353. int ret;
  354. if (!tz->ops->get_temp)
  355. return -EPERM;
  356. ret = tz->ops->get_temp(tz, &temperature);
  357. if (ret)
  358. return ret;
  359. return sprintf(buf, "%ld\n", temperature);
  360. }
  361. static ssize_t
  362. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  363. {
  364. struct thermal_zone_device *tz = to_thermal_zone(dev);
  365. enum thermal_device_mode mode;
  366. int result;
  367. if (!tz->ops->get_mode)
  368. return -EPERM;
  369. result = tz->ops->get_mode(tz, &mode);
  370. if (result)
  371. return result;
  372. return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
  373. : "disabled");
  374. }
  375. static ssize_t
  376. mode_store(struct device *dev, struct device_attribute *attr,
  377. const char *buf, size_t count)
  378. {
  379. struct thermal_zone_device *tz = to_thermal_zone(dev);
  380. int result;
  381. if (!tz->ops->set_mode)
  382. return -EPERM;
  383. if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
  384. result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
  385. else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
  386. result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
  387. else
  388. result = -EINVAL;
  389. if (result)
  390. return result;
  391. return count;
  392. }
  393. static ssize_t
  394. trip_point_type_show(struct device *dev, struct device_attribute *attr,
  395. char *buf)
  396. {
  397. struct thermal_zone_device *tz = to_thermal_zone(dev);
  398. enum thermal_trip_type type;
  399. int trip, result;
  400. if (!tz->ops->get_trip_type)
  401. return -EPERM;
  402. if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
  403. return -EINVAL;
  404. result = tz->ops->get_trip_type(tz, trip, &type);
  405. if (result)
  406. return result;
  407. switch (type) {
  408. case THERMAL_TRIP_CRITICAL:
  409. return sprintf(buf, "critical\n");
  410. case THERMAL_TRIP_HOT:
  411. return sprintf(buf, "hot\n");
  412. case THERMAL_TRIP_PASSIVE:
  413. return sprintf(buf, "passive\n");
  414. case THERMAL_TRIP_ACTIVE:
  415. return sprintf(buf, "active\n");
  416. default:
  417. return sprintf(buf, "unknown\n");
  418. }
  419. }
  420. static ssize_t
  421. trip_point_temp_store(struct device *dev, struct device_attribute *attr,
  422. const char *buf, size_t count)
  423. {
  424. struct thermal_zone_device *tz = to_thermal_zone(dev);
  425. int trip, ret;
  426. unsigned long temperature;
  427. if (!tz->ops->set_trip_temp)
  428. return -EPERM;
  429. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  430. return -EINVAL;
  431. if (kstrtoul(buf, 10, &temperature))
  432. return -EINVAL;
  433. ret = tz->ops->set_trip_temp(tz, trip, temperature);
  434. return ret ? ret : count;
  435. }
  436. static ssize_t
  437. trip_point_temp_show(struct device *dev, struct device_attribute *attr,
  438. char *buf)
  439. {
  440. struct thermal_zone_device *tz = to_thermal_zone(dev);
  441. int trip, ret;
  442. long temperature;
  443. if (!tz->ops->get_trip_temp)
  444. return -EPERM;
  445. if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
  446. return -EINVAL;
  447. ret = tz->ops->get_trip_temp(tz, trip, &temperature);
  448. if (ret)
  449. return ret;
  450. return sprintf(buf, "%ld\n", temperature);
  451. }
  452. static ssize_t
  453. trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
  454. const char *buf, size_t count)
  455. {
  456. struct thermal_zone_device *tz = to_thermal_zone(dev);
  457. int trip, ret;
  458. unsigned long temperature;
  459. if (!tz->ops->set_trip_hyst)
  460. return -EPERM;
  461. if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
  462. return -EINVAL;
  463. if (kstrtoul(buf, 10, &temperature))
  464. return -EINVAL;
  465. /*
  466. * We are not doing any check on the 'temperature' value
  467. * here. The driver implementing 'set_trip_hyst' has to
  468. * take care of this.
  469. */
  470. ret = tz->ops->set_trip_hyst(tz, trip, temperature);
  471. return ret ? ret : count;
  472. }
  473. static ssize_t
  474. trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
  475. char *buf)
  476. {
  477. struct thermal_zone_device *tz = to_thermal_zone(dev);
  478. int trip, ret;
  479. unsigned long temperature;
  480. if (!tz->ops->get_trip_hyst)
  481. return -EPERM;
  482. if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
  483. return -EINVAL;
  484. ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
  485. return ret ? ret : sprintf(buf, "%ld\n", temperature);
  486. }
  487. static ssize_t
  488. passive_store(struct device *dev, struct device_attribute *attr,
  489. const char *buf, size_t count)
  490. {
  491. struct thermal_zone_device *tz = to_thermal_zone(dev);
  492. struct thermal_cooling_device *cdev = NULL;
  493. int state;
  494. if (!sscanf(buf, "%d\n", &state))
  495. return -EINVAL;
  496. /* sanity check: values below 1000 millicelcius don't make sense
  497. * and can cause the system to go into a thermal heart attack
  498. */
  499. if (state && state < 1000)
  500. return -EINVAL;
  501. if (state && !tz->forced_passive) {
  502. mutex_lock(&thermal_list_lock);
  503. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  504. if (!strncmp("Processor", cdev->type,
  505. sizeof("Processor")))
  506. thermal_zone_bind_cooling_device(tz,
  507. THERMAL_TRIPS_NONE, cdev,
  508. THERMAL_NO_LIMIT,
  509. THERMAL_NO_LIMIT);
  510. }
  511. mutex_unlock(&thermal_list_lock);
  512. if (!tz->passive_delay)
  513. tz->passive_delay = 1000;
  514. } else if (!state && tz->forced_passive) {
  515. mutex_lock(&thermal_list_lock);
  516. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  517. if (!strncmp("Processor", cdev->type,
  518. sizeof("Processor")))
  519. thermal_zone_unbind_cooling_device(tz,
  520. THERMAL_TRIPS_NONE,
  521. cdev);
  522. }
  523. mutex_unlock(&thermal_list_lock);
  524. tz->passive_delay = 0;
  525. }
  526. tz->forced_passive = state;
  527. thermal_zone_device_update(tz);
  528. return count;
  529. }
  530. static ssize_t
  531. passive_show(struct device *dev, struct device_attribute *attr,
  532. char *buf)
  533. {
  534. struct thermal_zone_device *tz = to_thermal_zone(dev);
  535. return sprintf(buf, "%d\n", tz->forced_passive);
  536. }
  537. static ssize_t
  538. policy_store(struct device *dev, struct device_attribute *attr,
  539. const char *buf, size_t count)
  540. {
  541. int ret = -EINVAL;
  542. struct thermal_zone_device *tz = to_thermal_zone(dev);
  543. struct thermal_governor *gov;
  544. mutex_lock(&thermal_governor_lock);
  545. gov = __find_governor(buf);
  546. if (!gov)
  547. goto exit;
  548. tz->governor = gov;
  549. ret = count;
  550. exit:
  551. mutex_unlock(&thermal_governor_lock);
  552. return ret;
  553. }
  554. static ssize_t
  555. policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
  556. {
  557. struct thermal_zone_device *tz = to_thermal_zone(dev);
  558. return sprintf(buf, "%s\n", tz->governor->name);
  559. }
  560. static DEVICE_ATTR(type, 0444, type_show, NULL);
  561. static DEVICE_ATTR(temp, 0444, temp_show, NULL);
  562. static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
  563. static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
  564. static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
  565. /* sys I/F for cooling device */
  566. #define to_cooling_device(_dev) \
  567. container_of(_dev, struct thermal_cooling_device, device)
  568. static ssize_t
  569. thermal_cooling_device_type_show(struct device *dev,
  570. struct device_attribute *attr, char *buf)
  571. {
  572. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  573. return sprintf(buf, "%s\n", cdev->type);
  574. }
  575. static ssize_t
  576. thermal_cooling_device_max_state_show(struct device *dev,
  577. struct device_attribute *attr, char *buf)
  578. {
  579. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  580. unsigned long state;
  581. int ret;
  582. ret = cdev->ops->get_max_state(cdev, &state);
  583. if (ret)
  584. return ret;
  585. return sprintf(buf, "%ld\n", state);
  586. }
  587. static ssize_t
  588. thermal_cooling_device_cur_state_show(struct device *dev,
  589. struct device_attribute *attr, char *buf)
  590. {
  591. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  592. unsigned long state;
  593. int ret;
  594. ret = cdev->ops->get_cur_state(cdev, &state);
  595. if (ret)
  596. return ret;
  597. return sprintf(buf, "%ld\n", state);
  598. }
  599. static ssize_t
  600. thermal_cooling_device_cur_state_store(struct device *dev,
  601. struct device_attribute *attr,
  602. const char *buf, size_t count)
  603. {
  604. struct thermal_cooling_device *cdev = to_cooling_device(dev);
  605. unsigned long state;
  606. int result;
  607. if (!sscanf(buf, "%ld\n", &state))
  608. return -EINVAL;
  609. if ((long)state < 0)
  610. return -EINVAL;
  611. result = cdev->ops->set_cur_state(cdev, state);
  612. if (result)
  613. return result;
  614. return count;
  615. }
  616. static struct device_attribute dev_attr_cdev_type =
  617. __ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
  618. static DEVICE_ATTR(max_state, 0444,
  619. thermal_cooling_device_max_state_show, NULL);
  620. static DEVICE_ATTR(cur_state, 0644,
  621. thermal_cooling_device_cur_state_show,
  622. thermal_cooling_device_cur_state_store);
  623. static ssize_t
  624. thermal_cooling_device_trip_point_show(struct device *dev,
  625. struct device_attribute *attr, char *buf)
  626. {
  627. struct thermal_instance *instance;
  628. instance =
  629. container_of(attr, struct thermal_instance, attr);
  630. if (instance->trip == THERMAL_TRIPS_NONE)
  631. return sprintf(buf, "-1\n");
  632. else
  633. return sprintf(buf, "%d\n", instance->trip);
  634. }
  635. /* Device management */
  636. #if defined(CONFIG_THERMAL_HWMON)
  637. /* hwmon sys I/F */
  638. #include <linux/hwmon.h>
  639. /* thermal zone devices with the same type share one hwmon device */
  640. struct thermal_hwmon_device {
  641. char type[THERMAL_NAME_LENGTH];
  642. struct device *device;
  643. int count;
  644. struct list_head tz_list;
  645. struct list_head node;
  646. };
  647. struct thermal_hwmon_attr {
  648. struct device_attribute attr;
  649. char name[16];
  650. };
  651. /* one temperature input for each thermal zone */
  652. struct thermal_hwmon_temp {
  653. struct list_head hwmon_node;
  654. struct thermal_zone_device *tz;
  655. struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
  656. struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
  657. };
  658. static LIST_HEAD(thermal_hwmon_list);
  659. static ssize_t
  660. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  661. {
  662. struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
  663. return sprintf(buf, "%s\n", hwmon->type);
  664. }
  665. static DEVICE_ATTR(name, 0444, name_show, NULL);
  666. static ssize_t
  667. temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
  668. {
  669. long temperature;
  670. int ret;
  671. struct thermal_hwmon_attr *hwmon_attr
  672. = container_of(attr, struct thermal_hwmon_attr, attr);
  673. struct thermal_hwmon_temp *temp
  674. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  675. temp_input);
  676. struct thermal_zone_device *tz = temp->tz;
  677. ret = tz->ops->get_temp(tz, &temperature);
  678. if (ret)
  679. return ret;
  680. return sprintf(buf, "%ld\n", temperature);
  681. }
  682. static ssize_t
  683. temp_crit_show(struct device *dev, struct device_attribute *attr,
  684. char *buf)
  685. {
  686. struct thermal_hwmon_attr *hwmon_attr
  687. = container_of(attr, struct thermal_hwmon_attr, attr);
  688. struct thermal_hwmon_temp *temp
  689. = container_of(hwmon_attr, struct thermal_hwmon_temp,
  690. temp_crit);
  691. struct thermal_zone_device *tz = temp->tz;
  692. long temperature;
  693. int ret;
  694. ret = tz->ops->get_trip_temp(tz, 0, &temperature);
  695. if (ret)
  696. return ret;
  697. return sprintf(buf, "%ld\n", temperature);
  698. }
  699. static struct thermal_hwmon_device *
  700. thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
  701. {
  702. struct thermal_hwmon_device *hwmon;
  703. mutex_lock(&thermal_list_lock);
  704. list_for_each_entry(hwmon, &thermal_hwmon_list, node)
  705. if (!strcmp(hwmon->type, tz->type)) {
  706. mutex_unlock(&thermal_list_lock);
  707. return hwmon;
  708. }
  709. mutex_unlock(&thermal_list_lock);
  710. return NULL;
  711. }
  712. /* Find the temperature input matching a given thermal zone */
  713. static struct thermal_hwmon_temp *
  714. thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
  715. const struct thermal_zone_device *tz)
  716. {
  717. struct thermal_hwmon_temp *temp;
  718. mutex_lock(&thermal_list_lock);
  719. list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
  720. if (temp->tz == tz) {
  721. mutex_unlock(&thermal_list_lock);
  722. return temp;
  723. }
  724. mutex_unlock(&thermal_list_lock);
  725. return NULL;
  726. }
  727. static int
  728. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  729. {
  730. struct thermal_hwmon_device *hwmon;
  731. struct thermal_hwmon_temp *temp;
  732. int new_hwmon_device = 1;
  733. int result;
  734. hwmon = thermal_hwmon_lookup_by_type(tz);
  735. if (hwmon) {
  736. new_hwmon_device = 0;
  737. goto register_sys_interface;
  738. }
  739. hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
  740. if (!hwmon)
  741. return -ENOMEM;
  742. INIT_LIST_HEAD(&hwmon->tz_list);
  743. strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
  744. hwmon->device = hwmon_device_register(NULL);
  745. if (IS_ERR(hwmon->device)) {
  746. result = PTR_ERR(hwmon->device);
  747. goto free_mem;
  748. }
  749. dev_set_drvdata(hwmon->device, hwmon);
  750. result = device_create_file(hwmon->device, &dev_attr_name);
  751. if (result)
  752. goto free_mem;
  753. register_sys_interface:
  754. temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
  755. if (!temp) {
  756. result = -ENOMEM;
  757. goto unregister_name;
  758. }
  759. temp->tz = tz;
  760. hwmon->count++;
  761. snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
  762. "temp%d_input", hwmon->count);
  763. temp->temp_input.attr.attr.name = temp->temp_input.name;
  764. temp->temp_input.attr.attr.mode = 0444;
  765. temp->temp_input.attr.show = temp_input_show;
  766. sysfs_attr_init(&temp->temp_input.attr.attr);
  767. result = device_create_file(hwmon->device, &temp->temp_input.attr);
  768. if (result)
  769. goto free_temp_mem;
  770. if (tz->ops->get_crit_temp) {
  771. unsigned long temperature;
  772. if (!tz->ops->get_crit_temp(tz, &temperature)) {
  773. snprintf(temp->temp_crit.name,
  774. sizeof(temp->temp_crit.name),
  775. "temp%d_crit", hwmon->count);
  776. temp->temp_crit.attr.attr.name = temp->temp_crit.name;
  777. temp->temp_crit.attr.attr.mode = 0444;
  778. temp->temp_crit.attr.show = temp_crit_show;
  779. sysfs_attr_init(&temp->temp_crit.attr.attr);
  780. result = device_create_file(hwmon->device,
  781. &temp->temp_crit.attr);
  782. if (result)
  783. goto unregister_input;
  784. }
  785. }
  786. mutex_lock(&thermal_list_lock);
  787. if (new_hwmon_device)
  788. list_add_tail(&hwmon->node, &thermal_hwmon_list);
  789. list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
  790. mutex_unlock(&thermal_list_lock);
  791. return 0;
  792. unregister_input:
  793. device_remove_file(hwmon->device, &temp->temp_input.attr);
  794. free_temp_mem:
  795. kfree(temp);
  796. unregister_name:
  797. if (new_hwmon_device) {
  798. device_remove_file(hwmon->device, &dev_attr_name);
  799. hwmon_device_unregister(hwmon->device);
  800. }
  801. free_mem:
  802. if (new_hwmon_device)
  803. kfree(hwmon);
  804. return result;
  805. }
  806. static void
  807. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  808. {
  809. struct thermal_hwmon_device *hwmon;
  810. struct thermal_hwmon_temp *temp;
  811. hwmon = thermal_hwmon_lookup_by_type(tz);
  812. if (unlikely(!hwmon)) {
  813. /* Should never happen... */
  814. dev_dbg(&tz->device, "hwmon device lookup failed!\n");
  815. return;
  816. }
  817. temp = thermal_hwmon_lookup_temp(hwmon, tz);
  818. if (unlikely(!temp)) {
  819. /* Should never happen... */
  820. dev_dbg(&tz->device, "temperature input lookup failed!\n");
  821. return;
  822. }
  823. device_remove_file(hwmon->device, &temp->temp_input.attr);
  824. if (tz->ops->get_crit_temp)
  825. device_remove_file(hwmon->device, &temp->temp_crit.attr);
  826. mutex_lock(&thermal_list_lock);
  827. list_del(&temp->hwmon_node);
  828. kfree(temp);
  829. if (!list_empty(&hwmon->tz_list)) {
  830. mutex_unlock(&thermal_list_lock);
  831. return;
  832. }
  833. list_del(&hwmon->node);
  834. mutex_unlock(&thermal_list_lock);
  835. device_remove_file(hwmon->device, &dev_attr_name);
  836. hwmon_device_unregister(hwmon->device);
  837. kfree(hwmon);
  838. }
  839. #else
  840. static int
  841. thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
  842. {
  843. return 0;
  844. }
  845. static void
  846. thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
  847. {
  848. }
  849. #endif
  850. /**
  851. * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
  852. * @tz: thermal zone device
  853. * @trip: indicates which trip point the cooling devices is
  854. * associated with in this thermal zone.
  855. * @cdev: thermal cooling device
  856. *
  857. * This function is usually called in the thermal zone device .bind callback.
  858. */
  859. int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
  860. int trip,
  861. struct thermal_cooling_device *cdev,
  862. unsigned long upper, unsigned long lower)
  863. {
  864. struct thermal_instance *dev;
  865. struct thermal_instance *pos;
  866. struct thermal_zone_device *pos1;
  867. struct thermal_cooling_device *pos2;
  868. unsigned long max_state;
  869. int result;
  870. if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
  871. return -EINVAL;
  872. list_for_each_entry(pos1, &thermal_tz_list, node) {
  873. if (pos1 == tz)
  874. break;
  875. }
  876. list_for_each_entry(pos2, &thermal_cdev_list, node) {
  877. if (pos2 == cdev)
  878. break;
  879. }
  880. if (tz != pos1 || cdev != pos2)
  881. return -EINVAL;
  882. cdev->ops->get_max_state(cdev, &max_state);
  883. /* lower default 0, upper default max_state */
  884. lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
  885. upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
  886. if (lower > upper || upper > max_state)
  887. return -EINVAL;
  888. dev =
  889. kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
  890. if (!dev)
  891. return -ENOMEM;
  892. dev->tz = tz;
  893. dev->cdev = cdev;
  894. dev->trip = trip;
  895. dev->upper = upper;
  896. dev->lower = lower;
  897. dev->target = THERMAL_NO_TARGET;
  898. result = get_idr(&tz->idr, &tz->lock, &dev->id);
  899. if (result)
  900. goto free_mem;
  901. sprintf(dev->name, "cdev%d", dev->id);
  902. result =
  903. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  904. if (result)
  905. goto release_idr;
  906. sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
  907. sysfs_attr_init(&dev->attr.attr);
  908. dev->attr.attr.name = dev->attr_name;
  909. dev->attr.attr.mode = 0444;
  910. dev->attr.show = thermal_cooling_device_trip_point_show;
  911. result = device_create_file(&tz->device, &dev->attr);
  912. if (result)
  913. goto remove_symbol_link;
  914. mutex_lock(&tz->lock);
  915. mutex_lock(&cdev->lock);
  916. list_for_each_entry(pos, &tz->thermal_instances, tz_node)
  917. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  918. result = -EEXIST;
  919. break;
  920. }
  921. if (!result) {
  922. list_add_tail(&dev->tz_node, &tz->thermal_instances);
  923. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  924. }
  925. mutex_unlock(&cdev->lock);
  926. mutex_unlock(&tz->lock);
  927. if (!result)
  928. return 0;
  929. device_remove_file(&tz->device, &dev->attr);
  930. remove_symbol_link:
  931. sysfs_remove_link(&tz->device.kobj, dev->name);
  932. release_idr:
  933. release_idr(&tz->idr, &tz->lock, dev->id);
  934. free_mem:
  935. kfree(dev);
  936. return result;
  937. }
  938. EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
  939. /**
  940. * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
  941. * @tz: thermal zone device
  942. * @trip: indicates which trip point the cooling devices is
  943. * associated with in this thermal zone.
  944. * @cdev: thermal cooling device
  945. *
  946. * This function is usually called in the thermal zone device .unbind callback.
  947. */
  948. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
  949. int trip,
  950. struct thermal_cooling_device *cdev)
  951. {
  952. struct thermal_instance *pos, *next;
  953. mutex_lock(&tz->lock);
  954. mutex_lock(&cdev->lock);
  955. list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
  956. if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
  957. list_del(&pos->tz_node);
  958. list_del(&pos->cdev_node);
  959. mutex_unlock(&cdev->lock);
  960. mutex_unlock(&tz->lock);
  961. goto unbind;
  962. }
  963. }
  964. mutex_unlock(&cdev->lock);
  965. mutex_unlock(&tz->lock);
  966. return -ENODEV;
  967. unbind:
  968. device_remove_file(&tz->device, &pos->attr);
  969. sysfs_remove_link(&tz->device.kobj, pos->name);
  970. release_idr(&tz->idr, &tz->lock, pos->id);
  971. kfree(pos);
  972. return 0;
  973. }
  974. EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
  975. static void thermal_release(struct device *dev)
  976. {
  977. struct thermal_zone_device *tz;
  978. struct thermal_cooling_device *cdev;
  979. if (!strncmp(dev_name(dev), "thermal_zone",
  980. sizeof("thermal_zone") - 1)) {
  981. tz = to_thermal_zone(dev);
  982. kfree(tz);
  983. } else {
  984. cdev = to_cooling_device(dev);
  985. kfree(cdev);
  986. }
  987. }
  988. static struct class thermal_class = {
  989. .name = "thermal",
  990. .dev_release = thermal_release,
  991. };
  992. /**
  993. * thermal_cooling_device_register - register a new thermal cooling device
  994. * @type: the thermal cooling device type.
  995. * @devdata: device private data.
  996. * @ops: standard thermal cooling devices callbacks.
  997. */
  998. struct thermal_cooling_device *
  999. thermal_cooling_device_register(char *type, void *devdata,
  1000. const struct thermal_cooling_device_ops *ops)
  1001. {
  1002. struct thermal_cooling_device *cdev;
  1003. int result;
  1004. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1005. return ERR_PTR(-EINVAL);
  1006. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  1007. !ops->set_cur_state)
  1008. return ERR_PTR(-EINVAL);
  1009. cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
  1010. if (!cdev)
  1011. return ERR_PTR(-ENOMEM);
  1012. result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
  1013. if (result) {
  1014. kfree(cdev);
  1015. return ERR_PTR(result);
  1016. }
  1017. strcpy(cdev->type, type ? : "");
  1018. mutex_init(&cdev->lock);
  1019. INIT_LIST_HEAD(&cdev->thermal_instances);
  1020. cdev->ops = ops;
  1021. cdev->updated = true;
  1022. cdev->device.class = &thermal_class;
  1023. cdev->devdata = devdata;
  1024. dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  1025. result = device_register(&cdev->device);
  1026. if (result) {
  1027. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1028. kfree(cdev);
  1029. return ERR_PTR(result);
  1030. }
  1031. /* sys I/F */
  1032. if (type) {
  1033. result = device_create_file(&cdev->device, &dev_attr_cdev_type);
  1034. if (result)
  1035. goto unregister;
  1036. }
  1037. result = device_create_file(&cdev->device, &dev_attr_max_state);
  1038. if (result)
  1039. goto unregister;
  1040. result = device_create_file(&cdev->device, &dev_attr_cur_state);
  1041. if (result)
  1042. goto unregister;
  1043. /* Add 'this' new cdev to the global cdev list */
  1044. mutex_lock(&thermal_list_lock);
  1045. list_add(&cdev->node, &thermal_cdev_list);
  1046. mutex_unlock(&thermal_list_lock);
  1047. /* Update binding information for 'this' new cdev */
  1048. bind_cdev(cdev);
  1049. return cdev;
  1050. unregister:
  1051. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1052. device_unregister(&cdev->device);
  1053. return ERR_PTR(result);
  1054. }
  1055. EXPORT_SYMBOL(thermal_cooling_device_register);
  1056. /**
  1057. * thermal_cooling_device_unregister - removes the registered thermal cooling device
  1058. * @cdev: the thermal cooling device to remove.
  1059. *
  1060. * thermal_cooling_device_unregister() must be called when the device is no
  1061. * longer needed.
  1062. */
  1063. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  1064. {
  1065. int i;
  1066. const struct thermal_zone_params *tzp;
  1067. struct thermal_zone_device *tz;
  1068. struct thermal_cooling_device *pos = NULL;
  1069. if (!cdev)
  1070. return;
  1071. mutex_lock(&thermal_list_lock);
  1072. list_for_each_entry(pos, &thermal_cdev_list, node)
  1073. if (pos == cdev)
  1074. break;
  1075. if (pos != cdev) {
  1076. /* thermal cooling device not found */
  1077. mutex_unlock(&thermal_list_lock);
  1078. return;
  1079. }
  1080. list_del(&cdev->node);
  1081. /* Unbind all thermal zones associated with 'this' cdev */
  1082. list_for_each_entry(tz, &thermal_tz_list, node) {
  1083. if (tz->ops->unbind) {
  1084. tz->ops->unbind(tz, cdev);
  1085. continue;
  1086. }
  1087. if (!tz->tzp || !tz->tzp->tbp)
  1088. continue;
  1089. tzp = tz->tzp;
  1090. for (i = 0; i < tzp->num_tbps; i++) {
  1091. if (tzp->tbp[i].cdev == cdev) {
  1092. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1093. tzp->tbp[i].cdev = NULL;
  1094. }
  1095. }
  1096. }
  1097. mutex_unlock(&thermal_list_lock);
  1098. if (cdev->type[0])
  1099. device_remove_file(&cdev->device, &dev_attr_cdev_type);
  1100. device_remove_file(&cdev->device, &dev_attr_max_state);
  1101. device_remove_file(&cdev->device, &dev_attr_cur_state);
  1102. release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
  1103. device_unregister(&cdev->device);
  1104. return;
  1105. }
  1106. EXPORT_SYMBOL(thermal_cooling_device_unregister);
  1107. void thermal_cdev_update(struct thermal_cooling_device *cdev)
  1108. {
  1109. struct thermal_instance *instance;
  1110. unsigned long target = 0;
  1111. /* cooling device is updated*/
  1112. if (cdev->updated)
  1113. return;
  1114. mutex_lock(&cdev->lock);
  1115. /* Make sure cdev enters the deepest cooling state */
  1116. list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
  1117. if (instance->target == THERMAL_NO_TARGET)
  1118. continue;
  1119. if (instance->target > target)
  1120. target = instance->target;
  1121. }
  1122. mutex_unlock(&cdev->lock);
  1123. cdev->ops->set_cur_state(cdev, target);
  1124. cdev->updated = true;
  1125. }
  1126. EXPORT_SYMBOL(thermal_cdev_update);
  1127. /**
  1128. * notify_thermal_framework - Sensor drivers use this API to notify framework
  1129. * @tz: thermal zone device
  1130. * @trip: indicates which trip point has been crossed
  1131. *
  1132. * This function handles the trip events from sensor drivers. It starts
  1133. * throttling the cooling devices according to the policy configured.
  1134. * For CRITICAL and HOT trip points, this notifies the respective drivers,
  1135. * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
  1136. * The throttling policy is based on the configured platform data; if no
  1137. * platform data is provided, this uses the step_wise throttling policy.
  1138. */
  1139. void notify_thermal_framework(struct thermal_zone_device *tz, int trip)
  1140. {
  1141. handle_thermal_trip(tz, trip);
  1142. }
  1143. EXPORT_SYMBOL(notify_thermal_framework);
  1144. /**
  1145. * create_trip_attrs - create attributes for trip points
  1146. * @tz: the thermal zone device
  1147. * @mask: Writeable trip point bitmap.
  1148. */
  1149. static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
  1150. {
  1151. int indx;
  1152. int size = sizeof(struct thermal_attr) * tz->trips;
  1153. tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
  1154. if (!tz->trip_type_attrs)
  1155. return -ENOMEM;
  1156. tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
  1157. if (!tz->trip_temp_attrs) {
  1158. kfree(tz->trip_type_attrs);
  1159. return -ENOMEM;
  1160. }
  1161. if (tz->ops->get_trip_hyst) {
  1162. tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
  1163. if (!tz->trip_hyst_attrs) {
  1164. kfree(tz->trip_type_attrs);
  1165. kfree(tz->trip_temp_attrs);
  1166. return -ENOMEM;
  1167. }
  1168. }
  1169. for (indx = 0; indx < tz->trips; indx++) {
  1170. /* create trip type attribute */
  1171. snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
  1172. "trip_point_%d_type", indx);
  1173. sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
  1174. tz->trip_type_attrs[indx].attr.attr.name =
  1175. tz->trip_type_attrs[indx].name;
  1176. tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
  1177. tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
  1178. device_create_file(&tz->device,
  1179. &tz->trip_type_attrs[indx].attr);
  1180. /* create trip temp attribute */
  1181. snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
  1182. "trip_point_%d_temp", indx);
  1183. sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
  1184. tz->trip_temp_attrs[indx].attr.attr.name =
  1185. tz->trip_temp_attrs[indx].name;
  1186. tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
  1187. tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
  1188. if (mask & (1 << indx)) {
  1189. tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
  1190. tz->trip_temp_attrs[indx].attr.store =
  1191. trip_point_temp_store;
  1192. }
  1193. device_create_file(&tz->device,
  1194. &tz->trip_temp_attrs[indx].attr);
  1195. /* create Optional trip hyst attribute */
  1196. if (!tz->ops->get_trip_hyst)
  1197. continue;
  1198. snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
  1199. "trip_point_%d_hyst", indx);
  1200. sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
  1201. tz->trip_hyst_attrs[indx].attr.attr.name =
  1202. tz->trip_hyst_attrs[indx].name;
  1203. tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
  1204. tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
  1205. if (tz->ops->set_trip_hyst) {
  1206. tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
  1207. tz->trip_hyst_attrs[indx].attr.store =
  1208. trip_point_hyst_store;
  1209. }
  1210. device_create_file(&tz->device,
  1211. &tz->trip_hyst_attrs[indx].attr);
  1212. }
  1213. return 0;
  1214. }
  1215. static void remove_trip_attrs(struct thermal_zone_device *tz)
  1216. {
  1217. int indx;
  1218. for (indx = 0; indx < tz->trips; indx++) {
  1219. device_remove_file(&tz->device,
  1220. &tz->trip_type_attrs[indx].attr);
  1221. device_remove_file(&tz->device,
  1222. &tz->trip_temp_attrs[indx].attr);
  1223. if (tz->ops->get_trip_hyst)
  1224. device_remove_file(&tz->device,
  1225. &tz->trip_hyst_attrs[indx].attr);
  1226. }
  1227. kfree(tz->trip_type_attrs);
  1228. kfree(tz->trip_temp_attrs);
  1229. kfree(tz->trip_hyst_attrs);
  1230. }
  1231. /**
  1232. * thermal_zone_device_register - register a new thermal zone device
  1233. * @type: the thermal zone device type
  1234. * @trips: the number of trip points the thermal zone support
  1235. * @mask: a bit string indicating the writeablility of trip points
  1236. * @devdata: private device data
  1237. * @ops: standard thermal zone device callbacks
  1238. * @tzp: thermal zone platform parameters
  1239. * @passive_delay: number of milliseconds to wait between polls when
  1240. * performing passive cooling
  1241. * @polling_delay: number of milliseconds to wait between polls when checking
  1242. * whether trip points have been crossed (0 for interrupt
  1243. * driven systems)
  1244. *
  1245. * thermal_zone_device_unregister() must be called when the device is no
  1246. * longer needed. The passive cooling depends on the .get_trend() return value.
  1247. */
  1248. struct thermal_zone_device *thermal_zone_device_register(const char *type,
  1249. int trips, int mask, void *devdata,
  1250. const struct thermal_zone_device_ops *ops,
  1251. const struct thermal_zone_params *tzp,
  1252. int passive_delay, int polling_delay)
  1253. {
  1254. struct thermal_zone_device *tz;
  1255. enum thermal_trip_type trip_type;
  1256. int result;
  1257. int count;
  1258. int passive = 0;
  1259. if (type && strlen(type) >= THERMAL_NAME_LENGTH)
  1260. return ERR_PTR(-EINVAL);
  1261. if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
  1262. return ERR_PTR(-EINVAL);
  1263. if (!ops || !ops->get_temp)
  1264. return ERR_PTR(-EINVAL);
  1265. tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
  1266. if (!tz)
  1267. return ERR_PTR(-ENOMEM);
  1268. INIT_LIST_HEAD(&tz->thermal_instances);
  1269. idr_init(&tz->idr);
  1270. mutex_init(&tz->lock);
  1271. result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
  1272. if (result) {
  1273. kfree(tz);
  1274. return ERR_PTR(result);
  1275. }
  1276. strcpy(tz->type, type ? : "");
  1277. tz->ops = ops;
  1278. tz->tzp = tzp;
  1279. tz->device.class = &thermal_class;
  1280. tz->devdata = devdata;
  1281. tz->trips = trips;
  1282. tz->passive_delay = passive_delay;
  1283. tz->polling_delay = polling_delay;
  1284. dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1285. result = device_register(&tz->device);
  1286. if (result) {
  1287. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1288. kfree(tz);
  1289. return ERR_PTR(result);
  1290. }
  1291. /* sys I/F */
  1292. if (type) {
  1293. result = device_create_file(&tz->device, &dev_attr_type);
  1294. if (result)
  1295. goto unregister;
  1296. }
  1297. result = device_create_file(&tz->device, &dev_attr_temp);
  1298. if (result)
  1299. goto unregister;
  1300. if (ops->get_mode) {
  1301. result = device_create_file(&tz->device, &dev_attr_mode);
  1302. if (result)
  1303. goto unregister;
  1304. }
  1305. result = create_trip_attrs(tz, mask);
  1306. if (result)
  1307. goto unregister;
  1308. for (count = 0; count < trips; count++) {
  1309. tz->ops->get_trip_type(tz, count, &trip_type);
  1310. if (trip_type == THERMAL_TRIP_PASSIVE)
  1311. passive = 1;
  1312. }
  1313. if (!passive) {
  1314. result = device_create_file(&tz->device, &dev_attr_passive);
  1315. if (result)
  1316. goto unregister;
  1317. }
  1318. /* Create policy attribute */
  1319. result = device_create_file(&tz->device, &dev_attr_policy);
  1320. if (result)
  1321. goto unregister;
  1322. /* Update 'this' zone's governor information */
  1323. mutex_lock(&thermal_governor_lock);
  1324. if (tz->tzp)
  1325. tz->governor = __find_governor(tz->tzp->governor_name);
  1326. else
  1327. tz->governor = __find_governor(DEFAULT_THERMAL_GOVERNOR);
  1328. mutex_unlock(&thermal_governor_lock);
  1329. result = thermal_add_hwmon_sysfs(tz);
  1330. if (result)
  1331. goto unregister;
  1332. mutex_lock(&thermal_list_lock);
  1333. list_add_tail(&tz->node, &thermal_tz_list);
  1334. mutex_unlock(&thermal_list_lock);
  1335. /* Bind cooling devices for this zone */
  1336. bind_tz(tz);
  1337. INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
  1338. thermal_zone_device_update(tz);
  1339. if (!result)
  1340. return tz;
  1341. unregister:
  1342. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1343. device_unregister(&tz->device);
  1344. return ERR_PTR(result);
  1345. }
  1346. EXPORT_SYMBOL(thermal_zone_device_register);
  1347. /**
  1348. * thermal_device_unregister - removes the registered thermal zone device
  1349. * @tz: the thermal zone device to remove
  1350. */
  1351. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1352. {
  1353. int i;
  1354. const struct thermal_zone_params *tzp;
  1355. struct thermal_cooling_device *cdev;
  1356. struct thermal_zone_device *pos = NULL;
  1357. if (!tz)
  1358. return;
  1359. tzp = tz->tzp;
  1360. mutex_lock(&thermal_list_lock);
  1361. list_for_each_entry(pos, &thermal_tz_list, node)
  1362. if (pos == tz)
  1363. break;
  1364. if (pos != tz) {
  1365. /* thermal zone device not found */
  1366. mutex_unlock(&thermal_list_lock);
  1367. return;
  1368. }
  1369. list_del(&tz->node);
  1370. /* Unbind all cdevs associated with 'this' thermal zone */
  1371. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  1372. if (tz->ops->unbind) {
  1373. tz->ops->unbind(tz, cdev);
  1374. continue;
  1375. }
  1376. if (!tzp || !tzp->tbp)
  1377. break;
  1378. for (i = 0; i < tzp->num_tbps; i++) {
  1379. if (tzp->tbp[i].cdev == cdev) {
  1380. __unbind(tz, tzp->tbp[i].trip_mask, cdev);
  1381. tzp->tbp[i].cdev = NULL;
  1382. }
  1383. }
  1384. }
  1385. mutex_unlock(&thermal_list_lock);
  1386. thermal_zone_device_set_polling(tz, 0);
  1387. if (tz->type[0])
  1388. device_remove_file(&tz->device, &dev_attr_type);
  1389. device_remove_file(&tz->device, &dev_attr_temp);
  1390. if (tz->ops->get_mode)
  1391. device_remove_file(&tz->device, &dev_attr_mode);
  1392. device_remove_file(&tz->device, &dev_attr_policy);
  1393. remove_trip_attrs(tz);
  1394. tz->governor = NULL;
  1395. thermal_remove_hwmon_sysfs(tz);
  1396. release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
  1397. idr_destroy(&tz->idr);
  1398. mutex_destroy(&tz->lock);
  1399. device_unregister(&tz->device);
  1400. return;
  1401. }
  1402. EXPORT_SYMBOL(thermal_zone_device_unregister);
  1403. #ifdef CONFIG_NET
  1404. static struct genl_family thermal_event_genl_family = {
  1405. .id = GENL_ID_GENERATE,
  1406. .name = THERMAL_GENL_FAMILY_NAME,
  1407. .version = THERMAL_GENL_VERSION,
  1408. .maxattr = THERMAL_GENL_ATTR_MAX,
  1409. };
  1410. static struct genl_multicast_group thermal_event_mcgrp = {
  1411. .name = THERMAL_GENL_MCAST_GROUP_NAME,
  1412. };
  1413. int thermal_generate_netlink_event(u32 orig, enum events event)
  1414. {
  1415. struct sk_buff *skb;
  1416. struct nlattr *attr;
  1417. struct thermal_genl_event *thermal_event;
  1418. void *msg_header;
  1419. int size;
  1420. int result;
  1421. static unsigned int thermal_event_seqnum;
  1422. /* allocate memory */
  1423. size = nla_total_size(sizeof(struct thermal_genl_event)) +
  1424. nla_total_size(0);
  1425. skb = genlmsg_new(size, GFP_ATOMIC);
  1426. if (!skb)
  1427. return -ENOMEM;
  1428. /* add the genetlink message header */
  1429. msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
  1430. &thermal_event_genl_family, 0,
  1431. THERMAL_GENL_CMD_EVENT);
  1432. if (!msg_header) {
  1433. nlmsg_free(skb);
  1434. return -ENOMEM;
  1435. }
  1436. /* fill the data */
  1437. attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
  1438. sizeof(struct thermal_genl_event));
  1439. if (!attr) {
  1440. nlmsg_free(skb);
  1441. return -EINVAL;
  1442. }
  1443. thermal_event = nla_data(attr);
  1444. if (!thermal_event) {
  1445. nlmsg_free(skb);
  1446. return -EINVAL;
  1447. }
  1448. memset(thermal_event, 0, sizeof(struct thermal_genl_event));
  1449. thermal_event->orig = orig;
  1450. thermal_event->event = event;
  1451. /* send multicast genetlink message */
  1452. result = genlmsg_end(skb, msg_header);
  1453. if (result < 0) {
  1454. nlmsg_free(skb);
  1455. return result;
  1456. }
  1457. result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
  1458. if (result)
  1459. pr_info("failed to send netlink event:%d\n", result);
  1460. return result;
  1461. }
  1462. EXPORT_SYMBOL(thermal_generate_netlink_event);
  1463. static int genetlink_init(void)
  1464. {
  1465. int result;
  1466. result = genl_register_family(&thermal_event_genl_family);
  1467. if (result)
  1468. return result;
  1469. result = genl_register_mc_group(&thermal_event_genl_family,
  1470. &thermal_event_mcgrp);
  1471. if (result)
  1472. genl_unregister_family(&thermal_event_genl_family);
  1473. return result;
  1474. }
  1475. static void genetlink_exit(void)
  1476. {
  1477. genl_unregister_family(&thermal_event_genl_family);
  1478. }
  1479. #else /* !CONFIG_NET */
  1480. static inline int genetlink_init(void) { return 0; }
  1481. static inline void genetlink_exit(void) {}
  1482. #endif /* !CONFIG_NET */
  1483. static int __init thermal_init(void)
  1484. {
  1485. int result = 0;
  1486. result = class_register(&thermal_class);
  1487. if (result) {
  1488. idr_destroy(&thermal_tz_idr);
  1489. idr_destroy(&thermal_cdev_idr);
  1490. mutex_destroy(&thermal_idr_lock);
  1491. mutex_destroy(&thermal_list_lock);
  1492. }
  1493. result = genetlink_init();
  1494. return result;
  1495. }
  1496. static void __exit thermal_exit(void)
  1497. {
  1498. class_unregister(&thermal_class);
  1499. idr_destroy(&thermal_tz_idr);
  1500. idr_destroy(&thermal_cdev_idr);
  1501. mutex_destroy(&thermal_idr_lock);
  1502. mutex_destroy(&thermal_list_lock);
  1503. genetlink_exit();
  1504. }
  1505. fs_initcall(thermal_init);
  1506. module_exit(thermal_exit);