layout.c 27 KB

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