intel_hdmi_audio.c 52 KB

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