blob: 5c47a970843e7ced0895291f4dcffe05c2d0f9ae [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>
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030044
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -030045#define DRIVER_VERSION "0.0.2"
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -030046
47#define DRIVER_AUTHOR "Fabio Belavenuto <belavenuto@gmail.com>"
48#define DRIVER_DESC "A driver for the TEA5764 radio chip for EZX Phones."
49
50#define PINFO(format, ...)\
51 printk(KERN_INFO KBUILD_MODNAME ": "\
52 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
53#define PWARN(format, ...)\
54 printk(KERN_WARNING KBUILD_MODNAME ": "\
55 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
56# define PDEBUG(format, ...)\
57 printk(KERN_DEBUG KBUILD_MODNAME ": "\
58 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
59
60/* Frequency limits in MHz -- these are European values. For Japanese
61devices, that would be 76000 and 91000. */
62#define FREQ_MIN 87500
63#define FREQ_MAX 108000
64#define FREQ_MUL 16
65
66/* TEA5764 registers */
67#define TEA5764_MANID 0x002b
68#define TEA5764_CHIPID 0x5764
69
70#define TEA5764_INTREG_BLMSK 0x0001
71#define TEA5764_INTREG_FRRMSK 0x0002
72#define TEA5764_INTREG_LEVMSK 0x0008
73#define TEA5764_INTREG_IFMSK 0x0010
74#define TEA5764_INTREG_BLMFLAG 0x0100
75#define TEA5764_INTREG_FRRFLAG 0x0200
76#define TEA5764_INTREG_LEVFLAG 0x0800
77#define TEA5764_INTREG_IFFLAG 0x1000
78
79#define TEA5764_FRQSET_SUD 0x8000
80#define TEA5764_FRQSET_SM 0x4000
81
82#define TEA5764_TNCTRL_PUPD1 0x8000
83#define TEA5764_TNCTRL_PUPD0 0x4000
84#define TEA5764_TNCTRL_BLIM 0x2000
85#define TEA5764_TNCTRL_SWPM 0x1000
86#define TEA5764_TNCTRL_IFCTC 0x0800
87#define TEA5764_TNCTRL_AFM 0x0400
88#define TEA5764_TNCTRL_SMUTE 0x0200
89#define TEA5764_TNCTRL_SNC 0x0100
90#define TEA5764_TNCTRL_MU 0x0080
91#define TEA5764_TNCTRL_SSL1 0x0040
92#define TEA5764_TNCTRL_SSL0 0x0020
93#define TEA5764_TNCTRL_HLSI 0x0010
94#define TEA5764_TNCTRL_MST 0x0008
95#define TEA5764_TNCTRL_SWP 0x0004
96#define TEA5764_TNCTRL_DTC 0x0002
97#define TEA5764_TNCTRL_AHLSI 0x0001
98
99#define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
100#define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
101#define TEA5764_TUNCHK_TUNTO 0x0100
102#define TEA5764_TUNCHK_LD 0x0008
103#define TEA5764_TUNCHK_STEREO 0x0004
104
105#define TEA5764_TESTREG_TRIGFR 0x0800
106
107struct tea5764_regs {
108 u16 intreg; /* INTFLAG & INTMSK */
109 u16 frqset; /* FRQSETMSB & FRQSETLSB */
110 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
111 u16 frqchk; /* FRQCHKMSB & FRQCHKLSB */
112 u16 tunchk; /* IFCHK & LEVCHK */
113 u16 testreg; /* TESTBITS & TESTMODE */
114 u16 rdsstat; /* RDSSTAT1 & RDSSTAT2 */
115 u16 rdslb; /* RDSLBMSB & RDSLBLSB */
116 u16 rdspb; /* RDSPBMSB & RDSPBLSB */
117 u16 rdsbc; /* RDSBBC & RDSGBC */
118 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
119 u16 rdsbbl; /* PAUSEDET & RDSBBL */
120 u16 manid; /* MANID1 & MANID2 */
121 u16 chipid; /* CHIPID1 & CHIPID2 */
122} __attribute__ ((packed));
123
124struct tea5764_write_regs {
125 u8 intreg; /* INTMSK */
126 u16 frqset; /* FRQSETMSB & FRQSETLSB */
127 u16 tnctrl; /* TNCTRL1 & TNCTRL2 */
128 u16 testreg; /* TESTBITS & TESTMODE */
129 u16 rdsctrl; /* RDSCTRL1 & RDSCTRL2 */
130 u16 rdsbbl; /* PAUSEDET & RDSBBL */
131} __attribute__ ((packed));
132
Paul Bolle8c4343e2011-10-12 17:51:22 -0300133#ifdef CONFIG_RADIO_TEA5764_XTAL
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300134#define RADIO_TEA5764_XTAL 1
Paul Bolle8c4343e2011-10-12 17:51:22 -0300135#else
136#define RADIO_TEA5764_XTAL 0
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300137#endif
138
139static int radio_nr = -1;
140static int use_xtal = RADIO_TEA5764_XTAL;
141
142struct tea5764_device {
Hans Verkuil16b37172013-02-03 08:34:18 -0300143 struct v4l2_device v4l2_dev;
144 struct v4l2_ctrl_handler ctrl_handler;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300145 struct i2c_client *i2c_client;
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300146 struct video_device vdev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300147 struct tea5764_regs regs;
148 struct mutex mutex;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300149};
150
151/* I2C code related */
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -0200152static int tea5764_i2c_read(struct tea5764_device *radio)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300153{
154 int i;
155 u16 *p = (u16 *) &radio->regs;
156
157 struct i2c_msg msgs[1] = {
Shubhrajyoti D66ba9592012-09-18 08:22:33 -0300158 { .addr = radio->i2c_client->addr,
159 .flags = I2C_M_RD,
160 .len = sizeof(radio->regs),
161 .buf = (void *)&radio->regs
162 },
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300163 };
164 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
165 return -EIO;
166 for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
167 p[i] = __be16_to_cpu(p[i]);
168
169 return 0;
170}
171
Mauro Carvalho Chehab0dec8682012-10-27 18:35:46 -0200172static int tea5764_i2c_write(struct tea5764_device *radio)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300173{
174 struct tea5764_write_regs wr;
175 struct tea5764_regs *r = &radio->regs;
176 struct i2c_msg msgs[1] = {
Shubhrajyoti D66ba9592012-09-18 08:22:33 -0300177 {
178 .addr = radio->i2c_client->addr,
179 .len = sizeof(wr),
180 .buf = (void *)&wr
181 },
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300182 };
183 wr.intreg = r->intreg & 0xff;
184 wr.frqset = __cpu_to_be16(r->frqset);
185 wr.tnctrl = __cpu_to_be16(r->tnctrl);
186 wr.testreg = __cpu_to_be16(r->testreg);
187 wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
188 wr.rdsbbl = __cpu_to_be16(r->rdsbbl);
189 if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
190 return -EIO;
191 return 0;
192}
193
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300194static void tea5764_power_up(struct tea5764_device *radio)
195{
196 struct tea5764_regs *r = &radio->regs;
197
198 if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
199 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
200 TEA5764_TNCTRL_HLSI);
201 if (!use_xtal)
202 r->testreg |= TEA5764_TESTREG_TRIGFR;
203 else
204 r->testreg &= ~TEA5764_TESTREG_TRIGFR;
205
206 r->tnctrl |= TEA5764_TNCTRL_PUPD0;
207 tea5764_i2c_write(radio);
208 }
209}
210
211static void tea5764_power_down(struct tea5764_device *radio)
212{
213 struct tea5764_regs *r = &radio->regs;
214
215 if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
216 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
217 tea5764_i2c_write(radio);
218 }
219}
220
221static void tea5764_set_freq(struct tea5764_device *radio, int freq)
222{
223 struct tea5764_regs *r = &radio->regs;
224
225 /* formula: (freq [+ or -] 225000) / 8192 */
226 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
227 r->frqset = (freq + 225000) / 8192;
228 else
229 r->frqset = (freq - 225000) / 8192;
230}
231
232static int tea5764_get_freq(struct tea5764_device *radio)
233{
234 struct tea5764_regs *r = &radio->regs;
235
236 if (r->tnctrl & TEA5764_TNCTRL_HLSI)
237 return (r->frqchk * 8192) - 225000;
238 else
239 return (r->frqchk * 8192) + 225000;
240}
241
242/* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
243static void tea5764_tune(struct tea5764_device *radio, int freq)
244{
245 tea5764_set_freq(radio, freq);
246 if (tea5764_i2c_write(radio))
247 PWARN("Could not set frequency!");
248}
249
250static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
251{
252 struct tea5764_regs *r = &radio->regs;
253 int tnctrl = r->tnctrl;
254
255 if (audmode == V4L2_TUNER_MODE_MONO)
256 r->tnctrl |= TEA5764_TNCTRL_MST;
257 else
258 r->tnctrl &= ~TEA5764_TNCTRL_MST;
259 if (tnctrl != r->tnctrl)
260 tea5764_i2c_write(radio);
261}
262
263static int tea5764_get_audout_mode(struct tea5764_device *radio)
264{
265 struct tea5764_regs *r = &radio->regs;
266
267 if (r->tnctrl & TEA5764_TNCTRL_MST)
268 return V4L2_TUNER_MODE_MONO;
269 else
270 return V4L2_TUNER_MODE_STEREO;
271}
272
273static void tea5764_mute(struct tea5764_device *radio, int on)
274{
275 struct tea5764_regs *r = &radio->regs;
276 int tnctrl = r->tnctrl;
277
278 if (on)
279 r->tnctrl |= TEA5764_TNCTRL_MU;
280 else
281 r->tnctrl &= ~TEA5764_TNCTRL_MU;
282 if (tnctrl != r->tnctrl)
283 tea5764_i2c_write(radio);
284}
285
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300286/* V4L2 vidioc */
287static int vidioc_querycap(struct file *file, void *priv,
288 struct v4l2_capability *v)
289{
290 struct tea5764_device *radio = video_drvdata(file);
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300291 struct video_device *dev = &radio->vdev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300292
293 strlcpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
294 strlcpy(v->card, dev->name, sizeof(v->card));
Kay Sieversc3ef01c2009-03-24 16:38:22 -0700295 snprintf(v->bus_info, sizeof(v->bus_info),
296 "I2C:%s", dev_name(&dev->dev));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300297 v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
298 return 0;
299}
300
301static int vidioc_g_tuner(struct file *file, void *priv,
302 struct v4l2_tuner *v)
303{
304 struct tea5764_device *radio = video_drvdata(file);
305 struct tea5764_regs *r = &radio->regs;
306
307 if (v->index > 0)
308 return -EINVAL;
309
Julia Lawall909d15a2009-12-10 17:17:49 -0300310 memset(v, 0, sizeof(*v));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300311 strcpy(v->name, "FM");
312 v->type = V4L2_TUNER_RADIO;
313 tea5764_i2c_read(radio);
314 v->rangelow = FREQ_MIN * FREQ_MUL;
315 v->rangehigh = FREQ_MAX * FREQ_MUL;
316 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
317 if (r->tunchk & TEA5764_TUNCHK_STEREO)
Hans Verkuil5543e2b2009-06-20 06:29:12 -0300318 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
319 else
320 v->rxsubchans = V4L2_TUNER_SUB_MONO;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300321 v->audmode = tea5764_get_audout_mode(radio);
322 v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
323 v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
324
325 return 0;
326}
327
328static int vidioc_s_tuner(struct file *file, void *priv,
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300329 const struct v4l2_tuner *v)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300330{
331 struct tea5764_device *radio = video_drvdata(file);
332
333 if (v->index > 0)
334 return -EINVAL;
335
336 tea5764_set_audout_mode(radio, v->audmode);
337 return 0;
338}
339
340static int vidioc_s_frequency(struct file *file, void *priv,
Hans Verkuilb530a442013-03-19 04:09:26 -0300341 const struct v4l2_frequency *f)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300342{
343 struct tea5764_device *radio = video_drvdata(file);
344
Hans Verkuila3a9e282009-11-27 04:33:25 -0300345 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300346 return -EINVAL;
347 if (f->frequency == 0) {
348 /* We special case this as a power down control. */
349 tea5764_power_down(radio);
350 }
351 if (f->frequency < (FREQ_MIN * FREQ_MUL))
352 return -EINVAL;
353 if (f->frequency > (FREQ_MAX * FREQ_MUL))
354 return -EINVAL;
355 tea5764_power_up(radio);
356 tea5764_tune(radio, (f->frequency * 125) / 2);
357 return 0;
358}
359
360static int vidioc_g_frequency(struct file *file, void *priv,
361 struct v4l2_frequency *f)
362{
363 struct tea5764_device *radio = video_drvdata(file);
364 struct tea5764_regs *r = &radio->regs;
365
Hans Verkuila3a9e282009-11-27 04:33:25 -0300366 if (f->tuner != 0)
367 return -EINVAL;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300368 tea5764_i2c_read(radio);
Julia Lawall909d15a2009-12-10 17:17:49 -0300369 memset(f, 0, sizeof(*f));
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300370 f->type = V4L2_TUNER_RADIO;
371 if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
372 f->frequency = (tea5764_get_freq(radio) * 2) / 125;
373 else
374 f->frequency = 0;
375
376 return 0;
377}
378
Hans Verkuil16b37172013-02-03 08:34:18 -0300379static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300380{
Hans Verkuil16b37172013-02-03 08:34:18 -0300381 struct tea5764_device *radio =
382 container_of(ctrl->handler, struct tea5764_device, ctrl_handler);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300383
384 switch (ctrl->id) {
385 case V4L2_CID_AUDIO_MUTE:
Hans Verkuil16b37172013-02-03 08:34:18 -0300386 tea5764_mute(radio, ctrl->val);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300387 return 0;
388 }
389 return -EINVAL;
390}
391
392static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
393{
394 *i = 0;
395 return 0;
396}
397
398static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
399{
400 if (i != 0)
401 return -EINVAL;
402 return 0;
403}
404
405static int vidioc_g_audio(struct file *file, void *priv,
406 struct v4l2_audio *a)
407{
408 if (a->index > 1)
409 return -EINVAL;
410
411 strcpy(a->name, "Radio");
412 a->capability = V4L2_AUDCAP_STEREO;
413 return 0;
414}
415
416static int vidioc_s_audio(struct file *file, void *priv,
Hans Verkuil0e8025b92012-09-04 11:59:31 -0300417 const struct v4l2_audio *a)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300418{
419 if (a->index != 0)
420 return -EINVAL;
421
422 return 0;
423}
424
Hans Verkuil16b37172013-02-03 08:34:18 -0300425static const struct v4l2_ctrl_ops tea5764_ctrl_ops = {
426 .s_ctrl = tea5764_s_ctrl,
427};
428
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300429/* File system interface */
430static const struct v4l2_file_operations tea5764_fops = {
431 .owner = THIS_MODULE,
Hans Verkuilee71e422010-11-14 09:46:23 -0300432 .unlocked_ioctl = video_ioctl2,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300433};
434
435static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
436 .vidioc_querycap = vidioc_querycap,
437 .vidioc_g_tuner = vidioc_g_tuner,
438 .vidioc_s_tuner = vidioc_s_tuner,
439 .vidioc_g_audio = vidioc_g_audio,
440 .vidioc_s_audio = vidioc_s_audio,
441 .vidioc_g_input = vidioc_g_input,
442 .vidioc_s_input = vidioc_s_input,
443 .vidioc_g_frequency = vidioc_g_frequency,
444 .vidioc_s_frequency = vidioc_s_frequency,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300445};
446
447/* V4L2 interface */
448static struct video_device tea5764_radio_template = {
449 .name = "TEA5764 FM-Radio",
450 .fops = &tea5764_fops,
451 .ioctl_ops = &tea5764_ioctl_ops,
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300452 .release = video_device_release_empty,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300453};
454
455/* I2C probe: check if the device exists and register with v4l if it is */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800456static int tea5764_i2c_probe(struct i2c_client *client,
457 const struct i2c_device_id *id)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300458{
459 struct tea5764_device *radio;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300460 struct v4l2_device *v4l2_dev;
Hans Verkuil16b37172013-02-03 08:34:18 -0300461 struct v4l2_ctrl_handler *hdl;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300462 struct tea5764_regs *r;
463 int ret;
464
465 PDEBUG("probe");
Hans Verkuilee71e422010-11-14 09:46:23 -0300466 radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300467 if (!radio)
468 return -ENOMEM;
469
Hans Verkuil099f88e2013-02-03 08:25:32 -0300470 v4l2_dev = &radio->v4l2_dev;
471 ret = v4l2_device_register(&client->dev, v4l2_dev);
472 if (ret < 0) {
473 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
474 goto errfr;
475 }
Hans Verkuil16b37172013-02-03 08:34:18 -0300476
477 hdl = &radio->ctrl_handler;
478 v4l2_ctrl_handler_init(hdl, 1);
479 v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops,
480 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
481 v4l2_dev->ctrl_handler = hdl;
482 if (hdl->error) {
483 ret = hdl->error;
484 v4l2_err(v4l2_dev, "Could not register controls\n");
485 goto errunreg;
486 }
487
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300488 mutex_init(&radio->mutex);
489 radio->i2c_client = client;
490 ret = tea5764_i2c_read(radio);
491 if (ret)
Hans Verkuil099f88e2013-02-03 08:25:32 -0300492 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300493 r = &radio->regs;
494 PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
495 if (r->chipid != TEA5764_CHIPID ||
496 (r->manid & 0x0fff) != TEA5764_MANID) {
497 PWARN("This chip is not a TEA5764!");
498 ret = -EINVAL;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300499 goto errunreg;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300500 }
501
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300502 radio->vdev = tea5764_radio_template;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300503
504 i2c_set_clientdata(client, radio);
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300505 video_set_drvdata(&radio->vdev, radio);
506 radio->vdev.lock = &radio->mutex;
507 radio->vdev.v4l2_dev = v4l2_dev;
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300508
509 /* initialize and power off the chip */
510 tea5764_i2c_read(radio);
511 tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
512 tea5764_mute(radio, 1);
513 tea5764_power_down(radio);
514
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300515 ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr);
Hans Verkuilee71e422010-11-14 09:46:23 -0300516 if (ret < 0) {
517 PWARN("Could not register video device!");
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300518 goto errunreg;
Hans Verkuilee71e422010-11-14 09:46:23 -0300519 }
520
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300521 PINFO("registered.");
522 return 0;
Hans Verkuil099f88e2013-02-03 08:25:32 -0300523errunreg:
Hans Verkuil16b37172013-02-03 08:34:18 -0300524 v4l2_ctrl_handler_free(hdl);
Hans Verkuil099f88e2013-02-03 08:25:32 -0300525 v4l2_device_unregister(v4l2_dev);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300526errfr:
527 kfree(radio);
528 return ret;
529}
530
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800531static int tea5764_i2c_remove(struct i2c_client *client)
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300532{
533 struct tea5764_device *radio = i2c_get_clientdata(client);
534
535 PDEBUG("remove");
536 if (radio) {
537 tea5764_power_down(radio);
Hans Verkuilcf9033f2013-02-03 08:27:57 -0300538 video_unregister_device(&radio->vdev);
Hans Verkuil16b37172013-02-03 08:34:18 -0300539 v4l2_ctrl_handler_free(&radio->ctrl_handler);
Hans Verkuil099f88e2013-02-03 08:25:32 -0300540 v4l2_device_unregister(&radio->v4l2_dev);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300541 kfree(radio);
542 }
543 return 0;
544}
545
546/* I2C subsystem interface */
547static const struct i2c_device_id tea5764_id[] = {
548 { "radio-tea5764", 0 },
549 { } /* Terminating entry */
550};
551MODULE_DEVICE_TABLE(i2c, tea5764_id);
552
553static struct i2c_driver tea5764_i2c_driver = {
554 .driver = {
555 .name = "radio-tea5764",
556 .owner = THIS_MODULE,
557 },
558 .probe = tea5764_i2c_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800559 .remove = tea5764_i2c_remove,
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300560 .id_table = tea5764_id,
561};
562
Axel Linc6e8d862012-02-12 06:56:32 -0300563module_i2c_driver(tea5764_i2c_driver);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300564
565MODULE_AUTHOR(DRIVER_AUTHOR);
566MODULE_DESCRIPTION(DRIVER_DESC);
567MODULE_LICENSE("GPL");
Mauro Carvalho Chehab29834c12011-06-25 10:15:42 -0300568MODULE_VERSION(DRIVER_VERSION);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300569
Jean Delvare731884b2011-07-08 06:00:37 -0300570module_param(use_xtal, int, 0);
Fabio Belavenuto46a60cf2008-12-30 19:27:09 -0300571MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
572module_param(radio_nr, int, 0);
573MODULE_PARM_DESC(radio_nr, "video4linux device number to use");