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