layout.c 27 KB

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