tda7432.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * For the STS-Thompson TDA7432 audio processor chip
  3.  * 
  4.  * Handles audio functions: volume, balance, tone, loudness
  5.  * This driver will not complain if used with any 
  6.  * other i2c device with the same address.
  7.  *
  8.  * Muting and tone control by Jonathan Isom <jisom@ematic.com>
  9.  *
  10.  * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
  11.  * This code is placed under the terms of the GNU General Public License
  12.  * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
  13.  * Which was based on tda8425.c by Greg Alexander (c) 1998
  14.  *
  15.  * OPTIONS:
  16.  * debug    - set to 1 if you'd like to see debug messages
  17.  *            set to 2 if you'd like to be inundated with debug messages
  18.  *
  19.  * loudness - set between 0 and 15 for varying degrees of loudness effect
  20.  *
  21.  * maxvol   - set maximium volume to +20db (1), default is 0db(0)
  22.  *
  23.  *
  24.  *  Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
  25.  *   store if muted so we can return it
  26.  *   change balance only if flaged to
  27.  *  Revision: 0.6 - added tone controls
  28.  *  Revision: 0.5 - Fixed odd balance problem
  29.  *  Revision: 0.4 - added muting
  30.  *  Revision: 0.3 - Fixed silly reversed volume controls.  :)
  31.  *  Revision: 0.2 - Cleaned up #defines
  32.  * fixed volume control
  33.  *          Added I2C_DRIVERID_TDA7432
  34.  * added loudness insmod control
  35.  *  Revision: 0.1 - initial version
  36.  */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/kernel.h>
  40. #include <linux/sched.h>
  41. #include <linux/string.h>
  42. #include <linux/timer.h>
  43. #include <linux/delay.h>
  44. #include <linux/errno.h>
  45. #include <linux/slab.h>
  46. #include <linux/videodev.h>
  47. #include <linux/i2c.h>
  48. #include <linux/i2c-algo-bit.h>
  49. #include "bttv.h"
  50. #include "audiochip.h"
  51. #include "id.h"
  52. MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
  53. MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
  54. MODULE_LICENSE("GPL");
  55. MODULE_PARM(debug,"i");
  56. MODULE_PARM(loudness,"i");
  57. MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
  58. MODULE_PARM(maxvol,"i");
  59. static int maxvol = 0;
  60. static int loudness = 0; /* disable loudness by default */
  61. static int debug = 0;  /* insmod parameter */
  62. /* Address to scan (I2C address of this chip) */
  63. static unsigned short normal_i2c[] = {
  64. I2C_TDA7432 >> 1,
  65. I2C_CLIENT_END};
  66. static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
  67. static unsigned short probe[2]        = { I2C_CLIENT_END, I2C_CLIENT_END };
  68. static unsigned short probe_range[2]  = { I2C_CLIENT_END, I2C_CLIENT_END };
  69. static unsigned short ignore[2]       = { I2C_CLIENT_END, I2C_CLIENT_END };
  70. static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
  71. static unsigned short force[2]        = { I2C_CLIENT_END, I2C_CLIENT_END };
  72. static struct i2c_client_address_data addr_data = {
  73. normal_i2c, normal_i2c_range, 
  74. probe, probe_range, 
  75. ignore, ignore_range, 
  76. force
  77. };
  78. /* Structure of address and subaddresses for the tda7432 */
  79. struct tda7432 {
  80. int addr;
  81. int input;
  82. int volume;
  83. int muted;
  84. int bass, treble;
  85. int lf, lr, rf, rr;
  86. int loud;
  87. struct i2c_client c;
  88. };
  89. static struct i2c_driver driver;
  90. static struct i2c_client client_template;
  91. #define dprintk  if (debug) printk
  92. #define d2printk if (debug > 1) printk
  93. /* The TDA7432 is made by STS-Thompson
  94.  * http://www.st.com
  95.  * http://us.st.com/stonline/books/pdf/docs/4056.pdf
  96.  *
  97.  * TDA7432: I2C-bus controlled basic audio processor
  98.  *
  99.  * The TDA7432 controls basic audio functions like volume, balance,
  100.  * and tone control (including loudness).  It also has four channel
  101.  * output (for front and rear).  Since most vidcap cards probably
  102.  * don't have 4 channel output, this driver will set front & rear
  103.  * together (no independent control).
  104.  */ 
  105. /* Subaddresses for TDA7432 */
  106. #define TDA7432_IN 0x00 /* Input select                 */
  107. #define TDA7432_VL 0x01 /* Volume                       */
  108. #define TDA7432_TN 0x02 /* Bass, Treble (Tone)          */
  109. #define TDA7432_LF 0x03 /* Attenuation LF (Left Front)  */
  110. #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear)   */
  111. #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
  112. #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear)  */
  113. #define TDA7432_LD 0x07 /* Loudness                     */
  114. /* Masks for bits in TDA7432 subaddresses */
  115. /* Many of these not used - just for documentation */
  116. /* Subaddress 0x00 - Input selection and bass control */
  117. /* Bits 0,1,2 control input:
  118.  * 0x00 - Stereo input
  119.  * 0x02 - Mono input
  120.  * 0x03 - Mute  (Using Attenuators Plays better with modules)
  121.  * Mono probably isn't used - I'm guessing only the stereo
  122.  * input is connected on most cards, so we'll set it to stereo.
  123.  * 
  124.  * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
  125.  * Bit 4 controls bass range: 0/1 is extended/standard bass range
  126.  * 
  127.  * Highest 3 bits not used
  128.  */
  129.  
  130. #define TDA7432_STEREO_IN 0
  131. #define TDA7432_MONO_IN 2 /* Probably won't be used */
  132. #define TDA7432_BASS_SYM 1 << 3
  133. #define TDA7432_BASS_NORM 1 << 4
  134. /* Subaddress 0x01 - Volume */
  135. /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
  136.  * Recommended maximum is +20 dB
  137.  *
  138.  * +32dB: 0x00
  139.  * +20dB: 0x0c
  140.  *   0dB: 0x20
  141.  * -79dB: 0x6f
  142.  *
  143.  * MSB (bit 7) controls loudness: 1/0 is loudness on/off
  144.  */
  145. #define TDA7432_VOL_0DB 0x20
  146. #define TDA7432_LD_ON 1 << 7
  147. /* Subaddress 0x02 - Tone control */ 
  148. /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
  149.  * 0x0 is 14dB, 0x7 is 0dB
  150.  *
  151.  * Bit 3 controls treble attenuation/gain (sign)
  152.  * 1 = gain (+)
  153.  * 0 = attenuation (-)
  154.  *
  155.  * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
  156.  * (This is only true for normal base range, set in 0x00)
  157.  * 0x0 << 4 is 14dB, 0x7 is 0dB
  158.  * 
  159.  * Bit 7 controls bass attenuation/gain (sign)
  160.  * 1 << 7 = gain (+)
  161.  * 0 << 7 = attenuation (-) 
  162.  *
  163.  * Example:
  164.  * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
  165.  */
  166. #define TDA7432_TREBLE_0DB 0xf 
  167. #define TDA7432_TREBLE 7
  168. #define TDA7432_TREBLE_GAIN 1 << 3
  169. #define TDA7432_BASS_0DB 0xf
  170. #define TDA7432_BASS 7 << 4
  171. #define TDA7432_BASS_GAIN 1 << 7
  172.  
  173. /* Subaddress 0x03 - Left  Front attenuation */
  174. /* Subaddress 0x04 - Left  Rear  attenuation */
  175. /* Subaddress 0x05 - Right Front attenuation */
  176. /* Subaddress 0x06 - Right Rear  attenuation */
  177. /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
  178.  * in 1.5dB steps.
  179.  *
  180.  * 0x00 is     0dB
  181.  * 0x1f is -37.5dB
  182.  *
  183.  * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
  184.  * We'll use the mute on the input, though (above) 
  185.  * Bits 6,7 unused
  186.  */
  187. #define TDA7432_ATTEN_0DB 0x00
  188. #define TDA7432_MUTE        0x1 << 5
  189. /* Subaddress 0x07 - Loudness Control */
  190. /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
  191.  * when bit 4 is NOT set
  192.  *
  193.  * 0x0 is   0dB
  194.  * 0xf is -15dB
  195.  *
  196.  * If bit 4 is set, then there is a flat attenuation according to
  197.  * the lower 4 bits, as above.
  198.  *
  199.  * Bits 5,6,7 unused
  200.  */
  201.  
  202.  
  203. /* Begin code */
  204. static int tda7432_write(struct i2c_client *client, int subaddr, int val)
  205. {
  206. unsigned char buffer[2];
  207. d2printk("tda7432: In tda7432_writen");
  208. dprintk("tda7432: Writing %d 0x%xn", subaddr, val);
  209. buffer[0] = subaddr;
  210. buffer[1] = val;
  211. if (2 != i2c_master_send(client,buffer,2)) {
  212. printk(KERN_WARNING "tda7432: I/O error, trying (write %d 0x%x)n",
  213.        subaddr, val);
  214. return -1;
  215. }
  216. return 0;
  217. }
  218. /* I don't think we ever actually _read_ the chip... */
  219. #if 0
  220. static int tda7432_read(struct i2c_client *client)
  221. {
  222. unsigned char buffer;
  223. d2printk("tda7432: In tda7432_readn");
  224. if (1 != i2c_master_recv(client,&buffer,1)) {
  225. printk(KERN_WARNING "tda7432: I/O error, trying (read)n");
  226. return -1;
  227. }
  228. dprintk("tda7432: Read 0x%02xn", buffer); 
  229. return buffer;
  230. }
  231. #endif
  232. static int tda7432_set(struct i2c_client *client)
  233. {
  234. struct tda7432 *t = client->data;
  235. unsigned char buf[16];
  236. d2printk("tda7432: In tda7432_setn");
  237. dprintk(KERN_INFO 
  238. "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)n",
  239. t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
  240. buf[0]  = TDA7432_IN;
  241. buf[1]  = t->input;
  242. buf[2]  = t->volume;
  243. buf[3]  = t->bass;
  244. buf[4] = t->treble;
  245. buf[5]  = t->lf;
  246. buf[6]  = t->lr;
  247. buf[7]  = t->rf;
  248. buf[8]  = t->rr;
  249. buf[9]  = t->loud;
  250. if (10 != i2c_master_send(client,buf,10)) {
  251. printk(KERN_WARNING "tda7432: I/O error, trying tda7432_setn");
  252. return -1;
  253. }
  254. return 0;
  255. }
  256. static void do_tda7432_init(struct i2c_client *client)
  257. {
  258. struct tda7432 *t = client->data;
  259. d2printk("tda7432: In tda7432_initn");
  260. t->input  = TDA7432_STEREO_IN |  /* Main (stereo) input   */
  261.     TDA7432_BASS_SYM  |  /* Symmetric bass cut    */
  262.     TDA7432_BASS_NORM;   /* Normal bass range     */ 
  263. t->volume =  0x3b ;  /* -27dB Volume            */
  264. if (loudness)  /* Turn loudness on?     */
  265. t->volume |= TDA7432_LD_ON;
  266. t->muted    = VIDEO_AUDIO_MUTE;
  267. t->treble   = TDA7432_TREBLE_0DB; /* 0dB Treble            */
  268. t->bass = TDA7432_BASS_0DB;   /* 0dB Bass              */
  269. t->lf     = TDA7432_ATTEN_0DB;  /* 0dB attenuation       */
  270. t->lr     = TDA7432_ATTEN_0DB;  /* 0dB attenuation       */
  271. t->rf     = TDA7432_ATTEN_0DB;  /* 0dB attenuation       */
  272. t->rr     = TDA7432_ATTEN_0DB;  /* 0dB attenuation       */
  273. t->loud   = loudness;  /* insmod parameter      */
  274. tda7432_set(client);
  275. }
  276. /* *********************** *
  277.  * i2c interface functions *
  278.  * *********************** */
  279. static int tda7432_attach(struct i2c_adapter *adap, int addr,
  280.   unsigned short flags, int kind)
  281. {
  282. struct tda7432 *t;
  283. struct i2c_client *client;
  284. d2printk("tda7432: In tda7432_attachn");
  285. t = kmalloc(sizeof *t,GFP_KERNEL);
  286. if (!t)
  287. return -ENOMEM;
  288. memset(t,0,sizeof *t);
  289. client = &t->c;
  290.         memcpy(client,&client_template,sizeof(struct i2c_client));
  291.         client->adapter = adap;
  292.         client->addr = addr;
  293. client->data = t;
  294. do_tda7432_init(client);
  295. MOD_INC_USE_COUNT;
  296. strcpy(client->name,"TDA7432");
  297. printk(KERN_INFO "tda7432: initn");
  298. i2c_attach_client(client);
  299. return 0;
  300. }
  301. static int tda7432_probe(struct i2c_adapter *adap)
  302. {
  303. if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
  304. return i2c_probe(adap, &addr_data, tda7432_attach);
  305. return 0;
  306. }
  307. static int tda7432_detach(struct i2c_client *client)
  308. {
  309. struct tda7432 *t  = client->data;
  310. do_tda7432_init(client);
  311. i2c_detach_client(client);
  312. kfree(t);
  313. MOD_DEC_USE_COUNT;
  314. return 0;
  315. }
  316. static int tda7432_command(struct i2c_client *client,
  317.    unsigned int cmd, void *arg)
  318. {
  319. struct tda7432 *t = client->data;
  320. d2printk("tda7432: In tda7432_commandn");
  321. switch (cmd) {
  322. /* --- v4l ioctls --- */
  323. /* take care: bttv does userspace copying, we'll get a
  324.    kernel pointer here... */
  325. /* Query card - scale from TDA7432 settings to V4L settings */
  326. case VIDIOCGAUDIO:
  327. {
  328. struct video_audio *va = arg;
  329. dprintk("tda7432: VIDIOCGAUDIOn");
  330. va->flags |= VIDEO_AUDIO_VOLUME |
  331. VIDEO_AUDIO_BASS |
  332. VIDEO_AUDIO_TREBLE |
  333. VIDEO_AUDIO_MUTABLE;
  334. if (t->muted)
  335. va->flags |= VIDEO_AUDIO_MUTE;
  336. va->mode |= VIDEO_SOUND_STEREO;
  337. /* Master volume control
  338.  * V4L volume is min 0, max 65535
  339.  * TDA7432 Volume: 
  340.  * Min (-79dB) is 0x6f
  341.  * Max (+20dB) is 0x07 (630)
  342.  * Max (0dB) is 0x20 (829)
  343.  * (Mask out bit 7 of vol - it's for the loudness setting)
  344.  */
  345. if(!maxvol){  /* max +20db */
  346. va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
  347. } else {     /* max 0db   */
  348. va->volume = (int )(( 0x6f - (t->volume & 0x7F) ) * 829.557);
  349. }
  350. /* Balance depends on L,R attenuation
  351.  * V4L balance is 0 to 65535, middle is 32768
  352.  * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
  353.  * to scale up to V4L numbers, mult by 1057
  354.  * attenuation exists for lf, lr, rf, rr
  355.  * we use only lf and rf (front channels)
  356.  */
  357. if ( (t->lf) < (t->rf) )
  358. /* right is attenuated, balance shifted left */
  359. va->balance = (32768 - 1057*(t->rf));
  360. else
  361. /* left is attenuated, balance shifted right */
  362. va->balance = (32768 + 1057*(t->lf));
  363. /* Bass/treble 4 bits each */
  364. va->bass=t->bass;
  365. if(va->bass >= 0x8)
  366. va->bass = ~(va->bass - 0x8) & 0xf;
  367. va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
  368. va->treble=t->treble;
  369. if(va->treble >= 0x8)
  370. va->treble = ~(va->treble - 0x8) & 0xf;
  371. va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
  372. break; /* VIDIOCGAUDIO case */
  373. }
  374. /* Set card - scale from V4L settings to TDA7432 settings */
  375. case VIDIOCSAUDIO:
  376. {
  377. struct video_audio *va = arg;
  378. dprintk("tda7432: VIDEOCSAUDIOn");
  379. if(va->flags & VIDEO_AUDIO_VOLUME){
  380. if(!maxvol){ /* max +20db */
  381. t->volume = 0x6f - ( (va->volume)/630 );
  382. } else {    /* max 0db   */
  383. t->volume = 0x6f - ((int) (va->volume)/829.557 );
  384. }
  385. if (loudness) /* Turn on the loudness bit */
  386. t->volume |= TDA7432_LD_ON;
  387. tda7432_write(client,TDA7432_VL, t->volume);
  388. }
  389. if(va->flags & VIDEO_AUDIO_BASS)
  390. {
  391. t->bass = va->bass >> 12;
  392. if(t->bass>= 0x8)
  393. t->bass = (~t->bass & 0xf) + 0x8 ;
  394. }
  395. if(va->flags & VIDEO_AUDIO_TREBLE)
  396. {
  397. t->treble= va->treble >> 12;
  398. if(t->treble>= 0x8)
  399. t->treble = (~t->treble & 0xf) + 0x8 ;
  400. }
  401. if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
  402. tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
  403. if(va->flags & VIDEO_AUDIO_BALANCE) {
  404. if (va->balance < 32768) 
  405. {
  406. /* shifted to left, attenuate right */
  407. t->rr = (32768 - va->balance)/1057;
  408. t->rf = t->rr;
  409. t->lr = TDA7432_ATTEN_0DB;
  410. t->lf = TDA7432_ATTEN_0DB;
  411. }
  412. else if(va->balance > 32769)
  413. {
  414. /* shifted to right, attenuate left */
  415. t->lf = (va->balance - 32768)/1057;
  416. t->lr = t->lf;
  417. t->rr = TDA7432_ATTEN_0DB;
  418. t->rf = TDA7432_ATTEN_0DB;
  419. }
  420. else 
  421. {
  422. /* centered */
  423. t->rr = TDA7432_ATTEN_0DB;
  424. t->rf = TDA7432_ATTEN_0DB;
  425. t->lf = TDA7432_ATTEN_0DB;
  426. t->lr = TDA7432_ATTEN_0DB;
  427. }
  428. }
  429.   t->muted=(va->flags & VIDEO_AUDIO_MUTE);
  430. if (t->muted)
  431. {
  432. /* Mute & update balance*/
  433. tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
  434. tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
  435. tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
  436. tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
  437. } else {
  438. tda7432_write(client,TDA7432_LF, t->lf);
  439. tda7432_write(client,TDA7432_LR, t->lr);
  440. tda7432_write(client,TDA7432_RF, t->rf);
  441. tda7432_write(client,TDA7432_RR, t->rr);
  442. }
  443. break;
  444. } /* end of VIDEOCSAUDIO case */
  445. default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
  446. /* nothing */
  447. d2printk("tda7432: Defaultn");
  448. } /* end of (cmd) switch */
  449. return 0;
  450. }
  451. static struct i2c_driver driver = {
  452.         "i2c tda7432 driver",
  453. I2C_DRIVERID_TDA7432,
  454.         I2C_DF_NOTIFY,
  455. tda7432_probe,
  456.         tda7432_detach,
  457.         tda7432_command,
  458. };
  459. static struct i2c_client client_template =
  460. {
  461.         "(unset)", /* name */
  462.         -1,
  463.         0,
  464.         0,
  465.         NULL,
  466.         &driver
  467. };
  468. static int tda7432_init(void)
  469. {
  470. if ( (loudness < 0) || (loudness > 15) )
  471. {
  472. printk(KERN_ERR "tda7432: loudness parameter must be between 0 and 15n");
  473. return -EINVAL;
  474. }
  475. i2c_add_driver(&driver);
  476. return 0;
  477. }
  478. static void tda7432_fini(void)
  479. {
  480. i2c_del_driver(&driver);
  481. }
  482. module_init(tda7432_init);
  483. module_exit(tda7432_fini);
  484. /*
  485.  * Local variables:
  486.  * c-basic-offset: 8
  487.  * End:
  488.  */