1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/*
* driver/media/radio/radio-tea5764.c
*
* Driver for TEA5764 radio chip for linux 2.6.
* This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola.
* The I2C protocol is used for communicate with chip.
*
* Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
*
* Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* History:
* 2008-12-06 Fabio Belavenuto <belavenuto@gmail.com>
* initial code
*
* TODO:
* add platform_data support for IRQs platform dependencies
* add RDS support
*/
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h> /* Initdata */
#include <linux/videodev2.h> /* kernel radio structs */
#include <linux/i2c.h> /* I2C */
#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-event.h>
#define DRIVER_VERSION "0.0.2"
#define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
#define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
#define PINFO(format, ...)\
printk(KERN_INFO KBUILD_MODNAME ": "\
DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
#define PWARN(format, ...)\
printk(KERN_WARNING KBUILD_MODNAME ": "\
DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
# define PDEBUG(format, ...)\
printk(KERN_DEBUG KBUILD_MODNAME ": "\
DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
/* Frequency limits in MHz -- these are European values. For Japanese
devices, that would be 76000 and 91000. */
#define FREQ_MIN 87500U
#define FREQ_MAX 108000U
#define FREQ_MUL 16
/* TEA5764 registers */
#define TEA5764_MANID 0x002b
#define TEA5764_CHIPID 0x5764
#define TEA5764_INTREG_BLMSK 0x0001
#define TEA5764_INTREG_FRRMSK 0x0002
#define TEA5764_INTREG_LEVMSK 0x0008
#define TEA5764_INTREG_IFMSK 0x0010
#define TEA5764_INTREG_BLMFLAG 0x0100
#define TEA5764_INTREG_FRRFLAG 0x0200
#define TEA5764_INTREG_LEVFLAG 0x0800
#define TEA5764_INTREG_IFFLAG 0x1000
#define TEA5764_FRQSET_SUD 0x8000
#define TEA5764_FRQSET_SM 0x4000
#define TEA5764_TNCTRL_PUPD1 0x8000
#define TEA5764_TNCTRL_PUPD0 0x4000
#define TEA5764_TNCTRL_BLIM 0x2000
#define TEA5764_TNCTRL_SWPM 0x1000
#define TEA5764_TNCTRL_IFCTC 0x0800
#define TEA5764_TNCTRL_AFM 0x0400
#define TEA5764_TNCTRL_SMUTE 0x0200
#define TEA5764_TNCTRL_SNC 0x0100
#define TEA5764_TNCTRL_MU 0x0080
#define TEA5764_TNCTRL_SSL1 0x0040
#define TEA5764_TNCTRL_SSL0 0x0020
#define TEA5764_TNCTRL_HLSI 0x0010
#define TEA5764_TNCTRL_MST 0x0008
#define TEA5764_TNCTRL_SWP 0x0004
#define TEA5764_TNCTRL_DTC 0x0002
#define TEA5764_TNCTRL_AHLSI 0x0001
#define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
#define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
#define TEA5764_TUNCHK_TUNTO 0x0100
#define TEA5764_TUNCHK_LD 0x0008
#define TEA5764_TUNCHK_STEREO 0x0004
#define TEA5764_TESTREG_TRIGFR 0x0800
struct tea5764_regs {
u16 intreg; /* INTFLAG & INTMSK */
u16 frqset; /* FRQSETMSB & FRQSETLSB */
u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */
u16 tunchk; /* IFCHK & LEVCHK */
u16 testreg; /* TESTBITS & TESTMODE */
u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */
u16 rdslb; /* RDSLBMSB & RDSLBLSB */
u16 rdspb; /* RDSPBMSB & RDSPBLSB */
u16 rdsbc; /* RDSBBC & RDSGBC */
u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
u16 rdsbbl; /* PAUSEDET & RDSBBL */
u16 manid; /* MANID1 & MANID2 */
u16 chipid; /* CHIPID1 & CHIPID2 */
} __attribute__ ((packed));
struct tea5764_write_regs {
u8 intreg; /* INTMSK */
__be16 frqset; /* FRQSETMSB & FRQSETLSB */
__be16 tnctrl; /* TNCTRL1 & TNCTRL2 */
__be16 testreg; /* TESTBITS & TESTMODE */
__be16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
__be16 rdsbbl; /* PAUSEDET & RDSBBL */
} __attribute__ ((packed));
#ifdef CONFIG_RADIO_TEA5764_XTAL
#define RADIO_TEA5764_XTAL 1
#else
#define RADIO_TEA5764_XTAL 0
#endif
static int radio_nr = -1;
static int use_xtal = RADIO_TEA5764_XTAL;
struct tea5764_device {
struct v4l2_device v4l2_dev;
struct v4l2_ctrl_handler ctrl_handler;
struct i2c_client *i2c_client;
struct video_device vdev;
struct tea5764_regs regs;
struct mutex mutex;
};
/* I2C code related */
static int tea5764_i2c_read(struct tea5764_device *radio)
{
int i;
u16 *p = (u16 *) &radio->regs;
struct i2c_msg msgs[1] = {
{ .addr = radio->i2c_client->addr,
.flags = I2C_M_RD,
.len = sizeof(radio->regs),
.buf = (void *)&radio->regs
},
};
if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
return -EIO;
for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
p[i] = __be16_to_cpu((__force __be16)p[i]);
return 0;
}
static int tea5764_i2c_write(struct tea5764_device *radio)
{
struct tea5764_write_regs wr;
struct tea5764_regs *r = &radio->regs;
struct i2c_msg msgs[1] = {
{
.addr = radio->i2c_client->addr,
.len = sizeof(wr),
.buf = (void *)&wr
},
};
wr.intreg = r->intreg & 0xff;
wr.frqset = __cpu_to_be16(r->frqset);
wr.tnctrl = __cpu_to_be16(r->tnctrl);
wr.testreg = __cpu_to_be16(r->testreg);
wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
return -EIO;
return 0;
}
static void tea5764_power_up(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
TEA5764_TNCTRL_HLSI);
if (!use_xtal)
r->testreg |= TEA5764_TESTREG_TRIGFR;
else
r->testreg &= ~TEA5764_TESTREG_TRIGFR;
r->tnctrl |= TEA5764_TNCTRL_PUPD0;
tea5764_i2c_write(radio);
}
}
static void tea5764_power_down(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
tea5764_i2c_write(radio);
}
}
static void tea5764_set_freq(struct tea5764_device *radio, int freq)
{
struct tea5764_regs *r = &radio->regs;
/* formula: (freq [+ or -] 225000) / 8192 */
if (r->tnctrl & TEA5764_TNCTRL_HLSI)
r->frqset = (freq + 225000) / 8192;
else
r->frqset = (freq - 225000) / 8192;
}
static int tea5764_get_freq(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (r->tnctrl & TEA5764_TNCTRL_HLSI)
return (r->frqchk * 8192) - 225000;
else
return (r->frqchk * 8192) + 225000;
}
/* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
static void tea5764_tune(struct tea5764_device *radio, int freq)
{
tea5764_set_freq(radio, freq);
if (tea5764_i2c_write(radio))
PWARN("Could not set frequency!");
}
static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
{
struct tea5764_regs *r = &radio->regs;
int tnctrl = r->tnctrl;
if (audmode == V4L2_TUNER_MODE_MONO)
r->tnctrl |= TEA5764_TNCTRL_MST;
else
r->tnctrl &= ~TEA5764_TNCTRL_MST;
if (tnctrl != r->tnctrl)
tea5764_i2c_write(radio);
}
static int tea5764_get_audout_mode(struct tea5764_device *radio)
{
struct tea5764_regs *r = &radio->regs;
if (r->tnctrl & TEA5764_TNCTRL_MST)
return V4L2_TUNER_MODE_MONO;
else
return V4L2_TUNER_MODE_STEREO;
}
static void tea5764_mute(struct tea5764_device *radio, int on)
{
struct tea5764_regs
|