toonie.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Mac Mini "toonie" mixer control
  3. *
  4. * Copyright (c) 2005 by Benjamin Herrenschmidt <benh@kernel.crashing.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <sound/driver.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-dev.h>
  25. #include <linux/kmod.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <sound/core.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/machdep.h>
  32. #include <asm/pmac_feature.h>
  33. #include "pmac.h"
  34. #undef DEBUG
  35. #ifdef DEBUG
  36. #define DBG(fmt...) printk(fmt)
  37. #else
  38. #define DBG(fmt...)
  39. #endif
  40. struct pmac_gpio {
  41. unsigned int addr;
  42. u8 active_val;
  43. u8 inactive_val;
  44. u8 active_state;
  45. };
  46. struct pmac_toonie
  47. {
  48. struct pmac_gpio hp_detect_gpio;
  49. struct pmac_gpio hp_mute_gpio;
  50. struct pmac_gpio amp_mute_gpio;
  51. int hp_detect_irq;
  52. int auto_mute_notify;
  53. struct work_struct detect_work;
  54. };
  55. /*
  56. * gpio access
  57. */
  58. #define do_gpio_write(gp, val) \
  59. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, (gp)->addr, val)
  60. #define do_gpio_read(gp) \
  61. pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, (gp)->addr, 0)
  62. #define tumbler_gpio_free(gp) /* NOP */
  63. static void write_audio_gpio(struct pmac_gpio *gp, int active)
  64. {
  65. if (! gp->addr)
  66. return;
  67. active = active ? gp->active_val : gp->inactive_val;
  68. do_gpio_write(gp, active);
  69. DBG("(I) gpio %x write %d\n", gp->addr, active);
  70. }
  71. static int check_audio_gpio(struct pmac_gpio *gp)
  72. {
  73. int ret;
  74. if (! gp->addr)
  75. return 0;
  76. ret = do_gpio_read(gp);
  77. return (ret & 0xd) == (gp->active_val & 0xd);
  78. }
  79. static int read_audio_gpio(struct pmac_gpio *gp)
  80. {
  81. int ret;
  82. if (! gp->addr)
  83. return 0;
  84. ret = ((do_gpio_read(gp) & 0x02) !=0);
  85. return ret == gp->active_state;
  86. }
  87. enum { TOONIE_MUTE_HP, TOONIE_MUTE_AMP };
  88. static int toonie_get_mute_switch(snd_kcontrol_t *kcontrol,
  89. snd_ctl_elem_value_t *ucontrol)
  90. {
  91. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  92. struct pmac_toonie *mix = chip->mixer_data;
  93. struct pmac_gpio *gp;
  94. if (mix == NULL)
  95. return -ENODEV;
  96. switch(kcontrol->private_value) {
  97. case TOONIE_MUTE_HP:
  98. gp = &mix->hp_mute_gpio;
  99. break;
  100. case TOONIE_MUTE_AMP:
  101. gp = &mix->amp_mute_gpio;
  102. break;
  103. default:
  104. return -EINVAL;;
  105. }
  106. ucontrol->value.integer.value[0] = !check_audio_gpio(gp);
  107. return 0;
  108. }
  109. static int toonie_put_mute_switch(snd_kcontrol_t *kcontrol,
  110. snd_ctl_elem_value_t *ucontrol)
  111. {
  112. pmac_t *chip = snd_kcontrol_chip(kcontrol);
  113. struct pmac_toonie *mix = chip->mixer_data;
  114. struct pmac_gpio *gp;
  115. int val;
  116. if (chip->update_automute && chip->auto_mute)
  117. return 0; /* don't touch in the auto-mute mode */
  118. if (mix == NULL)
  119. return -ENODEV;
  120. switch(kcontrol->private_value) {
  121. case TOONIE_MUTE_HP:
  122. gp = &mix->hp_mute_gpio;
  123. break;
  124. case TOONIE_MUTE_AMP:
  125. gp = &mix->amp_mute_gpio;
  126. break;
  127. default:
  128. return -EINVAL;;
  129. }
  130. val = ! check_audio_gpio(gp);
  131. if (val != ucontrol->value.integer.value[0]) {
  132. write_audio_gpio(gp, ! ucontrol->value.integer.value[0]);
  133. return 1;
  134. }
  135. return 0;
  136. }
  137. static snd_kcontrol_new_t toonie_hp_sw __initdata = {
  138. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  139. .name = "Headphone Playback Switch",
  140. .info = snd_pmac_boolean_mono_info,
  141. .get = toonie_get_mute_switch,
  142. .put = toonie_put_mute_switch,
  143. .private_value = TOONIE_MUTE_HP,
  144. };
  145. static snd_kcontrol_new_t toonie_speaker_sw __initdata = {
  146. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  147. .name = "PC Speaker Playback Switch",
  148. .info = snd_pmac_boolean_mono_info,
  149. .get = toonie_get_mute_switch,
  150. .put = toonie_put_mute_switch,
  151. .private_value = TOONIE_MUTE_AMP,
  152. };
  153. /*
  154. * auto-mute stuffs
  155. */
  156. static int toonie_detect_headphone(pmac_t *chip)
  157. {
  158. struct pmac_toonie *mix = chip->mixer_data;
  159. int detect = 0;
  160. if (mix->hp_detect_gpio.addr)
  161. detect |= read_audio_gpio(&mix->hp_detect_gpio);
  162. return detect;
  163. }
  164. static void toonie_check_mute(pmac_t *chip, struct pmac_gpio *gp, int val,
  165. int do_notify, snd_kcontrol_t *sw)
  166. {
  167. if (check_audio_gpio(gp) != val) {
  168. write_audio_gpio(gp, val);
  169. if (do_notify)
  170. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  171. &sw->id);
  172. }
  173. }
  174. static void toonie_detect_handler(void *self)
  175. {
  176. pmac_t *chip = (pmac_t*) self;
  177. struct pmac_toonie *mix;
  178. int headphone;
  179. if (!chip)
  180. return;
  181. mix = chip->mixer_data;
  182. snd_assert(mix, return);
  183. headphone = toonie_detect_headphone(chip);
  184. DBG("headphone: %d, lineout: %d\n", headphone, lineout);
  185. if (headphone) {
  186. /* unmute headphone/lineout & mute speaker */
  187. toonie_check_mute(chip, &mix->hp_mute_gpio, 0,
  188. mix->auto_mute_notify, chip->master_sw_ctl);
  189. toonie_check_mute(chip, &mix->amp_mute_gpio, 1,
  190. mix->auto_mute_notify, chip->speaker_sw_ctl);
  191. } else {
  192. /* unmute speaker, mute others */
  193. toonie_check_mute(chip, &mix->amp_mute_gpio, 0,
  194. mix->auto_mute_notify, chip->speaker_sw_ctl);
  195. toonie_check_mute(chip, &mix->hp_mute_gpio, 1,
  196. mix->auto_mute_notify, chip->master_sw_ctl);
  197. }
  198. if (mix->auto_mute_notify) {
  199. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  200. &chip->hp_detect_ctl->id);
  201. }
  202. }
  203. static void toonie_update_automute(pmac_t *chip, int do_notify)
  204. {
  205. if (chip->auto_mute) {
  206. struct pmac_toonie *mix;
  207. mix = chip->mixer_data;
  208. snd_assert(mix, return);
  209. mix->auto_mute_notify = do_notify;
  210. schedule_work(&mix->detect_work);
  211. }
  212. }
  213. /* interrupt - headphone plug changed */
  214. static irqreturn_t toonie_hp_intr(int irq, void *devid, struct pt_regs *regs)
  215. {
  216. pmac_t *chip = devid;
  217. if (chip->update_automute && chip->initialized) {
  218. chip->update_automute(chip, 1);
  219. return IRQ_HANDLED;
  220. }
  221. return IRQ_NONE;
  222. }
  223. /* look for audio gpio device */
  224. static int find_audio_gpio(const char *name, const char *platform,
  225. struct pmac_gpio *gp)
  226. {
  227. struct device_node *np;
  228. u32 *base, addr;
  229. if (! (np = find_devices("gpio")))
  230. return -ENODEV;
  231. for (np = np->child; np; np = np->sibling) {
  232. char *property = get_property(np, "audio-gpio", NULL);
  233. if (property && strcmp(property, name) == 0)
  234. break;
  235. if (device_is_compatible(np, name))
  236. break;
  237. }
  238. if (np == NULL)
  239. return -ENODEV;
  240. base = (u32 *)get_property(np, "AAPL,address", NULL);
  241. if (! base) {
  242. base = (u32 *)get_property(np, "reg", NULL);
  243. if (!base) {
  244. DBG("(E) cannot find address for device %s !\n", name);
  245. return -ENODEV;
  246. }
  247. addr = *base;
  248. if (addr < 0x50)
  249. addr += 0x50;
  250. } else
  251. addr = *base;
  252. gp->addr = addr & 0x0000ffff;
  253. /* Try to find the active state, default to 0 ! */
  254. base = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
  255. if (base) {
  256. gp->active_state = *base;
  257. gp->active_val = (*base) ? 0x5 : 0x4;
  258. gp->inactive_val = (*base) ? 0x4 : 0x5;
  259. } else {
  260. u32 *prop = NULL;
  261. gp->active_state = 0;
  262. gp->active_val = 0x4;
  263. gp->inactive_val = 0x5;
  264. /* Here are some crude hacks to extract the GPIO polarity and
  265. * open collector informations out of the do-platform script
  266. * as we don't yet have an interpreter for these things
  267. */
  268. if (platform)
  269. prop = (u32 *)get_property(np, platform, NULL);
  270. if (prop) {
  271. if (prop[3] == 0x9 && prop[4] == 0x9) {
  272. gp->active_val = 0xd;
  273. gp->inactive_val = 0xc;
  274. }
  275. if (prop[3] == 0x1 && prop[4] == 0x1) {
  276. gp->active_val = 0x5;
  277. gp->inactive_val = 0x4;
  278. }
  279. }
  280. }
  281. DBG("(I) GPIO device %s found, offset: %x, active state: %d !\n",
  282. name, gp->addr, gp->active_state);
  283. return (np->n_intrs > 0) ? np->intrs[0].line : 0;
  284. }
  285. static void toonie_cleanup(pmac_t *chip)
  286. {
  287. struct pmac_toonie *mix = chip->mixer_data;
  288. if (! mix)
  289. return;
  290. if (mix->hp_detect_irq >= 0)
  291. free_irq(mix->hp_detect_irq, chip);
  292. kfree(mix);
  293. chip->mixer_data = NULL;
  294. }
  295. int snd_pmac_toonie_init(pmac_t *chip)
  296. {
  297. struct pmac_toonie *mix;
  298. mix = kmalloc(sizeof(*mix), GFP_KERNEL);
  299. if (! mix)
  300. return -ENOMEM;
  301. chip->mixer_data = mix;
  302. chip->mixer_free = toonie_cleanup;
  303. find_audio_gpio("headphone-mute", NULL, &mix->hp_mute_gpio);
  304. find_audio_gpio("amp-mute", NULL, &mix->amp_mute_gpio);
  305. mix->hp_detect_irq = find_audio_gpio("headphone-detect",
  306. NULL, &mix->hp_detect_gpio);
  307. strcpy(chip->card->mixername, "PowerMac Toonie");
  308. chip->master_sw_ctl = snd_ctl_new1(&toonie_hp_sw, chip);
  309. snd_ctl_add(chip->card, chip->master_sw_ctl);
  310. chip->speaker_sw_ctl = snd_ctl_new1(&toonie_speaker_sw, chip);
  311. snd_ctl_add(chip->card, chip->speaker_sw_ctl);
  312. INIT_WORK(&mix->detect_work, toonie_detect_handler, (void *)chip);
  313. if (mix->hp_detect_irq >= 0) {
  314. snd_pmac_add_automute(chip);
  315. chip->detect_headphone = toonie_detect_headphone;
  316. chip->update_automute = toonie_update_automute;
  317. toonie_update_automute(chip, 0);
  318. if (request_irq(mix->hp_detect_irq, toonie_hp_intr, 0,
  319. "Sound Headphone Detection", chip) < 0)
  320. mix->hp_detect_irq = -1;
  321. }
  322. return 0;
  323. }