msp3400.c 63 KB

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