msp3400-driver.c 26 KB

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