intel_hdmi_audio.c 49 KB

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