gpio-pmf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_sync(&rt->headphone_notify.work);
  96. cancel_delayed_work_sync(&rt->line_in_notify.work);
  97. cancel_delayed_work_sync(&rt->line_out_notify.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. kfree(rt->headphone_notify.gpio_private);
  102. kfree(rt->line_in_notify.gpio_private);
  103. kfree(rt->line_out_notify.gpio_private);
  104. }
  105. static void pmf_handle_notify_irq(void *data)
  106. {
  107. struct gpio_notification *notif = data;
  108. schedule_delayed_work(&notif->work, 0);
  109. }
  110. static int pmf_set_notify(struct gpio_runtime *rt,
  111. enum notify_type type,
  112. notify_func_t notify,
  113. void *data)
  114. {
  115. struct gpio_notification *notif;
  116. notify_func_t old;
  117. struct pmf_irq_client *irq_client;
  118. char *name;
  119. int err = -EBUSY;
  120. switch (type) {
  121. case AOA_NOTIFY_HEADPHONE:
  122. notif = &rt->headphone_notify;
  123. name = "headphone-detect";
  124. break;
  125. case AOA_NOTIFY_LINE_IN:
  126. notif = &rt->line_in_notify;
  127. name = "linein-detect";
  128. break;
  129. case AOA_NOTIFY_LINE_OUT:
  130. notif = &rt->line_out_notify;
  131. name = "lineout-detect";
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. mutex_lock(&notif->mutex);
  137. old = notif->notify;
  138. if (!old && !notify) {
  139. err = 0;
  140. goto out_unlock;
  141. }
  142. if (old && notify) {
  143. if (old == notify && notif->data == data)
  144. err = 0;
  145. goto out_unlock;
  146. }
  147. if (old && !notify) {
  148. irq_client = notif->gpio_private;
  149. pmf_unregister_irq_client(irq_client);
  150. kfree(irq_client);
  151. notif->gpio_private = NULL;
  152. }
  153. if (!old && notify) {
  154. irq_client = kzalloc(sizeof(struct pmf_irq_client),
  155. GFP_KERNEL);
  156. if (!irq_client) {
  157. err = -ENOMEM;
  158. goto out_unlock;
  159. }
  160. irq_client->data = notif;
  161. irq_client->handler = pmf_handle_notify_irq;
  162. irq_client->owner = THIS_MODULE;
  163. err = pmf_register_irq_client(rt->node,
  164. name,
  165. irq_client);
  166. if (err) {
  167. printk(KERN_ERR "snd-aoa: gpio layer failed to"
  168. " register %s irq (%d)\n", name, err);
  169. kfree(irq_client);
  170. goto out_unlock;
  171. }
  172. notif->gpio_private = irq_client;
  173. }
  174. notif->notify = notify;
  175. notif->data = data;
  176. err = 0;
  177. out_unlock:
  178. mutex_unlock(&notif->mutex);
  179. return err;
  180. }
  181. static int pmf_get_detect(struct gpio_runtime *rt,
  182. enum notify_type type)
  183. {
  184. char *name;
  185. int err = -EBUSY, ret;
  186. struct pmf_args args = { .count = 1, .u[0].p = &ret };
  187. switch (type) {
  188. case AOA_NOTIFY_HEADPHONE:
  189. name = "headphone-detect";
  190. break;
  191. case AOA_NOTIFY_LINE_IN:
  192. name = "linein-detect";
  193. break;
  194. case AOA_NOTIFY_LINE_OUT:
  195. name = "lineout-detect";
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. err = pmf_call_function(rt->node, name, &args);
  201. if (err)
  202. return err;
  203. return ret;
  204. }
  205. static struct gpio_methods methods = {
  206. .init = pmf_gpio_init,
  207. .exit = pmf_gpio_exit,
  208. .all_amps_off = pmf_gpio_all_amps_off,
  209. .all_amps_restore = pmf_gpio_all_amps_restore,
  210. .set_headphone = pmf_gpio_set_headphone,
  211. .set_speakers = pmf_gpio_set_amp,
  212. .set_lineout = pmf_gpio_set_lineout,
  213. .set_hw_reset = pmf_gpio_set_hw_reset,
  214. .get_headphone = pmf_gpio_get_headphone,
  215. .get_speakers = pmf_gpio_get_amp,
  216. .get_lineout = pmf_gpio_get_lineout,
  217. .set_notify = pmf_set_notify,
  218. .get_detect = pmf_get_detect,
  219. };
  220. struct gpio_methods *pmf_gpio_methods = &methods;
  221. EXPORT_SYMBOL_GPL(pmf_gpio_methods);