msp3400-kthreads.c 30 KB

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