2 * TDA9950 Consumer Electronics Control driver
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.
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.
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.
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>
44 CCONR_ENABLE_ERROR = BIT(4),
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,
66 struct i2c_client *client;
68 struct cec_adapter *adap;
69 struct tda9950_glue *glue;
71 struct cec_msg rx_msg;
72 struct cec_notifier *notify;
76 static int tda9950_write_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
79 u8 buf[CEC_MAX_MSG_SIZE + 3];
82 if (WARN_ON(cnt > sizeof(buf) - 1))
86 memcpy(buf + 1, p, cnt);
88 msg.addr = client->addr;
93 dev_dbg(&client->dev, "wr 0x%02x: %*ph\n", addr, cnt, p);
95 ret = i2c_transfer(client->adapter, &msg, 1);
97 dev_err(&client->dev, "Error %d writing to cec:0x%x\n", ret, addr);
98 return ret < 0 ? ret : 0;
101 static void tda9950_write(struct i2c_client *client, u8 addr, u8 val)
103 tda9950_write_range(client, addr, &val, 1);
106 static int tda9950_read_range(struct i2c_client *client, u8 addr, u8 *p, int cnt)
108 struct i2c_msg msg[2];
111 msg[0].addr = client->addr;
115 msg[1].addr = client->addr;
116 msg[1].flags = I2C_M_RD;
120 ret = i2c_transfer(client->adapter, msg, 2);
122 dev_err(&client->dev, "Error %d reading from cec:0x%x\n", ret, addr);
124 dev_dbg(&client->dev, "rd 0x%02x: %*ph\n", addr, cnt, p);
129 static u8 tda9950_read(struct i2c_client *client, u8 addr)
134 ret = tda9950_read_range(client, addr, &val, 1);
141 static irqreturn_t tda9950_irq(int irq, void *data)
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;
151 csr = tda9950_read(priv->client, REG_CSR);
152 if (!(csr & CSR_INT))
155 cconr = tda9950_read(priv->client, REG_CCONR) & CCONR_RETRY_MASK;
157 tda9950_read_range(priv->client, REG_CDR0, buf, sizeof(buf));
160 * This should never happen: the data sheet says that there will
161 * always be a valid message if the interrupt line is asserted.
164 dev_warn(&priv->client->dev, "interrupt pending, but no message?\n");
169 case CDR1_CNF: /* transmit result */
170 arb_lost_cnt = nack_cnt = err_cnt = 0;
172 case CDR2_CNF_SUCCESS:
173 tx_status = CEC_TX_STATUS_OK;
176 case CDR2_CNF_ARB_ERROR:
177 tx_status = CEC_TX_STATUS_ARB_LOST;
178 arb_lost_cnt = cconr;
181 case CDR2_CNF_NACK_ADDR:
182 tx_status = CEC_TX_STATUS_NACK;
186 default: /* some other error, refer to TDA9950 docs */
187 dev_err(&priv->client->dev, "CNF reply error 0x%02x\n",
189 tx_status = CEC_TX_STATUS_ERROR;
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);
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;
204 memcpy(priv->rx_msg.msg, buf + 2, priv->rx_msg.len);
205 cec_received_msg(priv->adap, &priv->rx_msg);
208 default: /* unknown */
209 dev_err(&priv->client->dev, "unknown service id 0x%02x\n",
217 static int tda9950_cec_transmit(struct cec_adapter *adap, u8 attempts,
218 u32 signal_free_time, struct cec_msg *msg)
220 struct tda9950_priv *priv = adap->priv;
221 u8 buf[CEC_MAX_MSG_SIZE + 2];
223 buf[0] = 2 + msg->len;
225 memcpy(buf + 2, msg->msg, msg->len);
230 tda9950_write(priv->client, REG_CCONR, attempts);
232 return tda9950_write_range(priv->client, REG_CDR0, buf, 2 + msg->len);
235 static int tda9950_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
237 struct tda9950_priv *priv = adap->priv;
241 if (addr == CEC_LOG_ADDR_INVALID)
242 addresses = priv->addresses = 0;
244 addresses = priv->addresses |= BIT(addr);
246 /* TDA9950 doesn't want address 15 set */
248 buf[0] = addresses >> 8;
251 return tda9950_write_range(priv->client, REG_ACKH, buf, 2);
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
260 static int tda9950_glue_open(struct tda9950_priv *priv)
264 if (priv->glue && priv->glue->open)
265 ret = priv->glue->open(priv->glue->data);
272 static void tda9950_glue_release(struct tda9950_priv *priv)
276 if (priv->glue && priv->glue->release)
277 priv->glue->release(priv->glue->data);
280 static int tda9950_open(struct tda9950_priv *priv)
282 struct i2c_client *client = priv->client;
285 ret = tda9950_glue_open(priv);
289 /* Reset the TDA9950, and wait 250ms for it to recover */
290 tda9950_write(client, REG_CCR, CCR_RESET);
293 tda9950_cec_adap_log_addr(priv->adap, CEC_LOG_ADDR_INVALID);
295 /* Start the command processor */
296 tda9950_write(client, REG_CCR, CCR_ON);
301 static void tda9950_release(struct tda9950_priv *priv)
303 struct i2c_client *client = priv->client;
307 /* Stop the command processor */
308 tda9950_write(client, REG_CCR, 0);
310 /* Wait up to .5s for it to signal non-busy */
312 csr = tda9950_read(client, REG_CSR);
313 if (!(csr & CSR_BUSY) || --timeout)
318 /* Warn the user that their IRQ may die if it's shared. */
320 dev_warn(&client->dev, "command processor failed to stop, irq%d may die (csr=0x%02x)\n",
323 tda9950_glue_release(priv);
326 static int tda9950_cec_adap_enable(struct cec_adapter *adap, bool enable)
328 struct tda9950_priv *priv = adap->priv;
331 tda9950_release(priv);
334 return tda9950_open(priv);
338 static 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,
345 * When operating as part of the TDA998x, we need to claim additional
346 * resources. These two hooks permit the management of those resources.
348 static void tda9950_devm_glue_exit(void *data)
350 struct tda9950_glue *glue = data;
352 if (glue && glue->exit)
353 glue->exit(glue->data);
356 static int tda9950_devm_glue_init(struct device *dev, struct tda9950_glue *glue)
360 if (glue && glue->init) {
361 ret = glue->init(glue->data);
366 ret = devm_add_action(dev, tda9950_devm_glue_exit, glue);
368 tda9950_devm_glue_exit(glue);
373 static void tda9950_cec_del(void *data)
375 struct tda9950_priv *priv = data;
377 cec_delete_adapter(priv->adap);
380 static int tda9950_probe(struct i2c_client *client,
381 const struct i2c_device_id *id)
383 struct tda9950_glue *glue = client->dev.platform_data;
384 struct device *dev = &client->dev;
385 struct tda9950_priv *priv;
386 unsigned long irqflags;
391 * We must have I2C functionality: our multi-byte accesses
392 * must be performed as a single contiguous transaction.
394 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
395 dev_err(&client->dev,
396 "adapter does not support I2C functionality\n");
400 /* We must have an interrupt to be functional. */
401 if (client->irq <= 0) {
402 dev_err(&client->dev, "driver requires an interrupt\n");
406 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
410 priv->client = client;
413 i2c_set_clientdata(client, priv);
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.
421 if (glue && glue->parent)
422 priv->hdmi = glue->parent;
424 priv->adap = cec_allocate_adapter(&tda9950_cec_ops, priv, "tda9950",
427 if (IS_ERR(priv->adap))
428 return PTR_ERR(priv->adap);
430 ret = devm_add_action(dev, tda9950_cec_del, priv);
432 cec_delete_adapter(priv->adap);
436 ret = tda9950_devm_glue_init(dev, glue);
440 ret = tda9950_glue_open(priv);
444 cvr = tda9950_read(client, REG_CVR);
446 dev_info(&client->dev,
447 "TDA9950 CEC interface, hardware version %u.%u\n",
450 tda9950_glue_release(priv);
452 irqflags = IRQF_TRIGGER_FALLING;
454 irqflags = glue->irq_flags;
456 ret = devm_request_threaded_irq(dev, client->irq, NULL, tda9950_irq,
457 irqflags | IRQF_SHARED | IRQF_ONESHOT,
458 dev_name(&client->dev), priv);
462 priv->notify = cec_notifier_get(priv->hdmi);
466 ret = cec_register_adapter(priv->adap, priv->hdmi);
468 cec_notifier_put(priv->notify);
473 * CEC documentation says we must not call cec_delete_adapter
474 * after a successful call to cec_register_adapter().
476 devm_remove_action(dev, tda9950_cec_del, priv);
478 cec_register_cec_notifier(priv->adap, priv->notify);
483 static int tda9950_remove(struct i2c_client *client)
485 struct tda9950_priv *priv = i2c_get_clientdata(client);
487 cec_unregister_adapter(priv->adap);
488 cec_notifier_put(priv->notify);
493 static struct i2c_device_id tda9950_ids[] = {
497 MODULE_DEVICE_TABLE(i2c, tda9950_ids);
499 static struct i2c_driver tda9950_driver = {
500 .probe = tda9950_probe,
501 .remove = tda9950_remove,
505 .id_table = tda9950_ids,
508 module_i2c_driver(tda9950_driver);
511 MODULE_DESCRIPTION("TDA9950/TDA998x Consumer Electronics Control Driver");
512 MODULE_LICENSE("GPL v2");