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