1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for the Chrontel CH7322 CEC Controller
5 * Copyright 2020 Google LLC.
11 * - This device powers on in Auto Mode which has limited functionality. This
12 * driver disables Auto Mode when it attaches.
16 #include <linux/cec.h>
17 #include <linux/dmi.h>
18 #include <linux/i2c.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/pci.h>
23 #include <linux/regmap.h>
24 #include <media/cec.h>
25 #include <media/cec-notifier.h>
27 #define CH7322_WRITE 0x00
28 #define CH7322_WRITE_MSENT 0x80
29 #define CH7322_WRITE_BOK 0x40
30 #define CH7322_WRITE_NMASK 0x0f
32 /* Write buffer is 0x01-0x10 */
33 #define CH7322_WRBUF 0x01
34 #define CH7322_WRBUF_LEN 0x10
36 #define CH7322_READ 0x40
37 #define CH7322_READ_NRDT 0x80
38 #define CH7322_READ_MSENT 0x20
39 #define CH7322_READ_NMASK 0x0f
41 /* Read buffer is 0x41-0x50 */
42 #define CH7322_RDBUF 0x41
43 #define CH7322_RDBUF_LEN 0x10
45 #define CH7322_MODE 0x11
46 #define CH7322_MODE_AUTO 0x78
47 #define CH7322_MODE_SW 0xb5
49 #define CH7322_RESET 0x12
50 #define CH7322_RESET_RST 0x00
52 #define CH7322_POWER 0x13
53 #define CH7322_POWER_FPD 0x04
55 #define CH7322_CFG0 0x17
56 #define CH7322_CFG0_EOBEN 0x40
57 #define CH7322_CFG0_PEOB 0x20
58 #define CH7322_CFG0_CLRSPP 0x10
59 #define CH7322_CFG0_FLOW 0x08
61 #define CH7322_CFG1 0x1a
62 #define CH7322_CFG1_STDBYO 0x04
63 #define CH7322_CFG1_HPBP 0x02
64 #define CH7322_CFG1_PIO 0x01
66 #define CH7322_INTCTL 0x1b
67 #define CH7322_INTCTL_INTPB 0x80
68 #define CH7322_INTCTL_STDBY 0x40
69 #define CH7322_INTCTL_HPDFALL 0x20
70 #define CH7322_INTCTL_HPDRISE 0x10
71 #define CH7322_INTCTL_RXMSG 0x08
72 #define CH7322_INTCTL_TXMSG 0x04
73 #define CH7322_INTCTL_NEWPHA 0x02
74 #define CH7322_INTCTL_ERROR 0x01
76 #define CH7322_DVCLKFNH 0x1d
77 #define CH7322_DVCLKFNL 0x1e
79 #define CH7322_CTL 0x31
80 #define CH7322_CTL_FSTDBY 0x80
81 #define CH7322_CTL_PLSEN 0x40
82 #define CH7322_CTL_PLSPB 0x20
83 #define CH7322_CTL_SPADL 0x10
84 #define CH7322_CTL_HINIT 0x08
85 #define CH7322_CTL_WPHYA 0x04
86 #define CH7322_CTL_H1T 0x02
87 #define CH7322_CTL_S1T 0x01
89 #define CH7322_PAWH 0x32
90 #define CH7322_PAWL 0x33
92 #define CH7322_ADDLW 0x34
93 #define CH7322_ADDLW_MASK 0xf0
95 #define CH7322_ADDLR 0x3d
96 #define CH7322_ADDLR_HPD 0x80
97 #define CH7322_ADDLR_MASK 0x0f
99 #define CH7322_INTDATA 0x3e
100 #define CH7322_INTDATA_MODE 0x80
101 #define CH7322_INTDATA_STDBY 0x40
102 #define CH7322_INTDATA_HPDFALL 0x20
103 #define CH7322_INTDATA_HPDRISE 0x10
104 #define CH7322_INTDATA_RXMSG 0x08
105 #define CH7322_INTDATA_TXMSG 0x04
106 #define CH7322_INTDATA_NEWPHA 0x02
107 #define CH7322_INTDATA_ERROR 0x01
109 #define CH7322_EVENT 0x3f
110 #define CH7322_EVENT_TXERR 0x80
111 #define CH7322_EVENT_HRST 0x40
112 #define CH7322_EVENT_HFST 0x20
113 #define CH7322_EVENT_PHACHG 0x10
114 #define CH7322_EVENT_ACTST 0x08
115 #define CH7322_EVENT_PHARDY 0x04
116 #define CH7322_EVENT_BSOK 0x02
117 #define CH7322_EVENT_ERRADCF 0x01
119 #define CH7322_DID 0x51
120 #define CH7322_DID_CH7322 0x5b
121 #define CH7322_DID_CH7323 0x5f
123 #define CH7322_REVISIONID 0x52
125 #define CH7322_PARH 0x53
126 #define CH7322_PARL 0x54
128 #define CH7322_IOCFG2 0x75
129 #define CH7322_IOCFG_CIO 0x80
130 #define CH7322_IOCFG_IOCFGMASK 0x78
131 #define CH7322_IOCFG_AUDIO 0x04
132 #define CH7322_IOCFG_SPAMST 0x02
133 #define CH7322_IOCFG_SPAMSP 0x01
135 #define CH7322_CTL3 0x7b
136 #define CH7322_CTL3_SWENA 0x80
137 #define CH7322_CTL3_FC_INIT 0x40
138 #define CH7322_CTL3_SML_FL 0x20
139 #define CH7322_CTL3_SM_RDST 0x10
140 #define CH7322_CTL3_SPP_CIAH 0x08
141 #define CH7322_CTL3_SPP_CIAL 0x04
142 #define CH7322_CTL3_SPP_ACTH 0x02
143 #define CH7322_CTL3_SPP_ACTL 0x01
145 /* BOK status means NACK */
146 #define CH7322_TX_FLAG_NACK BIT(0)
147 /* Device will retry automatically */
148 #define CH7322_TX_FLAG_RETRY BIT(1)
151 struct i2c_client *i2c;
152 struct regmap *regmap;
153 struct cec_adapter *cec;
154 struct mutex mutex; /* device access mutex */
158 static const struct regmap_config ch7322_regmap = {
161 .max_register = 0x7f,
162 .disable_locking = true,
165 static int ch7322_send_message(struct ch7322 *ch7322, const struct cec_msg *msg)
168 unsigned int len = msg->len;
172 WARN_ON(!mutex_is_locked(&ch7322->mutex));
174 if (len > CH7322_WRBUF_LEN || len < 1)
177 ret = regmap_read(ch7322->regmap, CH7322_WRITE, &val);
181 /* Buffer not ready */
182 if (!(val & CH7322_WRITE_MSENT))
185 if (cec_msg_opcode(msg) == -1 &&
186 cec_msg_initiator(msg) == cec_msg_destination(msg)) {
187 ch7322->tx_flags = CH7322_TX_FLAG_NACK | CH7322_TX_FLAG_RETRY;
188 } else if (cec_msg_is_broadcast(msg)) {
189 ch7322->tx_flags = CH7322_TX_FLAG_NACK;
191 ch7322->tx_flags = CH7322_TX_FLAG_RETRY;
194 ret = regmap_write(ch7322->regmap, CH7322_WRITE, len - 1);
198 for (i = 0; i < len; i++) {
199 ret = regmap_write(ch7322->regmap,
200 CH7322_WRBUF + i, msg->msg[i]);
208 static int ch7322_receive_message(struct ch7322 *ch7322, struct cec_msg *msg)
214 WARN_ON(!mutex_is_locked(&ch7322->mutex));
216 ret = regmap_read(ch7322->regmap, CH7322_READ, &val);
220 /* Message not ready */
221 if (!(val & CH7322_READ_NRDT))
224 msg->len = (val & CH7322_READ_NMASK) + 1;
226 /* Read entire RDBUF to clear state */
227 for (i = 0; i < CH7322_RDBUF_LEN; i++) {
228 ret = regmap_read(ch7322->regmap, CH7322_RDBUF + i, &val);
231 msg->msg[i] = (u8)val;
237 static void ch7322_tx_done(struct ch7322 *ch7322)
243 mutex_lock(&ch7322->mutex);
244 ret = regmap_read(ch7322->regmap, CH7322_WRITE, &val);
245 flags = ch7322->tx_flags;
246 mutex_unlock(&ch7322->mutex);
249 * The device returns a one-bit OK status which usually means ACK but
250 * actually means NACK when sending a logical address query or a
254 status = CEC_TX_STATUS_ERROR;
255 else if ((val & CH7322_WRITE_BOK) && (flags & CH7322_TX_FLAG_NACK))
256 status = CEC_TX_STATUS_NACK;
257 else if (val & CH7322_WRITE_BOK)
258 status = CEC_TX_STATUS_OK;
259 else if (flags & CH7322_TX_FLAG_NACK)
260 status = CEC_TX_STATUS_OK;
262 status = CEC_TX_STATUS_NACK;
264 if (status == CEC_TX_STATUS_NACK && (flags & CH7322_TX_FLAG_RETRY))
265 status |= CEC_TX_STATUS_MAX_RETRIES;
267 cec_transmit_attempt_done(ch7322->cec, status);
270 static void ch7322_rx_done(struct ch7322 *ch7322)
275 mutex_lock(&ch7322->mutex);
276 ret = ch7322_receive_message(ch7322, &msg);
277 mutex_unlock(&ch7322->mutex);
280 dev_err(&ch7322->i2c->dev, "cec receive error: %d\n", ret);
282 cec_received_msg(ch7322->cec, &msg);
286 * This device can either monitor the DDC lines to obtain the physical address
287 * or it can allow the host to program it. This driver lets the device obtain
290 static void ch7322_phys_addr(struct ch7322 *ch7322)
292 unsigned int pah, pal;
295 mutex_lock(&ch7322->mutex);
296 ret |= regmap_read(ch7322->regmap, CH7322_PARH, &pah);
297 ret |= regmap_read(ch7322->regmap, CH7322_PARL, &pal);
298 mutex_unlock(&ch7322->mutex);
301 dev_err(&ch7322->i2c->dev, "phys addr error\n");
303 cec_s_phys_addr(ch7322->cec, pal | (pah << 8), false);
306 static irqreturn_t ch7322_irq(int irq, void *dev)
308 struct ch7322 *ch7322 = dev;
309 unsigned int data = 0;
311 mutex_lock(&ch7322->mutex);
312 regmap_read(ch7322->regmap, CH7322_INTDATA, &data);
313 regmap_write(ch7322->regmap, CH7322_INTDATA, data);
314 mutex_unlock(&ch7322->mutex);
316 if (data & CH7322_INTDATA_HPDFALL)
317 cec_phys_addr_invalidate(ch7322->cec);
319 if (data & CH7322_INTDATA_TXMSG)
320 ch7322_tx_done(ch7322);
322 if (data & CH7322_INTDATA_RXMSG)
323 ch7322_rx_done(ch7322);
325 if (data & CH7322_INTDATA_NEWPHA)
326 ch7322_phys_addr(ch7322);
328 if (data & CH7322_INTDATA_ERROR)
329 dev_dbg(&ch7322->i2c->dev, "unknown error\n");
334 /* This device is always enabled */
335 static int ch7322_cec_adap_enable(struct cec_adapter *adap, bool enable)
340 static int ch7322_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
342 struct ch7322 *ch7322 = cec_get_drvdata(adap);
345 mutex_lock(&ch7322->mutex);
346 ret = regmap_update_bits(ch7322->regmap, CH7322_ADDLW,
347 CH7322_ADDLW_MASK, log_addr << 4);
348 mutex_unlock(&ch7322->mutex);
353 static int ch7322_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
354 u32 signal_free_time, struct cec_msg *msg)
356 struct ch7322 *ch7322 = cec_get_drvdata(adap);
359 mutex_lock(&ch7322->mutex);
360 ret = ch7322_send_message(ch7322, msg);
361 mutex_unlock(&ch7322->mutex);
366 static const struct cec_adap_ops ch7322_cec_adap_ops = {
367 .adap_enable = ch7322_cec_adap_enable,
368 .adap_log_addr = ch7322_cec_adap_log_addr,
369 .adap_transmit = ch7322_cec_adap_transmit,
372 #if IS_ENABLED(CONFIG_PCI) && IS_ENABLED(CONFIG_DMI)
374 struct ch7322_conn_match {
375 const char *dev_name;
376 const char *pci_name;
377 const char *port_name;
380 static struct ch7322_conn_match google_endeavour[] = {
381 { "i2c-PRP0001:00", "0000:00:02.0", "Port B" },
382 { "i2c-PRP0001:01", "0000:00:02.0", "Port C" },
386 static const struct dmi_system_id ch7322_dmi_table[] = {
389 DMI_MATCH(DMI_BOARD_VENDOR, "Google"),
390 DMI_MATCH(DMI_BOARD_NAME, "Endeavour"),
392 .driver_data = google_endeavour,
397 /* Make a best-effort attempt to locate a matching HDMI port */
398 static int ch7322_get_port(struct i2c_client *client,
402 const struct dmi_system_id *system;
403 const struct ch7322_conn_match *conn;
408 system = dmi_first_match(ch7322_dmi_table);
412 for (conn = system->driver_data; conn->dev_name; conn++) {
413 if (!strcmp(dev_name(&client->dev), conn->dev_name)) {
416 d = bus_find_device_by_name(&pci_bus_type, NULL,
419 return -EPROBE_DEFER;
424 *port = conn->port_name;
435 static int ch7322_get_port(struct i2c_client *client,
447 static int ch7322_probe(struct i2c_client *client)
449 struct device *hdmi_dev;
450 const char *port_name;
451 struct ch7322 *ch7322;
452 struct cec_notifier *notifier = NULL;
453 u32 caps = CEC_CAP_DEFAULTS;
457 ret = ch7322_get_port(client, &hdmi_dev, &port_name);
462 caps |= CEC_CAP_CONNECTOR_INFO;
464 ch7322 = devm_kzalloc(&client->dev, sizeof(*ch7322), GFP_KERNEL);
468 ch7322->regmap = devm_regmap_init_i2c(client, &ch7322_regmap);
469 if (IS_ERR(ch7322->regmap))
470 return PTR_ERR(ch7322->regmap);
472 ret = regmap_read(ch7322->regmap, CH7322_DID, &val);
476 if (val != CH7322_DID_CH7322)
479 mutex_init(&ch7322->mutex);
480 ch7322->i2c = client;
481 ch7322->tx_flags = 0;
483 i2c_set_clientdata(client, ch7322);
485 /* Disable auto mode */
486 ret = regmap_write(ch7322->regmap, CH7322_MODE, CH7322_MODE_SW);
490 /* Enable logical address register */
491 ret = regmap_update_bits(ch7322->regmap, CH7322_CTL,
492 CH7322_CTL_SPADL, CH7322_CTL_SPADL);
496 ch7322->cec = cec_allocate_adapter(&ch7322_cec_adap_ops, ch7322,
497 dev_name(&client->dev),
500 if (IS_ERR(ch7322->cec)) {
501 ret = PTR_ERR(ch7322->cec);
505 ch7322->cec->adap_controls_phys_addr = true;
508 notifier = cec_notifier_cec_adap_register(hdmi_dev,
517 /* Configure, mask, and clear interrupt */
518 ret = regmap_write(ch7322->regmap, CH7322_CFG1, 0);
521 ret = regmap_write(ch7322->regmap, CH7322_INTCTL, CH7322_INTCTL_INTPB);
524 ret = regmap_write(ch7322->regmap, CH7322_INTDATA, 0xff);
528 /* If HPD is up read physical address */
529 ret = regmap_read(ch7322->regmap, CH7322_ADDLR, &val);
532 if (val & CH7322_ADDLR_HPD)
533 ch7322_phys_addr(ch7322);
535 ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
537 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
538 client->name, ch7322);
542 /* Unmask interrupt */
543 mutex_lock(&ch7322->mutex);
544 ret = regmap_write(ch7322->regmap, CH7322_INTCTL, 0xff);
545 mutex_unlock(&ch7322->mutex);
550 ret = cec_register_adapter(ch7322->cec, &client->dev);
554 dev_info(&client->dev, "device registered\n");
560 cec_notifier_cec_adap_unregister(notifier, ch7322->cec);
562 cec_delete_adapter(ch7322->cec);
564 mutex_destroy(&ch7322->mutex);
568 static int ch7322_remove(struct i2c_client *client)
570 struct ch7322 *ch7322 = i2c_get_clientdata(client);
573 mutex_lock(&ch7322->mutex);
574 regmap_write(ch7322->regmap, CH7322_INTCTL, CH7322_INTCTL_INTPB);
575 mutex_unlock(&ch7322->mutex);
577 cec_unregister_adapter(ch7322->cec);
578 mutex_destroy(&ch7322->mutex);
580 dev_info(&client->dev, "device unregistered\n");
585 static const struct of_device_id ch7322_of_match[] = {
586 { .compatible = "chrontel,ch7322", },
589 MODULE_DEVICE_TABLE(of, ch7322_of_match);
591 static struct i2c_driver ch7322_i2c_driver = {
594 .of_match_table = of_match_ptr(ch7322_of_match),
596 .probe_new = ch7322_probe,
597 .remove = ch7322_remove,
600 module_i2c_driver(ch7322_i2c_driver);
602 MODULE_DESCRIPTION("Chrontel CH7322 CEC Controller Driver");
604 MODULE_LICENSE("GPL");