xref: /linux/drivers/media/pci/cx88/cx88-i2c.c (revision 52338415)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  *
5  * cx88-i2c.c  --  all the i2c code is here
6  *
7  * Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
8  *			   & Marcus Metzler (mocm@thp.uni-koeln.de)
9  * (c) 2002 Yurij Sysoev <yurij@naturesoft.net>
10  * (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
11  * (c) 2005 Mauro Carvalho Chehab <mchehab@kernel.org>
12  *	- Multituner support and i2c address binding
13  */
14 
15 #include "cx88.h"
16 
17 #include <linux/init.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 
21 #include <media/v4l2-common.h>
22 
23 static unsigned int i2c_debug;
24 module_param(i2c_debug, int, 0644);
25 MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]");
26 
27 static unsigned int i2c_scan;
28 module_param(i2c_scan, int, 0444);
29 MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time");
30 
31 static unsigned int i2c_udelay = 5;
32 module_param(i2c_udelay, int, 0644);
33 MODULE_PARM_DESC(i2c_udelay,
34 		 "i2c delay at insmod time, in usecs (should be 5 or higher). Lower value means higher bus speed.");
35 
36 #define dprintk(level, fmt, arg...) do {				\
37 	if (i2c_debug >= level)						\
38 		printk(KERN_DEBUG pr_fmt("%s: i2c:" fmt),		\
39 			__func__, ##arg);				\
40 } while (0)
41 
42 /* ----------------------------------------------------------------------- */
43 
44 static void cx8800_bit_setscl(void *data, int state)
45 {
46 	struct cx88_core *core = data;
47 
48 	if (state)
49 		core->i2c_state |= 0x02;
50 	else
51 		core->i2c_state &= ~0x02;
52 	cx_write(MO_I2C, core->i2c_state);
53 	cx_read(MO_I2C);
54 }
55 
56 static void cx8800_bit_setsda(void *data, int state)
57 {
58 	struct cx88_core *core = data;
59 
60 	if (state)
61 		core->i2c_state |= 0x01;
62 	else
63 		core->i2c_state &= ~0x01;
64 	cx_write(MO_I2C, core->i2c_state);
65 	cx_read(MO_I2C);
66 }
67 
68 static int cx8800_bit_getscl(void *data)
69 {
70 	struct cx88_core *core = data;
71 	u32 state;
72 
73 	state = cx_read(MO_I2C);
74 	return state & 0x02 ? 1 : 0;
75 }
76 
77 static int cx8800_bit_getsda(void *data)
78 {
79 	struct cx88_core *core = data;
80 	u32 state;
81 
82 	state = cx_read(MO_I2C);
83 	return state & 0x01;
84 }
85 
86 /* ----------------------------------------------------------------------- */
87 
88 static const struct i2c_algo_bit_data cx8800_i2c_algo_template = {
89 	.setsda  = cx8800_bit_setsda,
90 	.setscl  = cx8800_bit_setscl,
91 	.getsda  = cx8800_bit_getsda,
92 	.getscl  = cx8800_bit_getscl,
93 	.udelay  = 16,
94 	.timeout = 200,
95 };
96 
97 /* ----------------------------------------------------------------------- */
98 
99 static const char * const i2c_devs[128] = {
100 	[0x1c >> 1] = "lgdt330x",
101 	[0x86 >> 1] = "tda9887/cx22702",
102 	[0xa0 >> 1] = "eeprom",
103 	[0xc0 >> 1] = "tuner (analog)",
104 	[0xc2 >> 1] = "tuner (analog/dvb)",
105 	[0xc8 >> 1] = "xc5000",
106 };
107 
108 static void do_i2c_scan(const char *name, struct i2c_client *c)
109 {
110 	unsigned char buf;
111 	int i, rc;
112 
113 	for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
114 		c->addr = i;
115 		rc = i2c_master_recv(c, &buf, 0);
116 		if (rc < 0)
117 			continue;
118 		pr_info("i2c scan: found device @ 0x%x  [%s]\n",
119 			i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
120 	}
121 }
122 
123 /* init + register i2c adapter */
124 int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci)
125 {
126 	/* Prevents usage of invalid delay values */
127 	if (i2c_udelay < 5)
128 		i2c_udelay = 5;
129 
130 	core->i2c_algo = cx8800_i2c_algo_template;
131 
132 	core->i2c_adap.dev.parent = &pci->dev;
133 	strscpy(core->i2c_adap.name, core->name, sizeof(core->i2c_adap.name));
134 	core->i2c_adap.owner = THIS_MODULE;
135 	core->i2c_algo.udelay = i2c_udelay;
136 	core->i2c_algo.data = core;
137 	i2c_set_adapdata(&core->i2c_adap, &core->v4l2_dev);
138 	core->i2c_adap.algo_data = &core->i2c_algo;
139 	core->i2c_client.adapter = &core->i2c_adap;
140 	strscpy(core->i2c_client.name, "cx88xx internal", I2C_NAME_SIZE);
141 
142 	cx8800_bit_setscl(core, 1);
143 	cx8800_bit_setsda(core, 1);
144 
145 	core->i2c_rc = i2c_bit_add_bus(&core->i2c_adap);
146 	if (core->i2c_rc == 0) {
147 		static u8 tuner_data[] = {
148 			0x0b, 0xdc, 0x86, 0x52 };
149 		static struct i2c_msg tuner_msg = {
150 			.flags = 0,
151 			.addr = 0xc2 >> 1,
152 			.buf = tuner_data,
153 			.len = 4
154 		};
155 
156 		dprintk(1, "i2c register ok\n");
157 		switch (core->boardnr) {
158 		case CX88_BOARD_HAUPPAUGE_HVR1300:
159 		case CX88_BOARD_HAUPPAUGE_HVR3000:
160 		case CX88_BOARD_HAUPPAUGE_HVR4000:
161 			pr_info("i2c init: enabling analog demod on HVR1300/3000/4000 tuner\n");
162 			i2c_transfer(core->i2c_client.adapter, &tuner_msg, 1);
163 			break;
164 		default:
165 			break;
166 		}
167 		if (i2c_scan)
168 			do_i2c_scan(core->name, &core->i2c_client);
169 	} else
170 		pr_err("i2c register FAILED\n");
171 
172 	return core->i2c_rc;
173 }
174