blob: 35796e0352475b6536bfd47315107e418e6716fa [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* cx25840 - Conexant CX25840 audio/video decoder driver
2 *
3 * Copyright (C) 2004 Ulf Eklund
4 *
5 * Based on the saa7115 driver and on the first verison of Chris Kennedy's
6 * cx25840 driver.
7 *
8 * Changes by Tyler Trafford <tatrafford@comcast.net>
9 * - cleanup/rewrite for V4L2 API (2005)
10 *
11 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
12 *
Christopher Neufeld3e3bf272006-05-24 10:16:45 -030013 * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
14 * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
15 *
Steven Toth6d897612008-09-03 17:12:12 -030016 * CX23885 support by Steven Toth <stoth@linuxtv.org>.
Steven Tothf2340812008-01-10 01:22:39 -030017 *
Andy Walls52fd3dd2010-07-18 22:08:03 -030018 * CX2388[578] IRQ handling, IO Pin mux configuration and other small fixes are
19 * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net>
20 *
Hans Verkuilbd985162005-11-13 16:07:56 -080021 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version 2
24 * of the License, or (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 */
35
36
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/slab.h>
40#include <linux/videodev2.h>
41#include <linux/i2c.h>
Michael Krufkyf61b48f2007-09-01 02:02:51 -030042#include <linux/delay.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080043#include <media/v4l2-common.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030044#include <media/v4l2-chip-ident.h>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030045#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080046
Hans Verkuil31bc09b2006-03-25 10:26:09 -030047#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080048
49MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
Hans Verkuil1f4b3362005-11-13 16:08:05 -080050MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
Hans Verkuilbd985162005-11-13 16:07:56 -080051MODULE_LICENSE("GPL");
52
Andy Walls52fd3dd2010-07-18 22:08:03 -030053#define CX25840_VID_INT_STAT_REG 0x410
54#define CX25840_VID_INT_STAT_BITS 0x0000ffff
55#define CX25840_VID_INT_MASK_BITS 0xffff0000
56#define CX25840_VID_INT_MASK_SHFT 16
57#define CX25840_VID_INT_MASK_REG 0x412
58
59#define CX23885_AUD_MC_INT_MASK_REG 0x80c
60#define CX23885_AUD_MC_INT_STAT_BITS 0xffff0000
61#define CX23885_AUD_MC_INT_CTRL_BITS 0x0000ffff
62#define CX23885_AUD_MC_INT_STAT_SHFT 16
63
64#define CX25840_AUD_INT_CTRL_REG 0x812
65#define CX25840_AUD_INT_STAT_REG 0x813
66
67#define CX23885_PIN_CTRL_IRQ_REG 0x123
68#define CX23885_PIN_CTRL_IRQ_IR_STAT 0x40
69#define CX23885_PIN_CTRL_IRQ_AUD_STAT 0x20
70#define CX23885_PIN_CTRL_IRQ_VID_STAT 0x10
71
72#define CX25840_IR_STATS_REG 0x210
73#define CX25840_IR_IRQEN_REG 0x214
74
Adrian Bunkfe0d3df2008-07-21 16:33:48 -030075static int cx25840_debug;
Hans Verkuilbd985162005-11-13 16:07:56 -080076
Hans Verkuilb5fc7142006-01-11 22:41:36 -020077module_param_named(debug,cx25840_debug, int, 0644);
Hans Verkuilbd985162005-11-13 16:07:56 -080078
Hans Verkuilfac9e892006-01-09 15:32:40 -020079MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
Hans Verkuilbd985162005-11-13 16:07:56 -080080
Hans Verkuilbd985162005-11-13 16:07:56 -080081
82/* ----------------------------------------------------------------------- */
83
84int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
85{
86 u8 buffer[3];
87 buffer[0] = addr >> 8;
88 buffer[1] = addr & 0xff;
89 buffer[2] = value;
90 return i2c_master_send(client, buffer, 3);
91}
92
93int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
94{
95 u8 buffer[6];
96 buffer[0] = addr >> 8;
97 buffer[1] = addr & 0xff;
Hans Verkuil4a56eb32007-12-02 07:03:45 -030098 buffer[2] = value & 0xff;
99 buffer[3] = (value >> 8) & 0xff;
100 buffer[4] = (value >> 16) & 0xff;
101 buffer[5] = value >> 24;
Hans Verkuilbd985162005-11-13 16:07:56 -0800102 return i2c_master_send(client, buffer, 6);
103}
104
105u8 cx25840_read(struct i2c_client * client, u16 addr)
106{
Andy Walls5f272642010-07-18 17:16:16 -0300107 struct i2c_msg msgs[2];
108 u8 tx_buf[2], rx_buf[1];
Hans Verkuilbd985162005-11-13 16:07:56 -0800109
Andy Walls5f272642010-07-18 17:16:16 -0300110 /* Write register address */
111 tx_buf[0] = addr >> 8;
112 tx_buf[1] = addr & 0xff;
113 msgs[0].addr = client->addr;
114 msgs[0].flags = 0;
115 msgs[0].len = 2;
116 msgs[0].buf = (char *) tx_buf;
117
118 /* Read data from register */
119 msgs[1].addr = client->addr;
120 msgs[1].flags = I2C_M_RD;
121 msgs[1].len = 1;
122 msgs[1].buf = (char *) rx_buf;
123
124 if (i2c_transfer(client->adapter, msgs, 2) < 2)
Hans Verkuilbd985162005-11-13 16:07:56 -0800125 return 0;
126
Andy Walls5f272642010-07-18 17:16:16 -0300127 return rx_buf[0];
Hans Verkuilbd985162005-11-13 16:07:56 -0800128}
129
130u32 cx25840_read4(struct i2c_client * client, u16 addr)
131{
Andy Walls5f272642010-07-18 17:16:16 -0300132 struct i2c_msg msgs[2];
133 u8 tx_buf[2], rx_buf[4];
Hans Verkuilbd985162005-11-13 16:07:56 -0800134
Andy Walls5f272642010-07-18 17:16:16 -0300135 /* Write register address */
136 tx_buf[0] = addr >> 8;
137 tx_buf[1] = addr & 0xff;
138 msgs[0].addr = client->addr;
139 msgs[0].flags = 0;
140 msgs[0].len = 2;
141 msgs[0].buf = (char *) tx_buf;
142
143 /* Read data from registers */
144 msgs[1].addr = client->addr;
145 msgs[1].flags = I2C_M_RD;
146 msgs[1].len = 4;
147 msgs[1].buf = (char *) rx_buf;
148
149 if (i2c_transfer(client->adapter, msgs, 2) < 2)
Hans Verkuilbd985162005-11-13 16:07:56 -0800150 return 0;
151
Andy Walls5f272642010-07-18 17:16:16 -0300152 return (rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) |
153 rx_buf[0];
Hans Verkuilbd985162005-11-13 16:07:56 -0800154}
155
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300156int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
Hans Verkuilbd985162005-11-13 16:07:56 -0800157 u8 or_value)
158{
159 return cx25840_write(client, addr,
160 (cx25840_read(client, addr) & and_mask) |
161 or_value);
162}
163
Andy Walls52fd3dd2010-07-18 22:08:03 -0300164int cx25840_and_or4(struct i2c_client *client, u16 addr, u32 and_mask,
165 u32 or_value)
166{
167 return cx25840_write4(client, addr,
168 (cx25840_read4(client, addr) & and_mask) |
169 or_value);
170}
171
Hans Verkuilbd985162005-11-13 16:07:56 -0800172/* ----------------------------------------------------------------------- */
173
Hans Verkuila8bbf122006-01-09 15:25:42 -0200174static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
175 enum cx25840_audio_input aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800176
177/* ----------------------------------------------------------------------- */
178
Andy Wallsd06d5772010-07-18 19:39:54 -0300179static int cx23885_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
180 struct v4l2_subdev_io_pin_config *p)
181{
182 struct i2c_client *client = v4l2_get_subdevdata(sd);
183 int i;
184 u32 pin_ctrl;
185 u8 gpio_oe, gpio_data, strength;
186
187 pin_ctrl = cx25840_read4(client, 0x120);
188 gpio_oe = cx25840_read(client, 0x160);
189 gpio_data = cx25840_read(client, 0x164);
190
191 for (i = 0; i < n; i++) {
192 strength = p[i].strength;
193 if (strength > CX25840_PIN_DRIVE_FAST)
194 strength = CX25840_PIN_DRIVE_FAST;
195
196 switch (p[i].pin) {
197 case CX23885_PIN_IRQ_N_GPIO16:
198 if (p[i].function != CX23885_PAD_IRQ_N) {
199 /* GPIO16 */
200 pin_ctrl &= ~(0x1 << 25);
201 } else {
202 /* IRQ_N */
203 if (p[i].flags &
204 (V4L2_SUBDEV_IO_PIN_DISABLE |
205 V4L2_SUBDEV_IO_PIN_INPUT)) {
206 pin_ctrl &= ~(0x1 << 25);
207 } else {
208 pin_ctrl |= (0x1 << 25);
209 }
210 if (p[i].flags &
211 V4L2_SUBDEV_IO_PIN_ACTIVE_LOW) {
212 pin_ctrl &= ~(0x1 << 24);
213 } else {
214 pin_ctrl |= (0x1 << 24);
215 }
216 }
217 break;
218 case CX23885_PIN_IR_RX_GPIO19:
219 if (p[i].function != CX23885_PAD_GPIO19) {
220 /* IR_RX */
221 gpio_oe |= (0x1 << 0);
222 pin_ctrl &= ~(0x3 << 18);
223 pin_ctrl |= (strength << 18);
224 } else {
225 /* GPIO19 */
226 gpio_oe &= ~(0x1 << 0);
227 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
228 gpio_data &= ~(0x1 << 0);
229 gpio_data |= ((p[i].value & 0x1) << 0);
230 }
231 pin_ctrl &= ~(0x3 << 12);
232 pin_ctrl |= (strength << 12);
233 }
234 break;
235 case CX23885_PIN_IR_TX_GPIO20:
236 if (p[i].function != CX23885_PAD_GPIO20) {
237 /* IR_TX */
238 gpio_oe |= (0x1 << 1);
239 if (p[i].flags & V4L2_SUBDEV_IO_PIN_DISABLE)
240 pin_ctrl &= ~(0x1 << 10);
241 else
242 pin_ctrl |= (0x1 << 10);
243 pin_ctrl &= ~(0x3 << 18);
244 pin_ctrl |= (strength << 18);
245 } else {
246 /* GPIO20 */
247 gpio_oe &= ~(0x1 << 1);
248 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
249 gpio_data &= ~(0x1 << 1);
250 gpio_data |= ((p[i].value & 0x1) << 1);
251 }
252 pin_ctrl &= ~(0x3 << 12);
253 pin_ctrl |= (strength << 12);
254 }
255 break;
256 case CX23885_PIN_I2S_SDAT_GPIO21:
257 if (p[i].function != CX23885_PAD_GPIO21) {
258 /* I2S_SDAT */
259 /* TODO: Input or Output config */
260 gpio_oe |= (0x1 << 2);
261 pin_ctrl &= ~(0x3 << 22);
262 pin_ctrl |= (strength << 22);
263 } else {
264 /* GPIO21 */
265 gpio_oe &= ~(0x1 << 2);
266 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
267 gpio_data &= ~(0x1 << 2);
268 gpio_data |= ((p[i].value & 0x1) << 2);
269 }
270 pin_ctrl &= ~(0x3 << 12);
271 pin_ctrl |= (strength << 12);
272 }
273 break;
274 case CX23885_PIN_I2S_WCLK_GPIO22:
275 if (p[i].function != CX23885_PAD_GPIO22) {
276 /* I2S_WCLK */
277 /* TODO: Input or Output config */
278 gpio_oe |= (0x1 << 3);
279 pin_ctrl &= ~(0x3 << 22);
280 pin_ctrl |= (strength << 22);
281 } else {
282 /* GPIO22 */
283 gpio_oe &= ~(0x1 << 3);
284 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
285 gpio_data &= ~(0x1 << 3);
286 gpio_data |= ((p[i].value & 0x1) << 3);
287 }
288 pin_ctrl &= ~(0x3 << 12);
289 pin_ctrl |= (strength << 12);
290 }
291 break;
292 case CX23885_PIN_I2S_BCLK_GPIO23:
293 if (p[i].function != CX23885_PAD_GPIO23) {
294 /* I2S_BCLK */
295 /* TODO: Input or Output config */
296 gpio_oe |= (0x1 << 4);
297 pin_ctrl &= ~(0x3 << 22);
298 pin_ctrl |= (strength << 22);
299 } else {
300 /* GPIO23 */
301 gpio_oe &= ~(0x1 << 4);
302 if (p[i].flags & V4L2_SUBDEV_IO_PIN_SET_VALUE) {
303 gpio_data &= ~(0x1 << 4);
304 gpio_data |= ((p[i].value & 0x1) << 4);
305 }
306 pin_ctrl &= ~(0x3 << 12);
307 pin_ctrl |= (strength << 12);
308 }
309 break;
310 }
311 }
312
313 cx25840_write(client, 0x164, gpio_data);
314 cx25840_write(client, 0x160, gpio_oe);
315 cx25840_write4(client, 0x120, pin_ctrl);
316 return 0;
317}
318
319static int common_s_io_pin_config(struct v4l2_subdev *sd, size_t n,
320 struct v4l2_subdev_io_pin_config *pincfg)
321{
322 struct cx25840_state *state = to_state(sd);
323
324 if (is_cx2388x(state))
325 return cx23885_s_io_pin_config(sd, n, pincfg);
326 return 0;
327}
328
329/* ----------------------------------------------------------------------- */
330
Hans Verkuild92c20e2006-01-09 15:32:41 -0200331static void init_dll1(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800332{
333 /* This is the Hauppauge sequence used to
334 * initialize the Delay Lock Loop 1 (ADC DLL). */
335 cx25840_write(client, 0x159, 0x23);
336 cx25840_write(client, 0x15a, 0x87);
337 cx25840_write(client, 0x15b, 0x06);
Tyler Trafford38051452007-08-28 17:56:47 -0300338 udelay(10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800339 cx25840_write(client, 0x159, 0xe1);
Tyler Trafford38051452007-08-28 17:56:47 -0300340 udelay(10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800341 cx25840_write(client, 0x15a, 0x86);
342 cx25840_write(client, 0x159, 0xe0);
343 cx25840_write(client, 0x159, 0xe1);
344 cx25840_write(client, 0x15b, 0x10);
345}
346
Hans Verkuild92c20e2006-01-09 15:32:41 -0200347static void init_dll2(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800348{
349 /* This is the Hauppauge sequence used to
350 * initialize the Delay Lock Loop 2 (ADC DLL). */
351 cx25840_write(client, 0x15d, 0xe3);
352 cx25840_write(client, 0x15e, 0x86);
353 cx25840_write(client, 0x15f, 0x06);
Tyler Trafford38051452007-08-28 17:56:47 -0300354 udelay(10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800355 cx25840_write(client, 0x15d, 0xe1);
356 cx25840_write(client, 0x15d, 0xe0);
357 cx25840_write(client, 0x15d, 0xe1);
358}
359
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300360static void cx25836_initialize(struct i2c_client *client)
361{
362 /* reset configuration is described on page 3-77 of the CX25836 datasheet */
363 /* 2. */
364 cx25840_and_or(client, 0x000, ~0x01, 0x01);
365 cx25840_and_or(client, 0x000, ~0x01, 0x00);
366 /* 3a. */
367 cx25840_and_or(client, 0x15a, ~0x70, 0x00);
368 /* 3b. */
369 cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
370 /* 3c. */
371 cx25840_and_or(client, 0x159, ~0x02, 0x02);
372 /* 3d. */
Tyler Trafford38051452007-08-28 17:56:47 -0300373 udelay(10);
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300374 /* 3e. */
375 cx25840_and_or(client, 0x159, ~0x02, 0x00);
376 /* 3f. */
377 cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
378 /* 3g. */
379 cx25840_and_or(client, 0x159, ~0x01, 0x00);
380 cx25840_and_or(client, 0x159, ~0x01, 0x01);
381 /* 3h. */
382 cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
383}
384
Hans Verkuil21340ae2007-08-26 10:53:16 -0300385static void cx25840_work_handler(struct work_struct *work)
386{
387 struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
388 cx25840_loadfw(state->c);
389 wake_up(&state->fw_wait);
390}
391
Hans Verkuil89fc4eb2007-08-04 05:00:07 -0300392static void cx25840_initialize(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800393{
Hans Verkuil21340ae2007-08-26 10:53:16 -0300394 DEFINE_WAIT(wait);
Hans Verkuil9357b312008-11-29 12:50:06 -0300395 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil21340ae2007-08-26 10:53:16 -0300396 struct workqueue_struct *q;
Hans Verkuilbd985162005-11-13 16:07:56 -0800397
398 /* datasheet startup in numbered steps, refer to page 3-77 */
399 /* 2. */
400 cx25840_and_or(client, 0x803, ~0x10, 0x00);
401 /* The default of this register should be 4, but I get 0 instead.
402 * Set this register to 4 manually. */
403 cx25840_write(client, 0x000, 0x04);
404 /* 3. */
405 init_dll1(client);
406 init_dll2(client);
407 cx25840_write(client, 0x136, 0x0a);
408 /* 4. */
409 cx25840_write(client, 0x13c, 0x01);
410 cx25840_write(client, 0x13c, 0x00);
411 /* 5. */
Hans Verkuil21340ae2007-08-26 10:53:16 -0300412 /* Do the firmware load in a work handler to prevent.
413 Otherwise the kernel is blocked waiting for the
414 bit-banging i2c interface to finish uploading the
415 firmware. */
416 INIT_WORK(&state->fw_work, cx25840_work_handler);
417 init_waitqueue_head(&state->fw_wait);
418 q = create_singlethread_workqueue("cx25840_fw");
419 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
420 queue_work(q, &state->fw_work);
421 schedule();
422 finish_wait(&state->fw_wait, &wait);
423 destroy_workqueue(q);
424
Hans Verkuilbd985162005-11-13 16:07:56 -0800425 /* 6. */
426 cx25840_write(client, 0x115, 0x8c);
427 cx25840_write(client, 0x116, 0x07);
428 cx25840_write(client, 0x118, 0x02);
429 /* 7. */
430 cx25840_write(client, 0x4a5, 0x80);
431 cx25840_write(client, 0x4a5, 0x00);
432 cx25840_write(client, 0x402, 0x00);
433 /* 8. */
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300434 cx25840_and_or(client, 0x401, ~0x18, 0);
435 cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
436 /* steps 8c and 8d are done in change_input() */
Hans Verkuilbd985162005-11-13 16:07:56 -0800437 /* 10. */
438 cx25840_write(client, 0x8d3, 0x1f);
439 cx25840_write(client, 0x8e3, 0x03);
440
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300441 cx25840_std_setup(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800442
443 /* trial and error says these are needed to get audio */
444 cx25840_write(client, 0x914, 0xa0);
445 cx25840_write(client, 0x918, 0xa0);
446 cx25840_write(client, 0x919, 0x01);
447
448 /* stereo prefered */
449 cx25840_write(client, 0x809, 0x04);
450 /* AC97 shift */
451 cx25840_write(client, 0x8cf, 0x0f);
452
Hans Verkuila8bbf122006-01-09 15:25:42 -0200453 /* (re)set input */
454 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800455
456 /* start microcontroller */
457 cx25840_and_or(client, 0x803, ~0x10, 0x10);
458}
459
Steven Tothf2340812008-01-10 01:22:39 -0300460static void cx23885_initialize(struct i2c_client *client)
461{
462 DEFINE_WAIT(wait);
Hans Verkuil9357b312008-11-29 12:50:06 -0300463 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Steven Tothf2340812008-01-10 01:22:39 -0300464 struct workqueue_struct *q;
465
Andy Wallse283d782009-09-27 00:00:48 -0300466 /*
467 * Come out of digital power down
468 * The CX23888, at least, needs this, otherwise registers aside from
469 * 0x0-0x2 can't be read or written.
470 */
471 cx25840_write(client, 0x000, 0);
472
Steven Tothf2340812008-01-10 01:22:39 -0300473 /* Internal Reset */
474 cx25840_and_or(client, 0x102, ~0x01, 0x01);
475 cx25840_and_or(client, 0x102, ~0x01, 0x00);
476
477 /* Stop microcontroller */
478 cx25840_and_or(client, 0x803, ~0x10, 0x00);
479
480 /* DIF in reset? */
481 cx25840_write(client, 0x398, 0);
482
Andy Wallse283d782009-09-27 00:00:48 -0300483 /*
484 * Trust the default xtal, no division
485 * '885: 28.636363... MHz
486 * '887: 25.000000 MHz
487 * '888: 50.000000 MHz
488 */
Steven Tothf2340812008-01-10 01:22:39 -0300489 cx25840_write(client, 0x2, 0x76);
490
Andy Wallse283d782009-09-27 00:00:48 -0300491 /* Power up all the PLL's and DLL */
Steven Tothf2340812008-01-10 01:22:39 -0300492 cx25840_write(client, 0x1, 0x40);
493
Andy Wallse283d782009-09-27 00:00:48 -0300494 /* Sys PLL */
495 switch (state->id) {
496 case V4L2_IDENT_CX23888_AV:
497 /*
498 * 50.0 MHz * (0xb + 0xe8ba26/0x2000000)/4 = 5 * 28.636363 MHz
499 * 572.73 MHz before post divide
500 */
501 cx25840_write4(client, 0x11c, 0x00e8ba26);
502 cx25840_write4(client, 0x118, 0x0000040b);
503 break;
504 case V4L2_IDENT_CX23887_AV:
505 /*
506 * 25.0 MHz * (0x16 + 0x1d1744c/0x2000000)/4 = 5 * 28.636363 MHz
507 * 572.73 MHz before post divide
508 */
509 cx25840_write4(client, 0x11c, 0x01d1744c);
510 cx25840_write4(client, 0x118, 0x00000416);
511 break;
512 case V4L2_IDENT_CX23885_AV:
513 default:
514 /*
515 * 28.636363 MHz * (0x14 + 0x0/0x2000000)/4 = 5 * 28.636363 MHz
516 * 572.73 MHz before post divide
517 */
518 cx25840_write4(client, 0x11c, 0x00000000);
519 cx25840_write4(client, 0x118, 0x00000414);
520 break;
521 }
Steven Tothf2340812008-01-10 01:22:39 -0300522
523 /* Disable DIF bypass */
524 cx25840_write4(client, 0x33c, 0x00000001);
525
526 /* DIF Src phase inc */
527 cx25840_write4(client, 0x340, 0x0df7df83);
528
Andy Wallse283d782009-09-27 00:00:48 -0300529 /*
530 * Vid PLL
531 * Setup for a BT.656 pixel clock of 13.5 Mpixels/second
532 *
533 * 28.636363 MHz * (0xf + 0x02be2c9/0x2000000)/4 = 8 * 13.5 MHz
534 * 432.0 MHz before post divide
535 */
536 cx25840_write4(client, 0x10c, 0x002be2c9);
537 cx25840_write4(client, 0x108, 0x0000040f);
Steven Tothf2340812008-01-10 01:22:39 -0300538
539 /* Luma */
540 cx25840_write4(client, 0x414, 0x00107d12);
541
542 /* Chroma */
543 cx25840_write4(client, 0x420, 0x3d008282);
544
Andy Wallse283d782009-09-27 00:00:48 -0300545 /*
546 * Aux PLL
547 * Initial setup for audio sample clock:
548 * 48 ksps, 16 bits/sample, x160 multiplier = 122.88 MHz
549 * Intial I2S output/master clock(?):
550 * 48 ksps, 16 bits/sample, x16 multiplier = 12.288 MHz
551 */
552 switch (state->id) {
553 case V4L2_IDENT_CX23888_AV:
554 /*
555 * 50.0 MHz * (0x7 + 0x0bedfa4/0x2000000)/3 = 122.88 MHz
556 * 368.64 MHz before post divide
557 * 122.88 MHz / 0xa = 12.288 MHz
558 */
559 cx25840_write4(client, 0x114, 0x00bedfa4);
560 cx25840_write4(client, 0x110, 0x000a0307);
561 break;
562 case V4L2_IDENT_CX23887_AV:
563 /*
564 * 25.0 MHz * (0xe + 0x17dbf48/0x2000000)/3 = 122.88 MHz
565 * 368.64 MHz before post divide
566 * 122.88 MHz / 0xa = 12.288 MHz
567 */
568 cx25840_write4(client, 0x114, 0x017dbf48);
569 cx25840_write4(client, 0x110, 0x000a030e);
570 break;
571 case V4L2_IDENT_CX23885_AV:
572 default:
573 /*
574 * 28.636363 MHz * (0xc + 0x1bf0c9e/0x2000000)/3 = 122.88 MHz
575 * 368.64 MHz before post divide
576 * 122.88 MHz / 0xa = 12.288 MHz
577 */
578 cx25840_write4(client, 0x114, 0x01bf0c9e);
579 cx25840_write4(client, 0x110, 0x000a030c);
580 break;
581 };
Steven Tothf2340812008-01-10 01:22:39 -0300582
583 /* ADC2 input select */
584 cx25840_write(client, 0x102, 0x10);
585
586 /* VIN1 & VIN5 */
587 cx25840_write(client, 0x103, 0x11);
588
589 /* Enable format auto detect */
590 cx25840_write(client, 0x400, 0);
591 /* Fast subchroma lock */
592 /* White crush, Chroma AGC & Chroma Killer enabled */
593 cx25840_write(client, 0x401, 0xe8);
594
595 /* Select AFE clock pad output source */
596 cx25840_write(client, 0x144, 0x05);
597
Steven Tothf3d6f632009-07-23 12:18:54 -0300598 /* Drive GPIO2 direction and values for HVR1700
599 * where an onboard mux selects the output of demodulator
600 * vs the 417. Failure to set this results in no DTV.
601 * It's safe to set this across all Hauppauge boards
602 * currently, regardless of the board type.
603 */
604 cx25840_write(client, 0x160, 0x1d);
605 cx25840_write(client, 0x164, 0x00);
606
Steven Tothf2340812008-01-10 01:22:39 -0300607 /* Do the firmware load in a work handler to prevent.
608 Otherwise the kernel is blocked waiting for the
609 bit-banging i2c interface to finish uploading the
610 firmware. */
611 INIT_WORK(&state->fw_work, cx25840_work_handler);
612 init_waitqueue_head(&state->fw_wait);
613 q = create_singlethread_workqueue("cx25840_fw");
614 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
615 queue_work(q, &state->fw_work);
616 schedule();
617 finish_wait(&state->fw_wait, &wait);
618 destroy_workqueue(q);
619
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300620 cx25840_std_setup(client);
Steven Tothf2340812008-01-10 01:22:39 -0300621
622 /* (re)set input */
623 set_input(client, state->vid_input, state->aud_input);
624
625 /* start microcontroller */
626 cx25840_and_or(client, 0x803, ~0x10, 0x10);
Andy Walls52fd3dd2010-07-18 22:08:03 -0300627
628 /* Disable and clear video interrupts - we don't use them */
629 cx25840_write4(client, CX25840_VID_INT_STAT_REG, 0xffffffff);
630
631 /* Disable and clear audio interrupts - we don't use them */
632 cx25840_write(client, CX25840_AUD_INT_CTRL_REG, 0xff);
633 cx25840_write(client, CX25840_AUD_INT_STAT_REG, 0xff);
Steven Tothf2340812008-01-10 01:22:39 -0300634}
635
Hans Verkuilbd985162005-11-13 16:07:56 -0800636/* ----------------------------------------------------------------------- */
637
Sri Deevi149783b2009-03-03 06:07:42 -0300638static void cx231xx_initialize(struct i2c_client *client)
639{
640 DEFINE_WAIT(wait);
641 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
642 struct workqueue_struct *q;
643
644 /* Internal Reset */
645 cx25840_and_or(client, 0x102, ~0x01, 0x01);
646 cx25840_and_or(client, 0x102, ~0x01, 0x00);
647
648 /* Stop microcontroller */
649 cx25840_and_or(client, 0x803, ~0x10, 0x00);
650
651 /* DIF in reset? */
652 cx25840_write(client, 0x398, 0);
653
654 /* Trust the default xtal, no division */
655 /* This changes for the cx23888 products */
656 cx25840_write(client, 0x2, 0x76);
657
658 /* Bring down the regulator for AUX clk */
659 cx25840_write(client, 0x1, 0x40);
660
661 /* Disable DIF bypass */
662 cx25840_write4(client, 0x33c, 0x00000001);
663
664 /* DIF Src phase inc */
665 cx25840_write4(client, 0x340, 0x0df7df83);
666
Sri Deevi149783b2009-03-03 06:07:42 -0300667 /* Luma */
668 cx25840_write4(client, 0x414, 0x00107d12);
669
670 /* Chroma */
671 cx25840_write4(client, 0x420, 0x3d008282);
672
Sri Deevi149783b2009-03-03 06:07:42 -0300673 /* ADC2 input select */
674 cx25840_write(client, 0x102, 0x10);
675
676 /* VIN1 & VIN5 */
677 cx25840_write(client, 0x103, 0x11);
678
679 /* Enable format auto detect */
680 cx25840_write(client, 0x400, 0);
681 /* Fast subchroma lock */
682 /* White crush, Chroma AGC & Chroma Killer enabled */
683 cx25840_write(client, 0x401, 0xe8);
684
Sri Deevi149783b2009-03-03 06:07:42 -0300685 /* Do the firmware load in a work handler to prevent.
686 Otherwise the kernel is blocked waiting for the
687 bit-banging i2c interface to finish uploading the
688 firmware. */
689 INIT_WORK(&state->fw_work, cx25840_work_handler);
690 init_waitqueue_head(&state->fw_wait);
691 q = create_singlethread_workqueue("cx25840_fw");
692 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
693 queue_work(q, &state->fw_work);
694 schedule();
695 finish_wait(&state->fw_wait, &wait);
696 destroy_workqueue(q);
697
698 cx25840_std_setup(client);
699
700 /* (re)set input */
701 set_input(client, state->vid_input, state->aud_input);
702
703 /* start microcontroller */
704 cx25840_and_or(client, 0x803, ~0x10, 0x10);
705}
706
707/* ----------------------------------------------------------------------- */
708
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300709void cx25840_std_setup(struct i2c_client *client)
710{
Hans Verkuil9357b312008-11-29 12:50:06 -0300711 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300712 v4l2_std_id std = state->std;
713 int hblank, hactive, burst, vblank, vactive, sc;
714 int vblank656, src_decimation;
715 int luma_lpf, uv_lpf, comb;
716 u32 pll_int, pll_frac, pll_post;
717
718 /* datasheet startup, step 8d */
719 if (std & ~V4L2_STD_NTSC)
720 cx25840_write(client, 0x49f, 0x11);
721 else
722 cx25840_write(client, 0x49f, 0x14);
723
724 if (std & V4L2_STD_625_50) {
725 hblank = 132;
726 hactive = 720;
727 burst = 93;
728 vblank = 36;
729 vactive = 580;
730 vblank656 = 40;
731 src_decimation = 0x21f;
732 luma_lpf = 2;
733
734 if (std & V4L2_STD_SECAM) {
735 uv_lpf = 0;
736 comb = 0;
737 sc = 0x0a425f;
738 } else if (std == V4L2_STD_PAL_Nc) {
739 uv_lpf = 1;
740 comb = 0x20;
741 sc = 556453;
742 } else {
743 uv_lpf = 1;
744 comb = 0x20;
745 sc = 688739;
746 }
747 } else {
748 hactive = 720;
749 hblank = 122;
750 vactive = 487;
751 luma_lpf = 1;
752 uv_lpf = 1;
753
754 src_decimation = 0x21f;
755 if (std == V4L2_STD_PAL_60) {
756 vblank = 26;
757 vblank656 = 26;
758 burst = 0x5b;
759 luma_lpf = 2;
760 comb = 0x20;
761 sc = 688739;
762 } else if (std == V4L2_STD_PAL_M) {
763 vblank = 20;
764 vblank656 = 24;
765 burst = 0x61;
766 comb = 0x20;
767 sc = 555452;
768 } else {
769 vblank = 26;
770 vblank656 = 26;
771 burst = 0x5b;
772 comb = 0x66;
773 sc = 556063;
774 }
775 }
776
777 /* DEBUG: Displays configured PLL frequency */
Andy Walls2a03f032009-09-26 23:47:21 -0300778 if (!is_cx231xx(state)) {
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300779 pll_int = cx25840_read(client, 0x108);
780 pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
781 pll_post = cx25840_read(client, 0x109);
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300782 v4l_dbg(1, cx25840_debug, client,
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300783 "PLL regs = int: %u, frac: %u, post: %u\n",
784 pll_int, pll_frac, pll_post);
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300785
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300786 if (pll_post) {
787 int fin, fsc;
788 int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300789
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300790 pll /= pll_post;
791 v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
792 pll / 1000000, pll % 1000000);
793 v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
794 pll / 8000000, (pll / 8) % 1000000);
795
796 fin = ((u64)src_decimation * pll) >> 12;
797 v4l_dbg(1, cx25840_debug, client,
798 "ADC Sampling freq = %d.%06d MHz\n",
799 fin / 1000000, fin % 1000000);
800
801 fsc = (((u64)sc) * pll) >> 24L;
802 v4l_dbg(1, cx25840_debug, client,
803 "Chroma sub-carrier freq = %d.%06d MHz\n",
804 fsc / 1000000, fsc % 1000000);
805
806 v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
807 "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
808 "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
809 "sc 0x%06x\n",
810 hblank, hactive, vblank, vactive, vblank656,
811 src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
812 }
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300813 }
814
815 /* Sets horizontal blanking delay and active lines */
816 cx25840_write(client, 0x470, hblank);
817 cx25840_write(client, 0x471,
818 0xff & (((hblank >> 8) & 0x3) | (hactive << 4)));
819 cx25840_write(client, 0x472, hactive >> 4);
820
821 /* Sets burst gate delay */
822 cx25840_write(client, 0x473, burst);
823
824 /* Sets vertical blanking delay and active duration */
825 cx25840_write(client, 0x474, vblank);
826 cx25840_write(client, 0x475,
827 0xff & (((vblank >> 8) & 0x3) | (vactive << 4)));
828 cx25840_write(client, 0x476, vactive >> 4);
829 cx25840_write(client, 0x477, vblank656);
830
831 /* Sets src decimation rate */
832 cx25840_write(client, 0x478, 0xff & src_decimation);
833 cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));
834
835 /* Sets Luma and UV Low pass filters */
836 cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
837
838 /* Enables comb filters */
839 cx25840_write(client, 0x47b, comb);
840
841 /* Sets SC Step*/
842 cx25840_write(client, 0x47c, sc);
843 cx25840_write(client, 0x47d, 0xff & sc >> 8);
844 cx25840_write(client, 0x47e, 0xff & sc >> 16);
845
846 /* Sets VBI parameters */
847 if (std & V4L2_STD_625_50) {
848 cx25840_write(client, 0x47f, 0x01);
849 state->vbi_line_offset = 5;
850 } else {
851 cx25840_write(client, 0x47f, 0x00);
852 state->vbi_line_offset = 8;
853 }
854}
855
856/* ----------------------------------------------------------------------- */
857
Hans Verkuilbd985162005-11-13 16:07:56 -0800858static void input_change(struct i2c_client *client)
859{
Hans Verkuil9357b312008-11-29 12:50:06 -0300860 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil081b4962008-04-22 14:45:51 -0300861 v4l2_std_id std = state->std;
Hans Verkuilbd985162005-11-13 16:07:56 -0800862
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300863 /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
864 if (std & V4L2_STD_SECAM) {
865 cx25840_write(client, 0x402, 0);
866 }
867 else {
868 cx25840_write(client, 0x402, 0x04);
869 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
870 }
871 cx25840_and_or(client, 0x401, ~0x60, 0);
872 cx25840_and_or(client, 0x401, ~0x60, 0x60);
Sven Barth5af79f82010-07-10 15:02:21 -0300873
874 /* Don't write into audio registers on cx2583x chips */
875 if (is_cx2583x(state))
876 return;
877
Hans Verkuil82677612007-08-07 07:16:07 -0300878 cx25840_and_or(client, 0x810, ~0x01, 1);
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300879
Hans Verkuil39c4ad62007-08-05 14:24:17 -0300880 if (state->radio) {
881 cx25840_write(client, 0x808, 0xf9);
882 cx25840_write(client, 0x80b, 0x00);
883 }
884 else if (std & V4L2_STD_525_60) {
Hans Verkuild97a11e2006-02-07 06:48:40 -0200885 /* Certain Hauppauge PVR150 models have a hardware bug
886 that causes audio to drop out. For these models the
887 audio standard must be set explicitly.
888 To be precise: it affects cards with tuner models
889 85, 99 and 112 (model numbers from tveeprom). */
890 int hw_fix = state->pvr150_workaround;
891
892 if (std == V4L2_STD_NTSC_M_JP) {
Hans Verkuilf95006f2005-12-01 00:51:42 -0800893 /* Japan uses EIAJ audio standard */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200894 cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
895 } else if (std == V4L2_STD_NTSC_M_KR) {
896 /* South Korea uses A2 audio standard */
897 cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
Hans Verkuilf95006f2005-12-01 00:51:42 -0800898 } else {
899 /* Others use the BTSC audio standard */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200900 cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
Hans Verkuilf95006f2005-12-01 00:51:42 -0800901 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800902 cx25840_write(client, 0x80b, 0x00);
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300903 } else if (std & V4L2_STD_PAL) {
Aleksandr V. Piskunov3c3099d2009-09-25 18:16:21 -0300904 /* Autodetect audio standard and audio system */
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300905 cx25840_write(client, 0x808, 0xff);
Aleksandr V. Piskunov3c3099d2009-09-25 18:16:21 -0300906 /* Since system PAL-L is pretty much non-existant and
907 not used by any public broadcast network, force
908 6.5 MHz carrier to be interpreted as System DK,
909 this avoids DK audio detection instability */
910 cx25840_write(client, 0x80b, 0x00);
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300911 } else if (std & V4L2_STD_SECAM) {
Aleksandr V. Piskunov3c3099d2009-09-25 18:16:21 -0300912 /* Autodetect audio standard and audio system */
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300913 cx25840_write(client, 0x808, 0xff);
Aleksandr V. Piskunov3c3099d2009-09-25 18:16:21 -0300914 /* If only one of SECAM-DK / SECAM-L is required, then force
915 6.5MHz carrier, else autodetect it */
916 if ((std & V4L2_STD_SECAM_DK) &&
917 !(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
918 /* 6.5 MHz carrier to be interpreted as System DK */
919 cx25840_write(client, 0x80b, 0x00);
920 } else if (!(std & V4L2_STD_SECAM_DK) &&
921 (std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
922 /* 6.5 MHz carrier to be interpreted as System L */
923 cx25840_write(client, 0x80b, 0x08);
924 } else {
925 /* 6.5 MHz carrier to be autodetected */
926 cx25840_write(client, 0x80b, 0x10);
927 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800928 }
929
Hans Verkuil82677612007-08-07 07:16:07 -0300930 cx25840_and_or(client, 0x810, ~0x01, 0);
Hans Verkuilbd985162005-11-13 16:07:56 -0800931}
932
Hans Verkuila8bbf122006-01-09 15:25:42 -0200933static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
934 enum cx25840_audio_input aud_input)
Hans Verkuilbd985162005-11-13 16:07:56 -0800935{
Hans Verkuil9357b312008-11-29 12:50:06 -0300936 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuila8bbf122006-01-09 15:25:42 -0200937 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
938 vid_input <= CX25840_COMPOSITE8);
David T.L. Wongfb29ab92009-10-20 12:13:39 -0300939 u8 is_component = (vid_input & CX25840_COMPONENT_ON) ==
940 CX25840_COMPONENT_ON;
941 int luma = vid_input & 0xf0;
942 int chroma = vid_input & 0xf00;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200943 u8 reg;
Hans Verkuilbd985162005-11-13 16:07:56 -0800944
Steven Tothf2340812008-01-10 01:22:39 -0300945 v4l_dbg(1, cx25840_debug, client,
946 "decoder set video input %d, audio input %d\n",
947 vid_input, aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800948
Steven Tothf2340812008-01-10 01:22:39 -0300949 if (vid_input >= CX25840_VIN1_CH1) {
950 v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
951 vid_input);
952 reg = vid_input & 0xff;
Kusanagi Kouichi10e43d92010-01-22 04:54:46 -0300953 is_composite = !is_component &&
954 ((vid_input & CX25840_SVIDEO_ON) != CX25840_SVIDEO_ON);
Steven Tothf2340812008-01-10 01:22:39 -0300955
956 v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
957 reg, is_composite);
David T.L. Wongfb29ab92009-10-20 12:13:39 -0300958 } else if (is_composite) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200959 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
960 } else {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200961 if ((vid_input & ~0xff0) ||
Hans Verkuil45270a12008-06-07 11:18:17 -0300962 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
Hans Verkuila8bbf122006-01-09 15:25:42 -0200963 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
Steven Tothf2340812008-01-10 01:22:39 -0300964 v4l_err(client, "0x%04x is not a valid video input!\n",
965 vid_input);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200966 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800967 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200968 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
969 if (chroma >= CX25840_SVIDEO_CHROMA7) {
970 reg &= 0x3f;
971 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
Hans Verkuilbd985162005-11-13 16:07:56 -0800972 } else {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200973 reg &= 0xcf;
974 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
Hans Verkuilbd985162005-11-13 16:07:56 -0800975 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200976 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800977
Steven Tothf2340812008-01-10 01:22:39 -0300978 /* The caller has previously prepared the correct routing
979 * configuration in reg (for the cx23885) so we have no
980 * need to attempt to flip bits for earlier av decoders.
981 */
Andy Walls2a03f032009-09-26 23:47:21 -0300982 if (!is_cx2388x(state) && !is_cx231xx(state)) {
Steven Tothf2340812008-01-10 01:22:39 -0300983 switch (aud_input) {
984 case CX25840_AUDIO_SERIAL:
985 /* do nothing, use serial audio input */
986 break;
987 case CX25840_AUDIO4: reg &= ~0x30; break;
988 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
989 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
990 case CX25840_AUDIO7: reg &= ~0xc0; break;
991 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
Hans Verkuilbd985162005-11-13 16:07:56 -0800992
Steven Tothf2340812008-01-10 01:22:39 -0300993 default:
994 v4l_err(client, "0x%04x is not a valid audio input!\n",
995 aud_input);
996 return -EINVAL;
997 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800998 }
999
Hans Verkuila8bbf122006-01-09 15:25:42 -02001000 cx25840_write(client, 0x103, reg);
Steven Tothf2340812008-01-10 01:22:39 -03001001
David T.L. Wongfb29ab92009-10-20 12:13:39 -03001002 /* Set INPUT_MODE to Composite, S-Video or Component */
1003 if (is_component)
1004 cx25840_and_or(client, 0x401, ~0x6, 0x6);
1005 else
1006 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
Steven Tothf2340812008-01-10 01:22:39 -03001007
Andy Walls2a03f032009-09-26 23:47:21 -03001008 if (!is_cx2388x(state) && !is_cx231xx(state)) {
Steven Tothf2340812008-01-10 01:22:39 -03001009 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
1010 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
1011 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
1012 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
1013 cx25840_and_or(client, 0x102, ~0x4, 4);
1014 else
1015 cx25840_and_or(client, 0x102, ~0x4, 0);
1016 } else {
David T.L. Wongfb29ab92009-10-20 12:13:39 -03001017 /* Set DUAL_MODE_ADC2 to 1 if component*/
1018 cx25840_and_or(client, 0x102, ~0x4, is_component ? 0x4 : 0x0);
1019 if (is_composite) {
Steven Tothf2340812008-01-10 01:22:39 -03001020 /* ADC2 input select channel 2 */
1021 cx25840_and_or(client, 0x102, ~0x2, 0);
David T.L. Wongfb29ab92009-10-20 12:13:39 -03001022 } else if (!is_component) {
1023 /* S-Video */
1024 if (chroma >= CX25840_SVIDEO_CHROMA7) {
1025 /* ADC2 input select channel 3 */
1026 cx25840_and_or(client, 0x102, ~0x2, 2);
1027 } else {
1028 /* ADC2 input select channel 2 */
1029 cx25840_and_or(client, 0x102, ~0x2, 0);
1030 }
1031 }
Steven Tothf2340812008-01-10 01:22:39 -03001032 }
Hans Verkuila8bbf122006-01-09 15:25:42 -02001033
1034 state->vid_input = vid_input;
1035 state->aud_input = aud_input;
Sven Barth5af79f82010-07-10 15:02:21 -03001036 cx25840_audio_set_path(client);
1037 input_change(client);
Steven Tothf2340812008-01-10 01:22:39 -03001038
Andy Walls2a03f032009-09-26 23:47:21 -03001039 if (is_cx2388x(state)) {
Steven Tothf2340812008-01-10 01:22:39 -03001040 /* Audio channel 1 src : Parallel 1 */
1041 cx25840_write(client, 0x124, 0x03);
1042
1043 /* Select AFE clock pad output source */
1044 cx25840_write(client, 0x144, 0x05);
1045
1046 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
1047 cx25840_write(client, 0x914, 0xa0);
1048
1049 /* I2S_OUT_CTL:
1050 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
1051 * I2S_OUT_MASTER_MODE = Master
1052 */
1053 cx25840_write(client, 0x918, 0xa0);
1054 cx25840_write(client, 0x919, 0x01);
Andy Walls2a03f032009-09-26 23:47:21 -03001055 } else if (is_cx231xx(state)) {
Sri Deevi149783b2009-03-03 06:07:42 -03001056 /* Audio channel 1 src : Parallel 1 */
1057 cx25840_write(client, 0x124, 0x03);
1058
1059 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
1060 cx25840_write(client, 0x914, 0xa0);
1061
1062 /* I2S_OUT_CTL:
1063 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
1064 * I2S_OUT_MASTER_MODE = Master
1065 */
1066 cx25840_write(client, 0x918, 0xa0);
1067 cx25840_write(client, 0x919, 0x01);
Steven Tothf2340812008-01-10 01:22:39 -03001068 }
1069
Hans Verkuilbd985162005-11-13 16:07:56 -08001070 return 0;
1071}
1072
1073/* ----------------------------------------------------------------------- */
1074
Hans Verkuil081b4962008-04-22 14:45:51 -03001075static int set_v4lstd(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -08001076{
Hans Verkuil9357b312008-11-29 12:50:06 -03001077 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil081b4962008-04-22 14:45:51 -03001078 u8 fmt = 0; /* zero is autodetect */
1079 u8 pal_m = 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001080
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -02001081 /* First tests should be against specific std */
Hans Verkuil081b4962008-04-22 14:45:51 -03001082 if (state->std == V4L2_STD_NTSC_M_JP) {
1083 fmt = 0x2;
1084 } else if (state->std == V4L2_STD_NTSC_443) {
1085 fmt = 0x3;
1086 } else if (state->std == V4L2_STD_PAL_M) {
1087 pal_m = 1;
1088 fmt = 0x5;
1089 } else if (state->std == V4L2_STD_PAL_N) {
1090 fmt = 0x6;
1091 } else if (state->std == V4L2_STD_PAL_Nc) {
1092 fmt = 0x7;
1093 } else if (state->std == V4L2_STD_PAL_60) {
1094 fmt = 0x8;
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -02001095 } else {
1096 /* Then, test against generic ones */
Hans Verkuil081b4962008-04-22 14:45:51 -03001097 if (state->std & V4L2_STD_NTSC)
1098 fmt = 0x1;
1099 else if (state->std & V4L2_STD_PAL)
1100 fmt = 0x4;
1101 else if (state->std & V4L2_STD_SECAM)
1102 fmt = 0xc;
Hans Verkuilbd985162005-11-13 16:07:56 -08001103 }
1104
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -03001105 v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
1106
Hans Verkuil73dcddc2006-03-16 20:23:47 -03001107 /* Follow step 9 of section 3.16 in the cx25840 datasheet.
1108 Without this PAL may display a vertical ghosting effect.
1109 This happens for example with the Yuan MPC622. */
1110 if (fmt >= 4 && fmt < 8) {
1111 /* Set format to NTSC-M */
1112 cx25840_and_or(client, 0x400, ~0xf, 1);
1113 /* Turn off LCOMB */
1114 cx25840_and_or(client, 0x47b, ~6, 0);
1115 }
Hans Verkuilbd985162005-11-13 16:07:56 -08001116 cx25840_and_or(client, 0x400, ~0xf, fmt);
Hans Verkuil081b4962008-04-22 14:45:51 -03001117 cx25840_and_or(client, 0x403, ~0x3, pal_m);
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -03001118 cx25840_std_setup(client);
Andy Walls2a03f032009-09-26 23:47:21 -03001119 if (!is_cx2583x(state))
Hans Verkuil081b4962008-04-22 14:45:51 -03001120 input_change(client);
Hans Verkuilbd985162005-11-13 16:07:56 -08001121 return 0;
1122}
1123
Hans Verkuilbd985162005-11-13 16:07:56 -08001124/* ----------------------------------------------------------------------- */
1125
Hans Verkuile34e6582010-08-06 10:55:39 -03001126static int cx25840_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuilbd985162005-11-13 16:07:56 -08001127{
Hans Verkuile34e6582010-08-06 10:55:39 -03001128 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuil9357b312008-11-29 12:50:06 -03001129 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001130
1131 switch (ctrl->id) {
Hans Verkuilbd985162005-11-13 16:07:56 -08001132 case V4L2_CID_BRIGHTNESS:
Hans Verkuile34e6582010-08-06 10:55:39 -03001133 cx25840_write(client, 0x414, ctrl->val - 128);
Hans Verkuilbd985162005-11-13 16:07:56 -08001134 break;
1135
1136 case V4L2_CID_CONTRAST:
Hans Verkuile34e6582010-08-06 10:55:39 -03001137 cx25840_write(client, 0x415, ctrl->val << 1);
Hans Verkuilbd985162005-11-13 16:07:56 -08001138 break;
1139
1140 case V4L2_CID_SATURATION:
Hans Verkuile34e6582010-08-06 10:55:39 -03001141 cx25840_write(client, 0x420, ctrl->val << 1);
1142 cx25840_write(client, 0x421, ctrl->val << 1);
Hans Verkuilbd985162005-11-13 16:07:56 -08001143 break;
1144
1145 case V4L2_CID_HUE:
Hans Verkuile34e6582010-08-06 10:55:39 -03001146 cx25840_write(client, 0x422, ctrl->val);
Hans Verkuilbd985162005-11-13 16:07:56 -08001147 break;
1148
Hans Verkuilbd985162005-11-13 16:07:56 -08001149 default:
1150 return -EINVAL;
1151 }
1152
1153 return 0;
1154}
1155
1156/* ----------------------------------------------------------------------- */
1157
Hans Verkuil96fd0042010-05-09 09:48:50 -03001158static int cx25840_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
Hans Verkuilbd985162005-11-13 16:07:56 -08001159{
Hans Verkuil9357b312008-11-29 12:50:06 -03001160 struct cx25840_state *state = to_state(sd);
1161 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001162 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
Hans Verkuil081b4962008-04-22 14:45:51 -03001163 int is_50Hz = !(state->std & V4L2_STD_525_60);
Hans Verkuilbd985162005-11-13 16:07:56 -08001164
Hans Verkuil96fd0042010-05-09 09:48:50 -03001165 if (fmt->code != V4L2_MBUS_FMT_FIXED)
1166 return -EINVAL;
1167
1168 fmt->field = V4L2_FIELD_INTERLACED;
1169 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
1170
1171 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
1172 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
1173
1174 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
1175 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
1176
1177 Vlines = fmt->height + (is_50Hz ? 4 : 7);
1178
1179 if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||
1180 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
1181 v4l_err(client, "%dx%d is not a valid size!\n",
1182 fmt->width, fmt->height);
1183 return -ERANGE;
1184 }
1185
1186 HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);
1187 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
1188 VSC &= 0x1fff;
1189
1190 if (fmt->width >= 385)
1191 filter = 0;
1192 else if (fmt->width > 192)
1193 filter = 1;
1194 else if (fmt->width > 96)
1195 filter = 2;
1196 else
1197 filter = 3;
1198
1199 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
1200 fmt->width, fmt->height, HSC, VSC);
1201
1202 /* HSCALE=HSC */
1203 cx25840_write(client, 0x418, HSC & 0xff);
1204 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
1205 cx25840_write(client, 0x41a, HSC >> 16);
1206 /* VSCALE=VSC */
1207 cx25840_write(client, 0x41c, VSC & 0xff);
1208 cx25840_write(client, 0x41d, VSC >> 8);
1209 /* VS_INTRLACE=1 VFILT=filter */
1210 cx25840_write(client, 0x41e, 0x8 | filter);
1211 return 0;
1212}
1213
Hans Verkuilbd985162005-11-13 16:07:56 -08001214/* ----------------------------------------------------------------------- */
1215
Hans Verkuil1a392752007-09-13 11:44:47 -03001216static void log_video_status(struct i2c_client *client)
1217{
1218 static const char *const fmt_strs[] = {
1219 "0x0",
1220 "NTSC-M", "NTSC-J", "NTSC-4.43",
1221 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
1222 "0x9", "0xA", "0xB",
1223 "SECAM",
1224 "0xD", "0xE", "0xF"
1225 };
1226
Hans Verkuil9357b312008-11-29 12:50:06 -03001227 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil1a392752007-09-13 11:44:47 -03001228 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
1229 u8 gen_stat1 = cx25840_read(client, 0x40d);
1230 u8 gen_stat2 = cx25840_read(client, 0x40e);
1231 int vid_input = state->vid_input;
1232
1233 v4l_info(client, "Video signal: %spresent\n",
1234 (gen_stat2 & 0x20) ? "" : "not ");
1235 v4l_info(client, "Detected format: %s\n",
1236 fmt_strs[gen_stat1 & 0xf]);
1237
1238 v4l_info(client, "Specified standard: %s\n",
1239 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1240
1241 if (vid_input >= CX25840_COMPOSITE1 &&
1242 vid_input <= CX25840_COMPOSITE8) {
1243 v4l_info(client, "Specified video input: Composite %d\n",
1244 vid_input - CX25840_COMPOSITE1 + 1);
1245 } else {
1246 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1247 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1248 }
1249
1250 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1251}
1252
1253/* ----------------------------------------------------------------------- */
1254
1255static void log_audio_status(struct i2c_client *client)
1256{
Hans Verkuil9357b312008-11-29 12:50:06 -03001257 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil1a392752007-09-13 11:44:47 -03001258 u8 download_ctl = cx25840_read(client, 0x803);
1259 u8 mod_det_stat0 = cx25840_read(client, 0x804);
1260 u8 mod_det_stat1 = cx25840_read(client, 0x805);
1261 u8 audio_config = cx25840_read(client, 0x808);
1262 u8 pref_mode = cx25840_read(client, 0x809);
1263 u8 afc0 = cx25840_read(client, 0x80b);
1264 u8 mute_ctl = cx25840_read(client, 0x8d3);
1265 int aud_input = state->aud_input;
1266 char *p;
1267
1268 switch (mod_det_stat0) {
1269 case 0x00: p = "mono"; break;
1270 case 0x01: p = "stereo"; break;
1271 case 0x02: p = "dual"; break;
1272 case 0x04: p = "tri"; break;
1273 case 0x10: p = "mono with SAP"; break;
1274 case 0x11: p = "stereo with SAP"; break;
1275 case 0x12: p = "dual with SAP"; break;
1276 case 0x14: p = "tri with SAP"; break;
1277 case 0xfe: p = "forced mode"; break;
1278 default: p = "not defined";
1279 }
1280 v4l_info(client, "Detected audio mode: %s\n", p);
1281
1282 switch (mod_det_stat1) {
1283 case 0x00: p = "not defined"; break;
1284 case 0x01: p = "EIAJ"; break;
1285 case 0x02: p = "A2-M"; break;
1286 case 0x03: p = "A2-BG"; break;
1287 case 0x04: p = "A2-DK1"; break;
1288 case 0x05: p = "A2-DK2"; break;
1289 case 0x06: p = "A2-DK3"; break;
1290 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1291 case 0x08: p = "AM-L"; break;
1292 case 0x09: p = "NICAM-BG"; break;
1293 case 0x0a: p = "NICAM-DK"; break;
1294 case 0x0b: p = "NICAM-I"; break;
1295 case 0x0c: p = "NICAM-L"; break;
1296 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1297 case 0x0e: p = "IF FM Radio"; break;
1298 case 0x0f: p = "BTSC"; break;
1299 case 0x10: p = "high-deviation FM"; break;
1300 case 0x11: p = "very high-deviation FM"; break;
1301 case 0xfd: p = "unknown audio standard"; break;
1302 case 0xfe: p = "forced audio standard"; break;
1303 case 0xff: p = "no detected audio standard"; break;
1304 default: p = "not defined";
1305 }
1306 v4l_info(client, "Detected audio standard: %s\n", p);
Hans Verkuil1a392752007-09-13 11:44:47 -03001307 v4l_info(client, "Audio microcontroller: %s\n",
1308 (download_ctl & 0x10) ?
1309 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
1310
1311 switch (audio_config >> 4) {
1312 case 0x00: p = "undefined"; break;
1313 case 0x01: p = "BTSC"; break;
1314 case 0x02: p = "EIAJ"; break;
1315 case 0x03: p = "A2-M"; break;
1316 case 0x04: p = "A2-BG"; break;
1317 case 0x05: p = "A2-DK1"; break;
1318 case 0x06: p = "A2-DK2"; break;
1319 case 0x07: p = "A2-DK3"; break;
1320 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1321 case 0x09: p = "AM-L"; break;
1322 case 0x0a: p = "NICAM-BG"; break;
1323 case 0x0b: p = "NICAM-DK"; break;
1324 case 0x0c: p = "NICAM-I"; break;
1325 case 0x0d: p = "NICAM-L"; break;
1326 case 0x0e: p = "FM radio"; break;
1327 case 0x0f: p = "automatic detection"; break;
1328 default: p = "undefined";
1329 }
1330 v4l_info(client, "Configured audio standard: %s\n", p);
1331
1332 if ((audio_config >> 4) < 0xF) {
1333 switch (audio_config & 0xF) {
1334 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1335 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1336 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1337 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1338 case 0x04: p = "STEREO"; break;
1339 case 0x05: p = "DUAL1 (AB)"; break;
1340 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1341 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1342 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1343 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1344 case 0x0a: p = "SAP"; break;
1345 default: p = "undefined";
1346 }
1347 v4l_info(client, "Configured audio mode: %s\n", p);
1348 } else {
1349 switch (audio_config & 0xF) {
1350 case 0x00: p = "BG"; break;
1351 case 0x01: p = "DK1"; break;
1352 case 0x02: p = "DK2"; break;
1353 case 0x03: p = "DK3"; break;
1354 case 0x04: p = "I"; break;
1355 case 0x05: p = "L"; break;
1356 case 0x06: p = "BTSC"; break;
1357 case 0x07: p = "EIAJ"; break;
1358 case 0x08: p = "A2-M"; break;
1359 case 0x09: p = "FM Radio"; break;
1360 case 0x0f: p = "automatic standard and mode detection"; break;
1361 default: p = "undefined";
1362 }
1363 v4l_info(client, "Configured audio system: %s\n", p);
1364 }
1365
1366 if (aud_input) {
1367 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
1368 } else {
1369 v4l_info(client, "Specified audio input: External\n");
1370 }
1371
1372 switch (pref_mode & 0xf) {
1373 case 0: p = "mono/language A"; break;
1374 case 1: p = "language B"; break;
1375 case 2: p = "language C"; break;
1376 case 3: p = "analog fallback"; break;
1377 case 4: p = "stereo"; break;
1378 case 5: p = "language AC"; break;
1379 case 6: p = "language BC"; break;
1380 case 7: p = "language AB"; break;
1381 default: p = "undefined";
1382 }
1383 v4l_info(client, "Preferred audio mode: %s\n", p);
1384
1385 if ((audio_config & 0xf) == 0xf) {
1386 switch ((afc0 >> 3) & 0x3) {
1387 case 0: p = "system DK"; break;
1388 case 1: p = "system L"; break;
1389 case 2: p = "autodetect"; break;
1390 default: p = "undefined";
1391 }
1392 v4l_info(client, "Selected 65 MHz format: %s\n", p);
1393
1394 switch (afc0 & 0x7) {
1395 case 0: p = "chroma"; break;
1396 case 1: p = "BTSC"; break;
1397 case 2: p = "EIAJ"; break;
1398 case 3: p = "A2-M"; break;
1399 case 4: p = "autodetect"; break;
1400 default: p = "undefined";
1401 }
1402 v4l_info(client, "Selected 45 MHz format: %s\n", p);
1403 }
1404}
1405
1406/* ----------------------------------------------------------------------- */
1407
Hans Verkuilcc26b072009-03-29 19:20:26 -03001408/* This load_fw operation must be called to load the driver's firmware.
Hans Verkuil6ca187a2009-01-15 05:53:18 -03001409 Without this the audio standard detection will fail and you will
1410 only get mono.
1411
1412 Since loading the firmware is often problematic when the driver is
1413 compiled into the kernel I recommend postponing calling this function
1414 until the first open of the video device. Another reason for
1415 postponing it is that loading this firmware takes a long time (seconds)
1416 due to the slow i2c bus speed. So it will speed up the boot process if
1417 you can avoid loading the fw as long as the video device isn't used. */
Hans Verkuilcc26b072009-03-29 19:20:26 -03001418static int cx25840_load_fw(struct v4l2_subdev *sd)
Hans Verkuilbd985162005-11-13 16:07:56 -08001419{
Hans Verkuil9357b312008-11-29 12:50:06 -03001420 struct cx25840_state *state = to_state(sd);
1421 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilc976bc82007-07-22 12:52:40 -03001422
1423 if (!state->is_initialized) {
Hans Verkuilcc26b072009-03-29 19:20:26 -03001424 /* initialize and load firmware */
Hans Verkuilc976bc82007-07-22 12:52:40 -03001425 state->is_initialized = 1;
Andy Walls2a03f032009-09-26 23:47:21 -03001426 if (is_cx2583x(state))
Hans Verkuilc976bc82007-07-22 12:52:40 -03001427 cx25836_initialize(client);
Andy Walls2a03f032009-09-26 23:47:21 -03001428 else if (is_cx2388x(state))
Steven Tothf2340812008-01-10 01:22:39 -03001429 cx23885_initialize(client);
Andy Walls2a03f032009-09-26 23:47:21 -03001430 else if (is_cx231xx(state))
Sri Deevi149783b2009-03-03 06:07:42 -03001431 cx231xx_initialize(client);
Hans Verkuilc976bc82007-07-22 12:52:40 -03001432 else
Hans Verkuil89fc4eb2007-08-04 05:00:07 -03001433 cx25840_initialize(client);
Hans Verkuilc976bc82007-07-22 12:52:40 -03001434 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001435 return 0;
1436}
Hans Verkuilc976bc82007-07-22 12:52:40 -03001437
Hans Verkuilbd985162005-11-13 16:07:56 -08001438#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001439static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil9357b312008-11-29 12:50:06 -03001440{
1441 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001442
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001443 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil9357b312008-11-29 12:50:06 -03001444 return -EINVAL;
1445 if (!capable(CAP_SYS_ADMIN))
1446 return -EPERM;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001447 reg->size = 1;
Hans Verkuil9357b312008-11-29 12:50:06 -03001448 reg->val = cx25840_read(client, reg->reg & 0x0fff);
1449 return 0;
1450}
Steven Tothf2340812008-01-10 01:22:39 -03001451
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001452static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil9357b312008-11-29 12:50:06 -03001453{
1454 struct i2c_client *client = v4l2_get_subdevdata(sd);
1455
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001456 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil9357b312008-11-29 12:50:06 -03001457 return -EINVAL;
1458 if (!capable(CAP_SYS_ADMIN))
1459 return -EPERM;
1460 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
1461 return 0;
1462}
Hans Verkuilbd985162005-11-13 16:07:56 -08001463#endif
1464
Andy Walls3ccc6462009-12-24 13:06:08 -03001465static int cx25840_s_audio_stream(struct v4l2_subdev *sd, int enable)
1466{
1467 struct cx25840_state *state = to_state(sd);
1468 struct i2c_client *client = v4l2_get_subdevdata(sd);
1469 u8 v;
1470
1471 if (is_cx2583x(state) || is_cx2388x(state) || is_cx231xx(state))
1472 return 0;
1473
1474 v4l_dbg(1, cx25840_debug, client, "%s audio output\n",
1475 enable ? "enable" : "disable");
1476
1477 if (enable) {
1478 v = cx25840_read(client, 0x115) | 0x80;
1479 cx25840_write(client, 0x115, v);
1480 v = cx25840_read(client, 0x116) | 0x03;
1481 cx25840_write(client, 0x116, v);
1482 } else {
1483 v = cx25840_read(client, 0x115) & ~(0x80);
1484 cx25840_write(client, 0x115, v);
1485 v = cx25840_read(client, 0x116) & ~(0x03);
1486 cx25840_write(client, 0x116, v);
1487 }
1488 return 0;
1489}
1490
Hans Verkuil9357b312008-11-29 12:50:06 -03001491static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
1492{
1493 struct cx25840_state *state = to_state(sd);
1494 struct i2c_client *client = v4l2_get_subdevdata(sd);
Andy Walls3ccc6462009-12-24 13:06:08 -03001495 u8 v;
Hans Verkuil9357b312008-11-29 12:50:06 -03001496
Andy Walls3ccc6462009-12-24 13:06:08 -03001497 v4l_dbg(1, cx25840_debug, client, "%s video output\n",
Hans Verkuil9357b312008-11-29 12:50:06 -03001498 enable ? "enable" : "disable");
1499 if (enable) {
Andy Walls2a03f032009-09-26 23:47:21 -03001500 if (is_cx2388x(state) || is_cx231xx(state)) {
Andy Walls3ccc6462009-12-24 13:06:08 -03001501 v = cx25840_read(client, 0x421) | 0x0b;
Steven Tothf2340812008-01-10 01:22:39 -03001502 cx25840_write(client, 0x421, v);
1503 } else {
Andy Walls3ccc6462009-12-24 13:06:08 -03001504 v = cx25840_read(client, 0x115) | 0x0c;
1505 cx25840_write(client, 0x115, v);
1506 v = cx25840_read(client, 0x116) | 0x04;
1507 cx25840_write(client, 0x116, v);
Steven Tothf2340812008-01-10 01:22:39 -03001508 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001509 } else {
Andy Walls2a03f032009-09-26 23:47:21 -03001510 if (is_cx2388x(state) || is_cx231xx(state)) {
Andy Walls3ccc6462009-12-24 13:06:08 -03001511 v = cx25840_read(client, 0x421) & ~(0x0b);
Steven Tothf2340812008-01-10 01:22:39 -03001512 cx25840_write(client, 0x421, v);
1513 } else {
Andy Walls3ccc6462009-12-24 13:06:08 -03001514 v = cx25840_read(client, 0x115) & ~(0x0c);
1515 cx25840_write(client, 0x115, v);
1516 v = cx25840_read(client, 0x116) & ~(0x04);
1517 cx25840_write(client, 0x116, v);
Steven Tothf2340812008-01-10 01:22:39 -03001518 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001519 }
1520 return 0;
1521}
1522
Hans Verkuil9357b312008-11-29 12:50:06 -03001523static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1524{
1525 struct cx25840_state *state = to_state(sd);
1526 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001527
Hans Verkuil9357b312008-11-29 12:50:06 -03001528 if (state->radio == 0 && state->std == std)
1529 return 0;
1530 state->radio = 0;
1531 state->std = std;
1532 return set_v4lstd(client);
1533}
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001534
Hans Verkuil9357b312008-11-29 12:50:06 -03001535static int cx25840_s_radio(struct v4l2_subdev *sd)
1536{
1537 struct cx25840_state *state = to_state(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001538
Hans Verkuil9357b312008-11-29 12:50:06 -03001539 state->radio = 1;
1540 return 0;
1541}
Hans Verkuilbd985162005-11-13 16:07:56 -08001542
Hans Verkuil5325b422009-04-02 11:26:22 -03001543static int cx25840_s_video_routing(struct v4l2_subdev *sd,
1544 u32 input, u32 output, u32 config)
Hans Verkuil9357b312008-11-29 12:50:06 -03001545{
1546 struct cx25840_state *state = to_state(sd);
1547 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001548
Hans Verkuil5325b422009-04-02 11:26:22 -03001549 return set_input(client, input, state->aud_input);
Hans Verkuil9357b312008-11-29 12:50:06 -03001550}
Hans Verkuila8bbf122006-01-09 15:25:42 -02001551
Hans Verkuil5325b422009-04-02 11:26:22 -03001552static int cx25840_s_audio_routing(struct v4l2_subdev *sd,
1553 u32 input, u32 output, u32 config)
Hans Verkuil9357b312008-11-29 12:50:06 -03001554{
1555 struct cx25840_state *state = to_state(sd);
1556 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuila8bbf122006-01-09 15:25:42 -02001557
Hans Verkuil5325b422009-04-02 11:26:22 -03001558 return set_input(client, state->vid_input, input);
Hans Verkuil9357b312008-11-29 12:50:06 -03001559}
Hans Verkuilbd985162005-11-13 16:07:56 -08001560
Hans Verkuil9357b312008-11-29 12:50:06 -03001561static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
1562{
Hans Verkuil9357b312008-11-29 12:50:06 -03001563 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001564
Sven Barth5af79f82010-07-10 15:02:21 -03001565 input_change(client);
Hans Verkuil9357b312008-11-29 12:50:06 -03001566 return 0;
1567}
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001568
Hans Verkuil9357b312008-11-29 12:50:06 -03001569static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1570{
1571 struct cx25840_state *state = to_state(sd);
1572 struct i2c_client *client = v4l2_get_subdevdata(sd);
1573 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
1574 u8 mode;
1575 int val = 0;
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001576
Hans Verkuil9357b312008-11-29 12:50:06 -03001577 if (state->radio)
1578 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001579
Hans Verkuil9357b312008-11-29 12:50:06 -03001580 vt->signal = vpres ? 0xffff : 0x0;
Andy Walls2a03f032009-09-26 23:47:21 -03001581 if (is_cx2583x(state))
Hans Verkuil9357b312008-11-29 12:50:06 -03001582 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001583
Hans Verkuil9357b312008-11-29 12:50:06 -03001584 vt->capability |=
1585 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
1586 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
Hans Verkuilbd985162005-11-13 16:07:56 -08001587
Hans Verkuil9357b312008-11-29 12:50:06 -03001588 mode = cx25840_read(client, 0x804);
Hans Verkuilbd985162005-11-13 16:07:56 -08001589
Hans Verkuil9357b312008-11-29 12:50:06 -03001590 /* get rxsubchans and audmode */
1591 if ((mode & 0xf) == 1)
1592 val |= V4L2_TUNER_SUB_STEREO;
1593 else
1594 val |= V4L2_TUNER_SUB_MONO;
Hans Verkuilbd985162005-11-13 16:07:56 -08001595
Hans Verkuil9357b312008-11-29 12:50:06 -03001596 if (mode == 2 || mode == 4)
1597 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuilbd985162005-11-13 16:07:56 -08001598
Hans Verkuil9357b312008-11-29 12:50:06 -03001599 if (mode & 0x10)
1600 val |= V4L2_TUNER_SUB_SAP;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001601
Hans Verkuil9357b312008-11-29 12:50:06 -03001602 vt->rxsubchans = val;
1603 vt->audmode = state->audmode;
1604 return 0;
1605}
1606
1607static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1608{
1609 struct cx25840_state *state = to_state(sd);
1610 struct i2c_client *client = v4l2_get_subdevdata(sd);
1611
Andy Walls2a03f032009-09-26 23:47:21 -03001612 if (state->radio || is_cx2583x(state))
Hans Verkuil9357b312008-11-29 12:50:06 -03001613 return 0;
1614
1615 switch (vt->audmode) {
Hans Verkuilbd985162005-11-13 16:07:56 -08001616 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001617 /* mono -> mono
1618 stereo -> mono
1619 bilingual -> lang1 */
Hans Verkuilbd985162005-11-13 16:07:56 -08001620 cx25840_and_or(client, 0x809, ~0xf, 0x00);
1621 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -03001622 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001623 case V4L2_TUNER_MODE_LANG1:
1624 /* mono -> mono
1625 stereo -> stereo
1626 bilingual -> lang1 */
Hans Verkuilbd985162005-11-13 16:07:56 -08001627 cx25840_and_or(client, 0x809, ~0xf, 0x04);
1628 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -03001629 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001630 /* mono -> mono
1631 stereo -> stereo
1632 bilingual -> lang1/lang2 */
1633 cx25840_and_or(client, 0x809, ~0xf, 0x07);
1634 break;
Hans Verkuilbd985162005-11-13 16:07:56 -08001635 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001636 /* mono -> mono
Hans Verkuil301e22d2006-03-18 17:15:00 -03001637 stereo -> stereo
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001638 bilingual -> lang2 */
Hans Verkuilbd985162005-11-13 16:07:56 -08001639 cx25840_and_or(client, 0x809, ~0xf, 0x01);
1640 break;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001641 default:
1642 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -08001643 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001644 state->audmode = vt->audmode;
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001645 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001646}
1647
Hans Verkuil9357b312008-11-29 12:50:06 -03001648static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
1649{
1650 struct cx25840_state *state = to_state(sd);
1651 struct i2c_client *client = v4l2_get_subdevdata(sd);
1652
Andy Walls2a03f032009-09-26 23:47:21 -03001653 if (is_cx2583x(state))
Hans Verkuil9357b312008-11-29 12:50:06 -03001654 cx25836_initialize(client);
Andy Walls2a03f032009-09-26 23:47:21 -03001655 else if (is_cx2388x(state))
Hans Verkuil9357b312008-11-29 12:50:06 -03001656 cx23885_initialize(client);
Andy Walls2a03f032009-09-26 23:47:21 -03001657 else if (is_cx231xx(state))
Sri Deevi149783b2009-03-03 06:07:42 -03001658 cx231xx_initialize(client);
Hans Verkuil9357b312008-11-29 12:50:06 -03001659 else
1660 cx25840_initialize(client);
1661 return 0;
1662}
1663
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001664static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuil9357b312008-11-29 12:50:06 -03001665{
1666 struct cx25840_state *state = to_state(sd);
1667 struct i2c_client *client = v4l2_get_subdevdata(sd);
1668
1669 return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
1670}
1671
1672static int cx25840_log_status(struct v4l2_subdev *sd)
1673{
1674 struct cx25840_state *state = to_state(sd);
1675 struct i2c_client *client = v4l2_get_subdevdata(sd);
1676
1677 log_video_status(client);
Andy Walls2a03f032009-09-26 23:47:21 -03001678 if (!is_cx2583x(state))
Hans Verkuil9357b312008-11-29 12:50:06 -03001679 log_audio_status(client);
Andy Walls52fd3dd2010-07-18 22:08:03 -03001680 cx25840_ir_log_status(sd);
Hans Verkuile34e6582010-08-06 10:55:39 -03001681 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
Hans Verkuil9357b312008-11-29 12:50:06 -03001682 return 0;
1683}
1684
Andy Walls52fd3dd2010-07-18 22:08:03 -03001685static int cx23885_irq_handler(struct v4l2_subdev *sd, u32 status,
1686 bool *handled)
1687{
1688 struct cx25840_state *state = to_state(sd);
1689 struct i2c_client *c = v4l2_get_subdevdata(sd);
1690 u8 irq_stat, aud_stat, aud_en, ir_stat, ir_en;
1691 u32 vid_stat, aud_mc_stat;
1692 bool block_handled;
1693 int ret = 0;
1694
1695 irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
1696 v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (entry): %s %s %s\n",
1697 irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",
1698 irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",
1699 irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");
1700
1701 if ((is_cx23885(state) || is_cx23887(state))) {
1702 ir_stat = cx25840_read(c, CX25840_IR_STATS_REG);
1703 ir_en = cx25840_read(c, CX25840_IR_IRQEN_REG);
1704 v4l_dbg(2, cx25840_debug, c,
1705 "AV Core ir IRQ status: %#04x disables: %#04x\n",
1706 ir_stat, ir_en);
1707 if (irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT) {
1708 block_handled = false;
1709 ret = cx25840_ir_irq_handler(sd,
1710 status, &block_handled);
1711 if (block_handled)
1712 *handled = true;
1713 }
1714 }
1715
1716 aud_stat = cx25840_read(c, CX25840_AUD_INT_STAT_REG);
1717 aud_en = cx25840_read(c, CX25840_AUD_INT_CTRL_REG);
1718 v4l_dbg(2, cx25840_debug, c,
1719 "AV Core audio IRQ status: %#04x disables: %#04x\n",
1720 aud_stat, aud_en);
1721 aud_mc_stat = cx25840_read4(c, CX23885_AUD_MC_INT_MASK_REG);
1722 v4l_dbg(2, cx25840_debug, c,
1723 "AV Core audio MC IRQ status: %#06x enables: %#06x\n",
1724 aud_mc_stat >> CX23885_AUD_MC_INT_STAT_SHFT,
1725 aud_mc_stat & CX23885_AUD_MC_INT_CTRL_BITS);
1726 if (irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT) {
1727 if (aud_stat) {
1728 cx25840_write(c, CX25840_AUD_INT_STAT_REG, aud_stat);
1729 *handled = true;
1730 }
1731 }
1732
1733 vid_stat = cx25840_read4(c, CX25840_VID_INT_STAT_REG);
1734 v4l_dbg(2, cx25840_debug, c,
1735 "AV Core video IRQ status: %#06x disables: %#06x\n",
1736 vid_stat & CX25840_VID_INT_STAT_BITS,
1737 vid_stat >> CX25840_VID_INT_MASK_SHFT);
1738 if (irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT) {
1739 if (vid_stat & CX25840_VID_INT_STAT_BITS) {
1740 cx25840_write4(c, CX25840_VID_INT_STAT_REG, vid_stat);
1741 *handled = true;
1742 }
1743 }
1744
1745 irq_stat = cx25840_read(c, CX23885_PIN_CTRL_IRQ_REG);
1746 v4l_dbg(2, cx25840_debug, c, "AV Core IRQ status (exit): %s %s %s\n",
1747 irq_stat & CX23885_PIN_CTRL_IRQ_IR_STAT ? "ir" : " ",
1748 irq_stat & CX23885_PIN_CTRL_IRQ_AUD_STAT ? "aud" : " ",
1749 irq_stat & CX23885_PIN_CTRL_IRQ_VID_STAT ? "vid" : " ");
1750
1751 return ret;
1752}
1753
1754static int cx25840_irq_handler(struct v4l2_subdev *sd, u32 status,
1755 bool *handled)
1756{
1757 struct cx25840_state *state = to_state(sd);
1758
1759 *handled = false;
1760
1761 /* Only support the CX2388[578] AV Core for now */
1762 if (is_cx2388x(state))
1763 return cx23885_irq_handler(sd, status, handled);
1764
1765 return -ENODEV;
1766}
1767
Hans Verkuil9357b312008-11-29 12:50:06 -03001768/* ----------------------------------------------------------------------- */
1769
Hans Verkuile34e6582010-08-06 10:55:39 -03001770static const struct v4l2_ctrl_ops cx25840_ctrl_ops = {
1771 .s_ctrl = cx25840_s_ctrl,
1772};
1773
Hans Verkuil9357b312008-11-29 12:50:06 -03001774static const struct v4l2_subdev_core_ops cx25840_core_ops = {
1775 .log_status = cx25840_log_status,
1776 .g_chip_ident = cx25840_g_chip_ident,
Hans Verkuile34e6582010-08-06 10:55:39 -03001777 .g_ctrl = v4l2_subdev_g_ctrl,
1778 .s_ctrl = v4l2_subdev_s_ctrl,
1779 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1780 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1781 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1782 .queryctrl = v4l2_subdev_queryctrl,
1783 .querymenu = v4l2_subdev_querymenu,
Hans Verkuilf41737e2009-04-01 03:52:39 -03001784 .s_std = cx25840_s_std,
Hans Verkuil9357b312008-11-29 12:50:06 -03001785 .reset = cx25840_reset,
Hans Verkuilcc26b072009-03-29 19:20:26 -03001786 .load_fw = cx25840_load_fw,
Andy Wallsd06d5772010-07-18 19:39:54 -03001787 .s_io_pin_config = common_s_io_pin_config,
Hans Verkuil9357b312008-11-29 12:50:06 -03001788#ifdef CONFIG_VIDEO_ADV_DEBUG
1789 .g_register = cx25840_g_register,
1790 .s_register = cx25840_s_register,
1791#endif
Andy Walls52fd3dd2010-07-18 22:08:03 -03001792 .interrupt_service_routine = cx25840_irq_handler,
Hans Verkuil9357b312008-11-29 12:50:06 -03001793};
1794
1795static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
1796 .s_frequency = cx25840_s_frequency,
Hans Verkuil9357b312008-11-29 12:50:06 -03001797 .s_radio = cx25840_s_radio,
1798 .g_tuner = cx25840_g_tuner,
1799 .s_tuner = cx25840_s_tuner,
1800};
1801
1802static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
1803 .s_clock_freq = cx25840_s_clock_freq,
1804 .s_routing = cx25840_s_audio_routing,
Andy Walls3ccc6462009-12-24 13:06:08 -03001805 .s_stream = cx25840_s_audio_stream,
Hans Verkuil9357b312008-11-29 12:50:06 -03001806};
1807
1808static const struct v4l2_subdev_video_ops cx25840_video_ops = {
1809 .s_routing = cx25840_s_video_routing,
Hans Verkuil96fd0042010-05-09 09:48:50 -03001810 .s_mbus_fmt = cx25840_s_mbus_fmt,
Hans Verkuil9357b312008-11-29 12:50:06 -03001811 .s_stream = cx25840_s_stream,
1812};
1813
Hans Verkuil32cd5272010-03-14 09:57:30 -03001814static const struct v4l2_subdev_vbi_ops cx25840_vbi_ops = {
1815 .decode_vbi_line = cx25840_decode_vbi_line,
Hans Verkuil5393db42010-03-14 10:16:46 -03001816 .s_raw_fmt = cx25840_s_raw_fmt,
1817 .s_sliced_fmt = cx25840_s_sliced_fmt,
1818 .g_sliced_fmt = cx25840_g_sliced_fmt,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001819};
1820
Hans Verkuil9357b312008-11-29 12:50:06 -03001821static const struct v4l2_subdev_ops cx25840_ops = {
1822 .core = &cx25840_core_ops,
1823 .tuner = &cx25840_tuner_ops,
1824 .audio = &cx25840_audio_ops,
1825 .video = &cx25840_video_ops,
Hans Verkuil32cd5272010-03-14 09:57:30 -03001826 .vbi = &cx25840_vbi_ops,
Andy Walls52fd3dd2010-07-18 22:08:03 -03001827 .ir = &cx25840_ir_ops,
Hans Verkuil9357b312008-11-29 12:50:06 -03001828};
1829
Hans Verkuilbd985162005-11-13 16:07:56 -08001830/* ----------------------------------------------------------------------- */
1831
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001832static u32 get_cx2388x_ident(struct i2c_client *client)
1833{
1834 u32 ret;
1835
1836 /* Come out of digital power down */
1837 cx25840_write(client, 0x000, 0);
1838
Steven Toth8c2d7822009-09-27 15:54:47 -03001839 /* Detecting whether the part is cx23885/7/8 is more
1840 * difficult than it needs to be. No ID register. Instead we
1841 * probe certain registers indicated in the datasheets to look
1842 * for specific defaults that differ between the silicon designs. */
1843
1844 /* It's either 885/7 if the IR Tx Clk Divider register exists */
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001845 if (cx25840_read4(client, 0x204) & 0xffff) {
Steven Toth8c2d7822009-09-27 15:54:47 -03001846 /* CX23885 returns bogus repetitive byte values for the DIF,
1847 * which doesn't exist for it. (Ex. 8a8a8a8a or 31313131) */
1848 ret = cx25840_read4(client, 0x300);
1849 if (((ret & 0xffff0000) >> 16) == (ret & 0xffff)) {
1850 /* No DIF */
1851 ret = V4L2_IDENT_CX23885_AV;
1852 } else {
1853 /* CX23887 has a broken DIF, but the registers
1854 * appear valid (but unsed), good enough to detect. */
1855 ret = V4L2_IDENT_CX23887_AV;
1856 }
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001857 } else if (cx25840_read4(client, 0x300) & 0x0fffffff) {
1858 /* DIF PLL Freq Word reg exists; chip must be a CX23888 */
1859 ret = V4L2_IDENT_CX23888_AV;
1860 } else {
Steven Toth8c2d7822009-09-27 15:54:47 -03001861 v4l_err(client, "Unable to detect h/w, assuming cx23887\n");
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001862 ret = V4L2_IDENT_CX23887_AV;
1863 }
1864
1865 /* Back into digital power down */
1866 cx25840_write(client, 0x000, 2);
1867 return ret;
1868}
1869
Jean Delvared2653e92008-04-29 23:11:39 +02001870static int cx25840_probe(struct i2c_client *client,
1871 const struct i2c_device_id *did)
Hans Verkuilbd985162005-11-13 16:07:56 -08001872{
Hans Verkuilbd985162005-11-13 16:07:56 -08001873 struct cx25840_state *state;
Hans Verkuil9357b312008-11-29 12:50:06 -03001874 struct v4l2_subdev *sd;
Hans Verkuile34e6582010-08-06 10:55:39 -03001875 int default_volume;
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001876 u32 id = V4L2_IDENT_NONE;
Hans Verkuilbd985162005-11-13 16:07:56 -08001877 u16 device_id;
1878
Hans Verkuil188f3452007-09-16 10:47:15 -03001879 /* Check if the adapter supports the needed features */
1880 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1881 return -EIO;
1882
Hans Verkuil21340ae2007-08-26 10:53:16 -03001883 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
Hans Verkuilbd985162005-11-13 16:07:56 -08001884
1885 device_id = cx25840_read(client, 0x101) << 8;
1886 device_id |= cx25840_read(client, 0x100);
Steven Tothf2340812008-01-10 01:22:39 -03001887 v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
Hans Verkuilbd985162005-11-13 16:07:56 -08001888
1889 /* The high byte of the device ID should be
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001890 * 0x83 for the cx2583x and 0x84 for the cx2584x */
1891 if ((device_id & 0xff00) == 0x8300) {
1892 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001893 } else if ((device_id & 0xff00) == 0x8400) {
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001894 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
Hans Verkuil00ca7322009-03-13 13:36:00 -03001895 } else if (device_id == 0x0000) {
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001896 id = get_cx2388x_ident(client);
Sri Deevi149783b2009-03-03 06:07:42 -03001897 } else if ((device_id & 0xfff0) == 0x5A30) {
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001898 /* The CX23100 (0x5A3C = 23100) doesn't have an A/V decoder */
1899 id = V4L2_IDENT_CX2310X_AV;
1900 } else if ((device_id & 0xff) == (device_id >> 8)) {
1901 v4l_err(client,
1902 "likely a confused/unresponsive cx2388[578] A/V decoder"
1903 " found @ 0x%x (%s)\n",
1904 client->addr << 1, client->adapter->name);
1905 v4l_err(client, "A method to reset it from the cx25840 driver"
1906 " software is not known at this time\n");
1907 return -ENODEV;
1908 } else {
Hans Verkuilb5fc7142006-01-11 22:41:36 -02001909 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
Hans Verkuil188f3452007-09-16 10:47:15 -03001910 return -ENODEV;
Hans Verkuilbd985162005-11-13 16:07:56 -08001911 }
1912
Hans Verkuil21340ae2007-08-26 10:53:16 -03001913 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
Hans Verkuil9357b312008-11-29 12:50:06 -03001914 if (state == NULL)
Hans Verkuil21340ae2007-08-26 10:53:16 -03001915 return -ENOMEM;
Hans Verkuil21340ae2007-08-26 10:53:16 -03001916
Hans Verkuil9357b312008-11-29 12:50:06 -03001917 sd = &state->sd;
1918 v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
Hans Verkuile34e6582010-08-06 10:55:39 -03001919
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001920 switch (id) {
1921 case V4L2_IDENT_CX23885_AV:
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001922 v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n",
1923 client->addr << 1, client->adapter->name);
1924 break;
1925 case V4L2_IDENT_CX23887_AV:
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001926 v4l_info(client, "cx23887 A/V decoder found @ 0x%x (%s)\n",
1927 client->addr << 1, client->adapter->name);
1928 break;
1929 case V4L2_IDENT_CX23888_AV:
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001930 v4l_info(client, "cx23888 A/V decoder found @ 0x%x (%s)\n",
1931 client->addr << 1, client->adapter->name);
1932 break;
1933 case V4L2_IDENT_CX2310X_AV:
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001934 v4l_info(client, "cx%d A/V decoder found @ 0x%x (%s)\n",
1935 device_id, client->addr << 1, client->adapter->name);
1936 break;
1937 case V4L2_IDENT_CX25840:
1938 case V4L2_IDENT_CX25841:
1939 case V4L2_IDENT_CX25842:
1940 case V4L2_IDENT_CX25843:
1941 /* Note: revision '(device_id & 0x0f) == 2' was never built. The
1942 marking skips from 0x1 == 22 to 0x3 == 23. */
1943 v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
1944 (device_id & 0xfff0) >> 4,
1945 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1
1946 : (device_id & 0x0f),
1947 client->addr << 1, client->adapter->name);
1948 break;
1949 case V4L2_IDENT_CX25836:
1950 case V4L2_IDENT_CX25837:
Andy Wallsc7dd1ec2009-09-26 23:32:54 -03001951 default:
1952 v4l_info(client, "cx25%3x-%x found @ 0x%x (%s)\n",
1953 (device_id & 0xfff0) >> 4, device_id & 0x0f,
1954 client->addr << 1, client->adapter->name);
1955 break;
1956 }
Hans Verkuilbd985162005-11-13 16:07:56 -08001957
Hans Verkuil21340ae2007-08-26 10:53:16 -03001958 state->c = client;
Hans Verkuila8bbf122006-01-09 15:25:42 -02001959 state->vid_input = CX25840_COMPOSITE7;
1960 state->aud_input = CX25840_AUDIO8;
Hans Verkuil3578d3d2006-01-09 15:25:41 -02001961 state->audclk_freq = 48000;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001962 state->audmode = V4L2_TUNER_MODE_LANG1;
Christopher Neufeld3e3bf272006-05-24 10:16:45 -03001963 state->vbi_line_offset = 8;
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001964 state->id = id;
Hans Verkuil3434eb72007-04-27 12:31:08 -03001965 state->rev = device_id;
Hans Verkuile34e6582010-08-06 10:55:39 -03001966 v4l2_ctrl_handler_init(&state->hdl, 9);
1967 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1968 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1969 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1970 V4L2_CID_CONTRAST, 0, 127, 1, 64);
1971 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1972 V4L2_CID_SATURATION, 0, 127, 1, 64);
1973 v4l2_ctrl_new_std(&state->hdl, &cx25840_ctrl_ops,
1974 V4L2_CID_HUE, -128, 127, 1, 0);
1975 if (!is_cx2583x(state)) {
Andy Wallsf23b7952010-12-05 19:42:30 -03001976 default_volume = cx25840_read(client, 0x8d4);
1977 /*
1978 * Enforce the legacy PVR-350/MSP3400 to PVR-150/CX25843 volume
1979 * scale mapping limits to avoid -ERANGE errors when
1980 * initializing the volume control
1981 */
1982 if (default_volume > 228) {
1983 /* Bottom out at -96 dB, v4l2 vol range 0x2e00-0x2fff */
1984 default_volume = 228;
1985 cx25840_write(client, 0x8d4, 228);
1986 }
1987 else if (default_volume < 20) {
1988 /* Top out at + 8 dB, v4l2 vol range 0xfe00-0xffff */
1989 default_volume = 20;
1990 cx25840_write(client, 0x8d4, 20);
1991 }
1992 default_volume = (((228 - default_volume) >> 1) + 23) << 9;
Hans Verkuile34e6582010-08-06 10:55:39 -03001993
1994 state->volume = v4l2_ctrl_new_std(&state->hdl,
1995 &cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_VOLUME,
Andy Wallsfc00a1d2010-09-16 20:54:47 -03001996 0, 65535, 65535 / 100, default_volume);
Hans Verkuile34e6582010-08-06 10:55:39 -03001997 state->mute = v4l2_ctrl_new_std(&state->hdl,
1998 &cx25840_audio_ctrl_ops, V4L2_CID_AUDIO_MUTE,
1999 0, 1, 1, 0);
2000 v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
2001 V4L2_CID_AUDIO_BALANCE,
2002 0, 65535, 65535 / 100, 32768);
2003 v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
2004 V4L2_CID_AUDIO_BASS,
2005 0, 65535, 65535 / 100, 32768);
2006 v4l2_ctrl_new_std(&state->hdl, &cx25840_audio_ctrl_ops,
2007 V4L2_CID_AUDIO_TREBLE,
2008 0, 65535, 65535 / 100, 32768);
2009 }
2010 sd->ctrl_handler = &state->hdl;
2011 if (state->hdl.error) {
2012 int err = state->hdl.error;
2013
2014 v4l2_ctrl_handler_free(&state->hdl);
2015 kfree(state);
2016 return err;
2017 }
Sven Barth1e6406b2011-02-13 22:09:43 -03002018 if (!is_cx2583x(state))
2019 v4l2_ctrl_cluster(2, &state->volume);
Hans Verkuile34e6582010-08-06 10:55:39 -03002020 v4l2_ctrl_handler_setup(&state->hdl);
Steven Tothf2340812008-01-10 01:22:39 -03002021
Hans Verkuil3c7c9372011-01-08 07:08:02 -03002022 if (client->dev.platform_data) {
2023 struct cx25840_platform_data *pdata = client->dev.platform_data;
2024
2025 state->pvr150_workaround = pdata->pvr150_workaround;
2026 }
2027
Andy Walls52fd3dd2010-07-18 22:08:03 -03002028 cx25840_ir_probe(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08002029 return 0;
2030}
2031
Hans Verkuil1a392752007-09-13 11:44:47 -03002032static int cx25840_remove(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -08002033{
Hans Verkuil9357b312008-11-29 12:50:06 -03002034 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuile34e6582010-08-06 10:55:39 -03002035 struct cx25840_state *state = to_state(sd);
Hans Verkuil9357b312008-11-29 12:50:06 -03002036
Andy Walls52fd3dd2010-07-18 22:08:03 -03002037 cx25840_ir_remove(sd);
Hans Verkuil9357b312008-11-29 12:50:06 -03002038 v4l2_device_unregister_subdev(sd);
Hans Verkuile34e6582010-08-06 10:55:39 -03002039 v4l2_ctrl_handler_free(&state->hdl);
2040 kfree(state);
Hans Verkuilbd985162005-11-13 16:07:56 -08002041 return 0;
2042}
2043
Jean Delvareaf294862008-05-18 20:49:40 +02002044static const struct i2c_device_id cx25840_id[] = {
2045 { "cx25840", 0 },
2046 { }
2047};
2048MODULE_DEVICE_TABLE(i2c, cx25840_id);
2049
Hans Verkuilad62cdf2010-09-15 15:48:49 -03002050static struct i2c_driver cx25840_driver = {
2051 .driver = {
2052 .owner = THIS_MODULE,
2053 .name = "cx25840",
2054 },
2055 .probe = cx25840_probe,
2056 .remove = cx25840_remove,
2057 .id_table = cx25840_id,
Hans Verkuilbd985162005-11-13 16:07:56 -08002058};
Hans Verkuilad62cdf2010-09-15 15:48:49 -03002059
2060static __init int init_cx25840(void)
2061{
2062 return i2c_add_driver(&cx25840_driver);
2063}
2064
2065static __exit void exit_cx25840(void)
2066{
2067 i2c_del_driver(&cx25840_driver);
2068}
2069
2070module_init(init_cx25840);
2071module_exit(exit_cx25840);