at73c213.c 28 KB

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