2 * saa717x - Philips SAA717xHL video decoder driver
4 * Based on the saa7115 driver
6 * Changes by Ohta Kyuma <alpha292@bremen.or.jp>
7 * - Apply to SAA717x,NEC uPD64031,uPD64083. (1/31/2004)
9 * Changes by T.Adachi (tadachi@tadachi-net.com)
10 * - support audio, video scaler etc, and checked the initialize sequence.
12 * Cleaned up by Hans Verkuil <hverkuil@xs4all.nl>
14 * Note: this is a reversed engineered driver based on captures from
15 * the I2C bus under Windows. This chip is very similar to the saa7134,
16 * though. Unfortunately, this driver is currently only working for NTSC.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
34 #include <linux/videodev2.h>
35 #include <linux/i2c.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-ctrls.h>
39 MODULE_DESCRIPTION("Philips SAA717x audio/video decoder driver");
40 MODULE_AUTHOR("K. Ohta, T. Adachi, Hans Verkuil");
41 MODULE_LICENSE("GPL");
44 module_param(debug
, int, 0644);
45 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
49 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
52 struct saa717x_state
{
53 struct v4l2_subdev sd
;
54 struct v4l2_ctrl_handler hdl
;
66 u16 audio_main_treble
;
67 u16 audio_main_volume
;
68 u16 audio_main_balance
;
72 static inline struct saa717x_state
*to_state(struct v4l2_subdev
*sd
)
74 return container_of(sd
, struct saa717x_state
, sd
);
77 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
79 return &container_of(ctrl
->handler
, struct saa717x_state
, hdl
)->sd
;
82 /* ----------------------------------------------------------------------- */
85 #define TUNER_AUDIO_MONO 0 /* LL */
86 #define TUNER_AUDIO_STEREO 1 /* LR */
87 #define TUNER_AUDIO_LANG1 2 /* LL */
88 #define TUNER_AUDIO_LANG2 3 /* RR */
90 #define SAA717X_NTSC_WIDTH (704)
91 #define SAA717X_NTSC_HEIGHT (480)
93 /* ----------------------------------------------------------------------- */
95 static int saa717x_write(struct v4l2_subdev
*sd
, u32 reg
, u32 value
)
97 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
98 struct i2c_adapter
*adap
= client
->adapter
;
99 int fw_addr
= reg
== 0x454 || (reg
>= 0x464 && reg
<= 0x478) || reg
== 0x480 || reg
== 0x488;
100 unsigned char mm1
[6];
104 msg
.addr
= client
->addr
;
105 mm1
[0] = (reg
>> 8) & 0xff;
109 mm1
[4] = (value
>> 16) & 0xff;
110 mm1
[3] = (value
>> 8) & 0xff;
111 mm1
[2] = value
& 0xff;
113 mm1
[2] = value
& 0xff;
115 msg
.len
= fw_addr
? 5 : 3; /* Long Registers have *only* three bytes! */
117 v4l2_dbg(2, debug
, sd
, "wrote: reg 0x%03x=%08x\n", reg
, value
);
118 return i2c_transfer(adap
, &msg
, 1) == 1;
121 static void saa717x_write_regs(struct v4l2_subdev
*sd
, u32
*data
)
123 while (data
[0] || data
[1]) {
124 saa717x_write(sd
, data
[0], data
[1]);
129 static u32
saa717x_read(struct v4l2_subdev
*sd
, u32 reg
)
131 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
132 struct i2c_adapter
*adap
= client
->adapter
;
133 int fw_addr
= (reg
>= 0x404 && reg
<= 0x4b8) || reg
== 0x528;
134 unsigned char mm1
[2];
135 unsigned char mm2
[4] = { 0, 0, 0, 0 };
136 struct i2c_msg msgs
[2];
140 msgs
[1].flags
= I2C_M_RD
;
141 msgs
[0].addr
= msgs
[1].addr
= client
->addr
;
142 mm1
[0] = (reg
>> 8) & 0xff;
146 msgs
[1].len
= fw_addr
? 3 : 1; /* Multibyte Registers contains *only* 3 bytes */
148 i2c_transfer(adap
, msgs
, 2);
151 value
= (mm2
[2] << 16) | (mm2
[1] << 8) | mm2
[0];
155 v4l2_dbg(2, debug
, sd
, "read: reg 0x%03x=0x%08x\n", reg
, value
);
159 /* ----------------------------------------------------------------------- */
161 static u32 reg_init_initialize
[] =
163 /* from linux driver */
164 0x101, 0x008, /* Increment delay */
166 0x103, 0x000, /* Analog input control 2 */
167 0x104, 0x090, /* Analog input control 3 */
168 0x105, 0x090, /* Analog input control 4 */
169 0x106, 0x0eb, /* Horizontal sync start */
170 0x107, 0x0e0, /* Horizontal sync stop */
171 0x109, 0x055, /* Luminance control */
173 0x10f, 0x02a, /* Chroma gain control */
174 0x110, 0x000, /* Chroma control 2 */
176 0x114, 0x045, /* analog/ADC */
178 0x118, 0x040, /* RAW data gain */
179 0x119, 0x080, /* RAW data offset */
181 0x044, 0x000, /* VBI horizontal input window start (L) TASK A */
182 0x045, 0x000, /* VBI horizontal input window start (H) TASK A */
183 0x046, 0x0cf, /* VBI horizontal input window stop (L) TASK A */
184 0x047, 0x002, /* VBI horizontal input window stop (H) TASK A */
186 0x049, 0x000, /* VBI vertical input window start (H) TASK A */
188 0x04c, 0x0d0, /* VBI horizontal output length (L) TASK A */
189 0x04d, 0x002, /* VBI horizontal output length (H) TASK A */
191 0x064, 0x080, /* Lumina brightness TASK A */
192 0x065, 0x040, /* Luminance contrast TASK A */
193 0x066, 0x040, /* Chroma saturation TASK A */
195 0x068, 0x000, /* VBI horizontal scaling increment (L) TASK A */
196 0x069, 0x004, /* VBI horizontal scaling increment (H) TASK A */
197 0x06a, 0x000, /* VBI phase offset TASK A */
199 0x06e, 0x000, /* Horizontal phase offset Luma TASK A */
200 0x06f, 0x000, /* Horizontal phase offset Chroma TASK A */
202 0x072, 0x000, /* Vertical filter mode TASK A */
204 0x084, 0x000, /* VBI horizontal input window start (L) TAKS B */
205 0x085, 0x000, /* VBI horizontal input window start (H) TAKS B */
206 0x086, 0x0cf, /* VBI horizontal input window stop (L) TAKS B */
207 0x087, 0x002, /* VBI horizontal input window stop (H) TAKS B */
209 0x089, 0x000, /* VBI vertical input window start (H) TAKS B */
211 0x08c, 0x0d0, /* VBI horizontal output length (L) TASK B */
212 0x08d, 0x002, /* VBI horizontal output length (H) TASK B */
214 0x0a4, 0x080, /* Lumina brightness TASK B */
215 0x0a5, 0x040, /* Luminance contrast TASK B */
216 0x0a6, 0x040, /* Chroma saturation TASK B */
218 0x0a8, 0x000, /* VBI horizontal scaling increment (L) TASK B */
219 0x0a9, 0x004, /* VBI horizontal scaling increment (H) TASK B */
220 0x0aa, 0x000, /* VBI phase offset TASK B */
222 0x0ae, 0x000, /* Horizontal phase offset Luma TASK B */
223 0x0af, 0x000, /*Horizontal phase offset Chroma TASK B */
225 0x0b2, 0x000, /* Vertical filter mode TASK B */
227 0x00c, 0x000, /* Start point GREEN path */
228 0x00d, 0x000, /* Start point BLUE path */
229 0x00e, 0x000, /* Start point RED path */
231 0x010, 0x010, /* GREEN path gamma curve --- */
246 0x01f, 0x0ff, /* --- GREEN path gamma curve */
248 0x020, 0x010, /* BLUE path gamma curve --- */
263 0x02f, 0x0ff, /* --- BLUE path gamma curve */
265 0x030, 0x010, /* RED path gamma curve --- */
280 0x03f, 0x0ff, /* --- RED path gamma curve */
282 0x109, 0x085, /* Luminance control */
284 /**** from app start ****/
285 0x584, 0x000, /* AGC gain control */
286 0x585, 0x000, /* Program count */
287 0x586, 0x003, /* Status reset */
288 0x588, 0x0ff, /* Number of audio samples (L) */
289 0x589, 0x00f, /* Number of audio samples (M) */
290 0x58a, 0x000, /* Number of audio samples (H) */
291 0x58b, 0x000, /* Audio select */
292 0x58c, 0x010, /* Audio channel assign1 */
293 0x58d, 0x032, /* Audio channel assign2 */
294 0x58e, 0x054, /* Audio channel assign3 */
295 0x58f, 0x023, /* Audio format */
296 0x590, 0x000, /* SIF control */
298 0x595, 0x000, /* ?? */
299 0x596, 0x000, /* ?? */
300 0x597, 0x000, /* ?? */
302 0x464, 0x00, /* Digital input crossbar1 */
304 0x46c, 0xbbbb10, /* Digital output selection1-3 */
305 0x470, 0x101010, /* Digital output selection4-6 */
307 0x478, 0x00, /* Sound feature control */
309 0x474, 0x18, /* Softmute control */
311 0x454, 0x0425b9, /* Sound Easy programming(reset) */
312 0x454, 0x042539, /* Sound Easy programming(reset) */
315 /**** common setting( of DVD play, including scaler commands) ****/
316 0x042, 0x003, /* Data path configuration for VBI (TASK A) */
318 0x082, 0x003, /* Data path configuration for VBI (TASK B) */
320 0x108, 0x0f8, /* Sync control */
321 0x2a9, 0x0fd, /* ??? */
322 0x102, 0x089, /* select video input "mode 9" */
323 0x111, 0x000, /* Mode/delay control */
325 0x10e, 0x00a, /* Chroma control 1 */
327 0x594, 0x002, /* SIF, analog I/O select */
329 0x454, 0x0425b9, /* Sound */
633 static u32 reg_init_tuner_input
[] = {
634 0x108, 0x0f8, /* Sync control */
635 0x111, 0x000, /* Mode/delay control */
636 0x10e, 0x00a, /* Chroma control 1 */
641 static u32 reg_init_composite_input
[] = {
642 0x108, 0x0e8, /* Sync control */
643 0x111, 0x000, /* Mode/delay control */
644 0x10e, 0x04a, /* Chroma control 1 */
649 static u32 reg_init_svideo_input
[] = {
650 0x108, 0x0e8, /* Sync control */
651 0x111, 0x000, /* Mode/delay control */
652 0x10e, 0x04a, /* Chroma control 1 */
656 static u32 reg_set_audio_template
[4][2] =
659 tadachi 6/29 DMA audio output select?
661 7-4: DMA2, 3-0: DMA1 ch. DMA4, DMA3 DMA2, DMA1
662 0: MAIN left, 1: MAIN right
663 2: AUX1 left, 3: AUX1 right
664 4: AUX2 left, 5: AUX2 right
665 6: DPL left, 7: DPL right
666 8: DPL center, 9: DPL surround
667 A: monitor output, B: digital sense */
670 /* tadachi 6/29 DAC and I2S output select?
672 7-4:DAC right ch. 3-0:DAC left ch.
673 I2S1 right,left I2S2 right,left */
682 { /* for LANG2/SAP */
688 /* Get detected audio flags (from saa7134 driver) */
689 static void get_inf_dev_status(struct v4l2_subdev
*sd
,
690 int *dual_flag
, int *stereo_flag
)
694 static char *stdres
[0x20] = {
695 [0x00] = "no standard detected",
696 [0x01] = "B/G (in progress)",
697 [0x02] = "D/K (in progress)",
698 [0x03] = "M (in progress)",
701 [0x05] = "B/G NICAM",
702 [0x06] = "D/K A2 (1)",
703 [0x07] = "D/K A2 (2)",
704 [0x08] = "D/K A2 (3)",
705 [0x09] = "D/K NICAM",
713 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
714 [0x10] = "FM radio / IF 10.7 / 75 deemp",
715 [0x11] = "FM radio / IF sel / 50 deemp",
716 [0x12] = "FM radio / IF sel / 75 deemp",
718 [0x13 ... 0x1e] = "unknown",
719 [0x1f] = "??? [in progress]",
723 *dual_flag
= *stereo_flag
= 0;
725 /* (demdec status: 0x528) */
727 /* read current status */
728 reg_data3
= saa717x_read(sd
, 0x0528);
730 v4l2_dbg(1, debug
, sd
, "tvaudio thread status: 0x%x [%s%s%s]\n",
731 reg_data3
, stdres
[reg_data3
& 0x1f],
732 (reg_data3
& 0x000020) ? ",stereo" : "",
733 (reg_data3
& 0x000040) ? ",dual" : "");
734 v4l2_dbg(1, debug
, sd
, "detailed status: "
735 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
736 (reg_data3
& 0x000080) ? " A2/EIAJ pilot tone " : "",
737 (reg_data3
& 0x000100) ? " A2/EIAJ dual " : "",
738 (reg_data3
& 0x000200) ? " A2/EIAJ stereo " : "",
739 (reg_data3
& 0x000400) ? " A2/EIAJ noise mute " : "",
741 (reg_data3
& 0x000800) ? " BTSC/FM radio pilot " : "",
742 (reg_data3
& 0x001000) ? " SAP carrier " : "",
743 (reg_data3
& 0x002000) ? " BTSC stereo noise mute " : "",
744 (reg_data3
& 0x004000) ? " SAP noise mute " : "",
745 (reg_data3
& 0x008000) ? " VDSP " : "",
747 (reg_data3
& 0x010000) ? " NICST " : "",
748 (reg_data3
& 0x020000) ? " NICDU " : "",
749 (reg_data3
& 0x040000) ? " NICAM muted " : "",
750 (reg_data3
& 0x080000) ? " NICAM reserve sound " : "",
752 (reg_data3
& 0x100000) ? " init done " : "");
754 if (reg_data3
& 0x000220) {
755 v4l2_dbg(1, debug
, sd
, "ST!!!\n");
759 if (reg_data3
& 0x000140) {
760 v4l2_dbg(1, debug
, sd
, "DUAL!!!\n");
765 /* regs write to set audio mode */
766 static void set_audio_mode(struct v4l2_subdev
*sd
, int audio_mode
)
768 v4l2_dbg(1, debug
, sd
, "writing registers to set audio mode by set %d\n",
771 saa717x_write(sd
, 0x46c, reg_set_audio_template
[audio_mode
][0]);
772 saa717x_write(sd
, 0x470, reg_set_audio_template
[audio_mode
][1]);
775 /* write regs to set audio volume, bass and treble */
776 static int set_audio_regs(struct v4l2_subdev
*sd
,
777 struct saa717x_state
*decoder
)
779 u8 mute
= 0xac; /* -84 dB */
781 unsigned int work_l
, work_r
;
783 /* set SIF analog I/O select */
784 saa717x_write(sd
, 0x0594, decoder
->audio_input
);
785 v4l2_dbg(1, debug
, sd
, "set audio input %d\n",
786 decoder
->audio_input
);
788 /* normalize ( 65535 to 0 -> 24 to -40 (not -84)) */
789 work_l
= (min(65536 - decoder
->audio_main_balance
, 32768) * decoder
->audio_main_volume
) / 32768;
790 work_r
= (min(decoder
->audio_main_balance
, (u16
)32768) * decoder
->audio_main_volume
) / 32768;
791 decoder
->audio_main_vol_l
= (long)work_l
* (24 - (-40)) / 65535 - 40;
792 decoder
->audio_main_vol_r
= (long)work_r
* (24 - (-40)) / 65535 - 40;
794 /* set main volume */
795 /* main volume L[7-0],R[7-0],0x00 24=24dB,-83dB, -84(mute) */
796 /* def:0dB->6dB(MPG600GR) */
797 /* if mute is on, set mute */
798 if (decoder
->audio_main_mute
) {
799 val
= mute
| (mute
<< 8);
801 val
= (u8
)decoder
->audio_main_vol_l
|
802 ((u8
)decoder
->audio_main_vol_r
<< 8);
805 saa717x_write(sd
, 0x480, val
);
807 /* set bass and treble */
808 val
= decoder
->audio_main_bass
& 0x1f;
809 val
|= (decoder
->audio_main_treble
& 0x1f) << 5;
810 saa717x_write(sd
, 0x488, val
);
814 /********** scaling staff ***********/
815 static void set_h_prescale(struct v4l2_subdev
*sd
,
816 int task
, int prescale
)
818 static const struct {
825 /* XPSC XACL XC2_1 XDCG VPFY */
837 static const int count
= ARRAY_SIZE(vals
);
840 task_shift
= task
* 0x40;
841 for (i
= 0; i
< count
; i
++)
842 if (vals
[i
].xpsc
== prescale
)
847 /* horizonal prescaling */
848 saa717x_write(sd
, 0x60 + task_shift
, vals
[i
].xpsc
);
849 /* accumulation length */
850 saa717x_write(sd
, 0x61 + task_shift
, vals
[i
].xacl
);
852 saa717x_write(sd
, 0x62 + task_shift
,
853 (vals
[i
].xc2_1
<< 3) | vals
[i
].xdcg
);
854 /*FIR prefilter control */
855 saa717x_write(sd
, 0x63 + task_shift
,
856 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
859 /********** scaling staff ***********/
860 static void set_v_scale(struct v4l2_subdev
*sd
, int task
, int yscale
)
864 task_shift
= task
* 0x40;
865 /* Vertical scaling ratio (LOW) */
866 saa717x_write(sd
, 0x70 + task_shift
, yscale
& 0xff);
867 /* Vertical scaling ratio (HI) */
868 saa717x_write(sd
, 0x71 + task_shift
, yscale
>> 8);
871 static int saa717x_s_ctrl(struct v4l2_ctrl
*ctrl
)
873 struct v4l2_subdev
*sd
= to_sd(ctrl
);
874 struct saa717x_state
*state
= to_state(sd
);
877 case V4L2_CID_BRIGHTNESS
:
878 saa717x_write(sd
, 0x10a, ctrl
->val
);
881 case V4L2_CID_CONTRAST
:
882 saa717x_write(sd
, 0x10b, ctrl
->val
);
885 case V4L2_CID_SATURATION
:
886 saa717x_write(sd
, 0x10c, ctrl
->val
);
890 saa717x_write(sd
, 0x10d, ctrl
->val
);
893 case V4L2_CID_AUDIO_MUTE
:
894 state
->audio_main_mute
= ctrl
->val
;
897 case V4L2_CID_AUDIO_VOLUME
:
898 state
->audio_main_volume
= ctrl
->val
;
901 case V4L2_CID_AUDIO_BALANCE
:
902 state
->audio_main_balance
= ctrl
->val
;
905 case V4L2_CID_AUDIO_TREBLE
:
906 state
->audio_main_treble
= ctrl
->val
;
909 case V4L2_CID_AUDIO_BASS
:
910 state
->audio_main_bass
= ctrl
->val
;
916 set_audio_regs(sd
, state
);
920 static int saa717x_s_video_routing(struct v4l2_subdev
*sd
,
921 u32 input
, u32 output
, u32 config
)
923 struct saa717x_state
*decoder
= to_state(sd
);
924 int is_tuner
= input
& 0x80; /* tuner input flag */
928 v4l2_dbg(1, debug
, sd
, "decoder set input (%d)\n", input
);
929 /* inputs from 0-9 are available*/
930 /* saa717x have mode0-mode9 but mode5 is reserved. */
931 if (input
> 9 || input
== 5)
934 if (decoder
->input
!= input
) {
935 int input_line
= input
;
937 decoder
->input
= input_line
;
938 v4l2_dbg(1, debug
, sd
, "now setting %s input %d\n",
939 input_line
>= 6 ? "S-Video" : "Composite",
943 saa717x_write(sd
, 0x102,
944 (saa717x_read(sd
, 0x102) & 0xf0) |
947 /* bypass chrominance trap for modes 6..9 */
948 saa717x_write(sd
, 0x109,
949 (saa717x_read(sd
, 0x109) & 0x7f) |
950 (input_line
< 6 ? 0x0 : 0x80));
952 /* change audio_mode */
955 set_audio_mode(sd
, decoder
->tuner_audio_mode
);
957 /* Force to STEREO mode if Composite or
958 * S-Video were chosen */
959 set_audio_mode(sd
, TUNER_AUDIO_STEREO
);
961 /* change initialize procedure (Composite/S-Video) */
963 saa717x_write_regs(sd
, reg_init_tuner_input
);
964 else if (input_line
>= 6)
965 saa717x_write_regs(sd
, reg_init_svideo_input
);
967 saa717x_write_regs(sd
, reg_init_composite_input
);
973 #ifdef CONFIG_VIDEO_ADV_DEBUG
974 static int saa717x_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
976 reg
->val
= saa717x_read(sd
, reg
->reg
);
981 static int saa717x_s_register(struct v4l2_subdev
*sd
, const struct v4l2_dbg_register
*reg
)
983 u16 addr
= reg
->reg
& 0xffff;
984 u8 val
= reg
->val
& 0xff;
986 saa717x_write(sd
, addr
, val
);
991 static int saa717x_set_fmt(struct v4l2_subdev
*sd
,
992 struct v4l2_subdev_pad_config
*cfg
,
993 struct v4l2_subdev_format
*format
)
995 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
996 int prescale
, h_scale
, v_scale
;
998 v4l2_dbg(1, debug
, sd
, "decoder set size\n");
1000 if (format
->pad
|| fmt
->code
!= MEDIA_BUS_FMT_FIXED
)
1003 /* FIXME need better bounds checking here */
1004 if (fmt
->width
< 1 || fmt
->width
> 1440)
1006 if (fmt
->height
< 1 || fmt
->height
> 960)
1009 fmt
->field
= V4L2_FIELD_INTERLACED
;
1010 fmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1012 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
1015 /* scaling setting */
1016 /* NTSC and interlace only */
1017 prescale
= SAA717X_NTSC_WIDTH
/ fmt
->width
;
1020 h_scale
= 1024 * SAA717X_NTSC_WIDTH
/ prescale
/ fmt
->width
;
1022 v_scale
= 512 * 2 * SAA717X_NTSC_HEIGHT
/ fmt
->height
;
1024 /* Horizontal prescaling etc */
1025 set_h_prescale(sd
, 0, prescale
);
1026 set_h_prescale(sd
, 1, prescale
);
1028 /* Horizontal scaling increment */
1030 saa717x_write(sd
, 0x6C, (u8
)(h_scale
& 0xFF));
1031 saa717x_write(sd
, 0x6D, (u8
)((h_scale
>> 8) & 0xFF));
1033 saa717x_write(sd
, 0xAC, (u8
)(h_scale
& 0xFF));
1034 saa717x_write(sd
, 0xAD, (u8
)((h_scale
>> 8) & 0xFF));
1036 /* Vertical prescaling etc */
1037 set_v_scale(sd
, 0, v_scale
);
1038 set_v_scale(sd
, 1, v_scale
);
1040 /* set video output size */
1041 /* video number of pixels at output */
1043 saa717x_write(sd
, 0x5C, (u8
)(fmt
->width
& 0xFF));
1044 saa717x_write(sd
, 0x5D, (u8
)((fmt
->width
>> 8) & 0xFF));
1046 saa717x_write(sd
, 0x9C, (u8
)(fmt
->width
& 0xFF));
1047 saa717x_write(sd
, 0x9D, (u8
)((fmt
->width
>> 8) & 0xFF));
1049 /* video number of lines at output */
1051 saa717x_write(sd
, 0x5E, (u8
)(fmt
->height
& 0xFF));
1052 saa717x_write(sd
, 0x5F, (u8
)((fmt
->height
>> 8) & 0xFF));
1054 saa717x_write(sd
, 0x9E, (u8
)(fmt
->height
& 0xFF));
1055 saa717x_write(sd
, 0x9F, (u8
)((fmt
->height
>> 8) & 0xFF));
1059 static int saa717x_s_radio(struct v4l2_subdev
*sd
)
1061 struct saa717x_state
*decoder
= to_state(sd
);
1067 static int saa717x_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
1069 struct saa717x_state
*decoder
= to_state(sd
);
1071 v4l2_dbg(1, debug
, sd
, "decoder set norm ");
1072 v4l2_dbg(1, debug
, sd
, "(not yet implemented)\n");
1079 static int saa717x_s_audio_routing(struct v4l2_subdev
*sd
,
1080 u32 input
, u32 output
, u32 config
)
1082 struct saa717x_state
*decoder
= to_state(sd
);
1084 if (input
< 3) { /* FIXME! --tadachi */
1085 decoder
->audio_input
= input
;
1086 v4l2_dbg(1, debug
, sd
,
1087 "set decoder audio input to %d\n",
1088 decoder
->audio_input
);
1089 set_audio_regs(sd
, decoder
);
1095 static int saa717x_s_stream(struct v4l2_subdev
*sd
, int enable
)
1097 struct saa717x_state
*decoder
= to_state(sd
);
1099 v4l2_dbg(1, debug
, sd
, "decoder %s output\n",
1100 enable
? "enable" : "disable");
1101 decoder
->enable
= enable
;
1102 saa717x_write(sd
, 0x193, enable
? 0xa6 : 0x26);
1106 /* change audio mode */
1107 static int saa717x_s_tuner(struct v4l2_subdev
*sd
, const struct v4l2_tuner
*vt
)
1109 struct saa717x_state
*decoder
= to_state(sd
);
1112 "MONO", "STEREO", "LANG1", "LANG2/SAP"
1115 audio_mode
= TUNER_AUDIO_STEREO
;
1117 switch (vt
->audmode
) {
1118 case V4L2_TUNER_MODE_MONO
:
1119 audio_mode
= TUNER_AUDIO_MONO
;
1121 case V4L2_TUNER_MODE_STEREO
:
1122 audio_mode
= TUNER_AUDIO_STEREO
;
1124 case V4L2_TUNER_MODE_LANG2
:
1125 audio_mode
= TUNER_AUDIO_LANG2
;
1127 case V4L2_TUNER_MODE_LANG1
:
1128 audio_mode
= TUNER_AUDIO_LANG1
;
1132 v4l2_dbg(1, debug
, sd
, "change audio mode to %s\n",
1134 decoder
->tuner_audio_mode
= audio_mode
;
1135 /* The registers are not changed here. */
1136 /* See DECODER_ENABLE_OUTPUT section. */
1137 set_audio_mode(sd
, decoder
->tuner_audio_mode
);
1141 static int saa717x_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1143 struct saa717x_state
*decoder
= to_state(sd
);
1144 int dual_f
, stereo_f
;
1148 get_inf_dev_status(sd
, &dual_f
, &stereo_f
);
1150 v4l2_dbg(1, debug
, sd
, "DETECT==st:%d dual:%d\n",
1154 if ((dual_f
== 0) && (stereo_f
== 0)) {
1155 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1156 v4l2_dbg(1, debug
, sd
, "DETECT==MONO\n");
1160 if (stereo_f
== 1) {
1161 if (vt
->audmode
== V4L2_TUNER_MODE_STEREO
||
1162 vt
->audmode
== V4L2_TUNER_MODE_LANG1
) {
1163 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1164 v4l2_dbg(1, debug
, sd
, "DETECT==ST(ST)\n");
1166 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1167 v4l2_dbg(1, debug
, sd
, "DETECT==ST(MONO)\n");
1173 if (vt
->audmode
== V4L2_TUNER_MODE_LANG2
) {
1174 vt
->rxsubchans
= V4L2_TUNER_SUB_LANG2
| V4L2_TUNER_SUB_MONO
;
1175 v4l2_dbg(1, debug
, sd
, "DETECT==DUAL1\n");
1177 vt
->rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_MONO
;
1178 v4l2_dbg(1, debug
, sd
, "DETECT==DUAL2\n");
1184 static int saa717x_log_status(struct v4l2_subdev
*sd
)
1186 struct saa717x_state
*state
= to_state(sd
);
1188 v4l2_ctrl_handler_log_status(&state
->hdl
, sd
->name
);
1192 /* ----------------------------------------------------------------------- */
1194 static const struct v4l2_ctrl_ops saa717x_ctrl_ops
= {
1195 .s_ctrl
= saa717x_s_ctrl
,
1198 static const struct v4l2_subdev_core_ops saa717x_core_ops
= {
1199 #ifdef CONFIG_VIDEO_ADV_DEBUG
1200 .g_register
= saa717x_g_register
,
1201 .s_register
= saa717x_s_register
,
1203 .log_status
= saa717x_log_status
,
1206 static const struct v4l2_subdev_tuner_ops saa717x_tuner_ops
= {
1207 .g_tuner
= saa717x_g_tuner
,
1208 .s_tuner
= saa717x_s_tuner
,
1209 .s_radio
= saa717x_s_radio
,
1212 static const struct v4l2_subdev_video_ops saa717x_video_ops
= {
1213 .s_std
= saa717x_s_std
,
1214 .s_routing
= saa717x_s_video_routing
,
1215 .s_stream
= saa717x_s_stream
,
1218 static const struct v4l2_subdev_audio_ops saa717x_audio_ops
= {
1219 .s_routing
= saa717x_s_audio_routing
,
1222 static const struct v4l2_subdev_pad_ops saa717x_pad_ops
= {
1223 .set_fmt
= saa717x_set_fmt
,
1226 static const struct v4l2_subdev_ops saa717x_ops
= {
1227 .core
= &saa717x_core_ops
,
1228 .tuner
= &saa717x_tuner_ops
,
1229 .audio
= &saa717x_audio_ops
,
1230 .video
= &saa717x_video_ops
,
1231 .pad
= &saa717x_pad_ops
,
1234 /* ----------------------------------------------------------------------- */
1237 /* i2c implementation */
1239 /* ----------------------------------------------------------------------- */
1240 static int saa717x_probe(struct i2c_client
*client
,
1241 const struct i2c_device_id
*did
)
1243 struct saa717x_state
*decoder
;
1244 struct v4l2_ctrl_handler
*hdl
;
1245 struct v4l2_subdev
*sd
;
1249 /* Check if the adapter supports the needed features */
1250 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1253 decoder
= devm_kzalloc(&client
->dev
, sizeof(*decoder
), GFP_KERNEL
);
1254 if (decoder
== NULL
)
1258 v4l2_i2c_subdev_init(sd
, client
, &saa717x_ops
);
1260 if (saa717x_write(sd
, 0x5a4, 0xfe) &&
1261 saa717x_write(sd
, 0x5a5, 0x0f) &&
1262 saa717x_write(sd
, 0x5a6, 0x00) &&
1263 saa717x_write(sd
, 0x5a7, 0x01))
1264 id
= saa717x_read(sd
, 0x5a0);
1265 if (id
!= 0xc2 && id
!= 0x32 && id
!= 0xf2 && id
!= 0x6c) {
1266 v4l2_dbg(1, debug
, sd
, "saa717x not found (id=%02x)\n", id
);
1271 else if (id
== 0x32)
1273 else if (id
== 0x6c)
1277 v4l2_info(sd
, "%s found @ 0x%x (%s)\n", p
,
1278 client
->addr
<< 1, client
->adapter
->name
);
1280 hdl
= &decoder
->hdl
;
1281 v4l2_ctrl_handler_init(hdl
, 9);
1282 /* add in ascending ID order */
1283 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1284 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
1285 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1286 V4L2_CID_CONTRAST
, 0, 255, 1, 68);
1287 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1288 V4L2_CID_SATURATION
, 0, 255, 1, 64);
1289 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1290 V4L2_CID_HUE
, -128, 127, 1, 0);
1291 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1292 V4L2_CID_AUDIO_VOLUME
, 0, 65535, 65535 / 100, 42000);
1293 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1294 V4L2_CID_AUDIO_BALANCE
, 0, 65535, 65535 / 100, 32768);
1295 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1296 V4L2_CID_AUDIO_BASS
, -16, 15, 1, 0);
1297 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1298 V4L2_CID_AUDIO_TREBLE
, -16, 15, 1, 0);
1299 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1300 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
1301 sd
->ctrl_handler
= hdl
;
1303 int err
= hdl
->error
;
1305 v4l2_ctrl_handler_free(hdl
);
1309 decoder
->std
= V4L2_STD_NTSC
;
1310 decoder
->input
= -1;
1311 decoder
->enable
= 1;
1314 decoder
->playback
= 0; /* initially capture mode used */
1315 decoder
->audio
= 1; /* DECODER_AUDIO_48_KHZ */
1317 decoder
->audio_input
= 2; /* FIXME!! */
1319 decoder
->tuner_audio_mode
= TUNER_AUDIO_STEREO
;
1320 /* set volume, bass and treble */
1321 decoder
->audio_main_vol_l
= 6;
1322 decoder
->audio_main_vol_r
= 6;
1324 v4l2_dbg(1, debug
, sd
, "writing init values\n");
1327 saa717x_write_regs(sd
, reg_init_initialize
);
1329 v4l2_ctrl_handler_setup(hdl
);
1331 set_current_state(TASK_INTERRUPTIBLE
);
1332 schedule_timeout(2*HZ
);
1336 static int saa717x_remove(struct i2c_client
*client
)
1338 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1340 v4l2_device_unregister_subdev(sd
);
1341 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1345 /* ----------------------------------------------------------------------- */
1347 static const struct i2c_device_id saa717x_id
[] = {
1351 MODULE_DEVICE_TABLE(i2c
, saa717x_id
);
1353 static struct i2c_driver saa717x_driver
= {
1357 .probe
= saa717x_probe
,
1358 .remove
= saa717x_remove
,
1359 .id_table
= saa717x_id
,
1362 module_i2c_driver(saa717x_driver
);