2 * Copyright (C) 2005-2006 Micronas USA Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/i2c.h>
21 #include <linux/videodev2.h>
22 #include <media/tuner.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-ioctl.h>
28 /* #define MPX_DEBUG */
30 /* AS(IF/MPX) pin: LOW HIGH/OPEN
31 * IF/MPX address: 0x42/0x40 0x43/0x44
33 #define IF_I2C_ADDR 0x43
34 #define MPX_I2C_ADDR 0x44
36 static v4l2_std_id force_band;
37 static char force_band_str[] = "-";
38 module_param_string(force_band, force_band_str, sizeof(force_band_str), 0644);
39 static int force_mpx_mode = -1;
40 module_param(force_mpx_mode, int, 0644);
42 /* Store tuner info in the same format as tuner.c, so maybe we can put the
43 * Sony tuner support in there. */
44 struct sony_tunertype {
46 unsigned char Vendor; /* unused here */
47 unsigned char Type; /* unused here */
49 unsigned short thresh1; /* band switch VHF_LO <=> VHF_HI */
50 unsigned short thresh2; /* band switch VHF_HI <=> UHF */
55 unsigned short IFPCoff;
58 /* This array is indexed by (tuner_type - 200) */
59 static struct sony_tunertype sony_tuners[] = {
60 { "Sony PAL+SECAM (BTF-PG472Z)", 0, 0,
61 16*144.25, 16*427.25, 0x01, 0x02, 0x04, 0xc6, 623},
62 { "Sony NTSC_JP (BTF-PK467Z)", 0, 0,
63 16*220.25, 16*467.25, 0x01, 0x02, 0x04, 0xc6, 940},
64 { "Sony NTSC (BTF-PB463Z)", 0, 0,
65 16*130.25, 16*364.25, 0x01, 0x02, 0x04, 0xc6, 732},
68 struct wis_sony_tuner {
76 /* Basically the same as default_set_tv_freq() in tuner.c */
77 static int set_freq(struct i2c_client *client, int freq)
79 struct wis_sony_tuner *t = i2c_get_clientdata(client);
83 struct sony_tunertype *tun;
86 tun = &sony_tuners[t->type - 200];
87 if (freq < tun->thresh1) {
89 band_select = tun->VHF_L;
90 } else if (freq < tun->thresh2) {
92 band_select = tun->VHF_H;
95 band_select = tun->UHF;
97 printk(KERN_DEBUG "wis-sony-tuner: tuning to frequency %d.%04d (%s)\n",
98 freq / 16, (freq % 16) * 625, band_name);
99 n = freq + tun->IFPCoff;
102 buffer[1] = n & 0xff;
103 buffer[2] = tun->config;
104 buffer[3] = band_select;
105 i2c_master_send(client, buffer, 4);
110 static int mpx_write(struct i2c_client *client, int dev, int addr, int val)
116 buffer[1] = addr >> 8;
117 buffer[2] = addr & 0xff;
118 buffer[3] = val >> 8;
119 buffer[4] = val & 0xff;
120 msg.addr = MPX_I2C_ADDR;
124 i2c_transfer(client->adapter, &msg, 1);
129 * MPX register values for the BTF-PG472Z:
132 * MODUS SOURCE ACB PRESCAL PRESCAL PRESCAL SYSTEM VOLUME
133 * 10/0030 12/0008 12/0013 12/000E 12/0010 12/0000 10/0020 12/0000
134 * ---------------------------------------------------------------
135 * Auto 1003 0020 0100 2603 5000 XXXX 0001 7500
138 * Mono 1003 0020 0100 2603 5000 XXXX 0003 7500
139 * A2 1003 0020 0100 2601 5000 XXXX 0003 7500
140 * NICAM 1003 0120 0100 2603 5000 XXXX 0008 7500
143 * Mono 1003 0020 0100 2603 7900 XXXX 000A 7500
144 * NICAM 1003 0120 0100 2603 7900 XXXX 000A 7500
147 * Mono 1003 0020 0100 2603 5000 XXXX 0004 7500
148 * A2-1 1003 0020 0100 2601 5000 XXXX 0004 7500
149 * A2-2 1003 0020 0100 2601 5000 XXXX 0005 7500
150 * A2-3 1003 0020 0100 2601 5000 XXXX 0007 7500
151 * NICAM 1003 0120 0100 2603 5000 XXXX 000B 7500
154 * Mono 0003 0200 0100 7C03 5000 2200 0009 7500
155 * NICAM 0003 0120 0100 7C03 5000 XXXX 0009 7500
158 * Mono 1003 0200 0100 2B03 5000 2B00 0002 7500
160 * For Asia, replace the 0x26XX in FM_PRESCALE with 0x14XX.
162 * Bilingual selection in A2/NICAM:
164 * High byte of SOURCE Left chan Right chan
169 * Force mono in NICAM by setting the high byte of SOURCE to 0x02 (L/L') or
170 * 0x00 (all other bands). Force mono in A2 with FMONO_A2:
175 * Forced mono ON 07F0
176 * Forced mono OFF 0190
180 enum { AUD_MONO, AUD_A2, AUD_NICAM, AUD_NICAM_L } audio_mode;
189 } mpx_audio_modes[] = {
190 /* Auto */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
191 0x5000, 0x0000, 0x0001, 0x7500 },
192 /* B/G Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
193 0x5000, 0x0000, 0x0003, 0x7500 },
194 /* B/G A2 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
195 0x5000, 0x0000, 0x0003, 0x7500 },
196 /* B/G NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
197 0x5000, 0x0000, 0x0008, 0x7500 },
198 /* I Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
199 0x7900, 0x0000, 0x000A, 0x7500 },
200 /* I NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
201 0x7900, 0x0000, 0x000A, 0x7500 },
202 /* D/K Mono */ { AUD_MONO, 0x1003, 0x0020, 0x0100, 0x2603,
203 0x5000, 0x0000, 0x0004, 0x7500 },
204 /* D/K A2-1 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
205 0x5000, 0x0000, 0x0004, 0x7500 },
206 /* D/K A2-2 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
207 0x5000, 0x0000, 0x0005, 0x7500 },
208 /* D/K A2-3 */ { AUD_A2, 0x1003, 0x0020, 0x0100, 0x2601,
209 0x5000, 0x0000, 0x0007, 0x7500 },
210 /* D/K NICAM */ { AUD_NICAM, 0x1003, 0x0120, 0x0100, 0x2603,
211 0x5000, 0x0000, 0x000B, 0x7500 },
212 /* L/L' Mono */ { AUD_MONO, 0x0003, 0x0200, 0x0100, 0x7C03,
213 0x5000, 0x2200, 0x0009, 0x7500 },
214 /* L/L' NICAM */{ AUD_NICAM_L, 0x0003, 0x0120, 0x0100, 0x7C03,
215 0x5000, 0x0000, 0x0009, 0x7500 },
218 #define MPX_NUM_MODES ARRAY_SIZE(mpx_audio_modes)
220 static int mpx_setup(struct i2c_client *client)
222 struct wis_sony_tuner *t = i2c_get_clientdata(client);
231 msg.addr = MPX_I2C_ADDR;
235 i2c_transfer(client->adapter, &msg, 1);
237 i2c_transfer(client->adapter, &msg, 1);
239 if (mpx_audio_modes[t->mpxmode].audio_mode != AUD_MONO) {
240 switch (t->audmode) {
241 case V4L2_TUNER_MODE_MONO:
242 switch (mpx_audio_modes[t->mpxmode].audio_mode) {
244 source = mpx_audio_modes[t->mpxmode].source;
256 case V4L2_TUNER_MODE_STEREO:
257 source = mpx_audio_modes[t->mpxmode].source;
259 case V4L2_TUNER_MODE_LANG1:
262 case V4L2_TUNER_MODE_LANG2:
266 source |= mpx_audio_modes[t->mpxmode].source & 0x00ff;
268 source = mpx_audio_modes[t->mpxmode].source;
270 mpx_write(client, 0x10, 0x0030, mpx_audio_modes[t->mpxmode].modus);
271 mpx_write(client, 0x12, 0x0008, source);
272 mpx_write(client, 0x12, 0x0013, mpx_audio_modes[t->mpxmode].acb);
273 mpx_write(client, 0x12, 0x000e,
274 mpx_audio_modes[t->mpxmode].fm_prescale);
275 mpx_write(client, 0x12, 0x0010,
276 mpx_audio_modes[t->mpxmode].nicam_prescale);
277 mpx_write(client, 0x12, 0x000d,
278 mpx_audio_modes[t->mpxmode].scart_prescale);
279 mpx_write(client, 0x10, 0x0020, mpx_audio_modes[t->mpxmode].system);
280 mpx_write(client, 0x12, 0x0000, mpx_audio_modes[t->mpxmode].volume);
281 if (mpx_audio_modes[t->mpxmode].audio_mode == AUD_A2)
282 mpx_write(client, 0x10, 0x0022,
283 t->audmode == V4L2_TUNER_MODE_MONO ? 0x07f0 : 0x0190);
288 struct i2c_msg msgs[2];
290 printk(KERN_DEBUG "wis-sony-tuner: MPX registers: %04x %04x "
291 "%04x %04x %04x %04x %04x %04x\n",
292 mpx_audio_modes[t->mpxmode].modus,
294 mpx_audio_modes[t->mpxmode].acb,
295 mpx_audio_modes[t->mpxmode].fm_prescale,
296 mpx_audio_modes[t->mpxmode].nicam_prescale,
297 mpx_audio_modes[t->mpxmode].scart_prescale,
298 mpx_audio_modes[t->mpxmode].system,
299 mpx_audio_modes[t->mpxmode].volume);
303 msgs[0].addr = MPX_I2C_ADDR;
307 msgs[1].addr = MPX_I2C_ADDR;
308 msgs[1].flags = I2C_M_RD;
311 i2c_transfer(client->adapter, msgs, 2);
312 printk(KERN_DEBUG "wis-sony-tuner: MPX system: %02x%02x\n",
317 i2c_transfer(client->adapter, msgs, 2);
318 printk(KERN_DEBUG "wis-sony-tuner: MPX status: %02x%02x\n",
326 * IF configuration values for the BTF-PG472Z:
328 * B/G: 0x94 0x70 0x49
330 * D/K: 0x14 0x70 0x4b
336 static int set_if(struct i2c_client *client)
338 struct wis_sony_tuner *t = i2c_get_clientdata(client);
341 int default_mpx_mode = 0;
345 if (t->std & V4L2_STD_PAL_BG) {
349 default_mpx_mode = 1;
350 } else if (t->std & V4L2_STD_PAL_I) {
354 default_mpx_mode = 4;
355 } else if (t->std & V4L2_STD_PAL_DK) {
359 default_mpx_mode = 6;
360 } else if (t->std & V4L2_STD_SECAM_L) {
364 default_mpx_mode = 11;
366 msg.addr = IF_I2C_ADDR;
370 i2c_transfer(client->adapter, &msg, 1);
372 /* Select MPX mode if not forced by the user */
373 if (force_mpx_mode >= 0 || force_mpx_mode < MPX_NUM_MODES)
374 t->mpxmode = force_mpx_mode;
376 t->mpxmode = default_mpx_mode;
377 printk(KERN_DEBUG "wis-sony-tuner: setting MPX to mode %d\n",
384 static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
386 struct wis_sony_tuner *t = i2c_get_clientdata(client);
389 #ifdef TUNER_SET_TYPE_ADDR
390 case TUNER_SET_TYPE_ADDR:
392 struct tuner_setup *tun_setup = arg;
393 int *type = &tun_setup->type;
401 if (t->type != *type)
402 printk(KERN_ERR "wis-sony-tuner: type already "
403 "set to %d, ignoring request for %d\n",
409 case TUNER_SONY_BTF_PG472Z:
410 switch (force_band_str[0]) {
415 printk(KERN_INFO "wis-sony-tuner: forcing "
416 "tuner to PAL-B/G bands\n");
417 force_band = V4L2_STD_PAL_BG;
421 printk(KERN_INFO "wis-sony-tuner: forcing "
422 "tuner to PAL-I band\n");
423 force_band = V4L2_STD_PAL_I;
429 printk(KERN_INFO "wis-sony-tuner: forcing "
430 "tuner to PAL-D/K bands\n");
431 force_band = V4L2_STD_PAL_I;
435 printk(KERN_INFO "wis-sony-tuner: forcing "
436 "tuner to SECAM-L band\n");
437 force_band = V4L2_STD_SECAM_L;
446 t->std = V4L2_STD_PAL_BG;
449 case TUNER_SONY_BTF_PK467Z:
450 t->std = V4L2_STD_NTSC_M_JP;
452 case TUNER_SONY_BTF_PB463Z:
453 t->std = V4L2_STD_NTSC_M;
456 printk(KERN_ERR "wis-sony-tuner: tuner type %d is not "
457 "supported by this module\n", *type);
462 "wis-sony-tuner: type set to %d (%s)\n",
463 t->type, sony_tuners[t->type - 200].name);
466 case VIDIOC_G_FREQUENCY:
468 struct v4l2_frequency *f = arg;
470 f->frequency = t->freq;
473 case VIDIOC_S_FREQUENCY:
475 struct v4l2_frequency *f = arg;
477 t->freq = f->frequency;
478 set_freq(client, t->freq);
483 struct v4l2_standard *std = arg;
486 case TUNER_SONY_BTF_PG472Z:
487 switch (std->index) {
489 v4l2_video_std_construct(std,
490 V4L2_STD_PAL_BG, "PAL-B/G");
493 v4l2_video_std_construct(std,
494 V4L2_STD_PAL_I, "PAL-I");
497 v4l2_video_std_construct(std,
498 V4L2_STD_PAL_DK, "PAL-D/K");
501 v4l2_video_std_construct(std,
502 V4L2_STD_SECAM_L, "SECAM-L");
505 std->id = 0; /* hack to indicate EINVAL */
509 case TUNER_SONY_BTF_PK467Z:
510 if (std->index != 0) {
511 std->id = 0; /* hack to indicate EINVAL */
514 v4l2_video_std_construct(std,
515 V4L2_STD_NTSC_M_JP, "NTSC-J");
517 case TUNER_SONY_BTF_PB463Z:
518 if (std->index != 0) {
519 std->id = 0; /* hack to indicate EINVAL */
522 v4l2_video_std_construct(std, V4L2_STD_NTSC_M, "NTSC");
529 v4l2_std_id *std = arg;
536 v4l2_std_id *std = arg;
537 v4l2_std_id old = t->std;
540 case TUNER_SONY_BTF_PG472Z:
541 if (force_band && (*std & force_band) != *std &&
542 *std != V4L2_STD_PAL &&
543 *std != V4L2_STD_SECAM) {
544 printk(KERN_DEBUG "wis-sony-tuner: ignoring "
545 "requested TV standard in "
546 "favor of force_band value\n");
548 } else if (*std & V4L2_STD_PAL_BG) { /* default */
549 t->std = V4L2_STD_PAL_BG;
550 } else if (*std & V4L2_STD_PAL_I) {
551 t->std = V4L2_STD_PAL_I;
552 } else if (*std & V4L2_STD_PAL_DK) {
553 t->std = V4L2_STD_PAL_DK;
554 } else if (*std & V4L2_STD_SECAM_L) {
555 t->std = V4L2_STD_SECAM_L;
557 printk(KERN_ERR "wis-sony-tuner: TV standard "
559 *std = 0; /* hack to indicate EINVAL */
565 case TUNER_SONY_BTF_PK467Z:
566 if (!(*std & V4L2_STD_NTSC_M_JP)) {
567 printk(KERN_ERR "wis-sony-tuner: TV standard "
569 *std = 0; /* hack to indicate EINVAL */
572 case TUNER_SONY_BTF_PB463Z:
573 if (!(*std & V4L2_STD_NTSC_M)) {
574 printk(KERN_ERR "wis-sony-tuner: TV standard "
576 *std = 0; /* hack to indicate EINVAL */
582 case VIDIOC_QUERYSTD:
584 v4l2_std_id *std = arg;
587 case TUNER_SONY_BTF_PG472Z:
591 *std = V4L2_STD_PAL_BG | V4L2_STD_PAL_I |
592 V4L2_STD_PAL_DK | V4L2_STD_SECAM_L;
594 case TUNER_SONY_BTF_PK467Z:
595 *std = V4L2_STD_NTSC_M_JP;
597 case TUNER_SONY_BTF_PB463Z:
598 *std = V4L2_STD_NTSC_M;
605 struct v4l2_tuner *tun = arg;
607 memset(tun, 0, sizeof(*tun));
608 strcpy(tun->name, "Television");
609 tun->type = V4L2_TUNER_ANALOG_TV;
610 tun->rangelow = 0UL; /* does anything use these? */
611 tun->rangehigh = 0xffffffffUL;
613 case TUNER_SONY_BTF_PG472Z:
614 tun->capability = V4L2_TUNER_CAP_NORM |
615 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
616 V4L2_TUNER_CAP_LANG2;
617 tun->rxsubchans = V4L2_TUNER_SUB_MONO |
618 V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_LANG1 |
619 V4L2_TUNER_SUB_LANG2;
621 case TUNER_SONY_BTF_PK467Z:
622 case TUNER_SONY_BTF_PB463Z:
623 tun->capability = V4L2_TUNER_CAP_STEREO;
624 tun->rxsubchans = V4L2_TUNER_SUB_MONO |
625 V4L2_TUNER_SUB_STEREO;
628 tun->audmode = t->audmode;
633 struct v4l2_tuner *tun = arg;
636 case TUNER_SONY_BTF_PG472Z:
637 if (tun->audmode != t->audmode) {
638 t->audmode = tun->audmode;
642 case TUNER_SONY_BTF_PK467Z:
643 case TUNER_SONY_BTF_PB463Z:
654 static struct i2c_driver wis_sony_tuner_driver;
656 static struct i2c_client wis_sony_tuner_client_templ = {
657 .name = "Sony TV Tuner (WIS)",
658 .driver = &wis_sony_tuner_driver,
661 static int wis_sony_tuner_detect(struct i2c_adapter *adapter,
664 struct i2c_client *client;
665 struct wis_sony_tuner *t;
667 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
670 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
673 memcpy(client, &wis_sony_tuner_client_templ,
674 sizeof(wis_sony_tuner_client_templ));
675 client->adapter = adapter;
678 t = kmalloc(sizeof(struct wis_sony_tuner), GFP_KERNEL);
686 t->audmode = V4L2_TUNER_MODE_STEREO;
687 i2c_set_clientdata(client, t);
690 "wis-sony-tuner: initializing tuner at address %d on %s\n",
691 addr, adapter->name);
693 i2c_attach_client(client);
698 static int wis_sony_tuner_detach(struct i2c_client *client)
700 struct wis_sony_tuner *t = i2c_get_clientdata(client);
703 r = i2c_detach_client(client);
712 static struct i2c_driver wis_sony_tuner_driver = {
714 .name = "WIS Sony TV Tuner I2C driver",
716 .id = I2C_DRIVERID_WIS_SONY_TUNER,
717 .detach_client = wis_sony_tuner_detach,
718 .command = tuner_command,
721 static int __init wis_sony_tuner_init(void)
725 r = i2c_add_driver(&wis_sony_tuner_driver);
728 return wis_i2c_add_driver(wis_sony_tuner_driver.id,
729 wis_sony_tuner_detect);
732 static void __exit wis_sony_tuner_cleanup(void)
734 wis_i2c_del_driver(wis_sony_tuner_detect);
735 i2c_del_driver(&wis_sony_tuner_driver);
738 module_init(wis_sony_tuner_init);
739 module_exit(wis_sony_tuner_cleanup);
741 MODULE_LICENSE("GPL v2");