at73c213.c 28 KB

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