aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu
diff options
context:
space:
mode:
authorDave Airlie <airlied@redhat.com>2018-05-21 20:20:45 -0400
committerDave Airlie <airlied@redhat.com>2018-05-21 20:20:45 -0400
commit1dd6eb88af7a511b090fa847ed826bf3accf3bce (patch)
tree34c8b7c0da62e78d301946ba325b54f2e89a898f /drivers/gpu
parent3b064e6f7be75efbb5d8ec5991cd64c8ed09e0dd (diff)
parentba52762fb1430b2a2ea8127c1a292c15f13b8dac (diff)
Merge branch 'drm-tda998x-devel' of git://git.armlinux.org.uk/~rmk/linux-arm into drm-next
Please incorporate support for TDA998x I2C driver CEC Signed-off-by: Dave Airlie <airlied@redhat.com> Link: https://patchwork.freedesktop.org/patch/msgid/20180424095456.GA32460@rmk-PC.armlinux.org.uk
Diffstat (limited to 'drivers/gpu')
-rw-r--r--drivers/gpu/drm/i2c/Kconfig6
-rw-r--r--drivers/gpu/drm/i2c/Makefile1
-rw-r--r--drivers/gpu/drm/i2c/tda9950.c509
-rw-r--r--drivers/gpu/drm/i2c/tda998x_drv.c242
4 files changed, 731 insertions, 27 deletions
diff --git a/drivers/gpu/drm/i2c/Kconfig b/drivers/gpu/drm/i2c/Kconfig
index a6c92beb410a..65d3acb61c03 100644
--- a/drivers/gpu/drm/i2c/Kconfig
+++ b/drivers/gpu/drm/i2c/Kconfig
@@ -22,8 +22,14 @@ config DRM_I2C_SIL164
22config DRM_I2C_NXP_TDA998X 22config DRM_I2C_NXP_TDA998X
23 tristate "NXP Semiconductors TDA998X HDMI encoder" 23 tristate "NXP Semiconductors TDA998X HDMI encoder"
24 default m if DRM_TILCDC 24 default m if DRM_TILCDC
25 select CEC_CORE if CEC_NOTIFIER
25 select SND_SOC_HDMI_CODEC if SND_SOC 26 select SND_SOC_HDMI_CODEC if SND_SOC
26 help 27 help
27 Support for NXP Semiconductors TDA998X HDMI encoders. 28 Support for NXP Semiconductors TDA998X HDMI encoders.
28 29
30config DRM_I2C_NXP_TDA9950
31 tristate "NXP Semiconductors TDA9950/TDA998X HDMI CEC"
32 select CEC_NOTIFIER
33 select CEC_CORE
34
29endmenu 35endmenu
diff --git a/drivers/gpu/drm/i2c/Makefile b/drivers/gpu/drm/i2c/Makefile
index b20100c18ffb..a962f6f08568 100644
--- a/drivers/gpu/drm/i2c/Makefile
+++ b/drivers/gpu/drm/i2c/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_DRM_I2C_SIL164) += sil164.o
7 7
8tda998x-y := tda998x_drv.o 8tda998x-y := tda998x_drv.o
9obj-$(CONFIG_DRM_I2C_NXP_TDA998X) += tda998x.o 9obj-$(CONFIG_DRM_I2C_NXP_TDA998X) += tda998x.o
10obj-$(CONFIG_DRM_I2C_NXP_TDA9950) += tda9950.o
diff --git a/drivers/gpu/drm/i2c/tda9950.c b/drivers/gpu/drm/i2c/tda9950.c
new file mode 100644
index 000000000000..3f7396caad48
--- /dev/null
+++ b/drivers/gpu/drm/i2c/tda9950.c
@@ -0,0 +1,509 @@
1/*
2 * TDA9950 Consumer Electronics Control driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * The NXP TDA9950 implements the HDMI Consumer Electronics Control
9 * interface. The host interface is similar to a mailbox: the data
10 * registers starting at REG_CDR0 are written to send a command to the
11 * internal CPU, and replies are read from these registers.
12 *
13 * As the data registers represent a mailbox, they must be accessed
14 * as a single I2C transaction. See the TDA9950 data sheet for details.
15 */
16#include <linux/delay.h>
17#include <linux/i2c.h>
18#include <linux/interrupt.h>
19#include <linux/module.h>
20#include <linux/platform_data/tda9950.h>
21#include <linux/slab.h>
22#include <drm/drm_edid.h>
23#include <media/cec.h>
24#include <media/cec-notifier.h>
25
26enum {
27 REG_CSR = 0x00,
28 CSR_BUSY = BIT(7),
29 CSR_INT = BIT(6),
30 CSR_ERR = BIT(5),
31
32 REG_CER = 0x01,
33
34 REG_CVR = 0x02,
35
36 REG_CCR = 0x03,
37 CCR_RESET = BIT(7),
38 CCR_ON = BIT(6),
39
40 REG_ACKH = 0x04,
41 REG_ACKL = 0x05,
42
43 REG_CCONR = 0x06,
44 CCONR_ENABLE_ERROR = BIT(4),
45 CCONR_RETRY_MASK = 7,
46
47 REG_CDR0 = 0x07,
48
49 CDR1_REQ = 0x00,
50 CDR1_CNF = 0x01,
51 CDR1_IND = 0x81,
52 CDR1_ERR = 0x82,
53 CDR1_IER = 0x83,
54
55 CDR2_CNF_SUCCESS = 0x00,
56 CDR2_CNF_OFF_STATE = 0x80,
57 CDR2_CNF_BAD_REQ = 0x81,
58 CDR2_CNF_CEC_ACCESS = 0x82,
59 CDR2_CNF_ARB_ERROR = 0x83,
60 CDR2_CNF_BAD_TIMING = 0x84,
61 CDR2_CNF_NACK_ADDR = 0x85,
62 CDR2_CNF_NACK_DATA = 0x86,
63};
64
65struct tda9950_priv {
66 struct i2c_client *client;
67 struct device *hdmi;
68 struct cec_adapter *adap;
69 struct tda9950_glue *glue;
70 u16 addresses;
71 struct cec_msg rx_msg;
72 struct cec_notifier *notify;
73 bool open;
74};
75
76static int tda9950_write_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
77{
78 struct i2c_msg msg;
79 u8 buf[cnt + 1];
80 int ret;
81
82 buf[0] = addr;
83 memcpy(buf + 1, p, cnt);
84
85 msg.addr = client->addr;
86 msg.flags = 0;
87 msg.len = cnt + 1;
88 msg.buf = buf;
89
90 dev_dbg(&client->dev, "wr 0x%02x: %*ph\n", addr, cnt, p);
91
92 ret = i2c_transfer(client->adapter, &msg, 1);
93 if (ret < 0)
94 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
95 return ret < 0 ? ret : 0;
96}
97
98static void tda9950_write(struct i2c_client *client, u8 addr, u8 val)
99{
100 tda9950_write_range(client, addr, &val, 1);
101}
102
103static int tda9950_read_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
104{
105 struct i2c_msg msg[2];
106 int ret;
107
108 msg[0].addr = client->addr;
109 msg[0].flags = 0;
110 msg[0].len = 1;
111 msg[0].buf = &addr;
112 msg[1].addr = client->addr;
113 msg[1].flags = I2C_M_RD;
114 msg[1].len = cnt;
115 msg[1].buf = p;
116
117 ret = i2c_transfer(client->adapter, msg, 2);
118 if (ret < 0)
119 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
120
121 dev_dbg(&client->dev, "rd 0x%02x: %*ph\n", addr, cnt, p);
122
123 return ret;
124}
125
126static u8 tda9950_read(struct i2c_client *client, u8 addr)
127{
128 int ret;
129 u8 val;
130
131 ret = tda9950_read_range(client, addr, &val, 1);
132 if (ret < 0)
133 val = 0;
134
135 return val;
136}
137
138static irqreturn_t tda9950_irq(int irq, void *data)
139{
140 struct tda9950_priv *priv = data;
141 unsigned int tx_status;
142 u8 csr, cconr, buf[19];
143 u8 arb_lost_cnt, nack_cnt, err_cnt;
144
145 if (!priv->open)
146 return IRQ_NONE;
147
148 csr = tda9950_read(priv->client, REG_CSR);
149 if (!(csr & CSR_INT))
150 return IRQ_NONE;
151
152 cconr = tda9950_read(priv->client, REG_CCONR) & CCONR_RETRY_MASK;
153
154 tda9950_read_range(priv->client, REG_CDR0, buf, sizeof(buf));
155
156 /*
157 * This should never happen: the data sheet says that there will
158 * always be a valid message if the interrupt line is asserted.
159 */
160 if (buf[0] == 0) {
161 dev_warn(&priv->client->dev, "interrupt pending, but no message?\n");
162 return IRQ_NONE;
163 }
164
165 switch (buf[1]) {
166 case CDR1_CNF: /* transmit result */
167 arb_lost_cnt = nack_cnt = err_cnt = 0;
168 switch (buf[2]) {
169 case CDR2_CNF_SUCCESS:
170 tx_status = CEC_TX_STATUS_OK;
171 break;
172
173 case CDR2_CNF_ARB_ERROR:
174 tx_status = CEC_TX_STATUS_ARB_LOST;
175 arb_lost_cnt = cconr;
176 break;
177
178 case CDR2_CNF_NACK_ADDR:
179 tx_status = CEC_TX_STATUS_NACK;
180 nack_cnt = cconr;
181 break;
182
183 default: /* some other error, refer to TDA9950 docs */
184 dev_err(&priv->client->dev, "CNF reply error 0x%02x\n",
185 buf[2]);
186 tx_status = CEC_TX_STATUS_ERROR;
187 err_cnt = cconr;
188 break;
189 }
190 /* TDA9950 executes all retries for us */
191 tx_status |= CEC_TX_STATUS_MAX_RETRIES;
192 cec_transmit_done(priv->adap, tx_status, arb_lost_cnt,
193 nack_cnt, 0, err_cnt);
194 break;
195
196 case CDR1_IND:
197 priv->rx_msg.len = buf[0] - 2;
198 if (priv->rx_msg.len > CEC_MAX_MSG_SIZE)
199 priv->rx_msg.len = CEC_MAX_MSG_SIZE;
200
201 memcpy(priv->rx_msg.msg, buf + 2, priv->rx_msg.len);
202 cec_received_msg(priv->adap, &priv->rx_msg);
203 break;
204
205 default: /* unknown */
206 dev_err(&priv->client->dev, "unknown service id 0x%02x\n",
207 buf[1]);
208 break;
209 }
210
211 return IRQ_HANDLED;
212}
213
214static int tda9950_cec_transmit(struct cec_adapter *adap, u8 attempts,
215 u32 signal_free_time, struct cec_msg *msg)
216{
217 struct tda9950_priv *priv = adap->priv;
218 u8 buf[CEC_MAX_MSG_SIZE + 2];
219
220 buf[0] = 2 + msg->len;
221 buf[1] = CDR1_REQ;
222 memcpy(buf + 2, msg->msg, msg->len);
223
224 if (attempts > 5)
225 attempts = 5;
226
227 tda9950_write(priv->client, REG_CCONR, attempts);
228
229 return tda9950_write_range(priv->client, REG_CDR0, buf, 2 + msg->len);
230}
231
232static int tda9950_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
233{
234 struct tda9950_priv *priv = adap->priv;
235 u16 addresses;
236 u8 buf[2];
237
238 if (addr == CEC_LOG_ADDR_INVALID)
239 addresses = priv->addresses = 0;
240 else
241 addresses = priv->addresses |= BIT(addr);
242
243 /* TDA9950 doesn't want address 15 set */
244 addresses &= 0x7fff;
245 buf[0] = addresses >> 8;
246 buf[1] = addresses;
247
248 return tda9950_write_range(priv->client, REG_ACKH, buf, 2);
249}
250
251/*
252 * When operating as part of the TDA998x, we need additional handling
253 * to initialise and shut down the TDA9950 part of the device. These
254 * two hooks are provided to allow the TDA998x code to perform those
255 * activities.
256 */
257static int tda9950_glue_open(struct tda9950_priv *priv)
258{
259 int ret = 0;
260
261 if (priv->glue && priv->glue->open)
262 ret = priv->glue->open(priv->glue->data);
263
264 priv->open = true;
265
266 return ret;
267}
268
269static void tda9950_glue_release(struct tda9950_priv *priv)
270{
271 priv->open = false;
272
273 if (priv->glue && priv->glue->release)
274 priv->glue->release(priv->glue->data);
275}
276
277static int tda9950_open(struct tda9950_priv *priv)
278{
279 struct i2c_client *client = priv->client;
280 int ret;
281
282 ret = tda9950_glue_open(priv);
283 if (ret)
284 return ret;
285
286 /* Reset the TDA9950, and wait 250ms for it to recover */
287 tda9950_write(client, REG_CCR, CCR_RESET);
288 msleep(250);
289
290 tda9950_cec_adap_log_addr(priv->adap, CEC_LOG_ADDR_INVALID);
291
292 /* Start the command processor */
293 tda9950_write(client, REG_CCR, CCR_ON);
294
295 return 0;
296}
297
298static void tda9950_release(struct tda9950_priv *priv)
299{
300 struct i2c_client *client = priv->client;
301 int timeout = 50;
302 u8 csr;
303
304 /* Stop the command processor */
305 tda9950_write(client, REG_CCR, 0);
306
307 /* Wait up to .5s for it to signal non-busy */
308 do {
309 csr = tda9950_read(client, REG_CSR);
310 if (!(csr & CSR_BUSY) || --timeout)
311 break;
312 msleep(10);
313 } while (1);
314
315 /* Warn the user that their IRQ may die if it's shared. */
316 if (csr & CSR_BUSY)
317 dev_warn(&client->dev, "command processor failed to stop, irq%d may die (csr=0x%02x)\n",
318 client->irq, csr);
319
320 tda9950_glue_release(priv);
321}
322
323static int tda9950_cec_adap_enable(struct cec_adapter *adap, bool enable)
324{
325 struct tda9950_priv *priv = adap->priv;
326
327 if (!enable) {
328 tda9950_release(priv);
329 return 0;
330 } else {
331 return tda9950_open(priv);
332 }
333}
334
335static const struct cec_adap_ops tda9950_cec_ops = {
336 .adap_enable = tda9950_cec_adap_enable,
337 .adap_log_addr = tda9950_cec_adap_log_addr,
338 .adap_transmit = tda9950_cec_transmit,
339};
340
341/*
342 * When operating as part of the TDA998x, we need to claim additional
343 * resources. These two hooks permit the management of those resources.
344 */
345static void tda9950_devm_glue_exit(void *data)
346{
347 struct tda9950_glue *glue = data;
348
349 if (glue && glue->exit)
350 glue->exit(glue->data);
351}
352
353static int tda9950_devm_glue_init(struct device *dev, struct tda9950_glue *glue)
354{
355 int ret;
356
357 if (glue && glue->init) {
358 ret = glue->init(glue->data);
359 if (ret)
360 return ret;
361 }
362
363 ret = devm_add_action(dev, tda9950_devm_glue_exit, glue);
364 if (ret)
365 tda9950_devm_glue_exit(glue);
366
367 return ret;
368}
369
370static void tda9950_cec_del(void *data)
371{
372 struct tda9950_priv *priv = data;
373
374 cec_delete_adapter(priv->adap);
375}
376
377static int tda9950_probe(struct i2c_client *client,
378 const struct i2c_device_id *id)
379{
380 struct tda9950_glue *glue = client->dev.platform_data;
381 struct device *dev = &client->dev;
382 struct tda9950_priv *priv;
383 unsigned long irqflags;
384 int ret;
385 u8 cvr;
386
387 /*
388 * We must have I2C functionality: our multi-byte accesses
389 * must be performed as a single contiguous transaction.
390 */
391 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
392 dev_err(&client->dev,
393 "adapter does not support I2C functionality\n");
394 return -ENXIO;
395 }
396
397 /* We must have an interrupt to be functional. */
398 if (client->irq <= 0) {
399 dev_err(&client->dev, "driver requires an interrupt\n");
400 return -ENXIO;
401 }
402
403 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
404 if (!priv)
405 return -ENOMEM;
406
407 priv->client = client;
408 priv->glue = glue;
409
410 i2c_set_clientdata(client, priv);
411
412 /*
413 * If we're part of a TDA998x, we want the class devices to be
414 * associated with the HDMI Tx so we have a tight relationship
415 * between the HDMI interface and the CEC interface.
416 */
417 priv->hdmi = dev;
418 if (glue && glue->parent)
419 priv->hdmi = glue->parent;
420
421 priv->adap = cec_allocate_adapter(&tda9950_cec_ops, priv, "tda9950",
422 CEC_CAP_DEFAULTS,
423 CEC_MAX_LOG_ADDRS);
424 if (IS_ERR(priv->adap))
425 return PTR_ERR(priv->adap);
426
427 ret = devm_add_action(dev, tda9950_cec_del, priv);
428 if (ret) {
429 cec_delete_adapter(priv->adap);
430 return ret;
431 }
432
433 ret = tda9950_devm_glue_init(dev, glue);
434 if (ret)
435 return ret;
436
437 ret = tda9950_glue_open(priv);
438 if (ret)
439 return ret;
440
441 cvr = tda9950_read(client, REG_CVR);
442
443 dev_info(&client->dev,
444 "TDA9950 CEC interface, hardware version %u.%u\n",
445 cvr >> 4, cvr & 15);
446
447 tda9950_glue_release(priv);
448
449 irqflags = IRQF_TRIGGER_FALLING;
450 if (glue)
451 irqflags = glue->irq_flags;
452
453 ret = devm_request_threaded_irq(dev, client->irq, NULL, tda9950_irq,
454 irqflags | IRQF_SHARED | IRQF_ONESHOT,
455 dev_name(&client->dev), priv);
456 if (ret < 0)
457 return ret;
458
459 priv->notify = cec_notifier_get(priv->hdmi);
460 if (!priv->notify)
461 return -ENOMEM;
462
463 ret = cec_register_adapter(priv->adap, priv->hdmi);
464 if (ret < 0) {
465 cec_notifier_put(priv->notify);
466 return ret;
467 }
468
469 /*
470 * CEC documentation says we must not call cec_delete_adapter
471 * after a successful call to cec_register_adapter().
472 */
473 devm_remove_action(dev, tda9950_cec_del, priv);
474
475 cec_register_cec_notifier(priv->adap, priv->notify);
476
477 return 0;
478}
479
480static int tda9950_remove(struct i2c_client *client)
481{
482 struct tda9950_priv *priv = i2c_get_clientdata(client);
483
484 cec_unregister_adapter(priv->adap);
485 cec_notifier_put(priv->notify);
486
487 return 0;
488}
489
490static struct i2c_device_id tda9950_ids[] = {
491 { "tda9950", 0 },
492 { },
493};
494MODULE_DEVICE_TABLE(i2c, tda9950_ids);
495
496static struct i2c_driver tda9950_driver = {
497 .probe = tda9950_probe,
498 .remove = tda9950_remove,
499 .driver = {
500 .name = "tda9950",
501 },
502 .id_table = tda9950_ids,
503};
504
505module_i2c_driver(tda9950_driver);
506
507MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
508MODULE_DESCRIPTION("TDA9950/TDA998x Consumer Electronics Control Driver");
509MODULE_LICENSE("GPL v2");
diff --git a/drivers/gpu/drm/i2c/tda998x_drv.c b/drivers/gpu/drm/i2c/tda998x_drv.c
index 421c8a72369e..6ebd8842dbcc 100644
--- a/drivers/gpu/drm/i2c/tda998x_drv.c
+++ b/drivers/gpu/drm/i2c/tda998x_drv.c
@@ -16,8 +16,10 @@
16 */ 16 */
17 17
18#include <linux/component.h> 18#include <linux/component.h>
19#include <linux/gpio/consumer.h>
19#include <linux/hdmi.h> 20#include <linux/hdmi.h>
20#include <linux/module.h> 21#include <linux/module.h>
22#include <linux/platform_data/tda9950.h>
21#include <linux/irq.h> 23#include <linux/irq.h>
22#include <sound/asoundef.h> 24#include <sound/asoundef.h>
23#include <sound/hdmi-codec.h> 25#include <sound/hdmi-codec.h>
@@ -29,6 +31,8 @@
29#include <drm/drm_of.h> 31#include <drm/drm_of.h>
30#include <drm/i2c/tda998x.h> 32#include <drm/i2c/tda998x.h>
31 33
34#include <media/cec-notifier.h>
35
32#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__) 36#define DBG(fmt, ...) DRM_DEBUG(fmt"\n", ##__VA_ARGS__)
33 37
34struct tda998x_audio_port { 38struct tda998x_audio_port {
@@ -55,6 +59,7 @@ struct tda998x_priv {
55 struct platform_device *audio_pdev; 59 struct platform_device *audio_pdev;
56 struct mutex audio_mutex; 60 struct mutex audio_mutex;
57 61
62 struct mutex edid_mutex;
58 wait_queue_head_t wq_edid; 63 wait_queue_head_t wq_edid;
59 volatile int wq_edid_wait; 64 volatile int wq_edid_wait;
60 65
@@ -67,6 +72,9 @@ struct tda998x_priv {
67 struct drm_connector connector; 72 struct drm_connector connector;
68 73
69 struct tda998x_audio_port audio_port[2]; 74 struct tda998x_audio_port audio_port[2];
75 struct tda9950_glue cec_glue;
76 struct gpio_desc *calib;
77 struct cec_notifier *cec_notify;
70}; 78};
71 79
72#define conn_to_tda998x_priv(x) \ 80#define conn_to_tda998x_priv(x) \
@@ -345,6 +353,12 @@ struct tda998x_priv {
345#define REG_CEC_INTSTATUS 0xee /* read */ 353#define REG_CEC_INTSTATUS 0xee /* read */
346# define CEC_INTSTATUS_CEC (1 << 0) 354# define CEC_INTSTATUS_CEC (1 << 0)
347# define CEC_INTSTATUS_HDMI (1 << 1) 355# define CEC_INTSTATUS_HDMI (1 << 1)
356#define REG_CEC_CAL_XOSC_CTRL1 0xf2
357# define CEC_CAL_XOSC_CTRL1_ENA_CAL BIT(0)
358#define REG_CEC_DES_FREQ2 0xf5
359# define CEC_DES_FREQ2_DIS_AUTOCAL BIT(7)
360#define REG_CEC_CLK 0xf6
361# define CEC_CLK_FRO 0x11
348#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */ 362#define REG_CEC_FRO_IM_CLK_CTRL 0xfb /* read/write */
349# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7) 363# define CEC_FRO_IM_CLK_CTRL_GHOST_DIS (1 << 7)
350# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6) 364# define CEC_FRO_IM_CLK_CTRL_ENA_OTP (1 << 6)
@@ -359,6 +373,7 @@ struct tda998x_priv {
359# define CEC_RXSHPDLEV_HPD (1 << 1) 373# define CEC_RXSHPDLEV_HPD (1 << 1)
360 374
361#define REG_CEC_ENAMODS 0xff /* read/write */ 375#define REG_CEC_ENAMODS 0xff /* read/write */
376# define CEC_ENAMODS_EN_CEC_CLK (1 << 7)
362# define CEC_ENAMODS_DIS_FRO (1 << 6) 377# define CEC_ENAMODS_DIS_FRO (1 << 6)
363# define CEC_ENAMODS_DIS_CCLK (1 << 5) 378# define CEC_ENAMODS_DIS_CCLK (1 << 5)
364# define CEC_ENAMODS_EN_RXSENS (1 << 2) 379# define CEC_ENAMODS_EN_RXSENS (1 << 2)
@@ -417,6 +432,114 @@ cec_read(struct tda998x_priv *priv, u8 addr)
417 return val; 432 return val;
418} 433}
419 434
435static void cec_enamods(struct tda998x_priv *priv, u8 mods, bool enable)
436{
437 int val = cec_read(priv, REG_CEC_ENAMODS);
438
439 if (val < 0)
440 return;
441
442 if (enable)
443 val |= mods;
444 else
445 val &= ~mods;
446
447 cec_write(priv, REG_CEC_ENAMODS, val);
448}
449
450static void tda998x_cec_set_calibration(struct tda998x_priv *priv, bool enable)
451{
452 if (enable) {
453 u8 val;
454
455 cec_write(priv, 0xf3, 0xc0);
456 cec_write(priv, 0xf4, 0xd4);
457
458 /* Enable automatic calibration mode */
459 val = cec_read(priv, REG_CEC_DES_FREQ2);
460 val &= ~CEC_DES_FREQ2_DIS_AUTOCAL;
461 cec_write(priv, REG_CEC_DES_FREQ2, val);
462
463 /* Enable free running oscillator */
464 cec_write(priv, REG_CEC_CLK, CEC_CLK_FRO);
465 cec_enamods(priv, CEC_ENAMODS_DIS_FRO, false);
466
467 cec_write(priv, REG_CEC_CAL_XOSC_CTRL1,
468 CEC_CAL_XOSC_CTRL1_ENA_CAL);
469 } else {
470 cec_write(priv, REG_CEC_CAL_XOSC_CTRL1, 0);
471 }
472}
473
474/*
475 * Calibration for the internal oscillator: we need to set calibration mode,
476 * and then pulse the IRQ line low for a 10ms ± 1% period.
477 */
478static void tda998x_cec_calibration(struct tda998x_priv *priv)
479{
480 struct gpio_desc *calib = priv->calib;
481
482 mutex_lock(&priv->edid_mutex);
483 if (priv->hdmi->irq > 0)
484 disable_irq(priv->hdmi->irq);
485 gpiod_direction_output(calib, 1);
486 tda998x_cec_set_calibration(priv, true);
487
488 local_irq_disable();
489 gpiod_set_value(calib, 0);
490 mdelay(10);
491 gpiod_set_value(calib, 1);
492 local_irq_enable();
493
494 tda998x_cec_set_calibration(priv, false);
495 gpiod_direction_input(calib);
496 if (priv->hdmi->irq > 0)
497 enable_irq(priv->hdmi->irq);
498 mutex_unlock(&priv->edid_mutex);
499}
500
501static int tda998x_cec_hook_init(void *data)
502{
503 struct tda998x_priv *priv = data;
504 struct gpio_desc *calib;
505
506 calib = gpiod_get(&priv->hdmi->dev, "nxp,calib", GPIOD_ASIS);
507 if (IS_ERR(calib)) {
508 dev_warn(&priv->hdmi->dev, "failed to get calibration gpio: %ld\n",
509 PTR_ERR(calib));
510 return PTR_ERR(calib);
511 }
512
513 priv->calib = calib;
514
515 return 0;
516}
517
518static void tda998x_cec_hook_exit(void *data)
519{
520 struct tda998x_priv *priv = data;
521
522 gpiod_put(priv->calib);
523 priv->calib = NULL;
524}
525
526static int tda998x_cec_hook_open(void *data)
527{
528 struct tda998x_priv *priv = data;
529
530 cec_enamods(priv, CEC_ENAMODS_EN_CEC_CLK | CEC_ENAMODS_EN_CEC, true);
531 tda998x_cec_calibration(priv);
532
533 return 0;
534}
535
536static void tda998x_cec_hook_release(void *data)
537{
538 struct tda998x_priv *priv = data;
539
540 cec_enamods(priv, CEC_ENAMODS_EN_CEC_CLK | CEC_ENAMODS_EN_CEC, false);
541}
542
420static int 543static int
421set_page(struct tda998x_priv *priv, u16 reg) 544set_page(struct tda998x_priv *priv, u16 reg)
422{ 545{
@@ -657,10 +780,13 @@ static irqreturn_t tda998x_irq_thread(int irq, void *data)
657 sta, cec, lvl, flag0, flag1, flag2); 780 sta, cec, lvl, flag0, flag1, flag2);
658 781
659 if (cec & CEC_RXSHPDINT_HPD) { 782 if (cec & CEC_RXSHPDINT_HPD) {
660 if (lvl & CEC_RXSHPDLEV_HPD) 783 if (lvl & CEC_RXSHPDLEV_HPD) {
661 tda998x_edid_delay_start(priv); 784 tda998x_edid_delay_start(priv);
662 else 785 } else {
663 schedule_work(&priv->detect_work); 786 schedule_work(&priv->detect_work);
787 cec_notifier_set_phys_addr(priv->cec_notify,
788 CEC_PHYS_ADDR_INVALID);
789 }
664 790
665 handled = true; 791 handled = true;
666 } 792 }
@@ -981,6 +1107,8 @@ static int tda998x_connector_fill_modes(struct drm_connector *connector,
981 if (connector->edid_blob_ptr) { 1107 if (connector->edid_blob_ptr) {
982 struct edid *edid = (void *)connector->edid_blob_ptr->data; 1108 struct edid *edid = (void *)connector->edid_blob_ptr->data;
983 1109
1110 cec_notifier_set_phys_addr_from_edid(priv->cec_notify, edid);
1111
984 priv->sink_has_audio = drm_detect_monitor_audio(edid); 1112 priv->sink_has_audio = drm_detect_monitor_audio(edid);
985 } else { 1113 } else {
986 priv->sink_has_audio = false; 1114 priv->sink_has_audio = false;
@@ -1024,6 +1152,8 @@ static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
1024 offset = (blk & 1) ? 128 : 0; 1152 offset = (blk & 1) ? 128 : 0;
1025 segptr = blk / 2; 1153 segptr = blk / 2;
1026 1154
1155 mutex_lock(&priv->edid_mutex);
1156
1027 reg_write(priv, REG_DDC_ADDR, 0xa0); 1157 reg_write(priv, REG_DDC_ADDR, 0xa0);
1028 reg_write(priv, REG_DDC_OFFS, offset); 1158 reg_write(priv, REG_DDC_OFFS, offset);
1029 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60); 1159 reg_write(priv, REG_DDC_SEGM_ADDR, 0x60);
@@ -1043,14 +1173,15 @@ static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
1043 msecs_to_jiffies(100)); 1173 msecs_to_jiffies(100));
1044 if (i < 0) { 1174 if (i < 0) {
1045 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i); 1175 dev_err(&priv->hdmi->dev, "read edid wait err %d\n", i);
1046 return i; 1176 ret = i;
1177 goto failed;
1047 } 1178 }
1048 } else { 1179 } else {
1049 for (i = 100; i > 0; i--) { 1180 for (i = 100; i > 0; i--) {
1050 msleep(1); 1181 msleep(1);
1051 ret = reg_read(priv, REG_INT_FLAGS_2); 1182 ret = reg_read(priv, REG_INT_FLAGS_2);
1052 if (ret < 0) 1183 if (ret < 0)
1053 return ret; 1184 goto failed;
1054 if (ret & INT_FLAGS_2_EDID_BLK_RD) 1185 if (ret & INT_FLAGS_2_EDID_BLK_RD)
1055 break; 1186 break;
1056 } 1187 }
@@ -1058,17 +1189,22 @@ static int read_edid_block(void *data, u8 *buf, unsigned int blk, size_t length)
1058 1189
1059 if (i == 0) { 1190 if (i == 0) {
1060 dev_err(&priv->hdmi->dev, "read edid timeout\n"); 1191 dev_err(&priv->hdmi->dev, "read edid timeout\n");
1061 return -ETIMEDOUT; 1192 ret = -ETIMEDOUT;
1193 goto failed;
1062 } 1194 }
1063 1195
1064 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length); 1196 ret = reg_read_range(priv, REG_EDID_DATA_0, buf, length);
1065 if (ret != length) { 1197 if (ret != length) {
1066 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n", 1198 dev_err(&priv->hdmi->dev, "failed to read edid block %d: %d\n",
1067 blk, ret); 1199 blk, ret);
1068 return ret; 1200 goto failed;
1069 } 1201 }
1070 1202
1071 return 0; 1203 ret = 0;
1204
1205 failed:
1206 mutex_unlock(&priv->edid_mutex);
1207 return ret;
1072} 1208}
1073 1209
1074static int tda998x_connector_get_modes(struct drm_connector *connector) 1210static int tda998x_connector_get_modes(struct drm_connector *connector)
@@ -1423,6 +1559,9 @@ static void tda998x_destroy(struct tda998x_priv *priv)
1423 cancel_work_sync(&priv->detect_work); 1559 cancel_work_sync(&priv->detect_work);
1424 1560
1425 i2c_unregister_device(priv->cec); 1561 i2c_unregister_device(priv->cec);
1562
1563 if (priv->cec_notify)
1564 cec_notifier_put(priv->cec_notify);
1426} 1565}
1427 1566
1428/* I2C driver functions */ 1567/* I2C driver functions */
@@ -1472,10 +1611,16 @@ static int tda998x_get_audio_ports(struct tda998x_priv *priv,
1472static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv) 1611static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1473{ 1612{
1474 struct device_node *np = client->dev.of_node; 1613 struct device_node *np = client->dev.of_node;
1614 struct i2c_board_info cec_info;
1475 u32 video; 1615 u32 video;
1476 int rev_lo, rev_hi, ret; 1616 int rev_lo, rev_hi, ret;
1477 1617
1478 mutex_init(&priv->audio_mutex); /* Protect access from audio thread */ 1618 mutex_init(&priv->mutex); /* protect the page access */
1619 mutex_init(&priv->audio_mutex); /* protect access from audio thread */
1620 mutex_init(&priv->edid_mutex);
1621 init_waitqueue_head(&priv->edid_delay_waitq);
1622 timer_setup(&priv->edid_delay_timer, tda998x_edid_delay_done, 0);
1623 INIT_WORK(&priv->detect_work, tda998x_detect_work);
1479 1624
1480 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3); 1625 priv->vip_cntrl_0 = VIP_CNTRL_0_SWAP_A(2) | VIP_CNTRL_0_SWAP_B(3);
1481 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1); 1626 priv->vip_cntrl_1 = VIP_CNTRL_1_SWAP_C(0) | VIP_CNTRL_1_SWAP_D(1);
@@ -1485,14 +1630,6 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1485 priv->cec_addr = 0x34 + (client->addr & 0x03); 1630 priv->cec_addr = 0x34 + (client->addr & 0x03);
1486 priv->current_page = 0xff; 1631 priv->current_page = 0xff;
1487 priv->hdmi = client; 1632 priv->hdmi = client;
1488 priv->cec = i2c_new_dummy(client->adapter, priv->cec_addr);
1489 if (!priv->cec)
1490 return -ENODEV;
1491
1492 mutex_init(&priv->mutex); /* protect the page access */
1493 init_waitqueue_head(&priv->edid_delay_waitq);
1494 timer_setup(&priv->edid_delay_timer, tda998x_edid_delay_done, 0);
1495 INIT_WORK(&priv->detect_work, tda998x_detect_work);
1496 1633
1497 /* wake up the device: */ 1634 /* wake up the device: */
1498 cec_write(priv, REG_CEC_ENAMODS, 1635 cec_write(priv, REG_CEC_ENAMODS,
@@ -1502,10 +1639,15 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1502 1639
1503 /* read version: */ 1640 /* read version: */
1504 rev_lo = reg_read(priv, REG_VERSION_LSB); 1641 rev_lo = reg_read(priv, REG_VERSION_LSB);
1642 if (rev_lo < 0) {
1643 dev_err(&client->dev, "failed to read version: %d\n", rev_lo);
1644 return rev_lo;
1645 }
1646
1505 rev_hi = reg_read(priv, REG_VERSION_MSB); 1647 rev_hi = reg_read(priv, REG_VERSION_MSB);
1506 if (rev_lo < 0 || rev_hi < 0) { 1648 if (rev_hi < 0) {
1507 ret = rev_lo < 0 ? rev_lo : rev_hi; 1649 dev_err(&client->dev, "failed to read version: %d\n", rev_hi);
1508 goto fail; 1650 return rev_hi;
1509 } 1651 }
1510 1652
1511 priv->rev = rev_lo | rev_hi << 8; 1653 priv->rev = rev_lo | rev_hi << 8;
@@ -1529,7 +1671,7 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1529 default: 1671 default:
1530 dev_err(&client->dev, "found unsupported device: %04x\n", 1672 dev_err(&client->dev, "found unsupported device: %04x\n",
1531 priv->rev); 1673 priv->rev);
1532 goto fail; 1674 return -ENXIO;
1533 } 1675 }
1534 1676
1535 /* after reset, enable DDC: */ 1677 /* after reset, enable DDC: */
@@ -1545,6 +1687,15 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1545 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL, 1687 cec_write(priv, REG_CEC_FRO_IM_CLK_CTRL,
1546 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL); 1688 CEC_FRO_IM_CLK_CTRL_GHOST_DIS | CEC_FRO_IM_CLK_CTRL_IMCLK_SEL);
1547 1689
1690 /* ensure interrupts are disabled */
1691 cec_write(priv, REG_CEC_RXSHPDINTENA, 0);
1692
1693 /* clear pending interrupts */
1694 cec_read(priv, REG_CEC_RXSHPDINT);
1695 reg_read(priv, REG_INT_FLAGS_0);
1696 reg_read(priv, REG_INT_FLAGS_1);
1697 reg_read(priv, REG_INT_FLAGS_2);
1698
1548 /* initialize the optional IRQ */ 1699 /* initialize the optional IRQ */
1549 if (client->irq) { 1700 if (client->irq) {
1550 unsigned long irq_flags; 1701 unsigned long irq_flags;
@@ -1552,13 +1703,11 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1552 /* init read EDID waitqueue and HDP work */ 1703 /* init read EDID waitqueue and HDP work */
1553 init_waitqueue_head(&priv->wq_edid); 1704 init_waitqueue_head(&priv->wq_edid);
1554 1705
1555 /* clear pending interrupts */
1556 reg_read(priv, REG_INT_FLAGS_0);
1557 reg_read(priv, REG_INT_FLAGS_1);
1558 reg_read(priv, REG_INT_FLAGS_2);
1559
1560 irq_flags = 1706 irq_flags =
1561 irqd_get_trigger_type(irq_get_irq_data(client->irq)); 1707 irqd_get_trigger_type(irq_get_irq_data(client->irq));
1708
1709 priv->cec_glue.irq_flags = irq_flags;
1710
1562 irq_flags |= IRQF_SHARED | IRQF_ONESHOT; 1711 irq_flags |= IRQF_SHARED | IRQF_ONESHOT;
1563 ret = request_threaded_irq(client->irq, NULL, 1712 ret = request_threaded_irq(client->irq, NULL,
1564 tda998x_irq_thread, irq_flags, 1713 tda998x_irq_thread, irq_flags,
@@ -1567,13 +1716,46 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1567 dev_err(&client->dev, 1716 dev_err(&client->dev,
1568 "failed to request IRQ#%u: %d\n", 1717 "failed to request IRQ#%u: %d\n",
1569 client->irq, ret); 1718 client->irq, ret);
1570 goto fail; 1719 goto err_irq;
1571 } 1720 }
1572 1721
1573 /* enable HPD irq */ 1722 /* enable HPD irq */
1574 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD); 1723 cec_write(priv, REG_CEC_RXSHPDINTENA, CEC_RXSHPDLEV_HPD);
1575 } 1724 }
1576 1725
1726 priv->cec_notify = cec_notifier_get(&client->dev);
1727 if (!priv->cec_notify) {
1728 ret = -ENOMEM;
1729 goto fail;
1730 }
1731
1732 priv->cec_glue.parent = &client->dev;
1733 priv->cec_glue.data = priv;
1734 priv->cec_glue.init = tda998x_cec_hook_init;
1735 priv->cec_glue.exit = tda998x_cec_hook_exit;
1736 priv->cec_glue.open = tda998x_cec_hook_open;
1737 priv->cec_glue.release = tda998x_cec_hook_release;
1738
1739 /*
1740 * Some TDA998x are actually two I2C devices merged onto one piece
1741 * of silicon: TDA9989 and TDA19989 combine the HDMI transmitter
1742 * with a slightly modified TDA9950 CEC device. The CEC device
1743 * is at the TDA9950 address, with the address pins strapped across
1744 * to the TDA998x address pins. Hence, it always has the same
1745 * offset.
1746 */
1747 memset(&cec_info, 0, sizeof(cec_info));
1748 strlcpy(cec_info.type, "tda9950", sizeof(cec_info.type));
1749 cec_info.addr = priv->cec_addr;
1750 cec_info.platform_data = &priv->cec_glue;
1751 cec_info.irq = client->irq;
1752
1753 priv->cec = i2c_new_device(client->adapter, &cec_info);
1754 if (!priv->cec) {
1755 ret = -ENODEV;
1756 goto fail;
1757 }
1758
1577 /* enable EDID read irq: */ 1759 /* enable EDID read irq: */
1578 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD); 1760 reg_set(priv, REG_INT_FLAGS_2, INT_FLAGS_2_EDID_BLK_RD);
1579 1761
@@ -1596,12 +1778,18 @@ static int tda998x_create(struct i2c_client *client, struct tda998x_priv *priv)
1596 tda998x_audio_codec_init(priv, &client->dev); 1778 tda998x_audio_codec_init(priv, &client->dev);
1597 1779
1598 return 0; 1780 return 0;
1781
1599fail: 1782fail:
1600 /* if encoder_init fails, the encoder slave is never registered, 1783 /* if encoder_init fails, the encoder slave is never registered,
1601 * so cleanup here: 1784 * so cleanup here:
1602 */ 1785 */
1603 i2c_unregister_device(priv->cec); 1786 i2c_unregister_device(priv->cec);
1604 return -ENXIO; 1787 if (priv->cec_notify)
1788 cec_notifier_put(priv->cec_notify);
1789 if (client->irq)
1790 free_irq(client->irq, priv);
1791err_irq:
1792 return ret;
1605} 1793}
1606 1794
1607static void tda998x_encoder_prepare(struct drm_encoder *encoder) 1795static void tda998x_encoder_prepare(struct drm_encoder *encoder)