gpio_backlight.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_cansleep(gbl->gpio,
  36. brightness ? gbl->active : !gbl->active);
  37. return 0;
  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. .check_fb = gpio_backlight_check_fb,
  49. };
  50. static int gpio_backlight_probe_dt(struct platform_device *pdev,
  51. struct gpio_backlight *gbl)
  52. {
  53. struct device_node *np = pdev->dev.of_node;
  54. enum of_gpio_flags gpio_flags;
  55. gbl->gpio = of_get_gpio_flags(np, 0, &gpio_flags);
  56. if (!gpio_is_valid(gbl->gpio)) {
  57. if (gbl->gpio != -EPROBE_DEFER) {
  58. dev_err(&pdev->dev,
  59. "Error: The gpios parameter is missing or invalid.\n");
  60. }
  61. return gbl->gpio;
  62. }
  63. gbl->active = (gpio_flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1;
  64. gbl->def_value = of_property_read_bool(np, "default-on");
  65. return 0;
  66. }
  67. static int gpio_backlight_probe(struct platform_device *pdev)
  68. {
  69. struct gpio_backlight_platform_data *pdata =
  70. dev_get_platdata(&pdev->dev);
  71. struct backlight_properties props;
  72. struct backlight_device *bl;
  73. struct gpio_backlight *gbl;
  74. struct device_node *np = pdev->dev.of_node;
  75. int ret;
  76. if (!pdata && !np) {
  77. dev_err(&pdev->dev,
  78. "failed to find platform data or device tree node.\n");
  79. return -ENODEV;
  80. }
  81. gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
  82. if (gbl == NULL)
  83. return -ENOMEM;
  84. gbl->dev = &pdev->dev;
  85. if (np) {
  86. ret = gpio_backlight_probe_dt(pdev, gbl);
  87. if (ret)
  88. return ret;
  89. } else {
  90. gbl->fbdev = pdata->fbdev;
  91. gbl->gpio = pdata->gpio;
  92. gbl->active = pdata->active_low ? 0 : 1;
  93. gbl->def_value = pdata->def_value;
  94. }
  95. ret = devm_gpio_request_one(gbl->dev, gbl->gpio, GPIOF_DIR_OUT |
  96. (gbl->active ? GPIOF_INIT_LOW
  97. : GPIOF_INIT_HIGH),
  98. pdata ? pdata->name : "backlight");
  99. if (ret < 0) {
  100. dev_err(&pdev->dev, "unable to request GPIO\n");
  101. return ret;
  102. }
  103. memset(&props, 0, sizeof(props));
  104. props.type = BACKLIGHT_RAW;
  105. props.max_brightness = 1;
  106. bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
  107. &pdev->dev, gbl, &gpio_backlight_ops,
  108. &props);
  109. if (IS_ERR(bl)) {
  110. dev_err(&pdev->dev, "failed to register backlight\n");
  111. return PTR_ERR(bl);
  112. }
  113. bl->props.brightness = gbl->def_value;
  114. backlight_update_status(bl);
  115. platform_set_drvdata(pdev, bl);
  116. return 0;
  117. }
  118. #ifdef CONFIG_OF
  119. static struct of_device_id gpio_backlight_of_match[] = {
  120. { .compatible = "gpio-backlight" },
  121. { /* sentinel */ }
  122. };
  123. MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
  124. #endif
  125. static struct platform_driver gpio_backlight_driver = {
  126. .driver = {
  127. .name = "gpio-backlight",
  128. .of_match_table = of_match_ptr(gpio_backlight_of_match),
  129. },
  130. .probe = gpio_backlight_probe,
  131. };
  132. module_platform_driver(gpio_backlight_driver);
  133. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  134. MODULE_DESCRIPTION("GPIO-based Backlight Driver");
  135. MODULE_LICENSE("GPL");
  136. MODULE_ALIAS("platform:gpio-backlight");