msp3400-driver.c 30 KB

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