cs4231.c 64 KB

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