snd-aoa-gpio-feature.c 10 KB

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