cs4231.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  1. /*
  2. * Driver for CS4231 sound chips found on Sparcs.
  3. * Copyright (C) 2002 David S. Miller <davem@redhat.com>
  4. *
  5. * Based entirely upon drivers/sbus/audio/cs4231.c which is:
  6. * Copyright (C) 1996, 1997, 1998, 1998 Derrick J Brashear (shadow@andrew.cmu.edu)
  7. * and also sound/isa/cs423x/cs4231_lib.c which is:
  8. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  9. */
  10. #include <linux/config.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/moduleparam.h>
  18. #include <sound/driver.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/info.h>
  22. #include <sound/control.h>
  23. #include <sound/timer.h>
  24. #include <sound/initval.h>
  25. #include <sound/pcm_params.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #ifdef CONFIG_SBUS
  29. #define SBUS_SUPPORT
  30. #endif
  31. #ifdef SBUS_SUPPORT
  32. #include <asm/sbus.h>
  33. #endif
  34. #if defined(CONFIG_PCI) && defined(CONFIG_SPARC64)
  35. #define EBUS_SUPPORT
  36. #endif
  37. #ifdef EBUS_SUPPORT
  38. #include <linux/pci.h>
  39. #include <asm/ebus.h>
  40. #endif
  41. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  42. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  43. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  44. module_param_array(index, int, NULL, 0444);
  45. MODULE_PARM_DESC(index, "Index value for Sun CS4231 soundcard.");
  46. module_param_array(id, charp, NULL, 0444);
  47. MODULE_PARM_DESC(id, "ID string for Sun CS4231 soundcard.");
  48. module_param_array(enable, bool, NULL, 0444);
  49. MODULE_PARM_DESC(enable, "Enable Sun CS4231 soundcard.");
  50. MODULE_AUTHOR("Jaroslav Kysela, Derrick J. Brashear and David S. Miller");
  51. MODULE_DESCRIPTION("Sun CS4231");
  52. MODULE_LICENSE("GPL");
  53. MODULE_SUPPORTED_DEVICE("{{Sun,CS4231}}");
  54. #ifdef SBUS_SUPPORT
  55. struct sbus_dma_info {
  56. spinlock_t lock;
  57. int dir;
  58. void __iomem *regs;
  59. };
  60. #endif
  61. struct snd_cs4231;
  62. struct cs4231_dma_control {
  63. void (*prepare)(struct cs4231_dma_control *dma_cont, int dir);
  64. void (*enable)(struct cs4231_dma_control *dma_cont, int on);
  65. int (*request)(struct cs4231_dma_control *dma_cont, dma_addr_t bus_addr, size_t len);
  66. unsigned int (*address)(struct cs4231_dma_control *dma_cont);
  67. void (*reset)(struct snd_cs4231 *chip);
  68. void (*preallocate)(struct snd_cs4231 *chip, struct snd_pcm *pcm);
  69. #ifdef EBUS_SUPPORT
  70. struct ebus_dma_info ebus_info;
  71. #endif
  72. #ifdef SBUS_SUPPORT
  73. struct sbus_dma_info sbus_info;
  74. #endif
  75. };
  76. struct snd_cs4231 {
  77. spinlock_t lock;
  78. void __iomem *port;
  79. struct cs4231_dma_control p_dma;
  80. struct cs4231_dma_control c_dma;
  81. u32 flags;
  82. #define CS4231_FLAG_EBUS 0x00000001
  83. #define CS4231_FLAG_PLAYBACK 0x00000002
  84. #define CS4231_FLAG_CAPTURE 0x00000004
  85. struct snd_card *card;
  86. struct snd_pcm *pcm;
  87. struct snd_pcm_substream *playback_substream;
  88. unsigned int p_periods_sent;
  89. struct snd_pcm_substream *capture_substream;
  90. unsigned int c_periods_sent;
  91. struct snd_timer *timer;
  92. unsigned short mode;
  93. #define CS4231_MODE_NONE 0x0000
  94. #define CS4231_MODE_PLAY 0x0001
  95. #define CS4231_MODE_RECORD 0x0002
  96. #define CS4231_MODE_TIMER 0x0004
  97. #define CS4231_MODE_OPEN (CS4231_MODE_PLAY|CS4231_MODE_RECORD|CS4231_MODE_TIMER)
  98. unsigned char image[32]; /* registers image */
  99. int mce_bit;
  100. int calibrate_mute;
  101. struct mutex mce_mutex;
  102. struct mutex open_mutex;
  103. union {
  104. #ifdef SBUS_SUPPORT
  105. struct sbus_dev *sdev;
  106. #endif
  107. #ifdef EBUS_SUPPORT
  108. struct pci_dev *pdev;
  109. #endif
  110. } dev_u;
  111. unsigned int irq[2];
  112. unsigned int regs_size;
  113. struct snd_cs4231 *next;
  114. };
  115. static struct snd_cs4231 *cs4231_list;
  116. /* Eventually we can use sound/isa/cs423x/cs4231_lib.c directly, but for
  117. * now.... -DaveM
  118. */
  119. /* IO ports */
  120. #define CS4231P(chip, x) ((chip)->port + c_d_c_CS4231##x)
  121. /* XXX offsets are different than PC ISA chips... */
  122. #define c_d_c_CS4231REGSEL 0x0
  123. #define c_d_c_CS4231REG 0x4
  124. #define c_d_c_CS4231STATUS 0x8
  125. #define c_d_c_CS4231PIO 0xc
  126. /* codec registers */
  127. #define CS4231_LEFT_INPUT 0x00 /* left input control */
  128. #define CS4231_RIGHT_INPUT 0x01 /* right input control */
  129. #define CS4231_AUX1_LEFT_INPUT 0x02 /* left AUX1 input control */
  130. #define CS4231_AUX1_RIGHT_INPUT 0x03 /* right AUX1 input control */
  131. #define CS4231_AUX2_LEFT_INPUT 0x04 /* left AUX2 input control */
  132. #define CS4231_AUX2_RIGHT_INPUT 0x05 /* right AUX2 input control */
  133. #define CS4231_LEFT_OUTPUT 0x06 /* left output control register */
  134. #define CS4231_RIGHT_OUTPUT 0x07 /* right output control register */
  135. #define CS4231_PLAYBK_FORMAT 0x08 /* clock and data format - playback - bits 7-0 MCE */
  136. #define CS4231_IFACE_CTRL 0x09 /* interface control - bits 7-2 MCE */
  137. #define CS4231_PIN_CTRL 0x0a /* pin control */
  138. #define CS4231_TEST_INIT 0x0b /* test and initialization */
  139. #define CS4231_MISC_INFO 0x0c /* miscellaneaous information */
  140. #define CS4231_LOOPBACK 0x0d /* loopback control */
  141. #define CS4231_PLY_UPR_CNT 0x0e /* playback upper base count */
  142. #define CS4231_PLY_LWR_CNT 0x0f /* playback lower base count */
  143. #define CS4231_ALT_FEATURE_1 0x10 /* alternate #1 feature enable */
  144. #define CS4231_ALT_FEATURE_2 0x11 /* alternate #2 feature enable */
  145. #define CS4231_LEFT_LINE_IN 0x12 /* left line input control */
  146. #define CS4231_RIGHT_LINE_IN 0x13 /* right line input control */
  147. #define CS4231_TIMER_LOW 0x14 /* timer low byte */
  148. #define CS4231_TIMER_HIGH 0x15 /* timer high byte */
  149. #define CS4231_LEFT_MIC_INPUT 0x16 /* left MIC input control register (InterWave only) */
  150. #define CS4231_RIGHT_MIC_INPUT 0x17 /* right MIC input control register (InterWave only) */
  151. #define CS4236_EXT_REG 0x17 /* extended register access */
  152. #define CS4231_IRQ_STATUS 0x18 /* irq status register */
  153. #define CS4231_LINE_LEFT_OUTPUT 0x19 /* left line output control register (InterWave only) */
  154. #define CS4231_VERSION 0x19 /* CS4231(A) - version values */
  155. #define CS4231_MONO_CTRL 0x1a /* mono input/output control */
  156. #define CS4231_LINE_RIGHT_OUTPUT 0x1b /* right line output control register (InterWave only) */
  157. #define CS4235_LEFT_MASTER 0x1b /* left master output control */
  158. #define CS4231_REC_FORMAT 0x1c /* clock and data format - record - bits 7-0 MCE */
  159. #define CS4231_PLY_VAR_FREQ 0x1d /* playback variable frequency */
  160. #define CS4235_RIGHT_MASTER 0x1d /* right master output control */
  161. #define CS4231_REC_UPR_CNT 0x1e /* record upper count */
  162. #define CS4231_REC_LWR_CNT 0x1f /* record lower count */
  163. /* definitions for codec register select port - CODECP( REGSEL ) */
  164. #define CS4231_INIT 0x80 /* CODEC is initializing */
  165. #define CS4231_MCE 0x40 /* mode change enable */
  166. #define CS4231_TRD 0x20 /* transfer request disable */
  167. /* definitions for codec status register - CODECP( STATUS ) */
  168. #define CS4231_GLOBALIRQ 0x01 /* IRQ is active */
  169. /* definitions for codec irq status - CS4231_IRQ_STATUS */
  170. #define CS4231_PLAYBACK_IRQ 0x10
  171. #define CS4231_RECORD_IRQ 0x20
  172. #define CS4231_TIMER_IRQ 0x40
  173. #define CS4231_ALL_IRQS 0x70
  174. #define CS4231_REC_UNDERRUN 0x08
  175. #define CS4231_REC_OVERRUN 0x04
  176. #define CS4231_PLY_OVERRUN 0x02
  177. #define CS4231_PLY_UNDERRUN 0x01
  178. /* definitions for CS4231_LEFT_INPUT and CS4231_RIGHT_INPUT registers */
  179. #define CS4231_ENABLE_MIC_GAIN 0x20
  180. #define CS4231_MIXS_LINE 0x00
  181. #define CS4231_MIXS_AUX1 0x40
  182. #define CS4231_MIXS_MIC 0x80
  183. #define CS4231_MIXS_ALL 0xc0
  184. /* definitions for clock and data format register - CS4231_PLAYBK_FORMAT */
  185. #define CS4231_LINEAR_8 0x00 /* 8-bit unsigned data */
  186. #define CS4231_ALAW_8 0x60 /* 8-bit A-law companded */
  187. #define CS4231_ULAW_8 0x20 /* 8-bit U-law companded */
  188. #define CS4231_LINEAR_16 0x40 /* 16-bit twos complement data - little endian */
  189. #define CS4231_LINEAR_16_BIG 0xc0 /* 16-bit twos complement data - big endian */
  190. #define CS4231_ADPCM_16 0xa0 /* 16-bit ADPCM */
  191. #define CS4231_STEREO 0x10 /* stereo mode */
  192. /* bits 3-1 define frequency divisor */
  193. #define CS4231_XTAL1 0x00 /* 24.576 crystal */
  194. #define CS4231_XTAL2 0x01 /* 16.9344 crystal */
  195. /* definitions for interface control register - CS4231_IFACE_CTRL */
  196. #define CS4231_RECORD_PIO 0x80 /* record PIO enable */
  197. #define CS4231_PLAYBACK_PIO 0x40 /* playback PIO enable */
  198. #define CS4231_CALIB_MODE 0x18 /* calibration mode bits */
  199. #define CS4231_AUTOCALIB 0x08 /* auto calibrate */
  200. #define CS4231_SINGLE_DMA 0x04 /* use single DMA channel */
  201. #define CS4231_RECORD_ENABLE 0x02 /* record enable */
  202. #define CS4231_PLAYBACK_ENABLE 0x01 /* playback enable */
  203. /* definitions for pin control register - CS4231_PIN_CTRL */
  204. #define CS4231_IRQ_ENABLE 0x02 /* enable IRQ */
  205. #define CS4231_XCTL1 0x40 /* external control #1 */
  206. #define CS4231_XCTL0 0x80 /* external control #0 */
  207. /* definitions for test and init register - CS4231_TEST_INIT */
  208. #define CS4231_CALIB_IN_PROGRESS 0x20 /* auto calibrate in progress */
  209. #define CS4231_DMA_REQUEST 0x10 /* DMA request in progress */
  210. /* definitions for misc control register - CS4231_MISC_INFO */
  211. #define CS4231_MODE2 0x40 /* MODE 2 */
  212. #define CS4231_IW_MODE3 0x6c /* MODE 3 - InterWave enhanced mode */
  213. #define CS4231_4236_MODE3 0xe0 /* MODE 3 - CS4236+ enhanced mode */
  214. /* definitions for alternate feature 1 register - CS4231_ALT_FEATURE_1 */
  215. #define CS4231_DACZ 0x01 /* zero DAC when underrun */
  216. #define CS4231_TIMER_ENABLE 0x40 /* codec timer enable */
  217. #define CS4231_OLB 0x80 /* output level bit */
  218. /* SBUS DMA register defines. */
  219. #define APCCSR 0x10UL /* APC DMA CSR */
  220. #define APCCVA 0x20UL /* APC Capture DMA Address */
  221. #define APCCC 0x24UL /* APC Capture Count */
  222. #define APCCNVA 0x28UL /* APC Capture DMA Next Address */
  223. #define APCCNC 0x2cUL /* APC Capture Next Count */
  224. #define APCPVA 0x30UL /* APC Play DMA Address */
  225. #define APCPC 0x34UL /* APC Play Count */
  226. #define APCPNVA 0x38UL /* APC Play DMA Next Address */
  227. #define APCPNC 0x3cUL /* APC Play Next Count */
  228. /* Defines for SBUS DMA-routines */
  229. #define APCVA 0x0UL /* APC DMA Address */
  230. #define APCC 0x4UL /* APC Count */
  231. #define APCNVA 0x8UL /* APC DMA Next Address */
  232. #define APCNC 0xcUL /* APC Next Count */
  233. #define APC_PLAY 0x30UL /* Play registers start at 0x30 */
  234. #define APC_RECORD 0x20UL /* Record registers start at 0x20 */
  235. /* APCCSR bits */
  236. #define APC_INT_PENDING 0x800000 /* Interrupt Pending */
  237. #define APC_PLAY_INT 0x400000 /* Playback interrupt */
  238. #define APC_CAPT_INT 0x200000 /* Capture interrupt */
  239. #define APC_GENL_INT 0x100000 /* General interrupt */
  240. #define APC_XINT_ENA 0x80000 /* General ext int. enable */
  241. #define APC_XINT_PLAY 0x40000 /* Playback ext intr */
  242. #define APC_XINT_CAPT 0x20000 /* Capture ext intr */
  243. #define APC_XINT_GENL 0x10000 /* Error ext intr */
  244. #define APC_XINT_EMPT 0x8000 /* Pipe empty interrupt (0 write to pva) */
  245. #define APC_XINT_PEMP 0x4000 /* Play pipe empty (pva and pnva not set) */
  246. #define APC_XINT_PNVA 0x2000 /* Playback NVA dirty */
  247. #define APC_XINT_PENA 0x1000 /* play pipe empty Int enable */
  248. #define APC_XINT_COVF 0x800 /* Cap data dropped on floor */
  249. #define APC_XINT_CNVA 0x400 /* Capture NVA dirty */
  250. #define APC_XINT_CEMP 0x200 /* Capture pipe empty (cva and cnva not set) */
  251. #define APC_XINT_CENA 0x100 /* Cap. pipe empty int enable */
  252. #define APC_PPAUSE 0x80 /* Pause the play DMA */
  253. #define APC_CPAUSE 0x40 /* Pause the capture DMA */
  254. #define APC_CDC_RESET 0x20 /* CODEC RESET */
  255. #define APC_PDMA_READY 0x08 /* Play DMA Go */
  256. #define APC_CDMA_READY 0x04 /* Capture DMA Go */
  257. #define APC_CHIP_RESET 0x01 /* Reset the chip */
  258. /* EBUS DMA register offsets */
  259. #define EBDMA_CSR 0x00UL /* Control/Status */
  260. #define EBDMA_ADDR 0x04UL /* DMA Address */
  261. #define EBDMA_COUNT 0x08UL /* DMA Count */
  262. /*
  263. * Some variables
  264. */
  265. static unsigned char freq_bits[14] = {
  266. /* 5510 */ 0x00 | CS4231_XTAL2,
  267. /* 6620 */ 0x0E | CS4231_XTAL2,
  268. /* 8000 */ 0x00 | CS4231_XTAL1,
  269. /* 9600 */ 0x0E | CS4231_XTAL1,
  270. /* 11025 */ 0x02 | CS4231_XTAL2,
  271. /* 16000 */ 0x02 | CS4231_XTAL1,
  272. /* 18900 */ 0x04 | CS4231_XTAL2,
  273. /* 22050 */ 0x06 | CS4231_XTAL2,
  274. /* 27042 */ 0x04 | CS4231_XTAL1,
  275. /* 32000 */ 0x06 | CS4231_XTAL1,
  276. /* 33075 */ 0x0C | CS4231_XTAL2,
  277. /* 37800 */ 0x08 | CS4231_XTAL2,
  278. /* 44100 */ 0x0A | CS4231_XTAL2,
  279. /* 48000 */ 0x0C | CS4231_XTAL1
  280. };
  281. static unsigned int rates[14] = {
  282. 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
  283. 27042, 32000, 33075, 37800, 44100, 48000
  284. };
  285. static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
  286. .count = 14,
  287. .list = rates,
  288. };
  289. static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
  290. {
  291. return snd_pcm_hw_constraint_list(runtime, 0,
  292. SNDRV_PCM_HW_PARAM_RATE,
  293. &hw_constraints_rates);
  294. }
  295. static unsigned char snd_cs4231_original_image[32] =
  296. {
  297. 0x00, /* 00/00 - lic */
  298. 0x00, /* 01/01 - ric */
  299. 0x9f, /* 02/02 - la1ic */
  300. 0x9f, /* 03/03 - ra1ic */
  301. 0x9f, /* 04/04 - la2ic */
  302. 0x9f, /* 05/05 - ra2ic */
  303. 0xbf, /* 06/06 - loc */
  304. 0xbf, /* 07/07 - roc */
  305. 0x20, /* 08/08 - pdfr */
  306. CS4231_AUTOCALIB, /* 09/09 - ic */
  307. 0x00, /* 0a/10 - pc */
  308. 0x00, /* 0b/11 - ti */
  309. CS4231_MODE2, /* 0c/12 - mi */
  310. 0x00, /* 0d/13 - lbc */
  311. 0x00, /* 0e/14 - pbru */
  312. 0x00, /* 0f/15 - pbrl */
  313. 0x80, /* 10/16 - afei */
  314. 0x01, /* 11/17 - afeii */
  315. 0x9f, /* 12/18 - llic */
  316. 0x9f, /* 13/19 - rlic */
  317. 0x00, /* 14/20 - tlb */
  318. 0x00, /* 15/21 - thb */
  319. 0x00, /* 16/22 - la3mic/reserved */
  320. 0x00, /* 17/23 - ra3mic/reserved */
  321. 0x00, /* 18/24 - afs */
  322. 0x00, /* 19/25 - lamoc/version */
  323. 0x00, /* 1a/26 - mioc */
  324. 0x00, /* 1b/27 - ramoc/reserved */
  325. 0x20, /* 1c/28 - cdfr */
  326. 0x00, /* 1d/29 - res4 */
  327. 0x00, /* 1e/30 - cbru */
  328. 0x00, /* 1f/31 - cbrl */
  329. };
  330. static u8 __cs4231_readb(struct snd_cs4231 *cp, void __iomem *reg_addr)
  331. {
  332. #ifdef EBUS_SUPPORT
  333. if (cp->flags & CS4231_FLAG_EBUS) {
  334. return readb(reg_addr);
  335. } else {
  336. #endif
  337. #ifdef SBUS_SUPPORT
  338. return sbus_readb(reg_addr);
  339. #endif
  340. #ifdef EBUS_SUPPORT
  341. }
  342. #endif
  343. }
  344. static void __cs4231_writeb(struct snd_cs4231 *cp, u8 val, void __iomem *reg_addr)
  345. {
  346. #ifdef EBUS_SUPPORT
  347. if (cp->flags & CS4231_FLAG_EBUS) {
  348. return writeb(val, reg_addr);
  349. } else {
  350. #endif
  351. #ifdef SBUS_SUPPORT
  352. return sbus_writeb(val, reg_addr);
  353. #endif
  354. #ifdef EBUS_SUPPORT
  355. }
  356. #endif
  357. }
  358. /*
  359. * Basic I/O functions
  360. */
  361. static void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
  362. unsigned char mask, unsigned char value)
  363. {
  364. int timeout;
  365. unsigned char tmp;
  366. for (timeout = 250;
  367. timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
  368. timeout--)
  369. udelay(100);
  370. #ifdef CONFIG_SND_DEBUG
  371. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  372. snd_printdd("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  373. #endif
  374. if (chip->calibrate_mute) {
  375. chip->image[reg] &= mask;
  376. chip->image[reg] |= value;
  377. } else {
  378. __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
  379. mb();
  380. tmp = (chip->image[reg] & mask) | value;
  381. __cs4231_writeb(chip, tmp, CS4231P(chip, REG));
  382. chip->image[reg] = tmp;
  383. mb();
  384. }
  385. }
  386. static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
  387. {
  388. int timeout;
  389. for (timeout = 250;
  390. timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
  391. timeout--)
  392. udelay(100);
  393. #ifdef CONFIG_SND_DEBUG
  394. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  395. snd_printdd("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  396. #endif
  397. __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
  398. __cs4231_writeb(chip, value, CS4231P(chip, REG));
  399. mb();
  400. }
  401. static void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
  402. {
  403. int timeout;
  404. for (timeout = 250;
  405. timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
  406. timeout--)
  407. udelay(100);
  408. #ifdef CONFIG_SND_DEBUG
  409. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  410. snd_printdd("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
  411. #endif
  412. __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
  413. __cs4231_writeb(chip, value, CS4231P(chip, REG));
  414. chip->image[reg] = value;
  415. mb();
  416. }
  417. static unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
  418. {
  419. int timeout;
  420. unsigned char ret;
  421. for (timeout = 250;
  422. timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
  423. timeout--)
  424. udelay(100);
  425. #ifdef CONFIG_SND_DEBUG
  426. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  427. snd_printdd("in: auto calibration time out - reg = 0x%x\n", reg);
  428. #endif
  429. __cs4231_writeb(chip, chip->mce_bit | reg, CS4231P(chip, REGSEL));
  430. mb();
  431. ret = __cs4231_readb(chip, CS4231P(chip, REG));
  432. return ret;
  433. }
  434. /*
  435. * CS4231 detection / MCE routines
  436. */
  437. static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
  438. {
  439. int timeout;
  440. /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
  441. for (timeout = 5; timeout > 0; timeout--)
  442. __cs4231_readb(chip, CS4231P(chip, REGSEL));
  443. /* end of cleanup sequence */
  444. for (timeout = 500;
  445. timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT);
  446. timeout--)
  447. udelay(1000);
  448. }
  449. static void snd_cs4231_mce_up(struct snd_cs4231 *chip)
  450. {
  451. unsigned long flags;
  452. int timeout;
  453. spin_lock_irqsave(&chip->lock, flags);
  454. for (timeout = 250; timeout > 0 && (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT); timeout--)
  455. udelay(100);
  456. #ifdef CONFIG_SND_DEBUG
  457. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  458. snd_printdd("mce_up - auto calibration time out (0)\n");
  459. #endif
  460. chip->mce_bit |= CS4231_MCE;
  461. timeout = __cs4231_readb(chip, CS4231P(chip, REGSEL));
  462. if (timeout == 0x80)
  463. snd_printdd("mce_up [%p]: serious init problem - codec still busy\n", chip->port);
  464. if (!(timeout & CS4231_MCE))
  465. __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f), CS4231P(chip, REGSEL));
  466. spin_unlock_irqrestore(&chip->lock, flags);
  467. }
  468. static void snd_cs4231_mce_down(struct snd_cs4231 *chip)
  469. {
  470. unsigned long flags;
  471. int timeout;
  472. spin_lock_irqsave(&chip->lock, flags);
  473. snd_cs4231_busy_wait(chip);
  474. #ifdef CONFIG_SND_DEBUG
  475. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  476. snd_printdd("mce_down [%p] - auto calibration time out (0)\n", CS4231P(chip, REGSEL));
  477. #endif
  478. chip->mce_bit &= ~CS4231_MCE;
  479. timeout = __cs4231_readb(chip, CS4231P(chip, REGSEL));
  480. __cs4231_writeb(chip, chip->mce_bit | (timeout & 0x1f), CS4231P(chip, REGSEL));
  481. if (timeout == 0x80)
  482. snd_printdd("mce_down [%p]: serious init problem - codec still busy\n", chip->port);
  483. if ((timeout & CS4231_MCE) == 0) {
  484. spin_unlock_irqrestore(&chip->lock, flags);
  485. return;
  486. }
  487. snd_cs4231_busy_wait(chip);
  488. /* calibration process */
  489. for (timeout = 500; timeout > 0 && (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0; timeout--)
  490. udelay(100);
  491. if ((snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0) {
  492. snd_printd("cs4231_mce_down - auto calibration time out (1)\n");
  493. spin_unlock_irqrestore(&chip->lock, flags);
  494. return;
  495. }
  496. /* in 10ms increments, check condition, up to 250ms */
  497. timeout = 25;
  498. while (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) {
  499. spin_unlock_irqrestore(&chip->lock, flags);
  500. if (--timeout < 0) {
  501. snd_printk("mce_down - auto calibration time out (2)\n");
  502. return;
  503. }
  504. msleep(10);
  505. spin_lock_irqsave(&chip->lock, flags);
  506. }
  507. /* in 10ms increments, check condition, up to 100ms */
  508. timeout = 10;
  509. while (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT) {
  510. spin_unlock_irqrestore(&chip->lock, flags);
  511. if (--timeout < 0) {
  512. snd_printk("mce_down - auto calibration time out (3)\n");
  513. return;
  514. }
  515. msleep(10);
  516. spin_lock_irqsave(&chip->lock, flags);
  517. }
  518. spin_unlock_irqrestore(&chip->lock, flags);
  519. }
  520. static void snd_cs4231_advance_dma(struct cs4231_dma_control *dma_cont,
  521. struct snd_pcm_substream *substream,
  522. unsigned int *periods_sent)
  523. {
  524. struct snd_pcm_runtime *runtime = substream->runtime;
  525. while (1) {
  526. unsigned int period_size = snd_pcm_lib_period_bytes(substream);
  527. unsigned int offset = period_size * (*periods_sent);
  528. BUG_ON(period_size >= (1 << 24));
  529. if (dma_cont->request(dma_cont, runtime->dma_addr + offset, period_size))
  530. return;
  531. (*periods_sent) = ((*periods_sent) + 1) % runtime->periods;
  532. }
  533. }
  534. static void cs4231_dma_trigger(struct snd_pcm_substream *substream,
  535. unsigned int what, int on)
  536. {
  537. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  538. struct cs4231_dma_control *dma_cont;
  539. if (what & CS4231_PLAYBACK_ENABLE) {
  540. dma_cont = &chip->p_dma;
  541. if (on) {
  542. dma_cont->prepare(dma_cont, 0);
  543. dma_cont->enable(dma_cont, 1);
  544. snd_cs4231_advance_dma(dma_cont,
  545. chip->playback_substream,
  546. &chip->p_periods_sent);
  547. } else {
  548. dma_cont->enable(dma_cont, 0);
  549. }
  550. }
  551. if (what & CS4231_RECORD_ENABLE) {
  552. dma_cont = &chip->c_dma;
  553. if (on) {
  554. dma_cont->prepare(dma_cont, 1);
  555. dma_cont->enable(dma_cont, 1);
  556. snd_cs4231_advance_dma(dma_cont,
  557. chip->capture_substream,
  558. &chip->c_periods_sent);
  559. } else {
  560. dma_cont->enable(dma_cont, 0);
  561. }
  562. }
  563. }
  564. static int snd_cs4231_trigger(struct snd_pcm_substream *substream, int cmd)
  565. {
  566. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  567. int result = 0;
  568. switch (cmd) {
  569. case SNDRV_PCM_TRIGGER_START:
  570. case SNDRV_PCM_TRIGGER_STOP:
  571. {
  572. unsigned int what = 0;
  573. struct snd_pcm_substream *s;
  574. struct list_head *pos;
  575. unsigned long flags;
  576. snd_pcm_group_for_each(pos, substream) {
  577. s = snd_pcm_group_substream_entry(pos);
  578. if (s == chip->playback_substream) {
  579. what |= CS4231_PLAYBACK_ENABLE;
  580. snd_pcm_trigger_done(s, substream);
  581. } else if (s == chip->capture_substream) {
  582. what |= CS4231_RECORD_ENABLE;
  583. snd_pcm_trigger_done(s, substream);
  584. }
  585. }
  586. spin_lock_irqsave(&chip->lock, flags);
  587. if (cmd == SNDRV_PCM_TRIGGER_START) {
  588. cs4231_dma_trigger(substream, what, 1);
  589. chip->image[CS4231_IFACE_CTRL] |= what;
  590. } else {
  591. cs4231_dma_trigger(substream, what, 0);
  592. chip->image[CS4231_IFACE_CTRL] &= ~what;
  593. }
  594. snd_cs4231_out(chip, CS4231_IFACE_CTRL,
  595. chip->image[CS4231_IFACE_CTRL]);
  596. spin_unlock_irqrestore(&chip->lock, flags);
  597. break;
  598. }
  599. default:
  600. result = -EINVAL;
  601. break;
  602. }
  603. return result;
  604. }
  605. /*
  606. * CODEC I/O
  607. */
  608. static unsigned char snd_cs4231_get_rate(unsigned int rate)
  609. {
  610. int i;
  611. for (i = 0; i < 14; i++)
  612. if (rate == rates[i])
  613. return freq_bits[i];
  614. // snd_BUG();
  615. return freq_bits[13];
  616. }
  617. static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip, int format, int channels)
  618. {
  619. unsigned char rformat;
  620. rformat = CS4231_LINEAR_8;
  621. switch (format) {
  622. case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
  623. case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
  624. case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
  625. case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
  626. case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
  627. }
  628. if (channels > 1)
  629. rformat |= CS4231_STEREO;
  630. return rformat;
  631. }
  632. static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
  633. {
  634. unsigned long flags;
  635. mute = mute ? 1 : 0;
  636. spin_lock_irqsave(&chip->lock, flags);
  637. if (chip->calibrate_mute == mute) {
  638. spin_unlock_irqrestore(&chip->lock, flags);
  639. return;
  640. }
  641. if (!mute) {
  642. snd_cs4231_dout(chip, CS4231_LEFT_INPUT,
  643. chip->image[CS4231_LEFT_INPUT]);
  644. snd_cs4231_dout(chip, CS4231_RIGHT_INPUT,
  645. chip->image[CS4231_RIGHT_INPUT]);
  646. snd_cs4231_dout(chip, CS4231_LOOPBACK,
  647. chip->image[CS4231_LOOPBACK]);
  648. }
  649. snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT,
  650. mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
  651. snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT,
  652. mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
  653. snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT,
  654. mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
  655. snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT,
  656. mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
  657. snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT,
  658. mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
  659. snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT,
  660. mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
  661. snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN,
  662. mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
  663. snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN,
  664. mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
  665. snd_cs4231_dout(chip, CS4231_MONO_CTRL,
  666. mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
  667. chip->calibrate_mute = mute;
  668. spin_unlock_irqrestore(&chip->lock, flags);
  669. }
  670. static void snd_cs4231_playback_format(struct snd_cs4231 *chip, struct snd_pcm_hw_params *params,
  671. unsigned char pdfr)
  672. {
  673. unsigned long flags;
  674. mutex_lock(&chip->mce_mutex);
  675. snd_cs4231_calibrate_mute(chip, 1);
  676. snd_cs4231_mce_up(chip);
  677. spin_lock_irqsave(&chip->lock, flags);
  678. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
  679. (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
  680. (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
  681. pdfr);
  682. spin_unlock_irqrestore(&chip->lock, flags);
  683. snd_cs4231_mce_down(chip);
  684. snd_cs4231_calibrate_mute(chip, 0);
  685. mutex_unlock(&chip->mce_mutex);
  686. }
  687. static void snd_cs4231_capture_format(struct snd_cs4231 *chip, struct snd_pcm_hw_params *params,
  688. unsigned char cdfr)
  689. {
  690. unsigned long flags;
  691. mutex_lock(&chip->mce_mutex);
  692. snd_cs4231_calibrate_mute(chip, 1);
  693. snd_cs4231_mce_up(chip);
  694. spin_lock_irqsave(&chip->lock, flags);
  695. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
  696. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
  697. ((chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
  698. (cdfr & 0x0f));
  699. spin_unlock_irqrestore(&chip->lock, flags);
  700. snd_cs4231_mce_down(chip);
  701. snd_cs4231_mce_up(chip);
  702. spin_lock_irqsave(&chip->lock, flags);
  703. }
  704. snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
  705. spin_unlock_irqrestore(&chip->lock, flags);
  706. snd_cs4231_mce_down(chip);
  707. snd_cs4231_calibrate_mute(chip, 0);
  708. mutex_unlock(&chip->mce_mutex);
  709. }
  710. /*
  711. * Timer interface
  712. */
  713. static unsigned long snd_cs4231_timer_resolution(struct snd_timer *timer)
  714. {
  715. struct snd_cs4231 *chip = snd_timer_chip(timer);
  716. return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
  717. }
  718. static int snd_cs4231_timer_start(struct snd_timer *timer)
  719. {
  720. unsigned long flags;
  721. unsigned int ticks;
  722. struct snd_cs4231 *chip = snd_timer_chip(timer);
  723. spin_lock_irqsave(&chip->lock, flags);
  724. ticks = timer->sticks;
  725. if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
  726. (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
  727. (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
  728. snd_cs4231_out(chip, CS4231_TIMER_HIGH,
  729. chip->image[CS4231_TIMER_HIGH] =
  730. (unsigned char) (ticks >> 8));
  731. snd_cs4231_out(chip, CS4231_TIMER_LOW,
  732. chip->image[CS4231_TIMER_LOW] =
  733. (unsigned char) ticks);
  734. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
  735. chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE);
  736. }
  737. spin_unlock_irqrestore(&chip->lock, flags);
  738. return 0;
  739. }
  740. static int snd_cs4231_timer_stop(struct snd_timer *timer)
  741. {
  742. unsigned long flags;
  743. struct snd_cs4231 *chip = snd_timer_chip(timer);
  744. spin_lock_irqsave(&chip->lock, flags);
  745. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1,
  746. chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE);
  747. spin_unlock_irqrestore(&chip->lock, flags);
  748. return 0;
  749. }
  750. static void __init snd_cs4231_init(struct snd_cs4231 *chip)
  751. {
  752. unsigned long flags;
  753. snd_cs4231_mce_down(chip);
  754. #ifdef SNDRV_DEBUG_MCE
  755. snd_printdd("init: (1)\n");
  756. #endif
  757. snd_cs4231_mce_up(chip);
  758. spin_lock_irqsave(&chip->lock, flags);
  759. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  760. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO |
  761. CS4231_CALIB_MODE);
  762. chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
  763. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  764. spin_unlock_irqrestore(&chip->lock, flags);
  765. snd_cs4231_mce_down(chip);
  766. #ifdef SNDRV_DEBUG_MCE
  767. snd_printdd("init: (2)\n");
  768. #endif
  769. snd_cs4231_mce_up(chip);
  770. spin_lock_irqsave(&chip->lock, flags);
  771. snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
  772. spin_unlock_irqrestore(&chip->lock, flags);
  773. snd_cs4231_mce_down(chip);
  774. #ifdef SNDRV_DEBUG_MCE
  775. snd_printdd("init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]);
  776. #endif
  777. spin_lock_irqsave(&chip->lock, flags);
  778. snd_cs4231_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]);
  779. spin_unlock_irqrestore(&chip->lock, flags);
  780. snd_cs4231_mce_up(chip);
  781. spin_lock_irqsave(&chip->lock, flags);
  782. snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]);
  783. spin_unlock_irqrestore(&chip->lock, flags);
  784. snd_cs4231_mce_down(chip);
  785. #ifdef SNDRV_DEBUG_MCE
  786. snd_printdd("init: (4)\n");
  787. #endif
  788. snd_cs4231_mce_up(chip);
  789. spin_lock_irqsave(&chip->lock, flags);
  790. snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
  791. spin_unlock_irqrestore(&chip->lock, flags);
  792. snd_cs4231_mce_down(chip);
  793. #ifdef SNDRV_DEBUG_MCE
  794. snd_printdd("init: (5)\n");
  795. #endif
  796. }
  797. static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
  798. {
  799. unsigned long flags;
  800. mutex_lock(&chip->open_mutex);
  801. if ((chip->mode & mode)) {
  802. mutex_unlock(&chip->open_mutex);
  803. return -EAGAIN;
  804. }
  805. if (chip->mode & CS4231_MODE_OPEN) {
  806. chip->mode |= mode;
  807. mutex_unlock(&chip->open_mutex);
  808. return 0;
  809. }
  810. /* ok. now enable and ack CODEC IRQ */
  811. spin_lock_irqsave(&chip->lock, flags);
  812. snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
  813. CS4231_RECORD_IRQ |
  814. CS4231_TIMER_IRQ);
  815. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  816. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
  817. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
  818. snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
  819. CS4231_RECORD_IRQ |
  820. CS4231_TIMER_IRQ);
  821. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  822. spin_unlock_irqrestore(&chip->lock, flags);
  823. chip->mode = mode;
  824. mutex_unlock(&chip->open_mutex);
  825. return 0;
  826. }
  827. static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
  828. {
  829. unsigned long flags;
  830. mutex_lock(&chip->open_mutex);
  831. chip->mode &= ~mode;
  832. if (chip->mode & CS4231_MODE_OPEN) {
  833. mutex_unlock(&chip->open_mutex);
  834. return;
  835. }
  836. snd_cs4231_calibrate_mute(chip, 1);
  837. /* disable IRQ */
  838. spin_lock_irqsave(&chip->lock, flags);
  839. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  840. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
  841. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
  842. /* now disable record & playback */
  843. if (chip->image[CS4231_IFACE_CTRL] &
  844. (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  845. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
  846. spin_unlock_irqrestore(&chip->lock, flags);
  847. snd_cs4231_mce_up(chip);
  848. spin_lock_irqsave(&chip->lock, flags);
  849. chip->image[CS4231_IFACE_CTRL] &=
  850. ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
  851. CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
  852. snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
  853. spin_unlock_irqrestore(&chip->lock, flags);
  854. snd_cs4231_mce_down(chip);
  855. spin_lock_irqsave(&chip->lock, flags);
  856. }
  857. /* clear IRQ again */
  858. snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
  859. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
  860. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS)); /* clear IRQ */
  861. spin_unlock_irqrestore(&chip->lock, flags);
  862. snd_cs4231_calibrate_mute(chip, 0);
  863. chip->mode = 0;
  864. mutex_unlock(&chip->open_mutex);
  865. }
  866. /*
  867. * timer open/close
  868. */
  869. static int snd_cs4231_timer_open(struct snd_timer *timer)
  870. {
  871. struct snd_cs4231 *chip = snd_timer_chip(timer);
  872. snd_cs4231_open(chip, CS4231_MODE_TIMER);
  873. return 0;
  874. }
  875. static int snd_cs4231_timer_close(struct snd_timer * timer)
  876. {
  877. struct snd_cs4231 *chip = snd_timer_chip(timer);
  878. snd_cs4231_close(chip, CS4231_MODE_TIMER);
  879. return 0;
  880. }
  881. static struct snd_timer_hardware snd_cs4231_timer_table =
  882. {
  883. .flags = SNDRV_TIMER_HW_AUTO,
  884. .resolution = 9945,
  885. .ticks = 65535,
  886. .open = snd_cs4231_timer_open,
  887. .close = snd_cs4231_timer_close,
  888. .c_resolution = snd_cs4231_timer_resolution,
  889. .start = snd_cs4231_timer_start,
  890. .stop = snd_cs4231_timer_stop,
  891. };
  892. /*
  893. * ok.. exported functions..
  894. */
  895. static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
  896. struct snd_pcm_hw_params *hw_params)
  897. {
  898. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  899. unsigned char new_pdfr;
  900. int err;
  901. if ((err = snd_pcm_lib_malloc_pages(substream,
  902. params_buffer_bytes(hw_params))) < 0)
  903. return err;
  904. new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params),
  905. params_channels(hw_params)) |
  906. snd_cs4231_get_rate(params_rate(hw_params));
  907. snd_cs4231_playback_format(chip, hw_params, new_pdfr);
  908. return 0;
  909. }
  910. static int snd_cs4231_playback_hw_free(struct snd_pcm_substream *substream)
  911. {
  912. return snd_pcm_lib_free_pages(substream);
  913. }
  914. static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
  915. {
  916. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  917. struct snd_pcm_runtime *runtime = substream->runtime;
  918. unsigned long flags;
  919. spin_lock_irqsave(&chip->lock, flags);
  920. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
  921. CS4231_PLAYBACK_PIO);
  922. BUG_ON(runtime->period_size > 0xffff + 1);
  923. chip->p_periods_sent = 0;
  924. spin_unlock_irqrestore(&chip->lock, flags);
  925. return 0;
  926. }
  927. static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
  928. struct snd_pcm_hw_params *hw_params)
  929. {
  930. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  931. unsigned char new_cdfr;
  932. int err;
  933. if ((err = snd_pcm_lib_malloc_pages(substream,
  934. params_buffer_bytes(hw_params))) < 0)
  935. return err;
  936. new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params),
  937. params_channels(hw_params)) |
  938. snd_cs4231_get_rate(params_rate(hw_params));
  939. snd_cs4231_capture_format(chip, hw_params, new_cdfr);
  940. return 0;
  941. }
  942. static int snd_cs4231_capture_hw_free(struct snd_pcm_substream *substream)
  943. {
  944. return snd_pcm_lib_free_pages(substream);
  945. }
  946. static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
  947. {
  948. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  949. unsigned long flags;
  950. spin_lock_irqsave(&chip->lock, flags);
  951. chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE |
  952. CS4231_RECORD_PIO);
  953. chip->c_periods_sent = 0;
  954. spin_unlock_irqrestore(&chip->lock, flags);
  955. return 0;
  956. }
  957. static void snd_cs4231_overrange(struct snd_cs4231 *chip)
  958. {
  959. unsigned long flags;
  960. unsigned char res;
  961. spin_lock_irqsave(&chip->lock, flags);
  962. res = snd_cs4231_in(chip, CS4231_TEST_INIT);
  963. spin_unlock_irqrestore(&chip->lock, flags);
  964. if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
  965. chip->capture_substream->runtime->overrange++;
  966. }
  967. static void snd_cs4231_play_callback(struct snd_cs4231 *chip)
  968. {
  969. if (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE) {
  970. snd_pcm_period_elapsed(chip->playback_substream);
  971. snd_cs4231_advance_dma(&chip->p_dma, chip->playback_substream,
  972. &chip->p_periods_sent);
  973. }
  974. }
  975. static void snd_cs4231_capture_callback(struct snd_cs4231 *chip)
  976. {
  977. if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) {
  978. snd_pcm_period_elapsed(chip->capture_substream);
  979. snd_cs4231_advance_dma(&chip->c_dma, chip->capture_substream,
  980. &chip->c_periods_sent);
  981. }
  982. }
  983. static snd_pcm_uframes_t snd_cs4231_playback_pointer(struct snd_pcm_substream *substream)
  984. {
  985. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  986. struct cs4231_dma_control *dma_cont = &chip->p_dma;
  987. size_t ptr;
  988. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
  989. return 0;
  990. ptr = dma_cont->address(dma_cont);
  991. if (ptr != 0)
  992. ptr -= substream->runtime->dma_addr;
  993. return bytes_to_frames(substream->runtime, ptr);
  994. }
  995. static snd_pcm_uframes_t snd_cs4231_capture_pointer(struct snd_pcm_substream *substream)
  996. {
  997. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  998. struct cs4231_dma_control *dma_cont = &chip->c_dma;
  999. size_t ptr;
  1000. if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
  1001. return 0;
  1002. ptr = dma_cont->address(dma_cont);
  1003. if (ptr != 0)
  1004. ptr -= substream->runtime->dma_addr;
  1005. return bytes_to_frames(substream->runtime, ptr);
  1006. }
  1007. /*
  1008. */
  1009. static int __init snd_cs4231_probe(struct snd_cs4231 *chip)
  1010. {
  1011. unsigned long flags;
  1012. int i, id, vers;
  1013. unsigned char *ptr;
  1014. id = vers = 0;
  1015. for (i = 0; i < 50; i++) {
  1016. mb();
  1017. if (__cs4231_readb(chip, CS4231P(chip, REGSEL)) & CS4231_INIT)
  1018. udelay(2000);
  1019. else {
  1020. spin_lock_irqsave(&chip->lock, flags);
  1021. snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
  1022. id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
  1023. vers = snd_cs4231_in(chip, CS4231_VERSION);
  1024. spin_unlock_irqrestore(&chip->lock, flags);
  1025. if (id == 0x0a)
  1026. break; /* this is valid value */
  1027. }
  1028. }
  1029. snd_printdd("cs4231: port = %p, id = 0x%x\n", chip->port, id);
  1030. if (id != 0x0a)
  1031. return -ENODEV; /* no valid device found */
  1032. spin_lock_irqsave(&chip->lock, flags);
  1033. /* Reset DMA engine (sbus only). */
  1034. chip->p_dma.reset(chip);
  1035. __cs4231_readb(chip, CS4231P(chip, STATUS)); /* clear any pendings IRQ */
  1036. __cs4231_writeb(chip, 0, CS4231P(chip, STATUS));
  1037. mb();
  1038. spin_unlock_irqrestore(&chip->lock, flags);
  1039. chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
  1040. chip->image[CS4231_IFACE_CTRL] =
  1041. chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA;
  1042. chip->image[CS4231_ALT_FEATURE_1] = 0x80;
  1043. chip->image[CS4231_ALT_FEATURE_2] = 0x01;
  1044. if (vers & 0x20)
  1045. chip->image[CS4231_ALT_FEATURE_2] |= 0x02;
  1046. ptr = (unsigned char *) &chip->image;
  1047. snd_cs4231_mce_down(chip);
  1048. spin_lock_irqsave(&chip->lock, flags);
  1049. for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
  1050. snd_cs4231_out(chip, i, *ptr++);
  1051. spin_unlock_irqrestore(&chip->lock, flags);
  1052. snd_cs4231_mce_up(chip);
  1053. snd_cs4231_mce_down(chip);
  1054. mdelay(2);
  1055. return 0; /* all things are ok.. */
  1056. }
  1057. static struct snd_pcm_hardware snd_cs4231_playback =
  1058. {
  1059. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1060. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
  1061. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  1062. SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1063. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1064. SNDRV_PCM_FMTBIT_S16_BE),
  1065. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1066. .rate_min = 5510,
  1067. .rate_max = 48000,
  1068. .channels_min = 1,
  1069. .channels_max = 2,
  1070. .buffer_bytes_max = (32*1024),
  1071. .period_bytes_min = 4096,
  1072. .period_bytes_max = (32*1024),
  1073. .periods_min = 1,
  1074. .periods_max = 1024,
  1075. };
  1076. static struct snd_pcm_hardware snd_cs4231_capture =
  1077. {
  1078. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1079. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
  1080. .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW |
  1081. SNDRV_PCM_FMTBIT_IMA_ADPCM |
  1082. SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1083. SNDRV_PCM_FMTBIT_S16_BE),
  1084. .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
  1085. .rate_min = 5510,
  1086. .rate_max = 48000,
  1087. .channels_min = 1,
  1088. .channels_max = 2,
  1089. .buffer_bytes_max = (32*1024),
  1090. .period_bytes_min = 4096,
  1091. .period_bytes_max = (32*1024),
  1092. .periods_min = 1,
  1093. .periods_max = 1024,
  1094. };
  1095. static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
  1096. {
  1097. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1098. struct snd_pcm_runtime *runtime = substream->runtime;
  1099. int err;
  1100. runtime->hw = snd_cs4231_playback;
  1101. if ((err = snd_cs4231_open(chip, CS4231_MODE_PLAY)) < 0) {
  1102. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1103. return err;
  1104. }
  1105. chip->playback_substream = substream;
  1106. chip->p_periods_sent = 0;
  1107. snd_pcm_set_sync(substream);
  1108. snd_cs4231_xrate(runtime);
  1109. return 0;
  1110. }
  1111. static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
  1112. {
  1113. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1114. struct snd_pcm_runtime *runtime = substream->runtime;
  1115. int err;
  1116. runtime->hw = snd_cs4231_capture;
  1117. if ((err = snd_cs4231_open(chip, CS4231_MODE_RECORD)) < 0) {
  1118. snd_free_pages(runtime->dma_area, runtime->dma_bytes);
  1119. return err;
  1120. }
  1121. chip->capture_substream = substream;
  1122. chip->c_periods_sent = 0;
  1123. snd_pcm_set_sync(substream);
  1124. snd_cs4231_xrate(runtime);
  1125. return 0;
  1126. }
  1127. static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
  1128. {
  1129. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1130. snd_cs4231_close(chip, CS4231_MODE_PLAY);
  1131. chip->playback_substream = NULL;
  1132. return 0;
  1133. }
  1134. static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
  1135. {
  1136. struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
  1137. snd_cs4231_close(chip, CS4231_MODE_RECORD);
  1138. chip->capture_substream = NULL;
  1139. return 0;
  1140. }
  1141. /* XXX We can do some power-management, in particular on EBUS using
  1142. * XXX the audio AUXIO register...
  1143. */
  1144. static struct snd_pcm_ops snd_cs4231_playback_ops = {
  1145. .open = snd_cs4231_playback_open,
  1146. .close = snd_cs4231_playback_close,
  1147. .ioctl = snd_pcm_lib_ioctl,
  1148. .hw_params = snd_cs4231_playback_hw_params,
  1149. .hw_free = snd_cs4231_playback_hw_free,
  1150. .prepare = snd_cs4231_playback_prepare,
  1151. .trigger = snd_cs4231_trigger,
  1152. .pointer = snd_cs4231_playback_pointer,
  1153. };
  1154. static struct snd_pcm_ops snd_cs4231_capture_ops = {
  1155. .open = snd_cs4231_capture_open,
  1156. .close = snd_cs4231_capture_close,
  1157. .ioctl = snd_pcm_lib_ioctl,
  1158. .hw_params = snd_cs4231_capture_hw_params,
  1159. .hw_free = snd_cs4231_capture_hw_free,
  1160. .prepare = snd_cs4231_capture_prepare,
  1161. .trigger = snd_cs4231_trigger,
  1162. .pointer = snd_cs4231_capture_pointer,
  1163. };
  1164. static int __init snd_cs4231_pcm(struct snd_cs4231 *chip)
  1165. {
  1166. struct snd_pcm *pcm;
  1167. int err;
  1168. if ((err = snd_pcm_new(chip->card, "CS4231", 0, 1, 1, &pcm)) < 0)
  1169. return err;
  1170. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4231_playback_ops);
  1171. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4231_capture_ops);
  1172. /* global setup */
  1173. pcm->private_data = chip;
  1174. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  1175. strcpy(pcm->name, "CS4231");
  1176. chip->p_dma.preallocate(chip, pcm);
  1177. chip->pcm = pcm;
  1178. return 0;
  1179. }
  1180. static int __init snd_cs4231_timer(struct snd_cs4231 *chip)
  1181. {
  1182. struct snd_timer *timer;
  1183. struct snd_timer_id tid;
  1184. int err;
  1185. /* Timer initialization */
  1186. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  1187. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  1188. tid.card = chip->card->number;
  1189. tid.device = 0;
  1190. tid.subdevice = 0;
  1191. if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
  1192. return err;
  1193. strcpy(timer->name, "CS4231");
  1194. timer->private_data = chip;
  1195. timer->hw = snd_cs4231_timer_table;
  1196. chip->timer = timer;
  1197. return 0;
  1198. }
  1199. /*
  1200. * MIXER part
  1201. */
  1202. static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol,
  1203. struct snd_ctl_elem_info *uinfo)
  1204. {
  1205. static char *texts[4] = {
  1206. "Line", "CD", "Mic", "Mix"
  1207. };
  1208. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1209. snd_assert(chip->card != NULL, return -EINVAL);
  1210. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1211. uinfo->count = 2;
  1212. uinfo->value.enumerated.items = 4;
  1213. if (uinfo->value.enumerated.item > 3)
  1214. uinfo->value.enumerated.item = 3;
  1215. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  1216. return 0;
  1217. }
  1218. static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol,
  1219. struct snd_ctl_elem_value *ucontrol)
  1220. {
  1221. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1222. unsigned long flags;
  1223. spin_lock_irqsave(&chip->lock, flags);
  1224. ucontrol->value.enumerated.item[0] =
  1225. (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1226. ucontrol->value.enumerated.item[1] =
  1227. (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
  1228. spin_unlock_irqrestore(&chip->lock, flags);
  1229. return 0;
  1230. }
  1231. static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol,
  1232. struct snd_ctl_elem_value *ucontrol)
  1233. {
  1234. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1235. unsigned long flags;
  1236. unsigned short left, right;
  1237. int change;
  1238. if (ucontrol->value.enumerated.item[0] > 3 ||
  1239. ucontrol->value.enumerated.item[1] > 3)
  1240. return -EINVAL;
  1241. left = ucontrol->value.enumerated.item[0] << 6;
  1242. right = ucontrol->value.enumerated.item[1] << 6;
  1243. spin_lock_irqsave(&chip->lock, flags);
  1244. left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
  1245. right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
  1246. change = left != chip->image[CS4231_LEFT_INPUT] ||
  1247. right != chip->image[CS4231_RIGHT_INPUT];
  1248. snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
  1249. snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
  1250. spin_unlock_irqrestore(&chip->lock, flags);
  1251. return change;
  1252. }
  1253. static int snd_cs4231_info_single(struct snd_kcontrol *kcontrol,
  1254. struct snd_ctl_elem_info *uinfo)
  1255. {
  1256. int mask = (kcontrol->private_value >> 16) & 0xff;
  1257. uinfo->type = (mask == 1) ?
  1258. SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1259. uinfo->count = 1;
  1260. uinfo->value.integer.min = 0;
  1261. uinfo->value.integer.max = mask;
  1262. return 0;
  1263. }
  1264. static int snd_cs4231_get_single(struct snd_kcontrol *kcontrol,
  1265. struct snd_ctl_elem_value *ucontrol)
  1266. {
  1267. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1268. unsigned long flags;
  1269. int reg = kcontrol->private_value & 0xff;
  1270. int shift = (kcontrol->private_value >> 8) & 0xff;
  1271. int mask = (kcontrol->private_value >> 16) & 0xff;
  1272. int invert = (kcontrol->private_value >> 24) & 0xff;
  1273. spin_lock_irqsave(&chip->lock, flags);
  1274. ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
  1275. spin_unlock_irqrestore(&chip->lock, flags);
  1276. if (invert)
  1277. ucontrol->value.integer.value[0] =
  1278. (mask - ucontrol->value.integer.value[0]);
  1279. return 0;
  1280. }
  1281. static int snd_cs4231_put_single(struct snd_kcontrol *kcontrol,
  1282. struct snd_ctl_elem_value *ucontrol)
  1283. {
  1284. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1285. unsigned long flags;
  1286. int reg = kcontrol->private_value & 0xff;
  1287. int shift = (kcontrol->private_value >> 8) & 0xff;
  1288. int mask = (kcontrol->private_value >> 16) & 0xff;
  1289. int invert = (kcontrol->private_value >> 24) & 0xff;
  1290. int change;
  1291. unsigned short val;
  1292. val = (ucontrol->value.integer.value[0] & mask);
  1293. if (invert)
  1294. val = mask - val;
  1295. val <<= shift;
  1296. spin_lock_irqsave(&chip->lock, flags);
  1297. val = (chip->image[reg] & ~(mask << shift)) | val;
  1298. change = val != chip->image[reg];
  1299. snd_cs4231_out(chip, reg, val);
  1300. spin_unlock_irqrestore(&chip->lock, flags);
  1301. return change;
  1302. }
  1303. static int snd_cs4231_info_double(struct snd_kcontrol *kcontrol,
  1304. struct snd_ctl_elem_info *uinfo)
  1305. {
  1306. int mask = (kcontrol->private_value >> 24) & 0xff;
  1307. uinfo->type = mask == 1 ?
  1308. SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1309. uinfo->count = 2;
  1310. uinfo->value.integer.min = 0;
  1311. uinfo->value.integer.max = mask;
  1312. return 0;
  1313. }
  1314. static int snd_cs4231_get_double(struct snd_kcontrol *kcontrol,
  1315. struct snd_ctl_elem_value *ucontrol)
  1316. {
  1317. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1318. unsigned long flags;
  1319. int left_reg = kcontrol->private_value & 0xff;
  1320. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1321. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1322. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1323. int mask = (kcontrol->private_value >> 24) & 0xff;
  1324. int invert = (kcontrol->private_value >> 22) & 1;
  1325. spin_lock_irqsave(&chip->lock, flags);
  1326. ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
  1327. ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
  1328. spin_unlock_irqrestore(&chip->lock, flags);
  1329. if (invert) {
  1330. ucontrol->value.integer.value[0] =
  1331. (mask - ucontrol->value.integer.value[0]);
  1332. ucontrol->value.integer.value[1] =
  1333. (mask - ucontrol->value.integer.value[1]);
  1334. }
  1335. return 0;
  1336. }
  1337. static int snd_cs4231_put_double(struct snd_kcontrol *kcontrol,
  1338. struct snd_ctl_elem_value *ucontrol)
  1339. {
  1340. struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
  1341. unsigned long flags;
  1342. int left_reg = kcontrol->private_value & 0xff;
  1343. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  1344. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  1345. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  1346. int mask = (kcontrol->private_value >> 24) & 0xff;
  1347. int invert = (kcontrol->private_value >> 22) & 1;
  1348. int change;
  1349. unsigned short val1, val2;
  1350. val1 = ucontrol->value.integer.value[0] & mask;
  1351. val2 = ucontrol->value.integer.value[1] & mask;
  1352. if (invert) {
  1353. val1 = mask - val1;
  1354. val2 = mask - val2;
  1355. }
  1356. val1 <<= shift_left;
  1357. val2 <<= shift_right;
  1358. spin_lock_irqsave(&chip->lock, flags);
  1359. val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
  1360. val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
  1361. change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
  1362. snd_cs4231_out(chip, left_reg, val1);
  1363. snd_cs4231_out(chip, right_reg, val2);
  1364. spin_unlock_irqrestore(&chip->lock, flags);
  1365. return change;
  1366. }
  1367. #define CS4231_SINGLE(xname, xindex, reg, shift, mask, invert) \
  1368. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1369. .info = snd_cs4231_info_single, \
  1370. .get = snd_cs4231_get_single, .put = snd_cs4231_put_single, \
  1371. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  1372. #define CS4231_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  1373. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  1374. .info = snd_cs4231_info_double, \
  1375. .get = snd_cs4231_get_double, .put = snd_cs4231_put_double, \
  1376. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  1377. static struct snd_kcontrol_new snd_cs4231_controls[] __initdata = {
  1378. CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
  1379. CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
  1380. CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  1381. CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
  1382. CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
  1383. CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
  1384. CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
  1385. CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
  1386. CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
  1387. CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
  1388. CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
  1389. CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
  1390. CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
  1391. {
  1392. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1393. .name = "Capture Source",
  1394. .info = snd_cs4231_info_mux,
  1395. .get = snd_cs4231_get_mux,
  1396. .put = snd_cs4231_put_mux,
  1397. },
  1398. CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
  1399. CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
  1400. CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1),
  1401. /* SPARC specific uses of XCTL{0,1} general purpose outputs. */
  1402. CS4231_SINGLE("Line Out Switch", 0, CS4231_PIN_CTRL, 6, 1, 1),
  1403. CS4231_SINGLE("Headphone Out Switch", 0, CS4231_PIN_CTRL, 7, 1, 1)
  1404. };
  1405. static int __init snd_cs4231_mixer(struct snd_cs4231 *chip)
  1406. {
  1407. struct snd_card *card;
  1408. int err, idx;
  1409. snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
  1410. card = chip->card;
  1411. strcpy(card->mixername, chip->pcm->name);
  1412. for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
  1413. if ((err = snd_ctl_add(card,
  1414. snd_ctl_new1(&snd_cs4231_controls[idx],
  1415. chip))) < 0)
  1416. return err;
  1417. }
  1418. return 0;
  1419. }
  1420. static int dev;
  1421. static int __init cs4231_attach_begin(struct snd_card **rcard)
  1422. {
  1423. struct snd_card *card;
  1424. *rcard = NULL;
  1425. if (dev >= SNDRV_CARDS)
  1426. return -ENODEV;
  1427. if (!enable[dev]) {
  1428. dev++;
  1429. return -ENOENT;
  1430. }
  1431. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1432. if (card == NULL)
  1433. return -ENOMEM;
  1434. strcpy(card->driver, "CS4231");
  1435. strcpy(card->shortname, "Sun CS4231");
  1436. *rcard = card;
  1437. return 0;
  1438. }
  1439. static int __init cs4231_attach_finish(struct snd_card *card, struct snd_cs4231 *chip)
  1440. {
  1441. int err;
  1442. if ((err = snd_cs4231_pcm(chip)) < 0)
  1443. goto out_err;
  1444. if ((err = snd_cs4231_mixer(chip)) < 0)
  1445. goto out_err;
  1446. if ((err = snd_cs4231_timer(chip)) < 0)
  1447. goto out_err;
  1448. if ((err = snd_card_register(card)) < 0)
  1449. goto out_err;
  1450. chip->next = cs4231_list;
  1451. cs4231_list = chip;
  1452. dev++;
  1453. return 0;
  1454. out_err:
  1455. snd_card_free(card);
  1456. return err;
  1457. }
  1458. #ifdef SBUS_SUPPORT
  1459. static irqreturn_t snd_cs4231_sbus_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1460. {
  1461. unsigned long flags;
  1462. unsigned char status;
  1463. u32 csr;
  1464. struct snd_cs4231 *chip = dev_id;
  1465. /*This is IRQ is not raised by the cs4231*/
  1466. if (!(__cs4231_readb(chip, CS4231P(chip, STATUS)) & CS4231_GLOBALIRQ))
  1467. return IRQ_NONE;
  1468. /* ACK the APC interrupt. */
  1469. csr = sbus_readl(chip->port + APCCSR);
  1470. sbus_writel(csr, chip->port + APCCSR);
  1471. if ((csr & APC_PDMA_READY) &&
  1472. (csr & APC_PLAY_INT) &&
  1473. (csr & APC_XINT_PNVA) &&
  1474. !(csr & APC_XINT_EMPT))
  1475. snd_cs4231_play_callback(chip);
  1476. if ((csr & APC_CDMA_READY) &&
  1477. (csr & APC_CAPT_INT) &&
  1478. (csr & APC_XINT_CNVA) &&
  1479. !(csr & APC_XINT_EMPT))
  1480. snd_cs4231_capture_callback(chip);
  1481. status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
  1482. if (status & CS4231_TIMER_IRQ) {
  1483. if (chip->timer)
  1484. snd_timer_interrupt(chip->timer, chip->timer->sticks);
  1485. }
  1486. if ((status & CS4231_RECORD_IRQ) && (csr & APC_CDMA_READY))
  1487. snd_cs4231_overrange(chip);
  1488. /* ACK the CS4231 interrupt. */
  1489. spin_lock_irqsave(&chip->lock, flags);
  1490. snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
  1491. spin_unlock_irqrestore(&chip->lock, flags);
  1492. return 0;
  1493. }
  1494. /*
  1495. * SBUS DMA routines
  1496. */
  1497. static int sbus_dma_request(struct cs4231_dma_control *dma_cont, dma_addr_t bus_addr, size_t len)
  1498. {
  1499. unsigned long flags;
  1500. u32 test, csr;
  1501. int err;
  1502. struct sbus_dma_info *base = &dma_cont->sbus_info;
  1503. if (len >= (1 << 24))
  1504. return -EINVAL;
  1505. spin_lock_irqsave(&base->lock, flags);
  1506. csr = sbus_readl(base->regs + APCCSR);
  1507. err = -EINVAL;
  1508. test = APC_CDMA_READY;
  1509. if ( base->dir == APC_PLAY )
  1510. test = APC_PDMA_READY;
  1511. if (!(csr & test))
  1512. goto out;
  1513. err = -EBUSY;
  1514. csr = sbus_readl(base->regs + APCCSR);
  1515. test = APC_XINT_CNVA;
  1516. if ( base->dir == APC_PLAY )
  1517. test = APC_XINT_PNVA;
  1518. if (!(csr & test))
  1519. goto out;
  1520. err = 0;
  1521. sbus_writel(bus_addr, base->regs + base->dir + APCNVA);
  1522. sbus_writel(len, base->regs + base->dir + APCNC);
  1523. out:
  1524. spin_unlock_irqrestore(&base->lock, flags);
  1525. return err;
  1526. }
  1527. static void sbus_dma_prepare(struct cs4231_dma_control *dma_cont, int d)
  1528. {
  1529. unsigned long flags;
  1530. u32 csr, test;
  1531. struct sbus_dma_info *base = &dma_cont->sbus_info;
  1532. spin_lock_irqsave(&base->lock, flags);
  1533. csr = sbus_readl(base->regs + APCCSR);
  1534. test = APC_GENL_INT | APC_PLAY_INT | APC_XINT_ENA |
  1535. APC_XINT_PLAY | APC_XINT_PEMP | APC_XINT_GENL |
  1536. APC_XINT_PENA;
  1537. if ( base->dir == APC_RECORD )
  1538. test = APC_GENL_INT | APC_CAPT_INT | APC_XINT_ENA |
  1539. APC_XINT_CAPT | APC_XINT_CEMP | APC_XINT_GENL;
  1540. csr |= test;
  1541. sbus_writel(csr, base->regs + APCCSR);
  1542. spin_unlock_irqrestore(&base->lock, flags);
  1543. }
  1544. static void sbus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
  1545. {
  1546. unsigned long flags;
  1547. u32 csr, shift;
  1548. struct sbus_dma_info *base = &dma_cont->sbus_info;
  1549. spin_lock_irqsave(&base->lock, flags);
  1550. if (!on) {
  1551. if (base->dir == APC_PLAY) {
  1552. sbus_writel(0, base->regs + base->dir + APCNVA);
  1553. sbus_writel(1, base->regs + base->dir + APCC);
  1554. }
  1555. else
  1556. {
  1557. sbus_writel(0, base->regs + base->dir + APCNC);
  1558. sbus_writel(0, base->regs + base->dir + APCVA);
  1559. }
  1560. }
  1561. udelay(600);
  1562. csr = sbus_readl(base->regs + APCCSR);
  1563. shift = 0;
  1564. if ( base->dir == APC_PLAY )
  1565. shift = 1;
  1566. if (on)
  1567. csr &= ~(APC_CPAUSE << shift);
  1568. else
  1569. csr |= (APC_CPAUSE << shift);
  1570. sbus_writel(csr, base->regs + APCCSR);
  1571. if (on)
  1572. csr |= (APC_CDMA_READY << shift);
  1573. else
  1574. csr &= ~(APC_CDMA_READY << shift);
  1575. sbus_writel(csr, base->regs + APCCSR);
  1576. spin_unlock_irqrestore(&base->lock, flags);
  1577. }
  1578. static unsigned int sbus_dma_addr(struct cs4231_dma_control *dma_cont)
  1579. {
  1580. struct sbus_dma_info *base = &dma_cont->sbus_info;
  1581. return sbus_readl(base->regs + base->dir + APCVA);
  1582. }
  1583. static void sbus_dma_reset(struct snd_cs4231 *chip)
  1584. {
  1585. sbus_writel(APC_CHIP_RESET, chip->port + APCCSR);
  1586. sbus_writel(0x00, chip->port + APCCSR);
  1587. sbus_writel(sbus_readl(chip->port + APCCSR) | APC_CDC_RESET,
  1588. chip->port + APCCSR);
  1589. udelay(20);
  1590. sbus_writel(sbus_readl(chip->port + APCCSR) & ~APC_CDC_RESET,
  1591. chip->port + APCCSR);
  1592. sbus_writel(sbus_readl(chip->port + APCCSR) | (APC_XINT_ENA |
  1593. APC_XINT_PENA |
  1594. APC_XINT_CENA),
  1595. chip->port + APCCSR);
  1596. }
  1597. static void sbus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
  1598. {
  1599. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_SBUS,
  1600. snd_dma_sbus_data(chip->dev_u.sdev),
  1601. 64*1024, 128*1024);
  1602. }
  1603. /*
  1604. * Init and exit routines
  1605. */
  1606. static int snd_cs4231_sbus_free(struct snd_cs4231 *chip)
  1607. {
  1608. if (chip->irq[0])
  1609. free_irq(chip->irq[0], chip);
  1610. if (chip->port)
  1611. sbus_iounmap(chip->port, chip->regs_size);
  1612. kfree(chip);
  1613. return 0;
  1614. }
  1615. static int snd_cs4231_sbus_dev_free(struct snd_device *device)
  1616. {
  1617. struct snd_cs4231 *cp = device->device_data;
  1618. return snd_cs4231_sbus_free(cp);
  1619. }
  1620. static struct snd_device_ops snd_cs4231_sbus_dev_ops = {
  1621. .dev_free = snd_cs4231_sbus_dev_free,
  1622. };
  1623. static int __init snd_cs4231_sbus_create(struct snd_card *card,
  1624. struct sbus_dev *sdev,
  1625. int dev,
  1626. struct snd_cs4231 **rchip)
  1627. {
  1628. struct snd_cs4231 *chip;
  1629. int err;
  1630. *rchip = NULL;
  1631. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1632. if (chip == NULL)
  1633. return -ENOMEM;
  1634. spin_lock_init(&chip->lock);
  1635. spin_lock_init(&chip->c_dma.sbus_info.lock);
  1636. spin_lock_init(&chip->p_dma.sbus_info.lock);
  1637. mutex_init(&chip->mce_mutex);
  1638. mutex_init(&chip->open_mutex);
  1639. chip->card = card;
  1640. chip->dev_u.sdev = sdev;
  1641. chip->regs_size = sdev->reg_addrs[0].reg_size;
  1642. memcpy(&chip->image, &snd_cs4231_original_image,
  1643. sizeof(snd_cs4231_original_image));
  1644. chip->port = sbus_ioremap(&sdev->resource[0], 0,
  1645. chip->regs_size, "cs4231");
  1646. if (!chip->port) {
  1647. snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
  1648. return -EIO;
  1649. }
  1650. chip->c_dma.sbus_info.regs = chip->port;
  1651. chip->p_dma.sbus_info.regs = chip->port;
  1652. chip->c_dma.sbus_info.dir = APC_RECORD;
  1653. chip->p_dma.sbus_info.dir = APC_PLAY;
  1654. chip->p_dma.prepare = sbus_dma_prepare;
  1655. chip->p_dma.enable = sbus_dma_enable;
  1656. chip->p_dma.request = sbus_dma_request;
  1657. chip->p_dma.address = sbus_dma_addr;
  1658. chip->p_dma.reset = sbus_dma_reset;
  1659. chip->p_dma.preallocate = sbus_dma_preallocate;
  1660. chip->c_dma.prepare = sbus_dma_prepare;
  1661. chip->c_dma.enable = sbus_dma_enable;
  1662. chip->c_dma.request = sbus_dma_request;
  1663. chip->c_dma.address = sbus_dma_addr;
  1664. chip->c_dma.reset = sbus_dma_reset;
  1665. chip->c_dma.preallocate = sbus_dma_preallocate;
  1666. if (request_irq(sdev->irqs[0], snd_cs4231_sbus_interrupt,
  1667. SA_SHIRQ, "cs4231", chip)) {
  1668. snd_printdd("cs4231-%d: Unable to grab SBUS IRQ %s\n",
  1669. dev,
  1670. __irq_itoa(sdev->irqs[0]));
  1671. snd_cs4231_sbus_free(chip);
  1672. return -EBUSY;
  1673. }
  1674. chip->irq[0] = sdev->irqs[0];
  1675. if (snd_cs4231_probe(chip) < 0) {
  1676. snd_cs4231_sbus_free(chip);
  1677. return -ENODEV;
  1678. }
  1679. snd_cs4231_init(chip);
  1680. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  1681. chip, &snd_cs4231_sbus_dev_ops)) < 0) {
  1682. snd_cs4231_sbus_free(chip);
  1683. return err;
  1684. }
  1685. *rchip = chip;
  1686. return 0;
  1687. }
  1688. static int __init cs4231_sbus_attach(struct sbus_dev *sdev)
  1689. {
  1690. struct resource *rp = &sdev->resource[0];
  1691. struct snd_cs4231 *cp;
  1692. struct snd_card *card;
  1693. int err;
  1694. err = cs4231_attach_begin(&card);
  1695. if (err)
  1696. return err;
  1697. sprintf(card->longname, "%s at 0x%02lx:0x%08lx, irq %s",
  1698. card->shortname,
  1699. rp->flags & 0xffL,
  1700. rp->start,
  1701. __irq_itoa(sdev->irqs[0]));
  1702. if ((err = snd_cs4231_sbus_create(card, sdev, dev, &cp)) < 0) {
  1703. snd_card_free(card);
  1704. return err;
  1705. }
  1706. return cs4231_attach_finish(card, cp);
  1707. }
  1708. #endif
  1709. #ifdef EBUS_SUPPORT
  1710. static void snd_cs4231_ebus_play_callback(struct ebus_dma_info *p, int event, void *cookie)
  1711. {
  1712. struct snd_cs4231 *chip = cookie;
  1713. snd_cs4231_play_callback(chip);
  1714. }
  1715. static void snd_cs4231_ebus_capture_callback(struct ebus_dma_info *p, int event, void *cookie)
  1716. {
  1717. struct snd_cs4231 *chip = cookie;
  1718. snd_cs4231_capture_callback(chip);
  1719. }
  1720. /*
  1721. * EBUS DMA wrappers
  1722. */
  1723. static int _ebus_dma_request(struct cs4231_dma_control *dma_cont, dma_addr_t bus_addr, size_t len)
  1724. {
  1725. return ebus_dma_request(&dma_cont->ebus_info, bus_addr, len);
  1726. }
  1727. static void _ebus_dma_enable(struct cs4231_dma_control *dma_cont, int on)
  1728. {
  1729. ebus_dma_enable(&dma_cont->ebus_info, on);
  1730. }
  1731. static void _ebus_dma_prepare(struct cs4231_dma_control *dma_cont, int dir)
  1732. {
  1733. ebus_dma_prepare(&dma_cont->ebus_info, dir);
  1734. }
  1735. static unsigned int _ebus_dma_addr(struct cs4231_dma_control *dma_cont)
  1736. {
  1737. return ebus_dma_addr(&dma_cont->ebus_info);
  1738. }
  1739. static void _ebus_dma_reset(struct snd_cs4231 *chip)
  1740. {
  1741. return;
  1742. }
  1743. static void _ebus_dma_preallocate(struct snd_cs4231 *chip, struct snd_pcm *pcm)
  1744. {
  1745. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1746. snd_dma_pci_data(chip->dev_u.pdev),
  1747. 64*1024, 128*1024);
  1748. }
  1749. /*
  1750. * Init and exit routines
  1751. */
  1752. static int snd_cs4231_ebus_free(struct snd_cs4231 *chip)
  1753. {
  1754. if (chip->c_dma.ebus_info.regs) {
  1755. ebus_dma_unregister(&chip->c_dma.ebus_info);
  1756. iounmap(chip->c_dma.ebus_info.regs);
  1757. }
  1758. if (chip->p_dma.ebus_info.regs) {
  1759. ebus_dma_unregister(&chip->p_dma.ebus_info);
  1760. iounmap(chip->p_dma.ebus_info.regs);
  1761. }
  1762. if (chip->port)
  1763. iounmap(chip->port);
  1764. kfree(chip);
  1765. return 0;
  1766. }
  1767. static int snd_cs4231_ebus_dev_free(struct snd_device *device)
  1768. {
  1769. struct snd_cs4231 *cp = device->device_data;
  1770. return snd_cs4231_ebus_free(cp);
  1771. }
  1772. static struct snd_device_ops snd_cs4231_ebus_dev_ops = {
  1773. .dev_free = snd_cs4231_ebus_dev_free,
  1774. };
  1775. static int __init snd_cs4231_ebus_create(struct snd_card *card,
  1776. struct linux_ebus_device *edev,
  1777. int dev,
  1778. struct snd_cs4231 **rchip)
  1779. {
  1780. struct snd_cs4231 *chip;
  1781. int err;
  1782. *rchip = NULL;
  1783. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1784. if (chip == NULL)
  1785. return -ENOMEM;
  1786. spin_lock_init(&chip->lock);
  1787. spin_lock_init(&chip->c_dma.ebus_info.lock);
  1788. spin_lock_init(&chip->p_dma.ebus_info.lock);
  1789. mutex_init(&chip->mce_mutex);
  1790. mutex_init(&chip->open_mutex);
  1791. chip->flags |= CS4231_FLAG_EBUS;
  1792. chip->card = card;
  1793. chip->dev_u.pdev = edev->bus->self;
  1794. memcpy(&chip->image, &snd_cs4231_original_image,
  1795. sizeof(snd_cs4231_original_image));
  1796. strcpy(chip->c_dma.ebus_info.name, "cs4231(capture)");
  1797. chip->c_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
  1798. chip->c_dma.ebus_info.callback = snd_cs4231_ebus_capture_callback;
  1799. chip->c_dma.ebus_info.client_cookie = chip;
  1800. chip->c_dma.ebus_info.irq = edev->irqs[0];
  1801. strcpy(chip->p_dma.ebus_info.name, "cs4231(play)");
  1802. chip->p_dma.ebus_info.flags = EBUS_DMA_FLAG_USE_EBDMA_HANDLER;
  1803. chip->p_dma.ebus_info.callback = snd_cs4231_ebus_play_callback;
  1804. chip->p_dma.ebus_info.client_cookie = chip;
  1805. chip->p_dma.ebus_info.irq = edev->irqs[1];
  1806. chip->p_dma.prepare = _ebus_dma_prepare;
  1807. chip->p_dma.enable = _ebus_dma_enable;
  1808. chip->p_dma.request = _ebus_dma_request;
  1809. chip->p_dma.address = _ebus_dma_addr;
  1810. chip->p_dma.reset = _ebus_dma_reset;
  1811. chip->p_dma.preallocate = _ebus_dma_preallocate;
  1812. chip->c_dma.prepare = _ebus_dma_prepare;
  1813. chip->c_dma.enable = _ebus_dma_enable;
  1814. chip->c_dma.request = _ebus_dma_request;
  1815. chip->c_dma.address = _ebus_dma_addr;
  1816. chip->c_dma.reset = _ebus_dma_reset;
  1817. chip->c_dma.preallocate = _ebus_dma_preallocate;
  1818. chip->port = ioremap(edev->resource[0].start, 0x10);
  1819. chip->p_dma.ebus_info.regs = ioremap(edev->resource[1].start, 0x10);
  1820. chip->c_dma.ebus_info.regs = ioremap(edev->resource[2].start, 0x10);
  1821. if (!chip->port || !chip->p_dma.ebus_info.regs || !chip->c_dma.ebus_info.regs) {
  1822. snd_cs4231_ebus_free(chip);
  1823. snd_printdd("cs4231-%d: Unable to map chip registers.\n", dev);
  1824. return -EIO;
  1825. }
  1826. if (ebus_dma_register(&chip->c_dma.ebus_info)) {
  1827. snd_cs4231_ebus_free(chip);
  1828. snd_printdd("cs4231-%d: Unable to register EBUS capture DMA\n", dev);
  1829. return -EBUSY;
  1830. }
  1831. if (ebus_dma_irq_enable(&chip->c_dma.ebus_info, 1)) {
  1832. snd_cs4231_ebus_free(chip);
  1833. snd_printdd("cs4231-%d: Unable to enable EBUS capture IRQ\n", dev);
  1834. return -EBUSY;
  1835. }
  1836. if (ebus_dma_register(&chip->p_dma.ebus_info)) {
  1837. snd_cs4231_ebus_free(chip);
  1838. snd_printdd("cs4231-%d: Unable to register EBUS play DMA\n", dev);
  1839. return -EBUSY;
  1840. }
  1841. if (ebus_dma_irq_enable(&chip->p_dma.ebus_info, 1)) {
  1842. snd_cs4231_ebus_free(chip);
  1843. snd_printdd("cs4231-%d: Unable to enable EBUS play IRQ\n", dev);
  1844. return -EBUSY;
  1845. }
  1846. if (snd_cs4231_probe(chip) < 0) {
  1847. snd_cs4231_ebus_free(chip);
  1848. return -ENODEV;
  1849. }
  1850. snd_cs4231_init(chip);
  1851. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
  1852. chip, &snd_cs4231_ebus_dev_ops)) < 0) {
  1853. snd_cs4231_ebus_free(chip);
  1854. return err;
  1855. }
  1856. *rchip = chip;
  1857. return 0;
  1858. }
  1859. static int __init cs4231_ebus_attach(struct linux_ebus_device *edev)
  1860. {
  1861. struct snd_card *card;
  1862. struct snd_cs4231 *chip;
  1863. int err;
  1864. err = cs4231_attach_begin(&card);
  1865. if (err)
  1866. return err;
  1867. sprintf(card->longname, "%s at 0x%lx, irq %s",
  1868. card->shortname,
  1869. edev->resource[0].start,
  1870. __irq_itoa(edev->irqs[0]));
  1871. if ((err = snd_cs4231_ebus_create(card, edev, dev, &chip)) < 0) {
  1872. snd_card_free(card);
  1873. return err;
  1874. }
  1875. return cs4231_attach_finish(card, chip);
  1876. }
  1877. #endif
  1878. static int __init cs4231_init(void)
  1879. {
  1880. #ifdef SBUS_SUPPORT
  1881. struct sbus_bus *sbus;
  1882. struct sbus_dev *sdev;
  1883. #endif
  1884. #ifdef EBUS_SUPPORT
  1885. struct linux_ebus *ebus;
  1886. struct linux_ebus_device *edev;
  1887. #endif
  1888. int found;
  1889. found = 0;
  1890. #ifdef SBUS_SUPPORT
  1891. for_all_sbusdev(sdev, sbus) {
  1892. if (!strcmp(sdev->prom_name, "SUNW,CS4231")) {
  1893. if (cs4231_sbus_attach(sdev) == 0)
  1894. found++;
  1895. }
  1896. }
  1897. #endif
  1898. #ifdef EBUS_SUPPORT
  1899. for_each_ebus(ebus) {
  1900. for_each_ebusdev(edev, ebus) {
  1901. int match = 0;
  1902. if (!strcmp(edev->prom_name, "SUNW,CS4231")) {
  1903. match = 1;
  1904. } else if (!strcmp(edev->prom_name, "audio")) {
  1905. char compat[16];
  1906. prom_getstring(edev->prom_node, "compatible",
  1907. compat, sizeof(compat));
  1908. compat[15] = '\0';
  1909. if (!strcmp(compat, "SUNW,CS4231"))
  1910. match = 1;
  1911. }
  1912. if (match &&
  1913. cs4231_ebus_attach(edev) == 0)
  1914. found++;
  1915. }
  1916. }
  1917. #endif
  1918. return (found > 0) ? 0 : -EIO;
  1919. }
  1920. static void __exit cs4231_exit(void)
  1921. {
  1922. struct snd_cs4231 *p = cs4231_list;
  1923. while (p != NULL) {
  1924. struct snd_cs4231 *next = p->next;
  1925. snd_card_free(p->card);
  1926. p = next;
  1927. }
  1928. cs4231_list = NULL;
  1929. }
  1930. module_init(cs4231_init);
  1931. module_exit(cs4231_exit);