msp3400.c 55 KB

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