snd-aoa-gpio-feature.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 gpio_enable_dual_edge(int gpio)
  183. {
  184. int v;
  185. if (gpio == -1)
  186. return;
  187. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  188. v |= 0x80; /* enable dual edge */
  189. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
  190. }
  191. static void ftr_gpio_init(struct gpio_runtime *rt)
  192. {
  193. get_gpio("headphone-mute", NULL,
  194. &headphone_mute_gpio,
  195. &headphone_mute_gpio_activestate);
  196. get_gpio("amp-mute", NULL,
  197. &amp_mute_gpio,
  198. &amp_mute_gpio_activestate);
  199. get_gpio("lineout-mute", NULL,
  200. &lineout_mute_gpio,
  201. &lineout_mute_gpio_activestate);
  202. get_gpio("hw-reset", "audio-hw-reset",
  203. &hw_reset_gpio,
  204. &hw_reset_gpio_activestate);
  205. headphone_detect_node = get_gpio("headphone-detect", NULL,
  206. &headphone_detect_gpio,
  207. &headphone_detect_gpio_activestate);
  208. /* go Apple, and thanks for giving these different names
  209. * across the board... */
  210. lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
  211. &lineout_detect_gpio,
  212. &lineout_detect_gpio_activestate);
  213. linein_detect_node = get_gpio("linein-detect", "line-input-detect",
  214. &linein_detect_gpio,
  215. &linein_detect_gpio_activestate);
  216. gpio_enable_dual_edge(headphone_detect_gpio);
  217. gpio_enable_dual_edge(lineout_detect_gpio);
  218. gpio_enable_dual_edge(linein_detect_gpio);
  219. get_irq(headphone_detect_node, &headphone_detect_irq);
  220. get_irq(lineout_detect_node, &lineout_detect_irq);
  221. get_irq(linein_detect_node, &linein_detect_irq);
  222. ftr_gpio_all_amps_off(rt);
  223. rt->implementation_private = 0;
  224. INIT_WORK(&rt->headphone_notify.work, ftr_handle_notify,
  225. &rt->headphone_notify);
  226. INIT_WORK(&rt->line_in_notify.work, ftr_handle_notify,
  227. &rt->line_in_notify);
  228. INIT_WORK(&rt->line_out_notify.work, ftr_handle_notify,
  229. &rt->line_out_notify);
  230. mutex_init(&rt->headphone_notify.mutex);
  231. mutex_init(&rt->line_in_notify.mutex);
  232. mutex_init(&rt->line_out_notify.mutex);
  233. }
  234. static void ftr_gpio_exit(struct gpio_runtime *rt)
  235. {
  236. ftr_gpio_all_amps_off(rt);
  237. rt->implementation_private = 0;
  238. if (rt->headphone_notify.notify)
  239. free_irq(headphone_detect_irq, &rt->headphone_notify);
  240. if (rt->line_in_notify.gpio_private)
  241. free_irq(linein_detect_irq, &rt->line_in_notify);
  242. if (rt->line_out_notify.gpio_private)
  243. free_irq(lineout_detect_irq, &rt->line_out_notify);
  244. cancel_delayed_work(&rt->headphone_notify.work);
  245. cancel_delayed_work(&rt->line_in_notify.work);
  246. cancel_delayed_work(&rt->line_out_notify.work);
  247. flush_scheduled_work();
  248. mutex_destroy(&rt->headphone_notify.mutex);
  249. mutex_destroy(&rt->line_in_notify.mutex);
  250. mutex_destroy(&rt->line_out_notify.mutex);
  251. }
  252. static irqreturn_t ftr_handle_notify_irq(int xx,
  253. void *data,
  254. struct pt_regs *regs)
  255. {
  256. struct gpio_notification *notif = data;
  257. schedule_work(&notif->work);
  258. return IRQ_HANDLED;
  259. }
  260. static int ftr_set_notify(struct gpio_runtime *rt,
  261. enum notify_type type,
  262. notify_func_t notify,
  263. void *data)
  264. {
  265. struct gpio_notification *notif;
  266. notify_func_t old;
  267. int irq;
  268. char *name;
  269. int err = -EBUSY;
  270. switch (type) {
  271. case AOA_NOTIFY_HEADPHONE:
  272. notif = &rt->headphone_notify;
  273. name = "headphone-detect";
  274. irq = headphone_detect_irq;
  275. break;
  276. case AOA_NOTIFY_LINE_IN:
  277. notif = &rt->line_in_notify;
  278. name = "linein-detect";
  279. irq = linein_detect_irq;
  280. break;
  281. case AOA_NOTIFY_LINE_OUT:
  282. notif = &rt->line_out_notify;
  283. name = "lineout-detect";
  284. irq = lineout_detect_irq;
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. if (irq == -1)
  290. return -ENODEV;
  291. mutex_lock(&notif->mutex);
  292. old = notif->notify;
  293. if (!old && !notify) {
  294. err = 0;
  295. goto out_unlock;
  296. }
  297. if (old && notify) {
  298. if (old == notify && notif->data == data)
  299. err = 0;
  300. goto out_unlock;
  301. }
  302. if (old && !notify)
  303. free_irq(irq, notif);
  304. if (!old && notify) {
  305. err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
  306. if (err)
  307. goto out_unlock;
  308. }
  309. notif->notify = notify;
  310. notif->data = data;
  311. err = 0;
  312. out_unlock:
  313. mutex_unlock(&notif->mutex);
  314. return err;
  315. }
  316. static int ftr_get_detect(struct gpio_runtime *rt,
  317. enum notify_type type)
  318. {
  319. int gpio, ret, active;
  320. switch (type) {
  321. case AOA_NOTIFY_HEADPHONE:
  322. gpio = headphone_detect_gpio;
  323. active = headphone_detect_gpio_activestate;
  324. break;
  325. case AOA_NOTIFY_LINE_IN:
  326. gpio = linein_detect_gpio;
  327. active = linein_detect_gpio_activestate;
  328. break;
  329. case AOA_NOTIFY_LINE_OUT:
  330. gpio = lineout_detect_gpio;
  331. active = lineout_detect_gpio_activestate;
  332. break;
  333. default:
  334. return -EINVAL;
  335. }
  336. if (gpio == -1)
  337. return -ENODEV;
  338. ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  339. if (ret < 0)
  340. return ret;
  341. return ((ret >> 1) & 1) == active;
  342. }
  343. static struct gpio_methods methods = {
  344. .init = ftr_gpio_init,
  345. .exit = ftr_gpio_exit,
  346. .all_amps_off = ftr_gpio_all_amps_off,
  347. .all_amps_restore = ftr_gpio_all_amps_restore,
  348. .set_headphone = ftr_gpio_set_headphone,
  349. .set_speakers = ftr_gpio_set_amp,
  350. .set_lineout = ftr_gpio_set_lineout,
  351. .set_hw_reset = ftr_gpio_set_hw_reset,
  352. .get_headphone = ftr_gpio_get_headphone,
  353. .get_speakers = ftr_gpio_get_amp,
  354. .get_lineout = ftr_gpio_get_lineout,
  355. .set_notify = ftr_set_notify,
  356. .get_detect = ftr_get_detect,
  357. };
  358. struct gpio_methods *ftr_gpio_methods = &methods;
  359. EXPORT_SYMBOL_GPL(ftr_gpio_methods);