intel_hdmi_audio.c 52 KB

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