xref: /OK3568_Linux_fs/u-boot/drivers/video/bridge/video-bridge-uclass.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <edid.h>
12 #include <video_bridge.h>
13 
video_bridge_set_backlight(struct udevice * dev,int percent)14 int video_bridge_set_backlight(struct udevice *dev, int percent)
15 {
16 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
17 
18 	if (!ops->set_backlight)
19 		return -ENOSYS;
20 
21 	return ops->set_backlight(dev, percent);
22 }
23 
video_bridge_attach(struct udevice * dev)24 int video_bridge_attach(struct udevice *dev)
25 {
26 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
27 
28 	if (!ops->attach)
29 		return -ENOSYS;
30 
31 	return ops->attach(dev);
32 }
33 
video_bridge_check_attached(struct udevice * dev)34 int video_bridge_check_attached(struct udevice *dev)
35 {
36 	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
37 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
38 	int ret;
39 
40 	if (!ops->check_attached) {
41 		ret = dm_gpio_get_value(&uc_priv->hotplug);
42 
43 		return ret > 0 ? 0 : ret == 0 ? -ENOTCONN : ret;
44 	}
45 
46 	return ops->check_attached(dev);
47 }
48 
video_bridge_read_edid(struct udevice * dev,u8 * buf,int buf_size)49 int video_bridge_read_edid(struct udevice *dev, u8 *buf, int buf_size)
50 {
51 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
52 
53 	if (!ops || !ops->read_edid)
54 		return -ENOSYS;
55 	return ops->read_edid(dev, buf, buf_size);
56 }
57 
video_bridge_get_timing(struct udevice * dev)58 int video_bridge_get_timing(struct udevice *dev)
59 {
60 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
61 
62 	if (!ops || !ops->get_timing)
63 		return -ENOSYS;
64 	return ops->get_timing(dev);
65 }
66 
video_bridge_pre_probe(struct udevice * dev)67 static int video_bridge_pre_probe(struct udevice *dev)
68 {
69 	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
70 	int ret;
71 
72 	debug("%s\n", __func__);
73 	ret = gpio_request_by_name(dev, "sleep-gpios", 0,
74 				   &uc_priv->sleep, GPIOD_IS_OUT);
75 	if (ret) {
76 		debug("%s: Could not decode sleep-gpios (%d)\n", __func__, ret);
77 		if (ret != -ENOENT)
78 			return ret;
79 	}
80 	/*
81 	 * Drop this for now as we do not have driver model pinctrl support
82 	 *
83 	 * ret = dm_gpio_set_pull(&uc_priv->sleep, GPIO_PULL_NONE);
84 	 * if (ret) {
85 	 *	debug("%s: Could not set sleep pull value\n", __func__);
86 	 *	return ret;
87 	 * }
88 	 */
89 	ret = gpio_request_by_name(dev, "reset-gpios", 0, &uc_priv->reset,
90 				   GPIOD_IS_OUT);
91 	if (ret) {
92 		debug("%s: Could not decode reset-gpios (%d)\n", __func__, ret);
93 		if (ret != -ENOENT)
94 			return ret;
95 	}
96 	/*
97 	 * Drop this for now as we do not have driver model pinctrl support
98 	 *
99 	 * ret = dm_gpio_set_pull(&uc_priv->reset, GPIO_PULL_NONE);
100 	 * if (ret) {
101 	 *	debug("%s: Could not set reset pull value\n", __func__);
102 	 *	return ret;
103 	 * }
104 	 */
105 	ret = gpio_request_by_name(dev, "hotplug-gpios", 0, &uc_priv->hotplug,
106 				   GPIOD_IS_IN);
107 	if (ret) {
108 		debug("%s: Could not decode hotplug (%d)\n", __func__, ret);
109 		if (ret != -ENOENT)
110 			return ret;
111 	}
112 
113 	return 0;
114 }
115 
video_bridge_set_active(struct udevice * dev,bool active)116 int video_bridge_set_active(struct udevice *dev, bool active)
117 {
118 	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
119 	int ret = 0;
120 
121 	debug("%s: %d\n", __func__, active);
122 	if (uc_priv->sleep.dev) {
123 		ret = dm_gpio_set_value(&uc_priv->sleep, !active);
124 		if (ret)
125 			return ret;
126 	}
127 
128 	if (!active)
129 		return 0;
130 
131 	if (uc_priv->reset.dev) {
132 		ret = dm_gpio_set_value(&uc_priv->reset, true);
133 		if (ret)
134 			return ret;
135 		udelay(10);
136 		ret = dm_gpio_set_value(&uc_priv->reset, false);
137 	}
138 
139 	return ret;
140 }
141 
142 UCLASS_DRIVER(video_bridge) = {
143 	.id		= UCLASS_VIDEO_BRIDGE,
144 	.name		= "video_bridge",
145 	.per_device_auto_alloc_size	= sizeof(struct video_bridge_priv),
146 	.pre_probe	= video_bridge_pre_probe,
147 };
148