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.18 2005/07/07 03:02:55 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>
18 #include <media/tuner.h>
20 #define PREFIX "TEA5767 "
22 /*****************************************************************************/
24 /******************************
25 * Write mode register values *
26 ******************************/
29 #define TEA5767_MUTE 0x80 /* Mutes output */
30 #define TEA5767_SEARCH 0x40 /* Activates station search */
31 /* Bits 0-5 for divider MSB */
34 /* Bits 0-7 for divider LSB */
38 /* Station search from botton to up */
39 #define TEA5767_SEARCH_UP 0x80
41 /* Searches with ADC output = 10 */
42 #define TEA5767_SRCH_HIGH_LVL 0x60
44 /* Searches with ADC output = 10 */
45 #define TEA5767_SRCH_MID_LVL 0x40
47 /* Searches with ADC output = 5 */
48 #define TEA5767_SRCH_LOW_LVL 0x20
50 /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
51 #define TEA5767_HIGH_LO_INJECT 0x10
54 #define TEA5767_MONO 0x08
56 /* Disable right channel and turns to mono */
57 #define TEA5767_MUTE_RIGHT 0x04
59 /* Disable left channel and turns to mono */
60 #define TEA5767_MUTE_LEFT 0x02
62 #define TEA5767_PORT1_HIGH 0x01
65 #define TEA5767_PORT2_HIGH 0x80
66 /* Chips stops working. Only I2C bus remains on */
67 #define TEA5767_STDBY 0x40
69 /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
70 #define TEA5767_JAPAN_BAND 0x20
72 /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
73 #define TEA5767_XTAL_32768 0x10
75 /* Cuts weak signals */
76 #define TEA5767_SOFT_MUTE 0x08
78 /* Activates high cut control */
79 #define TEA5767_HIGH_CUT_CTRL 0x04
81 /* Activates stereo noise control */
82 #define TEA5767_ST_NOISE_CTL 0x02
84 /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
85 #define TEA5767_SRCH_IND 0x01
89 /* By activating, it will use Xtal at 13 MHz as reference for divider */
90 #define TEA5767_PLLREF_ENABLE 0x80
92 /* By activating, deemphasis=50, or else, deemphasis of 50us */
93 #define TEA5767_DEEMPH_75 0X40
95 /*****************************
96 * Read mode register values *
97 *****************************/
100 #define TEA5767_READY_FLAG_MASK 0x80
101 #define TEA5767_BAND_LIMIT_MASK 0X40
102 /* Bits 0-5 for divider MSB after search or preset */
104 /* Second register */
105 /* Bits 0-7 for divider LSB after search or preset */
108 #define TEA5767_STEREO_MASK 0x80
109 #define TEA5767_IF_CNTR_MASK 0x7f
112 #define TEA5767_ADC_LEVEL_MASK 0xf0
115 #define TEA5767_CHIP_ID_MASK 0x0f
117 /* Fiveth register */
118 /* Reserved for future extensions */
119 #define TEA5767_RESERVED_MASK 0xff
121 enum tea5767_xtal_freq {
122 TEA5767_LOW_LO_32768 = 0,
123 TEA5767_HIGH_LO_32768 = 1,
124 TEA5767_LOW_LO_13MHz = 2,
125 TEA5767_HIGH_LO_13MHz = 3,
129 /*****************************************************************************/
131 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
133 struct tuner *t = i2c_get_clientdata(c);
135 tuner_warn("This tuner doesn't support TV freq.\n");
138 static void tea5767_status_dump(unsigned char *buffer)
140 unsigned int div, frq;
142 if (TEA5767_READY_FLAG_MASK & buffer[0])
143 printk(PREFIX "Ready Flag ON\n");
145 printk(PREFIX "Ready Flag OFF\n");
147 if (TEA5767_BAND_LIMIT_MASK & buffer[0])
148 printk(PREFIX "Tuner at band limit\n");
150 printk(PREFIX "Tuner not at band limit\n");
152 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
154 switch (TEA5767_HIGH_LO_32768) {
155 case TEA5767_HIGH_LO_13MHz:
156 frq = 1000 * (div * 50 - 700 - 225) / 4; /* Freq in KHz */
158 case TEA5767_LOW_LO_13MHz:
159 frq = 1000 * (div * 50 + 700 + 225) / 4; /* Freq in KHz */
161 case TEA5767_LOW_LO_32768:
162 frq = 1000 * (div * 32768 / 1000 + 700 + 225) / 4; /* Freq in KHz */
164 case TEA5767_HIGH_LO_32768:
166 frq = 1000 * (div * 32768 / 1000 - 700 - 225) / 4; /* Freq in KHz */
169 buffer[0] = (div >> 8) & 0x3f;
170 buffer[1] = div & 0xff;
172 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
173 frq / 1000, frq % 1000, div);
175 if (TEA5767_STEREO_MASK & buffer[2])
176 printk(PREFIX "Stereo\n");
178 printk(PREFIX "Mono\n");
180 printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
182 printk(PREFIX "ADC Level = %d\n",
183 (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
185 printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
187 printk(PREFIX "Reserved = 0x%02x\n",
188 (buffer[4] & TEA5767_RESERVED_MASK));
191 /* Freq should be specifyed at 62.5 Hz */
192 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
194 struct tuner *t = i2c_get_clientdata(c);
195 unsigned char buffer[5];
199 tuner_dbg (PREFIX "radio freq counter %d\n", frq);
201 /* Rounds freq to next decimal value - for 62.5 KHz step */
202 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
204 buffer[2] = TEA5767_PORT1_HIGH;
205 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
206 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
209 if (t->mode == T_STANDBY) {
210 tuner_dbg("TEA5767 set to standby mode\n");
211 buffer[3] |= TEA5767_STDBY;
214 if (t->audmode == V4L2_TUNER_MODE_MONO) {
215 tuner_dbg("TEA5767 set to mono\n");
216 buffer[2] |= TEA5767_MONO;
218 tuner_dbg("TEA5767 set to stereo\n");
221 /* Should be replaced */
222 switch (TEA5767_HIGH_LO_32768) {
223 case TEA5767_HIGH_LO_13MHz:
224 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
225 buffer[2] |= TEA5767_HIGH_LO_INJECT;
226 buffer[4] |= TEA5767_PLLREF_ENABLE;
227 div = (frq * 4 / 16 + 700 + 225 + 25) / 50;
229 case TEA5767_LOW_LO_13MHz:
230 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
232 buffer[4] |= TEA5767_PLLREF_ENABLE;
233 div = (frq * 4 / 16 - 700 - 225 + 25) / 50;
235 case TEA5767_LOW_LO_32768:
236 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
237 buffer[3] |= TEA5767_XTAL_32768;
238 /* const 700=4000*175 Khz - to adjust freq to right value */
239 div = (1000 * (frq * 4 / 16 - 700 - 225) + 16384) >> 15;
241 case TEA5767_HIGH_LO_32768:
243 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
245 buffer[2] |= TEA5767_HIGH_LO_INJECT;
246 buffer[3] |= TEA5767_XTAL_32768;
247 div = (1000 * (frq * 4 / 16 + 700 + 225) + 16384) >> 15;
250 buffer[0] = (div >> 8) & 0x3f;
251 buffer[1] = div & 0xff;
254 tea5767_status_dump(buffer);
256 if (5 != (rc = i2c_master_send(c, buffer, 5)))
257 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
260 static int tea5767_signal(struct i2c_client *c)
262 unsigned char buffer[5];
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) << (13 - 4));
273 static int tea5767_stereo(struct i2c_client *c)
275 unsigned char buffer[5];
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 int tea5767_autodetection(struct i2c_client *c)
292 unsigned char buffer[5] = { 0xff, 0xff, 0xff, 0xff, 0xff };
294 struct tuner *t = i2c_get_clientdata(c);
296 if (5 != (rc = i2c_master_recv(c, buffer, 5))) {
297 tuner_warn("it is not a TEA5767. Received %i chars.\n", rc);
301 /* If all bytes are the same then it's a TV tuner and not a tea5767 chip. */
302 if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
303 buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
304 tuner_warn("All bytes are equal. It is not a TEA5767\n");
309 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
310 * bit 0 : internally set to 0
311 * Byte 5: bit 7:0 : == 0
314 if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) {
315 tuner_warn("Chip ID is not zero. It is not a TEA5767\n");
318 tuner_warn("TEA5767 detected.\n");
322 int tea5767_tuner_init(struct i2c_client *c)
324 struct tuner *t = i2c_get_clientdata(c);
326 if (tea5767_autodetection(c) == EINVAL)
329 tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5767HN FM Radio");
330 strlcpy(c->name, "tea5767", sizeof(c->name));
332 t->tv_freq = set_tv_freq;
333 t->radio_freq = set_radio_freq;
334 t->has_signal = tea5767_signal;
335 t->is_stereo = tea5767_stereo;