blob: 0516ecda54d35fc6a590750a3f27e76083e80003 [file] [log] [blame]
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -07001/*
2 * Atmel SSC driver
3 *
4 * Copyright (C) 2007 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/platform_device.h>
12#include <linux/list.h>
13#include <linux/clk.h>
14#include <linux/err.h>
15#include <linux/io.h>
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070016#include <linux/spinlock.h>
17#include <linux/atmel-ssc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Paul Gortmakereb12a672011-07-03 15:14:56 -040019#include <linux/module.h>
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070020
Bo Shen099343c2012-11-07 11:41:41 +080021#include <linux/of.h>
22
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070023/* Serialize access to ssc_list and user count */
24static DEFINE_SPINLOCK(user_lock);
25static LIST_HEAD(ssc_list);
26
27struct ssc_device *ssc_request(unsigned int ssc_num)
28{
29 int ssc_valid = 0;
30 struct ssc_device *ssc;
31
32 spin_lock(&user_lock);
33 list_for_each_entry(ssc, &ssc_list, list) {
Bo Shen099343c2012-11-07 11:41:41 +080034 if (ssc->pdev->dev.of_node) {
35 if (of_alias_get_id(ssc->pdev->dev.of_node, "ssc")
36 == ssc_num) {
Songjun Wuc706f2e2016-03-02 12:15:25 +010037 ssc->pdev->id = ssc_num;
Bo Shen099343c2012-11-07 11:41:41 +080038 ssc_valid = 1;
39 break;
40 }
41 } else if (ssc->pdev->id == ssc_num) {
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070042 ssc_valid = 1;
43 break;
44 }
45 }
46
47 if (!ssc_valid) {
48 spin_unlock(&user_lock);
Hans-Christian Egtvedtdfecb712009-02-04 15:12:17 -080049 pr_err("ssc: ssc%d platform device is missing\n", ssc_num);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070050 return ERR_PTR(-ENODEV);
51 }
52
53 if (ssc->user) {
54 spin_unlock(&user_lock);
55 dev_dbg(&ssc->pdev->dev, "module busy\n");
56 return ERR_PTR(-EBUSY);
57 }
58 ssc->user++;
59 spin_unlock(&user_lock);
60
Bo Shen49af54f2014-09-24 17:33:55 +080061 clk_prepare(ssc->clk);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070062
63 return ssc;
64}
65EXPORT_SYMBOL(ssc_request);
66
67void ssc_free(struct ssc_device *ssc)
68{
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020069 bool disable_clk = true;
70
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070071 spin_lock(&user_lock);
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020072 if (ssc->user)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070073 ssc->user--;
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020074 else {
75 disable_clk = false;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070076 dev_dbg(&ssc->pdev->dev, "device already free\n");
77 }
78 spin_unlock(&user_lock);
Boris BREZILLON9a9b1c62013-07-18 09:48:40 +020079
80 if (disable_clk)
Bo Shen49af54f2014-09-24 17:33:55 +080081 clk_unprepare(ssc->clk);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -070082}
83EXPORT_SYMBOL(ssc_free);
84
Bo Shen636036d22012-11-06 13:57:51 +080085static struct atmel_ssc_platform_data at91rm9200_config = {
86 .use_dma = 0,
Bo Shenc4027fa2014-06-11 18:14:39 +080087 .has_fslen_ext = 0,
88};
89
90static struct atmel_ssc_platform_data at91sam9rl_config = {
91 .use_dma = 0,
92 .has_fslen_ext = 1,
Bo Shen636036d22012-11-06 13:57:51 +080093};
94
95static struct atmel_ssc_platform_data at91sam9g45_config = {
96 .use_dma = 1,
Bo Shenc4027fa2014-06-11 18:14:39 +080097 .has_fslen_ext = 1,
Bo Shen636036d22012-11-06 13:57:51 +080098};
99
100static const struct platform_device_id atmel_ssc_devtypes[] = {
101 {
102 .name = "at91rm9200_ssc",
103 .driver_data = (unsigned long) &at91rm9200_config,
104 }, {
Bo Shenc4027fa2014-06-11 18:14:39 +0800105 .name = "at91sam9rl_ssc",
106 .driver_data = (unsigned long) &at91sam9rl_config,
107 }, {
Bo Shen636036d22012-11-06 13:57:51 +0800108 .name = "at91sam9g45_ssc",
109 .driver_data = (unsigned long) &at91sam9g45_config,
110 }, {
111 /* sentinel */
112 }
113};
114
Bo Shen099343c2012-11-07 11:41:41 +0800115#ifdef CONFIG_OF
116static const struct of_device_id atmel_ssc_dt_ids[] = {
117 {
118 .compatible = "atmel,at91rm9200-ssc",
119 .data = &at91rm9200_config,
120 }, {
Bo Shenc4027fa2014-06-11 18:14:39 +0800121 .compatible = "atmel,at91sam9rl-ssc",
122 .data = &at91sam9rl_config,
123 }, {
Bo Shen099343c2012-11-07 11:41:41 +0800124 .compatible = "atmel,at91sam9g45-ssc",
125 .data = &at91sam9g45_config,
126 }, {
127 /* sentinel */
128 }
129};
130MODULE_DEVICE_TABLE(of, atmel_ssc_dt_ids);
131#endif
132
133static inline const struct atmel_ssc_platform_data * __init
134 atmel_ssc_get_driver_data(struct platform_device *pdev)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700135{
Bo Shen099343c2012-11-07 11:41:41 +0800136 if (pdev->dev.of_node) {
137 const struct of_device_id *match;
138 match = of_match_node(atmel_ssc_dt_ids, pdev->dev.of_node);
139 if (match == NULL)
140 return NULL;
141 return match->data;
142 }
143
144 return (struct atmel_ssc_platform_data *)
145 platform_get_device_id(pdev)->driver_data;
146}
147
Bo Shen5c86ac62012-10-16 11:56:59 +0800148static int ssc_probe(struct platform_device *pdev)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700149{
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700150 struct resource *regs;
151 struct ssc_device *ssc;
Bo Shen099343c2012-11-07 11:41:41 +0800152 const struct atmel_ssc_platform_data *plat_dat;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700153
Bo Shen2e4de7b2012-10-16 11:56:58 +0800154 ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700155 if (!ssc) {
156 dev_dbg(&pdev->dev, "out of memory\n");
Bo Shen2e4de7b2012-10-16 11:56:58 +0800157 return -ENOMEM;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700158 }
159
Bo Shen2e4de7b2012-10-16 11:56:58 +0800160 ssc->pdev = pdev;
Bo Shen099343c2012-11-07 11:41:41 +0800161
162 plat_dat = atmel_ssc_get_driver_data(pdev);
163 if (!plat_dat)
164 return -ENODEV;
165 ssc->pdata = (struct atmel_ssc_platform_data *)plat_dat;
Bo Shen2e4de7b2012-10-16 11:56:58 +0800166
Bo Shen048d4ff2014-02-10 14:09:45 +0800167 if (pdev->dev.of_node) {
168 struct device_node *np = pdev->dev.of_node;
169 ssc->clk_from_rk_pin =
170 of_property_read_bool(np, "atmel,clk-from-rk-pin");
171 }
172
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700173 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding16847892013-01-21 11:09:10 +0100174 ssc->regs = devm_ioremap_resource(&pdev->dev, regs);
175 if (IS_ERR(ssc->regs))
176 return PTR_ERR(ssc->regs);
Bo Shen2e4de7b2012-10-16 11:56:58 +0800177
Nicolas Ferre31974362012-11-20 16:38:17 +0800178 ssc->phybase = regs->start;
179
Bo Shen2e4de7b2012-10-16 11:56:58 +0800180 ssc->clk = devm_clk_get(&pdev->dev, "pclk");
181 if (IS_ERR(ssc->clk)) {
182 dev_dbg(&pdev->dev, "no pclk clock defined\n");
183 return -ENXIO;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700184 }
185
186 /* disable all interrupts */
Boris BREZILLON6f0d9472013-06-07 18:26:09 +0200187 clk_prepare_enable(ssc->clk);
Joachim Eastwoodf4319ff2012-12-08 13:46:41 +0100188 ssc_writel(ssc->regs, IDR, -1);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700189 ssc_readl(ssc->regs, SR);
Boris BREZILLON6f0d9472013-06-07 18:26:09 +0200190 clk_disable_unprepare(ssc->clk);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700191
192 ssc->irq = platform_get_irq(pdev, 0);
193 if (!ssc->irq) {
194 dev_dbg(&pdev->dev, "could not get irq\n");
Bo Shen2e4de7b2012-10-16 11:56:58 +0800195 return -ENXIO;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700196 }
197
198 spin_lock(&user_lock);
199 list_add_tail(&ssc->list, &ssc_list);
200 spin_unlock(&user_lock);
201
202 platform_set_drvdata(pdev, ssc);
203
204 dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n",
205 ssc->regs, ssc->irq);
206
Bo Shen2e4de7b2012-10-16 11:56:58 +0800207 return 0;
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700208}
209
Bill Pemberton486a5c22012-11-19 13:26:02 -0500210static int ssc_remove(struct platform_device *pdev)
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700211{
212 struct ssc_device *ssc = platform_get_drvdata(pdev);
213
214 spin_lock(&user_lock);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700215 list_del(&ssc->list);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700216 spin_unlock(&user_lock);
217
218 return 0;
219}
220
221static struct platform_driver ssc_driver = {
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700222 .driver = {
223 .name = "ssc",
Bo Shen099343c2012-11-07 11:41:41 +0800224 .of_match_table = of_match_ptr(atmel_ssc_dt_ids),
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700225 },
Bo Shen636036d22012-11-06 13:57:51 +0800226 .id_table = atmel_ssc_devtypes,
Bo Shen5c86ac62012-10-16 11:56:59 +0800227 .probe = ssc_probe,
Linus Torvalds046e7d62012-12-13 11:51:23 -0800228 .remove = ssc_remove,
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700229};
Bo Shen5c86ac62012-10-16 11:56:59 +0800230module_platform_driver(ssc_driver);
Hans-Christian Egtvedteb1f2932007-10-16 23:26:11 -0700231
232MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
233MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91");
234MODULE_LICENSE("GPL");
Kay Sieversd6c23852008-04-15 14:34:33 -0700235MODULE_ALIAS("platform:ssc");