msp3400-kthreads.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Programming the mspx4xx sound processor family
  4. *
  5. * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/freezer.h>
  11. #include <linux/videodev2.h>
  12. #include <media/v4l2-common.h>
  13. #include <media/drv-intf/msp3400.h>
  14. #include <linux/kthread.h>
  15. #include <linux/suspend.h>
  16. #include "msp3400-driver.h"
  17. /* this one uses the automatic sound standard detection of newer msp34xx
  18. chip versions */
  19. static struct {
  20. int retval;
  21. int main, second;
  22. char *name;
  23. v4l2_std_id std;
  24. } msp_stdlist[] = {
  25. { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL },
  26. { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL },
  27. { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
  28. "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN },
  29. { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
  30. "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG },
  31. { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
  32. "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK },
  33. { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
  34. "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK },
  35. { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  36. "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK },
  37. { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
  38. "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK },
  39. { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
  40. "5.5/5.85 B/G NICAM FM", V4L2_STD_BG },
  41. { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  42. "6.5/5.85 L NICAM AM", V4L2_STD_L },
  43. { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
  44. "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I },
  45. { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  46. "6.5/5.85 D/K NICAM FM", V4L2_STD_DK },
  47. { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  48. "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK },
  49. { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
  50. "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK },
  51. { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
  52. "4.5 M BTSC-Stereo", V4L2_STD_MTS },
  53. { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
  54. "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS },
  55. { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
  56. "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP },
  57. { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  58. "10.7 FM-Stereo Radio", V4L2_STD_ALL },
  59. { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  60. "6.5 SAT-Mono", V4L2_STD_ALL },
  61. { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
  62. "7.02/7.20 SAT-Stereo", V4L2_STD_ALL },
  63. { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
  64. "7.2 SAT ADR", V4L2_STD_ALL },
  65. { -1, 0, 0, NULL, 0 }, /* EOF */
  66. };
  67. static struct msp3400c_init_data_dem {
  68. int fir1[6];
  69. int fir2[6];
  70. int cdo1;
  71. int cdo2;
  72. int ad_cv;
  73. int mode_reg;
  74. int dsp_src;
  75. int dsp_matrix;
  76. } msp3400c_init_data[] = {
  77. { /* AM (for carrier detect / msp3400) */
  78. {75, 19, 36, 35, 39, 40},
  79. {75, 19, 36, 35, 39, 40},
  80. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  81. 0x00d0, 0x0500, 0x0020, 0x3000
  82. }, { /* AM (for carrier detect / msp3410) */
  83. {-1, -1, -8, 2, 59, 126},
  84. {-1, -1, -8, 2, 59, 126},
  85. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  86. 0x00d0, 0x0100, 0x0020, 0x3000
  87. }, { /* FM Radio */
  88. {-8, -8, 4, 6, 78, 107},
  89. {-8, -8, 4, 6, 78, 107},
  90. MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  91. 0x00d0, 0x0480, 0x0020, 0x3000
  92. }, { /* Terrestrial FM-mono + FM-stereo */
  93. {3, 18, 27, 48, 66, 72},
  94. {3, 18, 27, 48, 66, 72},
  95. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  96. 0x00d0, 0x0480, 0x0030, 0x3000
  97. }, { /* Sat FM-mono */
  98. { 1, 9, 14, 24, 33, 37},
  99. { 3, 18, 27, 48, 66, 72},
  100. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  101. 0x00c6, 0x0480, 0x0000, 0x3000
  102. }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
  103. {-2, -8, -10, 10, 50, 86},
  104. {3, 18, 27, 48, 66, 72},
  105. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  106. 0x00d0, 0x0040, 0x0120, 0x3000
  107. }, { /* NICAM/FM -- I (6.0/6.552) */
  108. {2, 4, -6, -4, 40, 94},
  109. {3, 18, 27, 48, 66, 72},
  110. MSP_CARRIER(6.0), MSP_CARRIER(6.0),
  111. 0x00d0, 0x0040, 0x0120, 0x3000
  112. }, { /* NICAM/AM -- L (6.5/5.85) */
  113. {-2, -8, -10, 10, 50, 86},
  114. {-4, -12, -9, 23, 79, 126},
  115. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  116. 0x00c6, 0x0140, 0x0120, 0x7c00
  117. },
  118. };
  119. struct msp3400c_carrier_detect {
  120. int cdo;
  121. char *name;
  122. };
  123. static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
  124. /* main carrier */
  125. { MSP_CARRIER(4.5), "4.5 NTSC" },
  126. { MSP_CARRIER(5.5), "5.5 PAL B/G" },
  127. { MSP_CARRIER(6.0), "6.0 PAL I" },
  128. { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
  129. };
  130. static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
  131. /* PAL B/G */
  132. { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
  133. { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
  134. };
  135. static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
  136. /* PAL SAT / SECAM */
  137. { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
  138. { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
  139. { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
  140. { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
  141. { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
  142. { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
  143. };
  144. /* ------------------------------------------------------------------------ */
  145. const char *msp_standard_std_name(int std)
  146. {
  147. int i;
  148. for (i = 0; msp_stdlist[i].name != NULL; i++)
  149. if (msp_stdlist[i].retval == std)
  150. return msp_stdlist[i].name;
  151. return "unknown";
  152. }
  153. static v4l2_std_id msp_standard_std(int std)
  154. {
  155. int i;
  156. for (i = 0; msp_stdlist[i].name != NULL; i++)
  157. if (msp_stdlist[i].retval == std)
  158. return msp_stdlist[i].std;
  159. return V4L2_STD_ALL;
  160. }
  161. static void msp_set_source(struct i2c_client *client, u16 src)
  162. {
  163. struct msp_state *state = to_state(i2c_get_clientdata(client));
  164. if (msp_dolby) {
  165. msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
  166. msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
  167. } else {
  168. msp_write_dsp(client, 0x0008, src);
  169. msp_write_dsp(client, 0x0009, src);
  170. }
  171. msp_write_dsp(client, 0x000a, src);
  172. msp_write_dsp(client, 0x000b, src);
  173. msp_write_dsp(client, 0x000c, src);
  174. if (state->has_scart2_out)
  175. msp_write_dsp(client, 0x0041, src);
  176. }
  177. void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
  178. {
  179. msp_write_dem(client, 0x0093, cdo1 & 0xfff);
  180. msp_write_dem(client, 0x009b, cdo1 >> 12);
  181. msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
  182. msp_write_dem(client, 0x00ab, cdo2 >> 12);
  183. msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
  184. }
  185. void msp3400c_set_mode(struct i2c_client *client, int mode)
  186. {
  187. struct msp_state *state = to_state(i2c_get_clientdata(client));
  188. struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
  189. int tuner = (state->route_in >> 3) & 1;
  190. int i;
  191. dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode);
  192. state->mode = mode;
  193. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  194. msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
  195. for (i = 5; i >= 0; i--) /* fir 1 */
  196. msp_write_dem(client, 0x0001, data->fir1[i]);
  197. msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
  198. msp_write_dem(client, 0x0005, 0x0040);
  199. msp_write_dem(client, 0x0005, 0x0000);
  200. for (i = 5; i >= 0; i--)
  201. msp_write_dem(client, 0x0005, data->fir2[i]);
  202. msp_write_dem(client, 0x0083, data->mode_reg);
  203. msp3400c_set_carrier(client, data->cdo1, data->cdo2);
  204. msp_set_source(client, data->dsp_src);
  205. /* set prescales */
  206. /* volume prescale for SCART (AM mono input) */
  207. msp_write_dsp(client, 0x000d, 0x1900);
  208. msp_write_dsp(client, 0x000e, data->dsp_matrix);
  209. if (state->has_nicam) /* nicam prescale */
  210. msp_write_dsp(client, 0x0010, 0x5a00);
  211. }
  212. /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
  213. nor do they support stereo BTSC. */
  214. static void msp3400c_set_audmode(struct i2c_client *client)
  215. {
  216. static char *strmode[] = {
  217. "mono", "stereo", "lang2", "lang1", "lang1+lang2"
  218. };
  219. struct msp_state *state = to_state(i2c_get_clientdata(client));
  220. char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
  221. strmode[state->audmode] : "unknown";
  222. int src = 0; /* channel source: FM/AM, nicam or SCART */
  223. int audmode = state->audmode;
  224. if (state->opmode == OPMODE_AUTOSELECT) {
  225. /* this method would break everything, let's make sure
  226. * it's never called
  227. */
  228. dev_dbg_lvl(&client->dev, 1, msp_debug,
  229. "set_audmode called with mode=%d instead of set_source (ignored)\n",
  230. state->audmode);
  231. return;
  232. }
  233. /* Note: for the C and D revs no NTSC stereo + SAP is possible as
  234. the hardware does not support SAP. So the rxsubchans combination
  235. of STEREO | LANG2 does not occur. */
  236. if (state->mode != MSP_MODE_EXTERN) {
  237. /* switch to mono if only mono is available */
  238. if (state->rxsubchans == V4L2_TUNER_SUB_MONO)
  239. audmode = V4L2_TUNER_MODE_MONO;
  240. /* if bilingual */
  241. else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) {
  242. /* and mono or stereo, then fallback to lang1 */
  243. if (audmode == V4L2_TUNER_MODE_MONO ||
  244. audmode == V4L2_TUNER_MODE_STEREO)
  245. audmode = V4L2_TUNER_MODE_LANG1;
  246. }
  247. /* if stereo, and audmode is not mono, then switch to stereo */
  248. else if (audmode != V4L2_TUNER_MODE_MONO)
  249. audmode = V4L2_TUNER_MODE_STEREO;
  250. }
  251. /* switch demodulator */
  252. switch (state->mode) {
  253. case MSP_MODE_FM_TERRA:
  254. dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr);
  255. switch (audmode) {
  256. case V4L2_TUNER_MODE_STEREO:
  257. msp_write_dsp(client, 0x000e, 0x3001);
  258. break;
  259. case V4L2_TUNER_MODE_MONO:
  260. case V4L2_TUNER_MODE_LANG1:
  261. case V4L2_TUNER_MODE_LANG2:
  262. case V4L2_TUNER_MODE_LANG1_LANG2:
  263. msp_write_dsp(client, 0x000e, 0x3000);
  264. break;
  265. }
  266. break;
  267. case MSP_MODE_FM_SAT:
  268. dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr);
  269. switch (audmode) {
  270. case V4L2_TUNER_MODE_MONO:
  271. msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
  272. break;
  273. case V4L2_TUNER_MODE_STEREO:
  274. case V4L2_TUNER_MODE_LANG1_LANG2:
  275. msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
  276. break;
  277. case V4L2_TUNER_MODE_LANG1:
  278. msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  279. break;
  280. case V4L2_TUNER_MODE_LANG2:
  281. msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  282. break;
  283. }
  284. break;
  285. case MSP_MODE_FM_NICAM1:
  286. case MSP_MODE_FM_NICAM2:
  287. case MSP_MODE_AM_NICAM:
  288. dev_dbg_lvl(&client->dev, 1, msp_debug,
  289. "NICAM set_audmode: %s\n", modestr);
  290. if (state->nicam_on)
  291. src = 0x0100; /* NICAM */
  292. break;
  293. case MSP_MODE_BTSC:
  294. dev_dbg_lvl(&client->dev, 1, msp_debug,
  295. "BTSC set_audmode: %s\n", modestr);
  296. break;
  297. case MSP_MODE_EXTERN:
  298. dev_dbg_lvl(&client->dev, 1, msp_debug,
  299. "extern set_audmode: %s\n", modestr);
  300. src = 0x0200; /* SCART */
  301. break;
  302. case MSP_MODE_FM_RADIO:
  303. dev_dbg_lvl(&client->dev, 1, msp_debug,
  304. "FM-Radio set_audmode: %s\n", modestr);
  305. break;
  306. default:
  307. dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n");
  308. return;
  309. }
  310. /* switch audio */
  311. dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode);
  312. switch (audmode) {
  313. case V4L2_TUNER_MODE_STEREO:
  314. case V4L2_TUNER_MODE_LANG1_LANG2:
  315. src |= 0x0020;
  316. break;
  317. case V4L2_TUNER_MODE_MONO:
  318. if (state->mode == MSP_MODE_AM_NICAM) {
  319. dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n");
  320. /* AM mono decoding is handled by tuner, not MSP chip */
  321. /* SCART switching control register */
  322. msp_set_scart(client, SCART_MONO, 0);
  323. src = 0x0200;
  324. break;
  325. }
  326. if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
  327. src = 0x0030;
  328. break;
  329. case V4L2_TUNER_MODE_LANG1:
  330. break;
  331. case V4L2_TUNER_MODE_LANG2:
  332. src |= 0x0010;
  333. break;
  334. }
  335. dev_dbg_lvl(&client->dev, 1, msp_debug,
  336. "set_audmode final source/matrix = 0x%x\n", src);
  337. msp_set_source(client, src);
  338. }
  339. static void msp3400c_print_mode(struct i2c_client *client)
  340. {
  341. struct msp_state *state = to_state(i2c_get_clientdata(client));
  342. if (state->main == state->second)
  343. dev_dbg_lvl(&client->dev, 1, msp_debug,
  344. "mono sound carrier: %d.%03d MHz\n",
  345. state->main / 910000, (state->main / 910) % 1000);
  346. else
  347. dev_dbg_lvl(&client->dev, 1, msp_debug,
  348. "main sound carrier: %d.%03d MHz\n",
  349. state->main / 910000, (state->main / 910) % 1000);
  350. if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
  351. dev_dbg_lvl(&client->dev, 1, msp_debug,
  352. "NICAM/FM carrier : %d.%03d MHz\n",
  353. state->second / 910000, (state->second/910) % 1000);
  354. if (state->mode == MSP_MODE_AM_NICAM)
  355. dev_dbg_lvl(&client->dev, 1, msp_debug,
  356. "NICAM/AM carrier : %d.%03d MHz\n",
  357. state->second / 910000, (state->second / 910) % 1000);
  358. if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
  359. dev_dbg_lvl(&client->dev, 1, msp_debug,
  360. "FM-stereo carrier : %d.%03d MHz\n",
  361. state->second / 910000, (state->second / 910) % 1000);
  362. }
  363. }
  364. /* ----------------------------------------------------------------------- */
  365. static int msp3400c_detect_stereo(struct i2c_client *client)
  366. {
  367. struct msp_state *state = to_state(i2c_get_clientdata(client));
  368. int val;
  369. int rxsubchans = state->rxsubchans;
  370. int newnicam = state->nicam_on;
  371. int update = 0;
  372. switch (state->mode) {
  373. case MSP_MODE_FM_TERRA:
  374. val = msp_read_dsp(client, 0x18);
  375. if (val > 32767)
  376. val -= 65536;
  377. dev_dbg_lvl(&client->dev, 2, msp_debug,
  378. "stereo detect register: %d\n", val);
  379. if (val > 8192) {
  380. rxsubchans = V4L2_TUNER_SUB_STEREO;
  381. } else if (val < -4096) {
  382. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  383. } else {
  384. rxsubchans = V4L2_TUNER_SUB_MONO;
  385. }
  386. newnicam = 0;
  387. break;
  388. case MSP_MODE_FM_NICAM1:
  389. case MSP_MODE_FM_NICAM2:
  390. case MSP_MODE_AM_NICAM:
  391. val = msp_read_dem(client, 0x23);
  392. dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n",
  393. val & 1, (val & 0x1e) >> 1);
  394. if (val & 1) {
  395. /* nicam synced */
  396. switch ((val & 0x1e) >> 1) {
  397. case 0:
  398. case 8:
  399. rxsubchans = V4L2_TUNER_SUB_STEREO;
  400. break;
  401. case 1:
  402. case 9:
  403. rxsubchans = V4L2_TUNER_SUB_MONO;
  404. break;
  405. case 2:
  406. case 10:
  407. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  408. break;
  409. default:
  410. rxsubchans = V4L2_TUNER_SUB_MONO;
  411. break;
  412. }
  413. newnicam = 1;
  414. } else {
  415. newnicam = 0;
  416. rxsubchans = V4L2_TUNER_SUB_MONO;
  417. }
  418. break;
  419. }
  420. if (rxsubchans != state->rxsubchans) {
  421. update = 1;
  422. dev_dbg_lvl(&client->dev, 1, msp_debug,
  423. "watch: rxsubchans %02x => %02x\n",
  424. state->rxsubchans, rxsubchans);
  425. state->rxsubchans = rxsubchans;
  426. }
  427. if (newnicam != state->nicam_on) {
  428. update = 1;
  429. dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n",
  430. state->nicam_on, newnicam);
  431. state->nicam_on = newnicam;
  432. }
  433. return update;
  434. }
  435. /*
  436. * A kernel thread for msp3400 control -- we don't want to block the
  437. * in the ioctl while doing the sound carrier & stereo detect
  438. */
  439. /* stereo/multilang monitoring */
  440. static void watch_stereo(struct i2c_client *client)
  441. {
  442. struct msp_state *state = to_state(i2c_get_clientdata(client));
  443. if (msp_detect_stereo(client))
  444. msp_set_audmode(client);
  445. if (msp_once)
  446. state->watch_stereo = 0;
  447. }
  448. int msp3400c_thread(void *data)
  449. {
  450. struct i2c_client *client = data;
  451. struct msp_state *state = to_state(i2c_get_clientdata(client));
  452. struct msp3400c_carrier_detect *cd;
  453. int count, max1, max2, val1, val2, val, i;
  454. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n");
  455. state->detected_std = V4L2_STD_ALL;
  456. set_freezable();
  457. for (;;) {
  458. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n");
  459. msp_sleep(state, -1);
  460. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n");
  461. restart:
  462. dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
  463. state->restart = 0;
  464. if (kthread_should_stop())
  465. break;
  466. if (state->radio || MSP_MODE_EXTERN == state->mode) {
  467. /* no carrier scan, just unmute */
  468. dev_dbg_lvl(&client->dev, 1, msp_debug,
  469. "thread: no carrier scan\n");
  470. state->scan_in_progress = 0;
  471. msp_update_volume(state);
  472. continue;
  473. }
  474. /* mute audio */
  475. state->scan_in_progress = 1;
  476. msp_update_volume(state);
  477. msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
  478. val1 = val2 = 0;
  479. max1 = max2 = -1;
  480. state->watch_stereo = 0;
  481. state->nicam_on = 0;
  482. /* wait for tuner to settle down after a channel change */
  483. if (msp_sleep(state, 200))
  484. goto restart;
  485. /* carrier detect pass #1 -- main carrier */
  486. cd = msp3400c_carrier_detect_main;
  487. count = ARRAY_SIZE(msp3400c_carrier_detect_main);
  488. if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
  489. /* autodetect doesn't work well with AM ... */
  490. max1 = 3;
  491. count = 0;
  492. dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n");
  493. }
  494. for (i = 0; i < count; i++) {
  495. msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
  496. if (msp_sleep(state, 100))
  497. goto restart;
  498. val = msp_read_dsp(client, 0x1b);
  499. if (val > 32767)
  500. val -= 65536;
  501. if (val1 < val) {
  502. val1 = val;
  503. max1 = i;
  504. }
  505. dev_dbg_lvl(&client->dev, 1, msp_debug,
  506. "carrier1 val: %5d / %s\n", val, cd[i].name);
  507. }
  508. /* carrier detect pass #2 -- second (stereo) carrier */
  509. switch (max1) {
  510. case 1: /* 5.5 */
  511. cd = msp3400c_carrier_detect_55;
  512. count = ARRAY_SIZE(msp3400c_carrier_detect_55);
  513. break;
  514. case 3: /* 6.5 */
  515. cd = msp3400c_carrier_detect_65;
  516. count = ARRAY_SIZE(msp3400c_carrier_detect_65);
  517. break;
  518. case 0: /* 4.5 */
  519. case 2: /* 6.0 */
  520. default:
  521. cd = NULL;
  522. count = 0;
  523. break;
  524. }
  525. if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
  526. /* autodetect doesn't work well with AM ... */
  527. cd = NULL;
  528. count = 0;
  529. max2 = 0;
  530. }
  531. for (i = 0; i < count; i++) {
  532. msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
  533. if (msp_sleep(state, 100))
  534. goto restart;
  535. val = msp_read_dsp(client, 0x1b);
  536. if (val > 32767)
  537. val -= 65536;
  538. if (val2 < val) {
  539. val2 = val;
  540. max2 = i;
  541. }
  542. dev_dbg_lvl(&client->dev, 1, msp_debug,
  543. "carrier2 val: %5d / %s\n", val, cd[i].name);
  544. }
  545. if (max1 < 0 || max1 > 3)
  546. goto restart;
  547. /* program the msp3400 according to the results */
  548. state->main = msp3400c_carrier_detect_main[max1].cdo;
  549. switch (max1) {
  550. case 1: /* 5.5 */
  551. state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H;
  552. if (max2 == 0) {
  553. /* B/G FM-stereo */
  554. state->second = msp3400c_carrier_detect_55[max2].cdo;
  555. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  556. state->watch_stereo = 1;
  557. } else if (max2 == 1 && state->has_nicam) {
  558. /* B/G NICAM */
  559. state->second = msp3400c_carrier_detect_55[max2].cdo;
  560. msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
  561. state->nicam_on = 1;
  562. state->watch_stereo = 1;
  563. } else {
  564. goto no_second;
  565. }
  566. break;
  567. case 2: /* 6.0 */
  568. /* PAL I NICAM */
  569. state->detected_std = V4L2_STD_PAL_I;
  570. state->second = MSP_CARRIER(6.552);
  571. msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
  572. state->nicam_on = 1;
  573. state->watch_stereo = 1;
  574. break;
  575. case 3: /* 6.5 */
  576. if (max2 == 1 || max2 == 2) {
  577. /* D/K FM-stereo */
  578. state->second = msp3400c_carrier_detect_65[max2].cdo;
  579. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  580. state->watch_stereo = 1;
  581. state->detected_std = V4L2_STD_DK;
  582. } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
  583. /* L NICAM or AM-mono */
  584. state->second = msp3400c_carrier_detect_65[max2].cdo;
  585. msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
  586. state->watch_stereo = 1;
  587. state->detected_std = V4L2_STD_L;
  588. } else if (max2 == 0 && state->has_nicam) {
  589. /* D/K NICAM */
  590. state->second = msp3400c_carrier_detect_65[max2].cdo;
  591. msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
  592. state->nicam_on = 1;
  593. state->watch_stereo = 1;
  594. state->detected_std = V4L2_STD_DK;
  595. } else {
  596. goto no_second;
  597. }
  598. break;
  599. case 0: /* 4.5 */
  600. state->detected_std = V4L2_STD_MN;
  601. fallthrough;
  602. default:
  603. no_second:
  604. state->second = msp3400c_carrier_detect_main[max1].cdo;
  605. msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
  606. break;
  607. }
  608. msp3400c_set_carrier(client, state->second, state->main);
  609. /* unmute */
  610. state->scan_in_progress = 0;
  611. msp3400c_set_audmode(client);
  612. msp_update_volume(state);
  613. if (msp_debug)
  614. msp3400c_print_mode(client);
  615. /* monitor tv audio mode, the first time don't wait
  616. so long to get a quick stereo/bilingual result */
  617. count = 3;
  618. while (state->watch_stereo) {
  619. if (msp_sleep(state, count ? 1000 : 5000))
  620. goto restart;
  621. if (count)
  622. count--;
  623. watch_stereo(client);
  624. }
  625. }
  626. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  627. return 0;
  628. }
  629. int msp3410d_thread(void *data)
  630. {
  631. struct i2c_client *client = data;
  632. struct msp_state *state = to_state(i2c_get_clientdata(client));
  633. int val, i, std, count;
  634. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
  635. state->detected_std = V4L2_STD_ALL;
  636. set_freezable();
  637. for (;;) {
  638. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
  639. msp_sleep(state, -1);
  640. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
  641. restart:
  642. dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
  643. state->restart = 0;
  644. if (kthread_should_stop())
  645. break;
  646. if (state->mode == MSP_MODE_EXTERN) {
  647. /* no carrier scan needed, just unmute */
  648. dev_dbg_lvl(&client->dev, 1, msp_debug,
  649. "thread: no carrier scan\n");
  650. state->scan_in_progress = 0;
  651. msp_update_volume(state);
  652. continue;
  653. }
  654. /* mute audio */
  655. state->scan_in_progress = 1;
  656. msp_update_volume(state);
  657. /* start autodetect. Note: autodetect is not supported for
  658. NTSC-M and radio, hence we force the standard in those
  659. cases. */
  660. if (state->radio)
  661. std = 0x40;
  662. else
  663. std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
  664. state->watch_stereo = 0;
  665. state->nicam_on = 0;
  666. /* wait for tuner to settle down after a channel change */
  667. if (msp_sleep(state, 200))
  668. goto restart;
  669. if (msp_debug)
  670. dev_dbg_lvl(&client->dev, 2, msp_debug,
  671. "setting standard: %s (0x%04x)\n",
  672. msp_standard_std_name(std), std);
  673. if (std != 1) {
  674. /* programmed some specific mode */
  675. val = std;
  676. } else {
  677. /* triggered autodetect */
  678. msp_write_dem(client, 0x20, std);
  679. for (;;) {
  680. if (msp_sleep(state, 100))
  681. goto restart;
  682. /* check results */
  683. val = msp_read_dem(client, 0x7e);
  684. if (val < 0x07ff)
  685. break;
  686. dev_dbg_lvl(&client->dev, 2, msp_debug,
  687. "detection still in progress\n");
  688. }
  689. }
  690. for (i = 0; msp_stdlist[i].name != NULL; i++)
  691. if (msp_stdlist[i].retval == val)
  692. break;
  693. dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
  694. msp_standard_std_name(val), val);
  695. state->main = msp_stdlist[i].main;
  696. state->second = msp_stdlist[i].second;
  697. state->std = val;
  698. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  699. if (msp_amsound && !state->radio &&
  700. (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
  701. /* autodetection has failed, let backup */
  702. dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
  703. msp_stdlist[8].name ?
  704. msp_stdlist[8].name : "unknown", val);
  705. state->std = val = 0x0009;
  706. msp_write_dem(client, 0x20, val);
  707. } else {
  708. state->detected_std = msp_standard_std(state->std);
  709. }
  710. /* set stereo */
  711. switch (val) {
  712. case 0x0008: /* B/G NICAM */
  713. case 0x000a: /* I NICAM */
  714. case 0x000b: /* D/K NICAM */
  715. if (val == 0x000a)
  716. state->mode = MSP_MODE_FM_NICAM2;
  717. else
  718. state->mode = MSP_MODE_FM_NICAM1;
  719. /* just turn on stereo */
  720. state->nicam_on = 1;
  721. state->watch_stereo = 1;
  722. break;
  723. case 0x0009:
  724. state->mode = MSP_MODE_AM_NICAM;
  725. state->nicam_on = 1;
  726. state->watch_stereo = 1;
  727. break;
  728. case 0x0020: /* BTSC */
  729. /* The pre-'G' models only have BTSC-mono */
  730. state->mode = MSP_MODE_BTSC;
  731. break;
  732. case 0x0040: /* FM radio */
  733. state->mode = MSP_MODE_FM_RADIO;
  734. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  735. /* not needed in theory if we have radio, but
  736. short programming enables carrier mute */
  737. msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
  738. msp3400c_set_carrier(client, MSP_CARRIER(10.7),
  739. MSP_CARRIER(10.7));
  740. break;
  741. case 0x0002:
  742. case 0x0003:
  743. case 0x0004:
  744. case 0x0005:
  745. state->mode = MSP_MODE_FM_TERRA;
  746. state->watch_stereo = 1;
  747. break;
  748. }
  749. /* set various prescales */
  750. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  751. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  752. if (state->has_nicam)
  753. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  754. if (state->has_i2s_conf)
  755. msp_write_dem(client, 0x40, state->i2s_mode);
  756. /* unmute */
  757. msp3400c_set_audmode(client);
  758. state->scan_in_progress = 0;
  759. msp_update_volume(state);
  760. /* monitor tv audio mode, the first time don't wait
  761. so long to get a quick stereo/bilingual result */
  762. count = 3;
  763. while (state->watch_stereo) {
  764. if (msp_sleep(state, count ? 1000 : 5000))
  765. goto restart;
  766. if (count)
  767. count--;
  768. watch_stereo(client);
  769. }
  770. }
  771. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  772. return 0;
  773. }
  774. /* ----------------------------------------------------------------------- */
  775. /* msp34xxG + (autoselect no-thread)
  776. * this one uses both automatic standard detection and automatic sound
  777. * select which are available in the newer G versions
  778. * struct msp: only norm, acb and source are really used in this mode
  779. */
  780. static int msp34xxg_modus(struct i2c_client *client)
  781. {
  782. struct msp_state *state = to_state(i2c_get_clientdata(client));
  783. if (state->radio) {
  784. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
  785. return 0x0001;
  786. }
  787. if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
  788. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
  789. return 0x4001;
  790. }
  791. if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
  792. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
  793. return 0x0001;
  794. }
  795. if (state->v4l2_std == V4L2_STD_SECAM_L) {
  796. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
  797. return 0x6001;
  798. }
  799. if (state->v4l2_std & V4L2_STD_MN) {
  800. dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
  801. return 0x2001;
  802. }
  803. return 0x7001;
  804. }
  805. static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
  806. {
  807. struct msp_state *state = to_state(i2c_get_clientdata(client));
  808. int source, matrix;
  809. switch (state->audmode) {
  810. case V4L2_TUNER_MODE_MONO:
  811. source = 0; /* mono only */
  812. matrix = 0x30;
  813. break;
  814. case V4L2_TUNER_MODE_LANG2:
  815. source = 4; /* stereo or B */
  816. matrix = 0x10;
  817. break;
  818. case V4L2_TUNER_MODE_LANG1_LANG2:
  819. source = 1; /* stereo or A|B */
  820. matrix = 0x20;
  821. break;
  822. case V4L2_TUNER_MODE_LANG1:
  823. source = 3; /* stereo or A */
  824. matrix = 0x00;
  825. break;
  826. case V4L2_TUNER_MODE_STEREO:
  827. default:
  828. source = 3; /* stereo or A */
  829. matrix = 0x20;
  830. break;
  831. }
  832. if (in == MSP_DSP_IN_TUNER)
  833. source = (source << 8) | 0x20;
  834. /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
  835. instead of 11, 12, 13. So we add one for that msp version. */
  836. else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
  837. source = ((in + 1) << 8) | matrix;
  838. else
  839. source = (in << 8) | matrix;
  840. dev_dbg_lvl(&client->dev, 1, msp_debug,
  841. "set source to %d (0x%x) for output %02x\n", in, source, reg);
  842. msp_write_dsp(client, reg, source);
  843. }
  844. static void msp34xxg_set_sources(struct i2c_client *client)
  845. {
  846. struct msp_state *state = to_state(i2c_get_clientdata(client));
  847. u32 in = state->route_in;
  848. msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
  849. /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
  850. msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
  851. msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
  852. msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
  853. if (state->has_scart2_out)
  854. msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
  855. msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
  856. }
  857. /* (re-)initialize the msp34xxg */
  858. static void msp34xxg_reset(struct i2c_client *client)
  859. {
  860. struct msp_state *state = to_state(i2c_get_clientdata(client));
  861. int tuner = (state->route_in >> 3) & 1;
  862. int modus;
  863. /* initialize std to 1 (autodetect) to signal that no standard is
  864. selected yet. */
  865. state->std = 1;
  866. msp_reset(client);
  867. if (state->has_i2s_conf)
  868. msp_write_dem(client, 0x40, state->i2s_mode);
  869. /* step-by-step initialisation, as described in the manual */
  870. modus = msp34xxg_modus(client);
  871. modus |= tuner ? 0x100 : 0;
  872. msp_write_dem(client, 0x30, modus);
  873. /* write the dsps that may have an influence on
  874. standard/audio autodetection right now */
  875. msp34xxg_set_sources(client);
  876. msp_write_dsp(client, 0x0d, 0x1900); /* scart */
  877. msp_write_dsp(client, 0x0e, 0x3000); /* FM */
  878. if (state->has_nicam)
  879. msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
  880. /* set identification threshold. Personally, I
  881. * I set it to a higher value than the default
  882. * of 0x190 to ignore noisy stereo signals.
  883. * this needs tuning. (recommended range 0x00a0-0x03c0)
  884. * 0x7f0 = forced mono mode
  885. *
  886. * a2 threshold for stereo/bilingual.
  887. * Note: this register is part of the Manual/Compatibility mode.
  888. * It is supported by all 'G'-family chips.
  889. */
  890. msp_write_dem(client, 0x22, msp_stereo_thresh);
  891. }
  892. int msp34xxg_thread(void *data)
  893. {
  894. struct i2c_client *client = data;
  895. struct msp_state *state = to_state(i2c_get_clientdata(client));
  896. int val, i;
  897. dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
  898. state->detected_std = V4L2_STD_ALL;
  899. set_freezable();
  900. for (;;) {
  901. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
  902. msp_sleep(state, -1);
  903. dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
  904. restart:
  905. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
  906. state->restart = 0;
  907. if (kthread_should_stop())
  908. break;
  909. if (state->mode == MSP_MODE_EXTERN) {
  910. /* no carrier scan needed, just unmute */
  911. dev_dbg_lvl(&client->dev, 1, msp_debug,
  912. "thread: no carrier scan\n");
  913. state->scan_in_progress = 0;
  914. msp_update_volume(state);
  915. continue;
  916. }
  917. /* setup the chip*/
  918. msp34xxg_reset(client);
  919. state->std = state->radio ? 0x40 :
  920. (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
  921. msp_write_dem(client, 0x20, state->std);
  922. /* start autodetect */
  923. if (state->std != 1)
  924. goto unmute;
  925. /* watch autodetect */
  926. dev_dbg_lvl(&client->dev, 1, msp_debug,
  927. "started autodetect, waiting for result\n");
  928. for (i = 0; i < 10; i++) {
  929. if (msp_sleep(state, 100))
  930. goto restart;
  931. /* check results */
  932. val = msp_read_dem(client, 0x7e);
  933. if (val < 0x07ff) {
  934. state->std = val;
  935. break;
  936. }
  937. dev_dbg_lvl(&client->dev, 2, msp_debug,
  938. "detection still in progress\n");
  939. }
  940. if (state->std == 1) {
  941. dev_dbg_lvl(&client->dev, 1, msp_debug,
  942. "detection still in progress after 10 tries. giving up.\n");
  943. continue;
  944. }
  945. unmute:
  946. dev_dbg_lvl(&client->dev, 1, msp_debug,
  947. "detected standard: %s (0x%04x)\n",
  948. msp_standard_std_name(state->std), state->std);
  949. state->detected_std = msp_standard_std(state->std);
  950. if (state->std == 9) {
  951. /* AM NICAM mode */
  952. msp_write_dsp(client, 0x0e, 0x7c00);
  953. }
  954. /* unmute: dispatch sound to scart output, set scart volume */
  955. msp_update_volume(state);
  956. /* restore ACB */
  957. if (msp_write_dsp(client, 0x13, state->acb))
  958. return -1;
  959. /* the periodic stereo/SAP check is only relevant for
  960. the 0x20 standard (BTSC) */
  961. if (state->std != 0x20)
  962. continue;
  963. state->watch_stereo = 1;
  964. /* monitor tv audio mode, the first time don't wait
  965. in order to get a quick stereo/SAP update */
  966. watch_stereo(client);
  967. while (state->watch_stereo) {
  968. watch_stereo(client);
  969. if (msp_sleep(state, 5000))
  970. goto restart;
  971. }
  972. }
  973. dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
  974. return 0;
  975. }
  976. static int msp34xxg_detect_stereo(struct i2c_client *client)
  977. {
  978. struct msp_state *state = to_state(i2c_get_clientdata(client));
  979. int status = msp_read_dem(client, 0x0200);
  980. int is_bilingual = status & 0x100;
  981. int is_stereo = status & 0x40;
  982. int oldrx = state->rxsubchans;
  983. if (state->mode == MSP_MODE_EXTERN)
  984. return 0;
  985. state->rxsubchans = 0;
  986. if (is_stereo)
  987. state->rxsubchans = V4L2_TUNER_SUB_STEREO;
  988. else
  989. state->rxsubchans = V4L2_TUNER_SUB_MONO;
  990. if (is_bilingual) {
  991. if (state->std == 0x20)
  992. state->rxsubchans |= V4L2_TUNER_SUB_SAP;
  993. else
  994. state->rxsubchans =
  995. V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  996. }
  997. dev_dbg_lvl(&client->dev, 1, msp_debug,
  998. "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  999. status, is_stereo, is_bilingual, state->rxsubchans);
  1000. return (oldrx != state->rxsubchans);
  1001. }
  1002. static void msp34xxg_set_audmode(struct i2c_client *client)
  1003. {
  1004. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1005. if (state->std == 0x20) {
  1006. if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
  1007. (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
  1008. state->audmode == V4L2_TUNER_MODE_LANG2)) {
  1009. msp_write_dem(client, 0x20, 0x21);
  1010. } else {
  1011. msp_write_dem(client, 0x20, 0x20);
  1012. }
  1013. }
  1014. msp34xxg_set_sources(client);
  1015. }
  1016. void msp_set_audmode(struct i2c_client *client)
  1017. {
  1018. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1019. switch (state->opmode) {
  1020. case OPMODE_MANUAL:
  1021. case OPMODE_AUTODETECT:
  1022. msp3400c_set_audmode(client);
  1023. break;
  1024. case OPMODE_AUTOSELECT:
  1025. msp34xxg_set_audmode(client);
  1026. break;
  1027. }
  1028. }
  1029. int msp_detect_stereo(struct i2c_client *client)
  1030. {
  1031. struct msp_state *state = to_state(i2c_get_clientdata(client));
  1032. switch (state->opmode) {
  1033. case OPMODE_MANUAL:
  1034. case OPMODE_AUTODETECT:
  1035. return msp3400c_detect_stereo(client);
  1036. case OPMODE_AUTOSELECT:
  1037. return msp34xxg_detect_stereo(client);
  1038. }
  1039. return 0;
  1040. }