msp3400.c 60 KB

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