layout.c 27 KB

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