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 #include <drm/bridge/dw_hdmi.h>
16
17 #include <media/cec.h>
18 #include <media/cec-notifier.h>
19
20 #include "dw-hdmi-cec.h"
21
22 enum {
23 HDMI_IH_CEC_STAT0 = 0x0106,
24 HDMI_IH_MUTE_CEC_STAT0 = 0x0186,
25
26 HDMI_CEC_CTRL = 0x7d00,
27 CEC_CTRL_START = BIT(0),
28 CEC_CTRL_FRAME_TYP = 3 << 1,
29 CEC_CTRL_RETRY = 0 << 1,
30 CEC_CTRL_NORMAL = 1 << 1,
31 CEC_CTRL_IMMED = 2 << 1,
32
33 HDMI_CEC_STAT = 0x7d01,
34 CEC_STAT_DONE = BIT(0),
35 CEC_STAT_EOM = BIT(1),
36 CEC_STAT_NACK = BIT(2),
37 CEC_STAT_ARBLOST = BIT(3),
38 CEC_STAT_ERROR_INIT = BIT(4),
39 CEC_STAT_ERROR_FOLL = BIT(5),
40 CEC_STAT_WAKEUP = BIT(6),
41
42 HDMI_CEC_MASK = 0x7d02,
43 HDMI_CEC_POLARITY = 0x7d03,
44 HDMI_CEC_INT = 0x7d04,
45 HDMI_CEC_ADDR_L = 0x7d05,
46 HDMI_CEC_ADDR_H = 0x7d06,
47 HDMI_CEC_TX_CNT = 0x7d07,
48 HDMI_CEC_RX_CNT = 0x7d08,
49 HDMI_CEC_TX_DATA0 = 0x7d10,
50 HDMI_CEC_RX_DATA0 = 0x7d20,
51 HDMI_CEC_LOCK = 0x7d30,
52 HDMI_CEC_WKUPCTRL = 0x7d31,
53 };
54
55 struct dw_hdmi_cec {
56 struct dw_hdmi *hdmi;
57 const struct dw_hdmi_cec_ops *ops;
58 u32 addresses;
59 struct cec_adapter *adap;
60 struct cec_msg rx_msg;
61 unsigned int tx_status;
62 bool tx_done;
63 bool rx_done;
64 struct cec_notifier *notify;
65 int irq;
66 };
67
dw_hdmi_write(struct dw_hdmi_cec * cec,u8 val,int offset)68 static void dw_hdmi_write(struct dw_hdmi_cec *cec, u8 val, int offset)
69 {
70 cec->ops->write(cec->hdmi, val, offset);
71 }
72
dw_hdmi_read(struct dw_hdmi_cec * cec,int offset)73 static u8 dw_hdmi_read(struct dw_hdmi_cec *cec, int offset)
74 {
75 return cec->ops->read(cec->hdmi, offset);
76 }
77
dw_hdmi_cec_log_addr(struct cec_adapter * adap,u8 logical_addr)78 static int dw_hdmi_cec_log_addr(struct cec_adapter *adap, u8 logical_addr)
79 {
80 struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
81
82 if (logical_addr == CEC_LOG_ADDR_INVALID) {
83 cec->addresses = 0;
84 } else {
85 cec->addresses |= BIT(logical_addr) | BIT(0x0f);
86 }
87
88 dw_hdmi_write(cec, cec->addresses & 0xff, HDMI_CEC_ADDR_L);
89 dw_hdmi_write(cec, cec->addresses >> 0x8, HDMI_CEC_ADDR_H);
90
91 return 0;
92 }
93
dw_hdmi_cec_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)94 static int dw_hdmi_cec_transmit(struct cec_adapter *adap, u8 attempts, u32 signal_free_time, struct cec_msg *msg)
95 {
96 struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
97 unsigned int i, ctrl;
98
99 switch (signal_free_time) {
100 case CEC_SIGNAL_FREE_TIME_RETRY:
101 ctrl = CEC_CTRL_RETRY;
102 break;
103 case CEC_SIGNAL_FREE_TIME_NEW_INITIATOR:
104 default:
105 ctrl = CEC_CTRL_NORMAL;
106 break;
107 case CEC_SIGNAL_FREE_TIME_NEXT_XFER:
108 ctrl = CEC_CTRL_IMMED;
109 break;
110 }
111
112 for (i = 0; i < msg->len; i++) {
113 dw_hdmi_write(cec, msg->msg[i], HDMI_CEC_TX_DATA0 + i);
114 }
115
116 dw_hdmi_write(cec, msg->len, HDMI_CEC_TX_CNT);
117 dw_hdmi_write(cec, ctrl | CEC_CTRL_START, HDMI_CEC_CTRL);
118
119 return 0;
120 }
121
dw_hdmi_cec_hardirq(int irq,void * data)122 static irqreturn_t dw_hdmi_cec_hardirq(int irq, void *data)
123 {
124 struct cec_adapter *adap = data;
125 struct dw_hdmi_cec *cec = cec_get_drvdata(adap);
126 unsigned int stat = dw_hdmi_read(cec, HDMI_IH_CEC_STAT0);
127 irqreturn_t ret = IRQ_HANDLED;
128
129 if (stat == 0) {
130 return IRQ_NONE;
131 }
132
133 dw_hdmi_write(cec, stat, HDMI_IH_CEC_STAT0);
134
135 if (stat & CEC_STAT_ERROR_INIT) {
136 cec->tx_status = CEC_TX_STATUS_ERROR;
137 cec->tx_done = true;
138 ret = IRQ_WAKE_THREAD;
139 } else if (stat & CEC_STAT_DONE) {
140 cec->tx_status = CEC_TX_STATUS_OK;
141 cec->tx_done = true;
142 ret = IRQ_WAKE_THREAD;
143 } else if (stat & CEC_STAT_NACK) {
144 cec->tx_status = CEC_TX_STATUS_NACK;
145 cec->tx_done = true;
146 ret = IRQ_WAKE_THREAD;
147 }
148
149 if (stat & CEC_STAT_EOM) {
150 unsigned int len, i;
151
152 len = dw_hdmi_read(cec, HDMI_CEC_RX_CNT);
153 if (len > sizeof(cec->rx_msg.msg)) {
154 len = sizeof(cec->rx_msg.msg);
155 }
156
157 for (i = 0; i < len; i++) {
158 cec->rx_msg.msg[i] = dw_hdmi_read(cec, HDMI_CEC_RX_DATA0 + i);
159 }
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
dw_hdmi_cec_thread(int irq,void * data)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
dw_hdmi_cec_enable(struct cec_adapter * adap,bool enable)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 | CEC_STAT_DONE;
212 dw_hdmi_write(cec, irqs, HDMI_CEC_POLARITY);
213 dw_hdmi_write(cec, ~irqs, HDMI_CEC_MASK);
214 dw_hdmi_write(cec, ~irqs, HDMI_IH_MUTE_CEC_STAT0);
215 }
216 return 0;
217 }
218
219 static const struct cec_adap_ops dw_hdmi_cec_ops = {
220 .adap_enable = dw_hdmi_cec_enable,
221 .adap_log_addr = dw_hdmi_cec_log_addr,
222 .adap_transmit = dw_hdmi_cec_transmit,
223 };
224
dw_hdmi_cec_del(void * data)225 static void dw_hdmi_cec_del(void *data)
226 {
227 struct dw_hdmi_cec *cec = data;
228
229 cec_delete_adapter(cec->adap);
230 }
231
dw_hdmi_cec_probe(struct platform_device * pdev)232 static int dw_hdmi_cec_probe(struct platform_device *pdev)
233 {
234 struct dw_hdmi_cec_data *data = dev_get_platdata(&pdev->dev);
235 struct dw_hdmi_cec *cec;
236 int ret;
237
238 if (!data) {
239 return -ENXIO;
240 }
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
252 cec->irq = data->irq;
253 cec->ops = data->ops;
254 cec->hdmi = data->hdmi;
255
256 platform_set_drvdata(pdev, cec);
257
258 dw_hdmi_write(cec, 0, HDMI_CEC_TX_CNT);
259 dw_hdmi_write(cec, ~0, HDMI_CEC_MASK);
260 dw_hdmi_write(cec, ~0, HDMI_IH_MUTE_CEC_STAT0);
261 dw_hdmi_write(cec, 0, HDMI_CEC_POLARITY);
262
263 cec->adap = cec_allocate_adapter(&dw_hdmi_cec_ops, cec, "dw_hdmi", CEC_CAP_DEFAULTS | CEC_CAP_CONNECTOR_INFO,
264 CEC_MAX_LOG_ADDRS);
265 if (IS_ERR(cec->adap)) {
266 return PTR_ERR(cec->adap);
267 }
268
269 dw_hdmi_set_cec_adap(cec->hdmi, cec->adap);
270
271 /* override the module pointer */
272 cec->adap->owner = THIS_MODULE;
273
274 ret = devm_add_action(&pdev->dev, dw_hdmi_cec_del, cec);
275 if (ret) {
276 cec_delete_adapter(cec->adap);
277 return ret;
278 }
279
280 ret = devm_request_threaded_irq(&pdev->dev, cec->irq, dw_hdmi_cec_hardirq, dw_hdmi_cec_thread, IRQF_SHARED,
281 "dw-hdmi-cec", cec->adap);
282 if (ret < 0) {
283 return ret;
284 }
285
286 cec->notify = cec_notifier_cec_adap_register(pdev->dev.parent, NULL, cec->adap);
287 if (!cec->notify) {
288 return -ENOMEM;
289 }
290
291 ret = cec_register_adapter(cec->adap, pdev->dev.parent);
292 if (ret < 0) {
293 cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
294 return ret;
295 }
296
297 /*
298 * CEC documentation says we must not call cec_delete_adapter
299 * after a successful call to cec_register_adapter().
300 */
301 devm_remove_action(&pdev->dev, dw_hdmi_cec_del, cec);
302
303 return 0;
304 }
305
dw_hdmi_cec_remove(struct platform_device * pdev)306 static int dw_hdmi_cec_remove(struct platform_device *pdev)
307 {
308 struct dw_hdmi_cec *cec = platform_get_drvdata(pdev);
309
310 cec_notifier_cec_adap_unregister(cec->notify, cec->adap);
311 cec_unregister_adapter(cec->adap);
312
313 return 0;
314 }
315
316 static struct platform_driver dw_hdmi_cec_driver = {
317 .probe = dw_hdmi_cec_probe,
318 .remove = dw_hdmi_cec_remove,
319 .driver =
320 {
321 .name = "dw-hdmi-cec",
322 },
323 };
324 module_platform_driver(dw_hdmi_cec_driver);
325
326 MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
327 MODULE_DESCRIPTION("Synopsys Designware HDMI CEC driver for i.MX");
328 MODULE_LICENSE("GPL");
329 MODULE_ALIAS(PLATFORM_MODULE_PREFIX "dw-hdmi-cec");
330