Merge branch 'hwpoison' of git://git.kernel.org/pub/scm/linux/kernel/git/ak/linux...
[linux-block.git] / drivers / media / video / wm8739.c
CommitLineData
75c4570c
HV
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>
33b687cf 30#include <linux/videodev2.h>
f1460e97 31#include <media/v4l2-device.h>
74cab31c 32#include <media/v4l2-chip-ident.h>
f69d419a 33#include <media/v4l2-i2c-drv.h>
75c4570c
HV
34
35MODULE_DESCRIPTION("wm8739 driver");
36MODULE_AUTHOR("T. Adachi, Hans Verkuil");
37MODULE_LICENSE("GPL");
38
099b8e73 39static int debug;
75c4570c
HV
40
41module_param(debug, int, 0644);
42
43MODULE_PARM_DESC(debug, "Debug level (0-1)");
44
45
75c4570c
HV
46/* ------------------------------------------------------------------------ */
47
48enum {
49 R0 = 0, R1,
50 R5 = 5, R6, R7, R8, R9, R15 = 15,
51 TOT_REGS
52};
53
54struct wm8739_state {
f1460e97 55 struct v4l2_subdev sd;
75c4570c
HV
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
f1460e97
HV
64static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
65{
66 return container_of(sd, struct wm8739_state, sd);
67}
68
75c4570c
HV
69/* ------------------------------------------------------------------------ */
70
f1460e97 71static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
75c4570c 72{
f1460e97 73 struct i2c_client *client = v4l2_get_subdevdata(sd);
75c4570c
HV
74 int i;
75
76 if (reg < 0 || reg >= TOT_REGS) {
f1460e97 77 v4l2_err(sd, "Invalid register R%d\n", reg);
75c4570c
HV
78 return -1;
79 }
80
f1460e97 81 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
75c4570c 82
099b8e73
HV
83 for (i = 0; i < 3; i++)
84 if (i2c_smbus_write_byte_data(client,
85 (reg << 1) | (val >> 8), val & 0xff) == 0)
75c4570c 86 return 0;
f1460e97 87 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
75c4570c
HV
88 return -1;
89}
90
91/* write regs to set audio volume etc */
f1460e97 92static void wm8739_set_audio(struct v4l2_subdev *sd)
75c4570c 93{
f1460e97 94 struct wm8739_state *state = to_state(sd);
75c4570c
HV
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 */
f1460e97
HV
100 wm8739_write(sd, R0, (state->vol_l & 0x1f) | mute);
101 wm8739_write(sd, R1, (state->vol_r & 0x1f) | mute);
75c4570c
HV
102}
103
f1460e97 104static int wm8739_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
75c4570c 105{
f1460e97 106 struct wm8739_state *state = to_state(sd);
75c4570c
HV
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
f1460e97 127static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
75c4570c 128{
f1460e97 129 struct wm8739_state *state = to_state(sd);
75c4570c
HV
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. */
f1460e97 157 wm8739_set_audio(sd);
75c4570c
HV
158 return 0;
159}
160
161/* ------------------------------------------------------------------------ */
162
163static 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,
099b8e73 173 }, {
75c4570c
HV
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,
099b8e73 182 }, {
75c4570c
HV
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
f1460e97 196static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
75c4570c 197{
f1460e97
HV
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:
75c4570c
HV
217 break;
218 }
f1460e97
HV
219 /* activate */
220 wm8739_write(sd, R9, 0x001);
221 return 0;
222}
75c4570c 223
f1460e97
HV
224static int wm8739_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
225{
226 int i;
75c4570c 227
f1460e97
HV
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}
75c4570c 235
aecde8b5 236static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
f1460e97
HV
237{
238 struct i2c_client *client = v4l2_get_subdevdata(sd);
74cab31c 239
f1460e97
HV
240 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_WM8739, 0);
241}
75c4570c 242
f1460e97
HV
243static int wm8739_log_status(struct v4l2_subdev *sd)
244{
245 struct wm8739_state *state = to_state(sd);
75c4570c 246
f1460e97
HV
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)" : "");
75c4570c
HV
252 return 0;
253}
254
f1460e97
HV
255/* ----------------------------------------------------------------------- */
256
257static const struct v4l2_subdev_core_ops wm8739_core_ops = {
258 .log_status = wm8739_log_status,
259 .g_chip_ident = wm8739_g_chip_ident,
260 .queryctrl = wm8739_queryctrl,
261 .g_ctrl = wm8739_g_ctrl,
262 .s_ctrl = wm8739_s_ctrl,
263};
264
265static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
266 .s_clock_freq = wm8739_s_clock_freq,
267};
268
269static const struct v4l2_subdev_ops wm8739_ops = {
270 .core = &wm8739_core_ops,
271 .audio = &wm8739_audio_ops,
272};
273
75c4570c
HV
274/* ------------------------------------------------------------------------ */
275
276/* i2c implementation */
277
d2653e92
JD
278static int wm8739_probe(struct i2c_client *client,
279 const struct i2c_device_id *id)
75c4570c 280{
75c4570c 281 struct wm8739_state *state;
f1460e97 282 struct v4l2_subdev *sd;
75c4570c 283
188f3457
HV
284 /* Check if the adapter supports the needed features */
285 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
286 return -EIO;
287
099b8e73
HV
288 v4l_info(client, "chip found @ 0x%x (%s)\n",
289 client->addr << 1, client->adapter->name);
75c4570c
HV
290
291 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
6c832e36 292 if (state == NULL)
75c4570c 293 return -ENOMEM;
f1460e97
HV
294 sd = &state->sd;
295 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
75c4570c
HV
296 state->vol_l = 0x17; /* 0dB */
297 state->vol_r = 0x17; /* 0dB */
298 state->muted = 0;
299 state->balance = 32768;
300 /* normalize (12dB(31) to -34.5dB(0) [0dB(23)] -> 65535 to 0) */
301 state->volume = ((long)state->vol_l + 1) * 65535 / 31;
302 state->clock_freq = 48000;
75c4570c 303
099b8e73
HV
304 /* Initialize wm8739 */
305
306 /* reset */
f1460e97 307 wm8739_write(sd, R15, 0x00);
099b8e73 308 /* filter setting, high path, offet clear */
f1460e97 309 wm8739_write(sd, R5, 0x000);
099b8e73 310 /* ADC, OSC, Power Off mode Disable */
f1460e97 311 wm8739_write(sd, R6, 0x000);
099b8e73
HV
312 /* Digital Audio interface format:
313 Enable Master mode, 24 bit, MSB first/left justified */
f1460e97 314 wm8739_write(sd, R7, 0x049);
099b8e73 315 /* sampling control: normal, 256fs, 48KHz sampling rate */
f1460e97 316 wm8739_write(sd, R8, 0x000);
099b8e73 317 /* activate */
f1460e97 318 wm8739_write(sd, R9, 0x001);
099b8e73 319 /* set volume/mute */
f1460e97 320 wm8739_set_audio(sd);
75c4570c
HV
321 return 0;
322}
323
49457cc2 324static int wm8739_remove(struct i2c_client *client)
75c4570c 325{
f1460e97
HV
326 struct v4l2_subdev *sd = i2c_get_clientdata(client);
327
328 v4l2_device_unregister_subdev(sd);
329 kfree(to_state(sd));
75c4570c
HV
330 return 0;
331}
332
af294867
JD
333static const struct i2c_device_id wm8739_id[] = {
334 { "wm8739", 0 },
335 { }
336};
337MODULE_DEVICE_TABLE(i2c, wm8739_id);
338
49457cc2
HV
339static struct v4l2_i2c_driver_data v4l2_i2c_data = {
340 .name = "wm8739",
49457cc2
HV
341 .probe = wm8739_probe,
342 .remove = wm8739_remove,
af294867 343 .id_table = wm8739_id,
75c4570c 344};