tvaudio.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /*
  2. * experimental driver for simple i2c audio chips.
  3. *
  4. * Copyright (c) 2000 Gerd Knorr
  5. * based on code by:
  6. * Eric Sandeen (eric_sandeen@bigfoot.com)
  7. * Steve VanDeBogart (vandebo@uclink.berkeley.edu)
  8. * Greg Alexander (galexand@acm.org)
  9. *
  10. * This code is placed under the terms of the GNU General Public License
  11. *
  12. * OPTIONS:
  13. * debug - set to 1 if you'd like to see debug messages
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/timer.h>
  22. #include <linux/delay.h>
  23. #include <linux/errno.h>
  24. #include <linux/slab.h>
  25. #include <linux/videodev.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-algo-bit.h>
  28. #include <linux/init.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/kthread.h>
  31. #include <media/tvaudio.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/i2c-addr.h>
  34. /* ---------------------------------------------------------------------- */
  35. /* insmod args */
  36. static int debug = 0; /* insmod parameter */
  37. module_param(debug, int, 0644);
  38. MODULE_DESCRIPTION("device driver for various i2c TV sound decoder / audiomux chips");
  39. MODULE_AUTHOR("Eric Sandeen, Steve VanDeBogart, Greg Alexander, Gerd Knorr");
  40. MODULE_LICENSE("GPL");
  41. #define UNSET (-1U)
  42. /* ---------------------------------------------------------------------- */
  43. /* our structs */
  44. #define MAXREGS 64
  45. struct CHIPSTATE;
  46. typedef int (*getvalue)(int);
  47. typedef int (*checkit)(struct CHIPSTATE*);
  48. typedef int (*initialize)(struct CHIPSTATE*);
  49. typedef int (*getmode)(struct CHIPSTATE*);
  50. typedef void (*setmode)(struct CHIPSTATE*, int mode);
  51. typedef void (*checkmode)(struct CHIPSTATE*);
  52. /* i2c command */
  53. typedef struct AUDIOCMD {
  54. int count; /* # of bytes to send */
  55. unsigned char bytes[MAXREGS+1]; /* addr, data, data, ... */
  56. } audiocmd;
  57. /* chip description */
  58. struct CHIPDESC {
  59. char *name; /* chip name */
  60. int id; /* ID */
  61. int addr_lo, addr_hi; /* i2c address range */
  62. int registers; /* # of registers */
  63. int *insmodopt;
  64. checkit checkit;
  65. initialize initialize;
  66. int flags;
  67. #define CHIP_HAS_VOLUME 1
  68. #define CHIP_HAS_BASSTREBLE 2
  69. #define CHIP_HAS_INPUTSEL 4
  70. /* various i2c command sequences */
  71. audiocmd init;
  72. /* which register has which value */
  73. int leftreg,rightreg,treblereg,bassreg;
  74. /* initialize with (defaults to 65535/65535/32768/32768 */
  75. int leftinit,rightinit,trebleinit,bassinit;
  76. /* functions to convert the values (v4l -> chip) */
  77. getvalue volfunc,treblefunc,bassfunc;
  78. /* get/set mode */
  79. getmode getmode;
  80. setmode setmode;
  81. /* check / autoswitch audio after channel switches */
  82. checkmode checkmode;
  83. /* input switch register + values for v4l inputs */
  84. int inputreg;
  85. int inputmap[4];
  86. int inputmute;
  87. int inputmask;
  88. };
  89. static struct CHIPDESC chiplist[];
  90. /* current state of the chip */
  91. struct CHIPSTATE {
  92. struct i2c_client c;
  93. /* index into CHIPDESC array */
  94. int type;
  95. /* shadow register set */
  96. audiocmd shadow;
  97. /* current settings */
  98. __u16 left,right,treble,bass,muted,mode;
  99. int prevmode;
  100. int radio;
  101. int input;
  102. /* thread */
  103. struct task_struct *thread;
  104. struct timer_list wt;
  105. int watch_stereo;
  106. int audmode;
  107. };
  108. /* ---------------------------------------------------------------------- */
  109. /* i2c addresses */
  110. static unsigned short normal_i2c[] = {
  111. I2C_ADDR_TDA8425 >> 1,
  112. I2C_ADDR_TEA6300 >> 1,
  113. I2C_ADDR_TEA6420 >> 1,
  114. I2C_ADDR_TDA9840 >> 1,
  115. I2C_ADDR_TDA985x_L >> 1,
  116. I2C_ADDR_TDA985x_H >> 1,
  117. I2C_ADDR_TDA9874 >> 1,
  118. I2C_ADDR_PIC16C54 >> 1,
  119. I2C_CLIENT_END };
  120. I2C_CLIENT_INSMOD;
  121. static struct i2c_driver driver;
  122. static struct i2c_client client_template;
  123. /* ---------------------------------------------------------------------- */
  124. /* i2c I/O functions */
  125. static int chip_write(struct CHIPSTATE *chip, int subaddr, int val)
  126. {
  127. unsigned char buffer[2];
  128. if (-1 == subaddr) {
  129. v4l_dbg(1, debug, &chip->c, "%s: chip_write: 0x%x\n",
  130. chip->c.name, val);
  131. chip->shadow.bytes[1] = val;
  132. buffer[0] = val;
  133. if (1 != i2c_master_send(&chip->c,buffer,1)) {
  134. v4l_warn(&chip->c, "%s: I/O error (write 0x%x)\n",
  135. chip->c.name, val);
  136. return -1;
  137. }
  138. } else {
  139. v4l_dbg(1, debug, &chip->c, "%s: chip_write: reg%d=0x%x\n",
  140. chip->c.name, subaddr, val);
  141. chip->shadow.bytes[subaddr+1] = val;
  142. buffer[0] = subaddr;
  143. buffer[1] = val;
  144. if (2 != i2c_master_send(&chip->c,buffer,2)) {
  145. v4l_warn(&chip->c, "%s: I/O error (write reg%d=0x%x)\n",
  146. chip->c.name, subaddr, val);
  147. return -1;
  148. }
  149. }
  150. return 0;
  151. }
  152. static int chip_write_masked(struct CHIPSTATE *chip, int subaddr, int val, int mask)
  153. {
  154. if (mask != 0) {
  155. if (-1 == subaddr) {
  156. val = (chip->shadow.bytes[1] & ~mask) | (val & mask);
  157. } else {
  158. val = (chip->shadow.bytes[subaddr+1] & ~mask) | (val & mask);
  159. }
  160. }
  161. return chip_write(chip, subaddr, val);
  162. }
  163. static int chip_read(struct CHIPSTATE *chip)
  164. {
  165. unsigned char buffer;
  166. if (1 != i2c_master_recv(&chip->c,&buffer,1)) {
  167. v4l_warn(&chip->c, "%s: I/O error (read)\n",
  168. chip->c.name);
  169. return -1;
  170. }
  171. v4l_dbg(1, debug, &chip->c, "%s: chip_read: 0x%x\n",chip->c.name, buffer);
  172. return buffer;
  173. }
  174. static int chip_read2(struct CHIPSTATE *chip, int subaddr)
  175. {
  176. unsigned char write[1];
  177. unsigned char read[1];
  178. struct i2c_msg msgs[2] = {
  179. { chip->c.addr, 0, 1, write },
  180. { chip->c.addr, I2C_M_RD, 1, read }
  181. };
  182. write[0] = subaddr;
  183. if (2 != i2c_transfer(chip->c.adapter,msgs,2)) {
  184. v4l_warn(&chip->c, "%s: I/O error (read2)\n", chip->c.name);
  185. return -1;
  186. }
  187. v4l_dbg(1, debug, &chip->c, "%s: chip_read2: reg%d=0x%x\n",
  188. chip->c.name, subaddr,read[0]);
  189. return read[0];
  190. }
  191. static int chip_cmd(struct CHIPSTATE *chip, char *name, audiocmd *cmd)
  192. {
  193. int i;
  194. if (0 == cmd->count)
  195. return 0;
  196. /* update our shadow register set; print bytes if (debug > 0) */
  197. v4l_dbg(1, debug, &chip->c, "%s: chip_cmd(%s): reg=%d, data:",
  198. chip->c.name, name,cmd->bytes[0]);
  199. for (i = 1; i < cmd->count; i++) {
  200. if (debug)
  201. printk(" 0x%x",cmd->bytes[i]);
  202. chip->shadow.bytes[i+cmd->bytes[0]] = cmd->bytes[i];
  203. }
  204. if (debug)
  205. printk("\n");
  206. /* send data to the chip */
  207. if (cmd->count != i2c_master_send(&chip->c,cmd->bytes,cmd->count)) {
  208. v4l_warn(&chip->c, "%s: I/O error (%s)\n", chip->c.name, name);
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. /* ---------------------------------------------------------------------- */
  214. /* kernel thread for doing i2c stuff asyncronly
  215. * right now it is used only to check the audio mode (mono/stereo/whatever)
  216. * some time after switching to another TV channel, then turn on stereo
  217. * if available, ...
  218. */
  219. static void chip_thread_wake(unsigned long data)
  220. {
  221. struct CHIPSTATE *chip = (struct CHIPSTATE*)data;
  222. wake_up_process(chip->thread);
  223. }
  224. static int chip_thread(void *data)
  225. {
  226. struct CHIPSTATE *chip = data;
  227. struct CHIPDESC *desc = chiplist + chip->type;
  228. v4l_dbg(1, debug, &chip->c, "%s: thread started\n", chip->c.name);
  229. for (;;) {
  230. set_current_state(TASK_INTERRUPTIBLE);
  231. if (!kthread_should_stop())
  232. schedule();
  233. set_current_state(TASK_RUNNING);
  234. try_to_freeze();
  235. if (kthread_should_stop())
  236. break;
  237. v4l_dbg(1, debug, &chip->c, "%s: thread wakeup\n", chip->c.name);
  238. /* don't do anything for radio or if mode != auto */
  239. if (chip->radio || chip->mode != 0)
  240. continue;
  241. /* have a look what's going on */
  242. desc->checkmode(chip);
  243. /* schedule next check */
  244. mod_timer(&chip->wt, jiffies+2*HZ);
  245. }
  246. v4l_dbg(1, debug, &chip->c, "%s: thread exiting\n", chip->c.name);
  247. return 0;
  248. }
  249. static void generic_checkmode(struct CHIPSTATE *chip)
  250. {
  251. struct CHIPDESC *desc = chiplist + chip->type;
  252. int mode = desc->getmode(chip);
  253. if (mode == chip->prevmode)
  254. return;
  255. v4l_dbg(1, debug, &chip->c, "%s: thread checkmode\n", chip->c.name);
  256. chip->prevmode = mode;
  257. if (mode & VIDEO_SOUND_STEREO)
  258. desc->setmode(chip,VIDEO_SOUND_STEREO);
  259. else if (mode & VIDEO_SOUND_LANG1)
  260. desc->setmode(chip,VIDEO_SOUND_LANG1);
  261. else if (mode & VIDEO_SOUND_LANG2)
  262. desc->setmode(chip,VIDEO_SOUND_LANG2);
  263. else
  264. desc->setmode(chip,VIDEO_SOUND_MONO);
  265. }
  266. /* ---------------------------------------------------------------------- */
  267. /* audio chip descriptions - defines+functions for tda9840 */
  268. #define TDA9840_SW 0x00
  269. #define TDA9840_LVADJ 0x02
  270. #define TDA9840_STADJ 0x03
  271. #define TDA9840_TEST 0x04
  272. #define TDA9840_MONO 0x10
  273. #define TDA9840_STEREO 0x2a
  274. #define TDA9840_DUALA 0x12
  275. #define TDA9840_DUALB 0x1e
  276. #define TDA9840_DUALAB 0x1a
  277. #define TDA9840_DUALBA 0x16
  278. #define TDA9840_EXTERNAL 0x7a
  279. #define TDA9840_DS_DUAL 0x20 /* Dual sound identified */
  280. #define TDA9840_ST_STEREO 0x40 /* Stereo sound identified */
  281. #define TDA9840_PONRES 0x80 /* Power-on reset detected if = 1 */
  282. #define TDA9840_TEST_INT1SN 0x1 /* Integration time 0.5s when set */
  283. #define TDA9840_TEST_INTFU 0x02 /* Disables integrator function */
  284. static int tda9840_getmode(struct CHIPSTATE *chip)
  285. {
  286. int val, mode;
  287. val = chip_read(chip);
  288. mode = VIDEO_SOUND_MONO;
  289. if (val & TDA9840_DS_DUAL)
  290. mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  291. if (val & TDA9840_ST_STEREO)
  292. mode |= VIDEO_SOUND_STEREO;
  293. v4l_dbg(1, debug, &chip->c, "tda9840_getmode(): raw chip read: %d, return: %d\n",
  294. val, mode);
  295. return mode;
  296. }
  297. static void tda9840_setmode(struct CHIPSTATE *chip, int mode)
  298. {
  299. int update = 1;
  300. int t = chip->shadow.bytes[TDA9840_SW + 1] & ~0x7e;
  301. switch (mode) {
  302. case VIDEO_SOUND_MONO:
  303. t |= TDA9840_MONO;
  304. break;
  305. case VIDEO_SOUND_STEREO:
  306. t |= TDA9840_STEREO;
  307. break;
  308. case VIDEO_SOUND_LANG1:
  309. t |= TDA9840_DUALA;
  310. break;
  311. case VIDEO_SOUND_LANG2:
  312. t |= TDA9840_DUALB;
  313. break;
  314. default:
  315. update = 0;
  316. }
  317. if (update)
  318. chip_write(chip, TDA9840_SW, t);
  319. }
  320. static int tda9840_checkit(struct CHIPSTATE *chip)
  321. {
  322. int rc;
  323. rc = chip_read(chip);
  324. /* lower 5 bits should be 0 */
  325. return ((rc & 0x1f) == 0) ? 1 : 0;
  326. }
  327. /* ---------------------------------------------------------------------- */
  328. /* audio chip descriptions - defines+functions for tda985x */
  329. /* subaddresses for TDA9855 */
  330. #define TDA9855_VR 0x00 /* Volume, right */
  331. #define TDA9855_VL 0x01 /* Volume, left */
  332. #define TDA9855_BA 0x02 /* Bass */
  333. #define TDA9855_TR 0x03 /* Treble */
  334. #define TDA9855_SW 0x04 /* Subwoofer - not connected on DTV2000 */
  335. /* subaddresses for TDA9850 */
  336. #define TDA9850_C4 0x04 /* Control 1 for TDA9850 */
  337. /* subaddesses for both chips */
  338. #define TDA985x_C5 0x05 /* Control 2 for TDA9850, Control 1 for TDA9855 */
  339. #define TDA985x_C6 0x06 /* Control 3 for TDA9850, Control 2 for TDA9855 */
  340. #define TDA985x_C7 0x07 /* Control 4 for TDA9850, Control 3 for TDA9855 */
  341. #define TDA985x_A1 0x08 /* Alignment 1 for both chips */
  342. #define TDA985x_A2 0x09 /* Alignment 2 for both chips */
  343. #define TDA985x_A3 0x0a /* Alignment 3 for both chips */
  344. /* Masks for bits in TDA9855 subaddresses */
  345. /* 0x00 - VR in TDA9855 */
  346. /* 0x01 - VL in TDA9855 */
  347. /* lower 7 bits control gain from -71dB (0x28) to 16dB (0x7f)
  348. * in 1dB steps - mute is 0x27 */
  349. /* 0x02 - BA in TDA9855 */
  350. /* lower 5 bits control bass gain from -12dB (0x06) to 16.5dB (0x19)
  351. * in .5dB steps - 0 is 0x0E */
  352. /* 0x03 - TR in TDA9855 */
  353. /* 4 bits << 1 control treble gain from -12dB (0x3) to 12dB (0xb)
  354. * in 3dB steps - 0 is 0x7 */
  355. /* Masks for bits in both chips' subaddresses */
  356. /* 0x04 - SW in TDA9855, C4/Control 1 in TDA9850 */
  357. /* Unique to TDA9855: */
  358. /* 4 bits << 2 control subwoofer/surround gain from -14db (0x1) to 14db (0xf)
  359. * in 3dB steps - mute is 0x0 */
  360. /* Unique to TDA9850: */
  361. /* lower 4 bits control stereo noise threshold, over which stereo turns off
  362. * set to values of 0x00 through 0x0f for Ster1 through Ster16 */
  363. /* 0x05 - C5 - Control 1 in TDA9855 , Control 2 in TDA9850*/
  364. /* Unique to TDA9855: */
  365. #define TDA9855_MUTE 1<<7 /* GMU, Mute at outputs */
  366. #define TDA9855_AVL 1<<6 /* AVL, Automatic Volume Level */
  367. #define TDA9855_LOUD 1<<5 /* Loudness, 1==off */
  368. #define TDA9855_SUR 1<<3 /* Surround / Subwoofer 1==.5(L-R) 0==.5(L+R) */
  369. /* Bits 0 to 3 select various combinations
  370. * of line in and line out, only the
  371. * interesting ones are defined */
  372. #define TDA9855_EXT 1<<2 /* Selects inputs LIR and LIL. Pins 41 & 12 */
  373. #define TDA9855_INT 0 /* Selects inputs LOR and LOL. (internal) */
  374. /* Unique to TDA9850: */
  375. /* lower 4 bits contol SAP noise threshold, over which SAP turns off
  376. * set to values of 0x00 through 0x0f for SAP1 through SAP16 */
  377. /* 0x06 - C6 - Control 2 in TDA9855, Control 3 in TDA9850 */
  378. /* Common to TDA9855 and TDA9850: */
  379. #define TDA985x_SAP 3<<6 /* Selects SAP output, mute if not received */
  380. #define TDA985x_STEREO 1<<6 /* Selects Stereo ouput, mono if not received */
  381. #define TDA985x_MONO 0 /* Forces Mono output */
  382. #define TDA985x_LMU 1<<3 /* Mute (LOR/LOL for 9855, OUTL/OUTR for 9850) */
  383. /* Unique to TDA9855: */
  384. #define TDA9855_TZCM 1<<5 /* If set, don't mute till zero crossing */
  385. #define TDA9855_VZCM 1<<4 /* If set, don't change volume till zero crossing*/
  386. #define TDA9855_LINEAR 0 /* Linear Stereo */
  387. #define TDA9855_PSEUDO 1 /* Pseudo Stereo */
  388. #define TDA9855_SPAT_30 2 /* Spatial Stereo, 30% anti-phase crosstalk */
  389. #define TDA9855_SPAT_50 3 /* Spatial Stereo, 52% anti-phase crosstalk */
  390. #define TDA9855_E_MONO 7 /* Forced mono - mono select elseware, so useless*/
  391. /* 0x07 - C7 - Control 3 in TDA9855, Control 4 in TDA9850 */
  392. /* Common to both TDA9855 and TDA9850: */
  393. /* lower 4 bits control input gain from -3.5dB (0x0) to 4dB (0xF)
  394. * in .5dB steps - 0dB is 0x7 */
  395. /* 0x08, 0x09 - A1 and A2 (read/write) */
  396. /* Common to both TDA9855 and TDA9850: */
  397. /* lower 5 bites are wideband and spectral expander alignment
  398. * from 0x00 to 0x1f - nominal at 0x0f and 0x10 (read/write) */
  399. #define TDA985x_STP 1<<5 /* Stereo Pilot/detect (read-only) */
  400. #define TDA985x_SAPP 1<<6 /* SAP Pilot/detect (read-only) */
  401. #define TDA985x_STS 1<<7 /* Stereo trigger 1= <35mV 0= <30mV (write-only)*/
  402. /* 0x0a - A3 */
  403. /* Common to both TDA9855 and TDA9850: */
  404. /* lower 3 bits control timing current for alignment: -30% (0x0), -20% (0x1),
  405. * -10% (0x2), nominal (0x3), +10% (0x6), +20% (0x5), +30% (0x4) */
  406. #define TDA985x_ADJ 1<<7 /* Stereo adjust on/off (wideband and spectral */
  407. static int tda9855_volume(int val) { return val/0x2e8+0x27; }
  408. static int tda9855_bass(int val) { return val/0xccc+0x06; }
  409. static int tda9855_treble(int val) { return (val/0x1c71+0x3)<<1; }
  410. static int tda985x_getmode(struct CHIPSTATE *chip)
  411. {
  412. int mode;
  413. mode = ((TDA985x_STP | TDA985x_SAPP) &
  414. chip_read(chip)) >> 4;
  415. /* Add mono mode regardless of SAP and stereo */
  416. /* Allows forced mono */
  417. return mode | VIDEO_SOUND_MONO;
  418. }
  419. static void tda985x_setmode(struct CHIPSTATE *chip, int mode)
  420. {
  421. int update = 1;
  422. int c6 = chip->shadow.bytes[TDA985x_C6+1] & 0x3f;
  423. switch (mode) {
  424. case VIDEO_SOUND_MONO:
  425. c6 |= TDA985x_MONO;
  426. break;
  427. case VIDEO_SOUND_STEREO:
  428. c6 |= TDA985x_STEREO;
  429. break;
  430. case VIDEO_SOUND_LANG1:
  431. c6 |= TDA985x_SAP;
  432. break;
  433. default:
  434. update = 0;
  435. }
  436. if (update)
  437. chip_write(chip,TDA985x_C6,c6);
  438. }
  439. /* ---------------------------------------------------------------------- */
  440. /* audio chip descriptions - defines+functions for tda9873h */
  441. /* Subaddresses for TDA9873H */
  442. #define TDA9873_SW 0x00 /* Switching */
  443. #define TDA9873_AD 0x01 /* Adjust */
  444. #define TDA9873_PT 0x02 /* Port */
  445. /* Subaddress 0x00: Switching Data
  446. * B7..B0:
  447. *
  448. * B1, B0: Input source selection
  449. * 0, 0 internal
  450. * 1, 0 external stereo
  451. * 0, 1 external mono
  452. */
  453. #define TDA9873_INP_MASK 3
  454. #define TDA9873_INTERNAL 0
  455. #define TDA9873_EXT_STEREO 2
  456. #define TDA9873_EXT_MONO 1
  457. /* B3, B2: output signal select
  458. * B4 : transmission mode
  459. * 0, 0, 1 Mono
  460. * 1, 0, 0 Stereo
  461. * 1, 1, 1 Stereo (reversed channel)
  462. * 0, 0, 0 Dual AB
  463. * 0, 0, 1 Dual AA
  464. * 0, 1, 0 Dual BB
  465. * 0, 1, 1 Dual BA
  466. */
  467. #define TDA9873_TR_MASK (7 << 2)
  468. #define TDA9873_TR_MONO 4
  469. #define TDA9873_TR_STEREO 1 << 4
  470. #define TDA9873_TR_REVERSE (1 << 3) & (1 << 2)
  471. #define TDA9873_TR_DUALA 1 << 2
  472. #define TDA9873_TR_DUALB 1 << 3
  473. /* output level controls
  474. * B5: output level switch (0 = reduced gain, 1 = normal gain)
  475. * B6: mute (1 = muted)
  476. * B7: auto-mute (1 = auto-mute enabled)
  477. */
  478. #define TDA9873_GAIN_NORMAL 1 << 5
  479. #define TDA9873_MUTE 1 << 6
  480. #define TDA9873_AUTOMUTE 1 << 7
  481. /* Subaddress 0x01: Adjust/standard */
  482. /* Lower 4 bits (C3..C0) control stereo adjustment on R channel (-0.6 - +0.7 dB)
  483. * Recommended value is +0 dB
  484. */
  485. #define TDA9873_STEREO_ADJ 0x06 /* 0dB gain */
  486. /* Bits C6..C4 control FM stantard
  487. * C6, C5, C4
  488. * 0, 0, 0 B/G (PAL FM)
  489. * 0, 0, 1 M
  490. * 0, 1, 0 D/K(1)
  491. * 0, 1, 1 D/K(2)
  492. * 1, 0, 0 D/K(3)
  493. * 1, 0, 1 I
  494. */
  495. #define TDA9873_BG 0
  496. #define TDA9873_M 1
  497. #define TDA9873_DK1 2
  498. #define TDA9873_DK2 3
  499. #define TDA9873_DK3 4
  500. #define TDA9873_I 5
  501. /* C7 controls identification response time (1=fast/0=normal)
  502. */
  503. #define TDA9873_IDR_NORM 0
  504. #define TDA9873_IDR_FAST 1 << 7
  505. /* Subaddress 0x02: Port data */
  506. /* E1, E0 free programmable ports P1/P2
  507. 0, 0 both ports low
  508. 0, 1 P1 high
  509. 1, 0 P2 high
  510. 1, 1 both ports high
  511. */
  512. #define TDA9873_PORTS 3
  513. /* E2: test port */
  514. #define TDA9873_TST_PORT 1 << 2
  515. /* E5..E3 control mono output channel (together with transmission mode bit B4)
  516. *
  517. * E5 E4 E3 B4 OUTM
  518. * 0 0 0 0 mono
  519. * 0 0 1 0 DUAL B
  520. * 0 1 0 1 mono (from stereo decoder)
  521. */
  522. #define TDA9873_MOUT_MONO 0
  523. #define TDA9873_MOUT_FMONO 0
  524. #define TDA9873_MOUT_DUALA 0
  525. #define TDA9873_MOUT_DUALB 1 << 3
  526. #define TDA9873_MOUT_ST 1 << 4
  527. #define TDA9873_MOUT_EXTM (1 << 4 ) & (1 << 3)
  528. #define TDA9873_MOUT_EXTL 1 << 5
  529. #define TDA9873_MOUT_EXTR (1 << 5 ) & (1 << 3)
  530. #define TDA9873_MOUT_EXTLR (1 << 5 ) & (1 << 4)
  531. #define TDA9873_MOUT_MUTE (1 << 5 ) & (1 << 4) & (1 << 3)
  532. /* Status bits: (chip read) */
  533. #define TDA9873_PONR 0 /* Power-on reset detected if = 1 */
  534. #define TDA9873_STEREO 2 /* Stereo sound is identified */
  535. #define TDA9873_DUAL 4 /* Dual sound is identified */
  536. static int tda9873_getmode(struct CHIPSTATE *chip)
  537. {
  538. int val,mode;
  539. val = chip_read(chip);
  540. mode = VIDEO_SOUND_MONO;
  541. if (val & TDA9873_STEREO)
  542. mode |= VIDEO_SOUND_STEREO;
  543. if (val & TDA9873_DUAL)
  544. mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  545. v4l_dbg(1, debug, &chip->c, "tda9873_getmode(): raw chip read: %d, return: %d\n",
  546. val, mode);
  547. return mode;
  548. }
  549. static void tda9873_setmode(struct CHIPSTATE *chip, int mode)
  550. {
  551. int sw_data = chip->shadow.bytes[TDA9873_SW+1] & ~ TDA9873_TR_MASK;
  552. /* int adj_data = chip->shadow.bytes[TDA9873_AD+1] ; */
  553. if ((sw_data & TDA9873_INP_MASK) != TDA9873_INTERNAL) {
  554. v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): external input\n");
  555. return;
  556. }
  557. v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): chip->shadow.bytes[%d] = %d\n", TDA9873_SW+1, chip->shadow.bytes[TDA9873_SW+1]);
  558. v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): sw_data = %d\n", sw_data);
  559. switch (mode) {
  560. case VIDEO_SOUND_MONO:
  561. sw_data |= TDA9873_TR_MONO;
  562. break;
  563. case VIDEO_SOUND_STEREO:
  564. sw_data |= TDA9873_TR_STEREO;
  565. break;
  566. case VIDEO_SOUND_LANG1:
  567. sw_data |= TDA9873_TR_DUALA;
  568. break;
  569. case VIDEO_SOUND_LANG2:
  570. sw_data |= TDA9873_TR_DUALB;
  571. break;
  572. default:
  573. chip->mode = 0;
  574. return;
  575. }
  576. chip_write(chip, TDA9873_SW, sw_data);
  577. v4l_dbg(1, debug, &chip->c, "tda9873_setmode(): req. mode %d; chip_write: %d\n",
  578. mode, sw_data);
  579. }
  580. static int tda9873_checkit(struct CHIPSTATE *chip)
  581. {
  582. int rc;
  583. if (-1 == (rc = chip_read2(chip,254)))
  584. return 0;
  585. return (rc & ~0x1f) == 0x80;
  586. }
  587. /* ---------------------------------------------------------------------- */
  588. /* audio chip description - defines+functions for tda9874h and tda9874a */
  589. /* Dariusz Kowalewski <darekk@automex.pl> */
  590. /* Subaddresses for TDA9874H and TDA9874A (slave rx) */
  591. #define TDA9874A_AGCGR 0x00 /* AGC gain */
  592. #define TDA9874A_GCONR 0x01 /* general config */
  593. #define TDA9874A_MSR 0x02 /* monitor select */
  594. #define TDA9874A_C1FRA 0x03 /* carrier 1 freq. */
  595. #define TDA9874A_C1FRB 0x04 /* carrier 1 freq. */
  596. #define TDA9874A_C1FRC 0x05 /* carrier 1 freq. */
  597. #define TDA9874A_C2FRA 0x06 /* carrier 2 freq. */
  598. #define TDA9874A_C2FRB 0x07 /* carrier 2 freq. */
  599. #define TDA9874A_C2FRC 0x08 /* carrier 2 freq. */
  600. #define TDA9874A_DCR 0x09 /* demodulator config */
  601. #define TDA9874A_FMER 0x0a /* FM de-emphasis */
  602. #define TDA9874A_FMMR 0x0b /* FM dematrix */
  603. #define TDA9874A_C1OLAR 0x0c /* ch.1 output level adj. */
  604. #define TDA9874A_C2OLAR 0x0d /* ch.2 output level adj. */
  605. #define TDA9874A_NCONR 0x0e /* NICAM config */
  606. #define TDA9874A_NOLAR 0x0f /* NICAM output level adj. */
  607. #define TDA9874A_NLELR 0x10 /* NICAM lower error limit */
  608. #define TDA9874A_NUELR 0x11 /* NICAM upper error limit */
  609. #define TDA9874A_AMCONR 0x12 /* audio mute control */
  610. #define TDA9874A_SDACOSR 0x13 /* stereo DAC output select */
  611. #define TDA9874A_AOSR 0x14 /* analog output select */
  612. #define TDA9874A_DAICONR 0x15 /* digital audio interface config */
  613. #define TDA9874A_I2SOSR 0x16 /* I2S-bus output select */
  614. #define TDA9874A_I2SOLAR 0x17 /* I2S-bus output level adj. */
  615. #define TDA9874A_MDACOSR 0x18 /* mono DAC output select (tda9874a) */
  616. #define TDA9874A_ESP 0xFF /* easy standard progr. (tda9874a) */
  617. /* Subaddresses for TDA9874H and TDA9874A (slave tx) */
  618. #define TDA9874A_DSR 0x00 /* device status */
  619. #define TDA9874A_NSR 0x01 /* NICAM status */
  620. #define TDA9874A_NECR 0x02 /* NICAM error count */
  621. #define TDA9874A_DR1 0x03 /* add. data LSB */
  622. #define TDA9874A_DR2 0x04 /* add. data MSB */
  623. #define TDA9874A_LLRA 0x05 /* monitor level read-out LSB */
  624. #define TDA9874A_LLRB 0x06 /* monitor level read-out MSB */
  625. #define TDA9874A_SIFLR 0x07 /* SIF level */
  626. #define TDA9874A_TR2 252 /* test reg. 2 */
  627. #define TDA9874A_TR1 253 /* test reg. 1 */
  628. #define TDA9874A_DIC 254 /* device id. code */
  629. #define TDA9874A_SIC 255 /* software id. code */
  630. static int tda9874a_mode = 1; /* 0: A2, 1: NICAM */
  631. static int tda9874a_GCONR = 0xc0; /* default config. input pin: SIFSEL=0 */
  632. static int tda9874a_NCONR = 0x01; /* default NICAM config.: AMSEL=0,AMUTE=1 */
  633. static int tda9874a_ESP = 0x07; /* default standard: NICAM D/K */
  634. static int tda9874a_dic = -1; /* device id. code */
  635. /* insmod options for tda9874a */
  636. static unsigned int tda9874a_SIF = UNSET;
  637. static unsigned int tda9874a_AMSEL = UNSET;
  638. static unsigned int tda9874a_STD = UNSET;
  639. module_param(tda9874a_SIF, int, 0444);
  640. module_param(tda9874a_AMSEL, int, 0444);
  641. module_param(tda9874a_STD, int, 0444);
  642. /*
  643. * initialization table for tda9874 decoder:
  644. * - carrier 1 freq. registers (3 bytes)
  645. * - carrier 2 freq. registers (3 bytes)
  646. * - demudulator config register
  647. * - FM de-emphasis register (slow identification mode)
  648. * Note: frequency registers must be written in single i2c transfer.
  649. */
  650. static struct tda9874a_MODES {
  651. char *name;
  652. audiocmd cmd;
  653. } tda9874a_modelist[9] = {
  654. { "A2, B/G",
  655. { 9, { TDA9874A_C1FRA, 0x72,0x95,0x55, 0x77,0xA0,0x00, 0x00,0x00 }} },
  656. { "A2, M (Korea)",
  657. { 9, { TDA9874A_C1FRA, 0x5D,0xC0,0x00, 0x62,0x6A,0xAA, 0x20,0x22 }} },
  658. { "A2, D/K (1)",
  659. { 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x82,0x60,0x00, 0x00,0x00 }} },
  660. { "A2, D/K (2)",
  661. { 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x8C,0x75,0x55, 0x00,0x00 }} },
  662. { "A2, D/K (3)",
  663. { 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x77,0xA0,0x00, 0x00,0x00 }} },
  664. { "NICAM, I",
  665. { 9, { TDA9874A_C1FRA, 0x7D,0x00,0x00, 0x88,0x8A,0xAA, 0x08,0x33 }} },
  666. { "NICAM, B/G",
  667. { 9, { TDA9874A_C1FRA, 0x72,0x95,0x55, 0x79,0xEA,0xAA, 0x08,0x33 }} },
  668. { "NICAM, D/K", /* default */
  669. { 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x79,0xEA,0xAA, 0x08,0x33 }} },
  670. { "NICAM, L",
  671. { 9, { TDA9874A_C1FRA, 0x87,0x6A,0xAA, 0x79,0xEA,0xAA, 0x09,0x33 }} }
  672. };
  673. static int tda9874a_setup(struct CHIPSTATE *chip)
  674. {
  675. chip_write(chip, TDA9874A_AGCGR, 0x00); /* 0 dB */
  676. chip_write(chip, TDA9874A_GCONR, tda9874a_GCONR);
  677. chip_write(chip, TDA9874A_MSR, (tda9874a_mode) ? 0x03:0x02);
  678. if(tda9874a_dic == 0x11) {
  679. chip_write(chip, TDA9874A_FMMR, 0x80);
  680. } else { /* dic == 0x07 */
  681. chip_cmd(chip,"tda9874_modelist",&tda9874a_modelist[tda9874a_STD].cmd);
  682. chip_write(chip, TDA9874A_FMMR, 0x00);
  683. }
  684. chip_write(chip, TDA9874A_C1OLAR, 0x00); /* 0 dB */
  685. chip_write(chip, TDA9874A_C2OLAR, 0x00); /* 0 dB */
  686. chip_write(chip, TDA9874A_NCONR, tda9874a_NCONR);
  687. chip_write(chip, TDA9874A_NOLAR, 0x00); /* 0 dB */
  688. /* Note: If signal quality is poor you may want to change NICAM */
  689. /* error limit registers (NLELR and NUELR) to some greater values. */
  690. /* Then the sound would remain stereo, but won't be so clear. */
  691. chip_write(chip, TDA9874A_NLELR, 0x14); /* default */
  692. chip_write(chip, TDA9874A_NUELR, 0x50); /* default */
  693. if(tda9874a_dic == 0x11) {
  694. chip_write(chip, TDA9874A_AMCONR, 0xf9);
  695. chip_write(chip, TDA9874A_SDACOSR, (tda9874a_mode) ? 0x81:0x80);
  696. chip_write(chip, TDA9874A_AOSR, 0x80);
  697. chip_write(chip, TDA9874A_MDACOSR, (tda9874a_mode) ? 0x82:0x80);
  698. chip_write(chip, TDA9874A_ESP, tda9874a_ESP);
  699. } else { /* dic == 0x07 */
  700. chip_write(chip, TDA9874A_AMCONR, 0xfb);
  701. chip_write(chip, TDA9874A_SDACOSR, (tda9874a_mode) ? 0x81:0x80);
  702. chip_write(chip, TDA9874A_AOSR, 0x00); /* or 0x10 */
  703. }
  704. v4l_dbg(1, debug, &chip->c, "tda9874a_setup(): %s [0x%02X].\n",
  705. tda9874a_modelist[tda9874a_STD].name,tda9874a_STD);
  706. return 1;
  707. }
  708. static int tda9874a_getmode(struct CHIPSTATE *chip)
  709. {
  710. int dsr,nsr,mode;
  711. int necr; /* just for debugging */
  712. mode = VIDEO_SOUND_MONO;
  713. if(-1 == (dsr = chip_read2(chip,TDA9874A_DSR)))
  714. return mode;
  715. if(-1 == (nsr = chip_read2(chip,TDA9874A_NSR)))
  716. return mode;
  717. if(-1 == (necr = chip_read2(chip,TDA9874A_NECR)))
  718. return mode;
  719. /* need to store dsr/nsr somewhere */
  720. chip->shadow.bytes[MAXREGS-2] = dsr;
  721. chip->shadow.bytes[MAXREGS-1] = nsr;
  722. if(tda9874a_mode) {
  723. /* Note: DSR.RSSF and DSR.AMSTAT bits are also checked.
  724. * If NICAM auto-muting is enabled, DSR.AMSTAT=1 indicates
  725. * that sound has (temporarily) switched from NICAM to
  726. * mono FM (or AM) on 1st sound carrier due to high NICAM bit
  727. * error count. So in fact there is no stereo in this case :-(
  728. * But changing the mode to VIDEO_SOUND_MONO would switch
  729. * external 4052 multiplexer in audio_hook().
  730. */
  731. if(nsr & 0x02) /* NSR.S/MB=1 */
  732. mode |= VIDEO_SOUND_STEREO;
  733. if(nsr & 0x01) /* NSR.D/SB=1 */
  734. mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  735. } else {
  736. if(dsr & 0x02) /* DSR.IDSTE=1 */
  737. mode |= VIDEO_SOUND_STEREO;
  738. if(dsr & 0x04) /* DSR.IDDUA=1 */
  739. mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  740. }
  741. v4l_dbg(1, debug, &chip->c, "tda9874a_getmode(): DSR=0x%X, NSR=0x%X, NECR=0x%X, return: %d.\n",
  742. dsr, nsr, necr, mode);
  743. return mode;
  744. }
  745. static void tda9874a_setmode(struct CHIPSTATE *chip, int mode)
  746. {
  747. /* Disable/enable NICAM auto-muting (based on DSR.RSSF status bit). */
  748. /* If auto-muting is disabled, we can hear a signal of degrading quality. */
  749. if(tda9874a_mode) {
  750. if(chip->shadow.bytes[MAXREGS-2] & 0x20) /* DSR.RSSF=1 */
  751. tda9874a_NCONR &= 0xfe; /* enable */
  752. else
  753. tda9874a_NCONR |= 0x01; /* disable */
  754. chip_write(chip, TDA9874A_NCONR, tda9874a_NCONR);
  755. }
  756. /* Note: TDA9874A supports automatic FM dematrixing (FMMR register)
  757. * and has auto-select function for audio output (AOSR register).
  758. * Old TDA9874H doesn't support these features.
  759. * TDA9874A also has additional mono output pin (OUTM), which
  760. * on same (all?) tv-cards is not used, anyway (as well as MONOIN).
  761. */
  762. if(tda9874a_dic == 0x11) {
  763. int aosr = 0x80;
  764. int mdacosr = (tda9874a_mode) ? 0x82:0x80;
  765. switch(mode) {
  766. case VIDEO_SOUND_MONO:
  767. case VIDEO_SOUND_STEREO:
  768. break;
  769. case VIDEO_SOUND_LANG1:
  770. aosr = 0x80; /* auto-select, dual A/A */
  771. mdacosr = (tda9874a_mode) ? 0x82:0x80;
  772. break;
  773. case VIDEO_SOUND_LANG2:
  774. aosr = 0xa0; /* auto-select, dual B/B */
  775. mdacosr = (tda9874a_mode) ? 0x83:0x81;
  776. break;
  777. default:
  778. chip->mode = 0;
  779. return;
  780. }
  781. chip_write(chip, TDA9874A_AOSR, aosr);
  782. chip_write(chip, TDA9874A_MDACOSR, mdacosr);
  783. v4l_dbg(1, debug, &chip->c, "tda9874a_setmode(): req. mode %d; AOSR=0x%X, MDACOSR=0x%X.\n",
  784. mode, aosr, mdacosr);
  785. } else { /* dic == 0x07 */
  786. int fmmr,aosr;
  787. switch(mode) {
  788. case VIDEO_SOUND_MONO:
  789. fmmr = 0x00; /* mono */
  790. aosr = 0x10; /* A/A */
  791. break;
  792. case VIDEO_SOUND_STEREO:
  793. if(tda9874a_mode) {
  794. fmmr = 0x00;
  795. aosr = 0x00; /* handled by NICAM auto-mute */
  796. } else {
  797. fmmr = (tda9874a_ESP == 1) ? 0x05 : 0x04; /* stereo */
  798. aosr = 0x00;
  799. }
  800. break;
  801. case VIDEO_SOUND_LANG1:
  802. fmmr = 0x02; /* dual */
  803. aosr = 0x10; /* dual A/A */
  804. break;
  805. case VIDEO_SOUND_LANG2:
  806. fmmr = 0x02; /* dual */
  807. aosr = 0x20; /* dual B/B */
  808. break;
  809. default:
  810. chip->mode = 0;
  811. return;
  812. }
  813. chip_write(chip, TDA9874A_FMMR, fmmr);
  814. chip_write(chip, TDA9874A_AOSR, aosr);
  815. v4l_dbg(1, debug, &chip->c, "tda9874a_setmode(): req. mode %d; FMMR=0x%X, AOSR=0x%X.\n",
  816. mode, fmmr, aosr);
  817. }
  818. }
  819. static int tda9874a_checkit(struct CHIPSTATE *chip)
  820. {
  821. int dic,sic; /* device id. and software id. codes */
  822. if(-1 == (dic = chip_read2(chip,TDA9874A_DIC)))
  823. return 0;
  824. if(-1 == (sic = chip_read2(chip,TDA9874A_SIC)))
  825. return 0;
  826. v4l_dbg(1, debug, &chip->c, "tda9874a_checkit(): DIC=0x%X, SIC=0x%X.\n", dic, sic);
  827. if((dic == 0x11)||(dic == 0x07)) {
  828. v4l_info(&chip->c, "found tda9874%s.\n", (dic == 0x11) ? "a":"h");
  829. tda9874a_dic = dic; /* remember device id. */
  830. return 1;
  831. }
  832. return 0; /* not found */
  833. }
  834. static int tda9874a_initialize(struct CHIPSTATE *chip)
  835. {
  836. if (tda9874a_SIF > 2)
  837. tda9874a_SIF = 1;
  838. if (tda9874a_STD > 8)
  839. tda9874a_STD = 0;
  840. if(tda9874a_AMSEL > 1)
  841. tda9874a_AMSEL = 0;
  842. if(tda9874a_SIF == 1)
  843. tda9874a_GCONR = 0xc0; /* sound IF input 1 */
  844. else
  845. tda9874a_GCONR = 0xc1; /* sound IF input 2 */
  846. tda9874a_ESP = tda9874a_STD;
  847. tda9874a_mode = (tda9874a_STD < 5) ? 0 : 1;
  848. if(tda9874a_AMSEL == 0)
  849. tda9874a_NCONR = 0x01; /* auto-mute: analog mono input */
  850. else
  851. tda9874a_NCONR = 0x05; /* auto-mute: 1st carrier FM or AM */
  852. tda9874a_setup(chip);
  853. return 0;
  854. }
  855. /* ---------------------------------------------------------------------- */
  856. /* audio chip descriptions - defines+functions for tea6420 */
  857. #define TEA6300_VL 0x00 /* volume left */
  858. #define TEA6300_VR 0x01 /* volume right */
  859. #define TEA6300_BA 0x02 /* bass */
  860. #define TEA6300_TR 0x03 /* treble */
  861. #define TEA6300_FA 0x04 /* fader control */
  862. #define TEA6300_S 0x05 /* switch register */
  863. /* values for those registers: */
  864. #define TEA6300_S_SA 0x01 /* stereo A input */
  865. #define TEA6300_S_SB 0x02 /* stereo B */
  866. #define TEA6300_S_SC 0x04 /* stereo C */
  867. #define TEA6300_S_GMU 0x80 /* general mute */
  868. #define TEA6320_V 0x00 /* volume (0-5)/loudness off (6)/zero crossing mute(7) */
  869. #define TEA6320_FFR 0x01 /* fader front right (0-5) */
  870. #define TEA6320_FFL 0x02 /* fader front left (0-5) */
  871. #define TEA6320_FRR 0x03 /* fader rear right (0-5) */
  872. #define TEA6320_FRL 0x04 /* fader rear left (0-5) */
  873. #define TEA6320_BA 0x05 /* bass (0-4) */
  874. #define TEA6320_TR 0x06 /* treble (0-4) */
  875. #define TEA6320_S 0x07 /* switch register */
  876. /* values for those registers: */
  877. #define TEA6320_S_SA 0x07 /* stereo A input */
  878. #define TEA6320_S_SB 0x06 /* stereo B */
  879. #define TEA6320_S_SC 0x05 /* stereo C */
  880. #define TEA6320_S_SD 0x04 /* stereo D */
  881. #define TEA6320_S_GMU 0x80 /* general mute */
  882. #define TEA6420_S_SA 0x00 /* stereo A input */
  883. #define TEA6420_S_SB 0x01 /* stereo B */
  884. #define TEA6420_S_SC 0x02 /* stereo C */
  885. #define TEA6420_S_SD 0x03 /* stereo D */
  886. #define TEA6420_S_SE 0x04 /* stereo E */
  887. #define TEA6420_S_GMU 0x05 /* general mute */
  888. static int tea6300_shift10(int val) { return val >> 10; }
  889. static int tea6300_shift12(int val) { return val >> 12; }
  890. /* Assumes 16bit input (values 0x3f to 0x0c are unique, values less than */
  891. /* 0x0c mirror those immediately higher) */
  892. static int tea6320_volume(int val) { return (val / (65535/(63-12)) + 12) & 0x3f; }
  893. static int tea6320_shift11(int val) { return val >> 11; }
  894. static int tea6320_initialize(struct CHIPSTATE * chip)
  895. {
  896. chip_write(chip, TEA6320_FFR, 0x3f);
  897. chip_write(chip, TEA6320_FFL, 0x3f);
  898. chip_write(chip, TEA6320_FRR, 0x3f);
  899. chip_write(chip, TEA6320_FRL, 0x3f);
  900. return 0;
  901. }
  902. /* ---------------------------------------------------------------------- */
  903. /* audio chip descriptions - defines+functions for tda8425 */
  904. #define TDA8425_VL 0x00 /* volume left */
  905. #define TDA8425_VR 0x01 /* volume right */
  906. #define TDA8425_BA 0x02 /* bass */
  907. #define TDA8425_TR 0x03 /* treble */
  908. #define TDA8425_S1 0x08 /* switch functions */
  909. /* values for those registers: */
  910. #define TDA8425_S1_OFF 0xEE /* audio off (mute on) */
  911. #define TDA8425_S1_CH1 0xCE /* audio channel 1 (mute off) - "linear stereo" mode */
  912. #define TDA8425_S1_CH2 0xCF /* audio channel 2 (mute off) - "linear stereo" mode */
  913. #define TDA8425_S1_MU 0x20 /* mute bit */
  914. #define TDA8425_S1_STEREO 0x18 /* stereo bits */
  915. #define TDA8425_S1_STEREO_SPATIAL 0x18 /* spatial stereo */
  916. #define TDA8425_S1_STEREO_LINEAR 0x08 /* linear stereo */
  917. #define TDA8425_S1_STEREO_PSEUDO 0x10 /* pseudo stereo */
  918. #define TDA8425_S1_STEREO_MONO 0x00 /* forced mono */
  919. #define TDA8425_S1_ML 0x06 /* language selector */
  920. #define TDA8425_S1_ML_SOUND_A 0x02 /* sound a */
  921. #define TDA8425_S1_ML_SOUND_B 0x04 /* sound b */
  922. #define TDA8425_S1_ML_STEREO 0x06 /* stereo */
  923. #define TDA8425_S1_IS 0x01 /* channel selector */
  924. static int tda8425_shift10(int val) { return (val >> 10) | 0xc0; }
  925. static int tda8425_shift12(int val) { return (val >> 12) | 0xf0; }
  926. static int tda8425_initialize(struct CHIPSTATE *chip)
  927. {
  928. struct CHIPDESC *desc = chiplist + chip->type;
  929. int inputmap[4] = { /* tuner */ TDA8425_S1_CH2, /* radio */ TDA8425_S1_CH1,
  930. /* extern */ TDA8425_S1_CH1, /* intern */ TDA8425_S1_OFF};
  931. if (chip->c.adapter->id == I2C_HW_B_RIVA) {
  932. memcpy (desc->inputmap, inputmap, sizeof (inputmap));
  933. }
  934. return 0;
  935. }
  936. static void tda8425_setmode(struct CHIPSTATE *chip, int mode)
  937. {
  938. int s1 = chip->shadow.bytes[TDA8425_S1+1] & 0xe1;
  939. if (mode & VIDEO_SOUND_LANG1) {
  940. s1 |= TDA8425_S1_ML_SOUND_A;
  941. s1 |= TDA8425_S1_STEREO_PSEUDO;
  942. } else if (mode & VIDEO_SOUND_LANG2) {
  943. s1 |= TDA8425_S1_ML_SOUND_B;
  944. s1 |= TDA8425_S1_STEREO_PSEUDO;
  945. } else {
  946. s1 |= TDA8425_S1_ML_STEREO;
  947. if (mode & VIDEO_SOUND_MONO)
  948. s1 |= TDA8425_S1_STEREO_MONO;
  949. if (mode & VIDEO_SOUND_STEREO)
  950. s1 |= TDA8425_S1_STEREO_SPATIAL;
  951. }
  952. chip_write(chip,TDA8425_S1,s1);
  953. }
  954. /* ---------------------------------------------------------------------- */
  955. /* audio chip descriptions - defines+functions for pic16c54 (PV951) */
  956. /* the registers of 16C54, I2C sub address. */
  957. #define PIC16C54_REG_KEY_CODE 0x01 /* Not use. */
  958. #define PIC16C54_REG_MISC 0x02
  959. /* bit definition of the RESET register, I2C data. */
  960. #define PIC16C54_MISC_RESET_REMOTE_CTL 0x01 /* bit 0, Reset to receive the key */
  961. /* code of remote controller */
  962. #define PIC16C54_MISC_MTS_MAIN 0x02 /* bit 1 */
  963. #define PIC16C54_MISC_MTS_SAP 0x04 /* bit 2 */
  964. #define PIC16C54_MISC_MTS_BOTH 0x08 /* bit 3 */
  965. #define PIC16C54_MISC_SND_MUTE 0x10 /* bit 4, Mute Audio(Line-in and Tuner) */
  966. #define PIC16C54_MISC_SND_NOTMUTE 0x20 /* bit 5 */
  967. #define PIC16C54_MISC_SWITCH_TUNER 0x40 /* bit 6 , Switch to Line-in */
  968. #define PIC16C54_MISC_SWITCH_LINE 0x80 /* bit 7 , Switch to Tuner */
  969. /* ---------------------------------------------------------------------- */
  970. /* audio chip descriptions - defines+functions for TA8874Z */
  971. /* write 1st byte */
  972. #define TA8874Z_LED_STE 0x80
  973. #define TA8874Z_LED_BIL 0x40
  974. #define TA8874Z_LED_EXT 0x20
  975. #define TA8874Z_MONO_SET 0x10
  976. #define TA8874Z_MUTE 0x08
  977. #define TA8874Z_F_MONO 0x04
  978. #define TA8874Z_MODE_SUB 0x02
  979. #define TA8874Z_MODE_MAIN 0x01
  980. /* write 2nd byte */
  981. /*#define TA8874Z_TI 0x80 */ /* test mode */
  982. #define TA8874Z_SEPARATION 0x3f
  983. #define TA8874Z_SEPARATION_DEFAULT 0x10
  984. /* read */
  985. #define TA8874Z_B1 0x80
  986. #define TA8874Z_B0 0x40
  987. #define TA8874Z_CHAG_FLAG 0x20
  988. /*
  989. * B1 B0
  990. * mono L H
  991. * stereo L L
  992. * BIL H L
  993. */
  994. static int ta8874z_getmode(struct CHIPSTATE *chip)
  995. {
  996. int val, mode;
  997. val = chip_read(chip);
  998. mode = VIDEO_SOUND_MONO;
  999. if (val & TA8874Z_B1){
  1000. mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  1001. }else if (!(val & TA8874Z_B0)){
  1002. mode |= VIDEO_SOUND_STEREO;
  1003. }
  1004. /* v4l_dbg(1, debug, &chip->c, "ta8874z_getmode(): raw chip read: 0x%02x, return: 0x%02x\n", val, mode); */
  1005. return mode;
  1006. }
  1007. static audiocmd ta8874z_stereo = { 2, {0, TA8874Z_SEPARATION_DEFAULT}};
  1008. static audiocmd ta8874z_mono = {2, { TA8874Z_MONO_SET, TA8874Z_SEPARATION_DEFAULT}};
  1009. static audiocmd ta8874z_main = {2, { 0, TA8874Z_SEPARATION_DEFAULT}};
  1010. static audiocmd ta8874z_sub = {2, { TA8874Z_MODE_SUB, TA8874Z_SEPARATION_DEFAULT}};
  1011. static void ta8874z_setmode(struct CHIPSTATE *chip, int mode)
  1012. {
  1013. int update = 1;
  1014. audiocmd *t = NULL;
  1015. v4l_dbg(1, debug, &chip->c, "ta8874z_setmode(): mode: 0x%02x\n", mode);
  1016. switch(mode){
  1017. case VIDEO_SOUND_MONO:
  1018. t = &ta8874z_mono;
  1019. break;
  1020. case VIDEO_SOUND_STEREO:
  1021. t = &ta8874z_stereo;
  1022. break;
  1023. case VIDEO_SOUND_LANG1:
  1024. t = &ta8874z_main;
  1025. break;
  1026. case VIDEO_SOUND_LANG2:
  1027. t = &ta8874z_sub;
  1028. break;
  1029. default:
  1030. update = 0;
  1031. }
  1032. if(update)
  1033. chip_cmd(chip, "TA8874Z", t);
  1034. }
  1035. static int ta8874z_checkit(struct CHIPSTATE *chip)
  1036. {
  1037. int rc;
  1038. rc = chip_read(chip);
  1039. return ((rc & 0x1f) == 0x1f) ? 1 : 0;
  1040. }
  1041. /* ---------------------------------------------------------------------- */
  1042. /* audio chip descriptions - struct CHIPDESC */
  1043. /* insmod options to enable/disable individual audio chips */
  1044. static int tda8425 = 1;
  1045. static int tda9840 = 1;
  1046. static int tda9850 = 1;
  1047. static int tda9855 = 1;
  1048. static int tda9873 = 1;
  1049. static int tda9874a = 1;
  1050. static int tea6300 = 0; /* address clash with msp34xx */
  1051. static int tea6320 = 0; /* address clash with msp34xx */
  1052. static int tea6420 = 1;
  1053. static int pic16c54 = 1;
  1054. static int ta8874z = 0; /* address clash with tda9840 */
  1055. module_param(tda8425, int, 0444);
  1056. module_param(tda9840, int, 0444);
  1057. module_param(tda9850, int, 0444);
  1058. module_param(tda9855, int, 0444);
  1059. module_param(tda9873, int, 0444);
  1060. module_param(tda9874a, int, 0444);
  1061. module_param(tea6300, int, 0444);
  1062. module_param(tea6320, int, 0444);
  1063. module_param(tea6420, int, 0444);
  1064. module_param(pic16c54, int, 0444);
  1065. module_param(ta8874z, int, 0444);
  1066. static struct CHIPDESC chiplist[] = {
  1067. {
  1068. .name = "tda9840",
  1069. .id = I2C_DRIVERID_TDA9840,
  1070. .insmodopt = &tda9840,
  1071. .addr_lo = I2C_ADDR_TDA9840 >> 1,
  1072. .addr_hi = I2C_ADDR_TDA9840 >> 1,
  1073. .registers = 5,
  1074. .checkit = tda9840_checkit,
  1075. .getmode = tda9840_getmode,
  1076. .setmode = tda9840_setmode,
  1077. .checkmode = generic_checkmode,
  1078. .init = { 2, { TDA9840_TEST, TDA9840_TEST_INT1SN
  1079. /* ,TDA9840_SW, TDA9840_MONO */} }
  1080. },
  1081. {
  1082. .name = "tda9873h",
  1083. .id = I2C_DRIVERID_TDA9873,
  1084. .checkit = tda9873_checkit,
  1085. .insmodopt = &tda9873,
  1086. .addr_lo = I2C_ADDR_TDA985x_L >> 1,
  1087. .addr_hi = I2C_ADDR_TDA985x_H >> 1,
  1088. .registers = 3,
  1089. .flags = CHIP_HAS_INPUTSEL,
  1090. .getmode = tda9873_getmode,
  1091. .setmode = tda9873_setmode,
  1092. .checkmode = generic_checkmode,
  1093. .init = { 4, { TDA9873_SW, 0xa4, 0x06, 0x03 } },
  1094. .inputreg = TDA9873_SW,
  1095. .inputmute = TDA9873_MUTE | TDA9873_AUTOMUTE,
  1096. .inputmap = {0xa0, 0xa2, 0xa0, 0xa0},
  1097. .inputmask = TDA9873_INP_MASK|TDA9873_MUTE|TDA9873_AUTOMUTE,
  1098. },
  1099. {
  1100. .name = "tda9874h/a",
  1101. .id = I2C_DRIVERID_TDA9874,
  1102. .checkit = tda9874a_checkit,
  1103. .initialize = tda9874a_initialize,
  1104. .insmodopt = &tda9874a,
  1105. .addr_lo = I2C_ADDR_TDA9874 >> 1,
  1106. .addr_hi = I2C_ADDR_TDA9874 >> 1,
  1107. .getmode = tda9874a_getmode,
  1108. .setmode = tda9874a_setmode,
  1109. .checkmode = generic_checkmode,
  1110. },
  1111. {
  1112. .name = "tda9850",
  1113. .id = I2C_DRIVERID_TDA9850,
  1114. .insmodopt = &tda9850,
  1115. .addr_lo = I2C_ADDR_TDA985x_L >> 1,
  1116. .addr_hi = I2C_ADDR_TDA985x_H >> 1,
  1117. .registers = 11,
  1118. .getmode = tda985x_getmode,
  1119. .setmode = tda985x_setmode,
  1120. .init = { 8, { TDA9850_C4, 0x08, 0x08, TDA985x_STEREO, 0x07, 0x10, 0x10, 0x03 } }
  1121. },
  1122. {
  1123. .name = "tda9855",
  1124. .id = I2C_DRIVERID_TDA9855,
  1125. .insmodopt = &tda9855,
  1126. .addr_lo = I2C_ADDR_TDA985x_L >> 1,
  1127. .addr_hi = I2C_ADDR_TDA985x_H >> 1,
  1128. .registers = 11,
  1129. .flags = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE,
  1130. .leftreg = TDA9855_VL,
  1131. .rightreg = TDA9855_VR,
  1132. .bassreg = TDA9855_BA,
  1133. .treblereg = TDA9855_TR,
  1134. .volfunc = tda9855_volume,
  1135. .bassfunc = tda9855_bass,
  1136. .treblefunc = tda9855_treble,
  1137. .getmode = tda985x_getmode,
  1138. .setmode = tda985x_setmode,
  1139. .init = { 12, { 0, 0x6f, 0x6f, 0x0e, 0x07<<1, 0x8<<2,
  1140. TDA9855_MUTE | TDA9855_AVL | TDA9855_LOUD | TDA9855_INT,
  1141. TDA985x_STEREO | TDA9855_LINEAR | TDA9855_TZCM | TDA9855_VZCM,
  1142. 0x07, 0x10, 0x10, 0x03 }}
  1143. },
  1144. {
  1145. .name = "tea6300",
  1146. .id = I2C_DRIVERID_TEA6300,
  1147. .insmodopt = &tea6300,
  1148. .addr_lo = I2C_ADDR_TEA6300 >> 1,
  1149. .addr_hi = I2C_ADDR_TEA6300 >> 1,
  1150. .registers = 6,
  1151. .flags = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,
  1152. .leftreg = TEA6300_VR,
  1153. .rightreg = TEA6300_VL,
  1154. .bassreg = TEA6300_BA,
  1155. .treblereg = TEA6300_TR,
  1156. .volfunc = tea6300_shift10,
  1157. .bassfunc = tea6300_shift12,
  1158. .treblefunc = tea6300_shift12,
  1159. .inputreg = TEA6300_S,
  1160. .inputmap = { TEA6300_S_SA, TEA6300_S_SB, TEA6300_S_SC },
  1161. .inputmute = TEA6300_S_GMU,
  1162. },
  1163. {
  1164. .name = "tea6320",
  1165. .id = I2C_DRIVERID_TEA6300,
  1166. .initialize = tea6320_initialize,
  1167. .insmodopt = &tea6320,
  1168. .addr_lo = I2C_ADDR_TEA6300 >> 1,
  1169. .addr_hi = I2C_ADDR_TEA6300 >> 1,
  1170. .registers = 8,
  1171. .flags = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,
  1172. .leftreg = TEA6320_V,
  1173. .rightreg = TEA6320_V,
  1174. .bassreg = TEA6320_BA,
  1175. .treblereg = TEA6320_TR,
  1176. .volfunc = tea6320_volume,
  1177. .bassfunc = tea6320_shift11,
  1178. .treblefunc = tea6320_shift11,
  1179. .inputreg = TEA6320_S,
  1180. .inputmap = { TEA6320_S_SA, TEA6420_S_SB, TEA6300_S_SC, TEA6320_S_SD },
  1181. .inputmute = TEA6300_S_GMU,
  1182. },
  1183. {
  1184. .name = "tea6420",
  1185. .id = I2C_DRIVERID_TEA6420,
  1186. .insmodopt = &tea6420,
  1187. .addr_lo = I2C_ADDR_TEA6420 >> 1,
  1188. .addr_hi = I2C_ADDR_TEA6420 >> 1,
  1189. .registers = 1,
  1190. .flags = CHIP_HAS_INPUTSEL,
  1191. .inputreg = -1,
  1192. .inputmap = { TEA6420_S_SA, TEA6420_S_SB, TEA6420_S_SC },
  1193. .inputmute = TEA6300_S_GMU,
  1194. },
  1195. {
  1196. .name = "tda8425",
  1197. .id = I2C_DRIVERID_TDA8425,
  1198. .insmodopt = &tda8425,
  1199. .addr_lo = I2C_ADDR_TDA8425 >> 1,
  1200. .addr_hi = I2C_ADDR_TDA8425 >> 1,
  1201. .registers = 9,
  1202. .flags = CHIP_HAS_VOLUME | CHIP_HAS_BASSTREBLE | CHIP_HAS_INPUTSEL,
  1203. .leftreg = TDA8425_VL,
  1204. .rightreg = TDA8425_VR,
  1205. .bassreg = TDA8425_BA,
  1206. .treblereg = TDA8425_TR,
  1207. .volfunc = tda8425_shift10,
  1208. .bassfunc = tda8425_shift12,
  1209. .treblefunc = tda8425_shift12,
  1210. .inputreg = TDA8425_S1,
  1211. .inputmap = { TDA8425_S1_CH1, TDA8425_S1_CH1, TDA8425_S1_CH1 },
  1212. .inputmute = TDA8425_S1_OFF,
  1213. .setmode = tda8425_setmode,
  1214. .initialize = tda8425_initialize,
  1215. },
  1216. {
  1217. .name = "pic16c54 (PV951)",
  1218. .id = I2C_DRIVERID_PIC16C54_PV9,
  1219. .insmodopt = &pic16c54,
  1220. .addr_lo = I2C_ADDR_PIC16C54 >> 1,
  1221. .addr_hi = I2C_ADDR_PIC16C54>> 1,
  1222. .registers = 2,
  1223. .flags = CHIP_HAS_INPUTSEL,
  1224. .inputreg = PIC16C54_REG_MISC,
  1225. .inputmap = {PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_TUNER,
  1226. PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_LINE,
  1227. PIC16C54_MISC_SND_NOTMUTE|PIC16C54_MISC_SWITCH_LINE,
  1228. PIC16C54_MISC_SND_MUTE},
  1229. .inputmute = PIC16C54_MISC_SND_MUTE,
  1230. },
  1231. {
  1232. .name = "ta8874z",
  1233. .id = -1,
  1234. /*.id = I2C_DRIVERID_TA8874Z, */
  1235. .checkit = ta8874z_checkit,
  1236. .insmodopt = &ta8874z,
  1237. .addr_lo = I2C_ADDR_TDA9840 >> 1,
  1238. .addr_hi = I2C_ADDR_TDA9840 >> 1,
  1239. .registers = 2,
  1240. .getmode = ta8874z_getmode,
  1241. .setmode = ta8874z_setmode,
  1242. .checkmode = generic_checkmode,
  1243. .init = {2, { TA8874Z_MONO_SET, TA8874Z_SEPARATION_DEFAULT}},
  1244. },
  1245. { .name = NULL } /* EOF */
  1246. };
  1247. /* ---------------------------------------------------------------------- */
  1248. /* i2c registration */
  1249. static int chip_attach(struct i2c_adapter *adap, int addr, int kind)
  1250. {
  1251. struct CHIPSTATE *chip;
  1252. struct CHIPDESC *desc;
  1253. chip = kzalloc(sizeof(*chip),GFP_KERNEL);
  1254. if (!chip)
  1255. return -ENOMEM;
  1256. memcpy(&chip->c,&client_template,sizeof(struct i2c_client));
  1257. chip->c.adapter = adap;
  1258. chip->c.addr = addr;
  1259. i2c_set_clientdata(&chip->c, chip);
  1260. /* find description for the chip */
  1261. v4l_dbg(1, debug, &chip->c, "chip found @ 0x%x\n", addr<<1);
  1262. for (desc = chiplist; desc->name != NULL; desc++) {
  1263. if (0 == *(desc->insmodopt))
  1264. continue;
  1265. if (addr < desc->addr_lo ||
  1266. addr > desc->addr_hi)
  1267. continue;
  1268. if (desc->checkit && !desc->checkit(chip))
  1269. continue;
  1270. break;
  1271. }
  1272. if (desc->name == NULL) {
  1273. v4l_dbg(1, debug, &chip->c, "no matching chip description found\n");
  1274. return -EIO;
  1275. }
  1276. v4l_info(&chip->c, "%s found @ 0x%x (%s)\n", desc->name, addr<<1, adap->name);
  1277. if (desc->flags) {
  1278. v4l_dbg(1, debug, &chip->c, "matches:%s%s%s.\n",
  1279. (desc->flags & CHIP_HAS_VOLUME) ? " volume" : "",
  1280. (desc->flags & CHIP_HAS_BASSTREBLE) ? " bass/treble" : "",
  1281. (desc->flags & CHIP_HAS_INPUTSEL) ? " audiomux" : "");
  1282. }
  1283. /* fill required data structures */
  1284. strcpy(chip->c.name, desc->name);
  1285. chip->type = desc-chiplist;
  1286. chip->shadow.count = desc->registers+1;
  1287. chip->prevmode = -1;
  1288. chip->audmode = V4L2_TUNER_MODE_LANG1;
  1289. /* register */
  1290. i2c_attach_client(&chip->c);
  1291. /* initialization */
  1292. if (desc->initialize != NULL)
  1293. desc->initialize(chip);
  1294. else
  1295. chip_cmd(chip,"init",&desc->init);
  1296. if (desc->flags & CHIP_HAS_VOLUME) {
  1297. chip->left = desc->leftinit ? desc->leftinit : 65535;
  1298. chip->right = desc->rightinit ? desc->rightinit : 65535;
  1299. chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
  1300. chip_write(chip,desc->rightreg,desc->volfunc(chip->right));
  1301. }
  1302. if (desc->flags & CHIP_HAS_BASSTREBLE) {
  1303. chip->treble = desc->trebleinit ? desc->trebleinit : 32768;
  1304. chip->bass = desc->bassinit ? desc->bassinit : 32768;
  1305. chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass));
  1306. chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble));
  1307. }
  1308. chip->thread = NULL;
  1309. if (desc->checkmode) {
  1310. /* start async thread */
  1311. init_timer(&chip->wt);
  1312. chip->wt.function = chip_thread_wake;
  1313. chip->wt.data = (unsigned long)chip;
  1314. chip->thread = kthread_run(chip_thread, chip, chip->c.name);
  1315. if (IS_ERR(chip->thread)) {
  1316. v4l_warn(&chip->c, "%s: failed to create kthread\n",
  1317. chip->c.name);
  1318. chip->thread = NULL;
  1319. }
  1320. }
  1321. return 0;
  1322. }
  1323. static int chip_probe(struct i2c_adapter *adap)
  1324. {
  1325. /* don't attach on saa7146 based cards,
  1326. because dedicated drivers are used */
  1327. if ((adap->id == I2C_HW_SAA7146))
  1328. return 0;
  1329. if (adap->class & I2C_CLASS_TV_ANALOG)
  1330. return i2c_probe(adap, &addr_data, chip_attach);
  1331. return 0;
  1332. }
  1333. static int chip_detach(struct i2c_client *client)
  1334. {
  1335. struct CHIPSTATE *chip = i2c_get_clientdata(client);
  1336. del_timer_sync(&chip->wt);
  1337. if (chip->thread) {
  1338. /* shutdown async thread */
  1339. kthread_stop(chip->thread);
  1340. chip->thread = NULL;
  1341. }
  1342. i2c_detach_client(&chip->c);
  1343. kfree(chip);
  1344. return 0;
  1345. }
  1346. static int tvaudio_set_ctrl(struct CHIPSTATE *chip, struct v4l2_control *ctrl)
  1347. {
  1348. struct CHIPDESC *desc = chiplist + chip->type;
  1349. switch (ctrl->id) {
  1350. case V4L2_CID_AUDIO_MUTE:
  1351. if (ctrl->value < 0 || ctrl->value >= 2)
  1352. return -ERANGE;
  1353. chip->muted = ctrl->value;
  1354. if (chip->muted)
  1355. chip_write_masked(chip,desc->inputreg,desc->inputmute,desc->inputmask);
  1356. else
  1357. chip_write_masked(chip,desc->inputreg,
  1358. desc->inputmap[chip->input],desc->inputmask);
  1359. break;
  1360. default:
  1361. return -EINVAL;
  1362. }
  1363. return 0;
  1364. }
  1365. /* ---------------------------------------------------------------------- */
  1366. /* video4linux interface */
  1367. static int chip_command(struct i2c_client *client,
  1368. unsigned int cmd, void *arg)
  1369. {
  1370. struct CHIPSTATE *chip = i2c_get_clientdata(client);
  1371. struct CHIPDESC *desc = chiplist + chip->type;
  1372. v4l_dbg(1, debug, &chip->c, "%s: chip_command 0x%x\n", chip->c.name, cmd);
  1373. switch (cmd) {
  1374. case AUDC_SET_RADIO:
  1375. chip->radio = 1;
  1376. chip->watch_stereo = 0;
  1377. /* del_timer(&chip->wt); */
  1378. break;
  1379. /* --- v4l ioctls --- */
  1380. /* take care: bttv does userspace copying, we'll get a
  1381. kernel pointer here... */
  1382. case VIDIOCGAUDIO:
  1383. {
  1384. struct video_audio *va = arg;
  1385. if (desc->flags & CHIP_HAS_VOLUME) {
  1386. va->flags |= VIDEO_AUDIO_VOLUME;
  1387. va->volume = max(chip->left,chip->right);
  1388. if (va->volume)
  1389. va->balance = (32768*min(chip->left,chip->right))/
  1390. va->volume;
  1391. else
  1392. va->balance = 32768;
  1393. }
  1394. if (desc->flags & CHIP_HAS_BASSTREBLE) {
  1395. va->flags |= VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
  1396. va->bass = chip->bass;
  1397. va->treble = chip->treble;
  1398. }
  1399. if (!chip->radio) {
  1400. if (desc->getmode)
  1401. va->mode = desc->getmode(chip);
  1402. else
  1403. va->mode = VIDEO_SOUND_MONO;
  1404. }
  1405. break;
  1406. }
  1407. case VIDIOCSAUDIO:
  1408. {
  1409. struct video_audio *va = arg;
  1410. if (desc->flags & CHIP_HAS_VOLUME) {
  1411. chip->left = (min(65536 - va->balance,32768) *
  1412. va->volume) / 32768;
  1413. chip->right = (min(va->balance,(__u16)32768) *
  1414. va->volume) / 32768;
  1415. chip_write(chip,desc->leftreg,desc->volfunc(chip->left));
  1416. chip_write(chip,desc->rightreg,desc->volfunc(chip->right));
  1417. }
  1418. if (desc->flags & CHIP_HAS_BASSTREBLE) {
  1419. chip->bass = va->bass;
  1420. chip->treble = va->treble;
  1421. chip_write(chip,desc->bassreg,desc->bassfunc(chip->bass));
  1422. chip_write(chip,desc->treblereg,desc->treblefunc(chip->treble));
  1423. }
  1424. if (desc->setmode && va->mode) {
  1425. chip->watch_stereo = 0;
  1426. /* del_timer(&chip->wt); */
  1427. chip->mode = va->mode;
  1428. desc->setmode(chip,va->mode);
  1429. }
  1430. break;
  1431. }
  1432. case VIDIOC_S_CTRL:
  1433. return tvaudio_set_ctrl(chip, arg);
  1434. case VIDIOC_INT_G_AUDIO_ROUTING:
  1435. {
  1436. struct v4l2_routing *rt = arg;
  1437. rt->input = chip->input;
  1438. rt->output = 0;
  1439. break;
  1440. }
  1441. case VIDIOC_INT_S_AUDIO_ROUTING:
  1442. {
  1443. struct v4l2_routing *rt = arg;
  1444. if (!(desc->flags & CHIP_HAS_INPUTSEL) || rt->input >= 4)
  1445. return -EINVAL;
  1446. /* There are four inputs: tuner, radio, extern and intern. */
  1447. chip->input = rt->input;
  1448. if (chip->muted)
  1449. break;
  1450. chip_write_masked(chip, desc->inputreg,
  1451. desc->inputmap[chip->input], desc->inputmask);
  1452. break;
  1453. }
  1454. case VIDIOC_S_TUNER:
  1455. {
  1456. struct v4l2_tuner *vt = arg;
  1457. int mode = 0;
  1458. if (chip->radio)
  1459. break;
  1460. switch (vt->audmode) {
  1461. case V4L2_TUNER_MODE_MONO:
  1462. mode = VIDEO_SOUND_MONO;
  1463. break;
  1464. case V4L2_TUNER_MODE_STEREO:
  1465. case V4L2_TUNER_MODE_LANG1_LANG2:
  1466. mode = VIDEO_SOUND_STEREO;
  1467. break;
  1468. case V4L2_TUNER_MODE_LANG1:
  1469. mode = VIDEO_SOUND_LANG1;
  1470. break;
  1471. case V4L2_TUNER_MODE_LANG2:
  1472. mode = VIDEO_SOUND_LANG2;
  1473. break;
  1474. default:
  1475. return -EINVAL;
  1476. }
  1477. chip->audmode = vt->audmode;
  1478. if (desc->setmode && mode) {
  1479. chip->watch_stereo = 0;
  1480. /* del_timer(&chip->wt); */
  1481. chip->mode = mode;
  1482. desc->setmode(chip, mode);
  1483. }
  1484. break;
  1485. }
  1486. case VIDIOC_G_TUNER:
  1487. {
  1488. struct v4l2_tuner *vt = arg;
  1489. int mode = VIDEO_SOUND_MONO;
  1490. if (chip->radio)
  1491. break;
  1492. vt->audmode = chip->audmode;
  1493. vt->rxsubchans = 0;
  1494. vt->capability = V4L2_TUNER_CAP_STEREO |
  1495. V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
  1496. if (desc->getmode)
  1497. mode = desc->getmode(chip);
  1498. if (mode & VIDEO_SOUND_MONO)
  1499. vt->rxsubchans |= V4L2_TUNER_SUB_MONO;
  1500. if (mode & VIDEO_SOUND_STEREO)
  1501. vt->rxsubchans |= V4L2_TUNER_SUB_STEREO;
  1502. /* Note: for SAP it should be mono/lang2 or stereo/lang2.
  1503. When this module is converted fully to v4l2, then this
  1504. should change for those chips that can detect SAP. */
  1505. if (mode & VIDEO_SOUND_LANG1)
  1506. vt->rxsubchans = V4L2_TUNER_SUB_LANG1 |
  1507. V4L2_TUNER_SUB_LANG2;
  1508. break;
  1509. }
  1510. case VIDIOCSCHAN:
  1511. case VIDIOC_S_STD:
  1512. chip->radio = 0;
  1513. break;
  1514. case VIDIOCSFREQ:
  1515. case VIDIOC_S_FREQUENCY:
  1516. chip->mode = 0; /* automatic */
  1517. if (desc->checkmode) {
  1518. desc->setmode(chip,VIDEO_SOUND_MONO);
  1519. if (chip->prevmode != VIDEO_SOUND_MONO)
  1520. chip->prevmode = -1; /* reset previous mode */
  1521. mod_timer(&chip->wt, jiffies+2*HZ);
  1522. /* the thread will call checkmode() later */
  1523. }
  1524. break;
  1525. }
  1526. return 0;
  1527. }
  1528. static struct i2c_driver driver = {
  1529. .driver = {
  1530. .name = "tvaudio",
  1531. },
  1532. .id = I2C_DRIVERID_TVAUDIO,
  1533. .attach_adapter = chip_probe,
  1534. .detach_client = chip_detach,
  1535. .command = chip_command,
  1536. };
  1537. static struct i2c_client client_template =
  1538. {
  1539. .name = "(unset)",
  1540. .driver = &driver,
  1541. };
  1542. static int __init audiochip_init_module(void)
  1543. {
  1544. struct CHIPDESC *desc;
  1545. if (debug) {
  1546. printk(KERN_INFO "tvaudio: TV audio decoder + audio/video mux driver\n");
  1547. printk(KERN_INFO "tvaudio: known chips: ");
  1548. for (desc = chiplist; desc->name != NULL; desc++)
  1549. printk("%s%s", (desc == chiplist) ? "" : ", ", desc->name);
  1550. printk("\n");
  1551. }
  1552. return i2c_add_driver(&driver);
  1553. }
  1554. static void __exit audiochip_cleanup_module(void)
  1555. {
  1556. i2c_del_driver(&driver);
  1557. }
  1558. module_init(audiochip_init_module);
  1559. module_exit(audiochip_cleanup_module);
  1560. /*
  1561. * Local variables:
  1562. * c-basic-offset: 8
  1563. * End:
  1564. */