msp3400-kthreads.c 33 KB

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