snd-aoa-gpio-feature.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. 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. *irqptr = -1;
  101. if (!np)
  102. return;
  103. if (np->n_intrs != 1)
  104. return;
  105. *irqptr = np->intrs[0].line;
  106. }
  107. /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
  108. #define SWITCH_GPIO(name, v, on) \
  109. (((v)&~1) | ((on)? \
  110. (name##_gpio_activestate==0?4:5): \
  111. (name##_gpio_activestate==0?5:4)))
  112. #define FTR_GPIO(name, bit) \
  113. static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
  114. { \
  115. int v; \
  116. \
  117. if (unlikely(!rt)) return; \
  118. \
  119. if (name##_mute_gpio < 0) \
  120. return; \
  121. \
  122. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
  123. name##_mute_gpio, \
  124. 0); \
  125. \
  126. /* muted = !on... */ \
  127. v = SWITCH_GPIO(name##_mute, v, !on); \
  128. \
  129. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
  130. name##_mute_gpio, v); \
  131. \
  132. rt->implementation_private &= ~(1<<bit); \
  133. rt->implementation_private |= (!!on << bit); \
  134. } \
  135. static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
  136. { \
  137. if (unlikely(!rt)) return 0; \
  138. return (rt->implementation_private>>bit)&1; \
  139. }
  140. FTR_GPIO(headphone, 0);
  141. FTR_GPIO(amp, 1);
  142. FTR_GPIO(lineout, 2);
  143. static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
  144. {
  145. int v;
  146. if (unlikely(!rt)) return;
  147. if (hw_reset_gpio < 0)
  148. return;
  149. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
  150. hw_reset_gpio, 0);
  151. v = SWITCH_GPIO(hw_reset, v, on);
  152. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
  153. hw_reset_gpio, v);
  154. }
  155. static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
  156. {
  157. int saved;
  158. if (unlikely(!rt)) return;
  159. saved = rt->implementation_private;
  160. ftr_gpio_set_headphone(rt, 0);
  161. ftr_gpio_set_amp(rt, 0);
  162. ftr_gpio_set_lineout(rt, 0);
  163. rt->implementation_private = saved;
  164. }
  165. static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
  166. {
  167. int s;
  168. if (unlikely(!rt)) return;
  169. s = rt->implementation_private;
  170. ftr_gpio_set_headphone(rt, (s>>0)&1);
  171. ftr_gpio_set_amp(rt, (s>>1)&1);
  172. ftr_gpio_set_lineout(rt, (s>>2)&1);
  173. }
  174. static void ftr_handle_notify(void *data)
  175. {
  176. struct gpio_notification *notif = data;
  177. mutex_lock(&notif->mutex);
  178. if (notif->notify)
  179. notif->notify(notif->data);
  180. mutex_unlock(&notif->mutex);
  181. }
  182. static void ftr_gpio_init(struct gpio_runtime *rt)
  183. {
  184. get_gpio("headphone-mute", NULL,
  185. &headphone_mute_gpio,
  186. &headphone_mute_gpio_activestate);
  187. get_gpio("amp-mute", NULL,
  188. &amp_mute_gpio,
  189. &amp_mute_gpio_activestate);
  190. get_gpio("lineout-mute", NULL,
  191. &lineout_mute_gpio,
  192. &lineout_mute_gpio_activestate);
  193. get_gpio("hw-reset", "audio-hw-reset",
  194. &hw_reset_gpio,
  195. &hw_reset_gpio_activestate);
  196. headphone_detect_node = get_gpio("headphone-detect", NULL,
  197. &headphone_detect_gpio,
  198. &headphone_detect_gpio_activestate);
  199. /* go Apple, and thanks for giving these different names
  200. * across the board... */
  201. lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
  202. &lineout_detect_gpio,
  203. &lineout_detect_gpio_activestate);
  204. linein_detect_node = get_gpio("linein-detect", "line-input-detect",
  205. &linein_detect_gpio,
  206. &linein_detect_gpio_activestate);
  207. get_irq(headphone_detect_node, &headphone_detect_irq);
  208. get_irq(lineout_detect_node, &lineout_detect_irq);
  209. get_irq(linein_detect_node, &linein_detect_irq);
  210. ftr_gpio_all_amps_off(rt);
  211. rt->implementation_private = 0;
  212. INIT_WORK(&rt->headphone_notify.work, ftr_handle_notify,
  213. &rt->headphone_notify);
  214. INIT_WORK(&rt->line_in_notify.work, ftr_handle_notify,
  215. &rt->line_in_notify);
  216. INIT_WORK(&rt->line_out_notify.work, ftr_handle_notify,
  217. &rt->line_out_notify);
  218. mutex_init(&rt->headphone_notify.mutex);
  219. mutex_init(&rt->line_in_notify.mutex);
  220. mutex_init(&rt->line_out_notify.mutex);
  221. }
  222. static void ftr_gpio_exit(struct gpio_runtime *rt)
  223. {
  224. ftr_gpio_all_amps_off(rt);
  225. rt->implementation_private = 0;
  226. if (rt->headphone_notify.notify)
  227. free_irq(headphone_detect_irq, &rt->headphone_notify);
  228. if (rt->line_in_notify.gpio_private)
  229. free_irq(linein_detect_irq, &rt->line_in_notify);
  230. if (rt->line_out_notify.gpio_private)
  231. free_irq(lineout_detect_irq, &rt->line_out_notify);
  232. cancel_delayed_work(&rt->headphone_notify.work);
  233. cancel_delayed_work(&rt->line_in_notify.work);
  234. cancel_delayed_work(&rt->line_out_notify.work);
  235. flush_scheduled_work();
  236. mutex_destroy(&rt->headphone_notify.mutex);
  237. mutex_destroy(&rt->line_in_notify.mutex);
  238. mutex_destroy(&rt->line_out_notify.mutex);
  239. }
  240. static irqreturn_t ftr_handle_notify_irq(int xx,
  241. void *data,
  242. struct pt_regs *regs)
  243. {
  244. struct gpio_notification *notif = data;
  245. schedule_work(&notif->work);
  246. return IRQ_HANDLED;
  247. }
  248. static int ftr_set_notify(struct gpio_runtime *rt,
  249. enum notify_type type,
  250. notify_func_t notify,
  251. void *data)
  252. {
  253. struct gpio_notification *notif;
  254. notify_func_t old;
  255. int irq;
  256. char *name;
  257. int err = -EBUSY;
  258. switch (type) {
  259. case AOA_NOTIFY_HEADPHONE:
  260. notif = &rt->headphone_notify;
  261. name = "headphone-detect";
  262. irq = headphone_detect_irq;
  263. break;
  264. case AOA_NOTIFY_LINE_IN:
  265. notif = &rt->line_in_notify;
  266. name = "linein-detect";
  267. irq = linein_detect_irq;
  268. break;
  269. case AOA_NOTIFY_LINE_OUT:
  270. notif = &rt->line_out_notify;
  271. name = "lineout-detect";
  272. irq = lineout_detect_irq;
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. if (irq == -1)
  278. return -ENODEV;
  279. mutex_lock(&notif->mutex);
  280. old = notif->notify;
  281. if (!old && !notify) {
  282. err = 0;
  283. goto out_unlock;
  284. }
  285. if (old && notify) {
  286. if (old == notify && notif->data == data)
  287. err = 0;
  288. goto out_unlock;
  289. }
  290. if (old && !notify)
  291. free_irq(irq, notif);
  292. if (!old && notify) {
  293. err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
  294. if (err)
  295. goto out_unlock;
  296. }
  297. notif->notify = notify;
  298. notif->data = data;
  299. err = 0;
  300. out_unlock:
  301. mutex_unlock(&notif->mutex);
  302. return err;
  303. }
  304. static int ftr_get_detect(struct gpio_runtime *rt,
  305. enum notify_type type)
  306. {
  307. int gpio, ret, active;
  308. switch (type) {
  309. case AOA_NOTIFY_HEADPHONE:
  310. gpio = headphone_detect_gpio;
  311. active = headphone_detect_gpio_activestate;
  312. break;
  313. case AOA_NOTIFY_LINE_IN:
  314. gpio = linein_detect_gpio;
  315. active = linein_detect_gpio_activestate;
  316. break;
  317. case AOA_NOTIFY_LINE_OUT:
  318. gpio = lineout_detect_gpio;
  319. active = lineout_detect_gpio_activestate;
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. if (gpio == -1)
  325. return -ENODEV;
  326. ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  327. if (ret < 0)
  328. return ret;
  329. return ((ret >> 1) & 1) == active;
  330. }
  331. static struct gpio_methods methods = {
  332. .init = ftr_gpio_init,
  333. .exit = ftr_gpio_exit,
  334. .all_amps_off = ftr_gpio_all_amps_off,
  335. .all_amps_restore = ftr_gpio_all_amps_restore,
  336. .set_headphone = ftr_gpio_set_headphone,
  337. .set_speakers = ftr_gpio_set_amp,
  338. .set_lineout = ftr_gpio_set_lineout,
  339. .set_hw_reset = ftr_gpio_set_hw_reset,
  340. .get_headphone = ftr_gpio_get_headphone,
  341. .get_speakers = ftr_gpio_get_amp,
  342. .get_lineout = ftr_gpio_get_lineout,
  343. .set_notify = ftr_set_notify,
  344. .get_detect = ftr_get_detect,
  345. };
  346. struct gpio_methods *ftr_gpio_methods = &methods;
  347. EXPORT_SYMBOL_GPL(ftr_gpio_methods);