intel_hdmi_audio.c 53 KB

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