V4L/DVB (13344): soc-camera: properly initialise the device object when reusing
[safe/jmp/linux-2.6] / drivers / media / video / tda7432.c
index 07ba6d3..80f1cee 100644 (file)
@@ -8,6 +8,7 @@
  * Muting and tone control by Jonathan Isom <jisom@ematic.com>
  *
  * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
+ * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  * This code is placed under the terms of the GNU General Public License
  * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
  * Which was based on tda8425.c by Greg Alexander (c) 1998
  * loudness - set between 0 and 15 for varying degrees of loudness effect
  *
  * maxvol   - set maximium volume to +20db (1), default is 0db(0)
- *
- *
- *  Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
- *                             store if muted so we can return it
- *                             change balance only if flaged to
- *  Revision: 0.6 - added tone controls
- *  Revision: 0.5 - Fixed odd balance problem
- *  Revision: 0.4 - added muting
- *  Revision: 0.3 - Fixed silly reversed volume controls.  :)
- *  Revision: 0.2 - Cleaned up #defines
- *                     fixed volume control
- *          Added I2C_DRIVERID_TDA7432
- *                     added loudness insmod control
- *  Revision: 0.1 - initial version
  */
 
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
-#include <linux/sched.h>
 #include <linux/string.h>
 #include <linux/timer.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/slab.h>
-#include <linux/videodev.h>
+#include <linux/videodev2.h>
 #include <linux/i2c.h>
-#include <linux/i2c-algo-bit.h>
 
-#include "bttv.h"
-#include <media/audiochip.h>
-#include <media/id.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ioctl.h>
+#include <media/i2c-addr.h>
+#include <media/v4l2-i2c-drv.h>
 
 #ifndef VIDEO_AUDIO_BALANCE
 # define VIDEO_AUDIO_BALANCE 32
@@ -69,16 +55,11 @@ MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
 module_param(maxvol, int, S_IRUGO | S_IWUSR);
 
 
