msp3400-kthreads.c 29 KB

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