msp3400-driver.c 25 KB

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