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 /*****************************************************************************/
22 /******************************
23 * Write mode register values *
24 ******************************/
27 #define TEA5767_MUTE 0x80 /* Mutes output */
28 #define TEA5767_SEARCH 0x40 /* Activates station search */
29 /* Bits 0-5 for divider MSB */
32 /* Bits 0-7 for divider LSB */
36 /* Station search from botton to up */
37 #define TEA5767_SEARCH_UP 0x80
39 /* Searches with ADC output = 10 */
40 #define TEA5767_SRCH_HIGH_LVL 0x60
42 /* Searches with ADC output = 10 */
43 #define TEA5767_SRCH_MID_LVL 0x40
45 /* Searches with ADC output = 5 */
46 #define TEA5767_SRCH_LOW_LVL 0x20
48 /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
49 #define TEA5767_HIGH_LO_INJECT 0x10
52 #define TEA5767_MONO 0x08
54 /* Disable right channel and turns to mono */
55 #define TEA5767_MUTE_RIGHT 0x04
57 /* Disable left channel and turns to mono */
58 #define TEA5767_MUTE_LEFT 0x02
60 #define TEA5767_PORT1_HIGH 0x01
63 #define TEA5767_PORT2_HIGH 0x80
64 /* Chips stops working. Only I2C bus remains on */
65 #define TEA5767_STDBY 0x40
67 /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
68 #define TEA5767_JAPAN_BAND 0x20
70 /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
71 #define TEA5767_XTAL_32768 0x10
73 /* Cuts weak signals */
74 #define TEA5767_SOFT_MUTE 0x08
76 /* Activates high cut control */
77 #define TEA5767_HIGH_CUT_CTRL 0x04
79 /* Activates stereo noise control */
80 #define TEA5767_ST_NOISE_CTL 0x02
82 /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
83 #define TEA5767_SRCH_IND 0x01
87 /* By activating, it will use Xtal at 13 MHz as reference for divider */
88 #define TEA5767_PLLREF_ENABLE 0x80
90 /* By activating, deemphasis=50, or else, deemphasis of 50us */
91 #define TEA5767_DEEMPH_75 0X40
93 /*****************************
94 * Read mode register values *
95 *****************************/
98 #define TEA5767_READY_FLAG_MASK 0x80
99 #define TEA5767_BAND_LIMIT_MASK 0X40
100 /* Bits 0-5 for divider MSB after search or preset */
102 /* Second register */
103 /* Bits 0-7 for divider LSB after search or preset */
106 #define TEA5767_STEREO_MASK 0x80
107 #define TEA5767_IF_CNTR_MASK 0x7f
110 #define TEA5767_ADC_LEVEL_MASK 0xf0
113 #define TEA5767_CHIP_ID_MASK 0x0f
115 /* Fiveth register */
116 /* Reserved for future extensions */
117 #define TEA5767_RESERVED_MASK 0xff
119 enum tea5767_xtal_freq {
120 TEA5767_LOW_LO_32768 = 0,
121 TEA5767_HIGH_LO_32768 = 1,
122 TEA5767_LOW_LO_13MHz = 2,
123 TEA5767_HIGH_LO_13MHz = 3,
127 /*****************************************************************************/
129 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
131 struct tuner *t = i2c_get_clientdata(c);
133 tuner_warn("This tuner doesn't support TV freq.\n");
136 static void tea5767_status_dump(unsigned char *buffer)
138 unsigned int div, frq;
140 if (TEA5767_READY_FLAG_MASK & buffer[0])
141 printk(PREFIX "Ready Flag ON\n");
143 printk(PREFIX "Ready Flag OFF\n");
145 if (TEA5767_BAND_LIMIT_MASK & buffer[0])
146 printk(PREFIX "Tuner at band limit\n");
148 printk(PREFIX "Tuner not at band limit\n");
150 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
152 switch (TEA5767_HIGH_LO_32768) {
153 case TEA5767_HIGH_LO_13MHz:
154 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
156 case TEA5767_LOW_LO_13MHz:
157 frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */
159 case TEA5767_LOW_LO_32768:
160 frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */
162 case TEA5767_HIGH_LO_32768:
164 frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */
167 buffer[0] = (div >> 8) & 0x3f;
168 buffer[1] = div & 0xff;
170 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
171 frq / 1000, frq % 1000, div);
173 if (TEA5767_STEREO_MASK & buffer[2])
174 printk(PREFIX "Stereo\n");
176 printk(PREFIX "Mono\n");
178 printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
180 printk(PREFIX "ADC Level = %d\n",
181 (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
183 printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
185 printk(PREFIX "Reserved = 0x%02x\n",
186 (buffer[4] & TEA5767_RESERVED_MASK));
189 /* Freq should be specifyed at 62.5 Hz */
190 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
192 struct tuner *t = i2c_get_clientdata(c);
193 unsigned char buffer[5];
197 tuner_dbg (PREFIX "radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
199 /* Rounds freq to next decimal value - for 62.5 KHz step */
200 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
202 buffer[2] = TEA5767_PORT1_HIGH;
203 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
204 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
207 if (t->audmode == V4L2_TUNER_MODE_MONO) {
208 tuner_dbg("TEA5767 set to mono\n");
209 buffer[2] |= TEA5767_MONO;
211 tuner_dbg("TEA5767 set to stereo\n");
214 /* Should be replaced */
215 switch (TEA5767_HIGH_LO_32768) {
216 case TEA5767_HIGH_LO_13MHz:
217 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
218 buffer[2] |= TEA5767_HIGH_LO_INJECT;
219 buffer[4] |= TEA5767_PLLREF_ENABLE;
220 div = (frq * 4000 / 16 + 700000 + 225000 + 25000) / 50000;
222 case TEA5767_LOW_LO_13MHz:
223 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
225 buffer[4] |= TEA5767_PLLREF_ENABLE;
226 div = (frq * 4000 / 16 - 700000 - 225000 + 25000) / 50000;
228 case TEA5767_LOW_LO_32768:
229 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
230 buffer[3] |= TEA5767_XTAL_32768;
231 /* const 700=4000*175 Khz - to adjust freq to right value */
232 div = ((frq * 4000 / 16 - 700000 - 225000) + 16384) >> 15;
234 case TEA5767_HIGH_LO_32768:
236 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
238 buffer[2] |= TEA5767_HIGH_LO_INJECT;
239 buffer[3] |= TEA5767_XTAL_32768;
240 div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
243 buffer[0] = (div >> 8) & 0x3f;
244 buffer[1] = div & 0xff;
246 if (5 != (rc = i2c_master_send(c, buffer, 5)))
247 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
250 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
251 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
253 tea5767_status_dump(buffer);
257 static int tea5767_signal(struct i2c_client *c)
259 unsigned char buffer[5];
261 struct tuner *t = i2c_get_clientdata(c);
263 memset(buffer, 0, sizeof(buffer));
264 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
265 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
267 return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << (13 - 4));
270 static int tea5767_stereo(struct i2c_client *c)
272 unsigned char buffer[5];
274 struct tuner *t = i2c_get_clientdata(c);
276 memset(buffer, 0, sizeof(buffer));
277 if (5 != (rc = i2c_master_recv(c, buffer, 5)))
278 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
280 rc = buffer[2] & TEA5767_STEREO_MASK;
282 tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
284 return ((buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO : 0);
287 static void tea5767_standby(struct i2c_client *c)
289 unsigned char buffer[5];
290 struct tuner *t = i2c_get_clientdata(c);
293 div = (87500 * 4 + 700 + 225 + 25) / 50; /* Set frequency to 87.5 MHz */
294 buffer[0] = (div >> 8) & 0x3f;
295 buffer[1] = div & 0xff;
296 buffer[2] = TEA5767_PORT1_HIGH;
297 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
298 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND | TEA5767_STDBY;
301 if (5 != (rc = i2c_master_send(c, buffer, 5)))
302 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
305 int tea5767_autodetection(struct i2c_client *c)
307 unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
309 struct tuner *t = i2c_get_clientdata(c);
311 if ((rc = i2c_master_recv(c, buffer, 7))< 5) {
312 tuner_warn("It is not a TEA5767. Received %i bytes.\n", rc);
316 /* If all bytes are the same then it's a TV tuner and not a tea5767 */
317 if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
318 buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
319 tuner_warn("All bytes are equal. It is not a TEA5767\n");
324 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
325 * bit 0 : internally set to 0
326 * Byte 5: bit 7:0 : == 0
328 if (((buffer[3] & 0x0f) != 0x00) || (buffer[4] != 0x00)) {
329 tuner_warn("Chip ID is not zero. It is not a TEA5767\n");
333 /* It seems that tea5767 returns 0xff after the 5th byte */
334 if ((buffer[5] != 0xff) || (buffer[6] != 0xff)) {
335 tuner_warn("Returned more than 5 bytes. It is not a TEA5767\n");
339 tuner_warn("TEA5767 detected.\n");
343 int tea5767_tuner_init(struct i2c_client *c)
345 struct tuner *t = i2c_get_clientdata(c);
347 tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5767HN FM Radio");
348 strlcpy(c->name, "tea5767", sizeof(c->name));
350 t->tv_freq = set_tv_freq;
351 t->radio_freq = set_radio_freq;
352 t->has_signal = tea5767_signal;
353 t->is_stereo = tea5767_stereo;
354 t->standby = tea5767_standby;