msp3400-driver.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. if (state->has_i2s_conf)
  249. msp_write_dem(client, 0x40, state->i2s_mode);
  250. }
  251. void msp_set_audio(struct i2c_client *client)
  252. {
  253. struct msp_state *state = i2c_get_clientdata(client);
  254. int bal = 0, bass, treble, loudness;
  255. int val = 0;
  256. int reallymuted = state->muted | state->scan_in_progress;
  257. if (!reallymuted)
  258. val = (state->volume * 0x7f / 65535) << 8;
  259. v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
  260. state->muted ? "on" : "off", state->scan_in_progress ? "yes" : "no",
  261. state->volume);
  262. msp_write_dsp(client, 0x0000, val);
  263. msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
  264. if (state->has_scart2_out_volume)
  265. msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
  266. if (state->has_headphones)
  267. msp_write_dsp(client, 0x0006, val);
  268. if (!state->has_sound_processing)
  269. return;
  270. if (val)
  271. bal = (u8)((state->balance / 256) - 128);
  272. bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
  273. treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
  274. loudness = state->loudness ? ((5 * 4) << 8) : 0;
  275. v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
  276. state->balance, state->bass, state->treble, state->loudness);
  277. msp_write_dsp(client, 0x0001, bal << 8);
  278. msp_write_dsp(client, 0x0002, bass);
  279. msp_write_dsp(client, 0x0003, treble);
  280. msp_write_dsp(client, 0x0004, loudness);
  281. if (!state->has_headphones)
  282. return;
  283. msp_write_dsp(client, 0x0030, bal << 8);
  284. msp_write_dsp(client, 0x0031, bass);
  285. msp_write_dsp(client, 0x0032, treble);
  286. msp_write_dsp(client, 0x0033, loudness);
  287. }
  288. /* ------------------------------------------------------------------------ */
  289. static void msp_wake_thread(struct i2c_client *client)
  290. {
  291. struct msp_state *state = i2c_get_clientdata(client);
  292. if (NULL == state->kthread)
  293. return;
  294. state->watch_stereo = 0;
  295. state->restart = 1;
  296. wake_up_interruptible(&state->wq);
  297. }
  298. int msp_sleep(struct msp_state *state, int timeout)
  299. {
  300. DECLARE_WAITQUEUE(wait, current);
  301. add_wait_queue(&state->wq, &wait);
  302. if (!kthread_should_stop()) {
  303. if (timeout < 0) {
  304. set_current_state(TASK_INTERRUPTIBLE);
  305. schedule();
  306. } else {
  307. schedule_timeout_interruptible
  308. (msecs_to_jiffies(timeout));
  309. }
  310. }
  311. remove_wait_queue(&state->wq, &wait);
  312. try_to_freeze();
  313. return state->restart;
  314. }
  315. /* ------------------------------------------------------------------------ */
  316. static int msp_mode_v4l2_to_v4l1(int rxsubchans, int audmode)
  317. {
  318. if (rxsubchans == V4L2_TUNER_SUB_MONO)
  319. return VIDEO_SOUND_MONO;
  320. if (rxsubchans == V4L2_TUNER_SUB_STEREO)
  321. return VIDEO_SOUND_STEREO;
  322. if (audmode == V4L2_TUNER_MODE_LANG2)
  323. return VIDEO_SOUND_LANG2;
  324. return VIDEO_SOUND_LANG1;
  325. }
  326. static int msp_mode_v4l1_to_v4l2(int mode)
  327. {
  328. if (mode & VIDEO_SOUND_STEREO)
  329. return V4L2_TUNER_MODE_STEREO;
  330. if (mode & VIDEO_SOUND_LANG2)
  331. return V4L2_TUNER_MODE_LANG2;
  332. if (mode & VIDEO_SOUND_LANG1)
  333. return V4L2_TUNER_MODE_LANG1;
  334. return V4L2_TUNER_MODE_MONO;
  335. }
  336. static struct v4l2_queryctrl msp_qctrl_std[] = {
  337. {
  338. .id = V4L2_CID_AUDIO_VOLUME,
  339. .name = "Volume",
  340. .minimum = 0,
  341. .maximum = 65535,
  342. .step = 65535/100,
  343. .default_value = 58880,
  344. .flags = 0,
  345. .type = V4L2_CTRL_TYPE_INTEGER,
  346. },{
  347. .id = V4L2_CID_AUDIO_MUTE,
  348. .name = "Mute",
  349. .minimum = 0,
  350. .maximum = 1,
  351. .step = 1,
  352. .default_value = 1,
  353. .flags = 0,
  354. .type = V4L2_CTRL_TYPE_BOOLEAN,
  355. },
  356. };
  357. static struct v4l2_queryctrl msp_qctrl_sound_processing[] = {
  358. {
  359. .id = V4L2_CID_AUDIO_BALANCE,
  360. .name = "Balance",
  361. .minimum = 0,
  362. .maximum = 65535,
  363. .step = 65535/100,
  364. .default_value = 32768,
  365. .flags = 0,
  366. .type = V4L2_CTRL_TYPE_INTEGER,
  367. },{
  368. .id = V4L2_CID_AUDIO_BASS,
  369. .name = "Bass",
  370. .minimum = 0,
  371. .maximum = 65535,
  372. .step = 65535/100,
  373. .default_value = 32768,
  374. .type = V4L2_CTRL_TYPE_INTEGER,
  375. },{
  376. .id = V4L2_CID_AUDIO_TREBLE,
  377. .name = "Treble",
  378. .minimum = 0,
  379. .maximum = 65535,
  380. .step = 65535/100,
  381. .default_value = 32768,
  382. .type = V4L2_CTRL_TYPE_INTEGER,
  383. },{
  384. .id = V4L2_CID_AUDIO_LOUDNESS,
  385. .name = "Loudness",
  386. .minimum = 0,
  387. .maximum = 1,
  388. .step = 1,
  389. .default_value = 1,
  390. .flags = 0,
  391. .type = V4L2_CTRL_TYPE_BOOLEAN,
  392. },
  393. };
  394. static int msp_get_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  395. {
  396. struct msp_state *state = i2c_get_clientdata(client);
  397. switch (ctrl->id) {
  398. case V4L2_CID_AUDIO_VOLUME:
  399. ctrl->value = state->volume;
  400. break;
  401. case V4L2_CID_AUDIO_MUTE:
  402. ctrl->value = state->muted;
  403. break;
  404. case V4L2_CID_AUDIO_BALANCE:
  405. if (!state->has_sound_processing)
  406. return -EINVAL;
  407. ctrl->value = state->balance;
  408. break;
  409. case V4L2_CID_AUDIO_BASS:
  410. if (!state->has_sound_processing)
  411. return -EINVAL;
  412. ctrl->value = state->bass;
  413. break;
  414. case V4L2_CID_AUDIO_TREBLE:
  415. if (!state->has_sound_processing)
  416. return -EINVAL;
  417. ctrl->value = state->treble;
  418. break;
  419. case V4L2_CID_AUDIO_LOUDNESS:
  420. if (!state->has_sound_processing)
  421. return -EINVAL;
  422. ctrl->value = state->loudness;
  423. break;
  424. default:
  425. return -EINVAL;
  426. }
  427. return 0;
  428. }
  429. static int msp_set_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
  430. {
  431. struct msp_state *state = i2c_get_clientdata(client);
  432. switch (ctrl->id) {
  433. case V4L2_CID_AUDIO_VOLUME:
  434. state->volume = ctrl->value;
  435. if (state->volume == 0)
  436. state->balance = 32768;
  437. break;
  438. case V4L2_CID_AUDIO_MUTE:
  439. if (ctrl->value < 0 || ctrl->value >= 2)
  440. return -ERANGE;
  441. state->muted = ctrl->value;
  442. break;
  443. case V4L2_CID_AUDIO_BASS:
  444. if (!state->has_sound_processing)
  445. return -EINVAL;
  446. state->bass = ctrl->value;
  447. break;
  448. case V4L2_CID_AUDIO_TREBLE:
  449. if (!state->has_sound_processing)
  450. return -EINVAL;
  451. state->treble = ctrl->value;
  452. break;
  453. case V4L2_CID_AUDIO_LOUDNESS:
  454. if (!state->has_sound_processing)
  455. return -EINVAL;
  456. state->loudness = ctrl->value;
  457. break;
  458. case V4L2_CID_AUDIO_BALANCE:
  459. if (!state->has_sound_processing)
  460. return -EINVAL;
  461. state->balance = ctrl->value;
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. msp_set_audio(client);
  467. return 0;
  468. }
  469. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  470. {
  471. struct msp_state *state = i2c_get_clientdata(client);
  472. if (msp_debug >= 2)
  473. v4l_i2c_print_ioctl(client, cmd);
  474. switch (cmd) {
  475. case AUDC_SET_RADIO:
  476. if (state->radio)
  477. return 0;
  478. state->radio = 1;
  479. v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
  480. state->watch_stereo = 0;
  481. switch (state->opmode) {
  482. case OPMODE_MANUAL:
  483. /* set msp3400 to FM radio mode */
  484. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  485. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  486. MSP_CARRIER(10.7));
  487. msp_set_audio(client);
  488. break;
  489. case OPMODE_AUTODETECT:
  490. case OPMODE_AUTOSELECT:
  491. /* the thread will do for us */
  492. msp_wake_thread(client);
  493. break;
  494. }
  495. break;
  496. /* --- v4l ioctls --- */
  497. /* take care: bttv does userspace copying, we'll get a
  498. kernel pointer here... */
  499. case VIDIOCGAUDIO:
  500. {
  501. struct video_audio *va = arg;
  502. va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
  503. if (state->has_sound_processing)
  504. va->flags |= VIDEO_AUDIO_BALANCE |
  505. VIDEO_AUDIO_BASS |
  506. VIDEO_AUDIO_TREBLE;
  507. if (state->muted)
  508. va->flags |= VIDEO_AUDIO_MUTE;
  509. va->volume = state->volume;
  510. va->balance = state->volume ? state->balance : 32768;
  511. va->bass = state->bass;
  512. va->treble = state->treble;
  513. if (state->radio)
  514. break;
  515. if (state->opmode == OPMODE_AUTOSELECT)
  516. msp_detect_stereo(client);
  517. va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
  518. break;
  519. }
  520. case VIDIOCSAUDIO:
  521. {
  522. struct video_audio *va = arg;
  523. state->muted = (va->flags & VIDEO_AUDIO_MUTE);
  524. state->volume = va->volume;
  525. state->balance = va->balance;
  526. state->bass = va->bass;
  527. state->treble = va->treble;
  528. msp_set_audio(client);
  529. if (va->mode != 0 && state->radio == 0 &&
  530. state->audmode != msp_mode_v4l1_to_v4l2(va->mode)) {
  531. state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
  532. msp_set_audmode(client);
  533. }
  534. break;
  535. }
  536. case VIDIOCSCHAN:
  537. {
  538. struct video_channel *vc = arg;
  539. int update = 0;
  540. v4l2_std_id std;
  541. if (state->radio)
  542. update = 1;
  543. state->radio = 0;
  544. if (vc->norm == VIDEO_MODE_PAL)
  545. std = V4L2_STD_PAL;
  546. else if (vc->norm == VIDEO_MODE_SECAM)
  547. std = V4L2_STD_SECAM;
  548. else
  549. std = V4L2_STD_NTSC;
  550. if (std != state->v4l2_std) {
  551. state->v4l2_std = std;
  552. update = 1;
  553. }
  554. if (update)
  555. msp_wake_thread(client);
  556. break;
  557. }
  558. case VIDIOCSFREQ:
  559. case VIDIOC_S_FREQUENCY:
  560. {
  561. /* new channel -- kick audio carrier scan */
  562. msp_wake_thread(client);
  563. break;
  564. }
  565. /* --- v4l2 ioctls --- */
  566. case VIDIOC_S_STD:
  567. {
  568. v4l2_std_id *id = arg;
  569. int update = state->radio || state->v4l2_std != *id;
  570. state->v4l2_std = *id;
  571. state->radio = 0;
  572. if (update)
  573. msp_wake_thread(client);
  574. return 0;
  575. }
  576. case VIDIOC_INT_G_AUDIO_ROUTING:
  577. {
  578. struct v4l2_routing *rt = arg;
  579. *rt = state->routing;
  580. break;
  581. }
  582. case VIDIOC_INT_S_AUDIO_ROUTING:
  583. {
  584. struct v4l2_routing *rt = arg;
  585. int tuner = (rt->input >> 3) & 1;
  586. int sc_in = rt->input & 0x7;
  587. int sc1_out = rt->output & 0xf;
  588. int sc2_out = (rt->output >> 4) & 0xf;
  589. u16 val, reg;
  590. int i;
  591. int extern_input = 1;
  592. if (state->routing.input == rt->input &&
  593. state->routing.output == rt->output)
  594. break;
  595. state->routing = *rt;
  596. /* check if the tuner input is used */
  597. for (i = 0; i < 5; i++) {
  598. if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
  599. extern_input = 0;
  600. }
  601. if (extern_input)
  602. state->mode = MSP_MODE_EXTERN;
  603. else
  604. state->mode = MSP_MODE_AM_DETECT;
  605. msp_set_scart(client, sc_in, 0);
  606. msp_set_scart(client, sc1_out, 1);
  607. msp_set_scart(client, sc2_out, 2);
  608. msp_set_audmode(client);
  609. reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
  610. val = msp_read_dem(client, reg);
  611. msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
  612. /* wake thread when a new input is chosen */
  613. msp_wake_thread(client);
  614. break;
  615. }
  616. case VIDIOC_G_TUNER:
  617. {
  618. struct v4l2_tuner *vt = arg;
  619. if (state->radio)
  620. break;
  621. if (state->opmode == OPMODE_AUTOSELECT)
  622. msp_detect_stereo(client);
  623. vt->audmode = state->audmode;
  624. vt->rxsubchans = state->rxsubchans;
  625. vt->capability |= V4L2_TUNER_CAP_STEREO |
  626. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  627. break;
  628. }
  629. case VIDIOC_S_TUNER:
  630. {
  631. struct v4l2_tuner *vt = (struct v4l2_tuner *)arg;
  632. if (state->radio) /* TODO: add mono/stereo support for radio */
  633. break;
  634. if (state->audmode == vt->audmode)
  635. break;
  636. state->audmode = vt->audmode;
  637. /* only set audmode */
  638. msp_set_audmode(client);
  639. break;
  640. }
  641. case VIDIOC_INT_I2S_CLOCK_FREQ:
  642. {
  643. u32 *a = (u32 *)arg;
  644. v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", *a);
  645. switch (*a) {
  646. case 1024000:
  647. state->i2s_mode = 0;
  648. break;
  649. case 2048000:
  650. state->i2s_mode = 1;
  651. break;
  652. default:
  653. return -EINVAL;
  654. }
  655. break;
  656. }
  657. case VIDIOC_QUERYCTRL:
  658. {
  659. struct v4l2_queryctrl *qc = arg;
  660. int i;
  661. for (i = 0; i < ARRAY_SIZE(msp_qctrl_std); i++)
  662. if (qc->id && qc->id == msp_qctrl_std[i].id) {
  663. memcpy(qc, &msp_qctrl_std[i], sizeof(*qc));
  664. return 0;
  665. }
  666. if (!state->has_sound_processing)
  667. return -EINVAL;
  668. for (i = 0; i < ARRAY_SIZE(msp_qctrl_sound_processing); i++)
  669. if (qc->id && qc->id == msp_qctrl_sound_processing[i].id) {
  670. memcpy(qc, &msp_qctrl_sound_processing[i], sizeof(*qc));
  671. return 0;
  672. }
  673. return -EINVAL;
  674. }
  675. case VIDIOC_G_CTRL:
  676. return msp_get_ctrl(client, arg);
  677. case VIDIOC_S_CTRL:
  678. return msp_set_ctrl(client, arg);
  679. case VIDIOC_LOG_STATUS:
  680. {
  681. const char *p;
  682. if (state->opmode == OPMODE_AUTOSELECT)
  683. msp_detect_stereo(client);
  684. v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
  685. client->name, state->rev1, state->rev2);
  686. v4l_info(client, "Audio: volume %d%s\n",
  687. state->volume, state->muted ? " (muted)" : "");
  688. if (state->has_sound_processing) {
  689. v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
  690. state->balance, state->bass, state->treble,
  691. state->loudness ? "on" : "off");
  692. }
  693. switch (state->mode) {
  694. case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
  695. case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
  696. case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono + FM-stereo"; break;
  697. case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
  698. case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
  699. case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
  700. case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
  701. case MSP_MODE_BTSC: p = "BTSC"; break;
  702. case MSP_MODE_EXTERN: p = "External input"; break;
  703. default: p = "unknown"; break;
  704. }
  705. if (state->mode == MSP_MODE_EXTERN) {
  706. v4l_info(client, "Mode: %s\n", p);
  707. } else if (state->opmode == OPMODE_MANUAL) {
  708. v4l_info(client, "Mode: %s (%s%s)\n", p,
  709. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  710. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  711. } else {
  712. if (state->opmode == OPMODE_AUTODETECT)
  713. v4l_info(client, "Mode: %s\n", p);
  714. v4l_info(client, "Standard: %s (%s%s)\n",
  715. msp_standard_std_name(state->std),
  716. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  717. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  718. }
  719. v4l_info(client, "Audmode: 0x%04x\n", state->audmode);
  720. v4l_info(client, "Routing: 0x%08x (input) 0x%08x (output)\n",
  721. state->routing.input, state->routing.output);
  722. v4l_info(client, "ACB: 0x%04x\n", state->acb);
  723. break;
  724. }
  725. default:
  726. /* unknown */
  727. return -EINVAL;
  728. }
  729. return 0;
  730. }
  731. static int msp_suspend(struct device * dev, pm_message_t state)
  732. {
  733. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  734. v4l_dbg(1, msp_debug, client, "suspend\n");
  735. msp_reset(client);
  736. return 0;
  737. }
  738. static int msp_resume(struct device * dev)
  739. {
  740. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  741. v4l_dbg(1, msp_debug, client, "resume\n");
  742. msp_wake_thread(client);
  743. return 0;
  744. }
  745. /* ----------------------------------------------------------------------- */
  746. static struct i2c_driver i2c_driver;
  747. static int msp_attach(struct i2c_adapter *adapter, int address, int kind)
  748. {
  749. struct i2c_client *client;
  750. struct msp_state *state;
  751. int (*thread_func)(void *data) = NULL;
  752. int msp_hard;
  753. int msp_family;
  754. int msp_revision;
  755. int msp_product, msp_prod_hi, msp_prod_lo;
  756. int msp_rom;
  757. client = kmalloc(sizeof(*client), GFP_KERNEL);
  758. if (client == NULL)
  759. return -ENOMEM;
  760. memset(client, 0, sizeof(*client));
  761. client->addr = address;
  762. client->adapter = adapter;
  763. client->driver = &i2c_driver;
  764. snprintf(client->name, sizeof(client->name) - 1, "msp3400");
  765. if (msp_reset(client) == -1) {
  766. v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
  767. kfree(client);
  768. return -1;
  769. }
  770. state = kmalloc(sizeof(*state), GFP_KERNEL);
  771. if (state == NULL) {
  772. kfree(client);
  773. return -ENOMEM;
  774. }
  775. i2c_set_clientdata(client, state);
  776. memset(state, 0, sizeof(*state));
  777. state->v4l2_std = V4L2_STD_NTSC;
  778. state->audmode = V4L2_TUNER_MODE_STEREO;
  779. state->volume = 58880; /* 0db gain */
  780. state->balance = 32768; /* 0db gain */
  781. state->bass = 32768;
  782. state->treble = 32768;
  783. state->loudness = 0;
  784. state->input = -1;
  785. state->muted = 0;
  786. state->i2s_mode = 0;
  787. init_waitqueue_head(&state->wq);
  788. /* These are the reset input/output positions */
  789. state->routing.input = MSP_INPUT_DEFAULT;
  790. state->routing.output = MSP_OUTPUT_DEFAULT;
  791. state->rev1 = msp_read_dsp(client, 0x1e);
  792. if (state->rev1 != -1)
  793. state->rev2 = msp_read_dsp(client, 0x1f);
  794. v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n", state->rev1, state->rev2);
  795. if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
  796. v4l_dbg(1, msp_debug, client, "not an msp3400 (cannot read chip version)\n");
  797. kfree(state);
  798. kfree(client);
  799. return -1;
  800. }
  801. msp_set_audio(client);
  802. msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
  803. msp_product = (state->rev2 >> 8) & 0xff;
  804. msp_prod_hi = msp_product / 10;
  805. msp_prod_lo = msp_product % 10;
  806. msp_revision = (state->rev1 & 0x0f) + '@';
  807. msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
  808. msp_rom = state->rev2 & 0x1f;
  809. snprintf(client->name, sizeof(client->name), "MSP%d4%02d%c-%c%d",
  810. msp_family, msp_product,
  811. msp_revision, msp_hard, msp_rom);
  812. /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
  813. state->has_nicam = msp_prod_hi == 1 || msp_prod_hi == 5;
  814. /* Has radio support: was added with revision G */
  815. state->has_radio = msp_revision >= 'G';
  816. /* Has headphones output: not for stripped down products */
  817. state->has_headphones = msp_prod_lo < 5;
  818. /* Has scart2 input: not in stripped down products of the '3' family */
  819. state->has_scart2 = msp_family >= 4 || msp_prod_lo < 7;
  820. /* Has scart3 input: not in stripped down products of the '3' family */
  821. state->has_scart3 = msp_family >= 4 || msp_prod_lo < 5;
  822. /* Has scart4 input: not in pre D revisions, not in stripped D revs */
  823. state->has_scart4 = msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
  824. /* Has scart2 output: not in stripped down products of the '3' family */
  825. state->has_scart2_out = msp_family >= 4 || msp_prod_lo < 5;
  826. /* Has scart2 a volume control? Not in pre-D revisions. */
  827. state->has_scart2_out_volume = msp_revision > 'C' && state->has_scart2_out;
  828. /* Has a configurable i2s out? */
  829. state->has_i2s_conf = msp_revision >= 'G' && msp_prod_lo < 7;
  830. /* Has subwoofer output: not in pre-D revs and not in stripped down products */
  831. state->has_subwoofer = msp_revision >= 'D' && msp_prod_lo < 5;
  832. /* Has soundprocessing (bass/treble/balance/loudness/equalizer): not in
  833. stripped down products */
  834. state->has_sound_processing = msp_prod_lo < 7;
  835. /* Has Virtual Dolby Surround: only in msp34x1 */
  836. state->has_virtual_dolby_surround = msp_revision == 'G' && msp_prod_lo == 1;
  837. /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
  838. state->has_dolby_pro_logic = msp_revision == 'G' && msp_prod_lo == 2;
  839. state->opmode = opmode;
  840. if (state->opmode == OPMODE_AUTO) {
  841. /* MSP revision G and up have both autodetect and autoselect */
  842. if (msp_revision >= 'G')
  843. state->opmode = OPMODE_AUTOSELECT;
  844. /* MSP revision D and up have autodetect */
  845. else if (msp_revision >= 'D')
  846. state->opmode = OPMODE_AUTODETECT;
  847. else
  848. state->opmode = OPMODE_MANUAL;
  849. }
  850. /* hello world :-) */
  851. v4l_info(client, "%s found @ 0x%x (%s)\n", client->name, address << 1, adapter->name);
  852. v4l_info(client, "%s ", client->name);
  853. if (state->has_nicam && state->has_radio)
  854. printk("supports nicam and radio, ");
  855. else if (state->has_nicam)
  856. printk("supports nicam, ");
  857. else if (state->has_radio)
  858. printk("supports radio, ");
  859. printk("mode is ");
  860. /* version-specific initialization */
  861. switch (state->opmode) {
  862. case OPMODE_MANUAL:
  863. printk("manual");
  864. thread_func = msp3400c_thread;
  865. break;
  866. case OPMODE_AUTODETECT:
  867. printk("autodetect");
  868. thread_func = msp3410d_thread;
  869. break;
  870. case OPMODE_AUTOSELECT:
  871. printk("autodetect and autoselect");
  872. thread_func = msp34xxg_thread;
  873. break;
  874. }
  875. printk("\n");
  876. /* startup control thread if needed */
  877. if (thread_func) {
  878. state->kthread = kthread_run(thread_func, client, "msp34xx");
  879. if (state->kthread == NULL)
  880. v4l_warn(client, "kernel_thread() failed\n");
  881. msp_wake_thread(client);
  882. }
  883. /* done */
  884. i2c_attach_client(client);
  885. return 0;
  886. }
  887. static int msp_probe(struct i2c_adapter *adapter)
  888. {
  889. if (adapter->class & I2C_CLASS_TV_ANALOG)
  890. return i2c_probe(adapter, &addr_data, msp_attach);
  891. return 0;
  892. }
  893. static int msp_detach(struct i2c_client *client)
  894. {
  895. struct msp_state *state = i2c_get_clientdata(client);
  896. int err;
  897. /* shutdown control thread */
  898. if (state->kthread) {
  899. state->restart = 1;
  900. kthread_stop(state->kthread);
  901. }
  902. msp_reset(client);
  903. err = i2c_detach_client(client);
  904. if (err) {
  905. return err;
  906. }
  907. kfree(state);
  908. kfree(client);
  909. return 0;
  910. }
  911. /* ----------------------------------------------------------------------- */
  912. /* i2c implementation */
  913. static struct i2c_driver i2c_driver = {
  914. .id = I2C_DRIVERID_MSP3400,
  915. .attach_adapter = msp_probe,
  916. .detach_client = msp_detach,
  917. .command = msp_command,
  918. .driver = {
  919. .name = "msp3400",
  920. .suspend = msp_suspend,
  921. .resume = msp_resume,
  922. },
  923. };
  924. static int __init msp3400_init_module(void)
  925. {
  926. return i2c_add_driver(&i2c_driver);
  927. }
  928. static void __exit msp3400_cleanup_module(void)
  929. {
  930. i2c_del_driver(&i2c_driver);
  931. }
  932. module_init(msp3400_init_module);
  933. module_exit(msp3400_cleanup_module);
  934. /*
  935. * Overrides for Emacs so that we follow Linus's tabbing style.
  936. * ---------------------------------------------------------------------------
  937. * Local variables:
  938. * c-basic-offset: 8
  939. * End:
  940. */