sh_eth: R8A7740 supports packet shecksumming
[linux/fpc-iii.git] / drivers / media / i2c / wm8739.c
blobc885def54b15a0a65ec03d2f19621613e0dc4597
1 /*
2 * wm8739
4 * Copyright (C) 2005 T. Adachi <tadachi@tadachi-net.com>
6 * Copyright (C) 2005 Hans Verkuil <hverkuil@xs4all.nl>
7 * - Cleanup
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.
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.
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.
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/slab.h>
27 #include <linux/ioctl.h>
28 #include <linux/uaccess.h>
29 #include <linux/i2c.h>
30 #include <linux/videodev2.h>
31 #include <media/v4l2-device.h>
32 #include <media/v4l2-ctrls.h>
34 MODULE_DESCRIPTION("wm8739 driver");
35 MODULE_AUTHOR("T. Adachi, Hans Verkuil");
36 MODULE_LICENSE("GPL");
38 static int debug;
40 module_param(debug, int, 0644);
42 MODULE_PARM_DESC(debug, "Debug level (0-1)");
45 /* ------------------------------------------------------------------------ */
47 enum {
48 R0 = 0, R1,
49 R5 = 5, R6, R7, R8, R9, R15 = 15,
50 TOT_REGS
53 struct wm8739_state {
54 struct v4l2_subdev sd;
55 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;
62 u32 clock_freq;
65 static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
67 return container_of(sd, struct wm8739_state, sd);
70 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
72 return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
75 /* ------------------------------------------------------------------------ */
77 static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
79 struct i2c_client *client = v4l2_get_subdevdata(sd);
80 int i;
82 if (reg < 0 || reg >= TOT_REGS) {
83 v4l2_err(sd, "Invalid register R%d\n", reg);
84 return -1;
87 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
89 for (i = 0; i < 3; i++)
90 if (i2c_smbus_write_byte_data(client,
91 (reg << 1) | (val >> 8), val & 0xff) == 0)
92 return 0;
93 v4l2_err(sd, "I2C: cannot write %03x to register R%d\n", val, reg);
94 return -1;
97 static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
99 struct v4l2_subdev *sd = to_sd(ctrl);
100 struct wm8739_state *state = to_state(sd);
101 unsigned int work_l, work_r;
102 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;
106 switch (ctrl->id) {
107 case V4L2_CID_AUDIO_VOLUME:
108 break;
110 default:
111 return -EINVAL;
114 /* normalize ( 65535 to 0 -> 31 to 0 (12dB to -34.5dB) ) */
115 work_l = (min(65536 - state->balance->val, 32768) * state->volume->val) / 32768;
116 work_r = (min(state->balance->val, 32768) * state->volume->val) / 32768;
118 vol_l = (long)work_l * 31 / 65535;
119 vol_r = (long)work_r * 31 / 65535;
121 /* set audio volume etc. */
122 mute = state->mute->val ? 0x80 : 0;
124 /* Volume setting: bits 0-4, 0x1f = 12 dB, 0x00 = -34.5 dB
125 * Default setting: 0x17 = 0 dB
127 wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
128 wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
129 return 0;
132 /* ------------------------------------------------------------------------ */
134 static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
136 struct wm8739_state *state = to_state(sd);
138 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);
145 break;
146 case 48000:
147 /* 256fps, fs=48k */
148 wm8739_write(sd, R8, 0x000);
149 break;
150 case 32000:
151 /* 256fps, fs=32k */
152 wm8739_write(sd, R8, 0x018);
153 break;
154 default:
155 break;
157 /* activate */
158 wm8739_write(sd, R9, 0x001);
159 return 0;
162 static int wm8739_log_status(struct v4l2_subdev *sd)
164 struct wm8739_state *state = to_state(sd);
166 v4l2_info(sd, "Frequency: %u Hz\n", state->clock_freq);
167 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
168 return 0;
171 /* ----------------------------------------------------------------------- */
173 static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
174 .s_ctrl = wm8739_s_ctrl,
177 static const struct v4l2_subdev_core_ops wm8739_core_ops = {
178 .log_status = wm8739_log_status,
181 static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
182 .s_clock_freq = wm8739_s_clock_freq,
185 static const struct v4l2_subdev_ops wm8739_ops = {
186 .core = &wm8739_core_ops,
187 .audio = &wm8739_audio_ops,
190 /* ------------------------------------------------------------------------ */
192 /* i2c implementation */
194 static int wm8739_probe(struct i2c_client *client,
195 const struct i2c_device_id *id)
197 struct wm8739_state *state;
198 struct v4l2_subdev *sd;
200 /* Check if the adapter supports the needed features */
201 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
202 return -EIO;
204 v4l_info(client, "chip found @ 0x%x (%s)\n",
205 client->addr << 1, client->adapter->name);
207 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
208 if (state == NULL)
209 return -ENOMEM;
210 sd = &state->sd;
211 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
212 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;
223 v4l2_ctrl_handler_free(&state->hdl);
224 return err;
226 v4l2_ctrl_cluster(3, &state->volume);
228 state->clock_freq = 48000;
230 /* Initialize wm8739 */
232 /* reset */
233 wm8739_write(sd, R15, 0x00);
234 /* filter setting, high path, offet clear */
235 wm8739_write(sd, R5, 0x000);
236 /* ADC, OSC, Power Off mode Disable */
237 wm8739_write(sd, R6, 0x000);
238 /* Digital Audio interface format:
239 Enable Master mode, 24 bit, MSB first/left justified */
240 wm8739_write(sd, R7, 0x049);
241 /* sampling control: normal, 256fs, 48KHz sampling rate */
242 wm8739_write(sd, R8, 0x000);
243 /* activate */
244 wm8739_write(sd, R9, 0x001);
245 /* set volume/mute */
246 v4l2_ctrl_handler_setup(&state->hdl);
247 return 0;
250 static int wm8739_remove(struct i2c_client *client)
252 struct v4l2_subdev *sd = i2c_get_clientdata(client);
253 struct wm8739_state *state = to_state(sd);
255 v4l2_device_unregister_subdev(sd);
256 v4l2_ctrl_handler_free(&state->hdl);
257 return 0;
260 static const struct i2c_device_id wm8739_id[] = {
261 { "wm8739", 0 },
264 MODULE_DEVICE_TABLE(i2c, wm8739_id);
266 static struct i2c_driver wm8739_driver = {
267 .driver = {
268 .name = "wm8739",
270 .probe = wm8739_probe,
271 .remove = wm8739_remove,
272 .id_table = wm8739_id,
275 module_i2c_driver(wm8739_driver);