tea6420.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. tea6420 - i2c-driver for the tea6420 by SGS Thomson
  3. Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
  4. The tea6420 is a bus controlled audio-matrix with 5 stereo inputs,
  5. 4 stereo outputs and gain control for each output.
  6. It is cascadable, i.e. it can be found at the adresses 0x98
  7. and 0x9a on the i2c-bus.
  8. For detailed informations download the specifications directly
  9. from SGS Thomson at http://www.st.com
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/ioctl.h>
  24. #include <linux/i2c.h>
  25. #include "tea6420.h"
  26. static int debug = 0; /* insmod parameter */
  27. module_param(debug, int, 0644);
  28. MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
  29. #define dprintk(args...) \
  30. do { if (debug) { printk("%s: %s()[%d]: ",__stringify(KBUILD_MODNAME), __FUNCTION__, __LINE__); printk(args); } } while (0)
  31. /* addresses to scan, found only at 0x4c and/or 0x4d (7-Bit) */
  32. static unsigned short normal_i2c[] = { I2C_TEA6420_1, I2C_TEA6420_2, I2C_CLIENT_END };
  33. /* magic definition of all other variables and things */
  34. I2C_CLIENT_INSMOD;
  35. static struct i2c_driver driver;
  36. static struct i2c_client client_template;
  37. /* make a connection between the input 'i' and the output 'o'
  38. with gain 'g' for the tea6420-client 'client' (note: i = 6 means 'mute') */
  39. static int tea6420_switch(struct i2c_client *client, int i, int o, int g)
  40. {
  41. u8 byte = 0;
  42. int ret;
  43. dprintk("adr:0x%02x, i:%d, o:%d, g:%d\n", client->addr, i, o, g);
  44. /* check if the paramters are valid */
  45. if (i < 1 || i > 6 || o < 1 || o > 4 || g < 0 || g > 6 || g % 2 != 0)
  46. return -1;
  47. byte = ((o - 1) << 5);
  48. byte |= (i - 1);
  49. /* to understand this, have a look at the tea6420-specs (p.5) */
  50. switch (g) {
  51. case 0:
  52. byte |= (3 << 3);
  53. break;
  54. case 2:
  55. byte |= (2 << 3);
  56. break;
  57. case 4:
  58. byte |= (1 << 3);
  59. break;
  60. case 6:
  61. break;
  62. }
  63. ret = i2c_smbus_write_byte(client, byte);
  64. if (ret) {
  65. dprintk("i2c_smbus_write_byte() failed, ret:%d\n", ret);
  66. return -EIO;
  67. }
  68. return 0;
  69. }
  70. /* this function is called by i2c_probe */
  71. static int tea6420_detect(struct i2c_adapter *adapter, int address, int kind)
  72. {
  73. struct i2c_client *client;
  74. int err = 0, i = 0;
  75. /* let's see whether this adapter can support what we need */
  76. if (0 == i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE)) {
  77. return 0;
  78. }
  79. /* allocate memory for client structure */
  80. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  81. if (0 == client) {
  82. return -ENOMEM;
  83. }
  84. memset(client, 0x0, sizeof(struct i2c_client));
  85. /* fill client structure */
  86. memcpy(client, &client_template, sizeof(struct i2c_client));
  87. client->addr = address;
  88. client->adapter = adapter;
  89. /* tell the i2c layer a new client has arrived */
  90. if (0 != (err = i2c_attach_client(client))) {
  91. kfree(client);
  92. return err;
  93. }
  94. /* set initial values: set "mute"-input to all outputs at gain 0 */
  95. err = 0;
  96. for (i = 1; i < 5; i++) {
  97. err += tea6420_switch(client, 6, i, 0);
  98. }
  99. if (err) {
  100. dprintk("could not initialize tea6420\n");
  101. kfree(client);
  102. return -ENODEV;
  103. }
  104. printk("tea6420: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
  105. return 0;
  106. }
  107. static int attach(struct i2c_adapter *adapter)
  108. {
  109. /* let's see whether this is a know adapter we can attach to */
  110. if (adapter->id != I2C_HW_SAA7146) {
  111. dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
  112. return -ENODEV;
  113. }
  114. return i2c_probe(adapter, &addr_data, &tea6420_detect);
  115. }
  116. static int detach(struct i2c_client *client)
  117. {
  118. int ret = i2c_detach_client(client);
  119. kfree(client);
  120. return ret;
  121. }
  122. static int command(struct i2c_client *client, unsigned int cmd, void *arg)
  123. {
  124. struct tea6420_multiplex *a = (struct tea6420_multiplex *)arg;
  125. int result = 0;
  126. switch (cmd) {
  127. case TEA6420_SWITCH:
  128. result = tea6420_switch(client, a->in, a->out, a->gain);
  129. break;
  130. default:
  131. return -ENOIOCTLCMD;
  132. }
  133. return result;
  134. }
  135. static struct i2c_driver driver = {
  136. .owner = THIS_MODULE,
  137. .name = "tea6420",
  138. .id = I2C_DRIVERID_TEA6420,
  139. .flags = I2C_DF_NOTIFY,
  140. .attach_adapter = attach,
  141. .detach_client = detach,
  142. .command = command,
  143. };
  144. static struct i2c_client client_template = {
  145. .name = "tea6420",
  146. .driver = &driver,
  147. };
  148. static int __init this_module_init(void)
  149. {
  150. return i2c_add_driver(&driver);
  151. }
  152. static void __exit this_module_exit(void)
  153. {
  154. i2c_del_driver(&driver);
  155. }
  156. module_init(this_module_init);
  157. module_exit(this_module_exit);
  158. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  159. MODULE_DESCRIPTION("tea6420 driver");
  160. MODULE_LICENSE("GPL");