layout.c 27 KB

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