xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/bridge/synopsys/dw-hdmi-cec.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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/input.h>
8 #include <linux/interrupt.h>
9 #include <linux/io.h>
10 #include <linux/miscdevice.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 
16 #include <drm/drm_edid.h>
17 #include <drm/bridge/dw_hdmi.h>
18 
19 #include <media/cec.h>
20 #include <media/cec-notifier.h>
21 
22 #include "dw-hdmi-cec.h"
23 
24 enum {
25 	HDMI_IH_CEC_STAT0	= 0x0106,
26 	HDMI_IH_MUTE_CEC_STAT0	= 0x0186,
27 	HDMI_IH_MUTE		= 0x01ff,
28 
29 	HDMI_CEC_CTRL		= 0x7d00,
30 	CEC_TRANS_MASK		= 0x7,
31 	CEC_CTRL_STANDBY	= BIT(4),
32 	CEC_CTRL_START		= BIT(0),
33 	CEC_CTRL_FRAME_TYP	= 3 << 1,
34 	CEC_CTRL_RETRY		= 0 << 1,
35 	CEC_CTRL_NORMAL		= 1 << 1,
36 	CEC_CTRL_IMMED		= 2 << 1,
37 
38 	HDMI_CEC_STAT		= 0x7d01,
39 	CEC_STAT_DONE		= BIT(0),
40 	CEC_STAT_EOM		= BIT(1),
41 	CEC_STAT_NACK		= BIT(2),
42 	CEC_STAT_ARBLOST	= BIT(3),
43 	CEC_STAT_ERROR_INIT	= BIT(4),
44 	CEC_STAT_ERROR_FOLL	= BIT(5),
45 	CEC_STAT_WAKEUP		= BIT(6),
46 
47 	HDMI_CEC_MASK		= 0x7d02,
48 	HDMI_CEC_POLARITY	= 0x7d03,
49 	HDMI_CEC_INT		= 0x7d04,
50 	HDMI_CEC_ADDR_L		= 0x7d05,
51 	HDMI_CEC_ADDR_H		= 0x7d06,
52 	HDMI_CEC_TX_CNT		= 0x7d07,
53 	HDMI_CEC_RX_CNT		= 0x7d08,
54 	HDMI_CEC_TX_DATA0	= 0x7d10,
55 	HDMI_CEC_RX_DATA0	= 0x7d20,
56 	HDMI_CEC_RX_DATA1	= 0x7d21,
57 	HDMI_CEC_LOCK		= 0x7d30,
58 	HDMI_CEC_WKUPCTRL	= 0x7d31,
59 };
60 
61 struct dw_hdmi_cec {
62 	struct device *dev;
63 	struct dw_hdmi *hdmi;
64 	struct miscdevice misc_dev;
65 	const struct dw_hdmi_cec_ops *ops;
66 	u32 addresses;
67 	struct cec_adapter *adap;
68 	struct cec_msg rx_msg;
69 	unsigned int tx_status;
70 	bool tx_done;
71 	bool rx_done;
72 	struct cec_notifier *notify;
73 	struct input_dev *devinput;
74 	int irq;
75 	int wake_irq;
76 	bool wake_en;
77 	bool standby_en;
78 	struct mutex wake_lock;
79 };
80 
dw_hdmi_write(struct dw_hdmi_cec * cec,u8 val,int offset)81 static void dw_hdmi_write(struct dw_hdmi_cec *cec, u8 val, int offset)
82 {
83 	cec->ops->write(cec->hdmi, val, offset);
84 }
85 
dw_hdmi_read(struct dw_hdmi_cec * cec,int offset)86 static u8 dw_hdmi_read(struct dw_hdmi_cec *cec, int offset)
87 {
88 	return cec->ops->read(cec->hdmi, offset);
89 }
90 
dw_hdmi_mod(struct dw_hdmi_cec * cec,unsigned int offset,u8 mask,u8 val)91 static void dw_hdmi_mod(struct dw_hdmi_cec *cec, unsigned int offset, u8 mask, u8 val)
92 {
93 	cec->ops->mod(cec->hdmi, val, mask, offset);
94 }
95 
dw_hdmi_cec_log_addr(struct cec_adapter * adap,u8 logical_addr)96 static int dw_hdmi_cec_log_addr(struct cec_adapter *adap, u8 logical_addr)
97 {
98 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
99 
100 	if (logical_addr == CEC_LOG_ADDR_INVALID)
101 		cec->addresses = 0;
102 	else
103 		cec->addresses |= BIT(logical_addr) | BIT(15);
104 
105 	dw_hdmi_write(cec, cec->addresses & 255, HDMI_CEC_ADDR_L);
106 	dw_hdmi_write(cec, cec->addresses >> 8, HDMI_CEC_ADDR_H);
107 
108 	return 0;
109 }
110 
dw_hdmi_cec_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)111 static int dw_hdmi_cec_transmit(struct cec_adapter *adap, u8 attempts,
112 				u32 signal_free_time, struct cec_msg *msg)
113 {
114 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
115 	unsigned int i, ctrl;
116 
117 	switch (signal_free_time) {
118 	case CEC_SIGNAL_FREE_TIME_RETRY:
119 		ctrl = CEC_CTRL_RETRY;
120 		break;
121 	case CEC_SIGNAL_FREE_TIME_NEW_INITIATOR:
122 	default:
123 		ctrl = CEC_CTRL_NORMAL;
124 		break;
125 	case CEC_SIGNAL_FREE_TIME_NEXT_XFER:
126 		ctrl = CEC_CTRL_IMMED;
127 		break;
128 	}
129 
130 	for (i = 0; i < msg->len; i++)
131 		dw_hdmi_write(cec, msg->msg[i], HDMI_CEC_TX_DATA0 + i);
132 
133 	dw_hdmi_write(cec, msg->len, HDMI_CEC_TX_CNT);
134 	dw_hdmi_mod(cec, HDMI_CEC_CTRL, CEC_TRANS_MASK, ctrl | CEC_CTRL_START);
135 
136 	return 0;
137 }
138 
dw_hdmi_cec_hardirq(int irq,void * data)139 static irqreturn_t dw_hdmi_cec_hardirq(int irq, void *data)
140 {
141 	struct cec_adapter *adap = data;
142 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
143 	unsigned int stat = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0);
144 	irqreturn_t ret = IRQ_HANDLED;
145 
146 	if (stat == 0)
147 		return IRQ_NONE;
148 
149 	dw_hdmi_write(cec, stat, HDMI_IH_CEC_STAT0);
150 
151 	if (stat & CEC_STAT_ERROR_INIT) {
152 		cec->tx_status = CEC_TX_STATUS_ERROR;
153 		cec->tx_done = true;
154 		ret = IRQ_WAKE_THREAD;
155 	} else if (stat & CEC_STAT_DONE) {
156 		cec->tx_status = CEC_TX_STATUS_OK;
157 		cec->tx_done = true;
158 		ret = IRQ_WAKE_THREAD;
159 	} else if (stat & CEC_STAT_NACK) {
160 		cec->tx_status = CEC_TX_STATUS_NACK;
161 		cec->tx_done = true;
162 		ret = IRQ_WAKE_THREAD;
163 	}
164 
165 	if (stat & CEC_STAT_EOM) {
166 		unsigned int len, i;
167 
168 		len = dw_hdmi_read(cec, HDMI_CEC_RX_CNT);
169 		if (len > sizeof(cec->rx_msg.msg))
170 			len = sizeof(cec->rx_msg.msg);
171 
172 		for (i = 0; i < len; i++)
173 			cec->rx_msg.msg[i] =
174 				dw_hdmi_read(cec, HDMI_CEC_RX_DATA0 + i);
175 
176 		dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
177 
178 		cec->rx_msg.len = len;
179 		smp_wmb();
180 		cec->rx_done = true;
181 
182 		ret = IRQ_WAKE_THREAD;
183 	}
184 
185 	return ret;
186 }
187 
dw_hdmi_cec_thread(int irq,void * data)188 static irqreturn_t dw_hdmi_cec_thread(int irq, void *data)
189 {
190 	struct cec_adapter *adap = data;
191 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
192 
193 	if (cec->tx_done) {
194 		cec->tx_done = false;
195 		cec_transmit_attempt_done(adap, cec->tx_status);
196 	}
197 	if (cec->rx_done) {
198 		cec->rx_done = false;
199 		smp_rmb();
200 		cec_received_msg(adap, &cec->rx_msg);
201 	}
202 	return IRQ_HANDLED;
203 }
204 
dw_hdmi_cec_enable(struct cec_adapter * adap,bool enable)205 static int dw_hdmi_cec_enable(struct cec_adapter *adap, bool enable)
206 {
207 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
208 
209 	if (!enable) {
210 		dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);
211 
212 		if (cec->wake_en && cec->standby_en) {
213 			dw_hdmi_write(cec, 0xff, HDMI_IH_CEC_STAT0);
214 			dw_hdmi_mod(cec, HDMI_CEC_CTRL, CEC_CTRL_STANDBY, CEC_CTRL_STANDBY);
215 			dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
216 			dw_hdmi_write(cec, 0xff, HDMI_CEC_WKUPCTRL);
217 			dw_hdmi_write(cec, ~(1 << 6), HDMI_CEC_MASK);
218 			dw_hdmi_write(cec, ~(1 << 6), HDMI_IH_MUTE_CEC_STAT0);
219 			dw_hdmi_write(cec, 0x01, HDMI_IH_MUTE);
220 		} else {
221 			cec->ops->disable(cec->hdmi);
222 		}
223 	} else {
224 		unsigned int irqs;
225 
226 		dw_hdmi_cec_log_addr(cec->adap, CEC_LOG_ADDR_INVALID);
227 		dw_hdmi_mod(cec, HDMI_CEC_CTRL, CEC_CTRL_STANDBY, 0);
228 		dw_hdmi_write(cec, 0x02, HDMI_IH_MUTE);
229 		dw_hdmi_write(cec, ~0, HDMI_IH_CEC_STAT0);
230 		dw_hdmi_write(cec, 0, HDMI_CEC_LOCK);
231 
232 		cec->ops->enable(cec->hdmi);
233 
234 		irqs = CEC_STAT_ERROR_INIT | CEC_STAT_NACK | CEC_STAT_EOM |
235 		       CEC_STAT_DONE;
236 		dw_hdmi_write(cec, irqs, HDMI_CEC_POLARITY);
237 		dw_hdmi_write(cec, ~irqs, HDMI_CEC_MASK);
238 		dw_hdmi_write(cec, ~irqs, HDMI_IH_MUTE_CEC_STAT0);
239 	}
240 	return 0;
241 }
242 
243 static const struct cec_adap_ops dw_hdmi_cec_ops = {
244 	.adap_enable = dw_hdmi_cec_enable,
245 	.adap_log_addr = dw_hdmi_cec_log_addr,
246 	.adap_transmit = dw_hdmi_cec_transmit,
247 };
248 
dw_hdmi_cec_del(void * data)249 static void dw_hdmi_cec_del(void *data)
250 {
251 	struct dw_hdmi_cec *cec = data;
252 
253 	cec_delete_adapter(cec->adap);
254 }
255 
dw_hdmi_cec_wake_irq(int irq,void * data)256 static irqreturn_t dw_hdmi_cec_wake_irq(int irq, void *data)
257 {
258 	struct cec_adapter *adap = data;
259 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
260 	u8 cec_int;
261 
262 	cec_int = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0);
263 	if (!cec_int)
264 		return IRQ_NONE;
265 
266 	dw_hdmi_write(cec, 0x02, HDMI_IH_MUTE);
267 	dw_hdmi_write(cec, cec_int, HDMI_IH_CEC_STAT0);
268 	dw_hdmi_write(cec, 0x00, HDMI_CEC_WKUPCTRL);
269 
270 	if (!cec->wake_en)
271 		return IRQ_HANDLED;
272 
273 	return IRQ_WAKE_THREAD;
274 }
275 
dw_hdmi_cec_wake_thread(int irq,void * data)276 static irqreturn_t dw_hdmi_cec_wake_thread(int irq, void *data)
277 {
278 	struct cec_adapter *adap = data;
279 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
280 
281 	mutex_lock(&cec->wake_lock);
282 
283 	if (!cec->standby_en) {
284 		mutex_unlock(&cec->wake_lock);
285 		return IRQ_HANDLED;
286 	}
287 	cec->standby_en = false;
288 
289 	dev_dbg(cec->dev, "wakeup opcode:0x%x\n", dw_hdmi_read(cec, HDMI_CEC_RX_DATA1));
290 	input_event(cec->devinput, EV_KEY, KEY_POWER, 1);
291 	input_sync(cec->devinput);
292 	input_event(cec->devinput, EV_KEY, KEY_POWER, 0);
293 	input_sync(cec->devinput);
294 	mutex_unlock(&cec->wake_lock);
295 
296 	return IRQ_HANDLED;
297 }
298 
rockchip_hdmi_cec_input_init(struct dw_hdmi_cec * cec)299 static int rockchip_hdmi_cec_input_init(struct dw_hdmi_cec *cec)
300 {
301 	int err;
302 
303 	cec->devinput = devm_input_allocate_device(cec->dev);
304 	if (!cec->devinput)
305 		return -EPERM;
306 
307 	cec->devinput->name = "hdmi_cec_key";
308 	cec->devinput->phys = "hdmi_cec_key/input0";
309 	cec->devinput->id.bustype = BUS_HOST;
310 	cec->devinput->id.vendor = 0x0001;
311 	cec->devinput->id.product = 0x0001;
312 	cec->devinput->id.version = 0x0100;
313 
314 	err = input_register_device(cec->devinput);
315 	if (err < 0) {
316 		input_free_device(cec->devinput);
317 		return err;
318 	}
319 	input_set_capability(cec->devinput, EV_KEY, KEY_POWER);
320 
321 	return 0;
322 }
323 
cec_standby(struct cec_adapter * adap,__u8 __user * parg)324 static long cec_standby(struct cec_adapter *adap, __u8 __user *parg)
325 {
326 	u8 en;
327 	int ret;
328 	struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
329 
330 	mutex_lock(&cec->wake_lock);
331 	if (copy_from_user(&en, parg, sizeof(en))) {
332 		mutex_unlock(&cec->wake_lock);
333 		return -EFAULT;
334 	}
335 
336 	cec->standby_en = !en;
337 	ret = adap->ops->adap_enable(adap, en);
338 	mutex_unlock(&cec->wake_lock);
339 
340 	return ret;
341 }
342 
cec_func_en(struct dw_hdmi_cec * cec,int __user * parg)343 static long cec_func_en(struct dw_hdmi_cec *cec, int __user *parg)
344 {
345 	int en_mask;
346 
347 	if (copy_from_user(&en_mask, parg, sizeof(en_mask)))
348 		return -EFAULT;
349 
350 	cec->wake_en = (en_mask & CEC_EN) && (en_mask & CEC_WAKE);
351 
352 	return 0;
353 }
354 
dw_hdmi_cec_ioctl(struct file * f,unsigned int cmd,unsigned long arg)355 static long dw_hdmi_cec_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
356 {
357 	struct dw_hdmi_cec *cec;
358 	struct miscdevice *misc_dev;
359 	void __user *data;
360 
361 	if (!f)
362 		return -EFAULT;
363 
364 	misc_dev = f->private_data;
365 	cec = container_of(misc_dev, struct dw_hdmi_cec, misc_dev);
366 	data = (void __user *)arg;
367 
368 	switch (cmd) {
369 	case CEC_STANDBY:
370 		return cec_standby(cec->adap, data);
371 	case CEC_FUNC_EN:
372 		return cec_func_en(cec, data);
373 	default:
374 		return -EINVAL;
375 	}
376 
377 	return -ENOTTY;
378 }
379 
dw_hdmi_cec_open(struct inode * inode,struct file * f)380 static int dw_hdmi_cec_open(struct inode *inode, struct file *f)
381 {
382 	return 0;
383 }
384 
dw_hdmi_cec_release(struct inode * inode,struct file * f)385 static int dw_hdmi_cec_release(struct inode *inode, struct file *f)
386 {
387 	return 0;
388 }
389 
390 static const struct file_operations dw_hdmi_cec_file_operations = {
391 	.compat_ioctl = dw_hdmi_cec_ioctl,
392 	.unlocked_ioctl = dw_hdmi_cec_ioctl,
393 	.open = dw_hdmi_cec_open,
394 	.release = dw_hdmi_cec_release,
395 	.owner = THIS_MODULE,
396 };
397 
dw_hdmi_cec_hpd_wake_up(struct platform_device * pdev)398 static void dw_hdmi_cec_hpd_wake_up(struct platform_device *pdev)
399 {
400 	struct dw_hdmi_cec *cec = platform_get_drvdata(pdev);
401 
402 	mutex_lock(&cec->wake_lock);
403 
404 	if (!cec->standby_en) {
405 		mutex_unlock(&cec->wake_lock);
406 		return;
407 	}
408 	cec->standby_en = false;
409 
410 	dw_hdmi_write(cec, 0x02, HDMI_IH_MUTE);
411 
412 	input_event(cec->devinput, EV_KEY, KEY_POWER, 1);
413 	input_sync(cec->devinput);
414 	input_event(cec->devinput, EV_KEY, KEY_POWER, 0);
415 	input_sync(cec->devinput);
416 	mutex_unlock(&cec->wake_lock);
417 }
418 
419 static const struct dw_hdmi_cec_wake_ops cec_ops = {
420 	.hpd_wake_up = dw_hdmi_cec_hpd_wake_up,
421 };
422 
dw_hdmi_cec_probe(struct platform_device * pdev)423 static int dw_hdmi_cec_probe(struct platform_device *pdev)
424 {
425 	struct dw_hdmi_cec_data *data = dev_get_platdata(&pdev->dev);
426 	struct dw_hdmi_cec *cec;
427 	int ret;
428 
429 	if (!data)
430 		return -ENXIO;
431 
432 	/*
433 	 * Our device is just a convenience - we want to link to the real
434 	 * hardware device here, so that userspace can see the association
435 	 * between the HDMI hardware and its associated CEC chardev.
436 	 */
437 	cec = devm_kzalloc(&pdev->dev, sizeof(*cec), GFP_KERNEL);
438 	if (!cec)
439 		return -ENOMEM;
440 
441 	cec->dev = &pdev->dev;
442 	cec->irq = data->irq;
443 	cec->wake_irq = data->wake_irq;
444 	cec->ops = data->ops;
445 	cec->hdmi = data->hdmi;
446 
447 	mutex_init(&cec->wake_lock);
448 
449 	platform_set_drvdata(pdev, cec);
450 
451 	dw_hdmi_write(cec, 0, HDMI_CEC_TX_CNT);
452 	dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);
453 	dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);
454 	dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);
455 
456 	cec->adap = cec_allocate_adapter(&dw_hdmi_cec_ops, cec, "dw_hdmi",
457 					 CEC_CAP_DEFAULTS |
458 					 CEC_CAP_CONNECTOR_INFO,
459 					 CEC_MAX_LOG_ADDRS);
460 	if (IS_ERR(cec->adap))
461 		return PTR_ERR(cec->adap);
462 
463 	dw_hdmi_set_cec_adap(cec->hdmi, cec->adap);
464 
465 	/* override the module pointer */
466 	cec->adap->owner = THIS_MODULE;
467 
468 	ret = devm_add_action(&pdev->dev, dw_hdmi_cec_del, cec);
469 	if (ret) {
470 		cec_delete_adapter(cec->adap);
471 		return ret;
472 	}
473 
474 	ret = devm_request_threaded_irq(&pdev->dev, cec->irq,
475 					dw_hdmi_cec_hardirq,
476 					dw_hdmi_cec_thread, IRQF_SHARED | IRQF_ONESHOT,
477 					"dw-hdmi-cec", cec->adap);
478 	if (ret < 0)
479 		return ret;
480 
481 	if (cec->wake_irq > 0) {
482 		ret = devm_request_threaded_irq(&pdev->dev, cec->wake_irq,
483 						dw_hdmi_cec_wake_irq,
484 						dw_hdmi_cec_wake_thread,
485 						IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
486 						"cec-wakeup", cec->adap);
487 		if (ret) {
488 			dev_err(&pdev->dev,
489 				"hdmi_cec request_irq failed (%d).\n",
490 				ret);
491 			return ret;
492 		}
493 		device_init_wakeup(&pdev->dev, 1);
494 		enable_irq_wake(cec->wake_irq);
495 	}
496 
497 	cec->notify = cec_notifier_cec_adap_register(pdev->dev.parent,
498 						     NULL, cec->adap);
499 	if (!cec->notify)
500 		return -ENOMEM;
501 
502 	ret = cec_register_adapter(cec->adap, pdev->dev.parent);
503 	if (ret < 0) {
504 		cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
505 		return ret;
506 	}
507 
508 	/*
509 	 * CEC documentation says we must not call cec_delete_adapter
510 	 * after a successful call to cec_register_adapter().
511 	 */
512 	devm_remove_action(&pdev->dev, dw_hdmi_cec_del, cec);
513 
514 	rockchip_hdmi_cec_input_init(cec);
515 
516 	cec->misc_dev.name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "rk_cec");
517 	if (!cec->misc_dev.name)
518 		return -ENOMEM;
519 	cec->misc_dev.minor = MISC_DYNAMIC_MINOR;
520 	cec->misc_dev.fops = &dw_hdmi_cec_file_operations;
521 	cec->misc_dev.mode = 0666;
522 
523 	ret = misc_register(&cec->misc_dev);
524 
525 	dw_hdmi_cec_wake_ops_register(cec->hdmi, &cec_ops);
526 
527 	return ret;
528 }
529 
dw_hdmi_cec_remove(struct platform_device * pdev)530 static int dw_hdmi_cec_remove(struct platform_device *pdev)
531 {
532 	struct dw_hdmi_cec *cec = platform_get_drvdata(pdev);
533 
534 	cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
535 	cec_unregister_adapter(cec->adap);
536 	misc_deregister(&cec->misc_dev);
537 
538 	return 0;
539 }
540 
541 static struct platform_driver dw_hdmi_cec_driver = {
542 	.probe	= dw_hdmi_cec_probe,
543 	.remove	= dw_hdmi_cec_remove,
544 	.driver = {
545 		.name = "dw-hdmi-cec",
546 	},
547 };
548 module_platform_driver(dw_hdmi_cec_driver);
549 
550 MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
551 MODULE_DESCRIPTION("Synopsys Designware HDMI CEC driver for i.MX");
552 MODULE_LICENSE("GPL");
553 MODULE_ALIAS(PLATFORM_MODULE_PREFIX "dw-hdmi-cec");
554