1 /* drivers/media/platform/s5p-cec/s5p_cec.c
3 * Samsung S5P CEC driver
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This driver is based on the "cec interface driver for exynos soc" by
16 #include <linux/clk.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/timer.h>
25 #include <linux/workqueue.h>
26 #include <media/cec.h>
28 #include "exynos_hdmi_cec.h"
32 #define CEC_NAME "s5p-cec"
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "debug level (0-2)");
38 static int s5p_cec_adap_enable(struct cec_adapter *adap, bool enable)
40 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
43 pm_runtime_get_sync(cec->dev);
47 s5p_cec_set_divider(cec);
48 s5p_cec_threshold(cec);
50 s5p_cec_unmask_tx_interrupts(cec);
51 s5p_cec_unmask_rx_interrupts(cec);
52 s5p_cec_enable_rx(cec);
54 s5p_cec_mask_tx_interrupts(cec);
55 s5p_cec_mask_rx_interrupts(cec);
56 pm_runtime_disable(cec->dev);
62 static int s5p_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
64 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
66 s5p_cec_set_addr(cec, addr);
70 static int s5p_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
71 u32 signal_free_time, struct cec_msg *msg)
73 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
76 * Unclear if 0 retries are allowed by the hardware, so have 1 as
79 s5p_cec_copy_packet(cec, msg->msg, msg->len, max(1, attempts - 1));
83 static irqreturn_t s5p_cec_irq_handler(int irq, void *priv)
85 struct s5p_cec_dev *cec = priv;
88 status = s5p_cec_get_status(cec);
90 dev_dbg(cec->dev, "irq received\n");
92 if (status & CEC_STATUS_TX_DONE) {
93 if (status & CEC_STATUS_TX_ERROR) {
94 dev_dbg(cec->dev, "CEC_STATUS_TX_ERROR set\n");
95 cec->tx = STATE_ERROR;
97 dev_dbg(cec->dev, "CEC_STATUS_TX_DONE\n");
100 s5p_clr_pending_tx(cec);
103 if (status & CEC_STATUS_RX_DONE) {
104 if (status & CEC_STATUS_RX_ERROR) {
105 dev_dbg(cec->dev, "CEC_STATUS_RX_ERROR set\n");
106 s5p_cec_rx_reset(cec);
107 s5p_cec_enable_rx(cec);
109 dev_dbg(cec->dev, "CEC_STATUS_RX_DONE set\n");
110 if (cec->rx != STATE_IDLE)
111 dev_dbg(cec->dev, "Buffer overrun (worker did not process previous message)\n");
112 cec->rx = STATE_BUSY;
113 cec->msg.len = status >> 24;
114 cec->msg.rx_status = CEC_RX_STATUS_OK;
115 s5p_cec_get_rx_buf(cec, cec->msg.len,
117 cec->rx = STATE_DONE;
118 s5p_cec_enable_rx(cec);
120 /* Clear interrupt pending bit */
121 s5p_clr_pending_rx(cec);
123 return IRQ_WAKE_THREAD;
126 static irqreturn_t s5p_cec_irq_handler_thread(int irq, void *priv)
128 struct s5p_cec_dev *cec = priv;
130 dev_dbg(cec->dev, "irq processing thread\n");
133 cec_transmit_done(cec->adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
134 cec->tx = STATE_IDLE;
137 cec_transmit_done(cec->adap,
138 CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_ERROR,
140 cec->tx = STATE_IDLE;
143 dev_err(cec->dev, "state set to busy, this should not occur here\n");
151 cec_received_msg(cec->adap, &cec->msg);
152 cec->rx = STATE_IDLE;
161 static const struct cec_adap_ops s5p_cec_adap_ops = {
162 .adap_enable = s5p_cec_adap_enable,
163 .adap_log_addr = s5p_cec_adap_log_addr,
164 .adap_transmit = s5p_cec_adap_transmit,
167 static int s5p_cec_probe(struct platform_device *pdev)
169 struct device *dev = &pdev->dev;
170 struct resource *res;
171 struct s5p_cec_dev *cec;
174 cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
180 cec->irq = platform_get_irq(pdev, 0);
184 ret = devm_request_threaded_irq(dev, cec->irq, s5p_cec_irq_handler,
185 s5p_cec_irq_handler_thread, 0, pdev->name, cec);
189 cec->clk = devm_clk_get(dev, "hdmicec");
190 if (IS_ERR(cec->clk))
191 return PTR_ERR(cec->clk);
193 cec->pmu = syscon_regmap_lookup_by_phandle(dev->of_node,
194 "samsung,syscon-phandle");
195 if (IS_ERR(cec->pmu))
196 return -EPROBE_DEFER;
198 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
199 cec->reg = devm_ioremap_resource(dev, res);
200 if (IS_ERR(cec->reg))
201 return PTR_ERR(cec->reg);
203 cec->adap = cec_allocate_adapter(&s5p_cec_adap_ops, cec,
205 CEC_CAP_PHYS_ADDR | CEC_CAP_LOG_ADDRS | CEC_CAP_TRANSMIT |
206 CEC_CAP_PASSTHROUGH | CEC_CAP_RC, 1);
207 ret = PTR_ERR_OR_ZERO(cec->adap);
210 ret = cec_register_adapter(cec->adap, &pdev->dev);
212 cec_delete_adapter(cec->adap);
216 platform_set_drvdata(pdev, cec);
217 pm_runtime_enable(dev);
219 dev_dbg(dev, "successfuly probed\n");
223 static int s5p_cec_remove(struct platform_device *pdev)
225 struct s5p_cec_dev *cec = platform_get_drvdata(pdev);
227 cec_unregister_adapter(cec->adap);
228 pm_runtime_disable(&pdev->dev);
232 static int __maybe_unused s5p_cec_runtime_suspend(struct device *dev)
234 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
236 clk_disable_unprepare(cec->clk);
240 static int __maybe_unused s5p_cec_runtime_resume(struct device *dev)
242 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
245 ret = clk_prepare_enable(cec->clk);
251 static const struct dev_pm_ops s5p_cec_pm_ops = {
252 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
253 pm_runtime_force_resume)
254 SET_RUNTIME_PM_OPS(s5p_cec_runtime_suspend, s5p_cec_runtime_resume,
258 static const struct of_device_id s5p_cec_match[] = {
260 .compatible = "samsung,s5p-cec",
264 MODULE_DEVICE_TABLE(of, s5p_cec_match);
266 static struct platform_driver s5p_cec_pdrv = {
267 .probe = s5p_cec_probe,
268 .remove = s5p_cec_remove,
271 .of_match_table = s5p_cec_match,
272 .pm = &s5p_cec_pm_ops,
276 module_platform_driver(s5p_cec_pdrv);
278 MODULE_AUTHOR("Kamil Debski <kamil@wypas.org>");
279 MODULE_LICENSE("GPL");
280 MODULE_DESCRIPTION("Samsung S5P CEC driver");