audiodsp_module.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. #include <linux/version.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/types.h>
  5. #include <linux/string.h>
  6. #include <linux/io.h>
  7. #include <linux/fs.h>
  8. #include <linux/mm.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mutex.h>
  11. #include <linux/device.h>
  12. #include <linux/timer.h>
  13. #include <linux/delay.h>
  14. #include <asm/cacheflush.h>
  15. //#include <asm/arch/am_regs.h>
  16. #include <linux/major.h>
  17. #include <linux/slab.h>
  18. #ifdef CONFIG_ARCH_MESON6
  19. #include <mach/mod_gate.h>
  20. #endif
  21. //#include <asm/dsp/audiodsp_control.h>
  22. #include "audiodsp_control.h" // temp here
  23. #include <asm/uaccess.h>
  24. #include <linux/amports/amstream.h>
  25. #include <mach/am_regs.h>
  26. #include "audiodsp_module.h"
  27. #include "dsp_control.h"
  28. #include "dsp_microcode.h"
  29. #include "dsp_mailbox.h"
  30. #include "dsp_monitor.h"
  31. #include "dsp_codec.h"
  32. #include <linux/dma-mapping.h>
  33. #include <linux/amports/ptsserv.h>
  34. #include <linux/amports/timestamp.h>
  35. #include <linux/amports/tsync.h>
  36. extern void tsync_pcr_recover(void);extern void tsync_pcr_recover(void);
  37. MODULE_DESCRIPTION("AMLOGIC APOLLO Audio dsp driver");
  38. MODULE_LICENSE("GPL");
  39. MODULE_AUTHOR("Zhou Zhi <zhi.zhou@amlogic.com>");
  40. MODULE_VERSION("1.0.0");
  41. unsigned IEC958_mode_raw;
  42. unsigned IEC958_mode_codec;
  43. extern struct audio_info * get_audio_info(void);
  44. void audiodsp_moniter(unsigned long);
  45. static struct audiodsp_priv *audiodsp_p;
  46. #define DSP_DRIVER_NAME "audiodsp"
  47. #define DSP_NAME "dsp"
  48. /**
  49. * Audio codec necessary MIPS (KHz)
  50. */
  51. static unsigned int audiodsp_mips[]={
  52. 200000, //#define MCODEC_FMT_MPEG123 (1<<0)
  53. 200000, //#define MCODEC_FMT_AAC (1<<1)
  54. 200000, //#define MCODEC_FMT_AC3 (1<<2)
  55. 200000, //#define MCODEC_FMT_DTS (1<<3)
  56. 200000, //#define MCODEC_FMT_FLAC (1<<4)
  57. 200000, //#define MCODEC_FMT_COOK (1<<5)
  58. 200000, //#define MCODEC_FMT_AMR (1<<6)
  59. 200000, //#define MCODEC_FMT_RAAC (1<<7)
  60. 200000, //#define MCODEC_FMT_ADPCM (1<<8)
  61. 200000, //#define MCODEC_FMT_WMA (1<<9)
  62. 200000, //#define MCODEC_FMT_PCM (1<<10)
  63. 200000, //#define MCODEC_FMT_WMAPRO (1<<11)
  64. };
  65. #ifdef CONFIG_PM
  66. typedef struct {
  67. int event;
  68. //
  69. }audiodsp_pm_state_t;
  70. static audiodsp_pm_state_t pm_state;
  71. #endif
  72. static void audiodsp_prevent_sleep(void)
  73. {
  74. struct audiodsp_priv* priv = audiodsp_privdata();
  75. printk("audiodsp prevent sleep\n");
  76. wake_lock(&priv->wakelock);
  77. }
  78. static void audiodsp_allow_sleep(void)
  79. {
  80. struct audiodsp_priv *priv=audiodsp_privdata();
  81. printk("audiodsp allow sleep\n");
  82. wake_unlock(&priv->wakelock);
  83. }
  84. int audiodsp_start(void)
  85. {
  86. struct audiodsp_priv *priv=audiodsp_privdata();
  87. struct audiodsp_microcode *pmcode;
  88. struct audio_info *audio_info;
  89. int ret,i;
  90. priv->frame_format.valid=0;
  91. priv->decode_error_count=0;
  92. priv->last_valid_pts=0;
  93. priv->out_len_after_last_valid_pts = 0;
  94. priv->decode_fatal_err = 0;
  95. priv->first_lookup_over = 0;
  96. pmcode=audiodsp_find_supoort_mcode(priv,priv->stream_fmt);
  97. if(pmcode==NULL)
  98. {
  99. DSP_PRNT("have not find a valid mcode for fmt(0x%x)\n",priv->stream_fmt);
  100. return -1;
  101. }
  102. stop_audiodsp_monitor(priv);
  103. dsp_stop(priv);
  104. ret=dsp_start(priv,pmcode);
  105. if(ret==0){
  106. start_audiodsp_monitor(priv);
  107. #ifdef CONFIG_AM_VDEC_REAL
  108. if((pmcode->fmt == MCODEC_FMT_COOK) ||
  109. (pmcode->fmt == MCODEC_FMT_RAAC) ||
  110. (pmcode->fmt == MCODEC_FMT_AMR) ||
  111. (pmcode->fmt == MCODEC_FMT_WMA) ||
  112. (pmcode->fmt == MCODEC_FMT_ADPCM)||
  113. (pmcode->fmt == MCODEC_FMT_PCM) ||
  114. (pmcode->fmt == MCODEC_FMT_WMAPRO)||
  115. (pmcode->fmt == MCODEC_FMT_ALAC)||
  116. (pmcode->fmt == MCODEC_FMT_AC3) ||
  117. (pmcode->fmt == MCODEC_FMT_APE) ||
  118. (pmcode->fmt == MCODEC_FMT_FLAC))
  119. {
  120. DSP_PRNT("dsp send audio info\n");
  121. for(i = 0; i< 2000;i++){
  122. if(DSP_RD(DSP_AUDIOINFO_STATUS) == DSP_AUDIOINFO_READY)//maybe at audiodsp side,INT not enabled yet,so wait a while
  123. break;
  124. msleep(1);
  125. }
  126. if(i == 2000)
  127. DSP_PRNT("audiodsp not ready for info \n");
  128. DSP_WD(DSP_AUDIOINFO_STATUS,0);
  129. audio_info = get_audio_info();
  130. DSP_PRNT("kernel sent info first 4 byte[0x%x],[0x%x],[0x%x],[0x%x]\n\t",audio_info->extradata[0],\
  131. audio_info->extradata[1],audio_info->extradata[2],audio_info->extradata[3]);
  132. DSP_WD(DSP_GET_EXTRA_INFO_FINISH, 0);
  133. while(1){
  134. dsp_mailbox_send(priv, 1, M2B_IRQ4_AUDIO_INFO, 0, (const char*)audio_info, sizeof(struct audio_info));
  135. msleep(100);
  136. if(DSP_RD(DSP_GET_EXTRA_INFO_FINISH) == 0x12345678)
  137. break;
  138. }
  139. }
  140. #endif
  141. }
  142. return ret;
  143. }
  144. static int audiodsp_open(struct inode *node, struct file *file)
  145. {
  146. DSP_PRNT("dsp_open\n");
  147. #ifdef CONFIG_ARCH_MESON6
  148. switch_mod_gate_by_type(MOD_MEDIA_CPU, 1);
  149. /* Audio DSP firmware uses mailbox registers for communications
  150. * with host processor. And these mailbox registers unfortunately
  151. * falls into the assistant module, which is in vdec DOS clock domain.
  152. * So we have to make sure the clock domain is enabled/disabled when
  153. * audio firmware start/stop running.
  154. * Note the module_gating has ref count so a flag 0 does
  155. * not mean the vdec clock is gated off immediately.
  156. */
  157. switch_mod_gate_by_type(MOD_VDEC, 1);
  158. #endif
  159. audiodsp_prevent_sleep();
  160. return 0;
  161. }
  162. static unsigned long audiodsp_drop_pcm(unsigned long size)
  163. {
  164. struct audiodsp_priv *priv = audiodsp_privdata();
  165. size_t len;
  166. int count;
  167. unsigned long drop_bytes = size;
  168. mutex_lock(&priv->stream_buffer_mutex);
  169. if(priv->stream_buffer_mem == NULL || !priv->dsp_is_started)
  170. goto err;
  171. while(drop_bytes > 0){
  172. len =dsp_codec_get_bufer_data_len(priv);
  173. if(drop_bytes >= len){
  174. dsp_codec_inc_rd_addr(priv, len);
  175. drop_bytes -= len;
  176. msleep(50);
  177. count++;
  178. if(count > 20)
  179. break;
  180. }
  181. else {
  182. dsp_codec_inc_rd_addr(priv, drop_bytes);
  183. drop_bytes = 0;
  184. }
  185. }
  186. mutex_unlock(&priv->stream_buffer_mutex);
  187. if(count > 10)
  188. printk("drop pcm data timeout! count = %d\n", count);
  189. return (size - drop_bytes);
  190. err:
  191. mutex_unlock(&priv->stream_buffer_mutex);
  192. printk("error, can not drop pcm data!\n");
  193. return 0;
  194. }
  195. static long audiodsp_ioctl(struct file *file, unsigned int cmd,
  196. unsigned long args)
  197. {
  198. struct audiodsp_priv *priv=audiodsp_privdata();
  199. struct audiodsp_cmd *a_cmd;
  200. char name[64];
  201. int len;
  202. unsigned long pts;
  203. int ret=0;
  204. unsigned long drop_size;
  205. unsigned long *val=(unsigned long *)args;
  206. static int wait_format_times=0;
  207. switch(cmd)
  208. {
  209. case AUDIODSP_SET_FMT:
  210. priv->stream_fmt=args;
  211. if(args == MCODEC_FMT_DTS || args == MCODEC_FMT_AC3){
  212. IEC958_mode_codec = 1;
  213. }
  214. break;
  215. case AUDIODSP_START:
  216. priv->decoded_nb_frames = 0;
  217. priv->format_wait_count = 0;
  218. if(priv->stream_fmt<=0)
  219. {
  220. DSP_PRNT("Audio dsp steam format have not set!\n");
  221. }
  222. else
  223. {
  224. ret=audiodsp_start();
  225. }
  226. break;
  227. case AUDIODSP_STOP:
  228. //DSP_PRNT("audiodsp command stop\n");
  229. stop_audiodsp_monitor(priv);
  230. dsp_stop(priv);
  231. priv->decoded_nb_frames = 0;
  232. priv->format_wait_count = 0;
  233. break;
  234. case AUDIODSP_DECODE_START:
  235. if(priv->dsp_is_started)
  236. {
  237. dsp_codec_start(priv);
  238. wait_format_times=0;
  239. }
  240. else
  241. {
  242. DSP_PRNT("Audio dsp have not started\n");
  243. }
  244. break;
  245. case AUDIODSP_WAIT_FORMAT:
  246. if(priv->dsp_is_started){
  247. int ch = 0;
  248. struct audio_info *audio_format;
  249. audio_format = get_audio_info();
  250. wait_format_times++;
  251. ret = -1;
  252. if(audio_format->channels&&audio_format->sample_rate){
  253. priv->frame_format.channel_num = audio_format->channels>2?2:audio_format->channels;
  254. priv->frame_format.sample_rate = audio_format->sample_rate;
  255. priv->frame_format.data_width = 16;
  256. priv->frame_format.valid = CHANNEL_VALID|DATA_WIDTH_VALID|SAMPLE_RATE_VALID;
  257. DSP_PRNT("we DO NOT got format details from dsp,so use the info got from the header parsed instead\n");
  258. ret = 0;
  259. }else{
  260. if(!((priv->frame_format.valid & CHANNEL_VALID) &&(priv->frame_format.valid & SAMPLE_RATE_VALID) &&(priv->frame_format.valid & DATA_WIDTH_VALID))){
  261. if(wait_format_times>100){
  262. int audio_info = DSP_RD(DSP_AUDIO_FORMAT_INFO);
  263. if(audio_info){
  264. priv->frame_format.channel_num = audio_info&0xf;
  265. if(priv->frame_format.channel_num)
  266. priv->frame_format.valid |= CHANNEL_VALID;
  267. priv->frame_format.data_width= (audio_info>>4)&0x3f;
  268. if(priv->frame_format.data_width)
  269. priv->frame_format.valid |= DATA_WIDTH_VALID;
  270. priv->frame_format.sample_rate = (audio_info>>10);
  271. if(priv->frame_format.sample_rate)
  272. priv->frame_format.valid |= SAMPLE_RATE_VALID;
  273. DSP_PRNT("warning::got info from mailbox failed,read from regiser\n");
  274. ret = 0;
  275. }
  276. DSP_PRNT("dsp have not set the codec stream's format details,valid=%x\n",
  277. priv->frame_format.valid);
  278. priv->format_wait_count++;
  279. if(priv->format_wait_count > 5){
  280. if(audio_format->channels&&audio_format->sample_rate){
  281. priv->frame_format.channel_num = audio_format->channels>2?2:audio_format->channels;
  282. priv->frame_format.sample_rate = audio_format->sample_rate;
  283. priv->frame_format.data_width = 16;
  284. priv->frame_format.valid = CHANNEL_VALID|DATA_WIDTH_VALID|SAMPLE_RATE_VALID;
  285. DSP_PRNT("we have not got format details from dsp,so use the info got from the header parsed instead\n");
  286. ret = 0;
  287. }
  288. }
  289. }
  290. }
  291. }
  292. /* check the info got from dsp with the info parsed from header,we use the header info as the base */
  293. if(priv->frame_format.valid == (CHANNEL_VALID|DATA_WIDTH_VALID|SAMPLE_RATE_VALID)){
  294. DSP_PRNT("audio info from header: sr %d,ch %d\n",audio_format->sample_rate,audio_format->channels);
  295. if(audio_format->channels > 0 ){
  296. if(audio_format->channels > 2)
  297. ch = 2;
  298. else
  299. ch = audio_format->channels;
  300. if(ch != priv->frame_format.channel_num){
  301. DSP_PRNT(" ch num info from dsp and header not match,[dsp %d ch],[header %d ch]", \
  302. priv->frame_format.channel_num,ch);
  303. priv->frame_format.channel_num = ch;
  304. }
  305. }
  306. if(audio_format->sample_rate&&audio_format->sample_rate != priv->frame_format.sample_rate){
  307. DSP_PRNT(" sr num info from dsp and header not match,[dsp %d ],[header %d ]", \
  308. priv->frame_format.sample_rate,audio_format->sample_rate);
  309. priv->frame_format.sample_rate = audio_format->sample_rate;
  310. }
  311. ret = 0;
  312. DSP_PRNT("applied audio sr %d,ch num %d\n",priv->frame_format.sample_rate,priv->frame_format.channel_num);
  313. }
  314. /*Reset the PLL. Added by GK*/
  315. tsync_pcr_recover();
  316. }else{
  317. DSP_PRNT("Audio dsp have not started\n");
  318. }
  319. break;
  320. case AUDIODSP_DECODE_STOP:
  321. if(priv->dsp_is_started)
  322. {
  323. dsp_codec_stop(priv);
  324. }
  325. else
  326. {
  327. DSP_PRNT("Audio dsp have not started\n");
  328. }
  329. break;
  330. case AUDIODSP_REGISTER_FIRMWARE:
  331. a_cmd=(struct audiodsp_cmd *)args;
  332. // DSP_PRNT("register firware,%d,%s\n",a_cmd->fmt,a_cmd->data);
  333. len=a_cmd->data_len>64?64:a_cmd->data_len;
  334. if (copy_from_user(name,a_cmd->data,len)) {
  335. return -EFAULT;
  336. }
  337. name[len]='\0';
  338. ret=audiodsp_microcode_register(priv,
  339. a_cmd->fmt,
  340. name);
  341. break;
  342. case AUDIODSP_UNREGISTER_ALLFIRMWARE:
  343. audiodsp_microcode_free(priv);
  344. break;
  345. case AUDIODSP_GET_CHANNELS_NUM:
  346. *val=-1;/*mask data is not valid*/
  347. if(priv->frame_format.valid & CHANNEL_VALID)
  348. {
  349. *val=priv->frame_format.channel_num;
  350. }
  351. break;
  352. case AUDIODSP_GET_SAMPLERATE:
  353. *val=-1;/*mask data is not valid*/
  354. if(priv->frame_format.valid & SAMPLE_RATE_VALID)
  355. {
  356. *val=priv->frame_format.sample_rate;
  357. }
  358. break;
  359. case AUDIODSP_GET_DECODED_NB_FRAMES:
  360. *val=priv->decoded_nb_frames;
  361. break;
  362. case AUDIODSP_GET_BITS_PER_SAMPLE:
  363. *val=-1;/*mask data is not valid*/
  364. if(priv->frame_format.valid & DATA_WIDTH_VALID)
  365. {
  366. *val=priv->frame_format.data_width;
  367. }
  368. break;
  369. case AUDIODSP_GET_PTS:
  370. /*val=-1 is not valid*/
  371. *val=dsp_codec_get_current_pts(priv);
  372. break;
  373. case AUDIODSP_GET_FIRST_PTS_FLAG:
  374. if(priv->stream_fmt == MCODEC_FMT_COOK || priv->stream_fmt == MCODEC_FMT_RAAC)
  375. *val = 1;
  376. else
  377. *val = first_pts_checkin_complete(PTS_TYPE_AUDIO);
  378. break;
  379. case AUDIODSP_SYNC_AUDIO_START:
  380. if(get_user(pts, (unsigned long __user *)args)) {
  381. printk("Get start pts from user space fault! \n");
  382. return -EFAULT;
  383. }
  384. tsync_avevent(AUDIO_START, pts);
  385. break;
  386. case AUDIODSP_SYNC_AUDIO_PAUSE:
  387. tsync_avevent(AUDIO_PAUSE, 0);
  388. break;
  389. case AUDIODSP_SYNC_AUDIO_RESUME:
  390. tsync_avevent(AUDIO_RESUME, 0);
  391. break;
  392. case AUDIODSP_SYNC_AUDIO_TSTAMP_DISCONTINUITY:
  393. if(get_user(pts, (unsigned long __user *)args)){
  394. printk("Get audio discontinuity pts fault! \n");
  395. return -EFAULT;
  396. }
  397. tsync_avevent(AUDIO_TSTAMP_DISCONTINUITY, pts);
  398. break;
  399. case AUDIODSP_SYNC_GET_APTS:
  400. pts = timestamp_apts_get();
  401. if(put_user(pts, (unsigned long __user *)args)){
  402. printk("Put audio pts to user space fault! \n");
  403. return -EFAULT;
  404. }
  405. break;
  406. case AUDIODSP_SYNC_GET_PCRSCR:
  407. pts = timestamp_pcrscr_get();
  408. if(put_user(pts, (unsigned long __user *)args)){
  409. printk("Put pcrscr to user space fault! \n");
  410. return -EFAULT;
  411. }
  412. break;
  413. case AUDIODSP_SYNC_SET_APTS:
  414. if(get_user(pts, (unsigned long __user *)args)){
  415. printk("Get audio pts from user space fault! \n");
  416. return -EFAULT;
  417. }
  418. tsync_set_apts(pts);
  419. break;
  420. case AUDIODSP_DROP_PCMDATA:
  421. if(get_user(drop_size, (unsigned long __user *)args)){
  422. printk("Get pcm drop size from user space fault! \n");
  423. return -EFAULT;
  424. }
  425. audiodsp_drop_pcm(drop_size);
  426. break;
  427. default:
  428. DSP_PRNT("unsupport cmd number%d\n",cmd);
  429. ret=-1;
  430. }
  431. return ret;
  432. }
  433. static int audiodsp_release(struct inode *node, struct file *file)
  434. {
  435. DSP_PRNT("dsp_release\n");
  436. #ifdef CONFIG_ARCH_MESON6
  437. switch_mod_gate_by_type(MOD_VDEC, 0);
  438. switch_mod_gate_by_type(MOD_MEDIA_CPU, 0);
  439. #endif
  440. audiodsp_allow_sleep();
  441. return 0;
  442. }
  443. ssize_t audiodsp_read(struct file * file, char __user * ubuf, size_t size,
  444. loff_t * loff)
  445. {
  446. struct audiodsp_priv *priv=audiodsp_privdata();
  447. unsigned long rp,orp;
  448. size_t len;
  449. size_t else_len;
  450. size_t wlen;
  451. size_t w_else_len;
  452. int wait=0;
  453. char __user *pubuf=ubuf;
  454. dma_addr_t buf_map;
  455. #define MIN_READ 2 // 1 channel * 2 bytes per sample
  456. #define PCM_DATA_MIN 2
  457. #define PCM_DATA_ALGIN(x) (x & (~(PCM_DATA_MIN-1)))
  458. #define MAX_WAIT HZ/10
  459. mutex_lock(&priv->stream_buffer_mutex);
  460. if(priv->stream_buffer_mem==NULL || !priv->dsp_is_started)
  461. goto error_out;
  462. do{
  463. len=dsp_codec_get_bufer_data_len(priv);
  464. if(len>MIN_READ)
  465. break;
  466. else
  467. {
  468. if(wait>0)
  469. break;
  470. wait++;
  471. init_completion(&priv->decode_completion);
  472. wait_for_completion_timeout(&priv->decode_completion, MAX_WAIT);
  473. }
  474. }while(len<MIN_READ);
  475. if(len>priv->stream_buffer_size || len <0)
  476. {
  477. DSP_PRNT("audio stream buffer is bad len=%d\n",len);
  478. goto error_out;
  479. }
  480. len=min(len,size);
  481. len=PCM_DATA_ALGIN(len);
  482. else_len=len;
  483. rp=dsp_codec_get_rd_addr(priv);
  484. orp=rp;
  485. while(else_len>0)
  486. {
  487. wlen=priv->stream_buffer_end-rp;
  488. wlen=min(wlen,else_len);
  489. /// dma_cache_inv((unsigned long)rp,wlen);
  490. buf_map = dma_map_single(NULL, (void *)rp, wlen, DMA_FROM_DEVICE);
  491. w_else_len=copy_to_user((void*)pubuf,(const char *)(rp),wlen);
  492. if(w_else_len!=0)
  493. {
  494. DSP_PRNT("copyed error,%d,%d,[%p]<---[%lx]\n",w_else_len,wlen,pubuf,rp);
  495. wlen-=w_else_len;
  496. }
  497. dma_unmap_single(NULL, buf_map, wlen, DMA_FROM_DEVICE);
  498. else_len-=wlen;
  499. pubuf+=wlen;
  500. rp=dsp_codec_inc_rd_addr(priv,wlen);
  501. }
  502. priv->out_len_after_last_valid_pts+=len;
  503. mutex_unlock(&priv->stream_buffer_mutex);
  504. //u32 timestamp_pcrscr_get(void);
  505. //printk("current pts=%ld,src=%ld\n",dsp_codec_get_current_pts(priv),timestamp_pcrscr_get());
  506. return len;
  507. error_out:
  508. mutex_unlock(&priv->stream_buffer_mutex);
  509. printk("audiodsp_read failed\n");
  510. return -EINVAL;
  511. }
  512. ssize_t audiodsp_write(struct file * file, const char __user * ubuf, size_t size,
  513. loff_t * loff)
  514. {
  515. struct audiodsp_priv *priv=audiodsp_privdata();
  516. // int dsp_codec_start( struct audiodsp_priv *priv);
  517. // int dsp_codec_stop( struct audiodsp_priv *priv);
  518. audiodsp_microcode_register(priv,
  519. MCODEC_FMT_COOK,
  520. "audiodsp_codec_cook.bin");
  521. priv->stream_fmt=MCODEC_FMT_COOK;
  522. audiodsp_start();
  523. dsp_codec_start(priv);
  524. //dsp_codec_stop(priv);
  525. return size;
  526. }
  527. const static struct file_operations audiodsp_fops = {
  528. .owner = THIS_MODULE,
  529. .open =audiodsp_open,
  530. .read = audiodsp_read,
  531. .write = audiodsp_write,
  532. .release = audiodsp_release,
  533. .unlocked_ioctl = audiodsp_ioctl,
  534. };
  535. static int audiodsp_get_status(struct adec_status *astatus)
  536. {
  537. struct audiodsp_priv *priv=audiodsp_privdata();
  538. if(!astatus)
  539. return -EINVAL;
  540. if(priv->frame_format.valid & CHANNEL_VALID)
  541. astatus->channels=priv->frame_format.channel_num;
  542. else
  543. astatus->channels=0;
  544. if(priv->frame_format.valid & SAMPLE_RATE_VALID)
  545. astatus->sample_rate=priv->frame_format.sample_rate;
  546. else
  547. astatus->sample_rate=0;
  548. if(priv->frame_format.valid & DATA_WIDTH_VALID)
  549. astatus->resolution=priv->frame_format.data_width;
  550. else
  551. astatus->resolution=0;
  552. astatus->error_count=priv->decode_error_count;
  553. astatus->status=priv->dsp_is_started?0:1;
  554. return 0;
  555. }
  556. static int audiodsp_init_mcode(struct audiodsp_priv *priv)
  557. {
  558. spin_lock_init(&priv->mcode_lock);
  559. priv->mcode_id=0;
  560. priv->dsp_stack_start=0;
  561. priv->dsp_gstack_start=0;
  562. priv->dsp_heap_start=0;
  563. priv->code_mem_size=AUDIO_DSP_MEM_SIZE -REG_MEM_SIZE;
  564. priv->dsp_code_start=AUDIO_DSP_START_ADDR;
  565. DSP_PRNT("DSP start addr 0x%x\n",AUDIO_DSP_START_ADDR);
  566. priv->dsp_stack_size=1024*64;
  567. priv->dsp_gstack_size=512;
  568. priv->dsp_heap_size=1024*1024;
  569. priv->stream_buffer_mem=NULL;
  570. priv->stream_buffer_mem_size=32*1024;
  571. priv->stream_fmt=-1;
  572. INIT_LIST_HEAD(&priv->mcode_list);
  573. init_completion(&priv->decode_completion);
  574. mutex_init(&priv->stream_buffer_mutex);
  575. mutex_init(&priv->dsp_mutex);
  576. priv->last_stream_fmt=-1;
  577. priv->last_valid_pts=0;
  578. priv->out_len_after_last_valid_pts=0;
  579. priv->dsp_work_details = (struct dsp_working_info*)DSP_WORK_INFO;
  580. return 0;
  581. }
  582. static ssize_t codec_fmt_show(struct class* cla, struct class_attribute* attr, char* buf)
  583. {
  584. size_t ret = 0;
  585. struct audiodsp_priv *priv = audiodsp_privdata();
  586. ret = sprintf(buf, "The codec Format %d\n", priv->stream_fmt);
  587. return ret;
  588. }
  589. static ssize_t codec_mips_show(struct class* cla, struct class_attribute* attr, char* buf)
  590. {
  591. size_t ret = 0;
  592. struct audiodsp_priv *priv = audiodsp_privdata();
  593. if(priv->stream_fmt < sizeof(audiodsp_mips)){
  594. ret = sprintf(buf, "%d\n", audiodsp_mips[__builtin_ffs(priv->stream_fmt)]);
  595. }
  596. else{
  597. ret = sprintf(buf, "%d\n", 200000);
  598. }
  599. return ret;
  600. }
  601. static ssize_t codec_fatal_err_show(struct class* cla, struct class_attribute* attr, char* buf)
  602. {
  603. struct audiodsp_priv *priv = audiodsp_privdata();
  604. return sprintf(buf, "%d\n", priv->decode_fatal_err);
  605. }
  606. static ssize_t swap_buf_ptr_show(struct class *cla, struct class_attribute* attr, char* buf)
  607. {
  608. char *pbuf = buf;
  609. pbuf += sprintf(pbuf, "swap buffer wp: %lx\n", DSP_RD(DSP_DECODE_OUT_WD_PTR));
  610. pbuf += sprintf(pbuf, "swap buffer rp: %lx\n", DSP_RD(DSP_DECODE_OUT_RD_ADDR));
  611. return (pbuf - buf);
  612. }
  613. static ssize_t dsp_working_status_show(struct class* cla, struct class_attribute* attr, char* buf)
  614. {
  615. struct audiodsp_priv *priv = audiodsp_privdata();
  616. struct dsp_working_info *info = priv->dsp_work_details;
  617. char *pbuf = buf;
  618. pbuf += sprintf(pbuf, "\tdsp status 0x%lx\n", DSP_RD(DSP_STATUS));
  619. pbuf += sprintf(pbuf, "\tdsp sp 0x%x\n", info->sp);
  620. // pbuf += sprintf(pbuf, "\tdsp pc 0x%x\n", info->pc);
  621. pbuf += sprintf(pbuf, "\tdsp ilink1 0x%x\n", info->ilink1);
  622. pbuf += sprintf(pbuf, "\tdsp ilink2 0x%x\n", info->ilink2);
  623. pbuf += sprintf(pbuf, "\tdsp blink 0x%x\n", info->blink);
  624. pbuf += sprintf(pbuf, "\tdsp jeffies 0x%lx\n", DSP_RD(DSP_JIFFIES));
  625. pbuf += sprintf(pbuf, "\tdsp pcm wp 0x%lx\n", DSP_RD(DSP_DECODE_OUT_WD_ADDR));
  626. pbuf += sprintf(pbuf, "\tdsp pcm rp 0x%lx\n", DSP_RD(DSP_DECODE_OUT_RD_ADDR));
  627. pbuf += sprintf(pbuf, "\tdsp pcm buffered size 0x%lx\n", DSP_RD(DSP_BUFFERED_LEN));
  628. pbuf += sprintf(pbuf, "\tdsp es read offset 0x%lx\n", DSP_RD(DSP_AFIFO_RD_OFFSET1));
  629. return (pbuf- buf);
  630. }
  631. static ssize_t digital_raw_show(struct class*cla, struct class_attribute* attr, char* buf)
  632. {
  633. char* pbuf = buf;
  634. pbuf += sprintf(pbuf, "Digital output mode: %s\n", (IEC958_mode_raw==0)?"0 - PCM":"1 - RAW");
  635. return (pbuf-buf);
  636. }
  637. static ssize_t digital_raw_store(struct class* class, struct class_attribute* attr,
  638. const char* buf, size_t count )
  639. {
  640. printk("buf=%s\n", buf);
  641. if(buf[0] == '0'){
  642. IEC958_mode_raw = 0;
  643. }else if(buf[0] == '1'){
  644. IEC958_mode_raw = 1;
  645. }
  646. printk("IEC958_mode_raw=%d\n", IEC958_mode_raw);
  647. return count;
  648. }
  649. static struct class_attribute audiodsp_attrs[]={
  650. __ATTR_RO(codec_fmt),
  651. #ifdef CONFIG_ARCH_MESON1
  652. __ATTR_RO(codec_mips),
  653. #endif
  654. __ATTR_RO(codec_fatal_err),
  655. __ATTR_RO(swap_buf_ptr),
  656. __ATTR_RO(dsp_working_status),
  657. __ATTR(digital_raw, S_IRUGO | S_IWUSR, digital_raw_show, digital_raw_store),
  658. __ATTR_NULL
  659. };
  660. #ifdef CONFIG_PM
  661. static int audiodsp_suspend(struct device* dev, pm_message_t state)
  662. {
  663. struct audiodsp_priv *priv = audiodsp_privdata();
  664. if(wake_lock_active(&priv->wakelock)){
  665. return -1; // please stop dsp first
  666. }
  667. pm_state.event = state.event;
  668. if(state.event == PM_EVENT_SUSPEND){
  669. // should sleep cpu2 here after RevC chip
  670. msleep(50);
  671. }
  672. printk("audiodsp suspend\n");
  673. return 0;
  674. }
  675. static int audiodsp_resume(struct device* dev)
  676. {
  677. if(pm_state.event == PM_EVENT_SUSPEND){
  678. // wakeup cpu2
  679. pm_state.event = -1;
  680. }
  681. printk("audiodsp resumed\n");
  682. return 0;
  683. }
  684. #endif
  685. static struct class audiodsp_class = {
  686. .name = DSP_DRIVER_NAME,
  687. .class_attrs = audiodsp_attrs,
  688. #ifdef CONFIG_PM
  689. .suspend = audiodsp_suspend,
  690. .resume = audiodsp_resume,
  691. #else
  692. .suspend = NULL,
  693. .resume = NULL,
  694. #endif
  695. };
  696. int audiodsp_probe(void )
  697. {
  698. int res=0;
  699. struct audiodsp_priv *priv;
  700. priv=kmalloc(sizeof(struct audiodsp_priv),GFP_KERNEL);
  701. if(priv==NULL)
  702. {
  703. DSP_PRNT("Out of memory for audiodsp register\n");
  704. return -1;
  705. }
  706. priv->dsp_is_started=0;
  707. /*
  708. priv->p = ioremap_nocache(AUDIO_DSP_START_PHY_ADDR, S_1M);
  709. if(priv->p)
  710. DSP_PRNT("DSP IOREMAP to addr 0x%x\n",(unsigned)priv->p);
  711. else{
  712. DSP_PRNT("DSP IOREMAP error\n");
  713. goto error1;
  714. }
  715. */
  716. audiodsp_p=priv;
  717. audiodsp_init_mcode(priv);
  718. if(priv->dsp_heap_size){
  719. if(priv->dsp_heap_start==0)
  720. priv->dsp_heap_start=(unsigned long)kmalloc(priv->dsp_heap_size,GFP_KERNEL);
  721. if(priv->dsp_heap_start==0)
  722. {
  723. DSP_PRNT("kmalloc error,no memory for audio dsp dsp_set_heap\n");
  724. kfree(priv);
  725. return -ENOMEM;
  726. }
  727. }
  728. res = register_chrdev(AUDIODSP_MAJOR, DSP_NAME, &audiodsp_fops);
  729. if (res < 0) {
  730. DSP_PRNT("Can't register char devie for " DSP_NAME "\n");
  731. goto error1;
  732. } else {
  733. DSP_PRNT("register " DSP_NAME " to char divece(%d)\n",
  734. AUDIODSP_MAJOR);
  735. }
  736. res = class_register(&audiodsp_class);
  737. if(res <0 ){
  738. DSP_PRNT("Create audiodsp class failed\n");
  739. res = -EEXIST;
  740. goto error2;
  741. }
  742. priv->class = &audiodsp_class;
  743. priv->dev = device_create(priv->class,
  744. NULL, MKDEV(AUDIODSP_MAJOR, 0),
  745. NULL, "audiodsp0");
  746. if(priv->dev==NULL)
  747. {
  748. res = -EEXIST;
  749. goto error3;
  750. }
  751. audiodsp_init_mailbox(priv);
  752. init_audiodsp_monitor(priv);
  753. wake_lock_init(&priv->wakelock,WAKE_LOCK_SUSPEND, "audiodsp");
  754. #ifdef CONFIG_AM_STREAMING
  755. set_adec_func(audiodsp_get_status);
  756. #endif
  757. memset((void*)DSP_REG_OFFSET,0,REG_MEM_SIZE);
  758. return res;
  759. //error4:
  760. device_destroy(priv->class, MKDEV(AUDIODSP_MAJOR, 0));
  761. error3:
  762. class_destroy(priv->class);
  763. error2:
  764. unregister_chrdev(AUDIODSP_MAJOR, DSP_NAME);
  765. error1:
  766. kfree(priv);
  767. return res;
  768. }
  769. struct audiodsp_priv *audiodsp_privdata(void)
  770. {
  771. return audiodsp_p;
  772. }
  773. static int __init audiodsp_init_module(void)
  774. {
  775. return audiodsp_probe();
  776. }
  777. static void __exit audiodsp_exit_module(void)
  778. {
  779. struct audiodsp_priv *priv;
  780. priv=audiodsp_privdata();
  781. #ifdef CONFIG_AM_STREAMING
  782. set_adec_func(NULL);
  783. #endif
  784. dsp_stop(priv);
  785. stop_audiodsp_monitor(priv);
  786. audiodsp_release_mailbox(priv);
  787. release_audiodsp_monitor(priv);
  788. audiodsp_microcode_free(priv);
  789. /*
  790. iounmap(priv->p);
  791. */
  792. device_destroy(priv->class, MKDEV(AUDIODSP_MAJOR, 0));
  793. class_destroy(priv->class);
  794. unregister_chrdev(AUDIODSP_MAJOR, DSP_NAME);
  795. kfree(priv);
  796. priv=NULL;
  797. return;
  798. }
  799. module_init(audiodsp_init_module);
  800. module_exit(audiodsp_exit_module);