blob: d5965543ecab72af2bfc1874c172137c2ab76b93 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Hans Verkuil75c45702006-03-28 18:23:48 -030027#include <linux/ioctl.h>
28#include <asm/uaccess.h>
29#include <linux/i2c.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 Verkuilf687d192010-05-24 10:18:26 -030034#include <media/v4l2-ctrls.h>
Hans Verkuil75c45702006-03-28 18:23:48 -030035
36MODULE_DESCRIPTION("wm8739 driver");
37MODULE_AUTHOR("T. Adachi, Hans Verkuil");
38MODULE_LICENSE("GPL");
39
Hans Verkuil099b8e72007-11-01 07:45:54 -030040static int debug;
Hans Verkuil75c45702006-03-28 18:23:48 -030041
42module_param(debug, int, 0644);
43
44MODULE_PARM_DESC(debug, "Debug level (0-1)");
45
46
Hans Verkuil75c45702006-03-28 18:23:48 -030047/* ------------------------------------------------------------------------ */
48
49enum {
50 R0 = 0, R1,
51 R5 = 5, R6, R7, R8, R9, R15 = 15,
52 TOT_REGS
53};
54
55struct wm8739_state {
Hans Verkuilf1460e92008-11-29 13:02:26 -030056 struct v4l2_subdev sd;
Hans Verkuilf687d192010-05-24 10:18:26 -030057 struct v4l2_ctrl_handler hdl;
58 struct {
59 /* audio cluster */
60 struct v4l2_ctrl *volume;
61 struct v4l2_ctrl *mute;
62 struct v4l2_ctrl *balance;
63 };
Hans Verkuil75c45702006-03-28 18:23:48 -030064 u32 clock_freq;
Hans Verkuil75c45702006-03-28 18:23:48 -030065};
66
Hans Verkuilf1460e92008-11-29 13:02:26 -030067static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
68{
69 return container_of(sd, struct wm8739_state, sd);
70}
71
Hans Verkuilf687d192010-05-24 10:18:26 -030072static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
73{
74 return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
75}
76
Hans Verkuil75c45702006-03-28 18:23:48 -030077/* ------------------------------------------------------------------------ */
78
Hans Verkuilf1460e92008-11-29 13:02:26 -030079static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
Hans Verkuil75c45702006-03-28 18:23:48 -030080{
Hans Verkuilf1460e92008-11-29 13:02:26 -030081 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -030082 int i;
83
84 if (reg < 0 || reg >= TOT_REGS) {
Hans Verkuilf1460e92008-11-29 13:02:26 -030085 v4l2_err(sd, "Invalid register R%d\n", reg);
Hans Verkuil75c45702006-03-28 18:23:48 -030086 return -1;
87 }
88
Hans Verkuilf1460e92008-11-29 13:02:26 -030089 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
Hans Verkuil75c45702006-03-28 18:23:48 -030090
Hans Verkuil099b8e72007-11-01 07:45:54 -030091 for (i = 0; i < 3; i++)
92 if (i2c_smbus_write_byte_data(client,
93 (reg << 1) | (val >> 8), val & 0xff) == 0)
Hans Verkuil75c45702006-03-28 18:23:48 -030094 return 0;
Hans Verkuilf1460e92008-11-29 13:02:26 -030095 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
Hans Verkuil75c45702006-03-28 18:23:48 -030096 return -1;
97}
98
Hans Verkuilf687d192010-05-24 10:18:26 -030099static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil75c45702006-03-28 18:23:48 -0300100{
Hans Verkuilf687d192010-05-24 10:18:26 -0300101 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300102 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300103 unsigned int work_l, work_r;
Hans Verkuilf687d192010-05-24 10:18:26 -0300104 u8 vol_l; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
105 u8 vol_r; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
106 u16 mute;
Hans Verkuil75c45702006-03-28 18:23:48 -0300107
108 switch (ctrl->id) {
Hans Verkuil75c45702006-03-28 18:23:48 -0300109 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil75c45702006-03-28 18:23:48 -0300110 break;
111
112 default:
113 return -EINVAL;
114 }
115
116 /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
Hans Verkuilf687d192010-05-24 10:18:26 -0300117 work_l = (min(65536 - state->balance->val, 32768) * state->volume->val) / 32768;
118 work_r = (min(state->balance->val, 32768) * state->volume->val) / 32768;
Hans Verkuil75c45702006-03-28 18:23:48 -0300119
Hans Verkuilf687d192010-05-24 10:18:26 -0300120 vol_l = (long)work_l * 31 / 65535;
121 vol_r = (long)work_r * 31 / 65535;
Hans Verkuil75c45702006-03-28 18:23:48 -0300122
123 /* set audio volume etc. */
Hans Verkuilf687d192010-05-24 10:18:26 -0300124 mute = state->mute->val ? 0x80 : 0;
125
126 /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
127 * Default setting: 0x17 = 0 dB
128 */
129 wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
130 wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
Hans Verkuil75c45702006-03-28 18:23:48 -0300131 return 0;
132}
133
134/* ------------------------------------------------------------------------ */
135
Hans Verkuilf1460e92008-11-29 13:02:26 -0300136static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
Hans Verkuil75c45702006-03-28 18:23:48 -0300137{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300138 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300139
Hans Verkuilf1460e92008-11-29 13:02:26 -0300140 state->clock_freq = audiofreq;
141 /* de-activate */
142 wm8739_write(sd, R9, 0x000);
143 switch (audiofreq) {
144 case 44100:
145 /* 256fps, fs=44.1k */
146 wm8739_write(sd, R8, 0x020);
Hans Verkuil75c45702006-03-28 18:23:48 -0300147 break;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300148 case 48000:
149 /* 256fps, fs=48k */
150 wm8739_write(sd, R8, 0x000);
Hans Verkuil75c45702006-03-28 18:23:48 -0300151 break;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300152 case 32000:
153 /* 256fps, fs=32k */
154 wm8739_write(sd, R8, 0x018);
155 break;
Hans Verkuil75c45702006-03-28 18:23:48 -0300156 default:
Hans Verkuilf1460e92008-11-29 13:02:26 -0300157 break;
Hans Verkuil75c45702006-03-28 18:23:48 -0300158 }
Hans Verkuilf1460e92008-11-29 13:02:26 -0300159 /* activate */
160 wm8739_write(sd, R9, 0x001);
Hans Verkuil75c45702006-03-28 18:23:48 -0300161 return 0;
162}
163
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300164static int wm8739_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuilf1460e92008-11-29 13:02:26 -0300165{
166 struct i2c_client *client = v4l2_get_subdevdata(sd);
167
168 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_WM8739, 0);
169}
170
171static int wm8739_log_status(struct v4l2_subdev *sd)
172{
173 struct wm8739_state *state = to_state(sd);
174
175 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
Hans Verkuilf687d192010-05-24 10:18:26 -0300176 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300177 return 0;
178}
179
Hans Verkuilf1460e92008-11-29 13:02:26 -0300180/* ----------------------------------------------------------------------- */
181
Hans Verkuilf687d192010-05-24 10:18:26 -0300182static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
183 .s_ctrl = wm8739_s_ctrl,
184};
185
Hans Verkuilf1460e92008-11-29 13:02:26 -0300186static const struct v4l2_subdev_core_ops wm8739_core_ops = {
187 .log_status = wm8739_log_status,
188 .g_chip_ident = wm8739_g_chip_ident,
Hans Verkuilf687d192010-05-24 10:18:26 -0300189 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
190 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
191 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
192 .g_ctrl = v4l2_subdev_g_ctrl,
193 .s_ctrl = v4l2_subdev_s_ctrl,
194 .queryctrl = v4l2_subdev_queryctrl,
195 .querymenu = v4l2_subdev_querymenu,
Hans Verkuilf1460e92008-11-29 13:02:26 -0300196};
197
198static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
199 .s_clock_freq = wm8739_s_clock_freq,
200};
201
202static const struct v4l2_subdev_ops wm8739_ops = {
203 .core = &wm8739_core_ops,
204 .audio = &wm8739_audio_ops,
205};
206
Hans Verkuil75c45702006-03-28 18:23:48 -0300207/* ------------------------------------------------------------------------ */
208
209/* i2c implementation */
210
Jean Delvared2653e92008-04-29 23:11:39 +0200211static int wm8739_probe(struct i2c_client *client,
212 const struct i2c_device_id *id)
Hans Verkuil75c45702006-03-28 18:23:48 -0300213{
Hans Verkuil75c45702006-03-28 18:23:48 -0300214 struct wm8739_state *state;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300215 struct v4l2_subdev *sd;
Hans Verkuil75c45702006-03-28 18:23:48 -0300216
Hans Verkuil188f3452007-09-16 10:47:15 -0300217 /* Check if the adapter supports the needed features */
218 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
219 return -EIO;
220
Hans Verkuil099b8e72007-11-01 07:45:54 -0300221 v4l_info(client, "chip found @ 0x%x (%s)\n",
222 client->addr << 1, client->adapter->name);
Hans Verkuil75c45702006-03-28 18:23:48 -0300223
Hans Verkuilf687d192010-05-24 10:18:26 -0300224 state = kzalloc(sizeof(struct wm8739_state), GFP_KERNEL);
Hans Verkuil6c832e32008-08-24 06:29:30 -0300225 if (state == NULL)
Hans Verkuil75c45702006-03-28 18:23:48 -0300226 return -ENOMEM;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300227 sd = &state->sd;
228 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
Hans Verkuilf687d192010-05-24 10:18:26 -0300229 v4l2_ctrl_handler_init(&state->hdl, 2);
230 state->volume = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
231 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 50736);
232 state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
233 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
234 state->balance = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
235 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
236 sd->ctrl_handler = &state->hdl;
237 if (state->hdl.error) {
238 int err = state->hdl.error;
239
240 v4l2_ctrl_handler_free(&state->hdl);
241 kfree(state);
242 return err;
243 }
244 v4l2_ctrl_cluster(3, &state->volume);
245
Hans Verkuil75c45702006-03-28 18:23:48 -0300246 state->clock_freq = 48000;
Hans Verkuil75c45702006-03-28 18:23:48 -0300247
Hans Verkuil099b8e72007-11-01 07:45:54 -0300248 /* Initialize wm8739 */
249
250 /* reset */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300251 wm8739_write(sd, R15, 0x00);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300252 /* filter setting, high path, offet clear */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300253 wm8739_write(sd, R5, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300254 /* ADC, OSC, Power Off mode Disable */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300255 wm8739_write(sd, R6, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300256 /* Digital Audio interface format:
257 Enable Master mode, 24 bit, MSB first/left justified */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300258 wm8739_write(sd, R7, 0x049);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300259 /* sampling control: normal, 256fs, 48KHz sampling rate */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300260 wm8739_write(sd, R8, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300261 /* activate */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300262 wm8739_write(sd, R9, 0x001);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300263 /* set volume/mute */
Hans Verkuilf687d192010-05-24 10:18:26 -0300264 v4l2_ctrl_handler_setup(&state->hdl);
Hans Verkuil75c45702006-03-28 18:23:48 -0300265 return 0;
266}
267
Hans Verkuil49457cc2007-09-13 11:10:07 -0300268static int wm8739_remove(struct i2c_client *client)
Hans Verkuil75c45702006-03-28 18:23:48 -0300269{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300270 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuilf687d192010-05-24 10:18:26 -0300271 struct wm8739_state *state = to_state(sd);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300272
273 v4l2_device_unregister_subdev(sd);
Hans Verkuilf687d192010-05-24 10:18:26 -0300274 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300275 kfree(to_state(sd));
Hans Verkuil75c45702006-03-28 18:23:48 -0300276 return 0;
277}
278
Jean Delvareaf294862008-05-18 20:49:40 +0200279static const struct i2c_device_id wm8739_id[] = {
280 { "wm8739", 0 },
281 { }
282};
283MODULE_DEVICE_TABLE(i2c, wm8739_id);
284
Hans Verkuil49457cc2007-09-13 11:10:07 -0300285static struct v4l2_i2c_driver_data v4l2_i2c_data = {
286 .name = "wm8739",
Hans Verkuil49457cc2007-09-13 11:10:07 -0300287 .probe = wm8739_probe,
288 .remove = wm8739_remove,
Jean Delvareaf294862008-05-18 20:49:40 +0200289 .id_table = wm8739_id,
Hans Verkuil75c45702006-03-28 18:23:48 -0300290};