summaryrefslogtreecommitdiffstats
path: root/drivers/media/cec/i2c/ch7322.c
blob: 0814338c43e4bc63bc2c2d17b4cf998f7ff3c32d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// SPDX-License-Identifier: GPL-2.0
/*
 * Driver for the Chrontel CH7322 CEC Controller
 *
 * Copyright 2020 Google LLC.
 */

/*
 * Notes
 *
 * - This device powers on in Auto Mode which has limited functionality. This
 *   driver disables Auto Mode when it attaches.
 *
 */

#include <linux/cec.h>
#include <linux/dmi.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/regmap.h>
#include <media/cec.h>
#include <media/cec-notifier.h>

#define CH7322_WRITE		0x00
#define CH7322_WRITE_MSENT		0x80
#define CH7322_WRITE_BOK		0x40
#define CH7322_WRITE_NMASK		0x0f

/* Write buffer is 0x01-0x10 */
#define CH7322_WRBUF		0x01
#define CH7322_WRBUF_LEN	0x10

#define CH7322_READ		0x40
#define CH7322_READ_NRDT		0x80
#define CH7322_READ_MSENT		0x20
#define CH7322_READ_NMASK		0x0f

/* Read buffer is 0x41-0x50 */
#define CH7322_RDBUF		0x41
#define CH7322_RDBUF_LEN	0x10

#define CH7322_MODE		0x11
#define CH7322_MODE_AUTO		0x78
#define CH7322_MODE_SW			0xb5

#define CH7322_RESET		0x12
#define CH7322_RESET_RST		0x00

#define CH7322_POWER		0x13
#define CH7322_POWER_FPD		0x04

#define CH7322_CFG0		0x17
#define CH7322_CFG0_EOBEN		0x40
#define CH7322_CFG0_PEOB		0x20
#define CH7322_CFG0_CLRSPP		0x10
#define CH7322_CFG0_FLOW		0x08

#define CH7322_CFG1		0x1a
#define CH7322_CFG1_STDBYO		0x04
#define CH7322_CFG1_HPBP		0x02
#define CH7322_CFG1_PIO			0x01

#define CH7322_INTCTL		0x1b
#define CH7322_INTCTL_INTPB		0x80
#define CH7322_INTCTL_STDBY		0x40
#define CH7322_INTCTL_HPDFALL		0x20
#define CH7322_INTCTL_HPDRISE		0x10
#define CH7322_INTCTL_RXMSG		0x08
#define CH7322_INTCTL_TXMSG		0x04
#define CH7322_INTCTL_NEWPHA		0x02
#define CH7322_INTCTL_ERROR		0x01

#define CH7322_DVCLKFNH	0x1d
#define CH7322_DVCLKFNL	0x1e

#define CH7322_CTL		0x31
#define CH7322_CTL_FSTDBY		0x80
#define CH7322_CTL_PLSEN		0x40
#define CH7322_CTL_PLSPB		0x20
#define CH7322_CTL_SPADL		0x10
#define CH7322_CTL_HINIT		0x08
#define CH7322_CTL_WPHYA		0x04
#define CH7322_CTL_H1T			0x02
#define CH7322_CTL_S1T			0x01

#define CH7322_PAWH		0x32
#define CH7322_PAWL		0x33

#define CH7322_ADDLW		0x34
#define CH7322_ADDLW_MASK	0xf0

#define CH7322_ADDLR		0x3d
#define CH7322_ADDLR_HPD		0x80
#define CH7322_ADDLR_MASK		0x0f

#define CH7322_INTDATA		0x3e
#define CH7322_INTDATA_MODE		0x80
#define CH7322_INTDATA_STDBY		0x40
#define CH7322_INTDATA_HPDFALL		0x20
#define CH7322_INTDATA_HPDRISE		0x10
#define CH7322_INTDATA_RXMSG		0x08
#define CH7322_INTDATA_TXMSG		0x04
#define CH7322_INTDATA_NEWPHA		0x02
#define CH7322_INTDATA_ERROR		0x01

