dmasound_awacs.c 85 KB

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