1 // SPDX-License-Identifier: GPL-2.0-only
3 * gpio_backlight.c - Simple GPIO-controlled backlight
6 #include <linux/backlight.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
13 #include <linux/platform_data/gpio_backlight.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/slab.h>
18 struct gpio_backlight
{
20 struct gpio_desc
*gpiod
;
23 static int gpio_backlight_update_status(struct backlight_device
*bl
)
25 struct gpio_backlight
*gbl
= bl_get_data(bl
);
27 gpiod_set_value_cansleep(gbl
->gpiod
, backlight_get_brightness(bl
));
32 static bool gpio_backlight_controls_device(struct backlight_device
*bl
,
33 struct device
*display_dev
)
35 struct gpio_backlight
*gbl
= bl_get_data(bl
);
37 return !gbl
->dev
|| gbl
->dev
== display_dev
;
40 static const struct backlight_ops gpio_backlight_ops
= {
41 .options
= BL_CORE_SUSPENDRESUME
,
42 .update_status
= gpio_backlight_update_status
,
43 .controls_device
= gpio_backlight_controls_device
,
46 static int gpio_backlight_probe(struct platform_device
*pdev
)
48 struct device
*dev
= &pdev
->dev
;
49 struct gpio_backlight_platform_data
*pdata
= dev_get_platdata(dev
);
50 struct device_node
*of_node
= dev
->of_node
;
51 struct backlight_properties props
;
52 struct backlight_device
*bl
;
53 struct gpio_backlight
*gbl
;
54 int ret
, init_brightness
, def_value
;
56 gbl
= devm_kzalloc(dev
, sizeof(*gbl
), GFP_KERNEL
);
61 gbl
->dev
= pdata
->dev
;
63 def_value
= device_property_read_bool(dev
, "default-on");
65 gbl
->gpiod
= devm_gpiod_get(dev
, NULL
, GPIOD_ASIS
);
66 if (IS_ERR(gbl
->gpiod
))
67 return dev_err_probe(dev
, PTR_ERR(gbl
->gpiod
),
68 "The gpios parameter is missing or invalid\n");
70 memset(&props
, 0, sizeof(props
));
71 props
.type
= BACKLIGHT_RAW
;
72 props
.max_brightness
= 1;
73 bl
= devm_backlight_device_register(dev
, dev_name(dev
), dev
, gbl
,
74 &gpio_backlight_ops
, &props
);
76 dev_err(dev
, "failed to register backlight\n");
80 /* Set the initial power state */
81 if (!of_node
|| !of_node
->phandle
)
82 /* Not booted with device tree or no phandle link to the node */
83 bl
->props
.power
= def_value
? BACKLIGHT_POWER_ON
84 : BACKLIGHT_POWER_OFF
;
85 else if (gpiod_get_value_cansleep(gbl
->gpiod
) == 0)
86 bl
->props
.power
= BACKLIGHT_POWER_OFF
;
88 bl
->props
.power
= BACKLIGHT_POWER_ON
;
90 bl
->props
.brightness
= 1;
92 init_brightness
= backlight_get_brightness(bl
);
93 ret
= gpiod_direction_output(gbl
->gpiod
, init_brightness
);
95 dev_err(dev
, "failed to set initial brightness\n");
99 platform_set_drvdata(pdev
, bl
);
103 static struct of_device_id gpio_backlight_of_match
[] = {
104 { .compatible
= "gpio-backlight" },
108 MODULE_DEVICE_TABLE(of
, gpio_backlight_of_match
);
110 static struct platform_driver gpio_backlight_driver
= {
112 .name
= "gpio-backlight",
113 .of_match_table
= gpio_backlight_of_match
,
115 .probe
= gpio_backlight_probe
,
118 module_platform_driver(gpio_backlight_driver
);
120 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
121 MODULE_DESCRIPTION("GPIO-based Backlight Driver");
122 MODULE_LICENSE("GPL");
123 MODULE_ALIAS("platform:gpio-backlight");