msp3400-kthreads.c 31 KB

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