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