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/platform_data/cros_ec_commands.h>
18 #include <linux/platform_data/cros_ec_proto.h>
19 #include <media/cec.h>
20 #include <media/cec-notifier.h>
21
22 #define DRV_NAME "cros-ec-cec"
23
24 /**
25 * struct cros_ec_cec_port - Driver data for a single EC CEC port
26 *
27 * @port_num: port number
28 * @adap: CEC adapter
29 * @notify: CEC notifier pointer
30 * @rx_msg: storage for a received message
31 * @cros_ec_cec: pointer to the parent struct
32 */
33 struct cros_ec_cec_port {
34 int port_num;
35 struct cec_adapter *adap;
36 struct cec_notifier *notify;
37 struct cec_msg rx_msg;
38 struct cros_ec_cec *cros_ec_cec;
39 };
40
41 /**
42 * struct cros_ec_cec - Driver data for EC CEC
43 *
44 * @cros_ec: Pointer to EC device
45 * @notifier: Notifier info for responding to EC events
46 * @write_cmd_version: Highest supported version of EC_CMD_CEC_WRITE_MSG.
47 * @num_ports: Number of CEC ports
48 * @ports: Array of ports
49 */
50 struct cros_ec_cec {
51 struct cros_ec_device *cros_ec;
52 struct notifier_block notifier;
53 int write_cmd_version;
54 int num_ports;
55 struct cros_ec_cec_port *ports[EC_CEC_MAX_PORTS];
56 };
57
cros_ec_cec_received_message(struct cros_ec_cec_port * port,uint8_t * msg,uint8_t len)58 static void cros_ec_cec_received_message(struct cros_ec_cec_port *port,
59 uint8_t *msg, uint8_t len)
60 {
61 if (len > CEC_MAX_MSG_SIZE)
62 len = CEC_MAX_MSG_SIZE;
63
64 port->rx_msg.len = len;
65 memcpy(port->rx_msg.msg, msg, len);
66
67 cec_received_msg(port->adap, &port->rx_msg);
68 }
69
handle_cec_message(struct cros_ec_cec * cros_ec_cec)70 static void handle_cec_message(struct cros_ec_cec *cros_ec_cec)
71 {
72 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
73 uint8_t *cec_message = cros_ec->event_data.data.cec_message;
74 unsigned int len = cros_ec->event_size;
75 struct cros_ec_cec_port *port;
76 /*
77 * There are two ways of receiving CEC messages:
78 * 1. Old EC firmware which only supports one port sends the data in a
79 * cec_message MKBP event.
80 * 2. New EC firmware which supports multiple ports uses
81 * EC_MKBP_CEC_HAVE_DATA to notify that data is ready and
82 * EC_CMD_CEC_READ_MSG to read it.
83 * Check that the EC only has one CEC port, and then we can assume the
84 * message is from port 0.
85 */
86 if (cros_ec_cec->num_ports != 1) {
87 dev_err(cros_ec->dev,
88 "received cec_message on device with %d ports\n",
89 cros_ec_cec->num_ports);
90 return;
91 }
92 port = cros_ec_cec->ports[0];
93
94 cros_ec_cec_received_message(port, cec_message, len);
95 }
96
cros_ec_cec_read_message(struct cros_ec_cec_port * port)97 static void cros_ec_cec_read_message(struct cros_ec_cec_port *port)
98 {
99 struct cros_ec_device *cros_ec = port->cros_ec_cec->cros_ec;
100 struct ec_params_cec_read params = {
101 .port = port->port_num,
102 };
103 struct ec_response_cec_read response;
104 int ret;
105
106 ret = cros_ec_cmd(cros_ec, 0, EC_CMD_CEC_READ_MSG, ¶ms,
107 sizeof(params), &response, sizeof(response));
108 if (ret < 0) {
109 dev_err(cros_ec->dev,
110 "error reading CEC message on EC: %d\n", ret);
111 return;
112 }
113
114 cros_ec_cec_received_message(port, response.msg, response.msg_len);
115 }
116
handle_cec_event(struct cros_ec_cec * cros_ec_cec)117 static void handle_cec_event(struct cros_ec_cec *cros_ec_cec)
118 {
119 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
120 uint32_t cec_events = cros_ec->event_data.data.cec_events;
121 uint32_t port_num = EC_MKBP_EVENT_CEC_GET_PORT(cec_events);
122 uint32_t events = EC_MKBP_EVENT_CEC_GET_EVENTS(cec_events);
123 struct cros_ec_cec_port *port;
124
125 if (port_num >= cros_ec_cec->num_ports) {
126 dev_err(cros_ec->dev,
127 "received CEC event for invalid port %d\n", port_num);
128 return;
129 }
130 port = cros_ec_cec->ports[port_num];
131
132 if (events & EC_MKBP_CEC_SEND_OK)
133 cec_transmit_attempt_done(port->adap, CEC_TX_STATUS_OK);
134
135 /* FW takes care of all retries, tell core to avoid more retries */
136 if (events & EC_MKBP_CEC_SEND_FAILED)
137 cec_transmit_attempt_done(port->adap,
138 CEC_TX_STATUS_MAX_RETRIES |
139 CEC_TX_STATUS_NACK);
140
141 if (events & EC_MKBP_CEC_HAVE_DATA)
142 cros_ec_cec_read_message(port);
143 }
144
cros_ec_cec_event(struct notifier_block * nb,unsigned long queued_during_suspend,void * _notify)145 static int cros_ec_cec_event(struct notifier_block *nb,
146 unsigned long queued_during_suspend,
147 void *_notify)
148 {
149 struct cros_ec_cec *cros_ec_cec;
150 struct cros_ec_device *cros_ec;
151
152 cros_ec_cec = container_of(nb, struct cros_ec_cec, notifier);
153 cros_ec = cros_ec_cec->cros_ec;
154
155 if (cros_ec->event_data.event_type == EC_MKBP_EVENT_CEC_EVENT) {
156 handle_cec_event(cros_ec_cec);
157 return NOTIFY_OK;
158 }
159
160 if (cros_ec->event_data.event_type == EC_MKBP_EVENT_CEC_MESSAGE) {
161 handle_cec_message(cros_ec_cec);
162 return NOTIFY_OK;
163 }
164
165 return NOTIFY_DONE;
166 }
167
cros_ec_cec_set_log_addr(struct cec_adapter * adap,u8 logical_addr)168 static int cros_ec_cec_set_log_addr(struct cec_adapter *adap, u8 logical_addr)
169 {
170 struct cros_ec_cec_port *port = adap->priv;
171 struct cros_ec_cec *cros_ec_cec = port->cros_ec_cec;
172 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
173 struct ec_params_cec_set params = {
174 .cmd = CEC_CMD_LOGICAL_ADDRESS,
175 .port = port->port_num,
176 .val = logical_addr,
177 };
178 int ret;
179
180 ret = cros_ec_cmd(cros_ec, 0, EC_CMD_CEC_SET, ¶ms, sizeof(params),
181 NULL, 0);
182 if (ret < 0) {
183 dev_err(cros_ec->dev,
184 "error setting CEC logical address on EC: %d\n", ret);
185 return ret;
186 }
187
188 return 0;
189 }
190
cros_ec_cec_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * cec_msg)191 static int cros_ec_cec_transmit(struct cec_adapter *adap, u8 attempts,
192 u32 signal_free_time, struct cec_msg *cec_msg)
193 {
194 struct cros_ec_cec_port *port = adap->priv;
195 struct cros_ec_cec *cros_ec_cec = port->cros_ec_cec;
196 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
197 struct ec_params_cec_write params;
198 struct ec_params_cec_write_v1 params_v1;
199 int ret;
200
201 if (cros_ec_cec->write_cmd_version == 0) {
202 memcpy(params.msg, cec_msg->msg, cec_msg->len);
203 ret = cros_ec_cmd(cros_ec, 0, EC_CMD_CEC_WRITE_MSG, ¶ms,
204 cec_msg->len, NULL, 0);
205 } else {
206 params_v1.port = port->port_num;
207 params_v1.msg_len = cec_msg->len;
208 memcpy(params_v1.msg, cec_msg->msg, cec_msg->len);
209 ret = cros_ec_cmd(cros_ec, cros_ec_cec->write_cmd_version,
210 EC_CMD_CEC_WRITE_MSG, ¶ms_v1,
211 sizeof(params_v1), NULL, 0);
212 }
213
214 if (ret < 0) {
215 dev_err(cros_ec->dev,
216 "error writing CEC msg on EC: %d\n", ret);
217 return ret;
218 }
219
220 return 0;
221 }
222
cros_ec_cec_adap_enable(struct cec_adapter * adap,bool enable)223 static int cros_ec_cec_adap_enable(struct cec_adapter *adap, bool enable)
224 {
225 struct cros_ec_cec_port *port = adap->priv;
226 struct cros_ec_cec *cros_ec_cec = port->cros_ec_cec;
227 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
228 struct ec_params_cec_set params = {
229 .cmd = CEC_CMD_ENABLE,
230 .port = port->port_num,
231 .val = enable,
232 };
233 int ret;
234
235 ret = cros_ec_cmd(cros_ec, 0, EC_CMD_CEC_SET, ¶ms, sizeof(params),
236 NULL, 0);
237 if (ret < 0) {
238 dev_err(cros_ec->dev,
239 "error %sabling CEC on EC: %d\n",
240 (enable ? "en" : "dis"), ret);
241 return ret;
242 }
243
244 return 0;
245 }
246
247 static const struct cec_adap_ops cros_ec_cec_ops = {
248 .adap_enable = cros_ec_cec_adap_enable,
249 .adap_log_addr = cros_ec_cec_set_log_addr,
250 .adap_transmit = cros_ec_cec_transmit,
251 };
252
253 #ifdef CONFIG_PM_SLEEP
cros_ec_cec_suspend(struct device * dev)254 static int cros_ec_cec_suspend(struct device *dev)
255 {
256 struct platform_device *pdev = to_platform_device(dev);
257 struct cros_ec_cec *cros_ec_cec = dev_get_drvdata(&pdev->dev);
258
259 if (device_may_wakeup(dev))
260 enable_irq_wake(cros_ec_cec->cros_ec->irq);
261
262 return 0;
263 }
264
cros_ec_cec_resume(struct device * dev)265 static int cros_ec_cec_resume(struct device *dev)
266 {
267 struct platform_device *pdev = to_platform_device(dev);
268 struct cros_ec_cec *cros_ec_cec = dev_get_drvdata(&pdev->dev);
269
270 if (device_may_wakeup(dev))
271 disable_irq_wake(cros_ec_cec->cros_ec->irq);
272
273 return 0;
274 }
275 #endif
276
277 static SIMPLE_DEV_PM_OPS(cros_ec_cec_pm_ops,
278 cros_ec_cec_suspend, cros_ec_cec_resume);
279
280 #if IS_ENABLED(CONFIG_PCI) && IS_ENABLED(CONFIG_DMI)
281
282 /*
283 * Specify the DRM device name handling the HDMI output and the HDMI connector
284 * corresponding to each CEC port. The order of connectors must match the order
285 * in the EC (first connector is EC port 0, ...), and the number of connectors
286 * must match the number of ports in the EC (which can be queried using the
287 * EC_CMD_CEC_PORT_COUNT host command).
288 */
289
290 struct cec_dmi_match {
291 const char *sys_vendor;
292 const char *product_name;
293 const char *devname;
294 const char *const *conns;
295 };
296
297 static const char *const port_b_conns[] = { "Port B", NULL };
298 static const char *const port_db_conns[] = { "Port D", "Port B", NULL };
299 static const char *const port_ba_conns[] = { "Port B", "Port A", NULL };
300 static const char *const port_ab_conns[] = { "Port A", "Port B", NULL };
301 static const char *const port_d_conns[] = { "Port D", NULL };
302
303 static const struct cec_dmi_match cec_dmi_match_table[] = {
304 /* Google Fizz */
305 { "Google", "Fizz", "0000:00:02.0", port_b_conns },
306 /* Google Brask */
307 { "Google", "Brask", "0000:00:02.0", port_b_conns },
308 /* Google Moli */
309 { "Google", "Moli", "0000:00:02.0", port_b_conns },
310 /* Google Kinox */
311 { "Google", "Kinox", "0000:00:02.0", port_b_conns },
312 /* Google Kuldax */
313 { "Google", "Kuldax", "0000:00:02.0", port_b_conns },
314 /* Google Aurash */
315 { "Google", "Aurash", "0000:00:02.0", port_b_conns },
316 /* Google Gladios */
317 { "Google", "Gladios", "0000:00:02.0", port_b_conns },
318 /* Google Lisbon */
319 { "Google", "Lisbon", "0000:00:02.0", port_b_conns },
320 /* Google Dibbi */
321 { "Google", "Dibbi", "0000:00:02.0", port_db_conns },
322 /* Google Constitution */
323 { "Google", "Constitution", "0000:00:02.0", port_ba_conns },
324 /* Google Boxy */
325 { "Google", "Boxy", "0000:00:02.0", port_d_conns },
326 /* Google Taranza */
327 { "Google", "Taranza", "0000:00:02.0", port_db_conns },
328 /* Google Dexi */
329 { "Google", "Dexi", "0000:00:02.0", port_db_conns },
330 /* Google Dita */
331 { "Google", "Dita", "0000:00:02.0", port_db_conns },
332 /* Google Dirks */
333 { "Google", "Dirks", "0000:00:02.0", port_ab_conns },
334 /* Google Moxie */
335 { "Google", "Moxie", "0000:00:02.0", port_b_conns },
336 /* Google Kulnex */
337 { "Google", "Kulnex", "0000:00:02.0", port_b_conns },
338 /* Google Moxoe */
339 { "Google", "Moxoe", "0000:00:02.0", port_b_conns },
340 /* Google Dirkson */
341 { "Google", "Dirkson", "0000:00:02.0", port_ab_conns },
342 };
343
cros_ec_cec_find_hdmi_dev(struct device * dev,const char * const ** conns)344 static struct device *cros_ec_cec_find_hdmi_dev(struct device *dev,
345 const char * const **conns)
346 {
347 int i;
348
349 for (i = 0 ; i < ARRAY_SIZE(cec_dmi_match_table) ; ++i) {
350 const struct cec_dmi_match *m = &cec_dmi_match_table[i];
351
352 if (dmi_match(DMI_SYS_VENDOR, m->sys_vendor) &&
353 dmi_match(DMI_PRODUCT_NAME, m->product_name)) {
354 struct device *d;
355
356 /* Find the device, bail out if not yet registered */
357 d = bus_find_device_by_name(&pci_bus_type, NULL,
358 m->devname);
359 if (!d)
360 return ERR_PTR(-EPROBE_DEFER);
361 put_device(d);
362 *conns = m->conns;
363 return d;
364 }
365 }
366
367 /* Hardware support must be added in the cec_dmi_match_table */
368 dev_warn(dev, "CEC notifier not configured for this hardware\n");
369
370 return ERR_PTR(-ENODEV);
371 }
372
373 #else
374
cros_ec_cec_find_hdmi_dev(struct device * dev,const char * const ** conns)375 static struct device *cros_ec_cec_find_hdmi_dev(struct device *dev,
376 const char * const **conns)
377 {
378 return ERR_PTR(-ENODEV);
379 }
380
381 #endif
382
cros_ec_cec_get_num_ports(struct cros_ec_cec * cros_ec_cec)383 static int cros_ec_cec_get_num_ports(struct cros_ec_cec *cros_ec_cec)
384 {
385 struct ec_response_cec_port_count response;
386 int ret;
387
388 ret = cros_ec_cmd(cros_ec_cec->cros_ec, 0, EC_CMD_CEC_PORT_COUNT, NULL,
389 0, &response, sizeof(response));
390 if (ret < 0) {
391 /*
392 * Old EC firmware only supports one port and does not support
393 * the port count command, so fall back to assuming one port.
394 */
395 cros_ec_cec->num_ports = 1;
396 return 0;
397 }
398
399 if (response.port_count == 0) {
400 dev_err(cros_ec_cec->cros_ec->dev,
401 "EC reports 0 CEC ports\n");
402 return -ENODEV;
403 }
404
405 if (response.port_count > EC_CEC_MAX_PORTS) {
406 dev_err(cros_ec_cec->cros_ec->dev,
407 "EC reports too many ports: %d\n", response.port_count);
408 return -EINVAL;
409 }
410
411 cros_ec_cec->num_ports = response.port_count;
412 return 0;
413 }
414
cros_ec_cec_get_write_cmd_version(struct cros_ec_cec * cros_ec_cec)415 static int cros_ec_cec_get_write_cmd_version(struct cros_ec_cec *cros_ec_cec)
416 {
417 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
418 struct ec_params_get_cmd_versions_v1 params = {
419 .cmd = EC_CMD_CEC_WRITE_MSG,
420 };
421 struct ec_response_get_cmd_versions response;
422 int ret;
423
424 ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GET_CMD_VERSIONS, ¶ms,
425 sizeof(params), &response, sizeof(response));
426 if (ret < 0) {
427 dev_err(cros_ec->dev,
428 "error getting CEC write command version: %d\n", ret);
429 return ret;
430 }
431
432 if (response.version_mask & EC_VER_MASK(1)) {
433 cros_ec_cec->write_cmd_version = 1;
434 } else {
435 if (cros_ec_cec->num_ports != 1) {
436 dev_err(cros_ec->dev,
437 "v0 write command only supports 1 port, %d reported\n",
438 cros_ec_cec->num_ports);
439 return -EINVAL;
440 }
441 cros_ec_cec->write_cmd_version = 0;
442 }
443
444 return 0;
445 }
446
cros_ec_cec_init_port(struct device * dev,struct cros_ec_cec * cros_ec_cec,int port_num,struct device * hdmi_dev,const char * const * conns)447 static int cros_ec_cec_init_port(struct device *dev,
448 struct cros_ec_cec *cros_ec_cec,
449 int port_num, struct device *hdmi_dev,
450 const char * const *conns)
451 {
452 struct cros_ec_cec_port *port;
453 int ret;
454
455 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
456 if (!port)
457 return -ENOMEM;
458
459 port->cros_ec_cec = cros_ec_cec;
460 port->port_num = port_num;
461
462 port->adap = cec_allocate_adapter(&cros_ec_cec_ops, port, DRV_NAME,
463 CEC_CAP_DEFAULTS |
464 CEC_CAP_CONNECTOR_INFO, 1);
465 if (IS_ERR(port->adap))
466 return PTR_ERR(port->adap);
467
468 if (!conns[port_num]) {
469 dev_err(dev, "no conn for port %d\n", port_num);
470 ret = -ENODEV;
471 goto out_probe_adapter;
472 }
473
474 port->notify = cec_notifier_cec_adap_register(hdmi_dev, conns[port_num],
475 port->adap);
476 if (!port->notify) {
477 ret = -ENOMEM;
478 goto out_probe_adapter;
479 }
480
481 ret = cec_register_adapter(port->adap, dev);
482 if (ret < 0)
483 goto out_probe_notify;
484
485 cros_ec_cec->ports[port_num] = port;
486
487 return 0;
488
489 out_probe_notify:
490 cec_notifier_cec_adap_unregister(port->notify, port->adap);
491 out_probe_adapter:
492 cec_delete_adapter(port->adap);
493 return ret;
494 }
495
cros_ec_cec_probe(struct platform_device * pdev)496 static int cros_ec_cec_probe(struct platform_device *pdev)
497 {
498 struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
499 struct cros_ec_device *cros_ec = ec_dev->ec_dev;
500 struct cros_ec_cec *cros_ec_cec;
501 struct cros_ec_cec_port *port;
502 struct device *hdmi_dev;
503 const char * const *conns = NULL;
504 int ret;
505
506 hdmi_dev = cros_ec_cec_find_hdmi_dev(&pdev->dev, &conns);
507 if (IS_ERR(hdmi_dev))
508 return PTR_ERR(hdmi_dev);
509
510 cros_ec_cec = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_cec),
511 GFP_KERNEL);
512 if (!cros_ec_cec)
513 return -ENOMEM;
514
515 platform_set_drvdata(pdev, cros_ec_cec);
516 cros_ec_cec->cros_ec = cros_ec;
517
518 device_init_wakeup(&pdev->dev, 1);
519
520 ret = cros_ec_cec_get_num_ports(cros_ec_cec);
521 if (ret)
522 return ret;
523
524 ret = cros_ec_cec_get_write_cmd_version(cros_ec_cec);
525 if (ret)
526 return ret;
527
528 for (int i = 0; i < cros_ec_cec->num_ports; i++) {
529 ret = cros_ec_cec_init_port(&pdev->dev, cros_ec_cec, i,
530 hdmi_dev, conns);
531 if (ret)
532 goto unregister_ports;
533 }
534
535 /* Get CEC events from the EC. */
536 cros_ec_cec->notifier.notifier_call = cros_ec_cec_event;
537 ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
538 &cros_ec_cec->notifier);
539 if (ret) {
540 dev_err(&pdev->dev, "failed to register notifier\n");
541 goto unregister_ports;
542 }
543
544 return 0;
545
546 unregister_ports:
547 /*
548 * Unregister any adapters which have been registered. We don't add the
549 * port to the array until the adapter has been registered successfully,
550 * so any non-NULL ports must have been registered.
551 */
552 for (int i = 0; i < cros_ec_cec->num_ports; i++) {
553 port = cros_ec_cec->ports[i];
554 if (!port)
555 break;
556 cec_notifier_cec_adap_unregister(port->notify, port->adap);
557 cec_unregister_adapter(port->adap);
558 }
559 return ret;
560 }
561
cros_ec_cec_remove(struct platform_device * pdev)562 static void cros_ec_cec_remove(struct platform_device *pdev)
563 {
564 struct cros_ec_cec *cros_ec_cec = platform_get_drvdata(pdev);
565 struct device *dev = &pdev->dev;
566 struct cros_ec_cec_port *port;
567 int ret;
568
569 /*
570 * blocking_notifier_chain_unregister() only fails if the notifier isn't
571 * in the list. We know it was added to it by .probe(), so there should
572 * be no need for error checking. Be cautious and still check.
573 */
574 ret = blocking_notifier_chain_unregister(
575 &cros_ec_cec->cros_ec->event_notifier,
576 &cros_ec_cec->notifier);
577 if (ret)
578 dev_err(dev, "failed to unregister notifier\n");
579
580 for (int i = 0; i < cros_ec_cec->num_ports; i++) {
581 port = cros_ec_cec->ports[i];
582 cec_notifier_cec_adap_unregister(port->notify, port->adap);
583 cec_unregister_adapter(port->adap);
584 }
585 }
586
587 static const struct platform_device_id cros_ec_cec_id[] = {
588 { DRV_NAME, 0 },
589 {}
590 };
591 MODULE_DEVICE_TABLE(platform, cros_ec_cec_id);
592
593 static struct platform_driver cros_ec_cec_driver = {
594 .probe = cros_ec_cec_probe,
595 .remove = cros_ec_cec_remove,
596 .driver = {
597 .name = DRV_NAME,
598 .pm = &cros_ec_cec_pm_ops,
599 },
600 .id_table = cros_ec_cec_id,
601 };
602
603 module_platform_driver(cros_ec_cec_driver);
604
605 MODULE_DESCRIPTION("CEC driver for ChromeOS ECs");
606 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
607 MODULE_LICENSE("GPL");
608