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 9 #include <linux/backlight.h> 10 #include <linux/err.h> 11 #include <linux/fb.h> 12 #include <linux/gpio.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 22 struct gpio_backlight { 23 struct device *dev; 24 struct device *fbdev; 25 26 int gpio; 27 int active; 28 int def_value; 29 }; 30 31 static int gpio_backlight_update_status(struct backlight_device *bl) 32 { 33 struct gpio_backlight *gbl = bl_get_data(bl); 34 int brightness = bl->props.brightness; 35 36 if (bl->props.power != FB_BLANK_UNBLANK || 37 bl->props.fb_blank != FB_BLANK_UNBLANK || 38 bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) 39 brightness = 0; 40 41 gpio_set_value(gbl->gpio, brightness ? gbl->active : !gbl->active); 42 43 return 0; 44 } 45 46 static int gpio_backlight_get_brightness(struct backlight_device *bl) 47 { 48 return bl->props.brightness; 49 } 50 51 static int gpio_backlight_check_fb(struct backlight_device *bl, 52 struct fb_info *info) 53 { 54 struct gpio_backlight *gbl = bl_get_data(bl); 55 56 return gbl->fbdev == NULL || gbl->fbdev == info->dev; 57 } 58 59 static const struct backlight_ops gpio_backlight_ops = { 60 .options = BL_CORE_SUSPENDRESUME, 61 .update_status = gpio_backlight_update_status, 62 .get_brightness = gpio_backlight_get_brightness, 63 .check_fb = gpio_backlight_check_fb, 64 }; 65 66 static int gpio_backlight_probe_dt(struct platform_device *pdev, 67 struct gpio_backlight *gbl) 68 { 69 struct device_node *np = pdev->dev.of_node; 70 enum of_gpio_flags gpio_flags; 71 72 gbl->gpio = of_get_gpio_flags(np, 0, &gpio_flags); 73 74 if (!gpio_is_valid(gbl->gpio)) { 75 if (gbl->gpio != -EPROBE_DEFER) { 76 dev_err(&pdev->dev, 77 "Error: The gpios parameter is missing or invalid.\n"); 78 } 79 return gbl->gpio; 80 } 81 82 gbl->active = (gpio_flags & OF_GPIO_ACTIVE_LOW) ? 0 : 1; 83 84 gbl->def_value = of_property_read_bool(np, "default-on"); 85 86 return 0; 87 } 88 89 static int gpio_backlight_probe(struct platform_device *pdev) 90 { 91 struct gpio_backlight_platform_data *pdata = 92 dev_get_platdata(&pdev->dev); 93 struct backlight_properties props; 94 struct backlight_device *bl; 95 struct gpio_backlight *gbl; 96 struct device_node *np = pdev->dev.of_node; 97 int ret; 98 99 if (!pdata && !np) { 100 dev_err(&pdev->dev, 101 "failed to find platform data or device tree node.\n"); 102 return -ENODEV; 103 } 104 105 gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL); 106 if (gbl == NULL) 107 return -ENOMEM; 108 109 gbl->dev = &pdev->dev; 110 111 if (np) { 112 ret = gpio_backlight_probe_dt(pdev, gbl); 113 if (ret) 114 return ret; 115 } else { 116 gbl->fbdev = pdata->fbdev; 117 gbl->gpio = pdata->gpio; 118 gbl->active = pdata->active_low ? 0 : 1; 119 gbl->def_value = pdata->def_value; 120 } 121 122 ret = devm_gpio_request_one(gbl->dev, gbl->gpio, GPIOF_DIR_OUT | 123 (gbl->active ? GPIOF_INIT_LOW 124 : GPIOF_INIT_HIGH), 125 pdata ? pdata->name : "backlight"); 126 if (ret < 0) { 127 dev_err(&pdev->dev, "unable to request GPIO\n"); 128 return ret; 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 #endif 155 156 static struct platform_driver gpio_backlight_driver = { 157 .driver = { 158 .name = "gpio-backlight", 159 .owner = THIS_MODULE, 160 .of_match_table = of_match_ptr(gpio_backlight_of_match), 161 }, 162 .probe = gpio_backlight_probe, 163 }; 164 165 module_platform_driver(gpio_backlight_driver); 166 167 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 168 MODULE_DESCRIPTION("GPIO-based Backlight Driver"); 169 MODULE_LICENSE("GPL"); 170 MODULE_ALIAS("platform:gpio-backlight"); 171