dw_hdmi-ahb-audio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * DesignWare HDMI audio driver
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Written and tested against the Designware HDMI Tx found in iMX6.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <drm/bridge/dw_hdmi.h>
  15. #include <drm/drm_edid.h>
  16. #include <sound/asoundef.h>
  17. #include <sound/core.h>
  18. #include <sound/initval.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_drm_eld.h>
  21. #include <sound/pcm_iec958.h>
  22. #include "dw_hdmi-audio.h"
  23. #define DRIVER_NAME "dw-hdmi-ahb-audio"
  24. /* Provide some bits rather than bit offsets */
  25. enum {
  26. HDMI_AHB_DMA_CONF0_SW_FIFO_RST = BIT(7),
  27. HDMI_AHB_DMA_CONF0_EN_HLOCK = BIT(3),
  28. HDMI_AHB_DMA_START_START = BIT(0),
  29. HDMI_AHB_DMA_STOP_STOP = BIT(0),
  30. HDMI_IH_MUTE_AHBDMAAUD_STAT0_ERROR = BIT(5),
  31. HDMI_IH_MUTE_AHBDMAAUD_STAT0_LOST = BIT(4),
  32. HDMI_IH_MUTE_AHBDMAAUD_STAT0_RETRY = BIT(3),
  33. HDMI_IH_MUTE_AHBDMAAUD_STAT0_DONE = BIT(2),
  34. HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFFULL = BIT(1),
  35. HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFEMPTY = BIT(0),
  36. HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL =
  37. HDMI_IH_MUTE_AHBDMAAUD_STAT0_ERROR |
  38. HDMI_IH_MUTE_AHBDMAAUD_STAT0_LOST |
  39. HDMI_IH_MUTE_AHBDMAAUD_STAT0_RETRY |
  40. HDMI_IH_MUTE_AHBDMAAUD_STAT0_DONE |
  41. HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFFULL |
  42. HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFEMPTY,
  43. HDMI_IH_AHBDMAAUD_STAT0_ERROR = BIT(5),
  44. HDMI_IH_AHBDMAAUD_STAT0_LOST = BIT(4),
  45. HDMI_IH_AHBDMAAUD_STAT0_RETRY = BIT(3),
  46. HDMI_IH_AHBDMAAUD_STAT0_DONE = BIT(2),
  47. HDMI_IH_AHBDMAAUD_STAT0_BUFFFULL = BIT(1),
  48. HDMI_IH_AHBDMAAUD_STAT0_BUFFEMPTY = BIT(0),
  49. HDMI_IH_AHBDMAAUD_STAT0_ALL =
  50. HDMI_IH_AHBDMAAUD_STAT0_ERROR |
  51. HDMI_IH_AHBDMAAUD_STAT0_LOST |
  52. HDMI_IH_AHBDMAAUD_STAT0_RETRY |
  53. HDMI_IH_AHBDMAAUD_STAT0_DONE |
  54. HDMI_IH_AHBDMAAUD_STAT0_BUFFFULL |
  55. HDMI_IH_AHBDMAAUD_STAT0_BUFFEMPTY,
  56. HDMI_AHB_DMA_CONF0_INCR16 = 2 << 1,
  57. HDMI_AHB_DMA_CONF0_INCR8 = 1 << 1,
  58. HDMI_AHB_DMA_CONF0_INCR4 = 0,
  59. HDMI_AHB_DMA_CONF0_BURST_MODE = BIT(0),
  60. HDMI_AHB_DMA_MASK_DONE = BIT(7),
  61. HDMI_REVISION_ID = 0x0001,
  62. HDMI_IH_AHBDMAAUD_STAT0 = 0x0109,
  63. HDMI_IH_MUTE_AHBDMAAUD_STAT0 = 0x0189,
  64. HDMI_FC_AUDICONF2 = 0x1027,
  65. HDMI_FC_AUDSCONF = 0x1063,
  66. HDMI_FC_AUDSCONF_LAYOUT1 = 1 << 0,
  67. HDMI_FC_AUDSCONF_LAYOUT0 = 0 << 0,
  68. HDMI_AHB_DMA_CONF0 = 0x3600,
  69. HDMI_AHB_DMA_START = 0x3601,
  70. HDMI_AHB_DMA_STOP = 0x3602,
  71. HDMI_AHB_DMA_THRSLD = 0x3603,
  72. HDMI_AHB_DMA_STRADDR0 = 0x3604,
  73. HDMI_AHB_DMA_STPADDR0 = 0x3608,
  74. HDMI_AHB_DMA_MASK = 0x3614,
  75. HDMI_AHB_DMA_POL = 0x3615,
  76. HDMI_AHB_DMA_CONF1 = 0x3616,
  77. HDMI_AHB_DMA_BUFFPOL = 0x361a,
  78. };
  79. struct dw_hdmi_channel_conf {
  80. u8 conf1;
  81. u8 ca;
  82. };
  83. /*
  84. * The default mapping of ALSA channels to HDMI channels and speaker
  85. * allocation bits. Note that we can't do channel remapping here -
  86. * channels must be in the same order.
  87. *
  88. * Mappings for alsa-lib pcm/surround*.conf files:
  89. *
  90. * Front Sur4.0 Sur4.1 Sur5.0 Sur5.1 Sur7.1
  91. * Channels 2 4 6 6 6 8
  92. *
  93. * Our mapping from ALSA channel to CEA686D speaker name and HDMI channel:
  94. *
  95. * Number of ALSA channels
  96. * ALSA Channel 2 3 4 5 6 7 8
  97. * 0 FL:0 = = = = = =
  98. * 1 FR:1 = = = = = =
  99. * 2 FC:3 RL:4 LFE:2 = = =
  100. * 3 RR:5 RL:4 FC:3 = =
  101. * 4 RR:5 RL:4 = =
  102. * 5 RR:5 = =
  103. * 6 RC:6 =
  104. * 7 RLC/FRC RLC/FRC
  105. */
  106. static struct dw_hdmi_channel_conf default_hdmi_channel_config[7] = {
  107. { 0x03, 0x00 }, /* FL,FR */
  108. { 0x0b, 0x02 }, /* FL,FR,FC */
  109. { 0x33, 0x08 }, /* FL,FR,RL,RR */
  110. { 0x37, 0x09 }, /* FL,FR,LFE,RL,RR */
  111. { 0x3f, 0x0b }, /* FL,FR,LFE,FC,RL,RR */
  112. { 0x7f, 0x0f }, /* FL,FR,LFE,FC,RL,RR,RC */
  113. { 0xff, 0x13 }, /* FL,FR,LFE,FC,RL,RR,[FR]RC,[FR]LC */
  114. };
  115. struct snd_dw_hdmi {
  116. struct snd_card *card;
  117. struct snd_pcm *pcm;
  118. spinlock_t lock;
  119. struct dw_hdmi_audio_data data;
  120. struct snd_pcm_substream *substream;
  121. void (*reformat)(struct snd_dw_hdmi *, size_t, size_t);
  122. void *buf_src;
  123. void *buf_dst;
  124. dma_addr_t buf_addr;
  125. unsigned buf_offset;
  126. unsigned buf_period;
  127. unsigned buf_size;
  128. unsigned channels;
  129. u8 revision;
  130. u8 iec_offset;
  131. u8 cs[192][8];
  132. };
  133. static void dw_hdmi_writel(u32 val, void __iomem *ptr)
  134. {
  135. writeb_relaxed(val, ptr);
  136. writeb_relaxed(val >> 8, ptr + 1);
  137. writeb_relaxed(val >> 16, ptr + 2);
  138. writeb_relaxed(val >> 24, ptr + 3);
  139. }
  140. /*
  141. * Convert to hardware format: The userspace buffer contains IEC958 samples,
  142. * with the PCUV bits in bits 31..28 and audio samples in bits 27..4. We
  143. * need these to be in bits 27..24, with the IEC B bit in bit 28, and audio
  144. * samples in 23..0.
  145. *
  146. * Default preamble in bits 3..0: 8 = block start, 4 = even 2 = odd
  147. *
  148. * Ideally, we could do with having the data properly formatted in userspace.
  149. */
  150. static void dw_hdmi_reformat_iec958(struct snd_dw_hdmi *dw,
  151. size_t offset, size_t bytes)
  152. {
  153. u32 *src = dw->buf_src + offset;
  154. u32 *dst = dw->buf_dst + offset;
  155. u32 *end = dw->buf_src + offset + bytes;
  156. do {
  157. u32 b, sample = *src++;
  158. b = (sample & 8) << (28 - 3);
  159. sample >>= 4;
  160. *dst++ = sample | b;
  161. } while (src < end);
  162. }
  163. static u32 parity(u32 sample)
  164. {
  165. sample ^= sample >> 16;
  166. sample ^= sample >> 8;
  167. sample ^= sample >> 4;
  168. sample ^= sample >> 2;
  169. sample ^= sample >> 1;
  170. return (sample & 1) << 27;
  171. }
  172. static void dw_hdmi_reformat_s24(struct snd_dw_hdmi *dw,
  173. size_t offset, size_t bytes)
  174. {
  175. u32 *src = dw->buf_src + offset;
  176. u32 *dst = dw->buf_dst + offset;
  177. u32 *end = dw->buf_src + offset + bytes;
  178. do {
  179. unsigned i;
  180. u8 *cs;
  181. cs = dw->cs[dw->iec_offset++];
  182. if (dw->iec_offset >= 192)
  183. dw->iec_offset = 0;
  184. i = dw->channels;
  185. do {
  186. u32 sample = *src++;
  187. sample &= ~0xff000000;
  188. sample |= *cs++ << 24;
  189. sample |= parity(sample & ~0xf8000000);
  190. *dst++ = sample;
  191. } while (--i);
  192. } while (src < end);
  193. }
  194. static void dw_hdmi_create_cs(struct snd_dw_hdmi *dw,
  195. struct snd_pcm_runtime *runtime)
  196. {
  197. u8 cs[4];
  198. unsigned ch, i, j;
  199. snd_pcm_create_iec958_consumer(runtime, cs, sizeof(cs));
  200. memset(dw->cs, 0, sizeof(dw->cs));
  201. for (ch = 0; ch < 8; ch++) {
  202. cs[2] &= ~IEC958_AES2_CON_CHANNEL;
  203. cs[2] |= (ch + 1) << 4;
  204. for (i = 0; i < ARRAY_SIZE(cs); i++) {
  205. unsigned c = cs[i];
  206. for (j = 0; j < 8; j++, c >>= 1)
  207. dw->cs[i * 8 + j][ch] = (c & 1) << 2;
  208. }
  209. }
  210. dw->cs[0][0] |= BIT(4);
  211. }
  212. static void dw_hdmi_start_dma(struct snd_dw_hdmi *dw)
  213. {
  214. void __iomem *base = dw->data.base;
  215. unsigned offset = dw->buf_offset;
  216. unsigned period = dw->buf_period;
  217. u32 start, stop;
  218. dw->reformat(dw, offset, period);
  219. /* Clear all irqs before enabling irqs and starting DMA */
  220. writeb_relaxed(HDMI_IH_AHBDMAAUD_STAT0_ALL,
  221. base + HDMI_IH_AHBDMAAUD_STAT0);
  222. start = dw->buf_addr + offset;
  223. stop = start + period - 1;
  224. /* Setup the hardware start/stop addresses */
  225. dw_hdmi_writel(start, base + HDMI_AHB_DMA_STRADDR0);
  226. dw_hdmi_writel(stop, base + HDMI_AHB_DMA_STPADDR0);
  227. writeb_relaxed((u8)~HDMI_AHB_DMA_MASK_DONE, base + HDMI_AHB_DMA_MASK);
  228. writeb(HDMI_AHB_DMA_START_START, base + HDMI_AHB_DMA_START);
  229. offset += period;
  230. if (offset >= dw->buf_size)
  231. offset = 0;
  232. dw->buf_offset = offset;
  233. }
  234. static void dw_hdmi_stop_dma(struct snd_dw_hdmi *dw)
  235. {
  236. /* Disable interrupts before disabling DMA */
  237. writeb_relaxed(~0, dw->data.base + HDMI_AHB_DMA_MASK);
  238. writeb_relaxed(HDMI_AHB_DMA_STOP_STOP, dw->data.base + HDMI_AHB_DMA_STOP);
  239. }
  240. static irqreturn_t snd_dw_hdmi_irq(int irq, void *data)
  241. {
  242. struct snd_dw_hdmi *dw = data;
  243. struct snd_pcm_substream *substream;
  244. unsigned stat;
  245. stat = readb_relaxed(dw->data.base + HDMI_IH_AHBDMAAUD_STAT0);
  246. if (!stat)
  247. return IRQ_NONE;
  248. writeb_relaxed(stat, dw->data.base + HDMI_IH_AHBDMAAUD_STAT0);
  249. substream = dw->substream;
  250. if (stat & HDMI_IH_AHBDMAAUD_STAT0_DONE && substream) {
  251. snd_pcm_period_elapsed(substream);
  252. spin_lock(&dw->lock);
  253. if (dw->substream)
  254. dw_hdmi_start_dma(dw);
  255. spin_unlock(&dw->lock);
  256. }
  257. return IRQ_HANDLED;
  258. }
  259. static struct snd_pcm_hardware dw_hdmi_hw = {
  260. .info = SNDRV_PCM_INFO_INTERLEAVED |
  261. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  262. SNDRV_PCM_INFO_MMAP |
  263. SNDRV_PCM_INFO_MMAP_VALID,
  264. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE |
  265. SNDRV_PCM_FMTBIT_S24_LE,
  266. .rates = SNDRV_PCM_RATE_32000 |
  267. SNDRV_PCM_RATE_44100 |
  268. SNDRV_PCM_RATE_48000 |
  269. SNDRV_PCM_RATE_88200 |
  270. SNDRV_PCM_RATE_96000 |
  271. SNDRV_PCM_RATE_176400 |
  272. SNDRV_PCM_RATE_192000,
  273. .channels_min = 2,
  274. .channels_max = 8,
  275. .buffer_bytes_max = 1024 * 1024,
  276. .period_bytes_min = 256,
  277. .period_bytes_max = 8192, /* ERR004323: must limit to 8k */
  278. .periods_min = 2,
  279. .periods_max = 16,
  280. .fifo_size = 0,
  281. };
  282. static int dw_hdmi_open(struct snd_pcm_substream *substream)
  283. {
  284. struct snd_pcm_runtime *runtime = substream->runtime;
  285. struct snd_dw_hdmi *dw = substream->private_data;
  286. void __iomem *base = dw->data.base;
  287. int ret;
  288. runtime->hw = dw_hdmi_hw;
  289. ret = snd_pcm_hw_constraint_eld(runtime, dw->data.eld);
  290. if (ret < 0)
  291. return ret;
  292. ret = snd_pcm_limit_hw_rates(runtime);
  293. if (ret < 0)
  294. return ret;
  295. ret = snd_pcm_hw_constraint_integer(runtime,
  296. SNDRV_PCM_HW_PARAM_PERIODS);
  297. if (ret < 0)
  298. return ret;
  299. /* Limit the buffer size to the size of the preallocated buffer */
  300. ret = snd_pcm_hw_constraint_minmax(runtime,
  301. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  302. 0, substream->dma_buffer.bytes);
  303. if (ret < 0)
  304. return ret;
  305. /* Clear FIFO */
  306. writeb_relaxed(HDMI_AHB_DMA_CONF0_SW_FIFO_RST,
  307. base + HDMI_AHB_DMA_CONF0);
  308. /* Configure interrupt polarities */
  309. writeb_relaxed(~0, base + HDMI_AHB_DMA_POL);
  310. writeb_relaxed(~0, base + HDMI_AHB_DMA_BUFFPOL);
  311. /* Keep interrupts masked, and clear any pending */
  312. writeb_relaxed(~0, base + HDMI_AHB_DMA_MASK);
  313. writeb_relaxed(~0, base + HDMI_IH_AHBDMAAUD_STAT0);
  314. ret = request_irq(dw->data.irq, snd_dw_hdmi_irq, IRQF_SHARED,
  315. "dw-hdmi-audio", dw);
  316. if (ret)
  317. return ret;
  318. /* Un-mute done interrupt */
  319. writeb_relaxed(HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL &
  320. ~HDMI_IH_MUTE_AHBDMAAUD_STAT0_DONE,
  321. base + HDMI_IH_MUTE_AHBDMAAUD_STAT0);
  322. return 0;
  323. }
  324. static int dw_hdmi_close(struct snd_pcm_substream *substream)
  325. {
  326. struct snd_dw_hdmi *dw = substream->private_data;
  327. /* Mute all interrupts */
  328. writeb_relaxed(HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL,
  329. dw->data.base + HDMI_IH_MUTE_AHBDMAAUD_STAT0);
  330. free_irq(dw->data.irq, dw);
  331. return 0;
  332. }
  333. static int dw_hdmi_hw_free(struct snd_pcm_substream *substream)
  334. {
  335. return snd_pcm_lib_free_vmalloc_buffer(substream);
  336. }
  337. static int dw_hdmi_hw_params(struct snd_pcm_substream *substream,
  338. struct snd_pcm_hw_params *params)
  339. {
  340. /* Allocate the PCM runtime buffer, which is exposed to userspace. */
  341. return snd_pcm_lib_alloc_vmalloc_buffer(substream,
  342. params_buffer_bytes(params));
  343. }
  344. static int dw_hdmi_prepare(struct snd_pcm_substream *substream)
  345. {
  346. struct snd_pcm_runtime *runtime = substream->runtime;
  347. struct snd_dw_hdmi *dw = substream->private_data;
  348. u8 threshold, conf0, conf1, layout, ca;
  349. /* Setup as per 3.0.5 FSL 4.1.0 BSP */
  350. switch (dw->revision) {
  351. case 0x0a:
  352. conf0 = HDMI_AHB_DMA_CONF0_BURST_MODE |
  353. HDMI_AHB_DMA_CONF0_INCR4;
  354. if (runtime->channels == 2)
  355. threshold = 126;
  356. else
  357. threshold = 124;
  358. break;
  359. case 0x1a:
  360. conf0 = HDMI_AHB_DMA_CONF0_BURST_MODE |
  361. HDMI_AHB_DMA_CONF0_INCR8;
  362. threshold = 128;
  363. break;
  364. default:
  365. /* NOTREACHED */
  366. return -EINVAL;
  367. }
  368. dw_hdmi_set_sample_rate(dw->data.hdmi, runtime->rate);
  369. /* Minimum number of bytes in the fifo. */
  370. runtime->hw.fifo_size = threshold * 32;
  371. conf0 |= HDMI_AHB_DMA_CONF0_EN_HLOCK;
  372. conf1 = default_hdmi_channel_config[runtime->channels - 2].conf1;
  373. ca = default_hdmi_channel_config[runtime->channels - 2].ca;
  374. /*
  375. * For >2 channel PCM audio, we need to select layout 1
  376. * and set an appropriate channel map.
  377. */
  378. if (runtime->channels > 2)
  379. layout = HDMI_FC_AUDSCONF_LAYOUT1;
  380. else
  381. layout = HDMI_FC_AUDSCONF_LAYOUT0;
  382. writeb_relaxed(threshold, dw->data.base + HDMI_AHB_DMA_THRSLD);
  383. writeb_relaxed(conf0, dw->data.base + HDMI_AHB_DMA_CONF0);
  384. writeb_relaxed(conf1, dw->data.base + HDMI_AHB_DMA_CONF1);
  385. writeb_relaxed(layout, dw->data.base + HDMI_FC_AUDSCONF);
  386. writeb_relaxed(ca, dw->data.base + HDMI_FC_AUDICONF2);
  387. switch (runtime->format) {
  388. case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
  389. dw->reformat = dw_hdmi_reformat_iec958;
  390. break;
  391. case SNDRV_PCM_FORMAT_S24_LE:
  392. dw_hdmi_create_cs(dw, runtime);
  393. dw->reformat = dw_hdmi_reformat_s24;
  394. break;
  395. }
  396. dw->iec_offset = 0;
  397. dw->channels = runtime->channels;
  398. dw->buf_src = runtime->dma_area;
  399. dw->buf_dst = substream->dma_buffer.area;
  400. dw->buf_addr = substream->dma_buffer.addr;
  401. dw->buf_period = snd_pcm_lib_period_bytes(substream);
  402. dw->buf_size = snd_pcm_lib_buffer_bytes(substream);
  403. return 0;
  404. }
  405. static int dw_hdmi_trigger(struct snd_pcm_substream *substream, int cmd)
  406. {
  407. struct snd_dw_hdmi *dw = substream->private_data;
  408. unsigned long flags;
  409. int ret = 0;
  410. switch (cmd) {
  411. case SNDRV_PCM_TRIGGER_START:
  412. spin_lock_irqsave(&dw->lock, flags);
  413. dw->buf_offset = 0;
  414. dw->substream = substream;
  415. dw_hdmi_start_dma(dw);
  416. dw_hdmi_audio_enable(dw->data.hdmi);
  417. spin_unlock_irqrestore(&dw->lock, flags);
  418. substream->runtime->delay = substream->runtime->period_size;
  419. break;
  420. case SNDRV_PCM_TRIGGER_STOP:
  421. spin_lock_irqsave(&dw->lock, flags);
  422. dw->substream = NULL;
  423. dw_hdmi_stop_dma(dw);
  424. dw_hdmi_audio_disable(dw->data.hdmi);
  425. spin_unlock_irqrestore(&dw->lock, flags);
  426. break;
  427. default:
  428. ret = -EINVAL;
  429. break;
  430. }
  431. return ret;
  432. }
  433. static snd_pcm_uframes_t dw_hdmi_pointer(struct snd_pcm_substream *substream)
  434. {
  435. struct snd_pcm_runtime *runtime = substream->runtime;
  436. struct snd_dw_hdmi *dw = substream->private_data;
  437. /*
  438. * We are unable to report the exact hardware position as
  439. * reading the 32-bit DMA position using 8-bit reads is racy.
  440. */
  441. return bytes_to_frames(runtime, dw->buf_offset);
  442. }
  443. static struct snd_pcm_ops snd_dw_hdmi_ops = {
  444. .open = dw_hdmi_open,
  445. .close = dw_hdmi_close,
  446. .ioctl = snd_pcm_lib_ioctl,
  447. .hw_params = dw_hdmi_hw_params,
  448. .hw_free = dw_hdmi_hw_free,
  449. .prepare = dw_hdmi_prepare,
  450. .trigger = dw_hdmi_trigger,
  451. .pointer = dw_hdmi_pointer,
  452. .page = snd_pcm_lib_get_vmalloc_page,
  453. };
  454. static int snd_dw_hdmi_probe(struct platform_device *pdev)
  455. {
  456. const struct dw_hdmi_audio_data *data = pdev->dev.platform_data;
  457. struct device *dev = pdev->dev.parent;
  458. struct snd_dw_hdmi *dw;
  459. struct snd_card *card;
  460. struct snd_pcm *pcm;
  461. unsigned revision;
  462. int ret;
  463. writeb_relaxed(HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL,
  464. data->base + HDMI_IH_MUTE_AHBDMAAUD_STAT0);
  465. revision = readb_relaxed(data->base + HDMI_REVISION_ID);
  466. if (revision != 0x0a && revision != 0x1a) {
  467. dev_err(dev, "dw-hdmi-audio: unknown revision 0x%02x\n",
  468. revision);
  469. return -ENXIO;
  470. }
  471. ret = snd_card_new(dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  472. THIS_MODULE, sizeof(struct snd_dw_hdmi), &card);
  473. if (ret < 0)
  474. return ret;
  475. strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
  476. strlcpy(card->shortname, "DW-HDMI", sizeof(card->shortname));
  477. snprintf(card->longname, sizeof(card->longname),
  478. "%s rev 0x%02x, irq %d", card->shortname, revision,
  479. data->irq);
  480. dw = card->private_data;
  481. dw->card = card;
  482. dw->data = *data;
  483. dw->revision = revision;
  484. spin_lock_init(&dw->lock);
  485. ret = snd_pcm_new(card, "DW HDMI", 0, 1, 0, &pcm);
  486. if (ret < 0)
  487. goto err;
  488. dw->pcm = pcm;
  489. pcm->private_data = dw;
  490. strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
  491. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_dw_hdmi_ops);
  492. /*
  493. * To support 8-channel 96kHz audio reliably, we need 512k
  494. * to satisfy alsa with our restricted period (ERR004323).
  495. */
  496. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  497. dev, 128 * 1024, 1024 * 1024);
  498. ret = snd_card_register(card);
  499. if (ret < 0)
  500. goto err;
  501. platform_set_drvdata(pdev, dw);
  502. return 0;
  503. err:
  504. snd_card_free(card);
  505. return ret;
  506. }
  507. static int snd_dw_hdmi_remove(struct platform_device *pdev)
  508. {
  509. struct snd_dw_hdmi *dw = platform_get_drvdata(pdev);
  510. snd_card_free(dw->card);
  511. return 0;
  512. }
  513. #if defined(CONFIG_PM_SLEEP) && defined(IS_NOT_BROKEN)
  514. /*
  515. * This code is fine, but requires implementation in the dw_hdmi_trigger()
  516. * method which is currently missing as I have no way to test this.
  517. */
  518. static int snd_dw_hdmi_suspend(struct device *dev)
  519. {
  520. struct snd_dw_hdmi *dw = dev_get_drvdata(dev);
  521. snd_power_change_state(dw->card, SNDRV_CTL_POWER_D3cold);
  522. snd_pcm_suspend_all(dw->pcm);
  523. return 0;
  524. }
  525. static int snd_dw_hdmi_resume(struct device *dev)
  526. {
  527. struct snd_dw_hdmi *dw = dev_get_drvdata(dev);
  528. snd_power_change_state(dw->card, SNDRV_CTL_POWER_D0);
  529. return 0;
  530. }
  531. static SIMPLE_DEV_PM_OPS(snd_dw_hdmi_pm, snd_dw_hdmi_suspend,
  532. snd_dw_hdmi_resume);
  533. #define PM_OPS &snd_dw_hdmi_pm
  534. #else
  535. #define PM_OPS NULL
  536. #endif
  537. static struct platform_driver snd_dw_hdmi_driver = {
  538. .probe = snd_dw_hdmi_probe,
  539. .remove = snd_dw_hdmi_remove,
  540. .driver = {
  541. .name = DRIVER_NAME,
  542. .owner = THIS_MODULE,
  543. .pm = PM_OPS,
  544. },
  545. };
  546. module_platform_driver(snd_dw_hdmi_driver);
  547. MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
  548. MODULE_DESCRIPTION("Synopsis Designware HDMI AHB ALSA interface");
  549. MODULE_LICENSE("GPL v2");
  550. MODULE_ALIAS("platform:" DRIVER_NAME);