snd-aoa-fabric-layout.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * Apple Onboard Audio driver -- layout fabric
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. *
  8. *
  9. * This fabric module looks for sound codecs
  10. * based on the layout-id property in the device tree.
  11. *
  12. */
  13. #include <asm/prom.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include "../aoa.h"
  17. #include "../soundbus/soundbus.h"
  18. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  19. MODULE_LICENSE("GPL");
  20. MODULE_DESCRIPTION("Layout-ID fabric for snd-aoa");
  21. #define MAX_CODECS_PER_BUS 2
  22. /* These are the connections the layout fabric
  23. * knows about. It doesn't really care about the
  24. * input ones, but I thought I'd separate them
  25. * to give them proper names. The thing is that
  26. * Apple usually will distinguish the active output
  27. * by GPIOs, while the active input is set directly
  28. * on the codec. Hence we here tell the codec what
  29. * we think is connected. This information is hard-
  30. * coded below ... */
  31. #define CC_SPEAKERS (1<<0)
  32. #define CC_HEADPHONE (1<<1)
  33. #define CC_LINEOUT (1<<2)
  34. #define CC_DIGITALOUT (1<<3)
  35. #define CC_LINEIN (1<<4)
  36. #define CC_MICROPHONE (1<<5)
  37. #define CC_DIGITALIN (1<<6)
  38. /* pretty bogus but users complain...
  39. * This is a flag saying that the LINEOUT
  40. * should be renamed to HEADPHONE.
  41. * be careful with input detection! */
  42. #define CC_LINEOUT_LABELLED_HEADPHONE (1<<7)
  43. struct codec_connection {
  44. /* CC_ flags from above */
  45. int connected;
  46. /* codec dependent bit to be set in the aoa_codec.connected field.
  47. * This intentionally doesn't have any generic flags because the
  48. * fabric has to know the codec anyway and all codecs might have
  49. * different connectors */
  50. int codec_bit;
  51. };
  52. struct codec_connect_info {
  53. char *name;
  54. struct codec_connection *connections;
  55. };
  56. #define LAYOUT_FLAG_COMBO_LINEOUT_SPDIF (1<<0)
  57. struct layout {
  58. unsigned int layout_id;
  59. struct codec_connect_info codecs[MAX_CODECS_PER_BUS];
  60. int flags;
  61. /* if busname is not assigned, we use 'Master' below,
  62. * so that our layout table doesn't need to be filled
  63. * too much.
  64. * We only assign these two if we expect to find more
  65. * than one soundbus, i.e. on those machines with
  66. * multiple layout-ids */
  67. char *busname;
  68. int pcmid;
  69. };
  70. MODULE_ALIAS("sound-layout-41");
  71. MODULE_ALIAS("sound-layout-45");
  72. MODULE_ALIAS("sound-layout-51");
  73. MODULE_ALIAS("sound-layout-58");
  74. MODULE_ALIAS("sound-layout-60");
  75. MODULE_ALIAS("sound-layout-61");
  76. MODULE_ALIAS("sound-layout-64");
  77. MODULE_ALIAS("sound-layout-65");
  78. MODULE_ALIAS("sound-layout-68");
  79. MODULE_ALIAS("sound-layout-69");
  80. MODULE_ALIAS("sound-layout-70");
  81. MODULE_ALIAS("sound-layout-72");
  82. MODULE_ALIAS("sound-layout-80");
  83. MODULE_ALIAS("sound-layout-82");
  84. MODULE_ALIAS("sound-layout-84");
  85. MODULE_ALIAS("sound-layout-86");
  86. MODULE_ALIAS("sound-layout-92");
  87. MODULE_ALIAS("sound-layout-96");
  88. /* onyx with all but microphone connected */
  89. static struct codec_connection onyx_connections_nomic[] = {
  90. {
  91. .connected = CC_SPEAKERS | CC_HEADPHONE | CC_LINEOUT,
  92. .codec_bit = 0,
  93. },
  94. {
  95. .connected = CC_DIGITALOUT,
  96. .codec_bit = 1,
  97. },
  98. {
  99. .connected = CC_LINEIN,
  100. .codec_bit = 2,
  101. },
  102. {} /* terminate array by .connected == 0 */
  103. };
  104. /* onyx on machines without headphone */
  105. static struct codec_connection onyx_connections_noheadphones[] = {
  106. {
  107. .connected = CC_SPEAKERS | CC_LINEOUT |
  108. CC_LINEOUT_LABELLED_HEADPHONE,
  109. .codec_bit = 0,
  110. },
  111. {
  112. .connected = CC_DIGITALOUT,
  113. .codec_bit = 1,
  114. },
  115. /* FIXME: are these correct? probably not for all the machines
  116. * below ... If not this will need separating. */
  117. {
  118. .connected = CC_LINEIN,
  119. .codec_bit = 2,
  120. },
  121. {
  122. .connected = CC_MICROPHONE,
  123. .codec_bit = 3,
  124. },
  125. {} /* terminate array by .connected == 0 */
  126. };
  127. /* onyx on machines with real line-out */
  128. static struct codec_connection onyx_connections_reallineout[] = {
  129. {
  130. .connected = CC_SPEAKERS | CC_LINEOUT | CC_HEADPHONE,
  131. .codec_bit = 0,
  132. },
  133. {
  134. .connected = CC_DIGITALOUT,
  135. .codec_bit = 1,
  136. },
  137. {
  138. .connected = CC_LINEIN,
  139. .codec_bit = 2,
  140. },
  141. {} /* terminate array by .connected == 0 */
  142. };
  143. /* tas on machines without line out */
  144. static struct codec_connection tas_connections_nolineout[] = {
  145. {
  146. .connected = CC_SPEAKERS | CC_HEADPHONE,
  147. .codec_bit = 0,
  148. },
  149. {
  150. .connected = CC_LINEIN,
  151. .codec_bit = 2,
  152. },
  153. {
  154. .connected = CC_MICROPHONE,
  155. .codec_bit = 3,
  156. },
  157. {} /* terminate array by .connected == 0 */
  158. };
  159. /* tas on machines with neither line out nor line in */
  160. static struct codec_connection tas_connections_noline[] = {
  161. {
  162. .connected = CC_SPEAKERS | CC_HEADPHONE,
  163. .codec_bit = 0,
  164. },
  165. {
  166. .connected = CC_MICROPHONE,
  167. .codec_bit = 3,
  168. },
  169. {} /* terminate array by .connected == 0 */
  170. };
  171. /* tas on machines without microphone */
  172. static struct codec_connection tas_connections_nomic[] = {
  173. {
  174. .connected = CC_SPEAKERS | CC_HEADPHONE | CC_LINEOUT,
  175. .codec_bit = 0,
  176. },
  177. {
  178. .connected = CC_LINEIN,
  179. .codec_bit = 2,
  180. },
  181. {} /* terminate array by .connected == 0 */
  182. };
  183. /* tas on machines with everything connected */
  184. static struct codec_connection tas_connections_all[] = {
  185. {
  186. .connected = CC_SPEAKERS | CC_HEADPHONE | CC_LINEOUT,
  187. .codec_bit = 0,
  188. },
  189. {
  190. .connected = CC_LINEIN,
  191. .codec_bit = 2,
  192. },
  193. {
  194. .connected = CC_MICROPHONE,
  195. .codec_bit = 3,
  196. },
  197. {} /* terminate array by .connected == 0 */
  198. };
  199. static struct codec_connection toonie_connections[] = {
  200. {
  201. .connected = CC_SPEAKERS | CC_HEADPHONE,
  202. .codec_bit = 0,
  203. },
  204. {} /* terminate array by .connected == 0 */
  205. };
  206. static struct codec_connection topaz_input[] = {
  207. {
  208. .connected = CC_DIGITALIN,
  209. .codec_bit = 0,
  210. },
  211. {} /* terminate array by .connected == 0 */
  212. };
  213. static struct codec_connection topaz_output[] = {
  214. {
  215. .connected = CC_DIGITALOUT,
  216. .codec_bit = 1,
  217. },
  218. {} /* terminate array by .connected == 0 */
  219. };
  220. static struct codec_connection topaz_inout[] = {
  221. {
  222. .connected = CC_DIGITALIN,
  223. .codec_bit = 0,
  224. },
  225. {
  226. .connected = CC_DIGITALOUT,
  227. .codec_bit = 1,
  228. },
  229. {} /* terminate array by .connected == 0 */
  230. };
  231. static struct layout layouts[] = {
  232. /* last PowerBooks (15" Oct 2005) */
  233. { .layout_id = 82,
  234. .flags = LAYOUT_FLAG_COMBO_LINEOUT_SPDIF,
  235. .codecs[0] = {
  236. .name = "onyx",
  237. .connections = onyx_connections_noheadphones,
  238. },
  239. .codecs[1] = {
  240. .name = "topaz",
  241. .connections = topaz_input,
  242. },
  243. },
  244. /* PowerMac9,1 */
  245. { .layout_id = 60,
  246. .codecs[0] = {
  247. .name = "onyx",
  248. .connections = onyx_connections_reallineout,
  249. },
  250. },
  251. /* PowerMac9,1 */
  252. { .layout_id = 61,
  253. .codecs[0] = {
  254. .name = "topaz",
  255. .connections = topaz_input,
  256. },
  257. },
  258. /* PowerBook5,7 */
  259. { .layout_id = 64,
  260. .flags = LAYOUT_FLAG_COMBO_LINEOUT_SPDIF,
  261. .codecs[0] = {
  262. .name = "onyx",
  263. .connections = onyx_connections_noheadphones,
  264. },
  265. },
  266. /* PowerBook5,7 */
  267. { .layout_id = 65,
  268. .codecs[0] = {
  269. .name = "topaz",
  270. .connections = topaz_input,
  271. },
  272. },
  273. /* PowerBook5,9 [17" Oct 2005] */
  274. { .layout_id = 84,
  275. .flags = LAYOUT_FLAG_COMBO_LINEOUT_SPDIF,
  276. .codecs[0] = {
  277. .name = "onyx",
  278. .connections = onyx_connections_noheadphones,
  279. },
  280. .codecs[1] = {
  281. .name = "topaz",
  282. .connections = topaz_input,
  283. },
  284. },
  285. /* PowerMac8,1 */
  286. { .layout_id = 45,
  287. .codecs[0] = {
  288. .name = "onyx",
  289. .connections = onyx_connections_noheadphones,
  290. },
  291. .codecs[1] = {
  292. .name = "topaz",
  293. .connections = topaz_input,
  294. },
  295. },
  296. /* Quad PowerMac (analog in, analog/digital out) */
  297. { .layout_id = 68,
  298. .codecs[0] = {
  299. .name = "onyx",
  300. .connections = onyx_connections_nomic,
  301. },
  302. },
  303. /* Quad PowerMac (digital in) */
  304. { .layout_id = 69,
  305. .codecs[0] = {
  306. .name = "topaz",
  307. .connections = topaz_input,
  308. },
  309. .busname = "digital in", .pcmid = 1 },
  310. /* Early 2005 PowerBook (PowerBook 5,6) */
  311. { .layout_id = 70,
  312. .codecs[0] = {
  313. .name = "tas",
  314. .connections = tas_connections_nolineout,
  315. },
  316. },
  317. /* PowerBook 5,4 */
  318. { .layout_id = 51,
  319. .codecs[0] = {
  320. .name = "tas",
  321. .connections = tas_connections_nolineout,
  322. },
  323. },
  324. /* PowerBook6,7 */
  325. { .layout_id = 80,
  326. .codecs[0] = {
  327. .name = "tas",
  328. .connections = tas_connections_noline,
  329. },
  330. },
  331. /* PowerBook6,8 */
  332. { .layout_id = 72,
  333. .codecs[0] = {
  334. .name = "tas",
  335. .connections = tas_connections_nolineout,
  336. },
  337. },
  338. /* PowerMac8,2 */
  339. { .layout_id = 86,
  340. .codecs[0] = {
  341. .name = "onyx",
  342. .connections = onyx_connections_nomic,
  343. },
  344. .codecs[1] = {
  345. .name = "topaz",
  346. .connections = topaz_input,
  347. },
  348. },
  349. /* PowerBook6,7 */
  350. { .layout_id = 92,
  351. .codecs[0] = {
  352. .name = "tas",
  353. .connections = tas_connections_nolineout,
  354. },
  355. },
  356. /* PowerMac10,1 (Mac Mini) */
  357. { .layout_id = 58,
  358. .codecs[0] = {
  359. .name = "toonie",
  360. .connections = toonie_connections,
  361. },
  362. },
  363. {
  364. .layout_id = 96,
  365. .codecs[0] = {
  366. .name = "onyx",
  367. .connections = onyx_connections_noheadphones,
  368. },
  369. },
  370. /* unknown, untested, but this comes from Apple */
  371. { .layout_id = 41,
  372. .codecs[0] = {
  373. .name = "tas",
  374. .connections = tas_connections_all,
  375. },
  376. },
  377. { .layout_id = 36,
  378. .codecs[0] = {
  379. .name = "tas",
  380. .connections = tas_connections_nomic,
  381. },
  382. .codecs[1] = {
  383. .name = "topaz",
  384. .connections = topaz_inout,
  385. },
  386. },
  387. { .layout_id = 47,
  388. .codecs[0] = {
  389. .name = "onyx",
  390. .connections = onyx_connections_noheadphones,
  391. },
  392. },
  393. { .layout_id = 48,
  394. .codecs[0] = {
  395. .name = "topaz",
  396. .connections = topaz_input,
  397. },
  398. },
  399. { .layout_id = 49,
  400. .codecs[0] = {
  401. .name = "onyx",
  402. .connections = onyx_connections_nomic,
  403. },
  404. },
  405. { .layout_id = 50,
  406. .codecs[0] = {
  407. .name = "topaz",
  408. .connections = topaz_input,
  409. },
  410. },
  411. { .layout_id = 56,
  412. .codecs[0] = {
  413. .name = "onyx",
  414. .connections = onyx_connections_noheadphones,
  415. },
  416. },
  417. { .layout_id = 57,
  418. .codecs[0] = {
  419. .name = "topaz",
  420. .connections = topaz_input,
  421. },
  422. },
  423. { .layout_id = 62,
  424. .codecs[0] = {
  425. .name = "onyx",
  426. .connections = onyx_connections_noheadphones,
  427. },
  428. .codecs[1] = {
  429. .name = "topaz",
  430. .connections = topaz_output,
  431. },
  432. },
  433. { .layout_id = 66,
  434. .codecs[0] = {
  435. .name = "onyx",
  436. .connections = onyx_connections_noheadphones,
  437. },
  438. },
  439. { .layout_id = 67,
  440. .codecs[0] = {
  441. .name = "topaz",
  442. .connections = topaz_input,
  443. },
  444. },
  445. { .layout_id = 76,
  446. .codecs[0] = {
  447. .name = "tas",
  448. .connections = tas_connections_nomic,
  449. },
  450. .codecs[1] = {
  451. .name = "topaz",
  452. .connections = topaz_inout,
  453. },
  454. },
  455. { .layout_id = 90,
  456. .codecs[0] = {
  457. .name = "tas",
  458. .connections = tas_connections_noline,
  459. },
  460. },
  461. { .layout_id = 94,
  462. .codecs[0] = {
  463. .name = "onyx",
  464. /* but it has an external mic?? how to select? */
  465. .connections = onyx_connections_noheadphones,
  466. },
  467. },
  468. { .layout_id = 98,
  469. .codecs[0] = {
  470. .name = "toonie",
  471. .connections = toonie_connections,
  472. },
  473. },
  474. { .layout_id = 100,
  475. .codecs[0] = {
  476. .name = "topaz",
  477. .connections = topaz_input,
  478. },
  479. .codecs[1] = {
  480. .name = "onyx",
  481. .connections = onyx_connections_noheadphones,
  482. },
  483. },
  484. {}
  485. };
  486. static struct layout *find_layout_by_id(unsigned int id)
  487. {
  488. struct layout *l;
  489. l = layouts;
  490. while (l->layout_id) {
  491. if (l->layout_id == id)
  492. return l;
  493. l++;
  494. }
  495. return NULL;
  496. }
  497. static void use_layout(struct layout *l)
  498. {
  499. int i;
  500. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  501. if (l->codecs[i].name) {
  502. request_module("snd-aoa-codec-%s", l->codecs[i].name);
  503. }
  504. }
  505. /* now we wait for the codecs to call us back */
  506. }
  507. struct layout_dev;
  508. struct layout_dev_ptr {
  509. struct layout_dev *ptr;
  510. };
  511. struct layout_dev {
  512. struct list_head list;
  513. struct soundbus_dev *sdev;
  514. struct device_node *sound;
  515. struct aoa_codec *codecs[MAX_CODECS_PER_BUS];
  516. struct layout *layout;
  517. struct gpio_runtime gpio;
  518. /* we need these for headphone/lineout detection */
  519. struct snd_kcontrol *headphone_ctrl;
  520. struct snd_kcontrol *lineout_ctrl;
  521. struct snd_kcontrol *speaker_ctrl;
  522. struct snd_kcontrol *headphone_detected_ctrl;
  523. struct snd_kcontrol *lineout_detected_ctrl;
  524. struct layout_dev_ptr selfptr_headphone;
  525. struct layout_dev_ptr selfptr_lineout;
  526. u32 have_lineout_detect:1,
  527. have_headphone_detect:1,
  528. switch_on_headphone:1,
  529. switch_on_lineout:1;
  530. };
  531. static LIST_HEAD(layouts_list);
  532. static int layouts_list_items;
  533. /* this can go away but only if we allow multiple cards,
  534. * make the fabric handle all the card stuff, etc... */
  535. static struct layout_dev *layout_device;
  536. static int control_info(struct snd_kcontrol *kcontrol,
  537. struct snd_ctl_elem_info *uinfo)
  538. {
  539. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  540. uinfo->count = 1;
  541. uinfo->value.integer.min = 0;
  542. uinfo->value.integer.max = 1;
  543. return 0;
  544. }
  545. #define AMP_CONTROL(n, description) \
  546. static int n##_control_get(struct snd_kcontrol *kcontrol, \
  547. struct snd_ctl_elem_value *ucontrol) \
  548. { \
  549. struct gpio_runtime *gpio = snd_kcontrol_chip(kcontrol); \
  550. if (gpio->methods && gpio->methods->get_##n) \
  551. ucontrol->value.integer.value[0] = \
  552. gpio->methods->get_##n(gpio); \
  553. return 0; \
  554. } \
  555. static int n##_control_put(struct snd_kcontrol *kcontrol, \
  556. struct snd_ctl_elem_value *ucontrol) \
  557. { \
  558. struct gpio_runtime *gpio = snd_kcontrol_chip(kcontrol); \
  559. if (gpio->methods && gpio->methods->get_##n) \
  560. gpio->methods->set_##n(gpio, \
  561. ucontrol->value.integer.value[0]); \
  562. return 1; \
  563. } \
  564. static struct snd_kcontrol_new n##_ctl = { \
  565. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  566. .name = description, \
  567. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  568. .info = control_info, \
  569. .get = n##_control_get, \
  570. .put = n##_control_put, \
  571. }
  572. AMP_CONTROL(headphone, "Headphone Switch");
  573. AMP_CONTROL(speakers, "Speakers Switch");
  574. AMP_CONTROL(lineout, "Line-Out Switch");
  575. static int detect_choice_get(struct snd_kcontrol *kcontrol,
  576. struct snd_ctl_elem_value *ucontrol)
  577. {
  578. struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
  579. switch (kcontrol->private_value) {
  580. case 0:
  581. ucontrol->value.integer.value[0] = ldev->switch_on_headphone;
  582. break;
  583. case 1:
  584. ucontrol->value.integer.value[0] = ldev->switch_on_lineout;
  585. break;
  586. default:
  587. return -ENODEV;
  588. }
  589. return 0;
  590. }
  591. static int detect_choice_put(struct snd_kcontrol *kcontrol,
  592. struct snd_ctl_elem_value *ucontrol)
  593. {
  594. struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
  595. switch (kcontrol->private_value) {
  596. case 0:
  597. ldev->switch_on_headphone = !!ucontrol->value.integer.value[0];
  598. break;
  599. case 1:
  600. ldev->switch_on_lineout = !!ucontrol->value.integer.value[0];
  601. break;
  602. default:
  603. return -ENODEV;
  604. }
  605. return 1;
  606. }
  607. static struct snd_kcontrol_new headphone_detect_choice = {
  608. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  609. .name = "Headphone Detect Autoswitch",
  610. .info = control_info,
  611. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  612. .get = detect_choice_get,
  613. .put = detect_choice_put,
  614. .private_value = 0,
  615. };
  616. static struct snd_kcontrol_new lineout_detect_choice = {
  617. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  618. .name = "Line-Out Detect Autoswitch",
  619. .info = control_info,
  620. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  621. .get = detect_choice_get,
  622. .put = detect_choice_put,
  623. .private_value = 1,
  624. };
  625. static int detected_get(struct snd_kcontrol *kcontrol,
  626. struct snd_ctl_elem_value *ucontrol)
  627. {
  628. struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
  629. int v;
  630. switch (kcontrol->private_value) {
  631. case 0:
  632. v = ldev->gpio.methods->get_detect(&ldev->gpio,
  633. AOA_NOTIFY_HEADPHONE);
  634. break;
  635. case 1:
  636. v = ldev->gpio.methods->get_detect(&ldev->gpio,
  637. AOA_NOTIFY_LINE_OUT);
  638. break;
  639. default:
  640. return -ENODEV;
  641. }
  642. ucontrol->value.integer.value[0] = v;
  643. return 0;
  644. }
  645. static struct snd_kcontrol_new headphone_detected = {
  646. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  647. .name = "Headphone Detected",
  648. .info = control_info,
  649. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  650. .get = detected_get,
  651. .private_value = 0,
  652. };
  653. static struct snd_kcontrol_new lineout_detected = {
  654. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  655. .name = "Line-Out Detected",
  656. .info = control_info,
  657. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  658. .get = detected_get,
  659. .private_value = 1,
  660. };
  661. static int check_codec(struct aoa_codec *codec,
  662. struct layout_dev *ldev,
  663. struct codec_connect_info *cci)
  664. {
  665. u32 *ref;
  666. char propname[32];
  667. struct codec_connection *cc;
  668. /* if the codec has a 'codec' node, we require a reference */
  669. if (codec->node && (strcmp(codec->node->name, "codec") == 0)) {
  670. snprintf(propname, sizeof(propname),
  671. "platform-%s-codec-ref", codec->name);
  672. ref = (u32*)get_property(ldev->sound, propname, NULL);
  673. if (!ref) {
  674. printk(KERN_INFO "snd-aoa-fabric-layout: "
  675. "required property %s not present\n", propname);
  676. return -ENODEV;
  677. }
  678. if (*ref != codec->node->linux_phandle) {
  679. printk(KERN_INFO "snd-aoa-fabric-layout: "
  680. "%s doesn't match!\n", propname);
  681. return -ENODEV;
  682. }
  683. } else {
  684. if (layouts_list_items != 1) {
  685. printk(KERN_INFO "snd-aoa-fabric-layout: "
  686. "more than one soundbus, but no references.\n");
  687. return -ENODEV;
  688. }
  689. }
  690. codec->soundbus_dev = ldev->sdev;
  691. codec->gpio = &ldev->gpio;
  692. cc = cci->connections;
  693. if (!cc)
  694. return -EINVAL;
  695. printk(KERN_INFO "snd-aoa-fabric-layout: can use this codec\n");
  696. codec->connected = 0;
  697. codec->fabric_data = cc;
  698. while (cc->connected) {
  699. codec->connected |= 1<<cc->codec_bit;
  700. cc++;
  701. }
  702. return 0;
  703. }
  704. static int layout_found_codec(struct aoa_codec *codec)
  705. {
  706. struct layout_dev *ldev;
  707. int i;
  708. list_for_each_entry(ldev, &layouts_list, list) {
  709. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  710. if (!ldev->layout->codecs[i].name)
  711. continue;
  712. if (strcmp(ldev->layout->codecs[i].name, codec->name) == 0) {
  713. if (check_codec(codec,
  714. ldev,
  715. &ldev->layout->codecs[i]) == 0)
  716. return 0;
  717. }
  718. }
  719. }
  720. return -ENODEV;
  721. }
  722. static void layout_remove_codec(struct aoa_codec *codec)
  723. {
  724. int i;
  725. /* here remove the codec from the layout dev's
  726. * codec reference */
  727. codec->soundbus_dev = NULL;
  728. codec->gpio = NULL;
  729. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  730. }
  731. }
  732. static void layout_notify(void *data)
  733. {
  734. struct layout_dev_ptr *dptr = data;
  735. struct layout_dev *ldev;
  736. int v, update;
  737. struct snd_kcontrol *detected, *c;
  738. struct snd_card *card = aoa_get_card();
  739. ldev = dptr->ptr;
  740. if (data == &ldev->selfptr_headphone) {
  741. v = ldev->gpio.methods->get_detect(&ldev->gpio, AOA_NOTIFY_HEADPHONE);
  742. detected = ldev->headphone_detected_ctrl;
  743. update = ldev->switch_on_headphone;
  744. if (update) {
  745. ldev->gpio.methods->set_speakers(&ldev->gpio, !v);
  746. ldev->gpio.methods->set_headphone(&ldev->gpio, v);
  747. ldev->gpio.methods->set_lineout(&ldev->gpio, 0);
  748. }
  749. } else if (data == &ldev->selfptr_lineout) {
  750. v = ldev->gpio.methods->get_detect(&ldev->gpio, AOA_NOTIFY_LINE_OUT);
  751. detected = ldev->lineout_detected_ctrl;
  752. update = ldev->switch_on_lineout;
  753. if (update) {
  754. ldev->gpio.methods->set_speakers(&ldev->gpio, !v);
  755. ldev->gpio.methods->set_headphone(&ldev->gpio, 0);
  756. ldev->gpio.methods->set_lineout(&ldev->gpio, v);
  757. }
  758. } else
  759. return;
  760. if (detected)
  761. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &detected->id);
  762. if (update) {
  763. c = ldev->headphone_ctrl;
  764. if (c)
  765. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
  766. c = ldev->speaker_ctrl;
  767. if (c)
  768. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
  769. c = ldev->lineout_ctrl;
  770. if (c)
  771. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
  772. }
  773. }
  774. static void layout_attached_codec(struct aoa_codec *codec)
  775. {
  776. struct codec_connection *cc;
  777. struct snd_kcontrol *ctl;
  778. int headphones, lineout;
  779. struct layout_dev *ldev = layout_device;
  780. /* need to add this codec to our codec array! */
  781. cc = codec->fabric_data;
  782. headphones = codec->gpio->methods->get_detect(codec->gpio,
  783. AOA_NOTIFY_HEADPHONE);
  784. lineout = codec->gpio->methods->get_detect(codec->gpio,
  785. AOA_NOTIFY_LINE_OUT);
  786. while (cc->connected) {
  787. if (cc->connected & CC_SPEAKERS) {
  788. if (headphones <= 0 && lineout <= 0)
  789. ldev->gpio.methods->set_speakers(codec->gpio, 1);
  790. ctl = snd_ctl_new1(&speakers_ctl, codec->gpio);
  791. ldev->speaker_ctrl = ctl;
  792. aoa_snd_ctl_add(ctl);
  793. }
  794. if (cc->connected & CC_HEADPHONE) {
  795. if (headphones == 1)
  796. ldev->gpio.methods->set_headphone(codec->gpio, 1);
  797. ctl = snd_ctl_new1(&headphone_ctl, codec->gpio);
  798. ldev->headphone_ctrl = ctl;
  799. aoa_snd_ctl_add(ctl);
  800. ldev->have_headphone_detect =
  801. !ldev->gpio.methods
  802. ->set_notify(&ldev->gpio,
  803. AOA_NOTIFY_HEADPHONE,
  804. layout_notify,
  805. &ldev->selfptr_headphone);
  806. if (ldev->have_headphone_detect) {
  807. ctl = snd_ctl_new1(&headphone_detect_choice,
  808. ldev);
  809. aoa_snd_ctl_add(ctl);
  810. ctl = snd_ctl_new1(&headphone_detected,
  811. ldev);
  812. ldev->headphone_detected_ctrl = ctl;
  813. aoa_snd_ctl_add(ctl);
  814. }
  815. }
  816. if (cc->connected & CC_LINEOUT) {
  817. if (lineout == 1)
  818. ldev->gpio.methods->set_lineout(codec->gpio, 1);
  819. ctl = snd_ctl_new1(&lineout_ctl, codec->gpio);
  820. if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
  821. strlcpy(ctl->id.name,
  822. "Headphone Switch", sizeof(ctl->id.name));
  823. ldev->lineout_ctrl = ctl;
  824. aoa_snd_ctl_add(ctl);
  825. ldev->have_lineout_detect =
  826. !ldev->gpio.methods
  827. ->set_notify(&ldev->gpio,
  828. AOA_NOTIFY_LINE_OUT,
  829. layout_notify,
  830. &ldev->selfptr_lineout);
  831. if (ldev->have_lineout_detect) {
  832. ctl = snd_ctl_new1(&lineout_detect_choice,
  833. ldev);
  834. if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
  835. strlcpy(ctl->id.name,
  836. "Headphone Detect Autoswitch",
  837. sizeof(ctl->id.name));
  838. aoa_snd_ctl_add(ctl);
  839. ctl = snd_ctl_new1(&lineout_detected,
  840. ldev);
  841. if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
  842. strlcpy(ctl->id.name,
  843. "Headphone Detected",
  844. sizeof(ctl->id.name));
  845. ldev->lineout_detected_ctrl = ctl;
  846. aoa_snd_ctl_add(ctl);
  847. }
  848. }
  849. cc++;
  850. }
  851. /* now update initial state */
  852. if (ldev->have_headphone_detect)
  853. layout_notify(&ldev->selfptr_headphone);
  854. if (ldev->have_lineout_detect)
  855. layout_notify(&ldev->selfptr_lineout);
  856. }
  857. static struct aoa_fabric layout_fabric = {
  858. .name = "SoundByLayout",
  859. .owner = THIS_MODULE,
  860. .found_codec = layout_found_codec,
  861. .remove_codec = layout_remove_codec,
  862. .attached_codec = layout_attached_codec,
  863. };
  864. static int aoa_fabric_layout_probe(struct soundbus_dev *sdev)
  865. {
  866. struct device_node *sound = NULL;
  867. unsigned int *layout_id;
  868. struct layout *layout;
  869. struct layout_dev *ldev = NULL;
  870. int err;
  871. /* hm, currently we can only have one ... */
  872. if (layout_device)
  873. return -ENODEV;
  874. /* by breaking out we keep a reference */
  875. while ((sound = of_get_next_child(sdev->ofdev.node, sound))) {
  876. if (sound->type && strcasecmp(sound->type, "soundchip") == 0)
  877. break;
  878. }
  879. if (!sound) return -ENODEV;
  880. layout_id = (unsigned int *) get_property(sound, "layout-id", NULL);
  881. if (!layout_id)
  882. goto outnodev;
  883. printk(KERN_INFO "snd-aoa-fabric-layout: found bus with layout %d\n",
  884. *layout_id);
  885. layout = find_layout_by_id(*layout_id);
  886. if (!layout) {
  887. printk(KERN_ERR "snd-aoa-fabric-layout: unknown layout\n");
  888. goto outnodev;
  889. }
  890. ldev = kzalloc(sizeof(struct layout_dev), GFP_KERNEL);
  891. if (!ldev)
  892. goto outnodev;
  893. layout_device = ldev;
  894. ldev->sdev = sdev;
  895. ldev->sound = sound;
  896. ldev->layout = layout;
  897. ldev->gpio.node = sound->parent;
  898. switch (layout->layout_id) {
  899. case 41: /* that unknown machine no one seems to have */
  900. case 51: /* PowerBook5,4 */
  901. case 58: /* Mac Mini */
  902. ldev->gpio.methods = ftr_gpio_methods;
  903. printk(KERN_DEBUG
  904. "snd-aoa-fabric-layout: Using direct GPIOs\n");
  905. break;
  906. default:
  907. ldev->gpio.methods = pmf_gpio_methods;
  908. printk(KERN_DEBUG
  909. "snd-aoa-fabric-layout: Using PMF GPIOs\n");
  910. }
  911. ldev->selfptr_headphone.ptr = ldev;
  912. ldev->selfptr_lineout.ptr = ldev;
  913. sdev->ofdev.dev.driver_data = ldev;
  914. list_add(&ldev->list, &layouts_list);
  915. layouts_list_items++;
  916. /* assign these before registering ourselves, so
  917. * callbacks that are done during registration
  918. * already have the values */
  919. sdev->pcmid = ldev->layout->pcmid;
  920. if (ldev->layout->busname) {
  921. sdev->pcmname = ldev->layout->busname;
  922. } else {
  923. sdev->pcmname = "Master";
  924. }
  925. ldev->gpio.methods->init(&ldev->gpio);
  926. err = aoa_fabric_register(&layout_fabric);
  927. if (err && err != -EALREADY) {
  928. printk(KERN_INFO "snd-aoa-fabric-layout: can't use,"
  929. " another fabric is active!\n");
  930. goto outlistdel;
  931. }
  932. use_layout(layout);
  933. ldev->switch_on_headphone = 1;
  934. ldev->switch_on_lineout = 1;
  935. return 0;
  936. outlistdel:
  937. /* we won't be using these then... */
  938. ldev->gpio.methods->exit(&ldev->gpio);
  939. /* reset if we didn't use it */
  940. sdev->pcmname = NULL;
  941. sdev->pcmid = -1;
  942. list_del(&ldev->list);
  943. layouts_list_items--;
  944. outnodev:
  945. if (sound) of_node_put(sound);
  946. layout_device = NULL;
  947. if (ldev) kfree(ldev);
  948. return -ENODEV;
  949. }
  950. static int aoa_fabric_layout_remove(struct soundbus_dev *sdev)
  951. {
  952. struct layout_dev *ldev = sdev->ofdev.dev.driver_data;
  953. int i;
  954. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  955. if (ldev->codecs[i]) {
  956. aoa_fabric_unlink_codec(ldev->codecs[i]);
  957. }
  958. ldev->codecs[i] = NULL;
  959. }
  960. list_del(&ldev->list);
  961. layouts_list_items--;
  962. of_node_put(ldev->sound);
  963. ldev->gpio.methods->set_notify(&ldev->gpio,
  964. AOA_NOTIFY_HEADPHONE,
  965. NULL,
  966. NULL);
  967. ldev->gpio.methods->set_notify(&ldev->gpio,
  968. AOA_NOTIFY_LINE_OUT,
  969. NULL,
  970. NULL);
  971. ldev->gpio.methods->exit(&ldev->gpio);
  972. layout_device = NULL;
  973. kfree(ldev);
  974. sdev->pcmid = -1;
  975. sdev->pcmname = NULL;
  976. return 0;
  977. }
  978. #ifdef CONFIG_PM
  979. static int aoa_fabric_layout_suspend(struct soundbus_dev *sdev, pm_message_t state)
  980. {
  981. struct layout_dev *ldev = sdev->ofdev.dev.driver_data;
  982. printk("aoa_fabric_layout_suspend()\n");
  983. if (ldev->gpio.methods && ldev->gpio.methods->all_amps_off)
  984. ldev->gpio.methods->all_amps_off(&ldev->gpio);
  985. return 0;
  986. }
  987. static int aoa_fabric_layout_resume(struct soundbus_dev *sdev)
  988. {
  989. struct layout_dev *ldev = sdev->ofdev.dev.driver_data;
  990. printk("aoa_fabric_layout_resume()\n");
  991. if (ldev->gpio.methods && ldev->gpio.methods->all_amps_off)
  992. ldev->gpio.methods->all_amps_restore(&ldev->gpio);
  993. return 0;
  994. }
  995. #endif
  996. static struct soundbus_driver aoa_soundbus_driver = {
  997. .name = "snd_aoa_soundbus_drv",
  998. .owner = THIS_MODULE,
  999. .probe = aoa_fabric_layout_probe,
  1000. .remove = aoa_fabric_layout_remove,
  1001. #ifdef CONFIG_PM
  1002. .suspend = aoa_fabric_layout_suspend,
  1003. .resume = aoa_fabric_layout_resume,
  1004. #endif
  1005. };
  1006. static int __init aoa_fabric_layout_init(void)
  1007. {
  1008. int err;
  1009. err = soundbus_register_driver(&aoa_soundbus_driver);
  1010. if (err)
  1011. return err;
  1012. return 0;
  1013. }
  1014. static void __exit aoa_fabric_layout_exit(void)
  1015. {
  1016. soundbus_unregister_driver(&aoa_soundbus_driver);
  1017. aoa_fabric_unregister(&layout_fabric);
  1018. }
  1019. module_init(aoa_fabric_layout_init);
  1020. module_exit(aoa_fabric_layout_exit);