msp3400.c 52 KB

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