ml403-ac97cr.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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/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 int 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 __devinit
  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. if (ml403_ac97cr->port != NULL)
  983. iounmap(ml403_ac97cr->port);
  984. kfree(ml403_ac97cr);
  985. PDEBUG(INIT_INFO, "free(): (done)\n");
  986. return 0;
  987. }
  988. static int snd_ml403_ac97cr_dev_free(struct snd_device *snddev)
  989. {
  990. struct snd_ml403_ac97cr *ml403_ac97cr = snddev->device_data;
  991. PDEBUG(INIT_INFO, "dev_free():\n");
  992. return snd_ml403_ac97cr_free(ml403_ac97cr);
  993. }
  994. static int __devinit
  995. snd_ml403_ac97cr_create(struct snd_card *card, struct platform_device *pfdev,
  996. struct snd_ml403_ac97cr **rml403_ac97cr)
  997. {
  998. struct snd_ml403_ac97cr *ml403_ac97cr;
  999. int err;
  1000. static struct snd_device_ops ops = {
  1001. .dev_free = snd_ml403_ac97cr_dev_free,
  1002. };
  1003. struct resource *resource;
  1004. int irq;
  1005. *rml403_ac97cr = NULL;
  1006. ml403_ac97cr = kzalloc(sizeof(*ml403_ac97cr), GFP_KERNEL);
  1007. if (ml403_ac97cr == NULL)
  1008. return -ENOMEM;
  1009. spin_lock_init(&ml403_ac97cr->reg_lock);
  1010. mutex_init(&ml403_ac97cr->cdc_mutex);
  1011. ml403_ac97cr->card = card;
  1012. ml403_ac97cr->pfdev = pfdev;
  1013. ml403_ac97cr->irq = -1;
  1014. ml403_ac97cr->enable_irq = 0;
  1015. ml403_ac97cr->capture_irq = -1;
  1016. ml403_ac97cr->enable_capture_irq = 0;
  1017. ml403_ac97cr->port = NULL;
  1018. ml403_ac97cr->res_port = NULL;
  1019. PDEBUG(INIT_INFO, "Trying to reserve resources now ...\n");
  1020. resource = platform_get_resource(pfdev, IORESOURCE_MEM, 0);
  1021. /* get "port" */
  1022. ml403_ac97cr->port = ioremap_nocache(resource->start,
  1023. (resource->end) -
  1024. (resource->start) + 1);
  1025. if (ml403_ac97cr->port == NULL) {
  1026. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  1027. "unable to remap memory region (%pR)\n",
  1028. resource);
  1029. snd_ml403_ac97cr_free(ml403_ac97cr);
  1030. return -EBUSY;
  1031. }
  1032. snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
  1033. "remap controller memory region to "
  1034. "0x%x done\n", (unsigned int)ml403_ac97cr->port);
  1035. /* get irq */
  1036. irq = platform_get_irq(pfdev, 0);
  1037. if (request_irq(irq, snd_ml403_ac97cr_irq, IRQF_DISABLED,
  1038. dev_name(&pfdev->dev), (void *)ml403_ac97cr)) {
  1039. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  1040. "unable to grab IRQ %d\n",
  1041. irq);
  1042. snd_ml403_ac97cr_free(ml403_ac97cr);
  1043. return -EBUSY;
  1044. }
  1045. ml403_ac97cr->irq = irq;
  1046. snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
  1047. "request (playback) irq %d done\n",
  1048. ml403_ac97cr->irq);
  1049. irq = platform_get_irq(pfdev, 1);
  1050. if (request_irq(irq, snd_ml403_ac97cr_irq, IRQF_DISABLED,
  1051. dev_name(&pfdev->dev), (void *)ml403_ac97cr)) {
  1052. snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
  1053. "unable to grab IRQ %d\n",
  1054. irq);
  1055. snd_ml403_ac97cr_free(ml403_ac97cr);
  1056. return -EBUSY;
  1057. }
  1058. ml403_ac97cr->capture_irq = irq;
  1059. snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
  1060. "request (capture) irq %d done\n",
  1061. ml403_ac97cr->capture_irq);
  1062. err = snd_ml403_ac97cr_chip_init(ml403_ac97cr);
  1063. if (err < 0) {
  1064. snd_ml403_ac97cr_free(ml403_ac97cr);
  1065. return err;
  1066. }
  1067. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ml403_ac97cr, &ops);
  1068. if (err < 0) {
  1069. PDEBUG(INIT_FAILURE, "probe(): snd_device_new() failed!\n");
  1070. snd_ml403_ac97cr_free(ml403_ac97cr);
  1071. return err;
  1072. }
  1073. *rml403_ac97cr = ml403_ac97cr;
  1074. return 0;
  1075. }
  1076. static void snd_ml403_ac97cr_mixer_free(struct snd_ac97 *ac97)
  1077. {
  1078. struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
  1079. PDEBUG(INIT_INFO, "mixer_free():\n");
  1080. ml403_ac97cr->ac97 = NULL;
  1081. PDEBUG(INIT_INFO, "mixer_free(): (done)\n");
  1082. }
  1083. static int __devinit
  1084. snd_ml403_ac97cr_mixer(struct snd_ml403_ac97cr *ml403_ac97cr)
  1085. {
  1086. struct snd_ac97_bus *bus;
  1087. struct snd_ac97_template ac97;
  1088. int err;
  1089. static struct snd_ac97_bus_ops ops = {
  1090. .write = snd_ml403_ac97cr_codec_write,
  1091. .read = snd_ml403_ac97cr_codec_read,
  1092. };
  1093. PDEBUG(INIT_INFO, "mixer():\n");
  1094. err = snd_ac97_bus(ml403_ac97cr->card, 0, &ops, NULL, &bus);
  1095. if (err < 0)
  1096. return err;
  1097. memset(&ac97, 0, sizeof(ac97));
  1098. ml403_ac97cr->ac97_fake = 1;
  1099. lm4550_regfile_init();
  1100. #ifdef CODEC_STAT
  1101. ml403_ac97cr->ac97_read = 0;
  1102. ml403_ac97cr->ac97_write = 0;
  1103. #endif
  1104. ac97.private_data = ml403_ac97cr;
  1105. ac97.private_free = snd_ml403_ac97cr_mixer_free;
  1106. ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM |
  1107. AC97_SCAP_NO_SPDIF;
  1108. err = snd_ac97_mixer(bus, &ac97, &ml403_ac97cr->ac97);
  1109. ml403_ac97cr->ac97_fake = 0;
  1110. lm4550_regfile_write_values_after_init(ml403_ac97cr->ac97);
  1111. PDEBUG(INIT_INFO, "mixer(): (done) snd_ac97_mixer()=%d\n", err);
  1112. return err;
  1113. }
  1114. static int __devinit
  1115. snd_ml403_ac97cr_pcm(struct snd_ml403_ac97cr *ml403_ac97cr, int device,
  1116. struct snd_pcm **rpcm)
  1117. {
  1118. struct snd_pcm *pcm;
  1119. int err;
  1120. if (rpcm)
  1121. *rpcm = NULL;
  1122. err = snd_pcm_new(ml403_ac97cr->card, "ML403AC97CR/1", device, 1, 1,
  1123. &pcm);
  1124. if (err < 0)
  1125. return err;
  1126. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1127. &snd_ml403_ac97cr_playback_ops);
  1128. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1129. &snd_ml403_ac97cr_capture_ops);
  1130. pcm->private_data = ml403_ac97cr;
  1131. pcm->info_flags = 0;
  1132. strcpy(pcm->name, "ML403AC97CR DAC/ADC");
  1133. ml403_ac97cr->pcm = pcm;
  1134. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  1135. snd_dma_continuous_data(GFP_KERNEL),
  1136. 64 * 1024,
  1137. 128 * 1024);
  1138. if (rpcm)
  1139. *rpcm = pcm;
  1140. return 0;
  1141. }
  1142. static int __devinit snd_ml403_ac97cr_probe(struct platform_device *pfdev)
  1143. {
  1144. struct snd_card *card;
  1145. struct snd_ml403_ac97cr *ml403_ac97cr = NULL;
  1146. int err;
  1147. int dev = pfdev->id;
  1148. if (dev >= SNDRV_CARDS)
  1149. return -ENODEV;
  1150. if (!enable[dev])
  1151. return -ENOENT;
  1152. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  1153. if (err < 0)
  1154. return err;
  1155. err = snd_ml403_ac97cr_create(card, pfdev, &ml403_ac97cr);
  1156. if (err < 0) {
  1157. PDEBUG(INIT_FAILURE, "probe(): create failed!\n");
  1158. snd_card_free(card);
  1159. return err;
  1160. }
  1161. PDEBUG(INIT_INFO, "probe(): create done\n");
  1162. card->private_data = ml403_ac97cr;
  1163. err = snd_ml403_ac97cr_mixer(ml403_ac97cr);
  1164. if (err < 0) {
  1165. snd_card_free(card);
  1166. return err;
  1167. }
  1168. PDEBUG(INIT_INFO, "probe(): mixer done\n");
  1169. err = snd_ml403_ac97cr_pcm(ml403_ac97cr, 0, NULL);
  1170. if (err < 0) {
  1171. snd_card_free(card);
  1172. return err;
  1173. }
  1174. PDEBUG(INIT_INFO, "probe(): PCM done\n");
  1175. strcpy(card->driver, SND_ML403_AC97CR_DRIVER);
  1176. strcpy(card->shortname, "ML403 AC97 Controller Reference");
  1177. sprintf(card->longname, "%s %s at 0x%lx, irq %i & %i, device %i",
  1178. card->shortname, card->driver,
  1179. (unsigned long)ml403_ac97cr->port, ml403_ac97cr->irq,
  1180. ml403_ac97cr->capture_irq, dev + 1);
  1181. snd_card_set_dev(card, &pfdev->dev);
  1182. err = snd_card_register(card);
  1183. if (err < 0) {
  1184. snd_card_free(card);
  1185. return err;
  1186. }
  1187. platform_set_drvdata(pfdev, card);
  1188. PDEBUG(INIT_INFO, "probe(): (done)\n");
  1189. return 0;
  1190. }
  1191. static int snd_ml403_ac97cr_remove(struct platform_device *pfdev)
  1192. {
  1193. snd_card_free(platform_get_drvdata(pfdev));
  1194. platform_set_drvdata(pfdev, NULL);
  1195. return 0;
  1196. }
  1197. /* work with hotplug and coldplug */
  1198. MODULE_ALIAS("platform:" SND_ML403_AC97CR_DRIVER);
  1199. static struct platform_driver snd_ml403_ac97cr_driver = {
  1200. .probe = snd_ml403_ac97cr_probe,
  1201. .remove = snd_ml403_ac97cr_remove,
  1202. .driver = {
  1203. .name = SND_ML403_AC97CR_DRIVER,
  1204. .owner = THIS_MODULE,
  1205. },
  1206. };
  1207. static int __init alsa_card_ml403_ac97cr_init(void)
  1208. {
  1209. return platform_driver_register(&snd_ml403_ac97cr_driver);
  1210. }
  1211. static void __exit alsa_card_ml403_ac97cr_exit(void)
  1212. {
  1213. platform_driver_unregister(&snd_ml403_ac97cr_driver);
  1214. }
  1215. module_init(alsa_card_ml403_ac97cr_init)
  1216. module_exit(alsa_card_ml403_ac97cr_exit)