V4L/DVB (9723): cx18: Propagate staleness of mailbox and mdl ack data to work handler
[deliverable/linux.git] / drivers / media / video / cx18 / cx18-i2c.c
CommitLineData
1c1e45d1
HV
1/*
2 * cx18 I2C functions
3 *
4 * Derived from ivtv-i2c.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
b1526421 25#include "cx18-io.h"
1c1e45d1
HV
26#include "cx18-cards.h"
27#include "cx18-gpio.h"
28#include "cx18-av-core.h"
50510993 29#include "cx18-i2c.h"
ced07371 30#include "cx18-irq.h"
1c1e45d1 31
1c1e45d1
HV
32#define CX18_REG_I2C_1_WR 0xf15000
33#define CX18_REG_I2C_1_RD 0xf15008
34#define CX18_REG_I2C_2_WR 0xf25100
35#define CX18_REG_I2C_2_RD 0xf25108
36
37#define SETSCL_BIT 0x0001
38#define SETSDL_BIT 0x0002
39#define GETSCL_BIT 0x0004
40#define GETSDL_BIT 0x0008
41
1c1e45d1
HV
42#define CX18_CS5345_I2C_ADDR 0x4c
43
44/* This array should match the CX18_HW_ defines */
45static const u8 hw_driverids[] = {
46 I2C_DRIVERID_TUNER,
47 I2C_DRIVERID_TVEEPROM,
48 I2C_DRIVERID_CS5345,
49 0, /* CX18_HW_GPIO dummy driver ID */
50 0 /* CX18_HW_CX23418 dummy driver ID */
51};
52
53/* This array should match the CX18_HW_ defines */
54static const u8 hw_addrs[] = {
55 0,
56 0,
57 CX18_CS5345_I2C_ADDR,
58 0, /* CX18_HW_GPIO dummy driver ID */
59 0, /* CX18_HW_CX23418 dummy driver ID */
60};
61
62/* This array should match the CX18_HW_ defines */
63/* This might well become a card-specific array */
64static const u8 hw_bus[] = {
65 0,
66 0,
67 0,
68 0, /* CX18_HW_GPIO dummy driver ID */
69 0, /* CX18_HW_CX23418 dummy driver ID */
70};
71
72/* This array should match the CX18_HW_ defines */
af294867 73static const char * const hw_devicenames[] = {
1c1e45d1
HV
74 "tuner",
75 "tveeprom",
76 "cs5345",
77 "gpio",
78 "cx23418",
79};
80
81int cx18_i2c_register(struct cx18 *cx, unsigned idx)
82{
83 struct i2c_board_info info;
84 struct i2c_client *c;
85 u8 id, bus;
86 int i;
87
88 CX18_DEBUG_I2C("i2c client register\n");
89 if (idx >= ARRAY_SIZE(hw_driverids) || hw_driverids[idx] == 0)
90 return -1;
91 id = hw_driverids[idx];
92 bus = hw_bus[idx];
93 memset(&info, 0, sizeof(info));
af294867 94 strlcpy(info.type, hw_devicenames[idx], sizeof(info.type));
1c1e45d1
HV
95 info.addr = hw_addrs[idx];
96 for (i = 0; i < I2C_CLIENTS_MAX; i++)
97 if (cx->i2c_clients[i] == NULL)
98 break;
99
100 if (i == I2C_CLIENTS_MAX) {
101 CX18_ERR("insufficient room for new I2C client!\n");
102 return -ENOMEM;
103 }
104
105 if (id != I2C_DRIVERID_TUNER) {
106 c = i2c_new_device(&cx->i2c_adap[bus], &info);
107 if (c->driver == NULL)
108 i2c_unregister_device(c);
109 else
110 cx->i2c_clients[i] = c;
111 return cx->i2c_clients[i] ? 0 : -ENODEV;
112 }
113
114 /* special tuner handling */
115 c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->radio);
116 if (c && c->driver == NULL)
117 i2c_unregister_device(c);
118 else if (c)
119 cx->i2c_clients[i++] = c;
120 c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->demod);
121 if (c && c->driver == NULL)
122 i2c_unregister_device(c);
123 else if (c)
124 cx->i2c_clients[i++] = c;
125 c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->tv);
126 if (c && c->driver == NULL)
127 i2c_unregister_device(c);
128 else if (c)
129 cx->i2c_clients[i++] = c;
130 return 0;
131}
132
133static int attach_inform(struct i2c_client *client)
134{
135 return 0;
136}
137
138static int detach_inform(struct i2c_client *client)
139{
140 int i;
141 struct cx18 *cx = (struct cx18 *)i2c_get_adapdata(client->adapter);
142
143 CX18_DEBUG_I2C("i2c client detach\n");
144 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
145 if (cx->i2c_clients[i] == client) {
146 cx->i2c_clients[i] = NULL;
147 break;
148 }
149 }
150 CX18_DEBUG_I2C("i2c detach [client=%s,%s]\n",
151 client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed");
152
153 return 0;
154}
155
156static void cx18_setscl(void *data, int state)
157{
158 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
159 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
160 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
b1526421 161 u32 r = cx18_read_reg(cx, addr);
1c1e45d1
HV
162
163 if (state)
b1526421 164 cx18_write_reg_sync(cx, r | SETSCL_BIT, addr);
1c1e45d1 165 else
b1526421 166 cx18_write_reg_sync(cx, r & ~SETSCL_BIT, addr);
1c1e45d1
HV
167}
168
169static void cx18_setsda(void *data, int state)
170{
171 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
172 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
173 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
b1526421 174 u32 r = cx18_read_reg(cx, addr);
1c1e45d1
HV
175
176 if (state)
b1526421 177 cx18_write_reg_sync(cx, r | SETSDL_BIT, addr);
1c1e45d1 178 else
b1526421 179 cx18_write_reg_sync(cx, r & ~SETSDL_BIT, addr);
1c1e45d1
HV
180}
181
182static int cx18_getscl(void *data)
183{
184 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
185 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
186 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
187
b1526421 188 return cx18_read_reg(cx, addr) & GETSCL_BIT;
1c1e45d1
HV
189}
190
191static int cx18_getsda(void *data)
192{
193 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
194 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
195 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
196
b1526421 197 return cx18_read_reg(cx, addr) & GETSDL_BIT;
1c1e45d1
HV
198}
199
200/* template for i2c-bit-algo */
201static struct i2c_adapter cx18_i2c_adap_template = {
202 .name = "cx18 i2c driver",
203 .id = I2C_HW_B_CX2341X,
204 .algo = NULL, /* set by i2c-algo-bit */
205 .algo_data = NULL, /* filled from template */
206 .client_register = attach_inform,
207 .client_unregister = detach_inform,
208 .owner = THIS_MODULE,
209};
210
211#define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
212#define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
213
214static struct i2c_algo_bit_data cx18_i2c_algo_template = {
215 .setsda = cx18_setsda,
216 .setscl = cx18_setscl,
217 .getsda = cx18_getsda,
218 .getscl = cx18_getscl,
219 .udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/
220 .timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
221};
222
223static struct i2c_client cx18_i2c_client_template = {
224 .name = "cx18 internal",
225};
226
227int cx18_call_i2c_client(struct cx18 *cx, int addr, unsigned cmd, void *arg)
228{
229 struct i2c_client *client;
230 int retval;
231 int i;
232
233 CX18_DEBUG_I2C("call_i2c_client addr=%02x\n", addr);
234 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
235 client = cx->i2c_clients[i];
236 if (client == NULL || client->driver == NULL ||
237 client->driver->command == NULL)
238 continue;
239 if (addr == client->addr) {
240 retval = client->driver->command(client, cmd, arg);
241 return retval;
242 }
243 }
244 if (cmd != VIDIOC_G_CHIP_IDENT)
245 CX18_ERR("i2c addr 0x%02x not found for cmd 0x%x!\n",
246 addr, cmd);
247 return -ENODEV;
248}
249
250/* Find the i2c device based on the driver ID and return
251 its i2c address or -ENODEV if no matching device was found. */
252static int cx18_i2c_id_addr(struct cx18 *cx, u32 id)
253{
254 struct i2c_client *client;
255 int retval = -ENODEV;
256 int i;
257
258 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
259 client = cx->i2c_clients[i];
260 if (client == NULL || client->driver == NULL)
261 continue;
262 if (id == client->driver->id) {
263 retval = client->addr;
264 break;
265 }
266 }
267 return retval;
268}
269
270/* Find the i2c device name matching the DRIVERID */
271static const char *cx18_i2c_id_name(u32 id)
272{
273 int i;
274
275 for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
276 if (hw_driverids[i] == id)
af294867 277 return hw_devicenames[i];
1c1e45d1
HV
278 return "unknown device";
279}
280
281/* Find the i2c device name matching the CX18_HW_ flag */
282static const char *cx18_i2c_hw_name(u32 hw)
283{
284 int i;
285
286 for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
287 if (1 << i == hw)
af294867 288 return hw_devicenames[i];
1c1e45d1
HV
289 return "unknown device";
290}
291
292/* Find the i2c device matching the CX18_HW_ flag and return
293 its i2c address or -ENODEV if no matching device was found. */
294int cx18_i2c_hw_addr(struct cx18 *cx, u32 hw)
295{
296 int i;
297
298 for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
299 if (1 << i == hw)
300 return cx18_i2c_id_addr(cx, hw_driverids[i]);
301 return -ENODEV;
302}
303
304/* Calls i2c device based on CX18_HW_ flag. If hw == 0, then do nothing.
305 If hw == CX18_HW_GPIO then call the gpio handler. */
306int cx18_i2c_hw(struct cx18 *cx, u32 hw, unsigned int cmd, void *arg)
307{
308 int addr;
309
03c28085 310 if (hw == 0)
1c1e45d1 311 return 0;
03c28085
SD
312
313 if (hw == CX18_HW_GPIO)
314 return cx18_gpio(cx, cmd, arg);
315
1c1e45d1
HV
316 if (hw == CX18_HW_CX23418)
317 return cx18_av_cmd(cx, cmd, arg);
318
319 addr = cx18_i2c_hw_addr(cx, hw);
320 if (addr < 0) {
321 CX18_ERR("i2c hardware 0x%08x (%s) not found for cmd 0x%x!\n",
322 hw, cx18_i2c_hw_name(hw), cmd);
323 return addr;
324 }
325 return cx18_call_i2c_client(cx, addr, cmd, arg);
326}
327
328/* Calls i2c device based on I2C driver ID. */
329int cx18_i2c_id(struct cx18 *cx, u32 id, unsigned int cmd, void *arg)
330{
331 int addr;
332
333 addr = cx18_i2c_id_addr(cx, id);
334 if (addr < 0) {
335 if (cmd != VIDIOC_G_CHIP_IDENT)
336 CX18_ERR("i2c ID 0x%08x (%s) not found for cmd 0x%x!\n",
337 id, cx18_i2c_id_name(id), cmd);
338 return addr;
339 }
340 return cx18_call_i2c_client(cx, addr, cmd, arg);
341}
342
343/* broadcast cmd for all I2C clients and for the gpio subsystem */
344void cx18_call_i2c_clients(struct cx18 *cx, unsigned int cmd, void *arg)
345{
346 if (cx->i2c_adap[0].algo == NULL || cx->i2c_adap[1].algo == NULL) {
347 CX18_ERR("adapter is not set\n");
348 return;
349 }
350 cx18_av_cmd(cx, cmd, arg);
351 i2c_clients_command(&cx->i2c_adap[0], cmd, arg);
352 i2c_clients_command(&cx->i2c_adap[1], cmd, arg);
03c28085
SD
353 if (cx->hw_flags & CX18_HW_GPIO)
354 cx18_gpio(cx, cmd, arg);
1c1e45d1
HV
355}
356
357/* init + register i2c algo-bit adapter */
358int init_cx18_i2c(struct cx18 *cx)
359{
360 int i;
361 CX18_DEBUG_I2C("i2c init\n");
362
03c28085
SD
363 /* Sanity checks for the I2C hardware arrays. They must be the
364 * same size and GPIO/CX23418 must be the last entries.
365 */
366 if (ARRAY_SIZE(hw_driverids) != ARRAY_SIZE(hw_addrs) ||
367 ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs) ||
368 CX18_HW_GPIO != (1 << (ARRAY_SIZE(hw_addrs) - 2)) ||
369 CX18_HW_CX23418 != (1 << (ARRAY_SIZE(hw_addrs) - 1)) ||
370 hw_driverids[ARRAY_SIZE(hw_addrs) - 1]) {
371 CX18_ERR("Mismatched I2C hardware arrays\n");
372 return -ENODEV;
373 }
374
1c1e45d1
HV
375 for (i = 0; i < 2; i++) {
376 memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
377 sizeof(struct i2c_adapter));
378 memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
379 sizeof(struct i2c_algo_bit_data));
380 cx->i2c_algo_cb_data[i].cx = cx;
381 cx->i2c_algo_cb_data[i].bus_index = i;
382 cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
383 cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
384
385 sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
386 " #%d-%d", cx->num, i);
387 i2c_set_adapdata(&cx->i2c_adap[i], cx);
388
389 memcpy(&cx->i2c_client[i], &cx18_i2c_client_template,
390 sizeof(struct i2c_client));
391 sprintf(cx->i2c_client[i].name +
392 strlen(cx->i2c_client[i].name), "%d", i);
393 cx->i2c_client[i].adapter = &cx->i2c_adap[i];
394 cx->i2c_adap[i].dev.parent = &cx->dev->dev;
395 }
396
b1526421 397 if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
1c1e45d1 398 /* Reset/Unreset I2C hardware block */
b1526421 399 /* Clock select 220MHz */
ced07371
AW
400 cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
401 0x00000000, 0x10001000);
b1526421 402 /* Clock Enable */
ced07371
AW
403 cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
404 0x00001000, 0x10001000);
1c1e45d1
HV
405 }
406 /* courtesy of Steven Toth <stoth@hauppauge.com> */
ced07371
AW
407 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
408 if (!cx18_retry_mmio)
409 (void) cx18_read_reg(cx, 0xc7001c); /* sync */
1c1e45d1 410 mdelay(10);
ced07371
AW
411 cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
412 if (!cx18_retry_mmio)
413 (void) cx18_read_reg(cx, 0xc7001c); /* sync */
1c1e45d1 414 mdelay(10);
ced07371
AW
415 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
416 if (!cx18_retry_mmio)
417 (void) cx18_read_reg(cx, 0xc7001c); /* sync */
53ad02ef 418 mdelay(10);
1c1e45d1 419
b1526421 420 /* Set to edge-triggered intrs. */
ced07371 421 cx18_write_reg(cx, 0x00c00000, 0xc730c8);
b1526421 422 /* Clear any stale intrs */
ced07371
AW
423 cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
424 ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
1c1e45d1
HV
425
426 /* Hw I2C1 Clock Freq ~100kHz */
b1526421 427 cx18_write_reg_sync(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
1c1e45d1
HV
428 cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
429 cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
430
431 /* Hw I2C2 Clock Freq ~100kHz */
b1526421 432 cx18_write_reg_sync(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
1c1e45d1
HV
433 cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
434 cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
435
1f09e8a2
AW
436 cx18_reset_i2c_slaves_gpio(cx);
437
1c1e45d1
HV
438 return i2c_bit_add_bus(&cx->i2c_adap[0]) ||
439 i2c_bit_add_bus(&cx->i2c_adap[1]);
440}
441
442void exit_cx18_i2c(struct cx18 *cx)
443{
444 int i;
445 CX18_DEBUG_I2C("i2c exit\n");
b1526421
AW
446 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
447 CX18_REG_I2C_1_WR);
448 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
449 CX18_REG_I2C_2_WR);
1c1e45d1
HV
450
451 for (i = 0; i < 2; i++) {
452 i2c_del_adapter(&cx->i2c_adap[i]);
453 }
454}
455
456/*
457 Hauppauge HVR1600 should have:
458 32 cx24227
459 98 unknown
460 a0 eeprom
461 c2 tuner
462 e? zilog ir
463 */
This page took 0.130216 seconds and 5 git commands to generate.