#define CH7322_EVENT		0x3f
#define CH7322_EVENT_TXERR		0x80
#define CH7322_EVENT_HRST		0x40
#define CH7322_EVENT_HFST		0x20
#define CH7322_EVENT_PHACHG		0x10
#define CH7322_EVENT_ACTST		0x08
#define CH7322_EVENT_PHARDY		0x04
#define CH7322_EVENT_BSOK		0x02
#define CH7322_EVENT_ERRADCF		0x01

#define CH7322_DID		0x51
#define CH7322_DID_CH7322		0x5b
#define CH7322_DID_CH7323		0x5f

#define CH7322_REVISIONID	0x52

#define CH7322_PARH		0x53
#define CH7322_PARL		0x54

#define CH7322_IOCFG2		0x75
#define CH7322_IOCFG_CIO		0x80
#define CH7322_IOCFG_IOCFGMASK		0x78
#define CH7322_IOCFG_AUDIO		0x04
#define CH7322_IOCFG_SPAMST		0x02
#define CH7322_IOCFG_SPAMSP		0x01

#define CH7322_CTL3		0x7b
#define CH7322_CTL3_SWENA		0x80
#define CH7322_CTL3_FC_INIT		0x40
#define CH7322_CTL3_SML_FL		0x20
#define CH7322_CTL3_SM_RDST		0x10
#define CH7322_CTL3_SPP_CIAH		0x08
#define CH7322_CTL3_SPP_CIAL		0x04
#define CH7322_CTL3_SPP_ACTH		0x02
#define CH7322_CTL3_SPP_ACTL		0x01

/* BOK status means NACK */
#define CH7322_TX_FLAG_NACK	BIT(0)
/* Device will retry automatically */
#define CH7322_TX_FLAG_RETRY	BIT(1)

struct ch7322 {
	struct i2c_client *i2c;
	struct regmap *regmap;
	struct cec_adapter *cec;
	struct mutex mutex;	/* device access mutex */
	u8 tx_flags;
};

static const struct regmap_config ch7322_regmap = {
	.reg_bits = 8,
	.val_bits = 8,
	.max_register = 0x7f,
	.disable_locking = true,
};

static int ch7322_send_message(struct ch7322 *ch7322, const struct cec_msg *msg)
{
	unsigned int val;
	unsigned int len = msg->len;
	int ret;
	int i;

	WARN_ON(!mutex_is_locked(&ch7322->mutex));

	if (len > CH7322_WRBUF_LEN || len < 1)
		return -EINVAL;

	ret = regmap_read(ch7322->regmap, CH7322_WRITE, &val);
	if (ret)
		return ret;

	/* Buffer not ready */
	if (!(val & CH7322_WRITE_MSENT))
		return -EBUSY;

	if (cec_msg_opcode(msg) == -1 &&
	    cec_msg_initiator(msg) == cec_msg_destination(msg)) {
		ch7322->tx_flags = CH7322_TX_FLAG_NACK | CH7322_TX_FLAG_RETRY;
	} else if (cec_msg_is_broadcast(msg)) {
		ch7322->tx_flags = CH7322_TX_FLAG_NACK;
	} else {
		ch7322->tx_flags = CH7322_TX_FLAG_RETRY;
	}

	ret = regmap_write(ch7322->regmap, CH7322_WRITE, len - 1);
	if (ret)
		return ret;

	for (i = 0; i < len; i++) {
		ret = regmap_write(ch7322->regmap,
				   CH7322_WRBUF + i, msg->msg[i]);
		if (ret)
			return ret;
	}

	return 0;
}

