Merge tag 'drm-next-5.5-2019-11-22' of git://people.freedesktop.org/~agd5f/linux...
[sfrench/cifs-2.6.git] / drivers / media / platform / cec-gpio / cec-gpio.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4  */
5
6 #include <linux/module.h>
7 #include <linux/interrupt.h>
8 #include <linux/delay.h>
9 #include <linux/platform_device.h>
10 #include <linux/gpio/consumer.h>
11 #include <media/cec-pin.h>
12
13 struct cec_gpio {
14         struct cec_adapter      *adap;
15         struct device           *dev;
16
17         struct gpio_desc        *cec_gpio;
18         int                     cec_irq;
19         bool                    cec_is_low;
20
21         struct gpio_desc        *hpd_gpio;
22         int                     hpd_irq;
23         bool                    hpd_is_high;
24         ktime_t                 hpd_ts;
25
26         struct gpio_desc        *v5_gpio;
27         int                     v5_irq;
28         bool                    v5_is_high;
29         ktime_t                 v5_ts;
30 };
31
32 static bool cec_gpio_read(struct cec_adapter *adap)
33 {
34         struct cec_gpio *cec = cec_get_drvdata(adap);
35
36         if (cec->cec_is_low)
37                 return false;
38         return gpiod_get_value(cec->cec_gpio);
39 }
40
41 static void cec_gpio_high(struct cec_adapter *adap)
42 {
43         struct cec_gpio *cec = cec_get_drvdata(adap);
44
45         if (!cec->cec_is_low)
46                 return;
47         cec->cec_is_low = false;
48         gpiod_set_value(cec->cec_gpio, 1);
49 }
50
51 static void cec_gpio_low(struct cec_adapter *adap)
52 {
53         struct cec_gpio *cec = cec_get_drvdata(adap);
54
55         if (cec->cec_is_low)
56                 return;
57         cec->cec_is_low = true;
58         gpiod_set_value(cec->cec_gpio, 0);
59 }
60
61 static irqreturn_t cec_hpd_gpio_irq_handler_thread(int irq, void *priv)
62 {
63         struct cec_gpio *cec = priv;
64
65         cec_queue_pin_hpd_event(cec->adap, cec->hpd_is_high, cec->hpd_ts);
66         return IRQ_HANDLED;
67 }
68
69 static irqreturn_t cec_5v_gpio_irq_handler(int irq, void *priv)
70 {
71         struct cec_gpio *cec = priv;
72         bool is_high = gpiod_get_value(cec->v5_gpio);
73
74         if (is_high == cec->v5_is_high)
75                 return IRQ_HANDLED;
76         cec->v5_ts = ktime_get();
77         cec->v5_is_high = is_high;
78         return IRQ_WAKE_THREAD;
79 }
80
81 static irqreturn_t cec_5v_gpio_irq_handler_thread(int irq, void *priv)
82 {
83         struct cec_gpio *cec = priv;
84
85         cec_queue_pin_5v_event(cec->adap, cec->v5_is_high, cec->v5_ts);
86         return IRQ_HANDLED;
87 }
88
89 static irqreturn_t cec_hpd_gpio_irq_handler(int irq, void *priv)
90 {
91         struct cec_gpio *cec = priv;
92         bool is_high = gpiod_get_value(cec->hpd_gpio);
93
94         if (is_high == cec->hpd_is_high)
95                 return IRQ_HANDLED;
96         cec->hpd_ts = ktime_get();
97         cec->hpd_is_high = is_high;
98         return IRQ_WAKE_THREAD;
99 }
100
101 static irqreturn_t cec_gpio_irq_handler(int irq, void *priv)
102 {
103         struct cec_gpio *cec = priv;
104
105         cec_pin_changed(cec->adap, gpiod_get_value(cec->cec_gpio));
106         return IRQ_HANDLED;
107 }
108
109 static bool cec_gpio_enable_irq(struct cec_adapter *adap)
110 {
111         struct cec_gpio *cec = cec_get_drvdata(adap);
112
113         enable_irq(cec->cec_irq);
114         return true;
115 }
116
117 static void cec_gpio_disable_irq(struct cec_adapter *adap)
118 {
119         struct cec_gpio *cec = cec_get_drvdata(adap);
120
121         disable_irq(cec->cec_irq);
122 }
123
124 static void cec_gpio_status(struct cec_adapter *adap, struct seq_file *file)
125 {
126         struct cec_gpio *cec = cec_get_drvdata(adap);
127
128         seq_printf(file, "mode: %s\n", cec->cec_is_low ? "low-drive" : "read");
129         seq_printf(file, "using irq: %d\n", cec->cec_irq);
130         if (cec->hpd_gpio)
131                 seq_printf(file, "hpd: %s\n",
132                            cec->hpd_is_high ? "high" : "low");
133         if (cec->v5_gpio)
134                 seq_printf(file, "5V: %s\n",
135                            cec->v5_is_high ? "high" : "low");
136 }
137
138 static int cec_gpio_read_hpd(struct cec_adapter *adap)
139 {
140         struct cec_gpio *cec = cec_get_drvdata(adap);
141
142         if (!cec->hpd_gpio)
143                 return -ENOTTY;
144         return gpiod_get_value(cec->hpd_gpio);
145 }
146
147 static int cec_gpio_read_5v(struct cec_adapter *adap)
148 {
149         struct cec_gpio *cec = cec_get_drvdata(adap);
150
151         if (!cec->v5_gpio)
152                 return -ENOTTY;
153         return gpiod_get_value(cec->v5_gpio);
154 }
155
156 static void cec_gpio_free(struct cec_adapter *adap)
157 {
158         cec_gpio_disable_irq(adap);
159 }
160
161 static const struct cec_pin_ops cec_gpio_pin_ops = {
162         .read = cec_gpio_read,
163         .low = cec_gpio_low,
164         .high = cec_gpio_high,
165         .enable_irq = cec_gpio_enable_irq,
166         .disable_irq = cec_gpio_disable_irq,
167         .status = cec_gpio_status,
168         .free = cec_gpio_free,
169         .read_hpd = cec_gpio_read_hpd,
170         .read_5v = cec_gpio_read_5v,
171 };
172
173 static int cec_gpio_probe(struct platform_device *pdev)
174 {
175         struct device *dev = &pdev->dev;
176         struct cec_gpio *cec;
177         int ret;
178
179         cec = devm_kzalloc(dev, sizeof(*cec), GFP_KERNEL);
180         if (!cec)
181                 return -ENOMEM;
182
183         cec->dev = dev;
184
185         cec->cec_gpio = devm_gpiod_get(dev, "cec", GPIOD_OUT_HIGH_OPEN_DRAIN);
186         if (IS_ERR(cec->cec_gpio))
187                 return PTR_ERR(cec->cec_gpio);
188         cec->cec_irq = gpiod_to_irq(cec->cec_gpio);
189
190         cec->hpd_gpio = devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
191         if (IS_ERR(cec->hpd_gpio))
192                 return PTR_ERR(cec->hpd_gpio);
193
194         cec->v5_gpio = devm_gpiod_get_optional(dev, "v5", GPIOD_IN);
195         if (IS_ERR(cec->v5_gpio))
196                 return PTR_ERR(cec->v5_gpio);
197
198         cec->adap = cec_pin_allocate_adapter(&cec_gpio_pin_ops,
199                 cec, pdev->name, CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR |
200                                  CEC_CAP_MONITOR_ALL | CEC_CAP_MONITOR_PIN);
201         if (IS_ERR(cec->adap))
202                 return PTR_ERR(cec->adap);
203
204         ret = devm_request_irq(dev, cec->cec_irq, cec_gpio_irq_handler,
205                                IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
206                                cec->adap->name, cec);
207         if (ret)
208                 return ret;
209
210         cec_gpio_disable_irq(cec->adap);
211
212         if (cec->hpd_gpio) {
213                 cec->hpd_irq = gpiod_to_irq(cec->hpd_gpio);
214                 ret = devm_request_threaded_irq(dev, cec->hpd_irq,
215                         cec_hpd_gpio_irq_handler,
216                         cec_hpd_gpio_irq_handler_thread,
217                         IRQF_ONESHOT |
218                         IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
219                         "hpd-gpio", cec);
220                 if (ret)
221                         return ret;
222         }
223
224         if (cec->v5_gpio) {
225                 cec->v5_irq = gpiod_to_irq(cec->v5_gpio);
226                 ret = devm_request_threaded_irq(dev, cec->v5_irq,
227                         cec_5v_gpio_irq_handler,
228                         cec_5v_gpio_irq_handler_thread,
229                         IRQF_ONESHOT |
230                         IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
231                         "v5-gpio", cec);
232                 if (ret)
233                         return ret;
234         }
235
236         ret = cec_register_adapter(cec->adap, &pdev->dev);
237         if (ret) {
238                 cec_delete_adapter(cec->adap);
239                 return ret;
240         }
241
242         platform_set_drvdata(pdev, cec);
243         return 0;
244 }
245
246 static int cec_gpio_remove(struct platform_device *pdev)
247 {
248         struct cec_gpio *cec = platform_get_drvdata(pdev);
249
250         cec_unregister_adapter(cec->adap);
251         return 0;
252 }
253
254 static const struct of_device_id cec_gpio_match[] = {
255         {
256                 .compatible     = "cec-gpio",
257         },
258         {},
259 };
260 MODULE_DEVICE_TABLE(of, cec_gpio_match);
261
262 static struct platform_driver cec_gpio_pdrv = {
263         .probe  = cec_gpio_probe,
264         .remove = cec_gpio_remove,
265         .driver = {
266                 .name           = "cec-gpio",
267                 .of_match_table = cec_gpio_match,
268         },
269 };
270
271 module_platform_driver(cec_gpio_pdrv);
272
273 MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
274 MODULE_LICENSE("GPL v2");
275 MODULE_DESCRIPTION("CEC GPIO driver");