1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/slab.h>
27#include <linux/ioctl.h>
28#include <asm/uaccess.h>
29#include <linux/i2c.h>
30#include <linux/videodev2.h>
31#include <media/v4l2-device.h>
32#include <media/v4l2-ctrls.h>
33
34MODULE_DESCRIPTION("wm8739 driver");
35MODULE_AUTHOR("T. Adachi, Hans Verkuil");
36MODULE_LICENSE("GPL");
37
38static int debug;
39
40module_param(debug, int, 0644);
41
42MODULE_PARM_DESC(debug, "Debug level (0-1)");
43
44
45
46
47enum {
48 R0 = 0, R1,
49 R5 = 5, R6, R7, R8, R9, R15 = 15,
50 TOT_REGS
51};
52
53struct wm8739_state {
54 struct v4l2_subdev sd;
55 struct v4l2_ctrl_handler hdl;
56 struct {
57
58 struct v4l2_ctrl *volume;
59 struct v4l2_ctrl *mute;
60 struct v4l2_ctrl *balance;
61 };
62 u32 clock_freq;
63};
64
65static inline struct wm8739_state *to_state(struct v4l2_subdev *sd)
66{
67 return container_of(sd, struct wm8739_state, sd);
68}
69
70static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
71{
72 return &container_of(ctrl->handler, struct wm8739_state, hdl)->sd;
73}
74
75
76
77static int wm8739_write(struct v4l2_subdev *sd, int reg, u16 val)
78{
79 struct i2c_client *client = v4l2_get_subdevdata(sd);
80 int i;
81
82 if (reg < 0 || reg >= TOT_REGS) {
83 v4l2_err(sd, "Invalid register R%d\n", reg);
84 return -1;
85 }
86
87 v4l2_dbg(1, debug, sd, "write: %02x %02x\n", reg, val);
88
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;
95}
96
97static int wm8739_s_ctrl(struct v4l2_ctrl *ctrl)
98{
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;
103 u8 vol_r;
104 u16 mute;
105
106 switch (ctrl->id) {
107 case V4L2_CID_AUDIO_VOLUME:
108 break;
109
110 default:
111 return -EINVAL;
112 }
113
114
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;
117
118 vol_l = (long)work_l * 31 / 65535;
119 vol_r = (long)work_r * 31 / 65535;
120
121
122 mute = state->mute->val ? 0x80 : 0;
123
124
125
126
127 wm8739_write(sd, R0, (vol_l & 0x1f) | mute);
128 wm8739_write(sd, R1, (vol_r & 0x1f) | mute);
129 return 0;
130}
131
132
133
134static int wm8739_s_clock_freq(struct v4l2_subdev *sd, u32 audiofreq)
135{
136 struct wm8739_state *state = to_state(sd);
137
138 state->clock_freq = audiofreq;
139
140 wm8739_write(sd, R9, 0x000);
141 switch (audiofreq) {
142 case 44100:
143
144 wm8739_write(sd, R8, 0x020);
145 break;
146 case 48000:
147
148 wm8739_write(sd, R8, 0x000);
149 break;
150 case 32000:
151
152 wm8739_write(sd, R8, 0x018);
153 break;
154 default:
155 break;
156 }
157
158 wm8739_write(sd, R9, 0x001);
159 return 0;
160}
161
162static 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);
167 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
168 return 0;
169}
170
171
172
173static const struct v4l2_ctrl_ops wm8739_ctrl_ops = {
174 .s_ctrl = wm8739_s_ctrl,
175};
176
177static const struct v4l2_subdev_core_ops wm8739_core_ops = {
178 .log_status = wm8739_log_status,
179 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
180 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
181 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
182 .g_ctrl = v4l2_subdev_g_ctrl,
183 .s_ctrl = v4l2_subdev_s_ctrl,
184 .queryctrl = v4l2_subdev_queryctrl,
185 .querymenu = v4l2_subdev_querymenu,
186};
187
188static const struct v4l2_subdev_audio_ops wm8739_audio_ops = {
189 .s_clock_freq = wm8739_s_clock_freq,
190};
191
192static const struct v4l2_subdev_ops wm8739_ops = {
193 .core = &wm8739_core_ops,
194 .audio = &wm8739_audio_ops,
195};
196
197
198
199
200
201static int wm8739_probe(struct i2c_client *client,
202 const struct i2c_device_id *id)
203{
204 struct wm8739_state *state;
205 struct v4l2_subdev *sd;
206
207
208 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
209 return -EIO;
210
211 v4l_info(client, "chip found @ 0x%x (%s)\n",
212 client->addr << 1, client->adapter->name);
213
214 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
215 if (state == NULL)
216 return -ENOMEM;
217 sd = &state->sd;
218 v4l2_i2c_subdev_init(sd, client, &wm8739_ops);
219 v4l2_ctrl_handler_init(&state->hdl, 2);
220 state->volume = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
221 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 50736);
222 state->mute = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
223 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
224 state->balance = v4l2_ctrl_new_std(&state->hdl, &wm8739_ctrl_ops,
225 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
226 sd->ctrl_handler = &state->hdl;
227 if (state->hdl.error) {
228 int err = state->hdl.error;
229
230 v4l2_ctrl_handler_free(&state->hdl);
231 return err;
232 }
233 v4l2_ctrl_cluster(3, &state->volume);
234
235 state->clock_freq = 48000;
236
237
238
239
240 wm8739_write(sd, R15, 0x00);
241
242 wm8739_write(sd, R5, 0x000);
243
244 wm8739_write(sd, R6, 0x000);
245
246
247 wm8739_write(sd, R7, 0x049);
248
249 wm8739_write(sd, R8, 0x000);
250
251 wm8739_write(sd, R9, 0x001);
252
253 v4l2_ctrl_handler_setup(&state->hdl);
254 return 0;
255}
256
257static int wm8739_remove(struct i2c_client *client)
258{
259 struct v4l2_subdev *sd = i2c_get_clientdata(client);
260 struct wm8739_state *state = to_state(sd);
261
262 v4l2_device_unregister_subdev(sd);
263 v4l2_ctrl_handler_free(&state->hdl);
264 return 0;
265}
266
267static const struct i2c_device_id wm8739_id[] = {
268 { "wm8739", 0 },
269 { }
270};
271MODULE_DEVICE_TABLE(i2c, wm8739_id);
272
273static struct i2c_driver wm8739_driver = {
274 .driver = {
275 .owner = THIS_MODULE,
276 .name = "wm8739",
277 },
278 .probe = wm8739_probe,
279 .remove = wm8739_remove,
280 .id_table = wm8739_id,
281};
282
283module_i2c_driver(wm8739_driver);
284