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