ac97c.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Driver for Atmel AC97C
  3. *
  4. * Copyright (C) 2005-2009 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/bitmap.h>
  13. #include <linux/device.h>
  14. #include <linux/atmel_pdc.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mutex.h>
  21. #include <linux/types.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <sound/core.h>
  26. #include <sound/initval.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/ac97_codec.h>
  30. #include <sound/memalloc.h>
  31. #include "ac97c.h"
  32. /* Serialize access to opened variable */
  33. static DEFINE_MUTEX(opened_mutex);
  34. struct atmel_ac97c {
  35. struct clk *pclk;
  36. struct platform_device *pdev;
  37. struct snd_pcm_substream *playback_substream;
  38. struct snd_pcm_substream *capture_substream;
  39. struct snd_card *card;
  40. struct snd_pcm *pcm;
  41. struct snd_ac97 *ac97;
  42. struct snd_ac97_bus *ac97_bus;
  43. u64 cur_format;
  44. unsigned int cur_rate;
  45. int playback_period, capture_period;
  46. /* Serialize access to opened variable */
  47. spinlock_t lock;
  48. void __iomem *regs;
  49. int irq;
  50. int opened;
  51. struct gpio_desc *reset_pin;
  52. };
  53. #define get_chip(card) ((struct atmel_ac97c *)(card)->private_data)
  54. #define ac97c_writel(chip, reg, val) \
  55. __raw_writel((val), (chip)->regs + AC97C_##reg)
  56. #define ac97c_readl(chip, reg) \
  57. __raw_readl((chip)->regs + AC97C_##reg)
  58. static const struct snd_pcm_hardware atmel_ac97c_hw = {
  59. .info = (SNDRV_PCM_INFO_MMAP
  60. | SNDRV_PCM_INFO_MMAP_VALID
  61. | SNDRV_PCM_INFO_INTERLEAVED
  62. | SNDRV_PCM_INFO_BLOCK_TRANSFER
  63. | SNDRV_PCM_INFO_JOINT_DUPLEX
  64. | SNDRV_PCM_INFO_RESUME
  65. | SNDRV_PCM_INFO_PAUSE),
  66. .formats = (SNDRV_PCM_FMTBIT_S16_BE
  67. | SNDRV_PCM_FMTBIT_S16_LE),
  68. .rates = (SNDRV_PCM_RATE_CONTINUOUS),
  69. .rate_min = 4000,
  70. .rate_max = 48000,
  71. .channels_min = 1,
  72. .channels_max = 2,
  73. .buffer_bytes_max = 2 * 2 * 64 * 2048,
  74. .period_bytes_min = 4096,
  75. .period_bytes_max = 4096,
  76. .periods_min = 6,
  77. .periods_max = 64,
  78. };
  79. static int atmel_ac97c_playback_open(struct snd_pcm_substream *substream)
  80. {
  81. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  82. struct snd_pcm_runtime *runtime = substream->runtime;
  83. mutex_lock(&opened_mutex);
  84. chip->opened++;
  85. runtime->hw = atmel_ac97c_hw;
  86. if (chip->cur_rate) {
  87. runtime->hw.rate_min = chip->cur_rate;
  88. runtime->hw.rate_max = chip->cur_rate;
  89. }
  90. if (chip->cur_format)
  91. runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
  92. mutex_unlock(&opened_mutex);
  93. chip->playback_substream = substream;
  94. return 0;
  95. }
  96. static int atmel_ac97c_capture_open(struct snd_pcm_substream *substream)
  97. {
  98. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  99. struct snd_pcm_runtime *runtime = substream->runtime;
  100. mutex_lock(&opened_mutex);
  101. chip->opened++;
  102. runtime->hw = atmel_ac97c_hw;
  103. if (chip->cur_rate) {
  104. runtime->hw.rate_min = chip->cur_rate;
  105. runtime->hw.rate_max = chip->cur_rate;
  106. }
  107. if (chip->cur_format)
  108. runtime->hw.formats = pcm_format_to_bits(chip->cur_format);
  109. mutex_unlock(&opened_mutex);
  110. chip->capture_substream = substream;
  111. return 0;
  112. }
  113. static int atmel_ac97c_playback_close(struct snd_pcm_substream *substream)
  114. {
  115. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  116. mutex_lock(&opened_mutex);
  117. chip->opened--;
  118. if (!chip->opened) {
  119. chip->cur_rate = 0;
  120. chip->cur_format = 0;
  121. }
  122. mutex_unlock(&opened_mutex);
  123. chip->playback_substream = NULL;
  124. return 0;
  125. }
  126. static int atmel_ac97c_capture_close(struct snd_pcm_substream *substream)
  127. {
  128. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  129. mutex_lock(&opened_mutex);
  130. chip->opened--;
  131. if (!chip->opened) {
  132. chip->cur_rate = 0;
  133. chip->cur_format = 0;
  134. }
  135. mutex_unlock(&opened_mutex);
  136. chip->capture_substream = NULL;
  137. return 0;
  138. }
  139. static int atmel_ac97c_playback_hw_params(struct snd_pcm_substream *substream,
  140. struct snd_pcm_hw_params *hw_params)
  141. {
  142. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  143. int retval;
  144. retval = snd_pcm_lib_malloc_pages(substream,
  145. params_buffer_bytes(hw_params));
  146. if (retval < 0)
  147. return retval;
  148. /* Set restrictions to params. */
  149. mutex_lock(&opened_mutex);
  150. chip->cur_rate = params_rate(hw_params);
  151. chip->cur_format = params_format(hw_params);
  152. mutex_unlock(&opened_mutex);
  153. return retval;
  154. }
  155. static int atmel_ac97c_capture_hw_params(struct snd_pcm_substream *substream,
  156. struct snd_pcm_hw_params *hw_params)
  157. {
  158. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  159. int retval;
  160. retval = snd_pcm_lib_malloc_pages(substream,
  161. params_buffer_bytes(hw_params));
  162. if (retval < 0)
  163. return retval;
  164. /* Set restrictions to params. */
  165. mutex_lock(&opened_mutex);
  166. chip->cur_rate = params_rate(hw_params);
  167. chip->cur_format = params_format(hw_params);
  168. mutex_unlock(&opened_mutex);
  169. return retval;
  170. }
  171. static int atmel_ac97c_playback_prepare(struct snd_pcm_substream *substream)
  172. {
  173. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  174. struct snd_pcm_runtime *runtime = substream->runtime;
  175. int block_size = frames_to_bytes(runtime, runtime->period_size);
  176. unsigned long word = ac97c_readl(chip, OCA);
  177. int retval;
  178. chip->playback_period = 0;
  179. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  180. /* assign channels to AC97C channel A */
  181. switch (runtime->channels) {
  182. case 1:
  183. word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
  184. break;
  185. case 2:
  186. word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
  187. | AC97C_CH_ASSIGN(PCM_RIGHT, A);
  188. break;
  189. default:
  190. /* TODO: support more than two channels */
  191. return -EINVAL;
  192. }
  193. ac97c_writel(chip, OCA, word);
  194. /* configure sample format and size */
  195. word = ac97c_readl(chip, CAMR);
  196. if (chip->opened <= 1)
  197. word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  198. else
  199. word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  200. switch (runtime->format) {
  201. case SNDRV_PCM_FORMAT_S16_LE:
  202. break;
  203. case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
  204. word &= ~(AC97C_CMR_CEM_LITTLE);
  205. break;
  206. default:
  207. word = ac97c_readl(chip, OCA);
  208. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  209. ac97c_writel(chip, OCA, word);
  210. return -EINVAL;
  211. }
  212. /* Enable underrun interrupt on channel A */
  213. word |= AC97C_CSR_UNRUN;
  214. ac97c_writel(chip, CAMR, word);
  215. /* Enable channel A event interrupt */
  216. word = ac97c_readl(chip, IMR);
  217. word |= AC97C_SR_CAEVT;
  218. ac97c_writel(chip, IER, word);
  219. /* set variable rate if needed */
  220. if (runtime->rate != 48000) {
  221. word = ac97c_readl(chip, MR);
  222. word |= AC97C_MR_VRA;
  223. ac97c_writel(chip, MR, word);
  224. } else {
  225. word = ac97c_readl(chip, MR);
  226. word &= ~(AC97C_MR_VRA);
  227. ac97c_writel(chip, MR, word);
  228. }
  229. retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_FRONT_DAC_RATE,
  230. runtime->rate);
  231. if (retval)
  232. dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
  233. runtime->rate);
  234. /* Initialize and start the PDC */
  235. writel(runtime->dma_addr, chip->regs + ATMEL_PDC_TPR);
  236. writel(block_size / 2, chip->regs + ATMEL_PDC_TCR);
  237. writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_TNPR);
  238. writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
  239. return retval;
  240. }
  241. static int atmel_ac97c_capture_prepare(struct snd_pcm_substream *substream)
  242. {
  243. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  244. struct snd_pcm_runtime *runtime = substream->runtime;
  245. int block_size = frames_to_bytes(runtime, runtime->period_size);
  246. unsigned long word = ac97c_readl(chip, ICA);
  247. int retval;
  248. chip->capture_period = 0;
  249. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  250. /* assign channels to AC97C channel A */
  251. switch (runtime->channels) {
  252. case 1:
  253. word |= AC97C_CH_ASSIGN(PCM_LEFT, A);
  254. break;
  255. case 2:
  256. word |= AC97C_CH_ASSIGN(PCM_LEFT, A)
  257. | AC97C_CH_ASSIGN(PCM_RIGHT, A);
  258. break;
  259. default:
  260. /* TODO: support more than two channels */
  261. return -EINVAL;
  262. }
  263. ac97c_writel(chip, ICA, word);
  264. /* configure sample format and size */
  265. word = ac97c_readl(chip, CAMR);
  266. if (chip->opened <= 1)
  267. word = AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  268. else
  269. word |= AC97C_CMR_DMAEN | AC97C_CMR_SIZE_16;
  270. switch (runtime->format) {
  271. case SNDRV_PCM_FORMAT_S16_LE:
  272. break;
  273. case SNDRV_PCM_FORMAT_S16_BE: /* fall through */
  274. word &= ~(AC97C_CMR_CEM_LITTLE);
  275. break;
  276. default:
  277. word = ac97c_readl(chip, ICA);
  278. word &= ~(AC97C_CH_MASK(PCM_LEFT) | AC97C_CH_MASK(PCM_RIGHT));
  279. ac97c_writel(chip, ICA, word);
  280. return -EINVAL;
  281. }
  282. /* Enable overrun interrupt on channel A */
  283. word |= AC97C_CSR_OVRUN;
  284. ac97c_writel(chip, CAMR, word);
  285. /* Enable channel A event interrupt */
  286. word = ac97c_readl(chip, IMR);
  287. word |= AC97C_SR_CAEVT;
  288. ac97c_writel(chip, IER, word);
  289. /* set variable rate if needed */
  290. if (runtime->rate != 48000) {
  291. word = ac97c_readl(chip, MR);
  292. word |= AC97C_MR_VRA;
  293. ac97c_writel(chip, MR, word);
  294. } else {
  295. word = ac97c_readl(chip, MR);
  296. word &= ~(AC97C_MR_VRA);
  297. ac97c_writel(chip, MR, word);
  298. }
  299. retval = snd_ac97_set_rate(chip->ac97, AC97_PCM_LR_ADC_RATE,
  300. runtime->rate);
  301. if (retval)
  302. dev_dbg(&chip->pdev->dev, "could not set rate %d Hz\n",
  303. runtime->rate);
  304. /* Initialize and start the PDC */
  305. writel(runtime->dma_addr, chip->regs + ATMEL_PDC_RPR);
  306. writel(block_size / 2, chip->regs + ATMEL_PDC_RCR);
  307. writel(runtime->dma_addr + block_size, chip->regs + ATMEL_PDC_RNPR);
  308. writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
  309. return retval;
  310. }
  311. static int
  312. atmel_ac97c_playback_trigger(struct snd_pcm_substream *substream, int cmd)
  313. {
  314. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  315. unsigned long camr, ptcr = 0;
  316. camr = ac97c_readl(chip, CAMR);
  317. switch (cmd) {
  318. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  319. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  320. case SNDRV_PCM_TRIGGER_START:
  321. ptcr = ATMEL_PDC_TXTEN;
  322. camr |= AC97C_CMR_CENA | AC97C_CSR_ENDTX;
  323. break;
  324. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  325. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  326. case SNDRV_PCM_TRIGGER_STOP:
  327. ptcr |= ATMEL_PDC_TXTDIS;
  328. if (chip->opened <= 1)
  329. camr &= ~AC97C_CMR_CENA;
  330. break;
  331. default:
  332. return -EINVAL;
  333. }
  334. ac97c_writel(chip, CAMR, camr);
  335. writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
  336. return 0;
  337. }
  338. static int
  339. atmel_ac97c_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  340. {
  341. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  342. unsigned long camr, ptcr = 0;
  343. camr = ac97c_readl(chip, CAMR);
  344. ptcr = readl(chip->regs + ATMEL_PDC_PTSR);
  345. switch (cmd) {
  346. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  347. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  348. case SNDRV_PCM_TRIGGER_START:
  349. ptcr = ATMEL_PDC_RXTEN;
  350. camr |= AC97C_CMR_CENA | AC97C_CSR_ENDRX;
  351. break;
  352. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  353. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  354. case SNDRV_PCM_TRIGGER_STOP:
  355. ptcr |= ATMEL_PDC_RXTDIS;
  356. if (chip->opened <= 1)
  357. camr &= ~AC97C_CMR_CENA;
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. ac97c_writel(chip, CAMR, camr);
  363. writel(ptcr, chip->regs + ATMEL_PDC_PTCR);
  364. return 0;
  365. }
  366. static snd_pcm_uframes_t
  367. atmel_ac97c_playback_pointer(struct snd_pcm_substream *substream)
  368. {
  369. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  370. struct snd_pcm_runtime *runtime = substream->runtime;
  371. snd_pcm_uframes_t frames;
  372. unsigned long bytes;
  373. bytes = readl(chip->regs + ATMEL_PDC_TPR);
  374. bytes -= runtime->dma_addr;
  375. frames = bytes_to_frames(runtime, bytes);
  376. if (frames >= runtime->buffer_size)
  377. frames -= runtime->buffer_size;
  378. return frames;
  379. }
  380. static snd_pcm_uframes_t
  381. atmel_ac97c_capture_pointer(struct snd_pcm_substream *substream)
  382. {
  383. struct atmel_ac97c *chip = snd_pcm_substream_chip(substream);
  384. struct snd_pcm_runtime *runtime = substream->runtime;
  385. snd_pcm_uframes_t frames;
  386. unsigned long bytes;
  387. bytes = readl(chip->regs + ATMEL_PDC_RPR);
  388. bytes -= runtime->dma_addr;
  389. frames = bytes_to_frames(runtime, bytes);
  390. if (frames >= runtime->buffer_size)
  391. frames -= runtime->buffer_size;
  392. return frames;
  393. }
  394. static const struct snd_pcm_ops atmel_ac97_playback_ops = {
  395. .open = atmel_ac97c_playback_open,
  396. .close = atmel_ac97c_playback_close,
  397. .ioctl = snd_pcm_lib_ioctl,
  398. .hw_params = atmel_ac97c_playback_hw_params,
  399. .hw_free = snd_pcm_lib_free_pages,
  400. .prepare = atmel_ac97c_playback_prepare,
  401. .trigger = atmel_ac97c_playback_trigger,
  402. .pointer = atmel_ac97c_playback_pointer,
  403. };
  404. static const struct snd_pcm_ops atmel_ac97_capture_ops = {
  405. .open = atmel_ac97c_capture_open,
  406. .close = atmel_ac97c_capture_close,
  407. .ioctl = snd_pcm_lib_ioctl,
  408. .hw_params = atmel_ac97c_capture_hw_params,
  409. .hw_free = snd_pcm_lib_free_pages,
  410. .prepare = atmel_ac97c_capture_prepare,
  411. .trigger = atmel_ac97c_capture_trigger,
  412. .pointer = atmel_ac97c_capture_pointer,
  413. };
  414. static irqreturn_t atmel_ac97c_interrupt(int irq, void *dev)
  415. {
  416. struct atmel_ac97c *chip = (struct atmel_ac97c *)dev;
  417. irqreturn_t retval = IRQ_NONE;
  418. u32 sr = ac97c_readl(chip, SR);
  419. u32 casr = ac97c_readl(chip, CASR);
  420. u32 cosr = ac97c_readl(chip, COSR);
  421. u32 camr = ac97c_readl(chip, CAMR);
  422. if (sr & AC97C_SR_CAEVT) {
  423. struct snd_pcm_runtime *runtime;
  424. int offset, next_period, block_size;
  425. dev_dbg(&chip->pdev->dev, "channel A event%s%s%s%s%s%s\n",
  426. casr & AC97C_CSR_OVRUN ? " OVRUN" : "",
  427. casr & AC97C_CSR_RXRDY ? " RXRDY" : "",
  428. casr & AC97C_CSR_UNRUN ? " UNRUN" : "",
  429. casr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
  430. casr & AC97C_CSR_TXRDY ? " TXRDY" : "",
  431. !casr ? " NONE" : "");
  432. if ((casr & camr) & AC97C_CSR_ENDTX) {
  433. runtime = chip->playback_substream->runtime;
  434. block_size = frames_to_bytes(runtime, runtime->period_size);
  435. chip->playback_period++;
  436. if (chip->playback_period == runtime->periods)
  437. chip->playback_period = 0;
  438. next_period = chip->playback_period + 1;
  439. if (next_period == runtime->periods)
  440. next_period = 0;
  441. offset = block_size * next_period;
  442. writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_TNPR);
  443. writel(block_size / 2, chip->regs + ATMEL_PDC_TNCR);
  444. snd_pcm_period_elapsed(chip->playback_substream);
  445. }
  446. if ((casr & camr) & AC97C_CSR_ENDRX) {
  447. runtime = chip->capture_substream->runtime;
  448. block_size = frames_to_bytes(runtime, runtime->period_size);
  449. chip->capture_period++;
  450. if (chip->capture_period == runtime->periods)
  451. chip->capture_period = 0;
  452. next_period = chip->capture_period + 1;
  453. if (next_period == runtime->periods)
  454. next_period = 0;
  455. offset = block_size * next_period;
  456. writel(runtime->dma_addr + offset, chip->regs + ATMEL_PDC_RNPR);
  457. writel(block_size / 2, chip->regs + ATMEL_PDC_RNCR);
  458. snd_pcm_period_elapsed(chip->capture_substream);
  459. }
  460. retval = IRQ_HANDLED;
  461. }
  462. if (sr & AC97C_SR_COEVT) {
  463. dev_info(&chip->pdev->dev, "codec channel event%s%s%s%s%s\n",
  464. cosr & AC97C_CSR_OVRUN ? " OVRUN" : "",
  465. cosr & AC97C_CSR_RXRDY ? " RXRDY" : "",
  466. cosr & AC97C_CSR_TXEMPTY ? " TXEMPTY" : "",
  467. cosr & AC97C_CSR_TXRDY ? " TXRDY" : "",
  468. !cosr ? " NONE" : "");
  469. retval = IRQ_HANDLED;
  470. }
  471. if (retval == IRQ_NONE) {
  472. dev_err(&chip->pdev->dev, "spurious interrupt sr 0x%08x "
  473. "casr 0x%08x cosr 0x%08x\n", sr, casr, cosr);
  474. }
  475. return retval;
  476. }
  477. static const struct ac97_pcm at91_ac97_pcm_defs[] = {
  478. /* Playback */
  479. {
  480. .exclusive = 1,
  481. .r = { {
  482. .slots = ((1 << AC97_SLOT_PCM_LEFT)
  483. | (1 << AC97_SLOT_PCM_RIGHT)),
  484. } },
  485. },
  486. /* PCM in */
  487. {
  488. .stream = 1,
  489. .exclusive = 1,
  490. .r = { {
  491. .slots = ((1 << AC97_SLOT_PCM_LEFT)
  492. | (1 << AC97_SLOT_PCM_RIGHT)),
  493. } }
  494. },
  495. /* Mic in */
  496. {
  497. .stream = 1,
  498. .exclusive = 1,
  499. .r = { {
  500. .slots = (1<<AC97_SLOT_MIC),
  501. } }
  502. },
  503. };
  504. static int atmel_ac97c_pcm_new(struct atmel_ac97c *chip)
  505. {
  506. struct snd_pcm *pcm;
  507. struct snd_pcm_hardware hw = atmel_ac97c_hw;
  508. int retval;
  509. retval = snd_ac97_pcm_assign(chip->ac97_bus,
  510. ARRAY_SIZE(at91_ac97_pcm_defs),
  511. at91_ac97_pcm_defs);
  512. if (retval)
  513. return retval;
  514. retval = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
  515. if (retval)
  516. return retval;
  517. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &atmel_ac97_capture_ops);
  518. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &atmel_ac97_playback_ops);
  519. retval = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  520. &chip->pdev->dev, hw.periods_min * hw.period_bytes_min,
  521. hw.buffer_bytes_max);
  522. if (retval)
  523. return retval;
  524. pcm->private_data = chip;
  525. pcm->info_flags = 0;
  526. strcpy(pcm->name, chip->card->shortname);
  527. chip->pcm = pcm;
  528. return 0;
  529. }
  530. static int atmel_ac97c_mixer_new(struct atmel_ac97c *chip)
  531. {
  532. struct snd_ac97_template template;
  533. memset(&template, 0, sizeof(template));
  534. template.private_data = chip;
  535. return snd_ac97_mixer(chip->ac97_bus, &template, &chip->ac97);
  536. }
  537. static void atmel_ac97c_write(struct snd_ac97 *ac97, unsigned short reg,
  538. unsigned short val)
  539. {
  540. struct atmel_ac97c *chip = get_chip(ac97);
  541. unsigned long word;
  542. int timeout = 40;
  543. word = (reg & 0x7f) << 16 | val;
  544. do {
  545. if (ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) {
  546. ac97c_writel(chip, COTHR, word);
  547. return;
  548. }
  549. udelay(1);
  550. } while (--timeout);
  551. dev_dbg(&chip->pdev->dev, "codec write timeout\n");
  552. }
  553. static unsigned short atmel_ac97c_read(struct snd_ac97 *ac97,
  554. unsigned short reg)
  555. {
  556. struct atmel_ac97c *chip = get_chip(ac97);
  557. unsigned long word;
  558. int timeout = 40;
  559. int write = 10;
  560. word = (0x80 | (reg & 0x7f)) << 16;
  561. if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0)
  562. ac97c_readl(chip, CORHR);
  563. retry_write:
  564. timeout = 40;
  565. do {
  566. if ((ac97c_readl(chip, COSR) & AC97C_CSR_TXRDY) != 0) {
  567. ac97c_writel(chip, COTHR, word);
  568. goto read_reg;
  569. }
  570. udelay(10);
  571. } while (--timeout);
  572. if (!--write)
  573. goto timed_out;
  574. goto retry_write;
  575. read_reg:
  576. do {
  577. if ((ac97c_readl(chip, COSR) & AC97C_CSR_RXRDY) != 0) {
  578. unsigned short val = ac97c_readl(chip, CORHR);
  579. return val;
  580. }
  581. udelay(10);
  582. } while (--timeout);
  583. if (!--write)
  584. goto timed_out;
  585. goto retry_write;
  586. timed_out:
  587. dev_dbg(&chip->pdev->dev, "codec read timeout\n");
  588. return 0xffff;
  589. }
  590. static void atmel_ac97c_reset(struct atmel_ac97c *chip)
  591. {
  592. ac97c_writel(chip, MR, 0);
  593. ac97c_writel(chip, MR, AC97C_MR_ENA);
  594. ac97c_writel(chip, CAMR, 0);
  595. ac97c_writel(chip, COMR, 0);
  596. if (!IS_ERR(chip->reset_pin)) {
  597. gpiod_set_value(chip->reset_pin, 0);
  598. /* AC97 v2.2 specifications says minimum 1 us. */
  599. udelay(2);
  600. gpiod_set_value(chip->reset_pin, 1);
  601. } else {
  602. ac97c_writel(chip, MR, AC97C_MR_WRST | AC97C_MR_ENA);
  603. udelay(2);
  604. ac97c_writel(chip, MR, AC97C_MR_ENA);
  605. }
  606. }
  607. static const struct of_device_id atmel_ac97c_dt_ids[] = {
  608. { .compatible = "atmel,at91sam9263-ac97c", },
  609. { }
  610. };
  611. MODULE_DEVICE_TABLE(of, atmel_ac97c_dt_ids);
  612. static int atmel_ac97c_probe(struct platform_device *pdev)
  613. {
  614. struct device *dev = &pdev->dev;
  615. struct snd_card *card;
  616. struct atmel_ac97c *chip;
  617. struct resource *regs;
  618. struct clk *pclk;
  619. static struct snd_ac97_bus_ops ops = {
  620. .write = atmel_ac97c_write,
  621. .read = atmel_ac97c_read,
  622. };
  623. int retval;
  624. int irq;
  625. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  626. if (!regs) {
  627. dev_dbg(&pdev->dev, "no memory resource\n");
  628. return -ENXIO;
  629. }
  630. irq = platform_get_irq(pdev, 0);
  631. if (irq < 0) {
  632. dev_dbg(&pdev->dev, "could not get irq: %d\n", irq);
  633. return irq;
  634. }
  635. pclk = clk_get(&pdev->dev, "ac97_clk");
  636. if (IS_ERR(pclk)) {
  637. dev_dbg(&pdev->dev, "no peripheral clock\n");
  638. return PTR_ERR(pclk);
  639. }
  640. retval = clk_prepare_enable(pclk);
  641. if (retval)
  642. goto err_prepare_enable;
  643. retval = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1,
  644. SNDRV_DEFAULT_STR1, THIS_MODULE,
  645. sizeof(struct atmel_ac97c), &card);
  646. if (retval) {
  647. dev_dbg(&pdev->dev, "could not create sound card device\n");
  648. goto err_snd_card_new;
  649. }
  650. chip = get_chip(card);
  651. retval = request_irq(irq, atmel_ac97c_interrupt, 0, "AC97C", chip);
  652. if (retval) {
  653. dev_dbg(&pdev->dev, "unable to request irq %d\n", irq);
  654. goto err_request_irq;
  655. }
  656. chip->irq = irq;
  657. spin_lock_init(&chip->lock);
  658. strcpy(card->driver, "Atmel AC97C");
  659. strcpy(card->shortname, "Atmel AC97C");
  660. sprintf(card->longname, "Atmel AC97 controller");
  661. chip->card = card;
  662. chip->pclk = pclk;
  663. chip->pdev = pdev;
  664. chip->regs = ioremap(regs->start, resource_size(regs));
  665. if (!chip->regs) {
  666. dev_dbg(&pdev->dev, "could not remap register memory\n");
  667. retval = -ENOMEM;
  668. goto err_ioremap;
  669. }
  670. chip->reset_pin = devm_gpiod_get_index(dev, "ac97", 2, GPIOD_OUT_HIGH);
  671. if (IS_ERR(chip->reset_pin))
  672. dev_dbg(dev, "reset pin not available\n");
  673. atmel_ac97c_reset(chip);
  674. /* Enable overrun interrupt from codec channel */
  675. ac97c_writel(chip, COMR, AC97C_CSR_OVRUN);
  676. ac97c_writel(chip, IER, ac97c_readl(chip, IMR) | AC97C_SR_COEVT);
  677. retval = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus);
  678. if (retval) {
  679. dev_dbg(&pdev->dev, "could not register on ac97 bus\n");
  680. goto err_ac97_bus;
  681. }
  682. retval = atmel_ac97c_mixer_new(chip);
  683. if (retval) {
  684. dev_dbg(&pdev->dev, "could not register ac97 mixer\n");
  685. goto err_ac97_bus;
  686. }
  687. retval = atmel_ac97c_pcm_new(chip);
  688. if (retval) {
  689. dev_dbg(&pdev->dev, "could not register ac97 pcm device\n");
  690. goto err_ac97_bus;
  691. }
  692. retval = snd_card_register(card);
  693. if (retval) {
  694. dev_dbg(&pdev->dev, "could not register sound card\n");
  695. goto err_ac97_bus;
  696. }
  697. platform_set_drvdata(pdev, card);
  698. dev_info(&pdev->dev, "Atmel AC97 controller at 0x%p, irq = %d\n",
  699. chip->regs, irq);
  700. return 0;
  701. err_ac97_bus:
  702. iounmap(chip->regs);
  703. err_ioremap:
  704. free_irq(irq, chip);
  705. err_request_irq:
  706. snd_card_free(card);
  707. err_snd_card_new:
  708. clk_disable_unprepare(pclk);
  709. err_prepare_enable:
  710. clk_put(pclk);
  711. return retval;
  712. }
  713. #ifdef CONFIG_PM_SLEEP
  714. static int atmel_ac97c_suspend(struct device *pdev)
  715. {
  716. struct snd_card *card = dev_get_drvdata(pdev);
  717. struct atmel_ac97c *chip = card->private_data;
  718. clk_disable_unprepare(chip->pclk);
  719. return 0;
  720. }
  721. static int atmel_ac97c_resume(struct device *pdev)
  722. {
  723. struct snd_card *card = dev_get_drvdata(pdev);
  724. struct atmel_ac97c *chip = card->private_data;
  725. int ret = clk_prepare_enable(chip->pclk);
  726. return ret;
  727. }
  728. static SIMPLE_DEV_PM_OPS(atmel_ac97c_pm, atmel_ac97c_suspend, atmel_ac97c_resume);
  729. #define ATMEL_AC97C_PM_OPS &atmel_ac97c_pm
  730. #else
  731. #define ATMEL_AC97C_PM_OPS NULL
  732. #endif
  733. static int atmel_ac97c_remove(struct platform_device *pdev)
  734. {
  735. struct snd_card *card = platform_get_drvdata(pdev);
  736. struct atmel_ac97c *chip = get_chip(card);
  737. ac97c_writel(chip, CAMR, 0);
  738. ac97c_writel(chip, COMR, 0);
  739. ac97c_writel(chip, MR, 0);
  740. clk_disable_unprepare(chip->pclk);
  741. clk_put(chip->pclk);
  742. iounmap(chip->regs);
  743. free_irq(chip->irq, chip);
  744. snd_card_free(card);
  745. return 0;
  746. }
  747. static struct platform_driver atmel_ac97c_driver = {
  748. .probe = atmel_ac97c_probe,
  749. .remove = atmel_ac97c_remove,
  750. .driver = {
  751. .name = "atmel_ac97c",
  752. .pm = ATMEL_AC97C_PM_OPS,
  753. .of_match_table = atmel_ac97c_dt_ids,
  754. },
  755. };
  756. module_platform_driver(atmel_ac97c_driver);
  757. MODULE_LICENSE("GPL");
  758. MODULE_DESCRIPTION("Driver for Atmel AC97 controller");
  759. MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");