Path: blob/master/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c
26516 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Designware HDMI CEC driver3*4* Copyright (C) 2015-2017 Russell King.5*/6#include <linux/interrupt.h>7#include <linux/io.h>8#include <linux/module.h>9#include <linux/platform_device.h>10#include <linux/sched.h>11#include <linux/slab.h>1213#include <drm/drm_edid.h>1415#include <media/cec.h>16#include <media/cec-notifier.h>1718#include "dw-hdmi-cec.h"1920enum {21HDMI_IH_CEC_STAT0 = 0x0106,22HDMI_IH_MUTE_CEC_STAT0 = 0x0186,2324HDMI_CEC_CTRL = 0x7d00,25CEC_CTRL_START = BIT(0),26CEC_CTRL_FRAME_TYP = 3 << 1,27CEC_CTRL_RETRY = 0 << 1,28CEC_CTRL_NORMAL = 1 << 1,29CEC_CTRL_IMMED = 2 << 1,3031HDMI_CEC_STAT = 0x7d01,32CEC_STAT_DONE = BIT(0),33CEC_STAT_EOM = BIT(1),34CEC_STAT_NACK = BIT(2),35CEC_STAT_ARBLOST = BIT(3),36CEC_STAT_ERROR_INIT = BIT(4),37CEC_STAT_ERROR_FOLL = BIT(5),38CEC_STAT_WAKEUP = BIT(6),3940HDMI_CEC_MASK = 0x7d02,41HDMI_CEC_POLARITY = 0x7d03,42HDMI_CEC_INT = 0x7d04,43HDMI_CEC_ADDR_L = 0x7d05,44HDMI_CEC_ADDR_H = 0x7d06,45HDMI_CEC_TX_CNT = 0x7d07,46HDMI_CEC_RX_CNT = 0x7d08,47HDMI_CEC_TX_DATA0 = 0x7d10,48HDMI_CEC_RX_DATA0 = 0x7d20,49HDMI_CEC_LOCK = 0x7d30,50HDMI_CEC_WKUPCTRL = 0x7d31,51};5253struct dw_hdmi_cec {54struct dw_hdmi *hdmi;55const struct dw_hdmi_cec_ops *ops;56u32 addresses;57struct cec_adapter *adap;58struct cec_msg rx_msg;59unsigned int tx_status;60bool tx_done;61bool rx_done;62struct cec_notifier *notify;63int irq;6465u8 regs_polarity;66u8 regs_mask;67u8 regs_mute_stat0;68};6970static void dw_hdmi_write(struct dw_hdmi_cec *cec, u8 val, int offset)71{72cec->ops->write(cec->hdmi, val, offset);73}7475static u8 dw_hdmi_read(struct dw_hdmi_cec *cec, int offset)76{77return cec->ops->read(cec->hdmi, offset);78}7980static int dw_hdmi_cec_log_addr(struct cec_adapter *adap, u8 logical_addr)81{82struct dw_hdmi_cec *cec = cec_get_drvdata(adap);8384if (logical_addr == CEC_LOG_ADDR_INVALID)85cec->addresses = 0;86else87cec->addresses |= BIT(logical_addr) | BIT(15);8889dw_hdmi_write(cec, cec->addresses & 255, HDMI_CEC_ADDR_L);90dw_hdmi_write(cec, cec->addresses >> 8, HDMI_CEC_ADDR_H);9192return 0;93}9495static int dw_hdmi_cec_transmit(struct cec_adapter *adap, u8 attempts,96u32 signal_free_time, struct cec_msg *msg)97{98struct dw_hdmi_cec *cec = cec_get_drvdata(adap);99unsigned int i, ctrl;100101switch (signal_free_time) {102case CEC_SIGNAL_FREE_TIME_RETRY:103ctrl = CEC_CTRL_RETRY;104break;105case CEC_SIGNAL_FREE_TIME_NEW_INITIATOR:106default:107ctrl = CEC_CTRL_NORMAL;108break;109case CEC_SIGNAL_FREE_TIME_NEXT_XFER:110ctrl = CEC_CTRL_IMMED;111break;112}113114for (i = 0; i < msg->len; i++)115dw_hdmi_write(cec, msg->msg[i], HDMI_CEC_TX_DATA0 + i);116117dw_hdmi_write(cec, msg->len, HDMI_CEC_TX_CNT);118dw_hdmi_write(cec, ctrl | CEC_CTRL_START, HDMI_CEC_CTRL);119120return 0;121}122123static irqreturn_t dw_hdmi_cec_hardirq(int irq, void *data)124{125struct cec_adapter *adap = data;126struct dw_hdmi_cec *cec = cec_get_drvdata(adap);127unsigned int stat = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0);128irqreturn_t ret = IRQ_HANDLED;129130if (stat == 0)131return IRQ_NONE;132133dw_hdmi_write(cec, stat, HDMI_IH_CEC_STAT0);134135if (stat & CEC_STAT_ERROR_INIT) {136cec->tx_status = CEC_TX_STATUS_ERROR;137cec->tx_done = true;138ret = IRQ_WAKE_THREAD;139} else if (stat & CEC_STAT_DONE) {140cec->tx_status = CEC_TX_STATUS_OK;141cec->tx_done = true;142ret = IRQ_WAKE_THREAD;143} else if (stat & CEC_STAT_NACK) {144cec->tx_status = CEC_TX_STATUS_NACK;145cec->tx_done = true;146ret = IRQ_WAKE_THREAD;147} else if (stat & CEC_STAT_ARBLOST) {148cec->tx_status = CEC_TX_STATUS_ARB_LOST;149cec->tx_done = true;150ret = IRQ_WAKE_THREAD;151}152153if (stat & CEC_STAT_EOM) {154unsigned int len, i;155156len = dw_hdmi_read(cec, HDMI_CEC_RX_CNT);157if (len > sizeof(cec->rx_msg.msg))158len = sizeof(cec->rx_msg.msg);159160for (i = 0; i < len; i++)161cec->rx_msg.msg[i] =162dw_hdmi_read(cec, HDMI_CEC_RX_DATA0 + i);163164dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);165166cec->rx_msg.len = len;167smp_wmb();168cec->rx_done = true;169170ret = IRQ_WAKE_THREAD;171}172173return ret;174}175176static irqreturn_t dw_hdmi_cec_thread(int irq, void *data)177{178struct cec_adapter *adap = data;179struct dw_hdmi_cec *cec = cec_get_drvdata(adap);180181if (cec->tx_done) {182cec->tx_done = false;183cec_transmit_attempt_done(adap, cec->tx_status);184}185if (cec->rx_done) {186cec->rx_done = false;187smp_rmb();188cec_received_msg(adap, &cec->rx_msg);189}190return IRQ_HANDLED;191}192193static int dw_hdmi_cec_enable(struct cec_adapter *adap, bool enable)194{195struct dw_hdmi_cec *cec = cec_get_drvdata(adap);196197if (!enable) {198dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);199dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);200dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);201202cec->ops->disable(cec->hdmi);203} else {204unsigned int irqs;205206dw_hdmi_write(cec, 0, HDMI_CEC_CTRL);207dw_hdmi_write(cec, ~0, HDMI_IH_CEC_STAT0);208dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);209210dw_hdmi_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID);211212cec->ops->enable(cec->hdmi);213214irqs = CEC_STAT_ERROR_INIT | CEC_STAT_NACK | CEC_STAT_EOM |215CEC_STAT_ARBLOST | CEC_STAT_DONE;216dw_hdmi_write(cec, irqs, HDMI_CEC_POLARITY);217dw_hdmi_write(cec, ~irqs, HDMI_CEC_MASK);218dw_hdmi_write(cec, ~irqs, HDMI_IH_MUTE_CEC_STAT0);219}220return 0;221}222223static const struct cec_adap_ops dw_hdmi_cec_ops = {224.adap_enable = dw_hdmi_cec_enable,225.adap_log_addr = dw_hdmi_cec_log_addr,226.adap_transmit = dw_hdmi_cec_transmit,227};228229static void dw_hdmi_cec_del(void *data)230{231struct dw_hdmi_cec *cec = data;232233cec_delete_adapter(cec->adap);234}235236static int dw_hdmi_cec_probe(struct platform_device *pdev)237{238struct dw_hdmi_cec_data *data = dev_get_platdata(&pdev->dev);239struct dw_hdmi_cec *cec;240int ret;241242if (!data)243return -ENXIO;244245/*246* Our device is just a convenience - we want to link to the real247* hardware device here, so that userspace can see the association248* between the HDMI hardware and its associated CEC chardev.249*/250cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);251if (!cec)252return -ENOMEM;253254cec->irq = data->irq;255cec->ops = data->ops;256cec->hdmi = data->hdmi;257258platform_set_drvdata(pdev, cec);259260dw_hdmi_write(cec, 0, HDMI_CEC_TX_CNT);261dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);262dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);263dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);264265cec->adap = cec_allocate_adapter(&dw_hdmi_cec_ops, cec, "dw_hdmi",266CEC_CAP_DEFAULTS |267CEC_CAP_CONNECTOR_INFO,268CEC_MAX_LOG_ADDRS);269if (IS_ERR(cec->adap))270return PTR_ERR(cec->adap);271272/* override the module pointer */273cec->adap->owner = THIS_MODULE;274275ret = devm_add_action_or_reset(&pdev->dev, dw_hdmi_cec_del, cec);276if (ret)277return ret;278279ret = devm_request_threaded_irq(&pdev->dev, cec->irq,280dw_hdmi_cec_hardirq,281dw_hdmi_cec_thread, IRQF_SHARED,282"dw-hdmi-cec", cec->adap);283if (ret < 0)284return ret;285286cec->notify = cec_notifier_cec_adap_register(pdev->dev.parent,287NULL, cec->adap);288if (!cec->notify)289return -ENOMEM;290291ret = cec_register_adapter(cec->adap, pdev->dev.parent);292if (ret < 0) {293cec_notifier_cec_adap_unregister(cec->notify, cec->adap);294return ret;295}296297/*298* CEC documentation says we must not call cec_delete_adapter299* after a successful call to cec_register_adapter().300*/301devm_remove_action(&pdev->dev, dw_hdmi_cec_del, cec);302303return 0;304}305306static void dw_hdmi_cec_remove(struct platform_device *pdev)307{308struct dw_hdmi_cec *cec = platform_get_drvdata(pdev);309310cec_notifier_cec_adap_unregister(cec->notify, cec->adap);311cec_unregister_adapter(cec->adap);312}313314static int dw_hdmi_cec_resume(struct device *dev)315{316struct dw_hdmi_cec *cec = dev_get_drvdata(dev);317318/* Restore logical address */319dw_hdmi_write(cec, cec->addresses & 255, HDMI_CEC_ADDR_L);320dw_hdmi_write(cec, cec->addresses >> 8, HDMI_CEC_ADDR_H);321322/* Restore interrupt status/mask registers */323dw_hdmi_write(cec, cec->regs_polarity, HDMI_CEC_POLARITY);324dw_hdmi_write(cec, cec->regs_mask, HDMI_CEC_MASK);325dw_hdmi_write(cec, cec->regs_mute_stat0, HDMI_IH_MUTE_CEC_STAT0);326327return 0;328}329330static int dw_hdmi_cec_suspend(struct device *dev)331{332struct dw_hdmi_cec *cec = dev_get_drvdata(dev);333334/* store interrupt status/mask registers */335cec->regs_polarity = dw_hdmi_read(cec, HDMI_CEC_POLARITY);336cec->regs_mask = dw_hdmi_read(cec, HDMI_CEC_MASK);337cec->regs_mute_stat0 = dw_hdmi_read(cec, HDMI_IH_MUTE_CEC_STAT0);338339return 0;340}341342static const struct dev_pm_ops dw_hdmi_cec_pm = {343SYSTEM_SLEEP_PM_OPS(dw_hdmi_cec_suspend, dw_hdmi_cec_resume)344};345346static struct platform_driver dw_hdmi_cec_driver = {347.probe = dw_hdmi_cec_probe,348.remove = dw_hdmi_cec_remove,349.driver = {350.name = "dw-hdmi-cec",351.pm = pm_ptr(&dw_hdmi_cec_pm),352},353};354module_platform_driver(dw_hdmi_cec_driver);355356MODULE_AUTHOR("Russell King <[email protected]>");357MODULE_DESCRIPTION("Synopsys Designware HDMI CEC driver for i.MX");358MODULE_LICENSE("GPL");359MODULE_ALIAS(PLATFORM_MODULE_PREFIX "dw-hdmi-cec");360361362