msp3400-driver.c 29 KB

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