]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/tea5761.c
V4L/DVB (5766): ET61x251 driver updates
[linux-2.6-omap-h63xx.git] / drivers / media / video / tea5761.c
1 /*
2  * For Philips TEA5761 FM Chip
3  * I2C address is allways 0x20 (0x10 at 7-bit mode).
4  *
5  * Copyright (c) 2005-2007 Mauro Carvalho Chehab (mchehab@infradead.org)
6  * This code is placed under the terms of the GNUv2 General Public License
7  *
8  */
9
10 #include <linux/i2c.h>
11 #include <linux/videodev.h>
12 #include <linux/delay.h>
13 #include <media/tuner.h>
14
15 #define PREFIX "TEA5761 "
16
17 /* from tuner-core.c */
18 extern int tuner_debug;
19
20 /*****************************************************************************/
21
22 /***************************
23  * TEA5761HN I2C registers *
24  ***************************/
25
26 /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */
27
28         /* first byte for reading */
29 #define TEA5761_INTREG_IFFLAG           0x10
30 #define TEA5761_INTREG_LEVFLAG          0x8
31 #define TEA5761_INTREG_FRRFLAG          0x2
32 #define TEA5761_INTREG_BLFLAG           0x1
33
34         /* second byte for reading / byte for writing */
35 #define TEA5761_INTREG_IFMSK            0x10
36 #define TEA5761_INTREG_LEVMSK           0x8
37 #define TEA5761_INTREG_FRMSK            0x2
38 #define TEA5761_INTREG_BLMSK            0x1
39
40 /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */
41
42         /* First byte */
43 #define TEA5761_FRQSET_SEARCH_UP 0x80           /* 1=Station search from botton to up */
44 #define TEA5761_FRQSET_SEARCH_MODE 0x40         /* 1=Search mode */
45
46         /* Bits 0-5 for divider MSB */
47
48         /* Second byte */
49         /* Bits 0-7 for divider LSB */
50
51 /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */
52
53         /* first byte */
54
55 #define TEA5761_TNCTRL_PUPD_0   0x40    /* Power UP/Power Down MSB */
56 #define TEA5761_TNCTRL_BLIM     0X20    /* 1= Japan Frequencies, 0= European frequencies */
57 #define TEA5761_TNCTRL_SWPM     0x10    /* 1= software port is FRRFLAG */
58 #define TEA5761_TNCTRL_IFCTC    0x08    /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */
59 #define TEA5761_TNCTRL_AFM      0x04
60 #define TEA5761_TNCTRL_SMUTE    0x02    /* 1= Soft mute */
61 #define TEA5761_TNCTRL_SNC      0x01
62
63         /* second byte */
64
65 #define TEA5761_TNCTRL_MU       0x80    /* 1=Hard mute */
66 #define TEA5761_TNCTRL_SSL_1    0x40
67 #define TEA5761_TNCTRL_SSL_0    0x20
68 #define TEA5761_TNCTRL_HLSI     0x10
69 #define TEA5761_TNCTRL_MST      0x08    /* 1 = mono */
70 #define TEA5761_TNCTRL_SWP      0x04
71 #define TEA5761_TNCTRL_DTC      0x02    /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */
72 #define TEA5761_TNCTRL_AHLSI    0x01
73
74 /* FRQCHECK - Read: bytes 6 and 7  */
75         /* First byte */
76
77         /* Bits 0-5 for divider MSB */
78
79         /* Second byte */
80         /* Bits 0-7 for divider LSB */
81
82 /* TUNCHECK - Read: bytes 8 and 9  */
83
84         /* First byte */
85 #define TEA5761_TUNCHECK_IF_MASK        0x7e    /* IF count */
86 #define TEA5761_TUNCHECK_TUNTO          0x01
87
88         /* Second byte */
89 #define TEA5761_TUNCHECK_LEV_MASK       0xf0    /* Level Count */
90 #define TEA5761_TUNCHECK_LD             0x08
91 #define TEA5761_TUNCHECK_STEREO         0x04
92
93 /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */
94
95         /* All zero = no test mode */
96
97 /* MANID - Read: bytes 12 and 13 */
98
99         /* First byte - should be 0x10 */
100 #define TEA5767_MANID_VERSION_MASK      0xf0    /* Version = 1 */
101 #define TEA5767_MANID_ID_MSB_MASK       0x0f    /* Manufacurer ID - should be 0 */
102
103         /* Second byte - Should be 0x2b */
104
105 #define TEA5767_MANID_ID_LSB_MASK       0xfe    /* Manufacturer ID - should be 0x15 */
106 #define TEA5767_MANID_IDAV              0x01    /* 1 = Chip has ID, 0 = Chip has no ID */
107
108 /* Chip ID - Read: bytes 14 and 15 */
109
110         /* First byte - should be 0x57 */
111
112         /* Second byte - should be 0x61 */
113
114 /*****************************************************************************/
115
116 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
117 {
118         struct tuner *t = i2c_get_clientdata(c);
119
120         tuner_warn("This tuner doesn't support TV freq.\n");
121 }
122
123 #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */
124 static void tea5761_status_dump(unsigned char *buffer)
125 {
126         unsigned int div, frq;
127
128         div = ((buffer[2] & 0x3f) << 8) | buffer[3];
129
130         frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4;      /* Freq in KHz */
131
132         printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
133                frq / 1000, frq % 1000, div);
134 }
135
136 /* Freq should be specifyed at 62.5 Hz */
137 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
138 {
139         struct tuner *t = i2c_get_clientdata(c);
140         unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };
141         unsigned div;
142         int rc;
143
144         tuner_dbg (PREFIX "radio freq counter %d\n", frq);
145
146         if (t->mode == T_STANDBY) {
147                 tuner_dbg("TEA5761 set to standby mode\n");
148                 buffer[5] |= TEA5761_TNCTRL_MU;
149         } else {
150                 buffer[4] |= TEA5761_TNCTRL_PUPD_0;
151         }
152
153
154         if (t->audmode == V4L2_TUNER_MODE_MONO) {
155                 tuner_dbg("TEA5761 set to mono\n");
156                 buffer[5] |= TEA5761_TNCTRL_MST;
157 ;
158         } else {
159                 tuner_dbg("TEA5761 set to stereo\n");
160         }
161
162         div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15;
163         buffer[1] = (div >> 8) & 0x3f;
164         buffer[2] = div & 0xff;
165
166         if (tuner_debug)
167                 tea5761_status_dump(buffer);
168
169         if (7 != (rc = i2c_master_send(c, buffer, 7)))
170                 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
171 }
172
173 static int tea5761_signal(struct i2c_client *c)
174 {
175         unsigned char buffer[16];
176         int rc;
177         struct tuner *t = i2c_get_clientdata(c);
178
179         memset(buffer, 0, sizeof(buffer));
180         if (16 != (rc = i2c_master_recv(c, buffer, 16)))
181                 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
182
183         return ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));
184 }
185
186 static int tea5761_stereo(struct i2c_client *c)
187 {
188         unsigned char buffer[16];
189         int rc;
190         struct tuner *t = i2c_get_clientdata(c);
191
192         memset(buffer, 0, sizeof(buffer));
193         if (16 != (rc = i2c_master_recv(c, buffer, 16)))
194                 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
195
196         rc = buffer[9] & TEA5761_TUNCHECK_STEREO;
197
198         tuner_dbg("TEA5761 radio ST GET = %02x\n", rc);
199
200         return (rc ? V4L2_TUNER_SUB_STEREO : 0);
201 }
202
203 int tea5761_autodetection(struct i2c_client *c)
204 {
205         unsigned char buffer[16];
206         int rc;
207         struct tuner *t = i2c_get_clientdata(c);
208
209         if (16 != (rc = i2c_master_recv(c, buffer, 16))) {
210                 tuner_warn("it is not a TEA5761. Received %i chars.\n", rc);
211                 return EINVAL;
212         }
213
214         if (!((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061))) {
215                 tuner_warn("Manufacturer ID= 0x%02x, Chip ID = %02x%02x. It is not a TEA5761\n",buffer[13],buffer[14],buffer[15]);
216                 return EINVAL;
217         }
218         tuner_warn("TEA5761 detected.\n");
219         return 0;
220 }
221
222 static struct tuner_operations tea5761_tuner_ops = {
223         .set_tv_freq    = set_tv_freq,
224         .set_radio_freq = set_radio_freq,
225         .has_signal     = tea5761_signal,
226         .is_stereo      = tea5761_stereo,
227 };
228
229 int tea5761_tuner_init(struct i2c_client *c)
230 {
231         struct tuner *t = i2c_get_clientdata(c);
232
233         if (tea5761_autodetection(c) == EINVAL)
234                 return EINVAL;
235
236         tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5761HN FM Radio");
237         strlcpy(c->name, "tea5761", sizeof(c->name));
238
239         memcpy(&t->ops, &tea5761_tuner_ops, sizeof(struct tuner_operations));
240
241         return (0);
242 }