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