V4L/DVB (7466): Avoid minor model number warning when an OEM HVR1250 board is detected
[deliverable/linux.git] / drivers / media / video / tea5767.c
CommitLineData
586b0cab
MCC
1/*
2 * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview
3 * I2C address is allways 0xC0.
4 *
586b0cab 5 *
2e7c6dc3 6 * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@infradead.org)
586b0cab
MCC
7 * This code is placed under the terms of the GNU General Public License
8 *
9 * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa
10 * from their contributions on DScaler.
11 */
12
586b0cab 13#include <linux/i2c.h>
f7ce3cc6 14#include <linux/delay.h>
ffbb807c 15#include <linux/videodev.h>
8d0936ed
MK
16#include "tuner-i2c.h"
17#include "tea5767.h"
586b0cab 18
8d0936ed
MK
19static int debug = 0;
20module_param(debug, int, 0644);
21MODULE_PARM_DESC(debug, "enable verbose debug messages");
586b0cab 22
9dd659de 23#define PREFIX "tea5767"
fac9e899 24
0e1165e8 25/*****************************************************************************/
8d0936ed 26
0e1165e8
MCC
27struct tea5767_priv {
28 struct tuner_i2c_props i2c_props;
29 u32 frequency;
30 struct tea5767_ctrl ctrl;
db8a6956
MK
31};
32
586b0cab
MCC
33/*****************************************************************************/
34
35/******************************
36 * Write mode register values *
37 ******************************/
38
39/* First register */
f7ce3cc6
MCC
40#define TEA5767_MUTE 0x80 /* Mutes output */
41#define TEA5767_SEARCH 0x40 /* Activates station search */
586b0cab
MCC
42/* Bits 0-5 for divider MSB */
43
44/* Second register */
45/* Bits 0-7 for divider LSB */
46
47/* Third register */
48
49/* Station search from botton to up */
50#define TEA5767_SEARCH_UP 0x80
51
52/* Searches with ADC output = 10 */
53#define TEA5767_SRCH_HIGH_LVL 0x60
54
55/* Searches with ADC output = 10 */
56#define TEA5767_SRCH_MID_LVL 0x40
57
58/* Searches with ADC output = 5 */
59#define TEA5767_SRCH_LOW_LVL 0x20
60
61/* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
62#define TEA5767_HIGH_LO_INJECT 0x10
63
64/* Disable stereo */
65#define TEA5767_MONO 0x08
66
67/* Disable right channel and turns to mono */
68#define TEA5767_MUTE_RIGHT 0x04
69
70/* Disable left channel and turns to mono */
71#define TEA5767_MUTE_LEFT 0x02
72
73#define TEA5767_PORT1_HIGH 0x01
74
27487d44 75/* Fourth register */
586b0cab
MCC
76#define TEA5767_PORT2_HIGH 0x80
77/* Chips stops working. Only I2C bus remains on */
78#define TEA5767_STDBY 0x40
79
80/* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
81#define TEA5767_JAPAN_BAND 0x20
82
83/* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
84#define TEA5767_XTAL_32768 0x10
85
86/* Cuts weak signals */
87#define TEA5767_SOFT_MUTE 0x08
88
89/* Activates high cut control */
90#define TEA5767_HIGH_CUT_CTRL 0x04
91
92/* Activates stereo noise control */
93#define TEA5767_ST_NOISE_CTL 0x02
94
95/* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
96#define TEA5767_SRCH_IND 0x01
97
27487d44 98/* Fifth register */
586b0cab
MCC
99
100/* By activating, it will use Xtal at 13 MHz as reference for divider */
101#define TEA5767_PLLREF_ENABLE 0x80
102
103/* By activating, deemphasis=50, or else, deemphasis of 50us */
104#define TEA5767_DEEMPH_75 0X40
105
106/*****************************
107 * Read mode register values *
108 *****************************/
109
110/* First register */
111#define TEA5767_READY_FLAG_MASK 0x80
112#define TEA5767_BAND_LIMIT_MASK 0X40
113/* Bits 0-5 for divider MSB after search or preset */
114
115/* Second register */
116/* Bits 0-7 for divider LSB after search or preset */
117
118/* Third register */
119#define TEA5767_STEREO_MASK 0x80
120#define TEA5767_IF_CNTR_MASK 0x7f
121
27487d44 122/* Fourth register */
586b0cab
MCC
123#define TEA5767_ADC_LEVEL_MASK 0xf0
124
125/* should be 0 */
126#define TEA5767_CHIP_ID_MASK 0x0f
127
27487d44 128/* Fifth register */
586b0cab
MCC
129/* Reserved for future extensions */
130#define TEA5767_RESERVED_MASK 0xff
131
132/*****************************************************************************/
133
0e1165e8
MCC
134static void tea5767_status_dump(struct tea5767_priv *priv,
135 unsigned char *buffer)
586b0cab
MCC
136{
137 unsigned int div, frq;
138
139 if (TEA5767_READY_FLAG_MASK & buffer[0])
140 printk(PREFIX "Ready Flag ON\n");
141 else
142 printk(PREFIX "Ready Flag OFF\n");
143
144 if (TEA5767_BAND_LIMIT_MASK & buffer[0])
145 printk(PREFIX "Tuner at band limit\n");
146 else
147 printk(PREFIX "Tuner not at band limit\n");
148
f7ce3cc6 149 div = ((buffer[0] & 0x3f) << 8) | buffer[1];
586b0cab 150
0e1165e8 151 switch (priv->ctrl.xtal_freq) {
586b0cab 152 case TEA5767_HIGH_LO_13MHz:
c5287ba1 153 frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
586b0cab
MCC
154 break;
155 case TEA5767_LOW_LO_13MHz:
c5287ba1 156 frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */
586b0cab
MCC
157 break;
158 case TEA5767_LOW_LO_32768:
c5287ba1 159 frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */
586b0cab
MCC
160 break;
161 case TEA5767_HIGH_LO_32768:
162 default:
c5287ba1 163 frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */
586b0cab
MCC
164 break;
165 }
f7ce3cc6
MCC
166 buffer[0] = (div >> 8) & 0x3f;
167 buffer[1] = div & 0xff;
586b0cab
MCC
168
169 printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
f7ce3cc6 170 frq / 1000, frq % 1000, div);
586b0cab
MCC
171
172 if (TEA5767_STEREO_MASK & buffer[2])
173 printk(PREFIX "Stereo\n");
174 else
175 printk(PREFIX "Mono\n");
176
f7ce3cc6 177 printk(PREFIX "IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
586b0cab 178
f7ce3cc6
MCC
179 printk(PREFIX "ADC Level = %d\n",
180 (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
586b0cab 181
f7ce3cc6 182 printk(PREFIX "Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
586b0cab 183
f7ce3cc6
MCC
184 printk(PREFIX "Reserved = 0x%02x\n",
185 (buffer[4] & TEA5767_RESERVED_MASK));
586b0cab
MCC
186}
187
188/* Freq should be specifyed at 62.5 Hz */
8d0936ed
MK
189static int set_radio_freq(struct dvb_frontend *fe,
190 struct analog_parameters *params)
586b0cab 191{
8d0936ed
MK
192 struct tea5767_priv *priv = fe->tuner_priv;
193 unsigned int frq = params->frequency;
f7ce3cc6 194 unsigned char buffer[5];
586b0cab
MCC
195 unsigned div;
196 int rc;
197
8d0936ed 198 tuner_dbg("radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
586b0cab 199
0e1165e8 200 buffer[2] = 0;
586b0cab 201
0e1165e8
MCC
202 if (priv->ctrl.port1)
203 buffer[2] |= TEA5767_PORT1_HIGH;
f7ce3cc6 204
8d0936ed 205 if (params->audmode == V4L2_TUNER_MODE_MONO) {
586b0cab
MCC
206 tuner_dbg("TEA5767 set to mono\n");
207 buffer[2] |= TEA5767_MONO;
f7ce3cc6
MCC
208 } else {
209 tuner_dbg("TEA5767 set to stereo\n");
210 }
586b0cab 211
0e1165e8
MCC
212
213 buffer[3] = 0;
214
215 if (priv->ctrl.port2)
216 buffer[3] |= TEA5767_PORT2_HIGH;
217
218 if (priv->ctrl.high_cut)
219 buffer[3] |= TEA5767_HIGH_CUT_CTRL;
220
221 if (priv->ctrl.st_noise)
222 buffer[3] |= TEA5767_ST_NOISE_CTL;
223
224 if (priv->ctrl.soft_mute)
225 buffer[3] |= TEA5767_SOFT_MUTE;
226
227 if (priv->ctrl.japan_band)
228 buffer[3] |= TEA5767_JAPAN_BAND;
229
230 buffer[4] = 0;
231
232 if (priv->ctrl.deemph_75)
233 buffer[4] |= TEA5767_DEEMPH_75;
234
235 if (priv->ctrl.pllref)
236 buffer[4] |= TEA5767_PLLREF_ENABLE;
237
238
239 /* Rounds freq to next decimal value - for 62.5 KHz step */
240 /* frq = 20*(frq/16)+radio_frq[frq%16]; */
241
242 switch (priv->ctrl.xtal_freq) {
586b0cab 243 case TEA5767_HIGH_LO_13MHz:
8d0936ed 244 tuner_dbg("radio HIGH LO inject xtal @ 13 MHz\n");
586b0cab 245 buffer[2] |= TEA5767_HIGH_LO_INJECT;
27487d44 246 div = (frq * (4000 / 16) + 700000 + 225000 + 25000) / 50000;
586b0cab
MCC
247 break;
248 case TEA5767_LOW_LO_13MHz:
8d0936ed 249 tuner_dbg("radio LOW LO inject xtal @ 13 MHz\n");
586b0cab 250
27487d44 251 div = (frq * (4000 / 16) - 700000 - 225000 + 25000) / 50000;
586b0cab
MCC
252 break;
253 case TEA5767_LOW_LO_32768:
8d0936ed 254 tuner_dbg("radio LOW LO inject xtal @ 32,768 MHz\n");
586b0cab
MCC
255 buffer[3] |= TEA5767_XTAL_32768;
256 /* const 700=4000*175 Khz - to adjust freq to right value */
27487d44 257 div = ((frq * (4000 / 16) - 700000 - 225000) + 16384) >> 15;
586b0cab
MCC
258 break;
259 case TEA5767_HIGH_LO_32768:
260 default:
8d0936ed 261 tuner_dbg("radio HIGH LO inject xtal @ 32,768 MHz\n");
586b0cab
MCC
262
263 buffer[2] |= TEA5767_HIGH_LO_INJECT;
264 buffer[3] |= TEA5767_XTAL_32768;
c5287ba1 265 div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
586b0cab
MCC
266 break;
267 }
f7ce3cc6
MCC
268 buffer[0] = (div >> 8) & 0x3f;
269 buffer[1] = div & 0xff;
586b0cab 270
db8a6956 271 if (5 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 5)))
f7ce3cc6 272 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
c5287ba1 273
8d0936ed 274 if (debug) {
db8a6956 275 if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5)))
c5287ba1
MCC
276 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
277 else
0e1165e8 278 tea5767_status_dump(priv, buffer);
c5287ba1 279 }
8d0936ed
MK
280
281 priv->frequency = frq * 125 / 2;
282
283 return 0;
586b0cab
MCC
284}
285
6b897f2c 286static int tea5767_read_status(struct dvb_frontend *fe, char *buffer)
586b0cab 287{
8d0936ed 288 struct tea5767_priv *priv = fe->tuner_priv;
6b897f2c 289 int rc;
586b0cab 290
6b897f2c
MK
291 memset(buffer, 0, 5);
292 if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5))) {
f7ce3cc6 293 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
6b897f2c
MK
294 return -EREMOTEIO;
295 }
586b0cab 296
6b897f2c 297 return 0;
586b0cab
MCC
298}
299
6b897f2c 300static inline int tea5767_signal(struct dvb_frontend *fe, const char *buffer)
586b0cab 301{
8d0936ed 302 struct tea5767_priv *priv = fe->tuner_priv;
586b0cab 303
6b897f2c
MK
304 int signal = ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << 8);
305
306 tuner_dbg("Signal strength: %d\n", signal);
307
308 return signal;
309}
586b0cab 310
6b897f2c
MK
311static inline int tea5767_stereo(struct dvb_frontend *fe, const char *buffer)
312{
313 struct tea5767_priv *priv = fe->tuner_priv;
586b0cab 314
6b897f2c 315 int stereo = buffer[2] & TEA5767_STEREO_MASK;
586b0cab 316
6b897f2c
MK
317 tuner_dbg("Radio ST GET = %02x\n", stereo);
318
319 return (stereo ? V4L2_TUNER_SUB_STEREO : 0);
586b0cab
MCC
320}
321
8d0936ed
MK
322static int tea5767_get_status(struct dvb_frontend *fe, u32 *status)
323{
6b897f2c
MK
324 unsigned char buffer[5];
325
8d0936ed
MK
326 *status = 0;
327
6b897f2c
MK
328 if (0 == tea5767_read_status(fe, buffer)) {
329 if (tea5767_signal(fe, buffer))
330 *status = TUNER_STATUS_LOCKED;
331 if (tea5767_stereo(fe, buffer))
332 *status |= TUNER_STATUS_STEREO;
333 }
334
335 return 0;
336}
337
338static int tea5767_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
339{
340 unsigned char buffer[5];
341
342 *strength = 0;
8d0936ed 343
6b897f2c
MK
344 if (0 == tea5767_read_status(fe, buffer))
345 *strength = tea5767_signal(fe, buffer);
8d0936ed
MK
346
347 return 0;
348}
349
350static int tea5767_standby(struct dvb_frontend *fe)
793cf9e6
MCC
351{
352 unsigned char buffer[5];
8d0936ed 353 struct tea5767_priv *priv = fe->tuner_priv;
793cf9e6
MCC
354 unsigned div, rc;
355
356 div = (87500 * 4 + 700 + 225 + 25) / 50; /* Set frequency to 87.5 MHz */
357 buffer[0] = (div >> 8) & 0x3f;
358 buffer[1] = div & 0xff;
359 buffer[2] = TEA5767_PORT1_HIGH;
360 buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
361 TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND | TEA5767_STDBY;
362 buffer[4] = 0;
363
db8a6956 364 if (5 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 5)))
793cf9e6 365 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
8d0936ed
MK
366
367 return 0;
793cf9e6
MCC
368}
369
8d0936ed 370int tea5767_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)
586b0cab 371{
8d0936ed 372 struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };
fd3113e8 373 unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
586b0cab 374 int rc;
586b0cab 375
8d0936ed
MK
376 if ((rc = tuner_i2c_xfer_recv(&i2c, buffer, 7))< 5) {
377 printk(KERN_WARNING "It is not a TEA5767. Received %i bytes.\n", rc);
586b0cab
MCC
378 return EINVAL;
379 }
380
fd3113e8 381 /* If all bytes are the same then it's a TV tuner and not a tea5767 */
f7ce3cc6
MCC
382 if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
383 buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
8d0936ed 384 printk(KERN_WARNING "All bytes are equal. It is not a TEA5767\n");
586b0cab
MCC
385 return EINVAL;
386 }
387
388 /* Status bytes:
389 * Byte 4: bit 3:1 : CI (Chip Identification) == 0
f7ce3cc6 390 * bit 0 : internally set to 0
586b0cab
MCC
391 * Byte 5: bit 7:0 : == 0
392 */
793cf9e6 393 if (((buffer[3] & 0x0f) != 0x00) || (buffer[4] != 0x00)) {
8d0936ed 394 printk(KERN_WARNING "Chip ID is not zero. It is not a TEA5767\n");
586b0cab
MCC
395 return EINVAL;
396 }
c5287ba1 397
001abc93
MCC
398 /* It seems that tea5767 returns 0xff after the 5th byte */
399 if ((buffer[5] != 0xff) || (buffer[6] != 0xff)) {
8d0936ed 400 printk(KERN_WARNING "Returned more than 5 bytes. It is not a TEA5767\n");
001abc93
MCC
401 return EINVAL;
402 }
403
586b0cab
MCC
404 return 0;
405}
406
8d0936ed 407static int tea5767_release(struct dvb_frontend *fe)
db8a6956 408{
8d0936ed
MK
409 kfree(fe->tuner_priv);
410 fe->tuner_priv = NULL;
411
412 return 0;
db8a6956
MK
413}
414
8d0936ed
MK
415static int tea5767_get_frequency(struct dvb_frontend *fe, u32 *frequency)
416{
417 struct tea5767_priv *priv = fe->tuner_priv;
418 *frequency = priv->frequency;
0e1165e8
MCC
419
420 return 0;
421}
422
423static int tea5767_set_config (struct dvb_frontend *fe, void *priv_cfg)
424{
425 struct tea5767_priv *priv = fe->tuner_priv;
426
427 memcpy(&priv->ctrl, priv_cfg, sizeof(priv->ctrl));
428
8d0936ed
MK
429 return 0;
430}
431
432static struct dvb_tuner_ops tea5767_tuner_ops = {
433 .info = {
434 .name = "tea5767", // Philips TEA5767HN FM Radio
435 },
436
437 .set_analog_params = set_radio_freq,
0e1165e8 438 .set_config = tea5767_set_config,
8d0936ed
MK
439 .sleep = tea5767_standby,
440 .release = tea5767_release,
441 .get_frequency = tea5767_get_frequency,
442 .get_status = tea5767_get_status,
6b897f2c 443 .get_rf_strength = tea5767_get_rf_strength,
0f838f8d
MK
444};
445
8d0936ed
MK
446struct dvb_frontend *tea5767_attach(struct dvb_frontend *fe,
447 struct i2c_adapter* i2c_adap,
448 u8 i2c_addr)
586b0cab 449{
db8a6956
MK
450 struct tea5767_priv *priv = NULL;
451
452 priv = kzalloc(sizeof(struct tea5767_priv), GFP_KERNEL);
453 if (priv == NULL)
8d0936ed
MK
454 return NULL;
455 fe->tuner_priv = priv;
db8a6956 456
0e1165e8
MCC
457 priv->i2c_props.addr = i2c_addr;
458 priv->i2c_props.adap = i2c_adap;
459 priv->ctrl.xtal_freq = TEA5767_HIGH_LO_32768;
460 priv->ctrl.port1 = 1;
461 priv->ctrl.port2 = 1;
462 priv->ctrl.high_cut = 1;
463 priv->ctrl.st_noise = 1;
464 priv->ctrl.japan_band = 1;
586b0cab 465
8d0936ed
MK
466 memcpy(&fe->ops.tuner_ops, &tea5767_tuner_ops,
467 sizeof(struct dvb_tuner_ops));
586b0cab 468
8d0936ed 469 tuner_info("type set to %s\n", "Philips TEA5767HN FM Radio");
586b0cab 470
8d0936ed 471 return fe;
586b0cab 472}
8d0936ed 473
8d0936ed
MK
474EXPORT_SYMBOL_GPL(tea5767_attach);
475EXPORT_SYMBOL_GPL(tea5767_autodetection);
476
477MODULE_DESCRIPTION("Philips TEA5767 FM tuner driver");
478MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
479MODULE_LICENSE("GPL");
This page took 0.415384 seconds and 5 git commands to generate.