msp3400-driver.c 27 KB

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