blob: 4c9c4e01331ac2891f967c24466ef81eaf5c71b0 [file] [log] [blame]
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07001/*
2 * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview
3 * I2C address is allways 0xC0.
4 *
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07005 *
Mauro Carvalho Chehab2e7c6dc2006-04-03 07:53:40 -03006 * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@infradead.org)
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07007 * This code is placed under the terms of the GNU General Public License
8 *
9 * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa
10 * from their contributions on DScaler.
11 */
12
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070013#include <linux/i2c.h>
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070014#include <linux/delay.h>
Michael Krufkyffbb8072007-08-21 01:14:12 -030015#include <linux/videodev.h>
Michael Krufky8218b0b2007-06-26 13:12:08 -030016#include "tuner-driver.h"
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070017
18#define PREFIX "TEA5767 "
19
Hans Verkuilfac9e892006-01-09 15:32:40 -020020/* from tuner-core.c */
Hans Verkuilf9195de2006-01-11 19:01:01 -020021extern int tuner_debug;
Hans Verkuilfac9e892006-01-09 15:32:40 -020022
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070023/*****************************************************************************/
24
25/******************************
26 * Write mode register values *
27 ******************************/
28
29/* First register */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070030#define TEA5767_MUTE 0x80 /* Mutes output */
31#define TEA5767_SEARCH 0x40 /* Activates station search */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070032/* Bits 0-5 for divider MSB */
33
34/* Second register */
35/* Bits 0-7 for divider LSB */
36
37/* Third register */
38
39/* Station search from botton to up */
40#define TEA5767_SEARCH_UP 0x80
41
42/* Searches with ADC output = 10 */
43#define TEA5767_SRCH_HIGH_LVL 0x60
44
45/* Searches with ADC output = 10 */
46#define TEA5767_SRCH_MID_LVL 0x40
47
48/* Searches with ADC output = 5 */
49#define TEA5767_SRCH_LOW_LVL 0x20
50
51/* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
52#define TEA5767_HIGH_LO_INJECT 0x10
53
54/* Disable stereo */
55#define TEA5767_MONO 0x08
56
57/* Disable right channel and turns to mono */
58#define TEA5767_MUTE_RIGHT 0x04
59
60/* Disable left channel and turns to mono */
61#define TEA5767_MUTE_LEFT 0x02
62
63#define TEA5767_PORT1_HIGH 0x01
64
Hans Verkuil27487d42006-01-15 15:04:52 -020065/* Fourth register */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070066#define TEA5767_PORT2_HIGH 0x80
67/* Chips stops working. Only I2C bus remains on */
68#define TEA5767_STDBY 0x40
69
70/* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
71#define TEA5767_JAPAN_BAND 0x20
72
73/* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
74#define TEA5767_XTAL_32768 0x10
75
76/* Cuts weak signals */
77#define TEA5767_SOFT_MUTE 0x08
78
79/* Activates high cut control */
80#define TEA5767_HIGH_CUT_CTRL 0x04
81
82/* Activates stereo noise control */
83#define TEA5767_ST_NOISE_CTL 0x02
84
85/* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
86#define TEA5767_SRCH_IND 0x01
87
Hans Verkuil27487d42006-01-15 15:04:52 -020088/* Fifth register */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070089
90/* By activating, it will use Xtal at 13 MHz as reference for divider */
91#define TEA5767_PLLREF_ENABLE 0x80
92
93/* By activating, deemphasis=50, or else, deemphasis of 50us */
94#define TEA5767_DEEMPH_75 0X40
95
96/*****************************
97 * Read mode register values *
98 *****************************/
99
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 */
104
105/* Second register */
106/* Bits 0-7 for divider LSB after search or preset */
107
108/* Third register */
109#define TEA5767_STEREO_MASK 0x80
110#define TEA5767_IF_CNTR_MASK 0x7f
111
Hans Verkuil27487d42006-01-15 15:04:52 -0200112/* Fourth register */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700113#define TEA5767_ADC_LEVEL_MASK 0xf0
114
115/* should be 0 */
116#define TEA5767_CHIP_ID_MASK 0x0f
117
Hans Verkuil27487d42006-01-15 15:04:52 -0200118/* Fifth register */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700119/* Reserved for future extensions */
120#define TEA5767_RESERVED_MASK 0xff
121
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700122enum tea5767_xtal_freq {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800123 TEA5767_LOW_LO_32768 = 0,
124 TEA5767_HIGH_LO_32768 = 1,
125 TEA5767_LOW_LO_13MHz = 2,
126 TEA5767_HIGH_LO_13MHz = 3,
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700127};
128
129
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700130/*****************************************************************************/
131
132static void set_tv_freq(struct i2c_client *c, unsigned int freq)
133{
134 struct tuner *t = i2c_get_clientdata(c);
135
136 tuner_warn("This tuner doesn't support TV freq.\n");
137}
138
139static void tea5767_status_dump(unsigned char *buffer)
140{
141 unsigned int div, frq;
142
143 if (TEA5767_READY_FLAG_MASK & buffer[0])
144 printk(PREFIX "Ready Flag ON\n");
145 else
146 printk(PREFIX "Ready Flag OFF\n");
147
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");
152
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700153 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700154
155 switch (TEA5767_HIGH_LO_32768) {
156 case TEA5767_HIGH_LO_13MHz:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700157 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700158 break;
159 case TEA5767_LOW_LO_13MHz:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700160 frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700161 break;
162 case TEA5767_LOW_LO_32768:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700163 frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700164 break;
165 case TEA5767_HIGH_LO_32768:
166 default:
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700167 frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700168 break;
169 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700170 buffer[0] = (div >> 8) & 0x3f;
171 buffer[1] = div & 0xff;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700172
173 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700174 frq / 1000, frq % 1000, div);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700175
176 if (TEA5767_STEREO_MASK & buffer[2])
177 printk(PREFIX "Stereo\n");
178 else
179 printk(PREFIX "Mono\n");
180
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700181 printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700182
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700183 printk(PREFIX "ADC Level = %d\n",
184 (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700185
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700186 printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700187
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700188 printk(PREFIX "Reserved = 0x%02x\n",
189 (buffer[4] & TEA5767_RESERVED_MASK));
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700190}
191
192/* Freq should be specifyed at 62.5 Hz */
193static void set_radio_freq(struct i2c_client *c, unsigned int frq)
194{
195 struct tuner *t = i2c_get_clientdata(c);
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700196 unsigned char buffer[5];
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700197 unsigned div;
198 int rc;
199
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700200 tuner_dbg (PREFIX "radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700201
202 /* Rounds freq to next decimal value - for 62.5 KHz step */
203 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
204
205 buffer[2] = TEA5767_PORT1_HIGH;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700206 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
207 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
208 buffer[4] = 0;
209
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700210 if (t->audmode == V4L2_TUNER_MODE_MONO) {
211 tuner_dbg("TEA5767 set to mono\n");
212 buffer[2] |= TEA5767_MONO;
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700213 } else {
214 tuner_dbg("TEA5767 set to stereo\n");
215 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700216
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700217 /* Should be replaced */
218 switch (TEA5767_HIGH_LO_32768) {
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700219 case TEA5767_HIGH_LO_13MHz:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700220 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700221 buffer[2] |= TEA5767_HIGH_LO_INJECT;
222 buffer[4] |= TEA5767_PLLREF_ENABLE;
Hans Verkuil27487d42006-01-15 15:04:52 -0200223 div = (frq * (4000 / 16) + 700000 + 225000 + 25000) / 50000;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700224 break;
225 case TEA5767_LOW_LO_13MHz:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700226 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700227
228 buffer[4] |= TEA5767_PLLREF_ENABLE;
Hans Verkuil27487d42006-01-15 15:04:52 -0200229 div = (frq * (4000 / 16) - 700000 - 225000 + 25000) / 50000;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700230 break;
231 case TEA5767_LOW_LO_32768:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700232 tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700233 buffer[3] |= TEA5767_XTAL_32768;
234 /* const 700=4000*175 Khz - to adjust freq to right value */
Hans Verkuil27487d42006-01-15 15:04:52 -0200235 div = ((frq * (4000 / 16) - 700000 - 225000) + 16384) >> 15;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700236 break;
237 case TEA5767_HIGH_LO_32768:
238 default:
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700239 tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700240
241 buffer[2] |= TEA5767_HIGH_LO_INJECT;
242 buffer[3] |= TEA5767_XTAL_32768;
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700243 div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700244 break;
245 }
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700246 buffer[0] = (div >> 8) & 0x3f;
247 buffer[1] = div & 0xff;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700248
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700249 if (5 != (rc = i2c_master_send(c, buffer, 5)))
250 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700251
Hans Verkuilf9195de2006-01-11 19:01:01 -0200252 if (tuner_debug) {
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700253 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);
257 }
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700258}
259
260static int tea5767_signal(struct i2c_client *c)
261{
262 unsigned char buffer[5];
263 int rc;
264 struct tuner *t = i2c_get_clientdata(c);
265
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700266 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);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700269
Hans Verkuilf9a91f02006-01-09 15:25:42 -0200270 return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << 8);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700271}
272
273static int tea5767_stereo(struct i2c_client *c)
274{
275 unsigned char buffer[5];
276 int rc;
277 struct tuner *t = i2c_get_clientdata(c);
278
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700279 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);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700282
283 rc = buffer[2] & TEA5767_STEREO_MASK;
284
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700285 tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700286
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700287 return ((buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO : 0);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700288}
289
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700290static void tea5767_standby(struct i2c_client *c)
291{
292 unsigned char buffer[5];
293 struct tuner *t = i2c_get_clientdata(c);
294 unsigned div, rc;
295
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;
303
304 if (5 != (rc = i2c_master_send(c, buffer, 5)))
305 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
306}
307
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700308int tea5767_autodetection(struct i2c_client *c)
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700309{
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700310 unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700311 int rc;
312 struct tuner *t = i2c_get_clientdata(c);
313
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700314 if ((rc = i2c_master_recv(c, buffer, 7))< 5) {
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700315 tuner_warn("It is not a TEA5767. Received %i bytes.\n", rc);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700316 return EINVAL;
317 }
318
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700319 /* If all bytes are the same then it's a TV tuner and not a tea5767 */
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700320 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");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700323 return EINVAL;
324 }
325
326 /* Status bytes:
327 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700328 * bit 0 : internally set to 0
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700329 * Byte 5: bit 7:0 : == 0
330 */
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700331 if (((buffer[3] & 0x0f) != 0x00) || (buffer[4] != 0x00)) {
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700332 tuner_warn("Chip ID is not zero. It is not a TEA5767\n");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700333 return EINVAL;
334 }
Mauro Carvalho Chehabc5287ba2005-07-15 03:56:28 -0700335
Mauro Carvalho Chehab001abc92005-08-01 21:11:38 -0700336 /* 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;
340 }
341
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700342 tuner_warn("TEA5767 detected.\n");
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700343 return 0;
344}
345
Michael Krufky0f838f82007-06-06 16:17:17 -0300346static struct tuner_operations tea5767_tuner_ops = {
347 .set_tv_freq = set_tv_freq,
348 .set_radio_freq = set_radio_freq,
349 .has_signal = tea5767_signal,
350 .is_stereo = tea5767_stereo,
351 .standby = tea5767_standby,
352};
353
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700354int tea5767_tuner_init(struct i2c_client *c)
355{
356 struct tuner *t = i2c_get_clientdata(c);
357
Mauro Carvalho Chehab793cf9e2005-09-09 13:03:37 -0700358 tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5767HN FM Radio");
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -0700359 strlcpy(c->name, "tea5767", sizeof(c->name));
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700360
Michael Krufky0f838f82007-06-06 16:17:17 -0300361 memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700362
363 return (0);
364}