gpio-feature.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 lots of 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 master_mute_gpio;
  24. static int amp_mute_gpio;
  25. static int lineout_mute_gpio;
  26. static int hw_reset_gpio;
  27. static int lineout_detect_gpio;
  28. static int headphone_detect_gpio;
  29. static int linein_detect_gpio;
  30. /* see the SWITCH_GPIO macro */
  31. static int headphone_mute_gpio_activestate;
  32. static int master_mute_gpio_activestate;
  33. static int amp_mute_gpio_activestate;
  34. static int lineout_mute_gpio_activestate;
  35. static int hw_reset_gpio_activestate;
  36. static int lineout_detect_gpio_activestate;
  37. static int headphone_detect_gpio_activestate;
  38. static int linein_detect_gpio_activestate;
  39. /* node pointers that we save when getting the GPIO number
  40. * to get the interrupt later */
  41. static struct device_node *lineout_detect_node;
  42. static struct device_node *linein_detect_node;
  43. static struct device_node *headphone_detect_node;
  44. static int lineout_detect_irq;
  45. static int linein_detect_irq;
  46. static int headphone_detect_irq;
  47. static struct device_node *get_gpio(char *name,
  48. char *altname,
  49. int *gpioptr,
  50. int *gpioactiveptr)
  51. {
  52. struct device_node *np, *gpio;
  53. const u32 *reg;
  54. const char *audio_gpio;
  55. *gpioptr = -1;
  56. /* check if we can get it the easy way ... */
  57. np = of_find_node_by_name(NULL, name);
  58. if (!np) {
  59. /* some machines have only gpioX/extint-gpioX nodes,
  60. * and an audio-gpio property saying what it is ...
  61. * So what we have to do is enumerate all children
  62. * of the gpio node and check them all. */
  63. gpio = of_find_node_by_name(NULL, "gpio");
  64. if (!gpio)
  65. return NULL;
  66. while ((np = of_get_next_child(gpio, np))) {
  67. audio_gpio = of_get_property(np, "audio-gpio", NULL);
  68. if (!audio_gpio)
  69. continue;
  70. if (strcmp(audio_gpio, name) == 0)
  71. break;
  72. if (altname && (strcmp(audio_gpio, altname) == 0))
  73. break;
  74. }
  75. /* still not found, assume not there */
  76. if (!np)
  77. return NULL;
  78. }
  79. reg = of_get_property(np, "reg", NULL);
  80. if (!reg)
  81. return NULL;
  82. *gpioptr = *reg;
  83. /* this is a hack, usually the GPIOs 'reg' property
  84. * should have the offset based from the GPIO space
  85. * which is at 0x50, but apparently not always... */
  86. if (*gpioptr < 0x50)
  87. *gpioptr += 0x50;
  88. reg = of_get_property(np, "audio-gpio-active-state", NULL);
  89. if (!reg)
  90. /* Apple seems to default to 1, but
  91. * that doesn't seem right at least on most
  92. * machines. So until proven that the opposite
  93. * is necessary, we default to 0
  94. * (which, incidentally, snd-powermac also does...) */
  95. *gpioactiveptr = 0;
  96. else
  97. *gpioactiveptr = *reg;
  98. return np;
  99. }
  100. static void get_irq(struct device_node * np, int *irqptr)
  101. {
  102. if (np)
  103. *irqptr = irq_of_parse_and_map(np, 0);
  104. else
  105. *irqptr = NO_IRQ;
  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. FTR_GPIO(master, 3);
  144. static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
  145. {
  146. int v;
  147. if (unlikely(!rt)) return;
  148. if (hw_reset_gpio < 0)
  149. return;
  150. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
  151. hw_reset_gpio, 0);
  152. v = SWITCH_GPIO(hw_reset, v, on);
  153. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
  154. hw_reset_gpio, v);
  155. }
  156. static struct gpio_methods methods;
  157. static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
  158. {
  159. int saved;
  160. if (unlikely(!rt)) return;
  161. saved = rt->implementation_private;
  162. ftr_gpio_set_headphone(rt, 0);
  163. ftr_gpio_set_amp(rt, 0);
  164. ftr_gpio_set_lineout(rt, 0);
  165. if (methods.set_master)
  166. ftr_gpio_set_master(rt, 0);
  167. rt->implementation_private = saved;
  168. }
  169. static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
  170. {
  171. int s;
  172. if (unlikely(!rt)) return;
  173. s = rt->implementation_private;
  174. ftr_gpio_set_headphone(rt, (s>>0)&1);
  175. ftr_gpio_set_amp(rt, (s>>1)&1);
  176. ftr_gpio_set_lineout(rt, (s>>2)&1);
  177. if (methods.set_master)
  178. ftr_gpio_set_master(rt, (s>>3)&1);
  179. }
  180. static void ftr_handle_notify(struct work_struct *work)
  181. {
  182. struct gpio_notification *notif =
  183. container_of(work, struct gpio_notification, work.work);
  184. mutex_lock(&notif->mutex);
  185. if (notif->notify)
  186. notif->notify(notif->data);
  187. mutex_unlock(&notif->mutex);
  188. }
  189. static void gpio_enable_dual_edge(int gpio)
  190. {
  191. int v;
  192. if (gpio == -1)
  193. return;
  194. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  195. v |= 0x80; /* enable dual edge */
  196. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
  197. }
  198. static void ftr_gpio_init(struct gpio_runtime *rt)
  199. {
  200. get_gpio("headphone-mute", NULL,
  201. &headphone_mute_gpio,
  202. &headphone_mute_gpio_activestate);
  203. get_gpio("amp-mute", NULL,
  204. &amp_mute_gpio,
  205. &amp_mute_gpio_activestate);
  206. get_gpio("lineout-mute", NULL,
  207. &lineout_mute_gpio,
  208. &lineout_mute_gpio_activestate);
  209. get_gpio("hw-reset", "audio-hw-reset",
  210. &hw_reset_gpio,
  211. &hw_reset_gpio_activestate);
  212. if (get_gpio("master-mute", NULL,
  213. &master_mute_gpio,
  214. &master_mute_gpio_activestate)) {
  215. methods.set_master = ftr_gpio_set_master;
  216. methods.get_master = ftr_gpio_get_master;
  217. }
  218. headphone_detect_node = get_gpio("headphone-detect", NULL,
  219. &headphone_detect_gpio,
  220. &headphone_detect_gpio_activestate);
  221. /* go Apple, and thanks for giving these different names
  222. * across the board... */
  223. lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
  224. &lineout_detect_gpio,
  225. &lineout_detect_gpio_activestate);
  226. linein_detect_node = get_gpio("linein-detect", "line-input-detect",
  227. &linein_detect_gpio,
  228. &linein_detect_gpio_activestate);
  229. gpio_enable_dual_edge(headphone_detect_gpio);
  230. gpio_enable_dual_edge(lineout_detect_gpio);
  231. gpio_enable_dual_edge(linein_detect_gpio);
  232. get_irq(headphone_detect_node, &headphone_detect_irq);
  233. get_irq(lineout_detect_node, &lineout_detect_irq);
  234. get_irq(linein_detect_node, &linein_detect_irq);
  235. ftr_gpio_all_amps_off(rt);
  236. rt->implementation_private = 0;
  237. INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify);
  238. INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify);
  239. INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify);
  240. mutex_init(&rt->headphone_notify.mutex);
  241. mutex_init(&rt->line_in_notify.mutex);
  242. mutex_init(&rt->line_out_notify.mutex);
  243. }
  244. static void ftr_gpio_exit(struct gpio_runtime *rt)
  245. {
  246. ftr_gpio_all_amps_off(rt);
  247. rt->implementation_private = 0;
  248. if (rt->headphone_notify.notify)
  249. free_irq(headphone_detect_irq, &rt->headphone_notify);
  250. if (rt->line_in_notify.gpio_private)
  251. free_irq(linein_detect_irq, &rt->line_in_notify);
  252. if (rt->line_out_notify.gpio_private)
  253. free_irq(lineout_detect_irq, &rt->line_out_notify);
  254. cancel_delayed_work_sync(&rt->headphone_notify.work);
  255. cancel_delayed_work_sync(&rt->line_in_notify.work);
  256. cancel_delayed_work_sync(&rt->line_out_notify.work);
  257. mutex_destroy(&rt->headphone_notify.mutex);
  258. mutex_destroy(&rt->line_in_notify.mutex);
  259. mutex_destroy(&rt->line_out_notify.mutex);
  260. }
  261. static irqreturn_t ftr_handle_notify_irq(int xx, void *data)
  262. {
  263. struct gpio_notification *notif = data;
  264. schedule_delayed_work(&notif->work, 0);
  265. return IRQ_HANDLED;
  266. }
  267. static int ftr_set_notify(struct gpio_runtime *rt,
  268. enum notify_type type,
  269. notify_func_t notify,
  270. void *data)
  271. {
  272. struct gpio_notification *notif;
  273. notify_func_t old;
  274. int irq;
  275. char *name;
  276. int err = -EBUSY;
  277. switch (type) {
  278. case AOA_NOTIFY_HEADPHONE:
  279. notif = &rt->headphone_notify;
  280. name = "headphone-detect";
  281. irq = headphone_detect_irq;
  282. break;
  283. case AOA_NOTIFY_LINE_IN:
  284. notif = &rt->line_in_notify;
  285. name = "linein-detect";
  286. irq = linein_detect_irq;
  287. break;
  288. case AOA_NOTIFY_LINE_OUT:
  289. notif = &rt->line_out_notify;
  290. name = "lineout-detect";
  291. irq = lineout_detect_irq;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. if (irq == NO_IRQ)
  297. return -ENODEV;
  298. mutex_lock(&notif->mutex);
  299. old = notif->notify;
  300. if (!old && !notify) {
  301. err = 0;
  302. goto out_unlock;
  303. }
  304. if (old && notify) {
  305. if (old == notify && notif->data == data)
  306. err = 0;
  307. goto out_unlock;
  308. }
  309. if (old && !notify)
  310. free_irq(irq, notif);
  311. if (!old && notify) {
  312. err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
  313. if (err)
  314. goto out_unlock;
  315. }
  316. notif->notify = notify;
  317. notif->data = data;
  318. err = 0;
  319. out_unlock:
  320. mutex_unlock(&notif->mutex);
  321. return err;
  322. }
  323. static int ftr_get_detect(struct gpio_runtime *rt,
  324. enum notify_type type)
  325. {
  326. int gpio, ret, active;
  327. switch (type) {
  328. case AOA_NOTIFY_HEADPHONE:
  329. gpio = headphone_detect_gpio;
  330. active = headphone_detect_gpio_activestate;
  331. break;
  332. case AOA_NOTIFY_LINE_IN:
  333. gpio = linein_detect_gpio;
  334. active = linein_detect_gpio_activestate;
  335. break;
  336. case AOA_NOTIFY_LINE_OUT:
  337. gpio = lineout_detect_gpio;
  338. active = lineout_detect_gpio_activestate;
  339. break;
  340. default:
  341. return -EINVAL;
  342. }
  343. if (gpio == -1)
  344. return -ENODEV;
  345. ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  346. if (ret < 0)
  347. return ret;
  348. return ((ret >> 1) & 1) == active;
  349. }
  350. static struct gpio_methods methods = {
  351. .init = ftr_gpio_init,
  352. .exit = ftr_gpio_exit,
  353. .all_amps_off = ftr_gpio_all_amps_off,
  354. .all_amps_restore = ftr_gpio_all_amps_restore,
  355. .set_headphone = ftr_gpio_set_headphone,
  356. .set_speakers = ftr_gpio_set_amp,
  357. .set_lineout = ftr_gpio_set_lineout,
  358. .set_hw_reset = ftr_gpio_set_hw_reset,
  359. .get_headphone = ftr_gpio_get_headphone,
  360. .get_speakers = ftr_gpio_get_amp,
  361. .get_lineout = ftr_gpio_get_lineout,
  362. .set_notify = ftr_set_notify,
  363. .get_detect = ftr_get_detect,
  364. };
  365. struct gpio_methods *ftr_gpio_methods = &methods;
  366. EXPORT_SYMBOL_GPL(ftr_gpio_methods);