msp3400.c 59 KB

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