V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / tea5761.c
blobae105c2cd0ac74609ef8ce83967d2d938bf871b5
1 /*
2 * For Philips TEA5761 FM Chip
3 * I2C address is allways 0x20 (0x10 at 7-bit mode).
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
8 */
10 #include <linux/i2c.h>
11 #include <linux/videodev.h>
12 #include <linux/delay.h>
13 #include <media/tuner.h>
14 #include "tuner-driver.h"
16 #define PREFIX "TEA5761 "
18 /* from tuner-core.c */
19 extern int tuner_debug;
21 /*****************************************************************************/
23 /***************************
24 * TEA5761HN I2C registers *
25 ***************************/
27 /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */
29 /* first byte for reading */
30 #define TEA5761_INTREG_IFFLAG 0x10
31 #define TEA5761_INTREG_LEVFLAG 0x8
32 #define TEA5761_INTREG_FRRFLAG 0x2
33 #define TEA5761_INTREG_BLFLAG 0x1
35 /* second byte for reading / byte for writing */
36 #define TEA5761_INTREG_IFMSK 0x10
37 #define TEA5761_INTREG_LEVMSK 0x8
38 #define TEA5761_INTREG_FRMSK 0x2
39 #define TEA5761_INTREG_BLMSK 0x1
41 /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */
43 /* First byte */
44 #define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */
45 #define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */
47 /* Bits 0-5 for divider MSB */
49 /* Second byte */
50 /* Bits 0-7 for divider LSB */
52 /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */
54 /* first byte */
56 #define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */
57 #define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */
58 #define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */
59 #define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */
60 #define TEA5761_TNCTRL_AFM 0x04
61 #define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */
62 #define TEA5761_TNCTRL_SNC 0x01
64 /* second byte */
66 #define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */
67 #define TEA5761_TNCTRL_SSL_1 0x40
68 #define TEA5761_TNCTRL_SSL_0 0x20
69 #define TEA5761_TNCTRL_HLSI 0x10
70 #define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */
71 #define TEA5761_TNCTRL_SWP 0x04
72 #define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */
73 #define TEA5761_TNCTRL_AHLSI 0x01
75 /* FRQCHECK - Read: bytes 6 and 7 */
76 /* First byte */
78 /* Bits 0-5 for divider MSB */
80 /* Second byte */
81 /* Bits 0-7 for divider LSB */
83 /* TUNCHECK - Read: bytes 8 and 9 */
85 /* First byte */
86 #define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */
87 #define TEA5761_TUNCHECK_TUNTO 0x01
89 /* Second byte */
90 #define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */
91 #define TEA5761_TUNCHECK_LD 0x08
92 #define TEA5761_TUNCHECK_STEREO 0x04
94 /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */
96 /* All zero = no test mode */
98 /* MANID - Read: bytes 12 and 13 */
100 /* First byte - should be 0x10 */
101 #define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */
102 #define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */
104 /* Second byte - Should be 0x2b */
106 #define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */
107 #define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */
109 /* Chip ID - Read: bytes 14 and 15 */
111 /* First byte - should be 0x57 */
113 /* Second byte - should be 0x61 */
115 /*****************************************************************************/
117 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
119 struct tuner *t = i2c_get_clientdata(c);
121 tuner_warn("This tuner doesn't support TV freq.\n");
124 #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */
125 static void tea5761_status_dump(unsigned char *buffer)
127 unsigned int div, frq;
129 div = ((buffer[2] & 0x3f) << 8) | buffer[3];
131 frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */
133 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
134 frq / 1000, frq % 1000, div);
137 /* Freq should be specifyed at 62.5 Hz */
138 static void set_radio_freq(struct i2c_client *c, unsigned int frq)
140 struct tuner *t = i2c_get_clientdata(c);
141 unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };
142 unsigned div;
143 int rc;
145 tuner_dbg (PREFIX "radio freq counter %d\n", frq);
147 if (t->mode == T_STANDBY) {
148 tuner_dbg("TEA5761 set to standby mode\n");
149 buffer[5] |= TEA5761_TNCTRL_MU;
150 } else {
151 buffer[4] |= TEA5761_TNCTRL_PUPD_0;
155 if (t->audmode == V4L2_TUNER_MODE_MONO) {
156 tuner_dbg("TEA5761 set to mono\n");
157 buffer[5] |= TEA5761_TNCTRL_MST;
159 } else {
160 tuner_dbg("TEA5761 set to stereo\n");
163 div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15;
164 buffer[1] = (div >> 8) & 0x3f;
165 buffer[2] = div & 0xff;
167 if (tuner_debug)
168 tea5761_status_dump(buffer);
170 if (7 != (rc = i2c_master_send(c, buffer, 7)))
171 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
174 static int tea5761_signal(struct i2c_client *c)
176 unsigned char buffer[16];
177 int rc;
178 struct tuner *t = i2c_get_clientdata(c);
180 memset(buffer, 0, sizeof(buffer));
181 if (16 != (rc = i2c_master_recv(c, buffer, 16)))
182 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
184 return ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));
187 static int tea5761_stereo(struct i2c_client *c)
189 unsigned char buffer[16];
190 int rc;
191 struct tuner *t = i2c_get_clientdata(c);
193 memset(buffer, 0, sizeof(buffer));
194 if (16 != (rc = i2c_master_recv(c, buffer, 16)))
195 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
197 rc = buffer[9] & TEA5761_TUNCHECK_STEREO;
199 tuner_dbg("TEA5761 radio ST GET = %02x\n", rc);
201 return (rc ? V4L2_TUNER_SUB_STEREO : 0);
204 int tea5761_autodetection(struct i2c_client *c)
206 unsigned char buffer[16];
207 int rc;
208 struct tuner *t = i2c_get_clientdata(c);
210 if (16 != (rc = i2c_master_recv(c, buffer, 16))) {
211 tuner_warn("it is not a TEA5761. Received %i chars.\n", rc);
212 return EINVAL;
215 if (!((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061))) {
216 tuner_warn("Manufacturer ID= 0x%02x, Chip ID = %02x%02x. It is not a TEA5761\n",buffer[13],buffer[14],buffer[15]);
217 return EINVAL;
219 tuner_warn("TEA5761 detected.\n");
220 return 0;
223 static struct tuner_operations tea5761_tuner_ops = {
224 .set_tv_freq = set_tv_freq,
225 .set_radio_freq = set_radio_freq,
226 .has_signal = tea5761_signal,
227 .is_stereo = tea5761_stereo,
230 int tea5761_tuner_init(struct i2c_client *c)
232 struct tuner *t = i2c_get_clientdata(c);
234 if (tea5761_autodetection(c) == EINVAL)
235 return EINVAL;
237 tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5761HN FM Radio");
238 strlcpy(c->name, "tea5761", sizeof(c->name));
240 memcpy(&t->ops, &tea5761_tuner_ops, sizeof(struct tuner_operations));
242 return (0);