msp3400-kthreads.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 (state->has_nicam) {
  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 (state->radio || MSP_MODE_EXTERN == state->mode) {
  449. /* no carrier scan, just unmute */
  450. v4l_dbg(1, client, "thread: no carrier scan\n");
  451. msp_set_audio(client);
  452. continue;
  453. }
  454. /* mute */
  455. msp_set_mute(client);
  456. msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
  457. val1 = val2 = 0;
  458. max1 = max2 = -1;
  459. state->watch_stereo = 0;
  460. /* some time for the tuner to sync */
  461. if (msp_sleep(state,200))
  462. goto restart;
  463. /* carrier detect pass #1 -- main carrier */
  464. cd = msp3400c_carrier_detect_main;
  465. count = ARRAY_SIZE(msp3400c_carrier_detect_main);
  466. if (amsound && (state->std & V4L2_STD_SECAM)) {
  467. /* autodetect doesn't work well with AM ... */
  468. max1 = 3;
  469. count = 0;
  470. v4l_dbg(1, client, "AM sound override\n");
  471. }
  472. for (this = 0; this < count; this++) {
  473. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  474. if (msp_sleep(state,100))
  475. goto restart;
  476. val = msp_read_dsp(client, 0x1b);
  477. if (val > 32767)
  478. val -= 65536;
  479. if (val1 < val)
  480. val1 = val, max1 = this;
  481. v4l_dbg(1, client, "carrier1 val: %5d / %s\n", val,cd[this].name);
  482. }
  483. /* carrier detect pass #2 -- second (stereo) carrier */
  484. switch (max1) {
  485. case 1: /* 5.5 */
  486. cd = msp3400c_carrier_detect_55;
  487. count = ARRAY_SIZE(msp3400c_carrier_detect_55);
  488. break;
  489. case 3: /* 6.5 */
  490. cd = msp3400c_carrier_detect_65;
  491. count = ARRAY_SIZE(msp3400c_carrier_detect_65);
  492. break;
  493. case 0: /* 4.5 */
  494. case 2: /* 6.0 */
  495. default:
  496. cd = NULL;
  497. count = 0;
  498. break;
  499. }
  500. if (amsound && (state->std & V4L2_STD_SECAM)) {
  501. /* autodetect doesn't work well with AM ... */
  502. cd = NULL;
  503. count = 0;
  504. max2 = 0;
  505. }
  506. for (this = 0; this < count; this++) {
  507. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  508. if (msp_sleep(state,100))
  509. goto restart;
  510. val = msp_read_dsp(client, 0x1b);
  511. if (val > 32767)
  512. val -= 65536;
  513. if (val2 < val)
  514. val2 = val, max2 = this;
  515. v4l_dbg(1, client, "carrier2 val: %5d / %s\n", val,cd[this].name);
  516. }
  517. /* program the msp3400 according to the results */
  518. state->main = msp3400c_carrier_detect_main[max1].cdo;
  519. switch (max1) {
  520. case 1: /* 5.5 */
  521. if (max2 == 0) {
  522. /* B/G FM-stereo */
  523. state->second = msp3400c_carrier_detect_55[max2].cdo;
  524. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  525. state->nicam_on = 0;
  526. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  527. state->watch_stereo = 1;
  528. } else if (max2 == 1 && state->has_nicam) {
  529. /* B/G NICAM */
  530. state->second = msp3400c_carrier_detect_55[max2].cdo;
  531. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  532. state->nicam_on = 1;
  533. msp3400c_setcarrier(client, state->second, state->main);
  534. state->watch_stereo = 1;
  535. } else {
  536. goto no_second;
  537. }
  538. break;
  539. case 2: /* 6.0 */
  540. /* PAL I NICAM */
  541. state->second = MSP_CARRIER(6.552);
  542. msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
  543. state->nicam_on = 1;
  544. msp3400c_setcarrier(client, state->second, state->main);
  545. state->watch_stereo = 1;
  546. break;
  547. case 3: /* 6.5 */
  548. if (max2 == 1 || max2 == 2) {
  549. /* D/K FM-stereo */
  550. state->second = msp3400c_carrier_detect_65[max2].cdo;
  551. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  552. state->nicam_on = 0;
  553. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  554. state->watch_stereo = 1;
  555. } else if (max2 == 0 && (state->std & V4L2_STD_SECAM)) {
  556. /* L NICAM or AM-mono */
  557. state->second = msp3400c_carrier_detect_65[max2].cdo;
  558. msp3400c_setmode(client, MSP_MODE_AM_NICAM);
  559. state->nicam_on = 0;
  560. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  561. msp3400c_setcarrier(client, state->second, state->main);
  562. /* volume prescale for SCART (AM mono input) */
  563. msp_write_dsp(client, 0x000d, 0x1900);
  564. state->watch_stereo = 1;
  565. } else if (max2 == 0 && state->has_nicam) {
  566. /* D/K NICAM */
  567. state->second = msp3400c_carrier_detect_65[max2].cdo;
  568. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  569. state->nicam_on = 1;
  570. msp3400c_setcarrier(client, state->second, state->main);
  571. state->watch_stereo = 1;
  572. } else {
  573. goto no_second;
  574. }
  575. break;
  576. case 0: /* 4.5 */
  577. default:
  578. no_second:
  579. state->second = msp3400c_carrier_detect_main[max1].cdo;
  580. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  581. state->nicam_on = 0;
  582. msp3400c_setcarrier(client, state->second, state->main);
  583. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  584. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  585. break;
  586. }
  587. /* unmute */
  588. msp_set_audio(client);
  589. if (debug)
  590. msp3400c_print_mode(client);
  591. /* monitor tv audio mode */
  592. while (state->watch_stereo) {
  593. if (msp_sleep(state,5000))
  594. goto restart;
  595. watch_stereo(client);
  596. }
  597. }
  598. v4l_dbg(1, client, "thread: exit\n");
  599. return 0;
  600. }
  601. int msp3410d_thread(void *data)
  602. {
  603. struct i2c_client *client = data;
  604. struct msp_state *state = i2c_get_clientdata(client);
  605. int mode,val,i,std;
  606. v4l_dbg(1, client, "msp3410 daemon started\n");
  607. for (;;) {
  608. v4l_dbg(2, client, "msp3410 thread: sleep\n");
  609. msp_sleep(state,-1);
  610. v4l_dbg(2, client, "msp3410 thread: wakeup\n");
  611. restart:
  612. v4l_dbg(1, client, "thread: restart scan\n");
  613. state->restart = 0;
  614. if (kthread_should_stop())
  615. break;
  616. if (state->mode == MSP_MODE_EXTERN) {
  617. /* no carrier scan needed, just unmute */
  618. v4l_dbg(1, client, "thread: no carrier scan\n");
  619. msp_set_audio(client);
  620. continue;
  621. }
  622. /* put into sane state (and mute) */
  623. msp_reset(client);
  624. /* some time for the tuner to sync */
  625. if (msp_sleep(state,200))
  626. goto restart;
  627. /* start autodetect */
  628. mode = msp_modus(client);
  629. std = (state->std & V4L2_STD_NTSC) ? 0x20 : 1;
  630. msp_write_dem(client, 0x30, mode);
  631. msp_write_dem(client, 0x20, std);
  632. state->watch_stereo = 0;
  633. if (debug)
  634. v4l_dbg(1, client, "setting mode: %s (0x%04x)\n",
  635. msp_standard_mode_name(std), std);
  636. if (std != 1) {
  637. /* programmed some specific mode */
  638. val = std;
  639. } else {
  640. /* triggered autodetect */
  641. for (;;) {
  642. if (msp_sleep(state,100))
  643. goto restart;
  644. /* check results */
  645. val = msp_read_dem(client, 0x7e);
  646. if (val < 0x07ff)
  647. break;
  648. v4l_dbg(1, client, "detection still in progress\n");
  649. }
  650. }
  651. for (i = 0; msp_modelist[i].name != NULL; i++)
  652. if (msp_modelist[i].retval == val)
  653. break;
  654. v4l_dbg(1, client, "current mode: %s (0x%04x)\n",
  655. msp_standard_mode_name(val), val);
  656. state->main = msp_modelist[i].main;
  657. state->second = msp_modelist[i].second;
  658. if (amsound && (state->std & V4L2_STD_SECAM) && (val != 0x0009)) {
  659. /* autodetection has failed, let backup */
  660. v4l_dbg(1, client, "autodetection failed,"
  661. " switching to backup mode: %s (0x%04x)\n",
  662. msp_modelist[8].name ? msp_modelist[8].name : "unknown",val);
  663. val = 0x0009;
  664. msp_write_dem(client, 0x20, val);
  665. }
  666. /* set various prescales */
  667. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  668. msp_write_dsp(client, 0x0e, 0x2403); /* FM */
  669. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  670. /* set stereo */
  671. switch (val) {
  672. case 0x0008: /* B/G NICAM */
  673. case 0x000a: /* I NICAM */
  674. if (val == 0x0008)
  675. state->mode = MSP_MODE_FM_NICAM1;
  676. else
  677. state->mode = MSP_MODE_FM_NICAM2;
  678. /* just turn on stereo */
  679. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  680. state->nicam_on = 1;
  681. state->watch_stereo = 1;
  682. msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
  683. break;
  684. case 0x0009:
  685. state->mode = MSP_MODE_AM_NICAM;
  686. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  687. state->nicam_on = 1;
  688. msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
  689. state->watch_stereo = 1;
  690. break;
  691. case 0x0020: /* BTSC */
  692. /* just turn on stereo */
  693. state->mode = MSP_MODE_BTSC;
  694. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  695. state->nicam_on = 0;
  696. state->watch_stereo = 1;
  697. msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
  698. break;
  699. case 0x0040: /* FM radio */
  700. state->mode = MSP_MODE_FM_RADIO;
  701. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  702. state->audmode = V4L2_TUNER_MODE_STEREO;
  703. state->nicam_on = 0;
  704. state->watch_stereo = 0;
  705. /* not needed in theory if we have radio, but
  706. short programming enables carrier mute */
  707. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  708. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  709. MSP_CARRIER(10.7));
  710. /* scart routing */
  711. msp_set_scart(client,SCART_IN2,0);
  712. /* msp34xx does radio decoding */
  713. msp_write_dsp(client, 0x08, 0x0020);
  714. msp_write_dsp(client, 0x09, 0x0020);
  715. msp_write_dsp(client, 0x0b, 0x0020);
  716. break;
  717. case 0x0003:
  718. case 0x0004:
  719. case 0x0005:
  720. state->mode = MSP_MODE_FM_TERRA;
  721. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  722. state->audmode = V4L2_TUNER_MODE_MONO;
  723. state->nicam_on = 0;
  724. state->watch_stereo = 1;
  725. break;
  726. }
  727. /* unmute, restore misc registers */
  728. msp_set_audio(client);
  729. msp_write_dsp(client, 0x13, state->acb);
  730. msp_write_dem(client, 0x40, state->i2s_mode);
  731. /* monitor tv audio mode */
  732. while (state->watch_stereo) {
  733. if (msp_sleep(state,5000))
  734. goto restart;
  735. watch_stereo(client);
  736. }
  737. }
  738. v4l_dbg(1, client, "thread: exit\n");
  739. return 0;
  740. }
  741. /* ----------------------------------------------------------------------- */
  742. /* msp34xxG + (autoselect no-thread) */
  743. /* this one uses both automatic standard detection and automatic sound */
  744. /* select which are available in the newer G versions */
  745. /* struct msp: only norm, acb and source are really used in this mode */
  746. /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
  747. * the value for source is the same as bit 15:8 of DSP registers 0x08,
  748. * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
  749. *
  750. * this function replaces msp3400c_setstereo
  751. */
  752. static void msp34xxg_set_source(struct i2c_client *client, int source)
  753. {
  754. struct msp_state *state = i2c_get_clientdata(client);
  755. /* fix matrix mode to stereo and let the msp choose what
  756. * to output according to 'source', as recommended
  757. * for MONO (source==0) downmixing set bit[7:0] to 0x30
  758. */
  759. int value = (source & 0x07) << 8 | (source == 0 ? 0x30 : 0x20);
  760. v4l_dbg(1, client, "set source to %d (0x%x)\n", source, value);
  761. /* Loudspeaker Output */
  762. msp_write_dsp(client, 0x08, value);
  763. /* SCART1 DA Output */
  764. msp_write_dsp(client, 0x0a, value);
  765. /* Quasi-peak detector */
  766. msp_write_dsp(client, 0x0c, value);
  767. /*
  768. * set identification threshold. Personally, I
  769. * I set it to a higher value that the default
  770. * of 0x190 to ignore noisy stereo signals.
  771. * this needs tuning. (recommended range 0x00a0-0x03c0)
  772. * 0x7f0 = forced mono mode
  773. */
  774. /* a2 threshold for stereo/bilingual */
  775. msp_write_dem(client, 0x22, stereo_threshold);
  776. state->source = source;
  777. }
  778. /* (re-)initialize the msp34xxg, according to the current norm in state->norm
  779. * return 0 if it worked, -1 if it failed
  780. */
  781. static int msp34xxg_reset(struct i2c_client *client)
  782. {
  783. struct msp_state *state = i2c_get_clientdata(client);
  784. int modus, std;
  785. if (msp_reset(client))
  786. return -1;
  787. /* make sure that input/output is muted (paranoid mode) */
  788. /* ACB, mute DSP input, mute SCART 1 */
  789. if (msp_write_dsp(client, 0x13, 0x0f20))
  790. return -1;
  791. msp_write_dem(client, 0x40, state->i2s_mode);
  792. /* step-by-step initialisation, as described in the manual */
  793. modus = msp_modus(client);
  794. if (state->radio)
  795. std = 0x40;
  796. else
  797. std = (state->std & V4L2_STD_NTSC) ? 0x20 : 1;
  798. modus &= ~0x03; /* STATUS_CHANGE = 0 */
  799. modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION = 1 */
  800. if (msp_write_dem(client, 0x30, modus))
  801. return -1;
  802. if (msp_write_dem(client, 0x20, std))
  803. return -1;
  804. /* write the dsps that may have an influence on
  805. standard/audio autodetection right now */
  806. msp34xxg_set_source(client, state->source);
  807. /* AM/FM Prescale [15:8] 75khz deviation */
  808. if (msp_write_dsp(client, 0x0e, 0x3000))
  809. return -1;
  810. /* NICAM Prescale 9db gain (as recommended) */
  811. if (msp_write_dsp(client, 0x10, 0x5a00))
  812. return -1;
  813. return 0;
  814. }
  815. int msp34xxg_thread(void *data)
  816. {
  817. struct i2c_client *client = data;
  818. struct msp_state *state = i2c_get_clientdata(client);
  819. int val, std, i;
  820. v4l_dbg(1, client, "msp34xxg daemon started\n");
  821. state->source = 1; /* default */
  822. for (;;) {
  823. v4l_dbg(2, client, "msp34xxg thread: sleep\n");
  824. msp_sleep(state, -1);
  825. v4l_dbg(2, client, "msp34xxg thread: wakeup\n");
  826. restart:
  827. v4l_dbg(1, client, "thread: restart scan\n");
  828. state->restart = 0;
  829. if (kthread_should_stop())
  830. break;
  831. /* setup the chip*/
  832. msp34xxg_reset(client);
  833. std = standard;
  834. if (std != 0x01)
  835. goto unmute;
  836. /* watch autodetect */
  837. v4l_dbg(1, client, "triggered autodetect, waiting for result\n");
  838. for (i = 0; i < 10; i++) {
  839. if (msp_sleep(state, 100))
  840. goto restart;
  841. /* check results */
  842. val = msp_read_dem(client, 0x7e);
  843. if (val < 0x07ff) {
  844. std = val;
  845. break;
  846. }
  847. v4l_dbg(2, client, "detection still in progress\n");
  848. }
  849. if (std == 1) {
  850. v4l_dbg(1, client, "detection still in progress after 10 tries. giving up.\n");
  851. continue;
  852. }
  853. unmute:
  854. state->mode = std;
  855. v4l_dbg(1, client, "current mode: %s (0x%04x)\n",
  856. msp_standard_mode_name(std), std);
  857. /* unmute: dispatch sound to scart output, set scart volume */
  858. msp_set_audio(client);
  859. /* restore ACB */
  860. if (msp_write_dsp(client, 0x13, state->acb))
  861. return -1;
  862. msp_write_dem(client, 0x40, state->i2s_mode);
  863. }
  864. v4l_dbg(1, client, "thread: exit\n");
  865. return 0;
  866. }
  867. void msp34xxg_detect_stereo(struct i2c_client *client)
  868. {
  869. struct msp_state *state = i2c_get_clientdata(client);
  870. int status = msp_read_dem(client, 0x0200);
  871. int is_bilingual = status & 0x100;
  872. int is_stereo = status & 0x40;
  873. state->rxsubchans = 0;
  874. if (is_stereo)
  875. state->rxsubchans |= V4L2_TUNER_SUB_STEREO;
  876. else
  877. state->rxsubchans |= V4L2_TUNER_SUB_MONO;
  878. if (is_bilingual) {
  879. state->rxsubchans |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  880. /* I'm supposed to check whether it's SAP or not
  881. * and set only LANG2/SAP in this case. Yet, the MSP
  882. * does a lot of work to hide this and handle everything
  883. * the same way. I don't want to work around it so unless
  884. * this is a problem, I'll handle SAP just like lang1/lang2.
  885. */
  886. }
  887. v4l_dbg(1, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  888. status, is_stereo, is_bilingual, state->rxsubchans);
  889. }
  890. void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
  891. {
  892. struct msp_state *state = i2c_get_clientdata(client);
  893. int source;
  894. switch (audmode) {
  895. case V4L2_TUNER_MODE_MONO:
  896. source = 0; /* mono only */
  897. break;
  898. case V4L2_TUNER_MODE_STEREO:
  899. source = 1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
  900. /* problem: that could also mean 2 (scart input) */
  901. break;
  902. case V4L2_TUNER_MODE_LANG1:
  903. source = 3; /* stereo or A */
  904. break;
  905. case V4L2_TUNER_MODE_LANG2:
  906. source = 4; /* stereo or B */
  907. break;
  908. default:
  909. audmode = 0;
  910. source = 1;
  911. break;
  912. }
  913. state->audmode = audmode;
  914. msp34xxg_set_source(client, source);
  915. }