blob: f086e5e6e8441778db6fabdd800b995819b64fd3 [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 Verkuilf687d192010-05-24 10:18:26 -030032#include <media/v4l2-ctrls.h>
Hans Verkuil75c45702006-03-28 18:23:48 -030033
34MODULE_DESCRIPTION("wm8739 driver");
35MODULE_AUTHOR("T. Adachi, Hans Verkuil");
36MODULE_LICENSE("GPL");
37
Hans Verkuil099b8e72007-11-01 07:45:54 -030038static int debug;
Hans Verkuil75c45702006-03-28 18:23:48 -030039
40module_param(debug, int, 0644);
41
42MODULE_PARM_DESC(debug, "Debug level (0-1)");
43
44
Hans Verkuil75c45702006-03-28 18:23:48 -030045/* ------------------------------------------------------------------------ */
46
47enum {
48 R0 = 0, R1,
49 R5 = 5, R6, R7, R8, R9, R15 = 15,
50 TOT_REGS
51};
52
53struct wm8739_state {
Hans Verkuilf1460e92008-11-29 13:02:26 -030054 struct v4l2_subdev sd;
Hans Verkuilf687d192010-05-24 10:18:26 -030055 struct v4l2_ctrl_handler hdl;
56 struct {
57 /* audio cluster */
58 struct v4l2_ctrl *volume;
59 struct v4l2_ctrl *mute;
60 struct v4l2_ctrl *balance;
61 };
Hans Verkuil75c45702006-03-28 18:23:48 -030062 u32 clock_freq;
Hans Verkuil75c45702006-03-28 18:23:48 -030063};
64
Hans Verkuilf1460e92008-11-29 13:02:26 -030065static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
66{
67 return container_of(sd, struct wm8739_state, sd);
68}
69
Hans Verkuilf687d192010-05-24 10:18:26 -030070static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
71{
72 return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
73}
74
Hans Verkuil75c45702006-03-28 18:23:48 -030075/* ------------------------------------------------------------------------ */
76
Hans Verkuilf1460e92008-11-29 13:02:26 -030077static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
Hans Verkuil75c45702006-03-28 18:23:48 -030078{
Hans Verkuilf1460e92008-11-29 13:02:26 -030079 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -030080 int i;
81
82 if (reg < 0 || reg >= TOT_REGS) {
Hans Verkuilf1460e92008-11-29 13:02:26 -030083 v4l2_err(sd, "Invalid register R%d\n", reg);
Hans Verkuil75c45702006-03-28 18:23:48 -030084 return -1;
85 }
86
Hans Verkuilf1460e92008-11-29 13:02:26 -030087 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
Hans Verkuil75c45702006-03-28 18:23:48 -030088
Hans Verkuil099b8e72007-11-01 07:45:54 -030089 for (i = 0; i < 3; i++)
90 if (i2c_smbus_write_byte_data(client,
91 (reg << 1) | (val >> 8), val & 0xff) == 0)
Hans Verkuil75c45702006-03-28 18:23:48 -030092 return 0;
Hans Verkuilf1460e92008-11-29 13:02:26 -030093 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
Hans Verkuil75c45702006-03-28 18:23:48 -030094 return -1;
95}
96
Hans Verkuilf687d192010-05-24 10:18:26 -030097static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil75c45702006-03-28 18:23:48 -030098{
Hans Verkuilf687d192010-05-24 10:18:26 -030099 struct v4l2_subdev *sd = to_sd(ctrl);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300100 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300101 unsigned int work_l, work_r;
Hans Verkuilf687d192010-05-24 10:18:26 -0300102 u8 vol_l; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
103 u8 vol_r; /* +12dB to -34.5dB 1.5dB step (5bit) def:0dB */
104 u16 mute;
Hans Verkuil75c45702006-03-28 18:23:48 -0300105
106 switch (ctrl->id) {
Hans Verkuil75c45702006-03-28 18:23:48 -0300107 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil75c45702006-03-28 18:23:48 -0300108 break;
109
110 default:
111 return -EINVAL;
112 }
113
114 /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
Hans Verkuilf687d192010-05-24 10:18:26 -0300115 work_l = (min(65536 - state->balance->val, 32768) * state->volume->val) / 32768;
116 work_r = (min(state->balance->val, 32768) * state->volume->val) / 32768;
Hans Verkuil75c45702006-03-28 18:23:48 -0300117
Hans Verkuilf687d192010-05-24 10:18:26 -0300118 vol_l = (long)work_l * 31 / 65535;
119 vol_r = (long)work_r * 31 / 65535;
Hans Verkuil75c45702006-03-28 18:23:48 -0300120
121 /* set audio volume etc. */
Hans Verkuilf687d192010-05-24 10:18:26 -0300122 mute = state->mute->val ? 0x80 : 0;
123
124 /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
125 * Default setting: 0x17 = 0 dB
126 */
127 wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
128 wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
Hans Verkuil75c45702006-03-28 18:23:48 -0300129 return 0;
130}
131
132/* ------------------------------------------------------------------------ */
133
Hans Verkuilf1460e92008-11-29 13:02:26 -0300134static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
Hans Verkuil75c45702006-03-28 18:23:48 -0300135{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300136 struct wm8739_state *state = to_state(sd);
Hans Verkuil75c45702006-03-28 18:23:48 -0300137
Hans Verkuilf1460e92008-11-29 13:02:26 -0300138 state->clock_freq = audiofreq;
139 /* de-activate */
140 wm8739_write(sd, R9, 0x000);
141 switch (audiofreq) {
142 case 44100:
143 /* 256fps, fs=44.1k */
144 wm8739_write(sd, R8, 0x020);
Hans Verkuil75c45702006-03-28 18:23:48 -0300145 break;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300146 case 48000:
147 /* 256fps, fs=48k */
148 wm8739_write(sd, R8, 0x000);
Hans Verkuil75c45702006-03-28 18:23:48 -0300149 break;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300150 case 32000:
151 /* 256fps, fs=32k */
152 wm8739_write(sd, R8, 0x018);
153 break;
Hans Verkuil75c45702006-03-28 18:23:48 -0300154 default:
Hans Verkuilf1460e92008-11-29 13:02:26 -0300155 break;
Hans Verkuil75c45702006-03-28 18:23:48 -0300156 }
Hans Verkuilf1460e92008-11-29 13:02:26 -0300157 /* activate */
158 wm8739_write(sd, R9, 0x001);
Hans Verkuil75c45702006-03-28 18:23:48 -0300159 return 0;
160}
161
Hans Verkuilf1460e92008-11-29 13:02:26 -0300162static int wm8739_log_status(struct v4l2_subdev *sd)
163{
164 struct wm8739_state *state = to_state(sd);
165
166 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
Hans Verkuilf687d192010-05-24 10:18:26 -0300167 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300168 return 0;
169}
170
Hans Verkuilf1460e92008-11-29 13:02:26 -0300171/* ----------------------------------------------------------------------- */
172
Hans Verkuilf687d192010-05-24 10:18:26 -0300173static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
174 .s_ctrl = wm8739_s_ctrl,
175};
176
Hans Verkuilf1460e92008-11-29 13:02:26 -0300177static const struct v4l2_subdev_core_ops wm8739_core_ops = {
178 .log_status = wm8739_log_status,
Hans Verkuilf1460e92008-11-29 13:02:26 -0300179};
180
181static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
182 .s_clock_freq = wm8739_s_clock_freq,
183};
184
185static const struct v4l2_subdev_ops wm8739_ops = {
186 .core = &wm8739_core_ops,
187 .audio = &wm8739_audio_ops,
188};
189
Hans Verkuil75c45702006-03-28 18:23:48 -0300190/* ------------------------------------------------------------------------ */
191
192/* i2c implementation */
193
Jean Delvared2653e92008-04-29 23:11:39 +0200194static int wm8739_probe(struct i2c_client *client,
195 const struct i2c_device_id *id)
Hans Verkuil75c45702006-03-28 18:23:48 -0300196{
Hans Verkuil75c45702006-03-28 18:23:48 -0300197 struct wm8739_state *state;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300198 struct v4l2_subdev *sd;
Hans Verkuil75c45702006-03-28 18:23:48 -0300199
Hans Verkuil188f3452007-09-16 10:47:15 -0300200 /* Check if the adapter supports the needed features */
201 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
202 return -EIO;
203
Hans Verkuil099b8e72007-11-01 07:45:54 -0300204 v4l_info(client, "chip found @ 0x%x (%s)\n",
205 client->addr << 1, client->adapter->name);
Hans Verkuil75c45702006-03-28 18:23:48 -0300206
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300207 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuil6c832e32008-08-24 06:29:30 -0300208 if (state == NULL)
Hans Verkuil75c45702006-03-28 18:23:48 -0300209 return -ENOMEM;
Hans Verkuilf1460e92008-11-29 13:02:26 -0300210 sd = &state->sd;
211 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
Hans Verkuilf687d192010-05-24 10:18:26 -0300212 v4l2_ctrl_handler_init(&state->hdl, 2);
213 state->volume = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
214 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 50736);
215 state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
216 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
217 state->balance = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
218 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
219 sd->ctrl_handler = &state->hdl;
220 if (state->hdl.error) {
221 int err = state->hdl.error;
222
223 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuilf687d192010-05-24 10:18:26 -0300224 return err;
225 }
226 v4l2_ctrl_cluster(3, &state->volume);
227
Hans Verkuil75c45702006-03-28 18:23:48 -0300228 state->clock_freq = 48000;
Hans Verkuil75c45702006-03-28 18:23:48 -0300229
Hans Verkuil099b8e72007-11-01 07:45:54 -0300230 /* Initialize wm8739 */
231
232 /* reset */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300233 wm8739_write(sd, R15, 0x00);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300234 /* filter setting, high path, offet clear */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300235 wm8739_write(sd, R5, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300236 /* ADC, OSC, Power Off mode Disable */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300237 wm8739_write(sd, R6, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300238 /* Digital Audio interface format:
239 Enable Master mode, 24 bit, MSB first/left justified */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300240 wm8739_write(sd, R7, 0x049);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300241 /* sampling control: normal, 256fs, 48KHz sampling rate */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300242 wm8739_write(sd, R8, 0x000);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300243 /* activate */
Hans Verkuilf1460e92008-11-29 13:02:26 -0300244 wm8739_write(sd, R9, 0x001);
Hans Verkuil099b8e72007-11-01 07:45:54 -0300245 /* set volume/mute */
Hans Verkuilf687d192010-05-24 10:18:26 -0300246 v4l2_ctrl_handler_setup(&state->hdl);
Hans Verkuil75c45702006-03-28 18:23:48 -0300247 return 0;
248}
249
Hans Verkuil49457cc2007-09-13 11:10:07 -0300250static int wm8739_remove(struct i2c_client *client)
Hans Verkuil75c45702006-03-28 18:23:48 -0300251{
Hans Verkuilf1460e92008-11-29 13:02:26 -0300252 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Hans Verkuilf687d192010-05-24 10:18:26 -0300253 struct wm8739_state *state = to_state(sd);
Hans Verkuilf1460e92008-11-29 13:02:26 -0300254
255 v4l2_device_unregister_subdev(sd);
Hans Verkuilf687d192010-05-24 10:18:26 -0300256 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuil75c45702006-03-28 18:23:48 -0300257 return 0;
258}
259
Jean Delvareaf294862008-05-18 20:49:40 +0200260static const struct i2c_device_id wm8739_id[] = {
261 { "wm8739", 0 },
262 { }
263};
264MODULE_DEVICE_TABLE(i2c, wm8739_id);
265
Hans Verkuil49facfa2010-09-15 14:58:16 -0300266static struct i2c_driver wm8739_driver = {
267 .driver = {
Hans Verkuil49facfa2010-09-15 14:58:16 -0300268 .name = "wm8739",
269 },
270 .probe = wm8739_probe,
271 .remove = wm8739_remove,
272 .id_table = wm8739_id,
Hans Verkuil75c45702006-03-28 18:23:48 -0300273};
Hans Verkuil49facfa2010-09-15 14:58:16 -0300274
Axel Linc6e8d862012-02-12 06:56:32 -0300275module_i2c_driver(wm8739_driver);