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.11 2005/06/21 15:40:33 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/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
19 #include <linux/timer.h>
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/videodev.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-algo-bit.h>
27 #include <media/tuner.h>
29 /* Declared at tuner-core.c */
30 extern unsigned int tuner_debug;
32 #define PREFIX "TEA5767 "
34 /*****************************************************************************/
36 /******************************
37 * Write mode register values *
38 ******************************/
41 #define TEA5767_MUTE 0x80 /* Mutes output */
42 #define TEA5767_SEARCH 0x40 /* Activates station search */
43 /* Bits 0-5 for divider MSB */
46 /* Bits 0-7 for divider LSB */
50 /* Station search from botton to up */
51 #define TEA5767_SEARCH_UP 0x80
53 /* Searches with ADC output = 10 */
54 #define TEA5767_SRCH_HIGH_LVL 0x60
56 /* Searches with ADC output = 10 */
57 #define TEA5767_SRCH_MID_LVL 0x40
59 /* Searches with ADC output = 5 */
60 #define TEA5767_SRCH_LOW_LVL 0x20
62 /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
63 #define TEA5767_HIGH_LO_INJECT 0x10
66 #define TEA5767_MONO 0x08
68 /* Disable right channel and turns to mono */
69 #define TEA5767_MUTE_RIGHT 0x04
71 /* Disable left channel and turns to mono */
72 #define TEA5767_MUTE_LEFT 0x02
74 #define TEA5767_PORT1_HIGH 0x01
77 #define TEA5767_PORT2_HIGH 0x80
78 /* Chips stops working. Only I2C bus remains on */
79 #define TEA5767_STDBY 0x40
81 /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
82 #define TEA5767_JAPAN_BAND 0x20
84 /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
85 #define TEA5767_XTAL_32768 0x10
87 /* Cuts weak signals */
88 #define TEA5767_SOFT_MUTE 0x08
90 /* Activates high cut control */
91 #define TEA5767_HIGH_CUT_CTRL 0x04
93 /* Activates stereo noise control */
94 #define TEA5767_ST_NOISE_CTL 0x02
96 /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
97 #define TEA5767_SRCH_IND 0x01
101 /* By activating, it will use Xtal at 13 MHz as reference for divider */
102 #define TEA5767_PLLREF_ENABLE 0x80
104 /* By activating, deemphasis=50, or else, deemphasis of 50us */
105 #define TEA5767_DEEMPH_75 0X40
107 /*****************************
108 * Read mode register values *
109 *****************************/
112 #define TEA5767_READY_FLAG_MASK 0x80
113 #define TEA5767_BAND_LIMIT_MASK 0X40
114 /* Bits 0-5 for divider MSB after search or preset */
116 /* Second register */
117 /* Bits 0-7 for divider LSB after search or preset */
120 #define TEA5767_STEREO_MASK 0x80
121 #define TEA5767_IF_CNTR_MASK 0x7f
124 #define TEA5767_ADC_LEVEL_MASK 0xf0
127 #define TEA5767_CHIP_ID_MASK 0x0f
129 /* Fiveth register */
130 /* Reserved for future extensions */
131 #define TEA5767_RESERVED_MASK 0xff
133 /*****************************************************************************/
135 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
137 struct tuner *t = i2c_get_clientdata(c);
139 tuner_warn("This tuner doesn't support TV freq.\n");
142 static void tea5767_status_dump(unsigned char *buffer)
144 unsigned int div, frq;
146 if (TEA5767_READY_FLAG_MASK & buffer[0])
147 printk(PREFIX "Ready Flag ON\n");
149 printk(PREFIX "Ready Flag OFF\n");
151 if (TEA5767_BAND_LIMIT_MASK & buffer[0])
152 printk(PREFIX "Tuner at band limit\n");
154 printk(PREFIX "Tuner not at band limit\n");
156 div=((buffer[0]&0x3f)<<8) | buffer[1];
158 switch (TEA5767_HIGH_LO_32768) {
159 case TEA5767_HIGH_LO_13MHz:
160 frq = 1000*(div*50-700-225)/4; /* Freq in KHz */
162 case TEA5767_LOW_LO_13MHz:
163 frq = 1000*(div*50+700+225)/4; /* Freq in KHz */
165 case TEA5767_LOW_LO_32768:
166 frq = 1000*(div*32768/1000+700+225)/4; /* Freq in KHz */
168 case TEA5767_HIGH_LO_32768:
170 frq = 1000*(div*32768/1000-700-225)/4; /* Freq in KHz */
173 buffer[0] = (div>>8) & 0x3f;
174 buffer[1] = div & 0xff;
176 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
177 frq/1000,frq%1000,div);
179 if (TEA5767_STEREO_MASK & buffer[2])
180 printk(PREFIX "Stereo\n");
182 printk(PREFIX "Mono\n");
184 printk(PREFIX "IF Counter = %d\n",buffer[2] & TEA5767_IF_CNTR_MASK);
186 printk(PREFIX "ADC Level = %d\n",(buffer[3] & TEA5767_ADC_LEVEL_MASK)>>4);
188 printk(PREFIX "Chip ID = %d\n",(buffer[3] & TEA5767_CHIP_ID_MASK));
190 printk(PREFIX "Reserved = 0x%02x\n",(buffer[4] & TEA5767_RESERVED_MASK));
193 /* Freq should be specifyed at 62.5 Hz */
194 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
196 struct tuner *t = i2c_get_clientdata(c);
197 unsigned char buffer[5];
202 printk(PREFIX "radio freq counter %d\n",frq);
204 /* Rounds freq to next decimal value - for 62.5 KHz step */
205 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
207 buffer[2] = TEA5767_PORT1_HIGH;
208 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL | TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
211 if (t->audmode == V4L2_TUNER_MODE_MONO) {
212 tuner_dbg("TEA5767 set to mono\n");
213 buffer[2] |= TEA5767_MONO;
215 tuner_dbg("TEA5767 set to stereo\n");
218 case TEA5767_HIGH_LO_13MHz:
219 tuner_dbg("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
220 buffer[2] |= TEA5767_HIGH_LO_INJECT;
221 buffer[4] |= TEA5767_PLLREF_ENABLE;
222 div = (frq*4/16+700+225+25)/50;
224 case TEA5767_LOW_LO_13MHz:
225 tuner_dbg("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
227 buffer[4] |= TEA5767_PLLREF_ENABLE;
228 div = (frq*4/16-700-225+25)/50;
230 case TEA5767_LOW_LO_32768:
231 tuner_dbg("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
232 buffer[3] |= TEA5767_XTAL_32768;
233 /* const 700=4000*175 Khz - to adjust freq to right value */
234 div = (1000*(frq*4/16-700-225)+16384)>>15;
236 case TEA5767_HIGH_LO_32768:
238 tuner_dbg("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
240 buffer[2] |= TEA5767_HIGH_LO_INJECT;
241 buffer[3] |= TEA5767_XTAL_32768;
242 div = (1000*(frq*4/16+700+225)+16384)>>15;
245 buffer[0] = (div>>8) & 0x3f;
246 buffer[1] = div & 0xff;
249 tea5767_status_dump(buffer);
251 if (5 != (rc = i2c_master_send(c,buffer,5)))
252 tuner_warn("i2c i/o error: rc == %d (should be 5)\n",rc);
255 static int tea5767_signal(struct i2c_client *c)
257 unsigned char buffer[5];
259 struct tuner *t = i2c_get_clientdata(c);
261 memset(buffer,0,sizeof(buffer));
262 if (5 != (rc = i2c_master_recv(c,buffer,5)))
263 tuner_warn ( "i2c i/o error: rc == %d (should be 5)\n",rc);
265 return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) <<(13-4));
268 static int tea5767_stereo(struct i2c_client *c)
270 unsigned char buffer[5];
272 struct tuner *t = i2c_get_clientdata(c);
274 memset(buffer,0,sizeof(buffer));
275 if (5 != (rc = i2c_master_recv(c,buffer,5)))
276 tuner_warn ( "i2c i/o error: rc == %d (should be 5)\n",rc);
278 rc = buffer[2] & TEA5767_STEREO_MASK;
281 tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
283 return ( (buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO: 0);
286 int tea_detection(struct i2c_client *c)
288 unsigned char buffer[5]= { 0xff, 0xff, 0xff, 0xff, 0xff };
290 struct tuner *t = i2c_get_clientdata(c);
292 if (5 != (rc = i2c_master_recv(c,buffer,5))) {
293 tuner_warn ( "it is not a TEA5767. Received %i chars.\n",rc );
297 /* If all bytes are the same then it's a TV tuner and not a tea5767 chip. */
298 if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
299 buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
300 tuner_warn ( "All bytes are equal. It is not a TEA5767\n" );
305 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
306 * bit 0 : internally set to 0
307 * Byte 5: bit 7:0 : == 0
310 if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) {
311 tuner_warn ( "Chip ID is not zero. It is not a TEA5767\n" );
314 tuner_warn ( "TEA5767 detected.\n" );
318 int tea5767_tuner_init(struct i2c_client *c)
320 struct tuner *t = i2c_get_clientdata(c);
322 if (tea_detection(c)==EINVAL) return EINVAL;
324 tuner_info("type set to %d (%s)\n",
325 t->type, TEA5767_TUNER_NAME);
326 strlcpy(c->name, TEA5767_TUNER_NAME, sizeof(c->name));
328 t->tv_freq = set_tv_freq;
329 t->radio_freq = set_radio_freq;
330 t->has_signal = tea5767_signal;
331 t->is_stereo = tea5767_stereo;