gpio_backlight.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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> /* Only for legacy support */
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/platform_data/gpio_backlight.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. struct gpio_backlight {
  22. struct device *dev;
  23. struct device *fbdev;
  24. struct gpio_desc *gpiod;
  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. gpiod_set_value_cansleep(gbl->gpiod, brightness);
  36. return 0;
  37. }
  38. static int gpio_backlight_check_fb(struct backlight_device *bl,
  39. struct fb_info *info)
  40. {
  41. struct gpio_backlight *gbl = bl_get_data(bl);
  42. return gbl->fbdev == NULL || gbl->fbdev == info->dev;
  43. }
  44. static const struct backlight_ops gpio_backlight_ops = {
  45. .options = BL_CORE_SUSPENDRESUME,
  46. .update_status = gpio_backlight_update_status,
  47. .check_fb = gpio_backlight_check_fb,
  48. };
  49. static int gpio_backlight_probe_dt(struct platform_device *pdev,
  50. struct gpio_backlight *gbl)
  51. {
  52. struct device *dev = &pdev->dev;
  53. struct device_node *np = dev->of_node;
  54. enum gpiod_flags flags;
  55. int ret;
  56. gbl->def_value = of_property_read_bool(np, "default-on");
  57. flags = gbl->def_value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  58. gbl->gpiod = devm_gpiod_get(dev, NULL, flags);
  59. if (IS_ERR(gbl->gpiod)) {
  60. ret = PTR_ERR(gbl->gpiod);
  61. if (ret != -EPROBE_DEFER) {
  62. dev_err(dev,
  63. "Error: The gpios parameter is missing or invalid.\n");
  64. }
  65. return ret;
  66. }
  67. return 0;
  68. }
  69. static int gpio_backlight_probe(struct platform_device *pdev)
  70. {
  71. struct gpio_backlight_platform_data *pdata =
  72. dev_get_platdata(&pdev->dev);
  73. struct backlight_properties props;
  74. struct backlight_device *bl;
  75. struct gpio_backlight *gbl;
  76. struct device_node *np = pdev->dev.of_node;
  77. int ret;
  78. if (!pdata && !np) {
  79. dev_err(&pdev->dev,
  80. "failed to find platform data or device tree node.\n");
  81. return -ENODEV;
  82. }
  83. gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL);
  84. if (gbl == NULL)
  85. return -ENOMEM;
  86. gbl->dev = &pdev->dev;
  87. if (np) {
  88. ret = gpio_backlight_probe_dt(pdev, gbl);
  89. if (ret)
  90. return ret;
  91. } else {
  92. /*
  93. * Legacy platform data GPIO retrieveal. Do not expand
  94. * the use of this code path, currently only used by one
  95. * SH board.
  96. */
  97. unsigned long flags = GPIOF_DIR_OUT;
  98. gbl->fbdev = pdata->fbdev;
  99. gbl->def_value = pdata->def_value;
  100. flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW;
  101. ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags,
  102. pdata ? pdata->name : "backlight");
  103. if (ret < 0) {
  104. dev_err(&pdev->dev, "unable to request GPIO\n");
  105. return ret;
  106. }
  107. gbl->gpiod = gpio_to_desc(pdata->gpio);
  108. if (!gbl->gpiod)
  109. return -EINVAL;
  110. }
  111. memset(&props, 0, sizeof(props));
  112. props.type = BACKLIGHT_RAW;
  113. props.max_brightness = 1;
  114. bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
  115. &pdev->dev, gbl, &gpio_backlight_ops,
  116. &props);
  117. if (IS_ERR(bl)) {
  118. dev_err(&pdev->dev, "failed to register backlight\n");
  119. return PTR_ERR(bl);
  120. }
  121. bl->props.brightness = gbl->def_value;
  122. backlight_update_status(bl);
  123. platform_set_drvdata(pdev, bl);
  124. return 0;
  125. }
  126. #ifdef CONFIG_OF
  127. static struct of_device_id gpio_backlight_of_match[] = {
  128. { .compatible = "gpio-backlight" },
  129. { /* sentinel */ }
  130. };
  131. MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
  132. #endif
  133. static struct platform_driver gpio_backlight_driver = {
  134. .driver = {
  135. .name = "gpio-backlight",
  136. .of_match_table = of_match_ptr(gpio_backlight_of_match),
  137. },
  138. .probe = gpio_backlight_probe,
  139. };
  140. module_platform_driver(gpio_backlight_driver);
  141. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  142. MODULE_DESCRIPTION("GPIO-based Backlight Driver");
  143. MODULE_LICENSE("GPL");
  144. MODULE_ALIAS("platform:gpio-backlight");