dmasound_awacs.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194
  1. /*
  2. * linux/sound/oss/dmasound/dmasound_awacs.c
  3. *
  4. * PowerMac `AWACS' and `Burgundy' DMA Sound Driver
  5. * with some limited support for DACA & Tumbler
  6. *
  7. * See linux/sound/oss/dmasound/dmasound_core.c for copyright and
  8. * history prior to 2001/01/26.
  9. *
  10. * 26/01/2001 ed 0.1 Iain Sandoe
  11. * - added version info.
  12. * - moved dbdma command buffer allocation to PMacXXXSqSetup()
  13. * - fixed up beep dbdma cmd buffers
  14. *
  15. * 08/02/2001 [0.2]
  16. * - make SNDCTL_DSP_GETFMTS return the correct info for the h/w
  17. * - move soft format translations to a separate file
  18. * - [0.3] make SNDCTL_DSP_GETCAPS return correct info.
  19. * - [0.4] more informative machine name strings.
  20. * - [0.5]
  21. * - record changes.
  22. * - made the default_hard/soft entries.
  23. * 04/04/2001 [0.6]
  24. * - minor correction to bit assignments in awacs_defs.h
  25. * - incorporate mixer changes from 2.2.x back-port.
  26. * - take out passthru as a rec input (it isn't).
  27. * - make Input Gain slider work the 'right way up'.
  28. * - try to make the mixer sliders more logical - so now the
  29. * input selectors are just two-state (>50% == ON) and the
  30. * Input Gain slider handles the rest of the gain issues.
  31. * - try to pick slider representations that most closely match
  32. * the actual use - e.g. IGain for input gain...
  33. * - first stab at over/under-run detection.
  34. * - minor cosmetic changes to IRQ identification.
  35. * - fix bug where rates > max would be reported as supported.
  36. * - first stab at over/under-run detection.
  37. * - make use of i2c for mixer settings conditional on perch
  38. * rather than cuda (some machines without perch have cuda).
  39. * - fix bug where TX stops when dbdma status comes up "DEAD"
  40. * so far only reported on PowerComputing clones ... but.
  41. * - put in AWACS/Screamer register write timeouts.
  42. * - part way to partitioning the init() stuff
  43. * - first pass at 'tumbler' stuff (not support - just an attempt
  44. * to allow the driver to load on new G4s).
  45. * 01/02/2002 [0.7] - BenH
  46. * - all sort of minor bits went in since the latest update, I
  47. * bumped the version number for that reason
  48. *
  49. * 07/26/2002 [0.8] - BenH
  50. * - More minor bits since last changelog (I should be more careful
  51. * with those)
  52. * - Support for snapper & better tumbler integration by Toby Sargeant
  53. * - Headphone detect for scremer by Julien Blache
  54. * - More tumbler fixed by Andreas Schwab
  55. * 11/29/2003 [0.8.1] - Renzo Davoli (King Enzo)
  56. * - Support for Snapper line in
  57. * - snapper input resampling (for rates < 44100)
  58. * - software line gain control
  59. */
  60. /* GENERAL FIXME/TODO: check that the assumptions about what is written to
  61. mac-io is valid for DACA & Tumbler.
  62. This driver is in bad need of a rewrite. The dbdma code has to be split,
  63. some proper device-tree parsing code has to be written, etc...
  64. */
  65. #include <linux/types.h>
  66. #include <linux/module.h>
  67. #include <linux/config.h>
  68. #include <linux/slab.h>
  69. #include <linux/init.h>
  70. #include <linux/delay.h>
  71. #include <linux/soundcard.h>
  72. #include <linux/adb.h>
  73. #include <linux/nvram.h>
  74. #include <linux/tty.h>
  75. #include <linux/vt_kern.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/kmod.h>
  78. #include <linux/interrupt.h>
  79. #include <linux/input.h>
  80. #include <asm/semaphore.h>
  81. #ifdef CONFIG_ADB_CUDA
  82. #include <linux/cuda.h>
  83. #endif
  84. #ifdef CONFIG_ADB_PMU
  85. #include <linux/pmu.h>
  86. #endif
  87. #include <linux/i2c-dev.h>
  88. #include <asm/uaccess.h>
  89. #include <asm/prom.h>
  90. #include <asm/machdep.h>
  91. #include <asm/io.h>
  92. #include <asm/dbdma.h>
  93. #include <asm/pmac_feature.h>
  94. #include <asm/irq.h>
  95. #include <asm/nvram.h>
  96. #include "awacs_defs.h"
  97. #include "dmasound.h"
  98. #include "tas3001c.h"
  99. #include "tas3004.h"
  100. #include "tas_common.h"
  101. #define DMASOUND_AWACS_REVISION 0
  102. #define DMASOUND_AWACS_EDITION 7
  103. #define AWACS_SNAPPER 110 /* fake revision # for snapper */
  104. #define AWACS_BURGUNDY 100 /* fake revision # for burgundy */
  105. #define AWACS_TUMBLER 90 /* fake revision # for tumbler */
  106. #define AWACS_DACA 80 /* fake revision # for daca (ibook) */
  107. #define AWACS_AWACS 2 /* holding revision for AWACS */
  108. #define AWACS_SCREAMER 3 /* holding revision for Screamer */
  109. /*
  110. * Interrupt numbers and addresses, & info obtained from the device tree.
  111. */
  112. static int awacs_irq, awacs_tx_irq, awacs_rx_irq;
  113. static volatile struct awacs_regs __iomem *awacs;
  114. static volatile u32 __iomem *i2s;
  115. static volatile struct dbdma_regs __iomem *awacs_txdma, *awacs_rxdma;
  116. static int awacs_rate_index;
  117. static int awacs_subframe;
  118. static struct device_node* awacs_node;
  119. static struct device_node* i2s_node;
  120. static struct resource awacs_rsrc[3];
  121. static char awacs_name[64];
  122. static int awacs_revision;
  123. static int awacs_sleeping;
  124. static DECLARE_MUTEX(dmasound_sem);
  125. static int sound_device_id; /* exists after iMac revA */
  126. static int hw_can_byteswap = 1 ; /* most pmac sound h/w can */
  127. /* model info */
  128. /* To be replaced with better interaction with pmac_feature.c */
  129. static int is_pbook_3X00;
  130. static int is_pbook_g3;
  131. /* expansion info */
  132. static int has_perch;
  133. static int has_ziva;
  134. /* for earlier powerbooks which need fiddling with mac-io to enable
  135. * cd etc.
  136. */
  137. static unsigned char __iomem *latch_base;
  138. static unsigned char __iomem *macio_base;
  139. /*
  140. * Space for the DBDMA command blocks.
  141. */
  142. static void *awacs_tx_cmd_space;
  143. static volatile struct dbdma_cmd *awacs_tx_cmds;
  144. static int number_of_tx_cmd_buffers;
  145. static void *awacs_rx_cmd_space;
  146. static volatile struct dbdma_cmd *awacs_rx_cmds;
  147. static int number_of_rx_cmd_buffers;
  148. /*
  149. * Cached values of AWACS registers (we can't read them).
  150. * Except on the burgundy (and screamer). XXX
  151. */
  152. int awacs_reg[8];
  153. int awacs_reg1_save;
  154. /* tracking values for the mixer contents
  155. */
  156. static int spk_vol;
  157. static int line_vol;
  158. static int passthru_vol;
  159. static int ip_gain; /* mic preamp settings */
  160. static int rec_lev = 0x4545 ; /* default CD gain 69 % */
  161. static int mic_lev;
  162. static int cd_lev = 0x6363 ; /* 99 % */
  163. static int line_lev;
  164. static int hdp_connected;
  165. /*
  166. * Stuff for outputting a beep. The values range from -327 to +327
  167. * so we can multiply by an amplitude in the range 0..100 to get a
  168. * signed short value to put in the output buffer.
  169. */
  170. static short beep_wform[256] = {
  171. 0, 40, 79, 117, 153, 187, 218, 245,
  172. 269, 288, 304, 316, 323, 327, 327, 324,
  173. 318, 310, 299, 288, 275, 262, 249, 236,
  174. 224, 213, 204, 196, 190, 186, 183, 182,
  175. 182, 183, 186, 189, 192, 196, 200, 203,
  176. 206, 208, 209, 209, 209, 207, 204, 201,
  177. 197, 193, 188, 183, 179, 174, 170, 166,
  178. 163, 161, 160, 159, 159, 160, 161, 162,
  179. 164, 166, 168, 169, 171, 171, 171, 170,
  180. 169, 167, 163, 159, 155, 150, 144, 139,
  181. 133, 128, 122, 117, 113, 110, 107, 105,
  182. 103, 103, 103, 103, 104, 104, 105, 105,
  183. 105, 103, 101, 97, 92, 86, 78, 68,
  184. 58, 45, 32, 18, 3, -11, -26, -41,
  185. -55, -68, -79, -88, -95, -100, -102, -102,
  186. -99, -93, -85, -75, -62, -48, -33, -16,
  187. 0, 16, 33, 48, 62, 75, 85, 93,
  188. 99, 102, 102, 100, 95, 88, 79, 68,
  189. 55, 41, 26, 11, -3, -18, -32, -45,
  190. -58, -68, -78, -86, -92, -97, -101, -103,
  191. -105, -105, -105, -104, -104, -103, -103, -103,
  192. -103, -105, -107, -110, -113, -117, -122, -128,
  193. -133, -139, -144, -150, -155, -159, -163, -167,
  194. -169, -170, -171, -171, -171, -169, -168, -166,
  195. -164, -162, -161, -160, -159, -159, -160, -161,
  196. -163, -166, -170, -174, -179, -183, -188, -193,
  197. -197, -201, -204, -207, -209, -209, -209, -208,
  198. -206, -203, -200, -196, -192, -189, -186, -183,
  199. -182, -182, -183, -186, -190, -196, -204, -213,
  200. -224, -236, -249, -262, -275, -288, -299, -310,
  201. -318, -324, -327, -327, -323, -316, -304, -288,
  202. -269, -245, -218, -187, -153, -117, -79, -40,
  203. };
  204. /* beep support */
  205. #define BEEP_SRATE 22050 /* 22050 Hz sample rate */
  206. #define BEEP_BUFLEN 512
  207. #define BEEP_VOLUME 15 /* 0 - 100 */
  208. static int beep_vol = BEEP_VOLUME;
  209. static int beep_playing;
  210. static int awacs_beep_state;
  211. static short *beep_buf;
  212. static void *beep_dbdma_cmd_space;
  213. static volatile struct dbdma_cmd *beep_dbdma_cmd;
  214. /* Burgundy functions */
  215. static void awacs_burgundy_wcw(unsigned addr,unsigned newval);
  216. static unsigned awacs_burgundy_rcw(unsigned addr);
  217. static void awacs_burgundy_write_volume(unsigned address, int volume);
  218. static int awacs_burgundy_read_volume(unsigned address);
  219. static void awacs_burgundy_write_mvolume(unsigned address, int volume);
  220. static int awacs_burgundy_read_mvolume(unsigned address);
  221. /* we will allocate a single 'emergency' dbdma cmd block to use if the
  222. tx status comes up "DEAD". This happens on some PowerComputing Pmac
  223. clones, either owing to a bug in dbdma or some interaction between
  224. IDE and sound. However, this measure would deal with DEAD status if
  225. if appeared elsewhere.
  226. for the sake of memory efficiency we'll allocate this cmd as part of
  227. the beep cmd stuff.
  228. */
  229. static volatile struct dbdma_cmd *emergency_dbdma_cmd;
  230. #ifdef CONFIG_PM
  231. /*
  232. * Stuff for restoring after a sleep.
  233. */
  234. static int awacs_sleep_notify(struct pmu_sleep_notifier *self, int when);
  235. struct pmu_sleep_notifier awacs_sleep_notifier = {
  236. awacs_sleep_notify, SLEEP_LEVEL_SOUND,
  237. };
  238. #endif /* CONFIG_PM */
  239. /* for (soft) sample rate translations */
  240. int expand_bal; /* Balance factor for expanding (not volume!) */
  241. int expand_read_bal; /* Balance factor for expanding reads (not volume!) */
  242. /*** Low level stuff *********************************************************/
  243. static void *PMacAlloc(unsigned int size, gfp_t flags);
  244. static void PMacFree(void *ptr, unsigned int size);
  245. static int PMacIrqInit(void);
  246. #ifdef MODULE
  247. static void PMacIrqCleanup(void);
  248. #endif
  249. static void PMacSilence(void);
  250. static void PMacInit(void);
  251. static int PMacSetFormat(int format);
  252. static int PMacSetVolume(int volume);
  253. static void PMacPlay(void);
  254. static void PMacRecord(void);
  255. static irqreturn_t pmac_awacs_tx_intr(int irq, void *devid, struct pt_regs *regs);
  256. static irqreturn_t pmac_awacs_rx_intr(int irq, void *devid, struct pt_regs *regs);
  257. static irqreturn_t pmac_awacs_intr(int irq, void *devid, struct pt_regs *regs);
  258. static void awacs_write(int val);
  259. static int awacs_get_volume(int reg, int lshift);
  260. static int awacs_volume_setter(int volume, int n, int mute, int lshift);
  261. /*** Mid level stuff **********************************************************/
  262. static int PMacMixerIoctl(u_int cmd, u_long arg);
  263. static int PMacWriteSqSetup(void);
  264. static int PMacReadSqSetup(void);
  265. static void PMacAbortRead(void);
  266. extern TRANS transAwacsNormal ;
  267. extern TRANS transAwacsExpand ;
  268. extern TRANS transAwacsNormalRead ;
  269. extern TRANS transAwacsExpandRead ;
  270. extern int daca_init(void);
  271. extern void daca_cleanup(void);
  272. extern int daca_set_volume(uint left_vol, uint right_vol);
  273. extern void daca_get_volume(uint * left_vol, uint *right_vol);
  274. extern int daca_enter_sleep(void);
  275. extern int daca_leave_sleep(void);
  276. #define TRY_LOCK() \
  277. if ((rc = down_interruptible(&dmasound_sem)) != 0) \
  278. return rc;
  279. #define LOCK() down(&dmasound_sem);
  280. #define UNLOCK() up(&dmasound_sem);
  281. /* We use different versions that the ones provided in dmasound.h
  282. *
  283. * FIXME: Use different names ;)
  284. */
  285. #undef IOCTL_IN
  286. #undef IOCTL_OUT
  287. #define IOCTL_IN(arg, ret) \
  288. rc = get_user(ret, (int __user *)(arg)); \
  289. if (rc) break;
  290. #define IOCTL_OUT(arg, ret) \
  291. ioctl_return2((int __user *)(arg), ret)
  292. static inline int ioctl_return2(int __user *addr, int value)
  293. {
  294. return value < 0 ? value : put_user(value, addr);
  295. }
  296. /*** AE - TUMBLER / SNAPPER START ************************************************/
  297. int gpio_audio_reset, gpio_audio_reset_pol;
  298. int gpio_amp_mute, gpio_amp_mute_pol;
  299. int gpio_headphone_mute, gpio_headphone_mute_pol;
  300. int gpio_headphone_detect, gpio_headphone_detect_pol;
  301. int gpio_headphone_irq;
  302. int
  303. setup_audio_gpio(const char *name, const char* compatible, int *gpio_addr, int* gpio_pol)
  304. {
  305. struct device_node *np;
  306. u32* pp;
  307. np = find_devices("gpio");
  308. if (!np)
  309. return -ENODEV;
  310. np = np->child;
  311. while(np != 0) {
  312. if (name) {
  313. char *property = get_property(np,"audio-gpio",NULL);
  314. if (property != 0 && strcmp(property,name) == 0)
  315. break;
  316. } else if (compatible && device_is_compatible(np, compatible))
  317. break;
  318. np = np->sibling;
  319. }
  320. if (!np)
  321. return -ENODEV;
  322. pp = (u32 *)get_property(np, "AAPL,address", NULL);
  323. if (!pp)
  324. return -ENODEV;
  325. *gpio_addr = (*pp) & 0x0000ffff;
  326. pp = (u32 *)get_property(np, "audio-gpio-active-state", NULL);
  327. if (pp)
  328. *gpio_pol = *pp;
  329. else
  330. *gpio_pol = 1;
  331. if (np->n_intrs > 0)
  332. return np->intrs[0].line;
  333. return 0;
  334. }
  335. static inline void
  336. write_audio_gpio(int gpio_addr, int data)
  337. {
  338. if (!gpio_addr)
  339. return;
  340. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio_addr, data ? 0x05 : 0x04);
  341. }
  342. static inline int
  343. read_audio_gpio(int gpio_addr)
  344. {
  345. if (!gpio_addr)
  346. return 0;
  347. return ((pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio_addr, 0) & 0x02) !=0);
  348. }
  349. /*
  350. * Headphone interrupt via GPIO (Tumbler, Snapper, DACA)
  351. */
  352. static irqreturn_t
  353. headphone_intr(int irq, void *devid, struct pt_regs *regs)
  354. {
  355. unsigned long flags;
  356. spin_lock_irqsave(&dmasound.lock, flags);
  357. if (read_audio_gpio(gpio_headphone_detect) == gpio_headphone_detect_pol) {
  358. printk(KERN_INFO "Audio jack plugged, muting speakers.\n");
  359. write_audio_gpio(gpio_headphone_mute, !gpio_headphone_mute_pol);
  360. write_audio_gpio(gpio_amp_mute, gpio_amp_mute_pol);
  361. tas_output_device_change(sound_device_id,TAS_OUTPUT_HEADPHONES,0);
  362. } else {
  363. printk(KERN_INFO "Audio jack unplugged, enabling speakers.\n");
  364. write_audio_gpio(gpio_amp_mute, !gpio_amp_mute_pol);
  365. write_audio_gpio(gpio_headphone_mute, gpio_headphone_mute_pol);
  366. tas_output_device_change(sound_device_id,TAS_OUTPUT_INTERNAL_SPKR,0);
  367. }
  368. spin_unlock_irqrestore(&dmasound.lock, flags);
  369. return IRQ_HANDLED;
  370. }
  371. /* Initialize tumbler */
  372. static int
  373. tas_dmasound_init(void)
  374. {
  375. setup_audio_gpio(
  376. "audio-hw-reset",
  377. NULL,
  378. &gpio_audio_reset,
  379. &gpio_audio_reset_pol);
  380. setup_audio_gpio(
  381. "amp-mute",
  382. NULL,
  383. &gpio_amp_mute,
  384. &gpio_amp_mute_pol);
  385. setup_audio_gpio("headphone-mute",
  386. NULL,
  387. &gpio_headphone_mute,
  388. &gpio_headphone_mute_pol);
  389. gpio_headphone_irq = setup_audio_gpio(
  390. "headphone-detect",
  391. NULL,
  392. &gpio_headphone_detect,
  393. &gpio_headphone_detect_pol);
  394. /* Fix some broken OF entries in desktop machines */
  395. if (!gpio_headphone_irq)
  396. gpio_headphone_irq = setup_audio_gpio(
  397. NULL,
  398. "keywest-gpio15",
  399. &gpio_headphone_detect,
  400. &gpio_headphone_detect_pol);
  401. write_audio_gpio(gpio_audio_reset, gpio_audio_reset_pol);
  402. msleep(100);
  403. write_audio_gpio(gpio_audio_reset, !gpio_audio_reset_pol);
  404. msleep(100);
  405. if (gpio_headphone_irq) {
  406. if (request_irq(gpio_headphone_irq,headphone_intr,0,"Headphone detect",NULL) < 0) {
  407. printk(KERN_ERR "tumbler: Can't request headphone interrupt\n");
  408. gpio_headphone_irq = 0;
  409. } else {
  410. u8 val;
  411. /* Activate headphone status interrupts */
  412. val = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio_headphone_detect, 0);
  413. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio_headphone_detect, val | 0x80);
  414. /* Trigger it */
  415. headphone_intr(0,NULL,NULL);
  416. }
  417. }
  418. if (!gpio_headphone_irq) {
  419. /* Some machine enter this case ? */
  420. printk(KERN_WARNING "tumbler: Headphone detect IRQ not found, enabling all outputs !\n");
  421. write_audio_gpio(gpio_amp_mute, !gpio_amp_mute_pol);
  422. write_audio_gpio(gpio_headphone_mute, !gpio_headphone_mute_pol);
  423. }
  424. return 0;
  425. }
  426. static int
  427. tas_dmasound_cleanup(void)
  428. {
  429. if (gpio_headphone_irq)
  430. free_irq(gpio_headphone_irq, NULL);
  431. return 0;
  432. }
  433. /* We don't support 48k yet */
  434. static int tas_freqs[1] = { 44100 } ;
  435. static int tas_freqs_ok[1] = { 1 } ;
  436. /* don't know what to do really - just have to leave it where
  437. * OF left things
  438. */
  439. static int
  440. tas_set_frame_rate(void)
  441. {
  442. if (i2s) {
  443. out_le32(i2s + (I2S_REG_SERIAL_FORMAT >> 2), 0x41190000);
  444. out_le32(i2s + (I2S_REG_DATAWORD_SIZES >> 2), 0x02000200);
  445. }
  446. dmasound.hard.speed = 44100 ;
  447. awacs_rate_index = 0 ;
  448. return 44100 ;
  449. }
  450. static int
  451. tas_mixer_ioctl(u_int cmd, u_long arg)
  452. {
  453. int __user *argp = (int __user *)arg;
  454. int data;
  455. int rc;
  456. rc=tas_device_ioctl(cmd, arg);
  457. if (rc != -EINVAL) {
  458. return rc;
  459. }
  460. if ((cmd & ~0xff) == MIXER_WRITE(0) &&
  461. tas_supported_mixers() & (1<<(cmd & 0xff))) {
  462. rc = get_user(data, argp);
  463. if (rc<0) return rc;
  464. tas_set_mixer_level(cmd & 0xff, data);
  465. tas_get_mixer_level(cmd & 0xff, &data);
  466. return ioctl_return2(argp, data);
  467. }
  468. if ((cmd & ~0xff) == MIXER_READ(0) &&
  469. tas_supported_mixers() & (1<<(cmd & 0xff))) {
  470. tas_get_mixer_level(cmd & 0xff, &data);
  471. return ioctl_return2(argp, data);
  472. }
  473. switch(cmd) {
  474. case SOUND_MIXER_READ_DEVMASK:
  475. data = tas_supported_mixers() | SOUND_MASK_SPEAKER;
  476. rc = IOCTL_OUT(arg, data);
  477. break;
  478. case SOUND_MIXER_READ_STEREODEVS:
  479. data = tas_stereo_mixers();
  480. rc = IOCTL_OUT(arg, data);
  481. break;
  482. case SOUND_MIXER_READ_CAPS:
  483. rc = IOCTL_OUT(arg, 0);
  484. break;
  485. case SOUND_MIXER_READ_RECMASK:
  486. // XXX FIXME: find a way to check what is really available */
  487. data = SOUND_MASK_LINE | SOUND_MASK_MIC;
  488. rc = IOCTL_OUT(arg, data);
  489. break;
  490. case SOUND_MIXER_READ_RECSRC:
  491. if (awacs_reg[0] & MASK_MUX_AUDIN)
  492. data |= SOUND_MASK_LINE;
  493. if (awacs_reg[0] & MASK_MUX_MIC)
  494. data |= SOUND_MASK_MIC;
  495. rc = IOCTL_OUT(arg, data);
  496. break;
  497. case SOUND_MIXER_WRITE_RECSRC:
  498. IOCTL_IN(arg, data);
  499. data =0;
  500. rc = IOCTL_OUT(arg, data);
  501. break;
  502. case SOUND_MIXER_WRITE_SPEAKER: /* really bell volume */
  503. IOCTL_IN(arg, data);
  504. beep_vol = data & 0xff;
  505. /* fall through */
  506. case SOUND_MIXER_READ_SPEAKER:
  507. rc = IOCTL_OUT(arg, (beep_vol<<8) | beep_vol);
  508. break;
  509. case SOUND_MIXER_OUTMASK:
  510. case SOUND_MIXER_OUTSRC:
  511. default:
  512. rc = -EINVAL;
  513. }
  514. return rc;
  515. }
  516. static void __init
  517. tas_init_frame_rates(unsigned int *prop, unsigned int l)
  518. {
  519. int i ;
  520. if (prop) {
  521. for (i=0; i<1; i++)
  522. tas_freqs_ok[i] = 0;
  523. for (l /= sizeof(int); l > 0; --l) {
  524. unsigned int r = *prop++;
  525. /* Apple 'Fixed' format */
  526. if (r >= 0x10000)
  527. r >>= 16;
  528. for (i = 0; i < 1; ++i) {
  529. if (r == tas_freqs[i]) {
  530. tas_freqs_ok[i] = 1;
  531. break;
  532. }
  533. }
  534. }
  535. }
  536. /* else we assume that all the rates are available */
  537. }
  538. /*** AE - TUMBLER / SNAPPER END ************************************************/
  539. /*** Low level stuff *********************************************************/
  540. /*
  541. * PCI PowerMac, with AWACS, Screamer, Burgundy, DACA or Tumbler and DBDMA.
  542. */
  543. static void *PMacAlloc(unsigned int size, gfp_t flags)
  544. {
  545. return kmalloc(size, flags);
  546. }
  547. static void PMacFree(void *ptr, unsigned int size)
  548. {
  549. kfree(ptr);
  550. }
  551. static int __init PMacIrqInit(void)
  552. {
  553. if (awacs)
  554. if (request_irq(awacs_irq, pmac_awacs_intr, 0, "Built-in Sound misc", NULL))
  555. return 0;
  556. if (request_irq(awacs_tx_irq, pmac_awacs_tx_intr, 0, "Built-in Sound out", NULL)
  557. || request_irq(awacs_rx_irq, pmac_awacs_rx_intr, 0, "Built-in Sound in", NULL))
  558. return 0;
  559. return 1;
  560. }
  561. #ifdef MODULE
  562. static void PMacIrqCleanup(void)
  563. {
  564. /* turn off input & output dma */
  565. DBDMA_DO_STOP(awacs_txdma);
  566. DBDMA_DO_STOP(awacs_rxdma);
  567. if (awacs)
  568. /* disable interrupts from awacs interface */
  569. out_le32(&awacs->control, in_le32(&awacs->control) & 0xfff);
  570. /* Switch off the sound clock */
  571. pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, awacs_node, 0, 0);
  572. /* Make sure proper bits are set on pismo & tipb */
  573. if ((machine_is_compatible("PowerBook3,1") ||
  574. machine_is_compatible("PowerBook3,2")) && awacs) {
  575. awacs_reg[1] |= MASK_PAROUT0 | MASK_PAROUT1;
  576. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  577. msleep(200);
  578. }
  579. if (awacs)
  580. free_irq(awacs_irq, NULL);
  581. free_irq(awacs_tx_irq, NULL);
  582. free_irq(awacs_rx_irq, NULL);
  583. if (awacs)
  584. iounmap(awacs);
  585. if (i2s)
  586. iounmap(i2s);
  587. iounmap(awacs_txdma);
  588. iounmap(awacs_rxdma);
  589. release_mem_region(awacs_rsrc[0].start,
  590. awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
  591. release_mem_region(awacs_rsrc[1].start,
  592. awacs_rsrc[1].end - awacs_rsrc[1].start + 1);
  593. release_mem_region(awacs_rsrc[2].start,
  594. awacs_rsrc[2].end - awacs_rsrc[2].start + 1);
  595. kfree(awacs_tx_cmd_space);
  596. kfree(awacs_rx_cmd_space);
  597. kfree(beep_dbdma_cmd_space);
  598. kfree(beep_buf);
  599. #ifdef CONFIG_PM
  600. pmu_unregister_sleep_notifier(&awacs_sleep_notifier);
  601. #endif
  602. }
  603. #endif /* MODULE */
  604. static void PMacSilence(void)
  605. {
  606. /* turn off output dma */
  607. DBDMA_DO_STOP(awacs_txdma);
  608. }
  609. /* don't know what to do really - just have to leave it where
  610. * OF left things
  611. */
  612. static int daca_set_frame_rate(void)
  613. {
  614. if (i2s) {
  615. out_le32(i2s + (I2S_REG_SERIAL_FORMAT >> 2), 0x41190000);
  616. out_le32(i2s + (I2S_REG_DATAWORD_SIZES >> 2), 0x02000200);
  617. }
  618. dmasound.hard.speed = 44100 ;
  619. awacs_rate_index = 0 ;
  620. return 44100 ;
  621. }
  622. static int awacs_freqs[8] = {
  623. 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
  624. };
  625. static int awacs_freqs_ok[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
  626. static int
  627. awacs_set_frame_rate(int desired, int catch_r)
  628. {
  629. int tolerance, i = 8 ;
  630. /*
  631. * If we have a sample rate which is within catchRadius percent
  632. * of the requested value, we don't have to expand the samples.
  633. * Otherwise choose the next higher rate.
  634. * N.B.: burgundy awacs only works at 44100 Hz.
  635. */
  636. do {
  637. tolerance = catch_r * awacs_freqs[--i] / 100;
  638. if (awacs_freqs_ok[i]
  639. && dmasound.soft.speed <= awacs_freqs[i] + tolerance)
  640. break;
  641. } while (i > 0);
  642. dmasound.hard.speed = awacs_freqs[i];
  643. awacs_rate_index = i;
  644. out_le32(&awacs->control, MASK_IEPC | (i << 8) | 0x11 );
  645. awacs_reg[1] = (awacs_reg[1] & ~MASK_SAMPLERATE) | (i << 3);
  646. awacs_write(awacs_reg[1] | MASK_ADDR1);
  647. return dmasound.hard.speed;
  648. }
  649. static int
  650. burgundy_set_frame_rate(void)
  651. {
  652. awacs_rate_index = 0 ;
  653. awacs_reg[1] = (awacs_reg[1] & ~MASK_SAMPLERATE) ;
  654. /* XXX disable error interrupt on burgundy for now */
  655. out_le32(&awacs->control, MASK_IEPC | 0 | 0x11 | MASK_IEE);
  656. return 44100 ;
  657. }
  658. static int
  659. set_frame_rate(int desired, int catch_r)
  660. {
  661. switch (awacs_revision) {
  662. case AWACS_BURGUNDY:
  663. dmasound.hard.speed = burgundy_set_frame_rate();
  664. break ;
  665. case AWACS_TUMBLER:
  666. case AWACS_SNAPPER:
  667. dmasound.hard.speed = tas_set_frame_rate();
  668. break ;
  669. case AWACS_DACA:
  670. dmasound.hard.speed =
  671. daca_set_frame_rate();
  672. break ;
  673. default:
  674. dmasound.hard.speed = awacs_set_frame_rate(desired,
  675. catch_r);
  676. break ;
  677. }
  678. return dmasound.hard.speed ;
  679. }
  680. static void
  681. awacs_recalibrate(void)
  682. {
  683. /* Sorry for the horrible delays... I hope to get that improved
  684. * by making the whole PM process asynchronous in a future version
  685. */
  686. msleep(750);
  687. awacs_reg[1] |= MASK_CMUTE | MASK_AMUTE;
  688. awacs_write(awacs_reg[1] | MASK_RECALIBRATE | MASK_ADDR1);
  689. msleep(1000);
  690. awacs_write(awacs_reg[1] | MASK_ADDR1);
  691. }
  692. static void PMacInit(void)
  693. {
  694. int tolerance;
  695. switch (dmasound.soft.format) {
  696. case AFMT_S16_LE:
  697. case AFMT_U16_LE:
  698. if (hw_can_byteswap)
  699. dmasound.hard.format = AFMT_S16_LE;
  700. else
  701. dmasound.hard.format = AFMT_S16_BE;
  702. break;
  703. default:
  704. dmasound.hard.format = AFMT_S16_BE;
  705. break;
  706. }
  707. dmasound.hard.stereo = 1;
  708. dmasound.hard.size = 16;
  709. /* set dmasound.hard.speed - on the basis of what we want (soft)
  710. * and the tolerance we'll allow.
  711. */
  712. set_frame_rate(dmasound.soft.speed, catchRadius) ;
  713. tolerance = (catchRadius * dmasound.hard.speed) / 100;
  714. if (dmasound.soft.speed >= dmasound.hard.speed - tolerance) {
  715. dmasound.trans_write = &transAwacsNormal;
  716. dmasound.trans_read = &transAwacsNormalRead;
  717. } else {
  718. dmasound.trans_write = &transAwacsExpand;
  719. dmasound.trans_read = &transAwacsExpandRead;
  720. }
  721. if (awacs) {
  722. if (hw_can_byteswap && (dmasound.hard.format == AFMT_S16_LE))
  723. out_le32(&awacs->byteswap, BS_VAL);
  724. else
  725. out_le32(&awacs->byteswap, 0);
  726. }
  727. expand_bal = -dmasound.soft.speed;
  728. expand_read_bal = -dmasound.soft.speed;
  729. }
  730. static int PMacSetFormat(int format)
  731. {
  732. int size;
  733. int req_format = format;
  734. switch (format) {
  735. case AFMT_QUERY:
  736. return dmasound.soft.format;
  737. case AFMT_MU_LAW:
  738. case AFMT_A_LAW:
  739. case AFMT_U8:
  740. case AFMT_S8:
  741. size = 8;
  742. break;
  743. case AFMT_S16_LE:
  744. if(!hw_can_byteswap)
  745. format = AFMT_S16_BE;
  746. case AFMT_S16_BE:
  747. size = 16;
  748. break;
  749. case AFMT_U16_LE:
  750. if(!hw_can_byteswap)
  751. format = AFMT_U16_BE;
  752. case AFMT_U16_BE:
  753. size = 16;
  754. break;
  755. default: /* :-) */
  756. printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
  757. format);
  758. size = 8;
  759. format = AFMT_U8;
  760. }
  761. if (req_format == format) {
  762. dmasound.soft.format = format;
  763. dmasound.soft.size = size;
  764. if (dmasound.minDev == SND_DEV_DSP) {
  765. dmasound.dsp.format = format;
  766. dmasound.dsp.size = size;
  767. }
  768. }
  769. return format;
  770. }
  771. #define AWACS_VOLUME_TO_MASK(x) (15 - ((((x) - 1) * 15) / 99))
  772. #define AWACS_MASK_TO_VOLUME(y) (100 - ((y) * 99 / 15))
  773. static int awacs_get_volume(int reg, int lshift)
  774. {
  775. int volume;
  776. volume = AWACS_MASK_TO_VOLUME((reg >> lshift) & 0xf);
  777. volume |= AWACS_MASK_TO_VOLUME(reg & 0xf) << 8;
  778. return volume;
  779. }
  780. static int awacs_volume_setter(int volume, int n, int mute, int lshift)
  781. {
  782. int r1, rn;
  783. if (mute && volume == 0) {
  784. r1 = awacs_reg[1] | mute;
  785. } else {
  786. r1 = awacs_reg[1] & ~mute;
  787. rn = awacs_reg[n] & ~(0xf | (0xf << lshift));
  788. rn |= ((AWACS_VOLUME_TO_MASK(volume & 0xff) & 0xf) << lshift);
  789. rn |= AWACS_VOLUME_TO_MASK((volume >> 8) & 0xff) & 0xf;
  790. awacs_reg[n] = rn;
  791. awacs_write((n << 12) | rn);
  792. volume = awacs_get_volume(rn, lshift);
  793. }
  794. if (r1 != awacs_reg[1]) {
  795. awacs_reg[1] = r1;
  796. awacs_write(r1 | MASK_ADDR1);
  797. }
  798. return volume;
  799. }
  800. static int PMacSetVolume(int volume)
  801. {
  802. printk(KERN_WARNING "Bogus call to PMacSetVolume !\n");
  803. return 0;
  804. }
  805. static void awacs_setup_for_beep(int speed)
  806. {
  807. out_le32(&awacs->control,
  808. (in_le32(&awacs->control) & ~0x1f00)
  809. | ((speed > 0 ? speed : awacs_rate_index) << 8));
  810. if (hw_can_byteswap && (dmasound.hard.format == AFMT_S16_LE) && speed == -1)
  811. out_le32(&awacs->byteswap, BS_VAL);
  812. else
  813. out_le32(&awacs->byteswap, 0);
  814. }
  815. /* CHECK: how much of this *really* needs IRQs masked? */
  816. static void __PMacPlay(void)
  817. {
  818. volatile struct dbdma_cmd *cp;
  819. int next_frg, count;
  820. count = 300 ; /* > two cycles at the lowest sample rate */
  821. /* what we want to send next */
  822. next_frg = (write_sq.front + write_sq.active) % write_sq.max_count;
  823. if (awacs_beep_state) {
  824. /* sound takes precedence over beeps */
  825. /* stop the dma channel */
  826. out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  827. while ( (in_le32(&awacs_txdma->status) & RUN) && count--)
  828. udelay(1);
  829. if (awacs)
  830. awacs_setup_for_beep(-1);
  831. out_le32(&awacs_txdma->cmdptr,
  832. virt_to_bus(&(awacs_tx_cmds[next_frg])));
  833. beep_playing = 0;
  834. awacs_beep_state = 0;
  835. }
  836. /* this won't allow more than two frags to be in the output queue at
  837. once. (or one, if the max frags is 2 - because count can't exceed
  838. 2 in that case)
  839. */
  840. while (write_sq.active < 2 && write_sq.active < write_sq.count) {
  841. count = (write_sq.count == write_sq.active + 1) ?
  842. write_sq.rear_size:write_sq.block_size ;
  843. if (count < write_sq.block_size) {
  844. if (!write_sq.syncing) /* last block not yet filled,*/
  845. break; /* and we're not syncing or POST-ed */
  846. else {
  847. /* pretend the block is full to force a new
  848. block to be started on the next write */
  849. write_sq.rear_size = write_sq.block_size ;
  850. write_sq.syncing &= ~2 ; /* clear POST */
  851. }
  852. }
  853. cp = &awacs_tx_cmds[next_frg];
  854. st_le16(&cp->req_count, count);
  855. st_le16(&cp->xfer_status, 0);
  856. st_le16(&cp->command, OUTPUT_MORE + INTR_ALWAYS);
  857. /* put a STOP at the end of the queue - but only if we have
  858. space for it. This means that, if we under-run and we only
  859. have two fragments, we might re-play sound from an existing
  860. queued frag. I guess the solution to that is not to set two
  861. frags if you are likely to under-run...
  862. */
  863. if (write_sq.count < write_sq.max_count) {
  864. if (++next_frg >= write_sq.max_count)
  865. next_frg = 0 ; /* wrap */
  866. /* if we get here then we've underrun so we will stop*/
  867. st_le16(&awacs_tx_cmds[next_frg].command, DBDMA_STOP);
  868. }
  869. /* set the dbdma controller going, if it is not already */
  870. if (write_sq.active == 0)
  871. out_le32(&awacs_txdma->cmdptr, virt_to_bus(cp));
  872. (void)in_le32(&awacs_txdma->status);
  873. out_le32(&awacs_txdma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  874. ++write_sq.active;
  875. }
  876. }
  877. static void PMacPlay(void)
  878. {
  879. LOCK();
  880. if (!awacs_sleeping) {
  881. unsigned long flags;
  882. spin_lock_irqsave(&dmasound.lock, flags);
  883. __PMacPlay();
  884. spin_unlock_irqrestore(&dmasound.lock, flags);
  885. }
  886. UNLOCK();
  887. }
  888. static void PMacRecord(void)
  889. {
  890. unsigned long flags;
  891. if (read_sq.active)
  892. return;
  893. spin_lock_irqsave(&dmasound.lock, flags);
  894. /* This is all we have to do......Just start it up.
  895. */
  896. out_le32(&awacs_rxdma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
  897. read_sq.active = 1;
  898. spin_unlock_irqrestore(&dmasound.lock, flags);
  899. }
  900. /* if the TX status comes up "DEAD" - reported on some Power Computing machines
  901. we need to re-start the dbdma - but from a different physical start address
  902. and with a different transfer length. It would get very messy to do this
  903. with the normal dbdma_cmd blocks - we would have to re-write the buffer start
  904. addresses each time. So, we will keep a single dbdma_cmd block which can be
  905. fiddled with.
  906. When DEAD status is first reported the content of the faulted dbdma block is
  907. copied into the emergency buffer and we note that the buffer is in use.
  908. we then bump the start physical address by the amount that was successfully
  909. output before it died.
  910. On any subsequent DEAD result we just do the bump-ups (we know that we are
  911. already using the emergency dbdma_cmd).
  912. CHECK: this just tries to "do it". It is possible that we should abandon
  913. xfers when the number of residual bytes gets below a certain value - I can
  914. see that this might cause a loop-forever if too small a transfer causes
  915. DEAD status. However this is a TODO for now - we'll see what gets reported.
  916. When we get a successful transfer result with the emergency buffer we just
  917. pretend that it completed using the original dmdma_cmd and carry on. The
  918. 'next_cmd' field will already point back to the original loop of blocks.
  919. */
  920. static irqreturn_t
  921. pmac_awacs_tx_intr(int irq, void *devid, struct pt_regs *regs)
  922. {
  923. int i = write_sq.front;
  924. int stat;
  925. int i_nowrap = write_sq.front;
  926. volatile struct dbdma_cmd *cp;
  927. /* != 0 when we are dealing with a DEAD xfer */
  928. static int emergency_in_use;
  929. spin_lock(&dmasound.lock);
  930. while (write_sq.active > 0) { /* we expect to have done something*/
  931. if (emergency_in_use) /* we are dealing with DEAD xfer */
  932. cp = emergency_dbdma_cmd ;
  933. else
  934. cp = &awacs_tx_cmds[i];
  935. stat = ld_le16(&cp->xfer_status);
  936. if (stat & DEAD) {
  937. unsigned short req, res ;
  938. unsigned int phy ;
  939. #ifdef DEBUG_DMASOUND
  940. printk("dmasound_pmac: tx-irq: xfer died - patching it up...\n") ;
  941. #endif
  942. /* to clear DEAD status we must first clear RUN
  943. set it to quiescent to be on the safe side */
  944. (void)in_le32(&awacs_txdma->status);
  945. out_le32(&awacs_txdma->control,
  946. (RUN|PAUSE|FLUSH|WAKE) << 16);
  947. write_sq.died++ ;
  948. if (!emergency_in_use) { /* new problem */
  949. memcpy((void *)emergency_dbdma_cmd, (void *)cp,
  950. sizeof(struct dbdma_cmd));
  951. emergency_in_use = 1;
  952. cp = emergency_dbdma_cmd;
  953. }
  954. /* now bump the values to reflect the amount
  955. we haven't yet shifted */
  956. req = ld_le16(&cp->req_count);
  957. res = ld_le16(&cp->res_count);
  958. phy = ld_le32(&cp->phy_addr);
  959. phy += (req - res);
  960. st_le16(&cp->req_count, res);
  961. st_le16(&cp->res_count, 0);
  962. st_le16(&cp->xfer_status, 0);
  963. st_le32(&cp->phy_addr, phy);
  964. st_le32(&cp->cmd_dep, virt_to_bus(&awacs_tx_cmds[(i+1)%write_sq.max_count]));
  965. st_le16(&cp->command, OUTPUT_MORE | BR_ALWAYS | INTR_ALWAYS);
  966. /* point at our patched up command block */
  967. out_le32(&awacs_txdma->cmdptr, virt_to_bus(cp));
  968. /* we must re-start the controller */
  969. (void)in_le32(&awacs_txdma->status);
  970. /* should complete clearing the DEAD status */
  971. out_le32(&awacs_txdma->control,
  972. ((RUN|WAKE) << 16) + (RUN|WAKE));
  973. break; /* this block is still going */
  974. }
  975. if ((stat & ACTIVE) == 0)
  976. break; /* this frame is still going */
  977. if (emergency_in_use)
  978. emergency_in_use = 0 ; /* done that */
  979. --write_sq.count;
  980. --write_sq.active;
  981. i_nowrap++;
  982. if (++i >= write_sq.max_count)
  983. i = 0;
  984. }
  985. /* if we stopped and we were not sync-ing - then we under-ran */
  986. if( write_sq.syncing == 0 ){
  987. stat = in_le32(&awacs_txdma->status) ;
  988. /* we hit the dbdma_stop */
  989. if( (stat & ACTIVE) == 0 ) write_sq.xruns++ ;
  990. }
  991. /* if we used some data up then wake the writer to supply some more*/
  992. if (i_nowrap != write_sq.front)
  993. WAKE_UP(write_sq.action_queue);
  994. write_sq.front = i;
  995. /* but make sure we funnel what we've already got */\
  996. if (!awacs_sleeping)
  997. __PMacPlay();
  998. /* make the wake-on-empty conditional on syncing */
  999. if (!write_sq.active && (write_sq.syncing & 1))
  1000. WAKE_UP(write_sq.sync_queue); /* any time we're empty */
  1001. spin_unlock(&dmasound.lock);
  1002. return IRQ_HANDLED;
  1003. }
  1004. static irqreturn_t
  1005. pmac_awacs_rx_intr(int irq, void *devid, struct pt_regs *regs)
  1006. {
  1007. int stat ;
  1008. /* For some reason on my PowerBook G3, I get one interrupt
  1009. * when the interrupt vector is installed (like something is
  1010. * pending). This happens before the dbdma is initialized by
  1011. * us, so I just check the command pointer and if it is zero,
  1012. * just blow it off.
  1013. */
  1014. if (in_le32(&awacs_rxdma->cmdptr) == 0)
  1015. return IRQ_HANDLED;
  1016. /* We also want to blow 'em off when shutting down.
  1017. */
  1018. if (read_sq.active == 0)
  1019. return IRQ_HANDLED;
  1020. spin_lock(&dmasound.lock);
  1021. /* Check multiple buffers in case we were held off from
  1022. * interrupt processing for a long time. Geeze, I really hope
  1023. * this doesn't happen.
  1024. */
  1025. while ((stat=awacs_rx_cmds[read_sq.rear].xfer_status)) {
  1026. /* if we got a "DEAD" status then just log it for now.
  1027. and try to restart dma.
  1028. TODO: figure out how best to fix it up
  1029. */
  1030. if (stat & DEAD){
  1031. #ifdef DEBUG_DMASOUND
  1032. printk("dmasound_pmac: rx-irq: DIED - attempting resurection\n");
  1033. #endif
  1034. /* to clear DEAD status we must first clear RUN
  1035. set it to quiescent to be on the safe side */
  1036. (void)in_le32(&awacs_txdma->status);
  1037. out_le32(&awacs_txdma->control,
  1038. (RUN|PAUSE|FLUSH|WAKE) << 16);
  1039. awacs_rx_cmds[read_sq.rear].xfer_status = 0;
  1040. awacs_rx_cmds[read_sq.rear].res_count = 0;
  1041. read_sq.died++ ;
  1042. (void)in_le32(&awacs_txdma->status);
  1043. /* re-start the same block */
  1044. out_le32(&awacs_rxdma->cmdptr,
  1045. virt_to_bus(&awacs_rx_cmds[read_sq.rear]));
  1046. /* we must re-start the controller */
  1047. (void)in_le32(&awacs_rxdma->status);
  1048. /* should complete clearing the DEAD status */
  1049. out_le32(&awacs_rxdma->control,
  1050. ((RUN|WAKE) << 16) + (RUN|WAKE));
  1051. spin_unlock(&dmasound.lock);
  1052. return IRQ_HANDLED; /* try this block again */
  1053. }
  1054. /* Clear status and move on to next buffer.
  1055. */
  1056. awacs_rx_cmds[read_sq.rear].xfer_status = 0;
  1057. read_sq.rear++;
  1058. /* Wrap the buffer ring.
  1059. */
  1060. if (read_sq.rear >= read_sq.max_active)
  1061. read_sq.rear = 0;
  1062. /* If we have caught up to the front buffer, bump it.
  1063. * This will cause weird (but not fatal) results if the
  1064. * read loop is currently using this buffer. The user is
  1065. * behind in this case anyway, so weird things are going
  1066. * to happen.
  1067. */
  1068. if (read_sq.rear == read_sq.front) {
  1069. read_sq.front++;
  1070. read_sq.xruns++ ; /* we overan */
  1071. if (read_sq.front >= read_sq.max_active)
  1072. read_sq.front = 0;
  1073. }
  1074. }
  1075. WAKE_UP(read_sq.action_queue);
  1076. spin_unlock(&dmasound.lock);
  1077. return IRQ_HANDLED;
  1078. }
  1079. static irqreturn_t
  1080. pmac_awacs_intr(int irq, void *devid, struct pt_regs *regs)
  1081. {
  1082. int ctrl;
  1083. int status;
  1084. int r1;
  1085. spin_lock(&dmasound.lock);
  1086. ctrl = in_le32(&awacs->control);
  1087. status = in_le32(&awacs->codec_stat);
  1088. if (ctrl & MASK_PORTCHG) {
  1089. /* tested on Screamer, should work on others too */
  1090. if (awacs_revision == AWACS_SCREAMER) {
  1091. if (((status & MASK_HDPCONN) >> 3) && (hdp_connected == 0)) {
  1092. hdp_connected = 1;
  1093. r1 = awacs_reg[1] | MASK_SPKMUTE;
  1094. awacs_reg[1] = r1;
  1095. awacs_write(r1 | MASK_ADDR_MUTE);
  1096. } else if (((status & MASK_HDPCONN) >> 3 == 0) && (hdp_connected == 1)) {
  1097. hdp_connected = 0;
  1098. r1 = awacs_reg[1] & ~MASK_SPKMUTE;
  1099. awacs_reg[1] = r1;
  1100. awacs_write(r1 | MASK_ADDR_MUTE);
  1101. }
  1102. }
  1103. }
  1104. if (ctrl & MASK_CNTLERR) {
  1105. int err = (in_le32(&awacs->codec_stat) & MASK_ERRCODE) >> 16;
  1106. /* CHECK: we just swallow burgundy errors at the moment..*/
  1107. if (err != 0 && awacs_revision != AWACS_BURGUNDY)
  1108. printk(KERN_ERR "dmasound_pmac: error %x\n", err);
  1109. }
  1110. /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
  1111. out_le32(&awacs->control, ctrl);
  1112. spin_unlock(&dmasound.lock);
  1113. return IRQ_HANDLED;
  1114. }
  1115. static void
  1116. awacs_write(int val)
  1117. {
  1118. int count = 300 ;
  1119. if (awacs_revision >= AWACS_DACA || !awacs)
  1120. return ;
  1121. while ((in_le32(&awacs->codec_ctrl) & MASK_NEWECMD) && count--)
  1122. udelay(1) ; /* timeout is > 2 samples at lowest rate */
  1123. out_le32(&awacs->codec_ctrl, val | (awacs_subframe << 22));
  1124. (void)in_le32(&awacs->byteswap);
  1125. }
  1126. /* this is called when the beep timer expires... it will be called even
  1127. if the beep has been overidden by other sound output.
  1128. */
  1129. static void awacs_nosound(unsigned long xx)
  1130. {
  1131. unsigned long flags;
  1132. int count = 600 ; /* > four samples at lowest rate */
  1133. spin_lock_irqsave(&dmasound.lock, flags);
  1134. if (beep_playing) {
  1135. st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
  1136. out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  1137. while ((in_le32(&awacs_txdma->status) & RUN) && count--)
  1138. udelay(1);
  1139. if (awacs)
  1140. awacs_setup_for_beep(-1);
  1141. beep_playing = 0;
  1142. }
  1143. spin_unlock_irqrestore(&dmasound.lock, flags);
  1144. }
  1145. /*
  1146. * We generate the beep with a single dbdma command that loops a buffer
  1147. * forever - without generating interrupts.
  1148. *
  1149. * So, to stop it you have to stop dma output as per awacs_nosound.
  1150. */
  1151. static int awacs_beep_event(struct input_dev *dev, unsigned int type,
  1152. unsigned int code, int hz)
  1153. {
  1154. unsigned long flags;
  1155. int beep_speed = 0;
  1156. int srate;
  1157. int period, ncycles, nsamples;
  1158. int i, j, f;
  1159. short *p;
  1160. static int beep_hz_cache;
  1161. static int beep_nsamples_cache;
  1162. static int beep_volume_cache;
  1163. if (type != EV_SND)
  1164. return -1;
  1165. switch (code) {
  1166. case SND_BELL:
  1167. if (hz)
  1168. hz = 1000;
  1169. break;
  1170. case SND_TONE:
  1171. break;
  1172. default:
  1173. return -1;
  1174. }
  1175. if (beep_buf == NULL)
  1176. return -1;
  1177. /* quick-hack fix for DACA, Burgundy & Tumbler */
  1178. if (awacs_revision >= AWACS_DACA){
  1179. srate = 44100 ;
  1180. } else {
  1181. for (i = 0; i < 8 && awacs_freqs[i] >= BEEP_SRATE; ++i)
  1182. if (awacs_freqs_ok[i])
  1183. beep_speed = i;
  1184. srate = awacs_freqs[beep_speed];
  1185. }
  1186. if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
  1187. /* cancel beep currently playing */
  1188. awacs_nosound(0);
  1189. return 0;
  1190. }
  1191. spin_lock_irqsave(&dmasound.lock, flags);
  1192. if (beep_playing || write_sq.active || beep_buf == NULL) {
  1193. spin_unlock_irqrestore(&dmasound.lock, flags);
  1194. return -1; /* too hard, sorry :-( */
  1195. }
  1196. beep_playing = 1;
  1197. st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
  1198. spin_unlock_irqrestore(&dmasound.lock, flags);
  1199. if (hz == beep_hz_cache && beep_vol == beep_volume_cache) {
  1200. nsamples = beep_nsamples_cache;
  1201. } else {
  1202. period = srate * 256 / hz; /* fixed point */
  1203. ncycles = BEEP_BUFLEN * 256 / period;
  1204. nsamples = (period * ncycles) >> 8;
  1205. f = ncycles * 65536 / nsamples;
  1206. j = 0;
  1207. p = beep_buf;
  1208. for (i = 0; i < nsamples; ++i, p += 2) {
  1209. p[0] = p[1] = beep_wform[j >> 8] * beep_vol;
  1210. j = (j + f) & 0xffff;
  1211. }
  1212. beep_hz_cache = hz;
  1213. beep_volume_cache = beep_vol;
  1214. beep_nsamples_cache = nsamples;
  1215. }
  1216. st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
  1217. st_le16(&beep_dbdma_cmd->xfer_status, 0);
  1218. st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
  1219. st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
  1220. awacs_beep_state = 1;
  1221. spin_lock_irqsave(&dmasound.lock, flags);
  1222. if (beep_playing) { /* i.e. haven't been terminated already */
  1223. int count = 300 ;
  1224. out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
  1225. while ((in_le32(&awacs_txdma->status) & RUN) && count--)
  1226. udelay(1); /* timeout > 2 samples at lowest rate*/
  1227. if (awacs)
  1228. awacs_setup_for_beep(beep_speed);
  1229. out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
  1230. (void)in_le32(&awacs_txdma->status);
  1231. out_le32(&awacs_txdma->control, RUN | (RUN << 16));
  1232. }
  1233. spin_unlock_irqrestore(&dmasound.lock, flags);
  1234. return 0;
  1235. }
  1236. /* used in init and for wake-up */
  1237. static void
  1238. load_awacs(void)
  1239. {
  1240. awacs_write(awacs_reg[0] + MASK_ADDR0);
  1241. awacs_write(awacs_reg[1] + MASK_ADDR1);
  1242. awacs_write(awacs_reg[2] + MASK_ADDR2);
  1243. awacs_write(awacs_reg[4] + MASK_ADDR4);
  1244. if (awacs_revision == AWACS_SCREAMER) {
  1245. awacs_write(awacs_reg[5] + MASK_ADDR5);
  1246. msleep(100);
  1247. awacs_write(awacs_reg[6] + MASK_ADDR6);
  1248. msleep(2);
  1249. awacs_write(awacs_reg[1] + MASK_ADDR1);
  1250. awacs_write(awacs_reg[7] + MASK_ADDR7);
  1251. }
  1252. if (awacs) {
  1253. if (hw_can_byteswap && (dmasound.hard.format == AFMT_S16_LE))
  1254. out_le32(&awacs->byteswap, BS_VAL);
  1255. else
  1256. out_le32(&awacs->byteswap, 0);
  1257. }
  1258. }
  1259. #ifdef CONFIG_PM
  1260. /*
  1261. * Save state when going to sleep, restore it afterwards.
  1262. */
  1263. /* FIXME: sort out disabling/re-enabling of read stuff as well */
  1264. static int awacs_sleep_notify(struct pmu_sleep_notifier *self, int when)
  1265. {
  1266. unsigned long flags;
  1267. switch (when) {
  1268. case PBOOK_SLEEP_NOW:
  1269. LOCK();
  1270. awacs_sleeping = 1;
  1271. /* Tell the rest of the driver we are now going to sleep */
  1272. mb();
  1273. if (awacs_revision == AWACS_SCREAMER ||
  1274. awacs_revision == AWACS_AWACS) {
  1275. awacs_reg1_save = awacs_reg[1];
  1276. awacs_reg[1] |= MASK_AMUTE | MASK_CMUTE;
  1277. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  1278. }
  1279. PMacSilence();
  1280. /* stop rx - if going - a bit of a daft user... but */
  1281. out_le32(&awacs_rxdma->control, (RUN|WAKE|FLUSH << 16));
  1282. /* deny interrupts */
  1283. if (awacs)
  1284. disable_irq(awacs_irq);
  1285. disable_irq(awacs_tx_irq);
  1286. disable_irq(awacs_rx_irq);
  1287. /* Chip specific sleep code */
  1288. switch (awacs_revision) {
  1289. case AWACS_TUMBLER:
  1290. case AWACS_SNAPPER:
  1291. write_audio_gpio(gpio_headphone_mute, gpio_headphone_mute_pol);
  1292. write_audio_gpio(gpio_amp_mute, gpio_amp_mute_pol);
  1293. tas_enter_sleep();
  1294. write_audio_gpio(gpio_audio_reset, gpio_audio_reset_pol);
  1295. break ;
  1296. case AWACS_DACA:
  1297. daca_enter_sleep();
  1298. break ;
  1299. case AWACS_BURGUNDY:
  1300. break ;
  1301. case AWACS_SCREAMER:
  1302. case AWACS_AWACS:
  1303. default:
  1304. out_le32(&awacs->control, 0x11) ;
  1305. break ;
  1306. }
  1307. /* Disable sound clock */
  1308. pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, awacs_node, 0, 0);
  1309. /* According to Darwin, we do that after turning off the sound
  1310. * chip clock. All this will have to be cleaned up once we properly
  1311. * parse the OF sound-objects
  1312. */
  1313. if ((machine_is_compatible("PowerBook3,1") ||
  1314. machine_is_compatible("PowerBook3,2")) && awacs) {
  1315. awacs_reg[1] |= MASK_PAROUT0 | MASK_PAROUT1;
  1316. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  1317. msleep(200);
  1318. }
  1319. break;
  1320. case PBOOK_WAKE:
  1321. /* Enable sound clock */
  1322. pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, awacs_node, 0, 1);
  1323. if ((machine_is_compatible("PowerBook3,1") ||
  1324. machine_is_compatible("PowerBook3,2")) && awacs) {
  1325. msleep(100);
  1326. awacs_reg[1] &= ~(MASK_PAROUT0 | MASK_PAROUT1);
  1327. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  1328. msleep(300);
  1329. } else
  1330. msleep(1000);
  1331. /* restore settings */
  1332. switch (awacs_revision) {
  1333. case AWACS_TUMBLER:
  1334. case AWACS_SNAPPER:
  1335. write_audio_gpio(gpio_headphone_mute, gpio_headphone_mute_pol);
  1336. write_audio_gpio(gpio_amp_mute, gpio_amp_mute_pol);
  1337. write_audio_gpio(gpio_audio_reset, gpio_audio_reset_pol);
  1338. msleep(100);
  1339. write_audio_gpio(gpio_audio_reset, !gpio_audio_reset_pol);
  1340. msleep(150);
  1341. tas_leave_sleep(); /* Stub for now */
  1342. headphone_intr(0,NULL,NULL);
  1343. break;
  1344. case AWACS_DACA:
  1345. msleep(10); /* Check this !!! */
  1346. daca_leave_sleep();
  1347. break ; /* dont know how yet */
  1348. case AWACS_BURGUNDY:
  1349. break ;
  1350. case AWACS_SCREAMER:
  1351. case AWACS_AWACS:
  1352. default:
  1353. load_awacs() ;
  1354. break ;
  1355. }
  1356. /* Recalibrate chip */
  1357. if (awacs_revision == AWACS_SCREAMER && awacs)
  1358. awacs_recalibrate();
  1359. /* Make sure dma is stopped */
  1360. PMacSilence();
  1361. if (awacs)
  1362. enable_irq(awacs_irq);
  1363. enable_irq(awacs_tx_irq);
  1364. enable_irq(awacs_rx_irq);
  1365. if (awacs) {
  1366. /* OK, allow ints back again */
  1367. out_le32(&awacs->control, MASK_IEPC
  1368. | (awacs_rate_index << 8) | 0x11
  1369. | (awacs_revision < AWACS_DACA ? MASK_IEE: 0));
  1370. }
  1371. if (macio_base && is_pbook_g3) {
  1372. /* FIXME: should restore the setup we had...*/
  1373. out_8(macio_base + 0x37, 3);
  1374. } else if (is_pbook_3X00) {
  1375. in_8(latch_base + 0x190);
  1376. }
  1377. /* Remove mute */
  1378. if (awacs_revision == AWACS_SCREAMER ||
  1379. awacs_revision == AWACS_AWACS) {
  1380. awacs_reg[1] = awacs_reg1_save;
  1381. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  1382. }
  1383. awacs_sleeping = 0;
  1384. /* Resume pending sounds. */
  1385. /* we don't try to restart input... */
  1386. spin_lock_irqsave(&dmasound.lock, flags);
  1387. __PMacPlay();
  1388. spin_unlock_irqrestore(&dmasound.lock, flags);
  1389. UNLOCK();
  1390. }
  1391. return PBOOK_SLEEP_OK;
  1392. }
  1393. #endif /* CONFIG_PM */
  1394. /* All the burgundy functions: */
  1395. /* Waits for busy flag to clear */
  1396. static inline void
  1397. awacs_burgundy_busy_wait(void)
  1398. {
  1399. int count = 50; /* > 2 samples at 44k1 */
  1400. while ((in_le32(&awacs->codec_ctrl) & MASK_NEWECMD) && count--)
  1401. udelay(1) ;
  1402. }
  1403. static inline void
  1404. awacs_burgundy_extend_wait(void)
  1405. {
  1406. int count = 50 ; /* > 2 samples at 44k1 */
  1407. while ((!(in_le32(&awacs->codec_stat) & MASK_EXTEND)) && count--)
  1408. udelay(1) ;
  1409. count = 50;
  1410. while ((in_le32(&awacs->codec_stat) & MASK_EXTEND) && count--)
  1411. udelay(1);
  1412. }
  1413. static void
  1414. awacs_burgundy_wcw(unsigned addr, unsigned val)
  1415. {
  1416. out_le32(&awacs->codec_ctrl, addr + 0x200c00 + (val & 0xff));
  1417. awacs_burgundy_busy_wait();
  1418. out_le32(&awacs->codec_ctrl, addr + 0x200d00 +((val>>8) & 0xff));
  1419. awacs_burgundy_busy_wait();
  1420. out_le32(&awacs->codec_ctrl, addr + 0x200e00 +((val>>16) & 0xff));
  1421. awacs_burgundy_busy_wait();
  1422. out_le32(&awacs->codec_ctrl, addr + 0x200f00 +((val>>24) & 0xff));
  1423. awacs_burgundy_busy_wait();
  1424. }
  1425. static unsigned
  1426. awacs_burgundy_rcw(unsigned addr)
  1427. {
  1428. unsigned val = 0;
  1429. unsigned long flags;
  1430. /* should have timeouts here */
  1431. spin_lock_irqsave(&dmasound.lock, flags);
  1432. out_le32(&awacs->codec_ctrl, addr + 0x100000);
  1433. awacs_burgundy_busy_wait();
  1434. awacs_burgundy_extend_wait();
  1435. val += (in_le32(&awacs->codec_stat) >> 4) & 0xff;
  1436. out_le32(&awacs->codec_ctrl, addr + 0x100100);
  1437. awacs_burgundy_busy_wait();
  1438. awacs_burgundy_extend_wait();
  1439. val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<8;
  1440. out_le32(&awacs->codec_ctrl, addr + 0x100200);
  1441. awacs_burgundy_busy_wait();
  1442. awacs_burgundy_extend_wait();
  1443. val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<16;
  1444. out_le32(&awacs->codec_ctrl, addr + 0x100300);
  1445. awacs_burgundy_busy_wait();
  1446. awacs_burgundy_extend_wait();
  1447. val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<24;
  1448. spin_unlock_irqrestore(&dmasound.lock, flags);
  1449. return val;
  1450. }
  1451. static void
  1452. awacs_burgundy_wcb(unsigned addr, unsigned val)
  1453. {
  1454. out_le32(&awacs->codec_ctrl, addr + 0x300000 + (val & 0xff));
  1455. awacs_burgundy_busy_wait();
  1456. }
  1457. static unsigned
  1458. awacs_burgundy_rcb(unsigned addr)
  1459. {
  1460. unsigned val = 0;
  1461. unsigned long flags;
  1462. /* should have timeouts here */
  1463. spin_lock_irqsave(&dmasound.lock, flags);
  1464. out_le32(&awacs->codec_ctrl, addr + 0x100000);
  1465. awacs_burgundy_busy_wait();
  1466. awacs_burgundy_extend_wait();
  1467. val += (in_le32(&awacs->codec_stat) >> 4) & 0xff;
  1468. spin_unlock_irqrestore(&dmasound.lock, flags);
  1469. return val;
  1470. }
  1471. static int
  1472. awacs_burgundy_check(void)
  1473. {
  1474. /* Checks to see the chip is alive and kicking */
  1475. int error = in_le32(&awacs->codec_ctrl) & MASK_ERRCODE;
  1476. return error == 0xf0000;
  1477. }
  1478. static int
  1479. awacs_burgundy_init(void)
  1480. {
  1481. if (awacs_burgundy_check()) {
  1482. printk(KERN_WARNING "dmasound_pmac: burgundy not working :-(\n");
  1483. return 1;
  1484. }
  1485. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_OUTPUTENABLES,
  1486. DEF_BURGUNDY_OUTPUTENABLES);
  1487. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  1488. DEF_BURGUNDY_MORE_OUTPUTENABLES);
  1489. awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_OUTPUTSELECTS,
  1490. DEF_BURGUNDY_OUTPUTSELECTS);
  1491. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_INPSEL21,
  1492. DEF_BURGUNDY_INPSEL21);
  1493. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_INPSEL3,
  1494. DEF_BURGUNDY_INPSEL3);
  1495. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINCD,
  1496. DEF_BURGUNDY_GAINCD);
  1497. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINLINE,
  1498. DEF_BURGUNDY_GAINLINE);
  1499. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINMIC,
  1500. DEF_BURGUNDY_GAINMIC);
  1501. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINMODEM,
  1502. DEF_BURGUNDY_GAINMODEM);
  1503. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER,
  1504. DEF_BURGUNDY_ATTENSPEAKER);
  1505. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENLINEOUT,
  1506. DEF_BURGUNDY_ATTENLINEOUT);
  1507. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENHP,
  1508. DEF_BURGUNDY_ATTENHP);
  1509. awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_MASTER_VOLUME,
  1510. DEF_BURGUNDY_MASTER_VOLUME);
  1511. awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLCD,
  1512. DEF_BURGUNDY_VOLCD);
  1513. awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLLINE,
  1514. DEF_BURGUNDY_VOLLINE);
  1515. awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLMIC,
  1516. DEF_BURGUNDY_VOLMIC);
  1517. return 0;
  1518. }
  1519. static void
  1520. awacs_burgundy_write_volume(unsigned address, int volume)
  1521. {
  1522. int hardvolume,lvolume,rvolume;
  1523. lvolume = (volume & 0xff) ? (volume & 0xff) + 155 : 0;
  1524. rvolume = ((volume >>8)&0xff) ? ((volume >> 8)&0xff ) + 155 : 0;
  1525. hardvolume = lvolume + (rvolume << 16);
  1526. awacs_burgundy_wcw(address, hardvolume);
  1527. }
  1528. static int
  1529. awacs_burgundy_read_volume(unsigned address)
  1530. {
  1531. int softvolume,wvolume;
  1532. wvolume = awacs_burgundy_rcw(address);
  1533. softvolume = (wvolume & 0xff) - 155;
  1534. softvolume += (((wvolume >> 16) & 0xff) - 155)<<8;
  1535. return softvolume > 0 ? softvolume : 0;
  1536. }
  1537. static int
  1538. awacs_burgundy_read_mvolume(unsigned address)
  1539. {
  1540. int lvolume,rvolume,wvolume;
  1541. wvolume = awacs_burgundy_rcw(address);
  1542. wvolume &= 0xffff;
  1543. rvolume = (wvolume & 0xff) - 155;
  1544. lvolume = ((wvolume & 0xff00)>>8) - 155;
  1545. return lvolume + (rvolume << 8);
  1546. }
  1547. static void
  1548. awacs_burgundy_write_mvolume(unsigned address, int volume)
  1549. {
  1550. int lvolume,rvolume,hardvolume;
  1551. lvolume = (volume &0xff) ? (volume & 0xff) + 155 :0;
  1552. rvolume = ((volume >>8) & 0xff) ? (volume >> 8) + 155 :0;
  1553. hardvolume = lvolume + (rvolume << 8);
  1554. hardvolume += (hardvolume << 16);
  1555. awacs_burgundy_wcw(address, hardvolume);
  1556. }
  1557. /* End burgundy functions */
  1558. /* Set up output volumes on machines with the 'perch/whisper' extension card.
  1559. * this has an SGS i2c chip (7433) which is accessed using the cuda.
  1560. *
  1561. * TODO: split this out and make use of the other parts of the SGS chip to
  1562. * do Bass, Treble etc.
  1563. */
  1564. static void
  1565. awacs_enable_amp(int spkr_vol)
  1566. {
  1567. #ifdef CONFIG_ADB_CUDA
  1568. struct adb_request req;
  1569. if (sys_ctrler != SYS_CTRLER_CUDA)
  1570. return;
  1571. /* turn on headphones */
  1572. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
  1573. 0x8a, 4, 0);
  1574. while (!req.complete) cuda_poll();
  1575. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
  1576. 0x8a, 6, 0);
  1577. while (!req.complete) cuda_poll();
  1578. /* turn on speaker */
  1579. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
  1580. 0x8a, 3, (100 - (spkr_vol & 0xff)) * 32 / 100);
  1581. while (!req.complete) cuda_poll();
  1582. cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
  1583. 0x8a, 5, (100 - ((spkr_vol >> 8) & 0xff)) * 32 / 100);
  1584. while (!req.complete) cuda_poll();
  1585. cuda_request(&req, NULL, 5, CUDA_PACKET,
  1586. CUDA_GET_SET_IIC, 0x8a, 1, 0x29);
  1587. while (!req.complete) cuda_poll();
  1588. #endif /* CONFIG_ADB_CUDA */
  1589. }
  1590. /*** Mid level stuff *********************************************************/
  1591. /*
  1592. * /dev/mixer abstraction
  1593. */
  1594. static void do_line_lev(int data)
  1595. {
  1596. line_lev = data ;
  1597. awacs_reg[0] &= ~MASK_MUX_AUDIN;
  1598. if ((data & 0xff) >= 50)
  1599. awacs_reg[0] |= MASK_MUX_AUDIN;
  1600. awacs_write(MASK_ADDR0 | awacs_reg[0]);
  1601. }
  1602. static void do_ip_gain(int data)
  1603. {
  1604. ip_gain = data ;
  1605. data &= 0xff;
  1606. awacs_reg[0] &= ~MASK_GAINLINE;
  1607. if (awacs_revision == AWACS_SCREAMER) {
  1608. awacs_reg[6] &= ~MASK_MIC_BOOST ;
  1609. if (data >= 33) {
  1610. awacs_reg[0] |= MASK_GAINLINE;
  1611. if( data >= 66)
  1612. awacs_reg[6] |= MASK_MIC_BOOST ;
  1613. }
  1614. awacs_write(MASK_ADDR6 | awacs_reg[6]) ;
  1615. } else {
  1616. if (data >= 50)
  1617. awacs_reg[0] |= MASK_GAINLINE;
  1618. }
  1619. awacs_write(MASK_ADDR0 | awacs_reg[0]);
  1620. }
  1621. static void do_mic_lev(int data)
  1622. {
  1623. mic_lev = data ;
  1624. data &= 0xff;
  1625. awacs_reg[0] &= ~MASK_MUX_MIC;
  1626. if (data >= 50)
  1627. awacs_reg[0] |= MASK_MUX_MIC;
  1628. awacs_write(MASK_ADDR0 | awacs_reg[0]);
  1629. }
  1630. static void do_cd_lev(int data)
  1631. {
  1632. cd_lev = data ;
  1633. awacs_reg[0] &= ~MASK_MUX_CD;
  1634. if ((data & 0xff) >= 50)
  1635. awacs_reg[0] |= MASK_MUX_CD;
  1636. awacs_write(MASK_ADDR0 | awacs_reg[0]);
  1637. }
  1638. static void do_rec_lev(int data)
  1639. {
  1640. int left, right ;
  1641. rec_lev = data ;
  1642. /* need to fudge this to use the volume setter routine */
  1643. left = 100 - (data & 0xff) ; if( left < 0 ) left = 0 ;
  1644. right = 100 - ((data >> 8) & 0xff) ; if( right < 0 ) right = 0 ;
  1645. left |= (right << 8 );
  1646. left = awacs_volume_setter(left, 0, 0, 4);
  1647. }
  1648. static void do_passthru_vol(int data)
  1649. {
  1650. passthru_vol = data ;
  1651. awacs_reg[1] &= ~MASK_LOOPTHRU;
  1652. if (awacs_revision == AWACS_SCREAMER) {
  1653. if( data ) { /* switch it on for non-zero */
  1654. awacs_reg[1] |= MASK_LOOPTHRU;
  1655. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  1656. }
  1657. data = awacs_volume_setter(data, 5, 0, 6) ;
  1658. } else {
  1659. if ((data & 0xff) >= 50)
  1660. awacs_reg[1] |= MASK_LOOPTHRU;
  1661. awacs_write(MASK_ADDR1 | awacs_reg[1]);
  1662. data = (awacs_reg[1] & MASK_LOOPTHRU)? 100: 0;
  1663. }
  1664. }
  1665. static int awacs_mixer_ioctl(u_int cmd, u_long arg)
  1666. {
  1667. int data;
  1668. int rc;
  1669. switch (cmd) {
  1670. case SOUND_MIXER_READ_CAPS:
  1671. /* say we will allow multiple inputs? prob. wrong
  1672. so I'm switching it to single */
  1673. return IOCTL_OUT(arg, 1);
  1674. case SOUND_MIXER_READ_DEVMASK:
  1675. data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
  1676. | SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD
  1677. | SOUND_MASK_IGAIN | SOUND_MASK_RECLEV
  1678. | SOUND_MASK_ALTPCM
  1679. | SOUND_MASK_MONITOR;
  1680. rc = IOCTL_OUT(arg, data);
  1681. break;
  1682. case SOUND_MIXER_READ_RECMASK:
  1683. data = SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD;
  1684. rc = IOCTL_OUT(arg, data);
  1685. break;
  1686. case SOUND_MIXER_READ_RECSRC:
  1687. data = 0;
  1688. if (awacs_reg[0] & MASK_MUX_AUDIN)
  1689. data |= SOUND_MASK_LINE;
  1690. if (awacs_reg[0] & MASK_MUX_MIC)
  1691. data |= SOUND_MASK_MIC;
  1692. if (awacs_reg[0] & MASK_MUX_CD)
  1693. data |= SOUND_MASK_CD;
  1694. rc = IOCTL_OUT(arg, data);
  1695. break;
  1696. case SOUND_MIXER_WRITE_RECSRC:
  1697. IOCTL_IN(arg, data);
  1698. data &= (SOUND_MASK_LINE | SOUND_MASK_MIC | SOUND_MASK_CD);
  1699. awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
  1700. | MASK_MUX_AUDIN);
  1701. if (data & SOUND_MASK_LINE)
  1702. awacs_reg[0] |= MASK_MUX_AUDIN;
  1703. if (data & SOUND_MASK_MIC)
  1704. awacs_reg[0] |= MASK_MUX_MIC;
  1705. if (data & SOUND_MASK_CD)
  1706. awacs_reg[0] |= MASK_MUX_CD;
  1707. awacs_write(awacs_reg[0] | MASK_ADDR0);
  1708. rc = IOCTL_OUT(arg, data);
  1709. break;
  1710. case SOUND_MIXER_READ_STEREODEVS:
  1711. data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER| SOUND_MASK_RECLEV ;
  1712. if (awacs_revision == AWACS_SCREAMER)
  1713. data |= SOUND_MASK_MONITOR ;
  1714. rc = IOCTL_OUT(arg, data);
  1715. break;
  1716. case SOUND_MIXER_WRITE_VOLUME:
  1717. IOCTL_IN(arg, data);
  1718. line_vol = data ;
  1719. awacs_volume_setter(data, 2, 0, 6);
  1720. /* fall through */
  1721. case SOUND_MIXER_READ_VOLUME:
  1722. rc = IOCTL_OUT(arg, line_vol);
  1723. break;
  1724. case SOUND_MIXER_WRITE_SPEAKER:
  1725. IOCTL_IN(arg, data);
  1726. spk_vol = data ;
  1727. if (has_perch)
  1728. awacs_enable_amp(data);
  1729. else
  1730. (void)awacs_volume_setter(data, 4, MASK_CMUTE, 6);
  1731. /* fall though */
  1732. case SOUND_MIXER_READ_SPEAKER:
  1733. rc = IOCTL_OUT(arg, spk_vol);
  1734. break;
  1735. case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
  1736. IOCTL_IN(arg, data);
  1737. beep_vol = data & 0xff;
  1738. /* fall through */
  1739. case SOUND_MIXER_READ_ALTPCM:
  1740. rc = IOCTL_OUT(arg, beep_vol);
  1741. break;
  1742. case SOUND_MIXER_WRITE_LINE:
  1743. IOCTL_IN(arg, data);
  1744. do_line_lev(data) ;
  1745. /* fall through */
  1746. case SOUND_MIXER_READ_LINE:
  1747. rc = IOCTL_OUT(arg, line_lev);
  1748. break;
  1749. case SOUND_MIXER_WRITE_IGAIN:
  1750. IOCTL_IN(arg, data);
  1751. do_ip_gain(data) ;
  1752. /* fall through */
  1753. case SOUND_MIXER_READ_IGAIN:
  1754. rc = IOCTL_OUT(arg, ip_gain);
  1755. break;
  1756. case SOUND_MIXER_WRITE_MIC:
  1757. IOCTL_IN(arg, data);
  1758. do_mic_lev(data);
  1759. /* fall through */
  1760. case SOUND_MIXER_READ_MIC:
  1761. rc = IOCTL_OUT(arg, mic_lev);
  1762. break;
  1763. case SOUND_MIXER_WRITE_CD:
  1764. IOCTL_IN(arg, data);
  1765. do_cd_lev(data);
  1766. /* fall through */
  1767. case SOUND_MIXER_READ_CD:
  1768. rc = IOCTL_OUT(arg, cd_lev);
  1769. break;
  1770. case SOUND_MIXER_WRITE_RECLEV:
  1771. IOCTL_IN(arg, data);
  1772. do_rec_lev(data) ;
  1773. /* fall through */
  1774. case SOUND_MIXER_READ_RECLEV:
  1775. rc = IOCTL_OUT(arg, rec_lev);
  1776. break;
  1777. case MIXER_WRITE(SOUND_MIXER_MONITOR):
  1778. IOCTL_IN(arg, data);
  1779. do_passthru_vol(data) ;
  1780. /* fall through */
  1781. case MIXER_READ(SOUND_MIXER_MONITOR):
  1782. rc = IOCTL_OUT(arg, passthru_vol);
  1783. break;
  1784. default:
  1785. rc = -EINVAL;
  1786. }
  1787. return rc;
  1788. }
  1789. static void awacs_mixer_init(void)
  1790. {
  1791. awacs_volume_setter(line_vol, 2, 0, 6);
  1792. if (has_perch)
  1793. awacs_enable_amp(spk_vol);
  1794. else
  1795. (void)awacs_volume_setter(spk_vol, 4, MASK_CMUTE, 6);
  1796. do_line_lev(line_lev) ;
  1797. do_ip_gain(ip_gain) ;
  1798. do_mic_lev(mic_lev) ;
  1799. do_cd_lev(cd_lev) ;
  1800. do_rec_lev(rec_lev) ;
  1801. do_passthru_vol(passthru_vol) ;
  1802. }
  1803. static int burgundy_mixer_ioctl(u_int cmd, u_long arg)
  1804. {
  1805. int data;
  1806. int rc;
  1807. /* We are, we are, we are... Burgundy or better */
  1808. switch(cmd) {
  1809. case SOUND_MIXER_READ_DEVMASK:
  1810. data = SOUND_MASK_VOLUME | SOUND_MASK_CD |
  1811. SOUND_MASK_LINE | SOUND_MASK_MIC |
  1812. SOUND_MASK_SPEAKER | SOUND_MASK_ALTPCM;
  1813. rc = IOCTL_OUT(arg, data);
  1814. break;
  1815. case SOUND_MIXER_READ_RECMASK:
  1816. data = SOUND_MASK_LINE | SOUND_MASK_MIC
  1817. | SOUND_MASK_CD;
  1818. rc = IOCTL_OUT(arg, data);
  1819. break;
  1820. case SOUND_MIXER_READ_RECSRC:
  1821. data = 0;
  1822. if (awacs_reg[0] & MASK_MUX_AUDIN)
  1823. data |= SOUND_MASK_LINE;
  1824. if (awacs_reg[0] & MASK_MUX_MIC)
  1825. data |= SOUND_MASK_MIC;
  1826. if (awacs_reg[0] & MASK_MUX_CD)
  1827. data |= SOUND_MASK_CD;
  1828. rc = IOCTL_OUT(arg, data);
  1829. break;
  1830. case SOUND_MIXER_WRITE_RECSRC:
  1831. IOCTL_IN(arg, data);
  1832. data &= (SOUND_MASK_LINE
  1833. | SOUND_MASK_MIC | SOUND_MASK_CD);
  1834. awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
  1835. | MASK_MUX_AUDIN);
  1836. if (data & SOUND_MASK_LINE)
  1837. awacs_reg[0] |= MASK_MUX_AUDIN;
  1838. if (data & SOUND_MASK_MIC)
  1839. awacs_reg[0] |= MASK_MUX_MIC;
  1840. if (data & SOUND_MASK_CD)
  1841. awacs_reg[0] |= MASK_MUX_CD;
  1842. awacs_write(awacs_reg[0] | MASK_ADDR0);
  1843. rc = IOCTL_OUT(arg, data);
  1844. break;
  1845. case SOUND_MIXER_READ_STEREODEVS:
  1846. data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
  1847. | SOUND_MASK_RECLEV | SOUND_MASK_CD
  1848. | SOUND_MASK_LINE;
  1849. rc = IOCTL_OUT(arg, data);
  1850. break;
  1851. case SOUND_MIXER_READ_CAPS:
  1852. rc = IOCTL_OUT(arg, 0);
  1853. break;
  1854. case SOUND_MIXER_WRITE_VOLUME:
  1855. IOCTL_IN(arg, data);
  1856. awacs_burgundy_write_mvolume(MASK_ADDR_BURGUNDY_MASTER_VOLUME, data);
  1857. /* Fall through */
  1858. case SOUND_MIXER_READ_VOLUME:
  1859. rc = IOCTL_OUT(arg, awacs_burgundy_read_mvolume(MASK_ADDR_BURGUNDY_MASTER_VOLUME));
  1860. break;
  1861. case SOUND_MIXER_WRITE_SPEAKER:
  1862. IOCTL_IN(arg, data);
  1863. if (!(data & 0xff)) {
  1864. /* Mute the left speaker */
  1865. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  1866. awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) & ~0x2);
  1867. } else {
  1868. /* Unmute the left speaker */
  1869. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  1870. awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) | 0x2);
  1871. }
  1872. if (!(data & 0xff00)) {
  1873. /* Mute the right speaker */
  1874. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  1875. awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) & ~0x4);
  1876. } else {
  1877. /* Unmute the right speaker */
  1878. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
  1879. awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) | 0x4);
  1880. }
  1881. data = (((data&0xff)*16)/100 > 0xf ? 0xf :
  1882. (((data&0xff)*16)/100)) +
  1883. ((((data>>8)*16)/100 > 0xf ? 0xf :
  1884. ((((data>>8)*16)/100)))<<4);
  1885. awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER, ~data);
  1886. /* Fall through */
  1887. case SOUND_MIXER_READ_SPEAKER:
  1888. data = awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER);
  1889. data = (((data & 0xf)*100)/16) + ((((data>>4)*100)/16)<<8);
  1890. rc = IOCTL_OUT(arg, (~data) & 0x0000ffff);
  1891. break;
  1892. case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
  1893. IOCTL_IN(arg, data);
  1894. beep_vol = data & 0xff;
  1895. /* fall through */
  1896. case SOUND_MIXER_READ_ALTPCM:
  1897. rc = IOCTL_OUT(arg, beep_vol);
  1898. break;
  1899. case SOUND_MIXER_WRITE_LINE:
  1900. IOCTL_IN(arg, data);
  1901. awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLLINE, data);
  1902. /* fall through */
  1903. case SOUND_MIXER_READ_LINE:
  1904. data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLLINE);
  1905. rc = IOCTL_OUT(arg, data);
  1906. break;
  1907. case SOUND_MIXER_WRITE_MIC:
  1908. IOCTL_IN(arg, data);
  1909. /* Mic is mono device */
  1910. data = (data << 8) + (data << 24);
  1911. awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLMIC, data);
  1912. /* fall through */
  1913. case SOUND_MIXER_READ_MIC:
  1914. data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLMIC);
  1915. data <<= 24;
  1916. rc = IOCTL_OUT(arg, data);
  1917. break;
  1918. case SOUND_MIXER_WRITE_CD:
  1919. IOCTL_IN(arg, data);
  1920. awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLCD, data);
  1921. /* fall through */
  1922. case SOUND_MIXER_READ_CD:
  1923. data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLCD);
  1924. rc = IOCTL_OUT(arg, data);
  1925. break;
  1926. case SOUND_MIXER_WRITE_RECLEV:
  1927. IOCTL_IN(arg, data);
  1928. data = awacs_volume_setter(data, 0, 0, 4);
  1929. rc = IOCTL_OUT(arg, data);
  1930. break;
  1931. case SOUND_MIXER_READ_RECLEV:
  1932. data = awacs_get_volume(awacs_reg[0], 4);
  1933. rc = IOCTL_OUT(arg, data);
  1934. break;
  1935. case SOUND_MIXER_OUTMASK:
  1936. case SOUND_MIXER_OUTSRC:
  1937. default:
  1938. rc = -EINVAL;
  1939. }
  1940. return rc;
  1941. }
  1942. static int daca_mixer_ioctl(u_int cmd, u_long arg)
  1943. {
  1944. int data;
  1945. int rc;
  1946. /* And the DACA's no genius either! */
  1947. switch(cmd) {
  1948. case SOUND_MIXER_READ_DEVMASK:
  1949. data = SOUND_MASK_VOLUME;
  1950. rc = IOCTL_OUT(arg, data);
  1951. break;
  1952. case SOUND_MIXER_READ_RECMASK:
  1953. data = 0;
  1954. rc = IOCTL_OUT(arg, data);
  1955. break;
  1956. case SOUND_MIXER_READ_RECSRC:
  1957. data = 0;
  1958. rc = IOCTL_OUT(arg, data);
  1959. break;
  1960. case SOUND_MIXER_WRITE_RECSRC:
  1961. IOCTL_IN(arg, data);
  1962. data =0;
  1963. rc = IOCTL_OUT(arg, data);
  1964. break;
  1965. case SOUND_MIXER_READ_STEREODEVS:
  1966. data = SOUND_MASK_VOLUME;
  1967. rc = IOCTL_OUT(arg, data);
  1968. break;
  1969. case SOUND_MIXER_READ_CAPS:
  1970. rc = IOCTL_OUT(arg, 0);
  1971. break;
  1972. case SOUND_MIXER_WRITE_VOLUME:
  1973. IOCTL_IN(arg, data);
  1974. daca_set_volume(data, data);
  1975. /* Fall through */
  1976. case SOUND_MIXER_READ_VOLUME:
  1977. daca_get_volume(& data, &data);
  1978. rc = IOCTL_OUT(arg, data);
  1979. break;
  1980. case SOUND_MIXER_OUTMASK:
  1981. case SOUND_MIXER_OUTSRC:
  1982. default:
  1983. rc = -EINVAL;
  1984. }
  1985. return rc;
  1986. }
  1987. static int PMacMixerIoctl(u_int cmd, u_long arg)
  1988. {
  1989. int rc;
  1990. /* Different IOCTLS for burgundy and, eventually, DACA & Tumbler */
  1991. TRY_LOCK();
  1992. switch (awacs_revision){
  1993. case AWACS_BURGUNDY:
  1994. rc = burgundy_mixer_ioctl(cmd, arg);
  1995. break ;
  1996. case AWACS_DACA:
  1997. rc = daca_mixer_ioctl(cmd, arg);
  1998. break;
  1999. case AWACS_TUMBLER:
  2000. case AWACS_SNAPPER:
  2001. rc = tas_mixer_ioctl(cmd, arg);
  2002. break ;
  2003. default: /* ;-)) */
  2004. rc = awacs_mixer_ioctl(cmd, arg);
  2005. }
  2006. UNLOCK();
  2007. return rc;
  2008. }
  2009. static void PMacMixerInit(void)
  2010. {
  2011. switch (awacs_revision) {
  2012. case AWACS_TUMBLER:
  2013. printk("AE-Init tumbler mixer\n");
  2014. break ;
  2015. case AWACS_SNAPPER:
  2016. printk("AE-Init snapper mixer\n");
  2017. break ;
  2018. case AWACS_DACA:
  2019. case AWACS_BURGUNDY:
  2020. break ; /* don't know yet */
  2021. case AWACS_AWACS:
  2022. case AWACS_SCREAMER:
  2023. default:
  2024. awacs_mixer_init() ;
  2025. break ;
  2026. }
  2027. }
  2028. /* Write/Read sq setup functions:
  2029. Check to see if we have enough (or any) dbdma cmd buffers for the
  2030. user's fragment settings. If not, allocate some. If this fails we will
  2031. point at the beep buffer - as an emergency provision - to stop dma tromping
  2032. on some random bit of memory (if someone lets it go anyway).
  2033. The command buffers are then set up to point to the fragment buffers
  2034. (allocated elsewhere). We need n+1 commands the last of which holds
  2035. a NOP + loop to start.
  2036. */
  2037. static int PMacWriteSqSetup(void)
  2038. {
  2039. int i, count = 600 ;
  2040. volatile struct dbdma_cmd *cp;
  2041. LOCK();
  2042. /* stop the controller from doing any output - if it isn't already.
  2043. it _should_ be before this is called anyway */
  2044. out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  2045. while ((in_le32(&awacs_txdma->status) & RUN) && count--)
  2046. udelay(1);
  2047. #ifdef DEBUG_DMASOUND
  2048. if (count <= 0)
  2049. printk("dmasound_pmac: write sq setup: timeout waiting for dma to stop\n");
  2050. #endif
  2051. if ((write_sq.max_count + 1) > number_of_tx_cmd_buffers) {
  2052. kfree(awacs_tx_cmd_space);
  2053. number_of_tx_cmd_buffers = 0;
  2054. /* we need nbufs + 1 (for the loop) and we should request + 1
  2055. again because the DBDMA_ALIGN might pull the start up by up
  2056. to sizeof(struct dbdma_cmd) - 4.
  2057. */
  2058. awacs_tx_cmd_space = kmalloc
  2059. ((write_sq.max_count + 1 + 1) * sizeof(struct dbdma_cmd),
  2060. GFP_KERNEL);
  2061. if (awacs_tx_cmd_space == NULL) {
  2062. /* don't leave it dangling - nasty but better than a
  2063. random address */
  2064. out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
  2065. printk(KERN_ERR
  2066. "dmasound_pmac: can't allocate dbdma cmd buffers"
  2067. ", driver disabled\n");
  2068. UNLOCK();
  2069. return -ENOMEM;
  2070. }
  2071. awacs_tx_cmds = (volatile struct dbdma_cmd *)
  2072. DBDMA_ALIGN(awacs_tx_cmd_space);
  2073. number_of_tx_cmd_buffers = write_sq.max_count + 1;
  2074. }
  2075. cp = awacs_tx_cmds;
  2076. memset((void *)cp, 0, (write_sq.max_count+1) * sizeof(struct dbdma_cmd));
  2077. for (i = 0; i < write_sq.max_count; ++i, ++cp) {
  2078. st_le32(&cp->phy_addr, virt_to_bus(write_sq.buffers[i]));
  2079. }
  2080. st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
  2081. st_le32(&cp->cmd_dep, virt_to_bus(awacs_tx_cmds));
  2082. /* point the controller at the command stack - ready to go */
  2083. out_le32(&awacs_txdma->cmdptr, virt_to_bus(awacs_tx_cmds));
  2084. UNLOCK();
  2085. return 0;
  2086. }
  2087. static int PMacReadSqSetup(void)
  2088. {
  2089. int i, count = 600;
  2090. volatile struct dbdma_cmd *cp;
  2091. LOCK();
  2092. /* stop the controller from doing any input - if it isn't already.
  2093. it _should_ be before this is called anyway */
  2094. out_le32(&awacs_rxdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  2095. while ((in_le32(&awacs_rxdma->status) & RUN) && count--)
  2096. udelay(1);
  2097. #ifdef DEBUG_DMASOUND
  2098. if (count <= 0)
  2099. printk("dmasound_pmac: read sq setup: timeout waiting for dma to stop\n");
  2100. #endif
  2101. if ((read_sq.max_count+1) > number_of_rx_cmd_buffers ) {
  2102. kfree(awacs_rx_cmd_space);
  2103. number_of_rx_cmd_buffers = 0;
  2104. /* we need nbufs + 1 (for the loop) and we should request + 1 again
  2105. because the DBDMA_ALIGN might pull the start up by up to
  2106. sizeof(struct dbdma_cmd) - 4 (assuming kmalloc aligns 32 bits).
  2107. */
  2108. awacs_rx_cmd_space = kmalloc
  2109. ((read_sq.max_count + 1 + 1) * sizeof(struct dbdma_cmd),
  2110. GFP_KERNEL);
  2111. if (awacs_rx_cmd_space == NULL) {
  2112. /* don't leave it dangling - nasty but better than a
  2113. random address */
  2114. out_le32(&awacs_rxdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
  2115. printk(KERN_ERR
  2116. "dmasound_pmac: can't allocate dbdma cmd buffers"
  2117. ", driver disabled\n");
  2118. UNLOCK();
  2119. return -ENOMEM;
  2120. }
  2121. awacs_rx_cmds = (volatile struct dbdma_cmd *)
  2122. DBDMA_ALIGN(awacs_rx_cmd_space);
  2123. number_of_rx_cmd_buffers = read_sq.max_count + 1 ;
  2124. }
  2125. cp = awacs_rx_cmds;
  2126. memset((void *)cp, 0, (read_sq.max_count+1) * sizeof(struct dbdma_cmd));
  2127. /* Set dma buffers up in a loop */
  2128. for (i = 0; i < read_sq.max_count; i++,cp++) {
  2129. st_le32(&cp->phy_addr, virt_to_bus(read_sq.buffers[i]));
  2130. st_le16(&cp->command, INPUT_MORE + INTR_ALWAYS);
  2131. st_le16(&cp->req_count, read_sq.block_size);
  2132. st_le16(&cp->xfer_status, 0);
  2133. }
  2134. /* The next two lines make the thing loop around.
  2135. */
  2136. st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
  2137. st_le32(&cp->cmd_dep, virt_to_bus(awacs_rx_cmds));
  2138. /* point the controller at the command stack - ready to go */
  2139. out_le32(&awacs_rxdma->cmdptr, virt_to_bus(awacs_rx_cmds));
  2140. UNLOCK();
  2141. return 0;
  2142. }
  2143. /* TODO: this needs work to guarantee that when it returns DMA has stopped
  2144. but in a more elegant way than is done here....
  2145. */
  2146. static void PMacAbortRead(void)
  2147. {
  2148. int i;
  2149. volatile struct dbdma_cmd *cp;
  2150. LOCK();
  2151. /* give it a chance to update the output and provide the IRQ
  2152. that is expected.
  2153. */
  2154. out_le32(&awacs_rxdma->control, ((FLUSH) << 16) + FLUSH );
  2155. cp = awacs_rx_cmds;
  2156. for (i = 0; i < read_sq.max_count; i++,cp++)
  2157. st_le16(&cp->command, DBDMA_STOP);
  2158. /*
  2159. * We should probably wait for the thing to stop before we
  2160. * release the memory.
  2161. */
  2162. msleep(100) ; /* give it a (small) chance to act */
  2163. /* apply the sledgehammer approach - just stop it now */
  2164. out_le32(&awacs_rxdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
  2165. UNLOCK();
  2166. }
  2167. extern char *get_afmt_string(int);
  2168. static int PMacStateInfo(char *b, size_t sp)
  2169. {
  2170. int i, len = 0;
  2171. len = sprintf(b,"HW rates: ");
  2172. switch (awacs_revision){
  2173. case AWACS_DACA:
  2174. case AWACS_BURGUNDY:
  2175. len += sprintf(b,"44100 ") ;
  2176. break ;
  2177. case AWACS_TUMBLER:
  2178. case AWACS_SNAPPER:
  2179. for (i=0; i<1; i++){
  2180. if (tas_freqs_ok[i])
  2181. len += sprintf(b+len,"%d ", tas_freqs[i]) ;
  2182. }
  2183. break ;
  2184. case AWACS_AWACS:
  2185. case AWACS_SCREAMER:
  2186. default:
  2187. for (i=0; i<8; i++){
  2188. if (awacs_freqs_ok[i])
  2189. len += sprintf(b+len,"%d ", awacs_freqs[i]) ;
  2190. }
  2191. break ;
  2192. }
  2193. len += sprintf(b+len,"s/sec\n") ;
  2194. if (len < sp) {
  2195. len += sprintf(b+len,"HW AFMTS: ");
  2196. i = AFMT_U16_BE ;
  2197. while (i) {
  2198. if (i & dmasound.mach.hardware_afmts)
  2199. len += sprintf(b+len,"%s ",
  2200. get_afmt_string(i & dmasound.mach.hardware_afmts));
  2201. i >>= 1 ;
  2202. }
  2203. len += sprintf(b+len,"\n") ;
  2204. }
  2205. return len ;
  2206. }
  2207. /*** Machine definitions *****************************************************/
  2208. static SETTINGS def_hard = {
  2209. .format = AFMT_S16_BE,
  2210. .stereo = 1,
  2211. .size = 16,
  2212. .speed = 44100
  2213. } ;
  2214. static SETTINGS def_soft = {
  2215. .format = AFMT_S16_BE,
  2216. .stereo = 1,
  2217. .size = 16,
  2218. .speed = 44100
  2219. } ;
  2220. static MACHINE machPMac = {
  2221. .name = awacs_name,
  2222. .name2 = "PowerMac Built-in Sound",
  2223. .owner = THIS_MODULE,
  2224. .dma_alloc = PMacAlloc,
  2225. .dma_free = PMacFree,
  2226. .irqinit = PMacIrqInit,
  2227. #ifdef MODULE
  2228. .irqcleanup = PMacIrqCleanup,
  2229. #endif /* MODULE */
  2230. .init = PMacInit,
  2231. .silence = PMacSilence,
  2232. .setFormat = PMacSetFormat,
  2233. .setVolume = PMacSetVolume,
  2234. .play = PMacPlay,
  2235. .record = NULL, /* default to no record */
  2236. .mixer_init = PMacMixerInit,
  2237. .mixer_ioctl = PMacMixerIoctl,
  2238. .write_sq_setup = PMacWriteSqSetup,
  2239. .read_sq_setup = PMacReadSqSetup,
  2240. .state_info = PMacStateInfo,
  2241. .abort_read = PMacAbortRead,
  2242. .min_dsp_speed = 7350,
  2243. .max_dsp_speed = 44100,
  2244. .version = ((DMASOUND_AWACS_REVISION<<8) + DMASOUND_AWACS_EDITION)
  2245. };
  2246. /*** Config & Setup **********************************************************/
  2247. /* Check for pmac models that we care about in terms of special actions.
  2248. */
  2249. void __init
  2250. set_model(void)
  2251. {
  2252. /* portables/lap-tops */
  2253. if (machine_is_compatible("AAPL,3400/2400") ||
  2254. machine_is_compatible("AAPL,3500")) {
  2255. is_pbook_3X00 = 1 ;
  2256. }
  2257. if (machine_is_compatible("PowerBook1,1") || /* lombard */
  2258. machine_is_compatible("AAPL,PowerBook1998")){ /* wallstreet */
  2259. is_pbook_g3 = 1 ;
  2260. return ;
  2261. }
  2262. }
  2263. /* Get the OF node that tells us about the registers, interrupts etc. to use
  2264. for sound IO.
  2265. On most machines the sound IO OF node is the 'davbus' node. On newer pmacs
  2266. with DACA (& Tumbler) the node to use is i2s-a. On much older machines i.e.
  2267. before 9500 there is no davbus node and we have to use the 'awacs' property.
  2268. In the latter case we signal this by setting the codec value - so that the
  2269. code that looks for chip properties knows how to go about it.
  2270. */
  2271. static struct device_node* __init
  2272. get_snd_io_node(void)
  2273. {
  2274. struct device_node *np = NULL;
  2275. /* set up awacs_node for early OF which doesn't have a full set of
  2276. * properties on davbus
  2277. */
  2278. awacs_node = find_devices("awacs");
  2279. if (awacs_node)
  2280. awacs_revision = AWACS_AWACS;
  2281. /* powermac models after 9500 (other than those which use DACA or
  2282. * Tumbler) have a node called "davbus".
  2283. */
  2284. np = find_devices("davbus");
  2285. /*
  2286. * if we didn't find a davbus device, try 'i2s-a' since
  2287. * this seems to be what iBooks (& Tumbler) have.
  2288. */
  2289. if (np == NULL)
  2290. np = i2s_node = find_devices("i2s-a");
  2291. /* if we didn't find this - perhaps we are on an early model
  2292. * which _only_ has an 'awacs' node
  2293. */
  2294. if (np == NULL && awacs_node)
  2295. np = awacs_node ;
  2296. /* if we failed all these return null - this will cause the
  2297. * driver to give up...
  2298. */
  2299. return np ;
  2300. }
  2301. /* Get the OF node that contains the info about the sound chip, inputs s-rates
  2302. etc.
  2303. This node does not exist (or contains much reduced info) on earlier machines
  2304. we have to deduce the info other ways for these.
  2305. */
  2306. static struct device_node* __init
  2307. get_snd_info_node(struct device_node *io)
  2308. {
  2309. struct device_node *info;
  2310. info = find_devices("sound");
  2311. while (info && info->parent != io)
  2312. info = info->next;
  2313. return info;
  2314. }
  2315. /* Find out what type of codec we have.
  2316. */
  2317. static int __init
  2318. get_codec_type(struct device_node *info)
  2319. {
  2320. /* already set if pre-davbus model and info will be NULL */
  2321. int codec = awacs_revision ;
  2322. if (info) {
  2323. /* must do awacs first to allow screamer to overide it */
  2324. if (device_is_compatible(info, "awacs"))
  2325. codec = AWACS_AWACS ;
  2326. if (device_is_compatible(info, "screamer"))
  2327. codec = AWACS_SCREAMER;
  2328. if (device_is_compatible(info, "burgundy"))
  2329. codec = AWACS_BURGUNDY ;
  2330. if (device_is_compatible(info, "daca"))
  2331. codec = AWACS_DACA;
  2332. if (device_is_compatible(info, "tumbler"))
  2333. codec = AWACS_TUMBLER;
  2334. if (device_is_compatible(info, "snapper"))
  2335. codec = AWACS_SNAPPER;
  2336. }
  2337. return codec ;
  2338. }
  2339. /* find out what type, if any, of expansion card we have
  2340. */
  2341. static void __init
  2342. get_expansion_type(void)
  2343. {
  2344. if (find_devices("perch") != NULL)
  2345. has_perch = 1;
  2346. if (find_devices("pb-ziva-pc") != NULL)
  2347. has_ziva = 1;
  2348. /* need to work out how we deal with iMac SRS module */
  2349. }
  2350. /* set up frame rates.
  2351. * I suspect that these routines don't quite go about it the right way:
  2352. * - where there is more than one rate - I think that the first property
  2353. * value is the number of rates.
  2354. * TODO: check some more device trees and modify accordingly
  2355. * Set dmasound.mach.max_dsp_rate on the basis of these routines.
  2356. */
  2357. static void __init
  2358. awacs_init_frame_rates(unsigned int *prop, unsigned int l)
  2359. {
  2360. int i ;
  2361. if (prop) {
  2362. for (i=0; i<8; i++)
  2363. awacs_freqs_ok[i] = 0 ;
  2364. for (l /= sizeof(int); l > 0; --l) {
  2365. unsigned int r = *prop++;
  2366. /* Apple 'Fixed' format */
  2367. if (r >= 0x10000)
  2368. r >>= 16;
  2369. for (i = 0; i < 8; ++i) {
  2370. if (r == awacs_freqs[i]) {
  2371. awacs_freqs_ok[i] = 1;
  2372. break;
  2373. }
  2374. }
  2375. }
  2376. }
  2377. /* else we assume that all the rates are available */
  2378. }
  2379. static void __init
  2380. burgundy_init_frame_rates(unsigned int *prop, unsigned int l)
  2381. {
  2382. int temp[9] ;
  2383. int i = 0 ;
  2384. if (prop) {
  2385. for (l /= sizeof(int); l > 0; --l) {
  2386. unsigned int r = *prop++;
  2387. /* Apple 'Fixed' format */
  2388. if (r >= 0x10000)
  2389. r >>= 16;
  2390. temp[i] = r ;
  2391. i++ ; if(i>=9) i=8;
  2392. }
  2393. }
  2394. #ifdef DEBUG_DMASOUND
  2395. if (i > 1){
  2396. int j;
  2397. printk("dmasound_pmac: burgundy with multiple frame rates\n");
  2398. for(j=0; j<i; j++)
  2399. printk("%d ", temp[j]) ;
  2400. printk("\n") ;
  2401. }
  2402. #endif
  2403. }
  2404. static void __init
  2405. daca_init_frame_rates(unsigned int *prop, unsigned int l)
  2406. {
  2407. int temp[9] ;
  2408. int i = 0 ;
  2409. if (prop) {
  2410. for (l /= sizeof(int); l > 0; --l) {
  2411. unsigned int r = *prop++;
  2412. /* Apple 'Fixed' format */
  2413. if (r >= 0x10000)
  2414. r >>= 16;
  2415. temp[i] = r ;
  2416. i++ ; if(i>=9) i=8;
  2417. }
  2418. }
  2419. #ifdef DEBUG_DMASOUND
  2420. if (i > 1){
  2421. int j;
  2422. printk("dmasound_pmac: DACA with multiple frame rates\n");
  2423. for(j=0; j<i; j++)
  2424. printk("%d ", temp[j]) ;
  2425. printk("\n") ;
  2426. }
  2427. #endif
  2428. }
  2429. static void __init
  2430. init_frame_rates(unsigned int *prop, unsigned int l)
  2431. {
  2432. switch (awacs_revision) {
  2433. case AWACS_TUMBLER:
  2434. case AWACS_SNAPPER:
  2435. tas_init_frame_rates(prop, l);
  2436. break ;
  2437. case AWACS_DACA:
  2438. daca_init_frame_rates(prop, l);
  2439. break ;
  2440. case AWACS_BURGUNDY:
  2441. burgundy_init_frame_rates(prop, l);
  2442. break ;
  2443. default:
  2444. awacs_init_frame_rates(prop, l);
  2445. break ;
  2446. }
  2447. }
  2448. /* find things/machines that can't do mac-io byteswap
  2449. */
  2450. static void __init
  2451. set_hw_byteswap(struct device_node *io)
  2452. {
  2453. struct device_node *mio ;
  2454. unsigned int kl = 0 ;
  2455. /* if seems that Keylargo can't byte-swap */
  2456. for (mio = io->parent; mio ; mio = mio->parent) {
  2457. if (strcmp(mio->name, "mac-io") == 0) {
  2458. if (device_is_compatible(mio, "Keylargo"))
  2459. kl = 1;
  2460. break;
  2461. }
  2462. }
  2463. hw_can_byteswap = !kl;
  2464. }
  2465. /* Allocate the resources necessary for beep generation. This cannot be (quite)
  2466. done statically (yet) because we cannot do virt_to_bus() on static vars when
  2467. the code is loaded as a module.
  2468. for the sake of saving the possibility that two allocations will incur the
  2469. overhead of two pull-ups in DBDMA_ALIGN() we allocate the 'emergency' dmdma
  2470. command here as well... even tho' it is not part of the beep process.
  2471. */
  2472. int32_t
  2473. __init setup_beep(void)
  2474. {
  2475. /* Initialize beep stuff */
  2476. /* want one cmd buffer for beeps, and a second one for emergencies
  2477. - i.e. dbdma error conditions.
  2478. ask for three to allow for pull up in DBDMA_ALIGN().
  2479. */
  2480. beep_dbdma_cmd_space =
  2481. kmalloc((2 + 1) * sizeof(struct dbdma_cmd), GFP_KERNEL);
  2482. if(beep_dbdma_cmd_space == NULL) {
  2483. printk(KERN_ERR "dmasound_pmac: no beep dbdma cmd space\n") ;
  2484. return -ENOMEM ;
  2485. }
  2486. beep_dbdma_cmd = (volatile struct dbdma_cmd *)
  2487. DBDMA_ALIGN(beep_dbdma_cmd_space);
  2488. /* set up emergency dbdma cmd */
  2489. emergency_dbdma_cmd = beep_dbdma_cmd+1 ;
  2490. beep_buf = (short *) kmalloc(BEEP_BUFLEN * 4, GFP_KERNEL);
  2491. if (beep_buf == NULL) {
  2492. printk(KERN_ERR "dmasound_pmac: no memory for beep buffer\n");
  2493. kfree(beep_dbdma_cmd_space) ;
  2494. return -ENOMEM ;
  2495. }
  2496. return 0 ;
  2497. }
  2498. static struct input_dev *awacs_beep_dev;
  2499. int __init dmasound_awacs_init(void)
  2500. {
  2501. struct device_node *io = NULL, *info = NULL;
  2502. int vol, res;
  2503. if (_machine != _MACH_Pmac)
  2504. return -ENODEV;
  2505. awacs_subframe = 0;
  2506. awacs_revision = 0;
  2507. hw_can_byteswap = 1 ; /* most can */
  2508. /* look for models we need to handle specially */
  2509. set_model() ;
  2510. /* find the OF node that tells us about the dbdma stuff
  2511. */
  2512. io = get_snd_io_node();
  2513. if (io == NULL) {
  2514. #ifdef DEBUG_DMASOUND
  2515. printk("dmasound_pmac: couldn't find sound io OF node\n");
  2516. #endif
  2517. return -ENODEV ;
  2518. }
  2519. /* find the OF node that tells us about the sound sub-system
  2520. * this doesn't exist on pre-davbus machines (earlier than 9500)
  2521. */
  2522. if (awacs_revision != AWACS_AWACS) { /* set for pre-davbus */
  2523. info = get_snd_info_node(io) ;
  2524. if (info == NULL){
  2525. #ifdef DEBUG_DMASOUND
  2526. printk("dmasound_pmac: couldn't find 'sound' OF node\n");
  2527. #endif
  2528. return -ENODEV ;
  2529. }
  2530. }
  2531. awacs_revision = get_codec_type(info) ;
  2532. if (awacs_revision == 0) {
  2533. #ifdef DEBUG_DMASOUND
  2534. printk("dmasound_pmac: couldn't find a Codec we can handle\n");
  2535. #endif
  2536. return -ENODEV ; /* we don't know this type of h/w */
  2537. }
  2538. /* set up perch, ziva, SRS or whatever else we have as sound
  2539. * expansion.
  2540. */
  2541. get_expansion_type();
  2542. /* we've now got enough information to make up the audio topology.
  2543. * we will map the sound part of mac-io now so that we can probe for
  2544. * other info if necessary (early AWACS we want to read chip ids)
  2545. */
  2546. if (of_get_address(io, 2, NULL, NULL) == NULL || io->n_intrs < 3) {
  2547. /* OK - maybe we need to use the 'awacs' node (on earlier
  2548. * machines).
  2549. */
  2550. if (awacs_node) {
  2551. io = awacs_node ;
  2552. if (of_get_address(io, 2, NULL, NULL) == NULL ||
  2553. io->n_intrs < 3) {
  2554. printk("dmasound_pmac: can't use %s\n",
  2555. io->full_name);
  2556. return -ENODEV;
  2557. }
  2558. } else
  2559. printk("dmasound_pmac: can't use %s\n", io->full_name);
  2560. }
  2561. if (of_address_to_resource(io, 0, &awacs_rsrc[0]) ||
  2562. request_mem_region(awacs_rsrc[0].start,
  2563. awacs_rsrc[0].end - awacs_rsrc[0].start + 1,
  2564. " (IO)") == NULL) {
  2565. printk(KERN_ERR "dmasound: can't request IO resource !\n");
  2566. return -ENODEV;
  2567. }
  2568. if (of_address_to_resource(io, 1, &awacs_rsrc[1]) ||
  2569. request_mem_region(awacs_rsrc[1].start,
  2570. awacs_rsrc[1].end - awacs_rsrc[1].start + 1,
  2571. " (tx dma)") == NULL) {
  2572. release_mem_region(awacs_rsrc[0].start,
  2573. awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
  2574. printk(KERN_ERR "dmasound: can't request Tx DMA resource !\n");
  2575. return -ENODEV;
  2576. }
  2577. if (of_address_to_resource(io, 2, &awacs_rsrc[2]) ||
  2578. request_mem_region(awacs_rsrc[2].start,
  2579. awacs_rsrc[2].end - awacs_rsrc[2].start + 1,
  2580. " (rx dma)") == NULL) {
  2581. release_mem_region(awacs_rsrc[0].start,
  2582. awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
  2583. release_mem_region(awacs_rsrc[1].start,
  2584. awacs_rsrc[1].end - awacs_rsrc[1].start + 1);
  2585. printk(KERN_ERR "dmasound: can't request Rx DMA resource !\n");
  2586. return -ENODEV;
  2587. }
  2588. awacs_beep_dev = input_allocate_device();
  2589. if (!awacs_beep_dev) {
  2590. release_mem_region(awacs_rsrc[0].start,
  2591. awacs_rsrc[0].end - awacs_rsrc[0].start + 1);
  2592. release_mem_region(awacs_rsrc[1].start,
  2593. awacs_rsrc[1].end - awacs_rsrc[1].start + 1);
  2594. release_mem_region(awacs_rsrc[2].start,
  2595. awacs_rsrc[2].end - awacs_rsrc[2].start + 1);
  2596. printk(KERN_ERR "dmasound: can't allocate input device !\n");
  2597. return -ENOMEM;
  2598. }
  2599. awacs_beep_dev->name = "dmasound beeper";
  2600. awacs_beep_dev->phys = "macio/input0";
  2601. awacs_beep_dev->id.bustype = BUS_HOST;
  2602. awacs_beep_dev->event = awacs_beep_event;
  2603. awacs_beep_dev->sndbit[0] = BIT(SND_BELL) | BIT(SND_TONE);
  2604. awacs_beep_dev->evbit[0] = BIT(EV_SND);
  2605. /* all OF versions I've seen use this value */
  2606. if (i2s_node)
  2607. i2s = ioremap(awacs_rsrc[0].start, 0x1000);
  2608. else
  2609. awacs = ioremap(awacs_rsrc[0].start, 0x1000);
  2610. awacs_txdma = ioremap(awacs_rsrc[1].start, 0x100);
  2611. awacs_rxdma = ioremap(awacs_rsrc[2].start, 0x100);
  2612. /* first of all make sure that the chip is powered up....*/
  2613. pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, io, 0, 1);
  2614. if (awacs_revision == AWACS_SCREAMER && awacs)
  2615. awacs_recalibrate();
  2616. awacs_irq = io->intrs[0].line;
  2617. awacs_tx_irq = io->intrs[1].line;
  2618. awacs_rx_irq = io->intrs[2].line;
  2619. /* Hack for legacy crap that will be killed someday */
  2620. awacs_node = io;
  2621. /* if we have an awacs or screamer - probe the chip to make
  2622. * sure we have the right revision.
  2623. */
  2624. if (awacs_revision <= AWACS_SCREAMER){
  2625. uint32_t temp, rev, mfg ;
  2626. /* find out the awacs revision from the chip */
  2627. temp = in_le32(&awacs->codec_stat);
  2628. rev = (temp >> 12) & 0xf;
  2629. mfg = (temp >> 8) & 0xf;
  2630. #ifdef DEBUG_DMASOUND
  2631. printk("dmasound_pmac: Awacs/Screamer Codec Mfct: %d Rev %d\n", mfg, rev);
  2632. #endif
  2633. if (rev >= AWACS_SCREAMER)
  2634. awacs_revision = AWACS_SCREAMER ;
  2635. else
  2636. awacs_revision = rev ;
  2637. }
  2638. dmasound.mach = machPMac;
  2639. /* find out other bits & pieces from OF, these may be present
  2640. only on some models ... so be careful.
  2641. */
  2642. /* in the absence of a frame rates property we will use the defaults
  2643. */
  2644. if (info) {
  2645. unsigned int *prop, l;
  2646. sound_device_id = 0;
  2647. /* device ID appears post g3 b&w */
  2648. prop = (unsigned int *)get_property(info, "device-id", NULL);
  2649. if (prop != 0)
  2650. sound_device_id = *prop;
  2651. /* look for a property saying what sample rates
  2652. are available */
  2653. prop = (unsigned int *)get_property(info, "sample-rates", &l);
  2654. if (prop == 0)
  2655. prop = (unsigned int *) get_property
  2656. (info, "output-frame-rates", &l);
  2657. /* if it's there use it to set up frame rates */
  2658. init_frame_rates(prop, l) ;
  2659. }
  2660. if (awacs)
  2661. out_le32(&awacs->control, 0x11); /* set everything quiesent */
  2662. set_hw_byteswap(io) ; /* figure out if the h/w can do it */
  2663. #ifdef CONFIG_NVRAM
  2664. /* get default volume from nvram */
  2665. vol = ((pmac_xpram_read( 8 ) & 7 ) << 1 );
  2666. #else
  2667. vol = 0;
  2668. #endif
  2669. /* set up tracking values */
  2670. spk_vol = vol * 100 ;
  2671. spk_vol /= 7 ; /* get set value to a percentage */
  2672. spk_vol |= (spk_vol << 8) ; /* equal left & right */
  2673. line_vol = passthru_vol = spk_vol ;
  2674. /* fill regs that are shared between AWACS & Burgundy */
  2675. awacs_reg[2] = vol + (vol << 6);
  2676. awacs_reg[4] = vol + (vol << 6);
  2677. awacs_reg[5] = vol + (vol << 6); /* screamer has loopthru vol control */
  2678. awacs_reg[6] = 0; /* maybe should be vol << 3 for PCMCIA speaker */
  2679. awacs_reg[7] = 0;
  2680. awacs_reg[0] = MASK_MUX_CD;
  2681. awacs_reg[1] = MASK_LOOPTHRU;
  2682. /* FIXME: Only machines with external SRS module need MASK_PAROUT */
  2683. if (has_perch || sound_device_id == 0x5
  2684. || /*sound_device_id == 0x8 ||*/ sound_device_id == 0xb)
  2685. awacs_reg[1] |= MASK_PAROUT0 | MASK_PAROUT1;
  2686. switch (awacs_revision) {
  2687. case AWACS_TUMBLER:
  2688. tas_register_driver(&tas3001c_hooks);
  2689. tas_init(I2C_DRIVERID_TAS3001C, I2C_DRIVERNAME_TAS3001C);
  2690. tas_dmasound_init();
  2691. tas_post_init();
  2692. break ;
  2693. case AWACS_SNAPPER:
  2694. tas_register_driver(&tas3004_hooks);
  2695. tas_init(I2C_DRIVERID_TAS3004,I2C_DRIVERNAME_TAS3004);
  2696. tas_dmasound_init();
  2697. tas_post_init();
  2698. break;
  2699. case AWACS_DACA:
  2700. daca_init();
  2701. break;
  2702. case AWACS_BURGUNDY:
  2703. awacs_burgundy_init();
  2704. break ;
  2705. case AWACS_SCREAMER:
  2706. case AWACS_AWACS:
  2707. default:
  2708. load_awacs();
  2709. break ;
  2710. }
  2711. /* enable/set-up external modules - when we know how */
  2712. if (has_perch)
  2713. awacs_enable_amp(100 * 0x101);
  2714. /* Reset dbdma channels */
  2715. out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
  2716. while (in_le32(&awacs_txdma->status) & RUN)
  2717. udelay(1);
  2718. out_le32(&awacs_rxdma->control, (RUN|PAUSE|FLUSH|WAKE|DEAD) << 16);
  2719. while (in_le32(&awacs_rxdma->status) & RUN)
  2720. udelay(1);
  2721. /* Initialize beep stuff */
  2722. if ((res=setup_beep()))
  2723. return res ;
  2724. #ifdef CONFIG_PM
  2725. pmu_register_sleep_notifier(&awacs_sleep_notifier);
  2726. #endif /* CONFIG_PM */
  2727. /* Powerbooks have odd ways of enabling inputs such as
  2728. an expansion-bay CD or sound from an internal modem
  2729. or a PC-card modem. */
  2730. if (is_pbook_3X00) {
  2731. /*
  2732. * Enable CD and PC-card sound inputs.
  2733. * This is done by reading from address
  2734. * f301a000, + 0x10 to enable the expansion-bay
  2735. * CD sound input, + 0x80 to enable the PC-card
  2736. * sound input. The 0x100 enables the SCSI bus
  2737. * terminator power.
  2738. */
  2739. latch_base = ioremap (0xf301a000, 0x1000);
  2740. in_8(latch_base + 0x190);
  2741. } else if (is_pbook_g3) {
  2742. struct device_node* mio;
  2743. macio_base = NULL;
  2744. for (mio = io->parent; mio; mio = mio->parent) {
  2745. if (strcmp(mio->name, "mac-io") == 0) {
  2746. struct resource r;
  2747. if (of_address_to_resource(mio, 0, &r) == 0)
  2748. macio_base = ioremap(r.start, 0x40);
  2749. break;
  2750. }
  2751. }
  2752. /*
  2753. * Enable CD sound input.
  2754. * The relevant bits for writing to this byte are 0x8f.
  2755. * I haven't found out what the 0x80 bit does.
  2756. * For the 0xf bits, writing 3 or 7 enables the CD
  2757. * input, any other value disables it. Values
  2758. * 1, 3, 5, 7 enable the microphone. Values 0, 2,
  2759. * 4, 6, 8 - f enable the input from the modem.
  2760. * -- paulus.
  2761. */
  2762. if (macio_base)
  2763. out_8(macio_base + 0x37, 3);
  2764. }
  2765. if (hw_can_byteswap)
  2766. dmasound.mach.hardware_afmts = (AFMT_S16_BE | AFMT_S16_LE) ;
  2767. else
  2768. dmasound.mach.hardware_afmts = AFMT_S16_BE ;
  2769. /* shut out chips that do output only.
  2770. * may need to extend this to machines which have no inputs - even tho'
  2771. * they use screamer - IIRC one of the powerbooks is like this.
  2772. */
  2773. if (awacs_revision != AWACS_DACA) {
  2774. dmasound.mach.capabilities = DSP_CAP_DUPLEX ;
  2775. dmasound.mach.record = PMacRecord ;
  2776. }
  2777. dmasound.mach.default_hard = def_hard ;
  2778. dmasound.mach.default_soft = def_soft ;
  2779. switch (awacs_revision) {
  2780. case AWACS_BURGUNDY:
  2781. sprintf(awacs_name, "PowerMac Burgundy ") ;
  2782. break ;
  2783. case AWACS_DACA:
  2784. sprintf(awacs_name, "PowerMac DACA ") ;
  2785. break ;
  2786. case AWACS_TUMBLER:
  2787. sprintf(awacs_name, "PowerMac Tumbler ") ;
  2788. break ;
  2789. case AWACS_SNAPPER:
  2790. sprintf(awacs_name, "PowerMac Snapper ") ;
  2791. break ;
  2792. case AWACS_SCREAMER:
  2793. sprintf(awacs_name, "PowerMac Screamer ") ;
  2794. break ;
  2795. case AWACS_AWACS:
  2796. default:
  2797. sprintf(awacs_name, "PowerMac AWACS rev %d ", awacs_revision) ;
  2798. break ;
  2799. }
  2800. /*
  2801. * XXX: we should handle errors here, but that would mean
  2802. * rewriting the whole init code. later..
  2803. */
  2804. input_register_device(awacs_beep_dev);
  2805. return dmasound_init();
  2806. }
  2807. static void __exit dmasound_awacs_cleanup(void)
  2808. {
  2809. input_unregister_device(awacs_beep_dev);
  2810. switch (awacs_revision) {
  2811. case AWACS_TUMBLER:
  2812. case AWACS_SNAPPER:
  2813. tas_dmasound_cleanup();
  2814. tas_cleanup();
  2815. break ;
  2816. case AWACS_DACA:
  2817. daca_cleanup();
  2818. break;
  2819. }
  2820. dmasound_deinit();
  2821. }
  2822. MODULE_DESCRIPTION("PowerMac built-in audio driver.");
  2823. MODULE_LICENSE("GPL");
  2824. module_init(dmasound_awacs_init);
  2825. module_exit(dmasound_awacs_cleanup);