msp3400-driver.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. struct msp_state *state = i2c_get_clientdata(client);
  252. v4l_dbg(1, client, "mute audio\n");
  253. msp_write_dsp(client, 0x0000, 0);
  254. msp_write_dsp(client, 0x0007, 1);
  255. if (state->has_scart2_out_volume)
  256. msp_write_dsp(client, 0x0040, 1);
  257. if (state->has_headphones)
  258. msp_write_dsp(client, 0x0006, 0);
  259. }
  260. void msp_set_audio(struct i2c_client *client)
  261. {
  262. struct msp_state *state = i2c_get_clientdata(client);
  263. int bal = 0, bass, treble, loudness;
  264. int val = 0;
  265. if (!state->muted)
  266. val = (state->volume * 0x7f / 65535) << 8;
  267. v4l_dbg(1, client, "mute=%s volume=%d\n",
  268. state->muted ? "on" : "off", state->volume);
  269. msp_write_dsp(client, 0x0000, val);
  270. msp_write_dsp(client, 0x0007, state->muted ? 0x1 : (val | 0x1));
  271. if (state->has_scart2_out_volume)
  272. msp_write_dsp(client, 0x0040, state->muted ? 0x1 : (val | 0x1));
  273. if (state->has_headphones)
  274. msp_write_dsp(client, 0x0006, val);
  275. if (!state->has_sound_processing)
  276. return;
  277. if (val)
  278. bal = (u8)((state->balance / 256) - 128);
  279. bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
  280. treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
  281. loudness = state->loudness ? ((5 * 4) << 8) : 0;
  282. v4l_dbg(1, client, "balance=%d bass=%d treble=%d loudness=%d\n",
  283. state->balance, state->bass, state->treble, state->loudness);
  284. msp_write_dsp(client, 0x0001, bal << 8);
  285. msp_write_dsp(client, 0x0002, bass);
  286. msp_write_dsp(client, 0x0003, treble);
  287. msp_write_dsp(client, 0x0004, loudness);
  288. if (!state->has_headphones)
  289. return;
  290. msp_write_dsp(client, 0x0030, bal << 8);
  291. msp_write_dsp(client, 0x0031, bass);
  292. msp_write_dsp(client, 0x0032, treble);
  293. msp_write_dsp(client, 0x0033, loudness);
  294. }
  295. int msp_modus(struct i2c_client *client)
  296. {
  297. struct msp_state *state = i2c_get_clientdata(client);
  298. if (state->radio) {
  299. v4l_dbg(1, client, "video mode selected to Radio\n");
  300. return 0x0003;
  301. }
  302. if (state->std & V4L2_STD_PAL) {
  303. v4l_dbg(1, client, "video mode selected to PAL\n");
  304. #if 1
  305. /* experimental: not sure this works with all chip versions */
  306. return 0x7003;
  307. #else
  308. /* previous value, try this if it breaks ... */
  309. return 0x1003;
  310. #endif
  311. }
  312. if (state->std & V4L2_STD_NTSC) {
  313. v4l_dbg(1, client, "video mode selected to NTSC\n");
  314. return 0x2003;
  315. }
  316. if (state->std & V4L2_STD_SECAM) {
  317. v4l_dbg(1, client, "video mode selected to SECAM\n");
  318. return 0x0003;
  319. }
  320. return 0x0003;
  321. }
  322. /* ------------------------------------------------------------------------ */
  323. static void msp_wake_thread(struct i2c_client *client)
  324. {
  325. struct msp_state *state = i2c_get_clientdata(client);
  326. if (NULL == state->kthread)
  327. return;
  328. msp_set_mute(client);
  329. state->watch_stereo = 0;
  330. state->restart = 1;
  331. wake_up_interruptible(&state->wq);
  332. }
  333. int msp_sleep(struct msp_state *state, int timeout)
  334. {
  335. DECLARE_WAITQUEUE(wait, current);
  336. add_wait_queue(&state->wq, &wait);
  337. if (!kthread_should_stop()) {
  338. if (timeout < 0) {
  339. set_current_state(TASK_INTERRUPTIBLE);
  340. schedule();
  341. } else {
  342. schedule_timeout_interruptible
  343. (msecs_to_jiffies(timeout));
  344. }
  345. }
  346. remove_wait_queue(&state->wq, &wait);
  347. try_to_freeze();
  348. return state->restart;
  349. }
  350. /* ------------------------------------------------------------------------ */
  351. static int msp_mode_v4l2_to_v4l1(int rxsubchans)
  352. {
  353. int mode = 0;
  354. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  355. mode |= VIDEO_SOUND_STEREO;
  356. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  357. mode |= VIDEO_SOUND_LANG2;
  358. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  359. mode |= VIDEO_SOUND_LANG1;
  360. if (mode == 0)
  361. mode |= VIDEO_SOUND_MONO;
  362. return mode;
  363. }
  364. static int msp_mode_v4l1_to_v4l2(int mode)
  365. {
  366. if (mode & VIDEO_SOUND_STEREO)
  367. return V4L2_TUNER_MODE_STEREO;
  368. if (mode & VIDEO_SOUND_LANG2)
  369. return V4L2_TUNER_MODE_LANG2;
  370. if (mode & VIDEO_SOUND_LANG1)
  371. return V4L2_TUNER_MODE_LANG1;
  372. return V4L2_TUNER_MODE_MONO;
  373. }
  374. static void msp_any_detect_stereo(struct i2c_client *client)
  375. {
  376. struct msp_state *state = i2c_get_clientdata(client);
  377. switch (state->opmode) {
  378. case OPMODE_MANUAL:
  379. case OPMODE_AUTODETECT:
  380. autodetect_stereo(client);
  381. break;
  382. case OPMODE_AUTOSELECT:
  383. msp34xxg_detect_stereo(client);
  384. break;
  385. }
  386. }
  387. static struct v4l2_queryctrl msp_qctrl_std[] = {
  388. {
  389. .id = V4L2_CID_AUDIO_VOLUME,
  390. .name = "Volume",
  391. .minimum = 0,
  392. .maximum = 65535,
  393. .step = 65535/100,
  394. .default_value = 58880,
  395. .flags = 0,
  396. .type = V4L2_CTRL_TYPE_INTEGER,
  397. },{
  398. .id = V4L2_CID_AUDIO_MUTE,
  399. .name = "Mute",
  400. .minimum = 0,
  401. .maximum = 1,
  402. .step = 1,
  403. .default_value = 1,
  404. .flags = 0,
  405. .type = V4L2_CTRL_TYPE_BOOLEAN,
  406. },
  407. };
  408. static struct v4l2_queryctrl msp_qctrl_sound_processing[] = {
  409. {
  410. .id = V4L2_CID_AUDIO_BALANCE,
  411. .name = "Balance",
  412. .minimum = 0,
  413. .maximum = 65535,
  414. .step = 65535/100,
  415. .default_value = 32768,
  416. .flags = 0,
  417. .type = V4L2_CTRL_TYPE_INTEGER,
  418. },{
  419. .id = V4L2_CID_AUDIO_BASS,
  420. .name = "Bass",
  421. .minimum = 0,
  422. .maximum = 65535,
  423. .step = 65535/100,
  424. .default_value = 32768,
  425. .type = V4L2_CTRL_TYPE_INTEGER,
  426. },{
  427. .id = V4L2_CID_AUDIO_TREBLE,
  428. .name = "Treble",
  429. .minimum = 0,
  430. .maximum = 65535,
  431. .step = 65535/100,
  432. .default_value = 32768,
  433. .type = V4L2_CTRL_TYPE_INTEGER,
  434. },{
  435. .id = V4L2_CID_AUDIO_LOUDNESS,
  436. .name = "Loudness",
  437. .minimum = 0,
  438. .maximum = 1,
  439. .step = 1,
  440. .default_value = 1,
  441. .flags = 0,
  442. .type = V4L2_CTRL_TYPE_BOOLEAN,
  443. },
  444. };
  445. static void msp_any_set_audmode(struct i2c_client *client, int audmode)
  446. {
  447. struct msp_state *state = i2c_get_clientdata(client);
  448. switch (state->opmode) {
  449. case OPMODE_MANUAL:
  450. case OPMODE_AUTODETECT:
  451. state->watch_stereo = 0;
  452. msp3400c_setstereo(client, audmode);
  453. break;
  454. case OPMODE_AUTOSELECT:
  455. msp34xxg_set_audmode(client, audmode);
  456. break;
  457. }
  458. }
  459. static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  460. {
  461. struct msp_state *state = i2c_get_clientdata(client);
  462. switch (ctrl->id) {
  463. case V4L2_CID_AUDIO_VOLUME:
  464. ctrl->value = state->volume;
  465. break;
  466. case V4L2_CID_AUDIO_MUTE:
  467. ctrl->value = state->muted;
  468. break;
  469. case V4L2_CID_AUDIO_BALANCE:
  470. if (!state->has_sound_processing)
  471. return -EINVAL;
  472. ctrl->value = state->balance;
  473. break;
  474. case V4L2_CID_AUDIO_BASS:
  475. if (!state->has_sound_processing)
  476. return -EINVAL;
  477. ctrl->value = state->bass;
  478. break;
  479. case V4L2_CID_AUDIO_TREBLE:
  480. if (!state->has_sound_processing)
  481. return -EINVAL;
  482. ctrl->value = state->treble;
  483. break;
  484. case V4L2_CID_AUDIO_LOUDNESS:
  485. if (!state->has_sound_processing)
  486. return -EINVAL;
  487. ctrl->value = state->loudness;
  488. break;
  489. default:
  490. return -EINVAL;
  491. }
  492. return 0;
  493. }
  494. static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  495. {
  496. struct msp_state *state = i2c_get_clientdata(client);
  497. switch (ctrl->id) {
  498. case V4L2_CID_AUDIO_VOLUME:
  499. state->volume = ctrl->value;
  500. if (state->volume == 0)
  501. state->balance = 32768;
  502. break;
  503. case V4L2_CID_AUDIO_MUTE:
  504. if (ctrl->value < 0 || ctrl->value >= 2)
  505. return -ERANGE;
  506. state->muted = ctrl->value;
  507. break;
  508. case V4L2_CID_AUDIO_BASS:
  509. if (!state->has_sound_processing)
  510. return -EINVAL;
  511. state->bass = ctrl->value;
  512. break;
  513. case V4L2_CID_AUDIO_TREBLE:
  514. if (!state->has_sound_processing)
  515. return -EINVAL;
  516. state->treble = ctrl->value;
  517. break;
  518. case V4L2_CID_AUDIO_LOUDNESS:
  519. if (!state->has_sound_processing)
  520. return -EINVAL;
  521. state->loudness = ctrl->value;
  522. break;
  523. case V4L2_CID_AUDIO_BALANCE:
  524. if (!state->has_sound_processing)
  525. return -EINVAL;
  526. state->balance = ctrl->value;
  527. break;
  528. default:
  529. return -EINVAL;
  530. }
  531. msp_set_audio(client);
  532. return 0;
  533. }
  534. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  535. {
  536. struct msp_state *state = i2c_get_clientdata(client);
  537. u16 *sarg = arg;
  538. int scart = 0;
  539. if (debug >= 2)
  540. v4l_i2c_print_ioctl(client, cmd);
  541. switch (cmd) {
  542. case AUDC_SET_INPUT:
  543. if (*sarg == state->input)
  544. break;
  545. state->input = *sarg;
  546. switch (*sarg) {
  547. case AUDIO_RADIO:
  548. /* Hauppauge uses IN2 for the radio */
  549. state->mode = MSP_MODE_FM_RADIO;
  550. scart = SCART_IN2;
  551. break;
  552. case AUDIO_EXTERN_1:
  553. /* IN1 is often used for external input ... */
  554. state->mode = MSP_MODE_EXTERN;
  555. scart = SCART_IN1;
  556. break;
  557. case AUDIO_EXTERN_2:
  558. /* ... sometimes it is IN2 through ;) */
  559. state->mode = MSP_MODE_EXTERN;
  560. scart = SCART_IN2;
  561. break;
  562. case AUDIO_TUNER:
  563. state->mode = -1;
  564. break;
  565. default:
  566. if (*sarg & AUDIO_MUTE)
  567. msp_set_scart(client, SCART_MUTE, 0);
  568. break;
  569. }
  570. if (scart) {
  571. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  572. state->audmode = V4L2_TUNER_MODE_STEREO;
  573. msp_set_scart(client, scart, 0);
  574. msp_write_dsp(client, 0x000d, 0x1900);
  575. if (state->opmode != OPMODE_AUTOSELECT)
  576. msp3400c_setstereo(client, state->audmode);
  577. }
  578. msp_wake_thread(client);
  579. break;
  580. case AUDC_SET_RADIO:
  581. state->radio = 1;
  582. v4l_dbg(1, client, "switching to radio mode\n");
  583. state->watch_stereo = 0;
  584. switch (state->opmode) {
  585. case OPMODE_MANUAL:
  586. /* set msp3400 to FM radio mode */
  587. msp3400c_setmode(client, MSP_MODE_FM_RADIO);
  588. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  589. MSP_CARRIER(10.7));
  590. msp_set_audio(client);
  591. break;
  592. case OPMODE_AUTODETECT:
  593. case OPMODE_AUTOSELECT:
  594. /* the thread will do for us */
  595. msp_wake_thread(client);
  596. break;
  597. }
  598. break;
  599. /* --- v4l ioctls --- */
  600. /* take care: bttv does userspace copying, we'll get a
  601. kernel pointer here... */
  602. case VIDIOCGAUDIO:
  603. {
  604. struct video_audio *va = arg;
  605. va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
  606. if (state->has_sound_processing)
  607. va->flags |= VIDEO_AUDIO_BALANCE |
  608. VIDEO_AUDIO_BASS |
  609. VIDEO_AUDIO_TREBLE;
  610. if (state->muted)
  611. va->flags |= VIDEO_AUDIO_MUTE;
  612. va->volume = state->volume;
  613. va->balance = state->volume ? state->balance : 32768;
  614. va->bass = state->bass;
  615. va->treble = state->treble;
  616. if (state->opmode == OPMODE_AUTOSELECT)
  617. msp_any_detect_stereo(client);
  618. va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans);
  619. break;
  620. }
  621. case VIDIOCSAUDIO:
  622. {
  623. struct video_audio *va = arg;
  624. state->muted = (va->flags & VIDEO_AUDIO_MUTE);
  625. state->volume = va->volume;
  626. state->balance = va->balance;
  627. state->bass = va->bass;
  628. state->treble = va->treble;
  629. msp_set_audio(client);
  630. if (va->mode != 0 && state->radio == 0)
  631. msp_any_set_audmode(client, msp_mode_v4l1_to_v4l2(va->mode));
  632. break;
  633. }
  634. case VIDIOCSCHAN:
  635. {
  636. struct video_channel *vc = arg;
  637. int update = 0;
  638. v4l2_std_id std;
  639. if (state->radio)
  640. update = 1;
  641. state->radio = 0;
  642. if (vc->norm == VIDEO_MODE_PAL)
  643. std = V4L2_STD_PAL;
  644. else if (vc->norm == VIDEO_MODE_SECAM)
  645. std = V4L2_STD_SECAM;
  646. else
  647. std = V4L2_STD_NTSC;
  648. if (std != state->std) {
  649. state->std = std;
  650. update = 1;
  651. }
  652. if (update)
  653. msp_wake_thread(client);
  654. break;
  655. }
  656. case VIDIOCSFREQ:
  657. case VIDIOC_S_FREQUENCY:
  658. {
  659. /* new channel -- kick audio carrier scan */
  660. msp_wake_thread(client);
  661. break;
  662. }
  663. /* msp34xx specific */
  664. case MSP_SET_MATRIX:
  665. {
  666. struct msp_matrix *mspm = arg;
  667. msp_set_scart(client, mspm->input, mspm->output);
  668. break;
  669. }
  670. /* --- v4l2 ioctls --- */
  671. case VIDIOC_S_STD:
  672. {
  673. v4l2_std_id *id = arg;
  674. int update = state->radio || state->std != *id;
  675. state->std = *id;
  676. state->radio = 0;
  677. if (update)
  678. msp_wake_thread(client);
  679. return 0;
  680. }
  681. case VIDIOC_ENUMINPUT:
  682. {
  683. struct v4l2_input *i = arg;
  684. if (i->index != 0)
  685. return -EINVAL;
  686. i->type = V4L2_INPUT_TYPE_TUNER;
  687. switch (i->index) {
  688. case AUDIO_RADIO:
  689. strcpy(i->name, "Radio");
  690. break;
  691. case AUDIO_EXTERN_1:
  692. strcpy(i->name, "Extern 1");
  693. break;
  694. case AUDIO_EXTERN_2:
  695. strcpy(i->name, "Extern 2");
  696. break;
  697. case AUDIO_TUNER:
  698. strcpy(i->name, "Television");
  699. break;
  700. default:
  701. return -EINVAL;
  702. }
  703. return 0;
  704. }
  705. case VIDIOC_G_AUDIO:
  706. {
  707. struct v4l2_audio *a = arg;
  708. memset(a, 0, sizeof(*a));
  709. switch (a->index) {
  710. case AUDIO_RADIO:
  711. strcpy(a->name, "Radio");
  712. break;
  713. case AUDIO_EXTERN_1:
  714. strcpy(a->name, "Extern 1");
  715. break;
  716. case AUDIO_EXTERN_2:
  717. strcpy(a->name, "Extern 2");
  718. break;
  719. case AUDIO_TUNER:
  720. strcpy(a->name, "Television");
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. msp_any_detect_stereo(client);
  726. if (state->audmode == V4L2_TUNER_MODE_STEREO) {
  727. a->capability = V4L2_AUDCAP_STEREO;
  728. }
  729. break;
  730. }
  731. case VIDIOC_S_AUDIO:
  732. {
  733. struct v4l2_audio *sarg = arg;
  734. switch (sarg->index) {
  735. case AUDIO_RADIO:
  736. /* Hauppauge uses IN2 for the radio */
  737. state->mode = MSP_MODE_FM_RADIO;
  738. scart = SCART_IN2;
  739. break;
  740. case AUDIO_EXTERN_1:
  741. /* IN1 is often used for external input ... */
  742. state->mode = MSP_MODE_EXTERN;
  743. scart = SCART_IN1;
  744. break;
  745. case AUDIO_EXTERN_2:
  746. /* ... sometimes it is IN2 through ;) */
  747. state->mode = MSP_MODE_EXTERN;
  748. scart = SCART_IN2;
  749. break;
  750. case AUDIO_TUNER:
  751. state->mode = -1;
  752. break;
  753. }
  754. if (scart) {
  755. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  756. state->audmode = V4L2_TUNER_MODE_STEREO;
  757. msp_set_scart(client, scart, 0);
  758. msp_write_dsp(client, 0x000d, 0x1900);
  759. }
  760. if (sarg->capability == V4L2_AUDCAP_STEREO) {
  761. state->audmode = V4L2_TUNER_MODE_STEREO;
  762. } else {
  763. state->audmode &= ~V4L2_TUNER_MODE_STEREO;
  764. }
  765. msp_any_set_audmode(client, state->audmode);
  766. msp_wake_thread(client);
  767. break;
  768. }
  769. case VIDIOC_G_TUNER:
  770. {
  771. struct v4l2_tuner *vt = arg;
  772. if (state->opmode == OPMODE_AUTOSELECT)
  773. msp_any_detect_stereo(client);
  774. vt->audmode = state->audmode;
  775. vt->rxsubchans = state->rxsubchans;
  776. vt->capability = V4L2_TUNER_CAP_STEREO |
  777. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  778. break;
  779. }
  780. case VIDIOC_S_TUNER:
  781. {
  782. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  783. /* only set audmode */
  784. if (vt->audmode != -1 && vt->audmode != 0)
  785. msp_any_set_audmode(client, vt->audmode);
  786. break;
  787. }
  788. case VIDIOC_G_AUDOUT:
  789. {
  790. struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
  791. int idx = a->index;
  792. memset(a, 0, sizeof(*a));
  793. switch (idx) {
  794. case 0:
  795. strcpy(a->name, "Scart1 Out");
  796. break;
  797. case 1:
  798. strcpy(a->name, "Scart2 Out");
  799. break;
  800. case 2:
  801. strcpy(a->name, "I2S Out");
  802. break;
  803. default:
  804. return -EINVAL;
  805. }
  806. break;
  807. }
  808. case VIDIOC_S_AUDOUT:
  809. {
  810. struct v4l2_audioout *a = (struct v4l2_audioout *)arg;
  811. if (a->index < 0 || a->index > 2)
  812. return -EINVAL;
  813. v4l_dbg(1, client, "Setting audio out on msp34xx to input %i\n", a->index);
  814. msp_set_scart(client, state->in_scart, a->index + 1);
  815. break;
  816. }
  817. case VIDIOC_INT_I2S_CLOCK_FREQ:
  818. {
  819. u32 *a = (u32 *)arg;
  820. v4l_dbg(1, client, "Setting I2S speed to %d\n", *a);
  821. switch (*a) {
  822. case 1024000:
  823. state->i2s_mode = 0;
  824. break;
  825. case 2048000:
  826. state->i2s_mode = 1;
  827. break;
  828. default:
  829. return -EINVAL;
  830. }
  831. break;
  832. }
  833. case VIDIOC_QUERYCTRL:
  834. {
  835. struct v4l2_queryctrl *qc = arg;
  836. int i;
  837. for (i = 0; i < ARRAY_SIZE(msp_qctrl_std); i++)
  838. if (qc->id && qc->id == msp_qctrl_std[i].id) {
  839. memcpy(qc, &msp_qctrl_std[i], sizeof(*qc));
  840. return 0;
  841. }
  842. if (!state->has_sound_processing)
  843. return -EINVAL;
  844. for (i = 0; i < ARRAY_SIZE(msp_qctrl_sound_processing); i++)
  845. if (qc->id && qc->id == msp_qctrl_sound_processing[i].id) {
  846. memcpy(qc, &msp_qctrl_sound_processing[i], sizeof(*qc));
  847. return 0;
  848. }
  849. return -EINVAL;
  850. }
  851. case VIDIOC_G_CTRL:
  852. return msp_get_ctrl(client, arg);
  853. case VIDIOC_S_CTRL:
  854. return msp_set_ctrl(client, arg);
  855. case VIDIOC_LOG_STATUS:
  856. if (state->opmode == OPMODE_AUTOSELECT)
  857. msp_any_detect_stereo(client);
  858. v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
  859. client->name, state->rev1, state->rev2);
  860. v4l_info(client, "Audio: volume %d%s\n",
  861. state->volume, state->muted ? " (muted)" : "");
  862. if (state->has_sound_processing) {
  863. v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
  864. state->balance, state->bass, state->treble,
  865. state->loudness ? "on" : "off");
  866. }
  867. v4l_info(client, "Mode: %s (%s%s)\n", msp_standard_mode_name(state->mode),
  868. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  869. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  870. v4l_info(client, "ACB: 0x%04x\n", state->acb);
  871. break;
  872. default:
  873. /* nothing */
  874. break;
  875. }
  876. return 0;
  877. }
  878. static int msp_suspend(struct device * dev, pm_message_t state)
  879. {
  880. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  881. v4l_dbg(1, client, "suspend\n");
  882. msp_reset(client);
  883. return 0;
  884. }
  885. static int msp_resume(struct device * dev)
  886. {
  887. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  888. v4l_dbg(1, client, "resume\n");
  889. msp_wake_thread(client);
  890. return 0;
  891. }
  892. /* ----------------------------------------------------------------------- */
  893. static struct i2c_driver i2c_driver;
  894. static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
  895. {
  896. struct i2c_client *client;
  897. struct msp_state *state;
  898. int (*thread_func)(void *data) = NULL;
  899. int msp_hard;
  900. int msp_family;
  901. int msp_revision;
  902. int msp_product, msp_prod_hi, msp_prod_lo;
  903. int msp_rom;
  904. client = kmalloc(sizeof(*client), GFP_KERNEL);
  905. if (client == NULL)
  906. return -ENOMEM;
  907. memset(client, 0, sizeof(*client));
  908. client->addr = address;
  909. client->adapter = adapter;
  910. client->driver = &i2c_driver;
  911. snprintf(client->name, sizeof(client->name) - 1, "msp3400");
  912. if (msp_reset(client) == -1) {
  913. v4l_dbg(1, client, "msp3400 not found\n");
  914. kfree(client);
  915. return -1;
  916. }
  917. state = kmalloc(sizeof(*state), GFP_KERNEL);
  918. if (state == NULL) {
  919. kfree(client);
  920. return -ENOMEM;
  921. }
  922. i2c_set_clientdata(client, state);
  923. memset(state, 0, sizeof(*state));
  924. state->std = V4L2_STD_NTSC;
  925. state->volume = 58880; /* 0db gain */
  926. state->balance = 32768; /* 0db gain */
  927. state->bass = 32768;
  928. state->treble = 32768;
  929. state->loudness = 0;
  930. state->input = -1;
  931. state->muted = 0;
  932. state->i2s_mode = 0;
  933. init_waitqueue_head(&state->wq);
  934. state->rev1 = msp_read_dsp(client, 0x1e);
  935. if (state->rev1 != -1)
  936. state->rev2 = msp_read_dsp(client, 0x1f);
  937. v4l_dbg(1, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
  938. if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
  939. v4l_dbg(1, client, "not an msp3400 (cannot read chip version)\n");
  940. kfree(state);
  941. kfree(client);
  942. return -1;
  943. }
  944. msp_set_audio(client);
  945. msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
  946. msp_product = (state->rev2 >> 8) & 0xff;
  947. msp_prod_hi = msp_product / 10;
  948. msp_prod_lo = msp_product % 10;
  949. msp_revision = (state->rev1 & 0x0f) + '@';
  950. msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
  951. msp_rom = state->rev2 & 0x1f;
  952. snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
  953. msp_family, msp_product,
  954. msp_revision, msp_hard, msp_rom);
  955. /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
  956. state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
  957. /* Has radio support: was added with revision G */
  958. state->has_radio = msp_revision >= 'G';
  959. /* Has headphones output: not for stripped down products */
  960. state->has_headphones = msp_prod_lo < 5;
  961. /* Has scart4 input: not in pre D revisions, not in stripped D revs */
  962. state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
  963. /* Has scart2 and scart3 inputs and scart2 output: not in stripped
  964. down products of the '3' family */
  965. state->has_scart23_in_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
  966. /* Has scart2 a volume control? Not in pre-D revisions. */
  967. state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart23_in_scart2_out;
  968. /* Has subwoofer output: not in pre-D revs and not in stripped down products */
  969. state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
  970. /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
  971. stripped down products */
  972. state->has_sound_processing = msp_prod_lo < 7;
  973. /* Has Virtual Dolby Surround: only in msp34x1 */
  974. state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
  975. /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
  976. state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
  977. state->opmode = opmode;
  978. if (state->opmode == OPMODE_AUTO) {
  979. /* MSP revision G and up have both autodetect and autoselect */
  980. if (msp_revision >= 'G')
  981. state->opmode = OPMODE_AUTOSELECT;
  982. /* MSP revision D and up have autodetect */
  983. else if (msp_revision >= 'D')
  984. state->opmode = OPMODE_AUTODETECT;
  985. else
  986. state->opmode = OPMODE_MANUAL;
  987. }
  988. /* hello world :-) */
  989. v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
  990. v4l_info(client, "%s ", client->name);
  991. if (state->has_nicam && state->has_radio)
  992. printk("supports nicam and radio, ");
  993. else if (state->has_nicam)
  994. printk("supports nicam, ");
  995. else if (state->has_radio)
  996. printk("supports radio, ");
  997. printk("mode is ");
  998. /* version-specific initialization */
  999. switch (state->opmode) {
  1000. case OPMODE_MANUAL:
  1001. printk("manual");
  1002. thread_func = msp3400c_thread;
  1003. break;
  1004. case OPMODE_AUTODETECT:
  1005. printk("autodetect");
  1006. thread_func = msp3410d_thread;
  1007. break;
  1008. case OPMODE_AUTOSELECT:
  1009. printk("autodetect and autoselect");
  1010. thread_func = msp34xxg_thread;
  1011. break;
  1012. }
  1013. printk("\n");
  1014. /* startup control thread if needed */
  1015. if (thread_func) {
  1016. state->kthread = kthread_run(thread_func, client, "msp34xx");
  1017. if (state->kthread == NULL)
  1018. v4l_warn(client, "kernel_thread() failed\n");
  1019. msp_wake_thread(client);
  1020. }
  1021. /* done */
  1022. i2c_attach_client(client);
  1023. return 0;
  1024. }
  1025. static int msp_probe(struct i2c_adapter *adapter)
  1026. {
  1027. if (adapter->class & I2C_CLASS_TV_ANALOG)
  1028. return i2c_probe(adapter, &addr_data, msp_attach);
  1029. return 0;
  1030. }
  1031. static int msp_detach(struct i2c_client *client)
  1032. {
  1033. struct msp_state *state = i2c_get_clientdata(client);
  1034. int err;
  1035. /* shutdown control thread */
  1036. if (state->kthread) {
  1037. state->restart = 1;
  1038. kthread_stop(state->kthread);
  1039. }
  1040. msp_reset(client);
  1041. err = i2c_detach_client(client);
  1042. if (err) {
  1043. return err;
  1044. }
  1045. kfree(state);
  1046. kfree(client);
  1047. return 0;
  1048. }
  1049. /* ----------------------------------------------------------------------- */
  1050. /* i2c implementation */
  1051. static struct i2c_driver i2c_driver = {
  1052. .id = I2C_DRIVERID_MSP3400,
  1053. .attach_adapter = msp_probe,
  1054. .detach_client = msp_detach,
  1055. .command = msp_command,
  1056. .driver = {
  1057. .name = "msp3400",
  1058. .suspend = msp_suspend,
  1059. .resume = msp_resume,
  1060. },
  1061. };
  1062. static int __init msp3400_init_module(void)
  1063. {
  1064. return i2c_add_driver(&i2c_driver);
  1065. }
  1066. static void __exit msp3400_cleanup_module(void)
  1067. {
  1068. i2c_del_driver(&i2c_driver);
  1069. }
  1070. module_init(msp3400_init_module);
  1071. module_exit(msp3400_cleanup_module);
  1072. /*
  1073. * Overrides for Emacs so that we follow Linus's tabbing style.
  1074. * ---------------------------------------------------------------------------
  1075. * Local variables:
  1076. * c-basic-offset: 8
  1077. * End:
  1078. */