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