tas_common.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <linux/module.h>
  2. #include <linux/slab.h>
  3. #include <linux/proc_fs.h>
  4. #include <linux/ioport.h>
  5. #include <linux/sysctl.h>
  6. #include <linux/types.h>
  7. #include <linux/i2c.h>
  8. #include <linux/init.h>
  9. #include <linux/soundcard.h>
  10. #include <asm/uaccess.h>
  11. #include <asm/errno.h>
  12. #include <asm/io.h>
  13. #include <asm/prom.h>
  14. #include "tas_common.h"
  15. #define CALL0(proc) \
  16. do { \
  17. struct tas_data_t *self; \
  18. if (!tas_client || driver_hooks == NULL) \
  19. return -1; \
  20. self = dev_get_drvdata(&tas_client->dev); \
  21. if (driver_hooks->proc) \
  22. return driver_hooks->proc(self); \
  23. else \
  24. return -EINVAL; \
  25. } while (0)
  26. #define CALL(proc,arg...) \
  27. do { \
  28. struct tas_data_t *self; \
  29. if (!tas_client || driver_hooks == NULL) \
  30. return -1; \
  31. self = dev_get_drvdata(&tas_client->dev); \
  32. if (driver_hooks->proc) \
  33. return driver_hooks->proc(self, ## arg); \
  34. else \
  35. return -EINVAL; \
  36. } while (0)
  37. static u8 tas_i2c_address = 0x34;
  38. static struct i2c_client *tas_client;
  39. static struct device_node* tas_node;
  40. static int tas_attach_adapter(struct i2c_adapter *);
  41. static int tas_detach_client(struct i2c_client *);
  42. struct i2c_driver tas_driver = {
  43. .driver = {
  44. .name = "tas",
  45. },
  46. .attach_adapter = tas_attach_adapter,
  47. .detach_client = tas_detach_client,
  48. };
  49. struct tas_driver_hooks_t *driver_hooks;
  50. int
  51. tas_register_driver(struct tas_driver_hooks_t *hooks)
  52. {
  53. driver_hooks = hooks;
  54. return 0;
  55. }
  56. int
  57. tas_get_mixer_level(int mixer, uint *level)
  58. {
  59. CALL(get_mixer_level,mixer,level);
  60. }
  61. int
  62. tas_set_mixer_level(int mixer,uint level)
  63. {
  64. CALL(set_mixer_level,mixer,level);
  65. }
  66. int
  67. tas_enter_sleep(void)
  68. {
  69. CALL0(enter_sleep);
  70. }
  71. int
  72. tas_leave_sleep(void)
  73. {
  74. CALL0(leave_sleep);
  75. }
  76. int
  77. tas_supported_mixers(void)
  78. {
  79. CALL0(supported_mixers);
  80. }
  81. int
  82. tas_mixer_is_stereo(int mixer)
  83. {
  84. CALL(mixer_is_stereo,mixer);
  85. }
  86. int
  87. tas_stereo_mixers(void)
  88. {
  89. CALL0(stereo_mixers);
  90. }
  91. int
  92. tas_output_device_change(int device_id,int layout_id,int speaker_id)
  93. {
  94. CALL(output_device_change,device_id,layout_id,speaker_id);
  95. }
  96. int
  97. tas_device_ioctl(u_int cmd, u_long arg)
  98. {
  99. CALL(device_ioctl,cmd,arg);
  100. }
  101. int
  102. tas_post_init(void)
  103. {
  104. CALL0(post_init);
  105. }
  106. static int
  107. tas_detect_client(struct i2c_adapter *adapter, int address)
  108. {
  109. static const char *client_name = "tas Digital Equalizer";
  110. struct i2c_client *new_client;
  111. int rc = -ENODEV;
  112. if (!driver_hooks) {
  113. printk(KERN_ERR "tas_detect_client called with no hooks !\n");
  114. return -ENODEV;
  115. }
  116. new_client = kzalloc(sizeof(*new_client), GFP_KERNEL);
  117. if (!new_client)
  118. return -ENOMEM;
  119. new_client->addr = address;
  120. new_client->adapter = adapter;
  121. new_client->driver = &tas_driver;
  122. strlcpy(new_client->name, client_name, DEVICE_NAME_SIZE);
  123. if (driver_hooks->init(new_client))
  124. goto bail;
  125. /* Tell the i2c layer a new client has arrived */
  126. if (i2c_attach_client(new_client)) {
  127. driver_hooks->uninit(dev_get_drvdata(&new_client->dev));
  128. goto bail;
  129. }
  130. tas_client = new_client;
  131. return 0;
  132. bail:
  133. tas_client = NULL;
  134. kfree(new_client);
  135. return rc;
  136. }
  137. static int
  138. tas_attach_adapter(struct i2c_adapter *adapter)
  139. {
  140. if (!strncmp(adapter->name, "mac-io", 6))
  141. return tas_detect_client(adapter, tas_i2c_address);
  142. return 0;
  143. }
  144. static int
  145. tas_detach_client(struct i2c_client *client)
  146. {
  147. if (client == tas_client) {
  148. driver_hooks->uninit(dev_get_drvdata(&client->dev));
  149. i2c_detach_client(client);
  150. kfree(client);
  151. }
  152. return 0;
  153. }
  154. void
  155. tas_cleanup(void)
  156. {
  157. i2c_del_driver(&tas_driver);
  158. }
  159. int __init
  160. tas_init(int driver_id, const char *driver_name)
  161. {
  162. u32* paddr;
  163. printk(KERN_INFO "tas driver [%s])\n", driver_name);
  164. #ifndef CONFIG_I2C_POWERMAC
  165. request_module("i2c-powermac");
  166. #endif
  167. tas_node = find_devices("deq");
  168. if (tas_node == NULL)
  169. return -ENODEV;
  170. paddr = (u32 *)get_property(tas_node, "i2c-address", NULL);
  171. if (paddr) {
  172. tas_i2c_address = (*paddr) >> 1;
  173. printk(KERN_INFO "using i2c address: 0x%x from device-tree\n",
  174. tas_i2c_address);
  175. } else
  176. printk(KERN_INFO "using i2c address: 0x%x (default)\n",
  177. tas_i2c_address);
  178. return i2c_add_driver(&tas_driver);
  179. }