cs4231.c 64 KB

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