snd-aoa-fabric-layout.c 26 KB

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