toonie.c 9.2 KB

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