Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / media / video / cx18 / cx18-i2c.c
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"
25 #include "cx18-cards.h"
26 #include "cx18-gpio.h"
27 #include "cx18-av-core.h"
28 #include "cx18-i2c.h"
29
30 #include <media/ir-kbd-i2c.h>
31
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
42 #ifndef I2C_ADAP_CLASS_TV_ANALOG
43 #define I2C_ADAP_CLASS_TV_ANALOG I2C_CLASS_TV_ANALOG
44 #endif
45
46 #define CX18_CS5345_I2C_ADDR            0x4c
47
48 /* This array should match the CX18_HW_ defines */
49 static const u8 hw_driverids[] = {
50         I2C_DRIVERID_TUNER,
51         I2C_DRIVERID_TVEEPROM,
52         I2C_DRIVERID_CS5345,
53         0,              /* CX18_HW_GPIO dummy driver ID */
54         0               /* CX18_HW_CX23418 dummy driver ID */
55 };
56
57 /* This array should match the CX18_HW_ defines */
58 static const u8 hw_addrs[] = {
59         0,
60         0,
61         CX18_CS5345_I2C_ADDR,
62         0,              /* CX18_HW_GPIO dummy driver ID */
63         0,              /* CX18_HW_CX23418 dummy driver ID */
64 };
65
66 /* This array should match the CX18_HW_ defines */
67 /* This might well become a card-specific array */
68 static const u8 hw_bus[] = {
69         0,
70         0,
71         0,
72         0,              /* CX18_HW_GPIO dummy driver ID */
73         0,              /* CX18_HW_CX23418 dummy driver ID */
74 };
75
76 /* This array should match the CX18_HW_ defines */
77 static const char * const hw_drivernames[] = {
78         "tuner",
79         "tveeprom",
80         "cs5345",
81         "gpio",
82         "cx23418",
83 };
84
85 int cx18_i2c_register(struct cx18 *cx, unsigned idx)
86 {
87         struct i2c_board_info info;
88         struct i2c_client *c;
89         u8 id, bus;
90         int i;
91
92         CX18_DEBUG_I2C("i2c client register\n");
93         if (idx >= ARRAY_SIZE(hw_driverids) || hw_driverids[idx] == 0)
94                 return -1;
95         id = hw_driverids[idx];
96         bus = hw_bus[idx];
97         memset(&info, 0, sizeof(info));
98         strlcpy(info.driver_name, hw_drivernames[idx],
99                         sizeof(info.driver_name));
100         info.addr = hw_addrs[idx];
101         for (i = 0; i < I2C_CLIENTS_MAX; i++)
102                 if (cx->i2c_clients[i] == NULL)
103                         break;
104
105         if (i == I2C_CLIENTS_MAX) {
106                 CX18_ERR("insufficient room for new I2C client!\n");
107                 return -ENOMEM;
108         }
109
110         if (id != I2C_DRIVERID_TUNER) {
111                 c = i2c_new_device(&cx->i2c_adap[bus], &info);
112                 if (c->driver == NULL)
113                         i2c_unregister_device(c);
114                 else
115                         cx->i2c_clients[i] = c;
116                 return cx->i2c_clients[i] ? 0 : -ENODEV;
117         }
118
119         /* special tuner handling */
120         c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->radio);
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->demod);
126         if (c && c->driver == NULL)
127                 i2c_unregister_device(c);
128         else if (c)
129                 cx->i2c_clients[i++] = c;
130         c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->tv);
131         if (c && c->driver == NULL)
132                 i2c_unregister_device(c);
133         else if (c)
134                 cx->i2c_clients[i++] = c;
135         return 0;
136 }
137
138 static int attach_inform(struct i2c_client *client)
139 {
140         return 0;
141 }
142
143 static int detach_inform(struct i2c_client *client)
144 {
145         int i;
146         struct cx18 *cx = (struct cx18 *)i2c_get_adapdata(client->adapter);
147
148         CX18_DEBUG_I2C("i2c client detach\n");
149         for (i = 0; i < I2C_CLIENTS_MAX; i++) {
150                 if (cx->i2c_clients[i] == client) {
151                         cx->i2c_clients[i] = NULL;
152                         break;
153                 }
154         }
155         CX18_DEBUG_I2C("i2c detach [client=%s,%s]\n",
156                    client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed");
157
158         return 0;
159 }
160
161 static void cx18_setscl(void *data, int state)
162 {
163         struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
164         int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
165         u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
166         u32 r = read_reg(addr);
167
168         if (state)
169                 write_reg_sync(r | SETSCL_BIT, addr);
170         else
171                 write_reg_sync(r & ~SETSCL_BIT, addr);
172 }
173
174 static void cx18_setsda(void *data, int state)
175 {
176         struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
177         int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
178         u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
179         u32 r = read_reg(addr);
180
181         if (state)
182                 write_reg_sync(r | SETSDL_BIT, addr);
183         else
184                 write_reg_sync(r & ~SETSDL_BIT, addr);
185 }
186
187 static int cx18_getscl(void *data)
188 {
189         struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
190         int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
191         u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
192
193         return read_reg(addr) & GETSCL_BIT;
194 }
195
196 static int cx18_getsda(void *data)
197 {
198         struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
199         int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
200         u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
201
202         return read_reg(addr) & GETSDL_BIT;
203 }
204
205 /* template for i2c-bit-algo */
206 static struct i2c_adapter cx18_i2c_adap_template = {
207         .name = "cx18 i2c driver",
208         .id = I2C_HW_B_CX2341X,
209         .algo = NULL,                   /* set by i2c-algo-bit */
210         .algo_data = NULL,              /* filled from template */
211         .client_register = attach_inform,
212         .client_unregister = detach_inform,
213         .owner = THIS_MODULE,
214 };
215
216 #define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
217 #define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
218
219 static struct i2c_algo_bit_data cx18_i2c_algo_template = {
220         .setsda         = cx18_setsda,
221         .setscl         = cx18_setscl,
222         .getsda         = cx18_getsda,
223         .getscl         = cx18_getscl,
224         .udelay         = CX18_SCL_PERIOD/2,       /* 1/2 clock period in usec*/
225         .timeout        = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
226 };
227
228 static struct i2c_client cx18_i2c_client_template = {
229         .name = "cx18 internal",
230 };
231
232 int cx18_call_i2c_client(struct cx18 *cx, int addr, unsigned cmd, void *arg)
233 {
234         struct i2c_client *client;
235         int retval;
236         int i;
237
238         CX18_DEBUG_I2C("call_i2c_client addr=%02x\n", addr);
239         for (i = 0; i < I2C_CLIENTS_MAX; i++) {
240                 client = cx->i2c_clients[i];
241                 if (client == NULL || client->driver == NULL ||
242                                 client->driver->command == NULL)
243                         continue;
244                 if (addr == client->addr) {
245                         retval = client->driver->command(client, cmd, arg);
246                         return retval;
247                 }
248         }
249         if (cmd != VIDIOC_G_CHIP_IDENT)
250                 CX18_ERR("i2c addr 0x%02x not found for cmd 0x%x!\n",
251                                addr, cmd);
252         return -ENODEV;
253 }
254
255 /* Find the i2c device based on the driver ID and return
256    its i2c address or -ENODEV if no matching device was found. */
257 static int cx18_i2c_id_addr(struct cx18 *cx, u32 id)
258 {
259         struct i2c_client *client;
260         int retval = -ENODEV;
261         int i;
262
263         for (i = 0; i < I2C_CLIENTS_MAX; i++) {
264                 client = cx->i2c_clients[i];
265                 if (client == NULL || client->driver == NULL)
266                         continue;
267                 if (id == client->driver->id) {
268                         retval = client->addr;
269                         break;
270                 }
271         }
272         return retval;
273 }
274
275 /* Find the i2c device name matching the DRIVERID */
276 static const char *cx18_i2c_id_name(u32 id)
277 {
278         int i;
279
280         for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
281                 if (hw_driverids[i] == id)
282                         return hw_drivernames[i];
283         return "unknown device";
284 }
285
286 /* Find the i2c device name matching the CX18_HW_ flag */
287 static const char *cx18_i2c_hw_name(u32 hw)
288 {
289         int i;
290
291         for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
292                 if (1 << i == hw)
293                         return hw_drivernames[i];
294         return "unknown device";
295 }
296
297 /* Find the i2c device matching the CX18_HW_ flag and return
298    its i2c address or -ENODEV if no matching device was found. */
299 int cx18_i2c_hw_addr(struct cx18 *cx, u32 hw)
300 {
301         int i;
302
303         for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
304                 if (1 << i == hw)
305                         return cx18_i2c_id_addr(cx, hw_driverids[i]);
306         return -ENODEV;
307 }
308
309 /* Calls i2c device based on CX18_HW_ flag. If hw == 0, then do nothing.
310    If hw == CX18_HW_GPIO then call the gpio handler. */
311 int cx18_i2c_hw(struct cx18 *cx, u32 hw, unsigned int cmd, void *arg)
312 {
313         int addr;
314
315         if (hw == CX18_HW_GPIO || hw == 0)
316                 return 0;
317         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. */
330 int 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 */
345 void 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);
354 }
355
356 /* init + register i2c algo-bit adapter */
357 int init_cx18_i2c(struct cx18 *cx)
358 {
359         int i;
360         CX18_DEBUG_I2C("i2c init\n");
361
362         for (i = 0; i < 2; i++) {
363                 memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
364                         sizeof(struct i2c_adapter));
365                 memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
366                         sizeof(struct i2c_algo_bit_data));
367                 cx->i2c_algo_cb_data[i].cx = cx;
368                 cx->i2c_algo_cb_data[i].bus_index = i;
369                 cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
370                 cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
371
372                 sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
373                                 " #%d-%d", cx->num, i);
374                 i2c_set_adapdata(&cx->i2c_adap[i], cx);
375
376                 memcpy(&cx->i2c_client[i], &cx18_i2c_client_template,
377                         sizeof(struct i2c_client));
378                 sprintf(cx->i2c_client[i].name +
379                                 strlen(cx->i2c_client[i].name), "%d", i);
380                 cx->i2c_client[i].adapter = &cx->i2c_adap[i];
381                 cx->i2c_adap[i].dev.parent = &cx->dev->dev;
382         }
383
384         if (read_reg(CX18_REG_I2C_2_WR) != 0x0003c02f) {
385                 /* Reset/Unreset I2C hardware block */
386                 write_reg(0x10000000, 0xc71004); /* Clock select 220MHz */
387                 write_reg_sync(0x10001000, 0xc71024); /* Clock Enable */
388         }
389         /* courtesy of Steven Toth <stoth@hauppauge.com> */
390         write_reg_sync(0x00c00000, 0xc7001c);
391         mdelay(10);
392         write_reg_sync(0x00c000c0, 0xc7001c);
393         mdelay(10);
394         write_reg_sync(0x00c00000, 0xc7001c);
395
396         write_reg_sync(0x00c00000, 0xc730c8); /* Set to edge-triggered intrs. */
397         write_reg_sync(0x00c00000, 0xc730c4); /* Clear any stale intrs */
398
399         /* Hw I2C1 Clock Freq ~100kHz */
400         write_reg_sync(0x00021c0f & ~4, CX18_REG_I2C_1_WR);
401         cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
402         cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
403
404         /* Hw I2C2 Clock Freq ~100kHz */
405         write_reg_sync(0x00021c0f & ~4, CX18_REG_I2C_2_WR);
406         cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
407         cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
408
409         return i2c_bit_add_bus(&cx->i2c_adap[0]) ||
410                 i2c_bit_add_bus(&cx->i2c_adap[1]);
411 }
412
413 void exit_cx18_i2c(struct cx18 *cx)
414 {
415         int i;
416         CX18_DEBUG_I2C("i2c exit\n");
417         write_reg(read_reg(CX18_REG_I2C_1_WR) | 4, CX18_REG_I2C_1_WR);
418         write_reg(read_reg(CX18_REG_I2C_2_WR) | 4, CX18_REG_I2C_2_WR);
419
420         for (i = 0; i < 2; i++) {
421                 i2c_del_adapter(&cx->i2c_adap[i]);
422         }
423 }
424
425 /*
426    Hauppauge HVR1600 should have:
427    32 cx24227
428    98 unknown
429    a0 eeprom
430    c2 tuner
431    e? zilog ir
432    */