intel_hdmi_audio.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * intel_hdmi_audio.c - Intel HDMI audio driver
  3. *
  4. * Copyright (C) 2016 Intel Corp
  5. * Authors: Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>
  6. * Ramesh Babu K V <ramesh.babu@intel.com>
  7. * Vaibhav Agarwal <vaibhav.agarwal@intel.com>
  8. * Jerome Anand <jerome.anand@intel.com>
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. * ALSA driver for Intel HDMI audio
  22. */
  23. #include <linux/types.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/io.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/dma-mapping.h>
  31. #include <asm/cacheflush.h>
  32. #include <sound/core.h>
  33. #include <sound/asoundef.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/initval.h>
  37. #include <sound/control.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/intel_lpe_audio.h>
  40. #include "intel_hdmi_audio.h"
  41. /*standard module options for ALSA. This module supports only one card*/
  42. static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
  43. static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
  44. module_param_named(index, hdmi_card_index, int, 0444);
  45. MODULE_PARM_DESC(index,
  46. "Index value for INTEL Intel HDMI Audio controller.");
  47. module_param_named(id, hdmi_card_id, charp, 0444);
  48. MODULE_PARM_DESC(id,
  49. "ID string for INTEL Intel HDMI Audio controller.");
  50. /*
  51. * ELD SA bits in the CEA Speaker Allocation data block
  52. */
  53. static const int eld_speaker_allocation_bits[] = {
  54. [0] = FL | FR,
  55. [1] = LFE,
  56. [2] = FC,
  57. [3] = RL | RR,
  58. [4] = RC,
  59. [5] = FLC | FRC,
  60. [6] = RLC | RRC,
  61. /* the following are not defined in ELD yet */
  62. [7] = 0,
  63. };
  64. /*
  65. * This is an ordered list!
  66. *
  67. * The preceding ones have better chances to be selected by
  68. * hdmi_channel_allocation().
  69. */
  70. static struct cea_channel_speaker_allocation channel_allocations[] = {
  71. /* channel: 7 6 5 4 3 2 1 0 */
  72. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  73. /* 2.1 */
  74. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  75. /* Dolby Surround */
  76. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  77. /* surround40 */
  78. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  79. /* surround41 */
  80. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  81. /* surround50 */
  82. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  83. /* surround51 */
  84. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  85. /* 6.1 */
  86. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  87. /* surround71 */
  88. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  89. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  90. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  91. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  92. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  93. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  94. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  95. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  96. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  97. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  98. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  99. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  100. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  101. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  102. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  103. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  104. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  105. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  106. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  107. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  108. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  109. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  110. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  111. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  112. };
  113. static const struct channel_map_table map_tables[] = {
  114. { SNDRV_CHMAP_FL, 0x00, FL },
  115. { SNDRV_CHMAP_FR, 0x01, FR },
  116. { SNDRV_CHMAP_RL, 0x04, RL },
  117. { SNDRV_CHMAP_RR, 0x05, RR },
  118. { SNDRV_CHMAP_LFE, 0x02, LFE },
  119. { SNDRV_CHMAP_FC, 0x03, FC },
  120. { SNDRV_CHMAP_RLC, 0x06, RLC },
  121. { SNDRV_CHMAP_RRC, 0x07, RRC },
  122. {} /* terminator */
  123. };
  124. /* hardware capability structure */
  125. static const struct snd_pcm_hardware had_pcm_hardware = {
  126. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  127. SNDRV_PCM_INFO_MMAP |
  128. SNDRV_PCM_INFO_MMAP_VALID),
  129. .formats = SNDRV_PCM_FMTBIT_S24,
  130. .rates = SNDRV_PCM_RATE_32000 |
  131. SNDRV_PCM_RATE_44100 |
  132. SNDRV_PCM_RATE_48000 |
  133. SNDRV_PCM_RATE_88200 |
  134. SNDRV_PCM_RATE_96000 |
  135. SNDRV_PCM_RATE_176400 |
  136. SNDRV_PCM_RATE_192000,
  137. .rate_min = HAD_MIN_RATE,
  138. .rate_max = HAD_MAX_RATE,
  139. .channels_min = HAD_MIN_CHANNEL,
  140. .channels_max = HAD_MAX_CHANNEL,
  141. .buffer_bytes_max = HAD_MAX_BUFFER,
  142. .period_bytes_min = HAD_MIN_PERIOD_BYTES,
  143. .period_bytes_max = HAD_MAX_PERIOD_BYTES,
  144. .periods_min = HAD_MIN_PERIODS,
  145. .periods_max = HAD_MAX_PERIODS,
  146. .fifo_size = HAD_FIFO_SIZE,
  147. };
  148. /* Get the active PCM substream;
  149. * Call had_substream_put() for unreferecing.
  150. * Don't call this inside had_spinlock, as it takes by itself
  151. */
  152. static struct snd_pcm_substream *
  153. had_substream_get(struct snd_intelhad *intelhaddata)
  154. {
  155. struct snd_pcm_substream *substream;
  156. unsigned long flags;
  157. spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
  158. substream = intelhaddata->stream_info.substream;
  159. if (substream)
  160. intelhaddata->stream_info.substream_refcount++;
  161. spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
  162. return substream;
  163. }
  164. /* Unref the active PCM substream;
  165. * Don't call this inside had_spinlock, as it takes by itself
  166. */
  167. static void had_substream_put(struct snd_intelhad *intelhaddata)
  168. {
  169. unsigned long flags;
  170. spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
  171. intelhaddata->stream_info.substream_refcount--;
  172. spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
  173. }
  174. /* Register access functions */
  175. static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
  176. {
  177. *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
  178. }
  179. static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
  180. {
  181. iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
  182. }
  183. /*
  184. * enable / disable audio configuration
  185. *
  186. * The normal read/modify should not directly be used on VLV2 for
  187. * updating AUD_CONFIG register.
  188. * This is because:
  189. * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
  190. * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
  191. * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
  192. * register. This field should be 1xy binary for configuration with 6 or
  193. * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
  194. * causes the "channels" field to be updated as 0xy binary resulting in
  195. * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
  196. * appropriate value when doing read-modify of AUD_CONFIG register.
  197. */
  198. static void had_enable_audio(struct snd_intelhad *intelhaddata,
  199. bool enable)
  200. {
  201. /* update the cached value */
  202. intelhaddata->aud_config.regx.aud_en = enable;
  203. had_write_register(intelhaddata, AUD_CONFIG,
  204. intelhaddata->aud_config.regval);
  205. }
  206. /* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
  207. static void had_ack_irqs(struct snd_intelhad *ctx)
  208. {
  209. u32 status_reg;
  210. had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
  211. status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
  212. had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
  213. had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
  214. }
  215. /* Reset buffer pointers */
  216. static void had_reset_audio(struct snd_intelhad *intelhaddata)
  217. {
  218. had_write_register(intelhaddata, AUD_HDMI_STATUS,
  219. AUD_HDMI_STATUSG_MASK_FUNCRST);
  220. had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
  221. }
  222. /*
  223. * initialize audio channel status registers
  224. * This function is called in the prepare callback
  225. */
  226. static int had_prog_status_reg(struct snd_pcm_substream *substream,
  227. struct snd_intelhad *intelhaddata)
  228. {
  229. union aud_cfg cfg_val = {.regval = 0};
  230. union aud_ch_status_0 ch_stat0 = {.regval = 0};
  231. union aud_ch_status_1 ch_stat1 = {.regval = 0};
  232. int format;
  233. ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
  234. IEC958_AES0_NONAUDIO) >> 1;
  235. ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
  236. IEC958_AES3_CON_CLOCK) >> 4;
  237. cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
  238. switch (substream->runtime->rate) {
  239. case AUD_SAMPLE_RATE_32:
  240. ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
  241. break;
  242. case AUD_SAMPLE_RATE_44_1:
  243. ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
  244. break;
  245. case AUD_SAMPLE_RATE_48:
  246. ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
  247. break;
  248. case AUD_SAMPLE_RATE_88_2:
  249. ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
  250. break;
  251. case AUD_SAMPLE_RATE_96:
  252. ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
  253. break;
  254. case AUD_SAMPLE_RATE_176_4:
  255. ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
  256. break;
  257. case AUD_SAMPLE_RATE_192:
  258. ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
  259. break;
  260. default:
  261. /* control should never come here */
  262. return -EINVAL;
  263. }
  264. had_write_register(intelhaddata,
  265. AUD_CH_STATUS_0, ch_stat0.regval);
  266. format = substream->runtime->format;
  267. if (format == SNDRV_PCM_FORMAT_S16_LE) {
  268. ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
  269. ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
  270. } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
  271. ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
  272. ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
  273. } else {
  274. ch_stat1.regx.max_wrd_len = 0;
  275. ch_stat1.regx.wrd_len = 0;
  276. }
  277. had_write_register(intelhaddata,
  278. AUD_CH_STATUS_1, ch_stat1.regval);
  279. return 0;
  280. }
  281. /*
  282. * function to initialize audio
  283. * registers and buffer confgiuration registers
  284. * This function is called in the prepare callback
  285. */
  286. static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
  287. struct snd_intelhad *intelhaddata)
  288. {
  289. union aud_cfg cfg_val = {.regval = 0};
  290. union aud_buf_config buf_cfg = {.regval = 0};
  291. u8 channels;
  292. had_prog_status_reg(substream, intelhaddata);
  293. buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
  294. buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
  295. buf_cfg.regx.aud_delay = 0;
  296. had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
  297. channels = substream->runtime->channels;
  298. cfg_val.regx.num_ch = channels - 2;
  299. if (channels <= 2)
  300. cfg_val.regx.layout = LAYOUT0;
  301. else
  302. cfg_val.regx.layout = LAYOUT1;
  303. cfg_val.regx.val_bit = 1;
  304. /* fix up the DP bits */
  305. if (intelhaddata->dp_output) {
  306. cfg_val.regx.dp_modei = 1;
  307. cfg_val.regx.set = 1;
  308. }
  309. had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
  310. intelhaddata->aud_config = cfg_val;
  311. return 0;
  312. }
  313. /*
  314. * Compute derived values in channel_allocations[].
  315. */
  316. static void init_channel_allocations(void)
  317. {
  318. int i, j;
  319. struct cea_channel_speaker_allocation *p;
  320. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  321. p = channel_allocations + i;
  322. p->channels = 0;
  323. p->spk_mask = 0;
  324. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  325. if (p->speakers[j]) {
  326. p->channels++;
  327. p->spk_mask |= p->speakers[j];
  328. }
  329. }
  330. }
  331. /*
  332. * The transformation takes two steps:
  333. *
  334. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  335. * spk_mask => (channel_allocations[]) => ai->CA
  336. *
  337. * TODO: it could select the wrong CA from multiple candidates.
  338. */
  339. static int had_channel_allocation(struct snd_intelhad *intelhaddata,
  340. int channels)
  341. {
  342. int i;
  343. int ca = 0;
  344. int spk_mask = 0;
  345. /*
  346. * CA defaults to 0 for basic stereo audio
  347. */
  348. if (channels <= 2)
  349. return 0;
  350. /*
  351. * expand ELD's speaker allocation mask
  352. *
  353. * ELD tells the speaker mask in a compact(paired) form,
  354. * expand ELD's notions to match the ones used by Audio InfoFrame.
  355. */
  356. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  357. if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
  358. spk_mask |= eld_speaker_allocation_bits[i];
  359. }
  360. /* search for the first working match in the CA table */
  361. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  362. if (channels == channel_allocations[i].channels &&
  363. (spk_mask & channel_allocations[i].spk_mask) ==
  364. channel_allocations[i].spk_mask) {
  365. ca = channel_allocations[i].ca_index;
  366. break;
  367. }
  368. }
  369. dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
  370. return ca;
  371. }
  372. /* from speaker bit mask to ALSA API channel position */
  373. static int spk_to_chmap(int spk)
  374. {
  375. const struct channel_map_table *t = map_tables;
  376. for (; t->map; t++) {
  377. if (t->spk_mask == spk)
  378. return t->map;
  379. }
  380. return 0;
  381. }
  382. static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
  383. {
  384. int i, c;
  385. int spk_mask = 0;
  386. struct snd_pcm_chmap_elem *chmap;
  387. u8 eld_high, eld_high_mask = 0xF0;
  388. u8 high_msb;
  389. chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
  390. if (!chmap) {
  391. intelhaddata->chmap->chmap = NULL;
  392. return;
  393. }
  394. dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
  395. intelhaddata->eld[DRM_ELD_SPEAKER]);
  396. /* WA: Fix the max channel supported to 8 */
  397. /*
  398. * Sink may support more than 8 channels, if eld_high has more than
  399. * one bit set. SOC supports max 8 channels.
  400. * Refer eld_speaker_allocation_bits, for sink speaker allocation
  401. */
  402. /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
  403. eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
  404. if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
  405. /* eld_high & (eld_high-1): if more than 1 bit set */
  406. /* 0x1F: 7 channels */
  407. for (i = 1; i < 4; i++) {
  408. high_msb = eld_high & (0x80 >> i);
  409. if (high_msb) {
  410. intelhaddata->eld[DRM_ELD_SPEAKER] &=
  411. high_msb | 0xF;
  412. break;
  413. }
  414. }
  415. }
  416. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  417. if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
  418. spk_mask |= eld_speaker_allocation_bits[i];
  419. }
  420. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  421. if (spk_mask == channel_allocations[i].spk_mask) {
  422. for (c = 0; c < channel_allocations[i].channels; c++) {
  423. chmap->map[c] = spk_to_chmap(
  424. channel_allocations[i].speakers[
  425. (MAX_SPEAKERS - 1) - c]);
  426. }
  427. chmap->channels = channel_allocations[i].channels;
  428. intelhaddata->chmap->chmap = chmap;
  429. break;
  430. }
  431. }
  432. if (i >= ARRAY_SIZE(channel_allocations)) {
  433. intelhaddata->chmap->chmap = NULL;
  434. kfree(chmap);
  435. }
  436. }
  437. /*
  438. * ALSA API channel-map control callbacks
  439. */
  440. static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  441. struct snd_ctl_elem_info *uinfo)
  442. {
  443. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  444. struct snd_intelhad *intelhaddata = info->private_data;
  445. if (!intelhaddata->connected)
  446. return -ENODEV;
  447. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  448. uinfo->count = HAD_MAX_CHANNEL;
  449. uinfo->value.integer.min = 0;
  450. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  451. return 0;
  452. }
  453. static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  454. struct snd_ctl_elem_value *ucontrol)
  455. {
  456. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  457. struct snd_intelhad *intelhaddata = info->private_data;
  458. int i;
  459. const struct snd_pcm_chmap_elem *chmap;
  460. if (!intelhaddata->connected)
  461. return -ENODEV;
  462. mutex_lock(&intelhaddata->mutex);
  463. if (!intelhaddata->chmap->chmap) {
  464. mutex_unlock(&intelhaddata->mutex);
  465. return -ENODATA;
  466. }
  467. chmap = intelhaddata->chmap->chmap;
  468. for (i = 0; i < chmap->channels; i++)
  469. ucontrol->value.integer.value[i] = chmap->map[i];
  470. mutex_unlock(&intelhaddata->mutex);
  471. return 0;
  472. }
  473. static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
  474. struct snd_pcm *pcm)
  475. {
  476. int err;
  477. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  478. NULL, 0, (unsigned long)intelhaddata,
  479. &intelhaddata->chmap);
  480. if (err < 0)
  481. return err;
  482. intelhaddata->chmap->private_data = intelhaddata;
  483. intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
  484. intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
  485. intelhaddata->chmap->chmap = NULL;
  486. return 0;
  487. }
  488. /*
  489. * Initialize Data Island Packets registers
  490. * This function is called in the prepare callback
  491. */
  492. static void had_prog_dip(struct snd_pcm_substream *substream,
  493. struct snd_intelhad *intelhaddata)
  494. {
  495. int i;
  496. union aud_ctrl_st ctrl_state = {.regval = 0};
  497. union aud_info_frame2 frame2 = {.regval = 0};
  498. union aud_info_frame3 frame3 = {.regval = 0};
  499. u8 checksum = 0;
  500. u32 info_frame;
  501. int channels;
  502. int ca;
  503. channels = substream->runtime->channels;
  504. had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
  505. ca = had_channel_allocation(intelhaddata, channels);
  506. if (intelhaddata->dp_output) {
  507. info_frame = DP_INFO_FRAME_WORD1;
  508. frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
  509. } else {
  510. info_frame = HDMI_INFO_FRAME_WORD1;
  511. frame2.regx.chnl_cnt = substream->runtime->channels - 1;
  512. frame3.regx.chnl_alloc = ca;
  513. /* Calculte the byte wide checksum for all valid DIP words */
  514. for (i = 0; i < BYTES_PER_WORD; i++)
  515. checksum += (info_frame >> (i * 8)) & 0xff;
  516. for (i = 0; i < BYTES_PER_WORD; i++)
  517. checksum += (frame2.regval >> (i * 8)) & 0xff;
  518. for (i = 0; i < BYTES_PER_WORD; i++)
  519. checksum += (frame3.regval >> (i * 8)) & 0xff;
  520. frame2.regx.chksum = -(checksum);
  521. }
  522. had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
  523. had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
  524. had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
  525. /* program remaining DIP words with zero */
  526. for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
  527. had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
  528. ctrl_state.regx.dip_freq = 1;
  529. ctrl_state.regx.dip_en_sta = 1;
  530. had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
  531. }
  532. static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
  533. {
  534. u32 maud_val;
  535. /* Select maud according to DP 1.2 spec */
  536. if (link_rate == DP_2_7_GHZ) {
  537. switch (aud_samp_freq) {
  538. case AUD_SAMPLE_RATE_32:
  539. maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
  540. break;
  541. case AUD_SAMPLE_RATE_44_1:
  542. maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
  543. break;
  544. case AUD_SAMPLE_RATE_48:
  545. maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
  546. break;
  547. case AUD_SAMPLE_RATE_88_2:
  548. maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
  549. break;
  550. case AUD_SAMPLE_RATE_96:
  551. maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
  552. break;
  553. case AUD_SAMPLE_RATE_176_4:
  554. maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
  555. break;
  556. case HAD_MAX_RATE:
  557. maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
  558. break;
  559. default:
  560. maud_val = -EINVAL;
  561. break;
  562. }
  563. } else if (link_rate == DP_1_62_GHZ) {
  564. switch (aud_samp_freq) {
  565. case AUD_SAMPLE_RATE_32:
  566. maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
  567. break;
  568. case AUD_SAMPLE_RATE_44_1:
  569. maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
  570. break;
  571. case AUD_SAMPLE_RATE_48:
  572. maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
  573. break;
  574. case AUD_SAMPLE_RATE_88_2:
  575. maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
  576. break;
  577. case AUD_SAMPLE_RATE_96:
  578. maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
  579. break;
  580. case AUD_SAMPLE_RATE_176_4:
  581. maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
  582. break;
  583. case HAD_MAX_RATE:
  584. maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
  585. break;
  586. default:
  587. maud_val = -EINVAL;
  588. break;
  589. }
  590. } else
  591. maud_val = -EINVAL;
  592. return maud_val;
  593. }
  594. /*
  595. * Program HDMI audio CTS value
  596. *
  597. * @aud_samp_freq: sampling frequency of audio data
  598. * @tmds: sampling frequency of the display data
  599. * @link_rate: DP link rate
  600. * @n_param: N value, depends on aud_samp_freq
  601. * @intelhaddata: substream private data
  602. *
  603. * Program CTS register based on the audio and display sampling frequency
  604. */
  605. static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
  606. u32 n_param, struct snd_intelhad *intelhaddata)
  607. {
  608. u32 cts_val;
  609. u64 dividend, divisor;
  610. if (intelhaddata->dp_output) {
  611. /* Substitute cts_val with Maud according to DP 1.2 spec*/
  612. cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
  613. } else {
  614. /* Calculate CTS according to HDMI 1.3a spec*/
  615. dividend = (u64)tmds * n_param*1000;
  616. divisor = 128 * aud_samp_freq;
  617. cts_val = div64_u64(dividend, divisor);
  618. }
  619. dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
  620. tmds, n_param, cts_val);
  621. had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
  622. }
  623. static int had_calculate_n_value(u32 aud_samp_freq)
  624. {
  625. int n_val;
  626. /* Select N according to HDMI 1.3a spec*/
  627. switch (aud_samp_freq) {
  628. case AUD_SAMPLE_RATE_32:
  629. n_val = 4096;
  630. break;
  631. case AUD_SAMPLE_RATE_44_1:
  632. n_val = 6272;
  633. break;
  634. case AUD_SAMPLE_RATE_48:
  635. n_val = 6144;
  636. break;
  637. case AUD_SAMPLE_RATE_88_2:
  638. n_val = 12544;
  639. break;
  640. case AUD_SAMPLE_RATE_96:
  641. n_val = 12288;
  642. break;
  643. case AUD_SAMPLE_RATE_176_4:
  644. n_val = 25088;
  645. break;
  646. case HAD_MAX_RATE:
  647. n_val = 24576;
  648. break;
  649. default:
  650. n_val = -EINVAL;
  651. break;
  652. }
  653. return n_val;
  654. }
  655. /*
  656. * Program HDMI audio N value
  657. *
  658. * @aud_samp_freq: sampling frequency of audio data
  659. * @n_param: N value, depends on aud_samp_freq
  660. * @intelhaddata: substream private data
  661. *
  662. * This function is called in the prepare callback.
  663. * It programs based on the audio and display sampling frequency
  664. */
  665. static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
  666. struct snd_intelhad *intelhaddata)
  667. {
  668. int n_val;
  669. if (intelhaddata->dp_output) {
  670. /*
  671. * According to DP specs, Maud and Naud values hold
  672. * a relationship, which is stated as:
  673. * Maud/Naud = 512 * fs / f_LS_Clk
  674. * where, fs is the sampling frequency of the audio stream
  675. * and Naud is 32768 for Async clock.
  676. */
  677. n_val = DP_NAUD_VAL;
  678. } else
  679. n_val = had_calculate_n_value(aud_samp_freq);
  680. if (n_val < 0)
  681. return n_val;
  682. had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
  683. *n_param = n_val;
  684. return 0;
  685. }
  686. /*
  687. * PCM ring buffer handling
  688. *
  689. * The hardware provides a ring buffer with the fixed 4 buffer descriptors
  690. * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
  691. * moves at each period elapsed. The below illustrates how it works:
  692. *
  693. * At time=0
  694. * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
  695. * BD | 0 | 1 | 2 | 3 |
  696. *
  697. * At time=1 (period elapsed)
  698. * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
  699. * BD | 1 | 2 | 3 | 0 |
  700. *
  701. * At time=2 (second period elapsed)
  702. * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
  703. * BD | 2 | 3 | 0 | 1 |
  704. *
  705. * The bd_head field points to the index of the BD to be read. It's also the
  706. * position to be filled at next. The pcm_head and the pcm_filled fields
  707. * point to the indices of the current position and of the next position to
  708. * be filled, respectively. For PCM buffer there are both _head and _filled
  709. * because they may be difference when nperiods > 4. For example, in the
  710. * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
  711. *
  712. * pcm_head (=1) --v v-- pcm_filled (=5)
  713. * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
  714. * BD | 1 | 2 | 3 | 0 |
  715. * bd_head (=1) --^ ^-- next to fill (= bd_head)
  716. *
  717. * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
  718. * the hardware skips those BDs in the loop.
  719. *
  720. * An exceptional setup is the case with nperiods=1. Since we have to update
  721. * BDs after finishing one BD processing, we'd need at least two BDs, where
  722. * both BDs point to the same content, the same address, the same size of the
  723. * whole PCM buffer.
  724. */
  725. #define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
  726. #define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
  727. /* Set up a buffer descriptor at the "filled" position */
  728. static void had_prog_bd(struct snd_pcm_substream *substream,
  729. struct snd_intelhad *intelhaddata)
  730. {
  731. int idx = intelhaddata->bd_head;
  732. int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
  733. u32 addr = substream->runtime->dma_addr + ofs;
  734. addr |= AUD_BUF_VALID | AUD_BUF_INTR_EN;
  735. had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
  736. had_write_register(intelhaddata, AUD_BUF_LEN(idx),
  737. intelhaddata->period_bytes);
  738. /* advance the indices to the next */
  739. intelhaddata->bd_head++;
  740. intelhaddata->bd_head %= intelhaddata->num_bds;
  741. intelhaddata->pcmbuf_filled++;
  742. intelhaddata->pcmbuf_filled %= substream->runtime->periods;
  743. }
  744. /* invalidate a buffer descriptor with the given index */
  745. static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
  746. int idx)
  747. {
  748. had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
  749. had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
  750. }
  751. /* Initial programming of ring buffer */
  752. static void had_init_ringbuf(struct snd_pcm_substream *substream,
  753. struct snd_intelhad *intelhaddata)
  754. {
  755. struct snd_pcm_runtime *runtime = substream->runtime;
  756. int i, num_periods;
  757. num_periods = runtime->periods;
  758. intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
  759. /* set the minimum 2 BDs for num_periods=1 */
  760. intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
  761. intelhaddata->period_bytes =
  762. frames_to_bytes(runtime, runtime->period_size);
  763. WARN_ON(intelhaddata->period_bytes & 0x3f);
  764. intelhaddata->bd_head = 0;
  765. intelhaddata->pcmbuf_head = 0;
  766. intelhaddata->pcmbuf_filled = 0;
  767. for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
  768. if (i < intelhaddata->num_bds)
  769. had_prog_bd(substream, intelhaddata);
  770. else /* invalidate the rest */
  771. had_invalidate_bd(intelhaddata, i);
  772. }
  773. intelhaddata->bd_head = 0; /* reset at head again before starting */
  774. }
  775. /* process a bd, advance to the next */
  776. static void had_advance_ringbuf(struct snd_pcm_substream *substream,
  777. struct snd_intelhad *intelhaddata)
  778. {
  779. int num_periods = substream->runtime->periods;
  780. /* reprogram the next buffer */
  781. had_prog_bd(substream, intelhaddata);
  782. /* proceed to next */
  783. intelhaddata->pcmbuf_head++;
  784. intelhaddata->pcmbuf_head %= num_periods;
  785. }
  786. /* process the current BD(s);
  787. * returns the current PCM buffer byte position, or -EPIPE for underrun.
  788. */
  789. static int had_process_ringbuf(struct snd_pcm_substream *substream,
  790. struct snd_intelhad *intelhaddata)
  791. {
  792. int len, processed;
  793. unsigned long flags;
  794. processed = 0;
  795. spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
  796. for (;;) {
  797. /* get the remaining bytes on the buffer */
  798. had_read_register(intelhaddata,
  799. AUD_BUF_LEN(intelhaddata->bd_head),
  800. &len);
  801. if (len < 0 || len > intelhaddata->period_bytes) {
  802. dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
  803. len);
  804. len = -EPIPE;
  805. goto out;
  806. }
  807. if (len > 0) /* OK, this is the current buffer */
  808. break;
  809. /* len=0 => already empty, check the next buffer */
  810. if (++processed >= intelhaddata->num_bds) {
  811. len = -EPIPE; /* all empty? - report underrun */
  812. goto out;
  813. }
  814. had_advance_ringbuf(substream, intelhaddata);
  815. }
  816. len = intelhaddata->period_bytes - len;
  817. len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
  818. out:
  819. spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
  820. return len;
  821. }
  822. /* called from irq handler */
  823. static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
  824. {
  825. struct snd_pcm_substream *substream;
  826. if (!intelhaddata->connected)
  827. return; /* disconnected? - bail out */
  828. substream = had_substream_get(intelhaddata);
  829. if (!substream)
  830. return; /* no stream? - bail out */
  831. /* process or stop the stream */
  832. if (had_process_ringbuf(substream, intelhaddata) < 0)
  833. snd_pcm_stop_xrun(substream);
  834. else
  835. snd_pcm_period_elapsed(substream);
  836. had_substream_put(intelhaddata);
  837. }
  838. #define MAX_CNT 0xFF
  839. /*
  840. * The interrupt status 'sticky' bits might not be cleared by
  841. * setting '1' to that bit once...
  842. */
  843. static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
  844. {
  845. int i;
  846. u32 val;
  847. for (i = 0; i < MAX_CNT; i++) {
  848. /* clear bit30, 31 AUD_HDMI_STATUS */
  849. had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
  850. if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
  851. return;
  852. had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
  853. }
  854. dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
  855. }
  856. /* called from irq handler */
  857. static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
  858. {
  859. struct snd_pcm_substream *substream;
  860. /* Handle Underrun interrupt within Audio Unit */
  861. had_write_register(intelhaddata, AUD_CONFIG, 0);
  862. intelhaddata->aud_config.regval = 0;
  863. /* Reset buffer pointers */
  864. had_reset_audio(intelhaddata);
  865. wait_clear_underrun_bit(intelhaddata);
  866. if (!intelhaddata->connected)
  867. return; /* disconnected? - bail out */
  868. /* Report UNDERRUN error to above layers */
  869. substream = had_substream_get(intelhaddata);
  870. if (substream) {
  871. snd_pcm_stop_xrun(substream);
  872. had_substream_put(intelhaddata);
  873. }
  874. }
  875. /*
  876. * ALSA PCM open callback
  877. */
  878. static int had_pcm_open(struct snd_pcm_substream *substream)
  879. {
  880. struct snd_intelhad *intelhaddata;
  881. struct snd_pcm_runtime *runtime;
  882. int retval;
  883. intelhaddata = snd_pcm_substream_chip(substream);
  884. runtime = substream->runtime;
  885. pm_runtime_get_sync(intelhaddata->dev);
  886. if (!intelhaddata->connected) {
  887. dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
  888. __func__);
  889. retval = -ENODEV;
  890. goto error;
  891. }
  892. /* set the runtime hw parameter with local snd_pcm_hardware struct */
  893. runtime->hw = had_pcm_hardware;
  894. retval = snd_pcm_hw_constraint_integer(runtime,
  895. SNDRV_PCM_HW_PARAM_PERIODS);
  896. if (retval < 0)
  897. goto error;
  898. /* Make sure, that the period size is always aligned
  899. * 64byte boundary
  900. */
  901. retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
  902. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
  903. if (retval < 0)
  904. goto error;
  905. /* expose PCM substream */
  906. spin_lock_irq(&intelhaddata->had_spinlock);
  907. intelhaddata->stream_info.substream = substream;
  908. intelhaddata->stream_info.substream_refcount++;
  909. spin_unlock_irq(&intelhaddata->had_spinlock);
  910. return retval;
  911. error:
  912. pm_runtime_put(intelhaddata->dev);
  913. return retval;
  914. }
  915. /*
  916. * ALSA PCM close callback
  917. */
  918. static int had_pcm_close(struct snd_pcm_substream *substream)
  919. {
  920. struct snd_intelhad *intelhaddata;
  921. intelhaddata = snd_pcm_substream_chip(substream);
  922. /* unreference and sync with the pending PCM accesses */
  923. spin_lock_irq(&intelhaddata->had_spinlock);
  924. intelhaddata->stream_info.substream = NULL;
  925. intelhaddata->stream_info.substream_refcount--;
  926. while (intelhaddata->stream_info.substream_refcount > 0) {
  927. spin_unlock_irq(&intelhaddata->had_spinlock);
  928. cpu_relax();
  929. spin_lock_irq(&intelhaddata->had_spinlock);
  930. }
  931. spin_unlock_irq(&intelhaddata->had_spinlock);
  932. pm_runtime_put(intelhaddata->dev);
  933. return 0;
  934. }
  935. /*
  936. * ALSA PCM hw_params callback
  937. */
  938. static int had_pcm_hw_params(struct snd_pcm_substream *substream,
  939. struct snd_pcm_hw_params *hw_params)
  940. {
  941. struct snd_intelhad *intelhaddata;
  942. unsigned long addr;
  943. int pages, buf_size, retval;
  944. intelhaddata = snd_pcm_substream_chip(substream);
  945. buf_size = params_buffer_bytes(hw_params);
  946. retval = snd_pcm_lib_malloc_pages(substream, buf_size);
  947. if (retval < 0)
  948. return retval;
  949. dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
  950. __func__, buf_size);
  951. /* mark the pages as uncached region */
  952. addr = (unsigned long) substream->runtime->dma_area;
  953. pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  954. retval = set_memory_uc(addr, pages);
  955. if (retval) {
  956. dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
  957. retval);
  958. return retval;
  959. }
  960. memset(substream->runtime->dma_area, 0, buf_size);
  961. return retval;
  962. }
  963. /*
  964. * ALSA PCM hw_free callback
  965. */
  966. static int had_pcm_hw_free(struct snd_pcm_substream *substream)
  967. {
  968. unsigned long addr;
  969. u32 pages;
  970. /* mark back the pages as cached/writeback region before the free */
  971. if (substream->runtime->dma_area != NULL) {
  972. addr = (unsigned long) substream->runtime->dma_area;
  973. pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
  974. PAGE_SIZE;
  975. set_memory_wb(addr, pages);
  976. return snd_pcm_lib_free_pages(substream);
  977. }
  978. return 0;
  979. }
  980. /*
  981. * ALSA PCM trigger callback
  982. */
  983. static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  984. {
  985. int retval = 0;
  986. struct snd_intelhad *intelhaddata;
  987. intelhaddata = snd_pcm_substream_chip(substream);
  988. switch (cmd) {
  989. case SNDRV_PCM_TRIGGER_START:
  990. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  991. case SNDRV_PCM_TRIGGER_RESUME:
  992. /* Disable local INTRs till register prgmng is done */
  993. if (!intelhaddata->connected) {
  994. dev_dbg(intelhaddata->dev,
  995. "_START: HDMI cable plugged-out\n");
  996. retval = -ENODEV;
  997. break;
  998. }
  999. intelhaddata->stream_info.running = true;
  1000. /* Enable Audio */
  1001. had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
  1002. had_enable_audio(intelhaddata, true);
  1003. break;
  1004. case SNDRV_PCM_TRIGGER_STOP:
  1005. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1006. case SNDRV_PCM_TRIGGER_SUSPEND:
  1007. spin_lock(&intelhaddata->had_spinlock);
  1008. /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
  1009. intelhaddata->stream_info.running = false;
  1010. spin_unlock(&intelhaddata->had_spinlock);
  1011. /* Disable Audio */
  1012. had_enable_audio(intelhaddata, false);
  1013. /* Reset buffer pointers */
  1014. had_reset_audio(intelhaddata);
  1015. break;
  1016. default:
  1017. retval = -EINVAL;
  1018. }
  1019. return retval;
  1020. }
  1021. /*
  1022. * ALSA PCM prepare callback
  1023. */
  1024. static int had_pcm_prepare(struct snd_pcm_substream *substream)
  1025. {
  1026. int retval;
  1027. u32 disp_samp_freq, n_param;
  1028. u32 link_rate = 0;
  1029. struct snd_intelhad *intelhaddata;
  1030. struct snd_pcm_runtime *runtime;
  1031. intelhaddata = snd_pcm_substream_chip(substream);
  1032. runtime = substream->runtime;
  1033. if (!intelhaddata->connected) {
  1034. dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
  1035. __func__);
  1036. retval = -ENODEV;
  1037. goto prep_end;
  1038. }
  1039. dev_dbg(intelhaddata->dev, "period_size=%d\n",
  1040. (int)frames_to_bytes(runtime, runtime->period_size));
  1041. dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
  1042. dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
  1043. (int)snd_pcm_lib_buffer_bytes(substream));
  1044. dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
  1045. dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
  1046. /* Get N value in KHz */
  1047. disp_samp_freq = intelhaddata->tmds_clock_speed;
  1048. retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
  1049. if (retval) {
  1050. dev_err(intelhaddata->dev,
  1051. "programming N value failed %#x\n", retval);
  1052. goto prep_end;
  1053. }
  1054. if (intelhaddata->dp_output)
  1055. link_rate = intelhaddata->link_rate;
  1056. had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
  1057. n_param, intelhaddata);
  1058. had_prog_dip(substream, intelhaddata);
  1059. retval = had_init_audio_ctrl(substream, intelhaddata);
  1060. /* Prog buffer address */
  1061. had_init_ringbuf(substream, intelhaddata);
  1062. /*
  1063. * Program channel mapping in following order:
  1064. * FL, FR, C, LFE, RL, RR
  1065. */
  1066. had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
  1067. prep_end:
  1068. return retval;
  1069. }
  1070. /*
  1071. * ALSA PCM pointer callback
  1072. */
  1073. static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
  1074. {
  1075. struct snd_intelhad *intelhaddata;
  1076. int len;
  1077. intelhaddata = snd_pcm_substream_chip(substream);
  1078. if (!intelhaddata->connected)
  1079. return SNDRV_PCM_POS_XRUN;
  1080. len = had_process_ringbuf(substream, intelhaddata);
  1081. if (len < 0)
  1082. return SNDRV_PCM_POS_XRUN;
  1083. len = bytes_to_frames(substream->runtime, len);
  1084. /* wrapping may happen when periods=1 */
  1085. len %= substream->runtime->buffer_size;
  1086. return len;
  1087. }
  1088. /*
  1089. * ALSA PCM mmap callback
  1090. */
  1091. static int had_pcm_mmap(struct snd_pcm_substream *substream,
  1092. struct vm_area_struct *vma)
  1093. {
  1094. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1095. return remap_pfn_range(vma, vma->vm_start,
  1096. substream->dma_buffer.addr >> PAGE_SHIFT,
  1097. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  1098. }
  1099. /*
  1100. * ALSA PCM ops
  1101. */
  1102. static const struct snd_pcm_ops had_pcm_ops = {
  1103. .open = had_pcm_open,
  1104. .close = had_pcm_close,
  1105. .ioctl = snd_pcm_lib_ioctl,
  1106. .hw_params = had_pcm_hw_params,
  1107. .hw_free = had_pcm_hw_free,
  1108. .prepare = had_pcm_prepare,
  1109. .trigger = had_pcm_trigger,
  1110. .pointer = had_pcm_pointer,
  1111. .mmap = had_pcm_mmap,
  1112. };
  1113. /* process mode change of the running stream; called in mutex */
  1114. static int had_process_mode_change(struct snd_intelhad *intelhaddata)
  1115. {
  1116. struct snd_pcm_substream *substream;
  1117. int retval = 0;
  1118. u32 disp_samp_freq, n_param;
  1119. u32 link_rate = 0;
  1120. substream = had_substream_get(intelhaddata);
  1121. if (!substream)
  1122. return 0;
  1123. /* Disable Audio */
  1124. had_enable_audio(intelhaddata, false);
  1125. /* Update CTS value */
  1126. disp_samp_freq = intelhaddata->tmds_clock_speed;
  1127. retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
  1128. if (retval) {
  1129. dev_err(intelhaddata->dev,
  1130. "programming N value failed %#x\n", retval);
  1131. goto out;
  1132. }
  1133. if (intelhaddata->dp_output)
  1134. link_rate = intelhaddata->link_rate;
  1135. had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
  1136. n_param, intelhaddata);
  1137. /* Enable Audio */
  1138. had_enable_audio(intelhaddata, true);
  1139. out:
  1140. had_substream_put(intelhaddata);
  1141. return retval;
  1142. }
  1143. /* process hot plug, called from wq with mutex locked */
  1144. static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
  1145. {
  1146. struct snd_pcm_substream *substream;
  1147. spin_lock_irq(&intelhaddata->had_spinlock);
  1148. if (intelhaddata->connected) {
  1149. dev_dbg(intelhaddata->dev, "Device already connected\n");
  1150. spin_unlock_irq(&intelhaddata->had_spinlock);
  1151. return;
  1152. }
  1153. intelhaddata->connected = true;
  1154. dev_dbg(intelhaddata->dev,
  1155. "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
  1156. __func__, __LINE__);
  1157. spin_unlock_irq(&intelhaddata->had_spinlock);
  1158. /* Safety check */
  1159. substream = had_substream_get(intelhaddata);
  1160. if (substream) {
  1161. dev_dbg(intelhaddata->dev,
  1162. "Force to stop the active stream by disconnection\n");
  1163. /* Set runtime->state to hw_params done */
  1164. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1165. had_substream_put(intelhaddata);
  1166. }
  1167. had_build_channel_allocation_map(intelhaddata);
  1168. }
  1169. /* process hot unplug, called from wq with mutex locked */
  1170. static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
  1171. {
  1172. struct snd_pcm_substream *substream;
  1173. substream = had_substream_get(intelhaddata);
  1174. spin_lock_irq(&intelhaddata->had_spinlock);
  1175. if (!intelhaddata->connected) {
  1176. dev_dbg(intelhaddata->dev, "Device already disconnected\n");
  1177. spin_unlock_irq(&intelhaddata->had_spinlock);
  1178. goto out;
  1179. }
  1180. /* Disable Audio */
  1181. had_enable_audio(intelhaddata, false);
  1182. intelhaddata->connected = false;
  1183. dev_dbg(intelhaddata->dev,
  1184. "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
  1185. __func__, __LINE__);
  1186. spin_unlock_irq(&intelhaddata->had_spinlock);
  1187. /* Report to above ALSA layer */
  1188. if (substream)
  1189. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1190. out:
  1191. if (substream)
  1192. had_substream_put(intelhaddata);
  1193. kfree(intelhaddata->chmap->chmap);
  1194. intelhaddata->chmap->chmap = NULL;
  1195. }
  1196. /*
  1197. * ALSA iec958 and ELD controls
  1198. */
  1199. static int had_iec958_info(struct snd_kcontrol *kcontrol,
  1200. struct snd_ctl_elem_info *uinfo)
  1201. {
  1202. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1203. uinfo->count = 1;
  1204. return 0;
  1205. }
  1206. static int had_iec958_get(struct snd_kcontrol *kcontrol,
  1207. struct snd_ctl_elem_value *ucontrol)
  1208. {
  1209. struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
  1210. mutex_lock(&intelhaddata->mutex);
  1211. ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
  1212. ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
  1213. ucontrol->value.iec958.status[2] =
  1214. (intelhaddata->aes_bits >> 16) & 0xff;
  1215. ucontrol->value.iec958.status[3] =
  1216. (intelhaddata->aes_bits >> 24) & 0xff;
  1217. mutex_unlock(&intelhaddata->mutex);
  1218. return 0;
  1219. }
  1220. static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
  1221. struct snd_ctl_elem_value *ucontrol)
  1222. {
  1223. ucontrol->value.iec958.status[0] = 0xff;
  1224. ucontrol->value.iec958.status[1] = 0xff;
  1225. ucontrol->value.iec958.status[2] = 0xff;
  1226. ucontrol->value.iec958.status[3] = 0xff;
  1227. return 0;
  1228. }
  1229. static int had_iec958_put(struct snd_kcontrol *kcontrol,
  1230. struct snd_ctl_elem_value *ucontrol)
  1231. {
  1232. unsigned int val;
  1233. struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
  1234. int changed = 0;
  1235. val = (ucontrol->value.iec958.status[0] << 0) |
  1236. (ucontrol->value.iec958.status[1] << 8) |
  1237. (ucontrol->value.iec958.status[2] << 16) |
  1238. (ucontrol->value.iec958.status[3] << 24);
  1239. mutex_lock(&intelhaddata->mutex);
  1240. if (intelhaddata->aes_bits != val) {
  1241. intelhaddata->aes_bits = val;
  1242. changed = 1;
  1243. }
  1244. mutex_unlock(&intelhaddata->mutex);
  1245. return changed;
  1246. }
  1247. static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
  1248. struct snd_ctl_elem_info *uinfo)
  1249. {
  1250. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  1251. uinfo->count = HDMI_MAX_ELD_BYTES;
  1252. return 0;
  1253. }
  1254. static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
  1255. struct snd_ctl_elem_value *ucontrol)
  1256. {
  1257. struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
  1258. mutex_lock(&intelhaddata->mutex);
  1259. memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
  1260. HDMI_MAX_ELD_BYTES);
  1261. mutex_unlock(&intelhaddata->mutex);
  1262. return 0;
  1263. }
  1264. static const struct snd_kcontrol_new had_controls[] = {
  1265. {
  1266. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1267. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1268. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
  1269. .info = had_iec958_info, /* shared */
  1270. .get = had_iec958_mask_get,
  1271. },
  1272. {
  1273. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1274. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  1275. .info = had_iec958_info,
  1276. .get = had_iec958_get,
  1277. .put = had_iec958_put,
  1278. },
  1279. {
  1280. .access = (SNDRV_CTL_ELEM_ACCESS_READ |
  1281. SNDRV_CTL_ELEM_ACCESS_VOLATILE),
  1282. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1283. .name = "ELD",
  1284. .info = had_ctl_eld_info,
  1285. .get = had_ctl_eld_get,
  1286. },
  1287. };
  1288. /*
  1289. * audio interrupt handler
  1290. */
  1291. static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
  1292. {
  1293. struct snd_intelhad *ctx = dev_id;
  1294. u32 audio_stat, audio_reg;
  1295. audio_reg = AUD_HDMI_STATUS;
  1296. had_read_register(ctx, audio_reg, &audio_stat);
  1297. if (audio_stat & HDMI_AUDIO_UNDERRUN) {
  1298. had_write_register(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
  1299. had_process_buffer_underrun(ctx);
  1300. }
  1301. if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
  1302. had_write_register(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
  1303. had_process_buffer_done(ctx);
  1304. }
  1305. return IRQ_HANDLED;
  1306. }
  1307. /*
  1308. * monitor plug/unplug notification from i915; just kick off the work
  1309. */
  1310. static void notify_audio_lpe(struct platform_device *pdev)
  1311. {
  1312. struct snd_intelhad *ctx = platform_get_drvdata(pdev);
  1313. schedule_work(&ctx->hdmi_audio_wq);
  1314. }
  1315. /* the work to handle monitor hot plug/unplug */
  1316. static void had_audio_wq(struct work_struct *work)
  1317. {
  1318. struct snd_intelhad *ctx =
  1319. container_of(work, struct snd_intelhad, hdmi_audio_wq);
  1320. struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
  1321. pm_runtime_get_sync(ctx->dev);
  1322. mutex_lock(&ctx->mutex);
  1323. if (!pdata->hdmi_connected) {
  1324. dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
  1325. __func__);
  1326. memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
  1327. had_process_hot_unplug(ctx);
  1328. } else {
  1329. struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
  1330. dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
  1331. __func__, eld->port_id, pdata->tmds_clock_speed);
  1332. switch (eld->pipe_id) {
  1333. case 0:
  1334. ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
  1335. break;
  1336. case 1:
  1337. ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
  1338. break;
  1339. case 2:
  1340. ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
  1341. break;
  1342. default:
  1343. dev_dbg(ctx->dev, "Invalid pipe %d\n",
  1344. eld->pipe_id);
  1345. break;
  1346. }
  1347. memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
  1348. ctx->dp_output = pdata->dp_output;
  1349. ctx->tmds_clock_speed = pdata->tmds_clock_speed;
  1350. ctx->link_rate = pdata->link_rate;
  1351. had_process_hot_plug(ctx);
  1352. /* Process mode change if stream is active */
  1353. had_process_mode_change(ctx);
  1354. }
  1355. mutex_unlock(&ctx->mutex);
  1356. pm_runtime_put(ctx->dev);
  1357. }
  1358. /*
  1359. * PM callbacks
  1360. */
  1361. static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
  1362. {
  1363. struct snd_intelhad *ctx = dev_get_drvdata(dev);
  1364. struct snd_pcm_substream *substream;
  1365. substream = had_substream_get(ctx);
  1366. if (substream) {
  1367. snd_pcm_suspend(substream);
  1368. had_substream_put(ctx);
  1369. }
  1370. return 0;
  1371. }
  1372. static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
  1373. {
  1374. struct snd_intelhad *ctx = dev_get_drvdata(dev);
  1375. int err;
  1376. err = hdmi_lpe_audio_runtime_suspend(dev);
  1377. if (!err)
  1378. snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
  1379. return err;
  1380. }
  1381. static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
  1382. {
  1383. struct snd_intelhad *ctx = dev_get_drvdata(dev);
  1384. snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
  1385. return 0;
  1386. }
  1387. /* release resources */
  1388. static void hdmi_lpe_audio_free(struct snd_card *card)
  1389. {
  1390. struct snd_intelhad *ctx = card->private_data;
  1391. cancel_work_sync(&ctx->hdmi_audio_wq);
  1392. if (ctx->mmio_start)
  1393. iounmap(ctx->mmio_start);
  1394. if (ctx->irq >= 0)
  1395. free_irq(ctx->irq, ctx);
  1396. }
  1397. /*
  1398. * hdmi_lpe_audio_probe - start bridge with i915
  1399. *
  1400. * This function is called when the i915 driver creates the
  1401. * hdmi-lpe-audio platform device.
  1402. */
  1403. static int hdmi_lpe_audio_probe(struct platform_device *pdev)
  1404. {
  1405. struct snd_card *card;
  1406. struct snd_intelhad *ctx;
  1407. struct snd_pcm *pcm;
  1408. struct intel_hdmi_lpe_audio_pdata *pdata;
  1409. int irq;
  1410. struct resource *res_mmio;
  1411. int i, ret;
  1412. pdata = pdev->dev.platform_data;
  1413. if (!pdata) {
  1414. dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
  1415. return -EINVAL;
  1416. }
  1417. /* get resources */
  1418. irq = platform_get_irq(pdev, 0);
  1419. if (irq < 0) {
  1420. dev_err(&pdev->dev, "Could not get irq resource\n");
  1421. return -ENODEV;
  1422. }
  1423. res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1424. if (!res_mmio) {
  1425. dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
  1426. return -ENXIO;
  1427. }
  1428. /* create a card instance with ALSA framework */
  1429. ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
  1430. THIS_MODULE, sizeof(*ctx), &card);
  1431. if (ret)
  1432. return ret;
  1433. ctx = card->private_data;
  1434. spin_lock_init(&ctx->had_spinlock);
  1435. mutex_init(&ctx->mutex);
  1436. ctx->connected = false;
  1437. ctx->dev = &pdev->dev;
  1438. ctx->card = card;
  1439. ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
  1440. strcpy(card->driver, INTEL_HAD);
  1441. strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
  1442. strcpy(card->longname, "Intel HDMI/DP LPE Audio");
  1443. ctx->irq = -1;
  1444. ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
  1445. INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
  1446. card->private_free = hdmi_lpe_audio_free;
  1447. /* assume pipe A as default */
  1448. ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
  1449. platform_set_drvdata(pdev, ctx);
  1450. dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
  1451. __func__, (unsigned int)res_mmio->start,
  1452. (unsigned int)res_mmio->end);
  1453. ctx->mmio_start = ioremap_nocache(res_mmio->start,
  1454. (size_t)(resource_size(res_mmio)));
  1455. if (!ctx->mmio_start) {
  1456. dev_err(&pdev->dev, "Could not get ioremap\n");
  1457. ret = -EACCES;
  1458. goto err;
  1459. }
  1460. /* setup interrupt handler */
  1461. ret = request_irq(irq, display_pipe_interrupt_handler, 0,
  1462. pdev->name, ctx);
  1463. if (ret < 0) {
  1464. dev_err(&pdev->dev, "request_irq failed\n");
  1465. goto err;
  1466. }
  1467. ctx->irq = irq;
  1468. ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
  1469. MAX_CAP_STREAMS, &pcm);
  1470. if (ret)
  1471. goto err;
  1472. /* setup private data which can be retrieved when required */
  1473. pcm->private_data = ctx;
  1474. pcm->info_flags = 0;
  1475. strncpy(pcm->name, card->shortname, strlen(card->shortname));
  1476. /* setup the ops for playabck */
  1477. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
  1478. /* only 32bit addressable */
  1479. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  1480. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  1481. /* allocate dma pages;
  1482. * try to allocate 600k buffer as default which is large enough
  1483. */
  1484. snd_pcm_lib_preallocate_pages_for_all(pcm,
  1485. SNDRV_DMA_TYPE_DEV, NULL,
  1486. HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
  1487. /* create controls */
  1488. for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
  1489. ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
  1490. if (ret < 0)
  1491. goto err;
  1492. }
  1493. init_channel_allocations();
  1494. /* Register channel map controls */
  1495. ret = had_register_chmap_ctls(ctx, pcm);
  1496. if (ret < 0)
  1497. goto err;
  1498. ret = snd_card_register(card);
  1499. if (ret)
  1500. goto err;
  1501. spin_lock_irq(&pdata->lpe_audio_slock);
  1502. pdata->notify_audio_lpe = notify_audio_lpe;
  1503. pdata->notify_pending = false;
  1504. spin_unlock_irq(&pdata->lpe_audio_slock);
  1505. pm_runtime_set_active(&pdev->dev);
  1506. pm_runtime_enable(&pdev->dev);
  1507. dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
  1508. schedule_work(&ctx->hdmi_audio_wq);
  1509. return 0;
  1510. err:
  1511. snd_card_free(card);
  1512. return ret;
  1513. }
  1514. /*
  1515. * hdmi_lpe_audio_remove - stop bridge with i915
  1516. *
  1517. * This function is called when the platform device is destroyed.
  1518. */
  1519. static int hdmi_lpe_audio_remove(struct platform_device *pdev)
  1520. {
  1521. struct snd_intelhad *ctx = platform_get_drvdata(pdev);
  1522. snd_card_free(ctx->card);
  1523. return 0;
  1524. }
  1525. static const struct dev_pm_ops hdmi_lpe_audio_pm = {
  1526. SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
  1527. SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
  1528. };
  1529. static struct platform_driver hdmi_lpe_audio_driver = {
  1530. .driver = {
  1531. .name = "hdmi-lpe-audio",
  1532. .pm = &hdmi_lpe_audio_pm,
  1533. },
  1534. .probe = hdmi_lpe_audio_probe,
  1535. .remove = hdmi_lpe_audio_remove,
  1536. };
  1537. module_platform_driver(hdmi_lpe_audio_driver);
  1538. MODULE_ALIAS("platform:hdmi_lpe_audio");
  1539. MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
  1540. MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
  1541. MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
  1542. MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
  1543. MODULE_DESCRIPTION("Intel HDMI Audio driver");
  1544. MODULE_LICENSE("GPL v2");
  1545. MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");