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