snd-aoa-gpio-pmf.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Apple Onboard Audio pmf GPIOs
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. */
  8. #include <asm/pmac_feature.h>
  9. #include <asm/pmac_pfunc.h>
  10. #include "../aoa.h"
  11. #define PMF_GPIO(name, bit) \
  12. static void pmf_gpio_set_##name(struct gpio_runtime *rt, int on)\
  13. { \
  14. struct pmf_args args = { .count = 1, .u[0].v = !on }; \
  15. \
  16. if (unlikely(!rt)) return; \
  17. pmf_call_function(rt->node, #name "-mute", &args); \
  18. rt->implementation_private &= ~(1<<bit); \
  19. rt->implementation_private |= (!!on << bit); \
  20. } \
  21. static int pmf_gpio_get_##name(struct gpio_runtime *rt) \
  22. { \
  23. if (unlikely(!rt)) return 0; \
  24. return (rt->implementation_private>>bit)&1; \
  25. }
  26. PMF_GPIO(headphone, 0);
  27. PMF_GPIO(amp, 1);
  28. PMF_GPIO(lineout, 2);
  29. static void pmf_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
  30. {
  31. struct pmf_args args = { .count = 1, .u[0].v = !!on };
  32. if (unlikely(!rt)) return;
  33. pmf_call_function(rt->node, "hw-reset", &args);
  34. }
  35. static void pmf_gpio_all_amps_off(struct gpio_runtime *rt)
  36. {
  37. int saved;
  38. if (unlikely(!rt)) return;
  39. saved = rt->implementation_private;
  40. pmf_gpio_set_headphone(rt, 0);
  41. pmf_gpio_set_amp(rt, 0);
  42. pmf_gpio_set_lineout(rt, 0);
  43. rt->implementation_private = saved;
  44. }
  45. static void pmf_gpio_all_amps_restore(struct gpio_runtime *rt)
  46. {
  47. int s;
  48. if (unlikely(!rt)) return;
  49. s = rt->implementation_private;
  50. pmf_gpio_set_headphone(rt, (s>>0)&1);
  51. pmf_gpio_set_amp(rt, (s>>1)&1);
  52. pmf_gpio_set_lineout(rt, (s>>2)&1);
  53. }
  54. static void pmf_handle_notify(void *data)
  55. {
  56. struct gpio_notification *notif = data;
  57. mutex_lock(&notif->mutex);
  58. if (notif->notify)
  59. notif->notify(notif->data);
  60. mutex_unlock(&notif->mutex);
  61. }
  62. static void pmf_gpio_init(struct gpio_runtime *rt)
  63. {
  64. pmf_gpio_all_amps_off(rt);
  65. rt->implementation_private = 0;
  66. INIT_WORK(&rt->headphone_notify.work, pmf_handle_notify,
  67. &rt->headphone_notify);
  68. INIT_WORK(&rt->line_in_notify.work, pmf_handle_notify,
  69. &rt->line_in_notify);
  70. INIT_WORK(&rt->line_out_notify.work, pmf_handle_notify,
  71. &rt->line_out_notify);
  72. mutex_init(&rt->headphone_notify.mutex);
  73. mutex_init(&rt->line_in_notify.mutex);
  74. mutex_init(&rt->line_out_notify.mutex);
  75. }
  76. static void pmf_gpio_exit(struct gpio_runtime *rt)
  77. {
  78. pmf_gpio_all_amps_off(rt);
  79. rt->implementation_private = 0;
  80. if (rt->headphone_notify.gpio_private)
  81. pmf_unregister_irq_client(rt->headphone_notify.gpio_private);
  82. if (rt->line_in_notify.gpio_private)
  83. pmf_unregister_irq_client(rt->line_in_notify.gpio_private);
  84. if (rt->line_out_notify.gpio_private)
  85. pmf_unregister_irq_client(rt->line_out_notify.gpio_private);
  86. /* make sure no work is pending before freeing
  87. * all things */
  88. cancel_delayed_work(&rt->headphone_notify.work);
  89. cancel_delayed_work(&rt->line_in_notify.work);
  90. cancel_delayed_work(&rt->line_out_notify.work);
  91. flush_scheduled_work();
  92. mutex_destroy(&rt->headphone_notify.mutex);
  93. mutex_destroy(&rt->line_in_notify.mutex);
  94. mutex_destroy(&rt->line_out_notify.mutex);
  95. if (rt->headphone_notify.gpio_private)
  96. kfree(rt->headphone_notify.gpio_private);
  97. if (rt->line_in_notify.gpio_private)
  98. kfree(rt->line_in_notify.gpio_private);
  99. if (rt->line_out_notify.gpio_private)
  100. kfree(rt->line_out_notify.gpio_private);
  101. }
  102. static void pmf_handle_notify_irq(void *data)
  103. {
  104. struct gpio_notification *notif = data;
  105. schedule_work(&notif->work);
  106. }
  107. static int pmf_set_notify(struct gpio_runtime *rt,
  108. enum notify_type type,
  109. notify_func_t notify,
  110. void *data)
  111. {
  112. struct gpio_notification *notif;
  113. notify_func_t old;
  114. struct pmf_irq_client *irq_client;
  115. char *name;
  116. int err = -EBUSY;
  117. switch (type) {
  118. case AOA_NOTIFY_HEADPHONE:
  119. notif = &rt->headphone_notify;
  120. name = "headphone-detect";
  121. break;
  122. case AOA_NOTIFY_LINE_IN:
  123. notif = &rt->line_in_notify;
  124. name = "linein-detect";
  125. break;
  126. case AOA_NOTIFY_LINE_OUT:
  127. notif = &rt->line_out_notify;
  128. name = "lineout-detect";
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. mutex_lock(&notif->mutex);
  134. old = notif->notify;
  135. if (!old && !notify) {
  136. err = 0;
  137. goto out_unlock;
  138. }
  139. if (old && notify) {
  140. if (old == notify && notif->data == data)
  141. err = 0;
  142. goto out_unlock;
  143. }
  144. if (old && !notify) {
  145. irq_client = notif->gpio_private;
  146. pmf_unregister_irq_client(irq_client);
  147. kfree(irq_client);
  148. notif->gpio_private = NULL;
  149. }
  150. if (!old && notify) {
  151. irq_client = kzalloc(sizeof(struct pmf_irq_client),
  152. GFP_KERNEL);
  153. irq_client->data = notif;
  154. irq_client->handler = pmf_handle_notify_irq;
  155. irq_client->owner = THIS_MODULE;
  156. err = pmf_register_irq_client(rt->node,
  157. name,
  158. irq_client);
  159. if (err) {
  160. printk(KERN_ERR "snd-aoa: gpio layer failed to"
  161. " register %s irq (%d)\n", name, err);
  162. kfree(irq_client);
  163. goto out_unlock;
  164. }
  165. notif->gpio_private = irq_client;
  166. }
  167. notif->notify = notify;
  168. notif->data = data;
  169. err = 0;
  170. out_unlock:
  171. mutex_unlock(&notif->mutex);
  172. return err;
  173. }
  174. static int pmf_get_detect(struct gpio_runtime *rt,
  175. enum notify_type type)
  176. {
  177. char *name;
  178. int err = -EBUSY, ret;
  179. struct pmf_args args = { .count = 1, .u[0].p = &ret };
  180. switch (type) {
  181. case AOA_NOTIFY_HEADPHONE:
  182. name = "headphone-detect";
  183. break;
  184. case AOA_NOTIFY_LINE_IN:
  185. name = "linein-detect";
  186. break;
  187. case AOA_NOTIFY_LINE_OUT:
  188. name = "lineout-detect";
  189. break;
  190. default:
  191. return -EINVAL;
  192. }
  193. err = pmf_call_function(rt->node, name, &args);
  194. if (err)
  195. return err;
  196. return ret;
  197. }
  198. static struct gpio_methods methods = {
  199. .init = pmf_gpio_init,
  200. .exit = pmf_gpio_exit,
  201. .all_amps_off = pmf_gpio_all_amps_off,
  202. .all_amps_restore = pmf_gpio_all_amps_restore,
  203. .set_headphone = pmf_gpio_set_headphone,
  204. .set_speakers = pmf_gpio_set_amp,
  205. .set_lineout = pmf_gpio_set_lineout,
  206. .set_hw_reset = pmf_gpio_set_hw_reset,
  207. .get_headphone = pmf_gpio_get_headphone,
  208. .get_speakers = pmf_gpio_get_amp,
  209. .get_lineout = pmf_gpio_get_lineout,
  210. .set_notify = pmf_set_notify,
  211. .get_detect = pmf_get_detect,
  212. };
  213. struct gpio_methods *pmf_gpio_methods = &methods;
  214. EXPORT_SYMBOL_GPL(pmf_gpio_methods);