msp3400-driver.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  46. * 02110-1301, USA.
  47. */
  48. #include <linux/kernel.h>
  49. #include <linux/module.h>
  50. #include <linux/slab.h>
  51. #include <linux/i2c.h>
  52. #include <linux/videodev.h>
  53. #include <linux/videodev2.h>
  54. #include <media/v4l2-common.h>
  55. #include <media/v4l2-ioctl.h>
  56. #include <media/v4l2-i2c-drv-legacy.h>
  57. #include <media/tvaudio.h>
  58. #include <media/msp3400.h>
  59. #include <linux/kthread.h>
  60. #include <linux/freezer.h>
  61. #include "msp3400-driver.h"
  62. /* ---------------------------------------------------------------------- */
  63. MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
  64. MODULE_AUTHOR("Gerd Knorr");
  65. MODULE_LICENSE("GPL");
  66. /* module parameters */
  67. static int opmode = OPMODE_AUTO;
  68. int msp_debug; /* msp_debug output */
  69. int msp_once; /* no continous stereo monitoring */
  70. int msp_amsound; /* hard-wire AM sound at 6.5 Hz (france),
  71. the autoscan seems work well only with FM... */
  72. int msp_standard = 1; /* Override auto detect of audio msp_standard,
  73. if needed. */
  74. int msp_dolby;
  75. int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
  76. (msp34xxg only) 0x00a0-0x03c0 */
  77. /* read-only */
  78. module_param(opmode, int, 0444);
  79. /* read-write */
  80. module_param_named(once, msp_once, bool, 0644);
  81. module_param_named(debug, msp_debug, int, 0644);
  82. module_param_named(stereo_threshold, msp_stereo_thresh, int, 0644);
  83. module_param_named(standard, msp_standard, int, 0644);
  84. module_param_named(amsound, msp_amsound, bool, 0644);
  85. module_param_named(dolby, msp_dolby, bool, 0644);
  86. MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
  87. MODULE_PARM_DESC(once, "No continuous stereo monitoring");
  88. MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
  89. MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
  90. MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
  91. MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
  92. MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
  93. /* ---------------------------------------------------------------------- */
  94. /* control subaddress */
  95. #define I2C_MSP_CONTROL 0x00
  96. /* demodulator unit subaddress */
  97. #define I2C_MSP_DEM 0x10
  98. /* DSP unit subaddress */
  99. #define I2C_MSP_DSP 0x12
  100. /* Addresses to scan */
  101. static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
  102. I2C_CLIENT_INSMOD;
  103. /* ----------------------------------------------------------------------- */
  104. /* functions for talking to the MSP3400C Sound processor */
  105. int msp_reset(struct i2c_client *client)
  106. {
  107. /* reset and read revision code */
  108. static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
  109. static u8 reset_on[3] = { I2C_MSP_CONTROL, 0x00, 0x00 };
  110. static u8 write[3] = { I2C_MSP_DSP + 1, 0x00, 0x1e };
  111. u8 read[2];
  112. struct i2c_msg reset[2] = {
  113. { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
  114. { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
  115. };
  116. struct i2c_msg test[2] = {
  117. { client->addr, 0, 3, write },
  118. { client->addr, I2C_M_RD, 2, read },
  119. };
  120. v4l_dbg(3, msp_debug, client, "msp_reset\n");
  121. if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
  122. i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
  123. i2c_transfer(client->adapter, test, 2) != 2) {
  124. v4l_err(client, "chip reset failed\n");
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. static int msp_read(struct i2c_client *client, int dev, int addr)
  130. {
  131. int err, retval;
  132. u8 write[3];
  133. u8 read[2];
  134. struct i2c_msg msgs[2] = {
  135. { client->addr, 0, 3, write },
  136. { client->addr, I2C_M_RD, 2, read }
  137. };
  138. write[0] = dev + 1;
  139. write[1] = addr >> 8;
  140. write[2] = addr & 0xff;
  141. for (err = 0; err < 3; err++) {
  142. if (i2c_transfer(client->adapter, msgs, 2) == 2)
  143. break;
  144. v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
  145. dev, addr);
  146. schedule_timeout_interruptible(msecs_to_jiffies(10));
  147. }
  148. if (err == 3) {
  149. v4l_warn(client, "resetting chip, sound will go off.\n");
  150. msp_reset(client);
  151. return -1;
  152. }
  153. retval = read[0] << 8 | read[1];
  154. v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n",
  155. dev, addr, retval);
  156. return retval;
  157. }
  158. int msp_read_dem(struct i2c_client *client, int addr)
  159. {
  160. return msp_read(client, I2C_MSP_DEM, addr);
  161. }
  162. int msp_read_dsp(struct i2c_client *client, int addr)
  163. {
  164. return msp_read(client, I2C_MSP_DSP, addr);
  165. }
  166. static int msp_write(struct i2c_client *client, int dev, int addr, int val)
  167. {
  168. int err;
  169. u8 buffer[5];
  170. buffer[0] = dev;
  171. buffer[1] = addr >> 8;
  172. buffer[2] = addr & 0xff;
  173. buffer[3] = val >> 8;
  174. buffer[4] = val & 0xff;
  175. v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n",
  176. dev, addr, val);
  177. for (err = 0; err < 3; err++) {
  178. if (i2c_master_send(client, buffer, 5) == 5)
  179. break;
  180. v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
  181. dev, addr);
  182. schedule_timeout_interruptible(msecs_to_jiffies(10));
  183. }
  184. if (err == 3) {
  185. v4l_warn(client, "resetting chip, sound will go off.\n");
  186. msp_reset(client);
  187. return -1;
  188. }
  189. return 0;
  190. }
  191. int msp_write_dem(struct i2c_client *client, int addr, int val)
  192. {
  193. return msp_write(client, I2C_MSP_DEM, addr, val);
  194. }
  195. int msp_write_dsp(struct i2c_client *client, int addr, int val)
  196. {
  197. return msp_write(client, I2C_MSP_DSP, addr, val);
  198. }
  199. /* ----------------------------------------------------------------------- *
  200. * bits 9 8 5 - SCART DSP input Select:
  201. * 0 0 0 - SCART 1 to DSP input (reset position)
  202. * 0 1 0 - MONO to DSP input
  203. * 1 0 0 - SCART 2 to DSP input
  204. * 1 1 1 - Mute DSP input
  205. *
  206. * bits 11 10 6 - SCART 1 Output Select:
  207. * 0 0 0 - undefined (reset position)
  208. * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
  209. * 1 0 0 - MONO input to SCART 1 Output
  210. * 1 1 0 - SCART 1 DA to SCART 1 Output
  211. * 0 0 1 - SCART 2 DA to SCART 1 Output
  212. * 0 1 1 - SCART 1 Input to SCART 1 Output
  213. * 1 1 1 - Mute SCART 1 Output
  214. *
  215. * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART):
  216. * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position)
  217. * 0 1 0 - SCART 1 Input to SCART 2 Output
  218. * 1 0 0 - MONO input to SCART 2 Output
  219. * 0 0 1 - SCART 2 DA to SCART 2 Output
  220. * 0 1 1 - SCART 2 Input to SCART 2 Output
  221. * 1 1 0 - Mute SCART 2 Output
  222. *
  223. * Bits 4 to 0 should be zero.
  224. * ----------------------------------------------------------------------- */
  225. static int scarts[3][9] = {
  226. /* MASK IN1 IN2 IN3 IN4 IN1_DA IN2_DA MONO MUTE */
  227. /* SCART DSP Input select */
  228. { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1, -1, 0x0100, 0x0320 },
  229. /* SCART1 Output select */
  230. { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
  231. /* SCART2 Output select */
  232. { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
  233. };
  234. static char *scart_names[] = {
  235. "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
  236. };
  237. void msp_set_scart(struct i2c_client *client, int in, int out)
  238. {
  239. struct msp_state *state = i2c_get_clientdata(client);
  240. state->in_scart = in;
  241. if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
  242. if (-1 == scarts[out][in + 1])
  243. return;
  244. state->acb &= ~scarts[out][0];
  245. state->acb |= scarts[out][in + 1];
  246. } else
  247. state->acb = 0xf60; /* Mute Input and SCART 1 Output */
  248. v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
  249. scart_names[in], out, state->acb);
  250. msp_write_dsp(client, 0x13, state->acb);
  251. /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
  252. if (state->has_i2s_conf)
  253. msp_write_dem(client, 0x40, state->i2s_mode);
  254. }
  255. void msp_set_audio(struct i2c_client *client)
  256. {
  257. struct msp_state *state = i2c_get_clientdata(client);
  258. int bal = 0, bass, treble, loudness;
  259. int val = 0;
  260. int reallymuted = state->muted | state->scan_in_progress;
  261. if (!reallymuted)
  262. val = (state->volume * 0x7f / 65535) << 8;
  263. v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
  264. state->muted ? "on" : "off",
  265. state->scan_in_progress ? "yes" : "no",
  266. state->volume);
  267. msp_write_dsp(client, 0x0000, val);
  268. msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
  269. if (state->has_scart2_out_volume)
  270. msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
  271. if (state->has_headphones)
  272. msp_write_dsp(client, 0x0006, val);
  273. if (!state->has_sound_processing)
  274. return;
  275. if (val)
  276. bal = (u8)((state->balance / 256) - 128);
  277. bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
  278. treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
  279. loudness = state->loudness ? ((5 * 4) << 8) : 0;
  280. v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
  281. state->balance, state->bass, state->treble, state->loudness);
  282. msp_write_dsp(client, 0x0001, bal << 8);
  283. msp_write_dsp(client, 0x0002, bass);
  284. msp_write_dsp(client, 0x0003, treble);
  285. msp_write_dsp(client, 0x0004, loudness);
  286. if (!state->has_headphones)
  287. return;
  288. msp_write_dsp(client, 0x0030, bal << 8);
  289. msp_write_dsp(client, 0x0031, bass);
  290. msp_write_dsp(client, 0x0032, treble);
  291. msp_write_dsp(client, 0x0033, loudness);
  292. }
  293. /* ------------------------------------------------------------------------ */
  294. static void msp_wake_thread(struct i2c_client *client)
  295. {
  296. struct msp_state *state = i2c_get_clientdata(client);
  297. if (NULL == state->kthread)
  298. return;
  299. state->watch_stereo = 0;
  300. state->restart = 1;
  301. wake_up_interruptible(&state->wq);
  302. }
  303. int msp_sleep(struct msp_state *state, int timeout)
  304. {
  305. DECLARE_WAITQUEUE(wait, current);
  306. add_wait_queue(&state->wq, &wait);
  307. if (!kthread_should_stop()) {
  308. if (timeout < 0) {
  309. set_current_state(TASK_INTERRUPTIBLE);
  310. schedule();
  311. } else {
  312. schedule_timeout_interruptible
  313. (msecs_to_jiffies(timeout));
  314. }
  315. }
  316. remove_wait_queue(&state->wq, &wait);
  317. try_to_freeze();
  318. return state->restart;
  319. }
  320. /* ------------------------------------------------------------------------ */
  321. #ifdef CONFIG_VIDEO_ALLOW_V4L1
  322. static int msp_mode_v4l2_to_v4l1(int rxsubchans, int audmode)
  323. {
  324. if (rxsubchans == V4L2_TUNER_SUB_MONO)
  325. return VIDEO_SOUND_MONO;
  326. if (rxsubchans == V4L2_TUNER_SUB_STEREO)
  327. return VIDEO_SOUND_STEREO;
  328. if (audmode == V4L2_TUNER_MODE_LANG2)
  329. return VIDEO_SOUND_LANG2;
  330. return VIDEO_SOUND_LANG1;
  331. }
  332. static int msp_mode_v4l1_to_v4l2(int mode)
  333. {
  334. if (mode & VIDEO_SOUND_STEREO)
  335. return V4L2_TUNER_MODE_STEREO;
  336. if (mode & VIDEO_SOUND_LANG2)
  337. return V4L2_TUNER_MODE_LANG2;
  338. if (mode & VIDEO_SOUND_LANG1)
  339. return V4L2_TUNER_MODE_LANG1;
  340. return V4L2_TUNER_MODE_MONO;
  341. }
  342. #endif
  343. static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  344. {
  345. struct msp_state *state = i2c_get_clientdata(client);
  346. switch (ctrl->id) {
  347. case V4L2_CID_AUDIO_VOLUME:
  348. ctrl->value = state->volume;
  349. break;
  350. case V4L2_CID_AUDIO_MUTE:
  351. ctrl->value = state->muted;
  352. break;
  353. case V4L2_CID_AUDIO_BALANCE:
  354. if (!state->has_sound_processing)
  355. return -EINVAL;
  356. ctrl->value = state->balance;
  357. break;
  358. case V4L2_CID_AUDIO_BASS:
  359. if (!state->has_sound_processing)
  360. return -EINVAL;
  361. ctrl->value = state->bass;
  362. break;
  363. case V4L2_CID_AUDIO_TREBLE:
  364. if (!state->has_sound_processing)
  365. return -EINVAL;
  366. ctrl->value = state->treble;
  367. break;
  368. case V4L2_CID_AUDIO_LOUDNESS:
  369. if (!state->has_sound_processing)
  370. return -EINVAL;
  371. ctrl->value = state->loudness;
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. return 0;
  377. }
  378. static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  379. {
  380. struct msp_state *state = i2c_get_clientdata(client);
  381. switch (ctrl->id) {
  382. case V4L2_CID_AUDIO_VOLUME:
  383. state->volume = ctrl->value;
  384. if (state->volume == 0)
  385. state->balance = 32768;
  386. break;
  387. case V4L2_CID_AUDIO_MUTE:
  388. if (ctrl->value < 0 || ctrl->value >= 2)
  389. return -ERANGE;
  390. state->muted = ctrl->value;
  391. break;
  392. case V4L2_CID_AUDIO_BASS:
  393. if (!state->has_sound_processing)
  394. return -EINVAL;
  395. state->bass = ctrl->value;
  396. break;
  397. case V4L2_CID_AUDIO_TREBLE:
  398. if (!state->has_sound_processing)
  399. return -EINVAL;
  400. state->treble = ctrl->value;
  401. break;
  402. case V4L2_CID_AUDIO_LOUDNESS:
  403. if (!state->has_sound_processing)
  404. return -EINVAL;
  405. state->loudness = ctrl->value;
  406. break;
  407. case V4L2_CID_AUDIO_BALANCE:
  408. if (!state->has_sound_processing)
  409. return -EINVAL;
  410. state->balance = ctrl->value;
  411. break;
  412. default:
  413. return -EINVAL;
  414. }
  415. msp_set_audio(client);
  416. return 0;
  417. }
  418. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  419. {
  420. struct msp_state *state = i2c_get_clientdata(client);
  421. if (msp_debug >= 2)
  422. v4l_i2c_print_ioctl(client, cmd);
  423. switch (cmd) {
  424. case AUDC_SET_RADIO:
  425. if (state->radio)
  426. return 0;
  427. state->radio = 1;
  428. v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
  429. state->watch_stereo = 0;
  430. switch (state->opmode) {
  431. case OPMODE_MANUAL:
  432. /* set msp3400 to FM radio mode */
  433. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  434. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  435. MSP_CARRIER(10.7));
  436. msp_set_audio(client);
  437. break;
  438. case OPMODE_AUTODETECT:
  439. case OPMODE_AUTOSELECT:
  440. /* the thread will do for us */
  441. msp_wake_thread(client);
  442. break;
  443. }
  444. break;
  445. /* --- v4l ioctls --- */
  446. /* take care: bttv does userspace copying, we'll get a
  447. kernel pointer here... */
  448. #ifdef CONFIG_VIDEO_ALLOW_V4L1
  449. case VIDIOCGAUDIO:
  450. {
  451. struct video_audio *va = arg;
  452. va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
  453. if (state->has_sound_processing)
  454. va->flags |= VIDEO_AUDIO_BALANCE |
  455. VIDEO_AUDIO_BASS |
  456. VIDEO_AUDIO_TREBLE;
  457. if (state->muted)
  458. va->flags |= VIDEO_AUDIO_MUTE;
  459. va->volume = state->volume;
  460. va->balance = state->volume ? state->balance : 32768;
  461. va->bass = state->bass;
  462. va->treble = state->treble;
  463. if (state->radio)
  464. break;
  465. if (state->opmode == OPMODE_AUTOSELECT)
  466. msp_detect_stereo(client);
  467. va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
  468. break;
  469. }
  470. case VIDIOCSAUDIO:
  471. {
  472. struct video_audio *va = arg;
  473. state->muted = (va->flags & VIDEO_AUDIO_MUTE);
  474. state->volume = va->volume;
  475. state->balance = va->balance;
  476. state->bass = va->bass;
  477. state->treble = va->treble;
  478. msp_set_audio(client);
  479. if (va->mode != 0 && state->radio == 0 &&
  480. state->audmode != msp_mode_v4l1_to_v4l2(va->mode)) {
  481. state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
  482. msp_set_audmode(client);
  483. }
  484. break;
  485. }
  486. case VIDIOCSCHAN:
  487. {
  488. struct video_channel *vc = arg;
  489. int update = 0;
  490. v4l2_std_id std;
  491. if (state->radio)
  492. update = 1;
  493. state->radio = 0;
  494. if (vc->norm == VIDEO_MODE_PAL)
  495. std = V4L2_STD_PAL;
  496. else if (vc->norm == VIDEO_MODE_SECAM)
  497. std = V4L2_STD_SECAM;
  498. else
  499. std = V4L2_STD_NTSC;
  500. if (std != state->v4l2_std) {
  501. state->v4l2_std = std;
  502. update = 1;
  503. }
  504. if (update)
  505. msp_wake_thread(client);
  506. break;
  507. }
  508. case VIDIOCSFREQ:
  509. {
  510. /* new channel -- kick audio carrier scan */
  511. msp_wake_thread(client);
  512. break;
  513. }
  514. #endif
  515. case VIDIOC_S_FREQUENCY:
  516. {
  517. /* new channel -- kick audio carrier scan */
  518. msp_wake_thread(client);
  519. break;
  520. }
  521. /* --- v4l2 ioctls --- */
  522. case VIDIOC_S_STD:
  523. {
  524. v4l2_std_id *id = arg;
  525. int update = state->radio || state->v4l2_std != *id;
  526. state->v4l2_std = *id;
  527. state->radio = 0;
  528. if (update)
  529. msp_wake_thread(client);
  530. return 0;
  531. }
  532. case VIDIOC_INT_G_AUDIO_ROUTING:
  533. {
  534. struct v4l2_routing *rt = arg;
  535. *rt = state->routing;
  536. break;
  537. }
  538. case VIDIOC_INT_S_AUDIO_ROUTING:
  539. {
  540. struct v4l2_routing *rt = arg;
  541. int tuner = (rt->input >> 3) & 1;
  542. int sc_in = rt->input & 0x7;
  543. int sc1_out = rt->output & 0xf;
  544. int sc2_out = (rt->output >> 4) & 0xf;
  545. u16 val, reg;
  546. int i;
  547. int extern_input = 1;
  548. if (state->routing.input == rt->input &&
  549. state->routing.output == rt->output)
  550. break;
  551. state->routing = *rt;
  552. /* check if the tuner input is used */
  553. for (i = 0; i < 5; i++) {
  554. if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
  555. extern_input = 0;
  556. }
  557. state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
  558. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  559. msp_set_scart(client, sc_in, 0);
  560. msp_set_scart(client, sc1_out, 1);
  561. msp_set_scart(client, sc2_out, 2);
  562. msp_set_audmode(client);
  563. reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
  564. val = msp_read_dem(client, reg);
  565. msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
  566. /* wake thread when a new input is chosen */
  567. msp_wake_thread(client);
  568. break;
  569. }
  570. case VIDIOC_G_TUNER:
  571. {
  572. struct v4l2_tuner *vt = arg;
  573. if (state->radio)
  574. break;
  575. if (state->opmode == OPMODE_AUTOSELECT)
  576. msp_detect_stereo(client);
  577. vt->audmode = state->audmode;
  578. vt->rxsubchans = state->rxsubchans;
  579. vt->capability |= V4L2_TUNER_CAP_STEREO |
  580. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  581. break;
  582. }
  583. case VIDIOC_S_TUNER:
  584. {
  585. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  586. if (state->radio) /* TODO: add mono/stereo support for radio */
  587. break;
  588. if (state->audmode == vt->audmode)
  589. break;
  590. state->audmode = vt->audmode;
  591. /* only set audmode */
  592. msp_set_audmode(client);
  593. break;
  594. }
  595. case VIDIOC_INT_I2S_CLOCK_FREQ:
  596. {
  597. u32 *a = (u32 *)arg;
  598. v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
  599. switch (*a) {
  600. case 1024000:
  601. state->i2s_mode = 0;
  602. break;
  603. case 2048000:
  604. state->i2s_mode = 1;
  605. break;
  606. default:
  607. return -EINVAL;
  608. }
  609. break;
  610. }
  611. case VIDIOC_QUERYCTRL:
  612. {
  613. struct v4l2_queryctrl *qc = arg;
  614. switch (qc->id) {
  615. case V4L2_CID_AUDIO_VOLUME:
  616. case V4L2_CID_AUDIO_MUTE:
  617. return v4l2_ctrl_query_fill_std(qc);
  618. default:
  619. break;
  620. }
  621. if (!state->has_sound_processing)
  622. return -EINVAL;
  623. switch (qc->id) {
  624. case V4L2_CID_AUDIO_LOUDNESS:
  625. case V4L2_CID_AUDIO_BALANCE:
  626. case V4L2_CID_AUDIO_BASS:
  627. case V4L2_CID_AUDIO_TREBLE:
  628. return v4l2_ctrl_query_fill_std(qc);
  629. default:
  630. return -EINVAL;
  631. }
  632. }
  633. case VIDIOC_G_CTRL:
  634. return msp_get_ctrl(client, arg);
  635. case VIDIOC_S_CTRL:
  636. return msp_set_ctrl(client, arg);
  637. case VIDIOC_LOG_STATUS:
  638. {
  639. const char *p;
  640. if (state->opmode == OPMODE_AUTOSELECT)
  641. msp_detect_stereo(client);
  642. v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
  643. client->name, state->rev1, state->rev2);
  644. v4l_info(client, "Audio: volume %d%s\n",
  645. state->volume, state->muted ? " (muted)" : "");
  646. if (state->has_sound_processing) {
  647. v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
  648. state->balance, state->bass,
  649. state->treble,
  650. state->loudness ? "on" : "off");
  651. }
  652. switch (state->mode) {
  653. case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
  654. case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
  655. case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono/stereo"; break;
  656. case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
  657. case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
  658. case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
  659. case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
  660. case MSP_MODE_BTSC: p = "BTSC"; break;
  661. case MSP_MODE_EXTERN: p = "External input"; break;
  662. default: p = "unknown"; break;
  663. }
  664. if (state->mode == MSP_MODE_EXTERN) {
  665. v4l_info(client, "Mode: %s\n", p);
  666. } else if (state->opmode == OPMODE_MANUAL) {
  667. v4l_info(client, "Mode: %s (%s%s)\n", p,
  668. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  669. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  670. } else {
  671. if (state->opmode == OPMODE_AUTODETECT)
  672. v4l_info(client, "Mode: %s\n", p);
  673. v4l_info(client, "Standard: %s (%s%s)\n",
  674. msp_standard_std_name(state->std),
  675. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  676. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  677. }
  678. v4l_info(client, "Audmode: 0x%04x\n", state->audmode);
  679. v4l_info(client, "Routing: 0x%08x (input) 0x%08x (output)\n",
  680. state->routing.input, state->routing.output);
  681. v4l_info(client, "ACB: 0x%04x\n", state->acb);
  682. break;
  683. }
  684. case VIDIOC_G_CHIP_IDENT:
  685. return v4l2_chip_ident_i2c_client(client, arg, state->ident,
  686. (state->rev1 << 16) | state->rev2);
  687. default:
  688. /* unknown */
  689. return -EINVAL;
  690. }
  691. return 0;
  692. }
  693. static int msp_suspend(struct i2c_client *client, pm_message_t state)
  694. {
  695. v4l_dbg(1, msp_debug, client, "suspend\n");
  696. msp_reset(client);
  697. return 0;
  698. }
  699. static int msp_resume(struct i2c_client *client)
  700. {
  701. v4l_dbg(1, msp_debug, client, "resume\n");
  702. msp_wake_thread(client);
  703. return 0;
  704. }
  705. /* ----------------------------------------------------------------------- */
  706. static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
  707. {
  708. struct msp_state *state;
  709. int (*thread_func)(void *data) = NULL;
  710. int msp_hard;
  711. int msp_family;
  712. int msp_revision;
  713. int msp_product, msp_prod_hi, msp_prod_lo;
  714. int msp_rom;
  715. if (!id)
  716. strlcpy(client->name, "msp3400", sizeof(client->name));
  717. if (msp_reset(client) == -1) {
  718. v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
  719. return -ENODEV;
  720. }
  721. state = kzalloc(sizeof(*state), GFP_KERNEL);
  722. if (!state)
  723. return -ENOMEM;
  724. i2c_set_clientdata(client, state);
  725. state->v4l2_std = V4L2_STD_NTSC;
  726. state->audmode = V4L2_TUNER_MODE_STEREO;
  727. state->volume = 58880; /* 0db gain */
  728. state->balance = 32768; /* 0db gain */
  729. state->bass = 32768;
  730. state->treble = 32768;
  731. state->loudness = 0;
  732. state->input = -1;
  733. state->muted = 0;
  734. state->i2s_mode = 0;
  735. init_waitqueue_head(&state->wq);
  736. /* These are the reset input/output positions */
  737. state->routing.input = MSP_INPUT_DEFAULT;
  738. state->routing.output = MSP_OUTPUT_DEFAULT;
  739. state->rev1 = msp_read_dsp(client, 0x1e);
  740. if (state->rev1 != -1)
  741. state->rev2 = msp_read_dsp(client, 0x1f);
  742. v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n",
  743. state->rev1, state->rev2);
  744. if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
  745. v4l_dbg(1, msp_debug, client,
  746. "not an msp3400 (cannot read chip version)\n");
  747. kfree(state);
  748. return -ENODEV;
  749. }
  750. msp_set_audio(client);
  751. msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
  752. msp_product = (state->rev2 >> 8) & 0xff;
  753. msp_prod_hi = msp_product / 10;
  754. msp_prod_lo = msp_product % 10;
  755. msp_revision = (state->rev1 & 0x0f) + '@';
  756. msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
  757. msp_rom = state->rev2 & 0x1f;
  758. /* Rev B=2, C=3, D=4, G=7 */
  759. state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
  760. msp_revision - '@';
  761. /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
  762. state->has_nicam =
  763. msp_prod_hi == 1 || msp_prod_hi == 5;
  764. /* Has radio support: was added with revision G */
  765. state->has_radio =
  766. msp_revision >= 'G';
  767. /* Has headphones output: not for stripped down products */
  768. state->has_headphones =
  769. msp_prod_lo < 5;
  770. /* Has scart2 input: not in stripped down products of the '3' family */
  771. state->has_scart2 =
  772. msp_family >= 4 || msp_prod_lo < 7;
  773. /* Has scart3 input: not in stripped down products of the '3' family */
  774. state->has_scart3 =
  775. msp_family >= 4 || msp_prod_lo < 5;
  776. /* Has scart4 input: not in pre D revisions, not in stripped D revs */
  777. state->has_scart4 =
  778. msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
  779. /* Has scart2 output: not in stripped down products of
  780. * the '3' family */
  781. state->has_scart2_out =
  782. msp_family >= 4 || msp_prod_lo < 5;
  783. /* Has scart2 a volume control? Not in pre-D revisions. */
  784. state->has_scart2_out_volume =
  785. msp_revision > 'C' && state->has_scart2_out;
  786. /* Has a configurable i2s out? */
  787. state->has_i2s_conf =
  788. msp_revision >= 'G' && msp_prod_lo < 7;
  789. /* Has subwoofer output: not in pre-D revs and not in stripped down
  790. * products */
  791. state->has_subwoofer =
  792. msp_revision >= 'D' && msp_prod_lo < 5;
  793. /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
  794. * not in stripped down products */
  795. state->has_sound_processing =
  796. msp_prod_lo < 7;
  797. /* Has Virtual Dolby Surround: only in msp34x1 */
  798. state->has_virtual_dolby_surround =
  799. msp_revision == 'G' && msp_prod_lo == 1;
  800. /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
  801. state->has_dolby_pro_logic =
  802. msp_revision == 'G' && msp_prod_lo == 2;
  803. /* The msp343xG supports BTSC only and cannot do Automatic Standard
  804. * Detection. */
  805. state->force_btsc =
  806. msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
  807. state->opmode = opmode;
  808. if (state->opmode == OPMODE_AUTO) {
  809. /* MSP revision G and up have both autodetect and autoselect */
  810. if (msp_revision >= 'G')
  811. state->opmode = OPMODE_AUTOSELECT;
  812. /* MSP revision D and up have autodetect */
  813. else if (msp_revision >= 'D')
  814. state->opmode = OPMODE_AUTODETECT;
  815. else
  816. state->opmode = OPMODE_MANUAL;
  817. }
  818. /* hello world :-) */
  819. v4l_info(client, "MSP%d4%02d%c-%c%d found @ 0x%x (%s)\n",
  820. msp_family, msp_product,
  821. msp_revision, msp_hard, msp_rom,
  822. client->addr << 1, client->adapter->name);
  823. v4l_info(client, "%s ", client->name);
  824. if (state->has_nicam && state->has_radio)
  825. printk(KERN_CONT "supports nicam and radio, ");
  826. else if (state->has_nicam)
  827. printk(KERN_CONT "supports nicam, ");
  828. else if (state->has_radio)
  829. printk(KERN_CONT "supports radio, ");
  830. printk(KERN_CONT "mode is ");
  831. /* version-specific initialization */
  832. switch (state->opmode) {
  833. case OPMODE_MANUAL:
  834. printk(KERN_CONT "manual");
  835. thread_func = msp3400c_thread;
  836. break;
  837. case OPMODE_AUTODETECT:
  838. printk(KERN_CONT "autodetect");
  839. thread_func = msp3410d_thread;
  840. break;
  841. case OPMODE_AUTOSELECT:
  842. printk(KERN_CONT "autodetect and autoselect");
  843. thread_func = msp34xxg_thread;
  844. break;
  845. }
  846. printk(KERN_CONT "\n");
  847. /* startup control thread if needed */
  848. if (thread_func) {
  849. state->kthread = kthread_run(thread_func, client, "msp34xx");
  850. if (IS_ERR(state->kthread))
  851. v4l_warn(client, "kernel_thread() failed\n");
  852. msp_wake_thread(client);
  853. }
  854. return 0;
  855. }
  856. static int msp_remove(struct i2c_client *client)
  857. {
  858. struct msp_state *state = i2c_get_clientdata(client);
  859. /* shutdown control thread */
  860. if (state->kthread) {
  861. state->restart = 1;
  862. kthread_stop(state->kthread);
  863. }
  864. msp_reset(client);
  865. kfree(state);
  866. return 0;
  867. }
  868. /* ----------------------------------------------------------------------- */
  869. static const struct i2c_device_id msp_id[] = {
  870. { "msp3400", 0 },
  871. { }
  872. };
  873. MODULE_DEVICE_TABLE(i2c, msp_id);
  874. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  875. .name = "msp3400",
  876. .driverid = I2C_DRIVERID_MSP3400,
  877. .command = msp_command,
  878. .probe = msp_probe,
  879. .remove = msp_remove,
  880. .suspend = msp_suspend,
  881. .resume = msp_resume,
  882. .id_table = msp_id,
  883. };
  884. /*
  885. * Overrides for Emacs so that we follow Linus's tabbing style.
  886. * ---------------------------------------------------------------------------
  887. * Local variables:
  888. * c-basic-offset: 8
  889. * End:
  890. */