at73c213.c 28 KB

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