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