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