snd-aoa-fabric-layout.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. static int control_info(struct snd_kcontrol *kcontrol,
  552. struct snd_ctl_elem_info *uinfo)
  553. {
  554. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  555. uinfo->count = 1;
  556. uinfo->value.integer.min = 0;
  557. uinfo->value.integer.max = 1;
  558. return 0;
  559. }
  560. #define AMP_CONTROL(n, description) \
  561. static int n##_control_get(struct snd_kcontrol *kcontrol, \
  562. struct snd_ctl_elem_value *ucontrol) \
  563. { \
  564. struct gpio_runtime *gpio = snd_kcontrol_chip(kcontrol); \
  565. if (gpio->methods && gpio->methods->get_##n) \
  566. ucontrol->value.integer.value[0] = \
  567. gpio->methods->get_##n(gpio); \
  568. return 0; \
  569. } \
  570. static int n##_control_put(struct snd_kcontrol *kcontrol, \
  571. struct snd_ctl_elem_value *ucontrol) \
  572. { \
  573. struct gpio_runtime *gpio = snd_kcontrol_chip(kcontrol); \
  574. if (gpio->methods && gpio->methods->get_##n) \
  575. gpio->methods->set_##n(gpio, \
  576. ucontrol->value.integer.value[0]); \
  577. return 1; \
  578. } \
  579. static struct snd_kcontrol_new n##_ctl = { \
  580. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  581. .name = description, \
  582. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
  583. .info = control_info, \
  584. .get = n##_control_get, \
  585. .put = n##_control_put, \
  586. }
  587. AMP_CONTROL(headphone, "Headphone Switch");
  588. AMP_CONTROL(speakers, "Speakers Switch");
  589. AMP_CONTROL(lineout, "Line-Out Switch");
  590. static int detect_choice_get(struct snd_kcontrol *kcontrol,
  591. struct snd_ctl_elem_value *ucontrol)
  592. {
  593. struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
  594. switch (kcontrol->private_value) {
  595. case 0:
  596. ucontrol->value.integer.value[0] = ldev->switch_on_headphone;
  597. break;
  598. case 1:
  599. ucontrol->value.integer.value[0] = ldev->switch_on_lineout;
  600. break;
  601. default:
  602. return -ENODEV;
  603. }
  604. return 0;
  605. }
  606. static int detect_choice_put(struct snd_kcontrol *kcontrol,
  607. struct snd_ctl_elem_value *ucontrol)
  608. {
  609. struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
  610. switch (kcontrol->private_value) {
  611. case 0:
  612. ldev->switch_on_headphone = !!ucontrol->value.integer.value[0];
  613. break;
  614. case 1:
  615. ldev->switch_on_lineout = !!ucontrol->value.integer.value[0];
  616. break;
  617. default:
  618. return -ENODEV;
  619. }
  620. return 1;
  621. }
  622. static struct snd_kcontrol_new headphone_detect_choice = {
  623. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  624. .name = "Headphone Detect Autoswitch",
  625. .info = control_info,
  626. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  627. .get = detect_choice_get,
  628. .put = detect_choice_put,
  629. .private_value = 0,
  630. };
  631. static struct snd_kcontrol_new lineout_detect_choice = {
  632. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  633. .name = "Line-Out Detect Autoswitch",
  634. .info = control_info,
  635. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  636. .get = detect_choice_get,
  637. .put = detect_choice_put,
  638. .private_value = 1,
  639. };
  640. static int detected_get(struct snd_kcontrol *kcontrol,
  641. struct snd_ctl_elem_value *ucontrol)
  642. {
  643. struct layout_dev *ldev = snd_kcontrol_chip(kcontrol);
  644. int v;
  645. switch (kcontrol->private_value) {
  646. case 0:
  647. v = ldev->gpio.methods->get_detect(&ldev->gpio,
  648. AOA_NOTIFY_HEADPHONE);
  649. break;
  650. case 1:
  651. v = ldev->gpio.methods->get_detect(&ldev->gpio,
  652. AOA_NOTIFY_LINE_OUT);
  653. break;
  654. default:
  655. return -ENODEV;
  656. }
  657. ucontrol->value.integer.value[0] = v;
  658. return 0;
  659. }
  660. static struct snd_kcontrol_new headphone_detected = {
  661. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  662. .name = "Headphone Detected",
  663. .info = control_info,
  664. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  665. .get = detected_get,
  666. .private_value = 0,
  667. };
  668. static struct snd_kcontrol_new lineout_detected = {
  669. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  670. .name = "Line-Out Detected",
  671. .info = control_info,
  672. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  673. .get = detected_get,
  674. .private_value = 1,
  675. };
  676. static int check_codec(struct aoa_codec *codec,
  677. struct layout_dev *ldev,
  678. struct codec_connect_info *cci)
  679. {
  680. u32 *ref;
  681. char propname[32];
  682. struct codec_connection *cc;
  683. /* if the codec has a 'codec' node, we require a reference */
  684. if (codec->node && (strcmp(codec->node->name, "codec") == 0)) {
  685. snprintf(propname, sizeof(propname),
  686. "platform-%s-codec-ref", codec->name);
  687. ref = (u32*)get_property(ldev->sound, propname, NULL);
  688. if (!ref) {
  689. printk(KERN_INFO "snd-aoa-fabric-layout: "
  690. "required property %s not present\n", propname);
  691. return -ENODEV;
  692. }
  693. if (*ref != codec->node->linux_phandle) {
  694. printk(KERN_INFO "snd-aoa-fabric-layout: "
  695. "%s doesn't match!\n", propname);
  696. return -ENODEV;
  697. }
  698. } else {
  699. if (layouts_list_items != 1) {
  700. printk(KERN_INFO "snd-aoa-fabric-layout: "
  701. "more than one soundbus, but no references.\n");
  702. return -ENODEV;
  703. }
  704. }
  705. codec->soundbus_dev = ldev->sdev;
  706. codec->gpio = &ldev->gpio;
  707. cc = cci->connections;
  708. if (!cc)
  709. return -EINVAL;
  710. printk(KERN_INFO "snd-aoa-fabric-layout: can use this codec\n");
  711. codec->connected = 0;
  712. codec->fabric_data = cc;
  713. while (cc->connected) {
  714. codec->connected |= 1<<cc->codec_bit;
  715. cc++;
  716. }
  717. return 0;
  718. }
  719. static int layout_found_codec(struct aoa_codec *codec)
  720. {
  721. struct layout_dev *ldev;
  722. int i;
  723. list_for_each_entry(ldev, &layouts_list, list) {
  724. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  725. if (!ldev->layout->codecs[i].name)
  726. continue;
  727. if (strcmp(ldev->layout->codecs[i].name, codec->name) == 0) {
  728. if (check_codec(codec,
  729. ldev,
  730. &ldev->layout->codecs[i]) == 0)
  731. return 0;
  732. }
  733. }
  734. }
  735. return -ENODEV;
  736. }
  737. static void layout_remove_codec(struct aoa_codec *codec)
  738. {
  739. int i;
  740. /* here remove the codec from the layout dev's
  741. * codec reference */
  742. codec->soundbus_dev = NULL;
  743. codec->gpio = NULL;
  744. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  745. }
  746. }
  747. static void layout_notify(void *data)
  748. {
  749. struct layout_dev_ptr *dptr = data;
  750. struct layout_dev *ldev;
  751. int v, update;
  752. struct snd_kcontrol *detected, *c;
  753. struct snd_card *card = aoa_get_card();
  754. ldev = dptr->ptr;
  755. if (data == &ldev->selfptr_headphone) {
  756. v = ldev->gpio.methods->get_detect(&ldev->gpio, AOA_NOTIFY_HEADPHONE);
  757. detected = ldev->headphone_detected_ctrl;
  758. update = ldev->switch_on_headphone;
  759. if (update) {
  760. ldev->gpio.methods->set_speakers(&ldev->gpio, !v);
  761. ldev->gpio.methods->set_headphone(&ldev->gpio, v);
  762. ldev->gpio.methods->set_lineout(&ldev->gpio, 0);
  763. }
  764. } else if (data == &ldev->selfptr_lineout) {
  765. v = ldev->gpio.methods->get_detect(&ldev->gpio, AOA_NOTIFY_LINE_OUT);
  766. detected = ldev->lineout_detected_ctrl;
  767. update = ldev->switch_on_lineout;
  768. if (update) {
  769. ldev->gpio.methods->set_speakers(&ldev->gpio, !v);
  770. ldev->gpio.methods->set_headphone(&ldev->gpio, 0);
  771. ldev->gpio.methods->set_lineout(&ldev->gpio, v);
  772. }
  773. } else
  774. return;
  775. if (detected)
  776. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &detected->id);
  777. if (update) {
  778. c = ldev->headphone_ctrl;
  779. if (c)
  780. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
  781. c = ldev->speaker_ctrl;
  782. if (c)
  783. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
  784. c = ldev->lineout_ctrl;
  785. if (c)
  786. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &c->id);
  787. }
  788. }
  789. static void layout_attached_codec(struct aoa_codec *codec)
  790. {
  791. struct codec_connection *cc;
  792. struct snd_kcontrol *ctl;
  793. int headphones, lineout;
  794. struct layout_dev *ldev = layout_device;
  795. /* need to add this codec to our codec array! */
  796. cc = codec->fabric_data;
  797. headphones = codec->gpio->methods->get_detect(codec->gpio,
  798. AOA_NOTIFY_HEADPHONE);
  799. lineout = codec->gpio->methods->get_detect(codec->gpio,
  800. AOA_NOTIFY_LINE_OUT);
  801. while (cc->connected) {
  802. if (cc->connected & CC_SPEAKERS) {
  803. if (headphones <= 0 && lineout <= 0)
  804. ldev->gpio.methods->set_speakers(codec->gpio, 1);
  805. ctl = snd_ctl_new1(&speakers_ctl, codec->gpio);
  806. ldev->speaker_ctrl = ctl;
  807. aoa_snd_ctl_add(ctl);
  808. }
  809. if (cc->connected & CC_HEADPHONE) {
  810. if (headphones == 1)
  811. ldev->gpio.methods->set_headphone(codec->gpio, 1);
  812. ctl = snd_ctl_new1(&headphone_ctl, codec->gpio);
  813. ldev->headphone_ctrl = ctl;
  814. aoa_snd_ctl_add(ctl);
  815. ldev->have_headphone_detect =
  816. !ldev->gpio.methods
  817. ->set_notify(&ldev->gpio,
  818. AOA_NOTIFY_HEADPHONE,
  819. layout_notify,
  820. &ldev->selfptr_headphone);
  821. if (ldev->have_headphone_detect) {
  822. ctl = snd_ctl_new1(&headphone_detect_choice,
  823. ldev);
  824. aoa_snd_ctl_add(ctl);
  825. ctl = snd_ctl_new1(&headphone_detected,
  826. ldev);
  827. ldev->headphone_detected_ctrl = ctl;
  828. aoa_snd_ctl_add(ctl);
  829. }
  830. }
  831. if (cc->connected & CC_LINEOUT) {
  832. if (lineout == 1)
  833. ldev->gpio.methods->set_lineout(codec->gpio, 1);
  834. ctl = snd_ctl_new1(&lineout_ctl, codec->gpio);
  835. if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
  836. strlcpy(ctl->id.name,
  837. "Headphone Switch", sizeof(ctl->id.name));
  838. ldev->lineout_ctrl = ctl;
  839. aoa_snd_ctl_add(ctl);
  840. ldev->have_lineout_detect =
  841. !ldev->gpio.methods
  842. ->set_notify(&ldev->gpio,
  843. AOA_NOTIFY_LINE_OUT,
  844. layout_notify,
  845. &ldev->selfptr_lineout);
  846. if (ldev->have_lineout_detect) {
  847. ctl = snd_ctl_new1(&lineout_detect_choice,
  848. ldev);
  849. if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
  850. strlcpy(ctl->id.name,
  851. "Headphone Detect Autoswitch",
  852. sizeof(ctl->id.name));
  853. aoa_snd_ctl_add(ctl);
  854. ctl = snd_ctl_new1(&lineout_detected,
  855. ldev);
  856. if (cc->connected & CC_LINEOUT_LABELLED_HEADPHONE)
  857. strlcpy(ctl->id.name,
  858. "Headphone Detected",
  859. sizeof(ctl->id.name));
  860. ldev->lineout_detected_ctrl = ctl;
  861. aoa_snd_ctl_add(ctl);
  862. }
  863. }
  864. cc++;
  865. }
  866. /* now update initial state */
  867. if (ldev->have_headphone_detect)
  868. layout_notify(&ldev->selfptr_headphone);
  869. if (ldev->have_lineout_detect)
  870. layout_notify(&ldev->selfptr_lineout);
  871. }
  872. static struct aoa_fabric layout_fabric = {
  873. .name = "SoundByLayout",
  874. .owner = THIS_MODULE,
  875. .found_codec = layout_found_codec,
  876. .remove_codec = layout_remove_codec,
  877. .attached_codec = layout_attached_codec,
  878. };
  879. static int aoa_fabric_layout_probe(struct soundbus_dev *sdev)
  880. {
  881. struct device_node *sound = NULL;
  882. unsigned int *layout_id;
  883. struct layout *layout;
  884. struct layout_dev *ldev = NULL;
  885. int err;
  886. /* hm, currently we can only have one ... */
  887. if (layout_device)
  888. return -ENODEV;
  889. /* by breaking out we keep a reference */
  890. while ((sound = of_get_next_child(sdev->ofdev.node, sound))) {
  891. if (sound->type && strcasecmp(sound->type, "soundchip") == 0)
  892. break;
  893. }
  894. if (!sound) return -ENODEV;
  895. layout_id = (unsigned int *) get_property(sound, "layout-id", NULL);
  896. if (!layout_id)
  897. goto outnodev;
  898. printk(KERN_INFO "snd-aoa-fabric-layout: found bus with layout %d\n",
  899. *layout_id);
  900. layout = find_layout_by_id(*layout_id);
  901. if (!layout) {
  902. printk(KERN_ERR "snd-aoa-fabric-layout: unknown layout\n");
  903. goto outnodev;
  904. }
  905. ldev = kzalloc(sizeof(struct layout_dev), GFP_KERNEL);
  906. if (!ldev)
  907. goto outnodev;
  908. layout_device = ldev;
  909. ldev->sdev = sdev;
  910. ldev->sound = sound;
  911. ldev->layout = layout;
  912. ldev->gpio.node = sound->parent;
  913. switch (layout->layout_id) {
  914. case 41: /* that unknown machine no one seems to have */
  915. case 51: /* PowerBook5,4 */
  916. case 58: /* Mac Mini */
  917. ldev->gpio.methods = ftr_gpio_methods;
  918. printk(KERN_DEBUG
  919. "snd-aoa-fabric-layout: Using direct GPIOs\n");
  920. break;
  921. default:
  922. ldev->gpio.methods = pmf_gpio_methods;
  923. printk(KERN_DEBUG
  924. "snd-aoa-fabric-layout: Using PMF GPIOs\n");
  925. }
  926. ldev->selfptr_headphone.ptr = ldev;
  927. ldev->selfptr_lineout.ptr = ldev;
  928. sdev->ofdev.dev.driver_data = ldev;
  929. list_add(&ldev->list, &layouts_list);
  930. layouts_list_items++;
  931. /* assign these before registering ourselves, so
  932. * callbacks that are done during registration
  933. * already have the values */
  934. sdev->pcmid = ldev->layout->pcmid;
  935. if (ldev->layout->busname) {
  936. sdev->pcmname = ldev->layout->busname;
  937. } else {
  938. sdev->pcmname = "Master";
  939. }
  940. ldev->gpio.methods->init(&ldev->gpio);
  941. err = aoa_fabric_register(&layout_fabric, &sdev->ofdev.dev);
  942. if (err && err != -EALREADY) {
  943. printk(KERN_INFO "snd-aoa-fabric-layout: can't use,"
  944. " another fabric is active!\n");
  945. goto outlistdel;
  946. }
  947. use_layout(layout);
  948. ldev->switch_on_headphone = 1;
  949. ldev->switch_on_lineout = 1;
  950. return 0;
  951. outlistdel:
  952. /* we won't be using these then... */
  953. ldev->gpio.methods->exit(&ldev->gpio);
  954. /* reset if we didn't use it */
  955. sdev->pcmname = NULL;
  956. sdev->pcmid = -1;
  957. list_del(&ldev->list);
  958. layouts_list_items--;
  959. outnodev:
  960. of_node_put(sound);
  961. layout_device = NULL;
  962. kfree(ldev);
  963. return -ENODEV;
  964. }
  965. static int aoa_fabric_layout_remove(struct soundbus_dev *sdev)
  966. {
  967. struct layout_dev *ldev = sdev->ofdev.dev.driver_data;
  968. int i;
  969. for (i=0; i<MAX_CODECS_PER_BUS; i++) {
  970. if (ldev->codecs[i]) {
  971. aoa_fabric_unlink_codec(ldev->codecs[i]);
  972. }
  973. ldev->codecs[i] = NULL;
  974. }
  975. list_del(&ldev->list);
  976. layouts_list_items--;
  977. of_node_put(ldev->sound);
  978. ldev->gpio.methods->set_notify(&ldev->gpio,
  979. AOA_NOTIFY_HEADPHONE,
  980. NULL,
  981. NULL);
  982. ldev->gpio.methods->set_notify(&ldev->gpio,
  983. AOA_NOTIFY_LINE_OUT,
  984. NULL,
  985. NULL);
  986. ldev->gpio.methods->exit(&ldev->gpio);
  987. layout_device = NULL;
  988. kfree(ldev);
  989. sdev->pcmid = -1;
  990. sdev->pcmname = NULL;
  991. return 0;
  992. }
  993. #ifdef CONFIG_PM
  994. static int aoa_fabric_layout_suspend(struct soundbus_dev *sdev, pm_message_t state)
  995. {
  996. struct layout_dev *ldev = sdev->ofdev.dev.driver_data;
  997. if (ldev->gpio.methods && ldev->gpio.methods->all_amps_off)
  998. ldev->gpio.methods->all_amps_off(&ldev->gpio);
  999. return 0;
  1000. }
  1001. static int aoa_fabric_layout_resume(struct soundbus_dev *sdev)
  1002. {
  1003. struct layout_dev *ldev = sdev->ofdev.dev.driver_data;
  1004. if (ldev->gpio.methods && ldev->gpio.methods->all_amps_off)
  1005. ldev->gpio.methods->all_amps_restore(&ldev->gpio);
  1006. return 0;
  1007. }
  1008. #endif
  1009. static struct soundbus_driver aoa_soundbus_driver = {
  1010. .name = "snd_aoa_soundbus_drv",
  1011. .owner = THIS_MODULE,
  1012. .probe = aoa_fabric_layout_probe,
  1013. .remove = aoa_fabric_layout_remove,
  1014. #ifdef CONFIG_PM
  1015. .suspend = aoa_fabric_layout_suspend,
  1016. .resume = aoa_fabric_layout_resume,
  1017. #endif
  1018. .driver = {
  1019. .owner = THIS_MODULE,
  1020. }
  1021. };
  1022. static int __init aoa_fabric_layout_init(void)
  1023. {
  1024. int err;
  1025. err = soundbus_register_driver(&aoa_soundbus_driver);
  1026. if (err)
  1027. return err;
  1028. return 0;
  1029. }
  1030. static void __exit aoa_fabric_layout_exit(void)
  1031. {
  1032. soundbus_unregister_driver(&aoa_soundbus_driver);
  1033. aoa_fabric_unregister(&layout_fabric);
  1034. }
  1035. module_init(aoa_fabric_layout_init);
  1036. module_exit(aoa_fabric_layout_exit);