cs4231.c 65 KB

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