xref: /rk3399_rockchip-uboot/drivers/video/backlight_gpio.c (revision e5f40fb7af642b1065d584d00eea7b2307d9db5b)
1*e5f40fb7SPatrick Delaunay /*
2*e5f40fb7SPatrick Delaunay  * Copyright (C) 2017, STMicroelectronics - All Rights Reserved
3*e5f40fb7SPatrick Delaunay  * Author: Patrick Delaunay <patrick.delaunay@st.com>
4*e5f40fb7SPatrick Delaunay  *
5*e5f40fb7SPatrick Delaunay  * SPDX-License-Identifier:	GPL-2.0+
6*e5f40fb7SPatrick Delaunay  */
7*e5f40fb7SPatrick Delaunay 
8*e5f40fb7SPatrick Delaunay #include <common.h>
9*e5f40fb7SPatrick Delaunay #include <dm.h>
10*e5f40fb7SPatrick Delaunay #include <backlight.h>
11*e5f40fb7SPatrick Delaunay #include <asm/gpio.h>
12*e5f40fb7SPatrick Delaunay 
13*e5f40fb7SPatrick Delaunay DECLARE_GLOBAL_DATA_PTR;
14*e5f40fb7SPatrick Delaunay 
15*e5f40fb7SPatrick Delaunay struct gpio_backlight_priv {
16*e5f40fb7SPatrick Delaunay 	struct gpio_desc gpio;
17*e5f40fb7SPatrick Delaunay 	bool def_value;
18*e5f40fb7SPatrick Delaunay };
19*e5f40fb7SPatrick Delaunay 
gpio_backlight_enable(struct udevice * dev)20*e5f40fb7SPatrick Delaunay static int gpio_backlight_enable(struct udevice *dev)
21*e5f40fb7SPatrick Delaunay {
22*e5f40fb7SPatrick Delaunay 	struct gpio_backlight_priv *priv = dev_get_priv(dev);
23*e5f40fb7SPatrick Delaunay 
24*e5f40fb7SPatrick Delaunay 	dm_gpio_set_value(&priv->gpio, 1);
25*e5f40fb7SPatrick Delaunay 
26*e5f40fb7SPatrick Delaunay 	return 0;
27*e5f40fb7SPatrick Delaunay }
28*e5f40fb7SPatrick Delaunay 
gpio_backlight_ofdata_to_platdata(struct udevice * dev)29*e5f40fb7SPatrick Delaunay static int gpio_backlight_ofdata_to_platdata(struct udevice *dev)
30*e5f40fb7SPatrick Delaunay {
31*e5f40fb7SPatrick Delaunay 	struct gpio_backlight_priv *priv = dev_get_priv(dev);
32*e5f40fb7SPatrick Delaunay 	int ret;
33*e5f40fb7SPatrick Delaunay 
34*e5f40fb7SPatrick Delaunay 	ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio,
35*e5f40fb7SPatrick Delaunay 				   GPIOD_IS_OUT);
36*e5f40fb7SPatrick Delaunay 	if (ret) {
37*e5f40fb7SPatrick Delaunay 		debug("%s: Warning: cannot get GPIO: ret=%d\n",
38*e5f40fb7SPatrick Delaunay 		      __func__, ret);
39*e5f40fb7SPatrick Delaunay 		return ret;
40*e5f40fb7SPatrick Delaunay 	}
41*e5f40fb7SPatrick Delaunay 
42*e5f40fb7SPatrick Delaunay 	priv->def_value = dev_read_bool(dev, "default-on");
43*e5f40fb7SPatrick Delaunay 
44*e5f40fb7SPatrick Delaunay 	return 0;
45*e5f40fb7SPatrick Delaunay }
46*e5f40fb7SPatrick Delaunay 
gpio_backlight_probe(struct udevice * dev)47*e5f40fb7SPatrick Delaunay static int gpio_backlight_probe(struct udevice *dev)
48*e5f40fb7SPatrick Delaunay {
49*e5f40fb7SPatrick Delaunay 	struct gpio_backlight_priv *priv = dev_get_priv(dev);
50*e5f40fb7SPatrick Delaunay 
51*e5f40fb7SPatrick Delaunay 	if (priv->def_value)
52*e5f40fb7SPatrick Delaunay 		gpio_backlight_enable(dev);
53*e5f40fb7SPatrick Delaunay 
54*e5f40fb7SPatrick Delaunay 	return 0;
55*e5f40fb7SPatrick Delaunay }
56*e5f40fb7SPatrick Delaunay 
57*e5f40fb7SPatrick Delaunay static const struct backlight_ops gpio_backlight_ops = {
58*e5f40fb7SPatrick Delaunay 	.enable	= gpio_backlight_enable,
59*e5f40fb7SPatrick Delaunay };
60*e5f40fb7SPatrick Delaunay 
61*e5f40fb7SPatrick Delaunay static const struct udevice_id gpio_backlight_ids[] = {
62*e5f40fb7SPatrick Delaunay 	{ .compatible = "gpio-backlight" },
63*e5f40fb7SPatrick Delaunay 	{ }
64*e5f40fb7SPatrick Delaunay };
65*e5f40fb7SPatrick Delaunay 
66*e5f40fb7SPatrick Delaunay U_BOOT_DRIVER(gpio_backlight) = {
67*e5f40fb7SPatrick Delaunay 	.name	= "gpio_backlight",
68*e5f40fb7SPatrick Delaunay 	.id	= UCLASS_PANEL_BACKLIGHT,
69*e5f40fb7SPatrick Delaunay 	.of_match = gpio_backlight_ids,
70*e5f40fb7SPatrick Delaunay 	.ops	= &gpio_backlight_ops,
71*e5f40fb7SPatrick Delaunay 	.ofdata_to_platdata	= gpio_backlight_ofdata_to_platdata,
72*e5f40fb7SPatrick Delaunay 	.probe		= gpio_backlight_probe,
73*e5f40fb7SPatrick Delaunay 	.priv_auto_alloc_size	= sizeof(struct gpio_backlight_priv),
74*e5f40fb7SPatrick Delaunay };
75