msp3400.c 64 KB

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