blob: 6357dc44ab5ba92de3babfb6208384368582c99e [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 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030046static const u8 hw_addrs[] = {
Andy Wallsff2a2002009-02-20 23:52:13 -030047 0, /* CX18_HW_TUNER */
48 0, /* CX18_HW_TVEEPROM */
49 CX18_CS5345_I2C_ADDR, /* CX18_HW_CS5345 */
50 0, /* CX18_HW_DVB */
51 0, /* CX18_HW_418_AV */
52 0, /* CX18_HW_GPIO_AUDIO_MUX */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030053};
54
55/* This array should match the CX18_HW_ defines */
56/* This might well become a card-specific array */
57static const u8 hw_bus[] = {
Andy Wallsff2a2002009-02-20 23:52:13 -030058 1, /* CX18_HW_TUNER */
59 0, /* CX18_HW_TVEEPROM */
60 0, /* CX18_HW_CS5345 */
61 0, /* CX18_HW_DVB */
62 0, /* CX18_HW_418_AV */
63 0, /* CX18_HW_GPIO_AUDIO_MUX */
64};
65
66/* This array should match the CX18_HW_ defines */
67static const char * const hw_modules[] = {
68 "tuner", /* CX18_HW_TUNER */
69 NULL, /* CX18_HW_TVEEPROM */
70 "cs5345", /* CX18_HW_CS5345 */
71 NULL, /* CX18_HW_DVB */
72 NULL, /* CX18_HW_418_AV */
73 NULL, /* CX18_HW_GPIO_AUDIO_MUX */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030074};
75
76/* This array should match the CX18_HW_ defines */
Jean Delvareaf294862008-05-18 20:49:40 +020077static const char * const hw_devicenames[] = {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030078 "tuner",
79 "tveeprom",
80 "cs5345",
Andy Wallsff2a2002009-02-20 23:52:13 -030081 "cx23418_DTV",
82 "cx23418_AV",
83 "gpio_audio_mux",
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030084};
85
86int cx18_i2c_register(struct cx18 *cx, unsigned idx)
87{
Andy Wallsff2a2002009-02-20 23:52:13 -030088 struct v4l2_subdev *sd;
89 int bus = hw_bus[idx];
90 struct i2c_adapter *adap = &cx->i2c_adap[bus];
91 const char *mod = hw_modules[idx];
92 const char *type = hw_devicenames[idx];
93 u32 hw = 1 << idx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094
Andy Wallsff2a2002009-02-20 23:52:13 -030095 if (idx >= ARRAY_SIZE(hw_addrs))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030096 return -1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097
Andy Wallsff2a2002009-02-20 23:52:13 -030098 if (hw == CX18_HW_TUNER) {
99 /* special tuner group handling */
100 sd = v4l2_i2c_new_probed_subdev(adap, mod, type,
101 cx->card_i2c->radio);
102 if (sd != NULL)
103 sd->grp_id = hw;
104 sd = v4l2_i2c_new_probed_subdev(adap, mod, type,
105 cx->card_i2c->demod);
106 if (sd != NULL)
107 sd->grp_id = hw;
108 sd = v4l2_i2c_new_probed_subdev(adap, mod, type,
109 cx->card_i2c->tv);
110 if (sd != NULL)
111 sd->grp_id = hw;
112 return sd != NULL ? 0 : -1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300113 }
114
Andy Wallsff2a2002009-02-20 23:52:13 -0300115 /* Is it not an I2C device or one we do not wish to register? */
116 if (!hw_addrs[idx])
117 return -1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300118
Andy Wallsff2a2002009-02-20 23:52:13 -0300119 /* It's an I2C device other than an analog tuner */
120 sd = v4l2_i2c_new_subdev(adap, mod, type, hw_addrs[idx]);
121 if (sd != NULL)
122 sd->grp_id = hw;
123 return sd != NULL ? 0 : -1;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300124}
125
Andy Wallsff2a2002009-02-20 23:52:13 -0300126/* Find the first member of the subdev group id in hw */
127struct v4l2_subdev *cx18_find_hw(struct cx18 *cx, u32 hw)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300128{
Andy Wallsff2a2002009-02-20 23:52:13 -0300129 struct v4l2_subdev *result = NULL;
130 struct v4l2_subdev *sd;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300131
Andy Wallsff2a2002009-02-20 23:52:13 -0300132 spin_lock(&cx->v4l2_dev.lock);
133 v4l2_device_for_each_subdev(sd, &cx->v4l2_dev) {
134 if (sd->grp_id == hw) {
135 result = sd;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136 break;
137 }
138 }
Andy Wallsff2a2002009-02-20 23:52:13 -0300139 spin_unlock(&cx->v4l2_dev.lock);
140 return result;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300141}
142
143static void cx18_setscl(void *data, int state)
144{
145 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
146 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
147 u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
Andy Wallsb1526422008-08-30 16:03:44 -0300148 u32 r = cx18_read_reg(cx, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300149
150 if (state)
Andy Walls3f75c612008-11-16 23:33:41 -0300151 cx18_write_reg(cx, r | SETSCL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300152 else
Andy Walls3f75c612008-11-16 23:33:41 -0300153 cx18_write_reg(cx, r & ~SETSCL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300154}
155
156static void cx18_setsda(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;
Andy Wallsb1526422008-08-30 16:03:44 -0300161 u32 r = cx18_read_reg(cx, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162
163 if (state)
Andy Walls3f75c612008-11-16 23:33:41 -0300164 cx18_write_reg(cx, r | SETSDL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300165 else
Andy Walls3f75c612008-11-16 23:33:41 -0300166 cx18_write_reg(cx, r & ~SETSDL_BIT, addr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300167}
168
169static int cx18_getscl(void *data)
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_RD : CX18_REG_I2C_1_RD;
174
Andy Wallsb1526422008-08-30 16:03:44 -0300175 return cx18_read_reg(cx, addr) & GETSCL_BIT;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300176}
177
178static int cx18_getsda(void *data)
179{
180 struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
181 int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
182 u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
183
Andy Wallsb1526422008-08-30 16:03:44 -0300184 return cx18_read_reg(cx, addr) & GETSDL_BIT;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300185}
186
187/* template for i2c-bit-algo */
188static struct i2c_adapter cx18_i2c_adap_template = {
189 .name = "cx18 i2c driver",
190 .id = I2C_HW_B_CX2341X,
191 .algo = NULL, /* set by i2c-algo-bit */
192 .algo_data = NULL, /* filled from template */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300193 .owner = THIS_MODULE,
194};
195
196#define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
197#define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
198
199static struct i2c_algo_bit_data cx18_i2c_algo_template = {
200 .setsda = cx18_setsda,
201 .setscl = cx18_setscl,
202 .getsda = cx18_getsda,
203 .getscl = cx18_getscl,
204 .udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/
205 .timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
206};
207
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300208/* init + register i2c algo-bit adapter */
209int init_cx18_i2c(struct cx18 *cx)
210{
211 int i;
212 CX18_DEBUG_I2C("i2c init\n");
213
214 for (i = 0; i < 2; i++) {
Andy Wallsff2a2002009-02-20 23:52:13 -0300215 /* Setup algorithm for adapter */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300216 memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
217 sizeof(struct i2c_algo_bit_data));
218 cx->i2c_algo_cb_data[i].cx = cx;
219 cx->i2c_algo_cb_data[i].bus_index = i;
220 cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300221
Andy Wallsff2a2002009-02-20 23:52:13 -0300222 /* Setup adapter */
223 memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
224 sizeof(struct i2c_adapter));
225 cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300226 sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
Andy Walls5811cf92009-02-14 17:08:37 -0300227 " #%d-%d", cx->instance, i);
Andy Wallsff2a2002009-02-20 23:52:13 -0300228 i2c_set_adapdata(&cx->i2c_adap[i], &cx->v4l2_dev);
Andy Walls3d059132009-01-10 21:54:39 -0300229 cx->i2c_adap[i].dev.parent = &cx->pci_dev->dev;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300230 }
231
Andy Wallsb1526422008-08-30 16:03:44 -0300232 if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300233 /* Reset/Unreset I2C hardware block */
Andy Wallsb1526422008-08-30 16:03:44 -0300234 /* Clock select 220MHz */
Andy Wallsced07372008-11-02 10:59:04 -0300235 cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
236 0x00000000, 0x10001000);
Andy Wallsb1526422008-08-30 16:03:44 -0300237 /* Clock Enable */
Andy Wallsced07372008-11-02 10:59:04 -0300238 cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
239 0x00001000, 0x10001000);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300240 }
241 /* courtesy of Steven Toth <stoth@hauppauge.com> */
Andy Wallsced07372008-11-02 10:59:04 -0300242 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300243 mdelay(10);
Andy Wallsced07372008-11-02 10:59:04 -0300244 cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300245 mdelay(10);
Andy Wallsced07372008-11-02 10:59:04 -0300246 cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
Andy Walls53ad02e2008-07-06 19:36:52 -0300247 mdelay(10);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300248
Andy Wallsb1526422008-08-30 16:03:44 -0300249 /* Set to edge-triggered intrs. */
Andy Wallsced07372008-11-02 10:59:04 -0300250 cx18_write_reg(cx, 0x00c00000, 0xc730c8);
Andy Wallsb1526422008-08-30 16:03:44 -0300251 /* Clear any stale intrs */
Andy Wallsced07372008-11-02 10:59:04 -0300252 cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
253 ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300254
255 /* Hw I2C1 Clock Freq ~100kHz */
Andy Walls3f75c612008-11-16 23:33:41 -0300256 cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300257 cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
258 cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
259
260 /* Hw I2C2 Clock Freq ~100kHz */
Andy Walls3f75c612008-11-16 23:33:41 -0300261 cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300262 cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
263 cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
264
Andy Walls1f09e8a2008-06-22 01:27:00 -0300265 cx18_reset_i2c_slaves_gpio(cx);
266
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300267 return i2c_bit_add_bus(&cx->i2c_adap[0]) ||
268 i2c_bit_add_bus(&cx->i2c_adap[1]);
269}
270
271void exit_cx18_i2c(struct cx18 *cx)
272{
273 int i;
274 CX18_DEBUG_I2C("i2c exit\n");
Andy Wallsb1526422008-08-30 16:03:44 -0300275 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
276 CX18_REG_I2C_1_WR);
277 cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
278 CX18_REG_I2C_2_WR);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300279
280 for (i = 0; i < 2; i++) {
281 i2c_del_adapter(&cx->i2c_adap[i]);
282 }
283}
284
285/*
286 Hauppauge HVR1600 should have:
287 32 cx24227
288 98 unknown
289 a0 eeprom
290 c2 tuner
291 e? zilog ir
292 */