cs4231.c 62 KB

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