blob: 5d2f0d548469e1dd4808d8be5dfe8be62edfef0b [file] [log] [blame]
Russell Kingf0316f92015-12-05 18:41:28 +00001/*
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;
Kees Cook699112f2018-06-19 21:38:31 -070079 u8 buf[CEC_MAX_MSG_SIZE + 3];
Russell Kingf0316f92015-12-05 18:41:28 +000080 int ret;
81
Kees Cook699112f2018-06-19 21:38:31 -070082 if (WARN_ON(cnt > sizeof(buf) - 1))
83 return -EINVAL;
84
Russell Kingf0316f92015-12-05 18:41:28 +000085 buf[0] = addr;
86 memcpy(buf + 1, p, cnt);
87
88 msg.addr = client->addr;
89 msg.flags = 0;
90 msg.len = cnt + 1;
91 msg.buf = buf;
92
93 dev_dbg(&client->dev, "wr 0x%02x: %*ph\n", addr, cnt, p);
94
95 ret = i2c_transfer(client->adapter, &msg, 1);
96 if (ret < 0)
97 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
98 return ret < 0 ? ret : 0;
99}
100
101static void tda9950_write(struct i2c_client *client, u8 addr, u8 val)
102{
103 tda9950_write_range(client, addr, &val, 1);
104}
105
106static int tda9950_read_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
107{
108 struct i2c_msg msg[2];
109 int ret;
110
111 msg[0].addr = client->addr;
112 msg[0].flags = 0;
113 msg[0].len = 1;
114 msg[0].buf = &addr;
115 msg[1].addr = client->addr;
116 msg[1].flags = I2C_M_RD;
117 msg[1].len = cnt;
118 msg[1].buf = p;
119
120 ret = i2c_transfer(client->adapter, msg, 2);
121 if (ret < 0)
122 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
123
124 dev_dbg(&client->dev, "rd 0x%02x: %*ph\n", addr, cnt, p);
125
126 return ret;
127}
128
129static u8 tda9950_read(struct i2c_client *client, u8 addr)
130{
131 int ret;
132 u8 val;
133
134 ret = tda9950_read_range(client, addr, &val, 1);
135 if (ret < 0)
136 val = 0;
137
138 return val;
139}
140
141static irqreturn_t tda9950_irq(int irq, void *data)
142{
143 struct tda9950_priv *priv = data;
144 unsigned int tx_status;
145 u8 csr, cconr, buf[19];
146 u8 arb_lost_cnt, nack_cnt, err_cnt;
147
148 if (!priv->open)
149 return IRQ_NONE;
150
151 csr = tda9950_read(priv->client, REG_CSR);
152 if (!(csr & CSR_INT))
153 return IRQ_NONE;
154
155 cconr = tda9950_read(priv->client, REG_CCONR) & CCONR_RETRY_MASK;
156
157 tda9950_read_range(priv->client, REG_CDR0, buf, sizeof(buf));
158
159 /*
160 * This should never happen: the data sheet says that there will
161 * always be a valid message if the interrupt line is asserted.
162 */
163 if (buf[0] == 0) {
164 dev_warn(&priv->client->dev, "interrupt pending, but no message?\n");
165 return IRQ_NONE;
166 }
167
168 switch (buf[1]) {
169 case CDR1_CNF: /* transmit result */
170 arb_lost_cnt = nack_cnt = err_cnt = 0;
171 switch (buf[2]) {
172 case CDR2_CNF_SUCCESS:
173 tx_status = CEC_TX_STATUS_OK;
174 break;
175
176 case CDR2_CNF_ARB_ERROR:
177 tx_status = CEC_TX_STATUS_ARB_LOST;
178 arb_lost_cnt = cconr;
179 break;
180
181 case CDR2_CNF_NACK_ADDR:
182 tx_status = CEC_TX_STATUS_NACK;
183 nack_cnt = cconr;
184 break;
185
186 default: /* some other error, refer to TDA9950 docs */
187 dev_err(&priv->client->dev, "CNF reply error 0x%02x\n",
188 buf[2]);
189 tx_status = CEC_TX_STATUS_ERROR;
190 err_cnt = cconr;
191 break;
192 }
193 /* TDA9950 executes all retries for us */
194 tx_status |= CEC_TX_STATUS_MAX_RETRIES;
195 cec_transmit_done(priv->adap, tx_status, arb_lost_cnt,
196 nack_cnt, 0, err_cnt);
197 break;
198
199 case CDR1_IND:
200 priv->rx_msg.len = buf[0] - 2;
201 if (priv->rx_msg.len > CEC_MAX_MSG_SIZE)
202 priv->rx_msg.len = CEC_MAX_MSG_SIZE;
203
204 memcpy(priv->rx_msg.msg, buf + 2, priv->rx_msg.len);
205 cec_received_msg(priv->adap, &priv->rx_msg);
206 break;
207
208 default: /* unknown */
209 dev_err(&priv->client->dev, "unknown service id 0x%02x\n",
210 buf[1]);
211 break;
212 }
213
214 return IRQ_HANDLED;
215}
216
217static int tda9950_cec_transmit(struct cec_adapter *adap, u8 attempts,
218 u32 signal_free_time, struct cec_msg *msg)
219{
220 struct tda9950_priv *priv = adap->priv;
221 u8 buf[CEC_MAX_MSG_SIZE + 2];
222
223 buf[0] = 2 + msg->len;
224 buf[1] = CDR1_REQ;
225 memcpy(buf + 2, msg->msg, msg->len);
226
227 if (attempts > 5)
228 attempts = 5;
229
230 tda9950_write(priv->client, REG_CCONR, attempts);
231
232 return tda9950_write_range(priv->client, REG_CDR0, buf, 2 + msg->len);
233}
234
235static int tda9950_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
236{
237 struct tda9950_priv *priv = adap->priv;
238 u16 addresses;
239 u8 buf[2];
240
241 if (addr == CEC_LOG_ADDR_INVALID)
242 addresses = priv->addresses = 0;
243 else
244 addresses = priv->addresses |= BIT(addr);
245
246 /* TDA9950 doesn't want address 15 set */
247 addresses &= 0x7fff;
248 buf[0] = addresses >> 8;
249 buf[1] = addresses;
250
251 return tda9950_write_range(priv->client, REG_ACKH, buf, 2);
252}
253
254/*
255 * When operating as part of the TDA998x, we need additional handling
256 * to initialise and shut down the TDA9950 part of the device. These
257 * two hooks are provided to allow the TDA998x code to perform those
258 * activities.
259 */
260static int tda9950_glue_open(struct tda9950_priv *priv)
261{
262 int ret = 0;
263
264 if (priv->glue && priv->glue->open)
265 ret = priv->glue->open(priv->glue->data);
266
267 priv->open = true;
268
269 return ret;
270}
271
272static void tda9950_glue_release(struct tda9950_priv *priv)
273{
274 priv->open = false;
275
276 if (priv->glue && priv->glue->release)
277 priv->glue->release(priv->glue->data);
278}
279
280static int tda9950_open(struct tda9950_priv *priv)
281{
282 struct i2c_client *client = priv->client;
283 int ret;
284
285 ret = tda9950_glue_open(priv);
286 if (ret)
287 return ret;
288
289 /* Reset the TDA9950, and wait 250ms for it to recover */
290 tda9950_write(client, REG_CCR, CCR_RESET);
291 msleep(250);
292
293 tda9950_cec_adap_log_addr(priv->adap, CEC_LOG_ADDR_INVALID);
294
295 /* Start the command processor */
296 tda9950_write(client, REG_CCR, CCR_ON);
297
298 return 0;
299}
300
301static void tda9950_release(struct tda9950_priv *priv)
302{
303 struct i2c_client *client = priv->client;
304 int timeout = 50;
305 u8 csr;
306
307 /* Stop the command processor */
308 tda9950_write(client, REG_CCR, 0);
309
310 /* Wait up to .5s for it to signal non-busy */
311 do {
312 csr = tda9950_read(client, REG_CSR);
313 if (!(csr & CSR_BUSY) || --timeout)
314 break;
315 msleep(10);
316 } while (1);
317
318 /* Warn the user that their IRQ may die if it's shared. */
319 if (csr & CSR_BUSY)
320 dev_warn(&client->dev, "command processor failed to stop, irq%d may die (csr=0x%02x)\n",
321 client->irq, csr);
322
323 tda9950_glue_release(priv);
324}
325
326static int tda9950_cec_adap_enable(struct cec_adapter *adap, bool enable)
327{
328 struct tda9950_priv *priv = adap->priv;
329
330 if (!enable) {
331 tda9950_release(priv);
332 return 0;
333 } else {
334 return tda9950_open(priv);
335 }
336}
337
338static const struct cec_adap_ops tda9950_cec_ops = {
339 .adap_enable = tda9950_cec_adap_enable,
340 .adap_log_addr = tda9950_cec_adap_log_addr,
341 .adap_transmit = tda9950_cec_transmit,
342};
343
344/*
345 * When operating as part of the TDA998x, we need to claim additional
346 * resources. These two hooks permit the management of those resources.
347 */
348static void tda9950_devm_glue_exit(void *data)
349{
350 struct tda9950_glue *glue = data;
351
352 if (glue && glue->exit)
353 glue->exit(glue->data);
354}
355
356static int tda9950_devm_glue_init(struct device *dev, struct tda9950_glue *glue)
357{
358 int ret;
359
360 if (glue && glue->init) {
361 ret = glue->init(glue->data);
362 if (ret)
363 return ret;
364 }
365
366 ret = devm_add_action(dev, tda9950_devm_glue_exit, glue);
367 if (ret)
368 tda9950_devm_glue_exit(glue);
369
370 return ret;
371}
372
373static void tda9950_cec_del(void *data)
374{
375 struct tda9950_priv *priv = data;
376
377 cec_delete_adapter(priv->adap);
378}
379
380static int tda9950_probe(struct i2c_client *client,
381 const struct i2c_device_id *id)
382{
383 struct tda9950_glue *glue = client->dev.platform_data;
384 struct device *dev = &client->dev;
385 struct tda9950_priv *priv;
386 unsigned long irqflags;
387 int ret;
388 u8 cvr;
389
390 /*
391 * We must have I2C functionality: our multi-byte accesses
392 * must be performed as a single contiguous transaction.
393 */
394 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
395 dev_err(&client->dev,
396 "adapter does not support I2C functionality\n");
397 return -ENXIO;
398 }
399
400 /* We must have an interrupt to be functional. */
401 if (client->irq <= 0) {
402 dev_err(&client->dev, "driver requires an interrupt\n");
403 return -ENXIO;
404 }
405
406 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
407 if (!priv)
408 return -ENOMEM;
409
410 priv->client = client;
411 priv->glue = glue;
412
413 i2c_set_clientdata(client, priv);
414
415 /*
416 * If we're part of a TDA998x, we want the class devices to be
417 * associated with the HDMI Tx so we have a tight relationship
418 * between the HDMI interface and the CEC interface.
419 */
420 priv->hdmi = dev;
421 if (glue && glue->parent)
422 priv->hdmi = glue->parent;
423
424 priv->adap = cec_allocate_adapter(&tda9950_cec_ops, priv, "tda9950",
425 CEC_CAP_DEFAULTS,
426 CEC_MAX_LOG_ADDRS);
427 if (IS_ERR(priv->adap))
428 return PTR_ERR(priv->adap);
429
430 ret = devm_add_action(dev, tda9950_cec_del, priv);
431 if (ret) {
432 cec_delete_adapter(priv->adap);
433 return ret;
434 }
435
436 ret = tda9950_devm_glue_init(dev, glue);
437 if (ret)
438 return ret;
439
440 ret = tda9950_glue_open(priv);
441 if (ret)
442 return ret;
443
444 cvr = tda9950_read(client, REG_CVR);
445
446 dev_info(&client->dev,
447 "TDA9950 CEC interface, hardware version %u.%u\n",
448 cvr >> 4, cvr & 15);
449
450 tda9950_glue_release(priv);
451
452 irqflags = IRQF_TRIGGER_FALLING;
453 if (glue)
454 irqflags = glue->irq_flags;
455
456 ret = devm_request_threaded_irq(dev, client->irq, NULL, tda9950_irq,
457 irqflags | IRQF_SHARED | IRQF_ONESHOT,
458 dev_name(&client->dev), priv);
459 if (ret < 0)
460 return ret;
461
462 priv->notify = cec_notifier_get(priv->hdmi);
463 if (!priv->notify)
464 return -ENOMEM;
465
466 ret = cec_register_adapter(priv->adap, priv->hdmi);
467 if (ret < 0) {
468 cec_notifier_put(priv->notify);
469 return ret;
470 }
471
472 /*
473 * CEC documentation says we must not call cec_delete_adapter
474 * after a successful call to cec_register_adapter().
475 */
476 devm_remove_action(dev, tda9950_cec_del, priv);
477
478 cec_register_cec_notifier(priv->adap, priv->notify);
479
480 return 0;
481}
482
483static int tda9950_remove(struct i2c_client *client)
484{
485 struct tda9950_priv *priv = i2c_get_clientdata(client);
486
487 cec_unregister_adapter(priv->adap);
488 cec_notifier_put(priv->notify);
489
490 return 0;
491}
492
493static struct i2c_device_id tda9950_ids[] = {
494 { "tda9950", 0 },
495 { },
496};
497MODULE_DEVICE_TABLE(i2c, tda9950_ids);
498
499static struct i2c_driver tda9950_driver = {
500 .probe = tda9950_probe,
501 .remove = tda9950_remove,
502 .driver = {
503 .name = "tda9950",
504 },
505 .id_table = tda9950_ids,
506};
507
508module_i2c_driver(tda9950_driver);
509
510MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
511MODULE_DESCRIPTION("TDA9950/TDA998x Consumer Electronics Control Driver");
512MODULE_LICENSE("GPL v2");