1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* drivers/media/platform/s5p-cec/s5p_cec.c
4 * Samsung S5P CEC driver
6 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
8 * This driver is based on the "cec interface driver for exynos soc" by
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/of_platform.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/timer.h>
22 #include <linux/workqueue.h>
23 #include <media/cec.h>
24 #include <media/cec-notifier.h>
26 #include "exynos_hdmi_cec.h"
30 #define CEC_NAME "s5p-cec"
33 module_param(debug
, int, 0644);
34 MODULE_PARM_DESC(debug
, "debug level (0-2)");
36 static int s5p_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
38 struct s5p_cec_dev
*cec
= cec_get_drvdata(adap
);
41 pm_runtime_get_sync(cec
->dev
);
45 s5p_cec_set_divider(cec
);
46 s5p_cec_threshold(cec
);
48 s5p_cec_unmask_tx_interrupts(cec
);
49 s5p_cec_unmask_rx_interrupts(cec
);
50 s5p_cec_enable_rx(cec
);
52 s5p_cec_mask_tx_interrupts(cec
);
53 s5p_cec_mask_rx_interrupts(cec
);
54 pm_runtime_disable(cec
->dev
);
60 static int s5p_cec_adap_log_addr(struct cec_adapter
*adap
, u8 addr
)
62 struct s5p_cec_dev
*cec
= cec_get_drvdata(adap
);
64 s5p_cec_set_addr(cec
, addr
);
68 static int s5p_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
69 u32 signal_free_time
, struct cec_msg
*msg
)
71 struct s5p_cec_dev
*cec
= cec_get_drvdata(adap
);
74 * Unclear if 0 retries are allowed by the hardware, so have 1 as
77 s5p_cec_copy_packet(cec
, msg
->msg
, msg
->len
, max(1, attempts
- 1));
81 static irqreturn_t
s5p_cec_irq_handler(int irq
, void *priv
)
83 struct s5p_cec_dev
*cec
= priv
;
86 status
= s5p_cec_get_status(cec
);
88 dev_dbg(cec
->dev
, "irq received\n");
90 if (status
& CEC_STATUS_TX_DONE
) {
91 if (status
& CEC_STATUS_TX_NACK
) {
92 dev_dbg(cec
->dev
, "CEC_STATUS_TX_NACK set\n");
94 } else if (status
& CEC_STATUS_TX_ERROR
) {
95 dev_dbg(cec
->dev
, "CEC_STATUS_TX_ERROR set\n");
96 cec
->tx
= STATE_ERROR
;
98 dev_dbg(cec
->dev
, "CEC_STATUS_TX_DONE\n");
101 s5p_clr_pending_tx(cec
);
104 if (status
& CEC_STATUS_RX_DONE
) {
105 if (status
& CEC_STATUS_RX_ERROR
) {
106 dev_dbg(cec
->dev
, "CEC_STATUS_RX_ERROR set\n");
107 s5p_cec_rx_reset(cec
);
108 s5p_cec_enable_rx(cec
);
110 dev_dbg(cec
->dev
, "CEC_STATUS_RX_DONE set\n");
111 if (cec
->rx
!= STATE_IDLE
)
112 dev_dbg(cec
->dev
, "Buffer overrun (worker did not process previous message)\n");
113 cec
->rx
= STATE_BUSY
;
114 cec
->msg
.len
= status
>> 24;
115 cec
->msg
.rx_status
= CEC_RX_STATUS_OK
;
116 s5p_cec_get_rx_buf(cec
, cec
->msg
.len
,
118 cec
->rx
= STATE_DONE
;
119 s5p_cec_enable_rx(cec
);
121 /* Clear interrupt pending bit */
122 s5p_clr_pending_rx(cec
);
124 return IRQ_WAKE_THREAD
;
127 static irqreturn_t
s5p_cec_irq_handler_thread(int irq
, void *priv
)
129 struct s5p_cec_dev
*cec
= priv
;
131 dev_dbg(cec
->dev
, "irq processing thread\n");
134 cec_transmit_done(cec
->adap
, CEC_TX_STATUS_OK
, 0, 0, 0, 0);
135 cec
->tx
= STATE_IDLE
;
138 cec_transmit_done(cec
->adap
,
139 CEC_TX_STATUS_MAX_RETRIES
| CEC_TX_STATUS_NACK
,
141 cec
->tx
= STATE_IDLE
;
144 cec_transmit_done(cec
->adap
,
145 CEC_TX_STATUS_MAX_RETRIES
| CEC_TX_STATUS_ERROR
,
147 cec
->tx
= STATE_IDLE
;
150 dev_err(cec
->dev
, "state set to busy, this should not occur here\n");
158 cec_received_msg(cec
->adap
, &cec
->msg
);
159 cec
->rx
= STATE_IDLE
;
168 static const struct cec_adap_ops s5p_cec_adap_ops
= {
169 .adap_enable
= s5p_cec_adap_enable
,
170 .adap_log_addr
= s5p_cec_adap_log_addr
,
171 .adap_transmit
= s5p_cec_adap_transmit
,
174 static int s5p_cec_probe(struct platform_device
*pdev
)
176 struct device
*dev
= &pdev
->dev
;
177 struct device
*hdmi_dev
;
178 struct resource
*res
;
179 struct s5p_cec_dev
*cec
;
180 bool needs_hpd
= of_property_read_bool(pdev
->dev
.of_node
, "needs-hpd");
183 hdmi_dev
= cec_notifier_parse_hdmi_phandle(dev
);
185 if (IS_ERR(hdmi_dev
))
186 return PTR_ERR(hdmi_dev
);
188 cec
= devm_kzalloc(&pdev
->dev
, sizeof(*cec
), GFP_KERNEL
);
194 cec
->irq
= platform_get_irq(pdev
, 0);
198 ret
= devm_request_threaded_irq(dev
, cec
->irq
, s5p_cec_irq_handler
,
199 s5p_cec_irq_handler_thread
, 0, pdev
->name
, cec
);
203 cec
->clk
= devm_clk_get(dev
, "hdmicec");
204 if (IS_ERR(cec
->clk
))
205 return PTR_ERR(cec
->clk
);
207 cec
->pmu
= syscon_regmap_lookup_by_phandle(dev
->of_node
,
208 "samsung,syscon-phandle");
209 if (IS_ERR(cec
->pmu
))
210 return -EPROBE_DEFER
;
212 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
213 cec
->reg
= devm_ioremap_resource(dev
, res
);
214 if (IS_ERR(cec
->reg
))
215 return PTR_ERR(cec
->reg
);
217 cec
->adap
= cec_allocate_adapter(&s5p_cec_adap_ops
, cec
, CEC_NAME
,
218 CEC_CAP_DEFAULTS
| (needs_hpd
? CEC_CAP_NEEDS_HPD
: 0) |
219 CEC_CAP_CONNECTOR_INFO
, 1);
220 ret
= PTR_ERR_OR_ZERO(cec
->adap
);
224 cec
->notifier
= cec_notifier_cec_adap_register(hdmi_dev
, NULL
,
226 if (!cec
->notifier
) {
228 goto err_delete_adapter
;
231 ret
= cec_register_adapter(cec
->adap
, &pdev
->dev
);
235 platform_set_drvdata(pdev
, cec
);
236 pm_runtime_enable(dev
);
238 dev_dbg(dev
, "successfully probed\n");
242 cec_notifier_cec_adap_unregister(cec
->notifier
, cec
->adap
);
245 cec_delete_adapter(cec
->adap
);
249 static int s5p_cec_remove(struct platform_device
*pdev
)
251 struct s5p_cec_dev
*cec
= platform_get_drvdata(pdev
);
253 cec_notifier_cec_adap_unregister(cec
->notifier
, cec
->adap
);
254 cec_unregister_adapter(cec
->adap
);
255 pm_runtime_disable(&pdev
->dev
);
259 static int __maybe_unused
s5p_cec_runtime_suspend(struct device
*dev
)
261 struct s5p_cec_dev
*cec
= dev_get_drvdata(dev
);
263 clk_disable_unprepare(cec
->clk
);
267 static int __maybe_unused
s5p_cec_runtime_resume(struct device
*dev
)
269 struct s5p_cec_dev
*cec
= dev_get_drvdata(dev
);
272 ret
= clk_prepare_enable(cec
->clk
);
278 static const struct dev_pm_ops s5p_cec_pm_ops
= {
279 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
280 pm_runtime_force_resume
)
281 SET_RUNTIME_PM_OPS(s5p_cec_runtime_suspend
, s5p_cec_runtime_resume
,
285 static const struct of_device_id s5p_cec_match
[] = {
287 .compatible
= "samsung,s5p-cec",
291 MODULE_DEVICE_TABLE(of
, s5p_cec_match
);
293 static struct platform_driver s5p_cec_pdrv
= {
294 .probe
= s5p_cec_probe
,
295 .remove
= s5p_cec_remove
,
298 .of_match_table
= s5p_cec_match
,
299 .pm
= &s5p_cec_pm_ops
,
303 module_platform_driver(s5p_cec_pdrv
);
305 MODULE_AUTHOR("Kamil Debski <kamil@wypas.org>");
306 MODULE_LICENSE("GPL");
307 MODULE_DESCRIPTION("Samsung S5P CEC driver");