include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[linux-block.git] / drivers / media / common / tuners / tea5761.c
CommitLineData
8573a9e6
MCC
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>
5a0e3ad6 11#include <linux/slab.h>
8573a9e6 12#include <linux/delay.h>
7f6adeaf 13#include <linux/videodev2.h>
8573a9e6 14#include <media/tuner.h>
7ab10bf7
MK
15#include "tuner-i2c.h"
16#include "tea5761.h"
8573a9e6 17
ff699e6b 18static int debug;
7ab10bf7
MK
19module_param(debug, int, 0644);
20MODULE_PARM_DESC(debug, "enable verbose debug messages");
8573a9e6 21
db8a6956
MK
22struct tea5761_priv {
23 struct tuner_i2c_props i2c_props;
7ab10bf7
MK
24
25 u32 frequency;
db8a6956
MK
26};
27
8573a9e6
MCC
28/*****************************************************************************/
29
30/***************************
31 * TEA5761HN I2C registers *
32 ***************************/
33
34/* INTREG - Read: bytes 0 and 1 / Write: byte 0 */
35
36 /* first byte for reading */
37#define TEA5761_INTREG_IFFLAG 0x10
38#define TEA5761_INTREG_LEVFLAG 0x8
39#define TEA5761_INTREG_FRRFLAG 0x2
40#define TEA5761_INTREG_BLFLAG 0x1
41
42 /* second byte for reading / byte for writing */
43#define TEA5761_INTREG_IFMSK 0x10
44#define TEA5761_INTREG_LEVMSK 0x8
45#define TEA5761_INTREG_FRMSK 0x2
46#define TEA5761_INTREG_BLMSK 0x1
47
48/* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */
49
50 /* First byte */
51#define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */
52#define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */
53
54 /* Bits 0-5 for divider MSB */
55
56 /* Second byte */
57 /* Bits 0-7 for divider LSB */
58
59/* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */
60
61 /* first byte */
62
63#define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */
64#define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */
65#define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */
66#define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */
67#define TEA5761_TNCTRL_AFM 0x04
68#define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */
69#define TEA5761_TNCTRL_SNC 0x01
70
71 /* second byte */
72
73#define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */
74#define TEA5761_TNCTRL_SSL_1 0x40
75#define TEA5761_TNCTRL_SSL_0 0x20
76#define TEA5761_TNCTRL_HLSI 0x10
77#define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */
78#define TEA5761_TNCTRL_SWP 0x04
79#define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */
80#define TEA5761_TNCTRL_AHLSI 0x01
81
82/* FRQCHECK - Read: bytes 6 and 7 */
83 /* First byte */
84
85 /* Bits 0-5 for divider MSB */
86
87 /* Second byte */
88 /* Bits 0-7 for divider LSB */
89
90/* TUNCHECK - Read: bytes 8 and 9 */
91
92 /* First byte */
93#define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */
94#define TEA5761_TUNCHECK_TUNTO 0x01
95
96 /* Second byte */
97#define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */
98#define TEA5761_TUNCHECK_LD 0x08
99#define TEA5761_TUNCHECK_STEREO 0x04
100
101/* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */
102
103 /* All zero = no test mode */
104
105/* MANID - Read: bytes 12 and 13 */
106
107 /* First byte - should be 0x10 */
108#define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */
109#define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */
110
111 /* Second byte - Should be 0x2b */
112
113#define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */
114#define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */
115
116/* Chip ID - Read: bytes 14 and 15 */
117
118 /* First byte - should be 0x57 */
119
120 /* Second byte - should be 0x61 */
121
122/*****************************************************************************/
123
8573a9e6
MCC
124#define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */
125static void tea5761_status_dump(unsigned char *buffer)
126{
127 unsigned int div, frq;
128
129 div = ((buffer[2] & 0x3f) << 8) | buffer[3];
130
131 frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */
132
2756665c 133 printk(KERN_INFO "tea5761: Frequency %d.%03d KHz (divider = 0x%04x)\n",
8573a9e6
MCC
134 frq / 1000, frq % 1000, div);
135}
136
137/* Freq should be specifyed at 62.5 Hz */
7ab10bf7
MK
138static int set_radio_freq(struct dvb_frontend *fe,
139 struct analog_parameters *params)
8573a9e6 140{
7ab10bf7
MK
141 struct tea5761_priv *priv = fe->tuner_priv;
142 unsigned int frq = params->frequency;
8573a9e6
MCC
143 unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };
144 unsigned div;
145 int rc;
146
7ab10bf7 147 tuner_dbg("radio freq counter %d\n", frq);
8573a9e6 148
7ab10bf7 149 if (params->mode == T_STANDBY) {
8573a9e6
MCC
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
7ab10bf7 157 if (params->audmode == V4L2_TUNER_MODE_MONO) {
8573a9e6
MCC
158 tuner_dbg("TEA5761 set to mono\n");
159 buffer[5] |= TEA5761_TNCTRL_MST;
8573a9e6
MCC
160 } else {
161 tuner_dbg("TEA5761 set to stereo\n");
162 }
163
164 div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15;
165 buffer[1] = (div >> 8) & 0x3f;
166 buffer[2] = div & 0xff;
167
7ab10bf7 168 if (debug)
8573a9e6
MCC
169 tea5761_status_dump(buffer);
170
db8a6956 171 if (7 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 7)))
8573a9e6 172 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
7ab10bf7
MK
173
174 priv->frequency = frq * 125 / 2;
175
176 return 0;
8573a9e6
MCC
177}
178
fd443f74 179static int tea5761_read_status(struct dvb_frontend *fe, char *buffer)
8573a9e6 180{
7ab10bf7 181 struct tea5761_priv *priv = fe->tuner_priv;
fd443f74 182 int rc;
8573a9e6 183
fd443f74
MK
184 memset(buffer, 0, 16);
185 if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16))) {
186 tuner_warn("i2c i/o error: rc == %d (should be 16)\n", rc);
187 return -EREMOTEIO;
188 }
8573a9e6 189
fd443f74 190 return 0;
8573a9e6
MCC
191}
192
fd443f74 193static inline int tea5761_signal(struct dvb_frontend *fe, const char *buffer)
8573a9e6 194{
7ab10bf7 195 struct tea5761_priv *priv = fe->tuner_priv;
8573a9e6 196
fd443f74
MK
197 int signal = ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));
198
199 tuner_dbg("Signal strength: %d\n", signal);
8573a9e6 200
fd443f74
MK
201 return signal;
202}
8573a9e6 203
fd443f74
MK
204static inline int tea5761_stereo(struct dvb_frontend *fe, const char *buffer)
205{
206 struct tea5761_priv *priv = fe->tuner_priv;
8573a9e6 207
fd443f74
MK
208 int stereo = buffer[9] & TEA5761_TUNCHECK_STEREO;
209
210 tuner_dbg("Radio ST GET = %02x\n", stereo);
211
212 return (stereo ? V4L2_TUNER_SUB_STEREO : 0);
8573a9e6
MCC
213}
214
7ab10bf7
MK
215static int tea5761_get_status(struct dvb_frontend *fe, u32 *status)
216{
fd443f74 217 unsigned char buffer[16];
7ab10bf7
MK
218
219 *status = 0;
220
fd443f74
MK
221 if (0 == tea5761_read_status(fe, buffer)) {
222 if (tea5761_signal(fe, buffer))
223 *status = TUNER_STATUS_LOCKED;
224 if (tea5761_stereo(fe, buffer))
225 *status |= TUNER_STATUS_STEREO;
226 }
227
228 return 0;
229}
230
231static int tea5761_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
232{
233 unsigned char buffer[16];
234
235 *strength = 0;
7ab10bf7 236
fd443f74
MK
237 if (0 == tea5761_read_status(fe, buffer))
238 *strength = tea5761_signal(fe, buffer);
7ab10bf7
MK
239
240 return 0;
241}
242
243int tea5761_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)
8573a9e6
MCC
244{
245 unsigned char buffer[16];
246 int rc;
7ab10bf7 247 struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };
8573a9e6 248
db8a6956 249 if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) {
7ab10bf7 250 printk(KERN_WARNING "it is not a TEA5761. Received %i chars.\n", rc);
867e835f 251 return -EINVAL;
8573a9e6
MCC
252 }
253
867e835f
MCC
254 if ((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061)) {
255 printk(KERN_WARNING "Manufacturer ID= 0x%02x, Chip ID = %02x%02x."
256 " It is not a TEA5761\n",
257 buffer[13], buffer[14], buffer[15]);
258 return -EINVAL;
8573a9e6 259 }
867e835f
MCC
260 printk(KERN_WARNING "tea5761: TEA%02x%02x detected. "
261 "Manufacturer ID= 0x%02x\n",
262 buffer[14], buffer[15], buffer[13]);
263
7ab10bf7
MK
264 return 0;
265}
266
267static int tea5761_release(struct dvb_frontend *fe)
268{
269 kfree(fe->tuner_priv);
270 fe->tuner_priv = NULL;
271
8573a9e6
MCC
272 return 0;
273}
274
7ab10bf7 275static int tea5761_get_frequency(struct dvb_frontend *fe, u32 *frequency)
db8a6956 276{
7ab10bf7
MK
277 struct tea5761_priv *priv = fe->tuner_priv;
278 *frequency = priv->frequency;
279 return 0;
db8a6956
MK
280}
281
7ab10bf7
MK
282static struct dvb_tuner_ops tea5761_tuner_ops = {
283 .info = {
284 .name = "tea5761", // Philips TEA5761HN FM Radio
285 },
286 .set_analog_params = set_radio_freq,
287 .release = tea5761_release,
288 .get_frequency = tea5761_get_frequency,
289 .get_status = tea5761_get_status,
fd443f74 290 .get_rf_strength = tea5761_get_rf_strength,
e407cd54
MK
291};
292
7ab10bf7
MK
293struct dvb_frontend *tea5761_attach(struct dvb_frontend *fe,
294 struct i2c_adapter* i2c_adap,
295 u8 i2c_addr)
8573a9e6 296{
db8a6956 297 struct tea5761_priv *priv = NULL;
8573a9e6 298
4277106b 299 if (tea5761_autodetection(i2c_adap, i2c_addr) != 0)
7ab10bf7 300 return NULL;
8573a9e6 301
db8a6956
MK
302 priv = kzalloc(sizeof(struct tea5761_priv), GFP_KERNEL);
303 if (priv == NULL)
7ab10bf7
MK
304 return NULL;
305 fe->tuner_priv = priv;
db8a6956 306
7ab10bf7
MK
307 priv->i2c_props.addr = i2c_addr;
308 priv->i2c_props.adap = i2c_adap;
2756665c 309 priv->i2c_props.name = "tea5761";
db8a6956 310
7ab10bf7
MK
311 memcpy(&fe->ops.tuner_ops, &tea5761_tuner_ops,
312 sizeof(struct dvb_tuner_ops));
8573a9e6 313
7ab10bf7 314 tuner_info("type set to %s\n", "Philips TEA5761HN FM Radio");
8573a9e6 315
7ab10bf7 316 return fe;
8573a9e6 317}
7ab10bf7
MK
318
319
320EXPORT_SYMBOL_GPL(tea5761_attach);
321EXPORT_SYMBOL_GPL(tea5761_autodetection);
322
323MODULE_DESCRIPTION("Philips TEA5761 FM tuner driver");
324MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
325MODULE_LICENSE("GPL");