net: can: Convert to use devm_ioremap_resource
[deliverable/linux.git] / drivers / net / can / c_can / c_can_platform.c
CommitLineData
881ff67a
BS
1/*
2 * Platform CAN bus driver for Bosch C_CAN controller
3 *
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
6 *
7 * Borrowed heavily from the C_CAN driver originally written by:
8 * Copyright (C) 2007
9 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
11 *
12 * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
13 * Bosch C_CAN user manual can be obtained from:
14 * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
15 * users_manual_c_can.pdf
16 *
17 * This file is licensed under the terms of the GNU General Public
18 * License version 2. This program is licensed "as is" without any
19 * warranty of any kind, whether express or implied.
20 */
21
22#include <linux/kernel.h>
881ff67a
BS
23#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/if_arp.h>
28#include <linux/if_ether.h>
29#include <linux/list.h>
881ff67a
BS
30#include <linux/io.h>
31#include <linux/platform_device.h>
32#include <linux/clk.h>
2469627d
AC
33#include <linux/of.h>
34#include <linux/of_device.h>
006cd138 35#include <linux/pinctrl/consumer.h>
881ff67a
BS
36
37#include <linux/can/dev.h>
38
39#include "c_can.h"
40
52cde85a
AC
41#define CAN_RAMINIT_START_MASK(i) (1 << (i))
42
881ff67a
BS
43/*
44 * 16-bit c_can registers can be arranged differently in the memory
45 * architecture of different implementations. For example: 16-bit
46 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
47 * Handle the same by providing a common read/write interface.
48 */
49static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv,
33f81009 50 enum reg index)
881ff67a 51{
33f81009 52 return readw(priv->base + priv->regs[index]);
881ff67a
BS
53}
54
55static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv,
33f81009 56 enum reg index, u16 val)
881ff67a 57{
33f81009 58 writew(val, priv->base + priv->regs[index]);
881ff67a
BS
59}
60
61static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv,
33f81009 62 enum reg index)
881ff67a 63{
33f81009 64 return readw(priv->base + 2 * priv->regs[index]);
881ff67a
BS
65}
66
67static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv,
33f81009 68 enum reg index, u16 val)
881ff67a 69{
33f81009 70 writew(val, priv->base + 2 * priv->regs[index]);
881ff67a
BS
71}
72
52cde85a
AC
73static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
74{
75 u32 val;
76
77 val = readl(priv->raminit_ctrlreg);
78 if (enable)
79 val |= CAN_RAMINIT_START_MASK(priv->instance);
80 else
81 val &= ~CAN_RAMINIT_START_MASK(priv->instance);
82 writel(val, priv->raminit_ctrlreg);
83}
84
2469627d
AC
85static struct platform_device_id c_can_id_table[] = {
86 [BOSCH_C_CAN_PLATFORM] = {
87 .name = KBUILD_MODNAME,
88 .driver_data = BOSCH_C_CAN,
89 },
90 [BOSCH_C_CAN] = {
91 .name = "c_can",
92 .driver_data = BOSCH_C_CAN,
93 },
94 [BOSCH_D_CAN] = {
95 .name = "d_can",
96 .driver_data = BOSCH_D_CAN,
97 }, {
98 }
99};
69c0c5b1 100MODULE_DEVICE_TABLE(platform, c_can_id_table);
2469627d
AC
101
102static const struct of_device_id c_can_of_table[] = {
103 { .compatible = "bosch,c_can", .data = &c_can_id_table[BOSCH_C_CAN] },
104 { .compatible = "bosch,d_can", .data = &c_can_id_table[BOSCH_D_CAN] },
105 { /* sentinel */ },
106};
69c0c5b1 107MODULE_DEVICE_TABLE(of, c_can_of_table);
2469627d 108
3c8ac0f2 109static int c_can_plat_probe(struct platform_device *pdev)
881ff67a
BS
110{
111 int ret;
112 void __iomem *addr;
113 struct net_device *dev;
114 struct c_can_priv *priv;
2469627d 115 const struct of_device_id *match;
69927fcc 116 const struct platform_device_id *id;
006cd138 117 struct pinctrl *pinctrl;
52cde85a 118 struct resource *mem, *res;
b0052b08 119 int irq;
881ff67a
BS
120 struct clk *clk;
121
2469627d
AC
122 if (pdev->dev.of_node) {
123 match = of_match_device(c_can_of_table, &pdev->dev);
124 if (!match) {
125 dev_err(&pdev->dev, "Failed to find matching dt id\n");
126 ret = -EINVAL;
127 goto exit;
128 }
129 id = match->data;
130 } else {
131 id = platform_get_device_id(pdev);
132 }
133
006cd138
AC
134 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
135 if (IS_ERR(pinctrl))
136 dev_warn(&pdev->dev,
137 "failed to configure pins from driver\n");
138
881ff67a
BS
139 /* get the appropriate clk */
140 clk = clk_get(&pdev->dev, NULL);
141 if (IS_ERR(clk)) {
142 dev_err(&pdev->dev, "no clock defined\n");
143 ret = -ENODEV;
144 goto exit;
145 }
881ff67a
BS
146
147 /* get the platform data */
148 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
b0052b08
MKB
149 irq = platform_get_irq(pdev, 0);
150 if (!mem || irq <= 0) {
881ff67a
BS
151 ret = -ENODEV;
152 goto exit_free_clk;
153 }
154
155 if (!request_mem_region(mem->start, resource_size(mem),
156 KBUILD_MODNAME)) {
157 dev_err(&pdev->dev, "resource unavailable\n");
158 ret = -ENODEV;
159 goto exit_free_clk;
160 }
161
162 addr = ioremap(mem->start, resource_size(mem));
163 if (!addr) {
164 dev_err(&pdev->dev, "failed to map can port\n");
165 ret = -ENOMEM;
166 goto exit_release_mem;
167 }
168
169 /* allocate the c_can device */
170 dev = alloc_c_can_dev();
171 if (!dev) {
172 ret = -ENOMEM;
173 goto exit_iounmap;
174 }
175
176 priv = netdev_priv(dev);
69927fcc 177 switch (id->driver_data) {
f27b1db9 178 case BOSCH_C_CAN:
69927fcc
AC
179 priv->regs = reg_map_c_can;
180 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
181 case IORESOURCE_MEM_32BIT:
182 priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
183 priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
184 break;
185 case IORESOURCE_MEM_16BIT:
186 default:
187 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
188 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
189 break;
190 }
191 break;
f27b1db9 192 case BOSCH_D_CAN:
69927fcc
AC
193 priv->regs = reg_map_d_can;
194 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
195 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
196 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
52cde85a
AC
197
198 if (pdev->dev.of_node)
199 priv->instance = of_alias_get_id(pdev->dev.of_node, "d_can");
200 else
201 priv->instance = pdev->id;
202
203 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
32766fff
TB
204 priv->raminit_ctrlreg = devm_ioremap_resource(&pdev->dev, res);
205 if (IS_ERR(priv->raminit_ctrlreg) || priv->instance < 0)
52cde85a
AC
206 dev_info(&pdev->dev, "control memory is not used for raminit\n");
207 else
208 priv->raminit = c_can_hw_raminit;
69927fcc
AC
209 break;
210 default:
211 ret = -EINVAL;
212 goto exit_free_device;
213 }
881ff67a 214
b0052b08 215 dev->irq = irq;
33f81009 216 priv->base = addr;
4cdd34b2 217 priv->device = &pdev->dev;
881ff67a
BS
218 priv->can.clock.freq = clk_get_rate(clk);
219 priv->priv = clk;
82120032 220 priv->type = id->driver_data;
881ff67a 221
881ff67a
BS
222 platform_set_drvdata(pdev, dev);
223 SET_NETDEV_DEV(dev, &pdev->dev);
224
225 ret = register_c_can_dev(dev);
226 if (ret) {
227 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
228 KBUILD_MODNAME, ret);
229 goto exit_free_device;
230 }
231
232 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
33f81009 233 KBUILD_MODNAME, priv->base, dev->irq);
881ff67a
BS
234 return 0;
235
236exit_free_device:
881ff67a
BS
237 free_c_can_dev(dev);
238exit_iounmap:
239 iounmap(addr);
240exit_release_mem:
241 release_mem_region(mem->start, resource_size(mem));
242exit_free_clk:
881ff67a
BS
243 clk_put(clk);
244exit:
881ff67a
BS
245 dev_err(&pdev->dev, "probe failed\n");
246
247 return ret;
248}
249
3c8ac0f2 250static int c_can_plat_remove(struct platform_device *pdev)
881ff67a
BS
251{
252 struct net_device *dev = platform_get_drvdata(pdev);
253 struct c_can_priv *priv = netdev_priv(dev);
254 struct resource *mem;
255
256 unregister_c_can_dev(dev);
881ff67a
BS
257
258 free_c_can_dev(dev);
33f81009 259 iounmap(priv->base);
881ff67a
BS
260
261 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
262 release_mem_region(mem->start, resource_size(mem));
263
881ff67a 264 clk_put(priv->priv);
881ff67a
BS
265
266 return 0;
267}
268
82120032
AC
269#ifdef CONFIG_PM
270static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
271{
272 int ret;
273 struct net_device *ndev = platform_get_drvdata(pdev);
274 struct c_can_priv *priv = netdev_priv(ndev);
275
276 if (priv->type != BOSCH_D_CAN) {
277 dev_warn(&pdev->dev, "Not supported\n");
278 return 0;
279 }
280
281 if (netif_running(ndev)) {
282 netif_stop_queue(ndev);
283 netif_device_detach(ndev);
284 }
285
286 ret = c_can_power_down(ndev);
287 if (ret) {
288 netdev_err(ndev, "failed to enter power down mode\n");
289 return ret;
290 }
291
292 priv->can.state = CAN_STATE_SLEEPING;
293
294 return 0;
295}
296
297static int c_can_resume(struct platform_device *pdev)
298{
299 int ret;
300 struct net_device *ndev = platform_get_drvdata(pdev);
301 struct c_can_priv *priv = netdev_priv(ndev);
302
303 if (priv->type != BOSCH_D_CAN) {
304 dev_warn(&pdev->dev, "Not supported\n");
305 return 0;
306 }
307
308 ret = c_can_power_up(ndev);
309 if (ret) {
310 netdev_err(ndev, "Still in power down mode\n");
311 return ret;
312 }
313
314 priv->can.state = CAN_STATE_ERROR_ACTIVE;
315
316 if (netif_running(ndev)) {
317 netif_device_attach(ndev);
318 netif_start_queue(ndev);
319 }
320
321 return 0;
322}
323#else
324#define c_can_suspend NULL
325#define c_can_resume NULL
326#endif
327
881ff67a
BS
328static struct platform_driver c_can_plat_driver = {
329 .driver = {
330 .name = KBUILD_MODNAME,
331 .owner = THIS_MODULE,
2469627d 332 .of_match_table = of_match_ptr(c_can_of_table),
881ff67a
BS
333 },
334 .probe = c_can_plat_probe,
3c8ac0f2 335 .remove = c_can_plat_remove,
82120032
AC
336 .suspend = c_can_suspend,
337 .resume = c_can_resume,
69927fcc 338 .id_table = c_can_id_table,
881ff67a
BS
339};
340
871d3372 341module_platform_driver(c_can_plat_driver);
881ff67a
BS
342
343MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
344MODULE_LICENSE("GPL v2");
345MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");
This page took 0.202607 seconds and 5 git commands to generate.