at73c213.c 28 KB

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