1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2a616e63cSRussell King /* 3a616e63cSRussell King * Designware HDMI CEC driver 4a616e63cSRussell King * 5a616e63cSRussell King * Copyright (C) 2015-2017 Russell King. 6a616e63cSRussell King */ 7a616e63cSRussell King #include <linux/interrupt.h> 8a616e63cSRussell King #include <linux/io.h> 9a616e63cSRussell King #include <linux/module.h> 10a616e63cSRussell King #include <linux/platform_device.h> 11a616e63cSRussell King #include <linux/sched.h> 12a616e63cSRussell King #include <linux/slab.h> 13a616e63cSRussell King 14a616e63cSRussell King #include <drm/drm_edid.h> 15a616e63cSRussell King 16a616e63cSRussell King #include <media/cec.h> 17a616e63cSRussell King #include <media/cec-notifier.h> 18a616e63cSRussell King 19a616e63cSRussell King #include "dw-hdmi-cec.h" 20a616e63cSRussell King 21a616e63cSRussell King enum { 22a616e63cSRussell King HDMI_IH_CEC_STAT0 = 0x0106, 23a616e63cSRussell King HDMI_IH_MUTE_CEC_STAT0 = 0x0186, 24a616e63cSRussell King 25a616e63cSRussell King HDMI_CEC_CTRL = 0x7d00, 26a616e63cSRussell King CEC_CTRL_START = BIT(0), 27a616e63cSRussell King CEC_CTRL_FRAME_TYP = 3 << 1, 28a616e63cSRussell King CEC_CTRL_RETRY = 0 << 1, 29a616e63cSRussell King CEC_CTRL_NORMAL = 1 << 1, 30a616e63cSRussell King CEC_CTRL_IMMED = 2 << 1, 31a616e63cSRussell King 32a616e63cSRussell King HDMI_CEC_STAT = 0x7d01, 33a616e63cSRussell King CEC_STAT_DONE = BIT(0), 34a616e63cSRussell King CEC_STAT_EOM = BIT(1), 35a616e63cSRussell King CEC_STAT_NACK = BIT(2), 36a616e63cSRussell King CEC_STAT_ARBLOST = BIT(3), 37a616e63cSRussell King CEC_STAT_ERROR_INIT = BIT(4), 38a616e63cSRussell King CEC_STAT_ERROR_FOLL = BIT(5), 39a616e63cSRussell King CEC_STAT_WAKEUP = BIT(6), 40a616e63cSRussell King 41a616e63cSRussell King HDMI_CEC_MASK = 0x7d02, 42a616e63cSRussell King HDMI_CEC_POLARITY = 0x7d03, 43a616e63cSRussell King HDMI_CEC_INT = 0x7d04, 44a616e63cSRussell King HDMI_CEC_ADDR_L = 0x7d05, 45a616e63cSRussell King HDMI_CEC_ADDR_H = 0x7d06, 46a616e63cSRussell King HDMI_CEC_TX_CNT = 0x7d07, 47a616e63cSRussell King HDMI_CEC_RX_CNT = 0x7d08, 48a616e63cSRussell King HDMI_CEC_TX_DATA0 = 0x7d10, 49a616e63cSRussell King HDMI_CEC_RX_DATA0 = 0x7d20, 50a616e63cSRussell King HDMI_CEC_LOCK = 0x7d30, 51a616e63cSRussell King HDMI_CEC_WKUPCTRL = 0x7d31, 52a616e63cSRussell King }; 53a616e63cSRussell King 54a616e63cSRussell King struct dw_hdmi_cec { 55a616e63cSRussell King struct dw_hdmi *hdmi; 56a616e63cSRussell King const struct dw_hdmi_cec_ops *ops; 57a616e63cSRussell King u32 addresses; 58a616e63cSRussell King struct cec_adapter *adap; 59a616e63cSRussell King struct cec_msg rx_msg; 60a616e63cSRussell King unsigned int tx_status; 61a616e63cSRussell King bool tx_done; 62a616e63cSRussell King bool rx_done; 63a616e63cSRussell King struct cec_notifier *notify; 64a616e63cSRussell King int irq; 65a616e63cSRussell King }; 66a616e63cSRussell King 67a616e63cSRussell King static void dw_hdmi_write(struct dw_hdmi_cec *cec, u8 val, int offset) 68a616e63cSRussell King { 69a616e63cSRussell King cec->ops->write(cec->hdmi, val, offset); 70a616e63cSRussell King } 71a616e63cSRussell King 72a616e63cSRussell King static u8 dw_hdmi_read(struct dw_hdmi_cec *cec, int offset) 73a616e63cSRussell King { 74a616e63cSRussell King return cec->ops->read(cec->hdmi, offset); 75a616e63cSRussell King } 76a616e63cSRussell King 77a616e63cSRussell King static int dw_hdmi_cec_log_addr(struct cec_adapter *adap, u8 logical_addr) 78a616e63cSRussell King { 79a616e63cSRussell King struct dw_hdmi_cec *cec = cec_get_drvdata(adap); 80a616e63cSRussell King 81a616e63cSRussell King if (logical_addr == CEC_LOG_ADDR_INVALID) 82a616e63cSRussell King cec->addresses = 0; 83a616e63cSRussell King else 84a616e63cSRussell King cec->addresses |= BIT(logical_addr) | BIT(15); 85a616e63cSRussell King 86a616e63cSRussell King dw_hdmi_write(cec, cec->addresses & 255, HDMI_CEC_ADDR_L); 87a616e63cSRussell King dw_hdmi_write(cec, cec->addresses >> 8, HDMI_CEC_ADDR_H); 88a616e63cSRussell King 89a616e63cSRussell King return 0; 90a616e63cSRussell King } 91a616e63cSRussell King 92a616e63cSRussell King static int dw_hdmi_cec_transmit(struct cec_adapter *adap, u8 attempts, 93a616e63cSRussell King u32 signal_free_time, struct cec_msg *msg) 94a616e63cSRussell King { 95a616e63cSRussell King struct dw_hdmi_cec *cec = cec_get_drvdata(adap); 96a616e63cSRussell King unsigned int i, ctrl; 97a616e63cSRussell King 98a616e63cSRussell King switch (signal_free_time) { 99a616e63cSRussell King case CEC_SIGNAL_FREE_TIME_RETRY: 100a616e63cSRussell King ctrl = CEC_CTRL_RETRY; 101a616e63cSRussell King break; 102a616e63cSRussell King case CEC_SIGNAL_FREE_TIME_NEW_INITIATOR: 103a616e63cSRussell King default: 104a616e63cSRussell King ctrl = CEC_CTRL_NORMAL; 105a616e63cSRussell King break; 106a616e63cSRussell King case CEC_SIGNAL_FREE_TIME_NEXT_XFER: 107a616e63cSRussell King ctrl = CEC_CTRL_IMMED; 108a616e63cSRussell King break; 109a616e63cSRussell King } 110a616e63cSRussell King 111a616e63cSRussell King for (i = 0; i < msg->len; i++) 112a616e63cSRussell King dw_hdmi_write(cec, msg->msg[i], HDMI_CEC_TX_DATA0 + i); 113a616e63cSRussell King 114a616e63cSRussell King dw_hdmi_write(cec, msg->len, HDMI_CEC_TX_CNT); 115a616e63cSRussell King dw_hdmi_write(cec, ctrl | CEC_CTRL_START, HDMI_CEC_CTRL); 116a616e63cSRussell King 117a616e63cSRussell King return 0; 118a616e63cSRussell King } 119a616e63cSRussell King 120a616e63cSRussell King static irqreturn_t dw_hdmi_cec_hardirq(int irq, void *data) 121a616e63cSRussell King { 122a616e63cSRussell King struct cec_adapter *adap = data; 123a616e63cSRussell King struct dw_hdmi_cec *cec = cec_get_drvdata(adap); 124a616e63cSRussell King unsigned int stat = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0); 125a616e63cSRussell King irqreturn_t ret = IRQ_HANDLED; 126a616e63cSRussell King 127a616e63cSRussell King if (stat == 0) 128a616e63cSRussell King return IRQ_NONE; 129a616e63cSRussell King 130a616e63cSRussell King dw_hdmi_write(cec, stat, HDMI_IH_CEC_STAT0); 131a616e63cSRussell King 132a616e63cSRussell King if (stat & CEC_STAT_ERROR_INIT) { 133a616e63cSRussell King cec->tx_status = CEC_TX_STATUS_ERROR; 134a616e63cSRussell King cec->tx_done = true; 135a616e63cSRussell King ret = IRQ_WAKE_THREAD; 136a616e63cSRussell King } else if (stat & CEC_STAT_DONE) { 137a616e63cSRussell King cec->tx_status = CEC_TX_STATUS_OK; 138a616e63cSRussell King cec->tx_done = true; 139a616e63cSRussell King ret = IRQ_WAKE_THREAD; 140a616e63cSRussell King } else if (stat & CEC_STAT_NACK) { 141a616e63cSRussell King cec->tx_status = CEC_TX_STATUS_NACK; 142a616e63cSRussell King cec->tx_done = true; 143a616e63cSRussell King ret = IRQ_WAKE_THREAD; 144a616e63cSRussell King } 145a616e63cSRussell King 146a616e63cSRussell King if (stat & CEC_STAT_EOM) { 147a616e63cSRussell King unsigned int len, i; 148a616e63cSRussell King 149a616e63cSRussell King len = dw_hdmi_read(cec, HDMI_CEC_RX_CNT); 150a616e63cSRussell King if (len > sizeof(cec->rx_msg.msg)) 151a616e63cSRussell King len = sizeof(cec->rx_msg.msg); 152a616e63cSRussell King 153a616e63cSRussell King for (i = 0; i < len; i++) 154a616e63cSRussell King cec->rx_msg.msg[i] = 155a616e63cSRussell King dw_hdmi_read(cec, HDMI_CEC_RX_DATA0 + i); 156a616e63cSRussell King 157a616e63cSRussell King dw_hdmi_write(cec, 0, HDMI_CEC_LOCK); 158a616e63cSRussell King 159a616e63cSRussell King cec->rx_msg.len = len; 160a616e63cSRussell King smp_wmb(); 161a616e63cSRussell King cec->rx_done = true; 162a616e63cSRussell King 163a616e63cSRussell King ret = IRQ_WAKE_THREAD; 164a616e63cSRussell King } 165a616e63cSRussell King 166a616e63cSRussell King return ret; 167a616e63cSRussell King } 168a616e63cSRussell King 169a616e63cSRussell King static irqreturn_t dw_hdmi_cec_thread(int irq, void *data) 170a616e63cSRussell King { 171a616e63cSRussell King struct cec_adapter *adap = data; 172a616e63cSRussell King struct dw_hdmi_cec *cec = cec_get_drvdata(adap); 173a616e63cSRussell King 174a616e63cSRussell King if (cec->tx_done) { 175a616e63cSRussell King cec->tx_done = false; 176a616e63cSRussell King cec_transmit_attempt_done(adap, cec->tx_status); 177a616e63cSRussell King } 178a616e63cSRussell King if (cec->rx_done) { 179a616e63cSRussell King cec->rx_done = false; 180a616e63cSRussell King smp_rmb(); 181a616e63cSRussell King cec_received_msg(adap, &cec->rx_msg); 182a616e63cSRussell King } 183a616e63cSRussell King return IRQ_HANDLED; 184a616e63cSRussell King } 185a616e63cSRussell King 186a616e63cSRussell King static int dw_hdmi_cec_enable(struct cec_adapter *adap, bool enable) 187a616e63cSRussell King { 188a616e63cSRussell King struct dw_hdmi_cec *cec = cec_get_drvdata(adap); 189a616e63cSRussell King 190a616e63cSRussell King if (!enable) { 191a616e63cSRussell King dw_hdmi_write(cec, ~0, HDMI_CEC_MASK); 192a616e63cSRussell King dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0); 193a616e63cSRussell King dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY); 194a616e63cSRussell King 195a616e63cSRussell King cec->ops->disable(cec->hdmi); 196a616e63cSRussell King } else { 197a616e63cSRussell King unsigned int irqs; 198a616e63cSRussell King 199a616e63cSRussell King dw_hdmi_write(cec, 0, HDMI_CEC_CTRL); 200a616e63cSRussell King dw_hdmi_write(cec, ~0, HDMI_IH_CEC_STAT0); 201a616e63cSRussell King dw_hdmi_write(cec, 0, HDMI_CEC_LOCK); 202a616e63cSRussell King 203a616e63cSRussell King dw_hdmi_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID); 204a616e63cSRussell King 205a616e63cSRussell King cec->ops->enable(cec->hdmi); 206a616e63cSRussell King 207a616e63cSRussell King irqs = CEC_STAT_ERROR_INIT | CEC_STAT_NACK | CEC_STAT_EOM | 208a616e63cSRussell King CEC_STAT_DONE; 209a616e63cSRussell King dw_hdmi_write(cec, irqs, HDMI_CEC_POLARITY); 210a616e63cSRussell King dw_hdmi_write(cec, ~irqs, HDMI_CEC_MASK); 211a616e63cSRussell King dw_hdmi_write(cec, ~irqs, HDMI_IH_MUTE_CEC_STAT0); 212a616e63cSRussell King } 213a616e63cSRussell King return 0; 214a616e63cSRussell King } 215a616e63cSRussell King 216a616e63cSRussell King static const struct cec_adap_ops dw_hdmi_cec_ops = { 217a616e63cSRussell King .adap_enable = dw_hdmi_cec_enable, 218a616e63cSRussell King .adap_log_addr = dw_hdmi_cec_log_addr, 219a616e63cSRussell King .adap_transmit = dw_hdmi_cec_transmit, 220a616e63cSRussell King }; 221a616e63cSRussell King 222a616e63cSRussell King static void dw_hdmi_cec_del(void *data) 223a616e63cSRussell King { 224a616e63cSRussell King struct dw_hdmi_cec *cec = data; 225a616e63cSRussell King 226a616e63cSRussell King cec_delete_adapter(cec->adap); 227a616e63cSRussell King } 228a616e63cSRussell King 229a616e63cSRussell King static int dw_hdmi_cec_probe(struct platform_device *pdev) 230a616e63cSRussell King { 231a616e63cSRussell King struct dw_hdmi_cec_data *data = dev_get_platdata(&pdev->dev); 232a616e63cSRussell King struct dw_hdmi_cec *cec; 233a616e63cSRussell King int ret; 234a616e63cSRussell King 235a616e63cSRussell King if (!data) 236a616e63cSRussell King return -ENXIO; 237a616e63cSRussell King 238a616e63cSRussell King /* 239a616e63cSRussell King * Our device is just a convenience - we want to link to the real 240a616e63cSRussell King * hardware device here, so that userspace can see the association 241a616e63cSRussell King * between the HDMI hardware and its associated CEC chardev. 242a616e63cSRussell King */ 243a616e63cSRussell King cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL); 244a616e63cSRussell King if (!cec) 245a616e63cSRussell King return -ENOMEM; 246a616e63cSRussell King 247a616e63cSRussell King cec->irq = data->irq; 248a616e63cSRussell King cec->ops = data->ops; 249a616e63cSRussell King cec->hdmi = data->hdmi; 250a616e63cSRussell King 251a616e63cSRussell King platform_set_drvdata(pdev, cec); 252a616e63cSRussell King 253a616e63cSRussell King dw_hdmi_write(cec, 0, HDMI_CEC_TX_CNT); 254a616e63cSRussell King dw_hdmi_write(cec, ~0, HDMI_CEC_MASK); 255a616e63cSRussell King dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0); 256a616e63cSRussell King dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY); 257a616e63cSRussell King 258a616e63cSRussell King cec->adap = cec_allocate_adapter(&dw_hdmi_cec_ops, cec, "dw_hdmi", 25901b45d3cSDariusz Marcinkiewicz CEC_CAP_DEFAULTS | 26001b45d3cSDariusz Marcinkiewicz CEC_CAP_CONNECTOR_INFO, 261a616e63cSRussell King CEC_MAX_LOG_ADDRS); 262a616e63cSRussell King if (IS_ERR(cec->adap)) 263a616e63cSRussell King return PTR_ERR(cec->adap); 264a616e63cSRussell King 265a616e63cSRussell King /* override the module pointer */ 266a616e63cSRussell King cec->adap->owner = THIS_MODULE; 267a616e63cSRussell King 2689962601cSCai Huoqing ret = devm_add_action_or_reset(&pdev->dev, dw_hdmi_cec_del, cec); 2699962601cSCai Huoqing if (ret) 270a616e63cSRussell King return ret; 271a616e63cSRussell King 272a616e63cSRussell King ret = devm_request_threaded_irq(&pdev->dev, cec->irq, 273a616e63cSRussell King dw_hdmi_cec_hardirq, 274a616e63cSRussell King dw_hdmi_cec_thread, IRQF_SHARED, 275a616e63cSRussell King "dw-hdmi-cec", cec->adap); 276a616e63cSRussell King if (ret < 0) 277a616e63cSRussell King return ret; 278a616e63cSRussell King 27901b45d3cSDariusz Marcinkiewicz cec->notify = cec_notifier_cec_adap_register(pdev->dev.parent, 28001b45d3cSDariusz Marcinkiewicz NULL, cec->adap); 281a616e63cSRussell King if (!cec->notify) 282a616e63cSRussell King return -ENOMEM; 283a616e63cSRussell King 284a616e63cSRussell King ret = cec_register_adapter(cec->adap, pdev->dev.parent); 285a616e63cSRussell King if (ret < 0) { 28610d8f308SHans Verkuil cec_notifier_cec_adap_unregister(cec->notify, cec->adap); 287a616e63cSRussell King return ret; 288a616e63cSRussell King } 289a616e63cSRussell King 290a616e63cSRussell King /* 291a616e63cSRussell King * CEC documentation says we must not call cec_delete_adapter 292a616e63cSRussell King * after a successful call to cec_register_adapter(). 293a616e63cSRussell King */ 294a616e63cSRussell King devm_remove_action(&pdev->dev, dw_hdmi_cec_del, cec); 295a616e63cSRussell King 296a616e63cSRussell King return 0; 297a616e63cSRussell King } 298a616e63cSRussell King 299*88916982SUwe Kleine-König static void dw_hdmi_cec_remove(struct platform_device *pdev) 300a616e63cSRussell King { 301a616e63cSRussell King struct dw_hdmi_cec *cec = platform_get_drvdata(pdev); 302a616e63cSRussell King 30310d8f308SHans Verkuil cec_notifier_cec_adap_unregister(cec->notify, cec->adap); 304a616e63cSRussell King cec_unregister_adapter(cec->adap); 305a616e63cSRussell King } 306a616e63cSRussell King 307a616e63cSRussell King static struct platform_driver dw_hdmi_cec_driver = { 308a616e63cSRussell King .probe = dw_hdmi_cec_probe, 309*88916982SUwe Kleine-König .remove_new = dw_hdmi_cec_remove, 310a616e63cSRussell King .driver = { 311a616e63cSRussell King .name = "dw-hdmi-cec", 312a616e63cSRussell King }, 313a616e63cSRussell King }; 314a616e63cSRussell King module_platform_driver(dw_hdmi_cec_driver); 315a616e63cSRussell King 316a616e63cSRussell King MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>"); 317a616e63cSRussell King MODULE_DESCRIPTION("Synopsys Designware HDMI CEC driver for i.MX"); 318a616e63cSRussell King MODULE_LICENSE("GPL"); 319a616e63cSRussell King MODULE_ALIAS(PLATFORM_MODULE_PREFIX "dw-hdmi-cec"); 320