tvaudio.c 53 KB

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