blob: f6a5471e51aa613ba2bfaa19103ad9d121669c5d [file] [log] [blame]
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -03001/*
2 * driver/media/radio/radio-tea5764.c
3 *
4 * Driver for TEA5764 radio chip for linux 2.6.
5 * This driver is for TEA5764 chip from NXP, used in EZX phones from Motorola.
6 * The I2C protocol is used for communicate with chip.
7 *
8 * Based in radio-tea5761.c Copyright (C) 2005 Nokia Corporation
9 *
10 * Copyright (c) 2008 Fabio Belavenuto <belavenuto@gmail.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * History:
27 * 2008-12-06 Fabio Belavenuto <belavenuto@gmail.com>
28 * initial code
29 *
30 * TODO:
31 * add platform_data support for IRQs platform dependencies
32 * add RDS support
33 */
34#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030036#include <linux/module.h>
37#include <linux/init.h> /* Initdata */
38#include <linux/videodev2.h> /* kernel radio structs */
39#include <linux/i2c.h> /* I2C */
40#include <media/v4l2-common.h>
41#include <media/v4l2-ioctl.h>
Hans Verkuil099f88e2013-02-03 08:25:32 -030042#include <media/v4l2-device.h>
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030043
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030044#define DRIVER_VERSION "0.0.2"
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030045
46#define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
47#define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
48
49#define PINFO(format, ...)\
50 printk(KERN_INFO KBUILD_MODNAME ": "\
51 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
52#define PWARN(format, ...)\
53 printk(KERN_WARNING KBUILD_MODNAME ": "\
54 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
55# define PDEBUG(format, ...)\
56 printk(KERN_DEBUG KBUILD_MODNAME ": "\
57 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
58
59/* Frequency limits in MHz -- these are European values. For Japanese
60devices, that would be 76000 and 91000. */
61#define FREQ_MIN 87500
62#define FREQ_MAX 108000
63#define FREQ_MUL 16
64
65/* TEA5764 registers */
66#define TEA5764_MANID 0x002b
67#define TEA5764_CHIPID 0x5764
68
69#define TEA5764_INTREG_BLMSK 0x0001
70#define TEA5764_INTREG_FRRMSK 0x0002
71#define TEA5764_INTREG_LEVMSK 0x0008
72#define TEA5764_INTREG_IFMSK 0x0010
73#define TEA5764_INTREG_BLMFLAG 0x0100
74#define TEA5764_INTREG_FRRFLAG 0x0200
75#define TEA5764_INTREG_LEVFLAG 0x0800
76#define TEA5764_INTREG_IFFLAG 0x1000
77
78#define TEA5764_FRQSET_SUD 0x8000
79#define TEA5764_FRQSET_SM 0x4000
80
81#define TEA5764_TNCTRL_PUPD1 0x8000
82#define TEA5764_TNCTRL_PUPD0 0x4000
83#define TEA5764_TNCTRL_BLIM 0x2000
84#define TEA5764_TNCTRL_SWPM 0x1000
85#define TEA5764_TNCTRL_IFCTC 0x0800
86#define TEA5764_TNCTRL_AFM 0x0400
87#define TEA5764_TNCTRL_SMUTE 0x0200
88#define TEA5764_TNCTRL_SNC 0x0100
89#define TEA5764_TNCTRL_MU 0x0080
90#define TEA5764_TNCTRL_SSL1 0x0040
91#define TEA5764_TNCTRL_SSL0 0x0020
92#define TEA5764_TNCTRL_HLSI 0x0010
93#define TEA5764_TNCTRL_MST 0x0008
94#define TEA5764_TNCTRL_SWP 0x0004
95#define TEA5764_TNCTRL_DTC 0x0002
96#define TEA5764_TNCTRL_AHLSI 0x0001
97
98#define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
99#define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
100#define TEA5764_TUNCHK_TUNTO 0x0100
101#define TEA5764_TUNCHK_LD 0x0008
102#define TEA5764_TUNCHK_STEREO 0x0004
103
104#define TEA5764_TESTREG_TRIGFR 0x0800
105
106struct tea5764_regs {
107 u16 intreg; /* INTFLAG & INTMSK */
108 u16 frqset; /* FRQSETMSB & FRQSETLSB */
109 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
110 u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */
111 u16 tunchk; /* IFCHK & LEVCHK */
112 u16 testreg; /* TESTBITS & TESTMODE */
113 u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */
114 u16 rdslb; /* RDSLBMSB & RDSLBLSB */
115 u16 rdspb; /* RDSPBMSB & RDSPBLSB */
116 u16 rdsbc; /* RDSBBC & RDSGBC */
117 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
118 u16 rdsbbl; /* PAUSEDET & RDSBBL */
119 u16 manid; /* MANID1 & MANID2 */
120 u16 chipid; /* CHIPID1 & CHIPID2 */
121} __attribute__ ((packed));
122
123struct tea5764_write_regs {
124 u8 intreg; /* INTMSK */
125 u16 frqset; /* FRQSETMSB & FRQSETLSB */
126 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
127 u16 testreg; /* TESTBITS & TESTMODE */
128 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
129 u16 rdsbbl; /* PAUSEDET & RDSBBL */
130} __attribute__ ((packed));
131
Paul Bolle8c4343e2011-10-12 17:51:22 -0300132#ifdef CONFIG_RADIO_TEA5764_XTAL
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300133#define RADIO_TEA5764_XTAL 1
Paul Bolle8c4343e2011-10-12 17:51:22 -0300134#else
135#define RADIO_TEA5764_XTAL 0
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300136#endif
137
138static int radio_nr = -1;
139static int use_xtal = RADIO_TEA5764_XTAL;
140
141struct tea5764_device {
Hans Verkuil099f88e2013-02-03 08:25:32 -0300142 struct v4l2_device v4l2_dev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300143 struct i2c_client *i2c_client;
144 struct video_device *videodev;
145 struct tea5764_regs regs;
146 struct mutex mutex;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300147};
148
149/* I2C code related */
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -0200150static int tea5764_i2c_read(struct tea5764_device *radio)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300151{
152 int i;
153 u16 *p = (u16 *) &radio->regs;
154
155 struct i2c_msg msgs[1] = {
Shubhrajyoti D66ba9592012-09-18 08:22:33 -0300156 { .addr = radio->i2c_client->addr,
157 .flags = I2C_M_RD,
158 .len = sizeof(radio->regs),
159 .buf = (void *)&radio->regs
160 },
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300161 };
162 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
163 return -EIO;
164 for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
165 p[i] = __be16_to_cpu(p[i]);
166
167 return 0;
168}
169
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -0200170static int tea5764_i2c_write(struct tea5764_device *radio)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300171{
172 struct tea5764_write_regs wr;
173 struct tea5764_regs *r = &radio->regs;
174 struct i2c_msg msgs[1] = {
Shubhrajyoti D66ba9592012-09-18 08:22:33 -0300175 {
176 .addr = radio->i2c_client->addr,
177 .len = sizeof(wr),
178 .buf = (void *)&wr
179 },
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300180 };
181 wr.intreg = r->intreg & 0xff;
182 wr.frqset = __cpu_to_be16(r->frqset);
183 wr.tnctrl = __cpu_to_be16(r->tnctrl);
184 wr.testreg = __cpu_to_be16(r->testreg);
185 wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
186 wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
187 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
188 return -EIO;
189 return 0;
190}
191
192/* V4L2 code related */
193static struct v4l2_queryctrl radio_qctrl[] = {
194 {
195 .id = V4L2_CID_AUDIO_MUTE,
196 .name = "Mute",
197 .minimum = 0,
198 .maximum = 1,
199 .default_value = 1,
200 .type = V4L2_CTRL_TYPE_BOOLEAN,
201 }
202};
203
204static void tea5764_power_up(struct tea5764_device *radio)
205{
206 struct tea5764_regs *r = &radio->regs;
207
208 if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
209 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
210 TEA5764_TNCTRL_HLSI);
211 if (!use_xtal)
212 r->testreg |= TEA5764_TESTREG_TRIGFR;
213 else
214 r->testreg &= ~TEA5764_TESTREG_TRIGFR;
215
216 r->tnctrl |= TEA5764_TNCTRL_PUPD0;
217 tea5764_i2c_write(radio);
218 }
219}
220
221static void tea5764_power_down(struct tea5764_device *radio)
222{
223 struct tea5764_regs *r = &radio->regs;
224
225 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
226 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
227 tea5764_i2c_write(radio);
228 }
229}
230
231static void tea5764_set_freq(struct tea5764_device *radio, int freq)
232{
233 struct tea5764_regs *r = &radio->regs;
234
235 /* formula: (freq [+ or -] 225000) / 8192 */
236 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
237 r->frqset = (freq + 225000) / 8192;
238 else
239 r->frqset = (freq - 225000) / 8192;
240}
241
242static int tea5764_get_freq(struct tea5764_device *radio)
243{
244 struct tea5764_regs *r = &radio->regs;
245
246 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
247 return (r->frqchk * 8192) - 225000;
248 else
249 return (r->frqchk * 8192) + 225000;
250}
251
252/* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
253static void tea5764_tune(struct tea5764_device *radio, int freq)
254{
255 tea5764_set_freq(radio, freq);
256 if (tea5764_i2c_write(radio))
257 PWARN("Could not set frequency!");
258}
259
260static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
261{
262 struct tea5764_regs *r = &radio->regs;
263 int tnctrl = r->tnctrl;
264
265 if (audmode == V4L2_TUNER_MODE_MONO)
266 r->tnctrl |= TEA5764_TNCTRL_MST;
267 else
268 r->tnctrl &= ~TEA5764_TNCTRL_MST;
269 if (tnctrl != r->tnctrl)
270 tea5764_i2c_write(radio);
271}
272
273static int tea5764_get_audout_mode(struct tea5764_device *radio)
274{
275 struct tea5764_regs *r = &radio->regs;
276
277 if (r->tnctrl & TEA5764_TNCTRL_MST)
278 return V4L2_TUNER_MODE_MONO;
279 else
280 return V4L2_TUNER_MODE_STEREO;
281}
282
283static void tea5764_mute(struct tea5764_device *radio, int on)
284{
285 struct tea5764_regs *r = &radio->regs;
286 int tnctrl = r->tnctrl;
287
288 if (on)
289 r->tnctrl |= TEA5764_TNCTRL_MU;
290 else
291 r->tnctrl &= ~TEA5764_TNCTRL_MU;
292 if (tnctrl != r->tnctrl)
293 tea5764_i2c_write(radio);
294}
295
296static int tea5764_is_muted(struct tea5764_device *radio)
297{
298 return radio->regs.tnctrl & TEA5764_TNCTRL_MU;
299}
300
301/* V4L2 vidioc */
302static int vidioc_querycap(struct file *file, void *priv,
303 struct v4l2_capability *v)
304{
305 struct tea5764_device *radio = video_drvdata(file);
306 struct video_device *dev = radio->videodev;
307
308 strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
309 strlcpy(v->card, dev->name, sizeof(v->card));
Kay Sieversc3ef01c2009-03-24 16:38:22 -0700310 snprintf(v->bus_info, sizeof(v->bus_info),
311 "I2C:%s", dev_name(&dev->dev));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300312 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
313 return 0;
314}
315
316static int vidioc_g_tuner(struct file *file, void *priv,
317 struct v4l2_tuner *v)
318{
319 struct tea5764_device *radio = video_drvdata(file);
320 struct tea5764_regs *r = &radio->regs;
321
322 if (v->index > 0)
323 return -EINVAL;
324
Julia Lawall909d15a2009-12-10 17:17:49 -0300325 memset(v, 0, sizeof(*v));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300326 strcpy(v->name, "FM");
327 v->type = V4L2_TUNER_RADIO;
328 tea5764_i2c_read(radio);
329 v->rangelow = FREQ_MIN * FREQ_MUL;
330 v->rangehigh = FREQ_MAX * FREQ_MUL;
331 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
332 if (r->tunchk & TEA5764_TUNCHK_STEREO)
Hans Verkuil5543e2b2009-06-20 06:29:12 -0300333 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
334 else
335 v->rxsubchans = V4L2_TUNER_SUB_MONO;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300336 v->audmode = tea5764_get_audout_mode(radio);
337 v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
338 v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
339
340 return 0;
341}
342
343static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300344 const struct v4l2_tuner *v)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300345{
346 struct tea5764_device *radio = video_drvdata(file);
347
348 if (v->index > 0)
349 return -EINVAL;
350
351 tea5764_set_audout_mode(radio, v->audmode);
352 return 0;
353}
354
355static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300356 const struct v4l2_frequency *f)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300357{
358 struct tea5764_device *radio = video_drvdata(file);
359
Hans Verkuila3a9e282009-11-27 04:33:25 -0300360 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300361 return -EINVAL;
362 if (f->frequency == 0) {
363 /* We special case this as a power down control. */
364 tea5764_power_down(radio);
365 }
366 if (f->frequency < (FREQ_MIN * FREQ_MUL))
367 return -EINVAL;
368 if (f->frequency > (FREQ_MAX * FREQ_MUL))
369 return -EINVAL;
370 tea5764_power_up(radio);
371 tea5764_tune(radio, (f->frequency * 125) / 2);
372 return 0;
373}
374
375static int vidioc_g_frequency(struct file *file, void *priv,
376 struct v4l2_frequency *f)
377{
378 struct tea5764_device *radio = video_drvdata(file);
379 struct tea5764_regs *r = &radio->regs;
380
Hans Verkuila3a9e282009-11-27 04:33:25 -0300381 if (f->tuner != 0)
382 return -EINVAL;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300383 tea5764_i2c_read(radio);
Julia Lawall909d15a2009-12-10 17:17:49 -0300384 memset(f, 0, sizeof(*f));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300385 f->type = V4L2_TUNER_RADIO;
386 if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
387 f->frequency = (tea5764_get_freq(radio) * 2) / 125;
388 else
389 f->frequency = 0;
390
391 return 0;
392}
393
394static int vidioc_queryctrl(struct file *file, void *priv,
395 struct v4l2_queryctrl *qc)
396{
397 int i;
398
399 for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
400 if (qc->id && qc->id == radio_qctrl[i].id) {
401 memcpy(qc, &(radio_qctrl[i]), sizeof(*qc));
402 return 0;
403 }
404 }
405 return -EINVAL;
406}
407
408static int vidioc_g_ctrl(struct file *file, void *priv,
409 struct v4l2_control *ctrl)
410{
411 struct tea5764_device *radio = video_drvdata(file);
412
413 switch (ctrl->id) {
414 case V4L2_CID_AUDIO_MUTE:
415 tea5764_i2c_read(radio);
416 ctrl->value = tea5764_is_muted(radio) ? 1 : 0;
417 return 0;
418 }
419 return -EINVAL;
420}
421
422static int vidioc_s_ctrl(struct file *file, void *priv,
423 struct v4l2_control *ctrl)
424{
425 struct tea5764_device *radio = video_drvdata(file);
426
427 switch (ctrl->id) {
428 case V4L2_CID_AUDIO_MUTE:
429 tea5764_mute(radio, ctrl->value);
430 return 0;
431 }
432 return -EINVAL;
433}
434
435static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
436{
437 *i = 0;
438 return 0;
439}
440
441static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
442{
443 if (i != 0)
444 return -EINVAL;
445 return 0;
446}
447
448static int vidioc_g_audio(struct file *file, void *priv,
449 struct v4l2_audio *a)
450{
451 if (a->index > 1)
452 return -EINVAL;
453
454 strcpy(a->name, "Radio");
455 a->capability = V4L2_AUDCAP_STEREO;
456 return 0;
457}
458
459static int vidioc_s_audio(struct file *file, void *priv,
Hans Verkuil0e8025b92012-09-04 11:59:31 -0300460 const struct v4l2_audio *a)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300461{
462 if (a->index != 0)
463 return -EINVAL;
464
465 return 0;
466}
467
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300468/* File system interface */
469static const struct v4l2_file_operations tea5764_fops = {
470 .owner = THIS_MODULE,
Hans Verkuilee71e422010-11-14 09:46:23 -0300471 .unlocked_ioctl = video_ioctl2,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300472};
473
474static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
475 .vidioc_querycap = vidioc_querycap,
476 .vidioc_g_tuner = vidioc_g_tuner,
477 .vidioc_s_tuner = vidioc_s_tuner,
478 .vidioc_g_audio = vidioc_g_audio,
479 .vidioc_s_audio = vidioc_s_audio,
480 .vidioc_g_input = vidioc_g_input,
481 .vidioc_s_input = vidioc_s_input,
482 .vidioc_g_frequency = vidioc_g_frequency,
483 .vidioc_s_frequency = vidioc_s_frequency,
484 .vidioc_queryctrl = vidioc_queryctrl,
485 .vidioc_g_ctrl = vidioc_g_ctrl,
486 .vidioc_s_ctrl = vidioc_s_ctrl,
487};
488
489/* V4L2 interface */
490static struct video_device tea5764_radio_template = {
491 .name = "TEA5764 FM-Radio",
492 .fops = &tea5764_fops,
493 .ioctl_ops = &tea5764_ioctl_ops,
494 .release = video_device_release,
495};
496
497/* I2C probe: check if the device exists and register with v4l if it is */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800498static int tea5764_i2c_probe(struct i2c_client *client,
499 const struct i2c_device_id *id)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300500{
501 struct tea5764_device *radio;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300502 struct v4l2_device *v4l2_dev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300503 struct tea5764_regs *r;
504 int ret;
505
506 PDEBUG("probe");
Hans Verkuilee71e422010-11-14 09:46:23 -0300507 radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300508 if (!radio)
509 return -ENOMEM;
510
Hans Verkuil099f88e2013-02-03 08:25:32 -0300511 v4l2_dev = &radio->v4l2_dev;
512 ret = v4l2_device_register(&client->dev, v4l2_dev);
513 if (ret < 0) {
514 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
515 goto errfr;
516 }
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300517 mutex_init(&radio->mutex);
518 radio->i2c_client = client;
519 ret = tea5764_i2c_read(radio);
520 if (ret)
Hans Verkuil099f88e2013-02-03 08:25:32 -0300521 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300522 r = &radio->regs;
523 PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
524 if (r->chipid != TEA5764_CHIPID ||
525 (r->manid & 0x0fff) != TEA5764_MANID) {
526 PWARN("This chip is not a TEA5764!");
527 ret = -EINVAL;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300528 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300529 }
530
531 radio->videodev = video_device_alloc();
532 if (!(radio->videodev)) {
533 ret = -ENOMEM;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300534 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300535 }
Hans Verkuil099f88e2013-02-03 08:25:32 -0300536 *radio->videodev = tea5764_radio_template;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300537
538 i2c_set_clientdata(client, radio);
539 video_set_drvdata(radio->videodev, radio);
Hans Verkuilee71e422010-11-14 09:46:23 -0300540 radio->videodev->lock = &radio->mutex;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300541 radio->videodev->v4l2_dev = v4l2_dev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300542
543 /* initialize and power off the chip */
544 tea5764_i2c_read(radio);
545 tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
546 tea5764_mute(radio, 1);
547 tea5764_power_down(radio);
548
Hans Verkuilee71e422010-11-14 09:46:23 -0300549 ret = video_register_device(radio->videodev, VFL_TYPE_RADIO, radio_nr);
550 if (ret < 0) {
551 PWARN("Could not register video device!");
552 goto errrel;
553 }
554
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300555 PINFO("registered.");
556 return 0;
557errrel:
558 video_device_release(radio->videodev);
Hans Verkuil099f88e2013-02-03 08:25:32 -0300559errunreg:
560 v4l2_device_unregister(v4l2_dev);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300561errfr:
562 kfree(radio);
563 return ret;
564}
565
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800566static int tea5764_i2c_remove(struct i2c_client *client)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300567{
568 struct tea5764_device *radio = i2c_get_clientdata(client);
569
570 PDEBUG("remove");
571 if (radio) {
572 tea5764_power_down(radio);
573 video_unregister_device(radio->videodev);
Hans Verkuil099f88e2013-02-03 08:25:32 -0300574 v4l2_device_unregister(&radio->v4l2_dev);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300575 kfree(radio);
576 }
577 return 0;
578}
579
580/* I2C subsystem interface */
581static const struct i2c_device_id tea5764_id[] = {
582 { "radio-tea5764", 0 },
583 { } /* Terminating entry */
584};
585MODULE_DEVICE_TABLE(i2c, tea5764_id);
586
587static struct i2c_driver tea5764_i2c_driver = {
588 .driver = {
589 .name = "radio-tea5764",
590 .owner = THIS_MODULE,
591 },
592 .probe = tea5764_i2c_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800593 .remove = tea5764_i2c_remove,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300594 .id_table = tea5764_id,
595};
596
Axel Linc6e8d862012-02-12 06:56:32 -0300597module_i2c_driver(tea5764_i2c_driver);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300598
599MODULE_AUTHOR(DRIVER_AUTHOR);
600MODULE_DESCRIPTION(DRIVER_DESC);
601MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -0300602MODULE_VERSION(DRIVER_VERSION);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300603
Jean Delvare731884b2011-07-08 06:00:37 -0300604module_param(use_xtal, int, 0);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300605MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
606module_param(radio_nr, int, 0);
607MODULE_PARM_DESC(radio_nr, "video4linux device number to use");