aml_pcm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/init.h>
  5. #include <linux/errno.h>
  6. #include <linux/ioctl.h>
  7. #include <linux/delay.h>
  8. #include <linux/slab.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/soundcard.h>
  11. #include <linux/timer.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/major.h>
  14. #include <sound/core.h>
  15. #include <sound/pcm.h>
  16. #include <sound/initval.h>
  17. #include <sound/control.h>
  18. #include <sound/soc.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/rt5631.h>
  21. #include <mach/am_regs.h>
  22. #include <mach/pinmux.h>
  23. #include <linux/amports/amaudio.h>
  24. #include <mach/mod_gate.h>
  25. #include "aml_pcm.h"
  26. #include "aml_audio_hw.h"
  27. #define AOUT_EVENT_PREPARE 0x1
  28. extern int aout_notifier_call_chain(unsigned long val, void *v);
  29. unsigned int aml_pcm_playback_start_addr = 0;
  30. unsigned int aml_pcm_capture_start_addr = 0;
  31. unsigned int aml_pcm_capture_start_phy = 0;
  32. unsigned int aml_pcm_capture_buf_size = 0;
  33. unsigned int aml_pcm_playback_off = 0;
  34. unsigned int aml_pcm_playback_enable = 1;
  35. /*to keep the pcm status for clockgating*/
  36. static unsigned clock_gating_status = 0;
  37. static unsigned clock_gating_playback = 1;
  38. static unsigned clock_gating_capture = 2;
  39. static struct rt5631_platform_data *rt5631_snd_pdata = NULL;
  40. static struct aml_pcm_work_t{
  41. struct snd_pcm_substream *substream;
  42. struct work_struct aml_codec_workqueue;
  43. }aml_pcm_work;
  44. static int codec_power_switch(struct snd_pcm_substream *substream, unsigned int status);
  45. EXPORT_SYMBOL(aml_pcm_playback_start_addr);
  46. EXPORT_SYMBOL(aml_pcm_capture_start_addr);
  47. EXPORT_SYMBOL(aml_pcm_playback_off);
  48. EXPORT_SYMBOL(aml_pcm_playback_enable);
  49. static void aml_codec_power_switch_queue(struct work_struct* work)
  50. {
  51. struct aml_pcm_work_t* pwork = container_of(work, struct aml_pcm_work_t, aml_codec_workqueue);
  52. struct snd_pcm_substream* substream = pwork->substream;
  53. #ifdef _AML_PCM_DEBUG_
  54. printk("***Entered %s:%s\n", __FILE__,__func__);
  55. #endif
  56. codec_power_switch(substream, clock_gating_status);
  57. }
  58. /*--------------------------------------------------------------------------*\
  59. * Hardware definition
  60. \*--------------------------------------------------------------------------*/
  61. /* TODO: These values were taken from the AML platform driver, check
  62. * them against real values for AML
  63. */
  64. static const struct snd_pcm_hardware aml_pcm_hardware = {
  65. .info = SNDRV_PCM_INFO_INTERLEAVED|
  66. SNDRV_PCM_INFO_BLOCK_TRANSFER|
  67. SNDRV_PCM_INFO_PAUSE,
  68. .formats = SNDRV_PCM_FMTBIT_S16_LE|SNDRV_PCM_FMTBIT_S24_LE|SNDRV_PCM_FMTBIT_S32_LE,
  69. .period_bytes_min = 64,
  70. .period_bytes_max = 8*1024,
  71. .periods_min = 2,
  72. .periods_max = 1024,
  73. .buffer_bytes_max = 128 * 1024,
  74. .rate_min = 8000,
  75. .rate_max = 48000,
  76. .channels_min = 2,
  77. .channels_max = 2,
  78. .fifo_size = 0,
  79. };
  80. static const struct snd_pcm_hardware aml_pcm_capture = {
  81. .info = SNDRV_PCM_INFO_INTERLEAVED|
  82. SNDRV_PCM_INFO_BLOCK_TRANSFER|
  83. SNDRV_PCM_INFO_MMAP |
  84. SNDRV_PCM_INFO_MMAP_VALID |
  85. SNDRV_PCM_INFO_PAUSE,
  86. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  87. .period_bytes_min = 64,
  88. .period_bytes_max = 8*1024,
  89. .periods_min = 2,
  90. .periods_max = 1024,
  91. .buffer_bytes_max = 64 * 1024,
  92. .rate_min = 8000,
  93. .rate_max = 48000,
  94. .channels_min = 2,
  95. .channels_max = 2,
  96. .fifo_size = 0,
  97. };
  98. static char snd_pcm_tmp[32*1024];
  99. /*--------------------------------------------------------------------------*\
  100. * Data types
  101. \*--------------------------------------------------------------------------*/
  102. struct aml_runtime_data {
  103. struct aml_pcm_dma_params *params;
  104. dma_addr_t dma_buffer; /* physical address of dma buffer */
  105. dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
  106. struct snd_pcm *pcm;
  107. audio_stream_t s;
  108. struct timer_list timer; // timeer for playback and capture
  109. };
  110. static unsigned int period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
  111. static struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
  112. .count = ARRAY_SIZE(period_sizes),
  113. .list = period_sizes,
  114. .mask = 0
  115. };
  116. /*--------------------------------------------------------------------------*\
  117. * audio clock gating
  118. \*--------------------------------------------------------------------------*/
  119. static void aml_audio_clock_gating_disable(void)
  120. {
  121. #ifdef _AML_PCM_DEBUG_
  122. printk("***Entered %s:%s\n", __FILE__,__func__);
  123. #endif
  124. /*
  125. aml_clr_reg32_mask(P_HHI_GCLK_MPEG0, (1<<18));
  126. aml_clr_reg32_mask(P_HHI_GCLK_MPEG1, (1<<2)
  127. |(0xba<<6)
  128. );
  129. aml_clr_reg32_mask(P_HHI_GCLK_OTHER, (1<<18)
  130. |(0x6<<14)
  131. );
  132. */ aml_clr_reg32_mask( P_HHI_AUD_CLK_CNTL, (1 << 8));
  133. //printk("P_HHI_GCLK_MPEG0=disable--%#x\n\n", aml_read_reg32(P_HHI_GCLK_MPEG0));
  134. //printk("P_HHI_GCLK_MPEG1=disable--%#x\n\n", aml_read_reg32(P_HHI_GCLK_MPEG1));
  135. //printk("P_HHI_GCLK_OTHER=disable--%#x\n\n", aml_read_reg32(P_HHI_GCLK_OTHER));
  136. }
  137. static void aml_audio_clock_gating_enable(void)
  138. {
  139. #ifdef _AML_PCM_DEBUG_
  140. printk("***Entered %s:%s\n", __FILE__,__func__);
  141. #endif
  142. /* aml_set_reg32_mask(P_HHI_GCLK_MPEG0, (1<<18));
  143. aml_set_reg32_mask(P_HHI_GCLK_MPEG1, (1<<2)
  144. |(0xba<<6)
  145. );
  146. aml_set_reg32_mask(P_HHI_GCLK_OTHER, (1<<18)
  147. |(0x6<<14)
  148. );
  149. */ aml_set_reg32_mask( P_HHI_AUD_CLK_CNTL, (1 << 8));
  150. //printk("P_HHI_GCLK_MPEG0=enable--%#x\n\n", aml_read_reg32(P_HHI_GCLK_MPEG0));
  151. //printk("P_HHI_GCLK_MPEG1=enable--%#x\n\n", aml_read_reg32(P_HHI_GCLK_MPEG1));
  152. //printk("P_HHI_GCLK_OTHER=enable--%#x\n\n", aml_read_reg32(P_HHI_GCLK_OTHER));
  153. }
  154. static void aml_clock_gating(unsigned int status)
  155. {
  156. //printk("-----status=%d\n\n",status);
  157. if(status){
  158. aml_audio_clock_gating_enable();
  159. }
  160. else{
  161. aml_audio_clock_gating_disable();
  162. }
  163. }
  164. /*--------------------------------------------------------------------------*\
  165. * audio codec power management
  166. \*--------------------------------------------------------------------------*/
  167. static int codec_power_switch(struct snd_pcm_substream *substream, unsigned int status)
  168. {
  169. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  170. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  171. #ifdef _AML_PCM_DEBUG_
  172. printk("***Entered %s:%s\n", __FILE__,__func__);
  173. #endif
  174. if(status && codec_dai->driver->ops->startup)
  175. codec_dai->driver->ops->startup(substream, codec_dai);
  176. if(!status && codec_dai->driver->ops->shutdown)
  177. codec_dai->driver->ops->shutdown(substream, codec_dai);
  178. return 0;
  179. }
  180. /*--------------------------------------------------------------------------*\
  181. * Helper functions
  182. \*--------------------------------------------------------------------------*/
  183. static int aml_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  184. int stream)
  185. {
  186. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  187. struct snd_dma_buffer *buf = &substream->dma_buffer;
  188. size_t size = 0;
  189. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  190. size = aml_pcm_hardware.buffer_bytes_max;
  191. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  192. buf->dev.dev = pcm->card->dev;
  193. buf->private_data = NULL;
  194. /* one size for i2s output, another for 958, and 128 for alignment */
  195. buf->area = dma_alloc_coherent(pcm->card->dev, size*2+128,
  196. &buf->addr, GFP_KERNEL);
  197. printk("aml-pcm %d:"
  198. "preallocate_dma_buffer: area=%p, addr=%p, size=%d\n", stream,
  199. (void *) buf->area,
  200. (void *) buf->addr,
  201. size);
  202. aml_pcm_playback_start_addr = (unsigned int)buf->area;
  203. }else{
  204. size = aml_pcm_capture.buffer_bytes_max;
  205. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  206. buf->dev.dev = pcm->card->dev;
  207. buf->private_data = NULL;
  208. buf->area = dma_alloc_coherent(pcm->card->dev, size*2,
  209. &buf->addr, GFP_KERNEL);
  210. printk("aml-pcm %d:"
  211. "preallocate_dma_buffer: area=%p, addr=%p, size=%d\n", stream,
  212. (void *) buf->area,
  213. (void *) buf->addr,
  214. size);
  215. aml_pcm_capture_start_addr = (unsigned int)buf->area;
  216. aml_pcm_capture_start_phy = buf->addr;
  217. aml_pcm_capture_buf_size = size;
  218. }
  219. if (!buf->area)
  220. return -ENOMEM;
  221. buf->bytes = size;
  222. return 0;
  223. }
  224. /*--------------------------------------------------------------------------*\
  225. * ISR
  226. \*--------------------------------------------------------------------------*/
  227. /*--------------------------------------------------------------------------*\
  228. * PCM operations
  229. \*--------------------------------------------------------------------------*/
  230. static int aml_pcm_hw_params(struct snd_pcm_substream *substream,
  231. struct snd_pcm_hw_params *params)
  232. {
  233. struct snd_pcm_runtime *runtime = substream->runtime;
  234. struct aml_runtime_data *prtd = runtime->private_data;
  235. // struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  236. audio_stream_t *s = &prtd->s;
  237. /* this may get called several times by oss emulation
  238. * with different params */
  239. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  240. runtime->dma_bytes = params_buffer_bytes(params);
  241. s->I2S_addr = runtime->dma_addr;
  242. if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  243. /* s->last_ptr must initialized as dma buffer's start addr */
  244. s->last_ptr = runtime->dma_addr;
  245. }
  246. return 0;
  247. }
  248. static int aml_pcm_hw_free(struct snd_pcm_substream *substream)
  249. {
  250. struct aml_runtime_data *prtd = substream->runtime->private_data;
  251. struct aml_pcm_dma_params *params = prtd->params;
  252. if (params != NULL) {
  253. }
  254. return 0;
  255. }
  256. static int aml_pcm_prepare(struct snd_pcm_substream *substream)
  257. {
  258. struct snd_pcm_runtime *runtime = substream->runtime;
  259. struct aml_runtime_data *prtd = runtime->private_data;
  260. audio_stream_t *s = &prtd->s;
  261. int iec958 = 0;
  262. if(prtd == 0)
  263. return 0;
  264. switch(runtime->rate){
  265. case 192000:
  266. s->sample_rate = AUDIO_CLK_FREQ_192;
  267. break;
  268. case 176400:
  269. s->sample_rate = AUDIO_CLK_FREQ_1764;
  270. break;
  271. case 96000:
  272. s->sample_rate = AUDIO_CLK_FREQ_96;
  273. break;
  274. case 88200:
  275. s->sample_rate = AUDIO_CLK_FREQ_882;
  276. break;
  277. case 48000:
  278. s->sample_rate = AUDIO_CLK_FREQ_48;
  279. iec958 = 2;
  280. break;
  281. case 44100:
  282. s->sample_rate = AUDIO_CLK_FREQ_441;
  283. iec958 = 0;
  284. break;
  285. case 32000:
  286. s->sample_rate = AUDIO_CLK_FREQ_32;
  287. iec958 = 3;
  288. break;
  289. case 8000:
  290. s->sample_rate = AUDIO_CLK_FREQ_8;
  291. break;
  292. case 11025:
  293. s->sample_rate = AUDIO_CLK_FREQ_11;
  294. break;
  295. case 16000:
  296. s->sample_rate = AUDIO_CLK_FREQ_16;
  297. break;
  298. case 22050:
  299. s->sample_rate = AUDIO_CLK_FREQ_22;
  300. break;
  301. case 12000:
  302. s->sample_rate = AUDIO_CLK_FREQ_12;
  303. break;
  304. case 24000:
  305. s->sample_rate = AUDIO_CLK_FREQ_22;
  306. break;
  307. default:
  308. s->sample_rate = AUDIO_CLK_FREQ_441;
  309. break;
  310. };
  311. audio_set_clk(s->sample_rate, AUDIO_CLK_256FS);
  312. audio_util_set_dac_format(AUDIO_ALGOUT_DAC_FORMAT_DSP);
  313. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  314. //printk("aml_pcm_prepare SNDRV_PCM_STREAM_PLAYBACK: dma_addr=%x, dma_bytes=%x\n", runtime->dma_addr, runtime->dma_bytes);
  315. _aiu_958_channel_status_t set;
  316. _aiu_958_raw_setting_t raw_set;
  317. memset((void*)(&raw_set), 0, sizeof(raw_set));
  318. memset((void*)(&set), 0, sizeof(set));
  319. raw_set.chan_stat = &set;
  320. audio_set_aiubuf(runtime->dma_addr, runtime->dma_bytes);
  321. switch(runtime->format){
  322. case SNDRV_PCM_FORMAT_S32_LE:
  323. I2S_MODE = AIU_I2S_MODE_PCM32;
  324. IEC958_MODE = AIU_958_MODE_PCM32;
  325. break;
  326. case SNDRV_PCM_FORMAT_S24_LE:
  327. I2S_MODE = AIU_I2S_MODE_PCM24;
  328. IEC958_MODE = AIU_958_MODE_PCM24;
  329. break;
  330. case SNDRV_PCM_FORMAT_S16_LE:
  331. I2S_MODE = AIU_I2S_MODE_PCM16;
  332. IEC958_MODE = AIU_958_MODE_PCM16;
  333. break;
  334. }
  335. audio_set_i2s_mode(I2S_MODE);
  336. if(IEC958_MODE == AIU_958_MODE_PCM16 || IEC958_MODE == AIU_958_MODE_PCM24 ||
  337. IEC958_MODE == AIU_958_MODE_PCM32){
  338. set.chstat0_l = 0x0100;
  339. set.chstat0_r = 0x0100;
  340. audio_set_958outbuf(runtime->dma_addr, runtime->dma_bytes);
  341. }else{
  342. set.chstat0_l = 0x1902;
  343. set.chstat0_r = 0x1902;
  344. audio_set_958outbuf((runtime->dma_addr+runtime->dma_bytes+127)&(~127), runtime->dma_bytes);
  345. }
  346. audio_set_958_mode(IEC958_MODE, &raw_set);
  347. memset((void*)runtime->dma_area,0,runtime->dma_bytes * 2 + 128);
  348. }
  349. else{
  350. //printk("aml_pcm_prepare SNDRV_PCM_STREAM_CAPTURE: dma_addr=%x, dma_bytes=%x\n", runtime->dma_addr, runtime->dma_bytes);
  351. audio_in_i2s_set_buf(runtime->dma_addr, runtime->dma_bytes*2);
  352. memset((void*)runtime->dma_area,0,runtime->dma_bytes*2);
  353. {
  354. int * ppp = (int*)(runtime->dma_area+runtime->dma_bytes*2-8);
  355. ppp[0] = 0x78787878;
  356. ppp[1] = 0x78787878;
  357. }
  358. }
  359. aout_notifier_call_chain(AOUT_EVENT_PREPARE, substream);
  360. #if 0
  361. printk("Audio Parameters:\n");
  362. printk("\tsample rate: %d\n", runtime->rate);
  363. printk("\tchannel: %d\n", runtime->channels);
  364. printk("\tsample bits: %d\n", runtime->sample_bits);
  365. printk("\tformat: %s\n", snd_pcm_format_name(runtime->format));
  366. printk("\tperiod size: %ld\n", runtime->period_size);
  367. printk("\tperiods: %d\n", runtime->periods);
  368. #endif
  369. return 0;
  370. }
  371. static int aml_pcm_trigger(struct snd_pcm_substream *substream,
  372. int cmd)
  373. {
  374. struct snd_pcm_runtime *rtd = substream->runtime;
  375. struct aml_runtime_data *prtd = rtd->private_data;
  376. audio_stream_t *s = &prtd->s;
  377. int ret = 0;
  378. switch (cmd) {
  379. case SNDRV_PCM_TRIGGER_START:
  380. #ifdef CONFIG_ARCH_MESON6
  381. switch_mod_gate_by_type(MOD_AUDIO, 1);
  382. #endif
  383. del_timer_sync(&prtd->timer);
  384. spin_lock(&s->lock);
  385. prtd->timer.expires = jiffies + 1;
  386. del_timer(&prtd->timer);
  387. add_timer(&prtd->timer);
  388. // TODO
  389. if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  390. // printk("aml_pcm_trigger: playback start\n");
  391. clock_gating_status |= clock_gating_playback;
  392. //aml_clock_gating(clock_gating_status);
  393. //codec_power_switch(substream, clock_gating_status);
  394. audio_enable_ouput(1);
  395. }else{
  396. // printk("aml_pcm_trigger: capture start\n");
  397. clock_gating_status |= clock_gating_capture;
  398. //aml_clock_gating(clock_gating_status);
  399. //codec_power_switch(substream, clock_gating_status);
  400. audio_in_i2s_enable(1);
  401. {
  402. int * ppp = (int*)(rtd->dma_area+rtd->dma_bytes*2-8);
  403. ppp[0] = 0x78787878;
  404. ppp[1] = 0x78787878;
  405. }
  406. }
  407. s->active = 1;
  408. spin_unlock(&s->lock);
  409. break; /* SNDRV_PCM_TRIGGER_START */
  410. case SNDRV_PCM_TRIGGER_SUSPEND:
  411. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  412. case SNDRV_PCM_TRIGGER_STOP:
  413. // TODO
  414. spin_lock(&s->lock);
  415. s->active = 0;
  416. if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  417. // printk("aml_pcm_trigger: playback stop\n");
  418. audio_enable_ouput(0);
  419. clock_gating_status &= clock_gating_capture;
  420. //aml_clock_gating(clock_gating_status);
  421. //codec_power_switch(substream, clock_gating_status);
  422. }else{
  423. // printk("aml_pcm_trigger: capture stop\n");
  424. audio_in_i2s_enable(0);
  425. }
  426. #ifdef CONFIG_ARCH_MESON6
  427. switch_mod_gate_by_type(MOD_AUDIO, 0);
  428. #endif
  429. spin_unlock(&s->lock);
  430. break;
  431. case SNDRV_PCM_TRIGGER_RESUME:
  432. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  433. // TODO
  434. spin_lock(&s->lock);
  435. #ifdef CONFIG_ARCH_MESON6
  436. switch_mod_gate_by_type(MOD_AUDIO, 1);
  437. #endif
  438. s->active = 1;
  439. if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  440. // printk("aml_pcm_trigger: playback resume\n");
  441. audio_enable_ouput(1);
  442. clock_gating_status |= clock_gating_playback;
  443. //aml_clock_gating(clock_gating_status);
  444. //codec_power_switch(substream, clock_gating_status);
  445. }else{
  446. // printk("aml_pcm_trigger: capture resume\n");
  447. audio_in_i2s_enable(1);
  448. clock_gating_status |= clock_gating_capture;
  449. //aml_clock_gating(clock_gating_status);
  450. //codec_power_switch(substream, clock_gating_status);
  451. {
  452. int * ppp = (int*)(rtd->dma_area+rtd->dma_bytes*2-8);
  453. ppp[0] = 0x78787878;
  454. ppp[1] = 0x78787878;
  455. }
  456. }
  457. spin_unlock(&s->lock);
  458. break;
  459. default:
  460. ret = -EINVAL;
  461. }
  462. aml_pcm_work.substream = substream;
  463. schedule_work(&aml_pcm_work.aml_codec_workqueue);
  464. return ret;
  465. }
  466. static snd_pcm_uframes_t aml_pcm_pointer(
  467. struct snd_pcm_substream *substream)
  468. {
  469. struct snd_pcm_runtime *runtime = substream->runtime;
  470. struct aml_runtime_data *prtd = runtime->private_data;
  471. audio_stream_t *s = &prtd->s;
  472. unsigned int addr, ptr;
  473. if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  474. ptr = read_i2s_rd_ptr();
  475. addr = ptr - s->I2S_addr;
  476. return bytes_to_frames(runtime, addr);
  477. }else{
  478. ptr = audio_in_i2s_wr_ptr();
  479. addr = ptr - s->I2S_addr;
  480. return bytes_to_frames(runtime, addr)/2;
  481. }
  482. return 0;
  483. }
  484. static void aml_pcm_timer_callback(unsigned long data)
  485. {
  486. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)data;
  487. struct snd_pcm_runtime *runtime = substream->runtime;
  488. struct aml_runtime_data *prtd = runtime->private_data;
  489. audio_stream_t *s = &prtd->s;
  490. unsigned int last_ptr, size;
  491. if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  492. if(s->active == 1){
  493. spin_lock(&s->lock);
  494. last_ptr = read_i2s_rd_ptr();
  495. if (last_ptr < s->last_ptr) {
  496. size = runtime->dma_bytes + last_ptr - (s->last_ptr);
  497. } else {
  498. size = last_ptr - (s->last_ptr);
  499. }
  500. s->last_ptr = last_ptr;
  501. s->size += bytes_to_frames(substream->runtime, size);
  502. if (s->size >= runtime->period_size) {
  503. s->size %= runtime->period_size;
  504. spin_unlock(&s->lock);
  505. snd_pcm_period_elapsed(substream);
  506. spin_lock(&s->lock);
  507. }
  508. mod_timer(&prtd->timer, jiffies + 1);
  509. spin_unlock(&s->lock);
  510. }else{
  511. mod_timer(&prtd->timer, jiffies + 1);
  512. }
  513. }else{
  514. if(s->active == 1){
  515. spin_lock(&s->lock);
  516. last_ptr = (audio_in_i2s_wr_ptr() - s->I2S_addr) / 2;
  517. if (last_ptr < s->last_ptr) {
  518. size = runtime->dma_bytes + last_ptr - (s->last_ptr);
  519. } else {
  520. size = last_ptr - (s->last_ptr);
  521. }
  522. s->last_ptr = last_ptr;
  523. s->size += bytes_to_frames(substream->runtime, size);
  524. if (s->size >= runtime->period_size) {
  525. s->size %= runtime->period_size;
  526. spin_unlock(&s->lock);
  527. snd_pcm_period_elapsed(substream);
  528. spin_lock(&s->lock);
  529. }
  530. mod_timer(&prtd->timer, jiffies + 1);
  531. spin_unlock(&s->lock);
  532. }else{
  533. mod_timer(&prtd->timer, jiffies + 1);
  534. }
  535. }
  536. }
  537. static int aml_pcm_open(struct snd_pcm_substream *substream)
  538. {
  539. struct snd_pcm_runtime *runtime = substream->runtime;
  540. struct aml_runtime_data *prtd;
  541. int ret = 0;
  542. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  543. snd_soc_set_runtime_hwparams(substream, &aml_pcm_hardware);
  544. }else{
  545. snd_soc_set_runtime_hwparams(substream, &aml_pcm_capture);
  546. }
  547. /* ensure that peroid size is a multiple of 32bytes */
  548. ret = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_period_sizes);
  549. if (ret < 0)
  550. {
  551. printk("set period bytes constraint error\n");
  552. goto out;
  553. }
  554. /* ensure that buffer size is a multiple of period size */
  555. ret = snd_pcm_hw_constraint_integer(runtime,
  556. SNDRV_PCM_HW_PARAM_PERIODS);
  557. if (ret < 0)
  558. {
  559. printk("set period error\n");
  560. goto out;
  561. }
  562. prtd = kzalloc(sizeof(struct aml_runtime_data), GFP_KERNEL);
  563. if (prtd == NULL) {
  564. printk("alloc aml_runtime_data error\n");
  565. ret = -ENOMEM;
  566. goto out;
  567. }
  568. prtd->pcm = substream->pcm;
  569. prtd->timer.function = &aml_pcm_timer_callback;
  570. prtd->timer.data = (unsigned long)substream;
  571. init_timer(&prtd->timer);
  572. runtime->private_data = prtd;
  573. spin_lock_init(&prtd->s.lock);
  574. out:
  575. return ret;
  576. }
  577. static int aml_pcm_close(struct snd_pcm_substream *substream)
  578. {
  579. struct aml_runtime_data *prtd = substream->runtime->private_data;
  580. del_timer_sync(&prtd->timer);
  581. kfree(prtd);
  582. return 0;
  583. }
  584. static int aml_pcm_copy_playback(struct snd_pcm_runtime *runtime, int channel,
  585. snd_pcm_uframes_t pos,
  586. void __user *buf, snd_pcm_uframes_t count)
  587. {
  588. int res = 0;
  589. int n;
  590. int i = 0, j = 0;
  591. int align = runtime->channels * 32 / runtime->byte_align;
  592. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, pos);
  593. n = frames_to_bytes(runtime, count);
  594. if(aml_pcm_playback_enable == 0)
  595. return res;
  596. if(access_ok(VERIFY_READ, buf, frames_to_bytes(runtime, count))){
  597. if(runtime->format == SNDRV_PCM_FORMAT_S16_LE && I2S_MODE == AIU_I2S_MODE_PCM16){
  598. int16_t * tfrom, *to, *left, *right;
  599. tfrom = (int16_t*)buf;
  600. to = (int16_t*)hwbuf;
  601. left = to;
  602. right = to + 16;
  603. if (pos % align) {
  604. printk("audio data unligned: pos=%d, n=%d, align=%d\n", (int)pos, n, align);
  605. }
  606. for (j = 0; j < n; j += 64) {
  607. for (i = 0; i < 16; i++) {
  608. *left++ = (*tfrom++) ;
  609. *right++ = (*tfrom++);
  610. }
  611. left += 16;
  612. right += 16;
  613. }
  614. }else if(runtime->format == SNDRV_PCM_FORMAT_S24_LE && I2S_MODE == AIU_I2S_MODE_PCM24){
  615. int32_t *tfrom, *to, *left, *right;
  616. tfrom = (int32_t*)buf;
  617. to = (int32_t*) hwbuf;
  618. left = to;
  619. right = to + 8;
  620. if(pos % align){
  621. printk("audio data unaligned: pos=%d, n=%d, align=%d\n", (int)pos, n, align);
  622. }
  623. for(j=0; j< n; j+= 64){
  624. for(i=0; i<8; i++){
  625. *left++ = (*tfrom ++);
  626. *right++ = (*tfrom ++);
  627. }
  628. left += 8;
  629. right += 8;
  630. }
  631. }else if(runtime->format == SNDRV_PCM_FORMAT_S32_LE && I2S_MODE == AIU_I2S_MODE_PCM32){
  632. int32_t *tfrom, *to, *left, *right;
  633. tfrom = (int32_t*)buf;
  634. to = (int32_t*) hwbuf;
  635. left = to;
  636. right = to + 8;
  637. if(pos % align){
  638. printk("audio data unaligned: pos=%d, n=%d, align=%d\n", (int)pos, n, align);
  639. }
  640. for(j=0; j< n; j+= 64){
  641. for(i=0; i<8; i++){
  642. *left++ = (*tfrom ++)>>8;
  643. *right++ = (*tfrom ++)>>8;
  644. }
  645. left += 8;
  646. right += 8;
  647. }
  648. }
  649. }else{
  650. res = -EFAULT;
  651. }
  652. return res;
  653. }
  654. static int aml_pcm_copy_capture(struct snd_pcm_runtime *runtime, int channel,
  655. snd_pcm_uframes_t pos,
  656. void __user *buf, snd_pcm_uframes_t count)
  657. {
  658. unsigned int *tfrom, *left, *right;
  659. unsigned short *to;
  660. int res = 0;
  661. int n;
  662. int i = 0, j = 0;
  663. unsigned int t1, t2;
  664. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, pos)*2;
  665. to = (unsigned short *)snd_pcm_tmp;//buf;
  666. tfrom = (unsigned int *)hwbuf; // 32bit buffer
  667. n = frames_to_bytes(runtime, count);
  668. if(n > 32*1024){
  669. printk("Too many datas to read\n");
  670. return -EINVAL;
  671. }
  672. if(access_ok(VERIFY_WRITE, buf, frames_to_bytes(runtime, count))){
  673. left = tfrom;
  674. right = tfrom + 8;
  675. if (pos % 8) {
  676. printk("audio data unligned\n");
  677. }
  678. if((n*2)%64){
  679. printk("audio data unaligned 64 bytes\n");
  680. }
  681. for (j = 0; j < n*2 ; j += 64) {
  682. for (i = 0; i < 8; i++) {
  683. t1 = (*left++);
  684. t2 = (*right++);
  685. //printk("%08x,%08x,", t1, t2);
  686. *to++ = (unsigned short)((t1>>8)&0xffff);
  687. *to++ = (unsigned short)((t1>>8)&0xffff);//copy left channel to right
  688. //*to++ = (unsigned short)((t2>>8)&0xffff);
  689. }
  690. //printk("\n");
  691. left += 8;
  692. right += 8;
  693. }
  694. }
  695. res = copy_to_user(buf, snd_pcm_tmp,n);
  696. return res;
  697. }
  698. static int aml_pcm_copy(struct snd_pcm_substream *substream, int channel,
  699. snd_pcm_uframes_t pos,
  700. void __user *buf, snd_pcm_uframes_t count)
  701. {
  702. struct snd_pcm_runtime *runtime = substream->runtime;
  703. int ret = 0;
  704. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK){
  705. ret = aml_pcm_copy_playback(runtime, channel,pos, buf, count);
  706. }else{
  707. ret = aml_pcm_copy_capture(runtime, channel,pos, buf, count);
  708. }
  709. return ret;
  710. }
  711. int aml_pcm_silence(struct snd_pcm_substream *substream, int channel,
  712. snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  713. {
  714. char* ppos;
  715. int n;
  716. struct snd_pcm_runtime *runtime = substream->runtime;
  717. n = frames_to_bytes(runtime, count);
  718. ppos = runtime->dma_area + frames_to_bytes(runtime, pos);
  719. memset(ppos, 0, n);
  720. return 0;
  721. }
  722. static struct snd_pcm_ops aml_pcm_ops = {
  723. .open = aml_pcm_open,
  724. .close = aml_pcm_close,
  725. .ioctl = snd_pcm_lib_ioctl,
  726. .hw_params = aml_pcm_hw_params,
  727. .hw_free = aml_pcm_hw_free,
  728. .prepare = aml_pcm_prepare,
  729. .trigger = aml_pcm_trigger,
  730. .pointer = aml_pcm_pointer,
  731. .copy = aml_pcm_copy,
  732. .silence = aml_pcm_silence,
  733. };
  734. /*--------------------------------------------------------------------------*\
  735. * ASoC platform driver
  736. \*--------------------------------------------------------------------------*/
  737. static u64 aml_pcm_dmamask = 0xffffffff;
  738. static int aml_pcm_new(struct snd_card *card,
  739. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  740. {
  741. int ret = 0;
  742. if (!card->dev->dma_mask)
  743. card->dev->dma_mask = &aml_pcm_dmamask;
  744. if (!card->dev->coherent_dma_mask)
  745. card->dev->coherent_dma_mask = 0xffffffff;
  746. if (dai->driver->playback.channels_min) {
  747. ret = aml_pcm_preallocate_dma_buffer(pcm,
  748. SNDRV_PCM_STREAM_PLAYBACK);
  749. if (ret)
  750. goto out;
  751. }
  752. if (dai->driver->capture.channels_min) {
  753. pr_debug("aml-pcm:"
  754. "Allocating PCM capture DMA buffer\n");
  755. ret = aml_pcm_preallocate_dma_buffer(pcm,
  756. SNDRV_PCM_STREAM_CAPTURE);
  757. if (ret)
  758. goto out;
  759. }
  760. out:
  761. return ret;
  762. }
  763. static void aml_pcm_free_dma_buffers(struct snd_pcm *pcm)
  764. {
  765. struct snd_pcm_substream *substream;
  766. struct snd_dma_buffer *buf;
  767. int stream;
  768. for (stream = 0; stream < 2; stream++) {
  769. substream = pcm->streams[stream].substream;
  770. if (!substream)
  771. continue;
  772. buf = &substream->dma_buffer;
  773. if (!buf->area)
  774. continue;
  775. dma_free_coherent(pcm->card->dev, buf->bytes,
  776. buf->area, buf->addr);
  777. buf->area = NULL;
  778. }
  779. aml_pcm_playback_start_addr = 0;
  780. aml_pcm_capture_start_addr = 0;
  781. }
  782. #ifdef CONFIG_PM
  783. static int aml_pcm_suspend(struct snd_soc_dai *dai)
  784. {
  785. struct snd_pcm_runtime *runtime = dai->runtime;
  786. struct aml_runtime_data *prtd;
  787. struct aml_pcm_dma_params *params;
  788. if (!runtime)
  789. return 0;
  790. prtd = runtime->private_data;
  791. params = prtd->params;
  792. /* disable the PDC and save the PDC registers */
  793. // TODO
  794. printk("aml pcm suspend\n");
  795. return 0;
  796. }
  797. static int aml_pcm_resume(struct snd_soc_dai *dai)
  798. {
  799. struct snd_pcm_runtime *runtime = dai->runtime;
  800. struct aml_runtime_data *prtd;
  801. struct aml_pcm_dma_params *params;
  802. if (!runtime)
  803. return 0;
  804. prtd = runtime->private_data;
  805. params = prtd->params;
  806. /* restore the PDC registers and enable the PDC */
  807. // TODO
  808. printk("aml pcm resume\n");
  809. return 0;
  810. }
  811. #else
  812. #define aml_pcm_suspend NULL
  813. #define aml_pcm_resume NULL
  814. #endif
  815. #ifdef CONFIG_DEBUG_FS
  816. static struct dentry *debugfs_root;
  817. static struct dentry *debugfs_regs;
  818. static struct dentry *debugfs_mems;
  819. static int regs_open_file(struct inode *inode, struct file *file)
  820. {
  821. return 0;
  822. }
  823. /**
  824. * cat regs
  825. */
  826. static ssize_t regs_read_file(struct file *file, char __user *user_buf,
  827. size_t count, loff_t *ppos)
  828. {
  829. ssize_t ret;
  830. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  831. if (!buf)
  832. return -ENOMEM;
  833. ret = sprintf(buf, "Usage: \n"
  834. " echo base reg val >regs\t(set the register)\n"
  835. " echo base reg >regs\t(show the register)\n"
  836. " base -> c(cbus), x(aix), p(apb), h(ahb) \n"
  837. );
  838. if (ret >= 0)
  839. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  840. kfree(buf);
  841. return ret;
  842. }
  843. static int read_regs(char base, int reg)
  844. {
  845. int val = 0;
  846. switch(base){
  847. case 'c':
  848. val = READ_CBUS_REG(reg);
  849. break;
  850. case 'x':
  851. val = READ_AXI_REG(reg);
  852. break;
  853. case 'p':
  854. val = READ_APB_REG(reg);
  855. break;
  856. case 'h':
  857. val = READ_AHB_REG(reg);
  858. break;
  859. default:
  860. break;
  861. };
  862. printk("\tReg %x = %x\n", reg, val);
  863. return val;
  864. }
  865. static void write_regs(char base, int reg, int val)
  866. {
  867. switch(base){
  868. case 'c':
  869. WRITE_CBUS_REG(reg, val);
  870. break;
  871. case 'x':
  872. WRITE_AXI_REG(reg, val);
  873. break;
  874. case 'p':
  875. WRITE_APB_REG(reg, val);
  876. break;
  877. case 'h':
  878. WRITE_AHB_REG(reg, val);
  879. break;
  880. default:
  881. break;
  882. };
  883. printk("Write reg:%x = %x\n", reg, val);
  884. }
  885. static ssize_t regs_write_file(struct file *file,
  886. const char __user *user_buf, size_t count, loff_t *ppos)
  887. {
  888. char buf[32];
  889. int buf_size = 0;
  890. char *start = buf;
  891. unsigned long reg, value;
  892. char base;
  893. buf_size = min(count, (sizeof(buf)-1));
  894. if (copy_from_user(buf, user_buf, buf_size))
  895. return -EFAULT;
  896. buf[buf_size] = 0;
  897. while (*start == ' ')
  898. start++;
  899. base = *start;
  900. start ++;
  901. if(!(base =='c' || base == 'x' || base == 'p' || base == 'h')){
  902. return -EINVAL;
  903. }
  904. while (*start == ' ')
  905. start++;
  906. reg = simple_strtoul(start, &start, 16);
  907. while (*start == ' ')
  908. start++;
  909. if (strict_strtoul(start, 16, &value))
  910. {
  911. read_regs(base, reg);
  912. return -EINVAL;
  913. }
  914. write_regs(base, reg, value);
  915. return buf_size;
  916. }
  917. static const struct file_operations regs_fops = {
  918. .open = regs_open_file,
  919. .read = regs_read_file,
  920. .write = regs_write_file,
  921. };
  922. static int mems_open_file(struct inode *inode, struct file *file)
  923. {
  924. return 0;
  925. }
  926. static ssize_t mems_read_file(struct file *file, char __user *user_buf,
  927. size_t count, loff_t *ppos)
  928. {
  929. ssize_t ret;
  930. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  931. if (!buf)
  932. return -ENOMEM;
  933. ret = sprintf(buf, "Usage: \n"
  934. " echo vmem >mems\t(read 64 bytes from vmem)\n"
  935. " echo vmem val >mems (write int value to vmem\n"
  936. );
  937. if (ret >= 0)
  938. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  939. kfree(buf);
  940. return ret;
  941. }
  942. static ssize_t mems_write_file(struct file *file,
  943. const char __user *user_buf, size_t count, loff_t *ppos)
  944. {
  945. char buf[256];
  946. int buf_size = 0;
  947. char *start = buf;
  948. unsigned long mem, value;
  949. int i=0;
  950. unsigned* addr = 0;
  951. buf_size = min(count, (sizeof(buf)-1));
  952. if (copy_from_user(buf, user_buf, buf_size))
  953. return -EFAULT;
  954. buf[buf_size] = 0;
  955. while (*start == ' ')
  956. start++;
  957. mem = simple_strtoul(start, &start, 16);
  958. while (*start == ' ')
  959. start++;
  960. if (strict_strtoul(start, 16, &value))
  961. {
  962. addr = (unsigned*)mem;
  963. printk("%p: ", addr);
  964. for(i = 0; i< 8; i++){
  965. printk("%08x, ", addr[i]);
  966. }
  967. printk("\n");
  968. return -EINVAL;
  969. }
  970. addr = (unsigned*)mem;
  971. printk("%p: %08x\n", addr, *addr);
  972. *addr = value;
  973. printk("%p: %08x^\n", addr, *addr);
  974. return buf_size;
  975. }
  976. static const struct file_operations mems_fops={
  977. .open = mems_open_file,
  978. .read = mems_read_file,
  979. .write = mems_write_file,
  980. };
  981. static void aml_pcm_init_debugfs(void)
  982. {
  983. debugfs_root = debugfs_create_dir("aml",NULL);
  984. if (IS_ERR(debugfs_root) || !debugfs_root) {
  985. printk("aml: Failed to create debugfs directory\n");
  986. debugfs_root = NULL;
  987. }
  988. debugfs_regs = debugfs_create_file("regs", 0644, debugfs_root, NULL, &regs_fops);
  989. if(!debugfs_regs){
  990. printk("aml: Failed to create debugfs file\n");
  991. }
  992. debugfs_mems = debugfs_create_file("mems", 0644, debugfs_root, NULL, &mems_fops);
  993. if(!debugfs_mems){
  994. printk("aml: Failed to create debugfs file\n");
  995. }
  996. }
  997. static void aml_pcm_cleanup_debugfs(void)
  998. {
  999. debugfs_remove_recursive(debugfs_root);
  1000. }
  1001. #else
  1002. static void aml_pcm_init_debugfs(void)
  1003. {
  1004. }
  1005. static void aml_pcm_cleanup_debugfs(void)
  1006. {
  1007. }
  1008. #endif
  1009. struct snd_soc_platform_driver aml_soc_platform = {
  1010. .ops = &aml_pcm_ops,
  1011. .pcm_new = aml_pcm_new,
  1012. .pcm_free = aml_pcm_free_dma_buffers,
  1013. .suspend = aml_pcm_suspend,
  1014. .resume = aml_pcm_resume,
  1015. };
  1016. EXPORT_SYMBOL_GPL(aml_soc_platform);
  1017. static int __devinit aml_soc_platform_probe(struct platform_device *pdev)
  1018. {
  1019. INIT_WORK(&aml_pcm_work.aml_codec_workqueue, aml_codec_power_switch_queue);
  1020. return snd_soc_register_platform(&pdev->dev, &aml_soc_platform);
  1021. }
  1022. static int __devexit aml_soc_platform_remove(struct platform_device *pdev)
  1023. {
  1024. snd_soc_unregister_platform(&pdev->dev);
  1025. return 0;
  1026. }
  1027. static struct platform_driver aml_pcm_driver = {
  1028. .driver = {
  1029. .name = "aml-audio",
  1030. .owner = THIS_MODULE,
  1031. },
  1032. .probe = aml_soc_platform_probe,
  1033. .remove = __devexit_p(aml_soc_platform_remove),
  1034. };
  1035. static int __init aml_alsa_audio_init(void)
  1036. {
  1037. aml_pcm_init_debugfs();
  1038. return platform_driver_register(&aml_pcm_driver);
  1039. }
  1040. static void __exit aml_alsa_audio_exit(void)
  1041. {
  1042. aml_pcm_cleanup_debugfs();
  1043. platform_driver_unregister(&aml_pcm_driver);
  1044. }
  1045. module_init(aml_alsa_audio_init);
  1046. module_exit(aml_alsa_audio_exit);
  1047. MODULE_AUTHOR("AMLogic, Inc.");
  1048. MODULE_LICENSE("GPL");
  1049. MODULE_DESCRIPTION("AML audio driver for ALSA");