msp3400.c 52 KB

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