msp3400.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  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. #define MSP3400_MAX 4
  577. static struct i2c_client *msps[MSP3400_MAX];
  578. static void msp3400c_restore_dfp(struct i2c_client *client)
  579. {
  580. struct msp3400c *msp = i2c_get_clientdata(client);
  581. int i;
  582. for (i = 0; i < DFP_COUNT; i++) {
  583. if (-1 == msp->dfp_regs[i])
  584. continue;
  585. msp3400c_write(client, I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
  586. }
  587. }
  588. /* if the dfp_regs is set, set what's in there. Otherwise, set the default value */
  589. static int msp3400c_write_dfp_with_default(struct i2c_client *client,
  590. int addr, int default_value)
  591. {
  592. struct msp3400c *msp = i2c_get_clientdata(client);
  593. int value = default_value;
  594. if (addr < DFP_COUNT && -1 != msp->dfp_regs[addr])
  595. value = msp->dfp_regs[addr];
  596. return msp3400c_write(client, I2C_MSP3400C_DFP, addr, value);
  597. }
  598. /* ----------------------------------------------------------------------- */
  599. struct REGISTER_DUMP {
  600. int addr;
  601. char *name;
  602. };
  603. struct REGISTER_DUMP d1[] = {
  604. {0x007e, "autodetect"},
  605. {0x0023, "C_AD_BITS "},
  606. {0x0038, "ADD_BITS "},
  607. {0x003e, "CIB_BITS "},
  608. {0x0057, "ERROR_RATE"},
  609. };
  610. static int autodetect_stereo(struct i2c_client *client)
  611. {
  612. struct msp3400c *msp = i2c_get_clientdata(client);
  613. int val;
  614. int rxsubchans = msp->rxsubchans;
  615. int newnicam = msp->nicam_on;
  616. int update = 0;
  617. switch (msp->mode) {
  618. case MSP_MODE_FM_TERRA:
  619. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
  620. if (val > 32767)
  621. val -= 65536;
  622. dprintk("msp34xx: stereo detect register: %d\n",val);
  623. if (val > 4096) {
  624. rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
  625. } else if (val < -4096) {
  626. rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
  627. } else {
  628. rxsubchans = V4L2_TUNER_SUB_MONO;
  629. }
  630. newnicam = 0;
  631. break;
  632. case MSP_MODE_FM_NICAM1:
  633. case MSP_MODE_FM_NICAM2:
  634. case MSP_MODE_AM_NICAM:
  635. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
  636. dprintk("msp34xx: nicam sync=%d, mode=%d\n",
  637. val & 1, (val & 0x1e) >> 1);
  638. if (val & 1) {
  639. /* nicam synced */
  640. switch ((val & 0x1e) >> 1) {
  641. case 0:
  642. case 8:
  643. rxsubchans = V4L2_TUNER_SUB_STEREO;
  644. break;
  645. case 1:
  646. case 9:
  647. rxsubchans = V4L2_TUNER_SUB_MONO
  648. | V4L2_TUNER_SUB_LANG1;
  649. break;
  650. case 2:
  651. case 10:
  652. rxsubchans = V4L2_TUNER_SUB_MONO
  653. | V4L2_TUNER_SUB_LANG1
  654. | V4L2_TUNER_SUB_LANG2;
  655. break;
  656. default:
  657. rxsubchans = V4L2_TUNER_SUB_MONO;
  658. break;
  659. }
  660. newnicam=1;
  661. } else {
  662. newnicam = 0;
  663. rxsubchans = V4L2_TUNER_SUB_MONO;
  664. }
  665. break;
  666. case MSP_MODE_BTSC:
  667. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
  668. dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
  669. val,
  670. (val & 0x0002) ? "no" : "yes",
  671. (val & 0x0004) ? "no" : "yes",
  672. (val & 0x0040) ? "stereo" : "mono",
  673. (val & 0x0080) ? ", nicam 2nd mono" : "",
  674. (val & 0x0100) ? ", bilingual/SAP" : "");
  675. rxsubchans = V4L2_TUNER_SUB_MONO;
  676. if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
  677. if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
  678. break;
  679. }
  680. if (rxsubchans != msp->rxsubchans) {
  681. update = 1;
  682. dprintk("msp34xx: watch: rxsubchans %d => %d\n",
  683. msp->rxsubchans,rxsubchans);
  684. msp->rxsubchans = rxsubchans;
  685. }
  686. if (newnicam != msp->nicam_on) {
  687. update = 1;
  688. dprintk("msp34xx: watch: nicam %d => %d\n",
  689. msp->nicam_on,newnicam);
  690. msp->nicam_on = newnicam;
  691. }
  692. return update;
  693. }
  694. /*
  695. * A kernel thread for msp3400 control -- we don't want to block the
  696. * in the ioctl while doing the sound carrier & stereo detect
  697. */
  698. static int msp34xx_sleep(struct msp3400c *msp, int timeout)
  699. {
  700. DECLARE_WAITQUEUE(wait, current);
  701. add_wait_queue(&msp->wq, &wait);
  702. if (!kthread_should_stop()) {
  703. if (timeout < 0) {
  704. set_current_state(TASK_INTERRUPTIBLE);
  705. schedule();
  706. } else {
  707. schedule_timeout_interruptible
  708. (msecs_to_jiffies(timeout));
  709. }
  710. }
  711. remove_wait_queue(&msp->wq, &wait);
  712. try_to_freeze();
  713. return msp->restart;
  714. }
  715. /* stereo/multilang monitoring */
  716. static void watch_stereo(struct i2c_client *client)
  717. {
  718. struct msp3400c *msp = i2c_get_clientdata(client);
  719. if (autodetect_stereo(client)) {
  720. if (msp->stereo & V4L2_TUNER_MODE_STEREO)
  721. msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
  722. else if (msp->stereo & VIDEO_SOUND_LANG1)
  723. msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
  724. else
  725. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  726. }
  727. if (once)
  728. msp->watch_stereo = 0;
  729. }
  730. static int msp3400c_thread(void *data)
  731. {
  732. struct i2c_client *client = data;
  733. struct msp3400c *msp = i2c_get_clientdata(client);
  734. struct CARRIER_DETECT *cd;
  735. int count, max1,max2,val1,val2, val,this;
  736. printk("msp3400: kthread started\n");
  737. for (;;) {
  738. d2printk("msp3400: thread: sleep\n");
  739. msp34xx_sleep(msp,-1);
  740. d2printk("msp3400: thread: wakeup\n");
  741. restart:
  742. dprintk("msp3410: thread: restart scan\n");
  743. msp->restart = 0;
  744. if (kthread_should_stop())
  745. break;
  746. if (VIDEO_MODE_RADIO == msp->norm ||
  747. MSP_MODE_EXTERN == msp->mode) {
  748. /* no carrier scan, just unmute */
  749. printk("msp3400: thread: no carrier scan\n");
  750. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  751. continue;
  752. }
  753. /* mute */
  754. msp3400c_setvolume(client, msp->muted, 0, 0);
  755. msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
  756. val1 = val2 = 0;
  757. max1 = max2 = -1;
  758. msp->watch_stereo = 0;
  759. /* some time for the tuner to sync */
  760. if (msp34xx_sleep(msp,200))
  761. goto restart;
  762. /* carrier detect pass #1 -- main carrier */
  763. cd = carrier_detect_main;
  764. count = CARRIER_COUNT(carrier_detect_main);
  765. if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  766. /* autodetect doesn't work well with AM ... */
  767. max1 = 3;
  768. count = 0;
  769. dprintk("msp3400: AM sound override\n");
  770. }
  771. for (this = 0; this < count; this++) {
  772. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  773. if (msp34xx_sleep(msp,100))
  774. goto restart;
  775. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  776. if (val > 32767)
  777. val -= 65536;
  778. if (val1 < val)
  779. val1 = val, max1 = this;
  780. dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
  781. }
  782. /* carrier detect pass #2 -- second (stereo) carrier */
  783. switch (max1) {
  784. case 1: /* 5.5 */
  785. cd = carrier_detect_55;
  786. count = CARRIER_COUNT(carrier_detect_55);
  787. break;
  788. case 3: /* 6.5 */
  789. cd = carrier_detect_65;
  790. count = CARRIER_COUNT(carrier_detect_65);
  791. break;
  792. case 0: /* 4.5 */
  793. case 2: /* 6.0 */
  794. default:
  795. cd = NULL;
  796. count = 0;
  797. break;
  798. }
  799. if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  800. /* autodetect doesn't work well with AM ... */
  801. cd = NULL;
  802. count = 0;
  803. max2 = 0;
  804. }
  805. for (this = 0; this < count; this++) {
  806. msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  807. if (msp34xx_sleep(msp,100))
  808. goto restart;
  809. val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  810. if (val > 32767)
  811. val -= 65536;
  812. if (val2 < val)
  813. val2 = val, max2 = this;
  814. dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
  815. }
  816. /* programm the msp3400 according to the results */
  817. msp->main = carrier_detect_main[max1].cdo;
  818. switch (max1) {
  819. case 1: /* 5.5 */
  820. if (max2 == 0) {
  821. /* B/G FM-stereo */
  822. msp->second = carrier_detect_55[max2].cdo;
  823. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  824. msp->nicam_on = 0;
  825. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  826. msp->watch_stereo = 1;
  827. } else if (max2 == 1 && HAVE_NICAM(msp)) {
  828. /* B/G NICAM */
  829. msp->second = carrier_detect_55[max2].cdo;
  830. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  831. msp->nicam_on = 1;
  832. msp3400c_setcarrier(client, msp->second, msp->main);
  833. msp->watch_stereo = 1;
  834. } else {
  835. goto no_second;
  836. }
  837. break;
  838. case 2: /* 6.0 */
  839. /* PAL I NICAM */
  840. msp->second = MSP_CARRIER(6.552);
  841. msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
  842. msp->nicam_on = 1;
  843. msp3400c_setcarrier(client, msp->second, msp->main);
  844. msp->watch_stereo = 1;
  845. break;
  846. case 3: /* 6.5 */
  847. if (max2 == 1 || max2 == 2) {
  848. /* D/K FM-stereo */
  849. msp->second = carrier_detect_65[max2].cdo;
  850. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  851. msp->nicam_on = 0;
  852. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  853. msp->watch_stereo = 1;
  854. } else if (max2 == 0 &&
  855. msp->norm == VIDEO_MODE_SECAM) {
  856. /* L NICAM or AM-mono */
  857. msp->second = carrier_detect_65[max2].cdo;
  858. msp3400c_setmode(client, MSP_MODE_AM_NICAM);
  859. msp->nicam_on = 0;
  860. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  861. msp3400c_setcarrier(client, msp->second, msp->main);
  862. /* volume prescale for SCART (AM mono input) */
  863. msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
  864. msp->watch_stereo = 1;
  865. } else if (max2 == 0 && HAVE_NICAM(msp)) {
  866. /* D/K NICAM */
  867. msp->second = carrier_detect_65[max2].cdo;
  868. msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  869. msp->nicam_on = 1;
  870. msp3400c_setcarrier(client, msp->second, msp->main);
  871. msp->watch_stereo = 1;
  872. } else {
  873. goto no_second;
  874. }
  875. break;
  876. case 0: /* 4.5 */
  877. default:
  878. no_second:
  879. msp->second = carrier_detect_main[max1].cdo;
  880. msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  881. msp->nicam_on = 0;
  882. msp3400c_setcarrier(client, msp->second, msp->main);
  883. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  884. msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
  885. break;
  886. }
  887. /* unmute */
  888. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  889. msp3400c_restore_dfp(client);
  890. if (debug)
  891. msp3400c_print_mode(msp);
  892. /* monitor tv audio mode */
  893. while (msp->watch_stereo) {
  894. if (msp34xx_sleep(msp,5000))
  895. goto restart;
  896. watch_stereo(client);
  897. }
  898. }
  899. dprintk("msp3400: thread: exit\n");
  900. return 0;
  901. }
  902. /* ----------------------------------------------------------------------- */
  903. /* this one uses the automatic sound standard detection of newer */
  904. /* msp34xx chip versions */
  905. static struct MODES {
  906. int retval;
  907. int main, second;
  908. char *name;
  909. } modelist[] = {
  910. { 0x0000, 0, 0, "ERROR" },
  911. { 0x0001, 0, 0, "autodetect start" },
  912. { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
  913. { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
  914. { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
  915. { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
  916. { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
  917. { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
  918. { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
  919. { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
  920. { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
  921. { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
  922. { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
  923. { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
  924. { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
  925. { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
  926. { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
  927. { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
  928. { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
  929. { -1, 0, 0, NULL }, /* EOF */
  930. };
  931. static inline const char *msp34xx_standard_mode_name(int mode)
  932. {
  933. int i;
  934. for (i = 0; modelist[i].name != NULL; i++)
  935. if (modelist[i].retval == mode)
  936. return modelist[i].name;
  937. return "unknown";
  938. }
  939. static int msp34xx_modus(int norm)
  940. {
  941. switch (norm) {
  942. case VIDEO_MODE_PAL:
  943. dprintk("msp34xx: video mode selected to PAL\n");
  944. #if 1
  945. /* experimental: not sure this works with all chip versions */
  946. return 0x7003;
  947. #else
  948. /* previous value, try this if it breaks ... */
  949. return 0x1003;
  950. #endif
  951. case VIDEO_MODE_NTSC: /* BTSC */
  952. dprintk("msp34xx: video mode selected to NTSC\n");
  953. return 0x2003;
  954. case VIDEO_MODE_SECAM:
  955. dprintk("msp34xx: video mode selected to SECAM\n");
  956. return 0x0003;
  957. case VIDEO_MODE_RADIO:
  958. dprintk("msp34xx: video mode selected to Radio\n");
  959. return 0x0003;
  960. case VIDEO_MODE_AUTO:
  961. dprintk("msp34xx: video mode selected to Auto\n");
  962. return 0x2003;
  963. default:
  964. return 0x0003;
  965. }
  966. }
  967. static int msp34xx_standard(int norm)
  968. {
  969. switch (norm) {
  970. case VIDEO_MODE_PAL:
  971. return 1;
  972. case VIDEO_MODE_NTSC: /* BTSC */
  973. return 0x0020;
  974. case VIDEO_MODE_SECAM:
  975. return 1;
  976. case VIDEO_MODE_RADIO:
  977. return 0x0040;
  978. default:
  979. return 1;
  980. }
  981. }
  982. static int msp3410d_thread(void *data)
  983. {
  984. struct i2c_client *client = data;
  985. struct msp3400c *msp = i2c_get_clientdata(client);
  986. int mode,val,i,std;
  987. printk("msp3410: daemon started\n");
  988. for (;;) {
  989. d2printk("msp3410: thread: sleep\n");
  990. msp34xx_sleep(msp,-1);
  991. d2printk("msp3410: thread: wakeup\n");
  992. restart:
  993. dprintk("msp3410: thread: restart scan\n");
  994. msp->restart = 0;
  995. if (kthread_should_stop())
  996. break;
  997. if (msp->mode == MSP_MODE_EXTERN) {
  998. /* no carrier scan needed, just unmute */
  999. dprintk("msp3410: thread: no carrier scan\n");
  1000. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  1001. continue;
  1002. }
  1003. /* put into sane state (and mute) */
  1004. msp3400c_reset(client);
  1005. /* some time for the tuner to sync */
  1006. if (msp34xx_sleep(msp,200))
  1007. goto restart;
  1008. /* start autodetect */
  1009. mode = msp34xx_modus(msp->norm);
  1010. std = msp34xx_standard(msp->norm);
  1011. msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
  1012. msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
  1013. msp->watch_stereo = 0;
  1014. if (debug)
  1015. dprintk("msp3410: setting mode: %s (0x%04x)\n",
  1016. msp34xx_standard_mode_name(std) ,std);
  1017. if (std != 1) {
  1018. /* programmed some specific mode */
  1019. val = std;
  1020. } else {
  1021. /* triggered autodetect */
  1022. for (;;) {
  1023. if (msp34xx_sleep(msp,100))
  1024. goto restart;
  1025. /* check results */
  1026. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
  1027. if (val < 0x07ff)
  1028. break;
  1029. dprintk("msp3410: detection still in progress\n");
  1030. }
  1031. }
  1032. for (i = 0; modelist[i].name != NULL; i++)
  1033. if (modelist[i].retval == val)
  1034. break;
  1035. dprintk("msp3410: current mode: %s (0x%04x)\n",
  1036. modelist[i].name ? modelist[i].name : "unknown",
  1037. val);
  1038. msp->main = modelist[i].main;
  1039. msp->second = modelist[i].second;
  1040. if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
  1041. /* autodetection has failed, let backup */
  1042. dprintk("msp3410: autodetection failed,"
  1043. " switching to backup mode: %s (0x%04x)\n",
  1044. modelist[8].name ? modelist[8].name : "unknown",val);
  1045. val = 0x0009;
  1046. msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
  1047. }
  1048. /* set various prescales */
  1049. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
  1050. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
  1051. msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
  1052. /* set stereo */
  1053. switch (val) {
  1054. case 0x0008: /* B/G NICAM */
  1055. case 0x000a: /* I NICAM */
  1056. if (val == 0x0008)
  1057. msp->mode = MSP_MODE_FM_NICAM1;
  1058. else
  1059. msp->mode = MSP_MODE_FM_NICAM2;
  1060. /* just turn on stereo */
  1061. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1062. msp->nicam_on = 1;
  1063. msp->watch_stereo = 1;
  1064. msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
  1065. break;
  1066. case 0x0009:
  1067. msp->mode = MSP_MODE_AM_NICAM;
  1068. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  1069. msp->nicam_on = 1;
  1070. msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
  1071. msp->watch_stereo = 1;
  1072. break;
  1073. case 0x0020: /* BTSC */
  1074. /* just turn on stereo */
  1075. msp->mode = MSP_MODE_BTSC;
  1076. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1077. msp->nicam_on = 0;
  1078. msp->watch_stereo = 1;
  1079. msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
  1080. break;
  1081. case 0x0040: /* FM radio */
  1082. msp->mode = MSP_MODE_FM_RADIO;
  1083. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1084. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1085. msp->nicam_on = 0;
  1086. msp->watch_stereo = 0;
  1087. /* not needed in theory if HAVE_RADIO(), but
  1088. short programming enables carrier mute */
  1089. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  1090. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  1091. MSP_CARRIER(10.7));
  1092. /* scart routing */
  1093. msp3400c_set_scart(client,SCART_IN2,0);
  1094. /* msp34xx does radio decoding */
  1095. msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
  1096. msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
  1097. msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
  1098. break;
  1099. case 0x0003:
  1100. case 0x0004:
  1101. case 0x0005:
  1102. msp->mode = MSP_MODE_FM_TERRA;
  1103. msp->rxsubchans = V4L2_TUNER_SUB_MONO;
  1104. msp->audmode = V4L2_TUNER_MODE_MONO;
  1105. msp->nicam_on = 0;
  1106. msp->watch_stereo = 1;
  1107. break;
  1108. }
  1109. /* unmute, restore misc registers */
  1110. msp3400c_setbass(client, msp->bass);
  1111. msp3400c_settreble(client, msp->treble);
  1112. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  1113. msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
  1114. msp3400c_restore_dfp(client);
  1115. /* monitor tv audio mode */
  1116. while (msp->watch_stereo) {
  1117. if (msp34xx_sleep(msp,5000))
  1118. goto restart;
  1119. watch_stereo(client);
  1120. }
  1121. }
  1122. dprintk("msp3410: thread: exit\n");
  1123. return 0;
  1124. }
  1125. /* ----------------------------------------------------------------------- */
  1126. /* msp34xxG + (simpler no-thread) */
  1127. /* this one uses both automatic standard detection and automatic sound */
  1128. /* select which are available in the newer G versions */
  1129. /* struct msp: only norm, acb and source are really used in this mode */
  1130. static void msp34xxg_set_source(struct i2c_client *client, int source);
  1131. /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
  1132. * return 0 if it worked, -1 if it failed
  1133. */
  1134. static int msp34xxg_reset(struct i2c_client *client)
  1135. {
  1136. struct msp3400c *msp = i2c_get_clientdata(client);
  1137. int modus,std;
  1138. if (msp3400c_reset(client))
  1139. return -1;
  1140. /* make sure that input/output is muted (paranoid mode) */
  1141. if (msp3400c_write(client,
  1142. I2C_MSP3400C_DFP,
  1143. 0x13, /* ACB */
  1144. 0x0f20 /* mute DSP input, mute SCART 1 */))
  1145. return -1;
  1146. /* step-by-step initialisation, as described in the manual */
  1147. modus = msp34xx_modus(msp->norm);
  1148. std = msp34xx_standard(msp->norm);
  1149. modus &= ~0x03; /* STATUS_CHANGE=0 */
  1150. modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
  1151. if (msp3400c_write(client,
  1152. I2C_MSP3400C_DEM,
  1153. 0x30/*MODUS*/,
  1154. modus))
  1155. return -1;
  1156. if (msp3400c_write(client,
  1157. I2C_MSP3400C_DEM,
  1158. 0x20/*standard*/,
  1159. std))
  1160. return -1;
  1161. /* write the dfps that may have an influence on
  1162. standard/audio autodetection right now */
  1163. msp34xxg_set_source(client, msp->source);
  1164. if (msp3400c_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */
  1165. 0x3000
  1166. /* default: [15:8] 75khz deviation */
  1167. ))
  1168. return -1;
  1169. if (msp3400c_write_dfp_with_default(client, 0x10, /* NICAM Prescale */
  1170. 0x5a00
  1171. /* default: 9db gain (as recommended) */
  1172. ))
  1173. return -1;
  1174. return 0;
  1175. }
  1176. static int msp34xxg_thread(void *data)
  1177. {
  1178. struct i2c_client *client = data;
  1179. struct msp3400c *msp = i2c_get_clientdata(client);
  1180. int val, std, i;
  1181. printk("msp34xxg: daemon started\n");
  1182. msp->source = 1; /* default */
  1183. for (;;) {
  1184. d2printk("msp34xxg: thread: sleep\n");
  1185. msp34xx_sleep(msp,-1);
  1186. d2printk("msp34xxg: thread: wakeup\n");
  1187. restart:
  1188. dprintk("msp34xxg: thread: restart scan\n");
  1189. msp->restart = 0;
  1190. if (kthread_should_stop())
  1191. break;
  1192. /* setup the chip*/
  1193. msp34xxg_reset(client);
  1194. std = standard;
  1195. if (std != 0x01)
  1196. goto unmute;
  1197. /* watch autodetect */
  1198. dprintk("msp34xxg: triggered autodetect, waiting for result\n");
  1199. for (i = 0; i < 10; i++) {
  1200. if (msp34xx_sleep(msp,100))
  1201. goto restart;
  1202. /* check results */
  1203. val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
  1204. if (val < 0x07ff) {
  1205. std = val;
  1206. break;
  1207. }
  1208. dprintk("msp34xxg: detection still in progress\n");
  1209. }
  1210. if (0x01 == std) {
  1211. dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
  1212. continue;
  1213. }
  1214. unmute:
  1215. dprintk("msp34xxg: current mode: %s (0x%04x)\n",
  1216. msp34xx_standard_mode_name(std), std);
  1217. /* unmute: dispatch sound to scart output, set scart volume */
  1218. dprintk("msp34xxg: unmute\n");
  1219. msp3400c_setbass(client, msp->bass);
  1220. msp3400c_settreble(client, msp->treble);
  1221. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  1222. /* restore ACB */
  1223. if (msp3400c_write(client,
  1224. I2C_MSP3400C_DFP,
  1225. 0x13, /* ACB */
  1226. msp->acb))
  1227. return -1;
  1228. }
  1229. dprintk("msp34xxg: thread: exit\n");
  1230. return 0;
  1231. }
  1232. /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
  1233. * the value for source is the same as bit 15:8 of DFP registers 0x08,
  1234. * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
  1235. *
  1236. * this function replaces msp3400c_setstereo
  1237. */
  1238. static void msp34xxg_set_source(struct i2c_client *client, int source)
  1239. {
  1240. struct msp3400c *msp = i2c_get_clientdata(client);
  1241. /* fix matrix mode to stereo and let the msp choose what
  1242. * to output according to 'source', as recommended
  1243. * for MONO (source==0) downmixing set bit[7:0] to 0x30
  1244. */
  1245. int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
  1246. dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
  1247. msp3400c_write(client,
  1248. I2C_MSP3400C_DFP,
  1249. 0x08, /* Loudspeaker Output */
  1250. value);
  1251. msp3400c_write(client,
  1252. I2C_MSP3400C_DFP,
  1253. 0x0a, /* SCART1 DA Output */
  1254. value);
  1255. msp3400c_write(client,
  1256. I2C_MSP3400C_DFP,
  1257. 0x0c, /* Quasi-peak detector */
  1258. value);
  1259. /*
  1260. * set identification threshold. Personally, I
  1261. * I set it to a higher value that the default
  1262. * of 0x190 to ignore noisy stereo signals.
  1263. * this needs tuning. (recommended range 0x00a0-0x03c0)
  1264. * 0x7f0 = forced mono mode
  1265. */
  1266. msp3400c_write(client,
  1267. I2C_MSP3400C_DEM,
  1268. 0x22, /* a2 threshold for stereo/bilingual */
  1269. stereo_threshold);
  1270. msp->source=source;
  1271. }
  1272. static void msp34xxg_detect_stereo(struct i2c_client *client)
  1273. {
  1274. struct msp3400c *msp = i2c_get_clientdata(client);
  1275. int status = msp3400c_read(client,
  1276. I2C_MSP3400C_DEM,
  1277. 0x0200 /* STATUS */);
  1278. int is_bilingual = status&0x100;
  1279. int is_stereo = status&0x40;
  1280. msp->rxsubchans = 0;
  1281. if (is_stereo)
  1282. msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
  1283. else
  1284. msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
  1285. if (is_bilingual) {
  1286. msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
  1287. /* I'm supposed to check whether it's SAP or not
  1288. * and set only LANG2/SAP in this case. Yet, the MSP
  1289. * does a lot of work to hide this and handle everything
  1290. * the same way. I don't want to work around it so unless
  1291. * this is a problem, I'll handle SAP just like lang1/lang2.
  1292. */
  1293. }
  1294. dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
  1295. status, is_stereo, is_bilingual, msp->rxsubchans);
  1296. }
  1297. static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
  1298. {
  1299. struct msp3400c *msp = i2c_get_clientdata(client);
  1300. int source;
  1301. switch (audmode) {
  1302. case V4L2_TUNER_MODE_MONO:
  1303. source=0; /* mono only */
  1304. break;
  1305. case V4L2_TUNER_MODE_STEREO:
  1306. source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
  1307. /* problem: that could also mean 2 (scart input) */
  1308. break;
  1309. case V4L2_TUNER_MODE_LANG1:
  1310. source=3; /* stereo or A */
  1311. break;
  1312. case V4L2_TUNER_MODE_LANG2:
  1313. source=4; /* stereo or B */
  1314. break;
  1315. default:
  1316. audmode = 0;
  1317. source = 1;
  1318. break;
  1319. }
  1320. msp->audmode = audmode;
  1321. msp34xxg_set_source(client, source);
  1322. }
  1323. /* ----------------------------------------------------------------------- */
  1324. static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
  1325. static int msp_detach(struct i2c_client *client);
  1326. static int msp_probe(struct i2c_adapter *adap);
  1327. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
  1328. static int msp_suspend(struct device * dev, pm_message_t state);
  1329. static int msp_resume(struct device * dev);
  1330. static void msp_wake_thread(struct i2c_client *client);
  1331. static struct i2c_driver driver = {
  1332. .owner = THIS_MODULE,
  1333. .name = "i2c msp3400 driver",
  1334. .id = I2C_DRIVERID_MSP3400,
  1335. .flags = I2C_DF_NOTIFY,
  1336. .attach_adapter = msp_probe,
  1337. .detach_client = msp_detach,
  1338. .command = msp_command,
  1339. .driver = {
  1340. .suspend = msp_suspend,
  1341. .resume = msp_resume,
  1342. },
  1343. };
  1344. static struct i2c_client client_template =
  1345. {
  1346. .name = "(unset)",
  1347. .flags = I2C_CLIENT_ALLOW_USE,
  1348. .driver = &driver,
  1349. };
  1350. static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
  1351. {
  1352. struct msp3400c *msp;
  1353. struct i2c_client *c;
  1354. int (*thread_func)(void *data) = NULL;
  1355. int i;
  1356. client_template.adapter = adap;
  1357. client_template.addr = addr;
  1358. if (-1 == msp3400c_reset(&client_template)) {
  1359. dprintk("msp34xx: no chip found\n");
  1360. return -1;
  1361. }
  1362. if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
  1363. return -ENOMEM;
  1364. memcpy(c,&client_template,sizeof(struct i2c_client));
  1365. if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
  1366. kfree(c);
  1367. return -ENOMEM;
  1368. }
  1369. memset(msp,0,sizeof(struct msp3400c));
  1370. msp->norm = VIDEO_MODE_NTSC;
  1371. msp->left = 58880; /* 0db gain */
  1372. msp->right = 58880; /* 0db gain */
  1373. msp->bass = 32768;
  1374. msp->treble = 32768;
  1375. msp->input = -1;
  1376. msp->muted = 0;
  1377. for (i = 0; i < DFP_COUNT; i++)
  1378. msp->dfp_regs[i] = -1;
  1379. i2c_set_clientdata(c, msp);
  1380. init_waitqueue_head(&msp->wq);
  1381. if (-1 == msp3400c_reset(c)) {
  1382. kfree(msp);
  1383. kfree(c);
  1384. dprintk("msp34xx: no chip found\n");
  1385. return -1;
  1386. }
  1387. msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
  1388. if (-1 != msp->rev1)
  1389. msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
  1390. if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
  1391. kfree(msp);
  1392. kfree(c);
  1393. dprintk("msp34xx: error while reading chip version\n");
  1394. return -1;
  1395. }
  1396. printk(KERN_INFO "msp34xx: rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2);
  1397. msp3400c_setvolume(c, msp->muted, msp->left, msp->right);
  1398. snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
  1399. (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
  1400. ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
  1401. msp->opmode = opmode;
  1402. if (OPMODE_AUTO == msp->opmode) {
  1403. if (HAVE_SIMPLER(msp))
  1404. msp->opmode = OPMODE_SIMPLER;
  1405. else if (HAVE_SIMPLE(msp))
  1406. msp->opmode = OPMODE_SIMPLE;
  1407. else
  1408. msp->opmode = OPMODE_MANUAL;
  1409. }
  1410. /* hello world :-) */
  1411. printk(KERN_INFO "msp34xx: init: chip=%s", c->name);
  1412. if (HAVE_NICAM(msp))
  1413. printk(" +nicam");
  1414. if (HAVE_SIMPLE(msp))
  1415. printk(" +simple");
  1416. if (HAVE_SIMPLER(msp))
  1417. printk(" +simpler");
  1418. if (HAVE_RADIO(msp))
  1419. printk(" +radio");
  1420. /* version-specific initialization */
  1421. switch (msp->opmode) {
  1422. case OPMODE_MANUAL:
  1423. printk(" mode=manual");
  1424. thread_func = msp3400c_thread;
  1425. break;
  1426. case OPMODE_SIMPLE:
  1427. printk(" mode=simple");
  1428. thread_func = msp3410d_thread;
  1429. break;
  1430. case OPMODE_SIMPLER:
  1431. printk(" mode=simpler");
  1432. thread_func = msp34xxg_thread;
  1433. break;
  1434. }
  1435. printk("\n");
  1436. /* startup control thread if needed */
  1437. if (thread_func) {
  1438. msp->kthread = kthread_run(thread_func, c, "msp34xx");
  1439. if (NULL == msp->kthread)
  1440. printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
  1441. msp_wake_thread(c);
  1442. }
  1443. /* done */
  1444. i2c_attach_client(c);
  1445. /* update our own array */
  1446. for (i = 0; i < MSP3400_MAX; i++) {
  1447. if (NULL == msps[i]) {
  1448. msps[i] = c;
  1449. break;
  1450. }
  1451. }
  1452. return 0;
  1453. }
  1454. static int msp_detach(struct i2c_client *client)
  1455. {
  1456. struct msp3400c *msp = i2c_get_clientdata(client);
  1457. int i;
  1458. /* shutdown control thread */
  1459. if (msp->kthread) {
  1460. msp->restart = 1;
  1461. kthread_stop(msp->kthread);
  1462. }
  1463. msp3400c_reset(client);
  1464. /* update our own array */
  1465. for (i = 0; i < MSP3400_MAX; i++) {
  1466. if (client == msps[i]) {
  1467. msps[i] = NULL;
  1468. break;
  1469. }
  1470. }
  1471. i2c_detach_client(client);
  1472. kfree(msp);
  1473. kfree(client);
  1474. return 0;
  1475. }
  1476. static int msp_probe(struct i2c_adapter *adap)
  1477. {
  1478. if (adap->class & I2C_CLASS_TV_ANALOG)
  1479. return i2c_probe(adap, &addr_data, msp_attach);
  1480. return 0;
  1481. }
  1482. static void msp_wake_thread(struct i2c_client *client)
  1483. {
  1484. struct msp3400c *msp = i2c_get_clientdata(client);
  1485. if (NULL == msp->kthread)
  1486. return;
  1487. msp3400c_setvolume(client,msp->muted,0,0);
  1488. msp->watch_stereo = 0;
  1489. msp->restart = 1;
  1490. wake_up_interruptible(&msp->wq);
  1491. }
  1492. /* ----------------------------------------------------------------------- */
  1493. static int mode_v4l2_to_v4l1(int rxsubchans)
  1494. {
  1495. int mode = 0;
  1496. if (rxsubchans & V4L2_TUNER_SUB_STEREO)
  1497. mode |= VIDEO_SOUND_STEREO;
  1498. if (rxsubchans & V4L2_TUNER_SUB_LANG2)
  1499. mode |= VIDEO_SOUND_LANG2;
  1500. if (rxsubchans & V4L2_TUNER_SUB_LANG1)
  1501. mode |= VIDEO_SOUND_LANG1;
  1502. if (0 == mode)
  1503. mode |= VIDEO_SOUND_MONO;
  1504. return mode;
  1505. }
  1506. static int mode_v4l1_to_v4l2(int mode)
  1507. {
  1508. if (mode & VIDEO_SOUND_STEREO)
  1509. return V4L2_TUNER_MODE_STEREO;
  1510. if (mode & VIDEO_SOUND_LANG2)
  1511. return V4L2_TUNER_MODE_LANG2;
  1512. if (mode & VIDEO_SOUND_LANG1)
  1513. return V4L2_TUNER_MODE_LANG1;
  1514. return V4L2_TUNER_MODE_MONO;
  1515. }
  1516. static void msp_any_detect_stereo(struct i2c_client *client)
  1517. {
  1518. struct msp3400c *msp = i2c_get_clientdata(client);
  1519. switch (msp->opmode) {
  1520. case OPMODE_MANUAL:
  1521. case OPMODE_SIMPLE:
  1522. autodetect_stereo(client);
  1523. break;
  1524. case OPMODE_SIMPLER:
  1525. msp34xxg_detect_stereo(client);
  1526. break;
  1527. }
  1528. }
  1529. static void msp_any_set_audmode(struct i2c_client *client, int audmode)
  1530. {
  1531. struct msp3400c *msp = i2c_get_clientdata(client);
  1532. switch (msp->opmode) {
  1533. case OPMODE_MANUAL:
  1534. case OPMODE_SIMPLE:
  1535. msp->watch_stereo = 0;
  1536. msp3400c_setstereo(client, audmode);
  1537. break;
  1538. case OPMODE_SIMPLER:
  1539. msp34xxg_set_audmode(client, audmode);
  1540. break;
  1541. }
  1542. }
  1543. static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
  1544. {
  1545. struct msp3400c *msp = i2c_get_clientdata(client);
  1546. __u16 *sarg = arg;
  1547. int scart = 0;
  1548. switch (cmd) {
  1549. case AUDC_SET_INPUT:
  1550. dprintk("msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
  1551. if (*sarg == msp->input)
  1552. break;
  1553. msp->input = *sarg;
  1554. switch (*sarg) {
  1555. case AUDIO_RADIO:
  1556. /* Hauppauge uses IN2 for the radio */
  1557. msp->mode = MSP_MODE_FM_RADIO;
  1558. scart = SCART_IN2;
  1559. break;
  1560. case AUDIO_EXTERN_1:
  1561. /* IN1 is often used for external input ... */
  1562. msp->mode = MSP_MODE_EXTERN;
  1563. scart = SCART_IN1;
  1564. break;
  1565. case AUDIO_EXTERN_2:
  1566. /* ... sometimes it is IN2 through ;) */
  1567. msp->mode = MSP_MODE_EXTERN;
  1568. scart = SCART_IN2;
  1569. break;
  1570. case AUDIO_TUNER:
  1571. msp->mode = -1;
  1572. break;
  1573. default:
  1574. if (*sarg & AUDIO_MUTE)
  1575. msp3400c_set_scart(client,SCART_MUTE,0);
  1576. break;
  1577. }
  1578. if (scart) {
  1579. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1580. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1581. msp3400c_set_scart(client,scart,0);
  1582. msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
  1583. if (msp->opmode != OPMODE_SIMPLER)
  1584. msp3400c_setstereo(client, msp->audmode);
  1585. }
  1586. msp_wake_thread(client);
  1587. break;
  1588. case AUDC_SET_RADIO:
  1589. dprintk("msp34xx: AUDC_SET_RADIO\n");
  1590. msp->norm = VIDEO_MODE_RADIO;
  1591. dprintk("msp34xx: switching to radio mode\n");
  1592. msp->watch_stereo = 0;
  1593. switch (msp->opmode) {
  1594. case OPMODE_MANUAL:
  1595. /* set msp3400 to FM radio mode */
  1596. msp3400c_setmode(client,MSP_MODE_FM_RADIO);
  1597. msp3400c_setcarrier(client, MSP_CARRIER(10.7),
  1598. MSP_CARRIER(10.7));
  1599. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  1600. break;
  1601. case OPMODE_SIMPLE:
  1602. case OPMODE_SIMPLER:
  1603. /* the thread will do for us */
  1604. msp_wake_thread(client);
  1605. break;
  1606. }
  1607. break;
  1608. /* work-in-progress: hook to control the DFP registers */
  1609. case MSP_SET_DFPREG:
  1610. {
  1611. struct msp_dfpreg *r = arg;
  1612. int i;
  1613. if (r->reg < 0 || r->reg >= DFP_COUNT)
  1614. return -EINVAL;
  1615. for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++)
  1616. if (r->reg == bl_dfp[i])
  1617. return -EINVAL;
  1618. msp->dfp_regs[r->reg] = r->value;
  1619. msp3400c_write(client, I2C_MSP3400C_DFP, r->reg, r->value);
  1620. return 0;
  1621. }
  1622. case MSP_GET_DFPREG:
  1623. {
  1624. struct msp_dfpreg *r = arg;
  1625. if (r->reg < 0 || r->reg >= DFP_COUNT)
  1626. return -EINVAL;
  1627. r->value = msp3400c_read(client, I2C_MSP3400C_DFP, r->reg);
  1628. return 0;
  1629. }
  1630. /* --- v4l ioctls --- */
  1631. /* take care: bttv does userspace copying, we'll get a
  1632. kernel pointer here... */
  1633. case VIDIOCGAUDIO:
  1634. {
  1635. struct video_audio *va = arg;
  1636. dprintk("msp34xx: VIDIOCGAUDIO\n");
  1637. va->flags |= VIDEO_AUDIO_VOLUME |
  1638. VIDEO_AUDIO_BASS |
  1639. VIDEO_AUDIO_TREBLE |
  1640. VIDEO_AUDIO_MUTABLE;
  1641. if (msp->muted)
  1642. va->flags |= VIDEO_AUDIO_MUTE;
  1643. if (msp->muted)
  1644. va->flags |= VIDEO_AUDIO_MUTE;
  1645. va->volume = MAX(msp->left, msp->right);
  1646. va->balance = (32768 * MIN(msp->left, msp->right)) /
  1647. (va->volume ? va->volume : 1);
  1648. va->balance = (msp->left < msp->right) ?
  1649. (65535 - va->balance) : va->balance;
  1650. if (0 == va->volume)
  1651. va->balance = 32768;
  1652. va->bass = msp->bass;
  1653. va->treble = msp->treble;
  1654. msp_any_detect_stereo(client);
  1655. va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
  1656. break;
  1657. }
  1658. case VIDIOCSAUDIO:
  1659. {
  1660. struct video_audio *va = arg;
  1661. dprintk("msp34xx: VIDIOCSAUDIO\n");
  1662. msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
  1663. msp->left = (MIN(65536 - va->balance, 32768) *
  1664. va->volume) / 32768;
  1665. msp->right = (MIN(va->balance, 32768) * va->volume) / 32768;
  1666. msp->bass = va->bass;
  1667. msp->treble = va->treble;
  1668. dprintk("msp34xx: VIDIOCSAUDIO setting va->volume to %d\n",
  1669. va->volume);
  1670. dprintk("msp34xx: VIDIOCSAUDIO setting va->balance to %d\n",
  1671. va->balance);
  1672. dprintk("msp34xx: VIDIOCSAUDIO setting va->flags to %d\n",
  1673. va->flags);
  1674. dprintk("msp34xx: VIDIOCSAUDIO setting msp->left to %d\n",
  1675. msp->left);
  1676. dprintk("msp34xx: VIDIOCSAUDIO setting msp->right to %d\n",
  1677. msp->right);
  1678. dprintk("msp34xx: VIDIOCSAUDIO setting msp->bass to %d\n",
  1679. msp->bass);
  1680. dprintk("msp34xx: VIDIOCSAUDIO setting msp->treble to %d\n",
  1681. msp->treble);
  1682. dprintk("msp34xx: VIDIOCSAUDIO setting msp->mode to %d\n",
  1683. msp->mode);
  1684. msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  1685. msp3400c_setbass(client, msp->bass);
  1686. msp3400c_settreble(client, msp->treble);
  1687. if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
  1688. msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
  1689. break;
  1690. }
  1691. case VIDIOCSCHAN:
  1692. {
  1693. struct video_channel *vc = arg;
  1694. dprintk("msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
  1695. msp->norm = vc->norm;
  1696. msp_wake_thread(client);
  1697. break;
  1698. }
  1699. case VIDIOCSFREQ:
  1700. case VIDIOC_S_FREQUENCY:
  1701. {
  1702. /* new channel -- kick audio carrier scan */
  1703. dprintk("msp34xx: VIDIOCSFREQ\n");
  1704. msp_wake_thread(client);
  1705. break;
  1706. }
  1707. /* --- v4l2 ioctls --- */
  1708. case VIDIOC_S_STD:
  1709. {
  1710. v4l2_std_id *id = arg;
  1711. /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
  1712. if (*id & V4L2_STD_PAL) {
  1713. msp->norm=VIDEO_MODE_PAL;
  1714. } else if (*id & V4L2_STD_SECAM) {
  1715. msp->norm=VIDEO_MODE_SECAM;
  1716. } else {
  1717. msp->norm=VIDEO_MODE_NTSC;
  1718. }
  1719. msp_wake_thread(client);
  1720. return 0;
  1721. }
  1722. case VIDIOC_G_AUDIO:
  1723. {
  1724. struct v4l2_audio *a = arg;
  1725. memset(a,0,sizeof(*a));
  1726. switch (a->index) {
  1727. case AUDIO_RADIO:
  1728. strcpy(a->name,"Radio");
  1729. break;
  1730. case AUDIO_EXTERN_1:
  1731. strcpy(a->name,"Extern 1");
  1732. break;
  1733. case AUDIO_EXTERN_2:
  1734. strcpy(a->name,"Extern 2");
  1735. break;
  1736. case AUDIO_TUNER:
  1737. strcpy(a->name,"Television");
  1738. break;
  1739. default:
  1740. return -EINVAL;
  1741. }
  1742. msp_any_detect_stereo(client);
  1743. if (msp->audmode == V4L2_TUNER_MODE_STEREO) {
  1744. a->capability=V4L2_AUDCAP_STEREO;
  1745. }
  1746. break;
  1747. }
  1748. case VIDIOC_S_AUDIO:
  1749. {
  1750. struct v4l2_audio *sarg = arg;
  1751. switch (sarg->index) {
  1752. case AUDIO_RADIO:
  1753. /* Hauppauge uses IN2 for the radio */
  1754. msp->mode = MSP_MODE_FM_RADIO;
  1755. scart = SCART_IN2;
  1756. break;
  1757. case AUDIO_EXTERN_1:
  1758. /* IN1 is often used for external input ... */
  1759. msp->mode = MSP_MODE_EXTERN;
  1760. scart = SCART_IN1;
  1761. break;
  1762. case AUDIO_EXTERN_2:
  1763. /* ... sometimes it is IN2 through ;) */
  1764. msp->mode = MSP_MODE_EXTERN;
  1765. scart = SCART_IN2;
  1766. break;
  1767. case AUDIO_TUNER:
  1768. msp->mode = -1;
  1769. break;
  1770. }
  1771. if (scart) {
  1772. msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1773. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1774. msp3400c_set_scart(client,scart,0);
  1775. msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
  1776. }
  1777. if (sarg->capability==V4L2_AUDCAP_STEREO) {
  1778. msp->audmode = V4L2_TUNER_MODE_STEREO;
  1779. } else {
  1780. msp->audmode &= ~V4L2_TUNER_MODE_STEREO;
  1781. }
  1782. msp_any_set_audmode(client, msp->audmode);
  1783. msp_wake_thread(client);
  1784. break;
  1785. }
  1786. case VIDIOC_G_TUNER:
  1787. {
  1788. struct v4l2_tuner *vt = arg;
  1789. msp_any_detect_stereo(client);
  1790. vt->audmode = msp->audmode;
  1791. vt->rxsubchans = msp->rxsubchans;
  1792. vt->capability = V4L2_TUNER_CAP_STEREO |
  1793. V4L2_TUNER_CAP_LANG1|
  1794. V4L2_TUNER_CAP_LANG2;
  1795. break;
  1796. }
  1797. case VIDIOC_S_TUNER:
  1798. {
  1799. struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
  1800. /* only set audmode */
  1801. if (vt->audmode != -1 && vt->audmode != 0)
  1802. msp_any_set_audmode(client, vt->audmode);
  1803. break;
  1804. }
  1805. /* msp34xx specific */
  1806. case MSP_SET_MATRIX:
  1807. {
  1808. struct msp_matrix *mspm = arg;
  1809. dprintk("msp34xx: MSP_SET_MATRIX\n");
  1810. msp3400c_set_scart(client, mspm->input, mspm->output);
  1811. break;
  1812. }
  1813. default:
  1814. /* nothing */
  1815. break;
  1816. }
  1817. return 0;
  1818. }
  1819. static int msp_suspend(struct device * dev, pm_message_t state)
  1820. {
  1821. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  1822. dprintk("msp34xx: suspend\n");
  1823. msp3400c_reset(c);
  1824. return 0;
  1825. }
  1826. static int msp_resume(struct device * dev)
  1827. {
  1828. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  1829. dprintk("msp34xx: resume\n");
  1830. msp_wake_thread(c);
  1831. return 0;
  1832. }
  1833. /* ----------------------------------------------------------------------- */
  1834. static int __init msp3400_init_module(void)
  1835. {
  1836. return i2c_add_driver(&driver);
  1837. }
  1838. static void __exit msp3400_cleanup_module(void)
  1839. {
  1840. i2c_del_driver(&driver);
  1841. }
  1842. module_init(msp3400_init_module);
  1843. module_exit(msp3400_cleanup_module);
  1844. /*
  1845. * Overrides for Emacs so that we follow Linus's tabbing style.
  1846. * ---------------------------------------------------------------------------
  1847. * Local variables:
  1848. * c-basic-offset: 8
  1849. * End:
  1850. */