Commit | Line | Data |
---|---|---|
586b0cab MCC |
1 | /* |
2 | * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview | |
3 | * I2C address is allways 0xC0. | |
4 | * | |
001abc93 | 5 | * $Id: tea5767.c,v 1.27 2005/07/31 12:10:56 mchehab Exp $ |
586b0cab MCC |
6 | * |
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 | |
9 | * | |
10 | * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa | |
11 | * from their contributions on DScaler. | |
12 | */ | |
13 | ||
586b0cab | 14 | #include <linux/i2c.h> |
f7ce3cc6 MCC |
15 | #include <linux/videodev.h> |
16 | #include <linux/delay.h> | |
17 | #include <media/tuner.h> | |
586b0cab MCC |
18 | |
19 | #define PREFIX "TEA5767 " | |
20 | ||
21 | /*****************************************************************************/ | |
22 | ||
23 | /****************************** | |
24 | * Write mode register values * | |
25 | ******************************/ | |
26 | ||
27 | /* First register */ | |
f7ce3cc6 MCC |
28 | #define TEA5767_MUTE 0x80 /* Mutes output */ |
29 | #define TEA5767_SEARCH 0x40 /* Activates station search */ | |
586b0cab MCC |
30 | /* Bits 0-5 for divider MSB */ |
31 | ||
32 | /* Second register */ | |
33 | /* Bits 0-7 for divider LSB */ | |
34 | ||
35 | /* Third register */ | |
36 | ||
37 | /* Station search from botton to up */ | |
38 | #define TEA5767_SEARCH_UP 0x80 | |
39 | ||
40 | /* Searches with ADC output = 10 */ | |
41 | #define TEA5767_SRCH_HIGH_LVL 0x60 | |
42 | ||
43 | /* Searches with ADC output = 10 */ | |
44 | #define TEA5767_SRCH_MID_LVL 0x40 | |
45 | ||
46 | /* Searches with ADC output = 5 */ | |
47 | #define TEA5767_SRCH_LOW_LVL 0x20 | |
48 | ||
49 | /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */ | |
50 | #define TEA5767_HIGH_LO_INJECT 0x10 | |
51 | ||
52 | /* Disable stereo */ | |
53 | #define TEA5767_MONO 0x08 | |
54 | ||
55 | /* Disable right channel and turns to mono */ | |
56 | #define TEA5767_MUTE_RIGHT 0x04 | |
57 | ||
58 | /* Disable left channel and turns to mono */ | |
59 | #define TEA5767_MUTE_LEFT 0x02 | |
60 | ||
61 | #define TEA5767_PORT1_HIGH 0x01 | |
62 | ||
63 | /* Forth register */ | |
64 | #define TEA5767_PORT2_HIGH 0x80 | |
65 | /* Chips stops working. Only I2C bus remains on */ | |
66 | #define TEA5767_STDBY 0x40 | |
67 | ||
68 | /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */ | |
69 | #define TEA5767_JAPAN_BAND 0x20 | |
70 | ||
71 | /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */ | |
72 | #define TEA5767_XTAL_32768 0x10 | |
73 | ||
74 | /* Cuts weak signals */ | |
75 | #define TEA5767_SOFT_MUTE 0x08 | |
76 | ||
77 | /* Activates high cut control */ | |
78 | #define TEA5767_HIGH_CUT_CTRL 0x04 | |
79 | ||
80 | /* Activates stereo noise control */ | |
81 | #define TEA5767_ST_NOISE_CTL 0x02 | |
82 | ||
83 | /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */ | |
84 | #define TEA5767_SRCH_IND 0x01 | |
85 | ||
86 | /* Fiveth register */ | |
87 | ||
88 | /* By activating, it will use Xtal at 13 MHz as reference for divider */ | |
89 | #define TEA5767_PLLREF_ENABLE 0x80 | |
90 | ||
91 | /* By activating, deemphasis=50, or else, deemphasis of 50us */ | |
92 | #define TEA5767_DEEMPH_75 0X40 | |
93 | ||
94 | /***************************** | |
95 | * Read mode register values * | |
96 | *****************************/ | |
97 | ||
98 | /* First register */ | |
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 */ | |
102 | ||
103 | /* Second register */ | |
104 | /* Bits 0-7 for divider LSB after search or preset */ | |
105 | ||
106 | /* Third register */ | |
107 | #define TEA5767_STEREO_MASK 0x80 | |
108 | #define TEA5767_IF_CNTR_MASK 0x7f | |
109 | ||
110 | /* Four register */ | |
111 | #define TEA5767_ADC_LEVEL_MASK 0xf0 | |
112 | ||
113 | /* should be 0 */ | |
114 | #define TEA5767_CHIP_ID_MASK 0x0f | |
115 | ||
116 | /* Fiveth register */ | |
117 | /* Reserved for future extensions */ | |
118 | #define TEA5767_RESERVED_MASK 0xff | |
119 | ||
f7ce3cc6 MCC |
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, | |
125 | }; | |
126 | ||
127 | ||
586b0cab MCC |
128 | /*****************************************************************************/ |
129 | ||
130 | static void set_tv_freq(struct i2c_client *c, unsigned int freq) | |
131 | { | |
132 | struct tuner *t = i2c_get_clientdata(c); | |
133 | ||
134 | tuner_warn("This tuner doesn't support TV freq.\n"); | |
135 | } | |
136 | ||
137 | static void tea5767_status_dump(unsigned char *buffer) | |
138 | { | |
139 | unsigned int div, frq; | |
140 | ||
141 | if (TEA5767_READY_FLAG_MASK & buffer[0]) | |
142 | printk(PREFIX "Ready Flag ON\n"); | |
143 | else | |
144 | printk(PREFIX "Ready Flag OFF\n"); | |
145 | ||
146 | if (TEA5767_BAND_LIMIT_MASK & buffer[0]) | |
147 | printk(PREFIX "Tuner at band limit\n"); | |
148 | else | |
149 | printk(PREFIX "Tuner not at band limit\n"); | |
150 | ||
f7ce3cc6 | 151 | div = ((buffer[0] & 0x3f) << 8) | buffer[1]; |
586b0cab MCC |
152 | |
153 | switch (TEA5767_HIGH_LO_32768) { | |
154 | case TEA5767_HIGH_LO_13MHz: | |
c5287ba1 | 155 | frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */ |
586b0cab MCC |
156 | break; |
157 | case TEA5767_LOW_LO_13MHz: | |
c5287ba1 | 158 | frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */ |
586b0cab MCC |
159 | break; |
160 | case TEA5767_LOW_LO_32768: | |
c5287ba1 | 161 | frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */ |
586b0cab MCC |
162 | break; |
163 | case TEA5767_HIGH_LO_32768: | |
164 | default: | |
c5287ba1 | 165 | frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */ |
586b0cab MCC |
166 | break; |
167 | } | |
f7ce3cc6 MCC |
168 | buffer[0] = (div >> 8) & 0x3f; |
169 | buffer[1] = div & 0xff; | |
586b0cab MCC |
170 | |
171 | printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n", | |
f7ce3cc6 | 172 | frq / 1000, frq % 1000, div); |
586b0cab MCC |
173 | |
174 | if (TEA5767_STEREO_MASK & buffer[2]) | |
175 | printk(PREFIX "Stereo\n"); | |
176 | else | |
177 | printk(PREFIX "Mono\n"); | |
178 | ||
f7ce3cc6 | 179 | printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK); |
586b0cab | 180 | |
f7ce3cc6 MCC |
181 | printk(PREFIX "ADC Level = %d\n", |
182 | (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4); | |
586b0cab | 183 | |
f7ce3cc6 | 184 | printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK)); |
586b0cab | 185 | |
f7ce3cc6 MCC |
186 | printk(PREFIX "Reserved = 0x%02x\n", |
187 | (buffer[4] & TEA5767_RESERVED_MASK)); | |
586b0cab MCC |
188 | } |
189 | ||
190 | /* Freq should be specifyed at 62.5 Hz */ | |
191 | static void set_radio_freq(struct i2c_client *c, unsigned int frq) | |
192 | { | |
193 | struct tuner *t = i2c_get_clientdata(c); | |
f7ce3cc6 | 194 | unsigned char buffer[5]; |
586b0cab MCC |
195 | unsigned div; |
196 | int rc; | |
197 | ||
c5287ba1 | 198 | tuner_dbg (PREFIX "radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000); |
586b0cab MCC |
199 | |
200 | /* Rounds freq to next decimal value - for 62.5 KHz step */ | |
201 | /* frq = 20*(frq/16)+radio_frq[frq%16]; */ | |
202 | ||
203 | buffer[2] = TEA5767_PORT1_HIGH; | |
f7ce3cc6 MCC |
204 | buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL | |
205 | TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND; | |
206 | buffer[4] = 0; | |
207 | ||
208 | if (t->mode == T_STANDBY) { | |
209 | tuner_dbg("TEA5767 set to standby mode\n"); | |
210 | buffer[3] |= TEA5767_STDBY; | |
211 | } | |
586b0cab MCC |
212 | |
213 | if (t->audmode == V4L2_TUNER_MODE_MONO) { | |
214 | tuner_dbg("TEA5767 set to mono\n"); | |
215 | buffer[2] |= TEA5767_MONO; | |
f7ce3cc6 MCC |
216 | } else { |
217 | tuner_dbg("TEA5767 set to stereo\n"); | |
218 | } | |
586b0cab | 219 | |
f7ce3cc6 MCC |
220 | /* Should be replaced */ |
221 | switch (TEA5767_HIGH_LO_32768) { | |
586b0cab | 222 | case TEA5767_HIGH_LO_13MHz: |
f7ce3cc6 | 223 | tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n"); |
586b0cab MCC |
224 | buffer[2] |= TEA5767_HIGH_LO_INJECT; |
225 | buffer[4] |= TEA5767_PLLREF_ENABLE; | |
c5287ba1 | 226 | div = (frq * 4000 / 16 + 700000 + 225000 + 25000) / 50000; |
586b0cab MCC |
227 | break; |
228 | case TEA5767_LOW_LO_13MHz: | |
f7ce3cc6 | 229 | tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n"); |
586b0cab MCC |
230 | |
231 | buffer[4] |= TEA5767_PLLREF_ENABLE; | |
c5287ba1 | 232 | div = (frq * 4000 / 16 - 700000 - 225000 + 25000) / 50000; |
586b0cab MCC |
233 | break; |
234 | case TEA5767_LOW_LO_32768: | |
f7ce3cc6 | 235 | tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n"); |
586b0cab MCC |
236 | buffer[3] |= TEA5767_XTAL_32768; |
237 | /* const 700=4000*175 Khz - to adjust freq to right value */ | |
c5287ba1 | 238 | div = ((frq * 4000 / 16 - 700000 - 225000) + 16384) >> 15; |
586b0cab MCC |
239 | break; |
240 | case TEA5767_HIGH_LO_32768: | |
241 | default: | |
f7ce3cc6 | 242 | tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n"); |
586b0cab MCC |
243 | |
244 | buffer[2] |= TEA5767_HIGH_LO_INJECT; | |
245 | buffer[3] |= TEA5767_XTAL_32768; | |
c5287ba1 | 246 | div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15; |
586b0cab MCC |
247 | break; |
248 | } | |
f7ce3cc6 MCC |
249 | buffer[0] = (div >> 8) & 0x3f; |
250 | buffer[1] = div & 0xff; | |
586b0cab | 251 | |
f7ce3cc6 MCC |
252 | if (5 != (rc = i2c_master_send(c, buffer, 5))) |
253 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); | |
c5287ba1 MCC |
254 | |
255 | if (tuner_debug) { | |
256 | if (5 != (rc = i2c_master_recv(c, buffer, 5))) | |
257 | tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc); | |
258 | else | |
259 | tea5767_status_dump(buffer); | |
260 | } | |
586b0cab MCC |
261 | } |
262 | ||
263 | static int tea5767_signal(struct i2c_client *c) | |
264 | { | |
265 | unsigned char buffer[5]; | |
266 | int rc; | |
267 | struct tuner *t = i2c_get_clientdata(c); | |
268 | ||
f7ce3cc6 MCC |
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); | |
586b0cab | 272 | |
f7ce3cc6 | 273 | return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << (13 - 4)); |
586b0cab MCC |
274 | } |
275 | ||
276 | static int tea5767_stereo(struct i2c_client *c) | |
277 | { | |
278 | unsigned char buffer[5]; | |
279 | int rc; | |
280 | struct tuner *t = i2c_get_clientdata(c); | |
281 | ||
f7ce3cc6 MCC |
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); | |
586b0cab MCC |
285 | |
286 | rc = buffer[2] & TEA5767_STEREO_MASK; | |
287 | ||
f7ce3cc6 | 288 | tuner_dbg("TEA5767 radio ST GET = %02x\n", rc); |
586b0cab | 289 | |
f7ce3cc6 | 290 | return ((buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO : 0); |
586b0cab MCC |
291 | } |
292 | ||
f7ce3cc6 | 293 | int tea5767_autodetection(struct i2c_client *c) |
586b0cab | 294 | { |
fd3113e8 | 295 | unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
586b0cab MCC |
296 | int rc; |
297 | struct tuner *t = i2c_get_clientdata(c); | |
298 | ||
001abc93 | 299 | if (7 != (rc = i2c_master_recv(c, buffer, 7))) { |
c5287ba1 | 300 | tuner_warn("It is not a TEA5767. Received %i bytes.\n", rc); |
586b0cab MCC |
301 | return EINVAL; |
302 | } | |
303 | ||
fd3113e8 | 304 | /* If all bytes are the same then it's a TV tuner and not a tea5767 */ |
f7ce3cc6 MCC |
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"); | |
586b0cab MCC |
308 | return EINVAL; |
309 | } | |
310 | ||
311 | /* Status bytes: | |
312 | * Byte 4: bit 3:1 : CI (Chip Identification) == 0 | |
f7ce3cc6 | 313 | * bit 0 : internally set to 0 |
586b0cab MCC |
314 | * Byte 5: bit 7:0 : == 0 |
315 | */ | |
586b0cab | 316 | if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) { |
f7ce3cc6 | 317 | tuner_warn("Chip ID is not zero. It is not a TEA5767\n"); |
586b0cab MCC |
318 | return EINVAL; |
319 | } | |
fd3113e8 MCC |
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"); | |
323 | return EINVAL; | |
324 | } | |
c5287ba1 | 325 | |
001abc93 MCC |
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"); | |
329 | return EINVAL; | |
330 | } | |
331 | ||
f7ce3cc6 | 332 | tuner_warn("TEA5767 detected.\n"); |
586b0cab MCC |
333 | return 0; |
334 | } | |
335 | ||
336 | int tea5767_tuner_init(struct i2c_client *c) | |
337 | { | |
338 | struct tuner *t = i2c_get_clientdata(c); | |
339 | ||
001abc93 MCC |
340 | tuner_info("type set to %d (%s)\n", t->type, |
341 | "Philips TEA5767HN FM Radio"); | |
f7ce3cc6 | 342 | strlcpy(c->name, "tea5767", sizeof(c->name)); |
586b0cab | 343 | |
f7ce3cc6 | 344 | t->tv_freq = set_tv_freq; |
586b0cab MCC |
345 | t->radio_freq = set_radio_freq; |
346 | t->has_signal = tea5767_signal; | |
f7ce3cc6 | 347 | t->is_stereo = tea5767_stereo; |
586b0cab MCC |
348 | |
349 | return (0); | |
350 | } |