]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/i2c/msp3400-driver.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-bionic-kernel.git] / drivers / media / i2c / msp3400-driver.c
CommitLineData
53b0a1c6
HV
1/*
2 * Programming the mspx4xx sound processor family
3 *
4 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
5 *
6 * what works and what doesn't:
7 *
8 * AM-Mono
9 * Support for Hauppauge cards added (decoding handled by tuner) added by
10 * Frederic Crozat <fcrozat@mail.dotcom.fr>
11 *
12 * FM-Mono
13 * should work. The stereo modes are backward compatible to FM-mono,
14 * therefore FM-Mono should be allways available.
15 *
16 * FM-Stereo (B/G, used in germany)
17 * should work, with autodetect
18 *
19 * FM-Stereo (satellite)
20 * should work, no autodetect (i.e. default is mono, but you can
21 * switch to stereo -- untested)
22 *
23 * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
24 * should work, with autodetect. Support for NICAM was added by
25 * Pekka Pietikainen <pp@netppl.fi>
26 *
27 * TODO:
28 * - better SAT support
29 *
30 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
31 * using soundcore instead of OSS
32 *
33 * This program is free software; you can redistribute it and/or
34 * modify it under the terms of the GNU General Public License
35 * as published by the Free Software Foundation; either version 2
36 * of the License, or (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
53b0a1c6
HV
42 */
43
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/slab.h>
48#include <linux/i2c.h>
76efd62f
HV
49#include <linux/kthread.h>
50#include <linux/freezer.h>
2c26976d 51#include <linux/videodev2.h>
76efd62f 52#include <media/v4l2-device.h>
35ea11ff 53#include <media/v4l2-ioctl.h>
d647f0b7 54#include <media/drv-intf/msp3400.h>
b5dcee22 55#include <media/i2c/tvaudio.h>
49965a80 56#include "msp3400-driver.h"
53b0a1c6
HV
57
58/* ---------------------------------------------------------------------- */
59
60MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
61MODULE_AUTHOR("Gerd Knorr");
62MODULE_LICENSE("GPL");
63
64/* module parameters */
65static int opmode = OPMODE_AUTO;
a5ed425c 66int msp_debug; /* msp_debug output */
90ab5ee9
RR
67bool msp_once; /* no continuous stereo monitoring */
68bool msp_amsound; /* hard-wire AM sound at 6.5 Hz (france),
a5ed425c 69 the autoscan seems work well only with FM... */
d52c7385
HV
70int msp_standard = 1; /* Override auto detect of audio msp_standard,
71 if needed. */
90ab5ee9 72bool msp_dolby;
53b0a1c6 73
f167cb4e 74int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
53b0a1c6
HV
75 (msp34xxg only) 0x00a0-0x03c0 */
76
77/* read-only */
78module_param(opmode, int, 0444);
79
80/* read-write */
d52c7385
HV
81module_param_named(once, msp_once, bool, 0644);
82module_param_named(debug, msp_debug, int, 0644);
83module_param_named(stereo_threshold, msp_stereo_thresh, int, 0644);
84module_param_named(standard, msp_standard, int, 0644);
85module_param_named(amsound, msp_amsound, bool, 0644);
86module_param_named(dolby, msp_dolby, bool, 0644);
53b0a1c6
HV
87
88MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
89MODULE_PARM_DESC(once, "No continuous stereo monitoring");
90MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
91MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
92MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
93MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
70f23fd6 94MODULE_PARM_DESC(dolby, "Activates Dolby processing");
53b0a1c6
HV
95
96/* ---------------------------------------------------------------------- */
97
98/* control subaddress */
99#define I2C_MSP_CONTROL 0x00
100/* demodulator unit subaddress */
101#define I2C_MSP_DEM 0x10
102/* DSP unit subaddress */
103#define I2C_MSP_DSP 0x12
104
53b0a1c6 105
53b0a1c6
HV
106/* ----------------------------------------------------------------------- */
107/* functions for talking to the MSP3400C Sound processor */
108
109int msp_reset(struct i2c_client *client)
110{
111 /* reset and read revision code */
112 static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
113 static u8 reset_on[3] = { I2C_MSP_CONTROL, 0x00, 0x00 };
114 static u8 write[3] = { I2C_MSP_DSP + 1, 0x00, 0x1e };
115 u8 read[2];
116 struct i2c_msg reset[2] = {
1215af48
S
117 {
118 .addr = client->addr,
119 .flags = I2C_M_IGNORE_NAK,
120 .len = 3,
121 .buf = reset_off
122 },
123 {
124 .addr = client->addr,
125 .flags = I2C_M_IGNORE_NAK,
126 .len = 3,
127 .buf = reset_on
128 },
53b0a1c6
HV
129 };
130 struct i2c_msg test[2] = {
1215af48
S
131 {
132 .addr = client->addr,
133 .len = 3,
134 .buf = write
135 },
136 {
137 .addr = client->addr,
138 .flags = I2C_M_RD,
139 .len = 2,
140 .buf = read
141 },
53b0a1c6
HV
142 };
143
39ad799a 144 dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_reset\n");
53b0a1c6
HV
145 if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
146 i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
147 i2c_transfer(client->adapter, test, 2) != 2) {
39ad799a 148 dev_err(&client->dev, "chip reset failed\n");
53b0a1c6
HV
149 return -1;
150 }
151 return 0;
152}
153
154static int msp_read(struct i2c_client *client, int dev, int addr)
155{
156 int err, retval;
157 u8 write[3];
158 u8 read[2];
159 struct i2c_msg msgs[2] = {
1215af48
S
160 {
161 .addr = client->addr,
162 .len = 3,
163 .buf = write
164 },
165 {
166 .addr = client->addr,
167 .flags = I2C_M_RD,
168 .len = 2,
169 .buf = read
170 }
53b0a1c6
HV
171 };
172
173 write[0] = dev + 1;
174 write[1] = addr >> 8;
175 write[2] = addr & 0xff;
176
177 for (err = 0; err < 3; err++) {
178 if (i2c_transfer(client->adapter, msgs, 2) == 2)
179 break;
39ad799a 180 dev_warn(&client->dev, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
53b0a1c6 181 dev, addr);
f3a43d30 182 schedule_timeout_interruptible(msecs_to_jiffies(10));
53b0a1c6
HV
183 }
184 if (err == 3) {
39ad799a 185 dev_warn(&client->dev, "resetting chip, sound will go off.\n");
53b0a1c6
HV
186 msp_reset(client);
187 return -1;
188 }
189 retval = read[0] << 8 | read[1];
39ad799a 190 dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_read(0x%x, 0x%x): 0x%x\n",
d52c7385 191 dev, addr, retval);
53b0a1c6
HV
192 return retval;
193}
194
195int msp_read_dem(struct i2c_client *client, int addr)
196{
197 return msp_read(client, I2C_MSP_DEM, addr);
198}
199
200int msp_read_dsp(struct i2c_client *client, int addr)
201{
202 return msp_read(client, I2C_MSP_DSP, addr);
203}
204
205static int msp_write(struct i2c_client *client, int dev, int addr, int val)
206{
207 int err;
208 u8 buffer[5];
209
210 buffer[0] = dev;
211 buffer[1] = addr >> 8;
212 buffer[2] = addr & 0xff;
213 buffer[3] = val >> 8;
214 buffer[4] = val & 0xff;
215
39ad799a 216 dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_write(0x%x, 0x%x, 0x%x)\n",
d52c7385 217 dev, addr, val);
53b0a1c6
HV
218 for (err = 0; err < 3; err++) {
219 if (i2c_master_send(client, buffer, 5) == 5)
220 break;
39ad799a 221 dev_warn(&client->dev, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
53b0a1c6 222 dev, addr);
f3a43d30 223 schedule_timeout_interruptible(msecs_to_jiffies(10));
53b0a1c6
HV
224 }
225 if (err == 3) {
39ad799a 226 dev_warn(&client->dev, "resetting chip, sound will go off.\n");
53b0a1c6
HV
227 msp_reset(client);
228 return -1;
229 }
230 return 0;
231}
232
233int msp_write_dem(struct i2c_client *client, int addr, int val)
234{
235 return msp_write(client, I2C_MSP_DEM, addr, val);
236}
237
238int msp_write_dsp(struct i2c_client *client, int addr, int val)
239{
240 return msp_write(client, I2C_MSP_DSP, addr, val);
241}
242
243/* ----------------------------------------------------------------------- *
244 * bits 9 8 5 - SCART DSP input Select:
245 * 0 0 0 - SCART 1 to DSP input (reset position)
246 * 0 1 0 - MONO to DSP input
247 * 1 0 0 - SCART 2 to DSP input
248 * 1 1 1 - Mute DSP input
249 *
250 * bits 11 10 6 - SCART 1 Output Select:
251 * 0 0 0 - undefined (reset position)
252 * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
253 * 1 0 0 - MONO input to SCART 1 Output
254 * 1 1 0 - SCART 1 DA to SCART 1 Output
255 * 0 0 1 - SCART 2 DA to SCART 1 Output
256 * 0 1 1 - SCART 1 Input to SCART 1 Output
257 * 1 1 1 - Mute SCART 1 Output
258 *
259 * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART):
260 * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position)
261 * 0 1 0 - SCART 1 Input to SCART 2 Output
262 * 1 0 0 - MONO input to SCART 2 Output
263 * 0 0 1 - SCART 2 DA to SCART 2 Output
264 * 0 1 1 - SCART 2 Input to SCART 2 Output
265 * 1 1 0 - Mute SCART 2 Output
266 *
267 * Bits 4 to 0 should be zero.
268 * ----------------------------------------------------------------------- */
269
270static int scarts[3][9] = {
b930e1d8 271 /* MASK IN1 IN2 IN3 IN4 IN1_DA IN2_DA MONO MUTE */
53b0a1c6 272 /* SCART DSP Input select */
b930e1d8 273 { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1, -1, 0x0100, 0x0320 },
53b0a1c6 274 /* SCART1 Output select */
b930e1d8 275 { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
53b0a1c6 276 /* SCART2 Output select */
b930e1d8 277 { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
53b0a1c6
HV
278};
279
280static char *scart_names[] = {
b930e1d8 281 "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
53b0a1c6
HV
282};
283
284void msp_set_scart(struct i2c_client *client, int in, int out)
285{
76efd62f 286 struct msp_state *state = to_state(i2c_get_clientdata(client));
53b0a1c6 287
42772574 288 state->in_scart = in;
53b0a1c6 289
42772574
HV
290 if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
291 if (-1 == scarts[out][in + 1])
53b0a1c6
HV
292 return;
293
42772574
HV
294 state->acb &= ~scarts[out][0];
295 state->acb |= scarts[out][in + 1];
53b0a1c6
HV
296 } else
297 state->acb = 0xf60; /* Mute Input and SCART 1 Output */
298
39ad799a 299 dev_dbg_lvl(&client->dev, 1, msp_debug, "scart switch: %s => %d (ACB=0x%04x)\n",
d52c7385 300 scart_names[in], out, state->acb);
53b0a1c6
HV
301 msp_write_dsp(client, 0x13, state->acb);
302
303 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
dc555aa6
HV
304 if (state->has_i2s_conf)
305 msp_write_dem(client, 0x40, state->i2s_mode);
53b0a1c6
HV
306}
307
53b0a1c6
HV
308/* ------------------------------------------------------------------------ */
309
53b0a1c6
HV
310static void msp_wake_thread(struct i2c_client *client)
311{
76efd62f 312 struct msp_state *state = to_state(i2c_get_clientdata(client));
53b0a1c6
HV
313
314 if (NULL == state->kthread)
315 return;
53b0a1c6
HV
316 state->watch_stereo = 0;
317 state->restart = 1;
318 wake_up_interruptible(&state->wq);
319}
320
321int msp_sleep(struct msp_state *state, int timeout)
322{
323 DECLARE_WAITQUEUE(wait, current);
324
325 add_wait_queue(&state->wq, &wait);
326 if (!kthread_should_stop()) {
327 if (timeout < 0) {
328 set_current_state(TASK_INTERRUPTIBLE);
329 schedule();
330 } else {
331 schedule_timeout_interruptible
332 (msecs_to_jiffies(timeout));
333 }
334 }
335
336 remove_wait_queue(&state->wq, &wait);
337 try_to_freeze();
338 return state->restart;
339}
340
341/* ------------------------------------------------------------------------ */
53b0a1c6 342
ebc3bba5 343static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
53b0a1c6 344{
ebc3bba5
HV
345 struct msp_state *state = ctrl_to_state(ctrl);
346 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
347 int val = ctrl->val;
53b0a1c6
HV
348
349 switch (ctrl->id) {
ebc3bba5
HV
350 case V4L2_CID_AUDIO_VOLUME: {
351 /* audio volume cluster */
352 int reallymuted = state->muted->val | state->scan_in_progress;
353
354 if (!reallymuted)
355 val = (val * 0x7f / 65535) << 8;
356
39ad799a 357 dev_dbg_lvl(&client->dev, 1, msp_debug, "mute=%s scanning=%s volume=%d\n",
ebc3bba5
HV
358 state->muted->val ? "on" : "off",
359 state->scan_in_progress ? "yes" : "no",
360 state->volume->val);
361
362 msp_write_dsp(client, 0x0000, val);
363 msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
364 if (state->has_scart2_out_volume)
365 msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
366 if (state->has_headphones)
367 msp_write_dsp(client, 0x0006, val);
53b0a1c6 368 break;
53b0a1c6 369 }
53b0a1c6
HV
370
371 case V4L2_CID_AUDIO_BASS:
ebc3bba5
HV
372 val = ((val - 32768) * 0x60 / 65535) << 8;
373 msp_write_dsp(client, 0x0002, val);
374 if (state->has_headphones)
375 msp_write_dsp(client, 0x0031, val);
53b0a1c6
HV
376 break;
377
378 case V4L2_CID_AUDIO_TREBLE:
ebc3bba5
HV
379 val = ((val - 32768) * 0x60 / 65535) << 8;
380 msp_write_dsp(client, 0x0003, val);
381 if (state->has_headphones)
382 msp_write_dsp(client, 0x0032, val);
53b0a1c6
HV
383 break;
384
d312a46e 385 case V4L2_CID_AUDIO_LOUDNESS:
ebc3bba5
HV
386 val = val ? ((5 * 4) << 8) : 0;
387 msp_write_dsp(client, 0x0004, val);
388 if (state->has_headphones)
389 msp_write_dsp(client, 0x0033, val);
53b0a1c6
HV
390 break;
391
d312a46e 392 case V4L2_CID_AUDIO_BALANCE:
ebc3bba5
HV
393 val = (u8)((val / 256) - 128);
394 msp_write_dsp(client, 0x0001, val << 8);
395 if (state->has_headphones)
396 msp_write_dsp(client, 0x0030, val << 8);
53b0a1c6
HV
397 break;
398
399 default:
400 return -EINVAL;
401 }
53b0a1c6
HV
402 return 0;
403}
404
ebc3bba5
HV
405void msp_update_volume(struct msp_state *state)
406{
0310871d
HV
407 /* Force an update of the volume/mute cluster */
408 v4l2_ctrl_lock(state->volume);
409 state->volume->val = state->volume->cur.val;
410 state->muted->val = state->muted->cur.val;
411 msp_s_ctrl(state->volume);
412 v4l2_ctrl_unlock(state->volume);
ebc3bba5
HV
413}
414
76efd62f
HV
415/* --- v4l2 ioctls --- */
416static int msp_s_radio(struct v4l2_subdev *sd)
417{
418 struct msp_state *state = to_state(sd);
419 struct i2c_client *client = v4l2_get_subdevdata(sd);
53b0a1c6 420
76efd62f 421 if (state->radio)
53b0a1c6 422 return 0;
76efd62f 423 state->radio = 1;
39ad799a 424 dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to radio mode\n");
76efd62f
HV
425 state->watch_stereo = 0;
426 switch (state->opmode) {
427 case OPMODE_MANUAL:
428 /* set msp3400 to FM radio mode */
429 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
430 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
431 MSP_CARRIER(10.7));
ebc3bba5 432 msp_update_volume(state);
76efd62f
HV
433 break;
434 case OPMODE_AUTODETECT:
435 case OPMODE_AUTOSELECT:
436 /* the thread will do for us */
437 msp_wake_thread(client);
438 break;
53b0a1c6 439 }
76efd62f
HV
440 return 0;
441}
53b0a1c6 442
b530a447 443static int msp_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
76efd62f
HV
444{
445 struct i2c_client *client = v4l2_get_subdevdata(sd);
53b0a1c6 446
76efd62f
HV
447 /* new channel -- kick audio carrier scan */
448 msp_wake_thread(client);
449 return 0;
450}
2474ed44 451
5d1ed986
MCC
452static int msp_querystd(struct v4l2_subdev *sd, v4l2_std_id *id)
453{
454 struct msp_state *state = to_state(sd);
455 struct i2c_client *client = v4l2_get_subdevdata(sd);
456
457 *id &= state->detected_std;
458
39ad799a 459 dev_dbg_lvl(&client->dev, 2, msp_debug,
5d1ed986
MCC
460 "detected standard: %s(0x%08Lx)\n",
461 msp_standard_std_name(state->std), state->detected_std);
462
463 return 0;
464}
465
76efd62f
HV
466static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
467{
468 struct msp_state *state = to_state(sd);
469 struct i2c_client *client = v4l2_get_subdevdata(sd);
470 int update = state->radio || state->v4l2_std != id;
471
472 state->v4l2_std = id;
473 state->radio = 0;
474 if (update)
2eb606db 475 msp_wake_thread(client);
76efd62f
HV
476 return 0;
477}
478
5325b427
HV
479static int msp_s_routing(struct v4l2_subdev *sd,
480 u32 input, u32 output, u32 config)
76efd62f
HV
481{
482 struct msp_state *state = to_state(sd);
483 struct i2c_client *client = v4l2_get_subdevdata(sd);
5325b427
HV
484 int tuner = (input >> 3) & 1;
485 int sc_in = input & 0x7;
486 int sc1_out = output & 0xf;
487 int sc2_out = (output >> 4) & 0xf;
76efd62f
HV
488 u16 val, reg;
489 int i;
490 int extern_input = 1;
491
5325b427 492 if (state->route_in == input && state->route_out == output)
76efd62f 493 return 0;
5325b427
HV
494 state->route_in = input;
495 state->route_out = output;
76efd62f
HV
496 /* check if the tuner input is used */
497 for (i = 0; i < 5; i++) {
5325b427 498 if (((input >> (4 + i * 4)) & 0xf) == 0)
76efd62f 499 extern_input = 0;
53b0a1c6 500 }
76efd62f
HV
501 state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
502 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
503 msp_set_scart(client, sc_in, 0);
504 msp_set_scart(client, sc1_out, 1);
505 msp_set_scart(client, sc2_out, 2);
506 msp_set_audmode(client);
507 reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
508 val = msp_read_dem(client, reg);
509 msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
510 /* wake thread when a new input is chosen */
511 msp_wake_thread(client);
512 return 0;
513}
53b0a1c6 514
76efd62f
HV
515static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
516{
517 struct msp_state *state = to_state(sd);
518 struct i2c_client *client = v4l2_get_subdevdata(sd);
53b0a1c6 519
ddc6ff31 520 if (vt->type != V4L2_TUNER_ANALOG_TV)
76efd62f 521 return 0;
ddc6ff31
HV
522 if (!state->radio) {
523 if (state->opmode == OPMODE_AUTOSELECT)
524 msp_detect_stereo(client);
525 vt->rxsubchans = state->rxsubchans;
526 }
527 vt->audmode = state->audmode;
76efd62f
HV
528 vt->capability |= V4L2_TUNER_CAP_STEREO |
529 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
530 return 0;
531}
53b0a1c6 532
2f73c7c5 533static int msp_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
76efd62f
HV
534{
535 struct msp_state *state = to_state(sd);
536 struct i2c_client *client = v4l2_get_subdevdata(sd);
53b0a1c6 537
76efd62f
HV
538 if (state->radio) /* TODO: add mono/stereo support for radio */
539 return 0;
540 if (state->audmode == vt->audmode)
541 return 0;
542 state->audmode = vt->audmode;
543 /* only set audmode */
544 msp_set_audmode(client);
545 return 0;
546}
53b0a1c6 547
76efd62f
HV
548static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
549{
550 struct msp_state *state = to_state(sd);
551 struct i2c_client *client = v4l2_get_subdevdata(sd);
53b0a1c6 552
39ad799a 553 dev_dbg_lvl(&client->dev, 1, msp_debug, "Setting I2S speed to %d\n", freq);
53b0a1c6 554
76efd62f 555 switch (freq) {
d52c7385
HV
556 case 1024000:
557 state->i2s_mode = 0;
558 break;
559 case 2048000:
560 state->i2s_mode = 1;
561 break;
562 default:
563 return -EINVAL;
53b0a1c6 564 }
76efd62f
HV
565 return 0;
566}
53b0a1c6 567
76efd62f
HV
568static int msp_log_status(struct v4l2_subdev *sd)
569{
570 struct msp_state *state = to_state(sd);
571 struct i2c_client *client = v4l2_get_subdevdata(sd);
572 const char *p;
ebc3bba5 573 char prefix[V4L2_SUBDEV_NAME_SIZE + 20];
76efd62f
HV
574
575 if (state->opmode == OPMODE_AUTOSELECT)
576 msp_detect_stereo(client);
39ad799a 577 dev_info(&client->dev, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
76efd62f 578 client->name, state->rev1, state->rev2);
ebc3bba5
HV
579 snprintf(prefix, sizeof(prefix), "%s: Audio: ", sd->name);
580 v4l2_ctrl_handler_log_status(&state->hdl, prefix);
76efd62f 581 switch (state->mode) {
5af0c8f6
HV
582 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
583 case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
25985edc 584 case MSP_MODE_FM_TERRA: p = "Terrestrial FM-mono/stereo"; break;
5af0c8f6
HV
585 case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
586 case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
587 case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
588 case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
589 case MSP_MODE_BTSC: p = "BTSC"; break;
590 case MSP_MODE_EXTERN: p = "External input"; break;
591 default: p = "unknown"; break;
76efd62f
HV
592 }
593 if (state->mode == MSP_MODE_EXTERN) {
39ad799a 594 dev_info(&client->dev, "Mode: %s\n", p);
76efd62f 595 } else if (state->opmode == OPMODE_MANUAL) {
39ad799a 596 dev_info(&client->dev, "Mode: %s (%s%s)\n", p,
5af0c8f6
HV
597 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
598 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
76efd62f
HV
599 } else {
600 if (state->opmode == OPMODE_AUTODETECT)
39ad799a
MCC
601 dev_info(&client->dev, "Mode: %s\n", p);
602 dev_info(&client->dev, "Standard: %s (%s%s)\n",
5af0c8f6
HV
603 msp_standard_std_name(state->std),
604 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
605 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
53b0a1c6 606 }
39ad799a
MCC
607 dev_info(&client->dev, "Audmode: 0x%04x\n", state->audmode);
608 dev_info(&client->dev, "Routing: 0x%08x (input) 0x%08x (output)\n",
5325b427 609 state->route_in, state->route_out);
39ad799a 610 dev_info(&client->dev, "ACB: 0x%04x\n", state->acb);
53b0a1c6
HV
611 return 0;
612}
613
5cbd28df
MB
614#ifdef CONFIG_PM_SLEEP
615static int msp_suspend(struct device *dev)
53b0a1c6 616{
5cbd28df 617 struct i2c_client *client = to_i2c_client(dev);
39ad799a 618 dev_dbg_lvl(&client->dev, 1, msp_debug, "suspend\n");
53b0a1c6
HV
619 msp_reset(client);
620 return 0;
621}
622
5cbd28df 623static int msp_resume(struct device *dev)
53b0a1c6 624{
5cbd28df 625 struct i2c_client *client = to_i2c_client(dev);
39ad799a 626 dev_dbg_lvl(&client->dev, 1, msp_debug, "resume\n");
53b0a1c6
HV
627 msp_wake_thread(client);
628 return 0;
629}
5cbd28df 630#endif
53b0a1c6 631
76efd62f
HV
632/* ----------------------------------------------------------------------- */
633
ebc3bba5
HV
634static const struct v4l2_ctrl_ops msp_ctrl_ops = {
635 .s_ctrl = msp_s_ctrl,
636};
637
76efd62f
HV
638static const struct v4l2_subdev_core_ops msp_core_ops = {
639 .log_status = msp_log_status,
76efd62f
HV
640};
641
5d1ed986 642static const struct v4l2_subdev_video_ops msp_video_ops = {
8774bed9 643 .s_std = msp_s_std,
5d1ed986
MCC
644 .querystd = msp_querystd,
645};
646
76efd62f
HV
647static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
648 .s_frequency = msp_s_frequency,
649 .g_tuner = msp_g_tuner,
650 .s_tuner = msp_s_tuner,
651 .s_radio = msp_s_radio,
76efd62f
HV
652};
653
654static const struct v4l2_subdev_audio_ops msp_audio_ops = {
655 .s_routing = msp_s_routing,
656 .s_i2s_clock_freq = msp_s_i2s_clock_freq,
657};
658
659static const struct v4l2_subdev_ops msp_ops = {
660 .core = &msp_core_ops,
5d1ed986 661 .video = &msp_video_ops,
76efd62f
HV
662 .tuner = &msp_tuner_ops,
663 .audio = &msp_audio_ops,
664};
665
53b0a1c6
HV
666/* ----------------------------------------------------------------------- */
667
b68d75b9
MCC
668
669static const char * const opmode_str[] = {
670 [OPMODE_MANUAL] = "manual",
671 [OPMODE_AUTODETECT] = "autodetect",
672 [OPMODE_AUTOSELECT] = "autodetect and autoselect",
673};
674
d2653e92 675static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
53b0a1c6 676{
53b0a1c6 677 struct msp_state *state;
76efd62f 678 struct v4l2_subdev *sd;
ebc3bba5 679 struct v4l2_ctrl_handler *hdl;
53b0a1c6 680 int (*thread_func)(void *data) = NULL;
7560d7a4
HV
681 int msp_hard;
682 int msp_family;
683 int msp_revision;
684 int msp_product, msp_prod_hi, msp_prod_lo;
685 int msp_rom;
fb493282
MCC
686#if defined(CONFIG_MEDIA_CONTROLLER)
687 int ret;
688#endif
53b0a1c6 689
af294867
JD
690 if (!id)
691 strlcpy(client->name, "msp3400", sizeof(client->name));
53b0a1c6
HV
692
693 if (msp_reset(client) == -1) {
39ad799a 694 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
188f3457 695 return -ENODEV;
53b0a1c6
HV
696 }
697
c02b211d 698 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
d52c7385 699 if (!state)
53b0a1c6 700 return -ENOMEM;
b2a65760 701
76efd62f
HV
702 sd = &state->sd;
703 v4l2_i2c_subdev_init(sd, client, &msp_ops);
53b0a1c6 704
fb493282
MCC
705#if defined(CONFIG_MEDIA_CONTROLLER)
706 state->pads[IF_AUD_DEC_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
707 state->pads[IF_AUD_DEC_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
708
709 sd->entity.function = MEDIA_ENT_F_IF_AUD_DECODER;
710
711 ret = media_entity_pads_init(&sd->entity, 2, state->pads);
712 if (ret < 0)
713 return ret;
714#endif
715
5af0c8f6 716 state->v4l2_std = V4L2_STD_NTSC;
5d1ed986 717 state->detected_std = V4L2_STD_ALL;
0020d3ef 718 state->audmode = V4L2_TUNER_MODE_STEREO;
53b0a1c6 719 state->input = -1;
53b0a1c6
HV
720 state->i2s_mode = 0;
721 init_waitqueue_head(&state->wq);
2474ed44 722 /* These are the reset input/output positions */
5325b427
HV
723 state->route_in = MSP_INPUT_DEFAULT;
724 state->route_out = MSP_OUTPUT_DEFAULT;
53b0a1c6
HV
725
726 state->rev1 = msp_read_dsp(client, 0x1e);
727 if (state->rev1 != -1)
728 state->rev2 = msp_read_dsp(client, 0x1f);
39ad799a 729 dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
d52c7385 730 state->rev1, state->rev2);
53b0a1c6 731 if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
39ad799a 732 dev_dbg_lvl(&client->dev, 1, msp_debug,
d52c7385 733 "not an msp3400 (cannot read chip version)\n");
188f3457 734 return -ENODEV;
53b0a1c6
HV
735 }
736
7560d7a4
HV
737 msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
738 msp_product = (state->rev2 >> 8) & 0xff;
739 msp_prod_hi = msp_product / 10;
740 msp_prod_lo = msp_product % 10;
741 msp_revision = (state->rev1 & 0x0f) + '@';
742 msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
743 msp_rom = state->rev2 & 0x1f;
74cab31c 744 /* Rev B=2, C=3, D=4, G=7 */
d52c7385
HV
745 state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
746 msp_revision - '@';
7560d7a4
HV
747
748 /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
d52c7385
HV
749 state->has_nicam =
750 msp_prod_hi == 1 || msp_prod_hi == 5;
7560d7a4 751 /* Has radio support: was added with revision G */
d52c7385
HV
752 state->has_radio =
753 msp_revision >= 'G';
7560d7a4 754 /* Has headphones output: not for stripped down products */
d52c7385
HV
755 state->has_headphones =
756 msp_prod_lo < 5;
0020d3ef 757 /* Has scart2 input: not in stripped down products of the '3' family */
d52c7385
HV
758 state->has_scart2 =
759 msp_family >= 4 || msp_prod_lo < 7;
0020d3ef 760 /* Has scart3 input: not in stripped down products of the '3' family */
d52c7385
HV
761 state->has_scart3 =
762 msp_family >= 4 || msp_prod_lo < 5;
7560d7a4 763 /* Has scart4 input: not in pre D revisions, not in stripped D revs */
d52c7385
HV
764 state->has_scart4 =
765 msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
766 /* Has scart2 output: not in stripped down products of
767 * the '3' family */
768 state->has_scart2_out =
769 msp_family >= 4 || msp_prod_lo < 5;
d312a46e 770 /* Has scart2 a volume control? Not in pre-D revisions. */
d52c7385
HV
771 state->has_scart2_out_volume =
772 msp_revision > 'C' && state->has_scart2_out;
5af0c8f6 773 /* Has a configurable i2s out? */
d52c7385
HV
774 state->has_i2s_conf =
775 msp_revision >= 'G' && msp_prod_lo < 7;
776 /* Has subwoofer output: not in pre-D revs and not in stripped down
777 * products */
778 state->has_subwoofer =
779 msp_revision >= 'D' && msp_prod_lo < 5;
780 /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
781 * not in stripped down products */
782 state->has_sound_processing =
783 msp_prod_lo < 7;
7560d7a4 784 /* Has Virtual Dolby Surround: only in msp34x1 */
d52c7385
HV
785 state->has_virtual_dolby_surround =
786 msp_revision == 'G' && msp_prod_lo == 1;
7560d7a4 787 /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
d52c7385
HV
788 state->has_dolby_pro_logic =
789 msp_revision == 'G' && msp_prod_lo == 2;
790 /* The msp343xG supports BTSC only and cannot do Automatic Standard
791 * Detection. */
792 state->force_btsc =
793 msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
53b0a1c6
HV
794
795 state->opmode = opmode;
b68d75b9
MCC
796 if (state->opmode < OPMODE_MANUAL
797 || state->opmode > OPMODE_AUTOSELECT) {
53b0a1c6 798 /* MSP revision G and up have both autodetect and autoselect */
7560d7a4 799 if (msp_revision >= 'G')
53b0a1c6
HV
800 state->opmode = OPMODE_AUTOSELECT;
801 /* MSP revision D and up have autodetect */
7560d7a4 802 else if (msp_revision >= 'D')
53b0a1c6
HV
803 state->opmode = OPMODE_AUTODETECT;
804 else
805 state->opmode = OPMODE_MANUAL;
806 }
807
ebc3bba5
HV
808 hdl = &state->hdl;
809 v4l2_ctrl_handler_init(hdl, 6);
810 if (state->has_sound_processing) {
811 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
812 V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
813 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
814 V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
815 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
816 V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
817 }
818 state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
819 V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
820 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
821 V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
822 state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
823 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
824 sd->ctrl_handler = hdl;
825 if (hdl->error) {
826 int err = hdl->error;
827
828 v4l2_ctrl_handler_free(hdl);
ebc3bba5
HV
829 return err;
830 }
831
832 v4l2_ctrl_cluster(2, &state->volume);
833 v4l2_ctrl_handler_setup(hdl);
834
b68d75b9
MCC
835 dev_info(&client->dev,
836 "MSP%d4%02d%c-%c%d found on %s: supports %s%s%s, mode is %s\n",
837 msp_family, msp_product,
838 msp_revision, msp_hard, msp_rom,
839 client->adapter->name,
840 (state->has_nicam) ? "nicam" : "",
841 (state->has_nicam && state->has_radio) ? " and " : "",
842 (state->has_radio) ? "radio" : "",
843 opmode_str[state->opmode]);
53b0a1c6
HV
844
845 /* version-specific initialization */
846 switch (state->opmode) {
847 case OPMODE_MANUAL:
53b0a1c6
HV
848 thread_func = msp3400c_thread;
849 break;
850 case OPMODE_AUTODETECT:
53b0a1c6
HV
851 thread_func = msp3410d_thread;
852 break;
853 case OPMODE_AUTOSELECT:
53b0a1c6
HV
854 thread_func = msp34xxg_thread;
855 break;
856 }
53b0a1c6
HV
857
858 /* startup control thread if needed */
859 if (thread_func) {
860 state->kthread = kthread_run(thread_func, client, "msp34xx");
861
25821400 862 if (IS_ERR(state->kthread))
39ad799a 863 dev_warn(&client->dev, "kernel_thread() failed\n");
53b0a1c6
HV
864 msp_wake_thread(client);
865 }
53b0a1c6
HV
866 return 0;
867}
868
79518daf 869static int msp_remove(struct i2c_client *client)
53b0a1c6 870{
76efd62f 871 struct msp_state *state = to_state(i2c_get_clientdata(client));
53b0a1c6 872
76efd62f 873 v4l2_device_unregister_subdev(&state->sd);
53b0a1c6
HV
874 /* shutdown control thread */
875 if (state->kthread) {
876 state->restart = 1;
877 kthread_stop(state->kthread);
878 }
879 msp_reset(client);
880
ebc3bba5 881 v4l2_ctrl_handler_free(&state->hdl);
53b0a1c6
HV
882 return 0;
883}
884
885/* ----------------------------------------------------------------------- */
886
5cbd28df
MB
887static const struct dev_pm_ops msp3400_pm_ops = {
888 SET_SYSTEM_SLEEP_PM_OPS(msp_suspend, msp_resume)
889};
890
af294867
JD
891static const struct i2c_device_id msp_id[] = {
892 { "msp3400", 0 },
893 { }
894};
895MODULE_DEVICE_TABLE(i2c, msp_id);
896
61a489c8
HV
897static struct i2c_driver msp_driver = {
898 .driver = {
61a489c8 899 .name = "msp3400",
5cbd28df 900 .pm = &msp3400_pm_ops,
61a489c8
HV
901 },
902 .probe = msp_probe,
903 .remove = msp_remove,
61a489c8 904 .id_table = msp_id,
53b0a1c6
HV
905};
906
c6e8d86f 907module_i2c_driver(msp_driver);