msp3400-kthreads.c 29 KB

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