msp3400-driver.c 28 KB

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