msp3400-driver.c 26 KB

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