layout.c 26 KB

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