Merge branch 'for-2.6.27' of git://linux-nfs.org/~bfields/linux
[linux-2.6] / drivers / media / video / cs5345.c
1 /*
2  * cs5345 Cirrus Logic 24-bit, 192 kHz Stereo Audio ADC
3  * Copyright (C) 2007 Hans Verkuil
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20
21 #include <linux/version.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/i2c.h>
25 #include <linux/videodev2.h>
26 #include <media/v4l2-i2c-drv.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-chip-ident.h>
29
30 MODULE_DESCRIPTION("i2c device driver for cs5345 Audio ADC");
31 MODULE_AUTHOR("Hans Verkuil");
32 MODULE_LICENSE("GPL");
33
34 static int debug;
35
36 module_param(debug, bool, 0644);
37
38 MODULE_PARM_DESC(debug, "Debugging messages\n\t\t\t0=Off (default), 1=On");
39
40
41 /* ----------------------------------------------------------------------- */
42
43 static inline int cs5345_write(struct i2c_client *client, u8 reg, u8 value)
44 {
45         return i2c_smbus_write_byte_data(client, reg, value);
46 }
47
48 static inline int cs5345_read(struct i2c_client *client, u8 reg)
49 {
50         return i2c_smbus_read_byte_data(client, reg);
51 }
52
53 static int cs5345_command(struct i2c_client *client, unsigned cmd, void *arg)
54 {
55         struct v4l2_routing *route = arg;
56         struct v4l2_control *ctrl = arg;
57
58         switch (cmd) {
59         case VIDIOC_INT_G_AUDIO_ROUTING:
60                 route->input = cs5345_read(client, 0x09) & 7;
61                 route->input |= cs5345_read(client, 0x05) & 0x70;
62                 route->output = 0;
63                 break;
64
65         case VIDIOC_INT_S_AUDIO_ROUTING:
66                 if ((route->input & 0xf) > 6) {
67                         v4l_err(client, "Invalid input %d.\n", route->input);
68                         return -EINVAL;
69                 }
70                 cs5345_write(client, 0x09, route->input & 0xf);
71                 cs5345_write(client, 0x05, route->input & 0xf0);
72                 break;
73
74         case VIDIOC_G_CTRL:
75                 if (ctrl->id == V4L2_CID_AUDIO_MUTE) {
76                         ctrl->value = (cs5345_read(client, 0x04) & 0x08) != 0;
77                         break;
78                 }
79                 if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
80                         return -EINVAL;
81                 ctrl->value = cs5345_read(client, 0x07) & 0x3f;
82                 if (ctrl->value >= 32)
83                         ctrl->value = ctrl->value - 64;
84                 break;
85
86         case VIDIOC_S_CTRL:
87                 break;
88                 if (ctrl->id == V4L2_CID_AUDIO_MUTE) {
89                         cs5345_write(client, 0x04, ctrl->value ? 0x80 : 0);
90                         break;
91                 }
92                 if (ctrl->id != V4L2_CID_AUDIO_VOLUME)
93                         return -EINVAL;
94                 if (ctrl->value > 24 || ctrl->value < -24)
95                         return -EINVAL;
96                 cs5345_write(client, 0x07, ((u8)ctrl->value) & 0x3f);
97                 cs5345_write(client, 0x08, ((u8)ctrl->value) & 0x3f);
98                 break;
99
100 #ifdef CONFIG_VIDEO_ADV_DEBUG
101         case VIDIOC_DBG_G_REGISTER:
102         case VIDIOC_DBG_S_REGISTER:
103         {
104                 struct v4l2_register *reg = arg;
105
106                 if (!v4l2_chip_match_i2c_client(client,
107                                         reg->match_type, reg->match_chip))
108                         return -EINVAL;
109                 if (!capable(CAP_SYS_ADMIN))
110                         return -EPERM;
111                 if (cmd == VIDIOC_DBG_G_REGISTER)
112                         reg->val = cs5345_read(client, reg->reg & 0x1f);
113                 else
114                         cs5345_write(client, reg->reg & 0x1f, reg->val & 0x1f);
115                 break;
116         }
117 #endif
118
119         case VIDIOC_G_CHIP_IDENT:
120                 return v4l2_chip_ident_i2c_client(client,
121                                 arg, V4L2_IDENT_CS5345, 0);
122
123         case VIDIOC_LOG_STATUS:
124                 {
125                         u8 v = cs5345_read(client, 0x09) & 7;
126                         u8 m = cs5345_read(client, 0x04);
127                         int vol = cs5345_read(client, 0x08) & 0x3f;
128
129                         v4l_info(client, "Input:  %d%s\n", v,
130                                       (m & 0x80) ? " (muted)" : "");
131                         if (vol >= 32)
132                                 vol = vol - 64;
133                         v4l_info(client, "Volume: %d dB\n", vol);
134                         break;
135                 }
136
137         default:
138                 return -EINVAL;
139         }
140         return 0;
141 }
142
143 /* ----------------------------------------------------------------------- */
144
145 static int cs5345_probe(struct i2c_client *client,
146                         const struct i2c_device_id *id)
147 {
148         /* Check if the adapter supports the needed features */
149         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
150                 return -EIO;
151
152         v4l_info(client, "chip found @ 0x%x (%s)\n",
153                         client->addr << 1, client->adapter->name);
154
155         cs5345_write(client, 0x02, 0x00);
156         cs5345_write(client, 0x04, 0x01);
157         cs5345_write(client, 0x09, 0x01);
158         return 0;
159 }
160
161 /* ----------------------------------------------------------------------- */
162
163 static const struct i2c_device_id cs5345_id[] = {
164         { "cs5345", 0 },
165         { }
166 };
167 MODULE_DEVICE_TABLE(i2c, cs5345_id);
168
169 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
170         .name = "cs5345",
171         .driverid = I2C_DRIVERID_CS5345,
172         .command = cs5345_command,
173         .probe = cs5345_probe,
174         .id_table = cs5345_id,
175 };