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 <video_bridge.h>
12 
13 int video_bridge_set_backlight(struct udevice *dev, int percent)
14 {
15 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
16 
17 	if (!ops->set_backlight)
18 		return -ENOSYS;
19 
20 	return ops->set_backlight(dev, percent);
21 }
22 
23 int video_bridge_attach(struct udevice *dev)
24 {
25 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
26 
27 	if (!ops->attach)
28 		return -ENOSYS;
29 
30 	return ops->attach(dev);
31 }
32 
33 int video_bridge_check_attached(struct udevice *dev)
34 {
35 	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
36 	struct video_bridge_ops *ops = video_bridge_get_ops(dev);
37 	int ret;
38 
39 	if (!ops->check_attached) {
40 		ret = dm_gpio_get_value(&uc_priv->hotplug);
41 
42 		return ret > 0 ? 0 : ret == 0 ? -ENOTCONN : ret;
43 	}
44 
45 	return ops->check_attached(dev);
46 }
47 
48 static int video_bridge_pre_probe(struct udevice *dev)
49 {
50 	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
51 	int ret;
52 
53 	debug("%s\n", __func__);
54 	ret = gpio_request_by_name(dev, "sleep-gpios", 0,
55 				   &uc_priv->sleep, GPIOD_IS_OUT);
56 	if (ret) {
57 		debug("%s: Could not decode sleep-gpios (%d)\n", __func__, ret);
58 		if (ret != -ENOENT)
59 			return ret;
60 	}
61 	/*
62 	 * Drop this for now as we do not have driver model pinctrl support
63 	 *
64 	 * ret = dm_gpio_set_pull(&uc_priv->sleep, GPIO_PULL_NONE);
65 	 * if (ret) {
66 	 *	debug("%s: Could not set sleep pull value\n", __func__);
67 	 *	return ret;
68 	 * }
69 	 */
70 	ret = gpio_request_by_name(dev, "reset-gpios", 0, &uc_priv->reset,
71 				   GPIOD_IS_OUT);
72 	if (ret) {
73 		debug("%s: Could not decode reset-gpios (%d)\n", __func__, ret);
74 		if (ret != -ENOENT)
75 			return ret;
76 	}
77 	/*
78 	 * Drop this for now as we do not have driver model pinctrl support
79 	 *
80 	 * ret = dm_gpio_set_pull(&uc_priv->reset, GPIO_PULL_NONE);
81 	 * if (ret) {
82 	 *	debug("%s: Could not set reset pull value\n", __func__);
83 	 *	return ret;
84 	 * }
85 	 */
86 	ret = gpio_request_by_name(dev, "hotplug-gpios", 0, &uc_priv->hotplug,
87 				   GPIOD_IS_IN);
88 	if (ret) {
89 		debug("%s: Could not decode hotplug (%d)\n", __func__, ret);
90 		if (ret != -ENOENT)
91 			return ret;
92 	}
93 
94 	return 0;
95 }
96 
97 int video_bridge_set_active(struct udevice *dev, bool active)
98 {
99 	struct video_bridge_priv *uc_priv = dev_get_uclass_priv(dev);
100 	int ret;
101 
102 	debug("%s: %d\n", __func__, active);
103 	ret = dm_gpio_set_value(&uc_priv->sleep, !active);
104 	if (ret)
105 		return ret;
106 	if (active) {
107 		ret = dm_gpio_set_value(&uc_priv->reset, true);
108 		if (ret)
109 			return ret;
110 		udelay(10);
111 		ret = dm_gpio_set_value(&uc_priv->reset, false);
112 	}
113 
114 	return ret;
115 }
116 
117 UCLASS_DRIVER(video_bridge) = {
118 	.id		= UCLASS_VIDEO_BRIDGE,
119 	.name		= "video_bridge",
120 	.per_device_auto_alloc_size	= sizeof(struct video_bridge_priv),
121 	.pre_probe	= video_bridge_pre_probe,
122 };
123