gpio_backlight.c 3.3 KB

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