-/* Address to scan (I2C address of this chip) */
-static unsigned short normal_i2c[] = {
-       I2C_TDA7432 >> 1,
-       I2C_CLIENT_END,
-};
-I2C_CLIENT_INSMOD;
 
 /* Structure of address and subaddresses for the tda7432 */
 
 struct tda7432 {
+       struct v4l2_subdev sd;
        int addr;
        int input;
        int volume;
@@ -86,13 +67,12 @@ struct tda7432 {
        int bass, treble;
        int lf, lr, rf, rr;
        int loud;
-       struct i2c_client c;
 };
-static struct i2c_driver driver;
-static struct i2c_client client_template;
 
-#define dprintk  if (debug) printk
-#define d2printk if (debug > 1) printk
+static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
+{
+       return container_of(sd, struct tda7432, sd);
+}
 
 /* The TDA7432 is made by STS-Thompson
  * http://www.st.com
@@ -227,45 +207,33 @@ static struct i2c_client client_template;
 
 /* Begin code */
 
-static int tda7432_write(struct i2c_client *client, int subaddr, int val)
+static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
 {
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
        unsigned char buffer[2];
-       d2printk("tda7432: In tda7432_write\n");
-       dprintk("tda7432: Writing %d 0x%x\n", subaddr, val);
+
+       v4l2_dbg(2, debug, sd, "In tda7432_write\n");
+       v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
        buffer[0] = subaddr;
        buffer[1] = val;
-       if (2 != i2c_master_send(client,buffer,2)) {
-               printk(KERN_WARNING "tda7432: I/O error, trying (write %d 0x%x)\n",
+       if (2 != i2c_master_send(client, buffer, 2)) {
+               v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
                       subaddr, val);
                return -1;
        }
        return 0;
 }
 
-/* I don't think we ever actually _read_ the chip... */
-#if 0
-static int tda7432_read(struct i2c_client *client)
+static int tda7432_set(struct v4l2_subdev *sd)
 {
-       unsigned char buffer;
-       d2printk("tda7432: In tda7432_read\n");
-       if (1 != i2c_master_recv(client,&buffer,1)) {
-               printk(KERN_WARNING "tda7432: I/O error, trying (read)\n");
-               return -1;
-       }
-       dprintk("tda7432: Read 0x%02x\n", buffer);
-       return buffer;
-}
-#endif
-
-static int tda7432_set(struct i2c_client *client)
-{
-       struct tda7432 *t = i2c_get_clientdata(client);
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       struct tda7432 *t = to_state(sd);
        unsigned char buf[16];
-       d2printk("tda7432: In tda7432_set\n");
 
-       dprintk(KERN_INFO
+       v4l2_dbg(1, debug, sd,
                "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
-               t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
+               t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
+               t->rf, t->rr, t->loud);
        buf[0]  = TDA7432_IN;
        buf[1]  = t->input;
        buf[2]  = t->volume;
@@ -276,18 +244,19 @@ static int tda7432_set(struct i2c_client *client)
        buf[7]  = t->rf;
        buf[8]  = t->rr;
        buf[9]  = t->loud;
-       if (10 != i2c_master_send(client,buf,10)) {
-               printk(KERN_WARNING "tda7432: I/O error, trying tda7432_set\n");
+       if (10 != i2c_master_send(client, buf, 10)) {
+               v4l2_err(sd, "I/O error, trying tda7432_set\n");
                return -1;
        }
 
        return 0;
 }
 
-static void do_tda7432_init(struct i2c_client *client)
+static void do_tda7432_init(struct v4l2_subdev *sd)
 {
-       struct tda7432 *t = i2c_get_clientdata(client);
-       d2printk("tda7432: In tda7432_init\n");
+       struct tda7432 *t = to_state(sd);
+
+       v4l2_dbg(2, debug, sd, "In tda7432_init\n");
 
        t->input  = TDA7432_STEREO_IN |  /* Main (stereo) input   */
                    TDA7432_BASS_SYM  |  /* Symmetric bass cut    */
@@ -295,7 +264,7 @@ static void do_tda7432_init(struct i2c_client *client)
        t->volume =  0x3b ;                              /* -27dB Volume            */
        if (loudness)                    /* Turn loudness on?     */
                t->volume |= TDA7432_LD_ON;
-       t->muted    = VIDEO_AUDIO_MUTE;
+       t->muted    = 1;
        t->treble   = TDA7432_TREBLE_0DB; /* 0dB Treble            */
        t->bass         = TDA7432_BASS_0DB;      /* 0dB Bass              */
        t->lf     = TDA7432_ATTEN_0DB;   /* 0dB attenuation       */
@@ -304,252 +273,208 @@ static void do_tda7432_init(struct i2c_client *client)
        t->rr     = TDA7432_ATTEN_0DB;   /* 0dB attenuation       */
        t->loud   = loudness;            /* insmod parameter      */
 
-       tda7432_set(client);
-}
-
-/* *********************** *
- * i2c interface functions *
- * *********************** */
-
-static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
-{
-       struct tda7432 *t;
-       struct i2c_client *client;
-       d2printk("tda7432: In tda7432_attach\n");
-
-       t = kmalloc(sizeof *t,GFP_KERNEL);
-       if (!t)
-               return -ENOMEM;
-       memset(t,0,sizeof *t);
-
-       client = &t->c;
-        memcpy(client,&client_template,sizeof(struct i2c_client));
-        client->adapter = adap;
-        client->addr = addr;
-       i2c_set_clientdata(client, t);
-
-       do_tda7432_init(client);
-       printk(KERN_INFO "tda7432: init\n");
-
-       i2c_attach_client(client);
-       return 0;
+       tda7432_set(sd);
 }
 
-static int tda7432_probe(struct i2c_adapter *adap)
+static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
-#ifdef I2C_CLASS_TV_ANALOG
-       if (adap->class & I2C_CLASS_TV_ANALOG)
-               return i2c_probe(adap, &addr_data, tda7432_attach);
-#else
-       if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
-               return i2c_probe(adap, &addr_data, tda7432_attach);
-#endif
-       return 0;
-}
+       struct tda7432 *t = to_state(sd);
 
-static int tda7432_detach(struct i2c_client *client)
-{
-       struct tda7432 *t  = i2c_get_clientdata(client);
-
-       do_tda7432_init(client);
-       i2c_detach_client(client);
-
-       kfree(t);
-       return 0;
-}
-
-static int tda7432_command(struct i2c_client *client,
-                          unsigned int cmd, void *arg)
-{
-       struct tda7432 *t = i2c_get_clientdata(client);
-       d2printk("tda7432: In tda7432_command\n");
-
-       switch (cmd) {
-       /* --- v4l ioctls --- */
-       /* take care: bttv does userspace copying, we'll get a
-          kernel pointer here... */
-
-       /* Query card - scale from TDA7432 settings to V4L settings */
-       case VIDIOCGAUDIO:
-       {
-               struct video_audio *va = arg;
-               dprintk("tda7432: VIDIOCGAUDIO\n");
-
-               va->flags |= VIDEO_AUDIO_VOLUME |
-                       VIDEO_AUDIO_BASS |
-                       VIDEO_AUDIO_TREBLE |
-                       VIDEO_AUDIO_MUTABLE;
-               if (t->muted)
-                       va->flags |= VIDEO_AUDIO_MUTE;
-               va->mode |= VIDEO_SOUND_STEREO;
-               /* Master volume control
-                * V4L volume is min 0, max 65535
-                * TDA7432 Volume:
-                * Min (-79dB) is 0x6f
-                * Max (+20dB) is 0x07 (630)
-                * Max (0dB) is 0x20 (829)
-                * (Mask out bit 7 of vol - it's for the loudness setting)
-                */
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               ctrl->value=t->muted;
+               return 0;
+       case V4L2_CID_AUDIO_VOLUME:
                if (!maxvol){  /* max +20db */
-                       va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
+                       ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
                } else {       /* max 0db   */
-                       va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829;
+                       ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
                }
-
-               /* Balance depends on L,R attenuation
-                * V4L balance is 0 to 65535, middle is 32768
-                * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
-                * to scale up to V4L numbers, mult by 1057
-                * attenuation exists for lf, lr, rf, rr
-                * we use only lf and rf (front channels)
-                */
-
+               return 0;
+       case V4L2_CID_AUDIO_BALANCE:
+       {
                if ( (t->lf) < (t->rf) )
                        /* right is attenuated, balance shifted left */
-                       va->balance = (32768 - 1057*(t->rf));
+                       ctrl->value = (32768 - 1057*(t->rf));
                else
                        /* left is attenuated, balance shifted right */
-                       va->balance = (32768 + 1057*(t->lf));
-
+                       ctrl->value = (32768 + 1057*(t->lf));
+               return 0;
+       }
+       case V4L2_CID_AUDIO_BASS:
+       {
                /* Bass/treble 4 bits each */
-               va->bass=t->bass;
-               if(va->bass >= 0x8)
-                       va->bass = ~(va->bass - 0x8) & 0xf;
-               va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
-               va->treble=t->treble;
-               if(va->treble >= 0x8)
-                       va->treble = ~(va->treble - 0x8) & 0xf;
-               va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
-
-               break; /* VIDIOCGAUDIO case */
+               int bass=t->bass;
+               if(bass >= 0x8)
+                       bass = ~(bass - 0x8) & 0xf;
+               ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
+               return 0;
        }
-
-       /* Set card - scale from V4L settings to TDA7432 settings */
-       case VIDIOCSAUDIO:
+       case V4L2_CID_AUDIO_TREBLE:
        {
-               struct video_audio *va = arg;
-               dprintk("tda7432: VIDEOCSAUDIO\n");
+               int treble=t->treble;
+               if(treble >= 0x8)
+                       treble = ~(treble - 0x8) & 0xf;
+               ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
+               return 0;
+       }
+       }
+       return -EINVAL;
+}
 
-               if(va->flags & VIDEO_AUDIO_VOLUME){
-                       if(!maxvol){ /* max +20db */
-                               t->volume = 0x6f - ((va->volume)/630);
-                       } else {    /* max 0db   */
-                               t->volume = 0x6f - ((va->volume)/829);
-                       }
+static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+{
+       struct tda7432 *t = to_state(sd);
 
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               t->muted=ctrl->value;
+               break;
+       case V4L2_CID_AUDIO_VOLUME:
+               if(!maxvol){ /* max +20db */
+                       t->volume = 0x6f - ((ctrl->value)/630);
+               } else {    /* max 0db   */
+                       t->volume = 0x6f - ((ctrl->value)/829);
+               }
                if (loudness)           /* Turn on the loudness bit */
                        t->volume |= TDA7432_LD_ON;
 
-                       tda7432_write(client,TDA7432_VL, t->volume);
-               }
-
-               if(va->flags & VIDEO_AUDIO_BASS)
-               {
-                       t->bass = va->bass >> 12;
-                       if(t->bass>= 0x8)
-                                       t->bass = (~t->bass & 0xf) + 0x8 ;
-               }
-               if(va->flags & VIDEO_AUDIO_TREBLE)
-               {
-                       t->treble= va->treble >> 12;
-                       if(t->treble>= 0x8)
-                                       t->treble = (~t->treble & 0xf) + 0x8 ;
-               }
-               if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
-                       tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
-
-               if(va->flags & VIDEO_AUDIO_BALANCE)     {
-               if (va->balance < 32768)
-               {
+               tda7432_write(sd, TDA7432_VL, t->volume);
+               return 0;
+       case V4L2_CID_AUDIO_BALANCE:
+               if (ctrl->value < 32768) {
                        /* shifted to left, attenuate right */
-                       t->rr = (32768 - va->balance)/1057;
+                       t->rr = (32768 - ctrl->value)/1057;
                        t->rf = t->rr;
                        t->lr = TDA7432_ATTEN_0DB;
                        t->lf = TDA7432_ATTEN_0DB;
-               }
-               else if(va->balance > 32769)
-               {
+               } else if(ctrl->value > 32769) {
                        /* shifted to right, attenuate left */
-                       t->lf = (va->balance - 32768)/1057;
+                       t->lf = (ctrl->value - 32768)/1057;
                        t->lr = t->lf;
                        t->rr = TDA7432_ATTEN_0DB;
                        t->rf = TDA7432_ATTEN_0DB;
-               }
-               else
-               {
+               } else {
                        /* centered */
                        t->rr = TDA7432_ATTEN_0DB;
                        t->rf = TDA7432_ATTEN_0DB;
                        t->lf = TDA7432_ATTEN_0DB;
                        t->lr = TDA7432_ATTEN_0DB;
                }
-               }
-
-               t->muted=(va->flags & VIDEO_AUDIO_MUTE);
-               if (t->muted)
-               {
-                       /* Mute & update balance*/
-                       tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
-                       tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
-                       tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
-                       tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
-               } else {
-                       tda7432_write(client,TDA7432_LF, t->lf);
-                       tda7432_write(client,TDA7432_LR, t->lr);
-                       tda7432_write(client,TDA7432_RF, t->rf);
-                       tda7432_write(client,TDA7432_RR, t->rr);
-               }
-
                break;
+       case V4L2_CID_AUDIO_BASS:
+               t->bass = ctrl->value >> 12;
+               if(t->bass>= 0x8)
+                               t->bass = (~t->bass & 0xf) + 0x8 ;
+
+               tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
+               return 0;
+       case V4L2_CID_AUDIO_TREBLE:
+               t->treble= ctrl->value >> 12;
+               if(t->treble>= 0x8)
+                               t->treble = (~t->treble & 0xf) + 0x8 ;
+
+               tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
+               return 0;
+       default:
+               return -EINVAL;
+       }
 
-       } /* end of VIDEOCSAUDIO case */
-
-       default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
-
-               /* nothing */
-               d2printk("tda7432: Default\n");
-
-       } /* end of (cmd) switch */
-
+       /* Used for both mute and balance changes */
+       if (t->muted)
+       {
+               /* Mute & update balance*/
+               tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
+               tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
+               tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
+               tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
+       } else {
+               tda7432_write(sd, TDA7432_LF, t->lf);
+               tda7432_write(sd, TDA7432_LR, t->lr);
+               tda7432_write(sd, TDA7432_RF, t->rf);
+               tda7432_write(sd, TDA7432_RR, t->rr);
+       }
        return 0;
 }
 
-static struct i2c_driver driver = {
-       .owner           = THIS_MODULE,
-        .name            = "i2c tda7432 driver",
-       .id              = I2C_DRIVERID_TDA7432,
-        .flags           = I2C_DF_NOTIFY,
-       .attach_adapter  = tda7432_probe,
-        .detach_client   = tda7432_detach,
-        .command         = tda7432_command,
+static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
+{
+       switch (qc->id) {
+       case V4L2_CID_AUDIO_VOLUME:
+               return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
+       case V4L2_CID_AUDIO_MUTE:
+               return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
+       case V4L2_CID_AUDIO_BALANCE:
+       case V4L2_CID_AUDIO_BASS:
+       case V4L2_CID_AUDIO_TREBLE:
+               return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
+       }
+       return -EINVAL;
+}
+
+/* ----------------------------------------------------------------------- */
+
+static const struct v4l2_subdev_core_ops tda7432_core_ops = {
+       .queryctrl = tda7432_queryctrl,
+       .g_ctrl = tda7432_g_ctrl,
+       .s_ctrl = tda7432_s_ctrl,
 };
 
-static struct i2c_client client_template =
-{
-       I2C_DEVNAME("tda7432"),
-       .driver     = &driver,
+static const struct v4l2_subdev_ops tda7432_ops = {
+       .core = &tda7432_core_ops,
 };
 
-static int __init tda7432_init(void)
+/* ----------------------------------------------------------------------- */
+
+/* *********************** *
+ * i2c interface functions *
+ * *********************** */
+
+static int tda7432_probe(struct i2c_client *client,
+                       const struct i2c_device_id *id)
 {
-       if ( (loudness < 0) || (loudness > 15) ) {
-               printk(KERN_ERR "tda7432: loudness parameter must be between 0 and 15\n");
-               return -EINVAL;
+       struct tda7432 *t;
+       struct v4l2_subdev *sd;
+
+       v4l_info(client, "chip found @ 0x%02x (%s)\n",
+                       client->addr << 1, client->adapter->name);
+
+       t = kzalloc(sizeof(*t), GFP_KERNEL);
+       if (!t)
+               return -ENOMEM;
+       sd = &t->sd;
+       v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
+       if (loudness < 0 || loudness > 15) {
+               v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
+               if (loudness < 0)
+                       loudness = 0;
+               if (loudness > 15)
+                       loudness = 15;
        }
 
-       return i2c_add_driver(&driver);
+       do_tda7432_init(sd);
+       return 0;
 }
 
-static void __exit tda7432_fini(void)
+static int tda7432_remove(struct i2c_client *client)
 {
-       i2c_del_driver(&driver);
+       struct v4l2_subdev *sd = i2c_get_clientdata(client);
+
+       do_tda7432_init(sd);
+       v4l2_device_unregister_subdev(sd);
+       kfree(to_state(sd));
+       return 0;
 }
 
-module_init(tda7432_init);
-module_exit(tda7432_fini);
+static const struct i2c_device_id tda7432_id[] = {
+       { "tda7432", 0 },
+       { }
+};
+MODULE_DEVICE_TABLE(i2c, tda7432_id);
 
-/*
- * Local variables:
- * c-basic-offset: 8
- * End:
- */
+static struct v4l2_i2c_driver_data v4l2_i2c_data = {
+       .name = "tda7432",
+       .probe = tda7432_probe,
+       .remove = tda7432_remove,
+       .id_table = tda7432_id,
+};