]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/radio/radio-tea5761.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / media / radio / radio-tea5761.c
1 /*
2  * drivers/media/radio/radio-tea5761.c
3  *
4  * Copyright (C) 2005 Nokia Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/init.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <media/v4l2-common.h>
26 #include <asm/arch/gpio.h>
27 #include <asm/arch/board.h>
28
29 #define DRIVER_NAME "tea5761"
30
31 #define TEA5761_VERSION         KERNEL_VERSION(0, 0, 1)
32
33 #define TEA5761_I2C_ADDR        0x10
34
35 #define TEA5761_MANID           0x002b
36 #define TEA5761_CHIPID          0x5761
37
38 #define TEA5761_INTREG_BLMSK    0x0001
39 #define TEA5761_INTREG_FRRMSK   0x0002
40 #define TEA5761_INTREG_LEVMSK   0x0008
41 #define TEA5761_INTREG_IFMSK    0x0010
42 #define TEA5761_INTREG_BLMFLAG  0x0100
43 #define TEA5761_INTREG_FRRFLAG  0x0200
44 #define TEA5761_INTREG_LEVFLAG  0x0800
45 #define TEA5761_INTREG_IFFLAG   0x1000
46
47 #define TEA5761_FRQSET_SUD      0x8000
48 #define TEA5761_FRQSET_SM       0x4000
49
50 #define TEA5761_TNCTRL_PUPD0    0x4000
51 #define TEA5761_TNCTRL_BLIM     0x2000
52 #define TEA5761_TNCTRL_SWPM     0x1000
53 #define TEA5761_TNCTRL_IFCTC    0x0800
54 #define TEA5761_TNCTRL_AFM      0x0400
55 #define TEA5761_TNCTRL_SMUTE    0x0200
56 #define TEA5761_TNCTRL_SNC      0x0100
57 #define TEA5761_TNCTRL_MU       0x0080
58 #define TEA5761_TNCTRL_SSL1     0x0040
59 #define TEA5761_TNCTRL_SSL0     0x0020
60 #define TEA5761_TNCTRL_HLSI     0x0010
61 #define TEA5761_TNCTRL_MST      0x0008
62 #define TEA5761_TNCTRL_SWP      0x0004
63 #define TEA5761_TNCTRL_DTC      0x0002
64 #define TEA5761_TNCTRL_AHLSI    0x0001
65
66 #define TEA5761_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
67 #define TEA5761_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
68 #define TEA5761_TUNCHK_TUNTO    0x0100
69 #define TEA5761_TUNCHK_LD       0x0008
70 #define TEA5761_TUNCHK_STEREO   0x0004
71
72 #define TEA5761_TESTREG_TRIGFR  0x0800
73
74 #define TEA5761_FREQ_LOW        87500
75 #define TEA5761_FREQ_HIGH       108000
76
77 /* Probe for TEA5761 twice since the version N4B seems to be
78  * broken and needs two probes to be found */
79 static unsigned short normal_i2c[] = {
80         TEA5761_I2C_ADDR, TEA5761_I2C_ADDR, I2C_CLIENT_END
81 };
82
83 I2C_CLIENT_INSMOD;
84
85 struct tea5761_regs {
86         u16 intreg;
87         u16 frqset;
88         u16 tnctrl;
89         u16 frqchk;
90         u16 tunchk;
91         u16 testreg;
92         u16 manid;
93         u16 chipid;
94 } __attribute__ ((packed));
95
96 struct tea5761_write_regs {
97         u8 intreg;
98         u16 frqset;
99         u16 tnctrl;
100         u16 testreg;
101 } __attribute__ ((packed));
102
103 struct tea5761_device {
104         struct video_device     *video_dev;
105         struct i2c_client       *i2c_dev;
106         struct tea5761_regs     regs;
107         struct mutex            mutex;
108         int                     users;
109 };
110
111 static struct tea5761_device tea5761;
112
113 static struct i2c_driver        tea5761_driver;
114 static int radio_nr = -1;
115
116 static int tea5761_read_regs(struct tea5761_device *tea)
117 {
118         int rc, i;
119         u16 *p = (u16 *) &tea->regs;
120         struct i2c_client *client = tea->i2c_dev;
121
122         rc = i2c_master_recv(client, (void*) &tea->regs, sizeof(tea->regs));
123         for (i = 0; i < 8; i++) {
124                 p[i] = __be16_to_cpu(p[i]);
125         }
126
127         dev_dbg(&client->dev,
128                 "chip state: %04x %04x %04x %04x %04x %04x %04x %04x\n",
129                 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
130
131         if (rc < 0)
132                 dev_err(&client->dev, "read\n");
133
134         return rc;
135 }
136
137 static void tea5761_write_regs(struct tea5761_device *tea)
138 {
139         struct tea5761_write_regs wr;
140         struct tea5761_regs *r = &tea->regs;
141         struct i2c_client *client = tea->i2c_dev;
142         u8 *p = (u8 *) r;
143
144         wr.intreg = r->intreg & 0xff;
145         wr.frqset = __cpu_to_be16(r->frqset);
146         wr.tnctrl = __cpu_to_be16(r->tnctrl);
147         wr.testreg = __cpu_to_be16(r->testreg);
148
149         dev_dbg(&client->dev,
150                 "writing state: %02x %02x %02x %02x %02x %02x %02x\n",
151                 p[0], p[1], p[2], p[3], p[4], p[5], p[6]);
152         if (i2c_master_send(client, (void *) &wr, sizeof(wr)) < 0)
153                 dev_err(&client->dev, "write\n");
154 }
155
156 static void tea5761_power_up(struct tea5761_device *tea)
157 {
158         struct tea5761_regs *r = &tea->regs;
159
160         if (!(r->tnctrl & TEA5761_TNCTRL_PUPD0)) {
161                 r->tnctrl &= ~(TEA5761_TNCTRL_AFM | TEA5761_TNCTRL_MU |
162                                TEA5761_TNCTRL_HLSI);
163                 r->testreg |= TEA5761_TESTREG_TRIGFR;
164                 r->tnctrl |= TEA5761_TNCTRL_PUPD0;
165                 return tea5761_write_regs(tea);
166         }
167 }
168
169 static void tea5761_power_down(struct tea5761_device *tea)
170 {
171         struct tea5761_regs *r = &tea->regs;
172
173         if (r->tnctrl & TEA5761_TNCTRL_PUPD0) {
174                 r->tnctrl &= ~TEA5761_TNCTRL_PUPD0;
175                 return tea5761_write_regs(tea);
176         }
177 }
178
179 static void tea5761_set_freq(struct tea5761_device *tea, int freq)
180 {
181         struct tea5761_regs *r = &tea->regs;
182
183         if (r->tnctrl & TEA5761_TNCTRL_HLSI)
184                 r->frqset = (freq + 225000) / 8192;
185         else
186                 r->frqset = (freq - 225000) / 8192;
187 }
188
189 static int tea5761_get_freq(struct tea5761_device *tea)
190 {
191         struct tea5761_regs *r = &tea->regs;
192
193         if (r->tnctrl & TEA5761_TNCTRL_HLSI)
194                 return (r->frqchk * 8192) - 225000;
195         else
196                 return (r->frqchk * 8192) + 225000;
197 }
198
199 static void tea5761_tune(struct tea5761_device *tea, int freq)
200 {
201         tea5761_set_freq(tea, freq);
202         tea5761_write_regs(tea);
203 }
204
205 static void tea5761_set_audout_mode(struct tea5761_device *tea, int audmode)
206 {
207         struct tea5761_regs *r = &tea->regs;
208         int tnctrl = r->tnctrl;
209
210         if (audmode == V4L2_TUNER_MODE_MONO)
211                 r->tnctrl |= TEA5761_TNCTRL_MST;
212         else
213                 r->tnctrl &= ~TEA5761_TNCTRL_MST;
214         if (tnctrl != r->tnctrl)
215                 tea5761_write_regs(tea);
216 }
217
218 static int tea5761_get_audout_mode(struct tea5761_device *tea)
219 {
220         struct tea5761_regs *r = &tea->regs;
221
222         if (r->tnctrl & TEA5761_TNCTRL_MST)
223                 return V4L2_TUNER_MODE_MONO;
224         else
225                 return V4L2_TUNER_MODE_STEREO;
226 }
227
228 static void tea5761_mute(struct tea5761_device *tea, int on)
229 {
230         struct tea5761_regs *r = &tea->regs;
231         int tnctrl = r->tnctrl;
232
233         if (on)
234                 r->tnctrl |= TEA5761_TNCTRL_MU;
235         else
236                 r->tnctrl &= ~TEA5761_TNCTRL_MU;
237         if (tnctrl != r->tnctrl)
238                 tea5761_write_regs(tea);
239 }
240
241 static int tea5761_is_muted(struct tea5761_device *tea)
242 {
243         return tea->regs.tnctrl & TEA5761_TNCTRL_MU;
244 }
245
246 static int tea5761_do_ioctl(struct inode *inode, struct file *file,
247                             unsigned int cmd, void *arg)
248 {
249         struct tea5761_device *tea = file->private_data;
250         struct video_device *dev = tea->video_dev;
251         struct i2c_client *client = tea->i2c_dev;
252         struct tea5761_regs *r = &tea->regs;
253
254         union {
255                 struct v4l2_capability c;
256                 struct v4l2_tuner t;
257                 struct v4l2_frequency f;
258                 struct v4l2_queryctrl qc;
259                 struct v4l2_control ct;
260         } *u = arg;
261
262         tea5761_read_regs(tea);
263
264         switch (cmd) {
265         case VIDIOC_QUERYCAP:
266                 dev_dbg(&client->dev, "VIDIOC_QUERYCAP\n");
267                 memset(&u->c, 0, sizeof(u->c));
268                 strlcpy(u->c.driver, dev->dev->driver->name,
269                         sizeof(u->c.driver));
270                 strlcpy(u->c.card, dev->name, sizeof(u->c.card));
271                 snprintf(u->c.bus_info, sizeof(u->c.bus_info), "I2C:%s",
272                          dev->dev->bus_id);
273                 u->c.version = TEA5761_VERSION;
274                 u->c.capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
275                 break;
276
277         case VIDIOC_G_TUNER:
278                 /* Only one tuner chip */
279                 dev_dbg(&client->dev, "VIDIOC_G_TUNER\n");
280                 if (u->t.index != 0)
281                         return -EINVAL;
282
283                 memset(&u->t, 0, sizeof(u->t));
284                 u->t.type = V4L2_TUNER_RADIO;
285                 strlcpy(u->t.name, "FM", sizeof(u->t.name));
286                 /* Freq in 62.5Hz units */
287                 u->t.rangelow = TEA5761_FREQ_LOW * 16;
288                 u->t.rangehigh = TEA5761_FREQ_HIGH * 16;
289                 u->t.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
290                 if (r->tunchk & TEA5761_TUNCHK_STEREO)
291                         u->t.rxsubchans = V4L2_TUNER_SUB_STEREO;
292                 u->t.audmode = tea5761_get_audout_mode(tea);
293                 u->t.signal = TEA5761_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
294                 u->t.afc = TEA5761_TUNCHK_IFCNT(r->tunchk);
295                 break;
296
297         case VIDIOC_S_TUNER:
298                 /* Only tuner nro 0 can be selected. */
299                 dev_dbg(&client->dev, "VIDIOC_S_TUNER\n");
300                 if (u->t.index != 0)
301                         return -EINVAL;
302                 tea5761_set_audout_mode(tea, u->t.audmode);
303                 break;
304
305         case VIDIOC_G_FREQUENCY:
306                 dev_dbg(&client->dev, "VIDIOC_G_FREQUENCY\n");
307                 memset(&u->f, 0, sizeof(u->f));
308                 u->f.type = V4L2_TUNER_RADIO;
309                 if (r->tnctrl & TEA5761_TNCTRL_PUPD0)
310                         u->f.frequency = (tea5761_get_freq(tea) * 2) / 125;
311                 else
312                         u->f.frequency = 0;
313                 break;
314
315         case VIDIOC_S_FREQUENCY:
316                 dev_dbg(&client->dev, "VIDIOC_S_FREQUENCY %u\n",
317                         u->f.frequency);
318                 if (u->f.tuner != 0)
319                         return -EINVAL;
320                 if (u->f.frequency == 0) {
321                         /* We special case this as a power down
322                          * control. */
323                         tea5761_power_down(tea);
324                         break;
325                 }
326                 if (u->f.frequency < 16 * TEA5761_FREQ_LOW)
327                         return -EINVAL;
328                 if (u->f.frequency > 16 * TEA5761_FREQ_HIGH)
329                         return -EINVAL;
330
331                 tea5761_power_up(tea);
332                 tea5761_tune(tea, (u->f.frequency * 125) / 2);
333                 break;
334
335         case VIDIOC_QUERYCTRL:
336                 dev_dbg(&client->dev, "VIDIOC_QUERYCTRL %d\n", u->qc.id);
337                 if (u->qc.id != V4L2_CID_AUDIO_MUTE)
338                         return -EINVAL;
339                 strlcpy(u->qc.name, "Mute", sizeof(u->qc.name));
340                 u->qc.minimum = 0;
341                 u->qc.maximum = 1;
342                 u->qc.step = 1;
343                 u->qc.default_value = 0;
344                 u->qc.type = V4L2_CTRL_TYPE_BOOLEAN;
345                 break;
346
347         case VIDIOC_G_CTRL:
348                 dev_dbg(&client->dev, "VIDIOC_G_CTRL %d\n", u->ct.id);
349                 if (u->ct.id != V4L2_CID_AUDIO_MUTE)
350                         return -EINVAL;
351                 if (r->tnctrl & TEA5761_TNCTRL_PUPD0)
352                         u->ct.value = tea5761_is_muted(tea) ? 1 : 0;
353                 else
354                         u->ct.value = 0;
355                 break;
356
357         case VIDIOC_S_CTRL:
358                 dev_dbg(&client->dev, "VIDIOC_S_CTRL %d\n", u->ct.id);
359                 if (u->ct.id != V4L2_CID_AUDIO_MUTE)
360                         return -EINVAL;
361                 tea5761_mute(tea, u->ct.value);
362                 break;
363
364         default:
365                 return -ENOIOCTLCMD;
366         }
367
368         return 0;
369 }
370
371 static int tea5761_ioctl(struct inode *inode, struct file *file,
372                          unsigned int cmd, unsigned long arg)
373 {
374         return video_usercopy(inode, file, cmd, arg, tea5761_do_ioctl);
375 }
376
377 static int tea5761_open(struct inode *inode, struct file *file)
378 {
379         int minor = iminor(file->f_dentry->d_inode);
380         /* Currently we support only one device */
381         struct tea5761_device *tea = &tea5761;
382
383         if (tea->video_dev->minor != minor)
384                 return -ENODEV;
385
386         mutex_lock(&tea->mutex);
387         /* Only exclusive access */
388         if (tea->users) {
389                 mutex_unlock(&tea->mutex);
390                 return -EBUSY;
391         }
392         tea->users++;
393         mutex_unlock(&tea->mutex);
394
395         file->private_data = tea;
396         return 0;
397 }
398
399 static int tea5761_release(struct inode *inode, struct file *file)
400 {
401         struct tea5761_device *tea = file->private_data;
402
403         mutex_lock(&tea->mutex);
404         tea->users--;
405         mutex_unlock(&tea->mutex);
406
407         return 0;
408 }
409
410 static struct file_operations tea5761_fops = {
411         .owner          = THIS_MODULE,
412         .open           = tea5761_open,
413         .release        = tea5761_release,
414         .ioctl          = tea5761_ioctl,
415         .llseek         = no_llseek,
416 };
417
418 static struct video_device tea5761_video_device = {
419         .owner         = THIS_MODULE,
420         .name          = "TEA5761 FM-Radio",
421         .type          = VID_TYPE_TUNER,
422         .fops          = &tea5761_fops,
423         .release       = video_device_release
424 };
425
426 static int tea5761_probe(struct i2c_adapter *adapter, int address,
427                           int kind)
428 {
429         struct i2c_client *client;
430         struct video_device *video_dev;
431         int err = 0;
432         static const char *client_name = "TEA5761 FM-Radio";
433         struct tea5761_device *tea = &tea5761;
434         struct tea5761_regs   *r = &tea->regs;
435
436         mutex_init(&tea->mutex);
437         /* I2C detection and initialization */
438         client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
439         if (client == NULL) {
440                 dev_err(&adapter->dev, DRIVER_NAME
441                         ": couldn't allocate memory\n");
442                 return -ENOMEM;
443         }
444         tea->i2c_dev = client;
445
446         client->addr = address;
447         client->adapter = adapter;
448         client->driver = &tea5761_driver;
449         client->dev.driver = &tea5761_driver.driver;
450         client->flags = 0;
451         strlcpy(client->name, client_name, I2C_NAME_SIZE);
452
453         if (kind < 0) {
454                 if (tea5761_read_regs(tea) < 0) {
455                         dev_info(&client->dev,
456                                  "chip read failed for %d-%04x\n",
457                                  adapter->nr, address);
458                         goto err_tea_dev;
459                 }
460                 if (r->chipid != TEA5761_CHIPID) {
461                         dev_info(&client->dev,
462                                  "bad chipid (0x%04x) at %d-%04x\n",
463                                  r->chipid, adapter->nr, address);
464                         goto err_tea_dev;
465                 }
466                 if ((r->manid & 0x0fff) != TEA5761_MANID) {
467                         dev_info(&client->dev,
468                                  "bad manid (0x%04x) at %d-%04x\n",
469                                  r->manid, adapter->nr, address);
470                         goto err_tea_dev;
471                 }
472         }
473
474         err = i2c_attach_client(client);
475         if (err) {
476                 dev_err(&client->dev, "couldn't attach to address %d-%04x\n",
477                         adapter->nr, address);
478                 goto err_tea_dev;
479         }
480
481         /* V4L initialization */
482         video_dev = video_device_alloc();
483         if (video_dev == NULL) {
484                 dev_err(&client->dev, "couldn't allocate memory\n");
485                 err = -ENOMEM;
486                 goto err_i2c_attach;
487         }
488         tea->video_dev = video_dev;
489
490         *video_dev = tea5761_video_device;
491         video_dev->dev = &client->dev;
492         i2c_set_clientdata(client, video_dev);
493
494         /* initialize and power off the chip */
495         tea5761_read_regs(tea);
496         tea5761_set_audout_mode(tea, V4L2_TUNER_MODE_STEREO);
497         tea5761_mute(tea, 0);
498         tea5761_power_down(tea);
499
500         tea5761.video_dev = video_dev;
501         tea5761.i2c_dev = client;
502
503         err = video_register_device(video_dev, VFL_TYPE_RADIO, radio_nr);
504         if (err) {
505                 dev_err(&client->dev, "couldn't register video device\n");
506                 goto err_video_alloc;
507         }
508
509         dev_info(&client->dev, "tea5761 (version %d) detected at %d-%04x\n",
510                 (tea->regs.manid >> 12) & 0xf, adapter->nr, address);
511
512         return 0;
513
514 err_video_alloc:
515         video_device_release(video_dev);
516 err_i2c_attach:
517         i2c_detach_client(client);
518 err_tea_dev:
519         kfree(client);
520         return err;
521 }
522
523 static int tea5761_attach_adapter(struct i2c_adapter *adapter)
524 {
525         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
526                 return -EINVAL;
527
528         return i2c_probe(adapter, &addr_data, tea5761_probe);
529 }
530
531 static int tea5761_detach_client(struct i2c_client *client)
532 {
533         struct video_device *vd = i2c_get_clientdata(client);
534
535         i2c_detach_client(client);
536         video_unregister_device(vd);
537         kfree(client);
538
539         return 0;
540 }
541
542 static struct i2c_driver tea5761_driver = {
543         .id             = I2C_DRIVERID_TUNER,
544         .driver = {
545                 .name   = DRIVER_NAME,
546         },
547         .attach_adapter = tea5761_attach_adapter,
548         .detach_client  = tea5761_detach_client,
549 };
550
551 #if CONFIG_ARCH_OMAP
552 /* No way to pass platform device data. Enable here all the TEA5761
553  * devices, since I2C address scanning will need them to respond.
554  */
555 static int enable_gpio;
556
557 static int __init tea5761_dev_init(void)
558 {
559         const struct omap_tea5761_config *info;
560
561         info = omap_get_config(OMAP_TAG_TEA5761, struct omap_tea5761_config);
562         if (info) {
563                 enable_gpio = info->enable_gpio;
564         }
565
566         if (enable_gpio) {
567                 pr_debug(DRIVER_NAME ": enabling tea5761 at GPIO %d\n",
568                          enable_gpio);
569
570                 if (omap_request_gpio(enable_gpio) < 0) {
571                         printk(KERN_ERR DRIVER_NAME ": can't request GPIO %d\n",
572                                enable_gpio);
573                         return -ENODEV;
574                 }
575
576                 omap_set_gpio_direction(enable_gpio, 0);
577                 udelay(50);
578                 omap_set_gpio_dataout(enable_gpio, 1);
579         }
580
581         return 0;
582 }
583
584 static void __exit tea5761_dev_exit(void)
585 {
586         if (enable_gpio) {
587                 omap_set_gpio_dataout(enable_gpio, 0);
588                 omap_free_gpio(enable_gpio);
589         }
590 }
591 #else
592 static int __init tea5761_dev_init(void)
593 {
594 }
595
596 static void __exit tea5761_dev_exit(void)
597 {
598 }
599 #endif
600
601 static int __init tea5761_init(void)
602 {
603         int res;
604
605         if ((res = tea5761_dev_init()) < 0)
606                 return res;
607
608         if ((res = i2c_add_driver(&tea5761_driver))) {
609                 printk(KERN_ERR DRIVER_NAME ": driver registration failed\n");
610                 return res;
611         }
612
613         return 0;
614 }
615
616 static void __exit tea5761_exit(void)
617 {
618         i2c_del_driver(&tea5761_driver);
619         tea5761_dev_exit();
620 }
621
622 MODULE_AUTHOR("Timo Teräs");
623 MODULE_DESCRIPTION("I2C interface for TEA5761.");
624 MODULE_LICENSE("GPL");
625
626 module_param(radio_nr, int, 0);
627 MODULE_PARM_DESC(nr_radio, "video4linux device number to use");
628
629 module_init(tea5761_init)
630 module_exit(tea5761_exit)