snd-aoa-gpio-feature.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Apple Onboard Audio feature call GPIO control
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. *
  8. * This file contains the GPIO control routines for
  9. * direct (through feature calls) access to the GPIO
  10. * registers.
  11. */
  12. #include <asm/pmac_feature.h>
  13. #include <linux/interrupt.h>
  14. #include "../aoa.h"
  15. /* TODO: these are 20 global variables
  16. * that aren't used on most machines...
  17. * Move them into a dynamically allocated
  18. * structure and use that.
  19. */
  20. /* these are the GPIO numbers (register addresses as offsets into
  21. * the GPIO space) */
  22. static int headphone_mute_gpio;
  23. static int amp_mute_gpio;
  24. static int lineout_mute_gpio;
  25. static int hw_reset_gpio;
  26. static int lineout_detect_gpio;
  27. static int headphone_detect_gpio;
  28. static int linein_detect_gpio;
  29. /* see the SWITCH_GPIO macro */
  30. static int headphone_mute_gpio_activestate;
  31. static int amp_mute_gpio_activestate;
  32. static int lineout_mute_gpio_activestate;
  33. static int hw_reset_gpio_activestate;
  34. static int lineout_detect_gpio_activestate;
  35. static int headphone_detect_gpio_activestate;
  36. static int linein_detect_gpio_activestate;
  37. /* node pointers that we save when getting the GPIO number
  38. * to get the interrupt later */
  39. static struct device_node *lineout_detect_node;
  40. static struct device_node *linein_detect_node;
  41. static struct device_node *headphone_detect_node;
  42. static int lineout_detect_irq;
  43. static int linein_detect_irq;
  44. static int headphone_detect_irq;
  45. static struct device_node *get_gpio(char *name,
  46. char *altname,
  47. int *gpioptr,
  48. int *gpioactiveptr)
  49. {
  50. struct device_node *np, *gpio;
  51. u32 *reg;
  52. const char *audio_gpio;
  53. *gpioptr = -1;
  54. /* check if we can get it the easy way ... */
  55. np = of_find_node_by_name(NULL, name);
  56. if (!np) {
  57. /* some machines have only gpioX/extint-gpioX nodes,
  58. * and an audio-gpio property saying what it is ...
  59. * So what we have to do is enumerate all children
  60. * of the gpio node and check them all. */
  61. gpio = of_find_node_by_name(NULL, "gpio");
  62. if (!gpio)
  63. return NULL;
  64. while ((np = of_get_next_child(gpio, np))) {
  65. audio_gpio = get_property(np, "audio-gpio", NULL);
  66. if (!audio_gpio)
  67. continue;
  68. if (strcmp(audio_gpio, name) == 0)
  69. break;
  70. if (altname && (strcmp(audio_gpio, altname) == 0))
  71. break;
  72. }
  73. /* still not found, assume not there */
  74. if (!np)
  75. return NULL;
  76. }
  77. reg = (u32 *)get_property(np, "reg", NULL);
  78. if (!reg)
  79. return NULL;
  80. *gpioptr = *reg;
  81. /* this is a hack, usually the GPIOs 'reg' property
  82. * should have the offset based from the GPIO space
  83. * which is at 0x50, but apparently not always... */
  84. if (*gpioptr < 0x50)
  85. *gpioptr += 0x50;
  86. reg = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
  87. if (!reg)
  88. /* Apple seems to default to 1, but
  89. * that doesn't seem right at least on most
  90. * machines. So until proven that the opposite
  91. * is necessary, we default to 0
  92. * (which, incidentally, snd-powermac also does...) */
  93. *gpioactiveptr = 0;
  94. else
  95. *gpioactiveptr = *reg;
  96. return np;
  97. }
  98. static void get_irq(struct device_node * np, int *irqptr)
  99. {
  100. if (np)
  101. *irqptr = irq_of_parse_and_map(np, 0);
  102. else
  103. *irqptr = NO_IRQ;
  104. }
  105. /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
  106. #define SWITCH_GPIO(name, v, on) \
  107. (((v)&~1) | ((on)? \
  108. (name##_gpio_activestate==0?4:5): \
  109. (name##_gpio_activestate==0?5:4)))
  110. #define FTR_GPIO(name, bit) \
  111. static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
  112. { \
  113. int v; \
  114. \
  115. if (unlikely(!rt)) return; \
  116. \
  117. if (name##_mute_gpio < 0) \
  118. return; \
  119. \
  120. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
  121. name##_mute_gpio, \
  122. 0); \
  123. \
  124. /* muted = !on... */ \
  125. v = SWITCH_GPIO(name##_mute, v, !on); \
  126. \
  127. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
  128. name##_mute_gpio, v); \
  129. \
  130. rt->implementation_private &= ~(1<<bit); \
  131. rt->implementation_private |= (!!on << bit); \
  132. } \
  133. static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
  134. { \
  135. if (unlikely(!rt)) return 0; \
  136. return (rt->implementation_private>>bit)&1; \
  137. }
  138. FTR_GPIO(headphone, 0);
  139. FTR_GPIO(amp, 1);
  140. FTR_GPIO(lineout, 2);
  141. static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
  142. {
  143. int v;
  144. if (unlikely(!rt)) return;
  145. if (hw_reset_gpio < 0)
  146. return;
  147. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
  148. hw_reset_gpio, 0);
  149. v = SWITCH_GPIO(hw_reset, v, on);
  150. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
  151. hw_reset_gpio, v);
  152. }
  153. static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
  154. {
  155. int saved;
  156. if (unlikely(!rt)) return;
  157. saved = rt->implementation_private;
  158. ftr_gpio_set_headphone(rt, 0);
  159. ftr_gpio_set_amp(rt, 0);
  160. ftr_gpio_set_lineout(rt, 0);
  161. rt->implementation_private = saved;
  162. }
  163. static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
  164. {
  165. int s;
  166. if (unlikely(!rt)) return;
  167. s = rt->implementation_private;
  168. ftr_gpio_set_headphone(rt, (s>>0)&1);
  169. ftr_gpio_set_amp(rt, (s>>1)&1);
  170. ftr_gpio_set_lineout(rt, (s>>2)&1);
  171. }
  172. static void ftr_handle_notify(void *data)
  173. {
  174. struct gpio_notification *notif = data;
  175. mutex_lock(&notif->mutex);
  176. if (notif->notify)
  177. notif->notify(notif->data);
  178. mutex_unlock(&notif->mutex);
  179. }
  180. static void gpio_enable_dual_edge(int gpio)
  181. {
  182. int v;
  183. if (gpio == -1)
  184. return;
  185. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  186. v |= 0x80; /* enable dual edge */
  187. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
  188. }
  189. static void ftr_gpio_init(struct gpio_runtime *rt)
  190. {
  191. get_gpio("headphone-mute", NULL,
  192. &headphone_mute_gpio,
  193. &headphone_mute_gpio_activestate);
  194. get_gpio("amp-mute", NULL,
  195. &amp_mute_gpio,
  196. &amp_mute_gpio_activestate);
  197. get_gpio("lineout-mute", NULL,
  198. &lineout_mute_gpio,
  199. &lineout_mute_gpio_activestate);
  200. get_gpio("hw-reset", "audio-hw-reset",
  201. &hw_reset_gpio,
  202. &hw_reset_gpio_activestate);
  203. headphone_detect_node = get_gpio("headphone-detect", NULL,
  204. &headphone_detect_gpio,
  205. &headphone_detect_gpio_activestate);
  206. /* go Apple, and thanks for giving these different names
  207. * across the board... */
  208. lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
  209. &lineout_detect_gpio,
  210. &lineout_detect_gpio_activestate);
  211. linein_detect_node = get_gpio("linein-detect", "line-input-detect",
  212. &linein_detect_gpio,
  213. &linein_detect_gpio_activestate);
  214. gpio_enable_dual_edge(headphone_detect_gpio);
  215. gpio_enable_dual_edge(lineout_detect_gpio);
  216. gpio_enable_dual_edge(linein_detect_gpio);
  217. get_irq(headphone_detect_node, &headphone_detect_irq);
  218. get_irq(lineout_detect_node, &lineout_detect_irq);
  219. get_irq(linein_detect_node, &linein_detect_irq);
  220. ftr_gpio_all_amps_off(rt);
  221. rt->implementation_private = 0;
  222. INIT_WORK(&rt->headphone_notify.work, ftr_handle_notify,
  223. &rt->headphone_notify);
  224. INIT_WORK(&rt->line_in_notify.work, ftr_handle_notify,
  225. &rt->line_in_notify);
  226. INIT_WORK(&rt->line_out_notify.work, ftr_handle_notify,
  227. &rt->line_out_notify);
  228. mutex_init(&rt->headphone_notify.mutex);
  229. mutex_init(&rt->line_in_notify.mutex);
  230. mutex_init(&rt->line_out_notify.mutex);
  231. }
  232. static void ftr_gpio_exit(struct gpio_runtime *rt)
  233. {
  234. ftr_gpio_all_amps_off(rt);
  235. rt->implementation_private = 0;
  236. if (rt->headphone_notify.notify)
  237. free_irq(headphone_detect_irq, &rt->headphone_notify);
  238. if (rt->line_in_notify.gpio_private)
  239. free_irq(linein_detect_irq, &rt->line_in_notify);
  240. if (rt->line_out_notify.gpio_private)
  241. free_irq(lineout_detect_irq, &rt->line_out_notify);
  242. cancel_delayed_work(&rt->headphone_notify.work);
  243. cancel_delayed_work(&rt->line_in_notify.work);
  244. cancel_delayed_work(&rt->line_out_notify.work);
  245. flush_scheduled_work();
  246. mutex_destroy(&rt->headphone_notify.mutex);
  247. mutex_destroy(&rt->line_in_notify.mutex);
  248. mutex_destroy(&rt->line_out_notify.mutex);
  249. }
  250. static irqreturn_t ftr_handle_notify_irq(int xx, void *data)
  251. {
  252. struct gpio_notification *notif = data;
  253. schedule_work(&notif->work);
  254. return IRQ_HANDLED;
  255. }
  256. static int ftr_set_notify(struct gpio_runtime *rt,
  257. enum notify_type type,
  258. notify_func_t notify,
  259. void *data)
  260. {
  261. struct gpio_notification *notif;
  262. notify_func_t old;
  263. int irq;
  264. char *name;
  265. int err = -EBUSY;
  266. switch (type) {
  267. case AOA_NOTIFY_HEADPHONE:
  268. notif = &rt->headphone_notify;
  269. name = "headphone-detect";
  270. irq = headphone_detect_irq;
  271. break;
  272. case AOA_NOTIFY_LINE_IN:
  273. notif = &rt->line_in_notify;
  274. name = "linein-detect";
  275. irq = linein_detect_irq;
  276. break;
  277. case AOA_NOTIFY_LINE_OUT:
  278. notif = &rt->line_out_notify;
  279. name = "lineout-detect";
  280. irq = lineout_detect_irq;
  281. break;
  282. default:
  283. return -EINVAL;
  284. }
  285. if (irq == NO_IRQ)
  286. return -ENODEV;
  287. mutex_lock(&notif->mutex);
  288. old = notif->notify;
  289. if (!old && !notify) {
  290. err = 0;
  291. goto out_unlock;
  292. }
  293. if (old && notify) {
  294. if (old == notify && notif->data == data)
  295. err = 0;
  296. goto out_unlock;
  297. }
  298. if (old && !notify)
  299. free_irq(irq, notif);
  300. if (!old && notify) {
  301. err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
  302. if (err)
  303. goto out_unlock;
  304. }
  305. notif->notify = notify;
  306. notif->data = data;
  307. err = 0;
  308. out_unlock:
  309. mutex_unlock(&notif->mutex);
  310. return err;
  311. }
  312. static int ftr_get_detect(struct gpio_runtime *rt,
  313. enum notify_type type)
  314. {
  315. int gpio, ret, active;
  316. switch (type) {
  317. case AOA_NOTIFY_HEADPHONE:
  318. gpio = headphone_detect_gpio;
  319. active = headphone_detect_gpio_activestate;
  320. break;
  321. case AOA_NOTIFY_LINE_IN:
  322. gpio = linein_detect_gpio;
  323. active = linein_detect_gpio_activestate;
  324. break;
  325. case AOA_NOTIFY_LINE_OUT:
  326. gpio = lineout_detect_gpio;
  327. active = lineout_detect_gpio_activestate;
  328. break;
  329. default:
  330. return -EINVAL;
  331. }
  332. if (gpio == -1)
  333. return -ENODEV;
  334. ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  335. if (ret < 0)
  336. return ret;
  337. return ((ret >> 1) & 1) == active;
  338. }
  339. static struct gpio_methods methods = {
  340. .init = ftr_gpio_init,
  341. .exit = ftr_gpio_exit,
  342. .all_amps_off = ftr_gpio_all_amps_off,
  343. .all_amps_restore = ftr_gpio_all_amps_restore,
  344. .set_headphone = ftr_gpio_set_headphone,
  345. .set_speakers = ftr_gpio_set_amp,
  346. .set_lineout = ftr_gpio_set_lineout,
  347. .set_hw_reset = ftr_gpio_set_hw_reset,
  348. .get_headphone = ftr_gpio_get_headphone,
  349. .get_speakers = ftr_gpio_get_amp,
  350. .get_lineout = ftr_gpio_get_lineout,
  351. .set_notify = ftr_set_notify,
  352. .get_detect = ftr_get_detect,
  353. };
  354. struct gpio_methods *ftr_gpio_methods = &methods;
  355. EXPORT_SYMBOL_GPL(ftr_gpio_methods);