treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / video / backlight / gpio_backlight.c
blob75409ddfba3eb7355ff74a160f7bbabd810c6ab2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * gpio_backlight.c - Simple GPIO-controlled backlight
4 */
6 #include <linux/backlight.h>
7 #include <linux/err.h>
8 #include <linux/fb.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_data/gpio_backlight.h>
15 #include <linux/platform_device.h>
16 #include <linux/property.h>
17 #include <linux/slab.h>
19 struct gpio_backlight {
20 struct device *fbdev;
21 struct gpio_desc *gpiod;
24 static int gpio_backlight_get_next_brightness(struct backlight_device *bl)
26 int brightness = bl->props.brightness;
28 if (bl->props.power != FB_BLANK_UNBLANK ||
29 bl->props.fb_blank != FB_BLANK_UNBLANK ||
30 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
31 brightness = 0;
33 return brightness;
36 static int gpio_backlight_update_status(struct backlight_device *bl)
38 struct gpio_backlight *gbl = bl_get_data(bl);
39 int brightness = gpio_backlight_get_next_brightness(bl);
41 gpiod_set_value_cansleep(gbl->gpiod, brightness);
43 return 0;
46 static int gpio_backlight_check_fb(struct backlight_device *bl,
47 struct fb_info *info)
49 struct gpio_backlight *gbl = bl_get_data(bl);
51 return gbl->fbdev == NULL || gbl->fbdev == info->dev;
54 static const struct backlight_ops gpio_backlight_ops = {
55 .options = BL_CORE_SUSPENDRESUME,
56 .update_status = gpio_backlight_update_status,
57 .check_fb = gpio_backlight_check_fb,
60 static int gpio_backlight_probe(struct platform_device *pdev)
62 struct device *dev = &pdev->dev;
63 struct gpio_backlight_platform_data *pdata = dev_get_platdata(dev);
64 struct device_node *of_node = dev->of_node;
65 struct backlight_properties props;
66 struct backlight_device *bl;
67 struct gpio_backlight *gbl;
68 int ret, init_brightness, def_value;
70 gbl = devm_kzalloc(dev, sizeof(*gbl), GFP_KERNEL);
71 if (gbl == NULL)
72 return -ENOMEM;
74 if (pdata)
75 gbl->fbdev = pdata->fbdev;
77 def_value = device_property_read_bool(dev, "default-on");
79 gbl->gpiod = devm_gpiod_get(dev, NULL, GPIOD_ASIS);
80 if (IS_ERR(gbl->gpiod)) {
81 ret = PTR_ERR(gbl->gpiod);
82 if (ret != -EPROBE_DEFER)
83 dev_err(dev,
84 "Error: The gpios parameter is missing or invalid.\n");
85 return ret;
88 memset(&props, 0, sizeof(props));
89 props.type = BACKLIGHT_RAW;
90 props.max_brightness = 1;
91 bl = devm_backlight_device_register(dev, dev_name(dev), dev, gbl,
92 &gpio_backlight_ops, &props);
93 if (IS_ERR(bl)) {
94 dev_err(dev, "failed to register backlight\n");
95 return PTR_ERR(bl);
98 /* Set the initial power state */
99 if (!of_node || !of_node->phandle)
100 /* Not booted with device tree or no phandle link to the node */
101 bl->props.power = def_value ? FB_BLANK_UNBLANK
102 : FB_BLANK_POWERDOWN;
103 else if (gpiod_get_direction(gbl->gpiod) == 0 &&
104 gpiod_get_value_cansleep(gbl->gpiod) == 0)
105 bl->props.power = FB_BLANK_POWERDOWN;
106 else
107 bl->props.power = FB_BLANK_UNBLANK;
109 bl->props.brightness = 1;
111 init_brightness = gpio_backlight_get_next_brightness(bl);
112 ret = gpiod_direction_output(gbl->gpiod, init_brightness);
113 if (ret) {
114 dev_err(dev, "failed to set initial brightness\n");
115 return ret;
118 platform_set_drvdata(pdev, bl);
119 return 0;
122 static struct of_device_id gpio_backlight_of_match[] = {
123 { .compatible = "gpio-backlight" },
124 { /* sentinel */ }
127 MODULE_DEVICE_TABLE(of, gpio_backlight_of_match);
129 static struct platform_driver gpio_backlight_driver = {
130 .driver = {
131 .name = "gpio-backlight",
132 .of_match_table = gpio_backlight_of_match,
134 .probe = gpio_backlight_probe,
137 module_platform_driver(gpio_backlight_driver);
139 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
140 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
141 MODULE_LICENSE("GPL");
142 MODULE_ALIAS("platform:gpio-backlight");