blob: 3db966db83eb50c6888549c062fefd05ae591da9 [file] [log] [blame]
Hans Verkuil53b0a1c2006-01-09 15:32:39 -02001/*
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.
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020042 */
43
44
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/slab.h>
48#include <linux/i2c.h>
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020049#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080050#include <linux/freezer.h>
Mauro Carvalho Chehab2c269762009-03-30 08:18:10 -030051#include <linux/videodev2.h>
Hans Verkuil76efd622008-11-24 18:16:46 -030052#include <media/v4l2-device.h>
53#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehabd647f0b2015-11-13 19:40:07 -020054#include <media/drv-intf/msp3400.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020055#include <media/i2c/tvaudio.h>
Hans Verkuil49965a82006-03-19 08:45:38 -030056#include "msp3400-driver.h"
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020057
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;
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020066int msp_debug; /* msp_debug output */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103067bool msp_once; /* no continuous stereo monitoring */
68bool msp_amsound; /* hard-wire AM sound at 6.5 Hz (france),
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020069 the autoscan seems work well only with FM... */
Hans Verkuild52c7382007-12-12 08:25:18 -030070int msp_standard = 1; /* Override auto detect of audio msp_standard,
71 if needed. */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103072bool msp_dolby;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020073
Mauro Carvalho Chehabf167cb4e2006-01-11 19:41:49 -020074int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020075 (msp34xxg only) 0x00a0-0x03c0 */
76
77/* read-only */
78module_param(opmode, int, 0444);
79
80/* read-write */
Hans Verkuild52c7382007-12-12 08:25:18 -030081module_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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020087
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");
Justin P. Mattock70f23fd2011-05-10 10:16:21 +020094MODULE_PARM_DESC(dolby, "Activates Dolby processing");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -020095
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
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200105
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200106/* ----------------------------------------------------------------------- */
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] = {
Shubhrajyoti D1215af42012-09-18 08:22:34 -0300117 {
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 },
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200129 };
130 struct i2c_msg test[2] = {
Shubhrajyoti D1215af42012-09-18 08:22:34 -0300131 {
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 },
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200142 };
143
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200144 dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_reset\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200145 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) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200148 dev_err(&client->dev, "chip reset failed\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200149 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] = {
Shubhrajyoti D1215af42012-09-18 08:22:34 -0300160 {
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 }
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200171 };
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;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200180 dev_warn(&client->dev, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200181 dev, addr);
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -0300182 schedule_timeout_interruptible(msecs_to_jiffies(10));
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200183 }
184 if (err == 3) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200185 dev_warn(&client->dev, "resetting chip, sound will go off.\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200186 msp_reset(client);
187 return -1;
188 }
189 retval = read[0] << 8 | read[1];
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200190 dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_read(0x%x, 0x%x): 0x%x\n",
Hans Verkuild52c7382007-12-12 08:25:18 -0300191 dev, addr, retval);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200192 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
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200216 dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_write(0x%x, 0x%x, 0x%x)\n",
Hans Verkuild52c7382007-12-12 08:25:18 -0300217 dev, addr, val);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200218 for (err = 0; err < 3; err++) {
219 if (i2c_master_send(client, buffer, 5) == 5)
220 break;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200221 dev_warn(&client->dev, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200222 dev, addr);
Robert P. J. Dayf3a43d32007-07-16 10:46:42 -0300223 schedule_timeout_interruptible(msecs_to_jiffies(10));
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200224 }
225 if (err == 3) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200226 dev_warn(&client->dev, "resetting chip, sound will go off.\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200227 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] = {
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300271 /* MASK IN1 IN2 IN3 IN4 IN1_DA IN2_DA MONO MUTE */
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200272 /* SCART DSP Input select */
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300273 { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1, -1, 0x0100, 0x0320 },
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200274 /* SCART1 Output select */
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300275 { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200276 /* SCART2 Output select */
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300277 { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200278};
279
280static char *scart_names[] = {
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300281 "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200282};
283
284void msp_set_scart(struct i2c_client *client, int in, int out)
285{
Hans Verkuil76efd622008-11-24 18:16:46 -0300286 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200287
Hans Verkuil42772572006-03-19 06:48:51 -0300288 state->in_scart = in;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200289
Hans Verkuil42772572006-03-19 06:48:51 -0300290 if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
291 if (-1 == scarts[out][in + 1])
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200292 return;
293
Hans Verkuil42772572006-03-19 06:48:51 -0300294 state->acb &= ~scarts[out][0];
295 state->acb |= scarts[out][in + 1];
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200296 } else
297 state->acb = 0xf60; /* Mute Input and SCART 1 Output */
298
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200299 dev_dbg_lvl(&client->dev, 1, msp_debug, "scart switch: %s => %d (ACB=0x%04x)\n",
Hans Verkuild52c7382007-12-12 08:25:18 -0300300 scart_names[in], out, state->acb);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200301 msp_write_dsp(client, 0x13, state->acb);
302
303 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
Hans Verkuildc555aa2006-03-28 18:32:52 -0300304 if (state->has_i2s_conf)
305 msp_write_dem(client, 0x40, state->i2s_mode);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200306}
307
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200308/* ------------------------------------------------------------------------ */
309
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200310static void msp_wake_thread(struct i2c_client *client)
311{
Hans Verkuil76efd622008-11-24 18:16:46 -0300312 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200313
314 if (NULL == state->kthread)
315 return;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200316 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/* ------------------------------------------------------------------------ */
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200342
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300343static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200344{
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300345 struct msp_state *state = ctrl_to_state(ctrl);
346 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
347 int val = ctrl->val;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200348
349 switch (ctrl->id) {
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300350 case V4L2_CID_AUDIO_VOLUME: {
351 /* audio volume cluster */
352 int reallymuted = state->muted->val | state->scan_in_progress;
Hans Verkuild312a462006-01-09 18:21:36 -0200353
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300354 if (!reallymuted)
355 val = (val * 0x7f / 65535) << 8;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200356
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200357 dev_dbg_lvl(&client->dev, 1, msp_debug, "mute=%s scanning=%s volume=%d\n",
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300358 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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200368 break;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300369 }
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200370
371 case V4L2_CID_AUDIO_BASS:
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300372 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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200376 break;
377
378 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300379 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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200383 break;
384
Hans Verkuild312a462006-01-09 18:21:36 -0200385 case V4L2_CID_AUDIO_LOUDNESS:
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300386 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);
390 break;
391
392 case V4L2_CID_AUDIO_BALANCE:
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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200397 break;
398
399 default:
400 return -EINVAL;
401 }
402 return 0;
403}
404
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300405void msp_update_volume(struct msp_state *state)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200406{
Hans Verkuil03108712010-10-17 07:24:20 -0300407 /* 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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200413}
414
Hans Verkuil76efd622008-11-24 18:16:46 -0300415/* --- 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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200420
Hans Verkuil76efd622008-11-24 18:16:46 -0300421 if (state->radio)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200422 return 0;
Hans Verkuil76efd622008-11-24 18:16:46 -0300423 state->radio = 1;
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200424 dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to radio mode\n");
Hans Verkuil76efd622008-11-24 18:16:46 -0300425 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));
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300432 msp_update_volume(state);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300433 break;
Hans Verkuil76efd622008-11-24 18:16:46 -0300434 case OPMODE_AUTODETECT:
435 case OPMODE_AUTOSELECT:
436 /* the thread will do for us */
Hans Verkuil2eb606d2006-04-02 08:21:02 -0300437 msp_wake_thread(client);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200438 break;
439 }
Hans Verkuil76efd622008-11-24 18:16:46 -0300440 return 0;
441}
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200442
Hans Verkuilb530a442013-03-19 04:09:26 -0300443static int msp_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
Hans Verkuil76efd622008-11-24 18:16:46 -0300444{
445 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200446
Hans Verkuil76efd622008-11-24 18:16:46 -0300447 /* new channel -- kick audio carrier scan */
448 msp_wake_thread(client);
449 return 0;
450}
451
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300452static 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
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200459 dev_dbg_lvl(&client->dev, 2, msp_debug,
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300460 "detected standard: %s(0x%08Lx)\n",
461 msp_standard_std_name(state->std), state->detected_std);
462
463 return 0;
464}
465
Hans Verkuil76efd622008-11-24 18:16:46 -0300466static 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)
475 msp_wake_thread(client);
476 return 0;
477}
478
Hans Verkuil5325b422009-04-02 11:26:22 -0300479static int msp_s_routing(struct v4l2_subdev *sd,
480 u32 input, u32 output, u32 config)
Hans Verkuil76efd622008-11-24 18:16:46 -0300481{
482 struct msp_state *state = to_state(sd);
483 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil5325b422009-04-02 11:26:22 -0300484 int tuner = (input >> 3) & 1;
485 int sc_in = input & 0x7;
486 int sc1_out = output & 0xf;
487 int sc2_out = (output >> 4) & 0xf;
Hans Verkuil76efd622008-11-24 18:16:46 -0300488 u16 val, reg;
489 int i;
490 int extern_input = 1;
491
Hans Verkuil5325b422009-04-02 11:26:22 -0300492 if (state->route_in == input && state->route_out == output)
Hans Verkuil76efd622008-11-24 18:16:46 -0300493 return 0;
Hans Verkuil5325b422009-04-02 11:26:22 -0300494 state->route_in = input;
495 state->route_out = output;
Hans Verkuil76efd622008-11-24 18:16:46 -0300496 /* check if the tuner input is used */
497 for (i = 0; i < 5; i++) {
Hans Verkuil5325b422009-04-02 11:26:22 -0300498 if (((input >> (4 + i * 4)) & 0xf) == 0)
Hans Verkuil76efd622008-11-24 18:16:46 -0300499 extern_input = 0;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200500 }
Hans Verkuil76efd622008-11-24 18:16:46 -0300501 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}
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200514
Hans Verkuil76efd622008-11-24 18:16:46 -0300515static 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);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200519
Hans Verkuilddc6ff32011-06-26 06:16:03 -0300520 if (vt->type != V4L2_TUNER_ANALOG_TV)
Hans Verkuil76efd622008-11-24 18:16:46 -0300521 return 0;
Hans Verkuilddc6ff32011-06-26 06:16:03 -0300522 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;
Hans Verkuil76efd622008-11-24 18:16:46 -0300528 vt->capability |= V4L2_TUNER_CAP_STEREO |
529 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
530 return 0;
531}
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200532
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300533static int msp_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
Hans Verkuil76efd622008-11-24 18:16:46 -0300534{
535 struct msp_state *state = to_state(sd);
536 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200537
Hans Verkuil76efd622008-11-24 18:16:46 -0300538 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}
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200547
Hans Verkuil76efd622008-11-24 18:16:46 -0300548static 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);
552
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200553 dev_dbg_lvl(&client->dev, 1, msp_debug, "Setting I2S speed to %d\n", freq);
Hans Verkuil76efd622008-11-24 18:16:46 -0300554
555 switch (freq) {
Hans Verkuild52c7382007-12-12 08:25:18 -0300556 case 1024000:
557 state->i2s_mode = 0;
558 break;
559 case 2048000:
560 state->i2s_mode = 1;
561 break;
562 default:
563 return -EINVAL;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200564 }
Hans Verkuil76efd622008-11-24 18:16:46 -0300565 return 0;
566}
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200567
Hans Verkuil76efd622008-11-24 18:16:46 -0300568static 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;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300573 char prefix[V4L2_SUBDEV_NAME_SIZE + 20];
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200574
Hans Verkuil76efd622008-11-24 18:16:46 -0300575 if (state->opmode == OPMODE_AUTOSELECT)
576 msp_detect_stereo(client);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200577 dev_info(&client->dev, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
Hans Verkuil76efd622008-11-24 18:16:46 -0300578 client->name, state->rev1, state->rev2);
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300579 snprintf(prefix, sizeof(prefix), "%s: Audio: ", sd->name);
580 v4l2_ctrl_handler_log_status(&state->hdl, prefix);
Hans Verkuil76efd622008-11-24 18:16:46 -0300581 switch (state->mode) {
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200582 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
583 case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300584 case MSP_MODE_FM_TERRA: p = "Terrestrial FM-mono/stereo"; break;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200585 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;
Hans Verkuil76efd622008-11-24 18:16:46 -0300592 }
593 if (state->mode == MSP_MODE_EXTERN) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200594 dev_info(&client->dev, "Mode: %s\n", p);
Hans Verkuil76efd622008-11-24 18:16:46 -0300595 } else if (state->opmode == OPMODE_MANUAL) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200596 dev_info(&client->dev, "Mode: %s (%s%s)\n", p,
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200597 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
598 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
Hans Verkuil76efd622008-11-24 18:16:46 -0300599 } else {
600 if (state->opmode == OPMODE_AUTODETECT)
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200601 dev_info(&client->dev, "Mode: %s\n", p);
602 dev_info(&client->dev, "Standard: %s (%s%s)\n",
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200603 msp_standard_std_name(state->std),
604 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
605 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200606 }
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200607 dev_info(&client->dev, "Audmode: 0x%04x\n", state->audmode);
608 dev_info(&client->dev, "Routing: 0x%08x (input) 0x%08x (output)\n",
Hans Verkuil5325b422009-04-02 11:26:22 -0300609 state->route_in, state->route_out);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200610 dev_info(&client->dev, "ACB: 0x%04x\n", state->acb);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200611 return 0;
612}
613
Mark Brown5cbd28d2012-05-03 06:54:51 -0300614#ifdef CONFIG_PM_SLEEP
615static int msp_suspend(struct device *dev)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200616{
Mark Brown5cbd28d2012-05-03 06:54:51 -0300617 struct i2c_client *client = to_i2c_client(dev);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200618 dev_dbg_lvl(&client->dev, 1, msp_debug, "suspend\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200619 msp_reset(client);
620 return 0;
621}
622
Mark Brown5cbd28d2012-05-03 06:54:51 -0300623static int msp_resume(struct device *dev)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200624{
Mark Brown5cbd28d2012-05-03 06:54:51 -0300625 struct i2c_client *client = to_i2c_client(dev);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200626 dev_dbg_lvl(&client->dev, 1, msp_debug, "resume\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200627 msp_wake_thread(client);
628 return 0;
629}
Mark Brown5cbd28d2012-05-03 06:54:51 -0300630#endif
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200631
Hans Verkuil76efd622008-11-24 18:16:46 -0300632/* ----------------------------------------------------------------------- */
633
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300634static const struct v4l2_ctrl_ops msp_ctrl_ops = {
635 .s_ctrl = msp_s_ctrl,
636};
637
Hans Verkuil76efd622008-11-24 18:16:46 -0300638static const struct v4l2_subdev_core_ops msp_core_ops = {
639 .log_status = msp_log_status,
Hans Verkuil76efd622008-11-24 18:16:46 -0300640};
641
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300642static const struct v4l2_subdev_video_ops msp_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300643 .s_std = msp_s_std,
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300644 .querystd = msp_querystd,
645};
646
Hans Verkuil76efd622008-11-24 18:16:46 -0300647static 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,
Hans Verkuil76efd622008-11-24 18:16:46 -0300652};
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,
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300661 .video = &msp_video_ops,
Hans Verkuil76efd622008-11-24 18:16:46 -0300662 .tuner = &msp_tuner_ops,
663 .audio = &msp_audio_ops,
664};
665
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200666/* ----------------------------------------------------------------------- */
667
Mauro Carvalho Chehabb68d75b2016-10-20 08:03:00 -0200668
669static const char * const opmode_str[] = {
670 [OPMODE_MANUAL] = "manual",
671 [OPMODE_AUTODETECT] = "autodetect",
672 [OPMODE_AUTOSELECT] = "autodetect and autoselect",
673};
674
Jean Delvared2653e92008-04-29 23:11:39 +0200675static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200676{
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200677 struct msp_state *state;
Hans Verkuil76efd622008-11-24 18:16:46 -0300678 struct v4l2_subdev *sd;
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300679 struct v4l2_ctrl_handler *hdl;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200680 int (*thread_func)(void *data) = NULL;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200681 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;
Mauro Carvalho Chehabfb493282016-01-28 08:51:01 -0200686#if defined(CONFIG_MEDIA_CONTROLLER)
687 int ret;
688#endif
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200689
Jean Delvareaf294862008-05-18 20:49:40 +0200690 if (!id)
691 strlcpy(client->name, "msp3400", sizeof(client->name));
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200692
693 if (msp_reset(client) == -1) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200694 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
Hans Verkuil188f3452007-09-16 10:47:15 -0300695 return -ENODEV;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200696 }
697
Laurent Pinchartc02b2112013-05-02 08:29:43 -0300698 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
Hans Verkuild52c7382007-12-12 08:25:18 -0300699 if (!state)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200700 return -ENOMEM;
Mariusz Kozlowskib2a65762007-07-31 16:35:32 -0300701
Hans Verkuil76efd622008-11-24 18:16:46 -0300702 sd = &state->sd;
703 v4l2_i2c_subdev_init(sd, client, &msp_ops);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200704
Mauro Carvalho Chehabfb493282016-01-28 08:51:01 -0200705#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
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200716 state->v4l2_std = V4L2_STD_NTSC;
Mauro Carvalho Chehab5d1ed982011-10-04 09:44:02 -0300717 state->detected_std = V4L2_STD_ALL;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300718 state->audmode = V4L2_TUNER_MODE_STEREO;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200719 state->input = -1;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200720 state->i2s_mode = 0;
721 init_waitqueue_head(&state->wq);
Hans Verkuil2474ed42006-03-19 12:35:57 -0300722 /* These are the reset input/output positions */
Hans Verkuil5325b422009-04-02 11:26:22 -0300723 state->route_in = MSP_INPUT_DEFAULT;
724 state->route_out = MSP_OUTPUT_DEFAULT;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200725
726 state->rev1 = msp_read_dsp(client, 0x1e);
727 if (state->rev1 != -1)
728 state->rev2 = msp_read_dsp(client, 0x1f);
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200729 dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
Hans Verkuild52c7382007-12-12 08:25:18 -0300730 state->rev1, state->rev2);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200731 if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200732 dev_dbg_lvl(&client->dev, 1, msp_debug,
Hans Verkuild52c7382007-12-12 08:25:18 -0300733 "not an msp3400 (cannot read chip version)\n");
Hans Verkuil188f3452007-09-16 10:47:15 -0300734 return -ENODEV;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200735 }
736
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200737 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;
Hans Verkuil74cab312007-04-27 12:31:26 -0300744 /* Rev B=2, C=3, D=4, G=7 */
Hans Verkuild52c7382007-12-12 08:25:18 -0300745 state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
746 msp_revision - '@';
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200747
748 /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
Hans Verkuild52c7382007-12-12 08:25:18 -0300749 state->has_nicam =
750 msp_prod_hi == 1 || msp_prod_hi == 5;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200751 /* Has radio support: was added with revision G */
Hans Verkuild52c7382007-12-12 08:25:18 -0300752 state->has_radio =
753 msp_revision >= 'G';
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200754 /* Has headphones output: not for stripped down products */
Hans Verkuild52c7382007-12-12 08:25:18 -0300755 state->has_headphones =
756 msp_prod_lo < 5;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300757 /* Has scart2 input: not in stripped down products of the '3' family */
Hans Verkuild52c7382007-12-12 08:25:18 -0300758 state->has_scart2 =
759 msp_family >= 4 || msp_prod_lo < 7;
Hans Verkuil0020d3e2006-03-30 19:50:34 -0300760 /* Has scart3 input: not in stripped down products of the '3' family */
Hans Verkuild52c7382007-12-12 08:25:18 -0300761 state->has_scart3 =
762 msp_family >= 4 || msp_prod_lo < 5;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200763 /* Has scart4 input: not in pre D revisions, not in stripped D revs */
Hans Verkuild52c7382007-12-12 08:25:18 -0300764 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;
Hans Verkuild312a462006-01-09 18:21:36 -0200770 /* Has scart2 a volume control? Not in pre-D revisions. */
Hans Verkuild52c7382007-12-12 08:25:18 -0300771 state->has_scart2_out_volume =
772 msp_revision > 'C' && state->has_scart2_out;
Hans Verkuil5af0c8f2006-01-09 18:21:37 -0200773 /* Has a configurable i2s out? */
Hans Verkuild52c7382007-12-12 08:25:18 -0300774 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;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200784 /* Has Virtual Dolby Surround: only in msp34x1 */
Hans Verkuild52c7382007-12-12 08:25:18 -0300785 state->has_virtual_dolby_surround =
786 msp_revision == 'G' && msp_prod_lo == 1;
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200787 /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
Hans Verkuild52c7382007-12-12 08:25:18 -0300788 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;
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200794
795 state->opmode = opmode;
Mauro Carvalho Chehabb68d75b2016-10-20 08:03:00 -0200796 if (state->opmode < OPMODE_MANUAL
797 || state->opmode > OPMODE_AUTOSELECT) {
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200798 /* MSP revision G and up have both autodetect and autoselect */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200799 if (msp_revision >= 'G')
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200800 state->opmode = OPMODE_AUTOSELECT;
801 /* MSP revision D and up have autodetect */
Hans Verkuil7560d7a2006-01-09 18:21:32 -0200802 else if (msp_revision >= 'D')
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200803 state->opmode = OPMODE_AUTODETECT;
804 else
805 state->opmode = OPMODE_MANUAL;
806 }
807
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300808 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);
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300829 return err;
830 }
831
832 v4l2_ctrl_cluster(2, &state->volume);
833 v4l2_ctrl_handler_setup(hdl);
834
Mauro Carvalho Chehabb68d75b2016-10-20 08:03:00 -0200835 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]);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200844
845 /* version-specific initialization */
846 switch (state->opmode) {
847 case OPMODE_MANUAL:
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200848 thread_func = msp3400c_thread;
849 break;
850 case OPMODE_AUTODETECT:
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200851 thread_func = msp3410d_thread;
852 break;
853 case OPMODE_AUTOSELECT:
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200854 thread_func = msp34xxg_thread;
855 break;
856 }
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200857
858 /* startup control thread if needed */
859 if (thread_func) {
860 state->kthread = kthread_run(thread_func, client, "msp34xx");
861
Akinobu Mita25821402006-12-20 10:07:30 -0300862 if (IS_ERR(state->kthread))
Mauro Carvalho Chehab39ad7992016-11-16 09:10:14 -0200863 dev_warn(&client->dev, "kernel_thread() failed\n");
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200864 msp_wake_thread(client);
865 }
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200866 return 0;
867}
868
Hans Verkuil79518da2007-09-13 11:28:59 -0300869static int msp_remove(struct i2c_client *client)
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200870{
Hans Verkuil76efd622008-11-24 18:16:46 -0300871 struct msp_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200872
Hans Verkuil76efd622008-11-24 18:16:46 -0300873 v4l2_device_unregister_subdev(&state->sd);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200874 /* shutdown control thread */
875 if (state->kthread) {
876 state->restart = 1;
877 kthread_stop(state->kthread);
878 }
879 msp_reset(client);
880
Hans Verkuilebc3bba2010-05-24 10:01:58 -0300881 v4l2_ctrl_handler_free(&state->hdl);
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200882 return 0;
883}
884
885/* ----------------------------------------------------------------------- */
886
Mark Brown5cbd28d2012-05-03 06:54:51 -0300887static const struct dev_pm_ops msp3400_pm_ops = {
888 SET_SYSTEM_SLEEP_PM_OPS(msp_suspend, msp_resume)
889};
890
Jean Delvareaf294862008-05-18 20:49:40 +0200891static const struct i2c_device_id msp_id[] = {
892 { "msp3400", 0 },
893 { }
894};
895MODULE_DEVICE_TABLE(i2c, msp_id);
896
Hans Verkuil61a489c2010-09-15 15:22:34 -0300897static struct i2c_driver msp_driver = {
898 .driver = {
Hans Verkuil61a489c2010-09-15 15:22:34 -0300899 .name = "msp3400",
Mark Brown5cbd28d2012-05-03 06:54:51 -0300900 .pm = &msp3400_pm_ops,
Hans Verkuil61a489c2010-09-15 15:22:34 -0300901 },
902 .probe = msp_probe,
903 .remove = msp_remove,
Hans Verkuil61a489c2010-09-15 15:22:34 -0300904 .id_table = msp_id,
Hans Verkuil53b0a1c2006-01-09 15:32:39 -0200905};
906
Axel Linc6e8d862012-02-12 06:56:32 -0300907module_i2c_driver(msp_driver);