ml403-ac97cr.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * ALSA driver for Xilinx ML403 AC97 Controller Reference
  3. * IP: opb_ac97_controller_ref_v1_00_a (EDK 8.1i)
  4. * IP: opb_ac97_controller_ref_v1_00_a (EDK 9.1i)
  5. *
  6. * Copyright (c) by 2007 Joachim Foerster <JOFT@gmx.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /* Some notes / status of this driver:
  24. *
  25. * - Don't wonder about some strange implementations of things - especially the
  26. * (heavy) shadowing of codec registers, with which I tried to reduce read
  27. * accesses to a minimum, because after a variable amount of accesses, the AC97
  28. * controller doesn't raise the register access finished bit anymore ...
  29. *
  30. * - Playback support seems to be pretty stable - no issues here.
  31. * - Capture support "works" now, too. Overruns don't happen any longer so often.
  32. * But there might still be some ...
  33. */
  34. #include <linux/init.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/ioport.h>
  38. #include <linux/io.h>
  39. #include <linux/interrupt.h>
  40. /* HZ */
  41. #include <linux/param.h>
  42. /* jiffies, time_*() */
  43. #include <linux/jiffies.h>
  44. /* schedule_timeout*() */
  45. #include <linux/sched.h>
  46. /* spin_lock*() */
  47. #include <linux/spinlock.h>
  48. /* struct mutex, mutex_init(), mutex_*lock() */
  49. #include <linux/mutex.h>
  50. /* snd_printk(), snd_printd() */
  51. #include <sound/core.h>
  52. #include <sound/pcm.h>
  53. #include <sound/pcm_params.h>
  54. #include <sound/initval.h>
  55. #include <sound/ac97_codec.h>
  56. #include "pcm-indirect2.h"
  57. #define SND_ML403_AC97CR_DRIVER "ml403-ac97cr"
  58. MODULE_AUTHOR("Joachim Foerster <JOFT@gmx.de>");
  59. MODULE_DESCRIPTION("Xilinx ML403 AC97 Controller Reference");
  60. MODULE_LICENSE("GPL");
  61. MODULE_SUPPORTED_DEVICE("{{Xilinx,ML403 AC97 Controller Reference}}");
  62. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  63. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  64. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
  65. module_param_array(index, int, NULL, 0444);
  66. MODULE_PARM_DESC(index, "Index value for ML403 AC97 Controller Reference.");
  67. module_param_array(id, charp, NULL, 0444);
  68. MODULE_PARM_DESC(id, "ID string for ML403 AC97 Controller Reference.");
  69. module_param_array(enable, bool, NULL, 0444);
  70. MODULE_PARM_DESC(enable, "Enable this ML403 AC97 Controller Reference.");
  71. /* Special feature options */
  72. /*#define CODEC_WRITE_CHECK_RAF*/ /* don't return after a write to a codec
  73. * register, while RAF bit is not set
  74. */
  75. /* Debug options for code which may be removed completely in a final version */
  76. #ifdef CONFIG_SND_DEBUG
  77. /*#define CODEC_STAT*/ /* turn on some minimal "statistics"
  78. * about codec register usage
  79. */
  80. #define SND_PCM_INDIRECT2_STAT /* turn on some "statistics" about the
  81. * process of copying bytes from the
  82. * intermediate buffer to the hardware
  83. * fifo and the other way round
  84. */
  85. #endif
  86. /* Definition of a "level/facility dependent" printk(); may be removed
  87. * completely in a final version
  88. */
  89. #undef PDEBUG
  90. #ifdef CONFIG_SND_DEBUG
  91. /* "facilities" for PDEBUG */
  92. #define UNKNOWN (1<<0)
  93. #define CODEC_SUCCESS (1<<1)
  94. #define CODEC_FAKE (1<<2)
  95. #define INIT_INFO (1<<3)
  96. #define INIT_FAILURE (1<<4)
  97. #define WORK_INFO (1<<5)
  98. #define WORK_FAILURE (1<<6)
  99. #define PDEBUG_FACILITIES (UNKNOWN | INIT_FAILURE | WORK_FAILURE)
  100. #define PDEBUG(fac, fmt, args...) do { \
  101. if (fac & PDEBUG_FACILITIES) \
  102. snd_printd(KERN_DEBUG SND_ML403_AC97CR_DRIVER ": " \
  103. fmt, ##args); \
  104. } while (0)
  105. #else
  106. #define PDEBUG(fac, fmt, args...) /* nothing */
  107. #endif
  108. /* Defines for "waits"/timeouts (portions of HZ=250 on arch/ppc by default) */
  109. #define CODEC_TIMEOUT_ON_INIT 5 /* timeout for checking for codec
  110. * readiness (after insmod)
  111. */
  112. #ifndef CODEC_WRITE_CHECK_RAF
  113. #define CODEC_WAIT_AFTER_WRITE 100 /* general, static wait after a write
  114. * access to a codec register, may be
  115. * 0 to completely remove wait
  116. */
  117. #else
  118. #define CODEC_TIMEOUT_AFTER_WRITE 5 /* timeout after a write access to a
  119. * codec register, if RAF bit is used
  120. */
  121. #endif
  122. #define CODEC_TIMEOUT_AFTER_READ 5 /* timeout after a read access to a
  123. * codec register (checking RAF bit)
  124. */
  125. /* Infrastructure for codec register shadowing */
  126. #define LM4550_REG_OK (1<<0) /* register exists */
  127. #define LM4550_REG_DONEREAD (1<<1) /* read register once, value should be
  128. * the same currently in the register
  129. */
  130. #define LM4550_REG_NOSAVE (1<<2) /* values written to this register will
  131. * not be saved in the register
  132. */
  133. #define LM4550_REG_NOSHADOW (1<<3) /* don't do register shadowing, use plain
  134. * hardware access
  135. */
  136. #define LM4550_REG_READONLY (1<<4) /* register is read only */
  137. #define LM4550_REG_FAKEPROBE (1<<5) /* fake write _and_ read actions during
  138. * probe() correctly
  139. */
  140. #define LM4550_REG_FAKEREAD (1<<6) /* fake read access, always return
  141. * default value
  142. */
  143. #define LM4550_REG_ALLFAKE (LM4550_REG_FAKEREAD | LM4550_REG_FAKEPROBE)
  144. struct lm4550_reg {
  145. u16 value;
  146. u16 flag;
  147. u16 wmask;
  148. u16 def;
  149. };
  150. struct lm4550_reg lm4550_regfile[64] = {
  151. [AC97_RESET / 2] = {.flag = LM4550_REG_OK \
  152. | LM4550_REG_NOSAVE \
  153. | LM4550_REG_FAKEREAD,
  154. .def = 0x0D50},
  155. [AC97_MASTER / 2] = {.flag = LM4550_REG_OK
  156. | LM4550_REG_FAKEPROBE,
  157. .wmask = 0x9F1F,
  158. .def = 0x8000},
  159. [AC97_HEADPHONE / 2] = {.flag = LM4550_REG_OK \
  160. | LM4550_REG_FAKEPROBE,
  161. .wmask = 0x9F1F,
  162. .def = 0x8000},
  163. [AC97_MASTER_MONO / 2] = {.flag = LM4550_REG_OK \
  164. | LM4550_REG_FAKEPROBE,
  165. .wmask = 0x801F,
  166. .def = 0x8000},
  167. [AC97_PC_BEEP / 2] = {.flag = LM4550_REG_OK \
  168. | LM4550_REG_FAKEPROBE,
  169. .wmask = 0x801E,
  170. .def = 0x0},
  171. [AC97_PHONE / 2] = {.flag = LM4550_REG_OK \
  172. | LM4550_REG_FAKEPROBE,
  173. .wmask = 0x801F,
  174. .def = 0x8008},
  175. [AC97_MIC / 2] = {.flag = LM4550_REG_OK \
  176. | LM4550_REG_FAKEPROBE,
  177. .wmask = 0x805F,
  178. .def = 0x8008},
  179. [AC97_LINE / 2] = {.flag = LM4550_REG_OK \
  180. | LM4550_REG_FAKEPROBE,
  181. .wmask = 0x9F1F,
  182. .def = 0x8808},
  183. [AC97_CD / 2] = {.flag = LM4550_REG_OK \
  184. | LM4550_REG_FAKEPROBE,
  185. .wmask = 0x9F1F,
  186. .def = 0x8808},
  187. [AC97_VIDEO / 2] = {.flag = LM4550_REG_OK \
  188. | LM4550_REG_FAKEPROBE,
  189. .wmask = 0x9F1F,
  190. .def = 0x8808},
  191. [AC97_AUX / 2] = {.flag = LM4550_REG_OK \
  192. | LM4550_REG_FAKEPROBE,
  193. .wmask = 0x9F1F,
  194. .def = 0x8808},
  195. [AC97_PCM / 2] = {.flag = LM4550_REG_OK \
  196. | LM4550_REG_FAKEPROBE,
  197. .wmask = 0x9F1F,
  198. .def = 0x8008},
  199. [AC97_REC_SEL / 2] = {.flag = LM4550_REG_OK \
  200. | LM4550_REG_FAKEPROBE,
  201. .wmask = 0x707,
  202. .def = 0x0},
  203. [AC97_REC_GAIN / 2] = {.flag = LM4550_REG_OK \
  204. | LM4550_REG_FAKEPROBE,
  205. .wmask = 0x8F0F,
  206. .def = 0x8000},
  207. [AC97_GENERAL_PURPOSE / 2] = {.flag = LM4550_REG_OK \
  208. | LM4550_REG_FAKEPROBE,
  209. .def = 0x0,
  210. .wmask = 0xA380},
  211. [AC97_3D_CONTROL / 2] = {.flag = LM4550_REG_OK \
  212. | LM4550_REG_FAKEREAD \
  213. | LM4550_REG_READONLY,
  214. .def = 0x0101},
  215. [AC97_POWERDOWN / 2] = {.flag = LM4550_REG_OK \
  216. | LM4550_REG_NOSHADOW \
  217. | LM4550_REG_NOSAVE,
  218. .wmask = 0xFF00},
  219. /* may not write ones to
  220. * REF/ANL/DAC/ADC bits
  221. * FIXME: Is this ok?
  222. */
  223. [AC97_EXTENDED_ID / 2] = {.flag = LM4550_REG_OK \
  224. | LM4550_REG_FAKEREAD \
  225. | LM4550_REG_READONLY,
  226. .def = 0x0201}, /* primary codec */
  227. [AC97_EXTENDED_STATUS / 2] = {.flag = LM4550_REG_OK \
  228. | LM4550_REG_NOSHADOW \
  229. | LM4550_REG_NOSAVE,
  230. .wmask = 0x1},
  231. [AC97_PCM_FRONT_DAC_RATE / 2] = {.flag = LM4550_REG_OK \
  232. | LM4550_REG_FAKEPROBE,
  233. .def = 0xBB80,
  234. .wmask = 0xFFFF},
  235. [AC97_PCM_LR_ADC_RATE / 2] = {.flag = LM4550_REG_OK \
  236. | LM4550_REG_FAKEPROBE,
  237. .def = 0xBB80,
  238. .wmask = 0xFFFF},
  239. [AC97_VENDOR_ID1 / 2] = {.flag = LM4550_REG_OK \
  240. | LM4550_REG_READONLY \
  241. | LM4550_REG_FAKEREAD,
  242. .def = 0x4E53},
  243. [AC97_VENDOR_ID2 / 2] = {.flag = LM4550_REG_OK \
  244. | LM4550_REG_READONLY \
  245. | LM4550_REG_FAKEREAD,
  246. .def = 0x4350}
  247. };
  248. #define LM4550_RF_OK(reg) (lm4550_regfile[reg / 2].flag & LM4550_REG_OK)
  249. static void lm4550_regfile_init(void)
  250. {
  251. int i;
  252. for (i = 0; i < 64; i++)
  253. if (lm4550_regfile[i].flag & LM4550_REG_FAKEPROBE)
  254. lm4550_regfile[i].value = lm4550_regfile[i].def;
  255. }
  256. static void lm4550_regfile_write_values_after_init(struct snd_ac97 *ac97)
  257. {
  258. int i;
  259. for (i = 0; i < 64; i++)
  260. if ((lm4550_regfile[i].flag & LM4550_REG_FAKEPROBE) &&
  261. (lm4550_regfile[i].value != lm4550_regfile[i].def)) {
  262. PDEBUG(CODEC_FAKE, "lm4550_regfile_write_values_after_"
  263. "init(): reg=0x%x value=0x%x / %d is different "
  264. "from def=0x%x / %d\n",
  265. i, lm4550_regfile[i].value,
  266. lm4550_regfile[i].value, lm4550_regfile[i].def,
  267. lm4550_regfile[i].def);
  268. snd_ac97_write(ac97, i * 2, lm4550_regfile[i].value);
  269. lm4550_regfile[i].flag |= LM4550_REG_DONEREAD;
  270. }
  271. }
  272. /* direct registers */
  273. #define CR_REG(ml403_ac97cr, x) ((ml403_ac97cr)->port + CR_REG_##x)
  274. #define CR_REG_PLAYFIFO 0x00
  275. #define CR_PLAYDATA(a) ((a) & 0xFFFF)
  276. #define CR_REG_RECFIFO 0x04
  277. #define CR_RECDATA(a) ((a) & 0xFFFF)
  278. #define CR_REG_STATUS 0x08
  279. #define CR_RECOVER (1<<7)
  280. #define CR_PLAYUNDER (1<<6)
  281. #define CR_CODECREADY (1<<5)
  282. #define CR_RAF (1<<4)
  283. #define CR_RECEMPTY (1<<3)
  284. #define CR_RECFULL (1<<2)
  285. #define CR_PLAYHALF (1<<1)
  286. #define CR_PLAYFULL (1<<0)
  287. #define CR_REG_RESETFIFO 0x0C
  288. #define CR_RECRESET (1<<1)
  289. #define CR_PLAYRESET (1<<0)
  290. #define CR_REG_CODEC_ADDR 0x10
  291. /* UG082 says:
  292. * #define CR_CODEC_ADDR(a) ((a) << 1)
  293. * #define CR_CODEC_READ (1<<0)
  294. * #define CR_CODEC_WRITE (0<<0)
  295. */
  296. /* RefDesign example says: */
  297. #define CR_CODEC_ADDR(a) ((a) << 0)
  298. #define CR_CODEC_READ (1<<7)
  299. #define CR_CODEC_WRITE (0<<7)
  300. #define CR_REG_CODEC_DATAREAD 0x14
  301. #define CR_CODEC_DATAREAD(v) ((v) & 0xFFFF)
  302. #define CR_REG_CODEC_DATAWRITE 0x18
  303. #define CR_CODEC_DATAWRITE(v) ((v) & 0xFFFF)
  304. #define CR_FIFO_SIZE 32
  305. struct snd_ml403_ac97cr {
  306. /* lock for access to (controller) registers */
  307. spinlock_t reg_lock;
  308. /* mutex for the whole sequence of accesses to (controller) registers
  309. * which affect codec registers
  310. */
  311. struct mutex cdc_mutex;
  312. int irq; /* for playback */
  313. int enable_irq; /* for playback */
  314. int capture_irq;
  315. int enable_capture_irq;
  316. struct resource *res_port;
  317. void *port;
  318. struct snd_ac97 *ac97;
  319. int ac97_fake;
  320. #ifdef CODEC_STAT
  321. int ac97_read;
  322. int ac97_write;
  323. #endif
  324. struct platform_device *pfdev;
  325. struct snd_card *card;
  326. struct snd_pcm *pcm;
  327. struct snd_pcm_substream *playback_substream;
  328. struct snd_pcm_substream *capture_substream;
  329. struct snd_pcm_indirect2 ind_rec; /* for playback */
  330. struct snd_pcm_indirect2 capture_ind2_rec;
  331. };
  332. static struct snd_pcm_hardware snd_ml403_ac97cr_playback = {
  333. .info = (SNDRV_PCM_INFO_MMAP |
  334. SNDRV_PCM_INFO_INTERLEAVED |
  335. SNDRV_PCM_INFO_MMAP_VALID),
  336. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  337. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  338. SNDRV_PCM_RATE_8000_48000),
  339. .rate_min = 4000,
  340. .rate_max = 48000,
  341. .channels_min = 2,
  342. .channels_max = 2,
  343. .buffer_bytes_max = (128*1024),
  344. .period_bytes_min = CR_FIFO_SIZE/2,
  345. .period_bytes_max = (64*1024),
  346. .periods_min = 2,
  347. .periods_max = (128*1024)/(CR_FIFO_SIZE/2),
  348. .fifo_size = 0,
  349. };
  350. static struct snd_pcm_hardware snd_ml403_ac97cr_capture = {
  351. .info = (SNDRV_PCM_INFO_MMAP |
  352. SNDRV_PCM_INFO_INTERLEAVED |
  353. SNDRV_PCM_INFO_MMAP_VALID),
  354. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  355. .rates = (SNDRV_PCM_RATE_CONTINUOUS |
  356. SNDRV_PCM_RATE_8000_48000),
  357. .rate_min = 4000,
  358. .rate_max = 48000,
  359. .channels_min = 2,
  360. .channels_max = 2,
  361. .buffer_bytes_max = (128*1024),
  362. .period_bytes_min = CR_FIFO_SIZE/2,
  363. .period_bytes_max = (64*1024),
  364. .periods_min = 2,
  365. .periods_max = (128*1024)/(CR_FIFO_SIZE/2),
  366. .fifo_size = 0,
  367. };
  368. static size_t
  369. snd_ml403_ac97cr_playback_ind2_zero(struct snd_pcm_substream *substream,
  370. struct snd_pcm_indirect2 *rec)
  371. {
  372. struct snd_ml403_ac97cr *ml403_ac97cr;
  373. int copied_words = 0;
  374. u32 full = 0;
  375. ml403_ac97cr = snd_pcm_substream_chip(substream);
  376. spin_lock(&ml403_ac97cr->reg_lock);
  377. while ((full = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
  378. CR_PLAYFULL)) != CR_PLAYFULL) {
  379. out_be32(CR_REG(ml403_ac97cr, PLAYFIFO), 0);
  380. copied_words++;
  381. }
  382. rec->hw_ready = 0;
  383. spin_unlock(&ml403_ac97cr->reg_lock);
  384. return (size_t) (copied_words * 2);
  385. }
  386. static size_t
  387. snd_ml403_ac97cr_playback_ind2_copy(struct snd_pcm_substream *substream,
  388. struct snd_pcm_indirect2 *rec,
  389. size_t bytes)
  390. {
  391. struct snd_ml403_ac97cr *ml403_ac97cr;
  392. u16 *src;
  393. int copied_words = 0;
  394. u32 full = 0;
  395. ml403_ac97cr = snd_pcm_substream_chip(substream);
  396. src = (u16 *)(substream->runtime->dma_area + rec->sw_data);
  397. spin_lock(&ml403_ac97cr->reg_lock);
  398. while (((full = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
  399. CR_PLAYFULL)) != CR_PLAYFULL) && (bytes > 1)) {
  400. out_be32(CR_REG(ml403_ac97cr, PLAYFIFO),
  401. CR_PLAYDATA(src[copied_words]));
  402. copied_words++;
  403. bytes = bytes - 2;
  404. }
  405. if (full != CR_PLAYFULL)
  406. rec->hw_ready = 1;
  407. else
  408. rec->hw_ready = 0;
  409. spin_unlock(&ml403_ac97cr->reg_lock);
  410. return (size_t) (copied_words * 2);
  411. }
  412. static size_t
  413. snd_ml403_ac97cr_capture_ind2_null(struct snd_pcm_substream *substream,
  414. struct snd_pcm_indirect2 *rec)
  415. {
  416. struct snd_ml403_ac97cr *ml403_ac97cr;
  417. int copied_words = 0;
  418. u32 empty = 0;
  419. ml403_ac97cr = snd_pcm_substream_chip(substream);
  420. spin_lock(&ml403_ac97cr->reg_lock);
  421. while ((empty = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
  422. CR_RECEMPTY)) != CR_RECEMPTY) {
  423. volatile u32 trash;
  424. trash = CR_RECDATA(in_be32(CR_REG(ml403_ac97cr, RECFIFO)));
  425. /* Hmmmm, really necessary? Don't want call to in_be32()
  426. * to be optimised away!
  427. */
  428. trash++;
  429. copied_words++;
  430. }
  431. rec->hw_ready = 0;
  432. spin_unlock(&ml403_ac97cr->reg_lock);
  433. return (size_t) (copied_words * 2);
  434. }
  435. static size_t
  436. snd_ml403_ac97cr_capture_ind2_copy(struct snd_pcm_substream *substream,
  437. struct snd_pcm_indirect2 *rec, size_t bytes)
  438. {
  439. struct snd_ml403_ac97cr *ml403_ac97cr;
  440. u16 *dst;
  441. int copied_words = 0;
  442. u32 empty = 0;
  443. ml403_ac97cr = snd_pcm_substream_chip(substream);
  444. dst = (u16 *)(substream->runtime->dma_area + rec->sw_data);
  445. spin_lock(&ml403_ac97cr->reg_lock);
  446. while (((empty = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
  447. CR_RECEMPTY)) != CR_RECEMPTY) && (bytes > 1)) {
  448. dst[copied_words] = CR_RECDATA(in_be32(CR_REG(ml403_ac97cr,
  449. RECFIFO)));
  450. copied_words++;
  451. bytes = bytes - 2;
  452. }
  453. if (empty != CR_RECEMPTY)
  454. rec->hw_ready = 1;
  455. else
  456. rec->hw_ready = 0;
  457. spin_unlock(&ml403_ac97cr->reg_lock);
  458. return (size_t) (copied_words * 2);
  459. }
  460. static snd_pcm_uframes_t
  461. snd_ml403_ac97cr_pcm_pointer(struct snd_pcm_substream *substream)
  462. {
  463. struct snd_ml403_ac97cr *ml403_ac97cr;
  464. struct snd_pcm_indirect2 *ind2_rec = NULL;
  465. ml403_ac97cr = snd_pcm_substream_chip(substream);
  466. if (substream == ml403_ac97cr->playback_substream)
  467. ind2_rec = &ml403_ac97cr->ind_rec;
  468. if (substream == ml403_ac97cr->capture_substream)
  469. ind2_rec = &ml403_ac97cr->capture_ind2_rec;
  470. if (ind2_rec != NULL)
  471. return snd_pcm_indirect2_pointer(substream, ind2_rec);
  472. return (snd_pcm_uframes_t) 0;
  473. }
  474. static int
  475. snd_ml403_ac97cr_pcm_playback_trigger(struct snd_pcm_substream *substream,
  476. int cmd)
  477. {
  478. struct snd_ml403_ac97cr *ml403_ac97cr;
  479. int err = 0;
  480. ml403_ac97cr = snd_pcm_substream_chip(substream);
  481. switch (cmd) {
  482. case SNDRV_PCM_TRIGGER_START:
  483. PDEBUG(WORK_INFO, "trigger(playback): START\n");
  484. ml403_ac97cr->ind_rec.hw_ready = 1;
  485. /* clear play FIFO */
  486. out_be32(CR_REG(ml403_ac97cr, RESETFIFO), CR_PLAYRESET);
  487. /* enable play irq */
  488. ml403_ac97cr->enable_irq = 1;
  489. enable_irq(ml403_ac97cr->irq);
  490. break;
  491. case SNDRV_PCM_TRIGGER_STOP:
  492. PDEBUG(WORK_INFO, "trigger(playback): STOP\n");
  493. ml403_ac97cr->ind_rec.hw_ready = 0;
  494. #ifdef SND_PCM_INDIRECT2_STAT
  495. snd_pcm_indirect2_stat(substream, &ml403_ac97cr->ind_rec);
  496. #endif
  497. /* disable play irq */
  498. disable_irq_nosync(ml403_ac97cr->irq);
  499. ml403_ac97cr->enable_irq = 0;
  500. break;
  501. default:
  502. err = -EINVAL;
  503. break;
  504. }
  505. PDEBUG(WORK_INFO, "trigger(playback): (done)\n");
  506. return err;
  507. }
  508. static int
  509. snd_ml403_ac97cr_pcm_capture_trigger(struct snd_pcm_substream *substream,
  510. int cmd)
  511. {
  512. struct snd_ml403_ac97cr *ml403_ac97cr;
  513. int err = 0;
  514. ml403_ac97cr = snd_pcm_substream_chip(substream);
  515. switch (cmd) {
  516. case SNDRV_PCM_TRIGGER_START:
  517. PDEBUG(WORK_INFO, "trigger(capture): START\n");
  518. ml403_ac97cr->capture_ind2_rec.hw_ready = 0;
  519. /* clear record FIFO */
  520. out_be32(CR_REG(ml403_ac97cr, RESETFIFO), CR_RECRESET);
  521. /* enable record irq */
  522. ml403_ac97cr->enable_capture_irq = 1;
  523. enable_irq(ml403_ac97cr->capture_irq);
  524. break;
  525. case SNDRV_PCM_TRIGGER_STOP:
  526. PDEBUG(WORK_INFO, "trigger(capture): STOP\n");
  527. ml403_ac97cr->capture_ind2_rec.hw_ready = 0;
  528. #ifdef SND_PCM_INDIRECT2_STAT
  529. snd_pcm_indirect2_stat(substream,
  530. &ml403_ac97cr->capture_ind2_rec);
  531. #endif
  532. /* disable capture irq */
  533. disable_irq_nosync(ml403_ac97cr->capture_irq);
  534. ml403_ac97cr->enable_capture_irq = 0;
  535. break;
  536. default:
  537. err = -EINVAL;
  538. break;
  539. }
  540. PDEBUG(WORK_INFO, "trigger(capture): (done)\n");
  541. return err;
  542. }
  543. static int
  544. snd_ml403_ac97cr_pcm_playback_prepare(struct snd_pcm_substream *substream)
  545. {
  546. struct snd_ml403_ac97cr *ml403_ac97cr;
  547. struct snd_pcm_runtime *runtime;
  548. ml403_ac97cr = snd_pcm_substream_chip(substream);
  549. runtime = substream->runtime;
  550. PDEBUG(WORK_INFO,
  551. "prepare(): period_bytes=%d, minperiod_bytes=%d\n",
  552. snd_pcm_lib_period_bytes(substream), CR_FIFO_SIZE / 2);
  553. /* set sampling rate */
  554. snd_ac97_set_rate(ml403_ac97cr->ac97, AC97_PCM_FRONT_DAC_RATE,
  555. runtime->rate);
  556. PDEBUG(WORK_INFO, "prepare(): rate=%d\n", runtime->rate);
  557. /* init struct for intermediate buffer */
  558. memset(&ml403_ac97cr->ind_rec, 0,
  559. sizeof(struct snd_pcm_indirect2));
  560. ml403_ac97cr->ind_rec.hw_buffer_size = CR_FIFO_SIZE;
  561. ml403_ac97cr->ind_rec.sw_buffer_size =
  562. snd_pcm_lib_buffer_bytes(substream);
  563. ml403_ac97cr->ind_rec.min_periods = -1;
  564. ml403_ac97cr->ind_rec.min_multiple =
  565. snd_pcm_lib_period_bytes(substream) / (CR_FIFO_SIZE / 2);
  566. PDEBUG(WORK_INFO, "prepare(): hw_buffer_size=%d, "
  567. "sw_buffer_size=%d, min_multiple=%d\n",
  568. CR_FIFO_SIZE, ml403_ac97cr->ind_rec.sw_buffer_size,
  569. ml403_ac97cr->ind_rec.min_multiple);
  570. return 0;
  571. }
  572. static int
  573. snd_ml403_ac97cr_pcm_capture_prepare(struct snd_pcm_substream *substream)
  574. {
  575. struct snd_ml403_ac97cr *ml403_ac97cr;
  576. struct snd_pcm_runtime *runtime;
  577. ml403_ac97cr = snd_pcm_substream_chip(substream);
  578. runtime = substream->runtime;
  579. PDEBUG(WORK_INFO,
  580. "prepare(capture): period_bytes=%d, minperiod_bytes=%d\n",
  581. snd_pcm_lib_period_bytes(substream), CR_FIFO_SIZE / 2);
  582. /* set sampling rate */
  583. snd_ac97_set_rate(ml403_ac97cr->ac97, AC97_PCM_LR_ADC_RATE,
  584. runtime->rate);
  585. PDEBUG(WORK_INFO, "prepare(capture): rate=%d\n", runtime->rate);
  586. /* init struct for intermediate buffer */
  587. memset(&ml403_ac97cr->capture_ind2_rec, 0,
  588. sizeof(struct snd_pcm_indirect2));
  589. ml403_ac97cr->capture_ind2_rec.hw_buffer_size = CR_FIFO_SIZE;
  590. ml403_ac97cr->capture_ind2_rec.sw_buffer_size =
  591. snd_pcm_lib_buffer_bytes(substream);
  592. ml403_ac97cr->capture_ind2_rec.min_multiple =
  593. snd_pcm_lib_period_bytes(substream) / (CR_FIFO_SIZE / 2);
  594. PDEBUG(WORK_INFO, "prepare(capture): hw_buffer_size=%d, "
  595. "sw_buffer_size=%d, min_multiple=%d\n", CR_FIFO_SIZE,
  596. ml403_ac97cr->capture_ind2_rec.sw_buffer_size,
  597. ml403_ac97cr->capture_ind2_rec.min_multiple);
  598. return 0;
  599. }
  600. static int snd_ml403_ac97cr_hw_free(struct snd_pcm_substream *substream)
  601. {
  602. PDEBUG(WORK_INFO, "hw_free()\n");
  603. return snd_pcm_lib_free_pages(substream);
  604. }
  605. static int
  606. snd_ml403_ac97cr_hw_params(struct snd_pcm_substream *substream,
  607. struct snd_pcm_hw_params *hw_params)
  608. {
  609. PDEBUG(WORK_INFO, "hw_params(): desired buffer bytes=%d, desired "
  610. "period bytes=%d\n",
  611. params_buffer_bytes(hw_params), params_period_bytes(hw_params));
  612. return snd_pcm_lib_malloc_pages(substream,
  613. params_buffer_bytes(hw_params));
  614. }
  615. static int snd_ml403_ac97cr_playback_open(struct snd_pcm_substream *substream)
  616. {
  617. struct snd_ml403_ac97cr *ml403_ac97cr;
  618. struct snd_pcm_runtime *runtime;
  619. ml403_ac97cr = snd_pcm_substream_chip(substream);
  620. runtime = substream->runtime;
  621. PDEBUG(WORK_INFO, "open(playback)\n");
  622. ml403_ac97cr->playback_substream = substream;
  623. runtime->hw = snd_ml403_ac97cr_playback;
  624. snd_pcm_hw_constraint_step(runtime, 0,
  625. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  626. CR_FIFO_SIZE / 2);
  627. return 0;
  628. }
  629. static int snd_ml403_ac97cr_capture_open(struct snd_pcm_substream *substream)
  630. {
  631. struct snd_ml403_ac97cr *ml403_ac97cr;
  632. struct snd_pcm_runtime *runtime;
  633. ml403_ac97cr = snd_pcm_substream_chip(substream);
  634. runtime = substream->runtime;
  635. PDEBUG(WORK_INFO, "open(capture)\n");
  636. ml403_ac97cr->capture_substream = substream;
  637. runtime->hw = snd_ml403_ac97cr_capture;
  638. snd_pcm_hw_constraint_step(runtime, 0,
  639. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  640. CR_FIFO_SIZE / 2);
  641. return 0;
  642. }
  643. static int snd_ml403_ac97cr_playback_close(struct snd_pcm_substream *substream)
  644. {
  645. struct snd_ml403_ac97cr *ml403_ac97cr;
  646. ml403_ac97cr = snd_pcm_substream_chip(substream);
  647. PDEBUG(WORK_INFO, "close(playback)\n");
  648. ml403_ac97cr->playback_substream = NULL;
  649. return 0;
  650. }
  651. static int snd_ml403_ac97cr_capture_close(struct snd_pcm_substream *substream)
  652. {
  653. struct snd_ml403_ac97cr *ml403_ac97cr;
  654. ml403_ac97cr = snd_pcm_substream_chip(substream);
  655. PDEBUG(WORK_INFO, "close(capture)\n");
  656. ml403_ac97cr->capture_substream = NULL;
  657. return 0;
  658. }
  659. static struct snd_pcm_ops snd_ml403_ac97cr_playback_ops = {
  660. .open = snd_ml403_ac97cr_playback_open,
  661. .close = snd_ml403_ac97cr_playback_close,
  662. .ioctl = snd_pcm_lib_ioctl,
  663. .hw_params = snd_ml403_ac97cr_hw_params,
  664. .hw_free = snd_ml403_ac97cr_hw_free,
  665. .prepare = snd_ml403_ac97cr_pcm_playback_prepare,
  666. .trigger = snd_ml403_ac97cr_pcm_playback_trigger,
  667. .pointer = snd_ml403_ac97cr_pcm_pointer,
  668. };
  669. static struct snd_pcm_ops snd_ml403_ac97cr_capture_ops = {
  670. .open = snd_ml403_ac97cr_capture_open,
  671. .close = snd_ml403_ac97cr_capture_close,
  672. .ioctl = snd_pcm_lib_ioctl,
  673. .hw_params = snd_ml403_ac97cr_hw_params,
  674. .hw_free = snd_ml403_ac97cr_hw_free,
  675. .prepare = snd_ml403_ac97cr_pcm_capture_prepare,
  676. .trigger = snd_ml403_ac97cr_pcm_capture_trigger,
  677. .pointer = snd_ml403_ac97cr_pcm_pointer,
  678. };
  679. static irqreturn_t snd_ml403_ac97cr_irq(int irq, void *dev_id)
  680. {
  681. struct snd_ml403_ac97cr *ml403_ac97cr;
  682. struct platform_device *pfdev;
  683. int cmp_irq;
  684. ml403_ac97cr = (struct snd_ml403_ac97cr *)dev_id;
  685. if (ml403_ac97cr == NULL)
  686. return IRQ_NONE;
  687. pfdev = ml403_ac97cr->pfdev;
  688. /* playback interrupt */
  689. cmp_irq = platform_get_irq(pfdev, 0);
  690. if (irq == cmp_irq) {
  691. if (ml403_ac97cr->enable_irq)
  692. snd_pcm_indirect2_playback_interrupt(
  693. ml403_ac97cr->playback_substream,
  694. &ml403_ac97cr->ind_rec,
  695. snd_ml403_ac97cr_playback_ind2_copy,
  696. snd_ml403_ac97cr_playback_ind2_zero);
  697. else
  698. goto __disable_irq;
  699. } else {
  700. /* record interrupt */
  701. cmp_irq = platform_get_irq(pfdev, 1);
  702. if (irq == cmp_irq) {
  703. if (ml403_ac97cr->enable_capture_irq)
  704. snd_pcm_indirect2_capture_interrupt(
  705. ml403_ac97cr->capture_substream,
  706. &ml403_ac97cr->capture_ind2_rec,
  707. snd_ml403_ac97cr_capture_ind2_copy,
  708. snd_ml403_ac97cr_capture_ind2_null);
  709. else
  710. goto __disable_irq;
  711. } else
  712. return IRQ_NONE;
  713. }
  714. return IRQ_HANDLED;
  715. __disable_irq:
  716. PDEBUG(INIT_INFO, "irq(): irq %d is meant to be disabled! So, now try "
  717. "to disable it _really_!\n", irq);
  718. disable_irq_nosync(irq);
  719. return IRQ_HANDLED;
  720. }
  721. static unsigned short
  722. snd_ml403_ac97cr_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  723. {
  724. struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
  725. #ifdef CODEC_STAT
  726. u32 stat;
  727. u32 rafaccess = 0;
  728. #endif
  729. unsigned long end_time;
  730. u16 value = 0;
  731. if (!LM4550_RF_OK(reg)) {
  732. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  733. "access to unknown/unused codec register 0x%x "
  734. "ignored!\n", reg);
  735. return 0;
  736. }
  737. /* check if we can fake/answer this access from our shadow register */
  738. if ((lm4550_regfile[reg / 2].flag &
  739. (LM4550_REG_DONEREAD | LM4550_REG_ALLFAKE)) &&
  740. !(lm4550_regfile[reg / 2].flag & LM4550_REG_NOSHADOW)) {
  741. if (lm4550_regfile[reg / 2].flag & LM4550_REG_FAKEREAD) {
  742. PDEBUG(CODEC_FAKE, "codec_read(): faking read from "
  743. "reg=0x%x, val=0x%x / %d\n",
  744. reg, lm4550_regfile[reg / 2].def,
  745. lm4550_regfile[reg / 2].def);
  746. return lm4550_regfile[reg / 2].def;
  747. } else if ((lm4550_regfile[reg / 2].flag &
  748. LM4550_REG_FAKEPROBE) &&
  749. ml403_ac97cr->ac97_fake) {
  750. PDEBUG(CODEC_FAKE, "codec_read(): faking read from "
  751. "reg=0x%x, val=0x%x / %d (probe)\n",
  752. reg, lm4550_regfile[reg / 2].value,
  753. lm4550_regfile[reg / 2].value);
  754. return lm4550_regfile[reg / 2].value;
  755. } else {
  756. #ifdef CODEC_STAT
  757. PDEBUG(CODEC_FAKE, "codec_read(): read access "
  758. "answered by shadow register 0x%x (value=0x%x "
  759. "/ %d) (cw=%d cr=%d)\n",
  760. reg, lm4550_regfile[reg / 2].value,
  761. lm4550_regfile[reg / 2].value,
  762. ml403_ac97cr->ac97_write,
  763. ml403_ac97cr->ac97_read);
  764. #else
  765. PDEBUG(CODEC_FAKE, "codec_read(): read access "
  766. "answered by shadow register 0x%x (value=0x%x "
  767. "/ %d)\n",
  768. reg, lm4550_regfile[reg / 2].value,
  769. lm4550_regfile[reg / 2].value);
  770. #endif
  771. return lm4550_regfile[reg / 2].value;
  772. }
  773. }
  774. /* if we are here, we _have_ to access the codec really, no faking */
  775. if (mutex_lock_interruptible(&ml403_ac97cr->cdc_mutex) != 0)
  776. return 0;
  777. #ifdef CODEC_STAT
  778. ml403_ac97cr->ac97_read++;
  779. #endif
  780. spin_lock(&ml403_ac97cr->reg_lock);
  781. out_be32(CR_REG(ml403_ac97cr, CODEC_ADDR),
  782. CR_CODEC_ADDR(reg) | CR_CODEC_READ);
  783. spin_unlock(&ml403_ac97cr->reg_lock);
  784. end_time = jiffies + (HZ / CODEC_TIMEOUT_AFTER_READ);
  785. do {
  786. spin_lock(&ml403_ac97cr->reg_lock);
  787. #ifdef CODEC_STAT
  788. rafaccess++;
  789. stat = in_be32(CR_REG(ml403_ac97cr, STATUS));
  790. if ((stat & CR_RAF) == CR_RAF) {
  791. value = CR_CODEC_DATAREAD(
  792. in_be32(CR_REG(ml403_ac97cr, CODEC_DATAREAD)));
  793. PDEBUG(CODEC_SUCCESS, "codec_read(): (done) reg=0x%x, "
  794. "value=0x%x / %d (STATUS=0x%x)\n",
  795. reg, value, value, stat);
  796. #else
  797. if ((in_be32(CR_REG(ml403_ac97cr, STATUS)) &
  798. CR_RAF) == CR_RAF) {
  799. value = CR_CODEC_DATAREAD(
  800. in_be32(CR_REG(ml403_ac97cr, CODEC_DATAREAD)));
  801. PDEBUG(CODEC_SUCCESS, "codec_read(): (done) "
  802. "reg=0x%x, value=0x%x / %d\n",
  803. reg, value, value);
  804. #endif
  805. lm4550_regfile[reg / 2].value = value;
  806. lm4550_regfile[reg / 2].flag |= LM4550_REG_DONEREAD;
  807. spin_unlock(&ml403_ac97cr->reg_lock);
  808. mutex_unlock(&ml403_ac97cr->cdc_mutex);
  809. return value;
  810. }
  811. spin_unlock(&ml403_ac97cr->reg_lock);
  812. schedule_timeout_uninterruptible(1);
  813. } while (time_after(end_time, jiffies));
  814. /* read the DATAREAD register anyway, see comment below */
  815. spin_lock(&ml403_ac97cr->reg_lock);
  816. value =
  817. CR_CODEC_DATAREAD(in_be32(CR_REG(ml403_ac97cr, CODEC_DATAREAD)));
  818. spin_unlock(&ml403_ac97cr->reg_lock);
  819. #ifdef CODEC_STAT
  820. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  821. "timeout while codec read! "
  822. "(reg=0x%x, last STATUS=0x%x, DATAREAD=0x%x / %d, %d) "
  823. "(cw=%d, cr=%d)\n",
  824. reg, stat, value, value, rafaccess,
  825. ml403_ac97cr->ac97_write, ml403_ac97cr->ac97_read);
  826. #else
  827. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  828. "timeout while codec read! "
  829. "(reg=0x%x, DATAREAD=0x%x / %d)\n",
  830. reg, value, value);
  831. #endif
  832. /* BUG: This is PURE speculation! But after _most_ read timeouts the
  833. * value in the register is ok!
  834. */
  835. lm4550_regfile[reg / 2].value = value;
  836. lm4550_regfile[reg / 2].flag |= LM4550_REG_DONEREAD;
  837. mutex_unlock(&ml403_ac97cr->cdc_mutex);
  838. return value;
  839. }
  840. static void
  841. snd_ml403_ac97cr_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  842. unsigned short val)
  843. {
  844. struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
  845. #ifdef CODEC_STAT
  846. u32 stat;
  847. u32 rafaccess = 0;
  848. #endif
  849. #ifdef CODEC_WRITE_CHECK_RAF
  850. unsigned long end_time;
  851. #endif
  852. if (!LM4550_RF_OK(reg)) {
  853. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  854. "access to unknown/unused codec register 0x%x "
  855. "ignored!\n", reg);
  856. return;
  857. }
  858. if (lm4550_regfile[reg / 2].flag & LM4550_REG_READONLY) {
  859. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  860. "write access to read only codec register 0x%x "
  861. "ignored!\n", reg);
  862. return;
  863. }
  864. if ((val & lm4550_regfile[reg / 2].wmask) != val) {
  865. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  866. "write access to codec register 0x%x "
  867. "with bad value 0x%x / %d!\n",
  868. reg, val, val);
  869. val = val & lm4550_regfile[reg / 2].wmask;
  870. }
  871. if (((lm4550_regfile[reg / 2].flag & LM4550_REG_FAKEPROBE) &&
  872. ml403_ac97cr->ac97_fake) &&
  873. !(lm4550_regfile[reg / 2].flag & LM4550_REG_NOSHADOW)) {
  874. PDEBUG(CODEC_FAKE, "codec_write(): faking write to reg=0x%x, "
  875. "val=0x%x / %d\n", reg, val, val);
  876. lm4550_regfile[reg / 2].value = (val &
  877. lm4550_regfile[reg / 2].wmask);
  878. return;
  879. }
  880. if (mutex_lock_interruptible(&ml403_ac97cr->cdc_mutex) != 0)
  881. return;
  882. #ifdef CODEC_STAT
  883. ml403_ac97cr->ac97_write++;
  884. #endif
  885. spin_lock(&ml403_ac97cr->reg_lock);
  886. out_be32(CR_REG(ml403_ac97cr, CODEC_DATAWRITE),
  887. CR_CODEC_DATAWRITE(val));
  888. out_be32(CR_REG(ml403_ac97cr, CODEC_ADDR),
  889. CR_CODEC_ADDR(reg) | CR_CODEC_WRITE);
  890. spin_unlock(&ml403_ac97cr->reg_lock);
  891. #ifdef CODEC_WRITE_CHECK_RAF
  892. /* check CR_CODEC_RAF bit to see if write access to register is done;
  893. * loop until bit is set or timeout happens
  894. */
  895. end_time = jiffies + HZ / CODEC_TIMEOUT_AFTER_WRITE;
  896. do {
  897. spin_lock(&ml403_ac97cr->reg_lock);
  898. #ifdef CODEC_STAT
  899. rafaccess++;
  900. stat = in_be32(CR_REG(ml403_ac97cr, STATUS))
  901. if ((stat & CR_RAF) == CR_RAF) {
  902. #else
  903. if ((in_be32(CR_REG(ml403_ac97cr, STATUS)) &
  904. CR_RAF) == CR_RAF) {
  905. #endif
  906. PDEBUG(CODEC_SUCCESS, "codec_write(): (done) "
  907. "reg=0x%x, value=%d / 0x%x\n",
  908. reg, val, val);
  909. if (!(lm4550_regfile[reg / 2].flag &
  910. LM4550_REG_NOSHADOW) &&
  911. !(lm4550_regfile[reg / 2].flag &
  912. LM4550_REG_NOSAVE))
  913. lm4550_regfile[reg / 2].value = val;
  914. lm4550_regfile[reg / 2].flag |= LM4550_REG_DONEREAD;
  915. spin_unlock(&ml403_ac97cr->reg_lock);
  916. mutex_unlock(&ml403_ac97cr->cdc_mutex);
  917. return;
  918. }
  919. spin_unlock(&ml403_ac97cr->reg_lock);
  920. schedule_timeout_uninterruptible(1);
  921. } while (time_after(end_time, jiffies));
  922. #ifdef CODEC_STAT
  923. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  924. "timeout while codec write "
  925. "(reg=0x%x, val=0x%x / %d, last STATUS=0x%x, %d) "
  926. "(cw=%d, cr=%d)\n",
  927. reg, val, val, stat, rafaccess, ml403_ac97cr->ac97_write,
  928. ml403_ac97cr->ac97_read);
  929. #else
  930. snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
  931. "timeout while codec write (reg=0x%x, val=0x%x / %d)\n",
  932. reg, val, val);
  933. #endif
  934. #else /* CODEC_WRITE_CHECK_RAF */
  935. #if CODEC_WAIT_AFTER_WRITE > 0
  936. /* officially, in AC97 spec there is no possibility for a AC97
  937. * controller to determine, if write access is done or not - so: How
  938. * is Xilinx able to provide a RAF bit for write access?
  939. * => very strange, thus just don't check RAF bit (compare with
  940. * Xilinx's example app in EDK 8.1i) and wait
  941. */
  942. schedule_timeout_uninterruptible(HZ / CODEC_WAIT_AFTER_WRITE);
  943. #endif
  944. PDEBUG(CODEC_SUCCESS, "codec_write(): (done) "
  945. "reg=0x%x, value=%d / 0x%x (no RAF check)\n",
  946. reg, val, val);
  947. #endif
  948. mutex_unlock(&ml403_ac97cr->cdc_mutex);
  949. return;
  950. }
  951. static int __devinit
  952. snd_ml403_ac97cr_chip_init(struct snd_ml403_ac97cr *ml403_ac97cr)
  953. {
  954. unsigned long end_time;
  955. PDEBUG(INIT_INFO, "chip_init():\n");
  956. end_time = jiffies + HZ / CODEC_TIMEOUT_ON_INIT;
  957. do {
  958. if (in_be32(CR_REG(ml403_ac97cr, STATUS)) & CR_CODECREADY) {
  959. /* clear both hardware FIFOs */
  960. out_be32(CR_REG(ml403_ac97cr, RESETFIFO),
  961. CR_RECRESET | CR_PLAYRESET);
  962. PDEBUG(INIT_INFO, "chip_init(): (done)\n");
  963. return 0;
  964. }
  965. schedule_timeout_uninterruptible(1);
  966. } while (time_after(end_time, jiffies));
  967. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  968. "timeout while waiting for codec, "
  969. "not ready!\n");
  970. return -EBUSY;
  971. }
  972. static int snd_ml403_ac97cr_free(struct snd_ml403_ac97cr *ml403_ac97cr)
  973. {
  974. PDEBUG(INIT_INFO, "free():\n");
  975. /* irq release */
  976. if (ml403_ac97cr->irq >= 0)
  977. free_irq(ml403_ac97cr->irq, ml403_ac97cr);
  978. if (ml403_ac97cr->capture_irq >= 0)
  979. free_irq(ml403_ac97cr->capture_irq, ml403_ac97cr);
  980. /* give back "port" */
  981. if (ml403_ac97cr->port != NULL)
  982. iounmap(ml403_ac97cr->port);
  983. kfree(ml403_ac97cr);
  984. PDEBUG(INIT_INFO, "free(): (done)\n");
  985. return 0;
  986. }
  987. static int snd_ml403_ac97cr_dev_free(struct snd_device *snddev)
  988. {
  989. struct snd_ml403_ac97cr *ml403_ac97cr = snddev->device_data;
  990. PDEBUG(INIT_INFO, "dev_free():\n");
  991. return snd_ml403_ac97cr_free(ml403_ac97cr);
  992. }
  993. static int __devinit
  994. snd_ml403_ac97cr_create(struct snd_card *card, struct platform_device *pfdev,
  995. struct snd_ml403_ac97cr **rml403_ac97cr)
  996. {
  997. struct snd_ml403_ac97cr *ml403_ac97cr;
  998. int err;
  999. static struct snd_device_ops ops = {
  1000. .dev_free = snd_ml403_ac97cr_dev_free,
  1001. };
  1002. struct resource *resource;
  1003. int irq;
  1004. *rml403_ac97cr = NULL;
  1005. ml403_ac97cr = kzalloc(sizeof(*ml403_ac97cr), GFP_KERNEL);
  1006. if (ml403_ac97cr == NULL)
  1007. return -ENOMEM;
  1008. spin_lock_init(&ml403_ac97cr->reg_lock);
  1009. mutex_init(&ml403_ac97cr->cdc_mutex);
  1010. ml403_ac97cr->card = card;
  1011. ml403_ac97cr->pfdev = pfdev;
  1012. ml403_ac97cr->irq = -1;
  1013. ml403_ac97cr->enable_irq = 0;
  1014. ml403_ac97cr->capture_irq = -1;
  1015. ml403_ac97cr->enable_capture_irq = 0;
  1016. ml403_ac97cr->port = NULL;
  1017. ml403_ac97cr->res_port = NULL;
  1018. PDEBUG(INIT_INFO, "Trying to reserve resources now ...\n");
  1019. resource = platform_get_resource(pfdev, IORESOURCE_MEM, 0);
  1020. /* get "port" */
  1021. ml403_ac97cr->port = ioremap_nocache(resource->start,
  1022. (resource->end) -
  1023. (resource->start) + 1);
  1024. if (ml403_ac97cr->port == NULL) {
  1025. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  1026. "unable to remap memory region (%x to %x)\n",
  1027. resource->start, resource->end);
  1028. snd_ml403_ac97cr_free(ml403_ac97cr);
  1029. return -EBUSY;
  1030. }
  1031. snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
  1032. "remap controller memory region to "
  1033. "0x%x done\n", (unsigned int)ml403_ac97cr->port);
  1034. /* get irq */
  1035. irq = platform_get_irq(pfdev, 0);
  1036. if (request_irq(irq, snd_ml403_ac97cr_irq, IRQF_DISABLED,
  1037. pfdev->dev.bus_id, (void *)ml403_ac97cr)) {
  1038. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  1039. "unable to grab IRQ %d\n",
  1040. irq);
  1041. snd_ml403_ac97cr_free(ml403_ac97cr);
  1042. return -EBUSY;
  1043. }
  1044. ml403_ac97cr->irq = irq;
  1045. snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
  1046. "request (playback) irq %d done\n",
  1047. ml403_ac97cr->irq);
  1048. irq = platform_get_irq(pfdev, 1);
  1049. if (request_irq(irq, snd_ml403_ac97cr_irq, IRQF_DISABLED,
  1050. pfdev->dev.bus_id, (void *)ml403_ac97cr)) {
  1051. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  1052. "unable to grab IRQ %d\n",
  1053. irq);
  1054. snd_ml403_ac97cr_free(ml403_ac97cr);
  1055. return -EBUSY;
  1056. }
  1057. ml403_ac97cr->capture_irq = irq;
  1058. snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
  1059. "request (capture) irq %d done\n",
  1060. ml403_ac97cr->capture_irq);
  1061. err = snd_ml403_ac97cr_chip_init(ml403_ac97cr);
  1062. if (err < 0) {
  1063. snd_ml403_ac97cr_free(ml403_ac97cr);
  1064. return err;
  1065. }
  1066. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ml403_ac97cr, &ops);
  1067. if (err < 0) {
  1068. PDEBUG(INIT_FAILURE, "probe(): snd_device_new() failed!\n");
  1069. snd_ml403_ac97cr_free(ml403_ac97cr);
  1070. return err;
  1071. }
  1072. *rml403_ac97cr = ml403_ac97cr;
  1073. return 0;
  1074. }
  1075. static void snd_ml403_ac97cr_mixer_free(struct snd_ac97 *ac97)
  1076. {
  1077. struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
  1078. PDEBUG(INIT_INFO, "mixer_free():\n");
  1079. ml403_ac97cr->ac97 = NULL;
  1080. PDEBUG(INIT_INFO, "mixer_free(): (done)\n");
  1081. }
  1082. static int __devinit
  1083. snd_ml403_ac97cr_mixer(struct snd_ml403_ac97cr *ml403_ac97cr)
  1084. {
  1085. struct snd_ac97_bus *bus;
  1086. struct snd_ac97_template ac97;
  1087. int err;
  1088. static struct snd_ac97_bus_ops ops = {
  1089. .write = snd_ml403_ac97cr_codec_write,
  1090. .read = snd_ml403_ac97cr_codec_read,
  1091. };
  1092. PDEBUG(INIT_INFO, "mixer():\n");
  1093. err = snd_ac97_bus(ml403_ac97cr->card, 0, &ops, NULL, &bus);
  1094. if (err < 0)
  1095. return err;
  1096. memset(&ac97, 0, sizeof(ac97));
  1097. ml403_ac97cr->ac97_fake = 1;
  1098. lm4550_regfile_init();
  1099. #ifdef CODEC_STAT
  1100. ml403_ac97cr->ac97_read = 0;
  1101. ml403_ac97cr->ac97_write = 0;
  1102. #endif
  1103. ac97.private_data = ml403_ac97cr;
  1104. ac97.private_free = snd_ml403_ac97cr_mixer_free;
  1105. ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM |
  1106. AC97_SCAP_NO_SPDIF;
  1107. err = snd_ac97_mixer(bus, &ac97, &ml403_ac97cr->ac97);
  1108. ml403_ac97cr->ac97_fake = 0;
  1109. lm4550_regfile_write_values_after_init(ml403_ac97cr->ac97);
  1110. PDEBUG(INIT_INFO, "mixer(): (done) snd_ac97_mixer()=%d\n", err);
  1111. return err;
  1112. }
  1113. static int __devinit
  1114. snd_ml403_ac97cr_pcm(struct snd_ml403_ac97cr *ml403_ac97cr, int device,
  1115. struct snd_pcm **rpcm)
  1116. {
  1117. struct snd_pcm *pcm;
  1118. int err;
  1119. if (rpcm)
  1120. *rpcm = NULL;
  1121. err = snd_pcm_new(ml403_ac97cr->card, "ML403AC97CR/1", device, 1, 1,
  1122. &pcm);
  1123. if (err < 0)
  1124. return err;
  1125. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1126. &snd_ml403_ac97cr_playback_ops);
  1127. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1128. &snd_ml403_ac97cr_capture_ops);
  1129. pcm->private_data = ml403_ac97cr;
  1130. pcm->info_flags = 0;
  1131. strcpy(pcm->name, "ML403AC97CR DAC/ADC");
  1132. ml403_ac97cr->pcm = pcm;
  1133. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1134. snd_dma_continuous_data(GFP_KERNEL),
  1135. 64 * 1024,
  1136. 128 * 1024);
  1137. if (rpcm)
  1138. *rpcm = pcm;
  1139. return 0;
  1140. }
  1141. static int __devinit snd_ml403_ac97cr_probe(struct platform_device *pfdev)
  1142. {
  1143. struct snd_card *card;
  1144. struct snd_ml403_ac97cr *ml403_ac97cr = NULL;
  1145. int err;
  1146. int dev = pfdev->id;
  1147. if (dev >= SNDRV_CARDS)
  1148. return -ENODEV;
  1149. if (!enable[dev])
  1150. return -ENOENT;
  1151. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  1152. if (card == NULL)
  1153. return -ENOMEM;
  1154. err = snd_ml403_ac97cr_create(card, pfdev, &ml403_ac97cr);
  1155. if (err < 0) {
  1156. PDEBUG(INIT_FAILURE, "probe(): create failed!\n");
  1157. snd_card_free(card);
  1158. return err;
  1159. }
  1160. PDEBUG(INIT_INFO, "probe(): create done\n");
  1161. card->private_data = ml403_ac97cr;
  1162. err = snd_ml403_ac97cr_mixer(ml403_ac97cr);
  1163. if (err < 0) {
  1164. snd_card_free(card);
  1165. return err;
  1166. }
  1167. PDEBUG(INIT_INFO, "probe(): mixer done\n");
  1168. err = snd_ml403_ac97cr_pcm(ml403_ac97cr, 0, NULL);
  1169. if (err < 0) {
  1170. snd_card_free(card);
  1171. return err;
  1172. }
  1173. PDEBUG(INIT_INFO, "probe(): PCM done\n");
  1174. strcpy(card->driver, SND_ML403_AC97CR_DRIVER);
  1175. strcpy(card->shortname, "ML403 AC97 Controller Reference");
  1176. sprintf(card->longname, "%s %s at 0x%lx, irq %i & %i, device %i",
  1177. card->shortname, card->driver,
  1178. (unsigned long)ml403_ac97cr->port, ml403_ac97cr->irq,
  1179. ml403_ac97cr->capture_irq, dev + 1);
  1180. snd_card_set_dev(card, &pfdev->dev);
  1181. err = snd_card_register(card);
  1182. if (err < 0) {
  1183. snd_card_free(card);
  1184. return err;
  1185. }
  1186. platform_set_drvdata(pfdev, card);
  1187. PDEBUG(INIT_INFO, "probe(): (done)\n");
  1188. return 0;
  1189. }
  1190. static int snd_ml403_ac97cr_remove(struct platform_device *pfdev)
  1191. {
  1192. snd_card_free(platform_get_drvdata(pfdev));
  1193. platform_set_drvdata(pfdev, NULL);
  1194. return 0;
  1195. }
  1196. /* work with hotplug and coldplug */
  1197. MODULE_ALIAS("platform:" SND_ML403_AC97CR_DRIVER);
  1198. static struct platform_driver snd_ml403_ac97cr_driver = {
  1199. .probe = snd_ml403_ac97cr_probe,
  1200. .remove = snd_ml403_ac97cr_remove,
  1201. .driver = {
  1202. .name = SND_ML403_AC97CR_DRIVER,
  1203. .owner = THIS_MODULE,
  1204. },
  1205. };
  1206. static int __init alsa_card_ml403_ac97cr_init(void)
  1207. {
  1208. return platform_driver_register(&snd_ml403_ac97cr_driver);
  1209. }
  1210. static void __exit alsa_card_ml403_ac97cr_exit(void)
  1211. {
  1212. platform_driver_unregister(&snd_ml403_ac97cr_driver);
  1213. }
  1214. module_init(alsa_card_ml403_ac97cr_init)
  1215. module_exit(alsa_card_ml403_ac97cr_exit)