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