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