intel_hdmi_audio.c 50 KB

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