tda9840.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. tda9840 - i2c-driver for the tda9840 by SGS Thomson
  3. Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
  4. The tda9840 is a stereo/dual sound processor with digital
  5. identification. It can be found at address 0x84 on the i2c-bus.
  6. For detailed informations download the specifications directly
  7. from SGS Thomson at http://www.st.com
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/i2c.h>
  23. #include "tda9840.h"
  24. static int debug = 0; /* insmod parameter */
  25. module_param(debug, int, 0644);
  26. MODULE_PARM_DESC(debug, "Turn on/off device debugging (default:off).");
  27. #define dprintk(args...) \
  28. do { if (debug) { printk("%s: %s()[%d]: ",__stringify(KBUILD_MODNAME), __FUNCTION__, __LINE__); printk(args); } } while (0)
  29. #define SWITCH 0x00
  30. #define LEVEL_ADJUST 0x02
  31. #define STEREO_ADJUST 0x03
  32. #define TEST 0x04
  33. /* addresses to scan, found only at 0x42 (7-Bit) */
  34. static unsigned short normal_i2c[] = { I2C_TDA9840, I2C_CLIENT_END };
  35. /* magic definition of all other variables and things */
  36. I2C_CLIENT_INSMOD;
  37. static struct i2c_driver driver;
  38. static struct i2c_client client_template;
  39. static int command(struct i2c_client *client, unsigned int cmd, void *arg)
  40. {
  41. int result;
  42. int byte = *(int *)arg;
  43. switch (cmd) {
  44. case TDA9840_SWITCH:
  45. dprintk("TDA9840_SWITCH: 0x%02x\n", byte);
  46. if (byte != TDA9840_SET_MONO
  47. && byte != TDA9840_SET_MUTE
  48. && byte != TDA9840_SET_STEREO
  49. && byte != TDA9840_SET_LANG1
  50. && byte != TDA9840_SET_LANG2
  51. && byte != TDA9840_SET_BOTH
  52. && byte != TDA9840_SET_BOTH_R
  53. && byte != TDA9840_SET_EXTERNAL) {
  54. return -EINVAL;
  55. }
  56. result = i2c_smbus_write_byte_data(client, SWITCH, byte);
  57. if (result)
  58. dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
  59. break;
  60. case TDA9840_LEVEL_ADJUST:
  61. dprintk("TDA9840_LEVEL_ADJUST: %d\n", byte);
  62. /* check for correct range */
  63. if (byte > 25 || byte < -20)
  64. return -EINVAL;
  65. /* calculate actual value to set, see specs, page 18 */
  66. byte /= 5;
  67. if (0 < byte)
  68. byte += 0x8;
  69. else
  70. byte = -byte;
  71. result = i2c_smbus_write_byte_data(client, LEVEL_ADJUST, byte);
  72. if (result)
  73. dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
  74. break;
  75. case TDA9840_STEREO_ADJUST:
  76. dprintk("TDA9840_STEREO_ADJUST: %d\n", byte);
  77. /* check for correct range */
  78. if (byte > 25 || byte < -24)
  79. return -EINVAL;
  80. /* calculate actual value to set */
  81. byte /= 5;
  82. if (0 < byte)
  83. byte += 0x20;
  84. else
  85. byte = -byte;
  86. result = i2c_smbus_write_byte_data(client, STEREO_ADJUST, byte);
  87. if (result)
  88. dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
  89. break;
  90. case TDA9840_DETECT: {
  91. int *ret = (int *)arg;
  92. byte = i2c_smbus_read_byte_data(client, STEREO_ADJUST);
  93. if (byte == -1) {
  94. dprintk("i2c_smbus_read_byte_data() failed\n");
  95. return -EIO;
  96. }
  97. if (0 != (byte & 0x80)) {
  98. dprintk("TDA9840_DETECT: register contents invalid\n");
  99. return -EINVAL;
  100. }
  101. dprintk("TDA9840_DETECT: byte: 0x%02x\n", byte);
  102. *ret = ((byte & 0x60) >> 5);
  103. result = 0;
  104. break;
  105. }
  106. case TDA9840_TEST:
  107. dprintk("TDA9840_TEST: 0x%02x\n", byte);
  108. /* mask out irrelevant bits */
  109. byte &= 0x3;
  110. result = i2c_smbus_write_byte_data(client, TEST, byte);
  111. if (result)
  112. dprintk("i2c_smbus_write_byte() failed, ret:%d\n", result);
  113. break;
  114. default:
  115. return -ENOIOCTLCMD;
  116. }
  117. if (result)
  118. return -EIO;
  119. return 0;
  120. }
  121. static int detect(struct i2c_adapter *adapter, int address, int kind)
  122. {
  123. struct i2c_client *client;
  124. int result = 0;
  125. int byte = 0x0;
  126. /* let's see whether this adapter can support what we need */
  127. if (0 == i2c_check_functionality(adapter,
  128. I2C_FUNC_SMBUS_READ_BYTE_DATA |
  129. I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {
  130. return 0;
  131. }
  132. /* allocate memory for client structure */
  133. client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  134. if (0 == client) {
  135. printk("not enough kernel memory\n");
  136. return -ENOMEM;
  137. }
  138. /* fill client structure */
  139. memcpy(client, &client_template, sizeof(struct i2c_client));
  140. client->addr = address;
  141. client->adapter = adapter;
  142. /* tell the i2c layer a new client has arrived */
  143. if (0 != (result = i2c_attach_client(client))) {
  144. kfree(client);
  145. return result;
  146. }
  147. /* set initial values for level & stereo - adjustment, mode */
  148. byte = 0;
  149. result = command(client, TDA9840_LEVEL_ADJUST, &byte);
  150. result += command(client, TDA9840_STEREO_ADJUST, &byte);
  151. byte = TDA9840_SET_MONO;
  152. result = command(client, TDA9840_SWITCH, &byte);
  153. if (result) {
  154. dprintk("could not initialize tda9840\n");
  155. return -ENODEV;
  156. }
  157. printk("tda9840: detected @ 0x%02x on adapter %s\n", address, &client->adapter->name[0]);
  158. return 0;
  159. }
  160. static int attach(struct i2c_adapter *adapter)
  161. {
  162. /* let's see whether this is a know adapter we can attach to */
  163. if (adapter->id != I2C_HW_SAA7146) {
  164. dprintk("refusing to probe on unknown adapter [name='%s',id=0x%x]\n", adapter->name, adapter->id);
  165. return -ENODEV;
  166. }
  167. return i2c_probe(adapter, &addr_data, &detect);
  168. }
  169. static int detach(struct i2c_client *client)
  170. {
  171. int ret = i2c_detach_client(client);
  172. kfree(client);
  173. return ret;
  174. }
  175. static struct i2c_driver driver = {
  176. .owner = THIS_MODULE,
  177. .name = "tda9840",
  178. .id = I2C_DRIVERID_TDA9840,
  179. .flags = I2C_DF_NOTIFY,
  180. .attach_adapter = attach,
  181. .detach_client = detach,
  182. .command = command,
  183. };
  184. static struct i2c_client client_template = {
  185. .name = "tda9840",
  186. .driver = &driver,
  187. };
  188. static int __init this_module_init(void)
  189. {
  190. return i2c_add_driver(&driver);
  191. }
  192. static void __exit this_module_exit(void)
  193. {
  194. i2c_del_driver(&driver);
  195. }
  196. module_init(this_module_init);
  197. module_exit(this_module_exit);
  198. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  199. MODULE_DESCRIPTION("tda9840 driver");
  200. MODULE_LICENSE("GPL");