Linux-libre 5.4.49-gnu
[librecmc/linux-libre.git] / drivers / media / platform / cros-ec-cec / cros-ec-cec.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * CEC driver for ChromeOS Embedded Controller
4  *
5  * Copyright (c) 2018 BayLibre, SAS
6  * Author: Neil Armstrong <narmstrong@baylibre.com>
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/dmi.h>
13 #include <linux/pci.h>
14 #include <linux/cec.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/cros_ec.h>
18 #include <linux/platform_data/cros_ec_commands.h>
19 #include <linux/platform_data/cros_ec_proto.h>
20 #include <media/cec.h>
21 #include <media/cec-notifier.h>
22
23 #define DRV_NAME        "cros-ec-cec"
24
25 /**
26  * struct cros_ec_cec - Driver data for EC CEC
27  *
28  * @cros_ec: Pointer to EC device
29  * @notifier: Notifier info for responding to EC events
30  * @adap: CEC adapter
31  * @notify: CEC notifier pointer
32  * @rx_msg: storage for a received message
33  */
34 struct cros_ec_cec {
35         struct cros_ec_device *cros_ec;
36         struct notifier_block notifier;
37         struct cec_adapter *adap;
38         struct cec_notifier *notify;
39         struct cec_msg rx_msg;
40 };
41
42 static void handle_cec_message(struct cros_ec_cec *cros_ec_cec)
43 {
44         struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
45         uint8_t *cec_message = cros_ec->event_data.data.cec_message;
46         unsigned int len = cros_ec->event_size;
47
48         cros_ec_cec->rx_msg.len = len;
49         memcpy(cros_ec_cec->rx_msg.msg, cec_message, len);
50
51         cec_received_msg(cros_ec_cec->adap, &cros_ec_cec->rx_msg);
52 }
53
54 static void handle_cec_event(struct cros_ec_cec *cros_ec_cec)
55 {
56         struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
57         uint32_t events = cros_ec->event_data.data.cec_events;
58
59         if (events & EC_MKBP_CEC_SEND_OK)
60                 cec_transmit_attempt_done(cros_ec_cec->adap,
61                                           CEC_TX_STATUS_OK);
62
63         /* FW takes care of all retries, tell core to avoid more retries */
64         if (events & EC_MKBP_CEC_SEND_FAILED)
65                 cec_transmit_attempt_done(cros_ec_cec->adap,
66                                           CEC_TX_STATUS_MAX_RETRIES |
67                                           CEC_TX_STATUS_NACK);
68 }
69
70 static int cros_ec_cec_event(struct notifier_block *nb,
71                              unsigned long queued_during_suspend,
72                              void *_notify)
73 {
74         struct cros_ec_cec *cros_ec_cec;
75         struct cros_ec_device *cros_ec;
76
77         cros_ec_cec = container_of(nb, struct cros_ec_cec, notifier);
78         cros_ec = cros_ec_cec->cros_ec;
79
80         if (cros_ec->event_data.event_type == EC_MKBP_EVENT_CEC_EVENT) {
81                 handle_cec_event(cros_ec_cec);
82                 return NOTIFY_OK;
83         }
84
85         if (cros_ec->event_data.event_type == EC_MKBP_EVENT_CEC_MESSAGE) {
86                 handle_cec_message(cros_ec_cec);
87                 return NOTIFY_OK;
88         }
89
90         return NOTIFY_DONE;
91 }
92
93 static int cros_ec_cec_set_log_addr(struct cec_adapter *adap, u8 logical_addr)
94 {
95         struct cros_ec_cec *cros_ec_cec = adap->priv;
96         struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
97         struct {
98                 struct cros_ec_command msg;
99                 struct ec_params_cec_set data;
100         } __packed msg = {};
101         int ret;
102
103         msg.msg.command = EC_CMD_CEC_SET;
104         msg.msg.outsize = sizeof(msg.data);
105         msg.data.cmd = CEC_CMD_LOGICAL_ADDRESS;
106         msg.data.val = logical_addr;
107
108         ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
109         if (ret < 0) {
110                 dev_err(cros_ec->dev,
111                         "error setting CEC logical address on EC: %d\n", ret);
112                 return ret;
113         }
114
115         return 0;
116 }
117
118 static int cros_ec_cec_transmit(struct cec_adapter *adap, u8 attempts,
119                                 u32 signal_free_time, struct cec_msg *cec_msg)
120 {
121         struct cros_ec_cec *cros_ec_cec = adap->priv;
122         struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
123         struct {
124                 struct cros_ec_command msg;
125                 struct ec_params_cec_write data;
126         } __packed msg = {};
127         int ret;
128
129         msg.msg.command = EC_CMD_CEC_WRITE_MSG;
130         msg.msg.outsize = cec_msg->len;
131         memcpy(msg.data.msg, cec_msg->msg, cec_msg->len);
132
133         ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
134         if (ret < 0) {
135                 dev_err(cros_ec->dev,
136                         "error writing CEC msg on EC: %d\n", ret);
137                 return ret;
138         }
139
140         return 0;
141 }
142
143 static int cros_ec_cec_adap_enable(struct cec_adapter *adap, bool enable)
144 {
145         struct cros_ec_cec *cros_ec_cec = adap->priv;
146         struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
147         struct {
148                 struct cros_ec_command msg;
149                 struct ec_params_cec_set data;
150         } __packed msg = {};
151         int ret;
152
153         msg.msg.command = EC_CMD_CEC_SET;
154         msg.msg.outsize = sizeof(msg.data);
155         msg.data.cmd = CEC_CMD_ENABLE;
156         msg.data.val = enable;
157
158         ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
159         if (ret < 0) {
160                 dev_err(cros_ec->dev,
161                         "error %sabling CEC on EC: %d\n",
162                         (enable ? "en" : "dis"), ret);
163                 return ret;
164         }
165
166         return 0;
167 }
168
169 static const struct cec_adap_ops cros_ec_cec_ops = {
170         .adap_enable = cros_ec_cec_adap_enable,
171         .adap_log_addr = cros_ec_cec_set_log_addr,
172         .adap_transmit = cros_ec_cec_transmit,
173 };
174
175 #ifdef CONFIG_PM_SLEEP
176 static int cros_ec_cec_suspend(struct device *dev)
177 {
178         struct platform_device *pdev = to_platform_device(dev);
179         struct cros_ec_cec *cros_ec_cec = dev_get_drvdata(&pdev->dev);
180
181         if (device_may_wakeup(dev))
182                 enable_irq_wake(cros_ec_cec->cros_ec->irq);
183
184         return 0;
185 }
186
187 static int cros_ec_cec_resume(struct device *dev)
188 {
189         struct platform_device *pdev = to_platform_device(dev);
190         struct cros_ec_cec *cros_ec_cec = dev_get_drvdata(&pdev->dev);
191
192         if (device_may_wakeup(dev))
193                 disable_irq_wake(cros_ec_cec->cros_ec->irq);
194
195         return 0;
196 }
197 #endif
198
199 static SIMPLE_DEV_PM_OPS(cros_ec_cec_pm_ops,
200         cros_ec_cec_suspend, cros_ec_cec_resume);
201
202 #if IS_ENABLED(CONFIG_PCI) && IS_ENABLED(CONFIG_DMI)
203
204 /*
205  * The Firmware only handles a single CEC interface tied to a single HDMI
206  * connector we specify along with the DRM device name handling the HDMI output
207  */
208
209 struct cec_dmi_match {
210         const char *sys_vendor;
211         const char *product_name;
212         const char *devname;
213         const char *conn;
214 };
215
216 static const struct cec_dmi_match cec_dmi_match_table[] = {
217         /* Google Fizz */
218         { "Google", "Fizz", "0000:00:02.0", "Port B" },
219 };
220
221 static struct device *cros_ec_cec_find_hdmi_dev(struct device *dev,
222                                                 const char **conn)
223 {
224         int i;
225
226         for (i = 0 ; i < ARRAY_SIZE(cec_dmi_match_table) ; ++i) {
227                 const struct cec_dmi_match *m = &cec_dmi_match_table[i];
228
229                 if (dmi_match(DMI_SYS_VENDOR, m->sys_vendor) &&
230                     dmi_match(DMI_PRODUCT_NAME, m->product_name)) {
231                         struct device *d;
232
233                         /* Find the device, bail out if not yet registered */
234                         d = bus_find_device_by_name(&pci_bus_type, NULL,
235                                                     m->devname);
236                         if (!d)
237                                 return ERR_PTR(-EPROBE_DEFER);
238                         put_device(d);
239                         *conn = m->conn;
240                         return d;
241                 }
242         }
243
244         /* Hardware support must be added in the cec_dmi_match_table */
245         dev_warn(dev, "CEC notifier not configured for this hardware\n");
246
247         return ERR_PTR(-ENODEV);
248 }
249
250 #else
251
252 static struct device *cros_ec_cec_find_hdmi_dev(struct device *dev,
253                                                 const char **conn)
254 {
255         return ERR_PTR(-ENODEV);
256 }
257
258 #endif
259
260 static int cros_ec_cec_probe(struct platform_device *pdev)
261 {
262         struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
263         struct cros_ec_device *cros_ec = ec_dev->ec_dev;
264         struct cros_ec_cec *cros_ec_cec;
265         struct device *hdmi_dev;
266         const char *conn = NULL;
267         int ret;
268
269         hdmi_dev = cros_ec_cec_find_hdmi_dev(&pdev->dev, &conn);
270         if (IS_ERR(hdmi_dev))
271                 return PTR_ERR(hdmi_dev);
272
273         cros_ec_cec = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_cec),
274                                    GFP_KERNEL);
275         if (!cros_ec_cec)
276                 return -ENOMEM;
277
278         platform_set_drvdata(pdev, cros_ec_cec);
279         cros_ec_cec->cros_ec = cros_ec;
280
281         ret = device_init_wakeup(&pdev->dev, 1);
282         if (ret) {
283                 dev_err(&pdev->dev, "failed to initialize wakeup\n");
284                 return ret;
285         }
286
287         cros_ec_cec->adap = cec_allocate_adapter(&cros_ec_cec_ops, cros_ec_cec,
288                                                  DRV_NAME,
289                                                  CEC_CAP_DEFAULTS |
290                                                  CEC_CAP_CONNECTOR_INFO, 1);
291         if (IS_ERR(cros_ec_cec->adap))
292                 return PTR_ERR(cros_ec_cec->adap);
293
294         cros_ec_cec->notify = cec_notifier_cec_adap_register(hdmi_dev, conn,
295                                                              cros_ec_cec->adap);
296         if (!cros_ec_cec->notify) {
297                 ret = -ENOMEM;
298                 goto out_probe_adapter;
299         }
300
301         /* Get CEC events from the EC. */
302         cros_ec_cec->notifier.notifier_call = cros_ec_cec_event;
303         ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
304                                                &cros_ec_cec->notifier);
305         if (ret) {
306                 dev_err(&pdev->dev, "failed to register notifier\n");
307                 goto out_probe_notify;
308         }
309
310         ret = cec_register_adapter(cros_ec_cec->adap, &pdev->dev);
311         if (ret < 0)
312                 goto out_probe_notify;
313
314         return 0;
315
316 out_probe_notify:
317         cec_notifier_cec_adap_unregister(cros_ec_cec->notify);
318 out_probe_adapter:
319         cec_delete_adapter(cros_ec_cec->adap);
320         return ret;
321 }
322
323 static int cros_ec_cec_remove(struct platform_device *pdev)
324 {
325         struct cros_ec_cec *cros_ec_cec = platform_get_drvdata(pdev);
326         struct device *dev = &pdev->dev;
327         int ret;
328
329         ret = blocking_notifier_chain_unregister(
330                         &cros_ec_cec->cros_ec->event_notifier,
331                         &cros_ec_cec->notifier);
332
333         if (ret) {
334                 dev_err(dev, "failed to unregister notifier\n");
335                 return ret;
336         }
337
338         cec_notifier_cec_adap_unregister(cros_ec_cec->notify);
339         cec_unregister_adapter(cros_ec_cec->adap);
340
341         return 0;
342 }
343
344 static struct platform_driver cros_ec_cec_driver = {
345         .probe = cros_ec_cec_probe,
346         .remove  = cros_ec_cec_remove,
347         .driver = {
348                 .name = DRV_NAME,
349                 .pm = &cros_ec_cec_pm_ops,
350         },
351 };
352
353 module_platform_driver(cros_ec_cec_driver);
354
355 MODULE_DESCRIPTION("CEC driver for ChromeOS ECs");
356 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
357 MODULE_LICENSE("GPL");
358 MODULE_ALIAS("platform:" DRV_NAME);