snd-aoa-gpio-pmf.c 6.2 KB

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