David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * gpio_backlight.c - Simple GPIO-controlled backlight |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/backlight.h> |
| 7 | #include <linux/err.h> |
| 8 | #include <linux/fb.h> |
| 9 | #include <linux/gpio.h> /* Only for legacy support */ |
| 10 | #include <linux/gpio/consumer.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_gpio.h> |
| 16 | #include <linux/platform_data/gpio_backlight.h> |
| 17 | #include <linux/platform_device.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 18 | #include <linux/property.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | |
| 21 | struct gpio_backlight { |
| 22 | struct device *dev; |
| 23 | struct device *fbdev; |
| 24 | |
| 25 | struct gpio_desc *gpiod; |
| 26 | int def_value; |
| 27 | }; |
| 28 | |
| 29 | static int gpio_backlight_update_status(struct backlight_device *bl) |
| 30 | { |
| 31 | struct gpio_backlight *gbl = bl_get_data(bl); |
| 32 | int brightness = bl->props.brightness; |
| 33 | |
| 34 | if (bl->props.power != FB_BLANK_UNBLANK || |
| 35 | bl->props.fb_blank != FB_BLANK_UNBLANK || |
| 36 | bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) |
| 37 | brightness = 0; |
| 38 | |
| 39 | gpiod_set_value_cansleep(gbl->gpiod, brightness); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int gpio_backlight_check_fb(struct backlight_device *bl, |
| 45 | struct fb_info *info) |
| 46 | { |
| 47 | struct gpio_backlight *gbl = bl_get_data(bl); |
| 48 | |
| 49 | return gbl->fbdev == NULL || gbl->fbdev == info->dev; |
| 50 | } |
| 51 | |
| 52 | static const struct backlight_ops gpio_backlight_ops = { |
| 53 | .options = BL_CORE_SUSPENDRESUME, |
| 54 | .update_status = gpio_backlight_update_status, |
| 55 | .check_fb = gpio_backlight_check_fb, |
| 56 | }; |
| 57 | |
| 58 | static int gpio_backlight_probe_dt(struct platform_device *pdev, |
| 59 | struct gpio_backlight *gbl) |
| 60 | { |
| 61 | struct device *dev = &pdev->dev; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 62 | int ret; |
| 63 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 64 | gbl->def_value = device_property_read_bool(dev, "default-on"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 66 | gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | if (IS_ERR(gbl->gpiod)) { |
| 68 | ret = PTR_ERR(gbl->gpiod); |
| 69 | |
| 70 | if (ret != -EPROBE_DEFER) { |
| 71 | dev_err(dev, |
| 72 | "Error: The gpios parameter is missing or invalid.\n"); |
| 73 | } |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 80 | static int gpio_backlight_initial_power_state(struct gpio_backlight *gbl) |
| 81 | { |
| 82 | struct device_node *node = gbl->dev->of_node; |
| 83 | |
| 84 | /* Not booted with device tree or no phandle link to the node */ |
| 85 | if (!node || !node->phandle) |
| 86 | return gbl->def_value ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; |
| 87 | |
| 88 | /* if the enable GPIO is disabled, do not enable the backlight */ |
| 89 | if (gpiod_get_value_cansleep(gbl->gpiod) == 0) |
| 90 | return FB_BLANK_POWERDOWN; |
| 91 | |
| 92 | return FB_BLANK_UNBLANK; |
| 93 | } |
| 94 | |
| 95 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 96 | static int gpio_backlight_probe(struct platform_device *pdev) |
| 97 | { |
| 98 | struct gpio_backlight_platform_data *pdata = |
| 99 | dev_get_platdata(&pdev->dev); |
| 100 | struct backlight_properties props; |
| 101 | struct backlight_device *bl; |
| 102 | struct gpio_backlight *gbl; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 103 | int ret; |
| 104 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 105 | gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL); |
| 106 | if (gbl == NULL) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | gbl->dev = &pdev->dev; |
| 110 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 111 | if (pdev->dev.fwnode) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 112 | ret = gpio_backlight_probe_dt(pdev, gbl); |
| 113 | if (ret) |
| 114 | return ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 115 | } else if (pdata) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 116 | /* |
| 117 | * Legacy platform data GPIO retrieveal. Do not expand |
| 118 | * the use of this code path, currently only used by one |
| 119 | * SH board. |
| 120 | */ |
| 121 | unsigned long flags = GPIOF_DIR_OUT; |
| 122 | |
| 123 | gbl->fbdev = pdata->fbdev; |
| 124 | gbl->def_value = pdata->def_value; |
| 125 | flags |= gbl->def_value ? GPIOF_INIT_HIGH : GPIOF_INIT_LOW; |
| 126 | |
| 127 | ret = devm_gpio_request_one(gbl->dev, pdata->gpio, flags, |
| 128 | pdata ? pdata->name : "backlight"); |
| 129 | if (ret < 0) { |
| 130 | dev_err(&pdev->dev, "unable to request GPIO\n"); |
| 131 | return ret; |
| 132 | } |
| 133 | gbl->gpiod = gpio_to_desc(pdata->gpio); |
| 134 | if (!gbl->gpiod) |
| 135 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 136 | } else { |
| 137 | dev_err(&pdev->dev, |
| 138 | "failed to find platform data or device tree node.\n"); |
| 139 | return -ENODEV; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | memset(&props, 0, sizeof(props)); |
| 143 | props.type = BACKLIGHT_RAW; |
| 144 | props.max_brightness = 1; |
| 145 | bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev), |
| 146 | &pdev->dev, gbl, &gpio_backlight_ops, |
| 147 | &props); |
| 148 | if (IS_ERR(bl)) { |
| 149 | dev_err(&pdev->dev, "failed to register backlight\n"); |
| 150 | return PTR_ERR(bl); |
| 151 | } |
| 152 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 153 | bl->props.power = gpio_backlight_initial_power_state(gbl); |
| 154 | bl->props.brightness = 1; |
| 155 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 156 | backlight_update_status(bl); |
| 157 | |
| 158 | platform_set_drvdata(pdev, bl); |
| 159 | return 0; |
| 160 | } |
| 161 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 162 | static struct of_device_id gpio_backlight_of_match[] = { |
| 163 | { .compatible = "gpio-backlight" }, |
| 164 | { /* sentinel */ } |
| 165 | }; |
| 166 | |
| 167 | MODULE_DEVICE_TABLE(of, gpio_backlight_of_match); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 168 | |
| 169 | static struct platform_driver gpio_backlight_driver = { |
| 170 | .driver = { |
| 171 | .name = "gpio-backlight", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 172 | .of_match_table = gpio_backlight_of_match, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 173 | }, |
| 174 | .probe = gpio_backlight_probe, |
| 175 | }; |
| 176 | |
| 177 | module_platform_driver(gpio_backlight_driver); |
| 178 | |
| 179 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); |
| 180 | MODULE_DESCRIPTION("GPIO-based Backlight Driver"); |
| 181 | MODULE_LICENSE("GPL"); |
| 182 | MODULE_ALIAS("platform:gpio-backlight"); |