gpio_backlight.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/of.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/platform_data/gpio_backlight.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. struct gpio_backlight {
  21. struct device *dev;
  22. struct device *fbdev;
  23. int gpio;
  24. int active;
  25. int def_value;
  26. };
  27. static int gpio_backlight_update_status(struct backlight_device *bl)
  28. {
  29. struct gpio_backlight *gbl = bl_get_data(bl);
  30. int brightness = bl->props.brightness;
  31. if (bl->props.power != FB_BLANK_UNBLANK ||
  32. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  33. bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  34. brightness = 0;
  35. gpio_set_value(gbl->gpio, brightness ? gbl->active : !gbl->active);
  36. return 0;
  37. }
  38. static int gpio_backlight_get_brightness(struct backlight_device *bl)
  39. {
  40. return bl->props.brightness;
  41. }
  42. static int gpio_backlight_check_fb(struct backlight_device *bl,
  43. struct fb_info *info)
  44. {
  45. struct gpio_backlight *gbl = bl_get_data(bl);
  46. return gbl->fbdev == NULL || gbl->fbdev == info->dev;
  47. }
  48. static const struct backlight_ops gpio_backlight_ops = {
  49. .options = BL_CORE_SUSPENDRESUME,
  50. .update_status = gpio_backlight_update_status,
  51. .get_brightness = gpio_backlight_get_brightness,
  52. .check_fb = gpio_backlight_check_fb,
  53. };
  54. static int gpio_backlight_probe_dt(struct platform_device *pdev,
  55. struct gpio_backlight *gbl)
  56. {
  57. struct device_node *np = pdev->dev.of_node;
  58. enum of_gpio_flags gpio_flags;
  59. gbl->gpio = of_get_gpio_flags(np, 0, &gpio_flags);
  60. if (!gpio_is_valid(gbl->gpio)) {
  61. if (gbl->gpio != -EPROBE_DEFER) {
  62. dev_err(&pdev->dev,
  63. "Error: The gpios parameter is missing or invalid.\n");
  64. }
  65. return gbl->gpio;
  66. }
  67. gbl->active = (gpio_flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1;
  68. gbl->def_value = of_property_read_bool(np, "default-on");
  69. return 0;
  70. }
  71. static int gpio_backlight_probe(struct platform_device *pdev)
  72. {
  73. struct gpio_backlight_platform_data *pdata =
  74. dev_get_platdata(&pdev->dev);
  75. struct backlight_properties props;
  76. struct backlight_device *bl;
  77. struct gpio_backlight *gbl;
  78. struct device_node *np = pdev->dev.of_node;
  79. int ret;
  80. if (!pdata && !np) {
  81. dev_err(&pdev->dev,
  82. "failed to find platform data or device tree node.\n");
  83. return -ENODEV;
  84. }
  85. gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
  86. if (gbl == NULL)
  87. return -ENOMEM;
  88. gbl->dev = &pdev->dev;
  89. if (np) {
  90. ret = gpio_backlight_probe_dt(pdev, gbl);
  91. if (ret)
  92. return ret;
  93. } else {
  94. gbl->fbdev = pdata->fbdev;
  95. gbl->gpio = pdata->gpio;
  96. gbl->active = pdata->active_low ? 0 : 1;
  97. gbl->def_value = pdata->def_value;
  98. }
  99. ret = devm_gpio_request_one(gbl->dev, gbl->gpio, GPIOF_DIR_OUT |
  100. (gbl->active ? GPIOF_INIT_LOW
  101. : GPIOF_INIT_HIGH),
  102. pdata ? pdata->name : "backlight");
  103. if (ret < 0) {
  104. dev_err(&pdev->dev, "unable to request GPIO\n");
  105. return ret;
  106. }
  107. memset(&props, 0, sizeof(props));
  108. props.type = BACKLIGHT_RAW;
  109. props.max_brightness = 1;
  110. bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
  111. &pdev->dev, gbl, &gpio_backlight_ops,
  112. &props);
  113. if (IS_ERR(bl)) {
  114. dev_err(&pdev->dev, "failed to register backlight\n");
  115. return PTR_ERR(bl);
  116. }
  117. bl->props.brightness = gbl->def_value;
  118. backlight_update_status(bl);
  119. platform_set_drvdata(pdev, bl);
  120. return 0;
  121. }
  122. #ifdef CONFIG_OF
  123. static struct of_device_id gpio_backlight_of_match[] = {
  124. { .compatible = "gpio-backlight" },
  125. { /* sentinel */ }
  126. };
  127. #endif
  128. static struct platform_driver gpio_backlight_driver = {
  129. .driver = {
  130. .name = "gpio-backlight",
  131. .owner = THIS_MODULE,
  132. .of_match_table = of_match_ptr(gpio_backlight_of_match),
  133. },
  134. .probe = gpio_backlight_probe,
  135. };
  136. module_platform_driver(gpio_backlight_driver);
  137. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  138. MODULE_DESCRIPTION("GPIO-based Backlight Driver");
  139. MODULE_LICENSE("GPL");
  140. MODULE_ALIAS("platform:gpio-backlight");