1 // SPDX-License-Identifier: GPL-2.0+
3 * CEC driver for ChromeOS Embedded Controller
5 * Copyright (c) 2018 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
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 <media/cec.h>
18 #include <media/cec-notifier.h>
19 #include <linux/mfd/cros_ec.h>
20 #include <linux/mfd/cros_ec_commands.h>
22 #define DRV_NAME "cros-ec-cec"
25 * struct cros_ec_cec - Driver data for EC CEC
27 * @cros_ec: Pointer to EC device
28 * @notifier: Notifier info for responding to EC events
30 * @notify: CEC notifier pointer
31 * @rx_msg: storage for a received message
34 struct cros_ec_device *cros_ec;
35 struct notifier_block notifier;
36 struct cec_adapter *adap;
37 struct cec_notifier *notify;
38 struct cec_msg rx_msg;
41 static void handle_cec_message(struct cros_ec_cec *cros_ec_cec)
43 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
44 uint8_t *cec_message = cros_ec->event_data.data.cec_message;
45 unsigned int len = cros_ec->event_size;
47 if (len > CEC_MAX_MSG_SIZE)
48 len = CEC_MAX_MSG_SIZE;
49 cros_ec_cec->rx_msg.len = len;
50 memcpy(cros_ec_cec->rx_msg.msg, cec_message, len);
52 cec_received_msg(cros_ec_cec->adap, &cros_ec_cec->rx_msg);
55 static void handle_cec_event(struct cros_ec_cec *cros_ec_cec)
57 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
58 uint32_t events = cros_ec->event_data.data.cec_events;
60 if (events & EC_MKBP_CEC_SEND_OK)
61 cec_transmit_attempt_done(cros_ec_cec->adap,
64 /* FW takes care of all retries, tell core to avoid more retries */
65 if (events & EC_MKBP_CEC_SEND_FAILED)
66 cec_transmit_attempt_done(cros_ec_cec->adap,
67 CEC_TX_STATUS_MAX_RETRIES |
71 static int cros_ec_cec_event(struct notifier_block *nb,
72 unsigned long queued_during_suspend,
75 struct cros_ec_cec *cros_ec_cec;
76 struct cros_ec_device *cros_ec;
78 cros_ec_cec = container_of(nb, struct cros_ec_cec, notifier);
79 cros_ec = cros_ec_cec->cros_ec;
81 if (cros_ec->event_data.event_type == EC_MKBP_EVENT_CEC_EVENT) {
82 handle_cec_event(cros_ec_cec);
86 if (cros_ec->event_data.event_type == EC_MKBP_EVENT_CEC_MESSAGE) {
87 handle_cec_message(cros_ec_cec);
94 static int cros_ec_cec_set_log_addr(struct cec_adapter *adap, u8 logical_addr)
96 struct cros_ec_cec *cros_ec_cec = adap->priv;
97 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
99 struct cros_ec_command msg;
100 struct ec_params_cec_set data;
104 msg.msg.command = EC_CMD_CEC_SET;
105 msg.msg.outsize = sizeof(msg.data);
106 msg.data.cmd = CEC_CMD_LOGICAL_ADDRESS;
107 msg.data.val = logical_addr;
109 ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
111 dev_err(cros_ec->dev,
112 "error setting CEC logical address on EC: %d\n", ret);
119 static int cros_ec_cec_transmit(struct cec_adapter *adap, u8 attempts,
120 u32 signal_free_time, struct cec_msg *cec_msg)
122 struct cros_ec_cec *cros_ec_cec = adap->priv;
123 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
125 struct cros_ec_command msg;
126 struct ec_params_cec_write data;
130 msg.msg.command = EC_CMD_CEC_WRITE_MSG;
131 msg.msg.outsize = cec_msg->len;
132 memcpy(msg.data.msg, cec_msg->msg, cec_msg->len);
134 ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
136 dev_err(cros_ec->dev,
137 "error writing CEC msg on EC: %d\n", ret);
144 static int cros_ec_cec_adap_enable(struct cec_adapter *adap, bool enable)
146 struct cros_ec_cec *cros_ec_cec = adap->priv;
147 struct cros_ec_device *cros_ec = cros_ec_cec->cros_ec;
149 struct cros_ec_command msg;
150 struct ec_params_cec_set data;
154 msg.msg.command = EC_CMD_CEC_SET;
155 msg.msg.outsize = sizeof(msg.data);
156 msg.data.cmd = CEC_CMD_ENABLE;
157 msg.data.val = enable;
159 ret = cros_ec_cmd_xfer_status(cros_ec, &msg.msg);
161 dev_err(cros_ec->dev,
162 "error %sabling CEC on EC: %d\n",
163 (enable ? "en" : "dis"), ret);
170 static const struct cec_adap_ops cros_ec_cec_ops = {
171 .adap_enable = cros_ec_cec_adap_enable,
172 .adap_log_addr = cros_ec_cec_set_log_addr,
173 .adap_transmit = cros_ec_cec_transmit,
176 #ifdef CONFIG_PM_SLEEP
177 static int cros_ec_cec_suspend(struct device *dev)
179 struct platform_device *pdev = to_platform_device(dev);
180 struct cros_ec_cec *cros_ec_cec = dev_get_drvdata(&pdev->dev);
182 if (device_may_wakeup(dev))
183 enable_irq_wake(cros_ec_cec->cros_ec->irq);
188 static int cros_ec_cec_resume(struct device *dev)
190 struct platform_device *pdev = to_platform_device(dev);
191 struct cros_ec_cec *cros_ec_cec = dev_get_drvdata(&pdev->dev);
193 if (device_may_wakeup(dev))
194 disable_irq_wake(cros_ec_cec->cros_ec->irq);
200 static SIMPLE_DEV_PM_OPS(cros_ec_cec_pm_ops,
201 cros_ec_cec_suspend, cros_ec_cec_resume);
203 #if IS_ENABLED(CONFIG_PCI) && IS_ENABLED(CONFIG_DMI)
206 * The Firmware only handles a single CEC interface tied to a single HDMI
207 * connector we specify along with the DRM device name handling the HDMI output
210 struct cec_dmi_match {
217 static const struct cec_dmi_match cec_dmi_match_table[] = {
219 { "Google", "Fizz", "0000:00:02.0", "Port B" },
222 static int cros_ec_cec_get_notifier(struct device *dev,
223 struct cec_notifier **notify)
227 for (i = 0 ; i < ARRAY_SIZE(cec_dmi_match_table) ; ++i) {
228 const struct cec_dmi_match *m = &cec_dmi_match_table[i];
230 if (dmi_match(DMI_SYS_VENDOR, m->sys_vendor) &&
231 dmi_match(DMI_PRODUCT_NAME, m->product_name)) {
234 /* Find the device, bail out if not yet registered */
235 d = bus_find_device_by_name(&pci_bus_type, NULL,
238 return -EPROBE_DEFER;
240 *notify = cec_notifier_get_conn(d, m->conn);
245 /* Hardware support must be added in the cec_dmi_match_table */
246 dev_warn(dev, "CEC notifier not configured for this hardware\n");
253 static int cros_ec_cec_get_notifier(struct device *dev,
254 struct cec_notifier **notify)
261 static int cros_ec_cec_probe(struct platform_device *pdev)
263 struct cros_ec_dev *ec_dev = dev_get_drvdata(pdev->dev.parent);
264 struct cros_ec_device *cros_ec = ec_dev->ec_dev;
265 struct cros_ec_cec *cros_ec_cec;
268 cros_ec_cec = devm_kzalloc(&pdev->dev, sizeof(*cros_ec_cec),
273 platform_set_drvdata(pdev, cros_ec_cec);
274 cros_ec_cec->cros_ec = cros_ec;
276 ret = cros_ec_cec_get_notifier(&pdev->dev, &cros_ec_cec->notify);
280 ret = device_init_wakeup(&pdev->dev, 1);
282 dev_err(&pdev->dev, "failed to initialize wakeup\n");
286 cros_ec_cec->adap = cec_allocate_adapter(&cros_ec_cec_ops, cros_ec_cec,
287 DRV_NAME, CEC_CAP_DEFAULTS, 1);
288 if (IS_ERR(cros_ec_cec->adap))
289 return PTR_ERR(cros_ec_cec->adap);
291 /* Get CEC events from the EC. */
292 cros_ec_cec->notifier.notifier_call = cros_ec_cec_event;
293 ret = blocking_notifier_chain_register(&cros_ec->event_notifier,
294 &cros_ec_cec->notifier);
296 dev_err(&pdev->dev, "failed to register notifier\n");
297 cec_delete_adapter(cros_ec_cec->adap);
301 ret = cec_register_adapter(cros_ec_cec->adap, &pdev->dev);
303 cec_delete_adapter(cros_ec_cec->adap);
307 cec_register_cec_notifier(cros_ec_cec->adap, cros_ec_cec->notify);
312 static int cros_ec_cec_remove(struct platform_device *pdev)
314 struct cros_ec_cec *cros_ec_cec = platform_get_drvdata(pdev);
315 struct device *dev = &pdev->dev;
318 ret = blocking_notifier_chain_unregister(
319 &cros_ec_cec->cros_ec->event_notifier,
320 &cros_ec_cec->notifier);
323 dev_err(dev, "failed to unregister notifier\n");
327 cec_unregister_adapter(cros_ec_cec->adap);
329 if (cros_ec_cec->notify)
330 cec_notifier_put(cros_ec_cec->notify);
335 static struct platform_driver cros_ec_cec_driver = {
336 .probe = cros_ec_cec_probe,
337 .remove = cros_ec_cec_remove,
340 .pm = &cros_ec_cec_pm_ops,
344 module_platform_driver(cros_ec_cec_driver);
346 MODULE_DESCRIPTION("CEC driver for ChromeOS ECs");
347 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
348 MODULE_LICENSE("GPL");
349 MODULE_ALIAS("platform:" DRV_NAME);