msp3400-kthreads.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. default:
  605. no_second:
  606. state->second = msp3400c_carrier_detect_main[max1].cdo;
  607. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  608. break;
  609. }
  610. msp3400c_set_carrier(client, state->second, state->main);
  611. /* unmute */
  612. state->scan_in_progress = 0;
  613. msp3400c_set_audmode(client);
  614. msp_update_volume(state);
  615. if (msp_debug)
  616. msp3400c_print_mode(client);
  617. /* monitor tv audio mode, the first time don't wait
  618. so long to get a quick stereo/bilingual result */
  619. count = 3;
  620. while (state->watch_stereo) {
  621. if (msp_sleep(state, count ? 1000 : 5000))
  622. goto restart;
  623. if (count)
  624. count--;
  625. watch_stereo(client);
  626. }
  627. }
  628. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  629. return 0;
  630. }
  631. int msp3410d_thread(void *data)
  632. {
  633. struct i2c_client *client = data;
  634. struct msp_state *state = to_state(i2c_get_clientdata(client));
  635. int val, i, std, count;
  636. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
  637. state->detected_std = V4L2_STD_ALL;
  638. set_freezable();
  639. for (;;) {
  640. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
  641. msp_sleep(state, -1);
  642. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
  643. restart:
  644. dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
  645. state->restart = 0;
  646. if (kthread_should_stop())
  647. break;
  648. if (state->mode == MSP_MODE_EXTERN) {
  649. /* no carrier scan needed, just unmute */
  650. dev_dbg_lvl(&client->dev, 1, msp_debug,
  651. "thread: no carrier scan\n");
  652. state->scan_in_progress = 0;
  653. msp_update_volume(state);
  654. continue;
  655. }
  656. /* mute audio */
  657. state->scan_in_progress = 1;
  658. msp_update_volume(state);
  659. /* start autodetect. Note: autodetect is not supported for
  660. NTSC-M and radio, hence we force the standard in those
  661. cases. */
  662. if (state->radio)
  663. std = 0x40;
  664. else
  665. std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
  666. state->watch_stereo = 0;
  667. state->nicam_on = 0;
  668. /* wait for tuner to settle down after a channel change */
  669. if (msp_sleep(state, 200))
  670. goto restart;
  671. if (msp_debug)
  672. dev_dbg_lvl(&client->dev, 2, msp_debug,
  673. "setting standard: %s (0x%04x)\n",
  674. msp_standard_std_name(std), std);
  675. if (std != 1) {
  676. /* programmed some specific mode */
  677. val = std;
  678. } else {
  679. /* triggered autodetect */
  680. msp_write_dem(client, 0x20, std);
  681. for (;;) {
  682. if (msp_sleep(state, 100))
  683. goto restart;
  684. /* check results */
  685. val = msp_read_dem(client, 0x7e);
  686. if (val < 0x07ff)
  687. break;
  688. dev_dbg_lvl(&client->dev, 2, msp_debug,
  689. "detection still in progress\n");
  690. }
  691. }
  692. for (i = 0; msp_stdlist[i].name != NULL; i++)
  693. if (msp_stdlist[i].retval == val)
  694. break;
  695. dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
  696. msp_standard_std_name(val), val);
  697. state->main = msp_stdlist[i].main;
  698. state->second = msp_stdlist[i].second;
  699. state->std = val;
  700. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  701. if (msp_amsound && !state->radio &&
  702. (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
  703. /* autodetection has failed, let backup */
  704. dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
  705. msp_stdlist[8].name ?
  706. msp_stdlist[8].name : "unknown", val);
  707. state->std = val = 0x0009;
  708. msp_write_dem(client, 0x20, val);
  709. } else {
  710. state->detected_std = msp_standard_std(state->std);
  711. }
  712. /* set stereo */
  713. switch (val) {
  714. case 0x0008: /* B/G NICAM */
  715. case 0x000a: /* I NICAM */
  716. case 0x000b: /* D/K NICAM */
  717. if (val == 0x000a)
  718. state->mode = MSP_MODE_FM_NICAM2;
  719. else
  720. state->mode = MSP_MODE_FM_NICAM1;
  721. /* just turn on stereo */
  722. state->nicam_on = 1;
  723. state->watch_stereo = 1;
  724. break;
  725. case 0x0009:
  726. state->mode = MSP_MODE_AM_NICAM;
  727. state->nicam_on = 1;
  728. state->watch_stereo = 1;
  729. break;
  730. case 0x0020: /* BTSC */
  731. /* The pre-'G' models only have BTSC-mono */
  732. state->mode = MSP_MODE_BTSC;
  733. break;
  734. case 0x0040: /* FM radio */
  735. state->mode = MSP_MODE_FM_RADIO;
  736. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  737. /* not needed in theory if we have radio, but
  738. short programming enables carrier mute */
  739. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  740. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  741. MSP_CARRIER(10.7));
  742. break;
  743. case 0x0002:
  744. case 0x0003:
  745. case 0x0004:
  746. case 0x0005:
  747. state->mode = MSP_MODE_FM_TERRA;
  748. state->watch_stereo = 1;
  749. break;
  750. }
  751. /* set various prescales */
  752. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  753. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  754. if (state->has_nicam)
  755. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  756. if (state->has_i2s_conf)
  757. msp_write_dem(client, 0x40, state->i2s_mode);
  758. /* unmute */
  759. msp3400c_set_audmode(client);
  760. state->scan_in_progress = 0;
  761. msp_update_volume(state);
  762. /* monitor tv audio mode, the first time don't wait
  763. so long to get a quick stereo/bilingual result */
  764. count = 3;
  765. while (state->watch_stereo) {
  766. if (msp_sleep(state, count ? 1000 : 5000))
  767. goto restart;
  768. if (count)
  769. count--;
  770. watch_stereo(client);
  771. }
  772. }
  773. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  774. return 0;
  775. }
  776. /* ----------------------------------------------------------------------- */
  777. /* msp34xxG + (autoselect no-thread)
  778. * this one uses both automatic standard detection and automatic sound
  779. * select which are available in the newer G versions
  780. * struct msp: only norm, acb and source are really used in this mode
  781. */
  782. static int msp34xxg_modus(struct i2c_client *client)
  783. {
  784. struct msp_state *state = to_state(i2c_get_clientdata(client));
  785. if (state->radio) {
  786. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
  787. return 0x0001;
  788. }
  789. if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
  790. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
  791. return 0x4001;
  792. }
  793. if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
  794. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
  795. return 0x0001;
  796. }
  797. if (state->v4l2_std == V4L2_STD_SECAM_L) {
  798. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
  799. return 0x6001;
  800. }
  801. if (state->v4l2_std & V4L2_STD_MN) {
  802. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
  803. return 0x2001;
  804. }
  805. return 0x7001;
  806. }
  807. static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
  808. {
  809. struct msp_state *state = to_state(i2c_get_clientdata(client));
  810. int source, matrix;
  811. switch (state->audmode) {
  812. case V4L2_TUNER_MODE_MONO:
  813. source = 0; /* mono only */
  814. matrix = 0x30;
  815. break;
  816. case V4L2_TUNER_MODE_LANG2:
  817. source = 4; /* stereo or B */
  818. matrix = 0x10;
  819. break;
  820. case V4L2_TUNER_MODE_LANG1_LANG2:
  821. source = 1; /* stereo or A|B */
  822. matrix = 0x20;
  823. break;
  824. case V4L2_TUNER_MODE_LANG1:
  825. source = 3; /* stereo or A */
  826. matrix = 0x00;
  827. break;
  828. case V4L2_TUNER_MODE_STEREO:
  829. default:
  830. source = 3; /* stereo or A */
  831. matrix = 0x20;
  832. break;
  833. }
  834. if (in == MSP_DSP_IN_TUNER)
  835. source = (source << 8) | 0x20;
  836. /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
  837. instead of 11, 12, 13. So we add one for that msp version. */
  838. else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
  839. source = ((in + 1) << 8) | matrix;
  840. else
  841. source = (in << 8) | matrix;
  842. dev_dbg_lvl(&client->dev, 1, msp_debug,
  843. "set source to %d (0x%x) for output %02x\n", in, source, reg);
  844. msp_write_dsp(client, reg, source);
  845. }
  846. static void msp34xxg_set_sources(struct i2c_client *client)
  847. {
  848. struct msp_state *state = to_state(i2c_get_clientdata(client));
  849. u32 in = state->route_in;
  850. msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
  851. /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
  852. msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
  853. msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
  854. msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
  855. if (state->has_scart2_out)
  856. msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
  857. msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
  858. }
  859. /* (re-)initialize the msp34xxg */
  860. static void msp34xxg_reset(struct i2c_client *client)
  861. {
  862. struct msp_state *state = to_state(i2c_get_clientdata(client));
  863. int tuner = (state->route_in >> 3) & 1;
  864. int modus;
  865. /* initialize std to 1 (autodetect) to signal that no standard is
  866. selected yet. */
  867. state->std = 1;
  868. msp_reset(client);
  869. if (state->has_i2s_conf)
  870. msp_write_dem(client, 0x40, state->i2s_mode);
  871. /* step-by-step initialisation, as described in the manual */
  872. modus = msp34xxg_modus(client);
  873. modus |= tuner ? 0x100 : 0;
  874. msp_write_dem(client, 0x30, modus);
  875. /* write the dsps that may have an influence on
  876. standard/audio autodetection right now */
  877. msp34xxg_set_sources(client);
  878. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  879. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  880. if (state->has_nicam)
  881. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  882. /* set identification threshold. Personally, I
  883. * I set it to a higher value than the default
  884. * of 0x190 to ignore noisy stereo signals.
  885. * this needs tuning. (recommended range 0x00a0-0x03c0)
  886. * 0x7f0 = forced mono mode
  887. *
  888. * a2 threshold for stereo/bilingual.
  889. * Note: this register is part of the Manual/Compatibility mode.
  890. * It is supported by all 'G'-family chips.
  891. */
  892. msp_write_dem(client, 0x22, msp_stereo_thresh);
  893. }
  894. int msp34xxg_thread(void *data)
  895. {
  896. struct i2c_client *client = data;
  897. struct msp_state *state = to_state(i2c_get_clientdata(client));
  898. int val, i;
  899. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
  900. state->detected_std = V4L2_STD_ALL;
  901. set_freezable();
  902. for (;;) {
  903. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
  904. msp_sleep(state, -1);
  905. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
  906. restart:
  907. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
  908. state->restart = 0;
  909. if (kthread_should_stop())
  910. break;
  911. if (state->mode == MSP_MODE_EXTERN) {
  912. /* no carrier scan needed, just unmute */
  913. dev_dbg_lvl(&client->dev, 1, msp_debug,
  914. "thread: no carrier scan\n");
  915. state->scan_in_progress = 0;
  916. msp_update_volume(state);
  917. continue;
  918. }
  919. /* setup the chip*/
  920. msp34xxg_reset(client);
  921. state->std = state->radio ? 0x40 :
  922. (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
  923. msp_write_dem(client, 0x20, state->std);
  924. /* start autodetect */
  925. if (state->std != 1)
  926. goto unmute;
  927. /* watch autodetect */
  928. dev_dbg_lvl(&client->dev, 1, msp_debug,
  929. "started autodetect, waiting for result\n");
  930. for (i = 0; i < 10; i++) {
  931. if (msp_sleep(state, 100))
  932. goto restart;
  933. /* check results */
  934. val = msp_read_dem(client, 0x7e);
  935. if (val < 0x07ff) {
  936. state->std = val;
  937. break;
  938. }
  939. dev_dbg_lvl(&client->dev, 2, msp_debug,
  940. "detection still in progress\n");
  941. }
  942. if (state->std == 1) {
  943. dev_dbg_lvl(&client->dev, 1, msp_debug,
  944. "detection still in progress after 10 tries. giving up.\n");
  945. continue;
  946. }
  947. unmute:
  948. dev_dbg_lvl(&client->dev, 1, msp_debug,
  949. "detected standard: %s (0x%04x)\n",
  950. msp_standard_std_name(state->std), state->std);
  951. state->detected_std = msp_standard_std(state->std);
  952. if (state->std == 9) {
  953. /* AM NICAM mode */
  954. msp_write_dsp(client, 0x0e, 0x7c00);
  955. }
  956. /* unmute: dispatch sound to scart output, set scart volume */
  957. msp_update_volume(state);
  958. /* restore ACB */
  959. if (msp_write_dsp(client, 0x13, state->acb))
  960. return -1;
  961. /* the periodic stereo/SAP check is only relevant for
  962. the 0x20 standard (BTSC) */
  963. if (state->std != 0x20)
  964. continue;
  965. state->watch_stereo = 1;
  966. /* monitor tv audio mode, the first time don't wait
  967. in order to get a quick stereo/SAP update */
  968. watch_stereo(client);
  969. while (state->watch_stereo) {
  970. watch_stereo(client);
  971. if (msp_sleep(state, 5000))
  972. goto restart;
  973. }
  974. }
  975. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  976. return 0;
  977. }
  978. static int msp34xxg_detect_stereo(struct i2c_client *client)
  979. {
  980. struct msp_state *state = to_state(i2c_get_clientdata(client));
  981. int status = msp_read_dem(client, 0x0200);
  982. int is_bilingual = status & 0x100;
  983. int is_stereo = status & 0x40;
  984. int oldrx = state->rxsubchans;
  985. if (state->mode == MSP_MODE_EXTERN)
  986. return 0;
  987. state->rxsubchans = 0;
  988. if (is_stereo)
  989. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  990. else
  991. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  992. if (is_bilingual) {
  993. if (state->std == 0x20)
  994. state->rxsubchans |= V4L2_TUNER_SUB_SAP;
  995. else
  996. state->rxsubchans =
  997. V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  998. }
  999. dev_dbg_lvl(&client->dev, 1, msp_debug,
  1000. "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  1001. status, is_stereo, is_bilingual, state->rxsubchans);
  1002. return (oldrx != state->rxsubchans);
  1003. }
  1004. static void msp34xxg_set_audmode(struct i2c_client *client)
  1005. {
  1006. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1007. if (state->std == 0x20) {
  1008. if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
  1009. (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
  1010. state->audmode == V4L2_TUNER_MODE_LANG2)) {
  1011. msp_write_dem(client, 0x20, 0x21);
  1012. } else {
  1013. msp_write_dem(client, 0x20, 0x20);
  1014. }
  1015. }
  1016. msp34xxg_set_sources(client);
  1017. }
  1018. void msp_set_audmode(struct i2c_client *client)
  1019. {
  1020. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1021. switch (state->opmode) {
  1022. case OPMODE_MANUAL:
  1023. case OPMODE_AUTODETECT:
  1024. msp3400c_set_audmode(client);
  1025. break;
  1026. case OPMODE_AUTOSELECT:
  1027. msp34xxg_set_audmode(client);
  1028. break;
  1029. }
  1030. }
  1031. int msp_detect_stereo(struct i2c_client *client)
  1032. {
  1033. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1034. switch (state->opmode) {
  1035. case OPMODE_MANUAL:
  1036. case OPMODE_AUTODETECT:
  1037. return msp3400c_detect_stereo(client);
  1038. case OPMODE_AUTOSELECT:
  1039. return msp34xxg_detect_stereo(client);
  1040. }
  1041. return 0;
  1042. }