temp revert rk change
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / tegra_spdif_audio.c
1 /*
2  * arch/arm/mach-tegra/tegra_spdif_audio.c
3  *
4  * S/PDIF audio driver for NVIDIA Tegra SoCs
5  *
6  * Copyright (c) 2008-2009, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/fs.h>
27 #include <linux/mutex.h>
28 #include <linux/clk.h>
29 #include <linux/interrupt.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
32 #include <linux/spinlock.h>
33 #include <linux/uaccess.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/dmapool.h>
36 #include <linux/err.h>
37 #include <linux/spi/spi.h>
38 #include <linux/kfifo.h>
39 #include <linux/debugfs.h>
40 #include <linux/completion.h>
41 #include <linux/platform_device.h>
42 #include <linux/device.h>
43 #include <linux/io.h>
44 #include <linux/ktime.h>
45 #include <linux/sysfs.h>
46 #include <linux/wakelock.h>
47 #include <linux/delay.h>
48 #include <linux/tegra_audio.h>
49 #include <linux/pm.h>
50 #include <linux/workqueue.h>
51
52 #include <mach/dma.h>
53 #include <mach/iomap.h>
54 #include <mach/spdif.h>
55 #include <mach/audio.h>
56 #include <mach/irqs.h>
57
58 #include "clock.h"
59
60 #define PCM_BUFFER_MAX_SIZE_ORDER       (PAGE_SHIFT)
61
62 #define SPDIF_MAX_NUM_BUFS 4
63 /* Todo: Add IOCTL to configure the number of buffers. */
64 #define SPDIF_DEFAULT_TX_NUM_BUFS 2
65 #define SPDIF_DEFAULT_RX_NUM_BUFS 2
66 /* per stream (input/output) */
67 struct audio_stream {
68         int opened;
69         struct mutex lock;
70
71         bool active; /* is DMA in progress? */
72         int num_bufs;
73         void *buffer[SPDIF_MAX_NUM_BUFS];
74         dma_addr_t buf_phy[SPDIF_MAX_NUM_BUFS];
75         struct completion comp[SPDIF_MAX_NUM_BUFS];
76         struct tegra_dma_req dma_req[SPDIF_MAX_NUM_BUFS];
77         int last_queued;
78
79         int spdif_fifo_atn_level;
80
81         struct tegra_dma_channel *dma_chan;
82         bool stop;
83         struct completion stop_completion;
84         spinlock_t dma_req_lock;
85
86         struct work_struct allow_suspend_work;
87         struct wake_lock wake_lock;
88         char wake_lock_name[100];
89 };
90
91 struct audio_driver_state {
92         struct list_head next;
93
94         struct platform_device *pdev;
95         struct tegra_audio_platform_data *pdata;
96         phys_addr_t spdif_phys;
97         unsigned long spdif_base;
98
99         unsigned long dma_req_sel;
100         bool fifo_init;
101
102         int irq;
103
104         struct miscdevice misc_out;
105         struct miscdevice misc_out_ctl;
106         struct audio_stream out;
107 };
108
109 static inline bool pending_buffer_requests(struct audio_stream *stream)
110 {
111         int i;
112         for (i = 0; i < stream->num_bufs; i++)
113                 if (!completion_done(&stream->comp[i]))
114                         return true;
115         return false;
116 }
117
118 static inline int buf_size(struct audio_stream *s __attribute__((unused)))
119 {
120         return 1 << PCM_BUFFER_MAX_SIZE_ORDER;
121 }
122
123 static inline struct audio_driver_state *ads_from_misc_out(struct file *file)
124 {
125         struct miscdevice *m = file->private_data;
126         struct audio_driver_state *ads =
127                         container_of(m, struct audio_driver_state, misc_out);
128         BUG_ON(!ads);
129         return ads;
130 }
131
132 static inline struct audio_driver_state *ads_from_misc_out_ctl(
133                 struct file *file)
134 {
135         struct miscdevice *m = file->private_data;
136         struct audio_driver_state *ads =
137                         container_of(m, struct audio_driver_state,
138                                         misc_out_ctl);
139         BUG_ON(!ads);
140         return ads;
141 }
142
143 static inline struct audio_driver_state *ads_from_out(
144                         struct audio_stream *aos)
145 {
146         return container_of(aos, struct audio_driver_state, out);
147 }
148
149 static inline void prevent_suspend(struct audio_stream *as)
150 {
151         pr_debug("%s\n", __func__);
152         cancel_work_sync(&as->allow_suspend_work);
153         wake_lock(&as->wake_lock);
154 }
155
156 static void allow_suspend_worker(struct work_struct *w)
157 {
158         struct audio_stream *as = container_of(w,
159                         struct audio_stream, allow_suspend_work);
160         pr_debug("%s\n", __func__);
161         wake_unlock(&as->wake_lock);
162 }
163
164 static inline void allow_suspend(struct audio_stream *as)
165 {
166         schedule_work(&as->allow_suspend_work);
167 }
168
169 #define I2S_I2S_FIFO_TX_BUSY    I2S_I2S_STATUS_FIFO1_BSY
170 #define I2S_I2S_FIFO_TX_QS      I2S_I2S_STATUS_QS_FIFO1
171 #define I2S_I2S_FIFO_TX_ERR     I2S_I2S_STATUS_FIFO1_ERR
172
173 #define I2S_I2S_FIFO_RX_BUSY    I2S_I2S_STATUS_FIFO2_BSY
174 #define I2S_I2S_FIFO_RX_QS      I2S_I2S_STATUS_QS_FIFO2
175 #define I2S_I2S_FIFO_RX_ERR     I2S_I2S_STATUS_FIFO2_ERR
176
177 #define I2S_FIFO_ERR (I2S_I2S_STATUS_FIFO1_ERR | I2S_I2S_STATUS_FIFO2_ERR)
178
179
180 static inline void spdif_writel(unsigned long base, u32 val, u32 reg)
181 {
182         writel(val, base + reg);
183 }
184
185 static inline u32 spdif_readl(unsigned long base, u32 reg)
186 {
187         return readl(base + reg);
188 }
189
190 static inline void spdif_fifo_write(unsigned long base, u32 data)
191 {
192         spdif_writel(base, data, SPDIF_DATA_OUT_0);
193 }
194
195 static int spdif_fifo_set_attention_level(unsigned long base,
196                         unsigned level)
197 {
198         u32 val;
199
200         if (level > SPDIF_FIFO_ATN_LVL_TWELVE_SLOTS) {
201                 pr_err("%s: invalid fifo level selector %d\n", __func__,
202                         level);
203                 return -EINVAL;
204         }
205
206         val = spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
207
208         val &= ~SPDIF_DATA_FIFO_CSR_0_TX_ATN_LVL_MASK;
209         val |= level << SPDIF_DATA_FIFO_CSR_0_TX_ATN_LVL_SHIFT;
210
211
212         spdif_writel(base, val, SPDIF_DATA_FIFO_CSR_0);
213         return 0;
214 }
215
216 static void spdif_fifo_enable(unsigned long base, int on)
217 {
218         u32 val = spdif_readl(base, SPDIF_CTRL_0);
219         val &= ~(SPDIF_CTRL_0_TX_EN | SPDIF_CTRL_0_TC_EN | SPDIF_CTRL_0_TU_EN);
220         val |= on ? (SPDIF_CTRL_0_TX_EN) : 0;
221         val |= on ? (SPDIF_CTRL_0_TC_EN) : 0;
222
223         spdif_writel(base, val, SPDIF_CTRL_0);
224 }
225 #if 0
226 static bool spdif_is_fifo_enabled(unsigned long base)
227 {
228         u32 val = spdif_readl(base, SPDIF_CTRL_0);
229         return !!(val & SPDIF_CTRL_0_TX_EN);
230 }
231 #endif
232
233 static void spdif_fifo_clear(unsigned long base)
234 {
235         u32 val = spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
236         val &= ~(SPDIF_DATA_FIFO_CSR_0_TX_CLR | SPDIF_DATA_FIFO_CSR_0_TU_CLR);
237         val |= SPDIF_DATA_FIFO_CSR_0_TX_CLR | SPDIF_DATA_FIFO_CSR_0_TU_CLR;
238         spdif_writel(base, val, SPDIF_DATA_FIFO_CSR_0);
239 }
240
241
242 static int spdif_set_bit_mode(unsigned long base, unsigned mode)
243 {
244         u32 val = spdif_readl(base, SPDIF_CTRL_0);
245         val &= ~SPDIF_CTRL_0_BIT_MODE_MASK;
246
247         if (mode > SPDIF_BIT_MODE_MODERAW) {
248                 pr_err("%s: invalid bit_size selector %d\n", __func__,
249                         mode);
250                 return -EINVAL;
251         }
252
253         val |= mode << SPDIF_CTRL_0_BIT_MODE_SHIFT;
254
255         spdif_writel(base, val, SPDIF_CTRL_0);
256         return 0;
257 }
258
259 static int spdif_set_fifo_packed(unsigned long base, unsigned on)
260 {
261         u32 val = spdif_readl(base, SPDIF_CTRL_0);
262         val &= ~SPDIF_CTRL_0_PACK;
263         val |= on ? SPDIF_CTRL_0_PACK : 0;
264         spdif_writel(base, val, SPDIF_CTRL_0);
265         return 0;
266 }
267
268 #if 0
269 static void spdif_set_fifo_irq_on_err(unsigned long base, int on)
270 {
271         u32 val = spdif_readl(base, SPDIF_CTRL_0);
272         val &= ~SPDIF_CTRL_0_IE_TXE;
273         val |= on ? SPDIF_CTRL_0_IE_TXE : 0;
274         spdif_writel(base, val, SPDIF_CTRL_0);
275 }
276 #endif
277
278
279 static void spdif_enable_fifos(unsigned long base, int on)
280 {
281         u32 val = spdif_readl(base, SPDIF_CTRL_0);
282         if (on)
283                 val |= SPDIF_CTRL_0_TX_EN | SPDIF_CTRL_0_TC_EN |
284                        SPDIF_CTRL_0_IE_TXE;
285         else
286                 val &= ~(SPDIF_CTRL_0_TX_EN | SPDIF_CTRL_0_TC_EN |
287                          SPDIF_CTRL_0_IE_TXE);
288
289         spdif_writel(base, val, SPDIF_CTRL_0);
290 }
291
292 static inline u32 spdif_get_status(unsigned long base)
293 {
294         return spdif_readl(base, SPDIF_STATUS_0);
295 }
296
297 static inline u32 spdif_get_control(unsigned long base)
298 {
299         return spdif_readl(base, SPDIF_CTRL_0);
300 }
301
302 static inline void spdif_ack_status(unsigned long base)
303 {
304         return spdif_writel(base, spdif_readl(base, SPDIF_STATUS_0),
305                                 SPDIF_STATUS_0);
306 }
307
308 static inline u32 spdif_get_fifo_scr(unsigned long base)
309 {
310         return spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
311 }
312
313 static inline phys_addr_t spdif_get_fifo_phy_base(unsigned long phy_base)
314 {
315         return phy_base + SPDIF_DATA_OUT_0;
316 }
317
318 static inline u32 spdif_get_fifo_full_empty_count(unsigned long base)
319 {
320         u32 val = spdif_readl(base, SPDIF_DATA_FIFO_CSR_0);
321         val = val >> SPDIF_DATA_FIFO_CSR_0_TD_EMPTY_COUNT_SHIFT;
322         return val & SPDIF_DATA_FIFO_CSR_0_TD_EMPTY_COUNT_MASK;
323 }
324
325
326 static int spdif_set_sample_rate(struct audio_driver_state *state,
327                                 unsigned int sample_rate)
328 {
329         unsigned int clock_freq = 0;
330         struct clk *spdif_clk;
331
332         unsigned int ch_sta[] = {
333                 0x0, /* 44.1, default values */
334                 0x0,
335                 0x0,
336                 0x0,
337                 0x0,
338                 0x0,
339         };
340
341         switch (sample_rate) {
342         case 32000:
343                 clock_freq = 4096000; /* 4.0960 MHz */
344                 ch_sta[0] = 0x3 << 24;
345                 ch_sta[1] = 0xC << 4;
346                 break;
347         case 44100:
348                 clock_freq = 5644800; /* 5.6448 MHz */
349                 ch_sta[0] = 0x0;
350                 ch_sta[1] = 0xF << 4;
351                 break;
352         case 48000:
353                 clock_freq = 6144000; /* 6.1440MHz */
354                 ch_sta[0] = 0x2 << 24;
355                 ch_sta[1] = 0xD << 4;
356                 break;
357         case 88200:
358                 clock_freq = 11289600; /* 11.2896 MHz */
359                 break;
360         case 96000:
361                 clock_freq = 12288000; /* 12.288 MHz */
362                 break;
363         case 176400:
364                 clock_freq = 22579200; /* 22.5792 MHz */
365                 break;
366         case 192000:
367                 clock_freq = 24576000; /* 24.5760 MHz */
368                 break;
369         default:
370                 return -1;
371         }
372
373         spdif_clk = clk_get(&state->pdev->dev, NULL);
374         if (!spdif_clk) {
375                 dev_err(&state->pdev->dev, "%s: could not get spdif clock\n",
376                                 __func__);
377                 return -EIO;
378         }
379
380         clk_set_rate(spdif_clk, clock_freq);
381         if (clk_enable(spdif_clk)) {
382                 dev_err(&state->pdev->dev,
383                         "%s: failed to enable spdif_clk clock\n", __func__);
384                 return -EIO;
385         }
386         pr_info("%s: spdif_clk rate %ld\n", __func__, clk_get_rate(spdif_clk));
387
388         spdif_writel(state->spdif_base, ch_sta[0], SPDIF_CH_STA_TX_A_0);
389         spdif_writel(state->spdif_base, ch_sta[1], SPDIF_CH_STA_TX_B_0);
390         spdif_writel(state->spdif_base, ch_sta[2], SPDIF_CH_STA_TX_C_0);
391         spdif_writel(state->spdif_base, ch_sta[3], SPDIF_CH_STA_TX_D_0);
392         spdif_writel(state->spdif_base, ch_sta[4], SPDIF_CH_STA_TX_E_0);
393         spdif_writel(state->spdif_base, ch_sta[5], SPDIF_CH_STA_TX_F_0);
394
395         return 0;
396 }
397
398 static int init_stream_buffer(struct audio_stream *, int);
399
400 static int setup_dma(struct audio_driver_state *);
401 static void tear_down_dma(struct audio_driver_state *);
402 static void stop_dma_playback(struct audio_stream *);
403
404
405 struct sound_ops {
406         int (*setup)(struct audio_driver_state *);
407         void (*tear_down)(struct audio_driver_state *);
408         void (*stop_playback)(struct audio_stream *);
409 };
410
411 static const struct sound_ops dma_sound_ops = {
412         .setup = setup_dma,
413         .tear_down = tear_down_dma,
414         .stop_playback = stop_dma_playback,
415 };
416
417 static const struct sound_ops *sound_ops = &dma_sound_ops;
418
419
420
421 static bool stop_playback_if_necessary(struct audio_stream *aos)
422 {
423         unsigned long flags;
424         spin_lock_irqsave(&aos->dma_req_lock, flags);
425         pr_debug("%s\n", __func__);
426         if (!pending_buffer_requests(aos)) {
427                 pr_debug("%s: no more data to play back\n", __func__);
428                 sound_ops->stop_playback(aos);
429                 spin_unlock_irqrestore(&aos->dma_req_lock, flags);
430                 allow_suspend(aos);
431                 return true;
432         }
433         spin_unlock_irqrestore(&aos->dma_req_lock, flags);
434
435         return false;
436 }
437
438 /* playback */
439 static bool wait_till_stopped(struct audio_stream *as)
440 {
441         int rc;
442         pr_debug("%s: wait for completion\n", __func__);
443         rc = wait_for_completion_timeout(
444                         &as->stop_completion, HZ);
445         if (!rc)
446                 pr_err("%s: wait timed out", __func__);
447         if (rc < 0)
448                 pr_err("%s: wait error %d\n", __func__, rc);
449         allow_suspend(as);
450         pr_debug("%s: done: %d\n", __func__, rc);
451         return true;
452 }
453
454 /* Ask for playback to stop.  The _nosync means that
455  * as->lock has to be locked by the caller.
456  */
457 static void request_stop_nosync(struct audio_stream *as)
458 {
459         int i;
460         pr_debug("%s\n", __func__);
461         if (!as->stop) {
462                 as->stop = true;
463                 if (pending_buffer_requests(as))
464                         wait_till_stopped(as);
465                 for (i = 0; i < as->num_bufs; i++) {
466                         init_completion(&as->comp[i]);
467                         complete(&as->comp[i]);
468                 }
469         }
470         if (!tegra_dma_is_empty(as->dma_chan))
471                 pr_err("%s: DMA not empty!\n", __func__);
472         /* Stop the DMA then dequeue anything that's in progress. */
473         tegra_dma_cancel(as->dma_chan);
474         as->active = false; /* applies to recording only */
475         pr_debug("%s: done\n", __func__);
476 }
477
478 static void setup_dma_tx_request(struct tegra_dma_req *req,
479                 struct audio_stream *aos);
480
481 static int setup_dma(struct audio_driver_state *ads)
482 {
483         int rc, i;
484         pr_info("%s\n", __func__);
485
486         /* setup audio playback */
487         for (i = 0; i < ads->out.num_bufs; i++) {
488                 ads->out.buf_phy[i] = dma_map_single(&ads->pdev->dev,
489                                 ads->out.buffer[i],
490                                 buf_size(&ads->out),
491                                 DMA_TO_DEVICE);
492                 BUG_ON(!ads->out.buf_phy[i]);
493                 setup_dma_tx_request(&ads->out.dma_req[i], &ads->out);
494                 ads->out.dma_req[i].source_addr = ads->out.buf_phy[i];
495         }
496         ads->out.dma_chan =
497                  tegra_dma_allocate_channel(TEGRA_DMA_MODE_CONTINUOUS_SINGLE);
498         if (!ads->out.dma_chan) {
499                 pr_err("%s: error alloc output DMA channel: %ld\n",
500                         __func__, PTR_ERR(ads->out.dma_chan));
501                 rc = -ENODEV;
502                 goto fail_tx;
503         }
504         return 0;
505
506
507 fail_tx:
508
509         for (i = 0; i < ads->out.num_bufs; i++) {
510                 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phy[i],
511                                 buf_size(&ads->out),
512                                 DMA_TO_DEVICE);
513                 ads->out.buf_phy[i] = 0;
514         }
515         tegra_dma_free_channel(ads->out.dma_chan);
516         ads->out.dma_chan = 0;
517
518
519         return rc;
520 }
521
522 static void tear_down_dma(struct audio_driver_state *ads)
523 {
524         int i;
525         pr_info("%s\n", __func__);
526
527
528         tegra_dma_free_channel(ads->out.dma_chan);
529         for (i = 0; i < ads->out.num_bufs; i++) {
530                 dma_unmap_single(&ads->pdev->dev, ads->out.buf_phy[i],
531                                 buf_size(&ads->out),
532                                 DMA_TO_DEVICE);
533                 ads->out.buf_phy[i] = 0;
534         }
535
536         ads->out.dma_chan = NULL;
537 }
538
539 static void dma_tx_complete_callback(struct tegra_dma_req *req)
540 {
541         struct audio_stream *aos = req->dev;
542         unsigned req_num;
543
544         req_num = req - aos->dma_req;
545         pr_debug("%s: completed buffer %d size %d\n", __func__,
546                         req_num, req->bytes_transferred);
547         BUG_ON(req_num >= aos->num_bufs);
548
549         complete(&aos->comp[req_num]);
550
551         if (!pending_buffer_requests(aos)) {
552                 pr_debug("%s: Playback underflow", __func__);
553                 complete(&aos->stop_completion);
554         }
555 }
556
557
558 static void setup_dma_tx_request(struct tegra_dma_req *req,
559                 struct audio_stream *aos)
560 {
561         struct audio_driver_state *ads = ads_from_out(aos);
562
563         memset(req, 0, sizeof(*req));
564
565         req->complete = dma_tx_complete_callback;
566         req->dev = aos;
567         req->to_memory = false;
568         req->dest_addr = spdif_get_fifo_phy_base(ads->spdif_phys);
569         req->dest_bus_width = 32;
570         req->dest_wrap = 4;
571         req->source_wrap = 0;
572         req->source_bus_width = 32;
573         req->req_sel = ads->dma_req_sel;
574 }
575
576
577 static int start_playback(struct audio_stream *aos,
578                         struct tegra_dma_req *req)
579 {
580         int rc;
581         unsigned long flags;
582         struct audio_driver_state *ads = ads_from_out(aos);
583
584         pr_debug("%s: (writing %d)\n",
585                         __func__, req->size);
586
587         spin_lock_irqsave(&aos->dma_req_lock, flags);
588 #if 0
589         spdif_fifo_clear(ads->spdif_base);
590 #endif
591
592         spdif_fifo_set_attention_level(ads->spdif_base,
593                 ads->out.spdif_fifo_atn_level);
594
595         if (ads->fifo_init) {
596                 spdif_set_bit_mode(ads->spdif_base, SPDIF_BIT_MODE_MODE16BIT);
597                 spdif_set_fifo_packed(ads->spdif_base, 1);
598                 ads->fifo_init = false;
599         }
600
601         spdif_fifo_enable(ads->spdif_base, 1);
602
603         rc = tegra_dma_enqueue_req(aos->dma_chan, req);
604         spin_unlock_irqrestore(&aos->dma_req_lock, flags);
605
606         if (rc)
607                 pr_err("%s: could not enqueue TX DMA req\n", __func__);
608         return rc;
609 }
610
611 /* Called with aos->dma_req_lock taken. */
612 static void stop_dma_playback(struct audio_stream *aos)
613 {
614         int spin = 0;
615         struct audio_driver_state *ads = ads_from_out(aos);
616         pr_debug("%s\n", __func__);
617         spdif_fifo_enable(ads->spdif_base, 0);
618         while ((spdif_get_status(ads->spdif_base) & SPDIF_STATUS_0_TX_BSY) &&
619                         spin < 100) {
620                 udelay(10);
621                 if (spin++ > 50)
622                         pr_info("%s: spin %d\n", __func__, spin);
623         }
624         if (spin == 100)
625                 pr_warn("%s: spinny\n", __func__);
626         ads->fifo_init = true;
627 }
628
629
630
631 static irqreturn_t spdif_interrupt(int irq, void *data)
632 {
633         struct audio_driver_state *ads = data;
634         u32 status = spdif_get_status(ads->spdif_base);
635
636         pr_debug("%s: %08x\n", __func__, status);
637
638 /*      if (status & SPDIF_STATUS_0_TX_ERR) */
639                 spdif_ack_status(ads->spdif_base);
640
641         pr_debug("%s: done %08x\n", __func__,
642                         spdif_get_status(ads->spdif_base));
643         return IRQ_HANDLED;
644 }
645
646 static ssize_t tegra_spdif_write(struct file *file,
647                 const char __user *buf, size_t size, loff_t *off)
648 {
649         ssize_t rc = 0;
650         int out_buf;
651         struct tegra_dma_req *req;
652         struct audio_driver_state *ads = ads_from_misc_out(file);
653
654         mutex_lock(&ads->out.lock);
655
656         if (!IS_ALIGNED(size, 4) || size < 4 || size > buf_size(&ads->out)) {
657                 pr_err("%s: invalid user size %d\n", __func__, size);
658                 rc = -EINVAL;
659                 goto done;
660         }
661
662         pr_debug("%s: write %d bytes\n", __func__, size);
663
664         if (ads->out.stop) {
665                 pr_debug("%s: playback has been cancelled\n", __func__);
666                 goto done;
667         }
668
669         /* Decide which buf is next. */
670         out_buf = (ads->out.last_queued + 1) % ads->out.num_bufs;
671         req = &ads->out.dma_req[out_buf];
672
673         /* Wait for the buffer to be emptied (complete).  The maximum timeout
674          * value could be calculated dynamically based on buf_size(&ads->out).
675          * For a buffer size of 16k, at 44.1kHz/stereo/16-bit PCM, you would
676          * have ~93ms.
677          */
678         pr_debug("%s: waiting for buffer %d\n", __func__, out_buf);
679         rc = wait_for_completion_interruptible_timeout(
680                                 &ads->out.comp[out_buf], HZ);
681         if (!rc) {
682                 pr_err("%s: timeout", __func__);
683                 rc = -ETIMEDOUT;
684                 goto done;
685         } else if (rc < 0) {
686                 pr_err("%s: wait error %d", __func__, rc);
687                 goto done;
688         }
689
690         /* Fill the buffer and enqueue it. */
691         pr_debug("%s: acquired buffer %d, copying data\n", __func__, out_buf);
692         rc = copy_from_user(ads->out.buffer[out_buf], buf, size);
693         if (rc) {
694                 rc = -EFAULT;
695                 goto done;
696         }
697
698         prevent_suspend(&ads->out);
699
700         req->size = size;
701         dma_sync_single_for_device(NULL,
702                         req->source_addr, req->size, DMA_TO_DEVICE);
703         ads->out.last_queued = out_buf;
704         init_completion(&ads->out.stop_completion);
705
706         rc = start_playback(&ads->out, req);
707         if (!rc)
708                 rc = size;
709         else
710                 allow_suspend(&ads->out);
711
712 done:
713         mutex_unlock(&ads->out.lock);
714         return rc;
715 }
716
717 static long tegra_spdif_out_ioctl(struct file *file,
718                         unsigned int cmd, unsigned long arg)
719 {
720         int rc = 0;
721         struct audio_driver_state *ads = ads_from_misc_out_ctl(file);
722         struct audio_stream *aos = &ads->out;
723
724         mutex_lock(&aos->lock);
725
726         switch (cmd) {
727         case TEGRA_AUDIO_OUT_FLUSH:
728                 if (pending_buffer_requests(aos)) {
729                         pr_debug("%s: flushing\n", __func__);
730                         request_stop_nosync(aos);
731                         pr_debug("%s: flushed\n", __func__);
732                 }
733                 if (stop_playback_if_necessary(aos))
734                         pr_debug("%s: done (stopped)\n", __func__);
735                 aos->stop = false;
736                 break;
737         case TEGRA_AUDIO_OUT_SET_NUM_BUFS: {
738                 unsigned int num;
739                 if (copy_from_user(&num, (const void __user *)arg,
740                                         sizeof(num))) {
741                         rc = -EFAULT;
742                         break;
743                 }
744                 if (!num || num > SPDIF_MAX_NUM_BUFS) {
745                         pr_err("%s: invalid buffer count %d\n", __func__, num);
746                         rc = -EINVAL;
747                         break;
748                 }
749                 if (pending_buffer_requests(aos)) {
750                         pr_err("%s: playback in progress\n", __func__);
751                         rc = -EBUSY;
752                         break;
753                 }
754                 rc = init_stream_buffer(aos, num);
755                 if (rc < 0)
756                         break;
757                 aos->num_bufs = num;
758                 sound_ops->setup(ads);
759         }
760                 break;
761         case TEGRA_AUDIO_OUT_GET_NUM_BUFS:
762                 if (copy_to_user((void __user *)arg,
763                                 &aos->num_bufs, sizeof(aos->num_bufs)))
764                         rc = -EFAULT;
765                 break;
766         default:
767                 rc = -EINVAL;
768         }
769
770         mutex_unlock(&aos->lock);
771         return rc;
772 }
773
774
775 static int tegra_spdif_out_open(struct inode *inode, struct file *file)
776 {
777         int rc = 0;
778         int i;
779         struct audio_driver_state *ads = ads_from_misc_out(file);
780
781         pr_debug("%s\n", __func__);
782
783         mutex_lock(&ads->out.lock);
784
785         if (ads->out.opened) {
786                 rc = -EBUSY;
787                 goto done;
788         }
789
790         ads->out.opened = 1;
791         ads->out.stop = false;
792
793         for (i = 0; i < SPDIF_MAX_NUM_BUFS; i++) {
794                 init_completion(&ads->out.comp[i]);
795                 /* TX buf rest state is unqueued, complete. */
796                 complete(&ads->out.comp[i]);
797         }
798
799 done:
800         mutex_unlock(&ads->out.lock);
801         return rc;
802 }
803
804 static int tegra_spdif_out_release(struct inode *inode, struct file *file)
805 {
806         struct audio_driver_state *ads = ads_from_misc_out(file);
807
808         pr_debug("%s\n", __func__);
809
810         mutex_lock(&ads->out.lock);
811         ads->out.opened = 0;
812         request_stop_nosync(&ads->out);
813         if (stop_playback_if_necessary(&ads->out))
814                 pr_debug("%s: done (stopped)\n", __func__);
815         allow_suspend(&ads->out);
816         mutex_unlock(&ads->out.lock);
817         pr_debug("%s: done\n", __func__);
818         return 0;
819 }
820
821
822 static const struct file_operations tegra_spdif_out_fops = {
823         .owner = THIS_MODULE,
824         .open = tegra_spdif_out_open,
825         .release = tegra_spdif_out_release,
826         .write = tegra_spdif_write,
827 };
828
829 static int tegra_spdif_ctl_open(struct inode *inode, struct file *file)
830 {
831         return 0;
832 }
833
834 static int tegra_spdif_ctl_release(struct inode *inode, struct file *file)
835 {
836         return 0;
837 }
838
839 static const struct file_operations tegra_spdif_out_ctl_fops = {
840         .owner = THIS_MODULE,
841         .open = tegra_spdif_ctl_open,
842         .release = tegra_spdif_ctl_release,
843         .unlocked_ioctl = tegra_spdif_out_ioctl,
844 };
845
846 static int init_stream_buffer(struct audio_stream *s, int num)
847 {
848         int i, j;
849         pr_debug("%s (num %d)\n", __func__,  num);
850
851         for (i = 0; i < num; i++) {
852                 kfree(s->buffer[i]);
853                 s->buffer[i] =
854                         kmalloc(buf_size(s), GFP_KERNEL | GFP_DMA);
855                 if (!s->buffer[i]) {
856                         pr_err("%s: could not allocate buffer\n", __func__);
857                         for (j = i - 1; j >= 0; j--) {
858                                 kfree(s->buffer[j]);
859                                 s->buffer[j] = 0;
860                         }
861                         return -ENOMEM;
862                 }
863         }
864         return 0;
865 }
866
867
868 static int setup_misc_device(struct miscdevice *misc,
869                         const struct file_operations  *fops,
870                         const char *fmt, ...)
871 {
872         int rc = 0;
873         va_list args;
874         const int sz = 64;
875
876         va_start(args, fmt);
877
878         memset(misc, 0, sizeof(*misc));
879         misc->minor = MISC_DYNAMIC_MINOR;
880         misc->name  = kmalloc(sz, GFP_KERNEL);
881         if (!misc->name) {
882                 rc = -ENOMEM;
883                 goto done;
884         }
885
886         vsnprintf((char *)misc->name, sz, fmt, args);
887         misc->fops = fops;
888         if (misc_register(misc)) {
889                 pr_err("%s: could not register %s\n", __func__, misc->name);
890                 kfree(misc->name);
891                 rc = -EIO;
892                 goto done;
893         }
894
895 done:
896         va_end(args);
897         return rc;
898 }
899
900 static ssize_t dma_toggle_show(struct device *dev,
901                                 struct device_attribute *attr,
902                                 char *buf)
903 {
904         return sprintf(buf, "dma\n");
905 }
906
907 static ssize_t dma_toggle_store(struct device *dev,
908                         struct device_attribute *attr,
909                         const char *buf, size_t count)
910 {
911         pr_err("%s: Not implemented.", __func__);
912         return 0;
913 }
914
915 static DEVICE_ATTR(dma_toggle, 0644, dma_toggle_show, dma_toggle_store);
916
917 static ssize_t __attr_fifo_atn_read(char *buf, int atn_lvl)
918 {
919         switch (atn_lvl) {
920         case SPDIF_FIFO_ATN_LVL_ONE_SLOT:
921                 strncpy(buf, "1\n", 2);
922                 return 2;
923         case SPDIF_FIFO_ATN_LVL_FOUR_SLOTS:
924                 strncpy(buf, "4\n", 2);
925                 return 2;
926         case SPDIF_FIFO_ATN_LVL_EIGHT_SLOTS:
927                 strncpy(buf, "8\n", 2);
928                 return 2;
929         case SPDIF_FIFO_ATN_LVL_TWELVE_SLOTS:
930                 strncpy(buf, "12\n", 3);
931                 return 3;
932         default:
933                 BUG_ON(1);
934                 return -EIO;
935         }
936 }
937
938 static ssize_t __attr_fifo_atn_write(struct audio_driver_state *ads,
939                 struct audio_stream *as,
940                 int *fifo_lvl,
941                 const char *buf, size_t size)
942 {
943         int lvl;
944
945         if (size > 3) {
946                 pr_err("%s: buffer size %d too big\n", __func__, size);
947                 return -EINVAL;
948         }
949
950         if (sscanf(buf, "%d", &lvl) != 1) {
951                 pr_err("%s: invalid input string [%s]\n", __func__, buf);
952                 return -EINVAL;
953         }
954
955         switch (lvl) {
956         case 1:
957                 lvl = SPDIF_FIFO_ATN_LVL_ONE_SLOT;
958                 break;
959         case 4:
960                 lvl = SPDIF_FIFO_ATN_LVL_FOUR_SLOTS;
961                 break;
962         case 8:
963                 lvl = SPDIF_FIFO_ATN_LVL_EIGHT_SLOTS;
964                 break;
965         case 12:
966                 lvl = SPDIF_FIFO_ATN_LVL_TWELVE_SLOTS;
967                 break;
968         default:
969                 pr_err("%s: invalid attention level %d\n", __func__, lvl);
970                 return -EINVAL;
971         }
972
973         *fifo_lvl = lvl;
974         pr_info("%s: fifo level %d\n", __func__, *fifo_lvl);
975
976         return size;
977 }
978
979 static ssize_t tx_fifo_atn_show(struct device *dev,
980                                 struct device_attribute *attr,
981                                 char *buf)
982 {
983         struct tegra_audio_platform_data *pdata = dev->platform_data;
984         struct audio_driver_state *ads = pdata->driver_data;
985         return __attr_fifo_atn_read(buf, ads->out.spdif_fifo_atn_level);
986 }
987
988 static ssize_t tx_fifo_atn_store(struct device *dev,
989                         struct device_attribute *attr,
990                         const char *buf, size_t count)
991 {
992         ssize_t rc;
993         struct tegra_audio_platform_data *pdata = dev->platform_data;
994         struct audio_driver_state *ads = pdata->driver_data;
995         mutex_lock(&ads->out.lock);
996         if (pending_buffer_requests(&ads->out)) {
997                 pr_err("%s: playback in progress.\n", __func__);
998                 rc = -EBUSY;
999                 goto done;
1000         }
1001         rc = __attr_fifo_atn_write(ads, &ads->out,
1002                         &ads->out.spdif_fifo_atn_level,
1003                         buf, count);
1004 done:
1005         mutex_unlock(&ads->out.lock);
1006         return rc;
1007 }
1008
1009 static DEVICE_ATTR(tx_fifo_atn, 0644, tx_fifo_atn_show, tx_fifo_atn_store);
1010
1011
1012 static int spdif_configure(struct platform_device *pdev)
1013 {
1014         struct tegra_audio_platform_data *pdata = pdev->dev.platform_data;
1015         struct audio_driver_state *state = pdata->driver_data;
1016
1017         if (!state)
1018                 return -ENOMEM;
1019
1020         /* disable interrupts from SPDIF */
1021         spdif_writel(state->spdif_base, 0x0, SPDIF_CTRL_0);
1022         spdif_fifo_clear(state->spdif_base);
1023         spdif_enable_fifos(state->spdif_base, 0);
1024
1025         spdif_set_bit_mode(state->spdif_base, SPDIF_BIT_MODE_MODE16BIT);
1026         spdif_set_fifo_packed(state->spdif_base, 1);
1027
1028         spdif_fifo_set_attention_level(state->spdif_base,
1029                 state->out.spdif_fifo_atn_level);
1030
1031         spdif_set_sample_rate(state, 44100);
1032
1033         state->fifo_init = true;
1034         return 0;
1035 }
1036
1037 static int tegra_spdif_probe(struct platform_device *pdev)
1038 {
1039         int rc, i;
1040         struct resource *res;
1041         struct audio_driver_state *state;
1042
1043         pr_info("%s\n", __func__);
1044
1045         state = kzalloc(sizeof(*state), GFP_KERNEL);
1046         if (!state)
1047                 return -ENOMEM;
1048
1049         state->pdev = pdev;
1050         state->pdata = pdev->dev.platform_data;
1051         state->pdata->driver_data = state;
1052         BUG_ON(!state->pdata);
1053
1054         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1055         if (!res) {
1056                 dev_err(&pdev->dev, "no mem resource!\n");
1057                 return -ENODEV;
1058         }
1059
1060         if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
1061                 dev_err(&pdev->dev, "memory region already claimed!\n");
1062                 return -ENOMEM;
1063         }
1064
1065         state->spdif_phys = res->start;
1066         state->spdif_base = (unsigned long)ioremap(res->start,
1067                         res->end - res->start + 1);
1068         if (!state->spdif_base) {
1069                 dev_err(&pdev->dev, "cannot remap iomem!\n");
1070                 return -EIO;
1071         }
1072
1073         res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1074         if (!res) {
1075                 dev_err(&pdev->dev, "no dma resource!\n");
1076                 return -ENODEV;
1077         }
1078         state->dma_req_sel = res->start;
1079
1080         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1081         if (!res) {
1082                 dev_err(&pdev->dev, "no irq resource!\n");
1083                 return -ENODEV;
1084         }
1085         state->irq = res->start;
1086
1087         rc = spdif_configure(pdev);
1088         if (rc < 0)
1089                 return rc;
1090
1091         state->out.opened = 0;
1092         state->out.active = false;
1093         mutex_init(&state->out.lock);
1094         init_completion(&state->out.stop_completion);
1095         spin_lock_init(&state->out.dma_req_lock);
1096         state->out.dma_chan = NULL;
1097         state->out.num_bufs = SPDIF_DEFAULT_TX_NUM_BUFS;
1098         for (i = 0; i < SPDIF_MAX_NUM_BUFS; i++) {
1099                 init_completion(&state->out.comp[i]);
1100                         /* TX buf rest state is unqueued, complete. */
1101                 complete(&state->out.comp[i]);
1102                 state->out.buffer[i] = 0;
1103                 state->out.buf_phy[i] = 0;
1104         }
1105         state->out.last_queued = 0;
1106         rc = init_stream_buffer(&state->out, state->out.num_bufs);
1107         if (rc < 0)
1108                 return rc;
1109
1110         INIT_WORK(&state->out.allow_suspend_work, allow_suspend_worker);
1111         snprintf(state->out.wake_lock_name, sizeof(state->out.wake_lock_name),
1112                 "tegra-audio-spdif");
1113         wake_lock_init(&state->out.wake_lock, WAKE_LOCK_SUSPEND,
1114                         state->out.wake_lock_name);
1115
1116         if (request_irq(state->irq, spdif_interrupt,
1117                         IRQF_DISABLED, state->pdev->name, state) < 0) {
1118                 dev_err(&pdev->dev,
1119                         "%s: could not register handler for irq %d\n",
1120                         __func__, state->irq);
1121                 return -EIO;
1122         }
1123
1124         rc = setup_misc_device(&state->misc_out,
1125                         &tegra_spdif_out_fops,
1126                         "spdif_out");
1127         if (rc < 0)
1128                 return rc;
1129
1130         rc = setup_misc_device(&state->misc_out_ctl,
1131                         &tegra_spdif_out_ctl_fops,
1132                         "spdif_out_ctl");
1133         if (rc < 0)
1134                 return rc;
1135
1136         sound_ops->setup(state);
1137
1138         rc = device_create_file(&pdev->dev, &dev_attr_dma_toggle);
1139         if (rc < 0) {
1140                 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
1141                         __func__, dev_attr_dma_toggle.attr.name, rc);
1142                 return rc;
1143         }
1144
1145         rc = device_create_file(&pdev->dev, &dev_attr_tx_fifo_atn);
1146         if (rc < 0) {
1147                 dev_err(&pdev->dev, "%s: could not create sysfs entry %s: %d\n",
1148                         __func__, dev_attr_tx_fifo_atn.attr.name, rc);
1149                 return rc;
1150         }
1151
1152         return 0;
1153 }
1154
1155 #ifdef CONFIG_PM
1156 static int tegra_spdif_suspend(struct platform_device *pdev, pm_message_t mesg)
1157 {
1158         /* dev_info(&pdev->dev, "%s\n", __func__); */
1159         return 0;
1160 }
1161
1162 static int tegra_spdif_resume(struct platform_device *pdev)
1163 {
1164         return spdif_configure(pdev);
1165 }
1166 #endif /* CONFIG_PM */
1167
1168 static struct platform_driver tegra_spdif_driver = {
1169         .driver = {
1170                 .name = "spdif_out",
1171                 .owner = THIS_MODULE,
1172         },
1173         .probe = tegra_spdif_probe,
1174 #ifdef CONFIG_PM
1175         .suspend = tegra_spdif_suspend,
1176         .resume = tegra_spdif_resume,
1177 #endif
1178 };
1179
1180 static int __init tegra_spdif_init(void)
1181 {
1182         return platform_driver_register(&tegra_spdif_driver);
1183 }
1184
1185 module_init(tegra_spdif_init);
1186 MODULE_LICENSE("GPL");