cs4231.c 65 KB

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