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