es1968.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for ESS Maestro 1/2/2E Sound Card (started 21.8.99)
  4. * Copyright (c) by Matze Braun <[email protected]>.
  5. * Takashi Iwai <[email protected]>
  6. *
  7. * Most of the driver code comes from Zach Brown([email protected])
  8. * Alan Cox OSS Driver
  9. * Rewritted from card-es1938.c source.
  10. *
  11. * TODO:
  12. * Perhaps Synth
  13. *
  14. * Notes from Zach Brown about the driver code
  15. *
  16. * Hardware Description
  17. *
  18. * A working Maestro setup contains the Maestro chip wired to a
  19. * codec or 2. In the Maestro we have the APUs, the ASSP, and the
  20. * Wavecache. The APUs can be though of as virtual audio routing
  21. * channels. They can take data from a number of sources and perform
  22. * basic encodings of the data. The wavecache is a storehouse for
  23. * PCM data. Typically it deals with PCI and interracts with the
  24. * APUs. The ASSP is a wacky DSP like device that ESS is loth
  25. * to release docs on. Thankfully it isn't required on the Maestro
  26. * until you start doing insane things like FM emulation and surround
  27. * encoding. The codecs are almost always AC-97 compliant codecs,
  28. * but it appears that early Maestros may have had PT101 (an ESS
  29. * part?) wired to them. The only real difference in the Maestro
  30. * families is external goop like docking capability, memory for
  31. * the ASSP, and initialization differences.
  32. *
  33. * Driver Operation
  34. *
  35. * We only drive the APU/Wavecache as typical DACs and drive the
  36. * mixers in the codecs. There are 64 APUs. We assign 6 to each
  37. * /dev/dsp? device. 2 channels for output, and 4 channels for
  38. * input.
  39. *
  40. * Each APU can do a number of things, but we only really use
  41. * 3 basic functions. For playback we use them to convert PCM
  42. * data fetched over PCI by the wavecahche into analog data that
  43. * is handed to the codec. One APU for mono, and a pair for stereo.
  44. * When in stereo, the combination of smarts in the APU and Wavecache
  45. * decide which wavecache gets the left or right channel.
  46. *
  47. * For record we still use the old overly mono system. For each in
  48. * coming channel the data comes in from the codec, through a 'input'
  49. * APU, through another rate converter APU, and then into memory via
  50. * the wavecache and PCI. If its stereo, we mash it back into LRLR in
  51. * software. The pass between the 2 APUs is supposedly what requires us
  52. * to have a 512 byte buffer sitting around in wavecache/memory.
  53. *
  54. * The wavecache makes our life even more fun. First off, it can
  55. * only address the first 28 bits of PCI address space, making it
  56. * useless on quite a few architectures. Secondly, its insane.
  57. * It claims to fetch from 4 regions of PCI space, each 4 meg in length.
  58. * But that doesn't really work. You can only use 1 region. So all our
  59. * allocations have to be in 4meg of each other. Booo. Hiss.
  60. * So we have a module parameter, dsps_order, that is the order of
  61. * the number of dsps to provide. All their buffer space is allocated
  62. * on open time. The sonicvibes OSS routines we inherited really want
  63. * power of 2 buffers, so we have all those next to each other, then
  64. * 512 byte regions for the recording wavecaches. This ends up
  65. * wasting quite a bit of memory. The only fixes I can see would be
  66. * getting a kernel allocator that could work in zones, or figuring out
  67. * just how to coerce the WP into doing what we want.
  68. *
  69. * The indirection of the various registers means we have to spinlock
  70. * nearly all register accesses. We have the main register indirection
  71. * like the wave cache, maestro registers, etc. Then we have beasts
  72. * like the APU interface that is indirect registers gotten at through
  73. * the main maestro indirection. Ouch. We spinlock around the actual
  74. * ports on a per card basis. This means spinlock activity at each IO
  75. * operation, but the only IO operation clusters are in non critical
  76. * paths and it makes the code far easier to follow. Interrupts are
  77. * blocked while holding the locks because the int handler has to
  78. * get at some of them :(. The mixer interface doesn't, however.
  79. * We also have an OSS state lock that is thrown around in a few
  80. * places.
  81. */
  82. #include <linux/io.h>
  83. #include <linux/delay.h>
  84. #include <linux/interrupt.h>
  85. #include <linux/init.h>
  86. #include <linux/pci.h>
  87. #include <linux/dma-mapping.h>
  88. #include <linux/slab.h>
  89. #include <linux/gameport.h>
  90. #include <linux/module.h>
  91. #include <linux/mutex.h>
  92. #include <linux/input.h>
  93. #include <sound/core.h>
  94. #include <sound/pcm.h>
  95. #include <sound/mpu401.h>
  96. #include <sound/ac97_codec.h>
  97. #include <sound/initval.h>
  98. #ifdef CONFIG_SND_ES1968_RADIO
  99. #include <media/drv-intf/tea575x.h>
  100. #endif
  101. #define CARD_NAME "ESS Maestro1/2"
  102. #define DRIVER_NAME "ES1968"
  103. MODULE_DESCRIPTION("ESS Maestro");
  104. MODULE_LICENSE("GPL");
  105. #if IS_REACHABLE(CONFIG_GAMEPORT)
  106. #define SUPPORT_JOYSTICK 1
  107. #endif
  108. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 1-MAX */
  109. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  110. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  111. static int total_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1024 };
  112. static int pcm_substreams_p[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 4 };
  113. static int pcm_substreams_c[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1 };
  114. static int clock[SNDRV_CARDS];
  115. static int use_pm[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  116. static int enable_mpu[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2};
  117. #ifdef SUPPORT_JOYSTICK
  118. static bool joystick[SNDRV_CARDS];
  119. #endif
  120. static int radio_nr[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  121. module_param_array(index, int, NULL, 0444);
  122. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  123. module_param_array(id, charp, NULL, 0444);
  124. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  125. module_param_array(enable, bool, NULL, 0444);
  126. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  127. module_param_array(total_bufsize, int, NULL, 0444);
  128. MODULE_PARM_DESC(total_bufsize, "Total buffer size in kB.");
  129. module_param_array(pcm_substreams_p, int, NULL, 0444);
  130. MODULE_PARM_DESC(pcm_substreams_p, "PCM Playback substreams for " CARD_NAME " soundcard.");
  131. module_param_array(pcm_substreams_c, int, NULL, 0444);
  132. MODULE_PARM_DESC(pcm_substreams_c, "PCM Capture substreams for " CARD_NAME " soundcard.");
  133. module_param_array(clock, int, NULL, 0444);
  134. MODULE_PARM_DESC(clock, "Clock on " CARD_NAME " soundcard. (0 = auto-detect)");
  135. module_param_array(use_pm, int, NULL, 0444);
  136. MODULE_PARM_DESC(use_pm, "Toggle power-management. (0 = off, 1 = on, 2 = auto)");
  137. module_param_array(enable_mpu, int, NULL, 0444);
  138. MODULE_PARM_DESC(enable_mpu, "Enable MPU401. (0 = off, 1 = on, 2 = auto)");
  139. #ifdef SUPPORT_JOYSTICK
  140. module_param_array(joystick, bool, NULL, 0444);
  141. MODULE_PARM_DESC(joystick, "Enable joystick.");
  142. #endif
  143. module_param_array(radio_nr, int, NULL, 0444);
  144. MODULE_PARM_DESC(radio_nr, "Radio device numbers");
  145. #define NR_APUS 64
  146. #define NR_APU_REGS 16
  147. /* NEC Versas ? */
  148. #define NEC_VERSA_SUBID1 0x80581033
  149. #define NEC_VERSA_SUBID2 0x803c1033
  150. /* Mode Flags */
  151. #define ESS_FMT_STEREO 0x01
  152. #define ESS_FMT_16BIT 0x02
  153. #define DAC_RUNNING 1
  154. #define ADC_RUNNING 2
  155. /* Values for the ESM_LEGACY_AUDIO_CONTROL */
  156. #define ESS_DISABLE_AUDIO 0x8000
  157. #define ESS_ENABLE_SERIAL_IRQ 0x4000
  158. #define IO_ADRESS_ALIAS 0x0020
  159. #define MPU401_IRQ_ENABLE 0x0010
  160. #define MPU401_IO_ENABLE 0x0008
  161. #define GAME_IO_ENABLE 0x0004
  162. #define FM_IO_ENABLE 0x0002
  163. #define SB_IO_ENABLE 0x0001
  164. /* Values for the ESM_CONFIG_A */
  165. #define PIC_SNOOP1 0x4000
  166. #define PIC_SNOOP2 0x2000
  167. #define SAFEGUARD 0x0800
  168. #define DMA_CLEAR 0x0700
  169. #define DMA_DDMA 0x0000
  170. #define DMA_TDMA 0x0100
  171. #define DMA_PCPCI 0x0200
  172. #define POST_WRITE 0x0080
  173. #define PCI_TIMING 0x0040
  174. #define SWAP_LR 0x0020
  175. #define SUBTR_DECODE 0x0002
  176. /* Values for the ESM_CONFIG_B */
  177. #define SPDIF_CONFB 0x0100
  178. #define HWV_CONFB 0x0080
  179. #define DEBOUNCE 0x0040
  180. #define GPIO_CONFB 0x0020
  181. #define CHI_CONFB 0x0010
  182. #define IDMA_CONFB 0x0008 /*undoc */
  183. #define MIDI_FIX 0x0004 /*undoc */
  184. #define IRQ_TO_ISA 0x0001 /*undoc */
  185. /* Values for Ring Bus Control B */
  186. #define RINGB_2CODEC_ID_MASK 0x0003
  187. #define RINGB_DIS_VALIDATION 0x0008
  188. #define RINGB_EN_SPDIF 0x0010
  189. #define RINGB_EN_2CODEC 0x0020
  190. #define RINGB_SING_BIT_DUAL 0x0040
  191. /* ****Port Addresses**** */
  192. /* Write & Read */
  193. #define ESM_INDEX 0x02
  194. #define ESM_DATA 0x00
  195. /* AC97 + RingBus */
  196. #define ESM_AC97_INDEX 0x30
  197. #define ESM_AC97_DATA 0x32
  198. #define ESM_RING_BUS_DEST 0x34
  199. #define ESM_RING_BUS_CONTR_A 0x36
  200. #define ESM_RING_BUS_CONTR_B 0x38
  201. #define ESM_RING_BUS_SDO 0x3A
  202. /* WaveCache*/
  203. #define WC_INDEX 0x10
  204. #define WC_DATA 0x12
  205. #define WC_CONTROL 0x14
  206. /* ASSP*/
  207. #define ASSP_INDEX 0x80
  208. #define ASSP_MEMORY 0x82
  209. #define ASSP_DATA 0x84
  210. #define ASSP_CONTROL_A 0xA2
  211. #define ASSP_CONTROL_B 0xA4
  212. #define ASSP_CONTROL_C 0xA6
  213. #define ASSP_HOSTW_INDEX 0xA8
  214. #define ASSP_HOSTW_DATA 0xAA
  215. #define ASSP_HOSTW_IRQ 0xAC
  216. /* Midi */
  217. #define ESM_MPU401_PORT 0x98
  218. /* Others */
  219. #define ESM_PORT_HOST_IRQ 0x18
  220. #define IDR0_DATA_PORT 0x00
  221. #define IDR1_CRAM_POINTER 0x01
  222. #define IDR2_CRAM_DATA 0x02
  223. #define IDR3_WAVE_DATA 0x03
  224. #define IDR4_WAVE_PTR_LOW 0x04
  225. #define IDR5_WAVE_PTR_HI 0x05
  226. #define IDR6_TIMER_CTRL 0x06
  227. #define IDR7_WAVE_ROMRAM 0x07
  228. #define WRITEABLE_MAP 0xEFFFFF
  229. #define READABLE_MAP 0x64003F
  230. /* PCI Register */
  231. #define ESM_LEGACY_AUDIO_CONTROL 0x40
  232. #define ESM_ACPI_COMMAND 0x54
  233. #define ESM_CONFIG_A 0x50
  234. #define ESM_CONFIG_B 0x52
  235. #define ESM_DDMA 0x60
  236. /* Bob Bits */
  237. #define ESM_BOB_ENABLE 0x0001
  238. #define ESM_BOB_START 0x0001
  239. /* Host IRQ Control Bits */
  240. #define ESM_RESET_MAESTRO 0x8000
  241. #define ESM_RESET_DIRECTSOUND 0x4000
  242. #define ESM_HIRQ_ClkRun 0x0100
  243. #define ESM_HIRQ_HW_VOLUME 0x0040
  244. #define ESM_HIRQ_HARPO 0x0030 /* What's that? */
  245. #define ESM_HIRQ_ASSP 0x0010
  246. #define ESM_HIRQ_DSIE 0x0004
  247. #define ESM_HIRQ_MPU401 0x0002
  248. #define ESM_HIRQ_SB 0x0001
  249. /* Host IRQ Status Bits */
  250. #define ESM_MPU401_IRQ 0x02
  251. #define ESM_SB_IRQ 0x01
  252. #define ESM_SOUND_IRQ 0x04
  253. #define ESM_ASSP_IRQ 0x10
  254. #define ESM_HWVOL_IRQ 0x40
  255. #define ESS_SYSCLK 50000000
  256. #define ESM_BOB_FREQ 200
  257. #define ESM_BOB_FREQ_MAX 800
  258. #define ESM_FREQ_ESM1 (49152000L / 1024L) /* default rate 48000 */
  259. #define ESM_FREQ_ESM2 (50000000L / 1024L)
  260. /* APU Modes: reg 0x00, bit 4-7 */
  261. #define ESM_APU_MODE_SHIFT 4
  262. #define ESM_APU_MODE_MASK (0xf << 4)
  263. #define ESM_APU_OFF 0x00
  264. #define ESM_APU_16BITLINEAR 0x01 /* 16-Bit Linear Sample Player */
  265. #define ESM_APU_16BITSTEREO 0x02 /* 16-Bit Stereo Sample Player */
  266. #define ESM_APU_8BITLINEAR 0x03 /* 8-Bit Linear Sample Player */
  267. #define ESM_APU_8BITSTEREO 0x04 /* 8-Bit Stereo Sample Player */
  268. #define ESM_APU_8BITDIFF 0x05 /* 8-Bit Differential Sample Playrer */
  269. #define ESM_APU_DIGITALDELAY 0x06 /* Digital Delay Line */
  270. #define ESM_APU_DUALTAP 0x07 /* Dual Tap Reader */
  271. #define ESM_APU_CORRELATOR 0x08 /* Correlator */
  272. #define ESM_APU_INPUTMIXER 0x09 /* Input Mixer */
  273. #define ESM_APU_WAVETABLE 0x0A /* Wave Table Mode */
  274. #define ESM_APU_SRCONVERTOR 0x0B /* Sample Rate Convertor */
  275. #define ESM_APU_16BITPINGPONG 0x0C /* 16-Bit Ping-Pong Sample Player */
  276. #define ESM_APU_RESERVED1 0x0D /* Reserved 1 */
  277. #define ESM_APU_RESERVED2 0x0E /* Reserved 2 */
  278. #define ESM_APU_RESERVED3 0x0F /* Reserved 3 */
  279. /* reg 0x00 */
  280. #define ESM_APU_FILTER_Q_SHIFT 0
  281. #define ESM_APU_FILTER_Q_MASK (3 << 0)
  282. /* APU Filtey Q Control */
  283. #define ESM_APU_FILTER_LESSQ 0x00
  284. #define ESM_APU_FILTER_MOREQ 0x03
  285. #define ESM_APU_FILTER_TYPE_SHIFT 2
  286. #define ESM_APU_FILTER_TYPE_MASK (3 << 2)
  287. #define ESM_APU_ENV_TYPE_SHIFT 8
  288. #define ESM_APU_ENV_TYPE_MASK (3 << 8)
  289. #define ESM_APU_ENV_STATE_SHIFT 10
  290. #define ESM_APU_ENV_STATE_MASK (3 << 10)
  291. #define ESM_APU_END_CURVE (1 << 12)
  292. #define ESM_APU_INT_ON_LOOP (1 << 13)
  293. #define ESM_APU_DMA_ENABLE (1 << 14)
  294. /* reg 0x02 */
  295. #define ESM_APU_SUBMIX_GROUP_SHIRT 0
  296. #define ESM_APU_SUBMIX_GROUP_MASK (7 << 0)
  297. #define ESM_APU_SUBMIX_MODE (1 << 3)
  298. #define ESM_APU_6dB (1 << 4)
  299. #define ESM_APU_DUAL_EFFECT (1 << 5)
  300. #define ESM_APU_EFFECT_CHANNELS_SHIFT 6
  301. #define ESM_APU_EFFECT_CHANNELS_MASK (3 << 6)
  302. /* reg 0x03 */
  303. #define ESM_APU_STEP_SIZE_MASK 0x0fff
  304. /* reg 0x04 */
  305. #define ESM_APU_PHASE_SHIFT 0
  306. #define ESM_APU_PHASE_MASK (0xff << 0)
  307. #define ESM_APU_WAVE64K_PAGE_SHIFT 8 /* most 8bit of wave start offset */
  308. #define ESM_APU_WAVE64K_PAGE_MASK (0xff << 8)
  309. /* reg 0x05 - wave start offset */
  310. /* reg 0x06 - wave end offset */
  311. /* reg 0x07 - wave loop length */
  312. /* reg 0x08 */
  313. #define ESM_APU_EFFECT_GAIN_SHIFT 0
  314. #define ESM_APU_EFFECT_GAIN_MASK (0xff << 0)
  315. #define ESM_APU_TREMOLO_DEPTH_SHIFT 8
  316. #define ESM_APU_TREMOLO_DEPTH_MASK (0xf << 8)
  317. #define ESM_APU_TREMOLO_RATE_SHIFT 12
  318. #define ESM_APU_TREMOLO_RATE_MASK (0xf << 12)
  319. /* reg 0x09 */
  320. /* bit 0-7 amplitude dest? */
  321. #define ESM_APU_AMPLITUDE_NOW_SHIFT 8
  322. #define ESM_APU_AMPLITUDE_NOW_MASK (0xff << 8)
  323. /* reg 0x0a */
  324. #define ESM_APU_POLAR_PAN_SHIFT 0
  325. #define ESM_APU_POLAR_PAN_MASK (0x3f << 0)
  326. /* Polar Pan Control */
  327. #define ESM_APU_PAN_CENTER_CIRCLE 0x00
  328. #define ESM_APU_PAN_MIDDLE_RADIUS 0x01
  329. #define ESM_APU_PAN_OUTSIDE_RADIUS 0x02
  330. #define ESM_APU_FILTER_TUNING_SHIFT 8
  331. #define ESM_APU_FILTER_TUNING_MASK (0xff << 8)
  332. /* reg 0x0b */
  333. #define ESM_APU_DATA_SRC_A_SHIFT 0
  334. #define ESM_APU_DATA_SRC_A_MASK (0x7f << 0)
  335. #define ESM_APU_INV_POL_A (1 << 7)
  336. #define ESM_APU_DATA_SRC_B_SHIFT 8
  337. #define ESM_APU_DATA_SRC_B_MASK (0x7f << 8)
  338. #define ESM_APU_INV_POL_B (1 << 15)
  339. #define ESM_APU_VIBRATO_RATE_SHIFT 0
  340. #define ESM_APU_VIBRATO_RATE_MASK (0xf << 0)
  341. #define ESM_APU_VIBRATO_DEPTH_SHIFT 4
  342. #define ESM_APU_VIBRATO_DEPTH_MASK (0xf << 4)
  343. #define ESM_APU_VIBRATO_PHASE_SHIFT 8
  344. #define ESM_APU_VIBRATO_PHASE_MASK (0xff << 8)
  345. /* reg 0x0c */
  346. #define ESM_APU_RADIUS_SELECT (1 << 6)
  347. /* APU Filter Control */
  348. #define ESM_APU_FILTER_2POLE_LOPASS 0x00
  349. #define ESM_APU_FILTER_2POLE_BANDPASS 0x01
  350. #define ESM_APU_FILTER_2POLE_HIPASS 0x02
  351. #define ESM_APU_FILTER_1POLE_LOPASS 0x03
  352. #define ESM_APU_FILTER_1POLE_HIPASS 0x04
  353. #define ESM_APU_FILTER_OFF 0x05
  354. /* APU ATFP Type */
  355. #define ESM_APU_ATFP_AMPLITUDE 0x00
  356. #define ESM_APU_ATFP_TREMELO 0x01
  357. #define ESM_APU_ATFP_FILTER 0x02
  358. #define ESM_APU_ATFP_PAN 0x03
  359. /* APU ATFP Flags */
  360. #define ESM_APU_ATFP_FLG_OFF 0x00
  361. #define ESM_APU_ATFP_FLG_WAIT 0x01
  362. #define ESM_APU_ATFP_FLG_DONE 0x02
  363. #define ESM_APU_ATFP_FLG_INPROCESS 0x03
  364. /* capture mixing buffer size */
  365. #define ESM_MEM_ALIGN 0x1000
  366. #define ESM_MIXBUF_SIZE 0x400
  367. #define ESM_MODE_PLAY 0
  368. #define ESM_MODE_CAPTURE 1
  369. /* APU use in the driver */
  370. enum snd_enum_apu_type {
  371. ESM_APU_PCM_PLAY,
  372. ESM_APU_PCM_CAPTURE,
  373. ESM_APU_PCM_RATECONV,
  374. ESM_APU_FREE
  375. };
  376. /* chip type */
  377. enum {
  378. TYPE_MAESTRO, TYPE_MAESTRO2, TYPE_MAESTRO2E
  379. };
  380. /* DMA Hack! */
  381. struct esm_memory {
  382. struct snd_dma_buffer buf;
  383. int empty; /* status */
  384. struct list_head list;
  385. };
  386. /* Playback Channel */
  387. struct esschan {
  388. int running;
  389. u8 apu[4];
  390. u8 apu_mode[4];
  391. /* playback/capture pcm buffer */
  392. struct esm_memory *memory;
  393. /* capture mixer buffer */
  394. struct esm_memory *mixbuf;
  395. unsigned int hwptr; /* current hw pointer in bytes */
  396. unsigned int count; /* sample counter in bytes */
  397. unsigned int dma_size; /* total buffer size in bytes */
  398. unsigned int frag_size; /* period size in bytes */
  399. unsigned int wav_shift;
  400. u16 base[4]; /* offset for ptr */
  401. /* stereo/16bit flag */
  402. unsigned char fmt;
  403. int mode; /* playback / capture */
  404. int bob_freq; /* required timer frequency */
  405. struct snd_pcm_substream *substream;
  406. /* linked list */
  407. struct list_head list;
  408. #ifdef CONFIG_PM_SLEEP
  409. u16 wc_map[4];
  410. #endif
  411. };
  412. struct es1968 {
  413. /* Module Config */
  414. int total_bufsize; /* in bytes */
  415. int playback_streams, capture_streams;
  416. unsigned int clock; /* clock */
  417. /* for clock measurement */
  418. unsigned int in_measurement: 1;
  419. unsigned int measure_apu;
  420. unsigned int measure_lastpos;
  421. unsigned int measure_count;
  422. /* buffer */
  423. struct snd_dma_buffer dma;
  424. /* Resources... */
  425. int irq;
  426. unsigned long io_port;
  427. int type;
  428. struct pci_dev *pci;
  429. struct snd_card *card;
  430. struct snd_pcm *pcm;
  431. int do_pm; /* power-management enabled */
  432. /* DMA memory block */
  433. struct list_head buf_list;
  434. /* ALSA Stuff */
  435. struct snd_ac97 *ac97;
  436. struct snd_rawmidi *rmidi;
  437. spinlock_t reg_lock;
  438. unsigned int in_suspend;
  439. /* Maestro Stuff */
  440. u16 maestro_map[32];
  441. int bobclient; /* active timer instancs */
  442. int bob_freq; /* timer frequency */
  443. struct mutex memory_mutex; /* memory lock */
  444. /* APU states */
  445. unsigned char apu[NR_APUS];
  446. /* active substreams */
  447. struct list_head substream_list;
  448. spinlock_t substream_lock;
  449. #ifdef CONFIG_PM_SLEEP
  450. u16 apu_map[NR_APUS][NR_APU_REGS];
  451. #endif
  452. #ifdef SUPPORT_JOYSTICK
  453. struct gameport *gameport;
  454. #endif
  455. #ifdef CONFIG_SND_ES1968_INPUT
  456. struct input_dev *input_dev;
  457. char phys[64]; /* physical device path */
  458. #else
  459. struct snd_kcontrol *master_switch; /* for h/w volume control */
  460. struct snd_kcontrol *master_volume;
  461. #endif
  462. struct work_struct hwvol_work;
  463. #ifdef CONFIG_SND_ES1968_RADIO
  464. struct v4l2_device v4l2_dev;
  465. struct snd_tea575x tea;
  466. unsigned int tea575x_tuner;
  467. #endif
  468. };
  469. static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id);
  470. static const struct pci_device_id snd_es1968_ids[] = {
  471. /* Maestro 1 */
  472. { 0x1285, 0x0100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO },
  473. /* Maestro 2 */
  474. { 0x125d, 0x1968, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2 },
  475. /* Maestro 2E */
  476. { 0x125d, 0x1978, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_MULTIMEDIA_AUDIO << 8, 0xffff00, TYPE_MAESTRO2E },
  477. { 0, }
  478. };
  479. MODULE_DEVICE_TABLE(pci, snd_es1968_ids);
  480. /* *********************
  481. * Low Level Funcs! *
  482. *********************/
  483. /* no spinlock */
  484. static void __maestro_write(struct es1968 *chip, u16 reg, u16 data)
  485. {
  486. outw(reg, chip->io_port + ESM_INDEX);
  487. outw(data, chip->io_port + ESM_DATA);
  488. chip->maestro_map[reg] = data;
  489. }
  490. static inline void maestro_write(struct es1968 *chip, u16 reg, u16 data)
  491. {
  492. unsigned long flags;
  493. spin_lock_irqsave(&chip->reg_lock, flags);
  494. __maestro_write(chip, reg, data);
  495. spin_unlock_irqrestore(&chip->reg_lock, flags);
  496. }
  497. /* no spinlock */
  498. static u16 __maestro_read(struct es1968 *chip, u16 reg)
  499. {
  500. if (READABLE_MAP & (1 << reg)) {
  501. outw(reg, chip->io_port + ESM_INDEX);
  502. chip->maestro_map[reg] = inw(chip->io_port + ESM_DATA);
  503. }
  504. return chip->maestro_map[reg];
  505. }
  506. static inline u16 maestro_read(struct es1968 *chip, u16 reg)
  507. {
  508. unsigned long flags;
  509. u16 result;
  510. spin_lock_irqsave(&chip->reg_lock, flags);
  511. result = __maestro_read(chip, reg);
  512. spin_unlock_irqrestore(&chip->reg_lock, flags);
  513. return result;
  514. }
  515. /* Wait for the codec bus to be free */
  516. static int snd_es1968_ac97_wait(struct es1968 *chip)
  517. {
  518. int timeout = 100000;
  519. while (timeout-- > 0) {
  520. if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
  521. return 0;
  522. cond_resched();
  523. }
  524. dev_dbg(chip->card->dev, "ac97 timeout\n");
  525. return 1; /* timeout */
  526. }
  527. static int snd_es1968_ac97_wait_poll(struct es1968 *chip)
  528. {
  529. int timeout = 100000;
  530. while (timeout-- > 0) {
  531. if (!(inb(chip->io_port + ESM_AC97_INDEX) & 1))
  532. return 0;
  533. }
  534. dev_dbg(chip->card->dev, "ac97 timeout\n");
  535. return 1; /* timeout */
  536. }
  537. static void snd_es1968_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)
  538. {
  539. struct es1968 *chip = ac97->private_data;
  540. snd_es1968_ac97_wait(chip);
  541. /* Write the bus */
  542. outw(val, chip->io_port + ESM_AC97_DATA);
  543. /*msleep(1);*/
  544. outb(reg, chip->io_port + ESM_AC97_INDEX);
  545. /*msleep(1);*/
  546. }
  547. static unsigned short snd_es1968_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
  548. {
  549. u16 data = 0;
  550. struct es1968 *chip = ac97->private_data;
  551. snd_es1968_ac97_wait(chip);
  552. outb(reg | 0x80, chip->io_port + ESM_AC97_INDEX);
  553. /*msleep(1);*/
  554. if (!snd_es1968_ac97_wait_poll(chip)) {
  555. data = inw(chip->io_port + ESM_AC97_DATA);
  556. /*msleep(1);*/
  557. }
  558. return data;
  559. }
  560. /* no spinlock */
  561. static void apu_index_set(struct es1968 *chip, u16 index)
  562. {
  563. int i;
  564. __maestro_write(chip, IDR1_CRAM_POINTER, index);
  565. for (i = 0; i < 1000; i++)
  566. if (__maestro_read(chip, IDR1_CRAM_POINTER) == index)
  567. return;
  568. dev_dbg(chip->card->dev, "APU register select failed. (Timeout)\n");
  569. }
  570. /* no spinlock */
  571. static void apu_data_set(struct es1968 *chip, u16 data)
  572. {
  573. int i;
  574. for (i = 0; i < 1000; i++) {
  575. if (__maestro_read(chip, IDR0_DATA_PORT) == data)
  576. return;
  577. __maestro_write(chip, IDR0_DATA_PORT, data);
  578. }
  579. dev_dbg(chip->card->dev, "APU register set probably failed (Timeout)!\n");
  580. }
  581. /* no spinlock */
  582. static void __apu_set_register(struct es1968 *chip, u16 channel, u8 reg, u16 data)
  583. {
  584. if (snd_BUG_ON(channel >= NR_APUS))
  585. return;
  586. #ifdef CONFIG_PM_SLEEP
  587. chip->apu_map[channel][reg] = data;
  588. #endif
  589. reg |= (channel << 4);
  590. apu_index_set(chip, reg);
  591. apu_data_set(chip, data);
  592. }
  593. static void apu_set_register(struct es1968 *chip, u16 channel, u8 reg, u16 data)
  594. {
  595. unsigned long flags;
  596. spin_lock_irqsave(&chip->reg_lock, flags);
  597. __apu_set_register(chip, channel, reg, data);
  598. spin_unlock_irqrestore(&chip->reg_lock, flags);
  599. }
  600. static u16 __apu_get_register(struct es1968 *chip, u16 channel, u8 reg)
  601. {
  602. if (snd_BUG_ON(channel >= NR_APUS))
  603. return 0;
  604. reg |= (channel << 4);
  605. apu_index_set(chip, reg);
  606. return __maestro_read(chip, IDR0_DATA_PORT);
  607. }
  608. static u16 apu_get_register(struct es1968 *chip, u16 channel, u8 reg)
  609. {
  610. unsigned long flags;
  611. u16 v;
  612. spin_lock_irqsave(&chip->reg_lock, flags);
  613. v = __apu_get_register(chip, channel, reg);
  614. spin_unlock_irqrestore(&chip->reg_lock, flags);
  615. return v;
  616. }
  617. #if 0 /* ASSP is not supported */
  618. static void assp_set_register(struct es1968 *chip, u32 reg, u32 value)
  619. {
  620. unsigned long flags;
  621. spin_lock_irqsave(&chip->reg_lock, flags);
  622. outl(reg, chip->io_port + ASSP_INDEX);
  623. outl(value, chip->io_port + ASSP_DATA);
  624. spin_unlock_irqrestore(&chip->reg_lock, flags);
  625. }
  626. static u32 assp_get_register(struct es1968 *chip, u32 reg)
  627. {
  628. unsigned long flags;
  629. u32 value;
  630. spin_lock_irqsave(&chip->reg_lock, flags);
  631. outl(reg, chip->io_port + ASSP_INDEX);
  632. value = inl(chip->io_port + ASSP_DATA);
  633. spin_unlock_irqrestore(&chip->reg_lock, flags);
  634. return value;
  635. }
  636. #endif
  637. static void wave_set_register(struct es1968 *chip, u16 reg, u16 value)
  638. {
  639. unsigned long flags;
  640. spin_lock_irqsave(&chip->reg_lock, flags);
  641. outw(reg, chip->io_port + WC_INDEX);
  642. outw(value, chip->io_port + WC_DATA);
  643. spin_unlock_irqrestore(&chip->reg_lock, flags);
  644. }
  645. static u16 wave_get_register(struct es1968 *chip, u16 reg)
  646. {
  647. unsigned long flags;
  648. u16 value;
  649. spin_lock_irqsave(&chip->reg_lock, flags);
  650. outw(reg, chip->io_port + WC_INDEX);
  651. value = inw(chip->io_port + WC_DATA);
  652. spin_unlock_irqrestore(&chip->reg_lock, flags);
  653. return value;
  654. }
  655. /* *******************
  656. * Bob the Timer! *
  657. *******************/
  658. static void snd_es1968_bob_stop(struct es1968 *chip)
  659. {
  660. u16 reg;
  661. reg = __maestro_read(chip, 0x11);
  662. reg &= ~ESM_BOB_ENABLE;
  663. __maestro_write(chip, 0x11, reg);
  664. reg = __maestro_read(chip, 0x17);
  665. reg &= ~ESM_BOB_START;
  666. __maestro_write(chip, 0x17, reg);
  667. }
  668. static void snd_es1968_bob_start(struct es1968 *chip)
  669. {
  670. int prescale;
  671. int divide;
  672. /* compute ideal interrupt frequency for buffer size & play rate */
  673. /* first, find best prescaler value to match freq */
  674. for (prescale = 5; prescale < 12; prescale++)
  675. if (chip->bob_freq > (ESS_SYSCLK >> (prescale + 9)))
  676. break;
  677. /* next, back off prescaler whilst getting divider into optimum range */
  678. divide = 1;
  679. while ((prescale > 5) && (divide < 32)) {
  680. prescale--;
  681. divide <<= 1;
  682. }
  683. divide >>= 1;
  684. /* now fine-tune the divider for best match */
  685. for (; divide < 31; divide++)
  686. if (chip->bob_freq >
  687. ((ESS_SYSCLK >> (prescale + 9)) / (divide + 1))) break;
  688. /* divide = 0 is illegal, but don't let prescale = 4! */
  689. if (divide == 0) {
  690. divide++;
  691. if (prescale > 5)
  692. prescale--;
  693. } else if (divide > 1)
  694. divide--;
  695. __maestro_write(chip, 6, 0x9000 | (prescale << 5) | divide); /* set reg */
  696. /* Now set IDR 11/17 */
  697. __maestro_write(chip, 0x11, __maestro_read(chip, 0x11) | 1);
  698. __maestro_write(chip, 0x17, __maestro_read(chip, 0x17) | 1);
  699. }
  700. /* call with substream spinlock */
  701. static void snd_es1968_bob_inc(struct es1968 *chip, int freq)
  702. {
  703. chip->bobclient++;
  704. if (chip->bobclient == 1) {
  705. chip->bob_freq = freq;
  706. snd_es1968_bob_start(chip);
  707. } else if (chip->bob_freq < freq) {
  708. snd_es1968_bob_stop(chip);
  709. chip->bob_freq = freq;
  710. snd_es1968_bob_start(chip);
  711. }
  712. }
  713. /* call with substream spinlock */
  714. static void snd_es1968_bob_dec(struct es1968 *chip)
  715. {
  716. chip->bobclient--;
  717. if (chip->bobclient <= 0)
  718. snd_es1968_bob_stop(chip);
  719. else if (chip->bob_freq > ESM_BOB_FREQ) {
  720. /* check reduction of timer frequency */
  721. int max_freq = ESM_BOB_FREQ;
  722. struct esschan *es;
  723. list_for_each_entry(es, &chip->substream_list, list) {
  724. if (max_freq < es->bob_freq)
  725. max_freq = es->bob_freq;
  726. }
  727. if (max_freq != chip->bob_freq) {
  728. snd_es1968_bob_stop(chip);
  729. chip->bob_freq = max_freq;
  730. snd_es1968_bob_start(chip);
  731. }
  732. }
  733. }
  734. static int
  735. snd_es1968_calc_bob_rate(struct es1968 *chip, struct esschan *es,
  736. struct snd_pcm_runtime *runtime)
  737. {
  738. /* we acquire 4 interrupts per period for precise control.. */
  739. int freq = runtime->rate * 4;
  740. if (es->fmt & ESS_FMT_STEREO)
  741. freq <<= 1;
  742. if (es->fmt & ESS_FMT_16BIT)
  743. freq <<= 1;
  744. freq /= es->frag_size;
  745. if (freq < ESM_BOB_FREQ)
  746. freq = ESM_BOB_FREQ;
  747. else if (freq > ESM_BOB_FREQ_MAX)
  748. freq = ESM_BOB_FREQ_MAX;
  749. return freq;
  750. }
  751. /*************
  752. * PCM Part *
  753. *************/
  754. static u32 snd_es1968_compute_rate(struct es1968 *chip, u32 freq)
  755. {
  756. u32 rate = (freq << 16) / chip->clock;
  757. #if 0 /* XXX: do we need this? */
  758. if (rate > 0x10000)
  759. rate = 0x10000;
  760. #endif
  761. return rate;
  762. }
  763. /* get current pointer */
  764. static inline unsigned int
  765. snd_es1968_get_dma_ptr(struct es1968 *chip, struct esschan *es)
  766. {
  767. unsigned int offset;
  768. offset = apu_get_register(chip, es->apu[0], 5);
  769. offset -= es->base[0];
  770. return (offset & 0xFFFE); /* hardware is in words */
  771. }
  772. static void snd_es1968_apu_set_freq(struct es1968 *chip, int apu, int freq)
  773. {
  774. apu_set_register(chip, apu, 2,
  775. (apu_get_register(chip, apu, 2) & 0x00FF) |
  776. ((freq & 0xff) << 8) | 0x10);
  777. apu_set_register(chip, apu, 3, freq >> 8);
  778. }
  779. /* spin lock held */
  780. static inline void snd_es1968_trigger_apu(struct es1968 *esm, int apu, int mode)
  781. {
  782. /* set the APU mode */
  783. __apu_set_register(esm, apu, 0,
  784. (__apu_get_register(esm, apu, 0) & 0xff0f) |
  785. (mode << 4));
  786. }
  787. static void snd_es1968_pcm_start(struct es1968 *chip, struct esschan *es)
  788. {
  789. spin_lock(&chip->reg_lock);
  790. __apu_set_register(chip, es->apu[0], 5, es->base[0]);
  791. snd_es1968_trigger_apu(chip, es->apu[0], es->apu_mode[0]);
  792. if (es->mode == ESM_MODE_CAPTURE) {
  793. __apu_set_register(chip, es->apu[2], 5, es->base[2]);
  794. snd_es1968_trigger_apu(chip, es->apu[2], es->apu_mode[2]);
  795. }
  796. if (es->fmt & ESS_FMT_STEREO) {
  797. __apu_set_register(chip, es->apu[1], 5, es->base[1]);
  798. snd_es1968_trigger_apu(chip, es->apu[1], es->apu_mode[1]);
  799. if (es->mode == ESM_MODE_CAPTURE) {
  800. __apu_set_register(chip, es->apu[3], 5, es->base[3]);
  801. snd_es1968_trigger_apu(chip, es->apu[3], es->apu_mode[3]);
  802. }
  803. }
  804. spin_unlock(&chip->reg_lock);
  805. }
  806. static void snd_es1968_pcm_stop(struct es1968 *chip, struct esschan *es)
  807. {
  808. spin_lock(&chip->reg_lock);
  809. snd_es1968_trigger_apu(chip, es->apu[0], 0);
  810. snd_es1968_trigger_apu(chip, es->apu[1], 0);
  811. if (es->mode == ESM_MODE_CAPTURE) {
  812. snd_es1968_trigger_apu(chip, es->apu[2], 0);
  813. snd_es1968_trigger_apu(chip, es->apu[3], 0);
  814. }
  815. spin_unlock(&chip->reg_lock);
  816. }
  817. /* set the wavecache control reg */
  818. static void snd_es1968_program_wavecache(struct es1968 *chip, struct esschan *es,
  819. int channel, u32 addr, int capture)
  820. {
  821. u32 tmpval = (addr - 0x10) & 0xFFF8;
  822. if (! capture) {
  823. if (!(es->fmt & ESS_FMT_16BIT))
  824. tmpval |= 4; /* 8bit */
  825. if (es->fmt & ESS_FMT_STEREO)
  826. tmpval |= 2; /* stereo */
  827. }
  828. /* set the wavecache control reg */
  829. wave_set_register(chip, es->apu[channel] << 3, tmpval);
  830. #ifdef CONFIG_PM_SLEEP
  831. es->wc_map[channel] = tmpval;
  832. #endif
  833. }
  834. static void snd_es1968_playback_setup(struct es1968 *chip, struct esschan *es,
  835. struct snd_pcm_runtime *runtime)
  836. {
  837. u32 pa;
  838. int high_apu = 0;
  839. int channel, apu;
  840. int i, size;
  841. unsigned long flags;
  842. u32 freq;
  843. size = es->dma_size >> es->wav_shift;
  844. if (es->fmt & ESS_FMT_STEREO)
  845. high_apu++;
  846. for (channel = 0; channel <= high_apu; channel++) {
  847. apu = es->apu[channel];
  848. snd_es1968_program_wavecache(chip, es, channel, es->memory->buf.addr, 0);
  849. /* Offset to PCMBAR */
  850. pa = es->memory->buf.addr;
  851. pa -= chip->dma.addr;
  852. pa >>= 1; /* words */
  853. pa |= 0x00400000; /* System RAM (Bit 22) */
  854. if (es->fmt & ESS_FMT_STEREO) {
  855. /* Enable stereo */
  856. if (channel)
  857. pa |= 0x00800000; /* (Bit 23) */
  858. if (es->fmt & ESS_FMT_16BIT)
  859. pa >>= 1;
  860. }
  861. /* base offset of dma calcs when reading the pointer
  862. on this left one */
  863. es->base[channel] = pa & 0xFFFF;
  864. for (i = 0; i < 16; i++)
  865. apu_set_register(chip, apu, i, 0x0000);
  866. /* Load the buffer into the wave engine */
  867. apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
  868. apu_set_register(chip, apu, 5, pa & 0xFFFF);
  869. apu_set_register(chip, apu, 6, (pa + size) & 0xFFFF);
  870. /* setting loop == sample len */
  871. apu_set_register(chip, apu, 7, size);
  872. /* clear effects/env.. */
  873. apu_set_register(chip, apu, 8, 0x0000);
  874. /* set amp now to 0xd0 (?), low byte is 'amplitude dest'? */
  875. apu_set_register(chip, apu, 9, 0xD000);
  876. /* clear routing stuff */
  877. apu_set_register(chip, apu, 11, 0x0000);
  878. /* dma on, no envelopes, filter to all 1s) */
  879. apu_set_register(chip, apu, 0, 0x400F);
  880. if (es->fmt & ESS_FMT_16BIT)
  881. es->apu_mode[channel] = ESM_APU_16BITLINEAR;
  882. else
  883. es->apu_mode[channel] = ESM_APU_8BITLINEAR;
  884. if (es->fmt & ESS_FMT_STEREO) {
  885. /* set panning: left or right */
  886. /* Check: different panning. On my Canyon 3D Chipset the
  887. Channels are swapped. I don't know, about the output
  888. to the SPDif Link. Perhaps you have to change this
  889. and not the APU Regs 4-5. */
  890. apu_set_register(chip, apu, 10,
  891. 0x8F00 | (channel ? 0 : 0x10));
  892. es->apu_mode[channel] += 1; /* stereo */
  893. } else
  894. apu_set_register(chip, apu, 10, 0x8F08);
  895. }
  896. spin_lock_irqsave(&chip->reg_lock, flags);
  897. /* clear WP interrupts */
  898. outw(1, chip->io_port + 0x04);
  899. /* enable WP ints */
  900. outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
  901. spin_unlock_irqrestore(&chip->reg_lock, flags);
  902. freq = runtime->rate;
  903. /* set frequency */
  904. if (freq > 48000)
  905. freq = 48000;
  906. if (freq < 4000)
  907. freq = 4000;
  908. /* hmmm.. */
  909. if (!(es->fmt & ESS_FMT_16BIT) && !(es->fmt & ESS_FMT_STEREO))
  910. freq >>= 1;
  911. freq = snd_es1968_compute_rate(chip, freq);
  912. /* Load the frequency, turn on 6dB */
  913. snd_es1968_apu_set_freq(chip, es->apu[0], freq);
  914. snd_es1968_apu_set_freq(chip, es->apu[1], freq);
  915. }
  916. static void init_capture_apu(struct es1968 *chip, struct esschan *es, int channel,
  917. unsigned int pa, unsigned int bsize,
  918. int mode, int route)
  919. {
  920. int i, apu = es->apu[channel];
  921. es->apu_mode[channel] = mode;
  922. /* set the wavecache control reg */
  923. snd_es1968_program_wavecache(chip, es, channel, pa, 1);
  924. /* Offset to PCMBAR */
  925. pa -= chip->dma.addr;
  926. pa >>= 1; /* words */
  927. /* base offset of dma calcs when reading the pointer
  928. on this left one */
  929. es->base[channel] = pa & 0xFFFF;
  930. pa |= 0x00400000; /* bit 22 -> System RAM */
  931. /* Begin loading the APU */
  932. for (i = 0; i < 16; i++)
  933. apu_set_register(chip, apu, i, 0x0000);
  934. /* need to enable subgroups.. and we should probably
  935. have different groups for different /dev/dsps.. */
  936. apu_set_register(chip, apu, 2, 0x8);
  937. /* Load the buffer into the wave engine */
  938. apu_set_register(chip, apu, 4, ((pa >> 16) & 0xFF) << 8);
  939. apu_set_register(chip, apu, 5, pa & 0xFFFF);
  940. apu_set_register(chip, apu, 6, (pa + bsize) & 0xFFFF);
  941. apu_set_register(chip, apu, 7, bsize);
  942. /* clear effects/env.. */
  943. apu_set_register(chip, apu, 8, 0x00F0);
  944. /* amplitude now? sure. why not. */
  945. apu_set_register(chip, apu, 9, 0x0000);
  946. /* set filter tune, radius, polar pan */
  947. apu_set_register(chip, apu, 10, 0x8F08);
  948. /* route input */
  949. apu_set_register(chip, apu, 11, route);
  950. /* dma on, no envelopes, filter to all 1s) */
  951. apu_set_register(chip, apu, 0, 0x400F);
  952. }
  953. static void snd_es1968_capture_setup(struct es1968 *chip, struct esschan *es,
  954. struct snd_pcm_runtime *runtime)
  955. {
  956. int size;
  957. u32 freq;
  958. unsigned long flags;
  959. size = es->dma_size >> es->wav_shift;
  960. /* APU assignments:
  961. 0 = mono/left SRC
  962. 1 = right SRC
  963. 2 = mono/left Input Mixer
  964. 3 = right Input Mixer
  965. */
  966. /* data seems to flow from the codec, through an apu into
  967. the 'mixbuf' bit of page, then through the SRC apu
  968. and out to the real 'buffer'. ok. sure. */
  969. /* input mixer (left/mono) */
  970. /* parallel in crap, see maestro reg 0xC [8-11] */
  971. init_capture_apu(chip, es, 2,
  972. es->mixbuf->buf.addr, ESM_MIXBUF_SIZE/4, /* in words */
  973. ESM_APU_INPUTMIXER, 0x14);
  974. /* SRC (left/mono); get input from inputing apu */
  975. init_capture_apu(chip, es, 0, es->memory->buf.addr, size,
  976. ESM_APU_SRCONVERTOR, es->apu[2]);
  977. if (es->fmt & ESS_FMT_STEREO) {
  978. /* input mixer (right) */
  979. init_capture_apu(chip, es, 3,
  980. es->mixbuf->buf.addr + ESM_MIXBUF_SIZE/2,
  981. ESM_MIXBUF_SIZE/4, /* in words */
  982. ESM_APU_INPUTMIXER, 0x15);
  983. /* SRC (right) */
  984. init_capture_apu(chip, es, 1,
  985. es->memory->buf.addr + size*2, size,
  986. ESM_APU_SRCONVERTOR, es->apu[3]);
  987. }
  988. freq = runtime->rate;
  989. /* Sample Rate conversion APUs don't like 0x10000 for their rate */
  990. if (freq > 47999)
  991. freq = 47999;
  992. if (freq < 4000)
  993. freq = 4000;
  994. freq = snd_es1968_compute_rate(chip, freq);
  995. /* Load the frequency, turn on 6dB */
  996. snd_es1968_apu_set_freq(chip, es->apu[0], freq);
  997. snd_es1968_apu_set_freq(chip, es->apu[1], freq);
  998. /* fix mixer rate at 48khz. and its _must_ be 0x10000. */
  999. freq = 0x10000;
  1000. snd_es1968_apu_set_freq(chip, es->apu[2], freq);
  1001. snd_es1968_apu_set_freq(chip, es->apu[3], freq);
  1002. spin_lock_irqsave(&chip->reg_lock, flags);
  1003. /* clear WP interrupts */
  1004. outw(1, chip->io_port + 0x04);
  1005. /* enable WP ints */
  1006. outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ);
  1007. spin_unlock_irqrestore(&chip->reg_lock, flags);
  1008. }
  1009. /*******************
  1010. * ALSA Interface *
  1011. *******************/
  1012. static int snd_es1968_pcm_prepare(struct snd_pcm_substream *substream)
  1013. {
  1014. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1015. struct snd_pcm_runtime *runtime = substream->runtime;
  1016. struct esschan *es = runtime->private_data;
  1017. es->dma_size = snd_pcm_lib_buffer_bytes(substream);
  1018. es->frag_size = snd_pcm_lib_period_bytes(substream);
  1019. es->wav_shift = 1; /* maestro handles always 16bit */
  1020. es->fmt = 0;
  1021. if (snd_pcm_format_width(runtime->format) == 16)
  1022. es->fmt |= ESS_FMT_16BIT;
  1023. if (runtime->channels > 1) {
  1024. es->fmt |= ESS_FMT_STEREO;
  1025. if (es->fmt & ESS_FMT_16BIT) /* 8bit is already word shifted */
  1026. es->wav_shift++;
  1027. }
  1028. es->bob_freq = snd_es1968_calc_bob_rate(chip, es, runtime);
  1029. switch (es->mode) {
  1030. case ESM_MODE_PLAY:
  1031. snd_es1968_playback_setup(chip, es, runtime);
  1032. break;
  1033. case ESM_MODE_CAPTURE:
  1034. snd_es1968_capture_setup(chip, es, runtime);
  1035. break;
  1036. }
  1037. return 0;
  1038. }
  1039. static int snd_es1968_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  1040. {
  1041. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1042. struct esschan *es = substream->runtime->private_data;
  1043. spin_lock(&chip->substream_lock);
  1044. switch (cmd) {
  1045. case SNDRV_PCM_TRIGGER_START:
  1046. case SNDRV_PCM_TRIGGER_RESUME:
  1047. if (es->running)
  1048. break;
  1049. snd_es1968_bob_inc(chip, es->bob_freq);
  1050. es->count = 0;
  1051. es->hwptr = 0;
  1052. snd_es1968_pcm_start(chip, es);
  1053. es->running = 1;
  1054. break;
  1055. case SNDRV_PCM_TRIGGER_STOP:
  1056. case SNDRV_PCM_TRIGGER_SUSPEND:
  1057. if (! es->running)
  1058. break;
  1059. snd_es1968_pcm_stop(chip, es);
  1060. es->running = 0;
  1061. snd_es1968_bob_dec(chip);
  1062. break;
  1063. }
  1064. spin_unlock(&chip->substream_lock);
  1065. return 0;
  1066. }
  1067. static snd_pcm_uframes_t snd_es1968_pcm_pointer(struct snd_pcm_substream *substream)
  1068. {
  1069. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1070. struct esschan *es = substream->runtime->private_data;
  1071. unsigned int ptr;
  1072. ptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
  1073. return bytes_to_frames(substream->runtime, ptr % es->dma_size);
  1074. }
  1075. static const struct snd_pcm_hardware snd_es1968_playback = {
  1076. .info = (SNDRV_PCM_INFO_MMAP |
  1077. SNDRV_PCM_INFO_MMAP_VALID |
  1078. SNDRV_PCM_INFO_INTERLEAVED |
  1079. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1080. /*SNDRV_PCM_INFO_PAUSE |*/
  1081. SNDRV_PCM_INFO_RESUME),
  1082. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1083. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1084. .rate_min = 4000,
  1085. .rate_max = 48000,
  1086. .channels_min = 1,
  1087. .channels_max = 2,
  1088. .buffer_bytes_max = 65536,
  1089. .period_bytes_min = 256,
  1090. .period_bytes_max = 65536,
  1091. .periods_min = 1,
  1092. .periods_max = 1024,
  1093. .fifo_size = 0,
  1094. };
  1095. static const struct snd_pcm_hardware snd_es1968_capture = {
  1096. .info = (SNDRV_PCM_INFO_NONINTERLEAVED |
  1097. SNDRV_PCM_INFO_MMAP |
  1098. SNDRV_PCM_INFO_MMAP_VALID |
  1099. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1100. /*SNDRV_PCM_INFO_PAUSE |*/
  1101. SNDRV_PCM_INFO_RESUME),
  1102. .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE,
  1103. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1104. .rate_min = 4000,
  1105. .rate_max = 48000,
  1106. .channels_min = 1,
  1107. .channels_max = 2,
  1108. .buffer_bytes_max = 65536,
  1109. .period_bytes_min = 256,
  1110. .period_bytes_max = 65536,
  1111. .periods_min = 1,
  1112. .periods_max = 1024,
  1113. .fifo_size = 0,
  1114. };
  1115. /* *************************
  1116. * DMA memory management *
  1117. *************************/
  1118. /* Because the Maestro can only take addresses relative to the PCM base address
  1119. register :( */
  1120. static int calc_available_memory_size(struct es1968 *chip)
  1121. {
  1122. int max_size = 0;
  1123. struct esm_memory *buf;
  1124. mutex_lock(&chip->memory_mutex);
  1125. list_for_each_entry(buf, &chip->buf_list, list) {
  1126. if (buf->empty && buf->buf.bytes > max_size)
  1127. max_size = buf->buf.bytes;
  1128. }
  1129. mutex_unlock(&chip->memory_mutex);
  1130. if (max_size >= 128*1024)
  1131. max_size = 127*1024;
  1132. return max_size;
  1133. }
  1134. /* allocate a new memory chunk with the specified size */
  1135. static struct esm_memory *snd_es1968_new_memory(struct es1968 *chip, int size)
  1136. {
  1137. struct esm_memory *buf;
  1138. size = ALIGN(size, ESM_MEM_ALIGN);
  1139. mutex_lock(&chip->memory_mutex);
  1140. list_for_each_entry(buf, &chip->buf_list, list) {
  1141. if (buf->empty && buf->buf.bytes >= size)
  1142. goto __found;
  1143. }
  1144. mutex_unlock(&chip->memory_mutex);
  1145. return NULL;
  1146. __found:
  1147. if (buf->buf.bytes > size) {
  1148. struct esm_memory *chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
  1149. if (chunk == NULL) {
  1150. mutex_unlock(&chip->memory_mutex);
  1151. return NULL;
  1152. }
  1153. chunk->buf = buf->buf;
  1154. chunk->buf.bytes -= size;
  1155. chunk->buf.area += size;
  1156. chunk->buf.addr += size;
  1157. chunk->empty = 1;
  1158. buf->buf.bytes = size;
  1159. list_add(&chunk->list, &buf->list);
  1160. }
  1161. buf->empty = 0;
  1162. mutex_unlock(&chip->memory_mutex);
  1163. return buf;
  1164. }
  1165. /* free a memory chunk */
  1166. static void snd_es1968_free_memory(struct es1968 *chip, struct esm_memory *buf)
  1167. {
  1168. struct esm_memory *chunk;
  1169. mutex_lock(&chip->memory_mutex);
  1170. buf->empty = 1;
  1171. if (buf->list.prev != &chip->buf_list) {
  1172. chunk = list_entry(buf->list.prev, struct esm_memory, list);
  1173. if (chunk->empty) {
  1174. chunk->buf.bytes += buf->buf.bytes;
  1175. list_del(&buf->list);
  1176. kfree(buf);
  1177. buf = chunk;
  1178. }
  1179. }
  1180. if (buf->list.next != &chip->buf_list) {
  1181. chunk = list_entry(buf->list.next, struct esm_memory, list);
  1182. if (chunk->empty) {
  1183. buf->buf.bytes += chunk->buf.bytes;
  1184. list_del(&chunk->list);
  1185. kfree(chunk);
  1186. }
  1187. }
  1188. mutex_unlock(&chip->memory_mutex);
  1189. }
  1190. static void snd_es1968_free_dmabuf(struct es1968 *chip)
  1191. {
  1192. struct list_head *p;
  1193. if (! chip->dma.area)
  1194. return;
  1195. snd_dma_free_pages(&chip->dma);
  1196. while ((p = chip->buf_list.next) != &chip->buf_list) {
  1197. struct esm_memory *chunk = list_entry(p, struct esm_memory, list);
  1198. list_del(p);
  1199. kfree(chunk);
  1200. }
  1201. }
  1202. static int
  1203. snd_es1968_init_dmabuf(struct es1968 *chip)
  1204. {
  1205. int err;
  1206. struct esm_memory *chunk;
  1207. err = snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV,
  1208. &chip->pci->dev,
  1209. chip->total_bufsize, &chip->dma);
  1210. if (err < 0 || ! chip->dma.area) {
  1211. dev_err(chip->card->dev,
  1212. "can't allocate dma pages for size %d\n",
  1213. chip->total_bufsize);
  1214. return -ENOMEM;
  1215. }
  1216. if ((chip->dma.addr + chip->dma.bytes - 1) & ~((1 << 28) - 1)) {
  1217. snd_dma_free_pages(&chip->dma);
  1218. dev_err(chip->card->dev, "DMA buffer beyond 256MB.\n");
  1219. return -ENOMEM;
  1220. }
  1221. INIT_LIST_HEAD(&chip->buf_list);
  1222. /* allocate an empty chunk */
  1223. chunk = kmalloc(sizeof(*chunk), GFP_KERNEL);
  1224. if (chunk == NULL) {
  1225. snd_es1968_free_dmabuf(chip);
  1226. return -ENOMEM;
  1227. }
  1228. memset(chip->dma.area, 0, ESM_MEM_ALIGN);
  1229. chunk->buf = chip->dma;
  1230. chunk->buf.area += ESM_MEM_ALIGN;
  1231. chunk->buf.addr += ESM_MEM_ALIGN;
  1232. chunk->buf.bytes -= ESM_MEM_ALIGN;
  1233. chunk->empty = 1;
  1234. list_add(&chunk->list, &chip->buf_list);
  1235. return 0;
  1236. }
  1237. /* setup the dma_areas */
  1238. /* buffer is extracted from the pre-allocated memory chunk */
  1239. static int snd_es1968_hw_params(struct snd_pcm_substream *substream,
  1240. struct snd_pcm_hw_params *hw_params)
  1241. {
  1242. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1243. struct snd_pcm_runtime *runtime = substream->runtime;
  1244. struct esschan *chan = runtime->private_data;
  1245. int size = params_buffer_bytes(hw_params);
  1246. if (chan->memory) {
  1247. if (chan->memory->buf.bytes >= size) {
  1248. runtime->dma_bytes = size;
  1249. return 0;
  1250. }
  1251. snd_es1968_free_memory(chip, chan->memory);
  1252. }
  1253. chan->memory = snd_es1968_new_memory(chip, size);
  1254. if (chan->memory == NULL) {
  1255. dev_dbg(chip->card->dev,
  1256. "cannot allocate dma buffer: size = %d\n", size);
  1257. return -ENOMEM;
  1258. }
  1259. snd_pcm_set_runtime_buffer(substream, &chan->memory->buf);
  1260. return 1; /* area was changed */
  1261. }
  1262. /* remove dma areas if allocated */
  1263. static int snd_es1968_hw_free(struct snd_pcm_substream *substream)
  1264. {
  1265. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1266. struct snd_pcm_runtime *runtime = substream->runtime;
  1267. struct esschan *chan;
  1268. if (runtime->private_data == NULL)
  1269. return 0;
  1270. chan = runtime->private_data;
  1271. if (chan->memory) {
  1272. snd_es1968_free_memory(chip, chan->memory);
  1273. chan->memory = NULL;
  1274. }
  1275. return 0;
  1276. }
  1277. /*
  1278. * allocate APU pair
  1279. */
  1280. static int snd_es1968_alloc_apu_pair(struct es1968 *chip, int type)
  1281. {
  1282. int apu;
  1283. for (apu = 0; apu < NR_APUS; apu += 2) {
  1284. if (chip->apu[apu] == ESM_APU_FREE &&
  1285. chip->apu[apu + 1] == ESM_APU_FREE) {
  1286. chip->apu[apu] = chip->apu[apu + 1] = type;
  1287. return apu;
  1288. }
  1289. }
  1290. return -EBUSY;
  1291. }
  1292. /*
  1293. * release APU pair
  1294. */
  1295. static void snd_es1968_free_apu_pair(struct es1968 *chip, int apu)
  1296. {
  1297. chip->apu[apu] = chip->apu[apu + 1] = ESM_APU_FREE;
  1298. }
  1299. /******************
  1300. * PCM open/close *
  1301. ******************/
  1302. static int snd_es1968_playback_open(struct snd_pcm_substream *substream)
  1303. {
  1304. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1305. struct snd_pcm_runtime *runtime = substream->runtime;
  1306. struct esschan *es;
  1307. int apu1;
  1308. /* search 2 APUs */
  1309. apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
  1310. if (apu1 < 0)
  1311. return apu1;
  1312. es = kzalloc(sizeof(*es), GFP_KERNEL);
  1313. if (!es) {
  1314. snd_es1968_free_apu_pair(chip, apu1);
  1315. return -ENOMEM;
  1316. }
  1317. es->apu[0] = apu1;
  1318. es->apu[1] = apu1 + 1;
  1319. es->apu_mode[0] = 0;
  1320. es->apu_mode[1] = 0;
  1321. es->running = 0;
  1322. es->substream = substream;
  1323. es->mode = ESM_MODE_PLAY;
  1324. runtime->private_data = es;
  1325. runtime->hw = snd_es1968_playback;
  1326. runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
  1327. calc_available_memory_size(chip);
  1328. spin_lock_irq(&chip->substream_lock);
  1329. list_add(&es->list, &chip->substream_list);
  1330. spin_unlock_irq(&chip->substream_lock);
  1331. return 0;
  1332. }
  1333. static int snd_es1968_capture_open(struct snd_pcm_substream *substream)
  1334. {
  1335. struct snd_pcm_runtime *runtime = substream->runtime;
  1336. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1337. struct esschan *es;
  1338. int apu1, apu2;
  1339. apu1 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_CAPTURE);
  1340. if (apu1 < 0)
  1341. return apu1;
  1342. apu2 = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_RATECONV);
  1343. if (apu2 < 0) {
  1344. snd_es1968_free_apu_pair(chip, apu1);
  1345. return apu2;
  1346. }
  1347. es = kzalloc(sizeof(*es), GFP_KERNEL);
  1348. if (!es) {
  1349. snd_es1968_free_apu_pair(chip, apu1);
  1350. snd_es1968_free_apu_pair(chip, apu2);
  1351. return -ENOMEM;
  1352. }
  1353. es->apu[0] = apu1;
  1354. es->apu[1] = apu1 + 1;
  1355. es->apu[2] = apu2;
  1356. es->apu[3] = apu2 + 1;
  1357. es->apu_mode[0] = 0;
  1358. es->apu_mode[1] = 0;
  1359. es->apu_mode[2] = 0;
  1360. es->apu_mode[3] = 0;
  1361. es->running = 0;
  1362. es->substream = substream;
  1363. es->mode = ESM_MODE_CAPTURE;
  1364. /* get mixbuffer */
  1365. es->mixbuf = snd_es1968_new_memory(chip, ESM_MIXBUF_SIZE);
  1366. if (!es->mixbuf) {
  1367. snd_es1968_free_apu_pair(chip, apu1);
  1368. snd_es1968_free_apu_pair(chip, apu2);
  1369. kfree(es);
  1370. return -ENOMEM;
  1371. }
  1372. memset(es->mixbuf->buf.area, 0, ESM_MIXBUF_SIZE);
  1373. runtime->private_data = es;
  1374. runtime->hw = snd_es1968_capture;
  1375. runtime->hw.buffer_bytes_max = runtime->hw.period_bytes_max =
  1376. calc_available_memory_size(chip) - 1024; /* keep MIXBUF size */
  1377. snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  1378. spin_lock_irq(&chip->substream_lock);
  1379. list_add(&es->list, &chip->substream_list);
  1380. spin_unlock_irq(&chip->substream_lock);
  1381. return 0;
  1382. }
  1383. static int snd_es1968_playback_close(struct snd_pcm_substream *substream)
  1384. {
  1385. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1386. struct esschan *es;
  1387. if (substream->runtime->private_data == NULL)
  1388. return 0;
  1389. es = substream->runtime->private_data;
  1390. spin_lock_irq(&chip->substream_lock);
  1391. list_del(&es->list);
  1392. spin_unlock_irq(&chip->substream_lock);
  1393. snd_es1968_free_apu_pair(chip, es->apu[0]);
  1394. kfree(es);
  1395. return 0;
  1396. }
  1397. static int snd_es1968_capture_close(struct snd_pcm_substream *substream)
  1398. {
  1399. struct es1968 *chip = snd_pcm_substream_chip(substream);
  1400. struct esschan *es;
  1401. if (substream->runtime->private_data == NULL)
  1402. return 0;
  1403. es = substream->runtime->private_data;
  1404. spin_lock_irq(&chip->substream_lock);
  1405. list_del(&es->list);
  1406. spin_unlock_irq(&chip->substream_lock);
  1407. snd_es1968_free_memory(chip, es->mixbuf);
  1408. snd_es1968_free_apu_pair(chip, es->apu[0]);
  1409. snd_es1968_free_apu_pair(chip, es->apu[2]);
  1410. kfree(es);
  1411. return 0;
  1412. }
  1413. static const struct snd_pcm_ops snd_es1968_playback_ops = {
  1414. .open = snd_es1968_playback_open,
  1415. .close = snd_es1968_playback_close,
  1416. .hw_params = snd_es1968_hw_params,
  1417. .hw_free = snd_es1968_hw_free,
  1418. .prepare = snd_es1968_pcm_prepare,
  1419. .trigger = snd_es1968_pcm_trigger,
  1420. .pointer = snd_es1968_pcm_pointer,
  1421. };
  1422. static const struct snd_pcm_ops snd_es1968_capture_ops = {
  1423. .open = snd_es1968_capture_open,
  1424. .close = snd_es1968_capture_close,
  1425. .hw_params = snd_es1968_hw_params,
  1426. .hw_free = snd_es1968_hw_free,
  1427. .prepare = snd_es1968_pcm_prepare,
  1428. .trigger = snd_es1968_pcm_trigger,
  1429. .pointer = snd_es1968_pcm_pointer,
  1430. };
  1431. /*
  1432. * measure clock
  1433. */
  1434. #define CLOCK_MEASURE_BUFSIZE 16768 /* enough large for a single shot */
  1435. static void es1968_measure_clock(struct es1968 *chip)
  1436. {
  1437. int i, apu;
  1438. unsigned int pa, offset, t;
  1439. struct esm_memory *memory;
  1440. ktime_t start_time, stop_time;
  1441. ktime_t diff;
  1442. if (chip->clock == 0)
  1443. chip->clock = 48000; /* default clock value */
  1444. /* search 2 APUs (although one apu is enough) */
  1445. apu = snd_es1968_alloc_apu_pair(chip, ESM_APU_PCM_PLAY);
  1446. if (apu < 0) {
  1447. dev_err(chip->card->dev, "Hmm, cannot find empty APU pair!?\n");
  1448. return;
  1449. }
  1450. memory = snd_es1968_new_memory(chip, CLOCK_MEASURE_BUFSIZE);
  1451. if (!memory) {
  1452. dev_warn(chip->card->dev,
  1453. "cannot allocate dma buffer - using default clock %d\n",
  1454. chip->clock);
  1455. snd_es1968_free_apu_pair(chip, apu);
  1456. return;
  1457. }
  1458. memset(memory->buf.area, 0, CLOCK_MEASURE_BUFSIZE);
  1459. wave_set_register(chip, apu << 3, (memory->buf.addr - 0x10) & 0xfff8);
  1460. pa = (unsigned int)((memory->buf.addr - chip->dma.addr) >> 1);
  1461. pa |= 0x00400000; /* System RAM (Bit 22) */
  1462. /* initialize apu */
  1463. for (i = 0; i < 16; i++)
  1464. apu_set_register(chip, apu, i, 0x0000);
  1465. apu_set_register(chip, apu, 0, 0x400f);
  1466. apu_set_register(chip, apu, 4, ((pa >> 16) & 0xff) << 8);
  1467. apu_set_register(chip, apu, 5, pa & 0xffff);
  1468. apu_set_register(chip, apu, 6, (pa + CLOCK_MEASURE_BUFSIZE/2) & 0xffff);
  1469. apu_set_register(chip, apu, 7, CLOCK_MEASURE_BUFSIZE/2);
  1470. apu_set_register(chip, apu, 8, 0x0000);
  1471. apu_set_register(chip, apu, 9, 0xD000);
  1472. apu_set_register(chip, apu, 10, 0x8F08);
  1473. apu_set_register(chip, apu, 11, 0x0000);
  1474. spin_lock_irq(&chip->reg_lock);
  1475. outw(1, chip->io_port + 0x04); /* clear WP interrupts */
  1476. outw(inw(chip->io_port + ESM_PORT_HOST_IRQ) | ESM_HIRQ_DSIE, chip->io_port + ESM_PORT_HOST_IRQ); /* enable WP ints */
  1477. spin_unlock_irq(&chip->reg_lock);
  1478. snd_es1968_apu_set_freq(chip, apu, ((unsigned int)48000 << 16) / chip->clock); /* 48000 Hz */
  1479. chip->in_measurement = 1;
  1480. chip->measure_apu = apu;
  1481. spin_lock_irq(&chip->reg_lock);
  1482. snd_es1968_bob_inc(chip, ESM_BOB_FREQ);
  1483. __apu_set_register(chip, apu, 5, pa & 0xffff);
  1484. snd_es1968_trigger_apu(chip, apu, ESM_APU_16BITLINEAR);
  1485. start_time = ktime_get();
  1486. spin_unlock_irq(&chip->reg_lock);
  1487. msleep(50);
  1488. spin_lock_irq(&chip->reg_lock);
  1489. offset = __apu_get_register(chip, apu, 5);
  1490. stop_time = ktime_get();
  1491. snd_es1968_trigger_apu(chip, apu, 0); /* stop */
  1492. snd_es1968_bob_dec(chip);
  1493. chip->in_measurement = 0;
  1494. spin_unlock_irq(&chip->reg_lock);
  1495. /* check the current position */
  1496. offset -= (pa & 0xffff);
  1497. offset &= 0xfffe;
  1498. offset += chip->measure_count * (CLOCK_MEASURE_BUFSIZE/2);
  1499. diff = ktime_sub(stop_time, start_time);
  1500. t = ktime_to_us(diff);
  1501. if (t == 0) {
  1502. dev_err(chip->card->dev, "?? calculation error..\n");
  1503. } else {
  1504. offset *= 1000;
  1505. offset = (offset / t) * 1000 + ((offset % t) * 1000) / t;
  1506. if (offset < 47500 || offset > 48500) {
  1507. if (offset >= 40000 && offset <= 50000)
  1508. chip->clock = (chip->clock * offset) / 48000;
  1509. }
  1510. dev_info(chip->card->dev, "clocking to %d\n", chip->clock);
  1511. }
  1512. snd_es1968_free_memory(chip, memory);
  1513. snd_es1968_free_apu_pair(chip, apu);
  1514. }
  1515. /*
  1516. */
  1517. static void snd_es1968_pcm_free(struct snd_pcm *pcm)
  1518. {
  1519. struct es1968 *esm = pcm->private_data;
  1520. snd_es1968_free_dmabuf(esm);
  1521. esm->pcm = NULL;
  1522. }
  1523. static int
  1524. snd_es1968_pcm(struct es1968 *chip, int device)
  1525. {
  1526. struct snd_pcm *pcm;
  1527. int err;
  1528. /* get DMA buffer */
  1529. err = snd_es1968_init_dmabuf(chip);
  1530. if (err < 0)
  1531. return err;
  1532. /* set PCMBAR */
  1533. wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
  1534. wave_set_register(chip, 0x01FD, chip->dma.addr >> 12);
  1535. wave_set_register(chip, 0x01FE, chip->dma.addr >> 12);
  1536. wave_set_register(chip, 0x01FF, chip->dma.addr >> 12);
  1537. err = snd_pcm_new(chip->card, "ESS Maestro", device,
  1538. chip->playback_streams,
  1539. chip->capture_streams, &pcm);
  1540. if (err < 0)
  1541. return err;
  1542. pcm->private_data = chip;
  1543. pcm->private_free = snd_es1968_pcm_free;
  1544. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1968_playback_ops);
  1545. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1968_capture_ops);
  1546. pcm->info_flags = 0;
  1547. strcpy(pcm->name, "ESS Maestro");
  1548. chip->pcm = pcm;
  1549. return 0;
  1550. }
  1551. /*
  1552. * suppress jitter on some maestros when playing stereo
  1553. */
  1554. static void snd_es1968_suppress_jitter(struct es1968 *chip, struct esschan *es)
  1555. {
  1556. unsigned int cp1;
  1557. unsigned int cp2;
  1558. unsigned int diff;
  1559. cp1 = __apu_get_register(chip, 0, 5);
  1560. cp2 = __apu_get_register(chip, 1, 5);
  1561. diff = (cp1 > cp2 ? cp1 - cp2 : cp2 - cp1);
  1562. if (diff > 1)
  1563. __maestro_write(chip, IDR0_DATA_PORT, cp1);
  1564. }
  1565. /*
  1566. * update pointer
  1567. */
  1568. static void snd_es1968_update_pcm(struct es1968 *chip, struct esschan *es)
  1569. {
  1570. unsigned int hwptr;
  1571. unsigned int diff;
  1572. struct snd_pcm_substream *subs = es->substream;
  1573. if (subs == NULL || !es->running)
  1574. return;
  1575. hwptr = snd_es1968_get_dma_ptr(chip, es) << es->wav_shift;
  1576. hwptr %= es->dma_size;
  1577. diff = (es->dma_size + hwptr - es->hwptr) % es->dma_size;
  1578. es->hwptr = hwptr;
  1579. es->count += diff;
  1580. if (es->count > es->frag_size) {
  1581. spin_unlock(&chip->substream_lock);
  1582. snd_pcm_period_elapsed(subs);
  1583. spin_lock(&chip->substream_lock);
  1584. es->count %= es->frag_size;
  1585. }
  1586. }
  1587. /* The hardware volume works by incrementing / decrementing 2 counters
  1588. (without wrap around) in response to volume button presses and then
  1589. generating an interrupt. The pair of counters is stored in bits 1-3 and 5-7
  1590. of a byte wide register. The meaning of bits 0 and 4 is unknown. */
  1591. static void es1968_update_hw_volume(struct work_struct *work)
  1592. {
  1593. struct es1968 *chip = container_of(work, struct es1968, hwvol_work);
  1594. int x, val;
  1595. /* Figure out which volume control button was pushed,
  1596. based on differences from the default register
  1597. values. */
  1598. x = inb(chip->io_port + 0x1c) & 0xee;
  1599. /* Reset the volume control registers. */
  1600. outb(0x88, chip->io_port + 0x1c);
  1601. outb(0x88, chip->io_port + 0x1d);
  1602. outb(0x88, chip->io_port + 0x1e);
  1603. outb(0x88, chip->io_port + 0x1f);
  1604. if (chip->in_suspend)
  1605. return;
  1606. #ifndef CONFIG_SND_ES1968_INPUT
  1607. if (! chip->master_switch || ! chip->master_volume)
  1608. return;
  1609. val = snd_ac97_read(chip->ac97, AC97_MASTER);
  1610. switch (x) {
  1611. case 0x88:
  1612. /* mute */
  1613. val ^= 0x8000;
  1614. break;
  1615. case 0xaa:
  1616. /* volume up */
  1617. if ((val & 0x7f) > 0)
  1618. val--;
  1619. if ((val & 0x7f00) > 0)
  1620. val -= 0x0100;
  1621. break;
  1622. case 0x66:
  1623. /* volume down */
  1624. if ((val & 0x7f) < 0x1f)
  1625. val++;
  1626. if ((val & 0x7f00) < 0x1f00)
  1627. val += 0x0100;
  1628. break;
  1629. }
  1630. if (snd_ac97_update(chip->ac97, AC97_MASTER, val))
  1631. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  1632. &chip->master_volume->id);
  1633. #else
  1634. if (!chip->input_dev)
  1635. return;
  1636. val = 0;
  1637. switch (x) {
  1638. case 0x88:
  1639. /* The counters have not changed, yet we've received a HV
  1640. interrupt. According to tests run by various people this
  1641. happens when pressing the mute button. */
  1642. val = KEY_MUTE;
  1643. break;
  1644. case 0xaa:
  1645. /* counters increased by 1 -> volume up */
  1646. val = KEY_VOLUMEUP;
  1647. break;
  1648. case 0x66:
  1649. /* counters decreased by 1 -> volume down */
  1650. val = KEY_VOLUMEDOWN;
  1651. break;
  1652. }
  1653. if (val) {
  1654. input_report_key(chip->input_dev, val, 1);
  1655. input_sync(chip->input_dev);
  1656. input_report_key(chip->input_dev, val, 0);
  1657. input_sync(chip->input_dev);
  1658. }
  1659. #endif
  1660. }
  1661. /*
  1662. * interrupt handler
  1663. */
  1664. static irqreturn_t snd_es1968_interrupt(int irq, void *dev_id)
  1665. {
  1666. struct es1968 *chip = dev_id;
  1667. u32 event;
  1668. event = inb(chip->io_port + 0x1A);
  1669. if (!event)
  1670. return IRQ_NONE;
  1671. outw(inw(chip->io_port + 4) & 1, chip->io_port + 4);
  1672. if (event & ESM_HWVOL_IRQ)
  1673. schedule_work(&chip->hwvol_work);
  1674. /* else ack 'em all, i imagine */
  1675. outb(0xFF, chip->io_port + 0x1A);
  1676. if ((event & ESM_MPU401_IRQ) && chip->rmidi) {
  1677. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  1678. }
  1679. if (event & ESM_SOUND_IRQ) {
  1680. struct esschan *es;
  1681. spin_lock(&chip->substream_lock);
  1682. list_for_each_entry(es, &chip->substream_list, list) {
  1683. if (es->running) {
  1684. snd_es1968_update_pcm(chip, es);
  1685. if (es->fmt & ESS_FMT_STEREO)
  1686. snd_es1968_suppress_jitter(chip, es);
  1687. }
  1688. }
  1689. spin_unlock(&chip->substream_lock);
  1690. if (chip->in_measurement) {
  1691. unsigned int curp = __apu_get_register(chip, chip->measure_apu, 5);
  1692. if (curp < chip->measure_lastpos)
  1693. chip->measure_count++;
  1694. chip->measure_lastpos = curp;
  1695. }
  1696. }
  1697. return IRQ_HANDLED;
  1698. }
  1699. /*
  1700. * Mixer stuff
  1701. */
  1702. static int
  1703. snd_es1968_mixer(struct es1968 *chip)
  1704. {
  1705. struct snd_ac97_bus *pbus;
  1706. struct snd_ac97_template ac97;
  1707. #ifndef CONFIG_SND_ES1968_INPUT
  1708. struct snd_ctl_elem_id elem_id;
  1709. #endif
  1710. int err;
  1711. static const struct snd_ac97_bus_ops ops = {
  1712. .write = snd_es1968_ac97_write,
  1713. .read = snd_es1968_ac97_read,
  1714. };
  1715. err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus);
  1716. if (err < 0)
  1717. return err;
  1718. pbus->no_vra = 1; /* ES1968 doesn't need VRA */
  1719. memset(&ac97, 0, sizeof(ac97));
  1720. ac97.private_data = chip;
  1721. err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
  1722. if (err < 0)
  1723. return err;
  1724. #ifndef CONFIG_SND_ES1968_INPUT
  1725. /* attach master switch / volumes for h/w volume control */
  1726. memset(&elem_id, 0, sizeof(elem_id));
  1727. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1728. strcpy(elem_id.name, "Master Playback Switch");
  1729. chip->master_switch = snd_ctl_find_id(chip->card, &elem_id);
  1730. memset(&elem_id, 0, sizeof(elem_id));
  1731. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1732. strcpy(elem_id.name, "Master Playback Volume");
  1733. chip->master_volume = snd_ctl_find_id(chip->card, &elem_id);
  1734. #endif
  1735. return 0;
  1736. }
  1737. /*
  1738. * reset ac97 codec
  1739. */
  1740. static void snd_es1968_ac97_reset(struct es1968 *chip)
  1741. {
  1742. unsigned long ioaddr = chip->io_port;
  1743. unsigned short save_ringbus_a;
  1744. unsigned short save_68;
  1745. unsigned short w;
  1746. unsigned int vend;
  1747. /* save configuration */
  1748. save_ringbus_a = inw(ioaddr + 0x36);
  1749. //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38); /* clear second codec id? */
  1750. /* set command/status address i/o to 1st codec */
  1751. outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
  1752. outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
  1753. /* disable ac link */
  1754. outw(0x0000, ioaddr + 0x36);
  1755. save_68 = inw(ioaddr + 0x68);
  1756. pci_read_config_word(chip->pci, 0x58, &w); /* something magical with gpio and bus arb. */
  1757. pci_read_config_dword(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  1758. if (w & 1)
  1759. save_68 |= 0x10;
  1760. outw(0xfffe, ioaddr + 0x64); /* unmask gpio 0 */
  1761. outw(0x0001, ioaddr + 0x68); /* gpio write */
  1762. outw(0x0000, ioaddr + 0x60); /* write 0 to gpio 0 */
  1763. udelay(20);
  1764. outw(0x0001, ioaddr + 0x60); /* write 1 to gpio 1 */
  1765. msleep(20);
  1766. outw(save_68 | 0x1, ioaddr + 0x68); /* now restore .. */
  1767. outw((inw(ioaddr + 0x38) & 0xfffc) | 0x1, ioaddr + 0x38);
  1768. outw((inw(ioaddr + 0x3a) & 0xfffc) | 0x1, ioaddr + 0x3a);
  1769. outw((inw(ioaddr + 0x3c) & 0xfffc) | 0x1, ioaddr + 0x3c);
  1770. /* now the second codec */
  1771. /* disable ac link */
  1772. outw(0x0000, ioaddr + 0x36);
  1773. outw(0xfff7, ioaddr + 0x64); /* unmask gpio 3 */
  1774. save_68 = inw(ioaddr + 0x68);
  1775. outw(0x0009, ioaddr + 0x68); /* gpio write 0 & 3 ?? */
  1776. outw(0x0001, ioaddr + 0x60); /* write 1 to gpio */
  1777. udelay(20);
  1778. outw(0x0009, ioaddr + 0x60); /* write 9 to gpio */
  1779. msleep(500);
  1780. //outw(inw(ioaddr + 0x38) & 0xfffc, ioaddr + 0x38);
  1781. outw(inw(ioaddr + 0x3a) & 0xfffc, ioaddr + 0x3a);
  1782. outw(inw(ioaddr + 0x3c) & 0xfffc, ioaddr + 0x3c);
  1783. #if 0 /* the loop here needs to be much better if we want it.. */
  1784. dev_info(chip->card->dev, "trying software reset\n");
  1785. /* try and do a software reset */
  1786. outb(0x80 | 0x7c, ioaddr + 0x30);
  1787. for (w = 0;; w++) {
  1788. if ((inw(ioaddr + 0x30) & 1) == 0) {
  1789. if (inb(ioaddr + 0x32) != 0)
  1790. break;
  1791. outb(0x80 | 0x7d, ioaddr + 0x30);
  1792. if (((inw(ioaddr + 0x30) & 1) == 0)
  1793. && (inb(ioaddr + 0x32) != 0))
  1794. break;
  1795. outb(0x80 | 0x7f, ioaddr + 0x30);
  1796. if (((inw(ioaddr + 0x30) & 1) == 0)
  1797. && (inb(ioaddr + 0x32) != 0))
  1798. break;
  1799. }
  1800. if (w > 10000) {
  1801. outb(inb(ioaddr + 0x37) | 0x08, ioaddr + 0x37); /* do a software reset */
  1802. msleep(500); /* oh my.. */
  1803. outb(inb(ioaddr + 0x37) & ~0x08,
  1804. ioaddr + 0x37);
  1805. udelay(1);
  1806. outw(0x80, ioaddr + 0x30);
  1807. for (w = 0; w < 10000; w++) {
  1808. if ((inw(ioaddr + 0x30) & 1) == 0)
  1809. break;
  1810. }
  1811. }
  1812. }
  1813. #endif
  1814. if (vend == NEC_VERSA_SUBID1 || vend == NEC_VERSA_SUBID2) {
  1815. /* turn on external amp? */
  1816. outw(0xf9ff, ioaddr + 0x64);
  1817. outw(inw(ioaddr + 0x68) | 0x600, ioaddr + 0x68);
  1818. outw(0x0209, ioaddr + 0x60);
  1819. }
  1820. /* restore.. */
  1821. outw(save_ringbus_a, ioaddr + 0x36);
  1822. /* Turn on the 978 docking chip.
  1823. First frob the "master output enable" bit,
  1824. then set most of the playback volume control registers to max. */
  1825. outb(inb(ioaddr+0xc0)|(1<<5), ioaddr+0xc0);
  1826. outb(0xff, ioaddr+0xc3);
  1827. outb(0xff, ioaddr+0xc4);
  1828. outb(0xff, ioaddr+0xc6);
  1829. outb(0xff, ioaddr+0xc8);
  1830. outb(0x3f, ioaddr+0xcf);
  1831. outb(0x3f, ioaddr+0xd0);
  1832. }
  1833. static void snd_es1968_reset(struct es1968 *chip)
  1834. {
  1835. /* Reset */
  1836. outw(ESM_RESET_MAESTRO | ESM_RESET_DIRECTSOUND,
  1837. chip->io_port + ESM_PORT_HOST_IRQ);
  1838. udelay(10);
  1839. outw(0x0000, chip->io_port + ESM_PORT_HOST_IRQ);
  1840. udelay(10);
  1841. }
  1842. /*
  1843. * initialize maestro chip
  1844. */
  1845. static void snd_es1968_chip_init(struct es1968 *chip)
  1846. {
  1847. struct pci_dev *pci = chip->pci;
  1848. int i;
  1849. unsigned long iobase = chip->io_port;
  1850. u16 w;
  1851. u32 n;
  1852. /* We used to muck around with pci config space that
  1853. * we had no business messing with. We don't know enough
  1854. * about the machine to know which DMA mode is appropriate,
  1855. * etc. We were guessing wrong on some machines and making
  1856. * them unhappy. We now trust in the BIOS to do things right,
  1857. * which almost certainly means a new host of problems will
  1858. * arise with broken BIOS implementations. screw 'em.
  1859. * We're already intolerant of machines that don't assign
  1860. * IRQs.
  1861. */
  1862. /* Config Reg A */
  1863. pci_read_config_word(pci, ESM_CONFIG_A, &w);
  1864. w &= ~DMA_CLEAR; /* Clear DMA bits */
  1865. w &= ~(PIC_SNOOP1 | PIC_SNOOP2); /* Clear Pic Snoop Mode Bits */
  1866. w &= ~SAFEGUARD; /* Safeguard off */
  1867. w |= POST_WRITE; /* Posted write */
  1868. w |= PCI_TIMING; /* PCI timing on */
  1869. /* XXX huh? claims to be reserved.. */
  1870. w &= ~SWAP_LR; /* swap left/right
  1871. seems to only have effect on SB
  1872. Emulation */
  1873. w &= ~SUBTR_DECODE; /* Subtractive decode off */
  1874. pci_write_config_word(pci, ESM_CONFIG_A, w);
  1875. /* Config Reg B */
  1876. pci_read_config_word(pci, ESM_CONFIG_B, &w);
  1877. w &= ~(1 << 15); /* Turn off internal clock multiplier */
  1878. /* XXX how do we know which to use? */
  1879. w &= ~(1 << 14); /* External clock */
  1880. w &= ~SPDIF_CONFB; /* disable S/PDIF output */
  1881. w |= HWV_CONFB; /* HWV on */
  1882. w |= DEBOUNCE; /* Debounce off: easier to push the HW buttons */
  1883. w &= ~GPIO_CONFB; /* GPIO 4:5 */
  1884. w |= CHI_CONFB; /* Disconnect from the CHI. Enabling this made a dell 7500 work. */
  1885. w &= ~IDMA_CONFB; /* IDMA off (undocumented) */
  1886. w &= ~MIDI_FIX; /* MIDI fix off (undoc) */
  1887. w &= ~(1 << 1); /* reserved, always write 0 */
  1888. w &= ~IRQ_TO_ISA; /* IRQ to ISA off (undoc) */
  1889. pci_write_config_word(pci, ESM_CONFIG_B, w);
  1890. /* DDMA off */
  1891. pci_read_config_word(pci, ESM_DDMA, &w);
  1892. w &= ~(1 << 0);
  1893. pci_write_config_word(pci, ESM_DDMA, w);
  1894. /*
  1895. * Legacy mode
  1896. */
  1897. pci_read_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, &w);
  1898. w |= ESS_DISABLE_AUDIO; /* Disable Legacy Audio */
  1899. w &= ~ESS_ENABLE_SERIAL_IRQ; /* Disable SIRQ */
  1900. w &= ~(0x1f); /* disable mpu irq/io, game port, fm, SB */
  1901. pci_write_config_word(pci, ESM_LEGACY_AUDIO_CONTROL, w);
  1902. /* Set up 978 docking control chip. */
  1903. pci_read_config_word(pci, 0x58, &w);
  1904. w|=1<<2; /* Enable 978. */
  1905. w|=1<<3; /* Turn on 978 hardware volume control. */
  1906. w&=~(1<<11); /* Turn on 978 mixer volume control. */
  1907. pci_write_config_word(pci, 0x58, w);
  1908. /* Sound Reset */
  1909. snd_es1968_reset(chip);
  1910. /*
  1911. * Ring Bus Setup
  1912. */
  1913. /* setup usual 0x34 stuff.. 0x36 may be chip specific */
  1914. outw(0xC090, iobase + ESM_RING_BUS_DEST); /* direct sound, stereo */
  1915. udelay(20);
  1916. outw(0x3000, iobase + ESM_RING_BUS_CONTR_A); /* enable ringbus/serial */
  1917. udelay(20);
  1918. /*
  1919. * Reset the CODEC
  1920. */
  1921. snd_es1968_ac97_reset(chip);
  1922. /* Ring Bus Control B */
  1923. n = inl(iobase + ESM_RING_BUS_CONTR_B);
  1924. n &= ~RINGB_EN_SPDIF; /* SPDIF off */
  1925. //w |= RINGB_EN_2CODEC; /* enable 2nd codec */
  1926. outl(n, iobase + ESM_RING_BUS_CONTR_B);
  1927. /* Set hardware volume control registers to midpoints.
  1928. We can tell which button was pushed based on how they change. */
  1929. outb(0x88, iobase+0x1c);
  1930. outb(0x88, iobase+0x1d);
  1931. outb(0x88, iobase+0x1e);
  1932. outb(0x88, iobase+0x1f);
  1933. /* it appears some maestros (dell 7500) only work if these are set,
  1934. regardless of whether we use the assp or not. */
  1935. outb(0, iobase + ASSP_CONTROL_B);
  1936. outb(3, iobase + ASSP_CONTROL_A); /* M: Reserved bits... */
  1937. outb(0, iobase + ASSP_CONTROL_C); /* M: Disable ASSP, ASSP IRQ's and FM Port */
  1938. /*
  1939. * set up wavecache
  1940. */
  1941. for (i = 0; i < 16; i++) {
  1942. /* Write 0 into the buffer area 0x1E0->1EF */
  1943. outw(0x01E0 + i, iobase + WC_INDEX);
  1944. outw(0x0000, iobase + WC_DATA);
  1945. /* The 1.10 test program seem to write 0 into the buffer area
  1946. * 0x1D0-0x1DF too.*/
  1947. outw(0x01D0 + i, iobase + WC_INDEX);
  1948. outw(0x0000, iobase + WC_DATA);
  1949. }
  1950. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  1951. (wave_get_register(chip, IDR7_WAVE_ROMRAM) & 0xFF00));
  1952. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  1953. wave_get_register(chip, IDR7_WAVE_ROMRAM) | 0x100);
  1954. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  1955. wave_get_register(chip, IDR7_WAVE_ROMRAM) & ~0x200);
  1956. wave_set_register(chip, IDR7_WAVE_ROMRAM,
  1957. wave_get_register(chip, IDR7_WAVE_ROMRAM) | ~0x400);
  1958. maestro_write(chip, IDR2_CRAM_DATA, 0x0000);
  1959. /* Now back to the DirectSound stuff */
  1960. /* audio serial configuration.. ? */
  1961. maestro_write(chip, 0x08, 0xB004);
  1962. maestro_write(chip, 0x09, 0x001B);
  1963. maestro_write(chip, 0x0A, 0x8000);
  1964. maestro_write(chip, 0x0B, 0x3F37);
  1965. maestro_write(chip, 0x0C, 0x0098);
  1966. /* parallel in, has something to do with recording :) */
  1967. maestro_write(chip, 0x0C,
  1968. (maestro_read(chip, 0x0C) & ~0xF000) | 0x8000);
  1969. /* parallel out */
  1970. maestro_write(chip, 0x0C,
  1971. (maestro_read(chip, 0x0C) & ~0x0F00) | 0x0500);
  1972. maestro_write(chip, 0x0D, 0x7632);
  1973. /* Wave cache control on - test off, sg off,
  1974. enable, enable extra chans 1Mb */
  1975. w = inw(iobase + WC_CONTROL);
  1976. w &= ~0xFA00; /* Seems to be reserved? I don't know */
  1977. w |= 0xA000; /* reserved... I don't know */
  1978. w &= ~0x0200; /* Channels 56,57,58,59 as Extra Play,Rec Channel enable
  1979. Seems to crash the Computer if enabled... */
  1980. w |= 0x0100; /* Wave Cache Operation Enabled */
  1981. w |= 0x0080; /* Channels 60/61 as Placback/Record enabled */
  1982. w &= ~0x0060; /* Clear Wavtable Size */
  1983. w |= 0x0020; /* Wavetable Size : 1MB */
  1984. /* Bit 4 is reserved */
  1985. w &= ~0x000C; /* DMA Stuff? I don't understand what the datasheet means */
  1986. /* Bit 1 is reserved */
  1987. w &= ~0x0001; /* Test Mode off */
  1988. outw(w, iobase + WC_CONTROL);
  1989. /* Now clear the APU control ram */
  1990. for (i = 0; i < NR_APUS; i++) {
  1991. for (w = 0; w < NR_APU_REGS; w++)
  1992. apu_set_register(chip, i, w, 0);
  1993. }
  1994. }
  1995. /* Enable IRQ's */
  1996. static void snd_es1968_start_irq(struct es1968 *chip)
  1997. {
  1998. unsigned short w;
  1999. w = ESM_HIRQ_DSIE | ESM_HIRQ_HW_VOLUME;
  2000. if (chip->rmidi)
  2001. w |= ESM_HIRQ_MPU401;
  2002. outb(w, chip->io_port + 0x1A);
  2003. outw(w, chip->io_port + ESM_PORT_HOST_IRQ);
  2004. }
  2005. #ifdef CONFIG_PM_SLEEP
  2006. /*
  2007. * PM support
  2008. */
  2009. static int es1968_suspend(struct device *dev)
  2010. {
  2011. struct snd_card *card = dev_get_drvdata(dev);
  2012. struct es1968 *chip = card->private_data;
  2013. if (! chip->do_pm)
  2014. return 0;
  2015. chip->in_suspend = 1;
  2016. cancel_work_sync(&chip->hwvol_work);
  2017. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  2018. snd_ac97_suspend(chip->ac97);
  2019. snd_es1968_bob_stop(chip);
  2020. return 0;
  2021. }
  2022. static int es1968_resume(struct device *dev)
  2023. {
  2024. struct snd_card *card = dev_get_drvdata(dev);
  2025. struct es1968 *chip = card->private_data;
  2026. struct esschan *es;
  2027. if (! chip->do_pm)
  2028. return 0;
  2029. snd_es1968_chip_init(chip);
  2030. /* need to restore the base pointers.. */
  2031. if (chip->dma.addr) {
  2032. /* set PCMBAR */
  2033. wave_set_register(chip, 0x01FC, chip->dma.addr >> 12);
  2034. }
  2035. snd_es1968_start_irq(chip);
  2036. /* restore ac97 state */
  2037. snd_ac97_resume(chip->ac97);
  2038. list_for_each_entry(es, &chip->substream_list, list) {
  2039. switch (es->mode) {
  2040. case ESM_MODE_PLAY:
  2041. snd_es1968_playback_setup(chip, es, es->substream->runtime);
  2042. break;
  2043. case ESM_MODE_CAPTURE:
  2044. snd_es1968_capture_setup(chip, es, es->substream->runtime);
  2045. break;
  2046. }
  2047. }
  2048. /* start timer again */
  2049. if (chip->bobclient)
  2050. snd_es1968_bob_start(chip);
  2051. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  2052. chip->in_suspend = 0;
  2053. return 0;
  2054. }
  2055. static SIMPLE_DEV_PM_OPS(es1968_pm, es1968_suspend, es1968_resume);
  2056. #define ES1968_PM_OPS &es1968_pm
  2057. #else
  2058. #define ES1968_PM_OPS NULL
  2059. #endif /* CONFIG_PM_SLEEP */
  2060. #ifdef SUPPORT_JOYSTICK
  2061. #define JOYSTICK_ADDR 0x200
  2062. static int snd_es1968_create_gameport(struct es1968 *chip, int dev)
  2063. {
  2064. struct gameport *gp;
  2065. struct resource *r;
  2066. u16 val;
  2067. if (!joystick[dev])
  2068. return -ENODEV;
  2069. r = devm_request_region(&chip->pci->dev, JOYSTICK_ADDR, 8,
  2070. "ES1968 gameport");
  2071. if (!r)
  2072. return -EBUSY;
  2073. chip->gameport = gp = gameport_allocate_port();
  2074. if (!gp) {
  2075. dev_err(chip->card->dev,
  2076. "cannot allocate memory for gameport\n");
  2077. return -ENOMEM;
  2078. }
  2079. pci_read_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, &val);
  2080. pci_write_config_word(chip->pci, ESM_LEGACY_AUDIO_CONTROL, val | 0x04);
  2081. gameport_set_name(gp, "ES1968 Gameport");
  2082. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2083. gameport_set_dev_parent(gp, &chip->pci->dev);
  2084. gp->io = JOYSTICK_ADDR;
  2085. gameport_register_port(gp);
  2086. return 0;
  2087. }
  2088. static void snd_es1968_free_gameport(struct es1968 *chip)
  2089. {
  2090. if (chip->gameport) {
  2091. gameport_unregister_port(chip->gameport);
  2092. chip->gameport = NULL;
  2093. }
  2094. }
  2095. #else
  2096. static inline int snd_es1968_create_gameport(struct es1968 *chip, int dev) { return -ENOSYS; }
  2097. static inline void snd_es1968_free_gameport(struct es1968 *chip) { }
  2098. #endif
  2099. #ifdef CONFIG_SND_ES1968_INPUT
  2100. static int snd_es1968_input_register(struct es1968 *chip)
  2101. {
  2102. struct input_dev *input_dev;
  2103. int err;
  2104. input_dev = devm_input_allocate_device(&chip->pci->dev);
  2105. if (!input_dev)
  2106. return -ENOMEM;
  2107. snprintf(chip->phys, sizeof(chip->phys), "pci-%s/input0",
  2108. pci_name(chip->pci));
  2109. input_dev->name = chip->card->driver;
  2110. input_dev->phys = chip->phys;
  2111. input_dev->id.bustype = BUS_PCI;
  2112. input_dev->id.vendor = chip->pci->vendor;
  2113. input_dev->id.product = chip->pci->device;
  2114. input_dev->dev.parent = &chip->pci->dev;
  2115. __set_bit(EV_KEY, input_dev->evbit);
  2116. __set_bit(KEY_MUTE, input_dev->keybit);
  2117. __set_bit(KEY_VOLUMEDOWN, input_dev->keybit);
  2118. __set_bit(KEY_VOLUMEUP, input_dev->keybit);
  2119. err = input_register_device(input_dev);
  2120. if (err)
  2121. return err;
  2122. chip->input_dev = input_dev;
  2123. return 0;
  2124. }
  2125. #endif /* CONFIG_SND_ES1968_INPUT */
  2126. #ifdef CONFIG_SND_ES1968_RADIO
  2127. #define GPIO_DATA 0x60
  2128. #define IO_MASK 4 /* mask register offset from GPIO_DATA
  2129. bits 1=unmask write to given bit */
  2130. #define IO_DIR 8 /* direction register offset from GPIO_DATA
  2131. bits 0/1=read/write direction */
  2132. /* GPIO to TEA575x maps */
  2133. struct snd_es1968_tea575x_gpio {
  2134. u8 data, clk, wren, most;
  2135. char *name;
  2136. };
  2137. static const struct snd_es1968_tea575x_gpio snd_es1968_tea575x_gpios[] = {
  2138. { .data = 6, .clk = 7, .wren = 8, .most = 9, .name = "SF64-PCE2" },
  2139. { .data = 7, .clk = 8, .wren = 6, .most = 10, .name = "M56VAP" },
  2140. };
  2141. #define get_tea575x_gpio(chip) \
  2142. (&snd_es1968_tea575x_gpios[(chip)->tea575x_tuner])
  2143. static void snd_es1968_tea575x_set_pins(struct snd_tea575x *tea, u8 pins)
  2144. {
  2145. struct es1968 *chip = tea->private_data;
  2146. struct snd_es1968_tea575x_gpio gpio = *get_tea575x_gpio(chip);
  2147. u16 val = 0;
  2148. val |= (pins & TEA575X_DATA) ? (1 << gpio.data) : 0;
  2149. val |= (pins & TEA575X_CLK) ? (1 << gpio.clk) : 0;
  2150. val |= (pins & TEA575X_WREN) ? (1 << gpio.wren) : 0;
  2151. outw(val, chip->io_port + GPIO_DATA);
  2152. }
  2153. static u8 snd_es1968_tea575x_get_pins(struct snd_tea575x *tea)
  2154. {
  2155. struct es1968 *chip = tea->private_data;
  2156. struct snd_es1968_tea575x_gpio gpio = *get_tea575x_gpio(chip);
  2157. u16 val = inw(chip->io_port + GPIO_DATA);
  2158. u8 ret = 0;
  2159. if (val & (1 << gpio.data))
  2160. ret |= TEA575X_DATA;
  2161. if (val & (1 << gpio.most))
  2162. ret |= TEA575X_MOST;
  2163. return ret;
  2164. }
  2165. static void snd_es1968_tea575x_set_direction(struct snd_tea575x *tea, bool output)
  2166. {
  2167. struct es1968 *chip = tea->private_data;
  2168. unsigned long io = chip->io_port + GPIO_DATA;
  2169. u16 odir = inw(io + IO_DIR);
  2170. struct snd_es1968_tea575x_gpio gpio = *get_tea575x_gpio(chip);
  2171. if (output) {
  2172. outw(~((1 << gpio.data) | (1 << gpio.clk) | (1 << gpio.wren)),
  2173. io + IO_MASK);
  2174. outw(odir | (1 << gpio.data) | (1 << gpio.clk) | (1 << gpio.wren),
  2175. io + IO_DIR);
  2176. } else {
  2177. outw(~((1 << gpio.clk) | (1 << gpio.wren) | (1 << gpio.data) | (1 << gpio.most)),
  2178. io + IO_MASK);
  2179. outw((odir & ~((1 << gpio.data) | (1 << gpio.most)))
  2180. | (1 << gpio.clk) | (1 << gpio.wren), io + IO_DIR);
  2181. }
  2182. }
  2183. static const struct snd_tea575x_ops snd_es1968_tea_ops = {
  2184. .set_pins = snd_es1968_tea575x_set_pins,
  2185. .get_pins = snd_es1968_tea575x_get_pins,
  2186. .set_direction = snd_es1968_tea575x_set_direction,
  2187. };
  2188. #endif
  2189. static void snd_es1968_free(struct snd_card *card)
  2190. {
  2191. struct es1968 *chip = card->private_data;
  2192. cancel_work_sync(&chip->hwvol_work);
  2193. if (chip->io_port) {
  2194. outw(1, chip->io_port + 0x04); /* clear WP interrupts */
  2195. outw(0, chip->io_port + ESM_PORT_HOST_IRQ); /* disable IRQ */
  2196. }
  2197. #ifdef CONFIG_SND_ES1968_RADIO
  2198. snd_tea575x_exit(&chip->tea);
  2199. v4l2_device_unregister(&chip->v4l2_dev);
  2200. #endif
  2201. snd_es1968_free_gameport(chip);
  2202. }
  2203. struct ess_device_list {
  2204. unsigned short type; /* chip type */
  2205. unsigned short vendor; /* subsystem vendor id */
  2206. };
  2207. static const struct ess_device_list pm_allowlist[] = {
  2208. { TYPE_MAESTRO2E, 0x0e11 }, /* Compaq Armada */
  2209. { TYPE_MAESTRO2E, 0x1028 },
  2210. { TYPE_MAESTRO2E, 0x103c },
  2211. { TYPE_MAESTRO2E, 0x1179 },
  2212. { TYPE_MAESTRO2E, 0x14c0 }, /* HP omnibook 4150 */
  2213. { TYPE_MAESTRO2E, 0x1558 },
  2214. { TYPE_MAESTRO2E, 0x125d }, /* a PCI card, e.g. Terratec DMX */
  2215. { TYPE_MAESTRO2, 0x125d }, /* a PCI card, e.g. SF64-PCE2 */
  2216. };
  2217. static const struct ess_device_list mpu_denylist[] = {
  2218. { TYPE_MAESTRO2, 0x125d },
  2219. };
  2220. static int snd_es1968_create(struct snd_card *card,
  2221. struct pci_dev *pci,
  2222. int total_bufsize,
  2223. int play_streams,
  2224. int capt_streams,
  2225. int chip_type,
  2226. int do_pm,
  2227. int radio_nr)
  2228. {
  2229. struct es1968 *chip = card->private_data;
  2230. int i, err;
  2231. /* enable PCI device */
  2232. err = pcim_enable_device(pci);
  2233. if (err < 0)
  2234. return err;
  2235. /* check, if we can restrict PCI DMA transfers to 28 bits */
  2236. if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(28))) {
  2237. dev_err(card->dev,
  2238. "architecture does not support 28bit PCI busmaster DMA\n");
  2239. return -ENXIO;
  2240. }
  2241. /* Set Vars */
  2242. chip->type = chip_type;
  2243. spin_lock_init(&chip->reg_lock);
  2244. spin_lock_init(&chip->substream_lock);
  2245. INIT_LIST_HEAD(&chip->buf_list);
  2246. INIT_LIST_HEAD(&chip->substream_list);
  2247. mutex_init(&chip->memory_mutex);
  2248. INIT_WORK(&chip->hwvol_work, es1968_update_hw_volume);
  2249. chip->card = card;
  2250. chip->pci = pci;
  2251. chip->irq = -1;
  2252. chip->total_bufsize = total_bufsize; /* in bytes */
  2253. chip->playback_streams = play_streams;
  2254. chip->capture_streams = capt_streams;
  2255. err = pci_request_regions(pci, "ESS Maestro");
  2256. if (err < 0)
  2257. return err;
  2258. chip->io_port = pci_resource_start(pci, 0);
  2259. if (devm_request_irq(&pci->dev, pci->irq, snd_es1968_interrupt,
  2260. IRQF_SHARED, KBUILD_MODNAME, chip)) {
  2261. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  2262. return -EBUSY;
  2263. }
  2264. chip->irq = pci->irq;
  2265. card->sync_irq = chip->irq;
  2266. card->private_free = snd_es1968_free;
  2267. /* Clear Maestro_map */
  2268. for (i = 0; i < 32; i++)
  2269. chip->maestro_map[i] = 0;
  2270. /* Clear Apu Map */
  2271. for (i = 0; i < NR_APUS; i++)
  2272. chip->apu[i] = ESM_APU_FREE;
  2273. /* just to be sure */
  2274. pci_set_master(pci);
  2275. if (do_pm > 1) {
  2276. /* disable power-management if not on the allowlist */
  2277. unsigned short vend;
  2278. pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  2279. for (i = 0; i < (int)ARRAY_SIZE(pm_allowlist); i++) {
  2280. if (chip->type == pm_allowlist[i].type &&
  2281. vend == pm_allowlist[i].vendor) {
  2282. do_pm = 1;
  2283. break;
  2284. }
  2285. }
  2286. if (do_pm > 1) {
  2287. /* not matched; disabling pm */
  2288. dev_info(card->dev, "not attempting power management.\n");
  2289. do_pm = 0;
  2290. }
  2291. }
  2292. chip->do_pm = do_pm;
  2293. snd_es1968_chip_init(chip);
  2294. #ifdef CONFIG_SND_ES1968_RADIO
  2295. /* don't play with GPIOs on laptops */
  2296. if (chip->pci->subsystem_vendor != 0x125d)
  2297. return 0;
  2298. err = v4l2_device_register(&pci->dev, &chip->v4l2_dev);
  2299. if (err < 0)
  2300. return err;
  2301. chip->tea.v4l2_dev = &chip->v4l2_dev;
  2302. chip->tea.private_data = chip;
  2303. chip->tea.radio_nr = radio_nr;
  2304. chip->tea.ops = &snd_es1968_tea_ops;
  2305. sprintf(chip->tea.bus_info, "PCI:%s", pci_name(pci));
  2306. for (i = 0; i < ARRAY_SIZE(snd_es1968_tea575x_gpios); i++) {
  2307. chip->tea575x_tuner = i;
  2308. if (!snd_tea575x_init(&chip->tea, THIS_MODULE)) {
  2309. dev_info(card->dev, "detected TEA575x radio type %s\n",
  2310. get_tea575x_gpio(chip)->name);
  2311. strscpy(chip->tea.card, get_tea575x_gpio(chip)->name,
  2312. sizeof(chip->tea.card));
  2313. break;
  2314. }
  2315. }
  2316. #endif
  2317. return 0;
  2318. }
  2319. /*
  2320. */
  2321. static int __snd_es1968_probe(struct pci_dev *pci,
  2322. const struct pci_device_id *pci_id)
  2323. {
  2324. static int dev;
  2325. struct snd_card *card;
  2326. struct es1968 *chip;
  2327. unsigned int i;
  2328. int err;
  2329. if (dev >= SNDRV_CARDS)
  2330. return -ENODEV;
  2331. if (!enable[dev]) {
  2332. dev++;
  2333. return -ENOENT;
  2334. }
  2335. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  2336. sizeof(*chip), &card);
  2337. if (err < 0)
  2338. return err;
  2339. chip = card->private_data;
  2340. if (total_bufsize[dev] < 128)
  2341. total_bufsize[dev] = 128;
  2342. if (total_bufsize[dev] > 4096)
  2343. total_bufsize[dev] = 4096;
  2344. err = snd_es1968_create(card, pci,
  2345. total_bufsize[dev] * 1024, /* in bytes */
  2346. pcm_substreams_p[dev],
  2347. pcm_substreams_c[dev],
  2348. pci_id->driver_data,
  2349. use_pm[dev],
  2350. radio_nr[dev]);
  2351. if (err < 0)
  2352. return err;
  2353. switch (chip->type) {
  2354. case TYPE_MAESTRO2E:
  2355. strcpy(card->driver, "ES1978");
  2356. strcpy(card->shortname, "ESS ES1978 (Maestro 2E)");
  2357. break;
  2358. case TYPE_MAESTRO2:
  2359. strcpy(card->driver, "ES1968");
  2360. strcpy(card->shortname, "ESS ES1968 (Maestro 2)");
  2361. break;
  2362. case TYPE_MAESTRO:
  2363. strcpy(card->driver, "ESM1");
  2364. strcpy(card->shortname, "ESS Maestro 1");
  2365. break;
  2366. }
  2367. err = snd_es1968_pcm(chip, 0);
  2368. if (err < 0)
  2369. return err;
  2370. err = snd_es1968_mixer(chip);
  2371. if (err < 0)
  2372. return err;
  2373. if (enable_mpu[dev] == 2) {
  2374. /* check the deny list */
  2375. unsigned short vend;
  2376. pci_read_config_word(chip->pci, PCI_SUBSYSTEM_VENDOR_ID, &vend);
  2377. for (i = 0; i < ARRAY_SIZE(mpu_denylist); i++) {
  2378. if (chip->type == mpu_denylist[i].type &&
  2379. vend == mpu_denylist[i].vendor) {
  2380. enable_mpu[dev] = 0;
  2381. break;
  2382. }
  2383. }
  2384. }
  2385. if (enable_mpu[dev]) {
  2386. err = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  2387. chip->io_port + ESM_MPU401_PORT,
  2388. MPU401_INFO_INTEGRATED |
  2389. MPU401_INFO_IRQ_HOOK,
  2390. -1, &chip->rmidi);
  2391. if (err < 0)
  2392. dev_warn(card->dev, "skipping MPU-401 MIDI support..\n");
  2393. }
  2394. snd_es1968_create_gameport(chip, dev);
  2395. #ifdef CONFIG_SND_ES1968_INPUT
  2396. err = snd_es1968_input_register(chip);
  2397. if (err)
  2398. dev_warn(card->dev,
  2399. "Input device registration failed with error %i", err);
  2400. #endif
  2401. snd_es1968_start_irq(chip);
  2402. chip->clock = clock[dev];
  2403. if (! chip->clock)
  2404. es1968_measure_clock(chip);
  2405. sprintf(card->longname, "%s at 0x%lx, irq %i",
  2406. card->shortname, chip->io_port, chip->irq);
  2407. err = snd_card_register(card);
  2408. if (err < 0)
  2409. return err;
  2410. pci_set_drvdata(pci, card);
  2411. dev++;
  2412. return 0;
  2413. }
  2414. static int snd_es1968_probe(struct pci_dev *pci,
  2415. const struct pci_device_id *pci_id)
  2416. {
  2417. return snd_card_free_on_error(&pci->dev, __snd_es1968_probe(pci, pci_id));
  2418. }
  2419. static struct pci_driver es1968_driver = {
  2420. .name = KBUILD_MODNAME,
  2421. .id_table = snd_es1968_ids,
  2422. .probe = snd_es1968_probe,
  2423. .driver = {
  2424. .pm = ES1968_PM_OPS,
  2425. },
  2426. };
  2427. module_pci_driver(es1968_driver);