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.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>
38 #include <linux/videodev2.h>
39 #include <linux/i2c.h>
40 #include <media/v4l2-device.h>
41 #include <media/v4l2-ctrls.h>
43 MODULE_DESCRIPTION("Philips SAA717x audio/video decoder driver");
44 MODULE_AUTHOR("K. Ohta, T. Adachi, Hans Verkuil");
45 MODULE_LICENSE("GPL");
48 module_param(debug
, int, 0644);
49 MODULE_PARM_DESC(debug
, "Debug level (0-1)");
53 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
56 struct saa717x_state
{
57 struct v4l2_subdev sd
;
58 struct v4l2_ctrl_handler hdl
;
70 u16 audio_main_treble
;
71 u16 audio_main_volume
;
72 u16 audio_main_balance
;
76 static inline struct saa717x_state
*to_state(struct v4l2_subdev
*sd
)
78 return container_of(sd
, struct saa717x_state
, sd
);
81 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
83 return &container_of(ctrl
->handler
, struct saa717x_state
, hdl
)->sd
;
86 /* ----------------------------------------------------------------------- */
89 #define TUNER_AUDIO_MONO 0 /* LL */
90 #define TUNER_AUDIO_STEREO 1 /* LR */
91 #define TUNER_AUDIO_LANG1 2 /* LL */
92 #define TUNER_AUDIO_LANG2 3 /* RR */
94 #define SAA717X_NTSC_WIDTH (704)
95 #define SAA717X_NTSC_HEIGHT (480)
97 /* ----------------------------------------------------------------------- */
99 static int saa717x_write(struct v4l2_subdev
*sd
, u32 reg
, u32 value
)
101 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
102 struct i2c_adapter
*adap
= client
->adapter
;
103 int fw_addr
= reg
== 0x454 || (reg
>= 0x464 && reg
<= 0x478) || reg
== 0x480 || reg
== 0x488;
104 unsigned char mm1
[6];
108 msg
.addr
= client
->addr
;
109 mm1
[0] = (reg
>> 8) & 0xff;
113 mm1
[4] = (value
>> 16) & 0xff;
114 mm1
[3] = (value
>> 8) & 0xff;
115 mm1
[2] = value
& 0xff;
117 mm1
[2] = value
& 0xff;
119 msg
.len
= fw_addr
? 5 : 3; /* Long Registers have *only* three bytes! */
121 v4l2_dbg(2, debug
, sd
, "wrote: reg 0x%03x=%08x\n", reg
, value
);
122 return i2c_transfer(adap
, &msg
, 1) == 1;
125 static void saa717x_write_regs(struct v4l2_subdev
*sd
, u32
*data
)
127 while (data
[0] || data
[1]) {
128 saa717x_write(sd
, data
[0], data
[1]);
133 static u32
saa717x_read(struct v4l2_subdev
*sd
, u32 reg
)
135 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
136 struct i2c_adapter
*adap
= client
->adapter
;
137 int fw_addr
= (reg
>= 0x404 && reg
<= 0x4b8) || reg
== 0x528;
138 unsigned char mm1
[2];
139 unsigned char mm2
[4] = { 0, 0, 0, 0 };
140 struct i2c_msg msgs
[2];
144 msgs
[1].flags
= I2C_M_RD
;
145 msgs
[0].addr
= msgs
[1].addr
= client
->addr
;
146 mm1
[0] = (reg
>> 8) & 0xff;
150 msgs
[1].len
= fw_addr
? 3 : 1; /* Multibyte Registers contains *only* 3 bytes */
152 i2c_transfer(adap
, msgs
, 2);
155 value
= (mm2
[2] & 0xff) | ((mm2
[1] & 0xff) >> 8) | ((mm2
[0] & 0xff) >> 16);
157 value
= mm2
[0] & 0xff;
159 v4l2_dbg(2, debug
, sd
, "read: reg 0x%03x=0x%08x\n", reg
, value
);
163 /* ----------------------------------------------------------------------- */
165 static u32 reg_init_initialize
[] =
167 /* from linux driver */
168 0x101, 0x008, /* Increment delay */
170 0x103, 0x000, /* Analog input control 2 */
171 0x104, 0x090, /* Analog input control 3 */
172 0x105, 0x090, /* Analog input control 4 */
173 0x106, 0x0eb, /* Horizontal sync start */
174 0x107, 0x0e0, /* Horizontal sync stop */
175 0x109, 0x055, /* Luminance control */
177 0x10f, 0x02a, /* Chroma gain control */
178 0x110, 0x000, /* Chroma control 2 */
180 0x114, 0x045, /* analog/ADC */
182 0x118, 0x040, /* RAW data gain */
183 0x119, 0x080, /* RAW data offset */
185 0x044, 0x000, /* VBI horizontal input window start (L) TASK A */
186 0x045, 0x000, /* VBI horizontal input window start (H) TASK A */
187 0x046, 0x0cf, /* VBI horizontal input window stop (L) TASK A */
188 0x047, 0x002, /* VBI horizontal input window stop (H) TASK A */
190 0x049, 0x000, /* VBI vertical input window start (H) TASK A */
192 0x04c, 0x0d0, /* VBI horizontal output length (L) TASK A */
193 0x04d, 0x002, /* VBI horizontal output length (H) TASK A */
195 0x064, 0x080, /* Lumina brightness TASK A */
196 0x065, 0x040, /* Luminance contrast TASK A */
197 0x066, 0x040, /* Chroma saturation TASK A */
199 0x068, 0x000, /* VBI horizontal scaling increment (L) TASK A */
200 0x069, 0x004, /* VBI horizontal scaling increment (H) TASK A */
201 0x06a, 0x000, /* VBI phase offset TASK A */
203 0x06e, 0x000, /* Horizontal phase offset Luma TASK A */
204 0x06f, 0x000, /* Horizontal phase offset Chroma TASK A */
206 0x072, 0x000, /* Vertical filter mode TASK A */
208 0x084, 0x000, /* VBI horizontal input window start (L) TAKS B */
209 0x085, 0x000, /* VBI horizontal input window start (H) TAKS B */
210 0x086, 0x0cf, /* VBI horizontal input window stop (L) TAKS B */
211 0x087, 0x002, /* VBI horizontal input window stop (H) TAKS B */
213 0x089, 0x000, /* VBI vertical input window start (H) TAKS B */
215 0x08c, 0x0d0, /* VBI horizontal output length (L) TASK B */
216 0x08d, 0x002, /* VBI horizontal output length (H) TASK B */
218 0x0a4, 0x080, /* Lumina brightness TASK B */
219 0x0a5, 0x040, /* Luminance contrast TASK B */
220 0x0a6, 0x040, /* Chroma saturation TASK B */
222 0x0a8, 0x000, /* VBI horizontal scaling increment (L) TASK B */
223 0x0a9, 0x004, /* VBI horizontal scaling increment (H) TASK B */
224 0x0aa, 0x000, /* VBI phase offset TASK B */
226 0x0ae, 0x000, /* Horizontal phase offset Luma TASK B */
227 0x0af, 0x000, /*Horizontal phase offset Chroma TASK B */
229 0x0b2, 0x000, /* Vertical filter mode TASK B */
231 0x00c, 0x000, /* Start point GREEN path */
232 0x00d, 0x000, /* Start point BLUE path */
233 0x00e, 0x000, /* Start point RED path */
235 0x010, 0x010, /* GREEN path gamma curve --- */
250 0x01f, 0x0ff, /* --- GREEN path gamma curve */
252 0x020, 0x010, /* BLUE path gamma curve --- */
267 0x02f, 0x0ff, /* --- BLUE path gamma curve */
269 0x030, 0x010, /* RED path gamma curve --- */
284 0x03f, 0x0ff, /* --- RED path gamma curve */
286 0x109, 0x085, /* Luminance control */
288 /**** from app start ****/
289 0x584, 0x000, /* AGC gain control */
290 0x585, 0x000, /* Program count */
291 0x586, 0x003, /* Status reset */
292 0x588, 0x0ff, /* Number of audio samples (L) */
293 0x589, 0x00f, /* Number of audio samples (M) */
294 0x58a, 0x000, /* Number of audio samples (H) */
295 0x58b, 0x000, /* Audio select */
296 0x58c, 0x010, /* Audio channel assign1 */
297 0x58d, 0x032, /* Audio channel assign2 */
298 0x58e, 0x054, /* Audio channel assign3 */
299 0x58f, 0x023, /* Audio format */
300 0x590, 0x000, /* SIF control */
302 0x595, 0x000, /* ?? */
303 0x596, 0x000, /* ?? */
304 0x597, 0x000, /* ?? */
306 0x464, 0x00, /* Digital input crossbar1 */
308 0x46c, 0xbbbb10, /* Digital output selection1-3 */
309 0x470, 0x101010, /* Digital output selection4-6 */
311 0x478, 0x00, /* Sound feature control */
313 0x474, 0x18, /* Softmute control */
315 0x454, 0x0425b9, /* Sound Easy programming(reset) */
316 0x454, 0x042539, /* Sound Easy programming(reset) */
319 /**** common setting( of DVD play, including scaler commands) ****/
320 0x042, 0x003, /* Data path configuration for VBI (TASK A) */
322 0x082, 0x003, /* Data path configuration for VBI (TASK B) */
324 0x108, 0x0f8, /* Sync control */
325 0x2a9, 0x0fd, /* ??? */
326 0x102, 0x089, /* select video input "mode 9" */
327 0x111, 0x000, /* Mode/delay control */
329 0x10e, 0x00a, /* Chroma control 1 */
331 0x594, 0x002, /* SIF, analog I/O select */
333 0x454, 0x0425b9, /* Sound */
637 static u32 reg_init_tuner_input
[] = {
638 0x108, 0x0f8, /* Sync control */
639 0x111, 0x000, /* Mode/delay control */
640 0x10e, 0x00a, /* Chroma control 1 */
645 static u32 reg_init_composite_input
[] = {
646 0x108, 0x0e8, /* Sync control */
647 0x111, 0x000, /* Mode/delay control */
648 0x10e, 0x04a, /* Chroma control 1 */
653 static u32 reg_init_svideo_input
[] = {
654 0x108, 0x0e8, /* Sync control */
655 0x111, 0x000, /* Mode/delay control */
656 0x10e, 0x04a, /* Chroma control 1 */
660 static u32 reg_set_audio_template
[4][2] =
663 tadachi 6/29 DMA audio output select?
665 7-4: DMA2, 3-0: DMA1 ch. DMA4, DMA3 DMA2, DMA1
666 0: MAIN left, 1: MAIN right
667 2: AUX1 left, 3: AUX1 right
668 4: AUX2 left, 5: AUX2 right
669 6: DPL left, 7: DPL right
670 8: DPL center, 9: DPL surround
671 A: monitor output, B: digital sense */
674 /* tadachi 6/29 DAC and I2S output select?
676 7-4:DAC right ch. 3-0:DAC left ch.
677 I2S1 right,left I2S2 right,left */
686 { /* for LANG2/SAP */
692 /* Get detected audio flags (from saa7134 driver) */
693 static void get_inf_dev_status(struct v4l2_subdev
*sd
,
694 int *dual_flag
, int *stereo_flag
)
698 static char *stdres
[0x20] = {
699 [0x00] = "no standard detected",
700 [0x01] = "B/G (in progress)",
701 [0x02] = "D/K (in progress)",
702 [0x03] = "M (in progress)",
705 [0x05] = "B/G NICAM",
706 [0x06] = "D/K A2 (1)",
707 [0x07] = "D/K A2 (2)",
708 [0x08] = "D/K A2 (3)",
709 [0x09] = "D/K NICAM",
717 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
718 [0x10] = "FM radio / IF 10.7 / 75 deemp",
719 [0x11] = "FM radio / IF sel / 50 deemp",
720 [0x12] = "FM radio / IF sel / 75 deemp",
722 [0x13 ... 0x1e] = "unknown",
723 [0x1f] = "??? [in progress]",
727 *dual_flag
= *stereo_flag
= 0;
729 /* (demdec status: 0x528) */
731 /* read current status */
732 reg_data3
= saa717x_read(sd
, 0x0528);
734 v4l2_dbg(1, debug
, sd
, "tvaudio thread status: 0x%x [%s%s%s]\n",
735 reg_data3
, stdres
[reg_data3
& 0x1f],
736 (reg_data3
& 0x000020) ? ",stereo" : "",
737 (reg_data3
& 0x000040) ? ",dual" : "");
738 v4l2_dbg(1, debug
, sd
, "detailed status: "
739 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
740 (reg_data3
& 0x000080) ? " A2/EIAJ pilot tone " : "",
741 (reg_data3
& 0x000100) ? " A2/EIAJ dual " : "",
742 (reg_data3
& 0x000200) ? " A2/EIAJ stereo " : "",
743 (reg_data3
& 0x000400) ? " A2/EIAJ noise mute " : "",
745 (reg_data3
& 0x000800) ? " BTSC/FM radio pilot " : "",
746 (reg_data3
& 0x001000) ? " SAP carrier " : "",
747 (reg_data3
& 0x002000) ? " BTSC stereo noise mute " : "",
748 (reg_data3
& 0x004000) ? " SAP noise mute " : "",
749 (reg_data3
& 0x008000) ? " VDSP " : "",
751 (reg_data3
& 0x010000) ? " NICST " : "",
752 (reg_data3
& 0x020000) ? " NICDU " : "",
753 (reg_data3
& 0x040000) ? " NICAM muted " : "",
754 (reg_data3
& 0x080000) ? " NICAM reserve sound " : "",
756 (reg_data3
& 0x100000) ? " init done " : "");
758 if (reg_data3
& 0x000220) {
759 v4l2_dbg(1, debug
, sd
, "ST!!!\n");
763 if (reg_data3
& 0x000140) {
764 v4l2_dbg(1, debug
, sd
, "DUAL!!!\n");
769 /* regs write to set audio mode */
770 static void set_audio_mode(struct v4l2_subdev
*sd
, int audio_mode
)
772 v4l2_dbg(1, debug
, sd
, "writing registers to set audio mode by set %d\n",
775 saa717x_write(sd
, 0x46c, reg_set_audio_template
[audio_mode
][0]);
776 saa717x_write(sd
, 0x470, reg_set_audio_template
[audio_mode
][1]);
779 /* write regs to set audio volume, bass and treble */
780 static int set_audio_regs(struct v4l2_subdev
*sd
,
781 struct saa717x_state
*decoder
)
783 u8 mute
= 0xac; /* -84 dB */
785 unsigned int work_l
, work_r
;
787 /* set SIF analog I/O select */
788 saa717x_write(sd
, 0x0594, decoder
->audio_input
);
789 v4l2_dbg(1, debug
, sd
, "set audio input %d\n",
790 decoder
->audio_input
);
792 /* normalize ( 65535 to 0 -> 24 to -40 (not -84)) */
793 work_l
= (min(65536 - decoder
->audio_main_balance
, 32768) * decoder
->audio_main_volume
) / 32768;
794 work_r
= (min(decoder
->audio_main_balance
, (u16
)32768) * decoder
->audio_main_volume
) / 32768;
795 decoder
->audio_main_vol_l
= (long)work_l
* (24 - (-40)) / 65535 - 40;
796 decoder
->audio_main_vol_r
= (long)work_r
* (24 - (-40)) / 65535 - 40;
798 /* set main volume */
799 /* main volume L[7-0],R[7-0],0x00 24=24dB,-83dB, -84(mute) */
800 /* def:0dB->6dB(MPG600GR) */
801 /* if mute is on, set mute */
802 if (decoder
->audio_main_mute
) {
803 val
= mute
| (mute
<< 8);
805 val
= (u8
)decoder
->audio_main_vol_l
|
806 ((u8
)decoder
->audio_main_vol_r
<< 8);
809 saa717x_write(sd
, 0x480, val
);
811 /* set bass and treble */
812 val
= decoder
->audio_main_bass
& 0x1f;
813 val
|= (decoder
->audio_main_treble
& 0x1f) << 5;
814 saa717x_write(sd
, 0x488, val
);
818 /********** scaling staff ***********/
819 static void set_h_prescale(struct v4l2_subdev
*sd
,
820 int task
, int prescale
)
822 static const struct {
829 /* XPSC XACL XC2_1 XDCG VPFY */
841 static const int count
= ARRAY_SIZE(vals
);
844 task_shift
= task
* 0x40;
845 for (i
= 0; i
< count
; i
++)
846 if (vals
[i
].xpsc
== prescale
)
851 /* horizonal prescaling */
852 saa717x_write(sd
, 0x60 + task_shift
, vals
[i
].xpsc
);
853 /* accumulation length */
854 saa717x_write(sd
, 0x61 + task_shift
, vals
[i
].xacl
);
856 saa717x_write(sd
, 0x62 + task_shift
,
857 (vals
[i
].xc2_1
<< 3) | vals
[i
].xdcg
);
858 /*FIR prefilter control */
859 saa717x_write(sd
, 0x63 + task_shift
,
860 (vals
[i
].vpfy
<< 2) | vals
[i
].vpfy
);
863 /********** scaling staff ***********/
864 static void set_v_scale(struct v4l2_subdev
*sd
, int task
, int yscale
)
868 task_shift
= task
* 0x40;
869 /* Vertical scaling ratio (LOW) */
870 saa717x_write(sd
, 0x70 + task_shift
, yscale
& 0xff);
871 /* Vertical scaling ratio (HI) */
872 saa717x_write(sd
, 0x71 + task_shift
, yscale
>> 8);
875 static int saa717x_s_ctrl(struct v4l2_ctrl
*ctrl
)
877 struct v4l2_subdev
*sd
= to_sd(ctrl
);
878 struct saa717x_state
*state
= to_state(sd
);
881 case V4L2_CID_BRIGHTNESS
:
882 saa717x_write(sd
, 0x10a, ctrl
->val
);
885 case V4L2_CID_CONTRAST
:
886 saa717x_write(sd
, 0x10b, ctrl
->val
);
889 case V4L2_CID_SATURATION
:
890 saa717x_write(sd
, 0x10c, ctrl
->val
);
894 saa717x_write(sd
, 0x10d, ctrl
->val
);
897 case V4L2_CID_AUDIO_MUTE
:
898 state
->audio_main_mute
= ctrl
->val
;
901 case V4L2_CID_AUDIO_VOLUME
:
902 state
->audio_main_volume
= ctrl
->val
;
905 case V4L2_CID_AUDIO_BALANCE
:
906 state
->audio_main_balance
= ctrl
->val
;
909 case V4L2_CID_AUDIO_TREBLE
:
910 state
->audio_main_treble
= ctrl
->val
;
913 case V4L2_CID_AUDIO_BASS
:
914 state
->audio_main_bass
= ctrl
->val
;
920 set_audio_regs(sd
, state
);
924 static int saa717x_s_video_routing(struct v4l2_subdev
*sd
,
925 u32 input
, u32 output
, u32 config
)
927 struct saa717x_state
*decoder
= to_state(sd
);
928 int is_tuner
= input
& 0x80; /* tuner input flag */
932 v4l2_dbg(1, debug
, sd
, "decoder set input (%d)\n", input
);
933 /* inputs from 0-9 are available*/
934 /* saa717x have mode0-mode9 but mode5 is reserved. */
935 if (input
> 9 || input
== 5)
938 if (decoder
->input
!= input
) {
939 int input_line
= input
;
941 decoder
->input
= input_line
;
942 v4l2_dbg(1, debug
, sd
, "now setting %s input %d\n",
943 input_line
>= 6 ? "S-Video" : "Composite",
947 saa717x_write(sd
, 0x102,
948 (saa717x_read(sd
, 0x102) & 0xf0) |
951 /* bypass chrominance trap for modes 6..9 */
952 saa717x_write(sd
, 0x109,
953 (saa717x_read(sd
, 0x109) & 0x7f) |
954 (input_line
< 6 ? 0x0 : 0x80));
956 /* change audio_mode */
959 set_audio_mode(sd
, decoder
->tuner_audio_mode
);
961 /* Force to STEREO mode if Composite or
962 * S-Video were chosen */
963 set_audio_mode(sd
, TUNER_AUDIO_STEREO
);
965 /* change initialize procedure (Composite/S-Video) */
967 saa717x_write_regs(sd
, reg_init_tuner_input
);
968 else if (input_line
>= 6)
969 saa717x_write_regs(sd
, reg_init_svideo_input
);
971 saa717x_write_regs(sd
, reg_init_composite_input
);
977 #ifdef CONFIG_VIDEO_ADV_DEBUG
978 static int saa717x_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
980 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
982 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
984 if (!capable(CAP_SYS_ADMIN
))
986 reg
->val
= saa717x_read(sd
, reg
->reg
);
991 static int saa717x_s_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
993 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
994 u16 addr
= reg
->reg
& 0xffff;
995 u8 val
= reg
->val
& 0xff;
997 if (!v4l2_chip_match_i2c_client(client
, ®
->match
))
999 if (!capable(CAP_SYS_ADMIN
))
1001 saa717x_write(sd
, addr
, val
);
1006 static int saa717x_s_mbus_fmt(struct v4l2_subdev
*sd
, struct v4l2_mbus_framefmt
*fmt
)
1008 int prescale
, h_scale
, v_scale
;
1010 v4l2_dbg(1, debug
, sd
, "decoder set size\n");
1012 if (fmt
->code
!= V4L2_MBUS_FMT_FIXED
)
1015 /* FIXME need better bounds checking here */
1016 if (fmt
->width
< 1 || fmt
->width
> 1440)
1018 if (fmt
->height
< 1 || fmt
->height
> 960)
1021 fmt
->field
= V4L2_FIELD_INTERLACED
;
1022 fmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1024 /* scaling setting */
1025 /* NTSC and interlace only */
1026 prescale
= SAA717X_NTSC_WIDTH
/ fmt
->width
;
1029 h_scale
= 1024 * SAA717X_NTSC_WIDTH
/ prescale
/ fmt
->width
;
1031 v_scale
= 512 * 2 * SAA717X_NTSC_HEIGHT
/ fmt
->height
;
1033 /* Horizontal prescaling etc */
1034 set_h_prescale(sd
, 0, prescale
);
1035 set_h_prescale(sd
, 1, prescale
);
1037 /* Horizontal scaling increment */
1039 saa717x_write(sd
, 0x6C, (u8
)(h_scale
& 0xFF));
1040 saa717x_write(sd
, 0x6D, (u8
)((h_scale
>> 8) & 0xFF));
1042 saa717x_write(sd
, 0xAC, (u8
)(h_scale
& 0xFF));
1043 saa717x_write(sd
, 0xAD, (u8
)((h_scale
>> 8) & 0xFF));
1045 /* Vertical prescaling etc */
1046 set_v_scale(sd
, 0, v_scale
);
1047 set_v_scale(sd
, 1, v_scale
);
1049 /* set video output size */
1050 /* video number of pixels at output */
1052 saa717x_write(sd
, 0x5C, (u8
)(fmt
->width
& 0xFF));
1053 saa717x_write(sd
, 0x5D, (u8
)((fmt
->width
>> 8) & 0xFF));
1055 saa717x_write(sd
, 0x9C, (u8
)(fmt
->width
& 0xFF));
1056 saa717x_write(sd
, 0x9D, (u8
)((fmt
->width
>> 8) & 0xFF));
1058 /* video number of lines at output */
1060 saa717x_write(sd
, 0x5E, (u8
)(fmt
->height
& 0xFF));
1061 saa717x_write(sd
, 0x5F, (u8
)((fmt
->height
>> 8) & 0xFF));
1063 saa717x_write(sd
, 0x9E, (u8
)(fmt
->height
& 0xFF));
1064 saa717x_write(sd
, 0x9F, (u8
)((fmt
->height
>> 8) & 0xFF));
1068 static int saa717x_s_radio(struct v4l2_subdev
*sd
)
1070 struct saa717x_state
*decoder
= to_state(sd
);
1076 static int saa717x_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
1078 struct saa717x_state
*decoder
= to_state(sd
);
1080 v4l2_dbg(1, debug
, sd
, "decoder set norm ");
1081 v4l2_dbg(1, debug
, sd
, "(not yet implementd)\n");
1088 static int saa717x_s_audio_routing(struct v4l2_subdev
*sd
,
1089 u32 input
, u32 output
, u32 config
)
1091 struct saa717x_state
*decoder
= to_state(sd
);
1093 if (input
< 3) { /* FIXME! --tadachi */
1094 decoder
->audio_input
= input
;
1095 v4l2_dbg(1, debug
, sd
,
1096 "set decoder audio input to %d\n",
1097 decoder
->audio_input
);
1098 set_audio_regs(sd
, decoder
);
1104 static int saa717x_s_stream(struct v4l2_subdev
*sd
, int enable
)
1106 struct saa717x_state
*decoder
= to_state(sd
);
1108 v4l2_dbg(1, debug
, sd
, "decoder %s output\n",
1109 enable
? "enable" : "disable");
1110 decoder
->enable
= enable
;
1111 saa717x_write(sd
, 0x193, enable
? 0xa6 : 0x26);
1115 /* change audio mode */
1116 static int saa717x_s_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1118 struct saa717x_state
*decoder
= to_state(sd
);
1121 "MONO", "STEREO", "LANG1", "LANG2/SAP"
1124 audio_mode
= TUNER_AUDIO_STEREO
;
1126 switch (vt
->audmode
) {
1127 case V4L2_TUNER_MODE_MONO
:
1128 audio_mode
= TUNER_AUDIO_MONO
;
1130 case V4L2_TUNER_MODE_STEREO
:
1131 audio_mode
= TUNER_AUDIO_STEREO
;
1133 case V4L2_TUNER_MODE_LANG2
:
1134 audio_mode
= TUNER_AUDIO_LANG2
;
1136 case V4L2_TUNER_MODE_LANG1
:
1137 audio_mode
= TUNER_AUDIO_LANG1
;
1141 v4l2_dbg(1, debug
, sd
, "change audio mode to %s\n",
1143 decoder
->tuner_audio_mode
= audio_mode
;
1144 /* The registers are not changed here. */
1145 /* See DECODER_ENABLE_OUTPUT section. */
1146 set_audio_mode(sd
, decoder
->tuner_audio_mode
);
1150 static int saa717x_g_tuner(struct v4l2_subdev
*sd
, struct v4l2_tuner
*vt
)
1152 struct saa717x_state
*decoder
= to_state(sd
);
1153 int dual_f
, stereo_f
;
1157 get_inf_dev_status(sd
, &dual_f
, &stereo_f
);
1159 v4l2_dbg(1, debug
, sd
, "DETECT==st:%d dual:%d\n",
1163 if ((dual_f
== 0) && (stereo_f
== 0)) {
1164 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1165 v4l2_dbg(1, debug
, sd
, "DETECT==MONO\n");
1169 if (stereo_f
== 1) {
1170 if (vt
->audmode
== V4L2_TUNER_MODE_STEREO
||
1171 vt
->audmode
== V4L2_TUNER_MODE_LANG1
) {
1172 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1173 v4l2_dbg(1, debug
, sd
, "DETECT==ST(ST)\n");
1175 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1176 v4l2_dbg(1, debug
, sd
, "DETECT==ST(MONO)\n");
1182 if (vt
->audmode
== V4L2_TUNER_MODE_LANG2
) {
1183 vt
->rxsubchans
= V4L2_TUNER_SUB_LANG2
| V4L2_TUNER_SUB_MONO
;
1184 v4l2_dbg(1, debug
, sd
, "DETECT==DUAL1\n");
1186 vt
->rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_MONO
;
1187 v4l2_dbg(1, debug
, sd
, "DETECT==DUAL2\n");
1193 static int saa717x_log_status(struct v4l2_subdev
*sd
)
1195 struct saa717x_state
*state
= to_state(sd
);
1197 v4l2_ctrl_handler_log_status(&state
->hdl
, sd
->name
);
1201 /* ----------------------------------------------------------------------- */
1203 static const struct v4l2_ctrl_ops saa717x_ctrl_ops
= {
1204 .s_ctrl
= saa717x_s_ctrl
,
1207 static const struct v4l2_subdev_core_ops saa717x_core_ops
= {
1208 #ifdef CONFIG_VIDEO_ADV_DEBUG
1209 .g_register
= saa717x_g_register
,
1210 .s_register
= saa717x_s_register
,
1212 .s_std
= saa717x_s_std
,
1213 .g_ext_ctrls
= v4l2_subdev_g_ext_ctrls
,
1214 .try_ext_ctrls
= v4l2_subdev_try_ext_ctrls
,
1215 .s_ext_ctrls
= v4l2_subdev_s_ext_ctrls
,
1216 .g_ctrl
= v4l2_subdev_g_ctrl
,
1217 .s_ctrl
= v4l2_subdev_s_ctrl
,
1218 .queryctrl
= v4l2_subdev_queryctrl
,
1219 .querymenu
= v4l2_subdev_querymenu
,
1220 .log_status
= saa717x_log_status
,
1223 static const struct v4l2_subdev_tuner_ops saa717x_tuner_ops
= {
1224 .g_tuner
= saa717x_g_tuner
,
1225 .s_tuner
= saa717x_s_tuner
,
1226 .s_radio
= saa717x_s_radio
,
1229 static const struct v4l2_subdev_video_ops saa717x_video_ops
= {
1230 .s_routing
= saa717x_s_video_routing
,
1231 .s_mbus_fmt
= saa717x_s_mbus_fmt
,
1232 .s_stream
= saa717x_s_stream
,
1235 static const struct v4l2_subdev_audio_ops saa717x_audio_ops
= {
1236 .s_routing
= saa717x_s_audio_routing
,
1239 static const struct v4l2_subdev_ops saa717x_ops
= {
1240 .core
= &saa717x_core_ops
,
1241 .tuner
= &saa717x_tuner_ops
,
1242 .audio
= &saa717x_audio_ops
,
1243 .video
= &saa717x_video_ops
,
1246 /* ----------------------------------------------------------------------- */
1249 /* i2c implementation */
1251 /* ----------------------------------------------------------------------- */
1252 static int saa717x_probe(struct i2c_client
*client
,
1253 const struct i2c_device_id
*did
)
1255 struct saa717x_state
*decoder
;
1256 struct v4l2_ctrl_handler
*hdl
;
1257 struct v4l2_subdev
*sd
;
1261 /* Check if the adapter supports the needed features */
1262 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
1265 decoder
= kzalloc(sizeof(struct saa717x_state
), GFP_KERNEL
);
1266 if (decoder
== NULL
)
1270 v4l2_i2c_subdev_init(sd
, client
, &saa717x_ops
);
1272 if (saa717x_write(sd
, 0x5a4, 0xfe) &&
1273 saa717x_write(sd
, 0x5a5, 0x0f) &&
1274 saa717x_write(sd
, 0x5a6, 0x00) &&
1275 saa717x_write(sd
, 0x5a7, 0x01))
1276 id
= saa717x_read(sd
, 0x5a0);
1277 if (id
!= 0xc2 && id
!= 0x32 && id
!= 0xf2 && id
!= 0x6c) {
1278 v4l2_dbg(1, debug
, sd
, "saa717x not found (id=%02x)\n", id
);
1284 else if (id
== 0x32)
1286 else if (id
== 0x6c)
1290 v4l2_info(sd
, "%s found @ 0x%x (%s)\n", p
,
1291 client
->addr
<< 1, client
->adapter
->name
);
1293 hdl
= &decoder
->hdl
;
1294 v4l2_ctrl_handler_init(hdl
, 9);
1295 /* add in ascending ID order */
1296 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1297 V4L2_CID_BRIGHTNESS
, 0, 255, 1, 128);
1298 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1299 V4L2_CID_CONTRAST
, 0, 255, 1, 68);
1300 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1301 V4L2_CID_SATURATION
, 0, 255, 1, 64);
1302 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1303 V4L2_CID_HUE
, -128, 127, 1, 0);
1304 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1305 V4L2_CID_AUDIO_VOLUME
, 0, 65535, 65535 / 100, 42000);
1306 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1307 V4L2_CID_AUDIO_BALANCE
, 0, 65535, 65535 / 100, 32768);
1308 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1309 V4L2_CID_AUDIO_BASS
, -16, 15, 1, 0);
1310 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1311 V4L2_CID_AUDIO_TREBLE
, -16, 15, 1, 0);
1312 v4l2_ctrl_new_std(hdl
, &saa717x_ctrl_ops
,
1313 V4L2_CID_AUDIO_MUTE
, 0, 1, 1, 0);
1314 sd
->ctrl_handler
= hdl
;
1316 int err
= hdl
->error
;
1318 v4l2_ctrl_handler_free(hdl
);
1323 decoder
->std
= V4L2_STD_NTSC
;
1324 decoder
->input
= -1;
1325 decoder
->enable
= 1;
1328 decoder
->playback
= 0; /* initially capture mode used */
1329 decoder
->audio
= 1; /* DECODER_AUDIO_48_KHZ */
1331 decoder
->audio_input
= 2; /* FIXME!! */
1333 decoder
->tuner_audio_mode
= TUNER_AUDIO_STEREO
;
1334 /* set volume, bass and treble */
1335 decoder
->audio_main_vol_l
= 6;
1336 decoder
->audio_main_vol_r
= 6;
1338 v4l2_dbg(1, debug
, sd
, "writing init values\n");
1341 saa717x_write_regs(sd
, reg_init_initialize
);
1343 v4l2_ctrl_handler_setup(hdl
);
1345 set_current_state(TASK_INTERRUPTIBLE
);
1346 schedule_timeout(2*HZ
);
1350 static int saa717x_remove(struct i2c_client
*client
)
1352 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1354 v4l2_device_unregister_subdev(sd
);
1355 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1356 kfree(to_state(sd
));
1360 /* ----------------------------------------------------------------------- */
1362 static const struct i2c_device_id saa717x_id
[] = {
1366 MODULE_DEVICE_TABLE(i2c
, saa717x_id
);
1368 static struct i2c_driver saa717x_driver
= {
1370 .owner
= THIS_MODULE
,
1373 .probe
= saa717x_probe
,
1374 .remove
= saa717x_remove
,
1375 .id_table
= saa717x_id
,
1378 static __init
int init_saa717x(void)
1380 return i2c_add_driver(&saa717x_driver
);
1383 static __exit
void exit_saa717x(void)
1385 i2c_del_driver(&saa717x_driver
);
1388 module_init(init_saa717x
);
1389 module_exit(exit_saa717x
);