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