ml403-ac97cr.c 40 KB

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