msp3400-driver.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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.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. /* ----------------------------------------------------------------------- */
  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. schedule_timeout_interruptible(msecs_to_jiffies(10));
  143. }
  144. if (err == 3) {
  145. v4l_warn(client, "resetting chip, sound will go off.\n");
  146. msp_reset(client);
  147. return -1;
  148. }
  149. retval = read[0] << 8 | read[1];
  150. v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n",
  151. 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",
  172. dev, addr, val);
  173. for (err = 0; err < 3; err++) {
  174. if (i2c_master_send(client, buffer, 5) == 5)
  175. break;
  176. v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
  177. dev, addr);
  178. schedule_timeout_interruptible(msecs_to_jiffies(10));
  179. }
  180. if (err == 3) {
  181. v4l_warn(client, "resetting chip, sound will go off.\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 = to_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 = to_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",
  261. state->scan_in_progress ? "yes" : "no",
  262. state->volume);
  263. msp_write_dsp(client, 0x0000, val);
  264. msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
  265. if (state->has_scart2_out_volume)
  266. msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
  267. if (state->has_headphones)
  268. msp_write_dsp(client, 0x0006, val);
  269. if (!state->has_sound_processing)
  270. return;
  271. if (val)
  272. bal = (u8)((state->balance / 256) - 128);
  273. bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
  274. treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
  275. loudness = state->loudness ? ((5 * 4) << 8) : 0;
  276. v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
  277. state->balance, state->bass, state->treble, state->loudness);
  278. msp_write_dsp(client, 0x0001, bal << 8);
  279. msp_write_dsp(client, 0x0002, bass);
  280. msp_write_dsp(client, 0x0003, treble);
  281. msp_write_dsp(client, 0x0004, loudness);
  282. if (!state->has_headphones)
  283. return;
  284. msp_write_dsp(client, 0x0030, bal << 8);
  285. msp_write_dsp(client, 0x0031, bass);
  286. msp_write_dsp(client, 0x0032, treble);
  287. msp_write_dsp(client, 0x0033, loudness);
  288. }
  289. /* ------------------------------------------------------------------------ */
  290. static void msp_wake_thread(struct i2c_client *client)
  291. {
  292. struct msp_state *state = to_state(i2c_get_clientdata(client));
  293. if (NULL == state->kthread)
  294. return;
  295. state->watch_stereo = 0;
  296. state->restart = 1;
  297. wake_up_interruptible(&state->wq);
  298. }
  299. int msp_sleep(struct msp_state *state, int timeout)
  300. {
  301. DECLARE_WAITQUEUE(wait, current);
  302. add_wait_queue(&state->wq, &wait);
  303. if (!kthread_should_stop()) {
  304. if (timeout < 0) {
  305. set_current_state(TASK_INTERRUPTIBLE);
  306. schedule();
  307. } else {
  308. schedule_timeout_interruptible
  309. (msecs_to_jiffies(timeout));
  310. }
  311. }
  312. remove_wait_queue(&state->wq, &wait);
  313. try_to_freeze();
  314. return state->restart;
  315. }
  316. /* ------------------------------------------------------------------------ */
  317. static int msp_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  318. {
  319. struct msp_state *state = to_state(sd);
  320. switch (ctrl->id) {
  321. case V4L2_CID_AUDIO_VOLUME:
  322. ctrl->value = state->volume;
  323. break;
  324. case V4L2_CID_AUDIO_MUTE:
  325. ctrl->value = state->muted;
  326. break;
  327. case V4L2_CID_AUDIO_BALANCE:
  328. if (!state->has_sound_processing)
  329. return -EINVAL;
  330. ctrl->value = state->balance;
  331. break;
  332. case V4L2_CID_AUDIO_BASS:
  333. if (!state->has_sound_processing)
  334. return -EINVAL;
  335. ctrl->value = state->bass;
  336. break;
  337. case V4L2_CID_AUDIO_TREBLE:
  338. if (!state->has_sound_processing)
  339. return -EINVAL;
  340. ctrl->value = state->treble;
  341. break;
  342. case V4L2_CID_AUDIO_LOUDNESS:
  343. if (!state->has_sound_processing)
  344. return -EINVAL;
  345. ctrl->value = state->loudness;
  346. break;
  347. default:
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. static int msp_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  353. {
  354. struct msp_state *state = to_state(sd);
  355. struct i2c_client *client = v4l2_get_subdevdata(sd);
  356. switch (ctrl->id) {
  357. case V4L2_CID_AUDIO_VOLUME:
  358. state->volume = ctrl->value;
  359. if (state->volume == 0)
  360. state->balance = 32768;
  361. break;
  362. case V4L2_CID_AUDIO_MUTE:
  363. if (ctrl->value < 0 || ctrl->value >= 2)
  364. return -ERANGE;
  365. state->muted = ctrl->value;
  366. break;
  367. case V4L2_CID_AUDIO_BASS:
  368. if (!state->has_sound_processing)
  369. return -EINVAL;
  370. state->bass = ctrl->value;
  371. break;
  372. case V4L2_CID_AUDIO_TREBLE:
  373. if (!state->has_sound_processing)
  374. return -EINVAL;
  375. state->treble = ctrl->value;
  376. break;
  377. case V4L2_CID_AUDIO_LOUDNESS:
  378. if (!state->has_sound_processing)
  379. return -EINVAL;
  380. state->loudness = ctrl->value;
  381. break;
  382. case V4L2_CID_AUDIO_BALANCE:
  383. if (!state->has_sound_processing)
  384. return -EINVAL;
  385. state->balance = ctrl->value;
  386. break;
  387. default:
  388. return -EINVAL;
  389. }
  390. msp_set_audio(client);
  391. return 0;
  392. }
  393. /* --- v4l2 ioctls --- */
  394. static int msp_s_radio(struct v4l2_subdev *sd)
  395. {
  396. struct msp_state *state = to_state(sd);
  397. struct i2c_client *client = v4l2_get_subdevdata(sd);
  398. if (state->radio)
  399. return 0;
  400. state->radio = 1;
  401. v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
  402. state->watch_stereo = 0;
  403. switch (state->opmode) {
  404. case OPMODE_MANUAL:
  405. /* set msp3400 to FM radio mode */
  406. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  407. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  408. MSP_CARRIER(10.7));
  409. msp_set_audio(client);
  410. break;
  411. case OPMODE_AUTODETECT:
  412. case OPMODE_AUTOSELECT:
  413. /* the thread will do for us */
  414. msp_wake_thread(client);
  415. break;
  416. }
  417. return 0;
  418. }
  419. static int msp_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
  420. {
  421. struct i2c_client *client = v4l2_get_subdevdata(sd);
  422. /* new channel -- kick audio carrier scan */
  423. msp_wake_thread(client);
  424. return 0;
  425. }
  426. static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
  427. {
  428. struct msp_state *state = to_state(sd);
  429. struct i2c_client *client = v4l2_get_subdevdata(sd);
  430. int update = state->radio || state->v4l2_std != id;
  431. state->v4l2_std = id;
  432. state->radio = 0;
  433. if (update)
  434. msp_wake_thread(client);
  435. return 0;
  436. }
  437. static int msp_s_routing(struct v4l2_subdev *sd,
  438. u32 input, u32 output, u32 config)
  439. {
  440. struct msp_state *state = to_state(sd);
  441. struct i2c_client *client = v4l2_get_subdevdata(sd);
  442. int tuner = (input >> 3) & 1;
  443. int sc_in = input & 0x7;
  444. int sc1_out = output & 0xf;
  445. int sc2_out = (output >> 4) & 0xf;
  446. u16 val, reg;
  447. int i;
  448. int extern_input = 1;
  449. if (state->route_in == input && state->route_out == output)
  450. return 0;
  451. state->route_in = input;
  452. state->route_out = output;
  453. /* check if the tuner input is used */
  454. for (i = 0; i < 5; i++) {
  455. if (((input >> (4 + i * 4)) & 0xf) == 0)
  456. extern_input = 0;
  457. }
  458. state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
  459. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  460. msp_set_scart(client, sc_in, 0);
  461. msp_set_scart(client, sc1_out, 1);
  462. msp_set_scart(client, sc2_out, 2);
  463. msp_set_audmode(client);
  464. reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
  465. val = msp_read_dem(client, reg);
  466. msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
  467. /* wake thread when a new input is chosen */
  468. msp_wake_thread(client);
  469. return 0;
  470. }
  471. static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  472. {
  473. struct msp_state *state = to_state(sd);
  474. struct i2c_client *client = v4l2_get_subdevdata(sd);
  475. if (state->radio)
  476. return 0;
  477. if (state->opmode == OPMODE_AUTOSELECT)
  478. msp_detect_stereo(client);
  479. vt->audmode = state->audmode;
  480. vt->rxsubchans = state->rxsubchans;
  481. vt->capability |= V4L2_TUNER_CAP_STEREO |
  482. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  483. return 0;
  484. }
  485. static int msp_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  486. {
  487. struct msp_state *state = to_state(sd);
  488. struct i2c_client *client = v4l2_get_subdevdata(sd);
  489. if (state->radio) /* TODO: add mono/stereo support for radio */
  490. return 0;
  491. if (state->audmode == vt->audmode)
  492. return 0;
  493. state->audmode = vt->audmode;
  494. /* only set audmode */
  495. msp_set_audmode(client);
  496. return 0;
  497. }
  498. static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  499. {
  500. struct msp_state *state = to_state(sd);
  501. struct i2c_client *client = v4l2_get_subdevdata(sd);
  502. v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", freq);
  503. switch (freq) {
  504. case 1024000:
  505. state->i2s_mode = 0;
  506. break;
  507. case 2048000:
  508. state->i2s_mode = 1;
  509. break;
  510. default:
  511. return -EINVAL;
  512. }
  513. return 0;
  514. }
  515. static int msp_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  516. {
  517. struct msp_state *state = to_state(sd);
  518. switch (qc->id) {
  519. case V4L2_CID_AUDIO_VOLUME:
  520. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
  521. case V4L2_CID_AUDIO_MUTE:
  522. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  523. default:
  524. break;
  525. }
  526. if (!state->has_sound_processing)
  527. return -EINVAL;
  528. switch (qc->id) {
  529. case V4L2_CID_AUDIO_LOUDNESS:
  530. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  531. case V4L2_CID_AUDIO_BALANCE:
  532. case V4L2_CID_AUDIO_BASS:
  533. case V4L2_CID_AUDIO_TREBLE:
  534. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
  535. default:
  536. return -EINVAL;
  537. }
  538. return 0;
  539. }
  540. static int msp_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
  541. {
  542. struct msp_state *state = to_state(sd);
  543. struct i2c_client *client = v4l2_get_subdevdata(sd);
  544. return v4l2_chip_ident_i2c_client(client, chip, state->ident,
  545. (state->rev1 << 16) | state->rev2);
  546. }
  547. static int msp_log_status(struct v4l2_subdev *sd)
  548. {
  549. struct msp_state *state = to_state(sd);
  550. struct i2c_client *client = v4l2_get_subdevdata(sd);
  551. const char *p;
  552. if (state->opmode == OPMODE_AUTOSELECT)
  553. msp_detect_stereo(client);
  554. v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
  555. client->name, state->rev1, state->rev2);
  556. v4l_info(client, "Audio: volume %d%s\n",
  557. state->volume, state->muted ? " (muted)" : "");
  558. if (state->has_sound_processing) {
  559. v4l_info(client, "Audio: balance %d bass %d treble %d loudness %s\n",
  560. state->balance, state->bass,
  561. state->treble,
  562. state->loudness ? "on" : "off");
  563. }
  564. switch (state->mode) {
  565. case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
  566. case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
  567. case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono/stereo"; break;
  568. case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
  569. case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
  570. case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
  571. case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
  572. case MSP_MODE_BTSC: p = "BTSC"; break;
  573. case MSP_MODE_EXTERN: p = "External input"; break;
  574. default: p = "unknown"; break;
  575. }
  576. if (state->mode == MSP_MODE_EXTERN) {
  577. v4l_info(client, "Mode: %s\n", p);
  578. } else if (state->opmode == OPMODE_MANUAL) {
  579. v4l_info(client, "Mode: %s (%s%s)\n", p,
  580. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  581. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  582. } else {
  583. if (state->opmode == OPMODE_AUTODETECT)
  584. v4l_info(client, "Mode: %s\n", p);
  585. v4l_info(client, "Standard: %s (%s%s)\n",
  586. msp_standard_std_name(state->std),
  587. (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
  588. (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
  589. }
  590. v4l_info(client, "Audmode: 0x%04x\n", state->audmode);
  591. v4l_info(client, "Routing: 0x%08x (input) 0x%08x (output)\n",
  592. state->route_in, state->route_out);
  593. v4l_info(client, "ACB: 0x%04x\n", state->acb);
  594. return 0;
  595. }
  596. static int msp_suspend(struct i2c_client *client, pm_message_t state)
  597. {
  598. v4l_dbg(1, msp_debug, client, "suspend\n");
  599. msp_reset(client);
  600. return 0;
  601. }
  602. static int msp_resume(struct i2c_client *client)
  603. {
  604. v4l_dbg(1, msp_debug, client, "resume\n");
  605. msp_wake_thread(client);
  606. return 0;
  607. }
  608. /* ----------------------------------------------------------------------- */
  609. static const struct v4l2_subdev_core_ops msp_core_ops = {
  610. .log_status = msp_log_status,
  611. .g_chip_ident = msp_g_chip_ident,
  612. .g_ctrl = msp_g_ctrl,
  613. .s_ctrl = msp_s_ctrl,
  614. .queryctrl = msp_queryctrl,
  615. .s_std = msp_s_std,
  616. };
  617. static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
  618. .s_frequency = msp_s_frequency,
  619. .g_tuner = msp_g_tuner,
  620. .s_tuner = msp_s_tuner,
  621. .s_radio = msp_s_radio,
  622. };
  623. static const struct v4l2_subdev_audio_ops msp_audio_ops = {
  624. .s_routing = msp_s_routing,
  625. .s_i2s_clock_freq = msp_s_i2s_clock_freq,
  626. };
  627. static const struct v4l2_subdev_ops msp_ops = {
  628. .core = &msp_core_ops,
  629. .tuner = &msp_tuner_ops,
  630. .audio = &msp_audio_ops,
  631. };
  632. /* ----------------------------------------------------------------------- */
  633. static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
  634. {
  635. struct msp_state *state;
  636. struct v4l2_subdev *sd;
  637. int (*thread_func)(void *data) = NULL;
  638. int msp_hard;
  639. int msp_family;
  640. int msp_revision;
  641. int msp_product, msp_prod_hi, msp_prod_lo;
  642. int msp_rom;
  643. if (!id)
  644. strlcpy(client->name, "msp3400", sizeof(client->name));
  645. if (msp_reset(client) == -1) {
  646. v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
  647. return -ENODEV;
  648. }
  649. state = kzalloc(sizeof(*state), GFP_KERNEL);
  650. if (!state)
  651. return -ENOMEM;
  652. sd = &state->sd;
  653. v4l2_i2c_subdev_init(sd, client, &msp_ops);
  654. state->v4l2_std = V4L2_STD_NTSC;
  655. state->audmode = V4L2_TUNER_MODE_STEREO;
  656. state->volume = 58880; /* 0db gain */
  657. state->balance = 32768; /* 0db gain */
  658. state->bass = 32768;
  659. state->treble = 32768;
  660. state->loudness = 0;
  661. state->input = -1;
  662. state->muted = 0;
  663. state->i2s_mode = 0;
  664. init_waitqueue_head(&state->wq);
  665. /* These are the reset input/output positions */
  666. state->route_in = MSP_INPUT_DEFAULT;
  667. state->route_out = MSP_OUTPUT_DEFAULT;
  668. state->rev1 = msp_read_dsp(client, 0x1e);
  669. if (state->rev1 != -1)
  670. state->rev2 = msp_read_dsp(client, 0x1f);
  671. v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n",
  672. state->rev1, state->rev2);
  673. if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
  674. v4l_dbg(1, msp_debug, client,
  675. "not an msp3400 (cannot read chip version)\n");
  676. kfree(state);
  677. return -ENODEV;
  678. }
  679. msp_set_audio(client);
  680. msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
  681. msp_product = (state->rev2 >> 8) & 0xff;
  682. msp_prod_hi = msp_product / 10;
  683. msp_prod_lo = msp_product % 10;
  684. msp_revision = (state->rev1 & 0x0f) + '@';
  685. msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
  686. msp_rom = state->rev2 & 0x1f;
  687. /* Rev B=2, C=3, D=4, G=7 */
  688. state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
  689. msp_revision - '@';
  690. /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
  691. state->has_nicam =
  692. msp_prod_hi == 1 || msp_prod_hi == 5;
  693. /* Has radio support: was added with revision G */
  694. state->has_radio =
  695. msp_revision >= 'G';
  696. /* Has headphones output: not for stripped down products */
  697. state->has_headphones =
  698. msp_prod_lo < 5;
  699. /* Has scart2 input: not in stripped down products of the '3' family */
  700. state->has_scart2 =
  701. msp_family >= 4 || msp_prod_lo < 7;
  702. /* Has scart3 input: not in stripped down products of the '3' family */
  703. state->has_scart3 =
  704. msp_family >= 4 || msp_prod_lo < 5;
  705. /* Has scart4 input: not in pre D revisions, not in stripped D revs */
  706. state->has_scart4 =
  707. msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
  708. /* Has scart2 output: not in stripped down products of
  709. * the '3' family */
  710. state->has_scart2_out =
  711. msp_family >= 4 || msp_prod_lo < 5;
  712. /* Has scart2 a volume control? Not in pre-D revisions. */
  713. state->has_scart2_out_volume =
  714. msp_revision > 'C' && state->has_scart2_out;
  715. /* Has a configurable i2s out? */
  716. state->has_i2s_conf =
  717. msp_revision >= 'G' && msp_prod_lo < 7;
  718. /* Has subwoofer output: not in pre-D revs and not in stripped down
  719. * products */
  720. state->has_subwoofer =
  721. msp_revision >= 'D' && msp_prod_lo < 5;
  722. /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
  723. * not in stripped down products */
  724. state->has_sound_processing =
  725. msp_prod_lo < 7;
  726. /* Has Virtual Dolby Surround: only in msp34x1 */
  727. state->has_virtual_dolby_surround =
  728. msp_revision == 'G' && msp_prod_lo == 1;
  729. /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
  730. state->has_dolby_pro_logic =
  731. msp_revision == 'G' && msp_prod_lo == 2;
  732. /* The msp343xG supports BTSC only and cannot do Automatic Standard
  733. * Detection. */
  734. state->force_btsc =
  735. msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
  736. state->opmode = opmode;
  737. if (state->opmode == OPMODE_AUTO) {
  738. /* MSP revision G and up have both autodetect and autoselect */
  739. if (msp_revision >= 'G')
  740. state->opmode = OPMODE_AUTOSELECT;
  741. /* MSP revision D and up have autodetect */
  742. else if (msp_revision >= 'D')
  743. state->opmode = OPMODE_AUTODETECT;
  744. else
  745. state->opmode = OPMODE_MANUAL;
  746. }
  747. /* hello world :-) */
  748. v4l_info(client, "MSP%d4%02d%c-%c%d found @ 0x%x (%s)\n",
  749. msp_family, msp_product,
  750. msp_revision, msp_hard, msp_rom,
  751. client->addr << 1, client->adapter->name);
  752. v4l_info(client, "%s ", client->name);
  753. if (state->has_nicam && state->has_radio)
  754. printk(KERN_CONT "supports nicam and radio, ");
  755. else if (state->has_nicam)
  756. printk(KERN_CONT "supports nicam, ");
  757. else if (state->has_radio)
  758. printk(KERN_CONT "supports radio, ");
  759. printk(KERN_CONT "mode is ");
  760. /* version-specific initialization */
  761. switch (state->opmode) {
  762. case OPMODE_MANUAL:
  763. printk(KERN_CONT "manual");
  764. thread_func = msp3400c_thread;
  765. break;
  766. case OPMODE_AUTODETECT:
  767. printk(KERN_CONT "autodetect");
  768. thread_func = msp3410d_thread;
  769. break;
  770. case OPMODE_AUTOSELECT:
  771. printk(KERN_CONT "autodetect and autoselect");
  772. thread_func = msp34xxg_thread;
  773. break;
  774. }
  775. printk(KERN_CONT "\n");
  776. /* startup control thread if needed */
  777. if (thread_func) {
  778. state->kthread = kthread_run(thread_func, client, "msp34xx");
  779. if (IS_ERR(state->kthread))
  780. v4l_warn(client, "kernel_thread() failed\n");
  781. msp_wake_thread(client);
  782. }
  783. return 0;
  784. }
  785. static int msp_remove(struct i2c_client *client)
  786. {
  787. struct msp_state *state = to_state(i2c_get_clientdata(client));
  788. v4l2_device_unregister_subdev(&state->sd);
  789. /* shutdown control thread */
  790. if (state->kthread) {
  791. state->restart = 1;
  792. kthread_stop(state->kthread);
  793. }
  794. msp_reset(client);
  795. kfree(state);
  796. return 0;
  797. }
  798. /* ----------------------------------------------------------------------- */
  799. static const struct i2c_device_id msp_id[] = {
  800. { "msp3400", 0 },
  801. { }
  802. };
  803. MODULE_DEVICE_TABLE(i2c, msp_id);
  804. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  805. .name = "msp3400",
  806. .probe = msp_probe,
  807. .remove = msp_remove,
  808. .suspend = msp_suspend,
  809. .resume = msp_resume,
  810. .id_table = msp_id,
  811. };
  812. /*
  813. * Overrides for Emacs so that we follow Linus's tabbing style.
  814. * ---------------------------------------------------------------------------
  815. * Local variables:
  816. * c-basic-offset: 8
  817. * End:
  818. */