V4L/DVB (3326): Fix stereo and standard reporting of msp3400 (esp. for radio)
[linux-2.6/suspend2-2.6.18.git] / drivers / media / video / tea5767.c
blob261b7a3c041735ce88337db628495c2c0c485526
1 /*
2 * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview
3 * I2C address is allways 0xC0.
6 * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@brturbo.com.br)
7 * This code is placed under the terms of the GNU General Public License
9 * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa
10 * from their contributions on DScaler.
13 #include <linux/i2c.h>
14 #include <linux/videodev.h>
15 #include <linux/delay.h>
16 #include <media/tuner.h>
18 #define PREFIX "TEA5767 "
20 /* from tuner-core.c */
21 extern int debug;
23 /*****************************************************************************/
25 /******************************
26 * Write mode register values *
27 ******************************/
29 /* First register */
30 #define TEA5767_MUTE 0x80 /* Mutes output */
31 #define TEA5767_SEARCH 0x40 /* Activates station search */
32 /* Bits 0-5 for divider MSB */
34 /* Second register */
35 /* Bits 0-7 for divider LSB */
37 /* Third register */
39 /* Station search from botton to up */
40 #define TEA5767_SEARCH_UP 0x80
42 /* Searches with ADC output = 10 */
43 #define TEA5767_SRCH_HIGH_LVL 0x60
45 /* Searches with ADC output = 10 */
46 #define TEA5767_SRCH_MID_LVL 0x40
48 /* Searches with ADC output = 5 */
49 #define TEA5767_SRCH_LOW_LVL 0x20
51 /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
52 #define TEA5767_HIGH_LO_INJECT 0x10
54 /* Disable stereo */
55 #define TEA5767_MONO 0x08
57 /* Disable right channel and turns to mono */
58 #define TEA5767_MUTE_RIGHT 0x04
60 /* Disable left channel and turns to mono */
61 #define TEA5767_MUTE_LEFT 0x02
63 #define TEA5767_PORT1_HIGH 0x01
65 /* Forth register */
66 #define TEA5767_PORT2_HIGH 0x80
67 /* Chips stops working. Only I2C bus remains on */
68 #define TEA5767_STDBY 0x40
70 /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
71 #define TEA5767_JAPAN_BAND 0x20
73 /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
74 #define TEA5767_XTAL_32768 0x10
76 /* Cuts weak signals */
77 #define TEA5767_SOFT_MUTE 0x08
79 /* Activates high cut control */
80 #define TEA5767_HIGH_CUT_CTRL 0x04
82 /* Activates stereo noise control */
83 #define TEA5767_ST_NOISE_CTL 0x02
85 /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
86 #define TEA5767_SRCH_IND 0x01
88 /* Fiveth register */
90 /* By activating, it will use Xtal at 13 MHz as reference for divider */
91 #define TEA5767_PLLREF_ENABLE 0x80
93 /* By activating, deemphasis=50, or else, deemphasis of 50us */
94 #define TEA5767_DEEMPH_75 0X40
96 /*****************************
97 * Read mode register values *
98 *****************************/
100 /* First register */
101 #define TEA5767_READY_FLAG_MASK 0x80
102 #define TEA5767_BAND_LIMIT_MASK 0X40
103 /* Bits 0-5 for divider MSB after search or preset */
105 /* Second register */
106 /* Bits 0-7 for divider LSB after search or preset */
108 /* Third register */
109 #define TEA5767_STEREO_MASK 0x80
110 #define TEA5767_IF_CNTR_MASK 0x7f
112 /* Four register */
113 #define TEA5767_ADC_LEVEL_MASK 0xf0
115 /* should be 0 */
116 #define TEA5767_CHIP_ID_MASK 0x0f
118 /* Fiveth register */
119 /* Reserved for future extensions */
120 #define TEA5767_RESERVED_MASK 0xff
122 enum tea5767_xtal_freq {
123 TEA5767_LOW_LO_32768 = 0,
124 TEA5767_HIGH_LO_32768 = 1,
125 TEA5767_LOW_LO_13MHz = 2,
126 TEA5767_HIGH_LO_13MHz = 3,
130 /*****************************************************************************/
132 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
134 struct tuner *t = i2c_get_clientdata(c);
136 tuner_warn("This tuner doesn't support TV freq.\n");
139 static void tea5767_status_dump(unsigned char *buffer)
141 unsigned int div, frq;
143 if (TEA5767_READY_FLAG_MASK & buffer[0])
144 printk(PREFIX "Ready Flag ON\n");
145 else
146 printk(PREFIX "Ready Flag OFF\n");
148 if (TEA5767_BAND_LIMIT_MASK & buffer[0])
149 printk(PREFIX "Tuner at band limit\n");
150 else
151 printk(PREFIX "Tuner not at band limit\n");
153 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
155 switch (TEA5767_HIGH_LO_32768) {
156 case TEA5767_HIGH_LO_13MHz:
157 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
158 break;
159 case TEA5767_LOW_LO_13MHz:
160 frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */
161 break;
162 case TEA5767_LOW_LO_32768:
163 frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */
164 break;
165 case TEA5767_HIGH_LO_32768:
166 default:
167 frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */
168 break;
170 buffer[0] = (div >> 8) & 0x3f;
171 buffer[1] = div & 0xff;
173 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
174 frq / 1000, frq % 1000, div);
176 if (TEA5767_STEREO_MASK & buffer[2])
177 printk(PREFIX "Stereo\n");
178 else
179 printk(PREFIX "Mono\n");
181 printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
183 printk(PREFIX "ADC Level = %d\n",
184 (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
186 printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
188 printk(PREFIX "Reserved = 0x%02x\n",
189 (buffer[4] & TEA5767_RESERVED_MASK));
192 /* Freq should be specifyed at 62.5 Hz */
193 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
195 struct tuner *t = i2c_get_clientdata(c);
196 unsigned char buffer[5];
197 unsigned div;
198 int rc;
200 tuner_dbg (PREFIX "radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
202 /* Rounds freq to next decimal value - for 62.5 KHz step */
203 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
205 buffer[2] = TEA5767_PORT1_HIGH;
206 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
207 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
208 buffer[4] = 0;
210 if (t->audmode == V4L2_TUNER_MODE_MONO) {
211 tuner_dbg("TEA5767 set to mono\n");
212 buffer[2] |= TEA5767_MONO;
213 } else {
214 tuner_dbg("TEA5767 set to stereo\n");
217 /* Should be replaced */
218 switch (TEA5767_HIGH_LO_32768) {
219 case TEA5767_HIGH_LO_13MHz:
220 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
221 buffer[2] |= TEA5767_HIGH_LO_INJECT;
222 buffer[4] |= TEA5767_PLLREF_ENABLE;
223 div = (frq * 4000 / 16 + 700000 + 225000 + 25000) / 50000;
224 break;
225 case TEA5767_LOW_LO_13MHz:
226 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
228 buffer[4] |= TEA5767_PLLREF_ENABLE;
229 div = (frq * 4000 / 16 - 700000 - 225000 + 25000) / 50000;
230 break;
231 case TEA5767_LOW_LO_32768:
232 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
233 buffer[3] |= TEA5767_XTAL_32768;
234 /* const 700=4000*175 Khz - to adjust freq to right value */
235 div = ((frq * 4000 / 16 - 700000 - 225000) + 16384) >> 15;
236 break;
237 case TEA5767_HIGH_LO_32768:
238 default:
239 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
241 buffer[2] |= TEA5767_HIGH_LO_INJECT;
242 buffer[3] |= TEA5767_XTAL_32768;
243 div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
244 break;
246 buffer[0] = (div >> 8) & 0x3f;
247 buffer[1] = div & 0xff;
249 if (5 != (rc = i2c_master_send(c, buffer, 5)))
250 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
252 if (debug) {
253 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
254 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
255 else
256 tea5767_status_dump(buffer);
260 static int tea5767_signal(struct i2c_client *c)
262 unsigned char buffer[5];
263 int rc;
264 struct tuner *t = i2c_get_clientdata(c);
266 memset(buffer, 0, sizeof(buffer));
267 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
268 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
270 return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << 8);
273 static int tea5767_stereo(struct i2c_client *c)
275 unsigned char buffer[5];
276 int rc;
277 struct tuner *t = i2c_get_clientdata(c);
279 memset(buffer, 0, sizeof(buffer));
280 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
281 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
283 rc = buffer[2] & TEA5767_STEREO_MASK;
285 tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
287 return ((buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO : 0);
290 static void tea5767_standby(struct i2c_client *c)
292 unsigned char buffer[5];
293 struct tuner *t = i2c_get_clientdata(c);
294 unsigned div, rc;
296 div = (87500 * 4 + 700 + 225 + 25) / 50; /* Set frequency to 87.5 MHz */
297 buffer[0] = (div >> 8) & 0x3f;
298 buffer[1] = div & 0xff;
299 buffer[2] = TEA5767_PORT1_HIGH;
300 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
301 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND | TEA5767_STDBY;
302 buffer[4] = 0;
304 if (5 != (rc = i2c_master_send(c, buffer, 5)))
305 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
308 int tea5767_autodetection(struct i2c_client *c)
310 unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
311 int rc;
312 struct tuner *t = i2c_get_clientdata(c);
314 if ((rc = i2c_master_recv(c, buffer, 7))< 5) {
315 tuner_warn("It is not a TEA5767. Received %i bytes.\n", rc);
316 return EINVAL;
319 /* If all bytes are the same then it's a TV tuner and not a tea5767 */
320 if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
321 buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
322 tuner_warn("All bytes are equal. It is not a TEA5767\n");
323 return EINVAL;
326 /* Status bytes:
327 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
328 * bit 0 : internally set to 0
329 * Byte 5: bit 7:0 : == 0
331 if (((buffer[3] & 0x0f) != 0x00) || (buffer[4] != 0x00)) {
332 tuner_warn("Chip ID is not zero. It is not a TEA5767\n");
333 return EINVAL;
336 /* It seems that tea5767 returns 0xff after the 5th byte */
337 if ((buffer[5] != 0xff) || (buffer[6] != 0xff)) {
338 tuner_warn("Returned more than 5 bytes. It is not a TEA5767\n");
339 return EINVAL;
342 tuner_warn("TEA5767 detected.\n");
343 return 0;
346 int tea5767_tuner_init(struct i2c_client *c)
348 struct tuner *t = i2c_get_clientdata(c);
350 tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5767HN FM Radio");
351 strlcpy(c->name, "tea5767", sizeof(c->name));
353 t->tv_freq = set_tv_freq;
354 t->radio_freq = set_radio_freq;
355 t->has_signal = tea5767_signal;
356 t->is_stereo = tea5767_stereo;
357 t->standby = tea5767_standby;
359 return (0);