cs4231.c 65 KB

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