msp3400-kthreads.c 31 KB

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