blob: 84d9e77c0fbe1e8d5e737d26beb492c3ce85883b [file] [log] [blame]
Jassi Brar5033f432010-11-22 15:37:25 +09001/* sound/soc/samsung/i2s.c
Jassi Brar1c7ac012010-11-22 15:36:59 +09002 *
3 * ALSA SoC Audio Layer - Samsung I2S Controller driver
4 *
5 * Copyright (c) 2010 Samsung Electronics Co. Ltd.
Jaswinder Singhdf8ad332012-02-25 16:24:36 +05306 * Jaswinder Singh <jassisinghbrar@gmail.com>
Jassi Brar1c7ac012010-11-22 15:36:59 +09007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +010013#include <dt-bindings/sound/samsung-i2s.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090014#include <linux/delay.h>
15#include <linux/slab.h>
16#include <linux/clk.h>
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +010017#include <linux/clk-provider.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090018#include <linux/io.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
Padmavathi Venna40476f62013-01-18 17:17:01 +053020#include <linux/of.h>
21#include <linux/of_gpio.h>
Mark Brownc5cf4db2011-12-08 16:45:03 +080022#include <linux/pm_runtime.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090023
Jassi Brar1c7ac012010-11-22 15:36:59 +090024#include <sound/soc.h>
Seungwhan Youn0378b6a2011-01-11 07:26:06 +090025#include <sound/pcm_params.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090026
Arnd Bergmann436d42c2012-08-24 15:22:12 +020027#include <linux/platform_data/asoc-s3c.h>
Jassi Brar1c7ac012010-11-22 15:36:59 +090028
29#include "dma.h"
Sangbeom Kim61100f42011-07-20 17:07:12 +090030#include "idma.h"
Jassi Brar1c7ac012010-11-22 15:36:59 +090031#include "i2s.h"
Sangbeom Kim172a4532011-06-20 16:36:18 +090032#include "i2s-regs.h"
Jassi Brar1c7ac012010-11-22 15:36:59 +090033
34#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
35
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +053036enum samsung_dai_type {
37 TYPE_PRI,
38 TYPE_SEC,
39};
40
Padmavathi Vennaa5a56872014-11-07 12:24:40 +053041struct samsung_i2s_variant_regs {
42 unsigned int bfs_off;
43 unsigned int rfs_off;
44 unsigned int sdf_off;
45 unsigned int txr_off;
46 unsigned int rclksrc_off;
47 unsigned int mss_off;
48 unsigned int cdclkcon_off;
49 unsigned int lrp_off;
50 unsigned int bfs_mask;
51 unsigned int rfs_mask;
52 unsigned int ftx0cnt_off;
53};
54
Padmavathi Venna40476f62013-01-18 17:17:01 +053055struct samsung_i2s_dai_data {
56 int dai_type;
Padmavathi Venna7da493e2013-08-12 15:19:51 +053057 u32 quirks;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +053058 const struct samsung_i2s_variant_regs *i2s_variant_regs;
Padmavathi Venna40476f62013-01-18 17:17:01 +053059};
60
Jassi Brar1c7ac012010-11-22 15:36:59 +090061struct i2s_dai {
62 /* Platform device for this DAI */
63 struct platform_device *pdev;
Sylwester Nawrockiaf1cf5c2015-01-14 19:42:30 +010064 /* Memory mapped SFR region */
Jassi Brar1c7ac012010-11-22 15:36:59 +090065 void __iomem *addr;
Jassi Brar1c7ac012010-11-22 15:36:59 +090066 /* Rate of RCLK source clock */
67 unsigned long rclk_srcrate;
68 /* Frame Clock */
69 unsigned frmclk;
70 /*
71 * Specifically requested RCLK,BCLK by MACHINE Driver.
72 * 0 indicates CPU driver is free to choose any value.
73 */
74 unsigned rfs, bfs;
75 /* I2S Controller's core clock */
76 struct clk *clk;
77 /* Clock for generating I2S signals */
78 struct clk *op_clk;
Jassi Brar1c7ac012010-11-22 15:36:59 +090079 /* Pointer to the Primary_Fifo if this is Sec_Fifo, NULL otherwise */
80 struct i2s_dai *pri_dai;
81 /* Pointer to the Secondary_Fifo if it has one, NULL otherwise */
82 struct i2s_dai *sec_dai;
83#define DAI_OPENED (1 << 0) /* Dai is opened */
84#define DAI_MANAGER (1 << 1) /* Dai is the manager */
85 unsigned mode;
86 /* Driver for this DAI */
87 struct snd_soc_dai_driver i2s_dai_drv;
88 /* DMA parameters */
89 struct s3c_dma_params dma_playback;
90 struct s3c_dma_params dma_capture;
Sangbeom Kim61100f42011-07-20 17:07:12 +090091 struct s3c_dma_params idma_playback;
Arnd Bergmann9bdca822015-11-18 22:31:11 +010092 dma_filter_fn filter;
Jassi Brar1c7ac012010-11-22 15:36:59 +090093 u32 quirks;
94 u32 suspend_i2smod;
95 u32 suspend_i2scon;
96 u32 suspend_i2spsr;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +053097 const struct samsung_i2s_variant_regs *variant_regs;
Sylwester Nawrockif3670532015-01-14 19:42:35 +010098
99 /* Spinlock protecting access to the device's registers */
100 spinlock_t spinlock;
101 spinlock_t *lock;
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +0100102
103 /* Below fields are only valid if this is the primary FIFO */
104 struct clk *clk_table[3];
105 struct clk_onecell_data clk_data;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900106};
107
108/* Lock for cross i/f checks */
109static DEFINE_SPINLOCK(lock);
110
111/* If this is the 'overlay' stereo DAI */
112static inline bool is_secondary(struct i2s_dai *i2s)
113{
114 return i2s->pri_dai ? true : false;
115}
116
117/* If operating in SoC-Slave mode */
118static inline bool is_slave(struct i2s_dai *i2s)
119{
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530120 u32 mod = readl(i2s->addr + I2SMOD);
121 return (mod & (1 << i2s->variant_regs->mss_off)) ? true : false;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900122}
123
124/* If this interface of the controller is transmitting data */
125static inline bool tx_active(struct i2s_dai *i2s)
126{
127 u32 active;
128
129 if (!i2s)
130 return false;
131
Sangbeom Kim33195502011-06-10 10:36:54 +0900132 active = readl(i2s->addr + I2SCON);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900133
134 if (is_secondary(i2s))
135 active &= CON_TXSDMA_ACTIVE;
136 else
137 active &= CON_TXDMA_ACTIVE;
138
139 return active ? true : false;
140}
141
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100142/* Return pointer to the other DAI */
143static inline struct i2s_dai *get_other_dai(struct i2s_dai *i2s)
144{
145 return i2s->pri_dai ? : i2s->sec_dai;
146}
147
Jassi Brar1c7ac012010-11-22 15:36:59 +0900148/* If the other interface of the controller is transmitting data */
149static inline bool other_tx_active(struct i2s_dai *i2s)
150{
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100151 struct i2s_dai *other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900152
153 return tx_active(other);
154}
155
156/* If any interface of the controller is transmitting data */
157static inline bool any_tx_active(struct i2s_dai *i2s)
158{
159 return tx_active(i2s) || other_tx_active(i2s);
160}
161
162/* If this interface of the controller is receiving data */
163static inline bool rx_active(struct i2s_dai *i2s)
164{
165 u32 active;
166
167 if (!i2s)
168 return false;
169
Sangbeom Kim33195502011-06-10 10:36:54 +0900170 active = readl(i2s->addr + I2SCON) & CON_RXDMA_ACTIVE;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900171
172 return active ? true : false;
173}
174
175/* If the other interface of the controller is receiving data */
176static inline bool other_rx_active(struct i2s_dai *i2s)
177{
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100178 struct i2s_dai *other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900179
180 return rx_active(other);
181}
182
183/* If any interface of the controller is receiving data */
184static inline bool any_rx_active(struct i2s_dai *i2s)
185{
186 return rx_active(i2s) || other_rx_active(i2s);
187}
188
189/* If the other DAI is transmitting or receiving data */
190static inline bool other_active(struct i2s_dai *i2s)
191{
192 return other_rx_active(i2s) || other_tx_active(i2s);
193}
194
195/* If this DAI is transmitting or receiving data */
196static inline bool this_active(struct i2s_dai *i2s)
197{
198 return tx_active(i2s) || rx_active(i2s);
199}
200
201/* If the controller is active anyway */
202static inline bool any_active(struct i2s_dai *i2s)
203{
204 return this_active(i2s) || other_active(i2s);
205}
206
207static inline struct i2s_dai *to_info(struct snd_soc_dai *dai)
208{
209 return snd_soc_dai_get_drvdata(dai);
210}
211
212static inline bool is_opened(struct i2s_dai *i2s)
213{
214 if (i2s && (i2s->mode & DAI_OPENED))
215 return true;
216 else
217 return false;
218}
219
220static inline bool is_manager(struct i2s_dai *i2s)
221{
222 if (is_opened(i2s) && (i2s->mode & DAI_MANAGER))
223 return true;
224 else
225 return false;
226}
227
228/* Read RCLK of I2S (in multiples of LRCLK) */
229static inline unsigned get_rfs(struct i2s_dai *i2s)
230{
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530231 u32 rfs;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530232 rfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->rfs_off;
233 rfs &= i2s->variant_regs->rfs_mask;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900234
235 switch (rfs) {
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530236 case 7: return 192;
237 case 6: return 96;
238 case 5: return 128;
239 case 4: return 64;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900240 case 3: return 768;
241 case 2: return 384;
242 case 1: return 512;
243 default: return 256;
244 }
245}
246
247/* Write RCLK of I2S (in multiples of LRCLK) */
248static inline void set_rfs(struct i2s_dai *i2s, unsigned rfs)
249{
250 u32 mod = readl(i2s->addr + I2SMOD);
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530251 int rfs_shift = i2s->variant_regs->rfs_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900252
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530253 mod &= ~(i2s->variant_regs->rfs_mask << rfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900254
255 switch (rfs) {
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530256 case 192:
257 mod |= (EXYNOS7_MOD_RCLK_192FS << rfs_shift);
258 break;
259 case 96:
260 mod |= (EXYNOS7_MOD_RCLK_96FS << rfs_shift);
261 break;
262 case 128:
263 mod |= (EXYNOS7_MOD_RCLK_128FS << rfs_shift);
264 break;
265 case 64:
266 mod |= (EXYNOS7_MOD_RCLK_64FS << rfs_shift);
267 break;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900268 case 768:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530269 mod |= (MOD_RCLK_768FS << rfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900270 break;
271 case 512:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530272 mod |= (MOD_RCLK_512FS << rfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900273 break;
274 case 384:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530275 mod |= (MOD_RCLK_384FS << rfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900276 break;
277 default:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530278 mod |= (MOD_RCLK_256FS << rfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900279 break;
280 }
281
282 writel(mod, i2s->addr + I2SMOD);
283}
284
285/* Read Bit-Clock of I2S (in multiples of LRCLK) */
286static inline unsigned get_bfs(struct i2s_dai *i2s)
287{
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530288 u32 bfs;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530289 bfs = readl(i2s->addr + I2SMOD) >> i2s->variant_regs->bfs_off;
290 bfs &= i2s->variant_regs->bfs_mask;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900291
292 switch (bfs) {
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530293 case 8: return 256;
294 case 7: return 192;
295 case 6: return 128;
296 case 5: return 96;
297 case 4: return 64;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900298 case 3: return 24;
299 case 2: return 16;
300 case 1: return 48;
301 default: return 32;
302 }
303}
304
305/* Write Bit-Clock of I2S (in multiples of LRCLK) */
306static inline void set_bfs(struct i2s_dai *i2s, unsigned bfs)
307{
308 u32 mod = readl(i2s->addr + I2SMOD);
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530309 int tdm = i2s->quirks & QUIRK_SUPPORTS_TDM;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530310 int bfs_shift = i2s->variant_regs->bfs_off;
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530311
312 /* Non-TDM I2S controllers do not support BCLK > 48 * FS */
313 if (!tdm && bfs > 48) {
314 dev_err(&i2s->pdev->dev, "Unsupported BCLK divider\n");
315 return;
316 }
Jassi Brar1c7ac012010-11-22 15:36:59 +0900317
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530318 mod &= ~(i2s->variant_regs->bfs_mask << bfs_shift);
319
Jassi Brar1c7ac012010-11-22 15:36:59 +0900320 switch (bfs) {
321 case 48:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530322 mod |= (MOD_BCLK_48FS << bfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900323 break;
324 case 32:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530325 mod |= (MOD_BCLK_32FS << bfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900326 break;
327 case 24:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530328 mod |= (MOD_BCLK_24FS << bfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900329 break;
330 case 16:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530331 mod |= (MOD_BCLK_16FS << bfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900332 break;
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530333 case 64:
334 mod |= (EXYNOS5420_MOD_BCLK_64FS << bfs_shift);
335 break;
336 case 96:
337 mod |= (EXYNOS5420_MOD_BCLK_96FS << bfs_shift);
338 break;
339 case 128:
340 mod |= (EXYNOS5420_MOD_BCLK_128FS << bfs_shift);
341 break;
342 case 192:
343 mod |= (EXYNOS5420_MOD_BCLK_192FS << bfs_shift);
344 break;
345 case 256:
346 mod |= (EXYNOS5420_MOD_BCLK_256FS << bfs_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900347 break;
348 default:
349 dev_err(&i2s->pdev->dev, "Wrong BCLK Divider!\n");
350 return;
351 }
352
353 writel(mod, i2s->addr + I2SMOD);
354}
355
356/* Sample-Size */
357static inline int get_blc(struct i2s_dai *i2s)
358{
359 int blc = readl(i2s->addr + I2SMOD);
360
361 blc = (blc >> 13) & 0x3;
362
363 switch (blc) {
364 case 2: return 24;
365 case 1: return 8;
366 default: return 16;
367 }
368}
369
370/* TX Channel Control */
371static void i2s_txctrl(struct i2s_dai *i2s, int on)
372{
373 void __iomem *addr = i2s->addr;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530374 int txr_off = i2s->variant_regs->txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900375 u32 con = readl(addr + I2SCON);
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530376 u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900377
378 if (on) {
379 con |= CON_ACTIVE;
380 con &= ~CON_TXCH_PAUSE;
381
382 if (is_secondary(i2s)) {
383 con |= CON_TXSDMA_ACTIVE;
384 con &= ~CON_TXSDMA_PAUSE;
385 } else {
386 con |= CON_TXDMA_ACTIVE;
387 con &= ~CON_TXDMA_PAUSE;
388 }
389
390 if (any_rx_active(i2s))
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530391 mod |= 2 << txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900392 else
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530393 mod |= 0 << txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900394 } else {
395 if (is_secondary(i2s)) {
396 con |= CON_TXSDMA_PAUSE;
397 con &= ~CON_TXSDMA_ACTIVE;
398 } else {
399 con |= CON_TXDMA_PAUSE;
400 con &= ~CON_TXDMA_ACTIVE;
401 }
402
403 if (other_tx_active(i2s)) {
404 writel(con, addr + I2SCON);
405 return;
406 }
407
408 con |= CON_TXCH_PAUSE;
409
410 if (any_rx_active(i2s))
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530411 mod |= 1 << txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900412 else
413 con &= ~CON_ACTIVE;
414 }
415
416 writel(mod, addr + I2SMOD);
417 writel(con, addr + I2SCON);
418}
419
420/* RX Channel Control */
421static void i2s_rxctrl(struct i2s_dai *i2s, int on)
422{
423 void __iomem *addr = i2s->addr;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530424 int txr_off = i2s->variant_regs->txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900425 u32 con = readl(addr + I2SCON);
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530426 u32 mod = readl(addr + I2SMOD) & ~(3 << txr_off);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900427
428 if (on) {
429 con |= CON_RXDMA_ACTIVE | CON_ACTIVE;
430 con &= ~(CON_RXDMA_PAUSE | CON_RXCH_PAUSE);
431
432 if (any_tx_active(i2s))
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530433 mod |= 2 << txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900434 else
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530435 mod |= 1 << txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900436 } else {
437 con |= CON_RXDMA_PAUSE | CON_RXCH_PAUSE;
438 con &= ~CON_RXDMA_ACTIVE;
439
440 if (any_tx_active(i2s))
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530441 mod |= 0 << txr_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900442 else
443 con &= ~CON_ACTIVE;
444 }
445
446 writel(mod, addr + I2SMOD);
447 writel(con, addr + I2SCON);
448}
449
450/* Flush FIFO of an interface */
451static inline void i2s_fifo(struct i2s_dai *i2s, u32 flush)
452{
453 void __iomem *fic;
454 u32 val;
455
456 if (!i2s)
457 return;
458
459 if (is_secondary(i2s))
460 fic = i2s->addr + I2SFICS;
461 else
462 fic = i2s->addr + I2SFIC;
463
464 /* Flush the FIFO */
465 writel(readl(fic) | flush, fic);
466
467 /* Be patient */
468 val = msecs_to_loops(1) / 1000; /* 1 usec */
469 while (--val)
470 cpu_relax();
471
472 writel(readl(fic) & ~flush, fic);
473}
474
475static int i2s_set_sysclk(struct snd_soc_dai *dai,
476 int clk_id, unsigned int rfs, int dir)
477{
478 struct i2s_dai *i2s = to_info(dai);
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100479 struct i2s_dai *other = get_other_dai(i2s);
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530480 const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
481 unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
482 unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100483 u32 mod, mask, val = 0;
484
485 spin_lock(i2s->lock);
486 mod = readl(i2s->addr + I2SMOD);
487 spin_unlock(i2s->lock);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900488
489 switch (clk_id) {
Sylwester Nawrockic86d50f2014-05-19 19:30:38 +0200490 case SAMSUNG_I2S_OPCLK:
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100491 mask = MOD_OPCLK_MASK;
492 val = dir;
Sylwester Nawrockic86d50f2014-05-19 19:30:38 +0200493 break;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900494 case SAMSUNG_I2S_CDCLK:
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100495 mask = 1 << i2s_regs->cdclkcon_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900496 /* Shouldn't matter in GATING(CLOCK_IN) mode */
497 if (dir == SND_SOC_CLOCK_IN)
498 rfs = 0;
499
Charles Keepax133c2682014-09-09 16:51:49 +0100500 if ((rfs && other && other->rfs && (other->rfs != rfs)) ||
Jassi Brar1c7ac012010-11-22 15:36:59 +0900501 (any_active(i2s) &&
502 (((dir == SND_SOC_CLOCK_IN)
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530503 && !(mod & cdcon_mask)) ||
Jassi Brar1c7ac012010-11-22 15:36:59 +0900504 ((dir == SND_SOC_CLOCK_OUT)
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530505 && (mod & cdcon_mask))))) {
Jassi Brar1c7ac012010-11-22 15:36:59 +0900506 dev_err(&i2s->pdev->dev,
507 "%s:%d Other DAI busy\n", __func__, __LINE__);
508 return -EAGAIN;
509 }
510
511 if (dir == SND_SOC_CLOCK_IN)
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100512 val = 1 << i2s_regs->cdclkcon_off;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900513
514 i2s->rfs = rfs;
515 break;
516
517 case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
518 case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100519 mask = 1 << i2s_regs->rclksrc_off;
520
Jassi Brar1c7ac012010-11-22 15:36:59 +0900521 if ((i2s->quirks & QUIRK_NO_MUXPSR)
522 || (clk_id == SAMSUNG_I2S_RCLKSRC_0))
523 clk_id = 0;
524 else
525 clk_id = 1;
526
527 if (!any_active(i2s)) {
Sylwester Nawrockia6aba532014-05-22 12:10:52 +0200528 if (i2s->op_clk && !IS_ERR(i2s->op_clk)) {
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530529 if ((clk_id && !(mod & rsrc_mask)) ||
530 (!clk_id && (mod & rsrc_mask))) {
Thomas Abraham98614cf2012-10-03 08:46:58 +0900531 clk_disable_unprepare(i2s->op_clk);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900532 clk_put(i2s->op_clk);
533 } else {
Jassi Brar6ce534a2010-12-20 11:05:46 +0900534 i2s->rclk_srcrate =
535 clk_get_rate(i2s->op_clk);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900536 return 0;
537 }
538 }
539
Padmavathi Venna1974a042012-11-28 16:17:48 +0530540 if (clk_id)
541 i2s->op_clk = clk_get(&i2s->pdev->dev,
542 "i2s_opclk1");
543 else
544 i2s->op_clk = clk_get(&i2s->pdev->dev,
545 "i2s_opclk0");
Sylwester Nawrockia6aba532014-05-22 12:10:52 +0200546
547 if (WARN_ON(IS_ERR(i2s->op_clk)))
548 return PTR_ERR(i2s->op_clk);
549
Thomas Abraham98614cf2012-10-03 08:46:58 +0900550 clk_prepare_enable(i2s->op_clk);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900551 i2s->rclk_srcrate = clk_get_rate(i2s->op_clk);
552
553 /* Over-ride the other's */
554 if (other) {
555 other->op_clk = i2s->op_clk;
556 other->rclk_srcrate = i2s->rclk_srcrate;
557 }
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530558 } else if ((!clk_id && (mod & rsrc_mask))
559 || (clk_id && !(mod & rsrc_mask))) {
Jassi Brar1c7ac012010-11-22 15:36:59 +0900560 dev_err(&i2s->pdev->dev,
561 "%s:%d Other DAI busy\n", __func__, __LINE__);
562 return -EAGAIN;
563 } else {
564 /* Call can't be on the active DAI */
565 i2s->op_clk = other->op_clk;
566 i2s->rclk_srcrate = other->rclk_srcrate;
567 return 0;
568 }
569
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100570 if (clk_id == 1)
571 val = 1 << i2s_regs->rclksrc_off;
Padmavathi Vennab2de1d22014-11-20 15:33:17 +0530572 break;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900573 default:
574 dev_err(&i2s->pdev->dev, "We don't serve that!\n");
575 return -EINVAL;
576 }
577
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100578 spin_lock(i2s->lock);
579 mod = readl(i2s->addr + I2SMOD);
580 mod = (mod & ~mask) | val;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900581 writel(mod, i2s->addr + I2SMOD);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100582 spin_unlock(i2s->lock);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900583
584 return 0;
585}
586
587static int i2s_set_fmt(struct snd_soc_dai *dai,
588 unsigned int fmt)
589{
590 struct i2s_dai *i2s = to_info(dai);
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530591 int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100592 u32 mod, tmp = 0;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900593
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530594 lrp_shift = i2s->variant_regs->lrp_off;
595 sdf_shift = i2s->variant_regs->sdf_off;
596 mod_slave = 1 << i2s->variant_regs->mss_off;
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +0530597
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530598 sdf_mask = MOD_SDF_MASK << sdf_shift;
599 lrp_rlow = MOD_LR_RLOW << lrp_shift;
600
Jassi Brar1c7ac012010-11-22 15:36:59 +0900601 /* Format is priority */
602 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
603 case SND_SOC_DAIFMT_RIGHT_J:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530604 tmp |= lrp_rlow;
605 tmp |= (MOD_SDF_MSB << sdf_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900606 break;
607 case SND_SOC_DAIFMT_LEFT_J:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530608 tmp |= lrp_rlow;
609 tmp |= (MOD_SDF_LSB << sdf_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900610 break;
611 case SND_SOC_DAIFMT_I2S:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530612 tmp |= (MOD_SDF_IIS << sdf_shift);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900613 break;
614 default:
615 dev_err(&i2s->pdev->dev, "Format not supported\n");
616 return -EINVAL;
617 }
618
619 /*
620 * INV flag is relative to the FORMAT flag - if set it simply
621 * flips the polarity specified by the Standard
622 */
623 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
624 case SND_SOC_DAIFMT_NB_NF:
625 break;
626 case SND_SOC_DAIFMT_NB_IF:
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530627 if (tmp & lrp_rlow)
628 tmp &= ~lrp_rlow;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900629 else
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530630 tmp |= lrp_rlow;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900631 break;
632 default:
633 dev_err(&i2s->pdev->dev, "Polarity not supported\n");
634 return -EINVAL;
635 }
636
637 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
638 case SND_SOC_DAIFMT_CBM_CFM:
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530639 tmp |= mod_slave;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900640 break;
641 case SND_SOC_DAIFMT_CBS_CFS:
642 /* Set default source clock in Master mode */
643 if (i2s->rclk_srcrate == 0)
644 i2s_set_sysclk(dai, SAMSUNG_I2S_RCLKSRC_0,
645 0, SND_SOC_CLOCK_IN);
646 break;
647 default:
648 dev_err(&i2s->pdev->dev, "master/slave format not supported\n");
649 return -EINVAL;
650 }
651
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100652 spin_lock(i2s->lock);
653 mod = readl(i2s->addr + I2SMOD);
Padmavathi Vennab60be4a2013-07-26 19:06:48 +0530654 /*
655 * Don't change the I2S mode if any controller is active on this
656 * channel.
657 */
Jassi Brar1c7ac012010-11-22 15:36:59 +0900658 if (any_active(i2s) &&
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530659 ((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100660 spin_unlock(i2s->lock);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900661 dev_err(&i2s->pdev->dev,
662 "%s:%d Other DAI busy\n", __func__, __LINE__);
663 return -EAGAIN;
664 }
665
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530666 mod &= ~(sdf_mask | lrp_rlow | mod_slave);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900667 mod |= tmp;
668 writel(mod, i2s->addr + I2SMOD);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100669 spin_unlock(i2s->lock);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900670
671 return 0;
672}
673
674static int i2s_hw_params(struct snd_pcm_substream *substream,
675 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
676{
677 struct i2s_dai *i2s = to_info(dai);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100678 u32 mod, mask = 0, val = 0;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900679
680 if (!is_secondary(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100681 mask |= (MOD_DC2_EN | MOD_DC1_EN);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900682
683 switch (params_channels(params)) {
684 case 6:
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100685 val |= MOD_DC2_EN;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900686 case 4:
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100687 val |= MOD_DC1_EN;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900688 break;
689 case 2:
Sangsu Park588fb702012-03-16 15:40:53 +0900690 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
691 i2s->dma_playback.dma_size = 4;
692 else
693 i2s->dma_capture.dma_size = 4;
694 break;
695 case 1:
696 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
697 i2s->dma_playback.dma_size = 2;
698 else
699 i2s->dma_capture.dma_size = 2;
700
Jassi Brar1c7ac012010-11-22 15:36:59 +0900701 break;
702 default:
703 dev_err(&i2s->pdev->dev, "%d channels not supported\n",
704 params_channels(params));
705 return -EINVAL;
706 }
707
708 if (is_secondary(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100709 mask |= MOD_BLCS_MASK;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900710 else
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100711 mask |= MOD_BLCP_MASK;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900712
713 if (is_manager(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100714 mask |= MOD_BLC_MASK;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900715
Tushar Behera88ce1462014-05-23 17:35:39 +0530716 switch (params_width(params)) {
717 case 8:
Jassi Brar1c7ac012010-11-22 15:36:59 +0900718 if (is_secondary(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100719 val |= MOD_BLCS_8BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900720 else
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100721 val |= MOD_BLCP_8BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900722 if (is_manager(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100723 val |= MOD_BLC_8BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900724 break;
Tushar Behera88ce1462014-05-23 17:35:39 +0530725 case 16:
Jassi Brar1c7ac012010-11-22 15:36:59 +0900726 if (is_secondary(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100727 val |= MOD_BLCS_16BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900728 else
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100729 val |= MOD_BLCP_16BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900730 if (is_manager(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100731 val |= MOD_BLC_16BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900732 break;
Tushar Behera88ce1462014-05-23 17:35:39 +0530733 case 24:
Jassi Brar1c7ac012010-11-22 15:36:59 +0900734 if (is_secondary(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100735 val |= MOD_BLCS_24BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900736 else
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100737 val |= MOD_BLCP_24BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900738 if (is_manager(i2s))
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100739 val |= MOD_BLC_24BIT;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900740 break;
741 default:
742 dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
743 params_format(params));
744 return -EINVAL;
745 }
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100746
747 spin_lock(i2s->lock);
748 mod = readl(i2s->addr + I2SMOD);
749 mod = (mod & ~mask) | val;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900750 writel(mod, i2s->addr + I2SMOD);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100751 spin_unlock(i2s->lock);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900752
Mark Brownd37bdf72013-12-05 14:14:52 +0000753 samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
754
Jassi Brar1c7ac012010-11-22 15:36:59 +0900755 i2s->frmclk = params_rate(params);
756
757 return 0;
758}
759
760/* We set constraints on the substream acc to the version of I2S */
761static int i2s_startup(struct snd_pcm_substream *substream,
762 struct snd_soc_dai *dai)
763{
764 struct i2s_dai *i2s = to_info(dai);
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100765 struct i2s_dai *other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900766 unsigned long flags;
767
768 spin_lock_irqsave(&lock, flags);
769
770 i2s->mode |= DAI_OPENED;
771
772 if (is_manager(other))
773 i2s->mode &= ~DAI_MANAGER;
774 else
775 i2s->mode |= DAI_MANAGER;
776
Padmavathi Venna2d778282013-01-24 18:05:31 +0530777 if (!any_active(i2s) && (i2s->quirks & QUIRK_NEED_RSTCLR))
778 writel(CON_RSTCLR, i2s->addr + I2SCON);
779
Jassi Brar1c7ac012010-11-22 15:36:59 +0900780 spin_unlock_irqrestore(&lock, flags);
781
782 return 0;
783}
784
785static void i2s_shutdown(struct snd_pcm_substream *substream,
786 struct snd_soc_dai *dai)
787{
788 struct i2s_dai *i2s = to_info(dai);
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100789 struct i2s_dai *other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900790 unsigned long flags;
791
792 spin_lock_irqsave(&lock, flags);
793
794 i2s->mode &= ~DAI_OPENED;
795 i2s->mode &= ~DAI_MANAGER;
796
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +0100797 if (is_opened(other))
Jassi Brar1c7ac012010-11-22 15:36:59 +0900798 other->mode |= DAI_MANAGER;
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +0100799
Jassi Brar1c7ac012010-11-22 15:36:59 +0900800 /* Reset any constraint on RFS and BFS */
801 i2s->rfs = 0;
802 i2s->bfs = 0;
803
804 spin_unlock_irqrestore(&lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900805}
806
807static int config_setup(struct i2s_dai *i2s)
808{
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100809 struct i2s_dai *other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900810 unsigned rfs, bfs, blc;
811 u32 psr;
812
813 blc = get_blc(i2s);
814
815 bfs = i2s->bfs;
816
817 if (!bfs && other)
818 bfs = other->bfs;
819
820 /* Select least possible multiple(2) if no constraint set */
821 if (!bfs)
822 bfs = blc * 2;
823
824 rfs = i2s->rfs;
825
826 if (!rfs && other)
827 rfs = other->rfs;
828
829 if ((rfs == 256 || rfs == 512) && (blc == 24)) {
830 dev_err(&i2s->pdev->dev,
831 "%d-RFS not supported for 24-blc\n", rfs);
832 return -EINVAL;
833 }
834
835 if (!rfs) {
836 if (bfs == 16 || bfs == 32)
837 rfs = 256;
838 else
839 rfs = 384;
840 }
841
842 /* If already setup and running */
843 if (any_active(i2s) && (get_rfs(i2s) != rfs || get_bfs(i2s) != bfs)) {
844 dev_err(&i2s->pdev->dev,
845 "%s:%d Other DAI busy\n", __func__, __LINE__);
846 return -EAGAIN;
847 }
848
Jassi Brar1c7ac012010-11-22 15:36:59 +0900849 set_bfs(i2s, bfs);
850 set_rfs(i2s, rfs);
851
Padmavathi Venna77010012013-07-11 12:38:25 +0530852 /* Don't bother with PSR in Slave mode */
853 if (is_slave(i2s))
854 return 0;
855
Jassi Brar1c7ac012010-11-22 15:36:59 +0900856 if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
857 psr = i2s->rclk_srcrate / i2s->frmclk / rfs;
858 writel(((psr - 1) << 8) | PSR_PSREN, i2s->addr + I2SPSR);
859 dev_dbg(&i2s->pdev->dev,
860 "RCLK_SRC=%luHz PSR=%u, RCLK=%dfs, BCLK=%dfs\n",
861 i2s->rclk_srcrate, psr, rfs, bfs);
862 }
863
864 return 0;
865}
866
867static int i2s_trigger(struct snd_pcm_substream *substream,
868 int cmd, struct snd_soc_dai *dai)
869{
870 int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
871 struct snd_soc_pcm_runtime *rtd = substream->private_data;
872 struct i2s_dai *i2s = to_info(rtd->cpu_dai);
873 unsigned long flags;
874
875 switch (cmd) {
876 case SNDRV_PCM_TRIGGER_START:
877 case SNDRV_PCM_TRIGGER_RESUME:
878 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Sylwester Nawrockif3670532015-01-14 19:42:35 +0100879 spin_lock_irqsave(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900880
Jassi Brar1c7ac012010-11-22 15:36:59 +0900881 if (config_setup(i2s)) {
Sylwester Nawrockif3670532015-01-14 19:42:35 +0100882 spin_unlock_irqrestore(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900883 return -EINVAL;
884 }
885
886 if (capture)
887 i2s_rxctrl(i2s, 1);
888 else
889 i2s_txctrl(i2s, 1);
890
Sylwester Nawrockif3670532015-01-14 19:42:35 +0100891 spin_unlock_irqrestore(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900892 break;
893 case SNDRV_PCM_TRIGGER_STOP:
894 case SNDRV_PCM_TRIGGER_SUSPEND:
895 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Sylwester Nawrockif3670532015-01-14 19:42:35 +0100896 spin_lock_irqsave(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900897
Jassi Brarc90887f2012-02-25 16:42:34 +0530898 if (capture) {
Jassi Brar1c7ac012010-11-22 15:36:59 +0900899 i2s_rxctrl(i2s, 0);
Jassi Brar775bc972010-12-20 11:05:47 +0900900 i2s_fifo(i2s, FIC_RXFLUSH);
Jassi Brarc90887f2012-02-25 16:42:34 +0530901 } else {
902 i2s_txctrl(i2s, 0);
Jassi Brar775bc972010-12-20 11:05:47 +0900903 i2s_fifo(i2s, FIC_TXFLUSH);
Jassi Brarc90887f2012-02-25 16:42:34 +0530904 }
Jassi Brar775bc972010-12-20 11:05:47 +0900905
Sylwester Nawrockif3670532015-01-14 19:42:35 +0100906 spin_unlock_irqrestore(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900907 break;
908 }
909
910 return 0;
911}
912
913static int i2s_set_clkdiv(struct snd_soc_dai *dai,
914 int div_id, int div)
915{
916 struct i2s_dai *i2s = to_info(dai);
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100917 struct i2s_dai *other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900918
919 switch (div_id) {
920 case SAMSUNG_I2S_DIV_BCLK:
921 if ((any_active(i2s) && div && (get_bfs(i2s) != div))
922 || (other && other->bfs && (other->bfs != div))) {
923 dev_err(&i2s->pdev->dev,
924 "%s:%d Other DAI busy\n", __func__, __LINE__);
925 return -EAGAIN;
926 }
927 i2s->bfs = div;
928 break;
929 default:
930 dev_err(&i2s->pdev->dev,
931 "Invalid clock divider(%d)\n", div_id);
932 return -EINVAL;
933 }
934
935 return 0;
936}
937
938static snd_pcm_sframes_t
939i2s_delay(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
940{
941 struct i2s_dai *i2s = to_info(dai);
942 u32 reg = readl(i2s->addr + I2SFIC);
943 snd_pcm_sframes_t delay;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530944 const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900945
946 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
947 delay = FIC_RXCOUNT(reg);
948 else if (is_secondary(i2s))
949 delay = FICS_TXCOUNT(readl(i2s->addr + I2SFICS));
950 else
Padmavathi Vennaa5a56872014-11-07 12:24:40 +0530951 delay = (reg >> i2s_regs->ftx0cnt_off) & 0x7f;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900952
953 return delay;
954}
955
956#ifdef CONFIG_PM
957static int i2s_suspend(struct snd_soc_dai *dai)
958{
959 struct i2s_dai *i2s = to_info(dai);
960
Sylwester Nawrockid3d4e522014-07-04 16:05:45 +0200961 i2s->suspend_i2smod = readl(i2s->addr + I2SMOD);
962 i2s->suspend_i2scon = readl(i2s->addr + I2SCON);
963 i2s->suspend_i2spsr = readl(i2s->addr + I2SPSR);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900964
965 return 0;
966}
967
968static int i2s_resume(struct snd_soc_dai *dai)
969{
970 struct i2s_dai *i2s = to_info(dai);
971
Sylwester Nawrockid3d4e522014-07-04 16:05:45 +0200972 writel(i2s->suspend_i2scon, i2s->addr + I2SCON);
973 writel(i2s->suspend_i2smod, i2s->addr + I2SMOD);
974 writel(i2s->suspend_i2spsr, i2s->addr + I2SPSR);
Jassi Brar1c7ac012010-11-22 15:36:59 +0900975
976 return 0;
977}
978#else
979#define i2s_suspend NULL
980#define i2s_resume NULL
981#endif
982
983static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
984{
985 struct i2s_dai *i2s = to_info(dai);
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +0100986 struct i2s_dai *other = get_other_dai(i2s);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +0100987 unsigned long flags;
Jassi Brar1c7ac012010-11-22 15:36:59 +0900988
Sylwester Nawrocki0ec2ba82015-01-14 19:42:31 +0100989 if (is_secondary(i2s)) { /* If this is probe on the secondary DAI */
Mark Brown36885692013-10-19 15:23:15 +0100990 samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback,
991 NULL);
Sylwester Nawrocki872c26b2015-01-14 19:42:34 +0100992 } else {
993 samsung_asoc_init_dma_data(dai, &i2s->dma_playback,
994 &i2s->dma_capture);
995
996 if (i2s->quirks & QUIRK_NEED_RSTCLR)
997 writel(CON_RSTCLR, i2s->addr + I2SCON);
998
999 if (i2s->quirks & QUIRK_SUPPORTS_IDMA)
1000 idma_reg_addr_init(i2s->addr,
1001 i2s->sec_dai->idma_playback.dma_addr);
Mark Brown36885692013-10-19 15:23:15 +01001002 }
Jassi Brar1c7ac012010-11-22 15:36:59 +09001003
Jassi Brar1c7ac012010-11-22 15:36:59 +09001004 /* Reset any constraint on RFS and BFS */
1005 i2s->rfs = 0;
1006 i2s->bfs = 0;
Tushar Beherad66eac32014-04-23 13:34:24 +05301007 i2s->rclk_srcrate = 0;
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +01001008
1009 spin_lock_irqsave(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001010 i2s_txctrl(i2s, 0);
1011 i2s_rxctrl(i2s, 0);
1012 i2s_fifo(i2s, FIC_TXFLUSH);
1013 i2s_fifo(other, FIC_TXFLUSH);
1014 i2s_fifo(i2s, FIC_RXFLUSH);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +01001015 spin_unlock_irqrestore(i2s->lock, flags);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001016
1017 /* Gate CDCLK by default */
1018 if (!is_opened(other))
1019 i2s_set_sysclk(dai, SAMSUNG_I2S_CDCLK,
1020 0, SND_SOC_CLOCK_IN);
1021
1022 return 0;
1023}
1024
1025static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
1026{
1027 struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001028
Sylwester Nawrockic92f1d02015-01-14 19:42:32 +01001029 if (!is_secondary(i2s)) {
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +01001030 if (i2s->quirks & QUIRK_NEED_RSTCLR) {
1031 spin_lock(i2s->lock);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001032 writel(0, i2s->addr + I2SCON);
Sylwester Nawrockice8bcdb2015-01-14 19:42:36 +01001033 spin_unlock(i2s->lock);
1034 }
Jassi Brar1c7ac012010-11-22 15:36:59 +09001035 }
1036
Jassi Brar1c7ac012010-11-22 15:36:59 +09001037 return 0;
1038}
1039
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01001040static const struct snd_soc_dai_ops samsung_i2s_dai_ops = {
Jassi Brar1c7ac012010-11-22 15:36:59 +09001041 .trigger = i2s_trigger,
1042 .hw_params = i2s_hw_params,
1043 .set_fmt = i2s_set_fmt,
1044 .set_clkdiv = i2s_set_clkdiv,
1045 .set_sysclk = i2s_set_sysclk,
1046 .startup = i2s_startup,
1047 .shutdown = i2s_shutdown,
1048 .delay = i2s_delay,
1049};
1050
Kuninori Morimoto4b828532013-03-21 03:35:55 -07001051static const struct snd_soc_component_driver samsung_i2s_component = {
1052 .name = "samsung-i2s",
1053};
1054
Jassi Brar1c7ac012010-11-22 15:36:59 +09001055#define SAMSUNG_I2S_RATES SNDRV_PCM_RATE_8000_96000
1056
1057#define SAMSUNG_I2S_FMTS (SNDRV_PCM_FMTBIT_S8 | \
1058 SNDRV_PCM_FMTBIT_S16_LE | \
1059 SNDRV_PCM_FMTBIT_S24_LE)
1060
Bill Pembertonfdca21a2012-12-07 09:26:15 -05001061static struct i2s_dai *i2s_alloc_dai(struct platform_device *pdev, bool sec)
Jassi Brar1c7ac012010-11-22 15:36:59 +09001062{
1063 struct i2s_dai *i2s;
Prathyush Kc6f9b1e2013-04-02 16:53:02 +05301064 int ret;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001065
Mark Brownb960ce72011-12-03 20:30:37 +00001066 i2s = devm_kzalloc(&pdev->dev, sizeof(struct i2s_dai), GFP_KERNEL);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001067 if (i2s == NULL)
1068 return NULL;
1069
1070 i2s->pdev = pdev;
1071 i2s->pri_dai = NULL;
1072 i2s->sec_dai = NULL;
1073 i2s->i2s_dai_drv.symmetric_rates = 1;
1074 i2s->i2s_dai_drv.probe = samsung_i2s_dai_probe;
1075 i2s->i2s_dai_drv.remove = samsung_i2s_dai_remove;
1076 i2s->i2s_dai_drv.ops = &samsung_i2s_dai_ops;
1077 i2s->i2s_dai_drv.suspend = i2s_suspend;
1078 i2s->i2s_dai_drv.resume = i2s_resume;
Charles Keepaxa0ff6ea2013-09-11 15:27:29 +01001079 i2s->i2s_dai_drv.playback.channels_min = 1;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001080 i2s->i2s_dai_drv.playback.channels_max = 2;
1081 i2s->i2s_dai_drv.playback.rates = SAMSUNG_I2S_RATES;
1082 i2s->i2s_dai_drv.playback.formats = SAMSUNG_I2S_FMTS;
1083
1084 if (!sec) {
Sangsu Park588fb702012-03-16 15:40:53 +09001085 i2s->i2s_dai_drv.capture.channels_min = 1;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001086 i2s->i2s_dai_drv.capture.channels_max = 2;
1087 i2s->i2s_dai_drv.capture.rates = SAMSUNG_I2S_RATES;
1088 i2s->i2s_dai_drv.capture.formats = SAMSUNG_I2S_FMTS;
Prathyush Kc6f9b1e2013-04-02 16:53:02 +05301089 dev_set_drvdata(&i2s->pdev->dev, i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001090 } else { /* Create a new platform_device for Secondary */
Prathyush Kc6f9b1e2013-04-02 16:53:02 +05301091 i2s->pdev = platform_device_alloc("samsung-i2s-sec", -1);
Wei Yongjun29ca9c72013-10-25 17:06:24 +08001092 if (!i2s->pdev)
Jassi Brar1c7ac012010-11-22 15:36:59 +09001093 return NULL;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001094
Mark Brown2f6f0ff2013-08-01 11:02:47 +01001095 i2s->pdev->dev.parent = &pdev->dev;
1096
Prathyush Kc6f9b1e2013-04-02 16:53:02 +05301097 platform_set_drvdata(i2s->pdev, i2s);
1098 ret = platform_device_add(i2s->pdev);
1099 if (ret < 0)
1100 return NULL;
1101 }
Jassi Brar1c7ac012010-11-22 15:36:59 +09001102
1103 return i2s;
1104}
1105
Padmavathi Venna40476f62013-01-18 17:17:01 +05301106static const struct of_device_id exynos_i2s_match[];
1107
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301108static inline const struct samsung_i2s_dai_data *samsung_i2s_get_driver_data(
1109 struct platform_device *pdev)
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301110{
Sylwester Nawrocki9cf24742015-01-14 19:42:28 +01001111 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
Padmavathi Venna40476f62013-01-18 17:17:01 +05301112 const struct of_device_id *match;
1113 match = of_match_node(exynos_i2s_match, pdev->dev.of_node);
Sylwester Nawrocki9cf24742015-01-14 19:42:28 +01001114 return match ? match->data : NULL;
1115 } else {
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301116 return (struct samsung_i2s_dai_data *)
1117 platform_get_device_id(pdev)->driver_data;
Sylwester Nawrocki9cf24742015-01-14 19:42:28 +01001118 }
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301119}
1120
Rafael J. Wysocki641d3342014-12-13 00:42:18 +01001121#ifdef CONFIG_PM
R. Chandrasekar5b1d3c32013-01-30 17:41:04 +05301122static int i2s_runtime_suspend(struct device *dev)
1123{
1124 struct i2s_dai *i2s = dev_get_drvdata(dev);
1125
1126 clk_disable_unprepare(i2s->clk);
1127
1128 return 0;
1129}
1130
1131static int i2s_runtime_resume(struct device *dev)
1132{
1133 struct i2s_dai *i2s = dev_get_drvdata(dev);
1134
1135 clk_prepare_enable(i2s->clk);
1136
1137 return 0;
1138}
Rafael J. Wysocki641d3342014-12-13 00:42:18 +01001139#endif /* CONFIG_PM */
R. Chandrasekar5b1d3c32013-01-30 17:41:04 +05301140
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +01001141static void i2s_unregister_clocks(struct i2s_dai *i2s)
1142{
1143 int i;
1144
1145 for (i = 0; i < i2s->clk_data.clk_num; i++) {
1146 if (!IS_ERR(i2s->clk_table[i]))
1147 clk_unregister(i2s->clk_table[i]);
1148 }
1149}
1150
1151static void i2s_unregister_clock_provider(struct platform_device *pdev)
1152{
1153 struct i2s_dai *i2s = dev_get_drvdata(&pdev->dev);
1154
1155 of_clk_del_provider(pdev->dev.of_node);
1156 i2s_unregister_clocks(i2s);
1157}
1158
1159static int i2s_register_clock_provider(struct platform_device *pdev)
1160{
1161 struct device *dev = &pdev->dev;
1162 struct i2s_dai *i2s = dev_get_drvdata(dev);
1163 const char *clk_name[2] = { "i2s_opclk0", "i2s_opclk1" };
1164 const char *p_names[2] = { NULL };
1165 const struct samsung_i2s_variant_regs *reg_info = i2s->variant_regs;
1166 struct clk *rclksrc;
1167 int ret, i;
1168
1169 /* Register the clock provider only if it's expected in the DTB */
1170 if (!of_find_property(dev->of_node, "#clock-cells", NULL))
1171 return 0;
1172
1173 /* Get the RCLKSRC mux clock parent clock names */
1174 for (i = 0; i < ARRAY_SIZE(p_names); i++) {
1175 rclksrc = clk_get(dev, clk_name[i]);
1176 if (IS_ERR(rclksrc))
1177 continue;
1178 p_names[i] = __clk_get_name(rclksrc);
1179 clk_put(rclksrc);
1180 }
1181
1182 if (!(i2s->quirks & QUIRK_NO_MUXPSR)) {
1183 /* Activate the prescaler */
1184 u32 val = readl(i2s->addr + I2SPSR);
1185 writel(val | PSR_PSREN, i2s->addr + I2SPSR);
1186
1187 i2s->clk_table[CLK_I2S_RCLK_SRC] = clk_register_mux(NULL,
1188 "i2s_rclksrc", p_names, ARRAY_SIZE(p_names),
1189 CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT,
1190 i2s->addr + I2SMOD, reg_info->rclksrc_off,
1191 1, 0, i2s->lock);
1192
1193 i2s->clk_table[CLK_I2S_RCLK_PSR] = clk_register_divider(NULL,
1194 "i2s_presc", "i2s_rclksrc",
1195 CLK_SET_RATE_PARENT,
1196 i2s->addr + I2SPSR, 8, 6, 0, i2s->lock);
1197
1198 p_names[0] = "i2s_presc";
1199 i2s->clk_data.clk_num = 2;
1200 }
1201 of_property_read_string_index(dev->of_node,
1202 "clock-output-names", 0, &clk_name[0]);
1203
1204 i2s->clk_table[CLK_I2S_CDCLK] = clk_register_gate(NULL, clk_name[0],
1205 p_names[0], CLK_SET_RATE_PARENT,
1206 i2s->addr + I2SMOD, reg_info->cdclkcon_off,
1207 CLK_GATE_SET_TO_DISABLE, i2s->lock);
1208
1209 i2s->clk_data.clk_num += 1;
1210 i2s->clk_data.clks = i2s->clk_table;
1211
1212 ret = of_clk_add_provider(dev->of_node, of_clk_src_onecell_get,
1213 &i2s->clk_data);
1214 if (ret < 0) {
1215 dev_err(dev, "failed to add clock provider: %d\n", ret);
1216 i2s_unregister_clocks(i2s);
1217 }
1218
1219 return ret;
1220}
1221
Bill Pembertonfdca21a2012-12-07 09:26:15 -05001222static int samsung_i2s_probe(struct platform_device *pdev)
Jassi Brar1c7ac012010-11-22 15:36:59 +09001223{
Jassi Brar1c7ac012010-11-22 15:36:59 +09001224 struct i2s_dai *pri_dai, *sec_dai = NULL;
Padmavathi Venna40476f62013-01-18 17:17:01 +05301225 struct s3c_audio_pdata *i2s_pdata = pdev->dev.platform_data;
1226 struct samsung_i2s *i2s_cfg = NULL;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001227 struct resource *res;
Padmavathi Venna40476f62013-01-18 17:17:01 +05301228 u32 regs_base, quirks = 0, idma_addr = 0;
1229 struct device_node *np = pdev->dev.of_node;
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301230 const struct samsung_i2s_dai_data *i2s_dai_data;
Sylwester Nawrockic92f1d02015-01-14 19:42:32 +01001231 int ret;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001232
1233 /* Call during Seconday interface registration */
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301234 i2s_dai_data = samsung_i2s_get_driver_data(pdev);
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301235
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301236 if (i2s_dai_data->dai_type == TYPE_SEC) {
Jassi Brar1c7ac012010-11-22 15:36:59 +09001237 sec_dai = dev_get_drvdata(&pdev->dev);
Prathyush Ka9b977e2013-04-02 16:53:01 +05301238 if (!sec_dai) {
1239 dev_err(&pdev->dev, "Unable to get drvdata\n");
1240 return -EFAULT;
1241 }
Sylwester Nawrocki53f7faa2015-01-14 19:42:29 +01001242 ret = devm_snd_soc_register_component(&sec_dai->pdev->dev,
Mark Brownd644a112013-09-04 20:37:51 +01001243 &samsung_i2s_component,
1244 &sec_dai->i2s_dai_drv, 1);
Sylwester Nawrocki53f7faa2015-01-14 19:42:29 +01001245 if (ret != 0)
1246 return ret;
1247
Arnd Bergmann9bdca822015-11-18 22:31:11 +01001248 return samsung_asoc_dma_platform_register(&pdev->dev,
1249 sec_dai->filter);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001250 }
1251
Padmavathi Venna40476f62013-01-18 17:17:01 +05301252 pri_dai = i2s_alloc_dai(pdev, false);
1253 if (!pri_dai) {
1254 dev_err(&pdev->dev, "Unable to alloc I2S_pri\n");
1255 return -ENOMEM;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001256 }
1257
Sylwester Nawrockif3670532015-01-14 19:42:35 +01001258 spin_lock_init(&pri_dai->spinlock);
1259 pri_dai->lock = &pri_dai->spinlock;
1260
Padmavathi Venna40476f62013-01-18 17:17:01 +05301261 if (!np) {
Padmavathi Venna40476f62013-01-18 17:17:01 +05301262 if (i2s_pdata == NULL) {
1263 dev_err(&pdev->dev, "Can't work without s3c_audio_pdata\n");
1264 return -EINVAL;
1265 }
1266
Arnd Bergmannb9a1a742015-11-18 15:25:23 +01001267 pri_dai->dma_playback.slave = i2s_pdata->dma_playback;
1268 pri_dai->dma_capture.slave = i2s_pdata->dma_capture;
Arnd Bergmann9bdca822015-11-18 22:31:11 +01001269 pri_dai->filter = i2s_pdata->dma_filter;
Arnd Bergmannb9a1a742015-11-18 15:25:23 +01001270
Padmavathi Venna40476f62013-01-18 17:17:01 +05301271 if (&i2s_pdata->type)
1272 i2s_cfg = &i2s_pdata->type.i2s;
1273
1274 if (i2s_cfg) {
1275 quirks = i2s_cfg->quirks;
1276 idma_addr = i2s_cfg->idma_addr;
1277 }
1278 } else {
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301279 quirks = i2s_dai_data->quirks;
Padmavathi Venna40476f62013-01-18 17:17:01 +05301280 if (of_property_read_u32(np, "samsung,idma-addr",
1281 &idma_addr)) {
Padmavathi Vennab0759732014-11-07 12:24:39 +05301282 if (quirks & QUIRK_SUPPORTS_IDMA) {
1283 dev_info(&pdev->dev, "idma address is not"\
Padmavathi Venna40476f62013-01-18 17:17:01 +05301284 "specified");
Padmavathi Venna40476f62013-01-18 17:17:01 +05301285 }
1286 }
1287 }
Jassi Brar1c7ac012010-11-22 15:36:59 +09001288
1289 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sylwester Nawrockiaf1cf5c2015-01-14 19:42:30 +01001290 pri_dai->addr = devm_ioremap_resource(&pdev->dev, res);
1291 if (IS_ERR(pri_dai->addr))
1292 return PTR_ERR(pri_dai->addr);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001293
Jassi Brar1c7ac012010-11-22 15:36:59 +09001294 regs_base = res->start;
1295
Sylwester Nawrocki0ec2ba82015-01-14 19:42:31 +01001296 pri_dai->clk = devm_clk_get(&pdev->dev, "iis");
1297 if (IS_ERR(pri_dai->clk)) {
1298 dev_err(&pdev->dev, "Failed to get iis clock\n");
1299 return PTR_ERR(pri_dai->clk);
1300 }
Sylwester Nawrockic92f1d02015-01-14 19:42:32 +01001301
1302 ret = clk_prepare_enable(pri_dai->clk);
1303 if (ret != 0) {
1304 dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
1305 return ret;
1306 }
Jassi Brar1c7ac012010-11-22 15:36:59 +09001307 pri_dai->dma_playback.dma_addr = regs_base + I2STXD;
1308 pri_dai->dma_capture.dma_addr = regs_base + I2SRXD;
Padmavathi Venna40476f62013-01-18 17:17:01 +05301309 pri_dai->dma_playback.ch_name = "tx";
Padmavathi Venna40476f62013-01-18 17:17:01 +05301310 pri_dai->dma_capture.ch_name = "rx";
Jassi Brar1c7ac012010-11-22 15:36:59 +09001311 pri_dai->dma_playback.dma_size = 4;
1312 pri_dai->dma_capture.dma_size = 4;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001313 pri_dai->quirks = quirks;
Padmavathi Vennaa5a56872014-11-07 12:24:40 +05301314 pri_dai->variant_regs = i2s_dai_data->i2s_variant_regs;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001315
1316 if (quirks & QUIRK_PRI_6CHAN)
1317 pri_dai->i2s_dai_drv.playback.channels_max = 6;
1318
1319 if (quirks & QUIRK_SEC_DAI) {
1320 sec_dai = i2s_alloc_dai(pdev, true);
1321 if (!sec_dai) {
1322 dev_err(&pdev->dev, "Unable to alloc I2S_sec\n");
Sylwester Nawrockiaf1cf5c2015-01-14 19:42:30 +01001323 return -ENOMEM;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001324 }
Sylwester Nawrocki7e5d8702014-12-08 18:45:54 +01001325
Sylwester Nawrockif3670532015-01-14 19:42:35 +01001326 sec_dai->lock = &pri_dai->spinlock;
Sylwester Nawrocki7e5d8702014-12-08 18:45:54 +01001327 sec_dai->variant_regs = pri_dai->variant_regs;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001328 sec_dai->dma_playback.dma_addr = regs_base + I2STXDS;
Padmavathi Venna40476f62013-01-18 17:17:01 +05301329 sec_dai->dma_playback.ch_name = "tx-sec";
1330
Arnd Bergmann9bdca822015-11-18 22:31:11 +01001331 if (!np) {
Arnd Bergmannb9a1a742015-11-18 15:25:23 +01001332 sec_dai->dma_playback.slave = i2s_pdata->dma_play_sec;
Arnd Bergmann9bdca822015-11-18 22:31:11 +01001333 sec_dai->filter = i2s_pdata->dma_filter;
1334 }
Padmavathi Venna40476f62013-01-18 17:17:01 +05301335
Jassi Brar1c7ac012010-11-22 15:36:59 +09001336 sec_dai->dma_playback.dma_size = 4;
Sylwester Nawrockiaf1cf5c2015-01-14 19:42:30 +01001337 sec_dai->addr = pri_dai->addr;
Sylwester Nawrocki0ec2ba82015-01-14 19:42:31 +01001338 sec_dai->clk = pri_dai->clk;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001339 sec_dai->quirks = quirks;
Padmavathi Venna40476f62013-01-18 17:17:01 +05301340 sec_dai->idma_playback.dma_addr = idma_addr;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001341 sec_dai->pri_dai = pri_dai;
1342 pri_dai->sec_dai = sec_dai;
1343 }
1344
Mark Brown0429ffe2013-07-02 13:10:28 +01001345 if (i2s_pdata && i2s_pdata->cfg_gpio && i2s_pdata->cfg_gpio(pdev)) {
1346 dev_err(&pdev->dev, "Unable to configure gpio\n");
Sylwester Nawrockiaf1cf5c2015-01-14 19:42:30 +01001347 return -EINVAL;
Jassi Brar1c7ac012010-11-22 15:36:59 +09001348 }
1349
Mark Brownd644a112013-09-04 20:37:51 +01001350 devm_snd_soc_register_component(&pri_dai->pdev->dev,
1351 &samsung_i2s_component,
1352 &pri_dai->i2s_dai_drv, 1);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001353
Mark Brownc5cf4db2011-12-08 16:45:03 +08001354 pm_runtime_enable(&pdev->dev);
1355
Arnd Bergmann9bdca822015-11-18 22:31:11 +01001356 ret = samsung_asoc_dma_platform_register(&pdev->dev, pri_dai->filter);
Sylwester Nawrocki53f7faa2015-01-14 19:42:29 +01001357 if (ret != 0)
1358 return ret;
Padmavathi Vennaa08485d82012-12-07 13:59:21 +05301359
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +01001360 return i2s_register_clock_provider(pdev);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001361}
1362
Bill Pembertonfdca21a2012-12-07 09:26:15 -05001363static int samsung_i2s_remove(struct platform_device *pdev)
Jassi Brar1c7ac012010-11-22 15:36:59 +09001364{
1365 struct i2s_dai *i2s, *other;
1366
1367 i2s = dev_get_drvdata(&pdev->dev);
Sylwester Nawrockidcd60fc2015-01-14 19:42:33 +01001368 other = get_other_dai(i2s);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001369
1370 if (other) {
1371 other->pri_dai = NULL;
1372 other->sec_dai = NULL;
1373 } else {
Mark Brownc5cf4db2011-12-08 16:45:03 +08001374 pm_runtime_disable(&pdev->dev);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001375 }
1376
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +01001377 if (!is_secondary(i2s)) {
1378 i2s_unregister_clock_provider(pdev);
Sylwester Nawrockic92f1d02015-01-14 19:42:32 +01001379 clk_disable_unprepare(i2s->clk);
Sylwester Nawrocki074b89b2015-01-14 19:42:39 +01001380 }
Sylwester Nawrockic92f1d02015-01-14 19:42:32 +01001381
Jassi Brar1c7ac012010-11-22 15:36:59 +09001382 i2s->pri_dai = NULL;
1383 i2s->sec_dai = NULL;
1384
Jassi Brar1c7ac012010-11-22 15:36:59 +09001385 return 0;
1386}
1387
Padmavathi Vennaa5a56872014-11-07 12:24:40 +05301388static const struct samsung_i2s_variant_regs i2sv3_regs = {
1389 .bfs_off = 1,
1390 .rfs_off = 3,
1391 .sdf_off = 5,
1392 .txr_off = 8,
1393 .rclksrc_off = 10,
1394 .mss_off = 11,
1395 .cdclkcon_off = 12,
1396 .lrp_off = 7,
1397 .bfs_mask = 0x3,
1398 .rfs_mask = 0x3,
1399 .ftx0cnt_off = 8,
1400};
1401
1402static const struct samsung_i2s_variant_regs i2sv6_regs = {
1403 .bfs_off = 0,
1404 .rfs_off = 4,
1405 .sdf_off = 6,
1406 .txr_off = 8,
1407 .rclksrc_off = 10,
1408 .mss_off = 11,
1409 .cdclkcon_off = 12,
1410 .lrp_off = 15,
1411 .bfs_mask = 0xf,
1412 .rfs_mask = 0x3,
1413 .ftx0cnt_off = 8,
1414};
1415
1416static const struct samsung_i2s_variant_regs i2sv7_regs = {
1417 .bfs_off = 0,
1418 .rfs_off = 4,
1419 .sdf_off = 7,
1420 .txr_off = 9,
1421 .rclksrc_off = 11,
1422 .mss_off = 12,
1423 .cdclkcon_off = 22,
1424 .lrp_off = 15,
1425 .bfs_mask = 0xf,
1426 .rfs_mask = 0x7,
1427 .ftx0cnt_off = 0,
1428};
1429
1430static const struct samsung_i2s_variant_regs i2sv5_i2s1_regs = {
1431 .bfs_off = 0,
1432 .rfs_off = 3,
1433 .sdf_off = 6,
1434 .txr_off = 8,
1435 .rclksrc_off = 10,
1436 .mss_off = 11,
1437 .cdclkcon_off = 12,
1438 .lrp_off = 15,
1439 .bfs_mask = 0x7,
1440 .rfs_mask = 0x7,
1441 .ftx0cnt_off = 8,
1442};
1443
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301444static const struct samsung_i2s_dai_data i2sv3_dai_type = {
1445 .dai_type = TYPE_PRI,
1446 .quirks = QUIRK_NO_MUXPSR,
Padmavathi Vennaa5a56872014-11-07 12:24:40 +05301447 .i2s_variant_regs = &i2sv3_regs,
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301448};
1449
1450static const struct samsung_i2s_dai_data i2sv5_dai_type = {
1451 .dai_type = TYPE_PRI,
Padmavathi Vennab0759732014-11-07 12:24:39 +05301452 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1453 QUIRK_SUPPORTS_IDMA,
Padmavathi Vennaa5a56872014-11-07 12:24:40 +05301454 .i2s_variant_regs = &i2sv3_regs,
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301455};
1456
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +05301457static const struct samsung_i2s_dai_data i2sv6_dai_type = {
1458 .dai_type = TYPE_PRI,
1459 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
Padmavathi Vennab0759732014-11-07 12:24:39 +05301460 QUIRK_SUPPORTS_TDM | QUIRK_SUPPORTS_IDMA,
Padmavathi Vennaa5a56872014-11-07 12:24:40 +05301461 .i2s_variant_regs = &i2sv6_regs,
1462};
1463
1464static const struct samsung_i2s_dai_data i2sv7_dai_type = {
1465 .dai_type = TYPE_PRI,
1466 .quirks = QUIRK_PRI_6CHAN | QUIRK_SEC_DAI | QUIRK_NEED_RSTCLR |
1467 QUIRK_SUPPORTS_TDM,
1468 .i2s_variant_regs = &i2sv7_regs,
1469};
1470
1471static const struct samsung_i2s_dai_data i2sv5_dai_type_i2s1 = {
1472 .dai_type = TYPE_PRI,
1473 .quirks = QUIRK_PRI_6CHAN | QUIRK_NEED_RSTCLR,
1474 .i2s_variant_regs = &i2sv5_i2s1_regs,
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +05301475};
1476
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301477static const struct samsung_i2s_dai_data samsung_dai_type_pri = {
1478 .dai_type = TYPE_PRI,
1479};
1480
1481static const struct samsung_i2s_dai_data samsung_dai_type_sec = {
1482 .dai_type = TYPE_SEC,
1483};
1484
Krzysztof Kozlowskieb8ca0f2015-05-02 01:00:14 +09001485static const struct platform_device_id samsung_i2s_driver_ids[] = {
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301486 {
1487 .name = "samsung-i2s",
Mark Brown3f024982014-12-05 19:56:17 +00001488 .driver_data = (kernel_ulong_t)&i2sv3_dai_type,
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301489 }, {
1490 .name = "samsung-i2s-sec",
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301491 .driver_data = (kernel_ulong_t)&samsung_dai_type_sec,
Mark Brown3f024982014-12-05 19:56:17 +00001492 }, {
1493 .name = "samsung-i2sv4",
1494 .driver_data = (kernel_ulong_t)&i2sv5_dai_type,
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301495 },
1496 {},
1497};
Arnd Bergmann2af19552013-04-11 02:05:03 +02001498MODULE_DEVICE_TABLE(platform, samsung_i2s_driver_ids);
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301499
Padmavathi Venna40476f62013-01-18 17:17:01 +05301500#ifdef CONFIG_OF
Padmavathi Venna40476f62013-01-18 17:17:01 +05301501static const struct of_device_id exynos_i2s_match[] = {
Padmavathi Venna7da493e2013-08-12 15:19:51 +05301502 {
1503 .compatible = "samsung,s3c6410-i2s",
1504 .data = &i2sv3_dai_type,
1505 }, {
1506 .compatible = "samsung,s5pv210-i2s",
1507 .data = &i2sv5_dai_type,
Padmavathi Venna4ca0c0d2013-08-12 15:19:52 +05301508 }, {
1509 .compatible = "samsung,exynos5420-i2s",
1510 .data = &i2sv6_dai_type,
Padmavathi Vennaa5a56872014-11-07 12:24:40 +05301511 }, {
1512 .compatible = "samsung,exynos7-i2s",
1513 .data = &i2sv7_dai_type,
1514 }, {
1515 .compatible = "samsung,exynos7-i2s1",
1516 .data = &i2sv5_dai_type_i2s1,
Padmavathi Venna40476f62013-01-18 17:17:01 +05301517 },
1518 {},
1519};
1520MODULE_DEVICE_TABLE(of, exynos_i2s_match);
1521#endif
1522
R. Chandrasekar5b1d3c32013-01-30 17:41:04 +05301523static const struct dev_pm_ops samsung_i2s_pm = {
1524 SET_RUNTIME_PM_OPS(i2s_runtime_suspend,
1525 i2s_runtime_resume, NULL)
1526};
1527
Jassi Brar1c7ac012010-11-22 15:36:59 +09001528static struct platform_driver samsung_i2s_driver = {
1529 .probe = samsung_i2s_probe,
Bill Pembertonfdca21a2012-12-07 09:26:15 -05001530 .remove = samsung_i2s_remove,
Padmavathi Venna7c62eeb2013-01-18 17:17:00 +05301531 .id_table = samsung_i2s_driver_ids,
Jassi Brar1c7ac012010-11-22 15:36:59 +09001532 .driver = {
1533 .name = "samsung-i2s",
Padmavathi Venna40476f62013-01-18 17:17:01 +05301534 .of_match_table = of_match_ptr(exynos_i2s_match),
R. Chandrasekar5b1d3c32013-01-30 17:41:04 +05301535 .pm = &samsung_i2s_pm,
Jassi Brar1c7ac012010-11-22 15:36:59 +09001536 },
1537};
1538
Mark Browne00c3f52011-11-23 15:20:13 +00001539module_platform_driver(samsung_i2s_driver);
Jassi Brar1c7ac012010-11-22 15:36:59 +09001540
1541/* Module information */
Jaswinder Singhdf8ad332012-02-25 16:24:36 +05301542MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
Jassi Brar1c7ac012010-11-22 15:36:59 +09001543MODULE_DESCRIPTION("Samsung I2S Interface");
1544MODULE_ALIAS("platform:samsung-i2s");
1545MODULE_LICENSE("GPL");