gpio_backlight.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * gpio_backlight.c - Simple GPIO-controlled backlight
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/backlight.h>
  9. #include <linux/err.h>
  10. #include <linux/fb.h>
  11. #include <linux/gpio.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_data/gpio_backlight.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. struct gpio_backlight {
  19. struct device *dev;
  20. struct device *fbdev;
  21. int gpio;
  22. int active;
  23. };
  24. static int gpio_backlight_update_status(struct backlight_device *bl)
  25. {
  26. struct gpio_backlight *gbl = bl_get_data(bl);
  27. int brightness = bl->props.brightness;
  28. if (bl->props.power != FB_BLANK_UNBLANK ||
  29. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  30. bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  31. brightness = 0;
  32. gpio_set_value(gbl->gpio, brightness ? gbl->active : !gbl->active);
  33. return 0;
  34. }
  35. static int gpio_backlight_get_brightness(struct backlight_device *bl)
  36. {
  37. return bl->props.brightness;
  38. }
  39. static int gpio_backlight_check_fb(struct backlight_device *bl,
  40. struct fb_info *info)
  41. {
  42. struct gpio_backlight *gbl = bl_get_data(bl);
  43. return gbl->fbdev == NULL || gbl->fbdev == info->dev;
  44. }
  45. static const struct backlight_ops gpio_backlight_ops = {
  46. .options = BL_CORE_SUSPENDRESUME,
  47. .update_status = gpio_backlight_update_status,
  48. .get_brightness = gpio_backlight_get_brightness,
  49. .check_fb = gpio_backlight_check_fb,
  50. };
  51. static int gpio_backlight_probe(struct platform_device *pdev)
  52. {
  53. struct gpio_backlight_platform_data *pdata = pdev->dev.platform_data;
  54. struct backlight_properties props;
  55. struct backlight_device *bl;
  56. struct gpio_backlight *gbl;
  57. int ret;
  58. if (!pdata) {
  59. dev_err(&pdev->dev, "failed to find platform data\n");
  60. return -ENODEV;
  61. }
  62. gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
  63. if (gbl == NULL)
  64. return -ENOMEM;
  65. gbl->dev = &pdev->dev;
  66. gbl->fbdev = pdata->fbdev;
  67. gbl->gpio = pdata->gpio;
  68. gbl->active = pdata->active_low ? 0 : 1;
  69. ret = devm_gpio_request_one(gbl->dev, gbl->gpio, GPIOF_DIR_OUT |
  70. (gbl->active ? GPIOF_INIT_LOW
  71. : GPIOF_INIT_HIGH),
  72. pdata->name);
  73. if (ret < 0) {
  74. dev_err(&pdev->dev, "unable to request GPIO\n");
  75. return ret;
  76. }
  77. memset(&props, 0, sizeof(props));
  78. props.type = BACKLIGHT_RAW;
  79. props.max_brightness = 1;
  80. bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, gbl,
  81. &gpio_backlight_ops, &props);
  82. if (IS_ERR(bl)) {
  83. dev_err(&pdev->dev, "failed to register backlight\n");
  84. return PTR_ERR(bl);
  85. }
  86. bl->props.brightness = pdata->def_value;
  87. backlight_update_status(bl);
  88. platform_set_drvdata(pdev, bl);
  89. return 0;
  90. }
  91. static int gpio_backlight_remove(struct platform_device *pdev)
  92. {
  93. struct backlight_device *bl = platform_get_drvdata(pdev);
  94. backlight_device_unregister(bl);
  95. return 0;
  96. }
  97. static struct platform_driver gpio_backlight_driver = {
  98. .driver = {
  99. .name = "gpio-backlight",
  100. .owner = THIS_MODULE,
  101. },
  102. .probe = gpio_backlight_probe,
  103. .remove = gpio_backlight_remove,
  104. };
  105. module_platform_driver(gpio_backlight_driver);
  106. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  107. MODULE_DESCRIPTION("GPIO-based Backlight Driver");
  108. MODULE_LICENSE("GPL");
  109. MODULE_ALIAS("platform:gpio-backlight");