msp3400-kthreads.c 30 KB

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