blob: d6cd5537126c69bc76b5c019b17d0b01bb4a7eab [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -07002/*
3 * Atmel SSC driver
4 *
5 * Copyright (C) 2007 Atmel Corporation
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -07006 */
7
8#include <linux/platform_device.h>
9#include <linux/list.h>
10#include <linux/clk.h>
11#include <linux/err.h>
12#include <linux/io.h>
Michał Mirosławb037d602020-06-24 13:35:41 +020013#include <linux/mutex.h>
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070014#include <linux/atmel-ssc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmakereb12a672011-07-03 15:14:56 -040016#include <linux/module.h>
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070017
Bo Shen099343c2012-11-07 11:41:41 +080018#include <linux/of.h>
19
Peter Rosine8314d72016-12-06 20:22:36 +010020#include "../../sound/soc/atmel/atmel_ssc_dai.h"
21
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070022/* Serialize access to ssc_list and user count */
Michał Mirosławb037d602020-06-24 13:35:41 +020023static DEFINE_MUTEX(user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070024static LIST_HEAD(ssc_list);
25
26struct ssc_device *ssc_request(unsigned int ssc_num)
27{
28 int ssc_valid = 0;
29 struct ssc_device *ssc;
30
Michał Mirosławb037d602020-06-24 13:35:41 +020031 mutex_lock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070032 list_for_each_entry(ssc, &ssc_list, list) {
Bo Shen099343c2012-11-07 11:41:41 +080033 if (ssc->pdev->dev.of_node) {
34 if (of_alias_get_id(ssc->pdev->dev.of_node, "ssc")
35 == ssc_num) {
Songjun Wuc706f2e2016-03-02 12:15:25 +010036 ssc->pdev->id = ssc_num;
Bo Shen099343c2012-11-07 11:41:41 +080037 ssc_valid = 1;
38 break;
39 }
40 } else if (ssc->pdev->id == ssc_num) {
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070041 ssc_valid = 1;
42 break;
43 }
44 }
45
46 if (!ssc_valid) {
Michał Mirosławb037d602020-06-24 13:35:41 +020047 mutex_unlock(&user_lock);
Hans-Christian Egtvedtdfecb712009-02-04 15:12:17 -080048 pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070049 return ERR_PTR(-ENODEV);
50 }
51
52 if (ssc->user) {
Michał Mirosławb037d602020-06-24 13:35:41 +020053 mutex_unlock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070054 dev_dbg(&ssc->pdev->dev, "module busy\n");
55 return ERR_PTR(-EBUSY);
56 }
57 ssc->user++;
Michał Mirosławb037d602020-06-24 13:35:41 +020058 mutex_unlock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070059
Bo Shen49af54f2014-09-24 17:33:55 +080060 clk_prepare(ssc->clk);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070061
62 return ssc;
63}
64EXPORT_SYMBOL(ssc_request);
65
66void ssc_free(struct ssc_device *ssc)
67{
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020068 bool disable_clk = true;
69
Michał Mirosławb037d602020-06-24 13:35:41 +020070 mutex_lock(&user_lock);
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020071 if (ssc->user)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070072 ssc->user--;
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020073 else {
74 disable_clk = false;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070075 dev_dbg(&ssc->pdev->dev, "device already free\n");
76 }
Michał Mirosławb037d602020-06-24 13:35:41 +020077 mutex_unlock(&user_lock);
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020078
79 if (disable_clk)
Bo Shen49af54f2014-09-24 17:33:55 +080080 clk_unprepare(ssc->clk);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070081}
82EXPORT_SYMBOL(ssc_free);
83
Bo Shen636036d22012-11-06 13:57:51 +080084static struct atmel_ssc_platform_data at91rm9200_config = {
85 .use_dma = 0,
Bo Shenc4027fa2014-06-11 18:14:39 +080086 .has_fslen_ext = 0,
87};
88
89static struct atmel_ssc_platform_data at91sam9rl_config = {
90 .use_dma = 0,
91 .has_fslen_ext = 1,
Bo Shen636036d22012-11-06 13:57:51 +080092};
93
94static struct atmel_ssc_platform_data at91sam9g45_config = {
95 .use_dma = 1,
Bo Shenc4027fa2014-06-11 18:14:39 +080096 .has_fslen_ext = 1,
Bo Shen636036d22012-11-06 13:57:51 +080097};
98
99static const struct platform_device_id atmel_ssc_devtypes[] = {
100 {
101 .name = "at91rm9200_ssc",
102 .driver_data = (unsigned long) &at91rm9200_config,
103 }, {
Bo Shenc4027fa2014-06-11 18:14:39 +0800104 .name = "at91sam9rl_ssc",
105 .driver_data = (unsigned long) &at91sam9rl_config,
106 }, {
Bo Shen636036d22012-11-06 13:57:51 +0800107 .name = "at91sam9g45_ssc",
108 .driver_data = (unsigned long) &at91sam9g45_config,
109 }, {
110 /* sentinel */
111 }
112};
113
Bo Shen099343c2012-11-07 11:41:41 +0800114#ifdef CONFIG_OF
115static const struct of_device_id atmel_ssc_dt_ids[] = {
116 {
117 .compatible = "atmel,at91rm9200-ssc",
118 .data = &at91rm9200_config,
119 }, {
Bo Shenc4027fa2014-06-11 18:14:39 +0800120 .compatible = "atmel,at91sam9rl-ssc",
121 .data = &at91sam9rl_config,
122 }, {
Bo Shen099343c2012-11-07 11:41:41 +0800123 .compatible = "atmel,at91sam9g45-ssc",
124 .data = &at91sam9g45_config,
125 }, {
126 /* sentinel */
127 }
128};
129MODULE_DEVICE_TABLE(of, atmel_ssc_dt_ids);
130#endif
131
Nathan Chancellor7c973012018-10-17 10:09:02 -0700132static inline const struct atmel_ssc_platform_data *
Bo Shen099343c2012-11-07 11:41:41 +0800133 atmel_ssc_get_driver_data(struct platform_device *pdev)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700134{
Bo Shen099343c2012-11-07 11:41:41 +0800135 if (pdev->dev.of_node) {
136 const struct of_device_id *match;
137 match = of_match_node(atmel_ssc_dt_ids, pdev->dev.of_node);
138 if (match == NULL)
139 return NULL;
140 return match->data;
141 }
142
143 return (struct atmel_ssc_platform_data *)
144 platform_get_device_id(pdev)->driver_data;
145}
146
Peter Rosine8314d72016-12-06 20:22:36 +0100147#ifdef CONFIG_SND_ATMEL_SOC_SSC
148static int ssc_sound_dai_probe(struct ssc_device *ssc)
149{
150 struct device_node *np = ssc->pdev->dev.of_node;
151 int ret;
152 int id;
153
154 ssc->sound_dai = false;
155
156 if (!of_property_read_bool(np, "#sound-dai-cells"))
157 return 0;
158
159 id = of_alias_get_id(np, "ssc");
160 if (id < 0)
161 return id;
162
163 ret = atmel_ssc_set_audio(id);
164 ssc->sound_dai = !ret;
165
166 return ret;
167}
168
169static void ssc_sound_dai_remove(struct ssc_device *ssc)
170{
171 if (!ssc->sound_dai)
172 return;
173
174 atmel_ssc_put_audio(of_alias_get_id(ssc->pdev->dev.of_node, "ssc"));
175}
176#else
177static inline int ssc_sound_dai_probe(struct ssc_device *ssc)
178{
179 if (of_property_read_bool(ssc->pdev->dev.of_node, "#sound-dai-cells"))
180 return -ENOTSUPP;
181
182 return 0;
183}
184
185static inline void ssc_sound_dai_remove(struct ssc_device *ssc)
186{
187}
188#endif
189
Bo Shen5c86ac62012-10-16 11:56:59 +0800190static int ssc_probe(struct platform_device *pdev)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700191{
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700192 struct resource *regs;
193 struct ssc_device *ssc;
Bo Shen099343c2012-11-07 11:41:41 +0800194 const struct atmel_ssc_platform_data *plat_dat;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700195
Bo Shen2e4de7b2012-10-16 11:56:58 +0800196 ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700197 if (!ssc) {
198 dev_dbg(&pdev->dev, "out of memory\n");
Bo Shen2e4de7b2012-10-16 11:56:58 +0800199 return -ENOMEM;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700200 }
201
Bo Shen2e4de7b2012-10-16 11:56:58 +0800202 ssc->pdev = pdev;
Bo Shen099343c2012-11-07 11:41:41 +0800203
204 plat_dat = atmel_ssc_get_driver_data(pdev);
205 if (!plat_dat)
206 return -ENODEV;
207 ssc->pdata = (struct atmel_ssc_platform_data *)plat_dat;
Bo Shen2e4de7b2012-10-16 11:56:58 +0800208
Bo Shen048d4ff2014-02-10 14:09:45 +0800209 if (pdev->dev.of_node) {
210 struct device_node *np = pdev->dev.of_node;
211 ssc->clk_from_rk_pin =
212 of_property_read_bool(np, "atmel,clk-from-rk-pin");
213 }
214
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700215 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding16847892013-01-21 11:09:10 +0100216 ssc->regs = devm_ioremap_resource(&pdev->dev, regs);
217 if (IS_ERR(ssc->regs))
218 return PTR_ERR(ssc->regs);
Bo Shen2e4de7b2012-10-16 11:56:58 +0800219
Nicolas Ferre31974362012-11-20 16:38:17 +0800220 ssc->phybase = regs->start;
221
Bo Shen2e4de7b2012-10-16 11:56:58 +0800222 ssc->clk = devm_clk_get(&pdev->dev, "pclk");
223 if (IS_ERR(ssc->clk)) {
224 dev_dbg(&pdev->dev, "no pclk clock defined\n");
225 return -ENXIO;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700226 }
227
228 /* disable all interrupts */
Boris BREZILLON6f0d9472013-06-07 18:26:09 +0200229 clk_prepare_enable(ssc->clk);
Joachim Eastwoodf4319ff2012-12-08 13:46:41 +0100230 ssc_writel(ssc->regs, IDR, -1);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700231 ssc_readl(ssc->regs, SR);
Boris BREZILLON6f0d9472013-06-07 18:26:09 +0200232 clk_disable_unprepare(ssc->clk);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700233
234 ssc->irq = platform_get_irq(pdev, 0);
235 if (!ssc->irq) {
236 dev_dbg(&pdev->dev, "could not get irq\n");
Bo Shen2e4de7b2012-10-16 11:56:58 +0800237 return -ENXIO;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700238 }
239
Michał Mirosławb037d602020-06-24 13:35:41 +0200240 mutex_lock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700241 list_add_tail(&ssc->list, &ssc_list);
Michał Mirosławb037d602020-06-24 13:35:41 +0200242 mutex_unlock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700243
244 platform_set_drvdata(pdev, ssc);
245
246 dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
247 ssc->regs, ssc->irq);
248
Peter Rosine8314d72016-12-06 20:22:36 +0100249 if (ssc_sound_dai_probe(ssc))
250 dev_err(&pdev->dev, "failed to auto-setup ssc for audio\n");
251
Bo Shen2e4de7b2012-10-16 11:56:58 +0800252 return 0;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700253}
254
Bill Pemberton486a5c22012-11-19 13:26:02 -0500255static int ssc_remove(struct platform_device *pdev)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700256{
257 struct ssc_device *ssc = platform_get_drvdata(pdev);
258
Peter Rosine8314d72016-12-06 20:22:36 +0100259 ssc_sound_dai_remove(ssc);
260
Michał Mirosławb037d602020-06-24 13:35:41 +0200261 mutex_lock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700262 list_del(&ssc->list);
Michał Mirosławb037d602020-06-24 13:35:41 +0200263 mutex_unlock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700264
265 return 0;
266}
267
268static struct platform_driver ssc_driver = {
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700269 .driver = {
270 .name = "ssc",
Bo Shen099343c2012-11-07 11:41:41 +0800271 .of_match_table = of_match_ptr(atmel_ssc_dt_ids),
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700272 },
Bo Shen636036d22012-11-06 13:57:51 +0800273 .id_table = atmel_ssc_devtypes,
Bo Shen5c86ac62012-10-16 11:56:59 +0800274 .probe = ssc_probe,
Linus Torvalds046e7d62012-12-13 11:51:23 -0800275 .remove = ssc_remove,
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700276};
Bo Shen5c86ac62012-10-16 11:56:59 +0800277module_platform_driver(ssc_driver);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700278
279MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
280MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
281MODULE_LICENSE("GPL");
Kay Sieversd6c23852008-04-15 14:34:33 -0700282MODULE_ALIAS("platform:ssc");