static int ch7322_receive_message(struct ch7322 *ch7322, struct cec_msg *msg)
{
	unsigned int val;
	int ret = 0;
	int i;

	WARN_ON(!mutex_is_locked(&ch7322->mutex));

	ret = regmap_read(ch7322->regmap, CH7322_READ, &val);
	if (ret)
		return ret;

	/* Message not ready */
	if (!(val & CH7322_READ_NRDT))
		return -EIO;

	msg->len = (val & CH7322_READ_NMASK) + 1;

	/* Read entire RDBUF to clear state */
	for (i = 0; i < CH7322_RDBUF_LEN; i++) {
		ret = regmap_read(ch7322->regmap, CH7322_RDBUF + i, &val);
		if (ret)
			return ret;
		msg->msg[i] = (u8)val;
	}

	return 0;
}

static void ch7322_tx_done(struct ch7322 *ch7322)
{
	int ret;
	unsigned int val;
	u8 status, flags;

	mutex_lock(&ch7322->mutex);
	ret = regmap_read(ch7322->regmap, CH7322_WRITE, &val);
	flags = ch7322->tx_flags;
	mutex_unlock(&ch7322->mutex);

	/*
	 * The device returns a one-bit OK status which usually means ACK but
	 * actually means NACK when sending a logical address query or a
	 * broadcast.
	 */
	if (ret)
		status = CEC_TX_STATUS_ERROR;
	else if ((val & CH7322_WRITE_BOK) && (flags & CH7322_TX_FLAG_NACK))
		status = CEC_TX_STATUS_NACK;
	else if (val & CH7322_WRITE_BOK)
		status = CEC_TX_STATUS_OK;
	else if (flags & CH7322_TX_FLAG_NACK)
		status = CEC_TX_STATUS_OK;
	else
		status = CEC_TX_STATUS_NACK;

	if (status == CEC_TX_STATUS_NACK && (flags & CH7322_TX_FLAG_RETRY))
		status |= CEC_TX_STATUS_MAX_RETRIES;

	cec_transmit_attempt_done(ch7322->cec, status);
}

static void ch7322_rx_done(struct ch7322 *ch7322)
{
	struct cec_msg msg;
	int ret;

	mutex_lock(&ch7322->mutex);
	ret = ch7322_receive_message(ch7322, &msg);
	mutex_unlock(&ch7322->mutex);

	if (ret)
		dev_err(&ch7322->i2c->dev, "cec receive error: %d\n", ret);
	else
		cec_received_msg(ch7322->cec, &msg);
}

/*
 * This device can either monitor the DDC lines to obtain the physical address
 * or it can allow the host to program it. This driver lets the device obtain
 * it.
 */
static void ch7322_phys_addr(struct ch7322 *ch7322)
{
	unsigned int pah, pal;
	int ret = 0;

	mutex_lock(&ch7322->mutex);
	ret |= regmap_read(ch7322->regmap, CH7322_PARH, &pah);
	ret |= regmap_read(ch7322->regmap, CH7322_PARL, &pal);
	mutex_unlock(&ch7322->mutex);

	if (ret)
		dev_err(&ch7322->i2c->dev, "phys addr error\n");
	else
		cec_s_phys_addr(ch7322->cec, pal | (pah << 8), false);
}

static irqreturn_t ch7322_irq(int irq, void *dev)
{
	struct ch7322 *ch7322 = dev;
	unsigned int data = 0;

	mutex_lock(&ch7322->mutex);
	regmap_read(ch7322->regmap, CH7322_INTDATA, &data);
	regmap_write(ch7322->regmap, CH7322_INTDATA, data);
	mutex_unlock(&ch7322->mutex);

	if (data & CH7322_INTDATA_HPDFALL)
		cec_phys_addr_invalidate(ch7322->cec);

	if (data & CH7322_INTDATA_TXMSG)
		ch7322_tx_done(ch7322);

	if (data & CH7322_INTDATA_RXMSG)
		ch7322_rx_done(ch7322);

	if (data & CH7322_INTDATA_NEWPHA)
		ch7322_phys_addr(ch7322);

	if (data & CH7322_INTDATA_ERROR)
		dev_dbg(&ch7322->i2c->dev, "unknown error\n");

	return IRQ_HANDLED;
}

