backlight.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Backlight Lowlevel Control Abstraction
  3. *
  4. * Copyright (C) 2003,2004 Hewlett-Packard Company
  5. *
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/device.h>
  11. #include <linux/backlight.h>
  12. #include <linux/notifier.h>
  13. #include <linux/ctype.h>
  14. #include <linux/err.h>
  15. #include <linux/fb.h>
  16. #include <linux/slab.h>
  17. #ifdef CONFIG_PMAC_BACKLIGHT
  18. #include <asm/backlight.h>
  19. #endif
  20. static struct list_head backlight_dev_list;
  21. static struct mutex backlight_dev_list_mutex;
  22. static const char *const backlight_types[] = {
  23. [BACKLIGHT_RAW] = "raw",
  24. [BACKLIGHT_PLATFORM] = "platform",
  25. [BACKLIGHT_FIRMWARE] = "firmware",
  26. };
  27. #if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \
  28. defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE))
  29. /* This callback gets called when something important happens inside a
  30. * framebuffer driver. We're looking if that important event is blanking,
  31. * and if it is, we're switching backlight power as well ...
  32. */
  33. static int fb_notifier_callback(struct notifier_block *self,
  34. unsigned long event, void *data)
  35. {
  36. struct backlight_device *bd;
  37. struct fb_event *evdata = data;
  38. /* If we aren't interested in this event, skip it immediately ... */
  39. if (event != FB_EVENT_BLANK && event != FB_EVENT_CONBLANK)
  40. return 0;
  41. bd = container_of(self, struct backlight_device, fb_notif);
  42. mutex_lock(&bd->ops_lock);
  43. if (bd->ops)
  44. if (!bd->ops->check_fb ||
  45. bd->ops->check_fb(bd, evdata->info)) {
  46. bd->props.fb_blank = *(int *)evdata->data;
  47. if (bd->props.fb_blank == FB_BLANK_UNBLANK)
  48. bd->props.state &= ~BL_CORE_FBBLANK;
  49. else
  50. bd->props.state |= BL_CORE_FBBLANK;
  51. backlight_update_status(bd);
  52. }
  53. mutex_unlock(&bd->ops_lock);
  54. return 0;
  55. }
  56. static int backlight_register_fb(struct backlight_device *bd)
  57. {
  58. memset(&bd->fb_notif, 0, sizeof(bd->fb_notif));
  59. bd->fb_notif.notifier_call = fb_notifier_callback;
  60. return fb_register_client(&bd->fb_notif);
  61. }
  62. static void backlight_unregister_fb(struct backlight_device *bd)
  63. {
  64. fb_unregister_client(&bd->fb_notif);
  65. }
  66. #else
  67. static inline int backlight_register_fb(struct backlight_device *bd)
  68. {
  69. return 0;
  70. }
  71. static inline void backlight_unregister_fb(struct backlight_device *bd)
  72. {
  73. }
  74. #endif /* CONFIG_FB */
  75. static void backlight_generate_event(struct backlight_device *bd,
  76. enum backlight_update_reason reason)
  77. {
  78. char *envp[2];
  79. switch (reason) {
  80. case BACKLIGHT_UPDATE_SYSFS:
  81. envp[0] = "SOURCE=sysfs";
  82. break;
  83. case BACKLIGHT_UPDATE_HOTKEY:
  84. envp[0] = "SOURCE=hotkey";
  85. break;
  86. default:
  87. envp[0] = "SOURCE=unknown";
  88. break;
  89. }
  90. envp[1] = NULL;
  91. kobject_uevent_env(&bd->dev.kobj, KOBJ_CHANGE, envp);
  92. sysfs_notify(&bd->dev.kobj, NULL, "actual_brightness");
  93. }
  94. static ssize_t bl_power_show(struct device *dev, struct device_attribute *attr,
  95. char *buf)
  96. {
  97. struct backlight_device *bd = to_backlight_device(dev);
  98. return sprintf(buf, "%d\n", bd->props.power);
  99. }
  100. static ssize_t bl_power_store(struct device *dev, struct device_attribute *attr,
  101. const char *buf, size_t count)
  102. {
  103. int rc;
  104. struct backlight_device *bd = to_backlight_device(dev);
  105. unsigned long power;
  106. rc = kstrtoul(buf, 0, &power);
  107. if (rc)
  108. return rc;
  109. rc = -ENXIO;
  110. mutex_lock(&bd->ops_lock);
  111. if (bd->ops) {
  112. pr_debug("set power to %lu\n", power);
  113. if (bd->props.power != power) {
  114. bd->props.power = power;
  115. backlight_update_status(bd);
  116. }
  117. rc = count;
  118. }
  119. mutex_unlock(&bd->ops_lock);
  120. return rc;
  121. }
  122. static DEVICE_ATTR_RW(bl_power);
  123. static ssize_t brightness_show(struct device *dev,
  124. struct device_attribute *attr, char *buf)
  125. {
  126. struct backlight_device *bd = to_backlight_device(dev);
  127. return sprintf(buf, "%d\n", bd->props.brightness);
  128. }
  129. static ssize_t brightness_store(struct device *dev,
  130. struct device_attribute *attr, const char *buf, size_t count)
  131. {
  132. int rc;
  133. struct backlight_device *bd = to_backlight_device(dev);
  134. unsigned long brightness;
  135. rc = kstrtoul(buf, 0, &brightness);
  136. if (rc)
  137. return rc;
  138. rc = -ENXIO;
  139. mutex_lock(&bd->ops_lock);
  140. if (bd->ops) {
  141. if (brightness > bd->props.max_brightness)
  142. rc = -EINVAL;
  143. else {
  144. pr_debug("set brightness to %lu\n", brightness);
  145. bd->props.brightness = brightness;
  146. backlight_update_status(bd);
  147. rc = count;
  148. }
  149. }
  150. mutex_unlock(&bd->ops_lock);
  151. backlight_generate_event(bd, BACKLIGHT_UPDATE_SYSFS);
  152. return rc;
  153. }
  154. static DEVICE_ATTR_RW(brightness);
  155. static ssize_t type_show(struct device *dev, struct device_attribute *attr,
  156. char *buf)
  157. {
  158. struct backlight_device *bd = to_backlight_device(dev);
  159. return sprintf(buf, "%s\n", backlight_types[bd->props.type]);
  160. }
  161. static DEVICE_ATTR_RO(type);
  162. static ssize_t max_brightness_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct backlight_device *bd = to_backlight_device(dev);
  166. return sprintf(buf, "%d\n", bd->props.max_brightness);
  167. }
  168. static DEVICE_ATTR_RO(max_brightness);
  169. static ssize_t actual_brightness_show(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. int rc = -ENXIO;
  173. struct backlight_device *bd = to_backlight_device(dev);
  174. mutex_lock(&bd->ops_lock);
  175. if (bd->ops && bd->ops->get_brightness)
  176. rc = sprintf(buf, "%d\n", bd->ops->get_brightness(bd));
  177. mutex_unlock(&bd->ops_lock);
  178. return rc;
  179. }
  180. static DEVICE_ATTR_RO(actual_brightness);
  181. static struct class *backlight_class;
  182. #ifdef CONFIG_PM_SLEEP
  183. static int backlight_suspend(struct device *dev)
  184. {
  185. struct backlight_device *bd = to_backlight_device(dev);
  186. mutex_lock(&bd->ops_lock);
  187. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  188. bd->props.state |= BL_CORE_SUSPENDED;
  189. backlight_update_status(bd);
  190. }
  191. mutex_unlock(&bd->ops_lock);
  192. return 0;
  193. }
  194. static int backlight_resume(struct device *dev)
  195. {
  196. struct backlight_device *bd = to_backlight_device(dev);
  197. mutex_lock(&bd->ops_lock);
  198. if (bd->ops && bd->ops->options & BL_CORE_SUSPENDRESUME) {
  199. bd->props.state &= ~BL_CORE_SUSPENDED;
  200. backlight_update_status(bd);
  201. }
  202. mutex_unlock(&bd->ops_lock);
  203. return 0;
  204. }
  205. #endif
  206. static SIMPLE_DEV_PM_OPS(backlight_class_dev_pm_ops, backlight_suspend,
  207. backlight_resume);
  208. static void bl_device_release(struct device *dev)
  209. {
  210. struct backlight_device *bd = to_backlight_device(dev);
  211. kfree(bd);
  212. }
  213. static struct attribute *bl_device_attrs[] = {
  214. &dev_attr_bl_power.attr,
  215. &dev_attr_brightness.attr,
  216. &dev_attr_actual_brightness.attr,
  217. &dev_attr_max_brightness.attr,
  218. &dev_attr_type.attr,
  219. NULL,
  220. };
  221. ATTRIBUTE_GROUPS(bl_device);
  222. /**
  223. * backlight_force_update - tell the backlight subsystem that hardware state
  224. * has changed
  225. * @bd: the backlight device to update
  226. *
  227. * Updates the internal state of the backlight in response to a hardware event,
  228. * and generate a uevent to notify userspace
  229. */
  230. void backlight_force_update(struct backlight_device *bd,
  231. enum backlight_update_reason reason)
  232. {
  233. mutex_lock(&bd->ops_lock);
  234. if (bd->ops && bd->ops->get_brightness)
  235. bd->props.brightness = bd->ops->get_brightness(bd);
  236. mutex_unlock(&bd->ops_lock);
  237. backlight_generate_event(bd, reason);
  238. }
  239. EXPORT_SYMBOL(backlight_force_update);
  240. /**
  241. * backlight_device_register - create and register a new object of
  242. * backlight_device class.
  243. * @name: the name of the new object(must be the same as the name of the
  244. * respective framebuffer device).
  245. * @parent: a pointer to the parent device
  246. * @devdata: an optional pointer to be stored for private driver use. The
  247. * methods may retrieve it by using bl_get_data(bd).
  248. * @ops: the backlight operations structure.
  249. *
  250. * Creates and registers new backlight device. Returns either an
  251. * ERR_PTR() or a pointer to the newly allocated device.
  252. */
  253. struct backlight_device *backlight_device_register(const char *name,
  254. struct device *parent, void *devdata, const struct backlight_ops *ops,
  255. const struct backlight_properties *props)
  256. {
  257. struct backlight_device *new_bd;
  258. int rc;
  259. pr_debug("backlight_device_register: name=%s\n", name);
  260. new_bd = kzalloc(sizeof(struct backlight_device), GFP_KERNEL);
  261. if (!new_bd)
  262. return ERR_PTR(-ENOMEM);
  263. mutex_init(&new_bd->update_lock);
  264. mutex_init(&new_bd->ops_lock);
  265. new_bd->dev.class = backlight_class;
  266. new_bd->dev.parent = parent;
  267. new_bd->dev.release = bl_device_release;
  268. dev_set_name(&new_bd->dev, "%s", name);
  269. dev_set_drvdata(&new_bd->dev, devdata);
  270. /* Set default properties */
  271. if (props) {
  272. memcpy(&new_bd->props, props,
  273. sizeof(struct backlight_properties));
  274. if (props->type <= 0 || props->type >= BACKLIGHT_TYPE_MAX) {
  275. WARN(1, "%s: invalid backlight type", name);
  276. new_bd->props.type = BACKLIGHT_RAW;
  277. }
  278. } else {
  279. new_bd->props.type = BACKLIGHT_RAW;
  280. }
  281. rc = device_register(&new_bd->dev);
  282. if (rc) {
  283. kfree(new_bd);
  284. return ERR_PTR(rc);
  285. }
  286. rc = backlight_register_fb(new_bd);
  287. if (rc) {
  288. device_unregister(&new_bd->dev);
  289. return ERR_PTR(rc);
  290. }
  291. new_bd->ops = ops;
  292. #ifdef CONFIG_PMAC_BACKLIGHT
  293. mutex_lock(&pmac_backlight_mutex);
  294. if (!pmac_backlight)
  295. pmac_backlight = new_bd;
  296. mutex_unlock(&pmac_backlight_mutex);
  297. #endif
  298. mutex_lock(&backlight_dev_list_mutex);
  299. list_add(&new_bd->entry, &backlight_dev_list);
  300. mutex_unlock(&backlight_dev_list_mutex);
  301. return new_bd;
  302. }
  303. EXPORT_SYMBOL(backlight_device_register);
  304. bool backlight_device_registered(enum backlight_type type)
  305. {
  306. bool found = false;
  307. struct backlight_device *bd;
  308. mutex_lock(&backlight_dev_list_mutex);
  309. list_for_each_entry(bd, &backlight_dev_list, entry) {
  310. if (bd->props.type == type) {
  311. found = true;
  312. break;
  313. }
  314. }
  315. mutex_unlock(&backlight_dev_list_mutex);
  316. return found;
  317. }
  318. EXPORT_SYMBOL(backlight_device_registered);
  319. /**
  320. * backlight_device_unregister - unregisters a backlight device object.
  321. * @bd: the backlight device object to be unregistered and freed.
  322. *
  323. * Unregisters a previously registered via backlight_device_register object.
  324. */
  325. void backlight_device_unregister(struct backlight_device *bd)
  326. {
  327. if (!bd)
  328. return;
  329. mutex_lock(&backlight_dev_list_mutex);
  330. list_del(&bd->entry);
  331. mutex_unlock(&backlight_dev_list_mutex);
  332. #ifdef CONFIG_PMAC_BACKLIGHT
  333. mutex_lock(&pmac_backlight_mutex);
  334. if (pmac_backlight == bd)
  335. pmac_backlight = NULL;
  336. mutex_unlock(&pmac_backlight_mutex);
  337. #endif
  338. mutex_lock(&bd->ops_lock);
  339. bd->ops = NULL;
  340. mutex_unlock(&bd->ops_lock);
  341. backlight_unregister_fb(bd);
  342. device_unregister(&bd->dev);
  343. }
  344. EXPORT_SYMBOL(backlight_device_unregister);
  345. static void devm_backlight_device_release(struct device *dev, void *res)
  346. {
  347. struct backlight_device *backlight = *(struct backlight_device **)res;
  348. backlight_device_unregister(backlight);
  349. }
  350. static int devm_backlight_device_match(struct device *dev, void *res,
  351. void *data)
  352. {
  353. struct backlight_device **r = res;
  354. return *r == data;
  355. }
  356. /**
  357. * devm_backlight_device_register - resource managed backlight_device_register()
  358. * @dev: the device to register
  359. * @name: the name of the device
  360. * @parent: a pointer to the parent device
  361. * @devdata: an optional pointer to be stored for private driver use
  362. * @ops: the backlight operations structure
  363. * @props: the backlight properties
  364. *
  365. * @return a struct backlight on success, or an ERR_PTR on error
  366. *
  367. * Managed backlight_device_register(). The backlight_device returned
  368. * from this function are automatically freed on driver detach.
  369. * See backlight_device_register() for more information.
  370. */
  371. struct backlight_device *devm_backlight_device_register(struct device *dev,
  372. const char *name, struct device *parent, void *devdata,
  373. const struct backlight_ops *ops,
  374. const struct backlight_properties *props)
  375. {
  376. struct backlight_device **ptr, *backlight;
  377. ptr = devres_alloc(devm_backlight_device_release, sizeof(*ptr),
  378. GFP_KERNEL);
  379. if (!ptr)
  380. return ERR_PTR(-ENOMEM);
  381. backlight = backlight_device_register(name, parent, devdata, ops,
  382. props);
  383. if (!IS_ERR(backlight)) {
  384. *ptr = backlight;
  385. devres_add(dev, ptr);
  386. } else {
  387. devres_free(ptr);
  388. }
  389. return backlight;
  390. }
  391. EXPORT_SYMBOL(devm_backlight_device_register);
  392. /**
  393. * devm_backlight_device_unregister - resource managed backlight_device_unregister()
  394. * @dev: the device to unregister
  395. * @bd: the backlight device to unregister
  396. *
  397. * Deallocated a backlight allocated with devm_backlight_device_register().
  398. * Normally this function will not need to be called and the resource management
  399. * code will ensure that the resource is freed.
  400. */
  401. void devm_backlight_device_unregister(struct device *dev,
  402. struct backlight_device *bd)
  403. {
  404. int rc;
  405. rc = devres_release(dev, devm_backlight_device_release,
  406. devm_backlight_device_match, bd);
  407. WARN_ON(rc);
  408. }
  409. EXPORT_SYMBOL(devm_backlight_device_unregister);
  410. #ifdef CONFIG_OF
  411. static int of_parent_match(struct device *dev, const void *data)
  412. {
  413. return dev->parent && dev->parent->of_node == data;
  414. }
  415. /**
  416. * of_find_backlight_by_node() - find backlight device by device-tree node
  417. * @node: device-tree node of the backlight device
  418. *
  419. * Returns a pointer to the backlight device corresponding to the given DT
  420. * node or NULL if no such backlight device exists or if the device hasn't
  421. * been probed yet.
  422. *
  423. * This function obtains a reference on the backlight device and it is the
  424. * caller's responsibility to drop the reference by calling put_device() on
  425. * the backlight device's .dev field.
  426. */
  427. struct backlight_device *of_find_backlight_by_node(struct device_node *node)
  428. {
  429. struct device *dev;
  430. dev = class_find_device(backlight_class, NULL, node, of_parent_match);
  431. return dev ? to_backlight_device(dev) : NULL;
  432. }
  433. EXPORT_SYMBOL(of_find_backlight_by_node);
  434. #endif
  435. static void __exit backlight_class_exit(void)
  436. {
  437. class_destroy(backlight_class);
  438. }
  439. static int __init backlight_class_init(void)
  440. {
  441. backlight_class = class_create(THIS_MODULE, "backlight");
  442. if (IS_ERR(backlight_class)) {
  443. pr_warn("Unable to create backlight class; errno = %ld\n",
  444. PTR_ERR(backlight_class));
  445. return PTR_ERR(backlight_class);
  446. }
  447. backlight_class->dev_groups = bl_device_groups;
  448. backlight_class->pm = &backlight_class_dev_pm_ops;
  449. INIT_LIST_HEAD(&backlight_dev_list);
  450. mutex_init(&backlight_dev_list_mutex);
  451. return 0;
  452. }
  453. /*
  454. * if this is compiled into the kernel, we need to ensure that the
  455. * class is registered before users of the class try to register lcd's
  456. */
  457. postcore_initcall(backlight_class_init);
  458. module_exit(backlight_class_exit);
  459. MODULE_LICENSE("GPL");
  460. MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");
  461. MODULE_DESCRIPTION("Backlight Lowlevel Control Abstraction");