blob: 8941f58bed7f042ce5cce8a97bbfeb33ef20bd79 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 I2C functions
3 *
4 * Derived from ivtv-i2c.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls1ed9dcc2008-11-22 01:37:34 -03007 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030026#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#include "cx18-cards.h"
28#include "cx18-gpio.h"
29#include "cx18-av-core.h"
Adrian Bunk50510992008-05-05 18:25:22 -030030#include "cx18-i2c.h"
Andy Wallsced07372008-11-02 10:59:04 -030031#include "cx18-irq.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030032
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030033#define CX18_REG_I2C_1_WR 0xf15000
34#define CX18_REG_I2C_1_RD 0xf15008
35#define CX18_REG_I2C_2_WR 0xf25100
36#define CX18_REG_I2C_2_RD 0xf25108
37
38#define SETSCL_BIT 0x0001
39#define SETSDL_BIT 0x0002
40#define GETSCL_BIT 0x0004
41#define GETSDL_BIT 0x0008
42
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030043#define CX18_CS5345_I2C_ADDR 0x4c
44
45/* This array should match the CX18_HW_ defines */
46static const u8 hw_driverids[] = {
47 I2C_DRIVERID_TUNER,
48 I2C_DRIVERID_TVEEPROM,
49 I2C_DRIVERID_CS5345,
50 0, /* CX18_HW_GPIO dummy driver ID */
51 0 /* CX18_HW_CX23418 dummy driver ID */
52};
53
54/* This array should match the CX18_HW_ defines */
55static const u8 hw_addrs[] = {
56 0,
57 0,
58 CX18_CS5345_I2C_ADDR,
59 0, /* CX18_HW_GPIO dummy driver ID */
60 0, /* CX18_HW_CX23418 dummy driver ID */
61};
62
63/* This array should match the CX18_HW_ defines */
64/* This might well become a card-specific array */
65static const u8 hw_bus[] = {
66 0,
67 0,
68 0,
69 0, /* CX18_HW_GPIO dummy driver ID */
70 0, /* CX18_HW_CX23418 dummy driver ID */
71};
72
73/* This array should match the CX18_HW_ defines */
Jean Delvareaf294862008-05-18 20:49:40 +020074static const char * const hw_devicenames[] = {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030075 "tuner",
76 "tveeprom",
77 "cs5345",
78 "gpio",
79 "cx23418",
80};
81
82int cx18_i2c_register(struct cx18 *cx, unsigned idx)
83{
84 struct i2c_board_info info;
85 struct i2c_client *c;
86 u8 id, bus;
87 int i;
88
89 CX18_DEBUG_I2C("i2c client register\n");
90 if (idx >= ARRAY_SIZE(hw_driverids) || hw_driverids[idx] == 0)
91 return -1;
92 id = hw_driverids[idx];
93 bus = hw_bus[idx];
94 memset(&info, 0, sizeof(info));
Jean Delvareaf294862008-05-18 20:49:40 +020095 strlcpy(info.type, hw_devicenames[idx], sizeof(info.type));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030096 info.addr = hw_addrs[idx];
97 for (i = 0; i < I2C_CLIENTS_MAX; i++)
98 if (cx->i2c_clients[i] == NULL)
99 break;
100
101 if (i == I2C_CLIENTS_MAX) {
102 CX18_ERR("insufficient room for new I2C client!\n");
103 return -ENOMEM;
104 }
105
106 if (id != I2C_DRIVERID_TUNER) {
107 c = i2c_new_device(&cx->i2c_adap[bus], &info);
108 if (c->driver == NULL)
109 i2c_unregister_device(c);
110 else
111 cx->i2c_clients[i] = c;
112 return cx->i2c_clients[i] ? 0 : -ENODEV;
113 }
114
115 /* special tuner handling */
116 c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->radio);
117 if (c && c->driver == NULL)
118 i2c_unregister_device(c);
119 else if (c)
120 cx->i2c_clients[i++] = c;
121 c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->demod);
122 if (c && c->driver == NULL)
123 i2c_unregister_device(c);
124 else if (c)
125 cx->i2c_clients[i++] = c;
126 c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->tv);
127 if (c && c->driver == NULL)
128 i2c_unregister_device(c);
129 else if (c)
130 cx->i2c_clients[i++] = c;
131 return 0;
132}
133
134static int attach_inform(struct i2c_client *client)
135{
136 return 0;
137}
138
139static int detach_inform(struct i2c_client *client)
140{
141 int i;
142 struct cx18 *cx = (struct cx18 *)i2c_get_adapdata(client->adapter);
143
144 CX18_DEBUG_I2C("i2c client detach\n");
145 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
146 if (cx->i2c_clients[i] == client) {
147 cx->i2c_clients[i] = NULL;
148 break;
149 }
150 }
151 CX18_DEBUG_I2C("i2c detach [client=%s,%s]\n",
152 client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed");
153
154 return 0;
155}
156
157static void cx18_setscl(void *data, int state)
158{
159 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
160 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
161 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
Andy Wallsb1526422008-08-30 16:03:44 -0300162 u32 r = cx18_read_reg(cx, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300163
164 if (state)
Andy Walls3f75c612008-11-16 23:33:41 -0300165 cx18_write_reg(cx, r | SETSCL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300166 else
Andy Walls3f75c612008-11-16 23:33:41 -0300167 cx18_write_reg(cx, r & ~SETSCL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300168}
169
170static void cx18_setsda(void *data, int state)
171{
172 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
173 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
174 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
Andy Wallsb1526422008-08-30 16:03:44 -0300175 u32 r = cx18_read_reg(cx, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300176
177 if (state)
Andy Walls3f75c612008-11-16 23:33:41 -0300178 cx18_write_reg(cx, r | SETSDL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300179 else
Andy Walls3f75c612008-11-16 23:33:41 -0300180 cx18_write_reg(cx, r & ~SETSDL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300181}
182
183static int cx18_getscl(void *data)
184{
185 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
186 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
187 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
188
Andy Wallsb1526422008-08-30 16:03:44 -0300189 return cx18_read_reg(cx, addr) & GETSCL_BIT;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300190}
191
192static int cx18_getsda(void *data)
193{
194 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
195 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
196 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
197
Andy Wallsb1526422008-08-30 16:03:44 -0300198 return cx18_read_reg(cx, addr) & GETSDL_BIT;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300199}
200
201/* template for i2c-bit-algo */
202static struct i2c_adapter cx18_i2c_adap_template = {
203 .name = "cx18 i2c driver",
204 .id = I2C_HW_B_CX2341X,
205 .algo = NULL, /* set by i2c-algo-bit */
206 .algo_data = NULL, /* filled from template */
207 .client_register = attach_inform,
208 .client_unregister = detach_inform,
209 .owner = THIS_MODULE,
210};
211
212#define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
213#define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
214
215static struct i2c_algo_bit_data cx18_i2c_algo_template = {
216 .setsda = cx18_setsda,
217 .setscl = cx18_setscl,
218 .getsda = cx18_getsda,
219 .getscl = cx18_getscl,
220 .udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/
221 .timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
222};
223
224static struct i2c_client cx18_i2c_client_template = {
225 .name = "cx18 internal",
226};
227
228int cx18_call_i2c_client(struct cx18 *cx, int addr, unsigned cmd, void *arg)
229{
230 struct i2c_client *client;
231 int retval;
232 int i;
233
234 CX18_DEBUG_I2C("call_i2c_client addr=%02x\n", addr);
235 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
236 client = cx->i2c_clients[i];
237 if (client == NULL || client->driver == NULL ||
238 client->driver->command == NULL)
239 continue;
240 if (addr == client->addr) {
241 retval = client->driver->command(client, cmd, arg);
242 return retval;
243 }
244 }
245 if (cmd != VIDIOC_G_CHIP_IDENT)
246 CX18_ERR("i2c addr 0x%02x not found for cmd 0x%x!\n",
247 addr, cmd);
248 return -ENODEV;
249}
250
251/* Find the i2c device based on the driver ID and return
252 its i2c address or -ENODEV if no matching device was found. */
253static int cx18_i2c_id_addr(struct cx18 *cx, u32 id)
254{
255 struct i2c_client *client;
256 int retval = -ENODEV;
257 int i;
258
259 for (i = 0; i < I2C_CLIENTS_MAX; i++) {
260 client = cx->i2c_clients[i];
261 if (client == NULL || client->driver == NULL)
262 continue;
263 if (id == client->driver->id) {
264 retval = client->addr;
265 break;
266 }
267 }
268 return retval;
269}
270
271/* Find the i2c device name matching the DRIVERID */
272static const char *cx18_i2c_id_name(u32 id)
273{
274 int i;
275
276 for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
277 if (hw_driverids[i] == id)
Jean Delvareaf294862008-05-18 20:49:40 +0200278 return hw_devicenames[i];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300279 return "unknown device";
280}
281
282/* Find the i2c device name matching the CX18_HW_ flag */
283static const char *cx18_i2c_hw_name(u32 hw)
284{
285 int i;
286
287 for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
288 if (1 << i == hw)
Jean Delvareaf294862008-05-18 20:49:40 +0200289 return hw_devicenames[i];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300290 return "unknown device";
291}
292
293/* Find the i2c device matching the CX18_HW_ flag and return
294 its i2c address or -ENODEV if no matching device was found. */
295int cx18_i2c_hw_addr(struct cx18 *cx, u32 hw)
296{
297 int i;
298
299 for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
300 if (1 << i == hw)
301 return cx18_i2c_id_addr(cx, hw_driverids[i]);
302 return -ENODEV;
303}
304
305/* Calls i2c device based on CX18_HW_ flag. If hw == 0, then do nothing.
306 If hw == CX18_HW_GPIO then call the gpio handler. */
307int cx18_i2c_hw(struct cx18 *cx, u32 hw, unsigned int cmd, void *arg)
308{
309 int addr;
310
Sri Deevi03c28082008-06-21 11:06:44 -0300311 if (hw == 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300312 return 0;
Sri Deevi03c28082008-06-21 11:06:44 -0300313
314 if (hw == CX18_HW_GPIO)
315 return cx18_gpio(cx, cmd, arg);
316
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300317 if (hw == CX18_HW_CX23418)
318 return cx18_av_cmd(cx, cmd, arg);
319
320 addr = cx18_i2c_hw_addr(cx, hw);
321 if (addr < 0) {
322 CX18_ERR("i2c hardware 0x%08x (%s) not found for cmd 0x%x!\n",
323 hw, cx18_i2c_hw_name(hw), cmd);
324 return addr;
325 }
326 return cx18_call_i2c_client(cx, addr, cmd, arg);
327}
328
329/* Calls i2c device based on I2C driver ID. */
330int cx18_i2c_id(struct cx18 *cx, u32 id, unsigned int cmd, void *arg)
331{
332 int addr;
333
334 addr = cx18_i2c_id_addr(cx, id);
335 if (addr < 0) {
336 if (cmd != VIDIOC_G_CHIP_IDENT)
337 CX18_ERR("i2c ID 0x%08x (%s) not found for cmd 0x%x!\n",
338 id, cx18_i2c_id_name(id), cmd);
339 return addr;
340 }
341 return cx18_call_i2c_client(cx, addr, cmd, arg);
342}
343
344/* broadcast cmd for all I2C clients and for the gpio subsystem */
345void cx18_call_i2c_clients(struct cx18 *cx, unsigned int cmd, void *arg)
346{
347 if (cx->i2c_adap[0].algo == NULL || cx->i2c_adap[1].algo == NULL) {
348 CX18_ERR("adapter is not set\n");
349 return;
350 }
351 cx18_av_cmd(cx, cmd, arg);
352 i2c_clients_command(&cx->i2c_adap[0], cmd, arg);
353 i2c_clients_command(&cx->i2c_adap[1], cmd, arg);
Sri Deevi03c28082008-06-21 11:06:44 -0300354 if (cx->hw_flags & CX18_HW_GPIO)
355 cx18_gpio(cx, cmd, arg);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300356}
357
358/* init + register i2c algo-bit adapter */
359int init_cx18_i2c(struct cx18 *cx)
360{
361 int i;
362 CX18_DEBUG_I2C("i2c init\n");
363
Sri Deevi03c28082008-06-21 11:06:44 -0300364 /* Sanity checks for the I2C hardware arrays. They must be the
365 * same size and GPIO/CX23418 must be the last entries.
366 */
367 if (ARRAY_SIZE(hw_driverids) != ARRAY_SIZE(hw_addrs) ||
368 ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs) ||
369 CX18_HW_GPIO != (1 << (ARRAY_SIZE(hw_addrs) - 2)) ||
370 CX18_HW_CX23418 != (1 << (ARRAY_SIZE(hw_addrs) - 1)) ||
371 hw_driverids[ARRAY_SIZE(hw_addrs) - 1]) {
372 CX18_ERR("Mismatched I2C hardware arrays\n");
373 return -ENODEV;
374 }
375
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300376 for (i = 0; i < 2; i++) {
377 memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
378 sizeof(struct i2c_adapter));
379 memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
380 sizeof(struct i2c_algo_bit_data));
381 cx->i2c_algo_cb_data[i].cx = cx;
382 cx->i2c_algo_cb_data[i].bus_index = i;
383 cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
384 cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
385
386 sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
387 " #%d-%d", cx->num, i);
388 i2c_set_adapdata(&cx->i2c_adap[i], cx);
389
390 memcpy(&cx->i2c_client[i], &cx18_i2c_client_template,
391 sizeof(struct i2c_client));
392 sprintf(cx->i2c_client[i].name +
393 strlen(cx->i2c_client[i].name), "%d", i);
394 cx->i2c_client[i].adapter = &cx->i2c_adap[i];
395 cx->i2c_adap[i].dev.parent = &cx->dev->dev;
396 }
397
Andy Wallsb1526422008-08-30 16:03:44 -0300398 if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300399 /* Reset/Unreset I2C hardware block */
Andy Wallsb1526422008-08-30 16:03:44 -0300400 /* Clock select 220MHz */
Andy Wallsced07372008-11-02 10:59:04 -0300401 cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
402 0x00000000, 0x10001000);
Andy Wallsb1526422008-08-30 16:03:44 -0300403 /* Clock Enable */
Andy Wallsced07372008-11-02 10:59:04 -0300404 cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
405 0x00001000, 0x10001000);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300406 }
407 /* courtesy of Steven Toth <stoth@hauppauge.com> */
Andy Wallsced07372008-11-02 10:59:04 -0300408 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300409 mdelay(10);
Andy Wallsced07372008-11-02 10:59:04 -0300410 cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300411 mdelay(10);
Andy Wallsced07372008-11-02 10:59:04 -0300412 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
Andy Walls53ad02e2008-07-06 19:36:52 -0300413 mdelay(10);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300414
Andy Wallsb1526422008-08-30 16:03:44 -0300415 /* Set to edge-triggered intrs. */
Andy Wallsced07372008-11-02 10:59:04 -0300416 cx18_write_reg(cx, 0x00c00000, 0xc730c8);
Andy Wallsb1526422008-08-30 16:03:44 -0300417 /* Clear any stale intrs */
Andy Wallsced07372008-11-02 10:59:04 -0300418 cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
419 ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300420
421 /* Hw I2C1 Clock Freq ~100kHz */
Andy Walls3f75c612008-11-16 23:33:41 -0300422 cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300423 cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
424 cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
425
426 /* Hw I2C2 Clock Freq ~100kHz */
Andy Walls3f75c612008-11-16 23:33:41 -0300427 cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300428 cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
429 cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
430
Andy Walls1f09e8a2008-06-22 01:27:00 -0300431 cx18_reset_i2c_slaves_gpio(cx);
432
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300433 return i2c_bit_add_bus(&cx->i2c_adap[0]) ||
434 i2c_bit_add_bus(&cx->i2c_adap[1]);
435}
436
437void exit_cx18_i2c(struct cx18 *cx)
438{
439 int i;
440 CX18_DEBUG_I2C("i2c exit\n");
Andy Wallsb1526422008-08-30 16:03:44 -0300441 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
442 CX18_REG_I2C_1_WR);
443 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
444 CX18_REG_I2C_2_WR);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300445
446 for (i = 0; i < 2; i++) {
447 i2c_del_adapter(&cx->i2c_adap[i]);
448 }
449}
450
451/*
452 Hauppauge HVR1600 should have:
453 32 cx24227
454 98 unknown
455 a0 eeprom
456 c2 tuner
457 e? zilog ir
458 */