gpio-pmf.c 6.1 KB

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