/* This device is always enabled */
static int ch7322_cec_adap_enable(struct cec_adapter *adap, bool enable)
{
	return 0;
}

static int ch7322_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
{
	struct ch7322 *ch7322 = cec_get_drvdata(adap);
	int ret;

	mutex_lock(&ch7322->mutex);
	ret = regmap_update_bits(ch7322->regmap, CH7322_ADDLW,
				 CH7322_ADDLW_MASK, log_addr << 4);
	mutex_unlock(&ch7322->mutex);

	return ret;
}

static int ch7322_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
				    u32 signal_free_time, struct cec_msg *msg)
{
	struct ch7322 *ch7322 = cec_get_drvdata(adap);
	int ret;

	mutex_lock(&ch7322->mutex);
	ret = ch7322_send_message(ch7322, msg);
	mutex_unlock(&ch7322->mutex);

	return ret;
}

static const struct cec_adap_ops ch7322_cec_adap_ops = {
	.adap_enable = ch7322_cec_adap_enable,
	.adap_log_addr = ch7322_cec_adap_log_addr,
	.adap_transmit = ch7322_cec_adap_transmit,
};

#if IS_ENABLED(CONFIG_PCI) && IS_ENABLED(CONFIG_DMI)

struct ch7322_conn_match {
	const char *dev_name;
	const char *pci_name;
	const char *port_name;
};

static struct ch7322_conn_match google_endeavour[] = {
	{ "i2c-PRP0001:00", "0000:00:02.0", "Port B" },
	{ "i2c-PRP0001:01", "0000:00:02.0", "Port C" },
	{ },
};

static const struct dmi_system_id ch7322_dmi_table[] = {
	{
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Google"),
			DMI_MATCH(DMI_BOARD_NAME, "Endeavour"),
		},
		.driver_data = google_endeavour,
	},
	{ },
};

/* Make a best-effort attempt to locate a matching HDMI port */
static int ch7322_get_port(struct i2c_client *client,
			   struct device **dev,
			   const char **port)
{
	const struct dmi_system_id *system;
	const struct ch7322_conn_match *conn;

	*dev = NULL;
	*port = NULL;

	system = dmi_first_match(ch7322_dmi_table);
	if (!system)
		return 0;

	for (conn = system->driver_data; conn->dev_name; conn++) {
		if (!strcmp(dev_name(&client->dev), conn->dev_name)) {
			struct device *d;

			d = bus_find_device_by_name(&pci_bus_type, NULL,
						    conn->pci_name);
			if (!d)
				return -EPROBE_DEFER;

			put_device(d);

			*dev = d;
			*port = conn->port_name;

			return 0;
		}
	}

	return 0;
}

#else

static int ch7322_get_port(struct i2c_client *client,
			   struct device **dev,
			   const char **port)
{
	*dev = NULL;
	*port = NULL;

	return 0;
}

#endif

