blob: c22feedb36f50eb2b846152c0520d541f861614a [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>
Hans Verkuil16b37172013-02-03 08:34:18 -030043#include <media/v4l2-ctrls.h>
Hans Verkuil090fdf62013-02-03 08:39:09 -030044#include <media/v4l2-event.h>
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030045
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030046#define DRIVER_VERSION "0.0.2"
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030047
48#define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
49#define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
50
51#define PINFO(format, ...)\
52 printk(KERN_INFO KBUILD_MODNAME ": "\
53 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
54#define PWARN(format, ...)\
55 printk(KERN_WARNING KBUILD_MODNAME ": "\
56 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
57# define PDEBUG(format, ...)\
58 printk(KERN_DEBUG KBUILD_MODNAME ": "\
59 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
60
61/* Frequency limits in MHz -- these are European values. For Japanese
62devices, that would be 76000 and 91000. */
63#define FREQ_MIN 87500
64#define FREQ_MAX 108000
65#define FREQ_MUL 16
66
67/* TEA5764 registers */
68#define TEA5764_MANID 0x002b
69#define TEA5764_CHIPID 0x5764
70
71#define TEA5764_INTREG_BLMSK 0x0001
72#define TEA5764_INTREG_FRRMSK 0x0002
73#define TEA5764_INTREG_LEVMSK 0x0008
74#define TEA5764_INTREG_IFMSK 0x0010
75#define TEA5764_INTREG_BLMFLAG 0x0100
76#define TEA5764_INTREG_FRRFLAG 0x0200
77#define TEA5764_INTREG_LEVFLAG 0x0800
78#define TEA5764_INTREG_IFFLAG 0x1000
79
80#define TEA5764_FRQSET_SUD 0x8000
81#define TEA5764_FRQSET_SM 0x4000
82
83#define TEA5764_TNCTRL_PUPD1 0x8000
84#define TEA5764_TNCTRL_PUPD0 0x4000
85#define TEA5764_TNCTRL_BLIM 0x2000
86#define TEA5764_TNCTRL_SWPM 0x1000
87#define TEA5764_TNCTRL_IFCTC 0x0800
88#define TEA5764_TNCTRL_AFM 0x0400
89#define TEA5764_TNCTRL_SMUTE 0x0200
90#define TEA5764_TNCTRL_SNC 0x0100
91#define TEA5764_TNCTRL_MU 0x0080
92#define TEA5764_TNCTRL_SSL1 0x0040
93#define TEA5764_TNCTRL_SSL0 0x0020
94#define TEA5764_TNCTRL_HLSI 0x0010
95#define TEA5764_TNCTRL_MST 0x0008
96#define TEA5764_TNCTRL_SWP 0x0004
97#define TEA5764_TNCTRL_DTC 0x0002
98#define TEA5764_TNCTRL_AHLSI 0x0001
99
100#define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
101#define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
102#define TEA5764_TUNCHK_TUNTO 0x0100
103#define TEA5764_TUNCHK_LD 0x0008
104#define TEA5764_TUNCHK_STEREO 0x0004
105
106#define TEA5764_TESTREG_TRIGFR 0x0800
107
108struct tea5764_regs {
109 u16 intreg; /* INTFLAG & INTMSK */
110 u16 frqset; /* FRQSETMSB & FRQSETLSB */
111 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
112 u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */
113 u16 tunchk; /* IFCHK & LEVCHK */
114 u16 testreg; /* TESTBITS & TESTMODE */
115 u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */
116 u16 rdslb; /* RDSLBMSB & RDSLBLSB */
117 u16 rdspb; /* RDSPBMSB & RDSPBLSB */
118 u16 rdsbc; /* RDSBBC & RDSGBC */
119 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
120 u16 rdsbbl; /* PAUSEDET & RDSBBL */
121 u16 manid; /* MANID1 & MANID2 */
122 u16 chipid; /* CHIPID1 & CHIPID2 */
123} __attribute__ ((packed));
124
125struct tea5764_write_regs {
126 u8 intreg; /* INTMSK */
127 u16 frqset; /* FRQSETMSB & FRQSETLSB */
128 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
129 u16 testreg; /* TESTBITS & TESTMODE */
130 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
131 u16 rdsbbl; /* PAUSEDET & RDSBBL */
132} __attribute__ ((packed));
133
Paul Bolle8c4343e2011-10-12 17:51:22 -0300134#ifdef CONFIG_RADIO_TEA5764_XTAL
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300135#define RADIO_TEA5764_XTAL 1
Paul Bolle8c4343e2011-10-12 17:51:22 -0300136#else
137#define RADIO_TEA5764_XTAL 0
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300138#endif
139
140static int radio_nr = -1;
141static int use_xtal = RADIO_TEA5764_XTAL;
142
143struct tea5764_device {
Hans Verkuil16b37172013-02-03 08:34:18 -0300144 struct v4l2_device v4l2_dev;
145 struct v4l2_ctrl_handler ctrl_handler;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300146 struct i2c_client *i2c_client;
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300147 struct video_device vdev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300148 struct tea5764_regs regs;
149 struct mutex mutex;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300150};
151
152/* I2C code related */
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -0200153static int tea5764_i2c_read(struct tea5764_device *radio)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300154{
155 int i;
156 u16 *p = (u16 *) &radio->regs;
157
158 struct i2c_msg msgs[1] = {
Shubhrajyoti D66ba9592012-09-18 08:22:33 -0300159 { .addr = radio->i2c_client->addr,
160 .flags = I2C_M_RD,
161 .len = sizeof(radio->regs),
162 .buf = (void *)&radio->regs
163 },
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300164 };
165 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
166 return -EIO;
167 for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
168 p[i] = __be16_to_cpu(p[i]);
169
170 return 0;
171}
172
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -0200173static int tea5764_i2c_write(struct tea5764_device *radio)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300174{
175 struct tea5764_write_regs wr;
176 struct tea5764_regs *r = &radio->regs;
177 struct i2c_msg msgs[1] = {
Shubhrajyoti D66ba9592012-09-18 08:22:33 -0300178 {
179 .addr = radio->i2c_client->addr,
180 .len = sizeof(wr),
181 .buf = (void *)&wr
182 },
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300183 };
184 wr.intreg = r->intreg & 0xff;
185 wr.frqset = __cpu_to_be16(r->frqset);
186 wr.tnctrl = __cpu_to_be16(r->tnctrl);
187 wr.testreg = __cpu_to_be16(r->testreg);
188 wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
189 wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
190 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
191 return -EIO;
192 return 0;
193}
194
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300195static void tea5764_power_up(struct tea5764_device *radio)
196{
197 struct tea5764_regs *r = &radio->regs;
198
199 if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
200 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
201 TEA5764_TNCTRL_HLSI);
202 if (!use_xtal)
203 r->testreg |= TEA5764_TESTREG_TRIGFR;
204 else
205 r->testreg &= ~TEA5764_TESTREG_TRIGFR;
206
207 r->tnctrl |= TEA5764_TNCTRL_PUPD0;
208 tea5764_i2c_write(radio);
209 }
210}
211
212static void tea5764_power_down(struct tea5764_device *radio)
213{
214 struct tea5764_regs *r = &radio->regs;
215
216 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
217 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
218 tea5764_i2c_write(radio);
219 }
220}
221
222static void tea5764_set_freq(struct tea5764_device *radio, int freq)
223{
224 struct tea5764_regs *r = &radio->regs;
225
226 /* formula: (freq [+ or -] 225000) / 8192 */
227 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
228 r->frqset = (freq + 225000) / 8192;
229 else
230 r->frqset = (freq - 225000) / 8192;
231}
232
233static int tea5764_get_freq(struct tea5764_device *radio)
234{
235 struct tea5764_regs *r = &radio->regs;
236
237 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
238 return (r->frqchk * 8192) - 225000;
239 else
240 return (r->frqchk * 8192) + 225000;
241}
242
243/* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
244static void tea5764_tune(struct tea5764_device *radio, int freq)
245{
246 tea5764_set_freq(radio, freq);
247 if (tea5764_i2c_write(radio))
248 PWARN("Could not set frequency!");
249}
250
251static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
252{
253 struct tea5764_regs *r = &radio->regs;
254 int tnctrl = r->tnctrl;
255
256 if (audmode == V4L2_TUNER_MODE_MONO)
257 r->tnctrl |= TEA5764_TNCTRL_MST;
258 else
259 r->tnctrl &= ~TEA5764_TNCTRL_MST;
260 if (tnctrl != r->tnctrl)
261 tea5764_i2c_write(radio);
262}
263
264static int tea5764_get_audout_mode(struct tea5764_device *radio)
265{
266 struct tea5764_regs *r = &radio->regs;
267
268 if (r->tnctrl & TEA5764_TNCTRL_MST)
269 return V4L2_TUNER_MODE_MONO;
270 else
271 return V4L2_TUNER_MODE_STEREO;
272}
273
274static void tea5764_mute(struct tea5764_device *radio, int on)
275{
276 struct tea5764_regs *r = &radio->regs;
277 int tnctrl = r->tnctrl;
278
279 if (on)
280 r->tnctrl |= TEA5764_TNCTRL_MU;
281 else
282 r->tnctrl &= ~TEA5764_TNCTRL_MU;
283 if (tnctrl != r->tnctrl)
284 tea5764_i2c_write(radio);
285}
286
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300287/* V4L2 vidioc */
288static int vidioc_querycap(struct file *file, void *priv,
289 struct v4l2_capability *v)
290{
291 struct tea5764_device *radio = video_drvdata(file);
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300292 struct video_device *dev = &radio->vdev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300293
294 strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
295 strlcpy(v->card, dev->name, sizeof(v->card));
Kay Sieversc3ef01c2009-03-24 16:38:22 -0700296 snprintf(v->bus_info, sizeof(v->bus_info),
297 "I2C:%s", dev_name(&dev->dev));
Hans Verkuilcc9231f2013-02-03 08:36:42 -0300298 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
299 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300300 return 0;
301}
302
303static int vidioc_g_tuner(struct file *file, void *priv,
304 struct v4l2_tuner *v)
305{
306 struct tea5764_device *radio = video_drvdata(file);
307 struct tea5764_regs *r = &radio->regs;
308
309 if (v->index > 0)
310 return -EINVAL;
311
Julia Lawall909d15a2009-12-10 17:17:49 -0300312 memset(v, 0, sizeof(*v));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300313 strcpy(v->name, "FM");
314 v->type = V4L2_TUNER_RADIO;
315 tea5764_i2c_read(radio);
316 v->rangelow = FREQ_MIN * FREQ_MUL;
317 v->rangehigh = FREQ_MAX * FREQ_MUL;
318 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
319 if (r->tunchk & TEA5764_TUNCHK_STEREO)
Hans Verkuil5543e2b2009-06-20 06:29:12 -0300320 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
321 else
322 v->rxsubchans = V4L2_TUNER_SUB_MONO;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300323 v->audmode = tea5764_get_audout_mode(radio);
324 v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
325 v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
326
327 return 0;
328}
329
330static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300331 const struct v4l2_tuner *v)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300332{
333 struct tea5764_device *radio = video_drvdata(file);
334
335 if (v->index > 0)
336 return -EINVAL;
337
338 tea5764_set_audout_mode(radio, v->audmode);
339 return 0;
340}
341
342static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300343 const struct v4l2_frequency *f)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300344{
345 struct tea5764_device *radio = video_drvdata(file);
346
Hans Verkuila3a9e282009-11-27 04:33:25 -0300347 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300348 return -EINVAL;
349 if (f->frequency == 0) {
350 /* We special case this as a power down control. */
351 tea5764_power_down(radio);
352 }
353 if (f->frequency < (FREQ_MIN * FREQ_MUL))
354 return -EINVAL;
355 if (f->frequency > (FREQ_MAX * FREQ_MUL))
356 return -EINVAL;
357 tea5764_power_up(radio);
358 tea5764_tune(radio, (f->frequency * 125) / 2);
359 return 0;
360}
361
362static int vidioc_g_frequency(struct file *file, void *priv,
363 struct v4l2_frequency *f)
364{
365 struct tea5764_device *radio = video_drvdata(file);
366 struct tea5764_regs *r = &radio->regs;
367
Hans Verkuila3a9e282009-11-27 04:33:25 -0300368 if (f->tuner != 0)
369 return -EINVAL;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300370 tea5764_i2c_read(radio);
Julia Lawall909d15a2009-12-10 17:17:49 -0300371 memset(f, 0, sizeof(*f));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300372 f->type = V4L2_TUNER_RADIO;
373 if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
374 f->frequency = (tea5764_get_freq(radio) * 2) / 125;
375 else
376 f->frequency = 0;
377
378 return 0;
379}
380
Hans Verkuil16b37172013-02-03 08:34:18 -0300381static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300382{
Hans Verkuil16b37172013-02-03 08:34:18 -0300383 struct tea5764_device *radio =
384 container_of(ctrl->handler, struct tea5764_device, ctrl_handler);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300385
386 switch (ctrl->id) {
387 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil16b37172013-02-03 08:34:18 -0300388 tea5764_mute(radio, ctrl->val);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300389 return 0;
390 }
391 return -EINVAL;
392}
393
Hans Verkuil16b37172013-02-03 08:34:18 -0300394static const struct v4l2_ctrl_ops tea5764_ctrl_ops = {
395 .s_ctrl = tea5764_s_ctrl,
396};
397
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300398/* File system interface */
399static const struct v4l2_file_operations tea5764_fops = {
400 .owner = THIS_MODULE,
Hans Verkuil090fdf62013-02-03 08:39:09 -0300401 .open = v4l2_fh_open,
402 .release = v4l2_fh_release,
403 .poll = v4l2_ctrl_poll,
Hans Verkuilee71e422010-11-14 09:46:23 -0300404 .unlocked_ioctl = video_ioctl2,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300405};
406
407static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
408 .vidioc_querycap = vidioc_querycap,
409 .vidioc_g_tuner = vidioc_g_tuner,
410 .vidioc_s_tuner = vidioc_s_tuner,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300411 .vidioc_g_frequency = vidioc_g_frequency,
412 .vidioc_s_frequency = vidioc_s_frequency,
Hans Verkuil090fdf62013-02-03 08:39:09 -0300413 .vidioc_log_status = v4l2_ctrl_log_status,
414 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
415 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300416};
417
418/* V4L2 interface */
419static struct video_device tea5764_radio_template = {
420 .name = "TEA5764 FM-Radio",
421 .fops = &tea5764_fops,
422 .ioctl_ops = &tea5764_ioctl_ops,
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300423 .release = video_device_release_empty,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300424};
425
426/* I2C probe: check if the device exists and register with v4l if it is */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800427static int tea5764_i2c_probe(struct i2c_client *client,
428 const struct i2c_device_id *id)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300429{
430 struct tea5764_device *radio;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300431 struct v4l2_device *v4l2_dev;
Hans Verkuil16b37172013-02-03 08:34:18 -0300432 struct v4l2_ctrl_handler *hdl;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300433 struct tea5764_regs *r;
434 int ret;
435
436 PDEBUG("probe");
Hans Verkuilee71e422010-11-14 09:46:23 -0300437 radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300438 if (!radio)
439 return -ENOMEM;
440
Hans Verkuil099f88e2013-02-03 08:25:32 -0300441 v4l2_dev = &radio->v4l2_dev;
442 ret = v4l2_device_register(&client->dev, v4l2_dev);
443 if (ret < 0) {
444 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
445 goto errfr;
446 }
Hans Verkuil16b37172013-02-03 08:34:18 -0300447
448 hdl = &radio->ctrl_handler;
449 v4l2_ctrl_handler_init(hdl, 1);
450 v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops,
451 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
452 v4l2_dev->ctrl_handler = hdl;
453 if (hdl->error) {
454 ret = hdl->error;
455 v4l2_err(v4l2_dev, "Could not register controls\n");
456 goto errunreg;
457 }
458
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300459 mutex_init(&radio->mutex);
460 radio->i2c_client = client;
461 ret = tea5764_i2c_read(radio);
462 if (ret)
Hans Verkuil099f88e2013-02-03 08:25:32 -0300463 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300464 r = &radio->regs;
465 PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
466 if (r->chipid != TEA5764_CHIPID ||
467 (r->manid & 0x0fff) != TEA5764_MANID) {
468 PWARN("This chip is not a TEA5764!");
469 ret = -EINVAL;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300470 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300471 }
472
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300473 radio->vdev = tea5764_radio_template;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300474
475 i2c_set_clientdata(client, radio);
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300476 video_set_drvdata(&radio->vdev, radio);
477 radio->vdev.lock = &radio->mutex;
478 radio->vdev.v4l2_dev = v4l2_dev;
Hans Verkuil090fdf62013-02-03 08:39:09 -0300479 set_bit(V4L2_FL_USE_FH_PRIO, &radio->vdev.flags);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300480
481 /* initialize and power off the chip */
482 tea5764_i2c_read(radio);
483 tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
484 tea5764_mute(radio, 1);
485 tea5764_power_down(radio);
486
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300487 ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr);
Hans Verkuilee71e422010-11-14 09:46:23 -0300488 if (ret < 0) {
489 PWARN("Could not register video device!");
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300490 goto errunreg;
Hans Verkuilee71e422010-11-14 09:46:23 -0300491 }
492
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300493 PINFO("registered.");
494 return 0;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300495errunreg:
Hans Verkuil16b37172013-02-03 08:34:18 -0300496 v4l2_ctrl_handler_free(hdl);
Hans Verkuil099f88e2013-02-03 08:25:32 -0300497 v4l2_device_unregister(v4l2_dev);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300498errfr:
499 kfree(radio);
500 return ret;
501}
502
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800503static int tea5764_i2c_remove(struct i2c_client *client)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300504{
505 struct tea5764_device *radio = i2c_get_clientdata(client);
506
507 PDEBUG("remove");
508 if (radio) {
509 tea5764_power_down(radio);
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300510 video_unregister_device(&radio->vdev);
Hans Verkuil16b37172013-02-03 08:34:18 -0300511 v4l2_ctrl_handler_free(&radio->ctrl_handler);
Hans Verkuil099f88e2013-02-03 08:25:32 -0300512 v4l2_device_unregister(&radio->v4l2_dev);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300513 kfree(radio);
514 }
515 return 0;
516}
517
518/* I2C subsystem interface */
519static const struct i2c_device_id tea5764_id[] = {
520 { "radio-tea5764", 0 },
521 { } /* Terminating entry */
522};
523MODULE_DEVICE_TABLE(i2c, tea5764_id);
524
525static struct i2c_driver tea5764_i2c_driver = {
526 .driver = {
527 .name = "radio-tea5764",
528 .owner = THIS_MODULE,
529 },
530 .probe = tea5764_i2c_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800531 .remove = tea5764_i2c_remove,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300532 .id_table = tea5764_id,
533};
534
Axel Linc6e8d862012-02-12 06:56:32 -0300535module_i2c_driver(tea5764_i2c_driver);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300536
537MODULE_AUTHOR(DRIVER_AUTHOR);
538MODULE_DESCRIPTION(DRIVER_DESC);
539MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -0300540MODULE_VERSION(DRIVER_VERSION);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300541
Jean Delvare731884b2011-07-08 06:00:37 -0300542module_param(use_xtal, int, 0);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300543MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
544module_param(radio_nr, int, 0);
545MODULE_PARM_DESC(radio_nr, "video4linux device number to use");