1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * saa717x - Philips SAA717xHL video decoder driver
5 * Based on the saa7115 driver
8 * - Apply to SAA717x,NEC uPD64031,uPD64083. (1/31/2004)
11 * - support audio, video scaler etc, and checked the initialize sequence.
15 * Note: this is a reversed engineered driver based on captures from
16 * the I2C bus under Windows. This chip is very similar to the saa7134,
17 * though. Unfortunately, this driver is currently only working for NTSC.
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
25 #include <linux/videodev2.h>
26 #include <linux/i2c.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ctrls.h>
30 MODULE_DESCRIPTION("Philips SAA717x audio/video decoder driver");
31 MODULE_AUTHOR("K. Ohta, T. Adachi, Hans Verkuil");
32 MODULE_LICENSE("GPL");
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "Debug level (0-1)");
40 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
43 struct saa717x_state {
44 struct v4l2_subdev sd;
45 struct v4l2_ctrl_handler hdl;
57 u16 audio_main_treble;
58 u16 audio_main_volume;
59 u16 audio_main_balance;
63 static inline struct saa717x_state *to_state(struct v4l2_subdev *sd)
65 return container_of(sd, struct saa717x_state, sd);
68 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
70 return &container_of(ctrl->handler, struct saa717x_state, hdl)->sd;
73 /* ----------------------------------------------------------------------- */
76 #define TUNER_AUDIO_MONO 0 /* LL */
77 #define TUNER_AUDIO_STEREO 1 /* LR */
78 #define TUNER_AUDIO_LANG1 2 /* LL */
79 #define TUNER_AUDIO_LANG2 3 /* RR */
81 #define SAA717X_NTSC_WIDTH (704)
82 #define SAA717X_NTSC_HEIGHT (480)
84 /* ----------------------------------------------------------------------- */
86 static int saa717x_write(struct v4l2_subdev *sd, u32 reg, u32 value)
88 struct i2c_client *client = v4l2_get_subdevdata(sd);
89 struct i2c_adapter *adap = client->adapter;
90 int fw_addr = reg == 0x454 || (reg >= 0x464 && reg <= 0x478) || reg == 0x480 || reg == 0x488;
95 msg.addr = client->addr;
96 mm1[0] = (reg >> 8) & 0xff;
100 mm1[4] = (value >> 16) & 0xff;
101 mm1[3] = (value >> 8) & 0xff;
102 mm1[2] = value & 0xff;
104 mm1[2] = value & 0xff;
106 msg.len = fw_addr ? 5 : 3; /* Long Registers have *only* three bytes! */
108 v4l2_dbg(2, debug, sd, "wrote: reg 0x%03x=%08x\n", reg, value);
109 return i2c_transfer(adap, &msg, 1) == 1;
112 static void saa717x_write_regs(struct v4l2_subdev *sd, u32 *data)
114 while (data[0] || data[1]) {
115 saa717x_write(sd, data[0], data[1]);
120 static u32 saa717x_read(struct v4l2_subdev *sd, u32 reg)
122 struct i2c_client *client = v4l2_get_subdevdata(sd);
123 struct i2c_adapter *adap = client->adapter;
124 int fw_addr = (reg >= 0x404 && reg <= 0x4b8) || reg == 0x528;
125 unsigned char mm1[2];
126 unsigned char mm2[4] = { 0, 0, 0, 0 };
127 struct i2c_msg msgs[2];
131 msgs[1].flags = I2C_M_RD;
132 msgs[0].addr = msgs[1].addr = client->addr;
133 mm1[0] = (reg >> 8) & 0xff;
137 msgs[1].len = fw_addr ? 3 : 1; /* Multibyte Registers contains *only* 3 bytes */
139 i2c_transfer(adap, msgs, 2);
142 value = (mm2[2] << 16) | (mm2[1] << 8) | mm2[0];
146 v4l2_dbg(2, debug, sd, "read: reg 0x%03x=0x%08x\n", reg, value);
150 /* ----------------------------------------------------------------------- */
152 static u32 reg_init_initialize[] =
154 /* from linux driver */
155 0x101, 0x008, /* Increment delay */
157 0x103, 0x000, /* Analog input control 2 */
158 0x104, 0x090, /* Analog input control 3 */
159 0x105, 0x090, /* Analog input control 4 */
160 0x106, 0x0eb, /* Horizontal sync start */
161 0x107, 0x0e0, /* Horizontal sync stop */
162 0x109, 0x055, /* Luminance control */
164 0x10f, 0x02a, /* Chroma gain control */
165 0x110, 0x000, /* Chroma control 2 */
167 0x114, 0x045, /* analog/ADC */
169 0x118, 0x040, /* RAW data gain */
170 0x119, 0x080, /* RAW data offset */
172 0x044, 0x000, /* VBI horizontal input window start (L) TASK A */
173 0x045, 0x000, /* VBI horizontal input window start (H) TASK A */
174 0x046, 0x0cf, /* VBI horizontal input window stop (L) TASK A */
175 0x047, 0x002, /* VBI horizontal input window stop (H) TASK A */
177 0x049, 0x000, /* VBI vertical input window start (H) TASK A */
179 0x04c, 0x0d0, /* VBI horizontal output length (L) TASK A */
180 0x04d, 0x002, /* VBI horizontal output length (H) TASK A */
182 0x064, 0x080, /* Lumina brightness TASK A */
183 0x065, 0x040, /* Luminance contrast TASK A */
184 0x066, 0x040, /* Chroma saturation TASK A */
186 0x068, 0x000, /* VBI horizontal scaling increment (L) TASK A */
187 0x069, 0x004, /* VBI horizontal scaling increment (H) TASK A */
188 0x06a, 0x000, /* VBI phase offset TASK A */
190 0x06e, 0x000, /* Horizontal phase offset Luma TASK A */
191 0x06f, 0x000, /* Horizontal phase offset Chroma TASK A */
193 0x072, 0x000, /* Vertical filter mode TASK A */
195 0x084, 0x000, /* VBI horizontal input window start (L) TAKS B */
196 0x085, 0x000, /* VBI horizontal input window start (H) TAKS B */
197 0x086, 0x0cf, /* VBI horizontal input window stop (L) TAKS B */
198 0x087, 0x002, /* VBI horizontal input window stop (H) TAKS B */
200 0x089, 0x000, /* VBI vertical input window start (H) TAKS B */
202 0x08c, 0x0d0, /* VBI horizontal output length (L) TASK B */
203 0x08d, 0x002, /* VBI horizontal output length (H) TASK B */
205 0x0a4, 0x080, /* Lumina brightness TASK B */
206 0x0a5, 0x040, /* Luminance contrast TASK B */
207 0x0a6, 0x040, /* Chroma saturation TASK B */
209 0x0a8, 0x000, /* VBI horizontal scaling increment (L) TASK B */
210 0x0a9, 0x004, /* VBI horizontal scaling increment (H) TASK B */
211 0x0aa, 0x000, /* VBI phase offset TASK B */
213 0x0ae, 0x000, /* Horizontal phase offset Luma TASK B */
214 0x0af, 0x000, /*Horizontal phase offset Chroma TASK B */
216 0x0b2, 0x000, /* Vertical filter mode TASK B */
218 0x00c, 0x000, /* Start point GREEN path */
219 0x00d, 0x000, /* Start point BLUE path */
220 0x00e, 0x000, /* Start point RED path */
222 0x010, 0x010, /* GREEN path gamma curve --- */
237 0x01f, 0x0ff, /* --- GREEN path gamma curve */
239 0x020, 0x010, /* BLUE path gamma curve --- */
254 0x02f, 0x0ff, /* --- BLUE path gamma curve */
256 0x030, 0x010, /* RED path gamma curve --- */
271 0x03f, 0x0ff, /* --- RED path gamma curve */
273 0x109, 0x085, /* Luminance control */
275 /**** from app start ****/
276 0x584, 0x000, /* AGC gain control */
277 0x585, 0x000, /* Program count */
278 0x586, 0x003, /* Status reset */
279 0x588, 0x0ff, /* Number of audio samples (L) */
280 0x589, 0x00f, /* Number of audio samples (M) */
281 0x58a, 0x000, /* Number of audio samples (H) */
282 0x58b, 0x000, /* Audio select */
283 0x58c, 0x010, /* Audio channel assign1 */
284 0x58d, 0x032, /* Audio channel assign2 */
285 0x58e, 0x054, /* Audio channel assign3 */
286 0x58f, 0x023, /* Audio format */
287 0x590, 0x000, /* SIF control */
289 0x595, 0x000, /* ?? */
290 0x596, 0x000, /* ?? */
291 0x597, 0x000, /* ?? */
293 0x464, 0x00, /* Digital input crossbar1 */
295 0x46c, 0xbbbb10, /* Digital output selection1-3 */
296 0x470, 0x101010, /* Digital output selection4-6 */
298 0x478, 0x00, /* Sound feature control */
300 0x474, 0x18, /* Softmute control */
302 0x454, 0x0425b9, /* Sound Easy programming(reset) */
303 0x454, 0x042539, /* Sound Easy programming(reset) */
306 /**** common setting( of DVD play, including scaler commands) ****/
307 0x042, 0x003, /* Data path configuration for VBI (TASK A) */
309 0x082, 0x003, /* Data path configuration for VBI (TASK B) */
311 0x108, 0x0f8, /* Sync control */
312 0x2a9, 0x0fd, /* ??? */
313 0x102, 0x089, /* select video input "mode 9" */
314 0x111, 0x000, /* Mode/delay control */
316 0x10e, 0x00a, /* Chroma control 1 */
318 0x594, 0x002, /* SIF, analog I/O select */
320 0x454, 0x0425b9, /* Sound */
624 static u32 reg_init_tuner_input[] = {
625 0x108, 0x0f8, /* Sync control */
626 0x111, 0x000, /* Mode/delay control */
627 0x10e, 0x00a, /* Chroma control 1 */
632 static u32 reg_init_composite_input[] = {
633 0x108, 0x0e8, /* Sync control */
634 0x111, 0x000, /* Mode/delay control */
635 0x10e, 0x04a, /* Chroma control 1 */
640 static u32 reg_init_svideo_input[] = {
641 0x108, 0x0e8, /* Sync control */
642 0x111, 0x000, /* Mode/delay control */
643 0x10e, 0x04a, /* Chroma control 1 */
647 static u32 reg_set_audio_template[4][2] =
650 tadachi 6/29 DMA audio output select?
652 7-4: DMA2, 3-0: DMA1 ch. DMA4, DMA3 DMA2, DMA1
653 0: MAIN left, 1: MAIN right
654 2: AUX1 left, 3: AUX1 right
655 4: AUX2 left, 5: AUX2 right
656 6: DPL left, 7: DPL right
657 8: DPL center, 9: DPL surround
658 A: monitor output, B: digital sense */
661 /* tadachi 6/29 DAC and I2S output select?
663 7-4:DAC right ch. 3-0:DAC left ch.
664 I2S1 right,left I2S2 right,left */
673 { /* for LANG2/SAP */
679 /* Get detected audio flags (from saa7134 driver) */
680 static void get_inf_dev_status(struct v4l2_subdev *sd,
681 int *dual_flag, int *stereo_flag)
685 static char *stdres[0x20] = {
686 [0x00] = "no standard detected",
687 [0x01] = "B/G (in progress)",
688 [0x02] = "D/K (in progress)",
689 [0x03] = "M (in progress)",
692 [0x05] = "B/G NICAM",
693 [0x06] = "D/K A2 (1)",
694 [0x07] = "D/K A2 (2)",
695 [0x08] = "D/K A2 (3)",
696 [0x09] = "D/K NICAM",
704 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
705 [0x10] = "FM radio / IF 10.7 / 75 deemp",
706 [0x11] = "FM radio / IF sel / 50 deemp",
707 [0x12] = "FM radio / IF sel / 75 deemp",
709 [0x13 ... 0x1e] = "unknown",
710 [0x1f] = "??? [in progress]",
714 *dual_flag = *stereo_flag = 0;
716 /* (demdec status: 0x528) */
718 /* read current status */
719 reg_data3 = saa717x_read(sd, 0x0528);
721 v4l2_dbg(1, debug, sd, "tvaudio thread status: 0x%x [%s%s%s]\n",
722 reg_data3, stdres[reg_data3 & 0x1f],
723 (reg_data3 & 0x000020) ? ",stereo" : "",
724 (reg_data3 & 0x000040) ? ",dual" : "");
725 v4l2_dbg(1, debug, sd, "detailed status: "
726 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
727 (reg_data3 & 0x000080) ? " A2/EIAJ pilot tone " : "",
728 (reg_data3 & 0x000100) ? " A2/EIAJ dual " : "",
729 (reg_data3 & 0x000200) ? " A2/EIAJ stereo " : "",
730 (reg_data3 & 0x000400) ? " A2/EIAJ noise mute " : "",
732 (reg_data3 & 0x000800) ? " BTSC/FM radio pilot " : "",
733 (reg_data3 & 0x001000) ? " SAP carrier " : "",
734 (reg_data3 & 0x002000) ? " BTSC stereo noise mute " : "",
735 (reg_data3 & 0x004000) ? " SAP noise mute " : "",
736 (reg_data3 & 0x008000) ? " VDSP " : "",
738 (reg_data3 & 0x010000) ? " NICST " : "",
739 (reg_data3 & 0x020000) ? " NICDU " : "",
740 (reg_data3 & 0x040000) ? " NICAM muted " : "",
741 (reg_data3 & 0x080000) ? " NICAM reserve sound " : "",
743 (reg_data3 & 0x100000) ? " init done " : "");
745 if (reg_data3 & 0x000220) {
746 v4l2_dbg(1, debug, sd, "ST!!!\n");
750 if (reg_data3 & 0x000140) {
751 v4l2_dbg(1, debug, sd, "DUAL!!!\n");
756 /* regs write to set audio mode */
757 static void set_audio_mode(struct v4l2_subdev *sd, int audio_mode)
759 v4l2_dbg(1, debug, sd, "writing registers to set audio mode by set %d\n",
762 saa717x_write(sd, 0x46c, reg_set_audio_template[audio_mode][0]);
763 saa717x_write(sd, 0x470, reg_set_audio_template[audio_mode][1]);
766 /* write regs to set audio volume, bass and treble */
767 static int set_audio_regs(struct v4l2_subdev *sd,
768 struct saa717x_state *decoder)
770 u8 mute = 0xac; /* -84 dB */
772 unsigned int work_l, work_r;
774 /* set SIF analog I/O select */
775 saa717x_write(sd, 0x0594, decoder->audio_input);
776 v4l2_dbg(1, debug, sd, "set audio input %d\n",
777 decoder->audio_input);
779 /* normalize ( 65535 to 0 -> 24 to -40 (not -84)) */
780 work_l = (min(65536 - decoder->audio_main_balance, 32768) * decoder->audio_main_volume) / 32768;
781 work_r = (min(decoder->audio_main_balance, (u16)32768) * decoder->audio_main_volume) / 32768;
782 decoder->audio_main_vol_l = (long)work_l * (24 - (-40)) / 65535 - 40;
783 decoder->audio_main_vol_r = (long)work_r * (24 - (-40)) / 65535 - 40;
785 /* set main volume */
786 /* main volume L[7-0],R[7-0],0x00 24=24dB,-83dB, -84(mute) */
787 /* def:0dB->6dB(MPG600GR) */
788 /* if mute is on, set mute */
789 if (decoder->audio_main_mute) {
790 val = mute | (mute << 8);
792 val = (u8)decoder->audio_main_vol_l |
793 ((u8)decoder->audio_main_vol_r << 8);
796 saa717x_write(sd, 0x480, val);
798 /* set bass and treble */
799 val = decoder->audio_main_bass & 0x1f;
800 val |= (decoder->audio_main_treble & 0x1f) << 5;
801 saa717x_write(sd, 0x488, val);
805 /********** scaling staff ***********/
806 static void set_h_prescale(struct v4l2_subdev *sd,
807 int task, int prescale)
809 static const struct {
816 /* XPSC XACL XC2_1 XDCG VPFY */
828 static const int count = ARRAY_SIZE(vals);
831 task_shift = task * 0x40;
832 for (i = 0; i < count; i++)
833 if (vals[i].xpsc == prescale)
838 /* horizontal prescaling */
839 saa717x_write(sd, 0x60 + task_shift, vals[i].xpsc);
840 /* accumulation length */
841 saa717x_write(sd, 0x61 + task_shift, vals[i].xacl);
843 saa717x_write(sd, 0x62 + task_shift,
844 (vals[i].xc2_1 << 3) | vals[i].xdcg);
845 /*FIR prefilter control */
846 saa717x_write(sd, 0x63 + task_shift,
847 (vals[i].vpfy << 2) | vals[i].vpfy);
850 /********** scaling staff ***********/
851 static void set_v_scale(struct v4l2_subdev *sd, int task, int yscale)
855 task_shift = task * 0x40;
856 /* Vertical scaling ratio (LOW) */
857 saa717x_write(sd, 0x70 + task_shift, yscale & 0xff);
858 /* Vertical scaling ratio (HI) */
859 saa717x_write(sd, 0x71 + task_shift, yscale >> 8);
862 static int saa717x_s_ctrl(struct v4l2_ctrl *ctrl)
864 struct v4l2_subdev *sd = to_sd(ctrl);
865 struct saa717x_state *state = to_state(sd);
868 case V4L2_CID_BRIGHTNESS:
869 saa717x_write(sd, 0x10a, ctrl->val);
872 case V4L2_CID_CONTRAST:
873 saa717x_write(sd, 0x10b, ctrl->val);
876 case V4L2_CID_SATURATION:
877 saa717x_write(sd, 0x10c, ctrl->val);
881 saa717x_write(sd, 0x10d, ctrl->val);
884 case V4L2_CID_AUDIO_MUTE:
885 state->audio_main_mute = ctrl->val;
888 case V4L2_CID_AUDIO_VOLUME:
889 state->audio_main_volume = ctrl->val;
892 case V4L2_CID_AUDIO_BALANCE:
893 state->audio_main_balance = ctrl->val;
896 case V4L2_CID_AUDIO_TREBLE:
897 state->audio_main_treble = ctrl->val;
900 case V4L2_CID_AUDIO_BASS:
901 state->audio_main_bass = ctrl->val;
907 set_audio_regs(sd, state);
911 static int saa717x_s_video_routing(struct v4l2_subdev *sd,
912 u32 input, u32 output, u32 config)
914 struct saa717x_state *decoder = to_state(sd);
915 int is_tuner = input & 0x80; /* tuner input flag */
919 v4l2_dbg(1, debug, sd, "decoder set input (%d)\n", input);
920 /* inputs from 0-9 are available*/
921 /* saa717x have mode0-mode9 but mode5 is reserved. */
922 if (input > 9 || input == 5)
925 if (decoder->input != input) {
926 int input_line = input;
928 decoder->input = input_line;
929 v4l2_dbg(1, debug, sd, "now setting %s input %d\n",
930 input_line >= 6 ? "S-Video" : "Composite",
934 saa717x_write(sd, 0x102,
935 (saa717x_read(sd, 0x102) & 0xf0) |
938 /* bypass chrominance trap for modes 6..9 */
939 saa717x_write(sd, 0x109,
940 (saa717x_read(sd, 0x109) & 0x7f) |
941 (input_line < 6 ? 0x0 : 0x80));
943 /* change audio_mode */
946 set_audio_mode(sd, decoder->tuner_audio_mode);
948 /* Force to STEREO mode if Composite or
949 * S-Video were chosen */
950 set_audio_mode(sd, TUNER_AUDIO_STEREO);
952 /* change initialize procedure (Composite/S-Video) */
954 saa717x_write_regs(sd, reg_init_tuner_input);
955 else if (input_line >= 6)
956 saa717x_write_regs(sd, reg_init_svideo_input);
958 saa717x_write_regs(sd, reg_init_composite_input);
964 #ifdef CONFIG_VIDEO_ADV_DEBUG
965 static int saa717x_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
967 reg->val = saa717x_read(sd, reg->reg);
972 static int saa717x_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
974 u16 addr = reg->reg & 0xffff;
975 u8 val = reg->val & 0xff;
977 saa717x_write(sd, addr, val);
982 static int saa717x_set_fmt(struct v4l2_subdev *sd,
983 struct v4l2_subdev_state *sd_state,
984 struct v4l2_subdev_format *format)
986 struct v4l2_mbus_framefmt *fmt = &format->format;
987 int prescale, h_scale, v_scale;
989 v4l2_dbg(1, debug, sd, "decoder set size\n");
991 if (format->pad || fmt->code != MEDIA_BUS_FMT_FIXED)
994 /* FIXME need better bounds checking here */
995 if (fmt->width < 1 || fmt->width > 1440)
997 if (fmt->height < 1 || fmt->height > 960)
1000 fmt->field = V4L2_FIELD_INTERLACED;
1001 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
1003 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1006 /* scaling setting */
1007 /* NTSC and interlace only */
1008 prescale = SAA717X_NTSC_WIDTH / fmt->width;
1011 h_scale = 1024 * SAA717X_NTSC_WIDTH / prescale / fmt->width;
1013 v_scale = 512 * 2 * SAA717X_NTSC_HEIGHT / fmt->height;
1015 /* Horizontal prescaling etc */
1016 set_h_prescale(sd, 0, prescale);
1017 set_h_prescale(sd, 1, prescale);
1019 /* Horizontal scaling increment */
1021 saa717x_write(sd, 0x6C, (u8)(h_scale & 0xFF));
1022 saa717x_write(sd, 0x6D, (u8)((h_scale >> 8) & 0xFF));
1024 saa717x_write(sd, 0xAC, (u8)(h_scale & 0xFF));
1025 saa717x_write(sd, 0xAD, (u8)((h_scale >> 8) & 0xFF));
1027 /* Vertical prescaling etc */
1028 set_v_scale(sd, 0, v_scale);
1029 set_v_scale(sd, 1, v_scale);
1031 /* set video output size */
1032 /* video number of pixels at output */
1034 saa717x_write(sd, 0x5C, (u8)(fmt->width & 0xFF));
1035 saa717x_write(sd, 0x5D, (u8)((fmt->width >> 8) & 0xFF));
1037 saa717x_write(sd, 0x9C, (u8)(fmt->width & 0xFF));
1038 saa717x_write(sd, 0x9D, (u8)((fmt->width >> 8) & 0xFF));
1040 /* video number of lines at output */
1042 saa717x_write(sd, 0x5E, (u8)(fmt->height & 0xFF));
1043 saa717x_write(sd, 0x5F, (u8)((fmt->height >> 8) & 0xFF));
1045 saa717x_write(sd, 0x9E, (u8)(fmt->height & 0xFF));
1046 saa717x_write(sd, 0x9F, (u8)((fmt->height >> 8) & 0xFF));
1050 static int saa717x_s_radio(struct v4l2_subdev *sd)
1052 struct saa717x_state *decoder = to_state(sd);
1058 static int saa717x_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1060 struct saa717x_state *decoder = to_state(sd);
1062 v4l2_dbg(1, debug, sd, "decoder set norm ");
1063 v4l2_dbg(1, debug, sd, "(not yet implemented)\n");
1070 static int saa717x_s_audio_routing(struct v4l2_subdev *sd,
1071 u32 input, u32 output, u32 config)
1073 struct saa717x_state *decoder = to_state(sd);
1075 if (input < 3) { /* FIXME! --tadachi */
1076 decoder->audio_input = input;
1077 v4l2_dbg(1, debug, sd,
1078 "set decoder audio input to %d\n",
1079 decoder->audio_input);
1080 set_audio_regs(sd, decoder);
1086 static int saa717x_s_stream(struct v4l2_subdev *sd, int enable)
1088 struct saa717x_state *decoder = to_state(sd);
1090 v4l2_dbg(1, debug, sd, "decoder %s output\n",
1091 enable ? "enable" : "disable");
1092 decoder->enable = enable;
1093 saa717x_write(sd, 0x193, enable ? 0xa6 : 0x26);
1097 /* change audio mode */
1098 static int saa717x_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
1100 struct saa717x_state *decoder = to_state(sd);
1103 "MONO", "STEREO", "LANG1", "LANG2/SAP"
1106 audio_mode = TUNER_AUDIO_STEREO;
1108 switch (vt->audmode) {
1109 case V4L2_TUNER_MODE_MONO:
1110 audio_mode = TUNER_AUDIO_MONO;
1112 case V4L2_TUNER_MODE_STEREO:
1113 audio_mode = TUNER_AUDIO_STEREO;
1115 case V4L2_TUNER_MODE_LANG2:
1116 audio_mode = TUNER_AUDIO_LANG2;
1118 case V4L2_TUNER_MODE_LANG1:
1119 audio_mode = TUNER_AUDIO_LANG1;
1123 v4l2_dbg(1, debug, sd, "change audio mode to %s\n",
1125 decoder->tuner_audio_mode = audio_mode;
1126 /* The registers are not changed here. */
1127 /* See DECODER_ENABLE_OUTPUT section. */
1128 set_audio_mode(sd, decoder->tuner_audio_mode);
1132 static int saa717x_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1134 struct saa717x_state *decoder = to_state(sd);
1135 int dual_f, stereo_f;
1139 get_inf_dev_status(sd, &dual_f, &stereo_f);
1141 v4l2_dbg(1, debug, sd, "DETECT==st:%d dual:%d\n",
1145 if ((dual_f == 0) && (stereo_f == 0)) {
1146 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1147 v4l2_dbg(1, debug, sd, "DETECT==MONO\n");
1151 if (stereo_f == 1) {
1152 if (vt->audmode == V4L2_TUNER_MODE_STEREO ||
1153 vt->audmode == V4L2_TUNER_MODE_LANG1) {
1154 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1155 v4l2_dbg(1, debug, sd, "DETECT==ST(ST)\n");
1157 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1158 v4l2_dbg(1, debug, sd, "DETECT==ST(MONO)\n");
1164 if (vt->audmode == V4L2_TUNER_MODE_LANG2) {
1165 vt->rxsubchans = V4L2_TUNER_SUB_LANG2 | V4L2_TUNER_SUB_MONO;
1166 v4l2_dbg(1, debug, sd, "DETECT==DUAL1\n");
1168 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_MONO;
1169 v4l2_dbg(1, debug, sd, "DETECT==DUAL2\n");
1175 static int saa717x_log_status(struct v4l2_subdev *sd)
1177 struct saa717x_state *state = to_state(sd);
1179 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
1183 /* ----------------------------------------------------------------------- */
1185 static const struct v4l2_ctrl_ops saa717x_ctrl_ops = {
1186 .s_ctrl = saa717x_s_ctrl,
1189 static const struct v4l2_subdev_core_ops saa717x_core_ops = {
1190 #ifdef CONFIG_VIDEO_ADV_DEBUG
1191 .g_register = saa717x_g_register,
1192 .s_register = saa717x_s_register,
1194 .log_status = saa717x_log_status,
1197 static const struct v4l2_subdev_tuner_ops saa717x_tuner_ops = {
1198 .g_tuner = saa717x_g_tuner,
1199 .s_tuner = saa717x_s_tuner,
1200 .s_radio = saa717x_s_radio,
1203 static const struct v4l2_subdev_video_ops saa717x_video_ops = {
1204 .s_std = saa717x_s_std,
1205 .s_routing = saa717x_s_video_routing,
1206 .s_stream = saa717x_s_stream,
1209 static const struct v4l2_subdev_audio_ops saa717x_audio_ops = {
1210 .s_routing = saa717x_s_audio_routing,
1213 static const struct v4l2_subdev_pad_ops saa717x_pad_ops = {
1214 .set_fmt = saa717x_set_fmt,
1217 static const struct v4l2_subdev_ops saa717x_ops = {
1218 .core = &saa717x_core_ops,
1219 .tuner = &saa717x_tuner_ops,
1220 .audio = &saa717x_audio_ops,
1221 .video = &saa717x_video_ops,
1222 .pad = &saa717x_pad_ops,
1225 /* ----------------------------------------------------------------------- */
1228 /* i2c implementation */
1230 /* ----------------------------------------------------------------------- */
1231 static int saa717x_probe(struct i2c_client *client)
1233 struct saa717x_state *decoder;
1234 struct v4l2_ctrl_handler *hdl;
1235 struct v4l2_subdev *sd;
1239 /* Check if the adapter supports the needed features */
1240 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1243 decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
1244 if (decoder == NULL)
1248 v4l2_i2c_subdev_init(sd, client, &saa717x_ops);
1250 if (saa717x_write(sd, 0x5a4, 0xfe) &&
1251 saa717x_write(sd, 0x5a5, 0x0f) &&
1252 saa717x_write(sd, 0x5a6, 0x00) &&
1253 saa717x_write(sd, 0x5a7, 0x01))
1254 id = saa717x_read(sd, 0x5a0);
1255 if (id != 0xc2 && id != 0x32 && id != 0xf2 && id != 0x6c) {
1256 v4l2_dbg(1, debug, sd, "saa717x not found (id=%02x)\n", id);
1261 else if (id == 0x32)
1263 else if (id == 0x6c)
1267 v4l2_info(sd, "%s found @ 0x%x (%s)\n", p,
1268 client->addr << 1, client->adapter->name);
1270 hdl = &decoder->hdl;
1271 v4l2_ctrl_handler_init(hdl, 9);
1272 /* add in ascending ID order */
1273 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1274 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1275 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1276 V4L2_CID_CONTRAST, 0, 255, 1, 68);
1277 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1278 V4L2_CID_SATURATION, 0, 255, 1, 64);
1279 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1280 V4L2_CID_HUE, -128, 127, 1, 0);
1281 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1282 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 42000);
1283 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1284 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
1285 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1286 V4L2_CID_AUDIO_BASS, -16, 15, 1, 0);
1287 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1288 V4L2_CID_AUDIO_TREBLE, -16, 15, 1, 0);
1289 v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
1290 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
1291 sd->ctrl_handler = hdl;
1293 int err = hdl->error;
1295 v4l2_ctrl_handler_free(hdl);
1299 decoder->std = V4L2_STD_NTSC;
1300 decoder->input = -1;
1301 decoder->enable = 1;
1304 decoder->playback = 0; /* initially capture mode used */
1305 decoder->audio = 1; /* DECODER_AUDIO_48_KHZ */
1307 decoder->audio_input = 2; /* FIXME!! */
1309 decoder->tuner_audio_mode = TUNER_AUDIO_STEREO;
1310 /* set volume, bass and treble */
1311 decoder->audio_main_vol_l = 6;
1312 decoder->audio_main_vol_r = 6;
1314 v4l2_dbg(1, debug, sd, "writing init values\n");
1317 saa717x_write_regs(sd, reg_init_initialize);
1319 v4l2_ctrl_handler_setup(hdl);
1321 set_current_state(TASK_INTERRUPTIBLE);
1322 schedule_timeout(2*HZ);
1326 static void saa717x_remove(struct i2c_client *client)
1328 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1330 v4l2_device_unregister_subdev(sd);
1331 v4l2_ctrl_handler_free(sd->ctrl_handler);
1334 /* ----------------------------------------------------------------------- */
1336 static const struct i2c_device_id saa717x_id[] = {
1340 MODULE_DEVICE_TABLE(i2c, saa717x_id);
1342 static struct i2c_driver saa717x_driver = {
1346 .probe_new = saa717x_probe,
1347 .remove = saa717x_remove,
1348 .id_table = saa717x_id,
1351 module_i2c_driver(saa717x_driver);