intel_hdmi_audio.c 53 KB

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