gpio-feature.c 11 KB

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