KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / sound / aoa / fabrics / layout.c
blob9dc5806d23dd51134b90fb0f8c0966a0588ccd6c
1 /*
2 * Apple Onboard Audio driver -- layout/machine id fabric
4 * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
9 * This fabric module looks for sound codecs based on the
10 * layout-id or device-id property in the device tree.
12 #include <asm/prom.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include "../aoa.h"
17 #include "../soundbus/soundbus.h"
19 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("Layout-ID fabric for snd-aoa");
23 #define MAX_CODECS_PER_BUS 2
25 /* These are the connections the layout fabric
26 * knows about. It doesn't really care about the
27 * input ones, but I thought I'd separate them
28 * to give them proper names. The thing is that
29 * Apple usually will distinguish the active output
30 * by GPIOs, while the active input is set directly
31 * on the codec. Hence we here tell the codec what
32 * we think is connected. This information is hard-
33 * coded below ... */
34 #define CC_SPEAKERS (1<<0)
35 #define CC_HEADPHONE (1<<1)
36 #define CC_LINEOUT (1<<2)
37 #define CC_DIGITALOUT (1<<3)
38 #define CC_LINEIN (1<<4)
39 #define CC_MICROPHONE (1<<5)
40 #define CC_DIGITALIN (1<<6)
41 /* pretty bogus but users complain...
42 * This is a flag saying that the LINEOUT
43 * should be renamed to HEADPHONE.
44 * be careful with input detection! */
45 #define CC_LINEOUT_LABELLED_HEADPHONE (1<<7)
47 struct codec_connection {
48 /* CC_ flags from above */
49 int connected;
50 /* codec dependent bit to be set in the aoa_codec.connected field.
51 * This intentionally doesn't have any generic flags because the
52 * fabric has to know the codec anyway and all codecs might have
53 * different connectors */
54 int codec_bit;
57 struct codec_connect_info {
58 char *name;
59 struct codec_connection *connections;
62 #define LAYOUT_FLAG_COMBO_LINEOUT_SPDIF (1<<0)
64 struct layout {
65 unsigned int layout_id, device_id;
66 struct codec_connect_info codecs[MAX_CODECS_PER_BUS];
67 int flags;
69 /* if busname is not assigned, we use 'Master' below,
70 * so that our layout table doesn't need to be filled
71 * too much.
72 * We only assign these two if we expect to find more
73 * than one soundbus, i.e. on those machines with
74 * multiple layout-ids */
75 char *busname;
76 int pcmid;
79 MODULE_ALIAS("sound-layout-36");
80 MODULE_ALIAS("sound-layout-41");
81 MODULE_ALIAS("sound-layout-45");
82 MODULE_ALIAS("sound-layout-47");
83 MODULE_ALIAS("sound-layout-48");
84 MODULE_ALIAS("sound-layout-49");
85 MODULE_ALIAS("sound-layout-50");
86 MODULE_ALIAS("sound-layout-51");
87 MODULE_ALIAS("sound-layout-56");
88 MODULE_ALIAS("sound-layout-57");
89 MODULE_ALIAS("sound-layout-58");
90 MODULE_ALIAS("sound-layout-60");
91 MODULE_ALIAS("sound-layout-61");
92 MODULE_ALIAS("sound-layout-62");
93 MODULE_ALIAS("sound-layout-64");
94 MODULE_ALIAS("sound-layout-65");
95 MODULE_ALIAS("sound-layout-66");
96 MODULE_ALIAS("sound-layout-67");
97 MODULE_ALIAS("sound-layout-68");
98 MODULE_ALIAS("sound-layout-69");
99 MODULE_ALIAS("sound-layout-70");
100 MODULE_ALIAS("sound-layout-72");
101 MODULE_ALIAS("sound-layout-76");
102 MODULE_ALIAS("sound-layout-80");
103 MODULE_ALIAS("sound-layout-82");
104 MODULE_ALIAS("sound-layout-84");
105 MODULE_ALIAS("sound-layout-86");
106 MODULE_ALIAS("sound-layout-90");
107 MODULE_ALIAS("sound-layout-92");
108 MODULE_ALIAS("sound-layout-94");
109 MODULE_ALIAS("sound-layout-96");
110 MODULE_ALIAS("sound-layout-98");
111 MODULE_ALIAS("sound-layout-100");
113 MODULE_ALIAS("aoa-device-id-14");
114 MODULE_ALIAS("aoa-device-id-22");
115 MODULE_ALIAS("aoa-device-id-35");
116 MODULE_ALIAS("aoa-device-id-44");
118 /* onyx with all but microphone connected */
119 static struct codec_connection onyx_connections_nomic[] = {
121 .connected = CC_SPEAKERS | CC_HEADPHONE | CC_LINEOUT,
122 .codec_bit = 0,
125 .connected = CC_DIGITALOUT,
126 .codec_bit = 1,
129 .connected = CC_LINEIN,
130 .codec_bit = 2,
132 {} /* terminate array by .connected == 0 */
135 /* onyx on machines without headphone */
136 static struct codec_connection onyx_connections_noheadphones[] = {
138 .connected = CC_SPEAKERS | CC_LINEOUT |
139 CC_LINEOUT_LABELLED_HEADPHONE,
140 .codec_bit = 0,
143 .connected = CC_DIGITALOUT,
144 .codec_bit = 1,
146 /* FIXME: are these correct? probably not for all the machines
147 * below ... If not this will need separating. */
149 .connected = CC_LINEIN,
150 .codec_bit = 2,
153 .connected = CC_MICROPHONE,
154 .codec_bit = 3,
156 {} /* terminate array by .connected == 0 */
159 /* onyx on machines with real line-out */
160 static struct codec_connection onyx_connections_reallineout[] = {
162 .connected = CC_SPEAKERS | CC_LINEOUT | CC_HEADPHONE,
163 .codec_bit = 0,
166 .connected = CC_DIGITALOUT,
167 .codec_bit = 1,
170 .connected = CC_LINEIN,
171 .codec_bit = 2,
173 {} /* terminate array by .connected == 0 */
176 /* tas on machines without line out */
177 static struct codec_connection tas_connections_nolineout[] = {
179 .connected = CC_SPEAKERS | CC_HEADPHONE,
180 .codec_bit = 0,
183 .connected = CC_LINEIN,
184 .codec_bit = 2,
187 .connected = CC_MICROPHONE,
188 .codec_bit = 3,
190 {} /* terminate array by .connected == 0 */
193 /* tas on machines with neither line out nor line in */
194 static struct codec_connection tas_connections_noline[] = {
196 .connected = CC_SPEAKERS | CC_HEADPHONE,
197 .codec_bit = 0,
200 .connected = CC_MICROPHONE,
201 .codec_bit = 3,
203 {} /* terminate array by .connected == 0 */
206 /* tas on machines without microphone */
207 static struct codec_connection tas_connections_nomic[] = {
209 .connected = CC_SPEAKERS | CC_HEADPHONE | CC_LINEOUT,
210 .codec_bit = 0,
213 .connected = CC_LINEIN,
214 .codec_bit = 2,
216 {} /* terminate array by .connected == 0 */
219 /* tas on machines with everything connected */
220 static struct codec_connection tas_connections_all[] = {
222 .connected = CC_SPEAKERS | CC_HEADPHONE | CC_LINEOUT,
223 .codec_bit = 0,
226 .connected = CC_LINEIN,
227 .codec_bit = 2,
230 .connected = CC_MICROPHONE,
231 .codec_bit = 3,
233 {} /* terminate array by .connected == 0 */
236 static struct codec_connection toonie_connections[] = {
238 .connected = CC_SPEAKERS | CC_HEADPHONE,
239 .codec_bit = 0,
241 {} /* terminate array by .connected == 0 */
244 static struct codec_connection topaz_input[] = {
246 .connected = CC_DIGITALIN,
247 .codec_bit = 0,
249 {} /* terminate array by .connected == 0 */
252 static struct codec_connection topaz_output[] = {
254 .connected = CC_DIGITALOUT,
255 .codec_bit = 1,
257 {} /* terminate array by .connected == 0 */
260 static struct codec_connection topaz_inout[] = {
262 .connected = CC_DIGITALIN,
263 .codec_bit = 0,
266 .connected = CC_DIGITALOUT,
267 .codec_bit = 1,
269 {} /* terminate array by .connected == 0 */
272 static struct layout layouts[] = {
273 /* last PowerBooks (15" Oct 2005) */
274 { .layout_id = 82,
275 .flags = LAYOUT_FLAG_COMBO_LINEOUT_SPDIF,
276 .codecs[0] = {
277 .name = "onyx",
278 .connections = onyx_connections_noheadphones,
280 .codecs[1] = {
281 .name = "topaz",
282 .connections = topaz_input,
285 /* PowerMac9,1 */
286 { .layout_id = 60,
287 .codecs[0] = {
288 .name = "onyx",
289 .connections = onyx_connections_reallineout,
292 /* PowerMac9,1 */
293 { .layout_id = 61,
294 .codecs[0] = {
295 .name = "topaz",
296 .connections = topaz_input,
299 /* PowerBook5,7 */
300 { .layout_id = 64,
301 .flags = LAYOUT_FLAG_COMBO_LINEOUT_SPDIF,
302 .codecs[0] = {
303 .name = "onyx",
304 .connections = onyx_connections_noheadphones,
307 /* PowerBook5,7 */
308 { .layout_id = 65,
309 .codecs[0] = {
310 .name = "topaz",
311 .connections = topaz_input,
314 /* PowerBook5,9 [17" Oct 2005] */
315 { .layout_id = 84,
316 .flags = LAYOUT_FLAG_COMBO_LINEOUT_SPDIF,
317 .codecs[0] = {
318 .name = "onyx",
319 .connections = onyx_connections_noheadphones,
321 .codecs[1] = {
322 .name = "topaz",
323 .connections = topaz_input,
326 /* PowerMac8,1 */
327 { .layout_id = 45,
328 .codecs[0] = {
329 .name = "onyx",
330 .connections = onyx_connections_noheadphones,
332 .codecs[1] = {
333 .name = "topaz",
334 .connections = topaz_input,
337 /* Quad PowerMac (analog in, analog/digital out) */
338 { .layout_id = 68,
339 .codecs[0] = {
340 .name = "onyx",
341 .connections = onyx_connections_nomic,
344 /* Quad PowerMac (digital in) */
345 { .layout_id = 69,
346 .codecs[0] = {
347 .name = "topaz",
348 .connections = topaz_input,
350 .busname = "digital in", .pcmid = 1 },
351 /* Early 2005 PowerBook (PowerBook 5,6) */
352 { .layout_id = 70,
353 .codecs[0] = {
354 .name = "tas",
355 .connections = tas_connections_nolineout,
358 /* PowerBook 5,4 */
359 { .layout_id = 51,
360 .codecs[0] = {
361 .name = "tas",
362 .connections = tas_connections_nolineout,
365 /* PowerBook6,5 */
366 { .device_id = 44,
367 .codecs[0] = {
368 .name = "tas",
369 .connections = tas_connections_all,
372 /* PowerBook6,7 */
373 { .layout_id = 80,
374 .codecs[0] = {
375 .name = "tas",
376 .connections = tas_connections_noline,
379 /* PowerBook6,8 */
380 { .layout_id = 72,
381 .codecs[0] = {
382 .name = "tas",
383 .connections = tas_connections_nolineout,
386 /* PowerMac8,2 */
387 { .layout_id = 86,
388 .codecs[0] = {
389 .name = "onyx",
390 .connections = onyx_connections_nomic,
392 .codecs[1] = {
393 .name = "topaz",
394 .connections = topaz_input,
397 /* PowerBook6,7 */
398 { .layout_id = 92,
399 .codecs[0] = {
400 .name = "tas",
401 .connections = tas_connections_nolineout,
404 /* PowerMac10,1 (Mac Mini) */
405 { .layout_id = 58,
406 .codecs[0] = {
407 .name = "toonie",
408 .connections = toonie_connections,
412 .layout_id = 96,
413 .codecs[0] = {
414 .name = "onyx",
415 .connections = onyx_connections_noheadphones,
418 /* unknown, untested, but this comes from Apple */
419 { .layout_id = 41,
420 .codecs[0] = {
421 .name = "tas",
422 .connections = tas_connections_all,
425 { .layout_id = 36,
426 .codecs[0] = {
427 .name = "tas",
428 .connections = tas_connections_nomic,
430 .codecs[1] = {
431 .name = "topaz",
432 .connections = topaz_inout,
435 { .layout_id = 47,
436 .codecs[0] = {
437 .name = "onyx",
438 .connections = onyx_connections_noheadphones,
441 { .layout_id = 48,
442 .codecs[0] = {
443 .name = "topaz",
444 .connections = topaz_input,
447 { .layout_id = 49,
448 .codecs[0] = {
449 .name = "onyx",
450 .connections = onyx_connections_nomic,
453 { .layout_id = 50,
454 .codecs[0] = {
455 .name = "topaz",
456 .connections = topaz_input,
459 { .layout_id = 56,
460 .codecs[0] = {
461 .name = "onyx",
462 .connections = onyx_connections_noheadphones,
465 { .layout_id = 57,
466 .codecs[0] = {
467 .name = "topaz",
468 .connections = topaz_input,
471 { .layout_id = 62,
472 .codecs[0] = {
473 .name = "onyx",
474 .connections = onyx_connections_noheadphones,
476 .codecs[1] = {
477 .name = "topaz",
478 .connections = topaz_output,
481 { .layout_id = 66,
482 .codecs[0] = {
483 .name = "onyx",
484 .connections = onyx_connections_noheadphones,
487 { .layout_id = 67,
488 .codecs[0] = {
489 .name = "topaz",
490 .connections = topaz_input,
493 { .layout_id = 76,
494 .codecs[0] = {
495 .name = "tas",
496 .connections = tas_connections_nomic,
498 .codecs[1] = {
499 .name = "topaz",
500 .connections = topaz_inout,
503 { .layout_id = 90,
504 .codecs[0] = {
505 .name = "tas",
506 .connections = tas_connections_noline,
509 { .layout_id = 94,
510 .codecs[0] = {
511 .name = "onyx",
512 /* but it has an external mic?? how to select? */
513 .connections = onyx_connections_noheadphones,
516 { .layout_id = 98,
517 .codecs[0] = {
518 .name = "toonie",
519 .connections = toonie_connections,
522 { .layout_id = 100,
523 .codecs[0] = {
524 .name = "topaz",
525 .connections = topaz_input,
527 .codecs[1] = {
528 .name = "onyx",
529 .connections = onyx_connections_noheadphones,
532 /* PowerMac3,4 */
533 { .device_id = 14,
534 .codecs[0] = {
535 .name = "tas",
536 .connections = tas_connections_noline,
539 /* PowerMac3,6 */
540 { .device_id = 22,
541 .codecs[0] = {
542 .name = "tas",
543 .connections = tas_connections_all,
546 /* PowerBook5,2 */
547 { .device_id = 35,
548 .codecs[0] = {
549 .name = "tas",
550 .connections = tas_connections_all,
556 static struct layout *find_layout_by_id(unsigned int id)
558 struct layout *l;
560 l = layouts;
561 while (l->codecs[0].name) {
562 if (l->layout_id == id)
563 return l;
564 l++;
566 return NULL;
569 static struct layout *find_layout_by_device(unsigned int id)
571 struct layout *l;
573 l = layouts;
574 while (l->codecs[0].name) {
575 if (l->device_id == id)
576 return l;
577 l++;
579 return NULL;
582 static void use_layout(struct layout *l)
584 int i;
586 for (i=0; i<MAX_CODECS_PER_BUS; i++) {
587 if (l->codecs[i].name) {
588 request_module("snd-aoa-codec-%s", l->codecs[i].name);
591 /* now we wait for the codecs to call us back */
594 struct layout_dev;
596 struct layout_dev_ptr {
597 struct layout_dev *ptr;
600 struct layout_dev {
601 struct list_head list;
602 struct soundbus_dev *sdev;
603 struct device_node *sound;
604 struct aoa_codec *codecs[MAX_CODECS_PER_BUS];
605 struct layout *layout;
606 struct gpio_runtime gpio;
608 /* we need these for headphone/lineout detection */
609 struct snd_kcontrol *headphone_ctrl;
610 struct snd_kcontrol *lineout_ctrl;
611 struct snd_kcontrol *speaker_ctrl;
612 struct snd_kcontrol *master_ctrl;
613 struct snd_kcontrol *headphone_detected_ctrl;
614 struct snd_kcontrol *lineout_detected_ctrl;
616 struct layout_dev_ptr selfptr_headphone;
617 struct layout_dev_ptr selfptr_lineout;
619 u32 have_lineout_detect:1,
620 have_headphone_detect:1,
621 switch_on_headphone:1,
622 switch_on_lineout:1;
625 static LIST_HEAD(layouts_list);
626 static int layouts_list_items;
627 /* this can go away but only if we allow multiple cards,
628 * make the fabric handle all the card stuff, etc... */
629 static struct layout_dev *layout_device;
631 #define control_info snd_ctl_boolean_mono_info
633 #define AMP_CONTROL(n, description) \
634 static int n##_control_get(struct snd_kcontrol *kcontrol, \
635 struct snd_ctl_elem_value *ucontrol) \
637 struct gpio_runtime *gpio = snd_kcontrol_chip(kcontrol); \
638 if (gpio->methods && gpio->methods->get_##n) \
639 ucontrol->value.integer.value[0] = \
640 gpio->methods->get_##n(gpio); \
641 return 0; \
643 static int n##_control_put(struct snd_kcontrol *kcontrol, \
644 struct snd_ctl_elem_value *ucontrol) \
646 struct gpio_runtime *gpio = snd_kcontrol_chip(kcontrol); \
647 if (gpio->methods && gpio->methods->set_##n) \
648 gpio->methods->set_##n(gpio, \
649 !!ucontrol->value.integer.value[0]); \
650 return 1; \
652 static struct snd_kcontrol_new n##_ctl = { \
653 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
654 .name = description, \
655 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
656 .info = control_info, \
657 .get = n##_control_get, \
658 .put = n##_control_put, \
661 AMP_CONTROL(headphone, "Headphone Switch");
662 AMP_CONTROL(speakers, "Speakers Switch");
663 AMP_CONTROL(lineout, "Line-Out Switch");
664 AMP_CONTROL(master, "Master Switch");
666 static int detect_choice_get(struct snd_kcontrol *kcontrol,
667 struct snd_ctl_elem_value *ucontrol)
669 struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
671 switch (kcontrol->private_value) {
672 case 0:
673 ucontrol->value.integer.value[0] = ldev->switch_on_headphone;
674 break;
675 case 1:
676 ucontrol->value.integer.value[0] = ldev->switch_on_lineout;
677 break;
678 default:
679 return -ENODEV;
681 return 0;
684 static int detect_choice_put(struct snd_kcontrol *kcontrol,
685 struct snd_ctl_elem_value *ucontrol)
687 struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
689 switch (kcontrol->private_value) {
690 case 0:
691 ldev->switch_on_headphone = !!ucontrol->value.integer.value[0];
692 break;
693 case 1:
694 ldev->switch_on_lineout = !!ucontrol->value.integer.value[0];
695 break;
696 default:
697 return -ENODEV;
699 return 1;
702 static struct snd_kcontrol_new headphone_detect_choice = {
703 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
704 .name = "Headphone Detect Autoswitch",
705 .info = control_info,
706 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
707 .get = detect_choice_get,
708 .put = detect_choice_put,
709 .private_value = 0,
712 static struct snd_kcontrol_new lineout_detect_choice = {
713 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
714 .name = "Line-Out Detect Autoswitch",
715 .info = control_info,
716 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
717 .get = detect_choice_get,
718 .put = detect_choice_put,
719 .private_value = 1,
722 static int detected_get(struct snd_kcontrol *kcontrol,
723 struct snd_ctl_elem_value *ucontrol)
725 struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
726 int v;
728 switch (kcontrol->private_value) {
729 case 0:
730 v = ldev->gpio.methods->get_detect(&ldev->gpio,
731 AOA_NOTIFY_HEADPHONE);
732 break;
733 case 1:
734 v = ldev->gpio.methods->get_detect(&ldev->gpio,
735 AOA_NOTIFY_LINE_OUT);
736 break;
737 default:
738 return -ENODEV;
740 ucontrol->value.integer.value[0] = v;
741 return 0;
744 static struct snd_kcontrol_new headphone_detected = {
745 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
746 .name = "Headphone Detected",
747 .info = control_info,
748 .access = SNDRV_CTL_ELEM_ACCESS_READ,
749 .get = detected_get,
750 .private_value = 0,
753 static struct snd_kcontrol_new lineout_detected = {
754 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
755 .name = "Line-Out Detected",
756 .info = control_info,
757 .access = SNDRV_CTL_ELEM_ACCESS_READ,
758 .get = detected_get,
759 .private_value = 1,
762 static int check_codec(struct aoa_codec *codec,
763 struct layout_dev *ldev,
764 struct codec_connect_info *cci)
766 const u32 *ref;
767 char propname[32];
768 struct codec_connection *cc;
770 /* if the codec has a 'codec' node, we require a reference */
771 if (codec->node && (strcmp(codec->node->name, "codec") == 0)) {
772 snprintf(propname, sizeof(propname),
773 "platform-%s-codec-ref", codec->name);
774 ref = of_get_property(ldev->sound, propname, NULL);
775 if (!ref) {
776 printk(KERN_INFO "snd-aoa-fabric-layout: "
777 "required property %s not present\n", propname);
778 return -ENODEV;
780 if (*ref != codec->node->phandle) {
781 printk(KERN_INFO "snd-aoa-fabric-layout: "
782 "%s doesn't match!\n", propname);
783 return -ENODEV;
785 } else {
786 if (layouts_list_items != 1) {
787 printk(KERN_INFO "snd-aoa-fabric-layout: "
788 "more than one soundbus, but no references.\n");
789 return -ENODEV;
792 codec->soundbus_dev = ldev->sdev;
793 codec->gpio = &ldev->gpio;
795 cc = cci->connections;
796 if (!cc)
797 return -EINVAL;
799 printk(KERN_INFO "snd-aoa-fabric-layout: can use this codec\n");
801 codec->connected = 0;
802 codec->fabric_data = cc;
804 while (cc->connected) {
805 codec->connected |= 1<<cc->codec_bit;
806 cc++;
809 return 0;
812 static int layout_found_codec(struct aoa_codec *codec)
814 struct layout_dev *ldev;
815 int i;
817 list_for_each_entry(ldev, &layouts_list, list) {
818 for (i=0; i<MAX_CODECS_PER_BUS; i++) {
819 if (!ldev->layout->codecs[i].name)
820 continue;
821 if (strcmp(ldev->layout->codecs[i].name, codec->name) == 0) {
822 if (check_codec(codec,
823 ldev,
824 &ldev->layout->codecs[i]) == 0)
825 return 0;
829 return -ENODEV;
832 static void layout_remove_codec(struct aoa_codec *codec)
834 int i;
835 /* here remove the codec from the layout dev's
836 * codec reference */
838 codec->soundbus_dev = NULL;
839 codec->gpio = NULL;
840 for (i=0; i<MAX_CODECS_PER_BUS; i++) {
844 static void layout_notify(void *data)
846 struct layout_dev_ptr *dptr = data;
847 struct layout_dev *ldev;
848 int v, update;
849 struct snd_kcontrol *detected, *c;
850 struct snd_card *card = aoa_get_card();
852 ldev = dptr->ptr;
853 if (data == &ldev->selfptr_headphone) {
854 v = ldev->gpio.methods->get_detect(&ldev->gpio, AOA_NOTIFY_HEADPHONE);
855 detected = ldev->headphone_detected_ctrl;
856 update = ldev->switch_on_headphone;
857 if (update) {
858 ldev->gpio.methods->set_speakers(&ldev->gpio, !v);
859 ldev->gpio.methods->set_headphone(&ldev->gpio, v);
860 ldev->gpio.methods->set_lineout(&ldev->gpio, 0);
862 } else if (data == &ldev->selfptr_lineout) {
863 v = ldev->gpio.methods->get_detect(&ldev->gpio, AOA_NOTIFY_LINE_OUT);
864 detected = ldev->lineout_detected_ctrl;
865 update = ldev->switch_on_lineout;
866 if (update) {
867 ldev->gpio.methods->set_speakers(&ldev->gpio, !v);
868 ldev->gpio.methods->set_headphone(&ldev->gpio, 0);
869 ldev->gpio.methods->set_lineout(&ldev->gpio, v);
871 } else
872 return;
874 if (detected)
875 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &detected->id);
876 if (update) {
877 c = ldev->headphone_ctrl;
878 if (c)
879 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
880 c = ldev->speaker_ctrl;
881 if (c)
882 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
883 c = ldev->lineout_ctrl;
884 if (c)
885 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
889 static void layout_attached_codec(struct aoa_codec *codec)
891 struct codec_connection *cc;
892 struct snd_kcontrol *ctl;
893 int headphones, lineout;
894 struct layout_dev *ldev = layout_device;
896 /* need to add this codec to our codec array! */
898 cc = codec->fabric_data;
900 headphones = codec->gpio->methods->get_detect(codec->gpio,
901 AOA_NOTIFY_HEADPHONE);
902 lineout = codec->gpio->methods->get_detect(codec->gpio,
903 AOA_NOTIFY_LINE_OUT);
905 if (codec->gpio->methods->set_master) {
906 ctl = snd_ctl_new1(&master_ctl, codec->gpio);
907 ldev->master_ctrl = ctl;
908 aoa_snd_ctl_add(ctl);
910 while (cc->connected) {
911 if (cc->connected & CC_SPEAKERS) {
912 if (headphones <= 0 && lineout <= 0)
913 ldev->gpio.methods->set_speakers(codec->gpio, 1);
914 ctl = snd_ctl_new1(&speakers_ctl, codec->gpio);
915 ldev->speaker_ctrl = ctl;
916 aoa_snd_ctl_add(ctl);
918 if (cc->connected & CC_HEADPHONE) {
919 if (headphones == 1)
920 ldev->gpio.methods->set_headphone(codec->gpio, 1);
921 ctl = snd_ctl_new1(&headphone_ctl, codec->gpio);
922 ldev->headphone_ctrl = ctl;
923 aoa_snd_ctl_add(ctl);
924 ldev->have_headphone_detect =
925 !ldev->gpio.methods
926 ->set_notify(&ldev->gpio,
927 AOA_NOTIFY_HEADPHONE,
928 layout_notify,
929 &ldev->selfptr_headphone);
930 if (ldev->have_headphone_detect) {
931 ctl = snd_ctl_new1(&headphone_detect_choice,
932 ldev);
933 aoa_snd_ctl_add(ctl);
934 ctl = snd_ctl_new1(&headphone_detected,
935 ldev);
936 ldev->headphone_detected_ctrl = ctl;
937 aoa_snd_ctl_add(ctl);
940 if (cc->connected & CC_LINEOUT) {
941 if (lineout == 1)
942 ldev->gpio.methods->set_lineout(codec->gpio, 1);
943 ctl = snd_ctl_new1(&lineout_ctl, codec->gpio);
944 if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
945 strlcpy(ctl->id.name,
946 "Headphone Switch", sizeof(ctl->id.name));
947 ldev->lineout_ctrl = ctl;
948 aoa_snd_ctl_add(ctl);
949 ldev->have_lineout_detect =
950 !ldev->gpio.methods
951 ->set_notify(&ldev->gpio,
952 AOA_NOTIFY_LINE_OUT,
953 layout_notify,
954 &ldev->selfptr_lineout);
955 if (ldev->have_lineout_detect) {
956 ctl = snd_ctl_new1(&lineout_detect_choice,
957 ldev);
958 if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
959 strlcpy(ctl->id.name,
960 "Headphone Detect Autoswitch",
961 sizeof(ctl->id.name));
962 aoa_snd_ctl_add(ctl);
963 ctl = snd_ctl_new1(&lineout_detected,
964 ldev);
965 if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
966 strlcpy(ctl->id.name,
967 "Headphone Detected",
968 sizeof(ctl->id.name));
969 ldev->lineout_detected_ctrl = ctl;
970 aoa_snd_ctl_add(ctl);
973 cc++;
975 /* now update initial state */
976 if (ldev->have_headphone_detect)
977 layout_notify(&ldev->selfptr_headphone);
978 if (ldev->have_lineout_detect)
979 layout_notify(&ldev->selfptr_lineout);
982 static struct aoa_fabric layout_fabric = {
983 .name = "SoundByLayout",
984 .owner = THIS_MODULE,
985 .found_codec = layout_found_codec,
986 .remove_codec = layout_remove_codec,
987 .attached_codec = layout_attached_codec,
990 static int aoa_fabric_layout_probe(struct soundbus_dev *sdev)
992 struct device_node *sound = NULL;
993 const unsigned int *id;
994 struct layout *layout = NULL;
995 struct layout_dev *ldev = NULL;
996 int err;
998 /* hm, currently we can only have one ... */
999 if (layout_device)
1000 return -ENODEV;
1002 /* by breaking out we keep a reference */
1003 while ((sound = of_get_next_child(sdev->ofdev.dev.of_node, sound))) {
1004 if (sound->type && strcasecmp(sound->type, "soundchip") == 0)
1005 break;
1007 if (!sound)
1008 return -ENODEV;
1010 id = of_get_property(sound, "layout-id", NULL);
1011 if (id) {
1012 layout = find_layout_by_id(*id);
1013 } else {
1014 id = of_get_property(sound, "device-id", NULL);
1015 if (id)
1016 layout = find_layout_by_device(*id);
1019 if (!layout) {
1020 printk(KERN_ERR "snd-aoa-fabric-layout: unknown layout\n");
1021 goto outnodev;
1024 ldev = kzalloc(sizeof(struct layout_dev), GFP_KERNEL);
1025 if (!ldev)
1026 goto outnodev;
1028 layout_device = ldev;
1029 ldev->sdev = sdev;
1030 ldev->sound = sound;
1031 ldev->layout = layout;
1032 ldev->gpio.node = sound->parent;
1033 switch (layout->layout_id) {
1034 case 0: /* anything with device_id, not layout_id */
1035 case 41: /* that unknown machine no one seems to have */
1036 case 51: /* PowerBook5,4 */
1037 case 58: /* Mac Mini */
1038 ldev->gpio.methods = ftr_gpio_methods;
1039 printk(KERN_DEBUG
1040 "snd-aoa-fabric-layout: Using direct GPIOs\n");
1041 break;
1042 default:
1043 ldev->gpio.methods = pmf_gpio_methods;
1044 printk(KERN_DEBUG
1045 "snd-aoa-fabric-layout: Using PMF GPIOs\n");
1047 ldev->selfptr_headphone.ptr = ldev;
1048 ldev->selfptr_lineout.ptr = ldev;
1049 dev_set_drvdata(&sdev->ofdev.dev, ldev);
1050 list_add(&ldev->list, &layouts_list);
1051 layouts_list_items++;
1053 /* assign these before registering ourselves, so
1054 * callbacks that are done during registration
1055 * already have the values */
1056 sdev->pcmid = ldev->layout->pcmid;
1057 if (ldev->layout->busname) {
1058 sdev->pcmname = ldev->layout->busname;
1059 } else {
1060 sdev->pcmname = "Master";
1063 ldev->gpio.methods->init(&ldev->gpio);
1065 err = aoa_fabric_register(&layout_fabric, &sdev->ofdev.dev);
1066 if (err && err != -EALREADY) {
1067 printk(KERN_INFO "snd-aoa-fabric-layout: can't use,"
1068 " another fabric is active!\n");
1069 goto outlistdel;
1072 use_layout(layout);
1073 ldev->switch_on_headphone = 1;
1074 ldev->switch_on_lineout = 1;
1075 return 0;
1076 outlistdel:
1077 /* we won't be using these then... */
1078 ldev->gpio.methods->exit(&ldev->gpio);
1079 /* reset if we didn't use it */
1080 sdev->pcmname = NULL;
1081 sdev->pcmid = -1;
1082 list_del(&ldev->list);
1083 layouts_list_items--;
1084 kfree(ldev);
1085 outnodev:
1086 of_node_put(sound);
1087 layout_device = NULL;
1088 return -ENODEV;
1091 static int aoa_fabric_layout_remove(struct soundbus_dev *sdev)
1093 struct layout_dev *ldev = dev_get_drvdata(&sdev->ofdev.dev);
1094 int i;
1096 for (i=0; i<MAX_CODECS_PER_BUS; i++) {
1097 if (ldev->codecs[i]) {
1098 aoa_fabric_unlink_codec(ldev->codecs[i]);
1100 ldev->codecs[i] = NULL;
1102 list_del(&ldev->list);
1103 layouts_list_items--;
1104 of_node_put(ldev->sound);
1106 ldev->gpio.methods->set_notify(&ldev->gpio,
1107 AOA_NOTIFY_HEADPHONE,
1108 NULL,
1109 NULL);
1110 ldev->gpio.methods->set_notify(&ldev->gpio,
1111 AOA_NOTIFY_LINE_OUT,
1112 NULL,
1113 NULL);
1115 ldev->gpio.methods->exit(&ldev->gpio);
1116 layout_device = NULL;
1117 kfree(ldev);
1118 sdev->pcmid = -1;
1119 sdev->pcmname = NULL;
1120 return 0;
1123 #ifdef CONFIG_PM
1124 static int aoa_fabric_layout_suspend(struct soundbus_dev *sdev, pm_message_t state)
1126 struct layout_dev *ldev = dev_get_drvdata(&sdev->ofdev.dev);
1128 if (ldev->gpio.methods && ldev->gpio.methods->all_amps_off)
1129 ldev->gpio.methods->all_amps_off(&ldev->gpio);
1131 return 0;
1134 static int aoa_fabric_layout_resume(struct soundbus_dev *sdev)
1136 struct layout_dev *ldev = dev_get_drvdata(&sdev->ofdev.dev);
1138 if (ldev->gpio.methods && ldev->gpio.methods->all_amps_restore)
1139 ldev->gpio.methods->all_amps_restore(&ldev->gpio);
1141 return 0;
1143 #endif
1145 static struct soundbus_driver aoa_soundbus_driver = {
1146 .name = "snd_aoa_soundbus_drv",
1147 .owner = THIS_MODULE,
1148 .probe = aoa_fabric_layout_probe,
1149 .remove = aoa_fabric_layout_remove,
1150 #ifdef CONFIG_PM
1151 .suspend = aoa_fabric_layout_suspend,
1152 .resume = aoa_fabric_layout_resume,
1153 #endif
1154 .driver = {
1155 .owner = THIS_MODULE,
1159 static int __init aoa_fabric_layout_init(void)
1161 int err;
1163 err = soundbus_register_driver(&aoa_soundbus_driver);
1164 if (err)
1165 return err;
1166 return 0;
1169 static void __exit aoa_fabric_layout_exit(void)
1171 soundbus_unregister_driver(&aoa_soundbus_driver);
1172 aoa_fabric_unregister(&layout_fabric);
1175 module_init(aoa_fabric_layout_init);
1176 module_exit(aoa_fabric_layout_exit);