gpio_backlight.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * gpio_backlight.c - Simple GPIO-controlled backlight
  4. */
  5. #include <linux/backlight.h>
  6. #include <linux/err.h>
  7. #include <linux/fb.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_data/gpio_backlight.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/slab.h>
  17. struct gpio_backlight {
  18. struct device *fbdev;
  19. struct gpio_desc *gpiod;
  20. };
  21. static int gpio_backlight_update_status(struct backlight_device *bl)
  22. {
  23. struct gpio_backlight *gbl = bl_get_data(bl);
  24. gpiod_set_value_cansleep(gbl->gpiod, backlight_get_brightness(bl));
  25. return 0;
  26. }
  27. static int gpio_backlight_check_fb(struct backlight_device *bl,
  28. struct fb_info *info)
  29. {
  30. struct gpio_backlight *gbl = bl_get_data(bl);
  31. return gbl->fbdev == NULL || gbl->fbdev == info->dev;
  32. }
  33. static const struct backlight_ops gpio_backlight_ops = {
  34. .options = BL_CORE_SUSPENDRESUME,
  35. .update_status = gpio_backlight_update_status,
  36. .check_fb = gpio_backlight_check_fb,
  37. };
  38. static int gpio_backlight_probe(struct platform_device *pdev)
  39. {
  40. struct device *dev = &pdev->dev;
  41. struct gpio_backlight_platform_data *pdata = dev_get_platdata(dev);
  42. struct device_node *of_node = dev->of_node;
  43. struct backlight_properties props;
  44. struct backlight_device *bl;
  45. struct gpio_backlight *gbl;
  46. int ret, init_brightness, def_value;
  47. gbl = devm_kzalloc(dev, sizeof(*gbl), GFP_KERNEL);
  48. if (gbl == NULL)
  49. return -ENOMEM;
  50. if (pdata)
  51. gbl->fbdev = pdata->fbdev;
  52. def_value = device_property_read_bool(dev, "default-on");
  53. gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
  54. if (IS_ERR(gbl->gpiod)) {
  55. ret = PTR_ERR(gbl->gpiod);
  56. if (ret != -EPROBE_DEFER)
  57. dev_err(dev,
  58. "Error: The gpios parameter is missing or invalid.\n");
  59. return ret;
  60. }
  61. memset(&props, 0, sizeof(props));
  62. props.type = BACKLIGHT_RAW;
  63. props.max_brightness = 1;
  64. bl = devm_backlight_device_register(dev, dev_name(dev), dev, gbl,
  65. &gpio_backlight_ops, &props);
  66. if (IS_ERR(bl)) {
  67. dev_err(dev, "failed to register backlight\n");
  68. return PTR_ERR(bl);
  69. }
  70. /* Set the initial power state */
  71. if (!of_node || !of_node->phandle)
  72. /* Not booted with device tree or no phandle link to the node */
  73. bl->props.power = def_value ? FB_BLANK_UNBLANK
  74. : FB_BLANK_POWERDOWN;
  75. else if (gpiod_get_direction(gbl->gpiod) == 0 &&
  76. gpiod_get_value_cansleep(gbl->gpiod) == 0)
  77. bl->props.power = FB_BLANK_POWERDOWN;
  78. else
  79. bl->props.power = FB_BLANK_UNBLANK;
  80. bl->props.brightness = 1;
  81. init_brightness = backlight_get_brightness(bl);
  82. ret = gpiod_direction_output(gbl->gpiod, init_brightness);
  83. if (ret) {
  84. dev_err(dev, "failed to set initial brightness\n");
  85. return ret;
  86. }
  87. platform_set_drvdata(pdev, bl);
  88. return 0;
  89. }
  90. static struct of_device_id gpio_backlight_of_match[] = {
  91. { .compatible = "gpio-backlight" },
  92. { /* sentinel */ }
  93. };
  94. MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
  95. static struct platform_driver gpio_backlight_driver = {
  96. .driver = {
  97. .name = "gpio-backlight",
  98. .of_match_table = gpio_backlight_of_match,
  99. },
  100. .probe = gpio_backlight_probe,
  101. };
  102. module_platform_driver(gpio_backlight_driver);
  103. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  104. MODULE_DESCRIPTION("GPIO-based Backlight Driver");
  105. MODULE_LICENSE("GPL");
  106. MODULE_ALIAS("platform:gpio-backlight");