msp3400-kthreads.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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/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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  303. "NICAM set_audmode: %s\n", modestr);
  304. if (state->nicam_on)
  305. src = 0x0100; /* NICAM */
  306. break;
  307. case MSP_MODE_BTSC:
  308. v4l_dbg(1, msp_debug, client,
  309. "BTSC set_audmode: %s\n", modestr);
  310. break;
  311. case MSP_MODE_EXTERN:
  312. v4l_dbg(1, msp_debug, client,
  313. "extern set_audmode: %s\n", modestr);
  314. src = 0x0200; /* SCART */
  315. break;
  316. case MSP_MODE_FM_RADIO:
  317. v4l_dbg(1, msp_debug, client,
  318. "FM-Radio set_audmode: %s\n", modestr);
  319. break;
  320. default:
  321. v4l_dbg(1, msp_debug, client, "mono set_audmode\n");
  322. return;
  323. }
  324. /* switch audio */
  325. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client,
  358. "mono sound carrier: %d.%03d MHz\n",
  359. state->main / 910000, (state->main / 910) % 1000);
  360. else
  361. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(2, msp_debug, client,
  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. v4l_dbg(2, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
  469. state->detected_std = V4L2_STD_ALL;
  470. set_freezable();
  471. for (;;) {
  472. v4l_dbg(2, msp_debug, client, "msp3400 thread: sleep\n");
  473. msp_sleep(state, -1);
  474. v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
  475. restart:
  476. v4l_dbg(2, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client, "msp3410 daemon started\n");
  642. state->detected_std = V4L2_STD_ALL;
  643. set_freezable();
  644. for (;;) {
  645. v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
  646. msp_sleep(state, -1);
  647. v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
  648. restart:
  649. v4l_dbg(2, msp_debug, client, "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. v4l_dbg(1, msp_debug, client,
  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. v4l_dbg(2, msp_debug, client,
  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. v4l_dbg(2, msp_debug, client,
  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. v4l_dbg(1, msp_debug, client, "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. v4l_dbg(1, msp_debug, client, "autodetection failed,"
  710. " switching to backup standard: %s (0x%04x)\n",
  711. msp_stdlist[8].name ?
  712. msp_stdlist[8].name : "unknown", val);
  713. state->std = val = 0x0009;
  714. msp_write_dem(client, 0x20, val);
  715. } else {
  716. state->detected_std = msp_standard_std(state->std);
  717. }
  718. /* set stereo */
  719. switch (val) {
  720. case 0x0008: /* B/G NICAM */
  721. case 0x000a: /* I NICAM */
  722. case 0x000b: /* D/K NICAM */
  723. if (val == 0x000a)
  724. state->mode = MSP_MODE_FM_NICAM2;
  725. else
  726. state->mode = MSP_MODE_FM_NICAM1;
  727. /* just turn on stereo */
  728. state->nicam_on = 1;
  729. state->watch_stereo = 1;
  730. break;
  731. case 0x0009:
  732. state->mode = MSP_MODE_AM_NICAM;
  733. state->nicam_on = 1;
  734. state->watch_stereo = 1;
  735. break;
  736. case 0x0020: /* BTSC */
  737. /* The pre-'G' models only have BTSC-mono */
  738. state->mode = MSP_MODE_BTSC;
  739. break;
  740. case 0x0040: /* FM radio */
  741. state->mode = MSP_MODE_FM_RADIO;
  742. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  743. /* not needed in theory if we have radio, but
  744. short programming enables carrier mute */
  745. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  746. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  747. MSP_CARRIER(10.7));
  748. break;
  749. case 0x0002:
  750. case 0x0003:
  751. case 0x0004:
  752. case 0x0005:
  753. state->mode = MSP_MODE_FM_TERRA;
  754. state->watch_stereo = 1;
  755. break;
  756. }
  757. /* set various prescales */
  758. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  759. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  760. if (state->has_nicam)
  761. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  762. if (state->has_i2s_conf)
  763. msp_write_dem(client, 0x40, state->i2s_mode);
  764. /* unmute */
  765. msp3400c_set_audmode(client);
  766. state->scan_in_progress = 0;
  767. msp_update_volume(state);
  768. /* monitor tv audio mode, the first time don't wait
  769. so long to get a quick stereo/bilingual result */
  770. count = 3;
  771. while (state->watch_stereo) {
  772. if (msp_sleep(state, count ? 1000 : 5000))
  773. goto restart;
  774. if (count)
  775. count--;
  776. watch_stereo(client);
  777. }
  778. }
  779. v4l_dbg(1, msp_debug, client, "thread: exit\n");
  780. return 0;
  781. }
  782. /* ----------------------------------------------------------------------- */
  783. /* msp34xxG + (autoselect no-thread)
  784. * this one uses both automatic standard detection and automatic sound
  785. * select which are available in the newer G versions
  786. * struct msp: only norm, acb and source are really used in this mode
  787. */
  788. static int msp34xxg_modus(struct i2c_client *client)
  789. {
  790. struct msp_state *state = to_state(i2c_get_clientdata(client));
  791. if (state->radio) {
  792. v4l_dbg(1, msp_debug, client, "selected radio modus\n");
  793. return 0x0001;
  794. }
  795. if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
  796. v4l_dbg(1, msp_debug, client, "selected M (EIA-J) modus\n");
  797. return 0x4001;
  798. }
  799. if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
  800. v4l_dbg(1, msp_debug, client, "selected M (A2) modus\n");
  801. return 0x0001;
  802. }
  803. if (state->v4l2_std == V4L2_STD_SECAM_L) {
  804. v4l_dbg(1, msp_debug, client, "selected SECAM-L modus\n");
  805. return 0x6001;
  806. }
  807. if (state->v4l2_std & V4L2_STD_MN) {
  808. v4l_dbg(1, msp_debug, client, "selected M (BTSC) modus\n");
  809. return 0x2001;
  810. }
  811. return 0x7001;
  812. }
  813. static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
  814. {
  815. struct msp_state *state = to_state(i2c_get_clientdata(client));
  816. int source, matrix;
  817. switch (state->audmode) {
  818. case V4L2_TUNER_MODE_MONO:
  819. source = 0; /* mono only */
  820. matrix = 0x30;
  821. break;
  822. case V4L2_TUNER_MODE_LANG2:
  823. source = 4; /* stereo or B */
  824. matrix = 0x10;
  825. break;
  826. case V4L2_TUNER_MODE_LANG1_LANG2:
  827. source = 1; /* stereo or A|B */
  828. matrix = 0x20;
  829. break;
  830. case V4L2_TUNER_MODE_LANG1:
  831. source = 3; /* stereo or A */
  832. matrix = 0x00;
  833. break;
  834. case V4L2_TUNER_MODE_STEREO:
  835. default:
  836. source = 3; /* stereo or A */
  837. matrix = 0x20;
  838. break;
  839. }
  840. if (in == MSP_DSP_IN_TUNER)
  841. source = (source << 8) | 0x20;
  842. /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
  843. instead of 11, 12, 13. So we add one for that msp version. */
  844. else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
  845. source = ((in + 1) << 8) | matrix;
  846. else
  847. source = (in << 8) | matrix;
  848. v4l_dbg(1, msp_debug, client,
  849. "set source to %d (0x%x) for output %02x\n", in, source, reg);
  850. msp_write_dsp(client, reg, source);
  851. }
  852. static void msp34xxg_set_sources(struct i2c_client *client)
  853. {
  854. struct msp_state *state = to_state(i2c_get_clientdata(client));
  855. u32 in = state->route_in;
  856. msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
  857. /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
  858. msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
  859. msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
  860. msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
  861. if (state->has_scart2_out)
  862. msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
  863. msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
  864. }
  865. /* (re-)initialize the msp34xxg */
  866. static void msp34xxg_reset(struct i2c_client *client)
  867. {
  868. struct msp_state *state = to_state(i2c_get_clientdata(client));
  869. int tuner = (state->route_in >> 3) & 1;
  870. int modus;
  871. /* initialize std to 1 (autodetect) to signal that no standard is
  872. selected yet. */
  873. state->std = 1;
  874. msp_reset(client);
  875. if (state->has_i2s_conf)
  876. msp_write_dem(client, 0x40, state->i2s_mode);
  877. /* step-by-step initialisation, as described in the manual */
  878. modus = msp34xxg_modus(client);
  879. modus |= tuner ? 0x100 : 0;
  880. msp_write_dem(client, 0x30, modus);
  881. /* write the dsps that may have an influence on
  882. standard/audio autodetection right now */
  883. msp34xxg_set_sources(client);
  884. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  885. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  886. if (state->has_nicam)
  887. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  888. /* set identification threshold. Personally, I
  889. * I set it to a higher value than the default
  890. * of 0x190 to ignore noisy stereo signals.
  891. * this needs tuning. (recommended range 0x00a0-0x03c0)
  892. * 0x7f0 = forced mono mode
  893. *
  894. * a2 threshold for stereo/bilingual.
  895. * Note: this register is part of the Manual/Compatibility mode.
  896. * It is supported by all 'G'-family chips.
  897. */
  898. msp_write_dem(client, 0x22, msp_stereo_thresh);
  899. }
  900. int msp34xxg_thread(void *data)
  901. {
  902. struct i2c_client *client = data;
  903. struct msp_state *state = to_state(i2c_get_clientdata(client));
  904. int val, i;
  905. v4l_dbg(1, msp_debug, client, "msp34xxg daemon started\n");
  906. state->detected_std = V4L2_STD_ALL;
  907. set_freezable();
  908. for (;;) {
  909. v4l_dbg(2, msp_debug, client, "msp34xxg thread: sleep\n");
  910. msp_sleep(state, -1);
  911. v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
  912. restart:
  913. v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
  914. state->restart = 0;
  915. if (kthread_should_stop())
  916. break;
  917. if (state->mode == MSP_MODE_EXTERN) {
  918. /* no carrier scan needed, just unmute */
  919. v4l_dbg(1, msp_debug, client,
  920. "thread: no carrier scan\n");
  921. state->scan_in_progress = 0;
  922. msp_update_volume(state);
  923. continue;
  924. }
  925. /* setup the chip*/
  926. msp34xxg_reset(client);
  927. state->std = state->radio ? 0x40 :
  928. (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
  929. msp_write_dem(client, 0x20, state->std);
  930. /* start autodetect */
  931. if (state->std != 1)
  932. goto unmute;
  933. /* watch autodetect */
  934. v4l_dbg(1, msp_debug, client,
  935. "started autodetect, waiting for result\n");
  936. for (i = 0; i < 10; i++) {
  937. if (msp_sleep(state, 100))
  938. goto restart;
  939. /* check results */
  940. val = msp_read_dem(client, 0x7e);
  941. if (val < 0x07ff) {
  942. state->std = val;
  943. break;
  944. }
  945. v4l_dbg(2, msp_debug, client,
  946. "detection still in progress\n");
  947. }
  948. if (state->std == 1) {
  949. v4l_dbg(1, msp_debug, client,
  950. "detection still in progress after 10 tries. giving up.\n");
  951. continue;
  952. }
  953. unmute:
  954. v4l_dbg(1, msp_debug, client,
  955. "detected standard: %s (0x%04x)\n",
  956. msp_standard_std_name(state->std), state->std);
  957. state->detected_std = msp_standard_std(state->std);
  958. if (state->std == 9) {
  959. /* AM NICAM mode */
  960. msp_write_dsp(client, 0x0e, 0x7c00);
  961. }
  962. /* unmute: dispatch sound to scart output, set scart volume */
  963. msp_update_volume(state);
  964. /* restore ACB */
  965. if (msp_write_dsp(client, 0x13, state->acb))
  966. return -1;
  967. /* the periodic stereo/SAP check is only relevant for
  968. the 0x20 standard (BTSC) */
  969. if (state->std != 0x20)
  970. continue;
  971. state->watch_stereo = 1;
  972. /* monitor tv audio mode, the first time don't wait
  973. in order to get a quick stereo/SAP update */
  974. watch_stereo(client);
  975. while (state->watch_stereo) {
  976. watch_stereo(client);
  977. if (msp_sleep(state, 5000))
  978. goto restart;
  979. }
  980. }
  981. v4l_dbg(1, msp_debug, client, "thread: exit\n");
  982. return 0;
  983. }
  984. static int msp34xxg_detect_stereo(struct i2c_client *client)
  985. {
  986. struct msp_state *state = to_state(i2c_get_clientdata(client));
  987. int status = msp_read_dem(client, 0x0200);
  988. int is_bilingual = status & 0x100;
  989. int is_stereo = status & 0x40;
  990. int oldrx = state->rxsubchans;
  991. if (state->mode == MSP_MODE_EXTERN)
  992. return 0;
  993. state->rxsubchans = 0;
  994. if (is_stereo)
  995. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  996. else
  997. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  998. if (is_bilingual) {
  999. if (state->std == 0x20)
  1000. state->rxsubchans |= V4L2_TUNER_SUB_SAP;
  1001. else
  1002. state->rxsubchans =
  1003. V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  1004. }
  1005. v4l_dbg(1, msp_debug, client,
  1006. "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  1007. status, is_stereo, is_bilingual, state->rxsubchans);
  1008. return (oldrx != state->rxsubchans);
  1009. }
  1010. static void msp34xxg_set_audmode(struct i2c_client *client)
  1011. {
  1012. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1013. if (state->std == 0x20) {
  1014. if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
  1015. (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
  1016. state->audmode == V4L2_TUNER_MODE_LANG2)) {
  1017. msp_write_dem(client, 0x20, 0x21);
  1018. } else {
  1019. msp_write_dem(client, 0x20, 0x20);
  1020. }
  1021. }
  1022. msp34xxg_set_sources(client);
  1023. }
  1024. void msp_set_audmode(struct i2c_client *client)
  1025. {
  1026. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1027. switch (state->opmode) {
  1028. case OPMODE_MANUAL:
  1029. case OPMODE_AUTODETECT:
  1030. msp3400c_set_audmode(client);
  1031. break;
  1032. case OPMODE_AUTOSELECT:
  1033. msp34xxg_set_audmode(client);
  1034. break;
  1035. }
  1036. }
  1037. int msp_detect_stereo(struct i2c_client *client)
  1038. {
  1039. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1040. switch (state->opmode) {
  1041. case OPMODE_MANUAL:
  1042. case OPMODE_AUTODETECT:
  1043. return msp3400c_detect_stereo(client);
  1044. case OPMODE_AUTOSELECT:
  1045. return msp34xxg_detect_stereo(client);
  1046. }
  1047. return 0;
  1048. }