1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* drivers/media/platform/s5p-cec/s5p_cec.c
3 *
4 * Samsung S5P CEC driver
5 *
6 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 *
8 * This driver is based on the "cec interface driver for exynos soc" by
9 * SangPil Moon.
10 */
11
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>
17 #include <linux/of.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>
25
26 #include "exynos_hdmi_cec.h"
27 #include "regs-cec.h"
28 #include "s5p_cec.h"
29
30 #define CEC_NAME "s5p-cec"
31
32 static int debug;
33 module_param(debug, int, 0644);
34 MODULE_PARM_DESC(debug, "debug level (0-2)");
35
s5p_cec_adap_enable(struct cec_adapter * adap,bool enable)36 static int s5p_cec_adap_enable(struct cec_adapter *adap, bool enable)
37 {
38 int ret;
39 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
40
41 if (enable) {
42 ret = pm_runtime_resume_and_get(cec->dev);
43 if (ret < 0)
44 return ret;
45
46 s5p_cec_reset(cec);
47
48 s5p_cec_set_divider(cec);
49 s5p_cec_threshold(cec);
50
51 s5p_cec_unmask_tx_interrupts(cec);
52 s5p_cec_unmask_rx_interrupts(cec);
53 s5p_cec_enable_rx(cec);
54 } else {
55 s5p_cec_mask_tx_interrupts(cec);
56 s5p_cec_mask_rx_interrupts(cec);
57 pm_runtime_put(cec->dev);
58 }
59
60 return 0;
61 }
62
s5p_cec_adap_log_addr(struct cec_adapter * adap,u8 addr)63 static int s5p_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
64 {
65 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
66
67 s5p_cec_set_addr(cec, addr);
68 return 0;
69 }
70
s5p_cec_adap_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)71 static int s5p_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
72 u32 signal_free_time, struct cec_msg *msg)
73 {
74 struct s5p_cec_dev *cec = cec_get_drvdata(adap);
75
76 /*
77 * Unclear if 0 retries are allowed by the hardware, so have 1 as
78 * the minimum.
79 */
80 s5p_cec_copy_packet(cec, msg->msg, msg->len, max(1, attempts - 1));
81 return 0;
82 }
83
s5p_cec_irq_handler(int irq,void * priv)84 static irqreturn_t s5p_cec_irq_handler(int irq, void *priv)
85 {
86 struct s5p_cec_dev *cec = priv;
87 u32 status = 0;
88
89 status = s5p_cec_get_status(cec);
90
91 dev_dbg(cec->dev, "irq received\n");
92
93 if (status & CEC_STATUS_TX_DONE) {
94 if (status & CEC_STATUS_TX_NACK) {
95 dev_dbg(cec->dev, "CEC_STATUS_TX_NACK set\n");
96 cec->tx = STATE_NACK;
97 } else if (status & CEC_STATUS_TX_ERROR) {
98 dev_dbg(cec->dev, "CEC_STATUS_TX_ERROR set\n");
99 cec->tx = STATE_ERROR;
100 } else {
101 dev_dbg(cec->dev, "CEC_STATUS_TX_DONE\n");
102 cec->tx = STATE_DONE;
103 }
104 s5p_clr_pending_tx(cec);
105 }
106
107 if (status & CEC_STATUS_RX_DONE) {
108 if (status & CEC_STATUS_RX_ERROR) {
109 dev_dbg(cec->dev, "CEC_STATUS_RX_ERROR set\n");
110 s5p_cec_rx_reset(cec);
111 s5p_cec_enable_rx(cec);
112 } else {
113 dev_dbg(cec->dev, "CEC_STATUS_RX_DONE set\n");
114 if (cec->rx != STATE_IDLE)
115 dev_dbg(cec->dev, "Buffer overrun (worker did not process previous message)\n");
116 cec->rx = STATE_BUSY;
117 cec->msg.len = status >> 24;
118 if (cec->msg.len > CEC_MAX_MSG_SIZE)
119 cec->msg.len = CEC_MAX_MSG_SIZE;
120 cec->msg.rx_status = CEC_RX_STATUS_OK;
121 s5p_cec_get_rx_buf(cec, cec->msg.len,
122 cec->msg.msg);
123 cec->rx = STATE_DONE;
124 s5p_cec_enable_rx(cec);
125 }
126 /* Clear interrupt pending bit */
127 s5p_clr_pending_rx(cec);
128 }
129 return IRQ_WAKE_THREAD;
130 }
131
s5p_cec_irq_handler_thread(int irq,void * priv)132 static irqreturn_t s5p_cec_irq_handler_thread(int irq, void *priv)
133 {
134 struct s5p_cec_dev *cec = priv;
135
136 dev_dbg(cec->dev, "irq processing thread\n");
137 switch (cec->tx) {
138 case STATE_DONE:
139 cec_transmit_done(cec->adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
140 cec->tx = STATE_IDLE;
141 break;
142 case STATE_NACK:
143 cec_transmit_done(cec->adap,
144 CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_NACK,
145 0, 1, 0, 0);
146 cec->tx = STATE_IDLE;
147 break;
148 case STATE_ERROR:
149 cec_transmit_done(cec->adap,
150 CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_ERROR,
151 0, 0, 0, 1);
152 cec->tx = STATE_IDLE;
153 break;
154 case STATE_BUSY:
155 dev_err(cec->dev, "state set to busy, this should not occur here\n");
156 break;
157 default:
158 break;
159 }
160
161 switch (cec->rx) {
162 case STATE_DONE:
163 cec_received_msg(cec->adap, &cec->msg);
164 cec->rx = STATE_IDLE;
165 break;
166 default:
167 break;
168 }
169
170 return IRQ_HANDLED;
171 }
172
173 static const struct cec_adap_ops s5p_cec_adap_ops = {
174 .adap_enable = s5p_cec_adap_enable,
175 .adap_log_addr = s5p_cec_adap_log_addr,
176 .adap_transmit = s5p_cec_adap_transmit,
177 };
178
s5p_cec_probe(struct platform_device * pdev)179 static int s5p_cec_probe(struct platform_device *pdev)
180 {
181 struct device *dev = &pdev->dev;
182 struct device *hdmi_dev;
183 struct resource *res;
184 struct s5p_cec_dev *cec;
185 bool needs_hpd = of_property_read_bool(pdev->dev.of_node, "needs-hpd");
186 int ret;
187
188 hdmi_dev = cec_notifier_parse_hdmi_phandle(dev);
189
190 if (IS_ERR(hdmi_dev))
191 return PTR_ERR(hdmi_dev);
192
193 cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
194 if (!cec)
195 return -ENOMEM;
196
197 cec->dev = dev;
198
199 cec->irq = platform_get_irq(pdev, 0);
200 if (cec->irq < 0)
201 return cec->irq;
202
203 ret = devm_request_threaded_irq(dev, cec->irq, s5p_cec_irq_handler,
204 s5p_cec_irq_handler_thread, 0, pdev->name, cec);
205 if (ret)
206 return ret;
207
208 cec->clk = devm_clk_get(dev, "hdmicec");
209 if (IS_ERR(cec->clk))
210 return PTR_ERR(cec->clk);
211
212 cec->pmu = syscon_regmap_lookup_by_phandle(dev->of_node,
213 "samsung,syscon-phandle");
214 if (IS_ERR(cec->pmu))
215 return -EPROBE_DEFER;
216
217 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
218 cec->reg = devm_ioremap_resource(dev, res);
219 if (IS_ERR(cec->reg))
220 return PTR_ERR(cec->reg);
221
222 cec->adap = cec_allocate_adapter(&s5p_cec_adap_ops, cec, CEC_NAME,
223 CEC_CAP_DEFAULTS | (needs_hpd ? CEC_CAP_NEEDS_HPD : 0) |
224 CEC_CAP_CONNECTOR_INFO, 1);
225 ret = PTR_ERR_OR_ZERO(cec->adap);
226 if (ret)
227 return ret;
228
229 cec->notifier = cec_notifier_cec_adap_register(hdmi_dev, NULL,
230 cec->adap);
231 if (!cec->notifier) {
232 ret = -ENOMEM;
233 goto err_delete_adapter;
234 }
235
236 ret = cec_register_adapter(cec->adap, &pdev->dev);
237 if (ret)
238 goto err_notifier;
239
240 platform_set_drvdata(pdev, cec);
241 pm_runtime_enable(dev);
242
243 dev_dbg(dev, "successfully probed\n");
244 return 0;
245
246 err_notifier:
247 cec_notifier_cec_adap_unregister(cec->notifier, cec->adap);
248
249 err_delete_adapter:
250 cec_delete_adapter(cec->adap);
251 return ret;
252 }
253
s5p_cec_remove(struct platform_device * pdev)254 static int s5p_cec_remove(struct platform_device *pdev)
255 {
256 struct s5p_cec_dev *cec = platform_get_drvdata(pdev);
257
258 cec_notifier_cec_adap_unregister(cec->notifier, cec->adap);
259 cec_unregister_adapter(cec->adap);
260 pm_runtime_disable(&pdev->dev);
261 return 0;
262 }
263
s5p_cec_runtime_suspend(struct device * dev)264 static int __maybe_unused s5p_cec_runtime_suspend(struct device *dev)
265 {
266 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
267
268 clk_disable_unprepare(cec->clk);
269 return 0;
270 }
271
s5p_cec_runtime_resume(struct device * dev)272 static int __maybe_unused s5p_cec_runtime_resume(struct device *dev)
273 {
274 struct s5p_cec_dev *cec = dev_get_drvdata(dev);
275 int ret;
276
277 ret = clk_prepare_enable(cec->clk);
278 if (ret < 0)
279 return ret;
280 return 0;
281 }
282
283 static const struct dev_pm_ops s5p_cec_pm_ops = {
284 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
285 pm_runtime_force_resume)
286 SET_RUNTIME_PM_OPS(s5p_cec_runtime_suspend, s5p_cec_runtime_resume,
287 NULL)
288 };
289
290 static const struct of_device_id s5p_cec_match[] = {
291 {
292 .compatible = "samsung,s5p-cec",
293 },
294 {},
295 };
296 MODULE_DEVICE_TABLE(of, s5p_cec_match);
297
298 static struct platform_driver s5p_cec_pdrv = {
299 .probe = s5p_cec_probe,
300 .remove = s5p_cec_remove,
301 .driver = {
302 .name = CEC_NAME,
303 .of_match_table = s5p_cec_match,
304 .pm = &s5p_cec_pm_ops,
305 },
306 };
307
308 module_platform_driver(s5p_cec_pdrv);
309
310 MODULE_AUTHOR("Kamil Debski <kamil@wypas.org>");
311 MODULE_LICENSE("GPL");
312 MODULE_DESCRIPTION("Samsung S5P CEC driver");
313