media: media/radio: set device_caps in struct video_device
[sfrench/cifs-2.6.git] / drivers / media / radio / radio-tea5764.c
1 /*
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  * History:
23  * 2008-12-06   Fabio Belavenuto <belavenuto@gmail.com>
24  *              initial code
25  *
26  * TODO:
27  *  add platform_data support for IRQs platform dependencies
28  *  add RDS support
29  */
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/init.h>                 /* Initdata                     */
34 #include <linux/videodev2.h>            /* kernel radio structs         */
35 #include <linux/i2c.h>                  /* I2C                          */
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-device.h>
39 #include <media/v4l2-ctrls.h>
40 #include <media/v4l2-event.h>
41
42 #define DRIVER_VERSION  "0.0.2"
43
44 #define DRIVER_AUTHOR   "Fabio Belavenuto <belavenuto@gmail.com>"
45 #define DRIVER_DESC     "A driver for the TEA5764 radio chip for EZX Phones."
46
47 #define PINFO(format, ...)\
48         printk(KERN_INFO KBUILD_MODNAME ": "\
49                 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
50 #define PWARN(format, ...)\
51         printk(KERN_WARNING KBUILD_MODNAME ": "\
52                 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
53 # define PDEBUG(format, ...)\
54         printk(KERN_DEBUG KBUILD_MODNAME ": "\
55                 DRIVER_VERSION ": " format "\n", ## __VA_ARGS__)
56
57 /* Frequency limits in MHz -- these are European values.  For Japanese
58 devices, that would be 76000 and 91000.  */
59 #define FREQ_MIN  87500U
60 #define FREQ_MAX 108000U
61 #define FREQ_MUL 16
62
63 /* TEA5764 registers */
64 #define TEA5764_MANID           0x002b
65 #define TEA5764_CHIPID          0x5764
66
67 #define TEA5764_INTREG_BLMSK    0x0001
68 #define TEA5764_INTREG_FRRMSK   0x0002
69 #define TEA5764_INTREG_LEVMSK   0x0008
70 #define TEA5764_INTREG_IFMSK    0x0010
71 #define TEA5764_INTREG_BLMFLAG  0x0100
72 #define TEA5764_INTREG_FRRFLAG  0x0200
73 #define TEA5764_INTREG_LEVFLAG  0x0800
74 #define TEA5764_INTREG_IFFLAG   0x1000
75
76 #define TEA5764_FRQSET_SUD      0x8000
77 #define TEA5764_FRQSET_SM       0x4000
78
79 #define TEA5764_TNCTRL_PUPD1    0x8000
80 #define TEA5764_TNCTRL_PUPD0    0x4000
81 #define TEA5764_TNCTRL_BLIM     0x2000
82 #define TEA5764_TNCTRL_SWPM     0x1000
83 #define TEA5764_TNCTRL_IFCTC    0x0800
84 #define TEA5764_TNCTRL_AFM      0x0400
85 #define TEA5764_TNCTRL_SMUTE    0x0200
86 #define TEA5764_TNCTRL_SNC      0x0100
87 #define TEA5764_TNCTRL_MU       0x0080
88 #define TEA5764_TNCTRL_SSL1     0x0040
89 #define TEA5764_TNCTRL_SSL0     0x0020
90 #define TEA5764_TNCTRL_HLSI     0x0010
91 #define TEA5764_TNCTRL_MST      0x0008
92 #define TEA5764_TNCTRL_SWP      0x0004
93 #define TEA5764_TNCTRL_DTC      0x0002
94 #define TEA5764_TNCTRL_AHLSI    0x0001
95
96 #define TEA5764_TUNCHK_LEVEL(x) (((x) & 0x00F0) >> 4)
97 #define TEA5764_TUNCHK_IFCNT(x) (((x) & 0xFE00) >> 9)
98 #define TEA5764_TUNCHK_TUNTO    0x0100
99 #define TEA5764_TUNCHK_LD       0x0008
100 #define TEA5764_TUNCHK_STEREO   0x0004
101
102 #define TEA5764_TESTREG_TRIGFR  0x0800
103
104 struct tea5764_regs {
105         u16 intreg;                             /* INTFLAG & INTMSK */
106         u16 frqset;                             /* FRQSETMSB & FRQSETLSB */
107         u16 tnctrl;                             /* TNCTRL1 & TNCTRL2 */
108         u16 frqchk;                             /* FRQCHKMSB & FRQCHKLSB */
109         u16 tunchk;                             /* IFCHK & LEVCHK */
110         u16 testreg;                            /* TESTBITS & TESTMODE */
111         u16 rdsstat;                            /* RDSSTAT1 & RDSSTAT2 */
112         u16 rdslb;                              /* RDSLBMSB & RDSLBLSB */
113         u16 rdspb;                              /* RDSPBMSB & RDSPBLSB */
114         u16 rdsbc;                              /* RDSBBC & RDSGBC */
115         u16 rdsctrl;                            /* RDSCTRL1 & RDSCTRL2 */
116         u16 rdsbbl;                             /* PAUSEDET & RDSBBL */
117         u16 manid;                              /* MANID1 & MANID2 */
118         u16 chipid;                             /* CHIPID1 & CHIPID2 */
119 } __attribute__ ((packed));
120
121 struct tea5764_write_regs {
122         u8 intreg;                              /* INTMSK */
123         __be16 frqset;                          /* FRQSETMSB & FRQSETLSB */
124         __be16 tnctrl;                          /* TNCTRL1 & TNCTRL2 */
125         __be16 testreg;                         /* TESTBITS & TESTMODE */
126         __be16 rdsctrl;                         /* RDSCTRL1 & RDSCTRL2 */
127         __be16 rdsbbl;                          /* PAUSEDET & RDSBBL */
128 } __attribute__ ((packed));
129
130 #ifdef CONFIG_RADIO_TEA5764_XTAL
131 #define RADIO_TEA5764_XTAL 1
132 #else
133 #define RADIO_TEA5764_XTAL 0
134 #endif
135
136 static int radio_nr = -1;
137 static int use_xtal = RADIO_TEA5764_XTAL;
138
139 struct tea5764_device {
140         struct v4l2_device              v4l2_dev;
141         struct v4l2_ctrl_handler        ctrl_handler;
142         struct i2c_client               *i2c_client;
143         struct video_device             vdev;
144         struct tea5764_regs             regs;
145         struct mutex                    mutex;
146 };
147
148 /* I2C code related */
149 static int tea5764_i2c_read(struct tea5764_device *radio)
150 {
151         int i;
152         u16 *p = (u16 *) &radio->regs;
153
154         struct i2c_msg msgs[1] = {
155                 {       .addr = radio->i2c_client->addr,
156                         .flags = I2C_M_RD,
157                         .len = sizeof(radio->regs),
158                         .buf = (void *)&radio->regs
159                 },
160         };
161         if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
162                 return -EIO;
163         for (i = 0; i < sizeof(struct tea5764_regs) / sizeof(u16); i++)
164                 p[i] = __be16_to_cpu((__force __be16)p[i]);
165
166         return 0;
167 }
168
169 static int tea5764_i2c_write(struct tea5764_device *radio)
170 {
171         struct tea5764_write_regs wr;
172         struct tea5764_regs *r = &radio->regs;
173         struct i2c_msg msgs[1] = {
174                 {
175                         .addr = radio->i2c_client->addr,
176                         .len = sizeof(wr),
177                         .buf = (void *)&wr
178                 },
179         };
180         wr.intreg  = r->intreg & 0xff;
181         wr.frqset  = __cpu_to_be16(r->frqset);
182         wr.tnctrl  = __cpu_to_be16(r->tnctrl);
183         wr.testreg = __cpu_to_be16(r->testreg);
184         wr.rdsctrl = __cpu_to_be16(r->rdsctrl);
185         wr.rdsbbl  = __cpu_to_be16(r->rdsbbl);
186         if (i2c_transfer(radio->i2c_client->adapter, msgs, 1) != 1)
187                 return -EIO;
188         return 0;
189 }
190
191 static void tea5764_power_up(struct tea5764_device *radio)
192 {
193         struct tea5764_regs *r = &radio->regs;
194
195         if (!(r->tnctrl & TEA5764_TNCTRL_PUPD0)) {
196                 r->tnctrl &= ~(TEA5764_TNCTRL_AFM | TEA5764_TNCTRL_MU |
197                                TEA5764_TNCTRL_HLSI);
198                 if (!use_xtal)
199                         r->testreg |= TEA5764_TESTREG_TRIGFR;
200                 else
201                         r->testreg &= ~TEA5764_TESTREG_TRIGFR;
202
203                 r->tnctrl |= TEA5764_TNCTRL_PUPD0;
204                 tea5764_i2c_write(radio);
205         }
206 }
207
208 static void tea5764_power_down(struct tea5764_device *radio)
209 {
210         struct tea5764_regs *r = &radio->regs;
211
212         if (r->tnctrl & TEA5764_TNCTRL_PUPD0) {
213                 r->tnctrl &= ~TEA5764_TNCTRL_PUPD0;
214                 tea5764_i2c_write(radio);
215         }
216 }
217
218 static void tea5764_set_freq(struct tea5764_device *radio, int freq)
219 {
220         struct tea5764_regs *r = &radio->regs;
221
222         /* formula: (freq [+ or -] 225000) / 8192 */
223         if (r->tnctrl & TEA5764_TNCTRL_HLSI)
224                 r->frqset = (freq + 225000) / 8192;
225         else
226                 r->frqset = (freq - 225000) / 8192;
227 }
228
229 static int tea5764_get_freq(struct tea5764_device *radio)
230 {
231         struct tea5764_regs *r = &radio->regs;
232
233         if (r->tnctrl & TEA5764_TNCTRL_HLSI)
234                 return (r->frqchk * 8192) - 225000;
235         else
236                 return (r->frqchk * 8192) + 225000;
237 }
238
239 /* tune an frequency, freq is defined by v4l's TUNER_LOW, i.e. 1/16th kHz */
240 static void tea5764_tune(struct tea5764_device *radio, int freq)
241 {
242         tea5764_set_freq(radio, freq);
243         if (tea5764_i2c_write(radio))
244                 PWARN("Could not set frequency!");
245 }
246
247 static void tea5764_set_audout_mode(struct tea5764_device *radio, int audmode)
248 {
249         struct tea5764_regs *r = &radio->regs;
250         int tnctrl = r->tnctrl;
251
252         if (audmode == V4L2_TUNER_MODE_MONO)
253                 r->tnctrl |= TEA5764_TNCTRL_MST;
254         else
255                 r->tnctrl &= ~TEA5764_TNCTRL_MST;
256         if (tnctrl != r->tnctrl)
257                 tea5764_i2c_write(radio);
258 }
259
260 static int tea5764_get_audout_mode(struct tea5764_device *radio)
261 {
262         struct tea5764_regs *r = &radio->regs;
263
264         if (r->tnctrl & TEA5764_TNCTRL_MST)
265                 return V4L2_TUNER_MODE_MONO;
266         else
267                 return V4L2_TUNER_MODE_STEREO;
268 }
269
270 static void tea5764_mute(struct tea5764_device *radio, int on)
271 {
272         struct tea5764_regs *r = &radio->regs;
273         int tnctrl = r->tnctrl;
274
275         if (on)
276                 r->tnctrl |= TEA5764_TNCTRL_MU;
277         else
278                 r->tnctrl &= ~TEA5764_TNCTRL_MU;
279         if (tnctrl != r->tnctrl)
280                 tea5764_i2c_write(radio);
281 }
282
283 /* V4L2 vidioc */
284 static int vidioc_querycap(struct file *file, void  *priv,
285                                         struct v4l2_capability *v)
286 {
287         struct tea5764_device *radio = video_drvdata(file);
288         struct video_device *dev = &radio->vdev;
289
290         strscpy(v->driver, dev->dev.driver->name, sizeof(v->driver));
291         strscpy(v->card, dev->name, sizeof(v->card));
292         snprintf(v->bus_info, sizeof(v->bus_info),
293                  "I2C:%s", dev_name(&dev->dev));
294         return 0;
295 }
296
297 static int vidioc_g_tuner(struct file *file, void *priv,
298                                 struct v4l2_tuner *v)
299 {
300         struct tea5764_device *radio = video_drvdata(file);
301         struct tea5764_regs *r = &radio->regs;
302
303         if (v->index > 0)
304                 return -EINVAL;
305
306         strscpy(v->name, "FM", sizeof(v->name));
307         v->type = V4L2_TUNER_RADIO;
308         tea5764_i2c_read(radio);
309         v->rangelow   = FREQ_MIN * FREQ_MUL;
310         v->rangehigh  = FREQ_MAX * FREQ_MUL;
311         v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
312         if (r->tunchk & TEA5764_TUNCHK_STEREO)
313                 v->rxsubchans = V4L2_TUNER_SUB_STEREO;
314         else
315                 v->rxsubchans = V4L2_TUNER_SUB_MONO;
316         v->audmode = tea5764_get_audout_mode(radio);
317         v->signal = TEA5764_TUNCHK_LEVEL(r->tunchk) * 0xffff / 0xf;
318         v->afc = TEA5764_TUNCHK_IFCNT(r->tunchk);
319
320         return 0;
321 }
322
323 static int vidioc_s_tuner(struct file *file, void *priv,
324                                 const struct v4l2_tuner *v)
325 {
326         struct tea5764_device *radio = video_drvdata(file);
327
328         if (v->index > 0)
329                 return -EINVAL;
330
331         tea5764_set_audout_mode(radio, v->audmode);
332         return 0;
333 }
334
335 static int vidioc_s_frequency(struct file *file, void *priv,
336                                 const struct v4l2_frequency *f)
337 {
338         struct tea5764_device *radio = video_drvdata(file);
339         unsigned freq = f->frequency;
340
341         if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
342                 return -EINVAL;
343         if (freq == 0) {
344                 /* We special case this as a power down control. */
345                 tea5764_power_down(radio);
346                 /* Yes, that's what is returned in this case. This
347                    whole special case is non-compliant and should really
348                    be replaced with something better, but changing this
349                    might well break code that depends on this behavior.
350                    So we keep it as-is. */
351                 return -EINVAL;
352         }
353         freq = clamp(freq, FREQ_MIN * FREQ_MUL, FREQ_MAX * FREQ_MUL);
354         tea5764_power_up(radio);
355         tea5764_tune(radio, (freq * 125) / 2);
356         return 0;
357 }
358
359 static int vidioc_g_frequency(struct file *file, void *priv,
360                                 struct v4l2_frequency *f)
361 {
362         struct tea5764_device *radio = video_drvdata(file);
363         struct tea5764_regs *r = &radio->regs;
364
365         if (f->tuner != 0)
366                 return -EINVAL;
367         tea5764_i2c_read(radio);
368         f->type = V4L2_TUNER_RADIO;
369         if (r->tnctrl & TEA5764_TNCTRL_PUPD0)
370                 f->frequency = (tea5764_get_freq(radio) * 2) / 125;
371         else
372                 f->frequency = 0;
373
374         return 0;
375 }
376
377 static int tea5764_s_ctrl(struct v4l2_ctrl *ctrl)
378 {
379         struct tea5764_device *radio =
380                 container_of(ctrl->handler, struct tea5764_device, ctrl_handler);
381
382         switch (ctrl->id) {
383         case V4L2_CID_AUDIO_MUTE:
384                 tea5764_mute(radio, ctrl->val);
385                 return 0;
386         }
387         return -EINVAL;
388 }
389
390 static const struct v4l2_ctrl_ops tea5764_ctrl_ops = {
391         .s_ctrl = tea5764_s_ctrl,
392 };
393
394 /* File system interface */
395 static const struct v4l2_file_operations tea5764_fops = {
396         .owner          = THIS_MODULE,
397         .open           = v4l2_fh_open,
398         .release        = v4l2_fh_release,
399         .poll           = v4l2_ctrl_poll,
400         .unlocked_ioctl = video_ioctl2,
401 };
402
403 static const struct v4l2_ioctl_ops tea5764_ioctl_ops = {
404         .vidioc_querycap    = vidioc_querycap,
405         .vidioc_g_tuner     = vidioc_g_tuner,
406         .vidioc_s_tuner     = vidioc_s_tuner,
407         .vidioc_g_frequency = vidioc_g_frequency,
408         .vidioc_s_frequency = vidioc_s_frequency,
409         .vidioc_log_status  = v4l2_ctrl_log_status,
410         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
411         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
412 };
413
414 /* V4L2 interface */
415 static const struct video_device tea5764_radio_template = {
416         .name           = "TEA5764 FM-Radio",
417         .fops           = &tea5764_fops,
418         .ioctl_ops      = &tea5764_ioctl_ops,
419         .release        = video_device_release_empty,
420 };
421
422 /* I2C probe: check if the device exists and register with v4l if it is */
423 static int tea5764_i2c_probe(struct i2c_client *client,
424                              const struct i2c_device_id *id)
425 {
426         struct tea5764_device *radio;
427         struct v4l2_device *v4l2_dev;
428         struct v4l2_ctrl_handler *hdl;
429         struct tea5764_regs *r;
430         int ret;
431
432         PDEBUG("probe");
433         radio = kzalloc(sizeof(struct tea5764_device), GFP_KERNEL);
434         if (!radio)
435                 return -ENOMEM;
436
437         v4l2_dev = &radio->v4l2_dev;
438         ret = v4l2_device_register(&client->dev, v4l2_dev);
439         if (ret < 0) {
440                 v4l2_err(v4l2_dev, "could not register v4l2_device\n");
441                 goto errfr;
442         }
443
444         hdl = &radio->ctrl_handler;
445         v4l2_ctrl_handler_init(hdl, 1);
446         v4l2_ctrl_new_std(hdl, &tea5764_ctrl_ops,
447                         V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
448         v4l2_dev->ctrl_handler = hdl;
449         if (hdl->error) {
450                 ret = hdl->error;
451                 v4l2_err(v4l2_dev, "Could not register controls\n");
452                 goto errunreg;
453         }
454
455         mutex_init(&radio->mutex);
456         radio->i2c_client = client;
457         ret = tea5764_i2c_read(radio);
458         if (ret)
459                 goto errunreg;
460         r = &radio->regs;
461         PDEBUG("chipid = %04X, manid = %04X", r->chipid, r->manid);
462         if (r->chipid != TEA5764_CHIPID ||
463                 (r->manid & 0x0fff) != TEA5764_MANID) {
464                 PWARN("This chip is not a TEA5764!");
465                 ret = -EINVAL;
466                 goto errunreg;
467         }
468
469         radio->vdev = tea5764_radio_template;
470
471         i2c_set_clientdata(client, radio);
472         video_set_drvdata(&radio->vdev, radio);
473         radio->vdev.lock = &radio->mutex;
474         radio->vdev.v4l2_dev = v4l2_dev;
475         radio->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
476
477         /* initialize and power off the chip */
478         tea5764_i2c_read(radio);
479         tea5764_set_audout_mode(radio, V4L2_TUNER_MODE_STEREO);
480         tea5764_mute(radio, 1);
481         tea5764_power_down(radio);
482
483         ret = video_register_device(&radio->vdev, VFL_TYPE_RADIO, radio_nr);
484         if (ret < 0) {
485                 PWARN("Could not register video device!");
486                 goto errunreg;
487         }
488
489         PINFO("registered.");
490         return 0;
491 errunreg:
492         v4l2_ctrl_handler_free(hdl);
493         v4l2_device_unregister(v4l2_dev);
494 errfr:
495         kfree(radio);
496         return ret;
497 }
498
499 static int tea5764_i2c_remove(struct i2c_client *client)
500 {
501         struct tea5764_device *radio = i2c_get_clientdata(client);
502
503         PDEBUG("remove");
504         if (radio) {
505                 tea5764_power_down(radio);
506                 video_unregister_device(&radio->vdev);
507                 v4l2_ctrl_handler_free(&radio->ctrl_handler);
508                 v4l2_device_unregister(&radio->v4l2_dev);
509                 kfree(radio);
510         }
511         return 0;
512 }
513
514 /* I2C subsystem interface */
515 static const struct i2c_device_id tea5764_id[] = {
516         { "radio-tea5764", 0 },
517         { }                                     /* Terminating entry */
518 };
519 MODULE_DEVICE_TABLE(i2c, tea5764_id);
520
521 static struct i2c_driver tea5764_i2c_driver = {
522         .driver = {
523                 .name = "radio-tea5764",
524         },
525         .probe = tea5764_i2c_probe,
526         .remove = tea5764_i2c_remove,
527         .id_table = tea5764_id,
528 };
529
530 module_i2c_driver(tea5764_i2c_driver);
531
532 MODULE_AUTHOR(DRIVER_AUTHOR);
533 MODULE_DESCRIPTION(DRIVER_DESC);
534 MODULE_LICENSE("GPL");
535 MODULE_VERSION(DRIVER_VERSION);
536
537 module_param(use_xtal, int, 0);
538 MODULE_PARM_DESC(use_xtal, "Chip have a xtal connected in board");
539 module_param(radio_nr, int, 0);
540 MODULE_PARM_DESC(radio_nr, "video4linux device number to use");