msp3400-kthreads.c 31 KB

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