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