msp3400.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. /*
  2. * programming the msp34* sound processor family
  3. *
  4. * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
  5. *
  6. * what works and what doesn't:
  7. *
  8. * AM-Mono
  9. * Support for Hauppauge cards added (decoding handled by tuner) added by
  10. * Frederic Crozat <fcrozat@mail.dotcom.fr>
  11. *
  12. * FM-Mono
  13. * should work. The stereo modes are backward compatible to FM-mono,
  14. * therefore FM-Mono should be allways available.
  15. *
  16. * FM-Stereo (B/G, used in germany)
  17. * should work, with autodetect
  18. *
  19. * FM-Stereo (satellite)
  20. * should work, no autodetect (i.e. default is mono, but you can
  21. * switch to stereo -- untested)
  22. *
  23. * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
  24. * should work, with autodetect. Support for NICAM was added by
  25. * Pekka Pietikainen <pp@netppl.fi>
  26. *
  27. *
  28. * TODO:
  29. * - better SAT support
  30. *
  31. *
  32. * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
  33. * using soundcore instead of OSS
  34. *
  35. */
  36. #include <linux/config.h>
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/kernel.h>
  40. #include <linux/sched.h>
  41. #include <linux/string.h>
  42. #include <linux/timer.h>
  43. #include <linux/delay.h>
  44. #include <linux/errno.h>
  45. #include <linux/slab.h>
  46. #include <linux/i2c.h>
  47. #include <linux/videodev.h>
  48. #include <linux/init.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/kthread.h>
  51. #include <linux/suspend.h>
  52. #include <asm/semaphore.h>
  53. #include <asm/pgtable.h>
  54. #include <media/audiochip.h>
  55. #include <media/id.h>
  56. #include "msp3400.h"
  57. #define OPMODE_AUTO -1
  58. #define OPMODE_MANUAL 0
  59. #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
  60. #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
  61. /* insmod parameters */
  62. static int opmode = OPMODE_AUTO;
  63. static int debug = 0; /* debug output */
  64. static int once = 0; /* no continous stereo monitoring */
  65. static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
  66. the autoscan seems work well only with FM... */
  67. static int standard = 1; /* Override auto detect of audio standard, if needed. */
  68. static int dolby = 0;
  69. static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
  70. (msp34xxg only) 0x00a0-0x03c0 */
  71. struct msp3400c {
  72. int rev1,rev2;
  73. int opmode;
  74. int mode;
  75. int norm;
  76. int nicam_on;
  77. int acb;
  78. int main, second; /* sound carrier */
  79. int input;
  80. int source; /* see msp34xxg_set_source */
  81. /* v4l2 */
  82. int audmode;
  83. int rxsubchans;
  84. int muted;
  85. int volume, balance;
  86. int bass, treble;
  87. /* thread */
  88. struct task_struct *kthread;
  89. wait_queue_head_t wq;
  90. int restart:1;
  91. int watch_stereo:1;
  92. };
  93. #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
  94. #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
  95. #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
  96. #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
  97. #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
  98. /* ---------------------------------------------------------------------- */
  99. #define dprintk if (debug >= 1) printk
  100. #define d2printk if (debug >= 2) printk
  101. /* read-only */
  102. module_param(opmode, int, 0444);
  103. /* read-write */
  104. module_param(once, int, 0644);
  105. module_param(debug, int, 0644);
  106. module_param(stereo_threshold, int, 0644);
  107. module_param(standard, int, 0644);
  108. module_param(amsound, int, 0644);
  109. module_param(dolby, int, 0644);
  110. MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
  111. MODULE_PARM_DESC(once, "No continuous stereo monitoring");
  112. MODULE_PARM_DESC(debug, "Enable debug messages");
  113. MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
  114. MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
  115. MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
  116. MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
  117. MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
  118. MODULE_AUTHOR("Gerd Knorr");
  119. MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
  120. /* ---------------------------------------------------------------------- */
  121. #define I2C_MSP3400C 0x80
  122. #define I2C_MSP3400C_ALT 0x88
  123. #define I2C_MSP3400C_DEM 0x10
  124. #define I2C_MSP3400C_DFP 0x12
  125. /* Addresses to scan */
  126. static unsigned short normal_i2c[] = {
  127. I2C_MSP3400C >> 1,
  128. I2C_MSP3400C_ALT >> 1,
  129. I2C_CLIENT_END
  130. };
  131. I2C_CLIENT_INSMOD;
  132. /* ----------------------------------------------------------------------- */
  133. /* functions for talking to the MSP3400C Sound processor */
  134. static int msp3400c_reset(struct i2c_client *client)
  135. {
  136. /* reset and read revision code */
  137. static char reset_off[3] = { 0x00, 0x80, 0x00 };
  138. static char reset_on[3] = { 0x00, 0x00, 0x00 };
  139. static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
  140. char read[2];
  141. struct i2c_msg reset[2] = {
  142. { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
  143. { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
  144. };
  145. struct i2c_msg test[2] = {
  146. { client->addr, 0, 3, write },
  147. { client->addr, I2C_M_RD, 2, read },
  148. };
  149. if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
  150. (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
  151. (2 != i2c_transfer(client->adapter,test,2)) ) {
  152. printk(KERN_ERR "msp3400: chip reset failed\n");
  153. return -1;
  154. }
  155. return 0;
  156. }
  157. static int
  158. msp3400c_read(struct i2c_client *client, int dev, int addr)
  159. {
  160. int err;
  161. unsigned char write[3];
  162. unsigned char read[2];
  163. struct i2c_msg msgs[2] = {
  164. { client->addr, 0, 3, write },
  165. { client->addr, I2C_M_RD, 2, read }
  166. };
  167. write[0] = dev+1;
  168. write[1] = addr >> 8;
  169. write[2] = addr & 0xff;
  170. for (err = 0; err < 3;) {
  171. if (2 == i2c_transfer(client->adapter,msgs,2))
  172. break;
  173. err++;
  174. printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
  175. err, dev, addr);
  176. msleep(10);
  177. }
  178. if (3 == err) {
  179. printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
  180. msp3400c_reset(client);
  181. return -1;
  182. }
  183. return read[0] << 8 | read[1];
  184. }
  185. static int
  186. msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
  187. {
  188. int err;
  189. unsigned char buffer[5];
  190. buffer[0] = dev;
  191. buffer[1] = addr >> 8;
  192. buffer[2] = addr & 0xff;
  193. buffer[3] = val >> 8;
  194. buffer[4] = val & 0xff;
  195. for (err = 0; err < 3;) {
  196. if (5 == i2c_master_send(client, buffer, 5))
  197. break;
  198. err++;
  199. printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
  200. err, dev, addr);
  201. msleep(10);
  202. }
  203. if (3 == err) {
  204. printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
  205. msp3400c_reset(client);
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. /* ------------------------------------------------------------------------ */
  211. /* This macro is allowed for *constants* only, gcc must calculate it
  212. at compile time. Remember -- no floats in kernel mode */
  213. #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
  214. #define MSP_MODE_AM_DETECT 0
  215. #define MSP_MODE_FM_RADIO 2
  216. #define MSP_MODE_FM_TERRA 3
  217. #define MSP_MODE_FM_SAT 4
  218. #define MSP_MODE_FM_NICAM1 5
  219. #define MSP_MODE_FM_NICAM2 6
  220. #define MSP_MODE_AM_NICAM 7
  221. #define MSP_MODE_BTSC 8
  222. #define MSP_MODE_EXTERN 9
  223. static struct MSP_INIT_DATA_DEM {
  224. int fir1[6];
  225. int fir2[6];
  226. int cdo1;
  227. int cdo2;
  228. int ad_cv;
  229. int mode_reg;
  230. int dfp_src;
  231. int dfp_matrix;
  232. } msp_init_data[] = {
  233. /* AM (for carrier detect / msp3400) */
  234. { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
  235. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  236. 0x00d0, 0x0500, 0x0020, 0x3000},
  237. /* AM (for carrier detect / msp3410) */
  238. { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
  239. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  240. 0x00d0, 0x0100, 0x0020, 0x3000},
  241. /* FM Radio */
  242. { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
  243. MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  244. 0x00d0, 0x0480, 0x0020, 0x3000 },
  245. /* Terrestial FM-mono + FM-stereo */
  246. { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
  247. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  248. 0x00d0, 0x0480, 0x0030, 0x3000},
  249. /* Sat FM-mono */
  250. { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
  251. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  252. 0x00c6, 0x0480, 0x0000, 0x3000},
  253. /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
  254. { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
  255. MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  256. 0x00d0, 0x0040, 0x0120, 0x3000},
  257. /* NICAM/FM -- I (6.0/6.552) */
  258. { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
  259. MSP_CARRIER(6.0), MSP_CARRIER(6.0),
  260. 0x00d0, 0x0040, 0x0120, 0x3000},
  261. /* NICAM/AM -- L (6.5/5.85) */
  262. { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
  263. MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  264. 0x00c6, 0x0140, 0x0120, 0x7c03},
  265. };
  266. struct CARRIER_DETECT {
  267. int cdo;
  268. char *name;
  269. };
  270. static struct CARRIER_DETECT carrier_detect_main[] = {
  271. /* main carrier */
  272. { MSP_CARRIER(4.5), "4.5 NTSC" },
  273. { MSP_CARRIER(5.5), "5.5 PAL B/G" },
  274. { MSP_CARRIER(6.0), "6.0 PAL I" },
  275. { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
  276. };
  277. static struct CARRIER_DETECT carrier_detect_55[] = {
  278. /* PAL B/G */
  279. { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
  280. { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
  281. };
  282. static struct CARRIER_DETECT carrier_detect_65[] = {
  283. /* PAL SAT / SECAM */
  284. { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
  285. { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
  286. { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
  287. { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
  288. { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
  289. { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
  290. };
  291. #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
  292. /* ----------------------------------------------------------------------- */
  293. static int scarts[3][9] = {
  294. /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
  295. { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
  296. { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
  297. { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
  298. };
  299. static char *scart_names[] = {
  300. "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
  301. };
  302. static void
  303. msp3400c_set_scart(struct i2c_client *client, int in, int out)
  304. {
  305. struct msp3400c *msp = i2c_get_clientdata(client);
  306. if (-1 == scarts[out][in])
  307. return;
  308. dprintk(KERN_DEBUG
  309. "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
  310. msp->acb &= ~scarts[out][SCART_MASK];
  311. msp->acb |= scarts[out][in];
  312. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
  313. }
  314. /* ------------------------------------------------------------------------ */
  315. static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
  316. {
  317. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
  318. msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
  319. msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
  320. msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
  321. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
  322. }
  323. static void msp3400c_setvolume(struct i2c_client *client,
  324. int muted, int volume, int balance)
  325. {
  326. int val = 0, bal = 0;
  327. if (!muted) {
  328. /* 0x7f instead if 0x73 here has sound quality issues,
  329. * probably due to overmodulation + clipping ... */
  330. val = (volume * 0x73 / 65535) << 8;
  331. }
  332. if (val) {
  333. bal = (balance / 256) - 128;
  334. }
  335. dprintk(KERN_DEBUG
  336. "msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
  337. muted ? "on" : "off", volume, balance, val>>8, bal);
  338. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
  339. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
  340. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
  341. muted ? 0x01 : (val | 0x01));
  342. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, bal << 8);
  343. }
  344. static void msp3400c_setbass(struct i2c_client *client, int bass)
  345. {
  346. int val = ((bass-32768) * 0x60 / 65535) << 8;
  347. dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
  348. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
  349. }
  350. static void msp3400c_settreble(struct i2c_client *client, int treble)
  351. {
  352. int val = ((treble-32768) * 0x60 / 65535) << 8;
  353. dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
  354. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
  355. }
  356. static void msp3400c_setmode(struct i2c_client *client, int type)
  357. {
  358. struct msp3400c *msp = i2c_get_clientdata(client);
  359. int i;
  360. dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type);
  361. msp->mode = type;
  362. msp->audmode = V4L2_TUNER_MODE_MONO;
  363. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  364. msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
  365. msp_init_data[type].ad_cv);
  366. for (i = 5; i >= 0; i--) /* fir 1 */
  367. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
  368. msp_init_data[type].fir1[i]);
  369. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
  370. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
  371. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
  372. for (i = 5; i >= 0; i--)
  373. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
  374. msp_init_data[type].fir2[i]);
  375. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
  376. msp_init_data[type].mode_reg);
  377. msp3400c_setcarrier(client, msp_init_data[type].cdo1,
  378. msp_init_data[type].cdo2);
  379. msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
  380. if (dolby) {
  381. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
  382. 0x0520); /* I2S1 */
  383. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
  384. 0x0620); /* I2S2 */
  385. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
  386. msp_init_data[type].dfp_src);
  387. } else {
  388. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
  389. msp_init_data[type].dfp_src);
  390. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
  391. msp_init_data[type].dfp_src);
  392. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
  393. msp_init_data[type].dfp_src);
  394. }
  395. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
  396. msp_init_data[type].dfp_src);
  397. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
  398. msp_init_data[type].dfp_matrix);
  399. if (HAVE_NICAM(msp)) {
  400. /* nicam prescale */
  401. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
  402. }
  403. }
  404. static int best_audio_mode(int rxsubchans)
  405. {
  406. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  407. return V4L2_TUNER_MODE_STEREO;
  408. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  409. return V4L2_TUNER_MODE_LANG1;
  410. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  411. return V4L2_TUNER_MODE_LANG2;
  412. return V4L2_TUNER_MODE_MONO;
  413. }
  414. /* turn on/off nicam + stereo */
  415. static void msp3400c_set_audmode(struct i2c_client *client, int audmode)
  416. {
  417. static char *strmode[16] = {
  418. #if __GNUC__ >= 3
  419. [ 0 ... 15 ] = "invalid",
  420. #endif
  421. [ V4L2_TUNER_MODE_MONO ] = "mono",
  422. [ V4L2_TUNER_MODE_STEREO ] = "stereo",
  423. [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
  424. [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
  425. };
  426. struct msp3400c *msp = i2c_get_clientdata(client);
  427. int nicam=0; /* channel source: FM/AM or nicam */
  428. int src=0;
  429. BUG_ON(msp->opmode == OPMODE_SIMPLER);
  430. msp->audmode = audmode;
  431. /* switch demodulator */
  432. switch (msp->mode) {
  433. case MSP_MODE_FM_TERRA:
  434. dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",
  435. strmode[audmode]);
  436. msp3400c_setcarrier(client,msp->second,msp->main);
  437. switch (audmode) {
  438. case V4L2_TUNER_MODE_STEREO:
  439. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
  440. break;
  441. case V4L2_TUNER_MODE_MONO:
  442. case V4L2_TUNER_MODE_LANG1:
  443. case V4L2_TUNER_MODE_LANG2:
  444. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
  445. break;
  446. }
  447. break;
  448. case MSP_MODE_FM_SAT:
  449. dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",
  450. strmode[audmode]);
  451. switch (audmode) {
  452. case V4L2_TUNER_MODE_MONO:
  453. msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
  454. break;
  455. case V4L2_TUNER_MODE_STEREO:
  456. msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
  457. break;
  458. case V4L2_TUNER_MODE_LANG1:
  459. msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  460. break;
  461. case V4L2_TUNER_MODE_LANG2:
  462. msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  463. break;
  464. }
  465. break;
  466. case MSP_MODE_FM_NICAM1:
  467. case MSP_MODE_FM_NICAM2:
  468. case MSP_MODE_AM_NICAM:
  469. dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",
  470. strmode[audmode]);
  471. msp3400c_setcarrier(client,msp->second,msp->main);
  472. if (msp->nicam_on)
  473. nicam=0x0100;
  474. break;
  475. case MSP_MODE_BTSC:
  476. dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",
  477. strmode[audmode]);
  478. nicam=0x0300;
  479. break;
  480. case MSP_MODE_EXTERN:
  481. dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",
  482. strmode[audmode]);
  483. nicam = 0x0200;
  484. break;
  485. case MSP_MODE_FM_RADIO:
  486. dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",
  487. strmode[audmode]);
  488. break;
  489. default:
  490. dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
  491. return;
  492. }
  493. /* switch audio */
  494. switch (audmode) {
  495. case V4L2_TUNER_MODE_STEREO:
  496. src = 0x0020 | nicam;
  497. break;
  498. case V4L2_TUNER_MODE_MONO:
  499. if (msp->mode == MSP_MODE_AM_NICAM) {
  500. dprintk("msp3400: switching to AM mono\n");
  501. /* AM mono decoding is handled by tuner, not MSP chip */
  502. /* SCART switching control register */
  503. msp3400c_set_scart(client,SCART_MONO,0);
  504. src = 0x0200;
  505. break;
  506. }
  507. case V4L2_TUNER_MODE_LANG1:
  508. src = 0x0000 | nicam;
  509. break;
  510. case V4L2_TUNER_MODE_LANG2:
  511. src = 0x0010 | nicam;
  512. break;
  513. }
  514. dprintk(KERN_DEBUG
  515. "msp3400: setstereo final source/matrix = 0x%x\n", src);
  516. if (dolby) {
  517. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
  518. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
  519. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
  520. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
  521. } else {
  522. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
  523. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
  524. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
  525. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
  526. }
  527. }
  528. static void
  529. msp3400c_print_mode(struct msp3400c *msp)
  530. {
  531. if (msp->main == msp->second) {
  532. printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
  533. msp->main/910000,(msp->main/910)%1000);
  534. } else {
  535. printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
  536. msp->main/910000,(msp->main/910)%1000);
  537. }
  538. if (msp->mode == MSP_MODE_FM_NICAM1 ||
  539. msp->mode == MSP_MODE_FM_NICAM2)
  540. printk(KERN_DEBUG "msp3400: NICAM/FM carrier : %d.%03d MHz\n",
  541. msp->second/910000,(msp->second/910)%1000);
  542. if (msp->mode == MSP_MODE_AM_NICAM)
  543. printk(KERN_DEBUG "msp3400: NICAM/AM carrier : %d.%03d MHz\n",
  544. msp->second/910000,(msp->second/910)%1000);
  545. if (msp->mode == MSP_MODE_FM_TERRA &&
  546. msp->main != msp->second) {
  547. printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
  548. msp->second/910000,(msp->second/910)%1000);
  549. }
  550. }
  551. /* ----------------------------------------------------------------------- */
  552. struct REGISTER_DUMP {
  553. int addr;
  554. char *name;
  555. };
  556. static int
  557. autodetect_stereo(struct i2c_client *client)
  558. {
  559. struct msp3400c *msp = i2c_get_clientdata(client);
  560. int val;
  561. int rxsubchans = msp->rxsubchans;
  562. int newnicam = msp->nicam_on;
  563. int update = 0;
  564. switch (msp->mode) {
  565. case MSP_MODE_FM_TERRA:
  566. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
  567. if (val > 32767)
  568. val -= 65536;
  569. dprintk(KERN_DEBUG
  570. "msp34xx: stereo detect register: %d\n",val);
  571. if (val > 4096) {
  572. rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  573. } else if (val < -4096) {
  574. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  575. } else {
  576. rxsubchans = V4L2_TUNER_SUB_MONO;
  577. }
  578. newnicam = 0;
  579. break;
  580. case MSP_MODE_FM_NICAM1:
  581. case MSP_MODE_FM_NICAM2:
  582. case MSP_MODE_AM_NICAM:
  583. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
  584. dprintk(KERN_DEBUG
  585. "msp34xx: nicam sync=%d, mode=%d\n",
  586. val & 1, (val & 0x1e) >> 1);
  587. if (val & 1) {
  588. /* nicam synced */
  589. switch ((val & 0x1e) >> 1) {
  590. case 0:
  591. case 8:
  592. rxsubchans = V4L2_TUNER_SUB_STEREO;
  593. break;
  594. case 1:
  595. case 9:
  596. rxsubchans = V4L2_TUNER_SUB_MONO
  597. | V4L2_TUNER_SUB_LANG1;
  598. break;
  599. case 2:
  600. case 10:
  601. rxsubchans = V4L2_TUNER_SUB_MONO
  602. | V4L2_TUNER_SUB_LANG1
  603. | V4L2_TUNER_SUB_LANG2;
  604. break;
  605. default:
  606. rxsubchans = V4L2_TUNER_SUB_MONO;
  607. break;
  608. }
  609. newnicam=1;
  610. } else {
  611. newnicam = 0;
  612. rxsubchans = V4L2_TUNER_SUB_MONO;
  613. }
  614. break;
  615. case MSP_MODE_BTSC:
  616. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
  617. dprintk(KERN_DEBUG
  618. "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
  619. val,
  620. (val & 0x0002) ? "no" : "yes",
  621. (val & 0x0004) ? "no" : "yes",
  622. (val & 0x0040) ? "stereo" : "mono",
  623. (val & 0x0080) ? ", nicam 2nd mono" : "",
  624. (val & 0x0100) ? ", bilingual/SAP" : "");
  625. rxsubchans = V4L2_TUNER_SUB_MONO;
  626. if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
  627. if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
  628. break;
  629. }
  630. if (rxsubchans != msp->rxsubchans) {
  631. update = 1;
  632. dprintk(KERN_DEBUG "msp34xx: watch: rxsubchans %d => %d\n",
  633. msp->rxsubchans,rxsubchans);
  634. msp->rxsubchans = rxsubchans;
  635. }
  636. if (newnicam != msp->nicam_on) {
  637. update = 1;
  638. dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
  639. msp->nicam_on,newnicam);
  640. msp->nicam_on = newnicam;
  641. }
  642. return update;
  643. }
  644. /*
  645. * A kernel thread for msp3400 control -- we don't want to block the
  646. * in the ioctl while doing the sound carrier & stereo detect
  647. */
  648. static int msp34xx_sleep(struct msp3400c *msp, int timeout)
  649. {
  650. DECLARE_WAITQUEUE(wait, current);
  651. add_wait_queue(&msp->wq, &wait);
  652. if (!kthread_should_stop()) {
  653. if (timeout < 0) {
  654. set_current_state(TASK_INTERRUPTIBLE);
  655. schedule();
  656. } else {
  657. set_current_state(TASK_INTERRUPTIBLE);
  658. schedule_timeout(msecs_to_jiffies(timeout));
  659. }
  660. }
  661. remove_wait_queue(&msp->wq, &wait);
  662. try_to_freeze();
  663. return msp->restart;
  664. }
  665. /* stereo/multilang monitoring */
  666. static void watch_stereo(struct i2c_client *client)
  667. {
  668. struct msp3400c *msp = i2c_get_clientdata(client);
  669. if (autodetect_stereo(client))
  670. msp3400c_set_audmode(client,best_audio_mode(msp->rxsubchans));
  671. if (once)
  672. msp->watch_stereo = 0;
  673. }
  674. static int msp3400c_thread(void *data)
  675. {
  676. struct i2c_client *client = data;
  677. struct msp3400c *msp = i2c_get_clientdata(client);
  678. struct CARRIER_DETECT *cd;
  679. int count, max1,max2,val1,val2, val,this;
  680. printk("msp3400: kthread started\n");
  681. for (;;) {
  682. d2printk("msp3400: thread: sleep\n");
  683. msp34xx_sleep(msp,-1);
  684. d2printk("msp3400: thread: wakeup\n");
  685. restart:
  686. dprintk("msp3410: thread: restart scan\n");
  687. msp->restart = 0;
  688. if (kthread_should_stop())
  689. break;
  690. if (VIDEO_MODE_RADIO == msp->norm ||
  691. MSP_MODE_EXTERN == msp->mode) {
  692. /* no carrier scan, just unmute */
  693. printk("msp3400: thread: no carrier scan\n");
  694. msp3400c_setvolume(client, msp->muted,
  695. msp->volume, msp->balance);
  696. continue;
  697. }
  698. /* mute */
  699. msp3400c_setvolume(client, msp->muted, 0, 0);
  700. msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
  701. val1 = val2 = 0;
  702. max1 = max2 = -1;
  703. msp->watch_stereo = 0;
  704. /* some time for the tuner to sync */
  705. if (msp34xx_sleep(msp,200))
  706. goto restart;
  707. /* carrier detect pass #1 -- main carrier */
  708. cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
  709. if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  710. /* autodetect doesn't work well with AM ... */
  711. max1 = 3;
  712. count = 0;
  713. dprintk("msp3400: AM sound override\n");
  714. }
  715. for (this = 0; this < count; this++) {
  716. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  717. if (msp34xx_sleep(msp,100))
  718. goto restart;
  719. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  720. if (val > 32767)
  721. val -= 65536;
  722. if (val1 < val)
  723. val1 = val, max1 = this;
  724. dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
  725. }
  726. /* carrier detect pass #2 -- second (stereo) carrier */
  727. switch (max1) {
  728. case 1: /* 5.5 */
  729. cd = carrier_detect_55;
  730. count = CARRIER_COUNT(carrier_detect_55);
  731. break;
  732. case 3: /* 6.5 */
  733. cd = carrier_detect_65;
  734. count = CARRIER_COUNT(carrier_detect_65);
  735. break;
  736. case 0: /* 4.5 */
  737. case 2: /* 6.0 */
  738. default:
  739. cd = NULL; count = 0;
  740. break;
  741. }
  742. if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  743. /* autodetect doesn't work well with AM ... */
  744. cd = NULL; count = 0; max2 = 0;
  745. }
  746. for (this = 0; this < count; this++) {
  747. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  748. if (msp34xx_sleep(msp,100))
  749. goto restart;
  750. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  751. if (val > 32767)
  752. val -= 65536;
  753. if (val2 < val)
  754. val2 = val, max2 = this;
  755. dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
  756. }
  757. /* programm the msp3400 according to the results */
  758. msp->main = carrier_detect_main[max1].cdo;
  759. switch (max1) {
  760. case 1: /* 5.5 */
  761. if (max2 == 0) {
  762. /* B/G FM-stereo */
  763. msp->second = carrier_detect_55[max2].cdo;
  764. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  765. msp->nicam_on = 0;
  766. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  767. msp->watch_stereo = 1;
  768. } else if (max2 == 1 && HAVE_NICAM(msp)) {
  769. /* B/G NICAM */
  770. msp->second = carrier_detect_55[max2].cdo;
  771. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  772. msp->nicam_on = 1;
  773. msp3400c_setcarrier(client, msp->second, msp->main);
  774. msp->watch_stereo = 1;
  775. } else {
  776. goto no_second;
  777. }
  778. break;
  779. case 2: /* 6.0 */
  780. /* PAL I NICAM */
  781. msp->second = MSP_CARRIER(6.552);
  782. msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
  783. msp->nicam_on = 1;
  784. msp3400c_setcarrier(client, msp->second, msp->main);
  785. msp->watch_stereo = 1;
  786. break;
  787. case 3: /* 6.5 */
  788. if (max2 == 1 || max2 == 2) {
  789. /* D/K FM-stereo */
  790. msp->second = carrier_detect_65[max2].cdo;
  791. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  792. msp->nicam_on = 0;
  793. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  794. msp->watch_stereo = 1;
  795. } else if (max2 == 0 &&
  796. msp->norm == VIDEO_MODE_SECAM) {
  797. /* L NICAM or AM-mono */
  798. msp->second = carrier_detect_65[max2].cdo;
  799. msp3400c_setmode(client, MSP_MODE_AM_NICAM);
  800. msp->nicam_on = 0;
  801. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  802. msp3400c_setcarrier(client, msp->second, msp->main);
  803. /* volume prescale for SCART (AM mono input) */
  804. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
  805. msp->watch_stereo = 1;
  806. } else if (max2 == 0 && HAVE_NICAM(msp)) {
  807. /* D/K NICAM */
  808. msp->second = carrier_detect_65[max2].cdo;
  809. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  810. msp->nicam_on = 1;
  811. msp3400c_setcarrier(client, msp->second, msp->main);
  812. msp->watch_stereo = 1;
  813. } else {
  814. goto no_second;
  815. }
  816. break;
  817. case 0: /* 4.5 */
  818. default:
  819. no_second:
  820. msp->second = carrier_detect_main[max1].cdo;
  821. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  822. msp->nicam_on = 0;
  823. msp3400c_setcarrier(client, msp->second, msp->main);
  824. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  825. msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
  826. break;
  827. }
  828. /* unmute */
  829. msp3400c_setvolume(client, msp->muted,
  830. msp->volume, msp->balance);
  831. if (debug)
  832. msp3400c_print_mode(msp);
  833. /* monitor tv audio mode */
  834. while (msp->watch_stereo) {
  835. if (msp34xx_sleep(msp,5000))
  836. goto restart;
  837. watch_stereo(client);
  838. }
  839. }
  840. dprintk(KERN_DEBUG "msp3400: thread: exit\n");
  841. return 0;
  842. }
  843. /* ----------------------------------------------------------------------- */
  844. /* this one uses the automatic sound standard detection of newer */
  845. /* msp34xx chip versions */
  846. static struct MODES {
  847. int retval;
  848. int main, second;
  849. char *name;
  850. } modelist[] = {
  851. { 0x0000, 0, 0, "ERROR" },
  852. { 0x0001, 0, 0, "autodetect start" },
  853. { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
  854. { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
  855. { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
  856. { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
  857. { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
  858. { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
  859. { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
  860. { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
  861. { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
  862. { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
  863. { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
  864. { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
  865. { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
  866. { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
  867. { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
  868. { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
  869. { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
  870. { -1, 0, 0, NULL }, /* EOF */
  871. };
  872. static inline const char *msp34xx_standard_mode_name(int mode)
  873. {
  874. int i;
  875. for (i = 0; modelist[i].name != NULL; i++)
  876. if (modelist[i].retval == mode)
  877. return modelist[i].name;
  878. return "unknown";
  879. }
  880. static int msp34xx_modus(int norm)
  881. {
  882. switch (norm) {
  883. case VIDEO_MODE_PAL:
  884. #if 1
  885. /* experimental: not sure this works with all chip versions */
  886. return 0x7003;
  887. #else
  888. /* previous value, try this if it breaks ... */
  889. return 0x1003;
  890. #endif
  891. case VIDEO_MODE_NTSC: /* BTSC */
  892. return 0x2003;
  893. case VIDEO_MODE_SECAM:
  894. return 0x0003;
  895. case VIDEO_MODE_RADIO:
  896. return 0x0003;
  897. case VIDEO_MODE_AUTO:
  898. return 0x2003;
  899. default:
  900. return 0x0003;
  901. }
  902. }
  903. static int msp34xx_standard(int norm)
  904. {
  905. switch (norm) {
  906. case VIDEO_MODE_PAL:
  907. return 1;
  908. case VIDEO_MODE_NTSC: /* BTSC */
  909. return 0x0020;
  910. case VIDEO_MODE_SECAM:
  911. return 1;
  912. case VIDEO_MODE_RADIO:
  913. return 0x0040;
  914. default:
  915. return 1;
  916. }
  917. }
  918. static int msp3410d_thread(void *data)
  919. {
  920. struct i2c_client *client = data;
  921. struct msp3400c *msp = i2c_get_clientdata(client);
  922. int mode,val,i,std;
  923. printk("msp3410: daemon started\n");
  924. for (;;) {
  925. d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
  926. msp34xx_sleep(msp,-1);
  927. d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
  928. restart:
  929. dprintk("msp3410: thread: restart scan\n");
  930. msp->restart = 0;
  931. if (kthread_should_stop())
  932. break;
  933. if (msp->mode == MSP_MODE_EXTERN) {
  934. /* no carrier scan needed, just unmute */
  935. dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
  936. msp3400c_setvolume(client, msp->muted,
  937. msp->volume, msp->balance);
  938. continue;
  939. }
  940. /* put into sane state (and mute) */
  941. msp3400c_reset(client);
  942. /* some time for the tuner to sync */
  943. if (msp34xx_sleep(msp,200))
  944. goto restart;
  945. /* start autodetect */
  946. mode = msp34xx_modus(msp->norm);
  947. std = msp34xx_standard(msp->norm);
  948. msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
  949. msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
  950. msp->watch_stereo = 0;
  951. if (debug)
  952. printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
  953. msp34xx_standard_mode_name(std) ,std);
  954. if (std != 1) {
  955. /* programmed some specific mode */
  956. val = std;
  957. } else {
  958. /* triggered autodetect */
  959. for (;;) {
  960. if (msp34xx_sleep(msp,100))
  961. goto restart;
  962. /* check results */
  963. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
  964. if (val < 0x07ff)
  965. break;
  966. dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
  967. }
  968. }
  969. for (i = 0; modelist[i].name != NULL; i++)
  970. if (modelist[i].retval == val)
  971. break;
  972. dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
  973. modelist[i].name ? modelist[i].name : "unknown",
  974. val);
  975. msp->main = modelist[i].main;
  976. msp->second = modelist[i].second;
  977. if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
  978. /* autodetection has failed, let backup */
  979. dprintk(KERN_DEBUG "msp3410: autodetection failed,"
  980. " switching to backup mode: %s (0x%04x)\n",
  981. modelist[8].name ? modelist[8].name : "unknown",val);
  982. val = 0x0009;
  983. msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
  984. }
  985. /* set various prescales */
  986. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
  987. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
  988. msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
  989. /* set stereo */
  990. switch (val) {
  991. case 0x0008: /* B/G NICAM */
  992. case 0x000a: /* I NICAM */
  993. if (val == 0x0008)
  994. msp->mode = MSP_MODE_FM_NICAM1;
  995. else
  996. msp->mode = MSP_MODE_FM_NICAM2;
  997. /* just turn on stereo */
  998. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  999. msp->nicam_on = 1;
  1000. msp->watch_stereo = 1;
  1001. msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
  1002. break;
  1003. case 0x0009:
  1004. msp->mode = MSP_MODE_AM_NICAM;
  1005. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  1006. msp->nicam_on = 1;
  1007. msp3400c_set_audmode(client,V4L2_TUNER_MODE_MONO);
  1008. msp->watch_stereo = 1;
  1009. break;
  1010. case 0x0020: /* BTSC */
  1011. /* just turn on stereo */
  1012. msp->mode = MSP_MODE_BTSC;
  1013. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1014. msp->nicam_on = 0;
  1015. msp->watch_stereo = 1;
  1016. msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
  1017. break;
  1018. case 0x0040: /* FM radio */
  1019. msp->mode = MSP_MODE_FM_RADIO;
  1020. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1021. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1022. msp->nicam_on = 0;
  1023. msp->watch_stereo = 0;
  1024. /* not needed in theory if HAVE_RADIO(), but
  1025. short programming enables carrier mute */
  1026. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  1027. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  1028. MSP_CARRIER(10.7));
  1029. /* scart routing */
  1030. msp3400c_set_scart(client,SCART_IN2,0);
  1031. /* msp34xx does radio decoding */
  1032. msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
  1033. msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
  1034. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
  1035. break;
  1036. case 0x0003:
  1037. case 0x0004:
  1038. case 0x0005:
  1039. msp->mode = MSP_MODE_FM_TERRA;
  1040. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  1041. msp->audmode = V4L2_TUNER_MODE_MONO;
  1042. msp->nicam_on = 0;
  1043. msp->watch_stereo = 1;
  1044. break;
  1045. }
  1046. /* unmute, restore misc registers */
  1047. msp3400c_setbass(client, msp->bass);
  1048. msp3400c_settreble(client, msp->treble);
  1049. msp3400c_setvolume(client, msp->muted,
  1050. msp->volume, msp->balance);
  1051. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
  1052. /* monitor tv audio mode */
  1053. while (msp->watch_stereo) {
  1054. if (msp34xx_sleep(msp,5000))
  1055. goto restart;
  1056. watch_stereo(client);
  1057. }
  1058. }
  1059. dprintk(KERN_DEBUG "msp3410: thread: exit\n");
  1060. return 0;
  1061. }
  1062. /* ----------------------------------------------------------------------- */
  1063. /* msp34xxG + (simpler no-thread) */
  1064. /* this one uses both automatic standard detection and automatic sound */
  1065. /* select which are available in the newer G versions */
  1066. /* struct msp: only norm, acb and source are really used in this mode */
  1067. static void msp34xxg_set_source(struct i2c_client *client, int source);
  1068. /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
  1069. * return 0 if it worked, -1 if it failed
  1070. */
  1071. static int msp34xxg_init(struct i2c_client *client)
  1072. {
  1073. struct msp3400c *msp = i2c_get_clientdata(client);
  1074. int modus,std;
  1075. if (msp3400c_reset(client))
  1076. return -1;
  1077. /* make sure that input/output is muted (paranoid mode) */
  1078. if (msp3400c_write(client,
  1079. I2C_MSP3400C_DFP,
  1080. 0x13, /* ACB */
  1081. 0x0f20 /* mute DSP input, mute SCART 1 */))
  1082. return -1;
  1083. /* step-by-step initialisation, as described in the manual */
  1084. modus = msp34xx_modus(msp->norm);
  1085. std = msp34xx_standard(msp->norm);
  1086. modus &= ~0x03; /* STATUS_CHANGE=0 */
  1087. modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
  1088. if (msp3400c_write(client,
  1089. I2C_MSP3400C_DEM,
  1090. 0x30/*MODUS*/,
  1091. modus))
  1092. return -1;
  1093. if (msp3400c_write(client,
  1094. I2C_MSP3400C_DEM,
  1095. 0x20/*stanard*/,
  1096. std))
  1097. return -1;
  1098. /* write the dfps that may have an influence on
  1099. standard/audio autodetection right now */
  1100. msp34xxg_set_source(client, msp->source);
  1101. if (msp3400c_write(client, I2C_MSP3400C_DFP,
  1102. 0x0e, /* AM/FM Prescale */
  1103. 0x3000 /* default: [15:8] 75khz deviation */))
  1104. return -1;
  1105. if (msp3400c_write(client, I2C_MSP3400C_DFP,
  1106. 0x10, /* NICAM Prescale */
  1107. 0x5a00 /* default: 9db gain (as recommended) */))
  1108. return -1;
  1109. if (msp3400c_write(client,
  1110. I2C_MSP3400C_DEM,
  1111. 0x20, /* STANDARD SELECT */
  1112. standard /* default: 0x01 for automatic standard select*/))
  1113. return -1;
  1114. return 0;
  1115. }
  1116. static int msp34xxg_thread(void *data)
  1117. {
  1118. struct i2c_client *client = data;
  1119. struct msp3400c *msp = i2c_get_clientdata(client);
  1120. int val, std, i;
  1121. printk("msp34xxg: daemon started\n");
  1122. msp->source = 1; /* default */
  1123. for (;;) {
  1124. d2printk(KERN_DEBUG "msp34xxg: thread: sleep\n");
  1125. msp34xx_sleep(msp,-1);
  1126. d2printk(KERN_DEBUG "msp34xxg: thread: wakeup\n");
  1127. restart:
  1128. dprintk("msp34xxg: thread: restart scan\n");
  1129. msp->restart = 0;
  1130. if (kthread_should_stop())
  1131. break;
  1132. /* setup the chip*/
  1133. msp34xxg_init(client);
  1134. std = standard;
  1135. if (std != 0x01)
  1136. goto unmute;
  1137. /* watch autodetect */
  1138. dprintk("msp34xxg: triggered autodetect, waiting for result\n");
  1139. for (i = 0; i < 10; i++) {
  1140. if (msp34xx_sleep(msp,100))
  1141. goto restart;
  1142. /* check results */
  1143. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
  1144. if (val < 0x07ff) {
  1145. std = val;
  1146. break;
  1147. }
  1148. dprintk("msp34xxg: detection still in progress\n");
  1149. }
  1150. if (0x01 == std) {
  1151. dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
  1152. continue;
  1153. }
  1154. unmute:
  1155. dprintk("msp34xxg: current mode: %s (0x%04x)\n",
  1156. msp34xx_standard_mode_name(std), std);
  1157. /* unmute: dispatch sound to scart output, set scart volume */
  1158. dprintk("msp34xxg: unmute\n");
  1159. msp3400c_setbass(client, msp->bass);
  1160. msp3400c_settreble(client, msp->treble);
  1161. msp3400c_setvolume(client, msp->muted, msp->volume, msp->balance);
  1162. /* restore ACB */
  1163. if (msp3400c_write(client,
  1164. I2C_MSP3400C_DFP,
  1165. 0x13, /* ACB */
  1166. msp->acb))
  1167. return -1;
  1168. }
  1169. dprintk(KERN_DEBUG "msp34xxg: thread: exit\n");
  1170. return 0;
  1171. }
  1172. /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
  1173. * the value for source is the same as bit 15:8 of DFP registers 0x08,
  1174. * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
  1175. *
  1176. * this function replaces msp3400c_setstereo
  1177. */
  1178. static void msp34xxg_set_source(struct i2c_client *client, int source)
  1179. {
  1180. struct msp3400c *msp = i2c_get_clientdata(client);
  1181. /* fix matrix mode to stereo and let the msp choose what
  1182. * to output according to 'source', as recommended
  1183. * for MONO (source==0) downmixing set bit[7:0] to 0x30
  1184. */
  1185. int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
  1186. dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
  1187. msp3400c_write(client,
  1188. I2C_MSP3400C_DFP,
  1189. 0x08, /* Loudspeaker Output */
  1190. value);
  1191. msp3400c_write(client,
  1192. I2C_MSP3400C_DFP,
  1193. 0x0a, /* SCART1 DA Output */
  1194. value);
  1195. msp3400c_write(client,
  1196. I2C_MSP3400C_DFP,
  1197. 0x0c, /* Quasi-peak detector */
  1198. value);
  1199. /*
  1200. * set identification threshold. Personally, I
  1201. * I set it to a higher value that the default
  1202. * of 0x190 to ignore noisy stereo signals.
  1203. * this needs tuning. (recommended range 0x00a0-0x03c0)
  1204. * 0x7f0 = forced mono mode
  1205. */
  1206. msp3400c_write(client,
  1207. I2C_MSP3400C_DEM,
  1208. 0x22, /* a2 threshold for stereo/bilingual */
  1209. stereo_threshold);
  1210. msp->source=source;
  1211. }
  1212. static void msp34xxg_detect_stereo(struct i2c_client *client)
  1213. {
  1214. struct msp3400c *msp = i2c_get_clientdata(client);
  1215. int status = msp3400c_read(client,
  1216. I2C_MSP3400C_DEM,
  1217. 0x0200 /* STATUS */);
  1218. int is_bilingual = status&0x100;
  1219. int is_stereo = status&0x40;
  1220. msp->rxsubchans = 0;
  1221. if (is_stereo)
  1222. msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
  1223. else
  1224. msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
  1225. if (is_bilingual) {
  1226. msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
  1227. /* I'm supposed to check whether it's SAP or not
  1228. * and set only LANG2/SAP in this case. Yet, the MSP
  1229. * does a lot of work to hide this and handle everything
  1230. * the same way. I don't want to work around it so unless
  1231. * this is a problem, I'll handle SAP just like lang1/lang2.
  1232. */
  1233. }
  1234. dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  1235. status, is_stereo, is_bilingual, msp->rxsubchans);
  1236. }
  1237. static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
  1238. {
  1239. struct msp3400c *msp = i2c_get_clientdata(client);
  1240. int source;
  1241. switch (audmode) {
  1242. case V4L2_TUNER_MODE_MONO:
  1243. source=0; /* mono only */
  1244. break;
  1245. case V4L2_TUNER_MODE_STEREO:
  1246. source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
  1247. /* problem: that could also mean 2 (scart input) */
  1248. break;
  1249. case V4L2_TUNER_MODE_LANG1:
  1250. source=3; /* stereo or A */
  1251. break;
  1252. case V4L2_TUNER_MODE_LANG2:
  1253. source=4; /* stereo or B */
  1254. break;
  1255. default:
  1256. audmode = 0;
  1257. source = 1;
  1258. break;
  1259. }
  1260. msp->audmode = audmode;
  1261. msp34xxg_set_source(client, source);
  1262. }
  1263. /* ----------------------------------------------------------------------- */
  1264. static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
  1265. static int msp_detach(struct i2c_client *client);
  1266. static int msp_probe(struct i2c_adapter *adap);
  1267. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
  1268. static int msp_suspend(struct device * dev, pm_message_t state, u32 level);
  1269. static int msp_resume(struct device * dev, u32 level);
  1270. static void msp_wake_thread(struct i2c_client *client);
  1271. static struct i2c_driver driver = {
  1272. .owner = THIS_MODULE,
  1273. .name = "i2c msp3400 driver",
  1274. .id = I2C_DRIVERID_MSP3400,
  1275. .flags = I2C_DF_NOTIFY,
  1276. .attach_adapter = msp_probe,
  1277. .detach_client = msp_detach,
  1278. .command = msp_command,
  1279. .driver = {
  1280. .suspend = msp_suspend,
  1281. .resume = msp_resume,
  1282. },
  1283. };
  1284. static struct i2c_client client_template =
  1285. {
  1286. .name = "(unset)",
  1287. .flags = I2C_CLIENT_ALLOW_USE,
  1288. .driver = &driver,
  1289. };
  1290. static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
  1291. {
  1292. struct msp3400c *msp;
  1293. struct i2c_client *c;
  1294. int (*thread_func)(void *data) = NULL;
  1295. client_template.adapter = adap;
  1296. client_template.addr = addr;
  1297. if (-1 == msp3400c_reset(&client_template)) {
  1298. dprintk("msp34xx: no chip found\n");
  1299. return -1;
  1300. }
  1301. if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
  1302. return -ENOMEM;
  1303. memcpy(c,&client_template,sizeof(struct i2c_client));
  1304. if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
  1305. kfree(c);
  1306. return -ENOMEM;
  1307. }
  1308. memset(msp,0,sizeof(struct msp3400c));
  1309. msp->volume = 58880; /* 0db gain */
  1310. msp->balance = 32768;
  1311. msp->bass = 32768;
  1312. msp->treble = 32768;
  1313. msp->input = -1;
  1314. msp->muted = 1;
  1315. i2c_set_clientdata(c, msp);
  1316. init_waitqueue_head(&msp->wq);
  1317. if (-1 == msp3400c_reset(c)) {
  1318. kfree(msp);
  1319. kfree(c);
  1320. dprintk("msp34xx: no chip found\n");
  1321. return -1;
  1322. }
  1323. msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
  1324. if (-1 != msp->rev1)
  1325. msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
  1326. if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
  1327. kfree(msp);
  1328. kfree(c);
  1329. dprintk("msp34xx: error while reading chip version\n");
  1330. return -1;
  1331. }
  1332. msp3400c_setvolume(c, msp->muted, msp->volume, msp->balance);
  1333. snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
  1334. (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
  1335. ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
  1336. msp->opmode = opmode;
  1337. if (OPMODE_AUTO == msp->opmode) {
  1338. if (HAVE_SIMPLER(msp))
  1339. msp->opmode = OPMODE_SIMPLER;
  1340. else if (HAVE_SIMPLE(msp))
  1341. msp->opmode = OPMODE_SIMPLE;
  1342. else
  1343. msp->opmode = OPMODE_MANUAL;
  1344. }
  1345. /* hello world :-) */
  1346. printk(KERN_INFO "msp34xx: init: chip=%s", c->name);
  1347. if (HAVE_NICAM(msp))
  1348. printk(" +nicam");
  1349. if (HAVE_SIMPLE(msp))
  1350. printk(" +simple");
  1351. if (HAVE_SIMPLER(msp))
  1352. printk(" +simpler");
  1353. if (HAVE_RADIO(msp))
  1354. printk(" +radio");
  1355. /* version-specific initialization */
  1356. switch (msp->opmode) {
  1357. case OPMODE_MANUAL:
  1358. printk(" mode=manual");
  1359. thread_func = msp3400c_thread;
  1360. break;
  1361. case OPMODE_SIMPLE:
  1362. printk(" mode=simple");
  1363. thread_func = msp3410d_thread;
  1364. break;
  1365. case OPMODE_SIMPLER:
  1366. printk(" mode=simpler");
  1367. thread_func = msp34xxg_thread;
  1368. break;
  1369. }
  1370. printk("\n");
  1371. /* startup control thread if needed */
  1372. if (thread_func) {
  1373. msp->kthread = kthread_run(thread_func, c, "msp34xx");
  1374. if (NULL == msp->kthread)
  1375. printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
  1376. msp_wake_thread(c);
  1377. }
  1378. /* done */
  1379. i2c_attach_client(c);
  1380. return 0;
  1381. }
  1382. static int msp_detach(struct i2c_client *client)
  1383. {
  1384. struct msp3400c *msp = i2c_get_clientdata(client);
  1385. /* shutdown control thread */
  1386. if (msp->kthread >= 0) {
  1387. msp->restart = 1;
  1388. kthread_stop(msp->kthread);
  1389. }
  1390. msp3400c_reset(client);
  1391. i2c_detach_client(client);
  1392. kfree(msp);
  1393. kfree(client);
  1394. return 0;
  1395. }
  1396. static int msp_probe(struct i2c_adapter *adap)
  1397. {
  1398. if (adap->class & I2C_CLASS_TV_ANALOG)
  1399. return i2c_probe(adap, &addr_data, msp_attach);
  1400. return 0;
  1401. }
  1402. static void msp_wake_thread(struct i2c_client *client)
  1403. {
  1404. struct msp3400c *msp = i2c_get_clientdata(client);
  1405. if (NULL == msp->kthread)
  1406. return;
  1407. msp3400c_setvolume(client,msp->muted,0,0);
  1408. msp->watch_stereo = 0;
  1409. msp->restart = 1;
  1410. wake_up_interruptible(&msp->wq);
  1411. }
  1412. /* ----------------------------------------------------------------------- */
  1413. static int mode_v4l2_to_v4l1(int rxsubchans)
  1414. {
  1415. int mode = 0;
  1416. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  1417. mode |= VIDEO_SOUND_STEREO;
  1418. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  1419. mode |= VIDEO_SOUND_LANG2;
  1420. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  1421. mode |= VIDEO_SOUND_LANG1;
  1422. if (0 == mode)
  1423. mode |= VIDEO_SOUND_MONO;
  1424. return mode;
  1425. }
  1426. static int mode_v4l1_to_v4l2(int mode)
  1427. {
  1428. if (mode & VIDEO_SOUND_STEREO)
  1429. return V4L2_TUNER_MODE_STEREO;
  1430. if (mode & VIDEO_SOUND_LANG2)
  1431. return V4L2_TUNER_MODE_LANG2;
  1432. if (mode & VIDEO_SOUND_LANG1)
  1433. return V4L2_TUNER_MODE_LANG1;
  1434. return V4L2_TUNER_MODE_MONO;
  1435. }
  1436. static void msp_any_detect_stereo(struct i2c_client *client)
  1437. {
  1438. struct msp3400c *msp = i2c_get_clientdata(client);
  1439. switch (msp->opmode) {
  1440. case OPMODE_MANUAL:
  1441. case OPMODE_SIMPLE:
  1442. autodetect_stereo(client);
  1443. break;
  1444. case OPMODE_SIMPLER:
  1445. msp34xxg_detect_stereo(client);
  1446. break;
  1447. }
  1448. }
  1449. static void msp_any_set_audmode(struct i2c_client *client, int audmode)
  1450. {
  1451. struct msp3400c *msp = i2c_get_clientdata(client);
  1452. switch (msp->opmode) {
  1453. case OPMODE_MANUAL:
  1454. case OPMODE_SIMPLE:
  1455. msp->watch_stereo = 0;
  1456. msp3400c_set_audmode(client, audmode);
  1457. break;
  1458. case OPMODE_SIMPLER:
  1459. msp34xxg_set_audmode(client, audmode);
  1460. break;
  1461. }
  1462. }
  1463. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  1464. {
  1465. struct msp3400c *msp = i2c_get_clientdata(client);
  1466. __u16 *sarg = arg;
  1467. int scart = 0;
  1468. switch (cmd) {
  1469. case AUDC_SET_INPUT:
  1470. dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
  1471. if (*sarg == msp->input)
  1472. break;
  1473. msp->input = *sarg;
  1474. switch (*sarg) {
  1475. case AUDIO_RADIO:
  1476. /* Hauppauge uses IN2 for the radio */
  1477. msp->mode = MSP_MODE_FM_RADIO;
  1478. scart = SCART_IN2;
  1479. break;
  1480. case AUDIO_EXTERN_1:
  1481. /* IN1 is often used for external input ... */
  1482. msp->mode = MSP_MODE_EXTERN;
  1483. scart = SCART_IN1;
  1484. break;
  1485. case AUDIO_EXTERN_2:
  1486. /* ... sometimes it is IN2 through ;) */
  1487. msp->mode = MSP_MODE_EXTERN;
  1488. scart = SCART_IN2;
  1489. break;
  1490. case AUDIO_TUNER:
  1491. msp->mode = -1;
  1492. break;
  1493. default:
  1494. if (*sarg & AUDIO_MUTE)
  1495. msp3400c_set_scart(client,SCART_MUTE,0);
  1496. break;
  1497. }
  1498. if (scart) {
  1499. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1500. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1501. msp3400c_set_scart(client,scart,0);
  1502. msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
  1503. if (msp->opmode != OPMODE_SIMPLER)
  1504. msp3400c_set_audmode(client, msp->audmode);
  1505. }
  1506. msp_wake_thread(client);
  1507. break;
  1508. case AUDC_SET_RADIO:
  1509. dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
  1510. msp->norm = VIDEO_MODE_RADIO;
  1511. dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
  1512. msp->watch_stereo = 0;
  1513. switch (msp->opmode) {
  1514. case OPMODE_MANUAL:
  1515. /* set msp3400 to FM radio mode */
  1516. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  1517. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  1518. MSP_CARRIER(10.7));
  1519. msp3400c_setvolume(client, msp->muted,
  1520. msp->volume, msp->balance);
  1521. break;
  1522. case OPMODE_SIMPLE:
  1523. case OPMODE_SIMPLER:
  1524. /* the thread will do for us */
  1525. msp_wake_thread(client);
  1526. break;
  1527. }
  1528. break;
  1529. /* --- v4l ioctls --- */
  1530. /* take care: bttv does userspace copying, we'll get a
  1531. kernel pointer here... */
  1532. case VIDIOCGAUDIO:
  1533. {
  1534. struct video_audio *va = arg;
  1535. dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
  1536. va->flags |= VIDEO_AUDIO_VOLUME |
  1537. VIDEO_AUDIO_BASS |
  1538. VIDEO_AUDIO_TREBLE |
  1539. VIDEO_AUDIO_MUTABLE;
  1540. if (msp->muted)
  1541. va->flags |= VIDEO_AUDIO_MUTE;
  1542. va->volume = msp->volume;
  1543. va->balance = (va->volume) ? msp->balance : 32768;
  1544. va->bass = msp->bass;
  1545. va->treble = msp->treble;
  1546. msp_any_detect_stereo(client);
  1547. va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
  1548. break;
  1549. }
  1550. case VIDIOCSAUDIO:
  1551. {
  1552. struct video_audio *va = arg;
  1553. dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
  1554. msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
  1555. msp->volume = va->volume;
  1556. msp->balance = va->balance;
  1557. msp->bass = va->bass;
  1558. msp->treble = va->treble;
  1559. msp3400c_setvolume(client, msp->muted,
  1560. msp->volume, msp->balance);
  1561. msp3400c_setbass(client,msp->bass);
  1562. msp3400c_settreble(client,msp->treble);
  1563. if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
  1564. msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
  1565. break;
  1566. }
  1567. case VIDIOCSCHAN:
  1568. {
  1569. struct video_channel *vc = arg;
  1570. dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
  1571. msp->norm = vc->norm;
  1572. msp_wake_thread(client);
  1573. break;
  1574. }
  1575. case VIDIOCSFREQ:
  1576. case VIDIOC_S_FREQUENCY:
  1577. {
  1578. /* new channel -- kick audio carrier scan */
  1579. dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
  1580. msp_wake_thread(client);
  1581. break;
  1582. }
  1583. /* --- v4l2 ioctls --- */
  1584. case VIDIOC_G_TUNER:
  1585. {
  1586. struct v4l2_tuner *vt = arg;
  1587. msp_any_detect_stereo(client);
  1588. vt->audmode = msp->audmode;
  1589. vt->rxsubchans = msp->rxsubchans;
  1590. vt->capability = V4L2_TUNER_CAP_STEREO |
  1591. V4L2_TUNER_CAP_LANG1|
  1592. V4L2_TUNER_CAP_LANG2;
  1593. break;
  1594. }
  1595. case VIDIOC_S_TUNER:
  1596. {
  1597. struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
  1598. /* only set audmode */
  1599. if (vt->audmode != -1 && vt->audmode != 0)
  1600. msp_any_set_audmode(client, vt->audmode);
  1601. break;
  1602. }
  1603. /* msp34xx specific */
  1604. case MSP_SET_MATRIX:
  1605. {
  1606. struct msp_matrix *mspm = arg;
  1607. dprintk(KERN_DEBUG "msp34xx: MSP_SET_MATRIX\n");
  1608. msp3400c_set_scart(client, mspm->input, mspm->output);
  1609. break;
  1610. }
  1611. default:
  1612. /* nothing */
  1613. break;
  1614. }
  1615. return 0;
  1616. }
  1617. static int msp_suspend(struct device * dev, pm_message_t state, u32 level)
  1618. {
  1619. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  1620. dprintk("msp34xx: suspend\n");
  1621. msp3400c_reset(c);
  1622. return 0;
  1623. }
  1624. static int msp_resume(struct device * dev, u32 level)
  1625. {
  1626. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  1627. dprintk("msp34xx: resume\n");
  1628. msp_wake_thread(c);
  1629. return 0;
  1630. }
  1631. /* ----------------------------------------------------------------------- */
  1632. static int __init msp3400_init_module(void)
  1633. {
  1634. return i2c_add_driver(&driver);
  1635. }
  1636. static void __exit msp3400_cleanup_module(void)
  1637. {
  1638. i2c_del_driver(&driver);
  1639. }
  1640. module_init(msp3400_init_module);
  1641. module_exit(msp3400_cleanup_module);
  1642. /*
  1643. * Overrides for Emacs so that we follow Linus's tabbing style.
  1644. * ---------------------------------------------------------------------------
  1645. * Local variables:
  1646. * c-basic-offset: 8
  1647. * End:
  1648. */