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