at73c213.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Driver for AT73C213 16-bit stereo DAC connected to Atmel SSC
  3. *
  4. * Copyright (C) 2006-2007 Atmel Norway
  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. /*#define DEBUG*/
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.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/io.h>
  21. #include <sound/driver.h>
  22. #include <sound/initval.h>
  23. #include <sound/control.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <linux/atmel-ssc.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/at73c213.h>
  29. #include "at73c213.h"
  30. #define BITRATE_MIN 8000 /* Hardware limit? */
  31. #define BITRATE_TARGET CONFIG_SND_AT73C213_TARGET_BITRATE
  32. #define BITRATE_MAX 50000 /* Hardware limit. */
  33. /* Initial (hardware reset) AT73C213 register values. */
  34. static u8 snd_at73c213_original_image[18] =
  35. {
  36. 0x00, /* 00 - CTRL */
  37. 0x05, /* 01 - LLIG */
  38. 0x05, /* 02 - RLIG */
  39. 0x08, /* 03 - LPMG */
  40. 0x08, /* 04 - RPMG */
  41. 0x00, /* 05 - LLOG */
  42. 0x00, /* 06 - RLOG */
  43. 0x22, /* 07 - OLC */
  44. 0x09, /* 08 - MC */
  45. 0x00, /* 09 - CSFC */
  46. 0x00, /* 0A - MISC */
  47. 0x00, /* 0B - */
  48. 0x00, /* 0C - PRECH */
  49. 0x05, /* 0D - AUXG */
  50. 0x00, /* 0E - */
  51. 0x00, /* 0F - */
  52. 0x00, /* 10 - RST */
  53. 0x00, /* 11 - PA_CTRL */
  54. };
  55. struct snd_at73c213 {
  56. struct snd_card *card;
  57. struct snd_pcm *pcm;
  58. struct snd_pcm_substream *substream;
  59. struct at73c213_board_info *board;
  60. int irq;
  61. int period;
  62. unsigned long bitrate;
  63. struct clk *bitclk;
  64. struct ssc_device *ssc;
  65. struct spi_device *spi;
  66. u8 spi_wbuffer[2];
  67. u8 spi_rbuffer[2];
  68. /* Image of the SPI registers in AT73C213. */
  69. u8 reg_image[18];
  70. /* Protect registers against concurrent access. */
  71. spinlock_t lock;
  72. };
  73. #define get_chip(card) ((struct snd_at73c213 *)card->private_data)
  74. static int
  75. snd_at73c213_write_reg(struct snd_at73c213 *chip, u8 reg, u8 val)
  76. {
  77. struct spi_message msg;
  78. struct spi_transfer msg_xfer = {
  79. .len = 2,
  80. .cs_change = 0,
  81. };
  82. int retval;
  83. spi_message_init(&msg);
  84. chip->spi_wbuffer[0] = reg;
  85. chip->spi_wbuffer[1] = val;
  86. msg_xfer.tx_buf = chip->spi_wbuffer;
  87. msg_xfer.rx_buf = chip->spi_rbuffer;
  88. spi_message_add_tail(&msg_xfer, &msg);
  89. retval = spi_sync(chip->spi, &msg);
  90. if (!retval)
  91. chip->reg_image[reg] = val;
  92. return retval;
  93. }
  94. static struct snd_pcm_hardware snd_at73c213_playback_hw = {
  95. .info = SNDRV_PCM_INFO_INTERLEAVED |
  96. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  97. .formats = SNDRV_PCM_FMTBIT_S16_BE,
  98. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  99. .rate_min = 8000, /* Replaced by chip->bitrate later. */
  100. .rate_max = 50000, /* Replaced by chip->bitrate later. */
  101. .channels_min = 2,
  102. .channels_max = 2,
  103. .buffer_bytes_max = 64 * 1024 - 1,
  104. .period_bytes_min = 512,
  105. .period_bytes_max = 64 * 1024 - 1,
  106. .periods_min = 4,
  107. .periods_max = 1024,
  108. };
  109. /*
  110. * Calculate and set bitrate and divisions.
  111. */
  112. static int snd_at73c213_set_bitrate(struct snd_at73c213 *chip)
  113. {
  114. unsigned long ssc_rate = clk_get_rate(chip->ssc->clk);
  115. unsigned long dac_rate_new, ssc_div, status;
  116. unsigned long ssc_div_max, ssc_div_min;
  117. int max_tries;
  118. /*
  119. * We connect two clocks here, picking divisors so the I2S clocks
  120. * out data at the same rate the DAC clocks it in ... and as close
  121. * as practical to the desired target rate.
  122. *
  123. * The DAC master clock (MCLK) is programmable, and is either 256
  124. * or (not here) 384 times the I2S output clock (BCLK).
  125. */
  126. /* SSC clock / (bitrate * stereo * 16-bit). */
  127. ssc_div = ssc_rate / (BITRATE_TARGET * 2 * 16);
  128. ssc_div_min = ssc_rate / (BITRATE_MAX * 2 * 16);
  129. ssc_div_max = ssc_rate / (BITRATE_MIN * 2 * 16);
  130. max_tries = (ssc_div_max - ssc_div_min) / 2;
  131. if (max_tries < 1)
  132. max_tries = 1;
  133. /* ssc_div must be a power of 2. */
  134. ssc_div = (ssc_div + 1) & ~1UL;
  135. if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN) {
  136. ssc_div -= 2;
  137. if ((ssc_rate / (ssc_div * 2 * 16)) > BITRATE_MAX)
  138. return -ENXIO;
  139. }
  140. /* Search for a possible bitrate. */
  141. do {
  142. /* SSC clock / (ssc divider * 16-bit * stereo). */
  143. if ((ssc_rate / (ssc_div * 2 * 16)) < BITRATE_MIN)
  144. return -ENXIO;
  145. /* 256 / (2 * 16) = 8 */
  146. dac_rate_new = 8 * (ssc_rate / ssc_div);
  147. status = clk_round_rate(chip->board->dac_clk, dac_rate_new);
  148. if (status < 0)
  149. return status;
  150. /* Ignore difference smaller than 256 Hz. */
  151. if ((status/256) == (dac_rate_new/256))
  152. goto set_rate;
  153. ssc_div += 2;
  154. } while (--max_tries);
  155. /* Not able to find a valid bitrate. */
  156. return -ENXIO;
  157. set_rate:
  158. status = clk_set_rate(chip->board->dac_clk, status);
  159. if (status < 0)
  160. return status;
  161. /* Set divider in SSC device. */
  162. ssc_writel(chip->ssc->regs, CMR, ssc_div/2);
  163. /* SSC clock / (ssc divider * 16-bit * stereo). */
  164. chip->bitrate = ssc_rate / (ssc_div * 16 * 2);
  165. dev_info(&chip->spi->dev,
  166. "at73c213: supported bitrate is %lu (%lu divider)\n",
  167. chip->bitrate, ssc_div);
  168. return 0;
  169. }
  170. static int snd_at73c213_pcm_open(struct snd_pcm_substream *substream)
  171. {
  172. struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
  173. struct snd_pcm_runtime *runtime = substream->runtime;
  174. snd_at73c213_playback_hw.rate_min = chip->bitrate;
  175. snd_at73c213_playback_hw.rate_max = chip->bitrate;
  176. runtime->hw = snd_at73c213_playback_hw;
  177. chip->substream = substream;
  178. return 0;
  179. }
  180. static int snd_at73c213_pcm_close(struct snd_pcm_substream *substream)
  181. {
  182. struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
  183. chip->substream = NULL;
  184. return 0;
  185. }
  186. static int snd_at73c213_pcm_hw_params(struct snd_pcm_substream *substream,
  187. struct snd_pcm_hw_params *hw_params)
  188. {
  189. return snd_pcm_lib_malloc_pages(substream,
  190. params_buffer_bytes(hw_params));
  191. }
  192. static int snd_at73c213_pcm_hw_free(struct snd_pcm_substream *substream)
  193. {
  194. return snd_pcm_lib_free_pages(substream);
  195. }
  196. static int snd_at73c213_pcm_prepare(struct snd_pcm_substream *substream)
  197. {
  198. struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
  199. struct snd_pcm_runtime *runtime = substream->runtime;
  200. int block_size;
  201. block_size = frames_to_bytes(runtime, runtime->period_size);
  202. chip->period = 0;
  203. ssc_writel(chip->ssc->regs, PDC_TPR,
  204. (long)runtime->dma_addr);
  205. ssc_writel(chip->ssc->regs, PDC_TCR, runtime->period_size * 2);
  206. ssc_writel(chip->ssc->regs, PDC_TNPR,
  207. (long)runtime->dma_addr + block_size);
  208. ssc_writel(chip->ssc->regs, PDC_TNCR, runtime->period_size * 2);
  209. return 0;
  210. }
  211. static int snd_at73c213_pcm_trigger(struct snd_pcm_substream *substream,
  212. int cmd)
  213. {
  214. struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
  215. int retval = 0;
  216. spin_lock(&chip->lock);
  217. switch (cmd) {
  218. case SNDRV_PCM_TRIGGER_START:
  219. ssc_writel(chip->ssc->regs, IER, SSC_BIT(IER_ENDTX));
  220. ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTEN));
  221. break;
  222. case SNDRV_PCM_TRIGGER_STOP:
  223. ssc_writel(chip->ssc->regs, PDC_PTCR, SSC_BIT(PDC_PTCR_TXTDIS));
  224. ssc_writel(chip->ssc->regs, IDR, SSC_BIT(IDR_ENDTX));
  225. break;
  226. default:
  227. dev_dbg(&chip->spi->dev, "spurious command %x\n", cmd);
  228. retval = -EINVAL;
  229. break;
  230. }
  231. spin_unlock(&chip->lock);
  232. return retval;
  233. }
  234. static snd_pcm_uframes_t
  235. snd_at73c213_pcm_pointer(struct snd_pcm_substream *substream)
  236. {
  237. struct snd_at73c213 *chip = snd_pcm_substream_chip(substream);
  238. struct snd_pcm_runtime *runtime = substream->runtime;
  239. snd_pcm_uframes_t pos;
  240. unsigned long bytes;
  241. bytes = ssc_readl(chip->ssc->regs, PDC_TPR)
  242. - (unsigned long)runtime->dma_addr;
  243. pos = bytes_to_frames(runtime, bytes);
  244. if (pos >= runtime->buffer_size)
  245. pos -= runtime->buffer_size;
  246. return pos;
  247. }
  248. static struct snd_pcm_ops at73c213_playback_ops = {
  249. .open = snd_at73c213_pcm_open,
  250. .close = snd_at73c213_pcm_close,
  251. .ioctl = snd_pcm_lib_ioctl,
  252. .hw_params = snd_at73c213_pcm_hw_params,
  253. .hw_free = snd_at73c213_pcm_hw_free,
  254. .prepare = snd_at73c213_pcm_prepare,
  255. .trigger = snd_at73c213_pcm_trigger,
  256. .pointer = snd_at73c213_pcm_pointer,
  257. };
  258. static void snd_at73c213_pcm_free(struct snd_pcm *pcm)
  259. {
  260. struct snd_at73c213 *chip = snd_pcm_chip(pcm);
  261. if (chip->pcm) {
  262. snd_pcm_lib_preallocate_free_for_all(chip->pcm);
  263. chip->pcm = NULL;
  264. }
  265. }
  266. static int __devinit snd_at73c213_pcm_new(struct snd_at73c213 *chip, int device)
  267. {
  268. struct snd_pcm *pcm;
  269. int retval;
  270. retval = snd_pcm_new(chip->card, chip->card->shortname,
  271. device, 1, 0, &pcm);
  272. if (retval < 0)
  273. goto out;
  274. pcm->private_data = chip;
  275. pcm->private_free = snd_at73c213_pcm_free;
  276. pcm->info_flags = SNDRV_PCM_INFO_BLOCK_TRANSFER;
  277. strcpy(pcm->name, "at73c213");
  278. chip->pcm = pcm;
  279. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &at73c213_playback_ops);
  280. retval = snd_pcm_lib_preallocate_pages_for_all(chip->pcm,
  281. SNDRV_DMA_TYPE_DEV, &chip->ssc->pdev->dev,
  282. 64 * 1024, 64 * 1024);
  283. out:
  284. return retval;
  285. }
  286. static irqreturn_t snd_at73c213_interrupt(int irq, void *dev_id)
  287. {
  288. struct snd_at73c213 *chip = dev_id;
  289. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  290. u32 status;
  291. int offset;
  292. int block_size;
  293. int next_period;
  294. int retval = IRQ_NONE;
  295. spin_lock(&chip->lock);
  296. block_size = frames_to_bytes(runtime, runtime->period_size);
  297. status = ssc_readl(chip->ssc->regs, IMR);
  298. if (status & SSC_BIT(IMR_ENDTX)) {
  299. chip->period++;
  300. if (chip->period == runtime->periods)
  301. chip->period = 0;
  302. next_period = chip->period + 1;
  303. if (next_period == runtime->periods)
  304. next_period = 0;
  305. offset = block_size * next_period;
  306. ssc_writel(chip->ssc->regs, PDC_TNPR,
  307. (long)runtime->dma_addr + offset);
  308. ssc_writel(chip->ssc->regs, PDC_TNCR, runtime->period_size * 2);
  309. retval = IRQ_HANDLED;
  310. }
  311. ssc_readl(chip->ssc->regs, IMR);
  312. spin_unlock(&chip->lock);
  313. if (status & SSC_BIT(IMR_ENDTX))
  314. snd_pcm_period_elapsed(chip->substream);
  315. return retval;
  316. }
  317. /*
  318. * Mixer functions.
  319. */
  320. static int snd_at73c213_mono_get(struct snd_kcontrol *kcontrol,
  321. struct snd_ctl_elem_value *ucontrol)
  322. {
  323. struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
  324. int reg = kcontrol->private_value & 0xff;
  325. int shift = (kcontrol->private_value >> 8) & 0xff;
  326. int mask = (kcontrol->private_value >> 16) & 0xff;
  327. int invert = (kcontrol->private_value >> 24) & 0xff;
  328. spin_lock_irq(&chip->lock);
  329. ucontrol->value.integer.value[0] =
  330. (chip->reg_image[reg] >> shift) & mask;
  331. if (invert)
  332. ucontrol->value.integer.value[0] =
  333. mask - ucontrol->value.integer.value[0];
  334. spin_unlock_irq(&chip->lock);
  335. return 0;
  336. }
  337. static int snd_at73c213_mono_put(struct snd_kcontrol *kcontrol,
  338. struct snd_ctl_elem_value *ucontrol)
  339. {
  340. struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
  341. int reg = kcontrol->private_value & 0xff;
  342. int shift = (kcontrol->private_value >> 8) & 0xff;
  343. int mask = (kcontrol->private_value >> 16) & 0xff;
  344. int invert = (kcontrol->private_value >> 24) & 0xff;
  345. int change, retval;
  346. unsigned short val;
  347. val = (ucontrol->value.integer.value[0] & mask);
  348. if (invert)
  349. val = mask - val;
  350. val <<= shift;
  351. spin_lock_irq(&chip->lock);
  352. val = (chip->reg_image[reg] & ~(mask << shift)) | val;
  353. change = val != chip->reg_image[reg];
  354. retval = snd_at73c213_write_reg(chip, reg, val);
  355. spin_unlock_irq(&chip->lock);
  356. if (retval)
  357. return retval;
  358. return change;
  359. }
  360. static int snd_at73c213_stereo_info(struct snd_kcontrol *kcontrol,
  361. struct snd_ctl_elem_info *uinfo)
  362. {
  363. int mask = (kcontrol->private_value >> 24) & 0xff;
  364. if (mask == 1)
  365. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  366. else
  367. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  368. uinfo->count = 2;
  369. uinfo->value.integer.min = 0;
  370. uinfo->value.integer.max = mask;
  371. return 0;
  372. }
  373. static int snd_at73c213_stereo_get(struct snd_kcontrol *kcontrol,
  374. struct snd_ctl_elem_value *ucontrol)
  375. {
  376. struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
  377. int left_reg = kcontrol->private_value & 0xff;
  378. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  379. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  380. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  381. int mask = (kcontrol->private_value >> 24) & 0xff;
  382. int invert = (kcontrol->private_value >> 22) & 1;
  383. spin_lock_irq(&chip->lock);
  384. ucontrol->value.integer.value[0] =
  385. (chip->reg_image[left_reg] >> shift_left) & mask;
  386. ucontrol->value.integer.value[1] =
  387. (chip->reg_image[right_reg] >> shift_right) & mask;
  388. if (invert) {
  389. ucontrol->value.integer.value[0] =
  390. mask - ucontrol->value.integer.value[0];
  391. ucontrol->value.integer.value[1] =
  392. mask - ucontrol->value.integer.value[1];
  393. }
  394. spin_unlock_irq(&chip->lock);
  395. return 0;
  396. }
  397. static int snd_at73c213_stereo_put(struct snd_kcontrol *kcontrol,
  398. struct snd_ctl_elem_value *ucontrol)
  399. {
  400. struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
  401. int left_reg = kcontrol->private_value & 0xff;
  402. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  403. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  404. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  405. int mask = (kcontrol->private_value >> 24) & 0xff;
  406. int invert = (kcontrol->private_value >> 22) & 1;
  407. int change, retval;
  408. unsigned short val1, val2;
  409. val1 = ucontrol->value.integer.value[0] & mask;
  410. val2 = ucontrol->value.integer.value[1] & mask;
  411. if (invert) {
  412. val1 = mask - val1;
  413. val2 = mask - val2;
  414. }
  415. val1 <<= shift_left;
  416. val2 <<= shift_right;
  417. spin_lock_irq(&chip->lock);
  418. val1 = (chip->reg_image[left_reg] & ~(mask << shift_left)) | val1;
  419. val2 = (chip->reg_image[right_reg] & ~(mask << shift_right)) | val2;
  420. change = val1 != chip->reg_image[left_reg]
  421. || val2 != chip->reg_image[right_reg];
  422. retval = snd_at73c213_write_reg(chip, left_reg, val1);
  423. if (retval) {
  424. spin_unlock_irq(&chip->lock);
  425. goto out;
  426. }
  427. retval = snd_at73c213_write_reg(chip, right_reg, val2);
  428. if (retval) {
  429. spin_unlock_irq(&chip->lock);
  430. goto out;
  431. }
  432. spin_unlock_irq(&chip->lock);
  433. return change;
  434. out:
  435. return retval;
  436. }
  437. #define snd_at73c213_mono_switch_info snd_ctl_boolean_mono_info
  438. static int snd_at73c213_mono_switch_get(struct snd_kcontrol *kcontrol,
  439. struct snd_ctl_elem_value *ucontrol)
  440. {
  441. struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
  442. int reg = kcontrol->private_value & 0xff;
  443. int shift = (kcontrol->private_value >> 8) & 0xff;
  444. int invert = (kcontrol->private_value >> 24) & 0xff;
  445. spin_lock_irq(&chip->lock);
  446. ucontrol->value.integer.value[0] =
  447. (chip->reg_image[reg] >> shift) & 0x01;
  448. if (invert)
  449. ucontrol->value.integer.value[0] =
  450. 0x01 - ucontrol->value.integer.value[0];
  451. spin_unlock_irq(&chip->lock);
  452. return 0;
  453. }
  454. static int snd_at73c213_mono_switch_put(struct snd_kcontrol *kcontrol,
  455. struct snd_ctl_elem_value *ucontrol)
  456. {
  457. struct snd_at73c213 *chip = snd_kcontrol_chip(kcontrol);
  458. int reg = kcontrol->private_value & 0xff;
  459. int shift = (kcontrol->private_value >> 8) & 0xff;
  460. int mask = (kcontrol->private_value >> 16) & 0xff;
  461. int invert = (kcontrol->private_value >> 24) & 0xff;
  462. int change, retval;
  463. unsigned short val;
  464. if (ucontrol->value.integer.value[0])
  465. val = mask;
  466. else
  467. val = 0;
  468. if (invert)
  469. val = mask - val;
  470. val <<= shift;
  471. spin_lock_irq(&chip->lock);
  472. val |= (chip->reg_image[reg] & ~(mask << shift));
  473. change = val != chip->reg_image[reg];
  474. retval = snd_at73c213_write_reg(chip, reg, val);
  475. spin_unlock_irq(&chip->lock);
  476. if (retval)
  477. return retval;
  478. return change;
  479. }
  480. static int snd_at73c213_pa_volume_info(struct snd_kcontrol *kcontrol,
  481. struct snd_ctl_elem_info *uinfo)
  482. {
  483. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  484. uinfo->count = 1;
  485. uinfo->value.integer.min = 0;
  486. uinfo->value.integer.max = ((kcontrol->private_value >> 16) & 0xff) - 1;
  487. return 0;
  488. }
  489. static int snd_at73c213_line_capture_volume_info(
  490. struct snd_kcontrol *kcontrol,
  491. struct snd_ctl_elem_info *uinfo)
  492. {
  493. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  494. uinfo->count = 2;
  495. /* When inverted will give values 0x10001 => 0. */
  496. uinfo->value.integer.min = 14;
  497. uinfo->value.integer.max = 31;
  498. return 0;
  499. }
  500. static int snd_at73c213_aux_capture_volume_info(
  501. struct snd_kcontrol *kcontrol,
  502. struct snd_ctl_elem_info *uinfo)
  503. {
  504. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  505. uinfo->count = 1;
  506. /* When inverted will give values 0x10001 => 0. */
  507. uinfo->value.integer.min = 14;
  508. uinfo->value.integer.max = 31;
  509. return 0;
  510. }
  511. #define AT73C213_MONO_SWITCH(xname, xindex, reg, shift, mask, invert) \
  512. { \
  513. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  514. .name = xname, \
  515. .index = xindex, \
  516. .info = snd_at73c213_mono_switch_info, \
  517. .get = snd_at73c213_mono_switch_get, \
  518. .put = snd_at73c213_mono_switch_put, \
  519. .private_value = (reg | (shift << 8) | (mask << 16) | (invert << 24)) \
  520. }
  521. #define AT73C213_STEREO(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  522. { \
  523. .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  524. .name = xname, \
  525. .index = xindex, \
  526. .info = snd_at73c213_stereo_info, \
  527. .get = snd_at73c213_stereo_get, \
  528. .put = snd_at73c213_stereo_put, \
  529. .private_value = (left_reg | (right_reg << 8) \
  530. | (shift_left << 16) | (shift_right << 19) \
  531. | (mask << 24) | (invert << 22)) \
  532. }
  533. static struct snd_kcontrol_new snd_at73c213_controls[] __devinitdata = {
  534. AT73C213_STEREO("Master Playback Volume", 0, DAC_LMPG, DAC_RMPG, 0, 0, 0x1f, 1),
  535. AT73C213_STEREO("Master Playback Switch", 0, DAC_LMPG, DAC_RMPG, 5, 5, 1, 1),
  536. AT73C213_STEREO("PCM Playback Volume", 0, DAC_LLOG, DAC_RLOG, 0, 0, 0x1f, 1),
  537. AT73C213_STEREO("PCM Playback Switch", 0, DAC_LLOG, DAC_RLOG, 5, 5, 1, 1),
  538. AT73C213_MONO_SWITCH("Mono PA Playback Switch", 0, DAC_CTRL, DAC_CTRL_ONPADRV,
  539. 0x01, 0),
  540. {
  541. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  542. .name = "PA Playback Volume",
  543. .index = 0,
  544. .info = snd_at73c213_pa_volume_info,
  545. .get = snd_at73c213_mono_get,
  546. .put = snd_at73c213_mono_put,
  547. .private_value = PA_CTRL | (PA_CTRL_APAGAIN << 8) | \
  548. (0x0f << 16) | (1 << 24),
  549. },
  550. AT73C213_MONO_SWITCH("PA High Gain Playback Switch", 0, PA_CTRL, PA_CTRL_APALP,
  551. 0x01, 1),
  552. AT73C213_MONO_SWITCH("PA Playback Switch", 0, PA_CTRL, PA_CTRL_APAON, 0x01, 0),
  553. {
  554. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  555. .name = "Aux Capture Volume",
  556. .index = 0,
  557. .info = snd_at73c213_aux_capture_volume_info,
  558. .get = snd_at73c213_mono_get,
  559. .put = snd_at73c213_mono_put,
  560. .private_value = DAC_AUXG | (0 << 8) | (0x1f << 16) | (1 << 24),
  561. },
  562. AT73C213_MONO_SWITCH("Aux Capture Switch", 0, DAC_CTRL, DAC_CTRL_ONAUXIN,
  563. 0x01, 0),
  564. {
  565. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  566. .name = "Line Capture Volume",
  567. .index = 0,
  568. .info = snd_at73c213_line_capture_volume_info,
  569. .get = snd_at73c213_stereo_get,
  570. .put = snd_at73c213_stereo_put,
  571. .private_value = DAC_LLIG | (DAC_RLIG << 8) | (0 << 16) | (0 << 19)
  572. | (0x1f << 24) | (1 << 22),
  573. },
  574. AT73C213_MONO_SWITCH("Line Capture Switch", 0, DAC_CTRL, 0, 0x03, 0),
  575. };
  576. static int __devinit snd_at73c213_mixer(struct snd_at73c213 *chip)
  577. {
  578. struct snd_card *card;
  579. int errval, idx;
  580. if (chip == NULL || chip->pcm == NULL)
  581. return -EINVAL;
  582. card = chip->card;
  583. strcpy(card->mixername, chip->pcm->name);
  584. for (idx = 0; idx < ARRAY_SIZE(snd_at73c213_controls); idx++) {
  585. errval = snd_ctl_add(card,
  586. snd_ctl_new1(&snd_at73c213_controls[idx],
  587. chip));
  588. if (errval < 0)
  589. goto cleanup;
  590. }
  591. return 0;
  592. cleanup:
  593. for (idx = 1; idx < ARRAY_SIZE(snd_at73c213_controls) + 1; idx++) {
  594. struct snd_kcontrol *kctl;
  595. kctl = snd_ctl_find_numid(card, idx);
  596. if (kctl)
  597. snd_ctl_remove(card, kctl);
  598. }
  599. return errval;
  600. }
  601. /*
  602. * Device functions
  603. */
  604. static int snd_at73c213_ssc_init(struct snd_at73c213 *chip)
  605. {
  606. /*
  607. * Continuous clock output.
  608. * Starts on falling TF.
  609. * Delay 1 cycle (1 bit).
  610. * Periode is 16 bit (16 - 1).
  611. */
  612. ssc_writel(chip->ssc->regs, TCMR,
  613. SSC_BF(TCMR_CKO, 1)
  614. | SSC_BF(TCMR_START, 4)
  615. | SSC_BF(TCMR_STTDLY, 1)
  616. | SSC_BF(TCMR_PERIOD, 16 - 1));
  617. /*
  618. * Data length is 16 bit (16 - 1).
  619. * Transmit MSB first.
  620. * Transmit 2 words each transfer.
  621. * Frame sync length is 16 bit (16 - 1).
  622. * Frame starts on negative pulse.
  623. */
  624. ssc_writel(chip->ssc->regs, TFMR,
  625. SSC_BF(TFMR_DATLEN, 16 - 1)
  626. | SSC_BIT(TFMR_MSBF)
  627. | SSC_BF(TFMR_DATNB, 1)
  628. | SSC_BF(TFMR_FSLEN, 16 - 1)
  629. | SSC_BF(TFMR_FSOS, 1));
  630. return 0;
  631. }
  632. static int snd_at73c213_chip_init(struct snd_at73c213 *chip)
  633. {
  634. int retval;
  635. unsigned char dac_ctrl = 0;
  636. retval = snd_at73c213_set_bitrate(chip);
  637. if (retval)
  638. goto out;
  639. /* Enable DAC master clock. */
  640. clk_enable(chip->board->dac_clk);
  641. /* Initialize at73c213 on SPI bus. */
  642. retval = snd_at73c213_write_reg(chip, DAC_RST, 0x04);
  643. if (retval)
  644. goto out_clk;
  645. msleep(1);
  646. retval = snd_at73c213_write_reg(chip, DAC_RST, 0x03);
  647. if (retval)
  648. goto out_clk;
  649. /* Precharge everything. */
  650. retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0xff);
  651. if (retval)
  652. goto out_clk;
  653. retval = snd_at73c213_write_reg(chip, PA_CTRL, (1<<PA_CTRL_APAPRECH));
  654. if (retval)
  655. goto out_clk;
  656. retval = snd_at73c213_write_reg(chip, DAC_CTRL,
  657. (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR));
  658. if (retval)
  659. goto out_clk;
  660. msleep(50);
  661. /* Stop precharging PA. */
  662. retval = snd_at73c213_write_reg(chip, PA_CTRL,
  663. (1<<PA_CTRL_APALP) | 0x0f);
  664. if (retval)
  665. goto out_clk;
  666. msleep(450);
  667. /* Stop precharging DAC, turn on master power. */
  668. retval = snd_at73c213_write_reg(chip, DAC_PRECH, (1<<DAC_PRECH_ONMSTR));
  669. if (retval)
  670. goto out_clk;
  671. msleep(1);
  672. /* Turn on DAC. */
  673. dac_ctrl = (1<<DAC_CTRL_ONDACL) | (1<<DAC_CTRL_ONDACR)
  674. | (1<<DAC_CTRL_ONLNOL) | (1<<DAC_CTRL_ONLNOR);
  675. retval = snd_at73c213_write_reg(chip, DAC_CTRL, dac_ctrl);
  676. if (retval)
  677. goto out_clk;
  678. /* Mute sound. */
  679. retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
  680. if (retval)
  681. goto out_clk;
  682. retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
  683. if (retval)
  684. goto out_clk;
  685. retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
  686. if (retval)
  687. goto out_clk;
  688. retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
  689. if (retval)
  690. goto out_clk;
  691. retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
  692. if (retval)
  693. goto out_clk;
  694. retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
  695. if (retval)
  696. goto out_clk;
  697. retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
  698. if (retval)
  699. goto out_clk;
  700. /* Enable I2S device, i.e. clock output. */
  701. ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
  702. goto out;
  703. out_clk:
  704. clk_disable(chip->board->dac_clk);
  705. out:
  706. return retval;
  707. }
  708. static int snd_at73c213_dev_free(struct snd_device *device)
  709. {
  710. struct snd_at73c213 *chip = device->device_data;
  711. ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
  712. if (chip->irq >= 0) {
  713. free_irq(chip->irq, chip);
  714. chip->irq = -1;
  715. }
  716. return 0;
  717. }
  718. static int __devinit snd_at73c213_dev_init(struct snd_card *card,
  719. struct spi_device *spi)
  720. {
  721. static struct snd_device_ops ops = {
  722. .dev_free = snd_at73c213_dev_free,
  723. };
  724. struct snd_at73c213 *chip = get_chip(card);
  725. int irq, retval;
  726. irq = chip->ssc->irq;
  727. if (irq < 0)
  728. return irq;
  729. spin_lock_init(&chip->lock);
  730. chip->card = card;
  731. chip->irq = -1;
  732. retval = request_irq(irq, snd_at73c213_interrupt, 0, "at73c213", chip);
  733. if (retval) {
  734. dev_dbg(&chip->spi->dev, "unable to request irq %d\n", irq);
  735. goto out;
  736. }
  737. chip->irq = irq;
  738. memcpy(&chip->reg_image, &snd_at73c213_original_image,
  739. sizeof(snd_at73c213_original_image));
  740. retval = snd_at73c213_ssc_init(chip);
  741. if (retval)
  742. goto out_irq;
  743. retval = snd_at73c213_chip_init(chip);
  744. if (retval)
  745. goto out_irq;
  746. retval = snd_at73c213_pcm_new(chip, 0);
  747. if (retval)
  748. goto out_irq;
  749. retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  750. if (retval)
  751. goto out_irq;
  752. retval = snd_at73c213_mixer(chip);
  753. if (retval)
  754. goto out_snd_dev;
  755. snd_card_set_dev(card, &spi->dev);
  756. goto out;
  757. out_snd_dev:
  758. snd_device_free(card, chip);
  759. out_irq:
  760. free_irq(chip->irq, chip);
  761. chip->irq = -1;
  762. out:
  763. return retval;
  764. }
  765. static int snd_at73c213_probe(struct spi_device *spi)
  766. {
  767. struct snd_card *card;
  768. struct snd_at73c213 *chip;
  769. struct at73c213_board_info *board;
  770. int retval;
  771. char id[16];
  772. board = spi->dev.platform_data;
  773. if (!board) {
  774. dev_dbg(&spi->dev, "no platform_data\n");
  775. return -ENXIO;
  776. }
  777. if (!board->dac_clk) {
  778. dev_dbg(&spi->dev, "no DAC clk\n");
  779. return -ENXIO;
  780. }
  781. if (IS_ERR(board->dac_clk)) {
  782. dev_dbg(&spi->dev, "no DAC clk\n");
  783. return PTR_ERR(board->dac_clk);
  784. }
  785. retval = -ENOMEM;
  786. /* Allocate "card" using some unused identifiers. */
  787. snprintf(id, sizeof id, "at73c213_%d", board->ssc_id);
  788. card = snd_card_new(-1, id, THIS_MODULE, sizeof(struct snd_at73c213));
  789. if (!card)
  790. goto out;
  791. chip = card->private_data;
  792. chip->spi = spi;
  793. chip->board = board;
  794. chip->ssc = ssc_request(board->ssc_id);
  795. if (IS_ERR(chip->ssc)) {
  796. dev_dbg(&spi->dev, "could not get ssc%d device\n",
  797. board->ssc_id);
  798. retval = PTR_ERR(chip->ssc);
  799. goto out_card;
  800. }
  801. retval = snd_at73c213_dev_init(card, spi);
  802. if (retval)
  803. goto out_ssc;
  804. strcpy(card->driver, "at73c213");
  805. strcpy(card->shortname, board->shortname);
  806. sprintf(card->longname, "%s on irq %d", card->shortname, chip->irq);
  807. retval = snd_card_register(card);
  808. if (retval)
  809. goto out_ssc;
  810. dev_set_drvdata(&spi->dev, card);
  811. goto out;
  812. out_ssc:
  813. ssc_free(chip->ssc);
  814. out_card:
  815. snd_card_free(card);
  816. out:
  817. return retval;
  818. }
  819. static int __devexit snd_at73c213_remove(struct spi_device *spi)
  820. {
  821. struct snd_card *card = dev_get_drvdata(&spi->dev);
  822. struct snd_at73c213 *chip = card->private_data;
  823. int retval;
  824. /* Stop playback. */
  825. ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
  826. /* Mute sound. */
  827. retval = snd_at73c213_write_reg(chip, DAC_LMPG, 0x3f);
  828. if (retval)
  829. goto out;
  830. retval = snd_at73c213_write_reg(chip, DAC_RMPG, 0x3f);
  831. if (retval)
  832. goto out;
  833. retval = snd_at73c213_write_reg(chip, DAC_LLOG, 0x3f);
  834. if (retval)
  835. goto out;
  836. retval = snd_at73c213_write_reg(chip, DAC_RLOG, 0x3f);
  837. if (retval)
  838. goto out;
  839. retval = snd_at73c213_write_reg(chip, DAC_LLIG, 0x11);
  840. if (retval)
  841. goto out;
  842. retval = snd_at73c213_write_reg(chip, DAC_RLIG, 0x11);
  843. if (retval)
  844. goto out;
  845. retval = snd_at73c213_write_reg(chip, DAC_AUXG, 0x11);
  846. if (retval)
  847. goto out;
  848. /* Turn off PA. */
  849. retval = snd_at73c213_write_reg(chip, PA_CTRL,
  850. chip->reg_image[PA_CTRL] | 0x0f);
  851. if (retval)
  852. goto out;
  853. msleep(10);
  854. retval = snd_at73c213_write_reg(chip, PA_CTRL,
  855. (1 << PA_CTRL_APALP) | 0x0f);
  856. if (retval)
  857. goto out;
  858. /* Turn off external DAC. */
  859. retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x0c);
  860. if (retval)
  861. goto out;
  862. msleep(2);
  863. retval = snd_at73c213_write_reg(chip, DAC_CTRL, 0x00);
  864. if (retval)
  865. goto out;
  866. /* Turn off master power. */
  867. retval = snd_at73c213_write_reg(chip, DAC_PRECH, 0x00);
  868. if (retval)
  869. goto out;
  870. out:
  871. /* Stop DAC master clock. */
  872. clk_disable(chip->board->dac_clk);
  873. ssc_free(chip->ssc);
  874. snd_card_free(card);
  875. dev_set_drvdata(&spi->dev, NULL);
  876. return 0;
  877. }
  878. #ifdef CONFIG_PM
  879. static int snd_at73c213_suspend(struct spi_device *spi, pm_message_t msg)
  880. {
  881. struct snd_card *card = dev_get_drvdata(&spi->dev);
  882. struct snd_at73c213 *chip = card->private_data;
  883. ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXDIS));
  884. clk_disable(chip->board->dac_clk);
  885. return 0;
  886. }
  887. static int snd_at73c213_resume(struct spi_device *spi)
  888. {
  889. struct snd_card *card = dev_get_drvdata(&spi->dev);
  890. struct snd_at73c213 *chip = card->private_data;
  891. clk_enable(chip->board->dac_clk);
  892. ssc_writel(chip->ssc->regs, CR, SSC_BIT(CR_TXEN));
  893. return 0;
  894. }
  895. #else
  896. #define snd_at73c213_suspend NULL
  897. #define snd_at73c213_resume NULL
  898. #endif
  899. static struct spi_driver at73c213_driver = {
  900. .driver = {
  901. .name = "at73c213",
  902. },
  903. .probe = snd_at73c213_probe,
  904. .suspend = snd_at73c213_suspend,
  905. .resume = snd_at73c213_resume,
  906. .remove = __devexit_p(snd_at73c213_remove),
  907. };
  908. static int __init at73c213_init(void)
  909. {
  910. return spi_register_driver(&at73c213_driver);
  911. }
  912. module_init(at73c213_init);
  913. static void __exit at73c213_exit(void)
  914. {
  915. spi_unregister_driver(&at73c213_driver);
  916. }
  917. module_exit(at73c213_exit);
  918. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  919. MODULE_DESCRIPTION("Sound driver for AT73C213 with Atmel SSC");
  920. MODULE_LICENSE("GPL");