2 * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview
3 * I2C address is allways 0xC0.
5 * $Id: tea5767.c,v 1.27 2005/07/31 12:10:56 mchehab Exp $
7 * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@brturbo.com.br)
8 * This code is placed under the terms of the GNU General Public License
10 * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa
11 * from their contributions on DScaler.
14 #include <linux/i2c.h>
15 #include <linux/videodev.h>
16 #include <linux/delay.h>
17 #include <media/tuner.h>
19 #define PREFIX "TEA5767 "
21 /*****************************************************************************/
23 /******************************
24 * Write mode register values *
25 ******************************/
28 #define TEA5767_MUTE 0x80 /* Mutes output */
29 #define TEA5767_SEARCH 0x40 /* Activates station search */
30 /* Bits 0-5 for divider MSB */
33 /* Bits 0-7 for divider LSB */
37 /* Station search from botton to up */
38 #define TEA5767_SEARCH_UP 0x80
40 /* Searches with ADC output = 10 */
41 #define TEA5767_SRCH_HIGH_LVL 0x60
43 /* Searches with ADC output = 10 */
44 #define TEA5767_SRCH_MID_LVL 0x40
46 /* Searches with ADC output = 5 */
47 #define TEA5767_SRCH_LOW_LVL 0x20
49 /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
50 #define TEA5767_HIGH_LO_INJECT 0x10
53 #define TEA5767_MONO 0x08
55 /* Disable right channel and turns to mono */
56 #define TEA5767_MUTE_RIGHT 0x04
58 /* Disable left channel and turns to mono */
59 #define TEA5767_MUTE_LEFT 0x02
61 #define TEA5767_PORT1_HIGH 0x01
64 #define TEA5767_PORT2_HIGH 0x80
65 /* Chips stops working. Only I2C bus remains on */
66 #define TEA5767_STDBY 0x40
68 /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
69 #define TEA5767_JAPAN_BAND 0x20
71 /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
72 #define TEA5767_XTAL_32768 0x10
74 /* Cuts weak signals */
75 #define TEA5767_SOFT_MUTE 0x08
77 /* Activates high cut control */
78 #define TEA5767_HIGH_CUT_CTRL 0x04
80 /* Activates stereo noise control */
81 #define TEA5767_ST_NOISE_CTL 0x02
83 /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
84 #define TEA5767_SRCH_IND 0x01
88 /* By activating, it will use Xtal at 13 MHz as reference for divider */
89 #define TEA5767_PLLREF_ENABLE 0x80
91 /* By activating, deemphasis=50, or else, deemphasis of 50us */
92 #define TEA5767_DEEMPH_75 0X40
94 /*****************************
95 * Read mode register values *
96 *****************************/
99 #define TEA5767_READY_FLAG_MASK 0x80
100 #define TEA5767_BAND_LIMIT_MASK 0X40
101 /* Bits 0-5 for divider MSB after search or preset */
103 /* Second register */
104 /* Bits 0-7 for divider LSB after search or preset */
107 #define TEA5767_STEREO_MASK 0x80
108 #define TEA5767_IF_CNTR_MASK 0x7f
111 #define TEA5767_ADC_LEVEL_MASK 0xf0
114 #define TEA5767_CHIP_ID_MASK 0x0f
116 /* Fiveth register */
117 /* Reserved for future extensions */
118 #define TEA5767_RESERVED_MASK 0xff
120 enum tea5767_xtal_freq {
121 TEA5767_LOW_LO_32768 = 0,
122 TEA5767_HIGH_LO_32768 = 1,
123 TEA5767_LOW_LO_13MHz = 2,
124 TEA5767_HIGH_LO_13MHz = 3,
128 /*****************************************************************************/
130 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
132 struct tuner *t = i2c_get_clientdata(c);
134 tuner_warn("This tuner doesn't support TV freq.\n");
137 static void tea5767_status_dump(unsigned char *buffer)
139 unsigned int div, frq;
141 if (TEA5767_READY_FLAG_MASK & buffer[0])
142 printk(PREFIX "Ready Flag ON\n");
144 printk(PREFIX "Ready Flag OFF\n");
146 if (TEA5767_BAND_LIMIT_MASK & buffer[0])
147 printk(PREFIX "Tuner at band limit\n");
149 printk(PREFIX "Tuner not at band limit\n");
151 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
153 switch (TEA5767_HIGH_LO_32768) {
154 case TEA5767_HIGH_LO_13MHz:
155 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
157 case TEA5767_LOW_LO_13MHz:
158 frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */
160 case TEA5767_LOW_LO_32768:
161 frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */
163 case TEA5767_HIGH_LO_32768:
165 frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */
168 buffer[0] = (div >> 8) & 0x3f;
169 buffer[1] = div & 0xff;
171 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
172 frq / 1000, frq % 1000, div);
174 if (TEA5767_STEREO_MASK & buffer[2])
175 printk(PREFIX "Stereo\n");
177 printk(PREFIX "Mono\n");
179 printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
181 printk(PREFIX "ADC Level = %d\n",
182 (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
184 printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
186 printk(PREFIX "Reserved = 0x%02x\n",
187 (buffer[4] & TEA5767_RESERVED_MASK));
190 /* Freq should be specifyed at 62.5 Hz */
191 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
193 struct tuner *t = i2c_get_clientdata(c);
194 unsigned char buffer[5];
198 tuner_dbg (PREFIX "radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
200 /* Rounds freq to next decimal value - for 62.5 KHz step */
201 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
203 buffer[2] = TEA5767_PORT1_HIGH;
204 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
205 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
208 if (t->mode == T_STANDBY) {
209 tuner_dbg("TEA5767 set to standby mode\n");
210 buffer[3] |= TEA5767_STDBY;
213 if (t->audmode == V4L2_TUNER_MODE_MONO) {
214 tuner_dbg("TEA5767 set to mono\n");
215 buffer[2] |= TEA5767_MONO;
217 tuner_dbg("TEA5767 set to stereo\n");
220 /* Should be replaced */
221 switch (TEA5767_HIGH_LO_32768) {
222 case TEA5767_HIGH_LO_13MHz:
223 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
224 buffer[2] |= TEA5767_HIGH_LO_INJECT;
225 buffer[4] |= TEA5767_PLLREF_ENABLE;
226 div = (frq * 4000 / 16 + 700000 + 225000 + 25000) / 50000;
228 case TEA5767_LOW_LO_13MHz:
229 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
231 buffer[4] |= TEA5767_PLLREF_ENABLE;
232 div = (frq * 4000 / 16 - 700000 - 225000 + 25000) / 50000;
234 case TEA5767_LOW_LO_32768:
235 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
236 buffer[3] |= TEA5767_XTAL_32768;
237 /* const 700=4000*175 Khz - to adjust freq to right value */
238 div = ((frq * 4000 / 16 - 700000 - 225000) + 16384) >> 15;
240 case TEA5767_HIGH_LO_32768:
242 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
244 buffer[2] |= TEA5767_HIGH_LO_INJECT;
245 buffer[3] |= TEA5767_XTAL_32768;
246 div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
249 buffer[0] = (div >> 8) & 0x3f;
250 buffer[1] = div & 0xff;
252 if (5 != (rc = i2c_master_send(c, buffer, 5)))
253 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
256 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
257 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
259 tea5767_status_dump(buffer);
263 static int tea5767_signal(struct i2c_client *c)
265 unsigned char buffer[5];
267 struct tuner *t = i2c_get_clientdata(c);
269 memset(buffer, 0, sizeof(buffer));
270 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
271 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
273 return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << (13 - 4));
276 static int tea5767_stereo(struct i2c_client *c)
278 unsigned char buffer[5];
280 struct tuner *t = i2c_get_clientdata(c);
282 memset(buffer, 0, sizeof(buffer));
283 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
284 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
286 rc = buffer[2] & TEA5767_STEREO_MASK;
288 tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
290 return ((buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO : 0);
293 int tea5767_autodetection(struct i2c_client *c)
295 unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
297 struct tuner *t = i2c_get_clientdata(c);
299 if (7 != (rc = i2c_master_recv(c, buffer, 7))) {
300 tuner_warn("It is not a TEA5767. Received %i bytes.\n", rc);
304 /* If all bytes are the same then it's a TV tuner and not a tea5767 */
305 if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
306 buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
307 tuner_warn("All bytes are equal. It is not a TEA5767\n");
312 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
313 * bit 0 : internally set to 0
314 * Byte 5: bit 7:0 : == 0
316 if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) {
317 tuner_warn("Chip ID is not zero. It is not a TEA5767\n");
320 /* It seems that tea5767 returns 0xff after the 5th byte */
321 if ((buffer[5] != 0xff) || (buffer[6] != 0xff)) {
322 tuner_warn("Returned more than 5 bytes. It is not a TEA5767\n");
326 /* It seems that tea5767 returns 0xff after the 5th byte */
327 if ((buffer[5] != 0xff) || (buffer[6] != 0xff)) {
328 tuner_warn("Returned more than 5 bytes. It is not a TEA5767\n");
332 tuner_warn("TEA5767 detected.\n");
336 int tea5767_tuner_init(struct i2c_client *c)
338 struct tuner *t = i2c_get_clientdata(c);
340 tuner_info("type set to %d (%s)\n", t->type,
341 "Philips TEA5767HN FM Radio");
342 strlcpy(c->name, "tea5767", sizeof(c->name));
344 t->tv_freq = set_tv_freq;
345 t->radio_freq = set_radio_freq;
346 t->has_signal = tea5767_signal;
347 t->is_stereo = tea5767_stereo;