• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * IMG I2S input controller driver
3  *
4  * Copyright (C) 2015 Imagination Technologies Ltd.
5  *
6  * Author: Damien Horsley <Damien.Horsley@imgtec.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/reset.h>
21 
22 #include <sound/core.h>
23 #include <sound/dmaengine_pcm.h>
24 #include <sound/initval.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 
29 #define IMG_I2S_IN_RX_FIFO			0x0
30 
31 #define IMG_I2S_IN_CTL				0x4
32 #define IMG_I2S_IN_CTL_ACTIVE_CHAN_MASK		0xfffffffc
33 #define IMG_I2S_IN_CTL_ACTIVE_CH_SHIFT		2
34 #define IMG_I2S_IN_CTL_16PACK_MASK		BIT(1)
35 #define IMG_I2S_IN_CTL_ME_MASK			BIT(0)
36 
37 #define IMG_I2S_IN_CH_CTL			0x4
38 #define IMG_I2S_IN_CH_CTL_CCDEL_MASK		0x38000
39 #define IMG_I2S_IN_CH_CTL_CCDEL_SHIFT		15
40 #define IMG_I2S_IN_CH_CTL_FEN_MASK		BIT(14)
41 #define IMG_I2S_IN_CH_CTL_FMODE_MASK		BIT(13)
42 #define IMG_I2S_IN_CH_CTL_16PACK_MASK		BIT(12)
43 #define IMG_I2S_IN_CH_CTL_JUST_MASK		BIT(10)
44 #define IMG_I2S_IN_CH_CTL_PACKH_MASK		BIT(9)
45 #define IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK	BIT(8)
46 #define IMG_I2S_IN_CH_CTL_BLKP_MASK		BIT(7)
47 #define IMG_I2S_IN_CH_CTL_FIFO_FLUSH_MASK	BIT(6)
48 #define IMG_I2S_IN_CH_CTL_LRD_MASK		BIT(3)
49 #define IMG_I2S_IN_CH_CTL_FW_MASK		BIT(2)
50 #define IMG_I2S_IN_CH_CTL_SW_MASK		BIT(1)
51 #define IMG_I2S_IN_CH_CTL_ME_MASK		BIT(0)
52 
53 #define IMG_I2S_IN_CH_STRIDE			0x20
54 
55 struct img_i2s_in {
56 	void __iomem *base;
57 	struct clk *clk_sys;
58 	struct snd_dmaengine_dai_dma_data dma_data;
59 	struct device *dev;
60 	unsigned int max_i2s_chan;
61 	void __iomem *channel_base;
62 	unsigned int active_channels;
63 	struct snd_soc_dai_driver dai_driver;
64 	u32 suspend_ctl;
65 	u32 *suspend_ch_ctl;
66 };
67 
img_i2s_in_runtime_suspend(struct device * dev)68 static int img_i2s_in_runtime_suspend(struct device *dev)
69 {
70 	struct img_i2s_in *i2s = dev_get_drvdata(dev);
71 
72 	clk_disable_unprepare(i2s->clk_sys);
73 
74 	return 0;
75 }
76 
img_i2s_in_runtime_resume(struct device * dev)77 static int img_i2s_in_runtime_resume(struct device *dev)
78 {
79 	struct img_i2s_in *i2s = dev_get_drvdata(dev);
80 	int ret;
81 
82 	ret = clk_prepare_enable(i2s->clk_sys);
83 	if (ret) {
84 		dev_err(dev, "Unable to enable sys clock\n");
85 		return ret;
86 	}
87 
88 	return 0;
89 }
90 
img_i2s_in_writel(struct img_i2s_in * i2s,u32 val,u32 reg)91 static inline void img_i2s_in_writel(struct img_i2s_in *i2s, u32 val, u32 reg)
92 {
93 	writel(val, i2s->base + reg);
94 }
95 
img_i2s_in_readl(struct img_i2s_in * i2s,u32 reg)96 static inline u32 img_i2s_in_readl(struct img_i2s_in *i2s, u32 reg)
97 {
98 	return readl(i2s->base + reg);
99 }
100 
img_i2s_in_ch_writel(struct img_i2s_in * i2s,u32 chan,u32 val,u32 reg)101 static inline void img_i2s_in_ch_writel(struct img_i2s_in *i2s, u32 chan,
102 					u32 val, u32 reg)
103 {
104 	writel(val, i2s->channel_base + (chan * IMG_I2S_IN_CH_STRIDE) + reg);
105 }
106 
img_i2s_in_ch_readl(struct img_i2s_in * i2s,u32 chan,u32 reg)107 static inline u32 img_i2s_in_ch_readl(struct img_i2s_in *i2s, u32 chan,
108 					u32 reg)
109 {
110 	return readl(i2s->channel_base + (chan * IMG_I2S_IN_CH_STRIDE) + reg);
111 }
112 
img_i2s_in_ch_disable(struct img_i2s_in * i2s,u32 chan)113 static inline void img_i2s_in_ch_disable(struct img_i2s_in *i2s, u32 chan)
114 {
115 	u32 reg;
116 
117 	reg = img_i2s_in_ch_readl(i2s, chan, IMG_I2S_IN_CH_CTL);
118 	reg &= ~IMG_I2S_IN_CH_CTL_ME_MASK;
119 	img_i2s_in_ch_writel(i2s, chan, reg, IMG_I2S_IN_CH_CTL);
120 }
121 
img_i2s_in_ch_enable(struct img_i2s_in * i2s,u32 chan)122 static inline void img_i2s_in_ch_enable(struct img_i2s_in *i2s, u32 chan)
123 {
124 	u32 reg;
125 
126 	reg = img_i2s_in_ch_readl(i2s, chan, IMG_I2S_IN_CH_CTL);
127 	reg |= IMG_I2S_IN_CH_CTL_ME_MASK;
128 	img_i2s_in_ch_writel(i2s, chan, reg, IMG_I2S_IN_CH_CTL);
129 }
130 
img_i2s_in_disable(struct img_i2s_in * i2s)131 static inline void img_i2s_in_disable(struct img_i2s_in *i2s)
132 {
133 	u32 reg;
134 
135 	reg = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
136 	reg &= ~IMG_I2S_IN_CTL_ME_MASK;
137 	img_i2s_in_writel(i2s, reg, IMG_I2S_IN_CTL);
138 }
139 
img_i2s_in_enable(struct img_i2s_in * i2s)140 static inline void img_i2s_in_enable(struct img_i2s_in *i2s)
141 {
142 	u32 reg;
143 
144 	reg = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
145 	reg |= IMG_I2S_IN_CTL_ME_MASK;
146 	img_i2s_in_writel(i2s, reg, IMG_I2S_IN_CTL);
147 }
148 
img_i2s_in_flush(struct img_i2s_in * i2s)149 static inline void img_i2s_in_flush(struct img_i2s_in *i2s)
150 {
151 	int i;
152 	u32 reg;
153 
154 	for (i = 0; i < i2s->active_channels; i++) {
155 		reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
156 		reg |= IMG_I2S_IN_CH_CTL_FIFO_FLUSH_MASK;
157 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
158 		reg &= ~IMG_I2S_IN_CH_CTL_FIFO_FLUSH_MASK;
159 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
160 	}
161 }
162 
img_i2s_in_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)163 static int img_i2s_in_trigger(struct snd_pcm_substream *substream, int cmd,
164 	struct snd_soc_dai *dai)
165 {
166 	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
167 
168 	switch (cmd) {
169 	case SNDRV_PCM_TRIGGER_START:
170 	case SNDRV_PCM_TRIGGER_RESUME:
171 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
172 		img_i2s_in_enable(i2s);
173 		break;
174 
175 	case SNDRV_PCM_TRIGGER_STOP:
176 	case SNDRV_PCM_TRIGGER_SUSPEND:
177 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
178 		img_i2s_in_disable(i2s);
179 		break;
180 	default:
181 		return -EINVAL;
182 	}
183 
184 	return 0;
185 }
186 
img_i2s_in_check_rate(struct img_i2s_in * i2s,unsigned int sample_rate,unsigned int frame_size,unsigned int * bclk_filter_enable,unsigned int * bclk_filter_value)187 static int img_i2s_in_check_rate(struct img_i2s_in *i2s,
188 		unsigned int sample_rate, unsigned int frame_size,
189 		unsigned int *bclk_filter_enable,
190 		unsigned int *bclk_filter_value)
191 {
192 	unsigned int bclk_freq, cur_freq;
193 
194 	bclk_freq = sample_rate * frame_size;
195 
196 	cur_freq = clk_get_rate(i2s->clk_sys);
197 
198 	if (cur_freq >= bclk_freq * 8) {
199 		*bclk_filter_enable = 1;
200 		*bclk_filter_value = 0;
201 	} else if (cur_freq >= bclk_freq * 7) {
202 		*bclk_filter_enable = 1;
203 		*bclk_filter_value = 1;
204 	} else if (cur_freq >= bclk_freq * 6) {
205 		*bclk_filter_enable = 0;
206 		*bclk_filter_value = 0;
207 	} else {
208 		dev_err(i2s->dev,
209 			"Sys clock rate %u insufficient for sample rate %u\n",
210 			cur_freq, sample_rate);
211 		return -EINVAL;
212 	}
213 
214 	return 0;
215 }
216 
img_i2s_in_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)217 static int img_i2s_in_hw_params(struct snd_pcm_substream *substream,
218 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
219 {
220 	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
221 	unsigned int rate, channels, i2s_channels, frame_size;
222 	unsigned int bclk_filter_enable, bclk_filter_value;
223 	int i, ret = 0;
224 	u32 reg, control_mask, chan_control_mask;
225 	u32 control_set = 0, chan_control_set = 0;
226 	snd_pcm_format_t format;
227 
228 	rate = params_rate(params);
229 	format = params_format(params);
230 	channels = params_channels(params);
231 	i2s_channels = channels / 2;
232 
233 	switch (format) {
234 	case SNDRV_PCM_FORMAT_S32_LE:
235 		frame_size = 64;
236 		chan_control_set |= IMG_I2S_IN_CH_CTL_SW_MASK;
237 		chan_control_set |= IMG_I2S_IN_CH_CTL_FW_MASK;
238 		chan_control_set |= IMG_I2S_IN_CH_CTL_PACKH_MASK;
239 		break;
240 	case SNDRV_PCM_FORMAT_S24_LE:
241 		frame_size = 64;
242 		chan_control_set |= IMG_I2S_IN_CH_CTL_SW_MASK;
243 		chan_control_set |= IMG_I2S_IN_CH_CTL_FW_MASK;
244 		break;
245 	case SNDRV_PCM_FORMAT_S16_LE:
246 		frame_size = 32;
247 		control_set |= IMG_I2S_IN_CTL_16PACK_MASK;
248 		chan_control_set |= IMG_I2S_IN_CH_CTL_16PACK_MASK;
249 		break;
250 	default:
251 		return -EINVAL;
252 	}
253 
254 	if ((channels < 2) ||
255 	    (channels > (i2s->max_i2s_chan * 2)) ||
256 	    (channels % 2))
257 		return -EINVAL;
258 
259 	control_set |= ((i2s_channels - 1) << IMG_I2S_IN_CTL_ACTIVE_CH_SHIFT);
260 
261 	ret = img_i2s_in_check_rate(i2s, rate, frame_size,
262 			&bclk_filter_enable, &bclk_filter_value);
263 	if (ret < 0)
264 		return ret;
265 
266 	if (bclk_filter_enable)
267 		chan_control_set |= IMG_I2S_IN_CH_CTL_FEN_MASK;
268 
269 	if (bclk_filter_value)
270 		chan_control_set |= IMG_I2S_IN_CH_CTL_FMODE_MASK;
271 
272 	control_mask = IMG_I2S_IN_CTL_16PACK_MASK |
273 		       IMG_I2S_IN_CTL_ACTIVE_CHAN_MASK;
274 
275 	chan_control_mask = IMG_I2S_IN_CH_CTL_16PACK_MASK |
276 			    IMG_I2S_IN_CH_CTL_FEN_MASK |
277 			    IMG_I2S_IN_CH_CTL_FMODE_MASK |
278 			    IMG_I2S_IN_CH_CTL_SW_MASK |
279 			    IMG_I2S_IN_CH_CTL_FW_MASK |
280 			    IMG_I2S_IN_CH_CTL_PACKH_MASK;
281 
282 	reg = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
283 	reg = (reg & ~control_mask) | control_set;
284 	img_i2s_in_writel(i2s, reg, IMG_I2S_IN_CTL);
285 
286 	for (i = 0; i < i2s->active_channels; i++)
287 		img_i2s_in_ch_disable(i2s, i);
288 
289 	for (i = 0; i < i2s->max_i2s_chan; i++) {
290 		reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
291 		reg = (reg & ~chan_control_mask) | chan_control_set;
292 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
293 	}
294 
295 	i2s->active_channels = i2s_channels;
296 
297 	img_i2s_in_flush(i2s);
298 
299 	for (i = 0; i < i2s->active_channels; i++)
300 		img_i2s_in_ch_enable(i2s, i);
301 
302 	return 0;
303 }
304 
img_i2s_in_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)305 static int img_i2s_in_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
306 {
307 	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
308 	int i, ret;
309 	u32 chan_control_mask, lrd_set = 0, blkp_set = 0, chan_control_set = 0;
310 	u32 reg;
311 
312 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
313 	case SND_SOC_DAIFMT_NB_NF:
314 		lrd_set |= IMG_I2S_IN_CH_CTL_LRD_MASK;
315 		break;
316 	case SND_SOC_DAIFMT_NB_IF:
317 		break;
318 	case SND_SOC_DAIFMT_IB_NF:
319 		lrd_set |= IMG_I2S_IN_CH_CTL_LRD_MASK;
320 		blkp_set |= IMG_I2S_IN_CH_CTL_BLKP_MASK;
321 		break;
322 	case SND_SOC_DAIFMT_IB_IF:
323 		blkp_set |= IMG_I2S_IN_CH_CTL_BLKP_MASK;
324 		break;
325 	default:
326 		return -EINVAL;
327 	}
328 
329 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
330 	case SND_SOC_DAIFMT_I2S:
331 		chan_control_set |= IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK;
332 		break;
333 	case SND_SOC_DAIFMT_LEFT_J:
334 		break;
335 	default:
336 		return -EINVAL;
337 	}
338 
339 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
340 	case SND_SOC_DAIFMT_CBM_CFM:
341 		break;
342 	default:
343 		return -EINVAL;
344 	}
345 
346 	chan_control_mask = IMG_I2S_IN_CH_CTL_CLK_TRANS_MASK;
347 
348 	ret = pm_runtime_get_sync(i2s->dev);
349 	if (ret < 0) {
350 		pm_runtime_put_noidle(i2s->dev);
351 		return ret;
352 	}
353 
354 	for (i = 0; i < i2s->active_channels; i++)
355 		img_i2s_in_ch_disable(i2s, i);
356 
357 	/*
358 	 * BLKP and LRD must be set during separate register writes
359 	 */
360 	for (i = 0; i < i2s->max_i2s_chan; i++) {
361 		reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
362 		reg = (reg & ~chan_control_mask) | chan_control_set;
363 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
364 		reg = (reg & ~IMG_I2S_IN_CH_CTL_BLKP_MASK) | blkp_set;
365 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
366 		reg = (reg & ~IMG_I2S_IN_CH_CTL_LRD_MASK) | lrd_set;
367 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
368 	}
369 
370 	for (i = 0; i < i2s->active_channels; i++)
371 		img_i2s_in_ch_enable(i2s, i);
372 
373 	pm_runtime_put(i2s->dev);
374 
375 	return 0;
376 }
377 
378 static const struct snd_soc_dai_ops img_i2s_in_dai_ops = {
379 	.trigger = img_i2s_in_trigger,
380 	.hw_params = img_i2s_in_hw_params,
381 	.set_fmt = img_i2s_in_set_fmt
382 };
383 
img_i2s_in_dai_probe(struct snd_soc_dai * dai)384 static int img_i2s_in_dai_probe(struct snd_soc_dai *dai)
385 {
386 	struct img_i2s_in *i2s = snd_soc_dai_get_drvdata(dai);
387 
388 	snd_soc_dai_init_dma_data(dai, NULL, &i2s->dma_data);
389 
390 	return 0;
391 }
392 
393 static const struct snd_soc_component_driver img_i2s_in_component = {
394 	.name = "img-i2s-in"
395 };
396 
img_i2s_in_dma_prepare_slave_config(struct snd_pcm_substream * st,struct snd_pcm_hw_params * params,struct dma_slave_config * sc)397 static int img_i2s_in_dma_prepare_slave_config(struct snd_pcm_substream *st,
398 	struct snd_pcm_hw_params *params, struct dma_slave_config *sc)
399 {
400 	unsigned int i2s_channels = params_channels(params) / 2;
401 	struct snd_soc_pcm_runtime *rtd = st->private_data;
402 	struct snd_dmaengine_dai_dma_data *dma_data;
403 	int ret;
404 
405 	dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, st);
406 
407 	ret = snd_hwparams_to_dma_slave_config(st, params, sc);
408 	if (ret)
409 		return ret;
410 
411 	sc->src_addr = dma_data->addr;
412 	sc->src_addr_width = dma_data->addr_width;
413 	sc->src_maxburst = 4 * i2s_channels;
414 
415 	return 0;
416 }
417 
418 static const struct snd_dmaengine_pcm_config img_i2s_in_dma_config = {
419 	.prepare_slave_config = img_i2s_in_dma_prepare_slave_config
420 };
421 
img_i2s_in_probe(struct platform_device * pdev)422 static int img_i2s_in_probe(struct platform_device *pdev)
423 {
424 	struct img_i2s_in *i2s;
425 	struct resource *res;
426 	void __iomem *base;
427 	int ret, i;
428 	struct reset_control *rst;
429 	unsigned int max_i2s_chan_pow_2;
430 	struct device *dev = &pdev->dev;
431 
432 	i2s = devm_kzalloc(dev, sizeof(*i2s), GFP_KERNEL);
433 	if (!i2s)
434 		return -ENOMEM;
435 
436 	platform_set_drvdata(pdev, i2s);
437 
438 	i2s->dev = dev;
439 
440 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
441 	base = devm_ioremap_resource(dev, res);
442 	if (IS_ERR(base))
443 		return PTR_ERR(base);
444 
445 	i2s->base = base;
446 
447 	if (of_property_read_u32(pdev->dev.of_node, "img,i2s-channels",
448 			&i2s->max_i2s_chan)) {
449 		dev_err(dev, "No img,i2s-channels property\n");
450 		return -EINVAL;
451 	}
452 
453 	max_i2s_chan_pow_2 = 1 << get_count_order(i2s->max_i2s_chan);
454 
455 	i2s->channel_base = base + (max_i2s_chan_pow_2 * 0x20);
456 
457 	i2s->clk_sys = devm_clk_get(dev, "sys");
458 	if (IS_ERR(i2s->clk_sys)) {
459 		if (PTR_ERR(i2s->clk_sys) != -EPROBE_DEFER)
460 			dev_err(dev, "Failed to acquire clock 'sys'\n");
461 		return PTR_ERR(i2s->clk_sys);
462 	}
463 
464 	pm_runtime_enable(&pdev->dev);
465 	if (!pm_runtime_enabled(&pdev->dev)) {
466 		ret = img_i2s_in_runtime_resume(&pdev->dev);
467 		if (ret)
468 			goto err_pm_disable;
469 	}
470 	ret = pm_runtime_get_sync(&pdev->dev);
471 	if (ret < 0)
472 		goto err_suspend;
473 
474 	i2s->active_channels = 1;
475 	i2s->dma_data.addr = res->start + IMG_I2S_IN_RX_FIFO;
476 	i2s->dma_data.addr_width = 4;
477 
478 	i2s->dai_driver.probe = img_i2s_in_dai_probe;
479 	i2s->dai_driver.capture.channels_min = 2;
480 	i2s->dai_driver.capture.channels_max = i2s->max_i2s_chan * 2;
481 	i2s->dai_driver.capture.rates = SNDRV_PCM_RATE_8000_192000;
482 	i2s->dai_driver.capture.formats = SNDRV_PCM_FMTBIT_S32_LE |
483 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE;
484 	i2s->dai_driver.ops = &img_i2s_in_dai_ops;
485 
486 	rst = devm_reset_control_get_exclusive(dev, "rst");
487 	if (IS_ERR(rst)) {
488 		if (PTR_ERR(rst) == -EPROBE_DEFER) {
489 			ret = -EPROBE_DEFER;
490 			pm_runtime_put(&pdev->dev);
491 			goto err_suspend;
492 		}
493 
494 		dev_dbg(dev, "No top level reset found\n");
495 
496 		img_i2s_in_disable(i2s);
497 
498 		for (i = 0; i < i2s->max_i2s_chan; i++)
499 			img_i2s_in_ch_disable(i2s, i);
500 	} else {
501 		reset_control_assert(rst);
502 		reset_control_deassert(rst);
503 	}
504 
505 	img_i2s_in_writel(i2s, 0, IMG_I2S_IN_CTL);
506 
507 	for (i = 0; i < i2s->max_i2s_chan; i++)
508 		img_i2s_in_ch_writel(i2s, i,
509 			(4 << IMG_I2S_IN_CH_CTL_CCDEL_SHIFT) |
510 			IMG_I2S_IN_CH_CTL_JUST_MASK |
511 			IMG_I2S_IN_CH_CTL_FW_MASK, IMG_I2S_IN_CH_CTL);
512 
513 	pm_runtime_put(&pdev->dev);
514 
515 	i2s->suspend_ch_ctl = devm_kcalloc(dev,
516 		i2s->max_i2s_chan, sizeof(*i2s->suspend_ch_ctl), GFP_KERNEL);
517 	if (!i2s->suspend_ch_ctl) {
518 		ret = -ENOMEM;
519 		goto err_suspend;
520 	}
521 
522 	ret = devm_snd_soc_register_component(dev, &img_i2s_in_component,
523 						&i2s->dai_driver, 1);
524 	if (ret)
525 		goto err_suspend;
526 
527 	ret = devm_snd_dmaengine_pcm_register(dev, &img_i2s_in_dma_config, 0);
528 	if (ret)
529 		goto err_suspend;
530 
531 	return 0;
532 
533 err_suspend:
534 	if (!pm_runtime_enabled(&pdev->dev))
535 		img_i2s_in_runtime_suspend(&pdev->dev);
536 err_pm_disable:
537 	pm_runtime_disable(&pdev->dev);
538 
539 	return ret;
540 }
541 
img_i2s_in_dev_remove(struct platform_device * pdev)542 static int img_i2s_in_dev_remove(struct platform_device *pdev)
543 {
544 	pm_runtime_disable(&pdev->dev);
545 	if (!pm_runtime_status_suspended(&pdev->dev))
546 		img_i2s_in_runtime_suspend(&pdev->dev);
547 
548 	return 0;
549 }
550 
551 #ifdef CONFIG_PM_SLEEP
img_i2s_in_suspend(struct device * dev)552 static int img_i2s_in_suspend(struct device *dev)
553 {
554 	struct img_i2s_in *i2s = dev_get_drvdata(dev);
555 	int i, ret;
556 	u32 reg;
557 
558 	if (pm_runtime_status_suspended(dev)) {
559 		ret = img_i2s_in_runtime_resume(dev);
560 		if (ret)
561 			return ret;
562 	}
563 
564 	for (i = 0; i < i2s->max_i2s_chan; i++) {
565 		reg = img_i2s_in_ch_readl(i2s, i, IMG_I2S_IN_CH_CTL);
566 		i2s->suspend_ch_ctl[i] = reg;
567 	}
568 
569 	i2s->suspend_ctl = img_i2s_in_readl(i2s, IMG_I2S_IN_CTL);
570 
571 	img_i2s_in_runtime_suspend(dev);
572 
573 	return 0;
574 }
575 
img_i2s_in_resume(struct device * dev)576 static int img_i2s_in_resume(struct device *dev)
577 {
578 	struct img_i2s_in *i2s = dev_get_drvdata(dev);
579 	int i, ret;
580 	u32 reg;
581 
582 	ret = img_i2s_in_runtime_resume(dev);
583 	if (ret)
584 		return ret;
585 
586 	for (i = 0; i < i2s->max_i2s_chan; i++) {
587 		reg = i2s->suspend_ch_ctl[i];
588 		img_i2s_in_ch_writel(i2s, i, reg, IMG_I2S_IN_CH_CTL);
589 	}
590 
591 	img_i2s_in_writel(i2s, i2s->suspend_ctl, IMG_I2S_IN_CTL);
592 
593 	if (pm_runtime_status_suspended(dev))
594 		img_i2s_in_runtime_suspend(dev);
595 
596 	return 0;
597 }
598 #endif
599 
600 static const struct of_device_id img_i2s_in_of_match[] = {
601 	{ .compatible = "img,i2s-in" },
602 	{}
603 };
604 MODULE_DEVICE_TABLE(of, img_i2s_in_of_match);
605 
606 static const struct dev_pm_ops img_i2s_in_pm_ops = {
607 	SET_RUNTIME_PM_OPS(img_i2s_in_runtime_suspend,
608 			   img_i2s_in_runtime_resume, NULL)
609 	SET_SYSTEM_SLEEP_PM_OPS(img_i2s_in_suspend, img_i2s_in_resume)
610 };
611 
612 static struct platform_driver img_i2s_in_driver = {
613 	.driver = {
614 		.name = "img-i2s-in",
615 		.of_match_table = img_i2s_in_of_match,
616 		.pm = &img_i2s_in_pm_ops
617 	},
618 	.probe = img_i2s_in_probe,
619 	.remove = img_i2s_in_dev_remove
620 };
621 module_platform_driver(img_i2s_in_driver);
622 
623 MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
624 MODULE_DESCRIPTION("IMG I2S Input Driver");
625 MODULE_LICENSE("GPL v2");
626