msp3400-kthreads.c 33 KB

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