blob: 9965ba48cffaa293d45b5f8109eda108a2f7f694 [file] [log] [blame]
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -03001/*
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>
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -030011#include <linux/delay.h>
Michael Krufkyffbb8072007-08-21 01:14:12 -030012#include <linux/videodev.h>
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -030013#include <media/tuner.h>
Michael Krufky8218b0b2007-06-26 13:12:08 -030014#include "tuner-driver.h"
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -030015
16#define PREFIX "TEA5761 "
17
18/* from tuner-core.c */
19extern int tuner_debug;
20
Michael Krufkydb8a6952007-08-21 01:24:42 -030021struct tea5761_priv {
22 struct tuner_i2c_props i2c_props;
23};
24
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -030025/*****************************************************************************/
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
Michael Krufkydb8a6952007-08-21 01:24:42 -0300121static void set_tv_freq(struct tuner *t, unsigned int freq)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300122{
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300123 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 */
127static 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 */
Michael Krufkydb8a6952007-08-21 01:24:42 -0300140static void set_radio_freq(struct tuner *t, unsigned int frq)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300141{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300142 struct tea5761_priv *priv = t->priv;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300143 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
Michael Krufkydb8a6952007-08-21 01:24:42 -0300172 if (7 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 7)))
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300173 tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
174}
175
Michael Krufkydb8a6952007-08-21 01:24:42 -0300176static int tea5761_signal(struct tuner *t)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300177{
178 unsigned char buffer[16];
179 int rc;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300180 struct tea5761_priv *priv = t->priv;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300181
182 memset(buffer, 0, sizeof(buffer));
Michael Krufkydb8a6952007-08-21 01:24:42 -0300183 if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16)))
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300184 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
Michael Krufkydb8a6952007-08-21 01:24:42 -0300189static int tea5761_stereo(struct tuner *t)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300190{
191 unsigned char buffer[16];
192 int rc;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300193 struct tea5761_priv *priv = t->priv;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300194
195 memset(buffer, 0, sizeof(buffer));
Michael Krufkydb8a6952007-08-21 01:24:42 -0300196 if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16)))
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300197 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
Michael Krufkydb8a6952007-08-21 01:24:42 -0300206int tea5761_autodetection(struct tuner *t)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300207{
208 unsigned char buffer[16];
209 int rc;
Michael Krufkydb8a6952007-08-21 01:24:42 -0300210 struct tuner_i2c_props i2c = { .adap = t->i2c.adapter, .addr = t->i2c.addr };
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300211
Michael Krufkydb8a6952007-08-21 01:24:42 -0300212 if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) {
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300213 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
Michael Krufkydb8a6952007-08-21 01:24:42 -0300225static void tea5761_release(struct tuner *t)
226{
227 kfree(t->priv);
228 t->priv = NULL;
229}
230
Michael Krufkye407cd52007-06-06 16:16:29 -0300231static 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,
Michael Krufkydb8a6952007-08-21 01:24:42 -0300236 .release = tea5761_release,
Michael Krufkye407cd52007-06-06 16:16:29 -0300237};
238
Michael Krufkydb8a6952007-08-21 01:24:42 -0300239int tea5761_tuner_init(struct tuner *t)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300240{
Michael Krufkydb8a6952007-08-21 01:24:42 -0300241 struct tea5761_priv *priv = NULL;
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300242
Michael Krufkydb8a6952007-08-21 01:24:42 -0300243 if (tea5761_autodetection(t) == EINVAL)
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300244 return EINVAL;
245
Michael Krufkydb8a6952007-08-21 01:24:42 -0300246 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
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300254 tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5761HN FM Radio");
Michael Krufkydb8a6952007-08-21 01:24:42 -0300255 strlcpy(t->i2c.name, "tea5761", sizeof(t->i2c.name));
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300256
Michael Krufkye407cd52007-06-06 16:16:29 -0300257 memcpy(&t->ops, &tea5761_tuner_ops, sizeof(struct tuner_operations));
Mauro Carvalho Chehab8573a9e2007-04-08 01:09:11 -0300258
259 return (0);
260}