msp3400-kthreads.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/freezer.h>
  11. #include <linux/videodev2.h>
  12. #include <media/v4l2-common.h>
  13. #include <media/drv-intf/msp3400.h>
  14. #include <linux/kthread.h>
  15. #include <linux/suspend.h>
  16. #include "msp3400-driver.h"
  17. /* this one uses the automatic sound standard detection of newer msp34xx
  18. chip versions */
  19. static struct {
  20. int retval;
  21. int main, second;
  22. char *name;
  23. v4l2_std_id std;
  24. } msp_stdlist[] = {
  25. { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL },
  26. { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL },
  27. { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
  28. "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN },
  29. { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
  30. "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG },
  31. { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
  32. "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK },
  33. { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
  34. "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK },
  35. { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  36. "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK },
  37. { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
  38. "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK },
  39. { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
  40. "5.5/5.85 B/G NICAM FM", V4L2_STD_BG },
  41. { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  42. "6.5/5.85 L NICAM AM", V4L2_STD_L },
  43. { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
  44. "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I },
  45. { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  46. "6.5/5.85 D/K NICAM FM", V4L2_STD_DK },
  47. { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  48. "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK },
  49. { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  50. "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK },
  51. { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
  52. "4.5 M BTSC-Stereo", V4L2_STD_MTS },
  53. { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
  54. "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS },
  55. { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
  56. "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP },
  57. { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  58. "10.7 FM-Stereo Radio", V4L2_STD_ALL },
  59. { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  60. "6.5 SAT-Mono", V4L2_STD_ALL },
  61. { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
  62. "7.02/7.20 SAT-Stereo", V4L2_STD_ALL },
  63. { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
  64. "7.2 SAT ADR", V4L2_STD_ALL },
  65. { -1, 0, 0, NULL, 0 }, /* EOF */
  66. };
  67. static struct msp3400c_init_data_dem {
  68. int fir1[6];
  69. int fir2[6];
  70. int cdo1;
  71. int cdo2;
  72. int ad_cv;
  73. int mode_reg;
  74. int dsp_src;
  75. int dsp_matrix;
  76. } msp3400c_init_data[] = {
  77. { /* AM (for carrier detect / msp3400) */
  78. {75, 19, 36, 35, 39, 40},
  79. {75, 19, 36, 35, 39, 40},
  80. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  81. 0x00d0, 0x0500, 0x0020, 0x3000
  82. }, { /* AM (for carrier detect / msp3410) */
  83. {-1, -1, -8, 2, 59, 126},
  84. {-1, -1, -8, 2, 59, 126},
  85. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  86. 0x00d0, 0x0100, 0x0020, 0x3000
  87. }, { /* FM Radio */
  88. {-8, -8, 4, 6, 78, 107},
  89. {-8, -8, 4, 6, 78, 107},
  90. MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  91. 0x00d0, 0x0480, 0x0020, 0x3000
  92. }, { /* Terrestrial FM-mono + FM-stereo */
  93. {3, 18, 27, 48, 66, 72},
  94. {3, 18, 27, 48, 66, 72},
  95. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  96. 0x00d0, 0x0480, 0x0030, 0x3000
  97. }, { /* Sat FM-mono */
  98. { 1, 9, 14, 24, 33, 37},
  99. { 3, 18, 27, 48, 66, 72},
  100. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  101. 0x00c6, 0x0480, 0x0000, 0x3000
  102. }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
  103. {-2, -8, -10, 10, 50, 86},
  104. {3, 18, 27, 48, 66, 72},
  105. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  106. 0x00d0, 0x0040, 0x0120, 0x3000
  107. }, { /* NICAM/FM -- I (6.0/6.552) */
  108. {2, 4, -6, -4, 40, 94},
  109. {3, 18, 27, 48, 66, 72},
  110. MSP_CARRIER(6.0), MSP_CARRIER(6.0),
  111. 0x00d0, 0x0040, 0x0120, 0x3000
  112. }, { /* NICAM/AM -- L (6.5/5.85) */
  113. {-2, -8, -10, 10, 50, 86},
  114. {-4, -12, -9, 23, 79, 126},
  115. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  116. 0x00c6, 0x0140, 0x0120, 0x7c00
  117. },
  118. };
  119. struct msp3400c_carrier_detect {
  120. int cdo;
  121. char *name;
  122. };
  123. static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
  124. /* main carrier */
  125. { MSP_CARRIER(4.5), "4.5 NTSC" },
  126. { MSP_CARRIER(5.5), "5.5 PAL B/G" },
  127. { MSP_CARRIER(6.0), "6.0 PAL I" },
  128. { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
  129. };
  130. static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
  131. /* PAL B/G */
  132. { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
  133. { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
  134. };
  135. static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
  136. /* PAL SAT / SECAM */
  137. { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
  138. { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
  139. { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
  140. { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
  141. { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
  142. { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
  143. };
  144. /* ------------------------------------------------------------------------ */
  145. const char *msp_standard_std_name(int std)
  146. {
  147. int i;
  148. for (i = 0; msp_stdlist[i].name != NULL; i++)
  149. if (msp_stdlist[i].retval == std)
  150. return msp_stdlist[i].name;
  151. return "unknown";
  152. }
  153. static v4l2_std_id msp_standard_std(int std)
  154. {
  155. int i;
  156. for (i = 0; msp_stdlist[i].name != NULL; i++)
  157. if (msp_stdlist[i].retval == std)
  158. return msp_stdlist[i].std;
  159. return V4L2_STD_ALL;
  160. }
  161. static void msp_set_source(struct i2c_client *client, u16 src)
  162. {
  163. struct msp_state *state = to_state(i2c_get_clientdata(client));
  164. if (msp_dolby) {
  165. msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
  166. msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
  167. } else {
  168. msp_write_dsp(client, 0x0008, src);
  169. msp_write_dsp(client, 0x0009, src);
  170. }
  171. msp_write_dsp(client, 0x000a, src);
  172. msp_write_dsp(client, 0x000b, src);
  173. msp_write_dsp(client, 0x000c, src);
  174. if (state->has_scart2_out)
  175. msp_write_dsp(client, 0x0041, src);
  176. }
  177. void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
  178. {
  179. msp_write_dem(client, 0x0093, cdo1 & 0xfff);
  180. msp_write_dem(client, 0x009b, cdo1 >> 12);
  181. msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
  182. msp_write_dem(client, 0x00ab, cdo2 >> 12);
  183. msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
  184. }
  185. void msp3400c_set_mode(struct i2c_client *client, int mode)
  186. {
  187. struct msp_state *state = to_state(i2c_get_clientdata(client));
  188. struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
  189. int tuner = (state->route_in >> 3) & 1;
  190. int i;
  191. dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode);
  192. state->mode = mode;
  193. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  194. msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
  195. for (i = 5; i >= 0; i--) /* fir 1 */
  196. msp_write_dem(client, 0x0001, data->fir1[i]);
  197. msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
  198. msp_write_dem(client, 0x0005, 0x0040);
  199. msp_write_dem(client, 0x0005, 0x0000);
  200. for (i = 5; i >= 0; i--)
  201. msp_write_dem(client, 0x0005, data->fir2[i]);
  202. msp_write_dem(client, 0x0083, data->mode_reg);
  203. msp3400c_set_carrier(client, data->cdo1, data->cdo2);
  204. msp_set_source(client, data->dsp_src);
  205. /* set prescales */
  206. /* volume prescale for SCART (AM mono input) */
  207. msp_write_dsp(client, 0x000d, 0x1900);
  208. msp_write_dsp(client, 0x000e, data->dsp_matrix);
  209. if (state->has_nicam) /* nicam prescale */
  210. msp_write_dsp(client, 0x0010, 0x5a00);
  211. }
  212. /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
  213. nor do they support stereo BTSC. */
  214. static void msp3400c_set_audmode(struct i2c_client *client)
  215. {
  216. static char *strmode[] = {
  217. "mono", "stereo", "lang2", "lang1", "lang1+lang2"
  218. };
  219. struct msp_state *state = to_state(i2c_get_clientdata(client));
  220. char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
  221. strmode[state->audmode] : "unknown";
  222. int src = 0; /* channel source: FM/AM, nicam or SCART */
  223. int audmode = state->audmode;
  224. if (state->opmode == OPMODE_AUTOSELECT) {
  225. /* this method would break everything, let's make sure
  226. * it's never called
  227. */
  228. dev_dbg_lvl(&client->dev, 1, msp_debug,
  229. "set_audmode called with mode=%d instead of set_source (ignored)\n",
  230. state->audmode);
  231. return;
  232. }
  233. /* Note: for the C and D revs no NTSC stereo + SAP is possible as
  234. the hardware does not support SAP. So the rxsubchans combination
  235. of STEREO | LANG2 does not occur. */
  236. if (state->mode != MSP_MODE_EXTERN) {
  237. /* switch to mono if only mono is available */
  238. if (state->rxsubchans == V4L2_TUNER_SUB_MONO)
  239. audmode = V4L2_TUNER_MODE_MONO;
  240. /* if bilingual */
  241. else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) {
  242. /* and mono or stereo, then fallback to lang1 */
  243. if (audmode == V4L2_TUNER_MODE_MONO ||
  244. audmode == V4L2_TUNER_MODE_STEREO)
  245. audmode = V4L2_TUNER_MODE_LANG1;
  246. }
  247. /* if stereo, and audmode is not mono, then switch to stereo */
  248. else if (audmode != V4L2_TUNER_MODE_MONO)
  249. audmode = V4L2_TUNER_MODE_STEREO;
  250. }
  251. /* switch demodulator */
  252. switch (state->mode) {
  253. case MSP_MODE_FM_TERRA:
  254. dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr);
  255. switch (audmode) {
  256. case V4L2_TUNER_MODE_STEREO:
  257. msp_write_dsp(client, 0x000e, 0x3001);
  258. break;
  259. case V4L2_TUNER_MODE_MONO:
  260. case V4L2_TUNER_MODE_LANG1:
  261. case V4L2_TUNER_MODE_LANG2:
  262. case V4L2_TUNER_MODE_LANG1_LANG2:
  263. msp_write_dsp(client, 0x000e, 0x3000);
  264. break;
  265. }
  266. break;
  267. case MSP_MODE_FM_SAT:
  268. dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr);
  269. switch (audmode) {
  270. case V4L2_TUNER_MODE_MONO:
  271. msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
  272. break;
  273. case V4L2_TUNER_MODE_STEREO:
  274. case V4L2_TUNER_MODE_LANG1_LANG2:
  275. msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
  276. break;
  277. case V4L2_TUNER_MODE_LANG1:
  278. msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  279. break;
  280. case V4L2_TUNER_MODE_LANG2:
  281. msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  282. break;
  283. }
  284. break;
  285. case MSP_MODE_FM_NICAM1:
  286. case MSP_MODE_FM_NICAM2:
  287. case MSP_MODE_AM_NICAM:
  288. dev_dbg_lvl(&client->dev, 1, msp_debug,
  289. "NICAM set_audmode: %s\n", modestr);
  290. if (state->nicam_on)
  291. src = 0x0100; /* NICAM */
  292. break;
  293. case MSP_MODE_BTSC:
  294. dev_dbg_lvl(&client->dev, 1, msp_debug,
  295. "BTSC set_audmode: %s\n", modestr);
  296. break;
  297. case MSP_MODE_EXTERN:
  298. dev_dbg_lvl(&client->dev, 1, msp_debug,
  299. "extern set_audmode: %s\n", modestr);
  300. src = 0x0200; /* SCART */
  301. break;
  302. case MSP_MODE_FM_RADIO:
  303. dev_dbg_lvl(&client->dev, 1, msp_debug,
  304. "FM-Radio set_audmode: %s\n", modestr);
  305. break;
  306. default:
  307. dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n");
  308. return;
  309. }
  310. /* switch audio */
  311. dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode);
  312. switch (audmode) {
  313. case V4L2_TUNER_MODE_STEREO:
  314. case V4L2_TUNER_MODE_LANG1_LANG2:
  315. src |= 0x0020;
  316. break;
  317. case V4L2_TUNER_MODE_MONO:
  318. if (state->mode == MSP_MODE_AM_NICAM) {
  319. dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n");
  320. /* AM mono decoding is handled by tuner, not MSP chip */
  321. /* SCART switching control register */
  322. msp_set_scart(client, SCART_MONO, 0);
  323. src = 0x0200;
  324. break;
  325. }
  326. if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
  327. src = 0x0030;
  328. break;
  329. case V4L2_TUNER_MODE_LANG1:
  330. break;
  331. case V4L2_TUNER_MODE_LANG2:
  332. src |= 0x0010;
  333. break;
  334. }
  335. dev_dbg_lvl(&client->dev, 1, msp_debug,
  336. "set_audmode final source/matrix = 0x%x\n", src);
  337. msp_set_source(client, src);
  338. }
  339. static void msp3400c_print_mode(struct i2c_client *client)
  340. {
  341. struct msp_state *state = to_state(i2c_get_clientdata(client));
  342. if (state->main == state->second)
  343. dev_dbg_lvl(&client->dev, 1, msp_debug,
  344. "mono sound carrier: %d.%03d MHz\n",
  345. state->main / 910000, (state->main / 910) % 1000);
  346. else
  347. dev_dbg_lvl(&client->dev, 1, msp_debug,
  348. "main sound carrier: %d.%03d MHz\n",
  349. state->main / 910000, (state->main / 910) % 1000);
  350. if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
  351. dev_dbg_lvl(&client->dev, 1, msp_debug,
  352. "NICAM/FM carrier : %d.%03d MHz\n",
  353. state->second / 910000, (state->second/910) % 1000);
  354. if (state->mode == MSP_MODE_AM_NICAM)
  355. dev_dbg_lvl(&client->dev, 1, msp_debug,
  356. "NICAM/AM carrier : %d.%03d MHz\n",
  357. state->second / 910000, (state->second / 910) % 1000);
  358. if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
  359. dev_dbg_lvl(&client->dev, 1, msp_debug,
  360. "FM-stereo carrier : %d.%03d MHz\n",
  361. state->second / 910000, (state->second / 910) % 1000);
  362. }
  363. }
  364. /* ----------------------------------------------------------------------- */
  365. static int msp3400c_detect_stereo(struct i2c_client *client)
  366. {
  367. struct msp_state *state = to_state(i2c_get_clientdata(client));
  368. int val;
  369. int rxsubchans = state->rxsubchans;
  370. int newnicam = state->nicam_on;
  371. int update = 0;
  372. switch (state->mode) {
  373. case MSP_MODE_FM_TERRA:
  374. val = msp_read_dsp(client, 0x18);
  375. if (val > 32767)
  376. val -= 65536;
  377. dev_dbg_lvl(&client->dev, 2, msp_debug,
  378. "stereo detect register: %d\n", val);
  379. if (val > 8192) {
  380. rxsubchans = V4L2_TUNER_SUB_STEREO;
  381. } else if (val < -4096) {
  382. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  383. } else {
  384. rxsubchans = V4L2_TUNER_SUB_MONO;
  385. }
  386. newnicam = 0;
  387. break;
  388. case MSP_MODE_FM_NICAM1:
  389. case MSP_MODE_FM_NICAM2:
  390. case MSP_MODE_AM_NICAM:
  391. val = msp_read_dem(client, 0x23);
  392. dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n",
  393. val & 1, (val & 0x1e) >> 1);
  394. if (val & 1) {
  395. /* nicam synced */
  396. switch ((val & 0x1e) >> 1) {
  397. case 0:
  398. case 8:
  399. rxsubchans = V4L2_TUNER_SUB_STEREO;
  400. break;
  401. case 1:
  402. case 9:
  403. rxsubchans = V4L2_TUNER_SUB_MONO;
  404. break;
  405. case 2:
  406. case 10:
  407. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  408. break;
  409. default:
  410. rxsubchans = V4L2_TUNER_SUB_MONO;
  411. break;
  412. }
  413. newnicam = 1;
  414. } else {
  415. newnicam = 0;
  416. rxsubchans = V4L2_TUNER_SUB_MONO;
  417. }
  418. break;
  419. }
  420. if (rxsubchans != state->rxsubchans) {
  421. update = 1;
  422. dev_dbg_lvl(&client->dev, 1, msp_debug,
  423. "watch: rxsubchans %02x => %02x\n",
  424. state->rxsubchans, rxsubchans);
  425. state->rxsubchans = rxsubchans;
  426. }
  427. if (newnicam != state->nicam_on) {
  428. update = 1;
  429. dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n",
  430. state->nicam_on, newnicam);
  431. state->nicam_on = newnicam;
  432. }
  433. return update;
  434. }
  435. /*
  436. * A kernel thread for msp3400 control -- we don't want to block the
  437. * in the ioctl while doing the sound carrier & stereo detect
  438. */
  439. /* stereo/multilang monitoring */
  440. static void watch_stereo(struct i2c_client *client)
  441. {
  442. struct msp_state *state = to_state(i2c_get_clientdata(client));
  443. if (msp_detect_stereo(client))
  444. msp_set_audmode(client);
  445. if (msp_once)
  446. state->watch_stereo = 0;
  447. }
  448. int msp3400c_thread(void *data)
  449. {
  450. struct i2c_client *client = data;
  451. struct msp_state *state = to_state(i2c_get_clientdata(client));
  452. struct msp3400c_carrier_detect *cd;
  453. int count, max1, max2, val1, val2, val, i;
  454. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n");
  455. state->detected_std = V4L2_STD_ALL;
  456. set_freezable();
  457. for (;;) {
  458. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n");
  459. msp_sleep(state, -1);
  460. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n");
  461. restart:
  462. dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
  463. state->restart = 0;
  464. if (kthread_should_stop())
  465. break;
  466. if (state->radio || MSP_MODE_EXTERN == state->mode) {
  467. /* no carrier scan, just unmute */
  468. dev_dbg_lvl(&client->dev, 1, msp_debug,
  469. "thread: no carrier scan\n");
  470. state->scan_in_progress = 0;
  471. msp_update_volume(state);
  472. continue;
  473. }
  474. /* mute audio */
  475. state->scan_in_progress = 1;
  476. msp_update_volume(state);
  477. msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
  478. val1 = val2 = 0;
  479. max1 = max2 = -1;
  480. state->watch_stereo = 0;
  481. state->nicam_on = 0;
  482. /* wait for tuner to settle down after a channel change */
  483. if (msp_sleep(state, 200))
  484. goto restart;
  485. /* carrier detect pass #1 -- main carrier */
  486. cd = msp3400c_carrier_detect_main;
  487. count = ARRAY_SIZE(msp3400c_carrier_detect_main);
  488. if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
  489. /* autodetect doesn't work well with AM ... */
  490. max1 = 3;
  491. count = 0;
  492. dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n");
  493. }
  494. for (i = 0; i < count; i++) {
  495. msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
  496. if (msp_sleep(state, 100))
  497. goto restart;
  498. val = msp_read_dsp(client, 0x1b);
  499. if (val > 32767)
  500. val -= 65536;
  501. if (val1 < val) {
  502. val1 = val;
  503. max1 = i;
  504. }
  505. dev_dbg_lvl(&client->dev, 1, msp_debug,
  506. "carrier1 val: %5d / %s\n", val, cd[i].name);
  507. }
  508. /* carrier detect pass #2 -- second (stereo) carrier */
  509. switch (max1) {
  510. case 1: /* 5.5 */
  511. cd = msp3400c_carrier_detect_55;
  512. count = ARRAY_SIZE(msp3400c_carrier_detect_55);
  513. break;
  514. case 3: /* 6.5 */
  515. cd = msp3400c_carrier_detect_65;
  516. count = ARRAY_SIZE(msp3400c_carrier_detect_65);
  517. break;
  518. case 0: /* 4.5 */
  519. case 2: /* 6.0 */
  520. default:
  521. cd = NULL;
  522. count = 0;
  523. break;
  524. }
  525. if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
  526. /* autodetect doesn't work well with AM ... */
  527. cd = NULL;
  528. count = 0;
  529. max2 = 0;
  530. }
  531. for (i = 0; i < count; i++) {
  532. msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
  533. if (msp_sleep(state, 100))
  534. goto restart;
  535. val = msp_read_dsp(client, 0x1b);
  536. if (val > 32767)
  537. val -= 65536;
  538. if (val2 < val) {
  539. val2 = val;
  540. max2 = i;
  541. }
  542. dev_dbg_lvl(&client->dev, 1, msp_debug,
  543. "carrier2 val: %5d / %s\n", val, cd[i].name);
  544. }
  545. /* program the msp3400 according to the results */
  546. state->main = msp3400c_carrier_detect_main[max1].cdo;
  547. switch (max1) {
  548. case 1: /* 5.5 */
  549. state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H;
  550. if (max2 == 0) {
  551. /* B/G FM-stereo */
  552. state->second = msp3400c_carrier_detect_55[max2].cdo;
  553. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  554. state->watch_stereo = 1;
  555. } else if (max2 == 1 && state->has_nicam) {
  556. /* B/G NICAM */
  557. state->second = msp3400c_carrier_detect_55[max2].cdo;
  558. msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
  559. state->nicam_on = 1;
  560. state->watch_stereo = 1;
  561. } else {
  562. goto no_second;
  563. }
  564. break;
  565. case 2: /* 6.0 */
  566. /* PAL I NICAM */
  567. state->detected_std = V4L2_STD_PAL_I;
  568. state->second = MSP_CARRIER(6.552);
  569. msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
  570. state->nicam_on = 1;
  571. state->watch_stereo = 1;
  572. break;
  573. case 3: /* 6.5 */
  574. if (max2 == 1 || max2 == 2) {
  575. /* D/K FM-stereo */
  576. state->second = msp3400c_carrier_detect_65[max2].cdo;
  577. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  578. state->watch_stereo = 1;
  579. state->detected_std = V4L2_STD_DK;
  580. } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
  581. /* L NICAM or AM-mono */
  582. state->second = msp3400c_carrier_detect_65[max2].cdo;
  583. msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
  584. state->watch_stereo = 1;
  585. state->detected_std = V4L2_STD_L;
  586. } else if (max2 == 0 && state->has_nicam) {
  587. /* D/K NICAM */
  588. state->second = msp3400c_carrier_detect_65[max2].cdo;
  589. msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
  590. state->nicam_on = 1;
  591. state->watch_stereo = 1;
  592. state->detected_std = V4L2_STD_DK;
  593. } else {
  594. goto no_second;
  595. }
  596. break;
  597. case 0: /* 4.5 */
  598. state->detected_std = V4L2_STD_MN;
  599. fallthrough;
  600. default:
  601. no_second:
  602. state->second = msp3400c_carrier_detect_main[max1].cdo;
  603. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  604. break;
  605. }
  606. msp3400c_set_carrier(client, state->second, state->main);
  607. /* unmute */
  608. state->scan_in_progress = 0;
  609. msp3400c_set_audmode(client);
  610. msp_update_volume(state);
  611. if (msp_debug)
  612. msp3400c_print_mode(client);
  613. /* monitor tv audio mode, the first time don't wait
  614. so long to get a quick stereo/bilingual result */
  615. count = 3;
  616. while (state->watch_stereo) {
  617. if (msp_sleep(state, count ? 1000 : 5000))
  618. goto restart;
  619. if (count)
  620. count--;
  621. watch_stereo(client);
  622. }
  623. }
  624. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  625. return 0;
  626. }
  627. int msp3410d_thread(void *data)
  628. {
  629. struct i2c_client *client = data;
  630. struct msp_state *state = to_state(i2c_get_clientdata(client));
  631. int val, i, std, count;
  632. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
  633. state->detected_std = V4L2_STD_ALL;
  634. set_freezable();
  635. for (;;) {
  636. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
  637. msp_sleep(state, -1);
  638. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
  639. restart:
  640. dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
  641. state->restart = 0;
  642. if (kthread_should_stop())
  643. break;
  644. if (state->mode == MSP_MODE_EXTERN) {
  645. /* no carrier scan needed, just unmute */
  646. dev_dbg_lvl(&client->dev, 1, msp_debug,
  647. "thread: no carrier scan\n");
  648. state->scan_in_progress = 0;
  649. msp_update_volume(state);
  650. continue;
  651. }
  652. /* mute audio */
  653. state->scan_in_progress = 1;
  654. msp_update_volume(state);
  655. /* start autodetect. Note: autodetect is not supported for
  656. NTSC-M and radio, hence we force the standard in those
  657. cases. */
  658. if (state->radio)
  659. std = 0x40;
  660. else
  661. std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
  662. state->watch_stereo = 0;
  663. state->nicam_on = 0;
  664. /* wait for tuner to settle down after a channel change */
  665. if (msp_sleep(state, 200))
  666. goto restart;
  667. if (msp_debug)
  668. dev_dbg_lvl(&client->dev, 2, msp_debug,
  669. "setting standard: %s (0x%04x)\n",
  670. msp_standard_std_name(std), std);
  671. if (std != 1) {
  672. /* programmed some specific mode */
  673. val = std;
  674. } else {
  675. /* triggered autodetect */
  676. msp_write_dem(client, 0x20, std);
  677. for (;;) {
  678. if (msp_sleep(state, 100))
  679. goto restart;
  680. /* check results */
  681. val = msp_read_dem(client, 0x7e);
  682. if (val < 0x07ff)
  683. break;
  684. dev_dbg_lvl(&client->dev, 2, msp_debug,
  685. "detection still in progress\n");
  686. }
  687. }
  688. for (i = 0; msp_stdlist[i].name != NULL; i++)
  689. if (msp_stdlist[i].retval == val)
  690. break;
  691. dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
  692. msp_standard_std_name(val), val);
  693. state->main = msp_stdlist[i].main;
  694. state->second = msp_stdlist[i].second;
  695. state->std = val;
  696. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  697. if (msp_amsound && !state->radio &&
  698. (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
  699. /* autodetection has failed, let backup */
  700. dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
  701. msp_stdlist[8].name ?
  702. msp_stdlist[8].name : "unknown", val);
  703. state->std = val = 0x0009;
  704. msp_write_dem(client, 0x20, val);
  705. } else {
  706. state->detected_std = msp_standard_std(state->std);
  707. }
  708. /* set stereo */
  709. switch (val) {
  710. case 0x0008: /* B/G NICAM */
  711. case 0x000a: /* I NICAM */
  712. case 0x000b: /* D/K NICAM */
  713. if (val == 0x000a)
  714. state->mode = MSP_MODE_FM_NICAM2;
  715. else
  716. state->mode = MSP_MODE_FM_NICAM1;
  717. /* just turn on stereo */
  718. state->nicam_on = 1;
  719. state->watch_stereo = 1;
  720. break;
  721. case 0x0009:
  722. state->mode = MSP_MODE_AM_NICAM;
  723. state->nicam_on = 1;
  724. state->watch_stereo = 1;
  725. break;
  726. case 0x0020: /* BTSC */
  727. /* The pre-'G' models only have BTSC-mono */
  728. state->mode = MSP_MODE_BTSC;
  729. break;
  730. case 0x0040: /* FM radio */
  731. state->mode = MSP_MODE_FM_RADIO;
  732. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  733. /* not needed in theory if we have radio, but
  734. short programming enables carrier mute */
  735. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  736. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  737. MSP_CARRIER(10.7));
  738. break;
  739. case 0x0002:
  740. case 0x0003:
  741. case 0x0004:
  742. case 0x0005:
  743. state->mode = MSP_MODE_FM_TERRA;
  744. state->watch_stereo = 1;
  745. break;
  746. }
  747. /* set various prescales */
  748. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  749. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  750. if (state->has_nicam)
  751. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  752. if (state->has_i2s_conf)
  753. msp_write_dem(client, 0x40, state->i2s_mode);
  754. /* unmute */
  755. msp3400c_set_audmode(client);
  756. state->scan_in_progress = 0;
  757. msp_update_volume(state);
  758. /* monitor tv audio mode, the first time don't wait
  759. so long to get a quick stereo/bilingual result */
  760. count = 3;
  761. while (state->watch_stereo) {
  762. if (msp_sleep(state, count ? 1000 : 5000))
  763. goto restart;
  764. if (count)
  765. count--;
  766. watch_stereo(client);
  767. }
  768. }
  769. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  770. return 0;
  771. }
  772. /* ----------------------------------------------------------------------- */
  773. /* msp34xxG + (autoselect no-thread)
  774. * this one uses both automatic standard detection and automatic sound
  775. * select which are available in the newer G versions
  776. * struct msp: only norm, acb and source are really used in this mode
  777. */
  778. static int msp34xxg_modus(struct i2c_client *client)
  779. {
  780. struct msp_state *state = to_state(i2c_get_clientdata(client));
  781. if (state->radio) {
  782. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
  783. return 0x0001;
  784. }
  785. if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
  786. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
  787. return 0x4001;
  788. }
  789. if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
  790. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
  791. return 0x0001;
  792. }
  793. if (state->v4l2_std == V4L2_STD_SECAM_L) {
  794. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
  795. return 0x6001;
  796. }
  797. if (state->v4l2_std & V4L2_STD_MN) {
  798. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
  799. return 0x2001;
  800. }
  801. return 0x7001;
  802. }
  803. static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
  804. {
  805. struct msp_state *state = to_state(i2c_get_clientdata(client));
  806. int source, matrix;
  807. switch (state->audmode) {
  808. case V4L2_TUNER_MODE_MONO:
  809. source = 0; /* mono only */
  810. matrix = 0x30;
  811. break;
  812. case V4L2_TUNER_MODE_LANG2:
  813. source = 4; /* stereo or B */
  814. matrix = 0x10;
  815. break;
  816. case V4L2_TUNER_MODE_LANG1_LANG2:
  817. source = 1; /* stereo or A|B */
  818. matrix = 0x20;
  819. break;
  820. case V4L2_TUNER_MODE_LANG1:
  821. source = 3; /* stereo or A */
  822. matrix = 0x00;
  823. break;
  824. case V4L2_TUNER_MODE_STEREO:
  825. default:
  826. source = 3; /* stereo or A */
  827. matrix = 0x20;
  828. break;
  829. }
  830. if (in == MSP_DSP_IN_TUNER)
  831. source = (source << 8) | 0x20;
  832. /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
  833. instead of 11, 12, 13. So we add one for that msp version. */
  834. else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
  835. source = ((in + 1) << 8) | matrix;
  836. else
  837. source = (in << 8) | matrix;
  838. dev_dbg_lvl(&client->dev, 1, msp_debug,
  839. "set source to %d (0x%x) for output %02x\n", in, source, reg);
  840. msp_write_dsp(client, reg, source);
  841. }
  842. static void msp34xxg_set_sources(struct i2c_client *client)
  843. {
  844. struct msp_state *state = to_state(i2c_get_clientdata(client));
  845. u32 in = state->route_in;
  846. msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
  847. /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
  848. msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
  849. msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
  850. msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
  851. if (state->has_scart2_out)
  852. msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
  853. msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
  854. }
  855. /* (re-)initialize the msp34xxg */
  856. static void msp34xxg_reset(struct i2c_client *client)
  857. {
  858. struct msp_state *state = to_state(i2c_get_clientdata(client));
  859. int tuner = (state->route_in >> 3) & 1;
  860. int modus;
  861. /* initialize std to 1 (autodetect) to signal that no standard is
  862. selected yet. */
  863. state->std = 1;
  864. msp_reset(client);
  865. if (state->has_i2s_conf)
  866. msp_write_dem(client, 0x40, state->i2s_mode);
  867. /* step-by-step initialisation, as described in the manual */
  868. modus = msp34xxg_modus(client);
  869. modus |= tuner ? 0x100 : 0;
  870. msp_write_dem(client, 0x30, modus);
  871. /* write the dsps that may have an influence on
  872. standard/audio autodetection right now */
  873. msp34xxg_set_sources(client);
  874. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  875. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  876. if (state->has_nicam)
  877. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  878. /* set identification threshold. Personally, I
  879. * I set it to a higher value than the default
  880. * of 0x190 to ignore noisy stereo signals.
  881. * this needs tuning. (recommended range 0x00a0-0x03c0)
  882. * 0x7f0 = forced mono mode
  883. *
  884. * a2 threshold for stereo/bilingual.
  885. * Note: this register is part of the Manual/Compatibility mode.
  886. * It is supported by all 'G'-family chips.
  887. */
  888. msp_write_dem(client, 0x22, msp_stereo_thresh);
  889. }
  890. int msp34xxg_thread(void *data)
  891. {
  892. struct i2c_client *client = data;
  893. struct msp_state *state = to_state(i2c_get_clientdata(client));
  894. int val, i;
  895. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
  896. state->detected_std = V4L2_STD_ALL;
  897. set_freezable();
  898. for (;;) {
  899. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
  900. msp_sleep(state, -1);
  901. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
  902. restart:
  903. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
  904. state->restart = 0;
  905. if (kthread_should_stop())
  906. break;
  907. if (state->mode == MSP_MODE_EXTERN) {
  908. /* no carrier scan needed, just unmute */
  909. dev_dbg_lvl(&client->dev, 1, msp_debug,
  910. "thread: no carrier scan\n");
  911. state->scan_in_progress = 0;
  912. msp_update_volume(state);
  913. continue;
  914. }
  915. /* setup the chip*/
  916. msp34xxg_reset(client);
  917. state->std = state->radio ? 0x40 :
  918. (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
  919. msp_write_dem(client, 0x20, state->std);
  920. /* start autodetect */
  921. if (state->std != 1)
  922. goto unmute;
  923. /* watch autodetect */
  924. dev_dbg_lvl(&client->dev, 1, msp_debug,
  925. "started autodetect, waiting for result\n");
  926. for (i = 0; i < 10; i++) {
  927. if (msp_sleep(state, 100))
  928. goto restart;
  929. /* check results */
  930. val = msp_read_dem(client, 0x7e);
  931. if (val < 0x07ff) {
  932. state->std = val;
  933. break;
  934. }
  935. dev_dbg_lvl(&client->dev, 2, msp_debug,
  936. "detection still in progress\n");
  937. }
  938. if (state->std == 1) {
  939. dev_dbg_lvl(&client->dev, 1, msp_debug,
  940. "detection still in progress after 10 tries. giving up.\n");
  941. continue;
  942. }
  943. unmute:
  944. dev_dbg_lvl(&client->dev, 1, msp_debug,
  945. "detected standard: %s (0x%04x)\n",
  946. msp_standard_std_name(state->std), state->std);
  947. state->detected_std = msp_standard_std(state->std);
  948. if (state->std == 9) {
  949. /* AM NICAM mode */
  950. msp_write_dsp(client, 0x0e, 0x7c00);
  951. }
  952. /* unmute: dispatch sound to scart output, set scart volume */
  953. msp_update_volume(state);
  954. /* restore ACB */
  955. if (msp_write_dsp(client, 0x13, state->acb))
  956. return -1;
  957. /* the periodic stereo/SAP check is only relevant for
  958. the 0x20 standard (BTSC) */
  959. if (state->std != 0x20)
  960. continue;
  961. state->watch_stereo = 1;
  962. /* monitor tv audio mode, the first time don't wait
  963. in order to get a quick stereo/SAP update */
  964. watch_stereo(client);
  965. while (state->watch_stereo) {
  966. watch_stereo(client);
  967. if (msp_sleep(state, 5000))
  968. goto restart;
  969. }
  970. }
  971. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  972. return 0;
  973. }
  974. static int msp34xxg_detect_stereo(struct i2c_client *client)
  975. {
  976. struct msp_state *state = to_state(i2c_get_clientdata(client));
  977. int status = msp_read_dem(client, 0x0200);
  978. int is_bilingual = status & 0x100;
  979. int is_stereo = status & 0x40;
  980. int oldrx = state->rxsubchans;
  981. if (state->mode == MSP_MODE_EXTERN)
  982. return 0;
  983. state->rxsubchans = 0;
  984. if (is_stereo)
  985. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  986. else
  987. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  988. if (is_bilingual) {
  989. if (state->std == 0x20)
  990. state->rxsubchans |= V4L2_TUNER_SUB_SAP;
  991. else
  992. state->rxsubchans =
  993. V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  994. }
  995. dev_dbg_lvl(&client->dev, 1, msp_debug,
  996. "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  997. status, is_stereo, is_bilingual, state->rxsubchans);
  998. return (oldrx != state->rxsubchans);
  999. }
  1000. static void msp34xxg_set_audmode(struct i2c_client *client)
  1001. {
  1002. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1003. if (state->std == 0x20) {
  1004. if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
  1005. (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
  1006. state->audmode == V4L2_TUNER_MODE_LANG2)) {
  1007. msp_write_dem(client, 0x20, 0x21);
  1008. } else {
  1009. msp_write_dem(client, 0x20, 0x20);
  1010. }
  1011. }
  1012. msp34xxg_set_sources(client);
  1013. }
  1014. void msp_set_audmode(struct i2c_client *client)
  1015. {
  1016. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1017. switch (state->opmode) {
  1018. case OPMODE_MANUAL:
  1019. case OPMODE_AUTODETECT:
  1020. msp3400c_set_audmode(client);
  1021. break;
  1022. case OPMODE_AUTOSELECT:
  1023. msp34xxg_set_audmode(client);
  1024. break;
  1025. }
  1026. }
  1027. int msp_detect_stereo(struct i2c_client *client)
  1028. {
  1029. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1030. switch (state->opmode) {
  1031. case OPMODE_MANUAL:
  1032. case OPMODE_AUTODETECT:
  1033. return msp3400c_detect_stereo(client);
  1034. case OPMODE_AUTOSELECT:
  1035. return msp34xxg_detect_stereo(client);
  1036. }
  1037. return 0;
  1038. }