intel_hdmi_audio.c 53 KB

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