blob: b572ce288e14857d2e6f3f9fffe2675a5f111d89 [file] [log] [blame]
Hans Verkuil75c45702006-03-28 18:23:48 -03001/*
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>
Hans Verkuil33b687c2008-07-25 05:32:50 -030030#include <linux/videodev2.h>
Hans Verkuilf1460e92008-11-29 13:02:26 -030031#include <media/v4l2-device.h>
Hans Verkuil74cab312007-04-27 12:31:26 -030032#include <media/v4l2-chip-ident.h>
Hans Verkuilf69d4192007-12-12 07:24:27 -030033#include <media/v4l2-i2c-drv.h>
Hans Verkuil75c45702006-03-28 18:23:48 -030034
35MODULE_DESCRIPTION("wm8739 driver");
36MODULE_AUTHOR("T. Adachi, Hans Verkuil");
37MODULE_LICENSE("GPL");
38
Hans Verkuil099b8e72007-11-01 07:45:54 -030039static int debug;
Hans Verkuil75c45702006-03-28 18:23:48 -030040
41module_param(debug, int, 0644);
42
43MODULE_PARM_DESC(debug, "Debug level (0-1)");
44
45
Hans Verkuil75c45702006-03-28 18:23:48 -030046/* ------------------------------------------------------------------------ */
47
48enum {
49 R0 = 0, R1,
50 R5 = 5, R6, R7, R8, R9, R15 = 15,
51 TOT_REGS
52};
53
54struct wm8739_state {
Hans Verkuilf1460e92008-11-29 13:02:26 -030055 struct v4l2_subdev sd;
Hans Verkuil75c45702006-03-28 18:23:48 -030056 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
Hans Verkuilf1460e92008-11-29 13:02:26 -030064static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
65{
66 return container_of(sd, struct wm8739_state, sd);
67}
68
Hans Verkuil75c45702006-03-28 18:23:48 -030069/* ------------------------------------------------------------------------ */
70
Hans Verkuilf1460e92008-11-29 13:02:26 -030071static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
Hans Verkuil75c45702006-03-28 18:23:48 -030072{
Hans Verkuilf1460e92008-11-29 13:02:26 -030073 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -030074 int i;
75
76 if (reg < 0 || reg >= TOT_REGS) {
Hans Verkuilf1460e92008-11-29 13:02:26 -030077 v4l2_err(sd, "Invalid register R%d\n", reg);
Hans Verkuil75c45702006-03-28 18:23:48 -030078 return -1;
79 }
80
Hans Verkuilf1460e92008-11-29 13:02:26 -030081 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
Hans Verkuil75c45702006-03-28 18:23:48 -030082
Hans Verkuil099b8e72007-11-01 07:45:54 -030083 for (i = 0; i < 3; i++)
84 if (i2c_smbus_write_byte_data(client,
85 (reg << 1) | (val >> 8), val & 0xff) == 0)
Hans Verkuil75c45702006-03-28 18:23:48 -030086 return 0;
Hans Verkuilf1460e92008-11-29 13:02:26 -030087 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
Hans Verkuil75c45702006-03-28 18:23:48 -030088 return -1;
89}
90
91/* write regs to set audio volume etc */
Hans Verkuilf1460e92008-11-29 13:02:26 -030092static void wm8739_set_audio(struct v4l2_subdev *sd)
Hans Verkuil75c45702006-03-28 18:23:48 -030093{
Hans Verkuilf1460e92008-11-29 13:02:26 -030094 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -030095 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 */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300100 wm8739_write(sd, R0, (state->vol_l & 0x1f) | mute);
101 wm8739_write(sd, R1, (state->vol_r & 0x1f) | mute);
Hans Verkuil75c45702006-03-28 18:23:48 -0300102}
103
Hans Verkuilf1460e92008-11-29 13:02:26 -0300104static int wm8739_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Hans Verkuil75c45702006-03-28 18:23:48 -0300105{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300106 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300107
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
Hans Verkuilf1460e92008-11-29 13:02:26 -0300127static int wm8739_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Hans Verkuil75c45702006-03-28 18:23:48 -0300128{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300129 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300130 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. */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300157 wm8739_set_audio(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300158 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,
Hans Verkuil099b8e72007-11-01 07:45:54 -0300173 }, {
Hans Verkuil75c45702006-03-28 18:23:48 -0300174 .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,
Hans Verkuil099b8e72007-11-01 07:45:54 -0300182 }, {
Hans Verkuil75c45702006-03-28 18:23:48 -0300183 .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
Hans Verkuilf1460e92008-11-29 13:02:26 -0300196static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
Hans Verkuil75c45702006-03-28 18:23:48 -0300197{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300198 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300199
Hans Verkuilf1460e92008-11-29 13:02:26 -0300200 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);
Hans Verkuil75c45702006-03-28 18:23:48 -0300207 break;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300208 case 48000:
209 /* 256fps, fs=48k */
210 wm8739_write(sd, R8, 0x000);
Hans Verkuil75c45702006-03-28 18:23:48 -0300211 break;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300212 case 32000:
213 /* 256fps, fs=32k */
214 wm8739_write(sd, R8, 0x018);
215 break;
Hans Verkuil75c45702006-03-28 18:23:48 -0300216 default:
Hans Verkuilf1460e92008-11-29 13:02:26 -0300217 break;
Hans Verkuil75c45702006-03-28 18:23:48 -0300218 }
Hans Verkuilf1460e92008-11-29 13:02:26 -0300219 /* activate */
220 wm8739_write(sd, R9, 0x001);
Hans Verkuil75c45702006-03-28 18:23:48 -0300221 return 0;
222}
223
Hans Verkuilf1460e92008-11-29 13:02:26 -0300224static 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
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300236static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuilf1460e92008-11-29 13:02:26 -0300237{
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
243static 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
Hans Verkuilf1460e92008-11-29 13:02:26 -0300255/* ----------------------------------------------------------------------- */
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
Hans Verkuil75c45702006-03-28 18:23:48 -0300274/* ------------------------------------------------------------------------ */
275
276/* i2c implementation */
277
Jean Delvared2653e92008-04-29 23:11:39 +0200278static int wm8739_probe(struct i2c_client *client,
279 const struct i2c_device_id *id)
Hans Verkuil75c45702006-03-28 18:23:48 -0300280{
Hans Verkuil75c45702006-03-28 18:23:48 -0300281 struct wm8739_state *state;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300282 struct v4l2_subdev *sd;
Hans Verkuil75c45702006-03-28 18:23:48 -0300283
Hans Verkuil188f3452007-09-16 10:47:15 -0300284 /* Check if the adapter supports the needed features */
285 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
286 return -EIO;
287
Hans Verkuil099b8e72007-11-01 07:45:54 -0300288 v4l_info(client, "chip found @ 0x%x (%s)\n",
289 client->addr << 1, client->adapter->name);
Hans Verkuil75c45702006-03-28 18:23:48 -0300290
291 state = kmalloc(sizeof(struct wm8739_state), GFP_KERNEL);
Hans Verkuil6c832e32008-08-24 06:29:30 -0300292 if (state == NULL)
Hans Verkuil75c45702006-03-28 18:23:48 -0300293 return -ENOMEM;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300294 sd = &state->sd;
295 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
Hans Verkuil75c45702006-03-28 18:23:48 -0300296 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;
Hans Verkuil75c45702006-03-28 18:23:48 -0300303
Hans Verkuil099b8e72007-11-01 07:45:54 -0300304 /* Initialize wm8739 */
305
306 /* reset */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300307 wm8739_write(sd, R15, 0x00);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300308 /* filter setting, high path, offet clear */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300309 wm8739_write(sd, R5, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300310 /* ADC, OSC, Power Off mode Disable */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300311 wm8739_write(sd, R6, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300312 /* Digital Audio interface format:
313 Enable Master mode, 24 bit, MSB first/left justified */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300314 wm8739_write(sd, R7, 0x049);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300315 /* sampling control: normal, 256fs, 48KHz sampling rate */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300316 wm8739_write(sd, R8, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300317 /* activate */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300318 wm8739_write(sd, R9, 0x001);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300319 /* set volume/mute */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300320 wm8739_set_audio(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300321 return 0;
322}
323
Hans Verkuil49457cc2007-09-13 11:10:07 -0300324static int wm8739_remove(struct i2c_client *client)
Hans Verkuil75c45702006-03-28 18:23:48 -0300325{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300326 struct v4l2_subdev *sd = i2c_get_clientdata(client);
327
328 v4l2_device_unregister_subdev(sd);
329 kfree(to_state(sd));
Hans Verkuil75c45702006-03-28 18:23:48 -0300330 return 0;
331}
332
Jean Delvareaf294862008-05-18 20:49:40 +0200333static const struct i2c_device_id wm8739_id[] = {
334 { "wm8739", 0 },
335 { }
336};
337MODULE_DEVICE_TABLE(i2c, wm8739_id);
338
Hans Verkuil49457cc2007-09-13 11:10:07 -0300339static struct v4l2_i2c_driver_data v4l2_i2c_data = {
340 .name = "wm8739",
Hans Verkuil49457cc2007-09-13 11:10:07 -0300341 .probe = wm8739_probe,
342 .remove = wm8739_remove,
Jean Delvareaf294862008-05-18 20:49:40 +0200343 .id_table = wm8739_id,
Hans Verkuil75c45702006-03-28 18:23:48 -0300344};