4 * Copyright (C) 2005 T. Adachi <tadachi@tadachi-net.com>
6 * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
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.
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.
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.
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>
35 MODULE_DESCRIPTION("wm8739 driver");
36 MODULE_AUTHOR("T. Adachi, Hans Verkuil");
37 MODULE_LICENSE("GPL");
41 module_param(debug, int, 0644);
43 MODULE_PARM_DESC(debug, "Debug level (0-1)");
46 /* ------------------------------------------------------------------------ */
50 R5 = 5, R6, R7, R8, R9, R15 = 15,
55 struct v4l2_subdev sd;
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 */
64 static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
66 return container_of(sd, struct wm8739_state, sd);
69 /* ------------------------------------------------------------------------ */
71 static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
73 struct i2c_client *client = v4l2_get_subdevdata(sd);
76 if (reg < 0 || reg >= TOT_REGS) {
77 v4l2_err(sd, "Invalid register R%d\n", reg);
81 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
83 for (i = 0; i < 3; i++)
84 if (i2c_smbus_write_byte_data(client,
85 (reg << 1) | (val >> 8), val & 0xff) == 0)
87 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
91 /* write regs to set audio volume etc */
92 static void wm8739_set_audio(struct v4l2_subdev *sd)
94 struct wm8739_state *state = to_state(sd);
95 u16 mute = state->muted ? 0x80 : 0;
97 /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
98 * Default setting: 0x17 = 0 dB
100 wm8739_write(sd, R0, (state->vol_l & 0x1f) | mute);
101 wm8739_write(sd, R1, (state->vol_r & 0x1f) | mute);
104 static int wm8739_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
106 struct wm8739_state *state = to_state(sd);
109 case V4L2_CID_AUDIO_MUTE:
110 ctrl->value = state->muted;
113 case V4L2_CID_AUDIO_VOLUME:
114 ctrl->value = state->volume;
117 case V4L2_CID_AUDIO_BALANCE:
118 ctrl->value = state->balance;
127 static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
129 struct wm8739_state *state = to_state(sd);
130 unsigned int work_l, work_r;
133 case V4L2_CID_AUDIO_MUTE:
134 state->muted = ctrl->value;
137 case V4L2_CID_AUDIO_VOLUME:
138 state->volume = ctrl->value;
141 case V4L2_CID_AUDIO_BALANCE:
142 state->balance = ctrl->value;
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;
153 state->vol_l = (long)work_l * 31 / 65535;
154 state->vol_r = (long)work_r * 31 / 65535;
156 /* set audio volume etc. */
157 wm8739_set_audio(sd);
161 /* ------------------------------------------------------------------------ */
163 static struct v4l2_queryctrl wm8739_qctrl[] = {
165 .id = V4L2_CID_AUDIO_VOLUME,
170 .default_value = 58880,
172 .type = V4L2_CTRL_TYPE_INTEGER,
174 .id = V4L2_CID_AUDIO_MUTE,
181 .type = V4L2_CTRL_TYPE_BOOLEAN,
183 .id = V4L2_CID_AUDIO_BALANCE,
188 .default_value = 32768,
190 .type = V4L2_CTRL_TYPE_INTEGER,
194 /* ------------------------------------------------------------------------ */
196 static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
198 struct wm8739_state *state = to_state(sd);
200 state->clock_freq = audiofreq;
202 wm8739_write(sd, R9, 0x000);
205 /* 256fps, fs=44.1k */
206 wm8739_write(sd, R8, 0x020);
210 wm8739_write(sd, R8, 0x000);
214 wm8739_write(sd, R8, 0x018);
220 wm8739_write(sd, R9, 0x001);
224 static int wm8739_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
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));
236 static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
238 struct i2c_client *client = v4l2_get_subdevdata(sd);
240 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_WM8739, 0);
243 static int wm8739_log_status(struct v4l2_subdev *sd)
245 struct wm8739_state *state = to_state(sd);
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)" : "");
255 static int wm8739_command(struct i2c_client *client, unsigned cmd, void *arg)
257 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
260 /* ----------------------------------------------------------------------- */
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,
270 static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
271 .s_clock_freq = wm8739_s_clock_freq,
274 static const struct v4l2_subdev_ops wm8739_ops = {
275 .core = &wm8739_core_ops,
276 .audio = &wm8739_audio_ops,
279 /* ------------------------------------------------------------------------ */
281 /* i2c implementation */
283 static int wm8739_probe(struct i2c_client *client,
284 const struct i2c_device_id *id)
286 struct wm8739_state *state;
287 struct v4l2_subdev *sd;
289 /* Check if the adapter supports the needed features */
290 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
293 v4l_info(client, "chip found @ 0x%x (%s)\n",
294 client->addr << 1, client->adapter->name);
296 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
300 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
301 state->vol_l = 0x17; /* 0dB */
302 state->vol_r = 0x17; /* 0dB */
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;
309 /* Initialize wm8739 */
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);
323 wm8739_write(sd, R9, 0x001);
324 /* set volume/mute */
325 wm8739_set_audio(sd);
329 static int wm8739_remove(struct i2c_client *client)
331 struct v4l2_subdev *sd = i2c_get_clientdata(client);
333 v4l2_device_unregister_subdev(sd);
338 static const struct i2c_device_id wm8739_id[] = {
342 MODULE_DEVICE_TABLE(i2c, wm8739_id);
344 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
346 .command = wm8739_command,
347 .probe = wm8739_probe,
348 .remove = wm8739_remove,
349 .id_table = wm8739_id,