ac4fee1d0251443a510a56e3eb4b8eab538887f3
[linux-2.6.git] / drivers / media / video / wm8739.c
1 /*
2  * wm8739
3  *
4  * Copyright (C) 2005 T. Adachi <tadachi@tadachi-net.com>
5  *
6  * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
7  * - Cleanup
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/ioctl.h>
27 #include <asm/uaccess.h>
28 #include <linux/i2c.h>
29 #include <linux/i2c-id.h>
30 #include <linux/videodev.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-chip-ident.h>
33 #include <media/v4l2-i2c-drv-legacy.h>
34
35 MODULE_DESCRIPTION("wm8739 driver");
36 MODULE_AUTHOR("T. Adachi, Hans Verkuil");
37 MODULE_LICENSE("GPL");
38
39 static int debug;
40 static unsigned short normal_i2c[] = { 0x34 >> 1, 0x36 >> 1, I2C_CLIENT_END };
41
42 module_param(debug, int, 0644);
43
44 MODULE_PARM_DESC(debug, "Debug level (0-1)");
45
46
47 I2C_CLIENT_INSMOD;
48
49 /* ------------------------------------------------------------------------ */
50
51 enum {
52         R0 = 0, R1,
53         R5 = 5, R6, R7, R8, R9, R15 = 15,
54         TOT_REGS
55 };
56
57 struct wm8739_state {
58         u32 clock_freq;
59         u8 muted;
60         u16 volume;
61         u16 balance;
62         u8 vol_l;               /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
63         u8 vol_r;               /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
64 };
65
66 /* ------------------------------------------------------------------------ */
67
68 static int wm8739_write(struct i2c_client *client, int reg, u16 val)
69 {
70         int i;
71
72         if (reg < 0 || reg >= TOT_REGS) {
73                 v4l_err(client, "Invalid register R%d\n", reg);
74                 return -1;
75         }
76
77         v4l_dbg(1, debug, client, "write: %02x %02x\n", reg, val);
78
79         for (i = 0; i < 3; i++)
80                 if (i2c_smbus_write_byte_data(client,
81                                 (reg << 1) | (val >> 8), val & 0xff) == 0)
82                         return 0;
83         v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg);
84         return -1;
85 }
86
87 /* write regs to set audio volume etc */
88 static void wm8739_set_audio(struct i2c_client *client)
89 {
90         struct wm8739_state *state = i2c_get_clientdata(client);
91         u16 mute = state->muted ? 0x80 : 0;
92
93         /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
94          * Default setting: 0x17 = 0 dB
95          */
96         wm8739_write(client, R0, (state->vol_l & 0x1f) | mute);
97         wm8739_write(client, R1, (state->vol_r & 0x1f) | mute);
98 }
99
100 static int wm8739_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
101 {
102         struct wm8739_state *state = i2c_get_clientdata(client);
103
104         switch (ctrl->id) {
105         case V4L2_CID_AUDIO_MUTE:
106                 ctrl->value = state->muted;
107                 break;
108
109         case V4L2_CID_AUDIO_VOLUME:
110                 ctrl->value = state->volume;
111                 break;
112
113         case V4L2_CID_AUDIO_BALANCE:
114                 ctrl->value = state->balance;
115                 break;
116
117         default:
118                 return -EINVAL;
119         }
120         return 0;
121 }
122
123 static int wm8739_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
124 {
125         struct wm8739_state *state = i2c_get_clientdata(client);
126         unsigned int work_l, work_r;
127
128         switch (ctrl->id) {
129         case V4L2_CID_AUDIO_MUTE:
130                 state->muted = ctrl->value;
131                 break;
132
133         case V4L2_CID_AUDIO_VOLUME:
134                 state->volume = ctrl->value;
135                 break;
136
137         case V4L2_CID_AUDIO_BALANCE:
138                 state->balance = ctrl->value;
139                 break;
140
141         default:
142                 return -EINVAL;
143         }
144
145         /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
146         work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768;
147         work_r = (min(state->balance, (u16)32768) * state->volume) / 32768;
148
149         state->vol_l = (long)work_l * 31 / 65535;
150         state->vol_r = (long)work_r * 31 / 65535;
151
152         /* set audio volume etc. */
153         wm8739_set_audio(client);
154         return 0;
155 }
156
157 /* ------------------------------------------------------------------------ */
158
159 static struct v4l2_queryctrl wm8739_qctrl[] = {
160         {
161                 .id            = V4L2_CID_AUDIO_VOLUME,
162                 .name          = "Volume",
163                 .minimum       = 0,
164                 .maximum       = 65535,
165                 .step          = 65535/100,
166                 .default_value = 58880,
167                 .flags         = 0,
168                 .type          = V4L2_CTRL_TYPE_INTEGER,
169         }, {
170                 .id            = V4L2_CID_AUDIO_MUTE,
171                 .name          = "Mute",
172                 .minimum       = 0,
173                 .maximum       = 1,
174                 .step          = 1,
175                 .default_value = 1,
176                 .flags         = 0,
177                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
178         }, {
179                 .id            = V4L2_CID_AUDIO_BALANCE,
180                 .name          = "Balance",
181                 .minimum       = 0,
182                 .maximum       = 65535,
183                 .step          = 65535/100,
184                 .default_value = 32768,
185                 .flags         = 0,
186                 .type          = V4L2_CTRL_TYPE_INTEGER,
187         }
188 };
189
190 /* ------------------------------------------------------------------------ */
191
192 static int wm8739_command(struct i2c_client *client, unsigned cmd, void *arg)
193 {
194         struct wm8739_state *state = i2c_get_clientdata(client);
195
196         switch (cmd) {
197         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
198         {
199                 u32 audiofreq = *(u32 *)arg;
200
201                 state->clock_freq = audiofreq;
202                 /* de-activate */
203                 wm8739_write(client, R9, 0x000);
204                 switch (audiofreq) {
205                 case 44100:
206                         /* 256fps, fs=44.1k */
207                         wm8739_write(client, R8, 0x020);
208                         break;
209                 case 48000:
210                         /* 256fps, fs=48k */
211                         wm8739_write(client, R8, 0x000);
212                         break;
213                 case 32000:
214                         /* 256fps, fs=32k */
215                         wm8739_write(client, R8, 0x018);
216                         break;
217                 default:
218                         break;
219                 }
220                 /* activate */
221                 wm8739_write(client, R9, 0x001);
222                 break;
223         }
224
225         case VIDIOC_G_CTRL:
226                 return wm8739_get_ctrl(client, arg);
227
228         case VIDIOC_S_CTRL:
229                 return wm8739_set_ctrl(client, arg);
230
231         case VIDIOC_QUERYCTRL:
232         {
233                 struct v4l2_queryctrl *qc = arg;
234                 int i;
235
236                 for (i = 0; i < ARRAY_SIZE(wm8739_qctrl); i++)
237                         if (qc->id && qc->id == wm8739_qctrl[i].id) {
238                                 memcpy(qc, &wm8739_qctrl[i], sizeof(*qc));
239                                 return 0;
240                         }
241                 return -EINVAL;
242         }
243
244         case VIDIOC_G_CHIP_IDENT:
245                 return v4l2_chip_ident_i2c_client(client,
246                                 arg, V4L2_IDENT_WM8739, 0);
247
248         case VIDIOC_LOG_STATUS:
249                 v4l_info(client, "Frequency: %u Hz\n", state->clock_freq);
250                 v4l_info(client, "Volume L:  %02x%s\n", state->vol_l & 0x1f,
251                                 state->muted ? " (muted)" : "");
252                 v4l_info(client, "Volume R:  %02x%s\n", state->vol_r & 0x1f,
253                                 state->muted ? " (muted)" : "");
254                 break;
255
256         default:
257                 return -EINVAL;
258         }
259
260         return 0;
261 }
262
263 /* ------------------------------------------------------------------------ */
264
265 /* i2c implementation */
266
267 static int wm8739_probe(struct i2c_client *client)
268 {
269         struct wm8739_state *state;
270
271         /* Check if the adapter supports the needed features */
272         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
273                 return -EIO;
274
275         v4l_info(client, "chip found @ 0x%x (%s)\n",
276                         client->addr << 1, client->adapter->name);
277
278         state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
279         if (state == NULL) {
280                 kfree(client);
281                 return -ENOMEM;
282         }
283         state->vol_l = 0x17; /* 0dB */
284         state->vol_r = 0x17; /* 0dB */
285         state->muted = 0;
286         state->balance = 32768;
287         /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */
288         state->volume = ((long)state->vol_l + 1) * 65535 / 31;
289         state->clock_freq = 48000;
290         i2c_set_clientdata(client, state);
291
292         /* Initialize wm8739 */
293
294         /* reset */
295         wm8739_write(client, R15, 0x00);
296         /* filter setting, high path, offet clear */
297         wm8739_write(client, R5, 0x000);
298         /* ADC, OSC, Power Off mode Disable */
299         wm8739_write(client, R6, 0x000);
300         /* Digital Audio interface format:
301            Enable Master mode, 24 bit, MSB first/left justified */
302         wm8739_write(client, R7, 0x049);
303         /* sampling control: normal, 256fs, 48KHz sampling rate */
304         wm8739_write(client, R8, 0x000);
305         /* activate */
306         wm8739_write(client, R9, 0x001);
307         /* set volume/mute */
308         wm8739_set_audio(client);
309         return 0;
310 }
311
312 static int wm8739_remove(struct i2c_client *client)
313 {
314         kfree(i2c_get_clientdata(client));
315         return 0;
316 }
317
318 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
319         .name = "wm8739",
320         .driverid = I2C_DRIVERID_WM8739,
321         .command = wm8739_command,
322         .probe = wm8739_probe,
323         .remove = wm8739_remove,
324 };
325