static int ch7322_probe(struct i2c_client *client)
{
	struct device *hdmi_dev;
	const char *port_name;
	struct ch7322 *ch7322;
	struct cec_notifier *notifier = NULL;
	u32 caps = CEC_CAP_DEFAULTS;
	int ret;
	unsigned int val;

	ret = ch7322_get_port(client, &hdmi_dev, &port_name);
	if (ret)
		return ret;

	if (hdmi_dev)
		caps |= CEC_CAP_CONNECTOR_INFO;

	ch7322 = devm_kzalloc(&client->dev, sizeof(*ch7322), GFP_KERNEL);
	if (!ch7322)
		return -ENOMEM;

	ch7322->regmap = devm_regmap_init_i2c(client, &ch7322_regmap);
	if (IS_ERR(ch7322->regmap))
		return PTR_ERR(ch7322->regmap);

	ret = regmap_read(ch7322->regmap, CH7322_DID, &val);
	if (ret)
		return ret;

	if (val != CH7322_DID_CH7322)
		return -EOPNOTSUPP;

	mutex_init(&ch7322->mutex);
	ch7322->i2c = client;
	ch7322->tx_flags = 0;

	i2c_set_clientdata(client, ch7322);

	/* Disable auto mode */
	ret = regmap_write(ch7322->regmap, CH7322_MODE, CH7322_MODE_SW);
	if (ret)
		goto err_mutex;

	/* Enable logical address register */
	ret = regmap_update_bits(ch7322->regmap, CH7322_CTL,
				 CH7322_CTL_SPADL, CH7322_CTL_SPADL);
	if (ret)
		goto err_mutex;

	ch7322->cec = cec_allocate_adapter(&ch7322_cec_adap_ops, ch7322,
					   dev_name(&client->dev),
					   caps, 1);

	if (IS_ERR(ch7322->cec)) {
		ret = PTR_ERR(ch7322->cec);
		goto err_mutex;
	}

	ch7322->cec->adap_controls_phys_addr = true;

	if (hdmi_dev) {
		notifier = cec_notifier_cec_adap_register(hdmi_dev,
							  port_name,
							  ch7322->cec);
		if (!notifier) {
			ret = -ENOMEM;
			goto err_cec;
		}
	}

	/* Configure, mask, and clear interrupt */
	ret = regmap_write(ch7322->regmap, CH7322_CFG1, 0);
	if (ret)
		goto err_notifier;
	ret = regmap_write(ch7322->regmap, CH7322_INTCTL, CH7322_INTCTL_INTPB);
	if (ret)
		goto err_notifier;
	ret = regmap_write(ch7322->regmap, CH7322_INTDATA, 0xff);
	if (ret)
		goto err_notifier;

	/* If HPD is up read physical address */
	ret = regmap_read(ch7322->regmap, CH7322_ADDLR, &val);
	if (ret)
		goto err_notifier;
	if (val & CH7322_ADDLR_HPD)
		ch7322_phys_addr(ch7322);

	ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
					ch7322_irq,
					IRQF_ONESHOT | IRQF_TRIGGER_RISING,
					client->name, ch7322);
	if (ret)
		goto err_notifier;

	/* Unmask interrupt */
	mutex_lock(&ch7322->mutex);
	ret = regmap_write(ch7322->regmap, CH7322_INTCTL, 0xff);
	mutex_unlock(&ch7322->mutex);

	if (ret)
		goto err_notifier;

	ret = cec_register_adapter(ch7322->cec, &client->dev);
	if (ret)
		goto err_notifier;

	dev_info(&client->dev, "device registered\n");

	return 0;

err_notifier:
	if (notifier)
		cec_notifier_cec_adap_unregister(notifier, ch7322->cec);
err_cec:
	cec_delete_adapter(ch7322->cec);
err_mutex:
	mutex_destroy(&ch7322->mutex);
	return ret;
}

static int ch7322_remove(struct i2c_client *client)
{
	struct ch7322 *ch7322 = i2c_get_clientdata(client);

	/* Mask interrupt */
	mutex_lock(&ch7322->mutex);
	regmap_write(ch7322->regmap, CH7322_INTCTL, CH7322_INTCTL_INTPB);
	mutex_unlock(&ch7322->mutex);

	cec_unregister_adapter(ch7322->cec);
	mutex_destroy(&ch7322->mutex);

	dev_info(&client->dev, "device unregistered\n");

	return 0;
}

static const struct of_device_id ch7322_of_match[] = {
	{ .compatible = "chrontel,ch7322", },
	{},
};
MODULE_DEVICE_TABLE(of, ch7322_of_match);

static struct i2c_driver ch7322_i2c_driver = {
	.driver = {
		.name = "ch7322",
		.of_match_table = of_match_ptr(ch7322_of_match),
	},
	.probe_new	= ch7322_probe,
	.remove		= ch7322_remove,
};

module_i2c_driver(ch7322_i2c_driver);

MODULE_DESCRIPTION("Chrontel CH7322 CEC Controller Driver");
MODULE_AUTHOR("Jeff Chase <jnchase@google.com>");
MODULE_LICENSE("GPL");