tas_common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 int tas_attach_adapter(struct i2c_adapter *);
  40. static int tas_detach_client(struct i2c_client *);
  41. struct i2c_driver tas_driver = {
  42. .driver = {
  43. .name = "tas",
  44. },
  45. .attach_adapter = tas_attach_adapter,
  46. .detach_client = tas_detach_client,
  47. };
  48. struct tas_driver_hooks_t *driver_hooks;
  49. int
  50. tas_register_driver(struct tas_driver_hooks_t *hooks)
  51. {
  52. driver_hooks = hooks;
  53. return 0;
  54. }
  55. int
  56. tas_get_mixer_level(int mixer, uint *level)
  57. {
  58. CALL(get_mixer_level,mixer,level);
  59. }
  60. int
  61. tas_set_mixer_level(int mixer,uint level)
  62. {
  63. CALL(set_mixer_level,mixer,level);
  64. }
  65. int
  66. tas_enter_sleep(void)
  67. {
  68. CALL0(enter_sleep);
  69. }
  70. int
  71. tas_leave_sleep(void)
  72. {
  73. CALL0(leave_sleep);
  74. }
  75. int
  76. tas_supported_mixers(void)
  77. {
  78. CALL0(supported_mixers);
  79. }
  80. int
  81. tas_mixer_is_stereo(int mixer)
  82. {
  83. CALL(mixer_is_stereo,mixer);
  84. }
  85. int
  86. tas_stereo_mixers(void)
  87. {
  88. CALL0(stereo_mixers);
  89. }
  90. int
  91. tas_output_device_change(int device_id,int layout_id,int speaker_id)
  92. {
  93. CALL(output_device_change,device_id,layout_id,speaker_id);
  94. }
  95. int
  96. tas_device_ioctl(u_int cmd, u_long arg)
  97. {
  98. CALL(device_ioctl,cmd,arg);
  99. }
  100. int
  101. tas_post_init(void)
  102. {
  103. CALL0(post_init);
  104. }
  105. static int
  106. tas_detect_client(struct i2c_adapter *adapter, int address)
  107. {
  108. static const char *client_name = "tas Digital Equalizer";
  109. struct i2c_client *new_client;
  110. int rc = -ENODEV;
  111. if (!driver_hooks) {
  112. printk(KERN_ERR "tas_detect_client called with no hooks !\n");
  113. return -ENODEV;
  114. }
  115. new_client = kzalloc(sizeof(*new_client), GFP_KERNEL);
  116. if (!new_client)
  117. return -ENOMEM;
  118. new_client->addr = address;
  119. new_client->adapter = adapter;
  120. new_client->driver = &tas_driver;
  121. strlcpy(new_client->name, client_name, DEVICE_NAME_SIZE);
  122. if (driver_hooks->init(new_client))
  123. goto bail;
  124. /* Tell the i2c layer a new client has arrived */
  125. if (i2c_attach_client(new_client)) {
  126. driver_hooks->uninit(dev_get_drvdata(&new_client->dev));
  127. goto bail;
  128. }
  129. tas_client = new_client;
  130. return 0;
  131. bail:
  132. tas_client = NULL;
  133. kfree(new_client);
  134. return rc;
  135. }
  136. static int
  137. tas_attach_adapter(struct i2c_adapter *adapter)
  138. {
  139. if (!strncmp(adapter->name, "mac-io", 6))
  140. return tas_detect_client(adapter, tas_i2c_address);
  141. return 0;
  142. }
  143. static int
  144. tas_detach_client(struct i2c_client *client)
  145. {
  146. if (client == tas_client) {
  147. driver_hooks->uninit(dev_get_drvdata(&client->dev));
  148. i2c_detach_client(client);
  149. kfree(client);
  150. }
  151. return 0;
  152. }
  153. void
  154. tas_cleanup(void)
  155. {
  156. i2c_del_driver(&tas_driver);
  157. }
  158. int __init
  159. tas_init(int driver_id, const char *driver_name)
  160. {
  161. const u32* paddr;
  162. struct device_node *tas_node;
  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 = of_find_node_by_name("deq");
  168. if (tas_node == NULL)
  169. return -ENODEV;
  170. paddr = of_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. of_node_put(tas_node);
  179. return i2c_add_driver(&tas_driver);
  180. }