gpio-feature.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. of_node_put(np);
  83. return NULL;
  84. }
  85. *gpioptr = *reg;
  86. /* this is a hack, usually the GPIOs 'reg' property
  87. * should have the offset based from the GPIO space
  88. * which is at 0x50, but apparently not always... */
  89. if (*gpioptr < 0x50)
  90. *gpioptr += 0x50;
  91. reg = of_get_property(np, "audio-gpio-active-state", NULL);
  92. if (!reg)
  93. /* Apple seems to default to 1, but
  94. * that doesn't seem right at least on most
  95. * machines. So until proven that the opposite
  96. * is necessary, we default to 0
  97. * (which, incidentally, snd-powermac also does...) */
  98. *gpioactiveptr = 0;
  99. else
  100. *gpioactiveptr = *reg;
  101. return np;
  102. }
  103. static void get_irq(struct device_node * np, int *irqptr)
  104. {
  105. if (np)
  106. *irqptr = irq_of_parse_and_map(np, 0);
  107. else
  108. *irqptr = NO_IRQ;
  109. }
  110. /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */
  111. #define SWITCH_GPIO(name, v, on) \
  112. (((v)&~1) | ((on)? \
  113. (name##_gpio_activestate==0?4:5): \
  114. (name##_gpio_activestate==0?5:4)))
  115. #define FTR_GPIO(name, bit) \
  116. static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\
  117. { \
  118. int v; \
  119. \
  120. if (unlikely(!rt)) return; \
  121. \
  122. if (name##_mute_gpio < 0) \
  123. return; \
  124. \
  125. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \
  126. name##_mute_gpio, \
  127. 0); \
  128. \
  129. /* muted = !on... */ \
  130. v = SWITCH_GPIO(name##_mute, v, !on); \
  131. \
  132. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \
  133. name##_mute_gpio, v); \
  134. \
  135. rt->implementation_private &= ~(1<<bit); \
  136. rt->implementation_private |= (!!on << bit); \
  137. } \
  138. static int ftr_gpio_get_##name(struct gpio_runtime *rt) \
  139. { \
  140. if (unlikely(!rt)) return 0; \
  141. return (rt->implementation_private>>bit)&1; \
  142. }
  143. FTR_GPIO(headphone, 0);
  144. FTR_GPIO(amp, 1);
  145. FTR_GPIO(lineout, 2);
  146. FTR_GPIO(master, 3);
  147. static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on)
  148. {
  149. int v;
  150. if (unlikely(!rt)) return;
  151. if (hw_reset_gpio < 0)
  152. return;
  153. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL,
  154. hw_reset_gpio, 0);
  155. v = SWITCH_GPIO(hw_reset, v, on);
  156. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL,
  157. hw_reset_gpio, v);
  158. }
  159. static struct gpio_methods methods;
  160. static void ftr_gpio_all_amps_off(struct gpio_runtime *rt)
  161. {
  162. int saved;
  163. if (unlikely(!rt)) return;
  164. saved = rt->implementation_private;
  165. ftr_gpio_set_headphone(rt, 0);
  166. ftr_gpio_set_amp(rt, 0);
  167. ftr_gpio_set_lineout(rt, 0);
  168. if (methods.set_master)
  169. ftr_gpio_set_master(rt, 0);
  170. rt->implementation_private = saved;
  171. }
  172. static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt)
  173. {
  174. int s;
  175. if (unlikely(!rt)) return;
  176. s = rt->implementation_private;
  177. ftr_gpio_set_headphone(rt, (s>>0)&1);
  178. ftr_gpio_set_amp(rt, (s>>1)&1);
  179. ftr_gpio_set_lineout(rt, (s>>2)&1);
  180. if (methods.set_master)
  181. ftr_gpio_set_master(rt, (s>>3)&1);
  182. }
  183. static void ftr_handle_notify(struct work_struct *work)
  184. {
  185. struct gpio_notification *notif =
  186. container_of(work, struct gpio_notification, work.work);
  187. mutex_lock(&notif->mutex);
  188. if (notif->notify)
  189. notif->notify(notif->data);
  190. mutex_unlock(&notif->mutex);
  191. }
  192. static void gpio_enable_dual_edge(int gpio)
  193. {
  194. int v;
  195. if (gpio == -1)
  196. return;
  197. v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  198. v |= 0x80; /* enable dual edge */
  199. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v);
  200. }
  201. static void ftr_gpio_init(struct gpio_runtime *rt)
  202. {
  203. get_gpio("headphone-mute", NULL,
  204. &headphone_mute_gpio,
  205. &headphone_mute_gpio_activestate);
  206. get_gpio("amp-mute", NULL,
  207. &amp_mute_gpio,
  208. &amp_mute_gpio_activestate);
  209. get_gpio("lineout-mute", NULL,
  210. &lineout_mute_gpio,
  211. &lineout_mute_gpio_activestate);
  212. get_gpio("hw-reset", "audio-hw-reset",
  213. &hw_reset_gpio,
  214. &hw_reset_gpio_activestate);
  215. if (get_gpio("master-mute", NULL,
  216. &master_mute_gpio,
  217. &master_mute_gpio_activestate)) {
  218. methods.set_master = ftr_gpio_set_master;
  219. methods.get_master = ftr_gpio_get_master;
  220. }
  221. headphone_detect_node = get_gpio("headphone-detect", NULL,
  222. &headphone_detect_gpio,
  223. &headphone_detect_gpio_activestate);
  224. /* go Apple, and thanks for giving these different names
  225. * across the board... */
  226. lineout_detect_node = get_gpio("lineout-detect", "line-output-detect",
  227. &lineout_detect_gpio,
  228. &lineout_detect_gpio_activestate);
  229. linein_detect_node = get_gpio("linein-detect", "line-input-detect",
  230. &linein_detect_gpio,
  231. &linein_detect_gpio_activestate);
  232. gpio_enable_dual_edge(headphone_detect_gpio);
  233. gpio_enable_dual_edge(lineout_detect_gpio);
  234. gpio_enable_dual_edge(linein_detect_gpio);
  235. get_irq(headphone_detect_node, &headphone_detect_irq);
  236. get_irq(lineout_detect_node, &lineout_detect_irq);
  237. get_irq(linein_detect_node, &linein_detect_irq);
  238. ftr_gpio_all_amps_off(rt);
  239. rt->implementation_private = 0;
  240. INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify);
  241. INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify);
  242. INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify);
  243. mutex_init(&rt->headphone_notify.mutex);
  244. mutex_init(&rt->line_in_notify.mutex);
  245. mutex_init(&rt->line_out_notify.mutex);
  246. }
  247. static void ftr_gpio_exit(struct gpio_runtime *rt)
  248. {
  249. ftr_gpio_all_amps_off(rt);
  250. rt->implementation_private = 0;
  251. if (rt->headphone_notify.notify)
  252. free_irq(headphone_detect_irq, &rt->headphone_notify);
  253. if (rt->line_in_notify.gpio_private)
  254. free_irq(linein_detect_irq, &rt->line_in_notify);
  255. if (rt->line_out_notify.gpio_private)
  256. free_irq(lineout_detect_irq, &rt->line_out_notify);
  257. cancel_delayed_work_sync(&rt->headphone_notify.work);
  258. cancel_delayed_work_sync(&rt->line_in_notify.work);
  259. cancel_delayed_work_sync(&rt->line_out_notify.work);
  260. mutex_destroy(&rt->headphone_notify.mutex);
  261. mutex_destroy(&rt->line_in_notify.mutex);
  262. mutex_destroy(&rt->line_out_notify.mutex);
  263. }
  264. static irqreturn_t ftr_handle_notify_irq(int xx, void *data)
  265. {
  266. struct gpio_notification *notif = data;
  267. schedule_delayed_work(&notif->work, 0);
  268. return IRQ_HANDLED;
  269. }
  270. static int ftr_set_notify(struct gpio_runtime *rt,
  271. enum notify_type type,
  272. notify_func_t notify,
  273. void *data)
  274. {
  275. struct gpio_notification *notif;
  276. notify_func_t old;
  277. int irq;
  278. char *name;
  279. int err = -EBUSY;
  280. switch (type) {
  281. case AOA_NOTIFY_HEADPHONE:
  282. notif = &rt->headphone_notify;
  283. name = "headphone-detect";
  284. irq = headphone_detect_irq;
  285. break;
  286. case AOA_NOTIFY_LINE_IN:
  287. notif = &rt->line_in_notify;
  288. name = "linein-detect";
  289. irq = linein_detect_irq;
  290. break;
  291. case AOA_NOTIFY_LINE_OUT:
  292. notif = &rt->line_out_notify;
  293. name = "lineout-detect";
  294. irq = lineout_detect_irq;
  295. break;
  296. default:
  297. return -EINVAL;
  298. }
  299. if (irq == NO_IRQ)
  300. return -ENODEV;
  301. mutex_lock(&notif->mutex);
  302. old = notif->notify;
  303. if (!old && !notify) {
  304. err = 0;
  305. goto out_unlock;
  306. }
  307. if (old && notify) {
  308. if (old == notify && notif->data == data)
  309. err = 0;
  310. goto out_unlock;
  311. }
  312. if (old && !notify)
  313. free_irq(irq, notif);
  314. if (!old && notify) {
  315. err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif);
  316. if (err)
  317. goto out_unlock;
  318. }
  319. notif->notify = notify;
  320. notif->data = data;
  321. err = 0;
  322. out_unlock:
  323. mutex_unlock(&notif->mutex);
  324. return err;
  325. }
  326. static int ftr_get_detect(struct gpio_runtime *rt,
  327. enum notify_type type)
  328. {
  329. int gpio, ret, active;
  330. switch (type) {
  331. case AOA_NOTIFY_HEADPHONE:
  332. gpio = headphone_detect_gpio;
  333. active = headphone_detect_gpio_activestate;
  334. break;
  335. case AOA_NOTIFY_LINE_IN:
  336. gpio = linein_detect_gpio;
  337. active = linein_detect_gpio_activestate;
  338. break;
  339. case AOA_NOTIFY_LINE_OUT:
  340. gpio = lineout_detect_gpio;
  341. active = lineout_detect_gpio_activestate;
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. if (gpio == -1)
  347. return -ENODEV;
  348. ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0);
  349. if (ret < 0)
  350. return ret;
  351. return ((ret >> 1) & 1) == active;
  352. }
  353. static struct gpio_methods methods = {
  354. .init = ftr_gpio_init,
  355. .exit = ftr_gpio_exit,
  356. .all_amps_off = ftr_gpio_all_amps_off,
  357. .all_amps_restore = ftr_gpio_all_amps_restore,
  358. .set_headphone = ftr_gpio_set_headphone,
  359. .set_speakers = ftr_gpio_set_amp,
  360. .set_lineout = ftr_gpio_set_lineout,
  361. .set_hw_reset = ftr_gpio_set_hw_reset,
  362. .get_headphone = ftr_gpio_get_headphone,
  363. .get_speakers = ftr_gpio_get_amp,
  364. .get_lineout = ftr_gpio_get_lineout,
  365. .set_notify = ftr_set_notify,
  366. .get_detect = ftr_get_detect,
  367. };
  368. struct gpio_methods *ftr_gpio_methods = &methods;
  369. EXPORT_SYMBOL_GPL(ftr_gpio_methods);