intel_hdmi_audio.c 50 KB

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