V4L/DVB (10081): pxa-camera: call try_fmt() camera device method with correct pixel...
[linux-2.6] / 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/videodev2.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-chip-ident.h>
33 #include <media/v4l2-i2c-drv.h>
34
35 MODULE_DESCRIPTION("wm8739 driver");
36 MODULE_AUTHOR("T. Adachi, Hans Verkuil");
37 MODULE_LICENSE("GPL");
38
39 static int debug;
40
41 module_param(debug, int, 0644);
42
43 MODULE_PARM_DESC(debug, "Debug level (0-1)");
44
45
46 /* ------------------------------------------------------------------------ */
47
48 enum {
49         R0 = 0, R1,
50         R5 = 5, R6, R7, R8, R9, R15 = 15,
51         TOT_REGS
52 };
53
54 struct wm8739_state {
55         struct v4l2_subdev sd;
56         u32 clock_freq;
57         u8 muted;
58         u16 volume;
59         u16 balance;
60         u8 vol_l;               /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
61         u8 vol_r;               /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
62 };
63
64 static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
65 {
66         return container_of(sd, struct wm8739_state, sd);
67 }
68
69 /* ------------------------------------------------------------------------ */
70
71 static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
72 {
73         struct i2c_client *client = v4l2_get_subdevdata(sd);
74         int i;
75
76         if (reg < 0 || reg >= TOT_REGS) {
77                 v4l2_err(sd, "Invalid register R%d\n", reg);
78                 return -1;
79         }
80
81         v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
82
83         for (i = 0; i < 3; i++)
84                 if (i2c_smbus_write_byte_data(client,
85                                 (reg << 1) | (val >> 8), val & 0xff) == 0)
86                         return 0;
87         v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
88         return -1;
89 }
90
91 /* write regs to set audio volume etc */
92 static void wm8739_set_audio(struct v4l2_subdev *sd)
93 {
94         struct wm8739_state *state = to_state(sd);
95         u16 mute = state->muted ? 0x80 : 0;
96
97         /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
98          * Default setting: 0x17 = 0 dB
99          */
100         wm8739_write(sd, R0, (state->vol_l & 0x1f) | mute);
101         wm8739_write(sd, R1, (state->vol_r & 0x1f) | mute);
102 }
103
104 static int wm8739_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
105 {
106         struct wm8739_state *state = to_state(sd);
107
108         switch (ctrl->id) {
109         case V4L2_CID_AUDIO_MUTE:
110                 ctrl->value = state->muted;
111                 break;
112
113         case V4L2_CID_AUDIO_VOLUME:
114                 ctrl->value = state->volume;
115                 break;
116
117         case V4L2_CID_AUDIO_BALANCE:
118                 ctrl->value = state->balance;
119                 break;
120
121         default:
122                 return -EINVAL;
123         }
124         return 0;
125 }
126
127 static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
128 {
129         struct wm8739_state *state = to_state(sd);
130         unsigned int work_l, work_r;
131
132         switch (ctrl->id) {
133         case V4L2_CID_AUDIO_MUTE:
134                 state->muted = ctrl->value;
135                 break;
136
137         case V4L2_CID_AUDIO_VOLUME:
138                 state->volume = ctrl->value;
139                 break;
140
141         case V4L2_CID_AUDIO_BALANCE:
142                 state->balance = ctrl->value;
143                 break;
144
145         default:
146                 return -EINVAL;
147         }
148
149         /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
150         work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768;
151         work_r = (min(state->balance, (u16)32768) * state->volume) / 32768;
152
153         state->vol_l = (long)work_l * 31 / 65535;
154         state->vol_r = (long)work_r * 31 / 65535;
155
156         /* set audio volume etc. */
157         wm8739_set_audio(sd);
158         return 0;
159 }
160
161 /* ------------------------------------------------------------------------ */
162
163 static struct v4l2_queryctrl wm8739_qctrl[] = {
164         {
165                 .id            = V4L2_CID_AUDIO_VOLUME,
166                 .name          = "Volume",
167                 .minimum       = 0,
168                 .maximum       = 65535,
169                 .step          = 65535/100,
170                 .default_value = 58880,
171                 .flags         = 0,
172                 .type          = V4L2_CTRL_TYPE_INTEGER,
173         }, {
174                 .id            = V4L2_CID_AUDIO_MUTE,
175                 .name          = "Mute",
176                 .minimum       = 0,
177                 .maximum       = 1,
178                 .step          = 1,
179                 .default_value = 1,
180                 .flags         = 0,
181                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
182         }, {
183                 .id            = V4L2_CID_AUDIO_BALANCE,
184                 .name          = "Balance",
185                 .minimum       = 0,
186                 .maximum       = 65535,
187                 .step          = 65535/100,
188                 .default_value = 32768,
189                 .flags         = 0,
190                 .type          = V4L2_CTRL_TYPE_INTEGER,
191         }
192 };
193
194 /* ------------------------------------------------------------------------ */
195
196 static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
197 {
198         struct wm8739_state *state = to_state(sd);
199
200         state->clock_freq = audiofreq;
201         /* de-activate */
202         wm8739_write(sd, R9, 0x000);
203         switch (audiofreq) {
204         case 44100:
205                 /* 256fps, fs=44.1k */
206                 wm8739_write(sd, R8, 0x020);
207                 break;
208         case 48000:
209                 /* 256fps, fs=48k */
210                 wm8739_write(sd, R8, 0x000);
211                 break;
212         case 32000:
213                 /* 256fps, fs=32k */
214                 wm8739_write(sd, R8, 0x018);
215                 break;
216         default:
217                 break;
218         }
219         /* activate */
220         wm8739_write(sd, R9, 0x001);
221         return 0;
222 }
223
224 static int wm8739_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
225 {
226         int i;
227
228         for (i = 0; i < ARRAY_SIZE(wm8739_qctrl); i++)
229                 if (qc->id && qc->id == wm8739_qctrl[i].id) {
230                         memcpy(qc, &wm8739_qctrl[i], sizeof(*qc));
231                         return 0;
232                 }
233         return -EINVAL;
234 }
235
236 static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip)
237 {
238         struct i2c_client *client = v4l2_get_subdevdata(sd);
239
240         return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_WM8739, 0);
241 }
242
243 static int wm8739_log_status(struct v4l2_subdev *sd)
244 {
245         struct wm8739_state *state = to_state(sd);
246
247         v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
248         v4l2_info(sd, "Volume L:  %02x%s\n", state->vol_l & 0x1f,
249                         state->muted ? " (muted)" : "");
250         v4l2_info(sd, "Volume R:  %02x%s\n", state->vol_r & 0x1f,
251                         state->muted ? " (muted)" : "");
252         return 0;
253 }
254
255 static int wm8739_command(struct i2c_client *client, unsigned cmd, void *arg)
256 {
257         return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
258 }
259
260 /* ----------------------------------------------------------------------- */
261
262 static const struct v4l2_subdev_core_ops wm8739_core_ops = {
263         .log_status = wm8739_log_status,
264         .g_chip_ident = wm8739_g_chip_ident,
265         .queryctrl = wm8739_queryctrl,
266         .g_ctrl = wm8739_g_ctrl,
267         .s_ctrl = wm8739_s_ctrl,
268 };
269
270 static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
271         .s_clock_freq = wm8739_s_clock_freq,
272 };
273
274 static const struct v4l2_subdev_ops wm8739_ops = {
275         .core = &wm8739_core_ops,
276         .audio = &wm8739_audio_ops,
277 };
278
279 /* ------------------------------------------------------------------------ */
280
281 /* i2c implementation */
282
283 static int wm8739_probe(struct i2c_client *client,
284                         const struct i2c_device_id *id)
285 {
286         struct wm8739_state *state;
287         struct v4l2_subdev *sd;
288
289         /* Check if the adapter supports the needed features */
290         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
291                 return -EIO;
292
293         v4l_info(client, "chip found @ 0x%x (%s)\n",
294                         client->addr << 1, client->adapter->name);
295
296         state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
297         if (state == NULL)
298                 return -ENOMEM;
299         sd = &state->sd;
300         v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
301         state->vol_l = 0x17; /* 0dB */
302         state->vol_r = 0x17; /* 0dB */
303         state->muted = 0;
304         state->balance = 32768;
305         /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */
306         state->volume = ((long)state->vol_l + 1) * 65535 / 31;
307         state->clock_freq = 48000;
308
309         /* Initialize wm8739 */
310
311         /* reset */
312         wm8739_write(sd, R15, 0x00);
313         /* filter setting, high path, offet clear */
314         wm8739_write(sd, R5, 0x000);
315         /* ADC, OSC, Power Off mode Disable */
316         wm8739_write(sd, R6, 0x000);
317         /* Digital Audio interface format:
318            Enable Master mode, 24 bit, MSB first/left justified */
319         wm8739_write(sd, R7, 0x049);
320         /* sampling control: normal, 256fs, 48KHz sampling rate */
321         wm8739_write(sd, R8, 0x000);
322         /* activate */
323         wm8739_write(sd, R9, 0x001);
324         /* set volume/mute */
325         wm8739_set_audio(sd);
326         return 0;
327 }
328
329 static int wm8739_remove(struct i2c_client *client)
330 {
331         struct v4l2_subdev *sd = i2c_get_clientdata(client);
332
333         v4l2_device_unregister_subdev(sd);
334         kfree(to_state(sd));
335         return 0;
336 }
337
338 static const struct i2c_device_id wm8739_id[] = {
339         { "wm8739", 0 },
340         { }
341 };
342 MODULE_DEVICE_TABLE(i2c, wm8739_id);
343
344 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
345         .name = "wm8739",
346         .driverid = I2C_DRIVERID_WM8739,
347         .command = wm8739_command,
348         .probe = wm8739_probe,
349         .remove = wm8739_remove,
350         .id_table = wm8739_id,
351 };