| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 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> | 
| Hans Verkuil | 74cab31 | 2007-04-27 12:31:26 -0300 | [diff] [blame] | 32 | #include <media/v4l2-chip-ident.h> | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 33 | #include <media/v4l2-i2c-drv-legacy.h> | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 34 |  | 
|  | 35 | MODULE_DESCRIPTION("wm8739 driver"); | 
|  | 36 | MODULE_AUTHOR("T. Adachi, Hans Verkuil"); | 
|  | 37 | MODULE_LICENSE("GPL"); | 
|  | 38 |  | 
|  | 39 | static int debug = 0; | 
|  | 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, (reg << 1) | | 
|  | 81 | (val >> 8), val & 0xff) == 0) { | 
|  | 82 | return 0; | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 | v4l_err(client, "I2C: cannot write %03x to register R%d\n", val, reg); | 
|  | 86 | return -1; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | /* write regs to set audio volume etc */ | 
|  | 90 | static void wm8739_set_audio(struct i2c_client *client) | 
|  | 91 | { | 
|  | 92 | struct wm8739_state *state = i2c_get_clientdata(client); | 
|  | 93 | u16 mute = state->muted ? 0x80 : 0; | 
|  | 94 |  | 
|  | 95 | /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB | 
|  | 96 | * Default setting: 0x17 = 0 dB | 
|  | 97 | */ | 
|  | 98 | wm8739_write(client, R0, (state->vol_l & 0x1f) | mute); | 
|  | 99 | wm8739_write(client, R1, (state->vol_r & 0x1f) | mute); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | static int wm8739_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl) | 
|  | 103 | { | 
|  | 104 | struct wm8739_state *state = i2c_get_clientdata(client); | 
|  | 105 |  | 
|  | 106 | switch (ctrl->id) { | 
|  | 107 | case V4L2_CID_AUDIO_MUTE: | 
|  | 108 | ctrl->value = state->muted; | 
|  | 109 | break; | 
|  | 110 |  | 
|  | 111 | case V4L2_CID_AUDIO_VOLUME: | 
|  | 112 | ctrl->value = state->volume; | 
|  | 113 | break; | 
|  | 114 |  | 
|  | 115 | case V4L2_CID_AUDIO_BALANCE: | 
|  | 116 | ctrl->value = state->balance; | 
|  | 117 | break; | 
|  | 118 |  | 
|  | 119 | default: | 
|  | 120 | return -EINVAL; | 
|  | 121 | } | 
|  | 122 | return 0; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | static int wm8739_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl) | 
|  | 126 | { | 
|  | 127 | struct wm8739_state *state = i2c_get_clientdata(client); | 
|  | 128 | unsigned int work_l, work_r; | 
|  | 129 |  | 
|  | 130 | switch (ctrl->id) { | 
|  | 131 | case V4L2_CID_AUDIO_MUTE: | 
|  | 132 | state->muted = ctrl->value; | 
|  | 133 | break; | 
|  | 134 |  | 
|  | 135 | case V4L2_CID_AUDIO_VOLUME: | 
|  | 136 | state->volume = ctrl->value; | 
|  | 137 | break; | 
|  | 138 |  | 
|  | 139 | case V4L2_CID_AUDIO_BALANCE: | 
|  | 140 | state->balance = ctrl->value; | 
|  | 141 | break; | 
|  | 142 |  | 
|  | 143 | default: | 
|  | 144 | return -EINVAL; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */ | 
|  | 148 | work_l = (min(65536 - state->balance, 32768) * state->volume) / 32768; | 
|  | 149 | work_r = (min(state->balance, (u16)32768) * state->volume) / 32768; | 
|  | 150 |  | 
|  | 151 | state->vol_l = (long)work_l * 31 / 65535; | 
|  | 152 | state->vol_r = (long)work_r * 31 / 65535; | 
|  | 153 |  | 
|  | 154 | /* set audio volume etc. */ | 
|  | 155 | wm8739_set_audio(client); | 
|  | 156 | return 0; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | /* ------------------------------------------------------------------------ */ | 
|  | 160 |  | 
|  | 161 | static struct v4l2_queryctrl wm8739_qctrl[] = { | 
|  | 162 | { | 
|  | 163 | .id            = V4L2_CID_AUDIO_VOLUME, | 
|  | 164 | .name          = "Volume", | 
|  | 165 | .minimum       = 0, | 
|  | 166 | .maximum       = 65535, | 
|  | 167 | .step          = 65535/100, | 
|  | 168 | .default_value = 58880, | 
|  | 169 | .flags         = 0, | 
|  | 170 | .type          = V4L2_CTRL_TYPE_INTEGER, | 
|  | 171 | },{ | 
|  | 172 | .id            = V4L2_CID_AUDIO_MUTE, | 
|  | 173 | .name          = "Mute", | 
|  | 174 | .minimum       = 0, | 
|  | 175 | .maximum       = 1, | 
|  | 176 | .step          = 1, | 
|  | 177 | .default_value = 1, | 
|  | 178 | .flags         = 0, | 
|  | 179 | .type          = V4L2_CTRL_TYPE_BOOLEAN, | 
|  | 180 | },{ | 
|  | 181 | .id            = V4L2_CID_AUDIO_BALANCE, | 
|  | 182 | .name          = "Balance", | 
|  | 183 | .minimum       = 0, | 
|  | 184 | .maximum       = 65535, | 
|  | 185 | .step          = 65535/100, | 
|  | 186 | .default_value = 32768, | 
|  | 187 | .flags         = 0, | 
|  | 188 | .type          = V4L2_CTRL_TYPE_INTEGER, | 
|  | 189 | } | 
|  | 190 | }; | 
|  | 191 |  | 
|  | 192 | /* ------------------------------------------------------------------------ */ | 
|  | 193 |  | 
|  | 194 | static int wm8739_command(struct i2c_client *client, unsigned int cmd, void *arg) | 
|  | 195 | { | 
|  | 196 | struct wm8739_state *state = i2c_get_clientdata(client); | 
|  | 197 |  | 
|  | 198 | switch (cmd) { | 
|  | 199 | case VIDIOC_INT_AUDIO_CLOCK_FREQ: | 
|  | 200 | { | 
|  | 201 | u32 audiofreq = *(u32 *)arg; | 
|  | 202 |  | 
|  | 203 | state->clock_freq = audiofreq; | 
|  | 204 | wm8739_write(client, R9, 0x000);	/* de-activate */ | 
|  | 205 | switch (audiofreq) { | 
|  | 206 | case 44100: | 
|  | 207 | wm8739_write(client, R8, 0x020); /* 256fps, fs=44.1k     */ | 
|  | 208 | break; | 
|  | 209 | case 48000: | 
|  | 210 | wm8739_write(client, R8, 0x000); /* 256fps, fs=48k       */ | 
|  | 211 | break; | 
|  | 212 | case 32000: | 
|  | 213 | wm8739_write(client, R8, 0x018); /* 256fps, fs=32k       */ | 
|  | 214 | break; | 
|  | 215 | default: | 
|  | 216 | break; | 
|  | 217 | } | 
|  | 218 | wm8739_write(client, R9, 0x001);	/* activate */ | 
|  | 219 | break; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | case VIDIOC_G_CTRL: | 
|  | 223 | return wm8739_get_ctrl(client, arg); | 
|  | 224 |  | 
|  | 225 | case VIDIOC_S_CTRL: | 
|  | 226 | return wm8739_set_ctrl(client, arg); | 
|  | 227 |  | 
|  | 228 | case VIDIOC_QUERYCTRL: | 
|  | 229 | { | 
|  | 230 | struct v4l2_queryctrl *qc = arg; | 
|  | 231 | int i; | 
|  | 232 |  | 
|  | 233 | for (i = 0; i < ARRAY_SIZE(wm8739_qctrl); i++) | 
|  | 234 | if (qc->id && qc->id == wm8739_qctrl[i].id) { | 
|  | 235 | memcpy(qc, &wm8739_qctrl[i], sizeof(*qc)); | 
|  | 236 | return 0; | 
|  | 237 | } | 
|  | 238 | return -EINVAL; | 
|  | 239 | } | 
|  | 240 |  | 
| Hans Verkuil | 74cab31 | 2007-04-27 12:31:26 -0300 | [diff] [blame] | 241 | case VIDIOC_G_CHIP_IDENT: | 
|  | 242 | return v4l2_chip_ident_i2c_client(client, arg, V4L2_IDENT_WM8739, 0); | 
|  | 243 |  | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 244 | case VIDIOC_LOG_STATUS: | 
|  | 245 | v4l_info(client, "Frequency: %u Hz\n", state->clock_freq); | 
|  | 246 | v4l_info(client, "Volume L:  %02x%s\n", state->vol_l & 0x1f, | 
|  | 247 | state->muted ? " (muted)" : ""); | 
|  | 248 | v4l_info(client, "Volume R:  %02x%s\n", state->vol_r & 0x1f, | 
|  | 249 | state->muted ? " (muted)" : ""); | 
|  | 250 | break; | 
|  | 251 |  | 
|  | 252 | default: | 
|  | 253 | return -EINVAL; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | return 0; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | /* ------------------------------------------------------------------------ */ | 
|  | 260 |  | 
|  | 261 | /* i2c implementation */ | 
|  | 262 |  | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 263 | static int wm8739_probe(struct i2c_client *client) | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 264 | { | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 265 | struct wm8739_state *state; | 
|  | 266 |  | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 267 | v4l_info(client, "chip found @ 0x%x (%s)\n", client->addr << 1, client->adapter->name); | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 268 |  | 
|  | 269 | state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL); | 
|  | 270 | if (state == NULL) { | 
|  | 271 | kfree(client); | 
|  | 272 | return -ENOMEM; | 
|  | 273 | } | 
|  | 274 | state->vol_l = 0x17; /* 0dB */ | 
|  | 275 | state->vol_r = 0x17; /* 0dB */ | 
|  | 276 | state->muted = 0; | 
|  | 277 | state->balance = 32768; | 
|  | 278 | /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */ | 
|  | 279 | state->volume = ((long)state->vol_l + 1) * 65535 / 31; | 
|  | 280 | state->clock_freq = 48000; | 
|  | 281 | i2c_set_clientdata(client, state); | 
|  | 282 |  | 
|  | 283 | /* initialize wm8739 */ | 
|  | 284 | wm8739_write(client, R15, 0x00); /* reset */ | 
|  | 285 | wm8739_write(client, R5, 0x000); /* filter setting, high path, offet clear */ | 
|  | 286 | wm8739_write(client, R6, 0x000); /* ADC, OSC, Power Off mode Disable */ | 
|  | 287 | wm8739_write(client, R7, 0x049); /* Digital Audio interface format */ | 
|  | 288 | /* Enable Master mode */ | 
|  | 289 | /* 24 bit, MSB first/left justified */ | 
|  | 290 | wm8739_write(client, R8, 0x000); /* sampling control */ | 
|  | 291 | /* normal, 256fs, 48KHz sampling rate */ | 
|  | 292 | wm8739_write(client, R9, 0x001); /* activate */ | 
|  | 293 | wm8739_set_audio(client); 	 /* set volume/mute */ | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 294 | return 0; | 
|  | 295 | } | 
|  | 296 |  | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 297 | static int wm8739_remove(struct i2c_client *client) | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 298 | { | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 299 | kfree(i2c_get_clientdata(client)); | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 300 | return 0; | 
|  | 301 | } | 
|  | 302 |  | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 303 | static struct v4l2_i2c_driver_data v4l2_i2c_data = { | 
|  | 304 | .name = "wm8739", | 
|  | 305 | .driverid = I2C_DRIVERID_WM8739, | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 306 | .command = wm8739_command, | 
| Hans Verkuil | 49457cc | 2007-09-13 11:10:07 -0300 | [diff] [blame^] | 307 | .probe = wm8739_probe, | 
|  | 308 | .remove = wm8739_remove, | 
| Hans Verkuil | 75c4570 | 2006-03-28 18:23:48 -0300 | [diff] [blame] | 309 | }; | 
|  | 310 |  |