mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / can / c_can / c_can_platform.c
blob294ced3cc227520883c6ebe50ac255634d0f0f7c
1 /*
2 * Platform CAN bus driver for Bosch C_CAN controller
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
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>
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
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.
22 #include <linux/kernel.h>
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>
30 #include <linux/io.h>
31 #include <linux/platform_device.h>
32 #include <linux/clk.h>
33 #include <linux/of.h>
34 #include <linux/of_device.h>
36 #include <linux/can/dev.h>
38 #include "c_can.h"
40 #define CAN_RAMINIT_START_MASK(i) (1 << (i))
43 * 16-bit c_can registers can be arranged differently in the memory
44 * architecture of different implementations. For example: 16-bit
45 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
46 * Handle the same by providing a common read/write interface.
48 static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv,
49 enum reg index)
51 return readw(priv->base + priv->regs[index]);
54 static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv,
55 enum reg index, u16 val)
57 writew(val, priv->base + priv->regs[index]);
60 static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv,
61 enum reg index)
63 return readw(priv->base + 2 * priv->regs[index]);
66 static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv,
67 enum reg index, u16 val)
69 writew(val, priv->base + 2 * priv->regs[index]);
72 static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
74 u32 val;
76 val = readl(priv->raminit_ctrlreg);
77 if (enable)
78 val |= CAN_RAMINIT_START_MASK(priv->instance);
79 else
80 val &= ~CAN_RAMINIT_START_MASK(priv->instance);
81 writel(val, priv->raminit_ctrlreg);
84 static struct platform_device_id c_can_id_table[] = {
85 [BOSCH_C_CAN_PLATFORM] = {
86 .name = KBUILD_MODNAME,
87 .driver_data = BOSCH_C_CAN,
89 [BOSCH_C_CAN] = {
90 .name = "c_can",
91 .driver_data = BOSCH_C_CAN,
93 [BOSCH_D_CAN] = {
94 .name = "d_can",
95 .driver_data = BOSCH_D_CAN,
96 }, {
99 MODULE_DEVICE_TABLE(platform, c_can_id_table);
101 static const struct of_device_id c_can_of_table[] = {
102 { .compatible = "bosch,c_can", .data = &c_can_id_table[BOSCH_C_CAN] },
103 { .compatible = "bosch,d_can", .data = &c_can_id_table[BOSCH_D_CAN] },
104 { /* sentinel */ },
106 MODULE_DEVICE_TABLE(of, c_can_of_table);
108 static int c_can_plat_probe(struct platform_device *pdev)
110 int ret;
111 void __iomem *addr;
112 struct net_device *dev;
113 struct c_can_priv *priv;
114 const struct of_device_id *match;
115 const struct platform_device_id *id;
116 struct resource *mem, *res;
117 int irq;
118 struct clk *clk;
120 if (pdev->dev.of_node) {
121 match = of_match_device(c_can_of_table, &pdev->dev);
122 if (!match) {
123 dev_err(&pdev->dev, "Failed to find matching dt id\n");
124 ret = -EINVAL;
125 goto exit;
127 id = match->data;
128 } else {
129 id = platform_get_device_id(pdev);
132 /* get the appropriate clk */
133 clk = clk_get(&pdev->dev, NULL);
134 if (IS_ERR(clk)) {
135 dev_err(&pdev->dev, "no clock defined\n");
136 ret = -ENODEV;
137 goto exit;
140 /* get the platform data */
141 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
142 irq = platform_get_irq(pdev, 0);
143 if (!mem || irq <= 0) {
144 ret = -ENODEV;
145 goto exit_free_clk;
148 if (!request_mem_region(mem->start, resource_size(mem),
149 KBUILD_MODNAME)) {
150 dev_err(&pdev->dev, "resource unavailable\n");
151 ret = -ENODEV;
152 goto exit_free_clk;
155 addr = ioremap(mem->start, resource_size(mem));
156 if (!addr) {
157 dev_err(&pdev->dev, "failed to map can port\n");
158 ret = -ENOMEM;
159 goto exit_release_mem;
162 /* allocate the c_can device */
163 dev = alloc_c_can_dev();
164 if (!dev) {
165 ret = -ENOMEM;
166 goto exit_iounmap;
169 priv = netdev_priv(dev);
170 switch (id->driver_data) {
171 case BOSCH_C_CAN:
172 priv->regs = reg_map_c_can;
173 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
174 case IORESOURCE_MEM_32BIT:
175 priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
176 priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
177 break;
178 case IORESOURCE_MEM_16BIT:
179 default:
180 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
181 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
182 break;
184 break;
185 case BOSCH_D_CAN:
186 priv->regs = reg_map_d_can;
187 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
188 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
189 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
191 if (pdev->dev.of_node)
192 priv->instance = of_alias_get_id(pdev->dev.of_node, "d_can");
193 else
194 priv->instance = pdev->id;
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
197 priv->raminit_ctrlreg = devm_ioremap_resource(&pdev->dev, res);
198 if (IS_ERR(priv->raminit_ctrlreg) || (int)priv->instance < 0)
199 dev_info(&pdev->dev, "control memory is not used for raminit\n");
200 else
201 priv->raminit = c_can_hw_raminit;
202 break;
203 default:
204 ret = -EINVAL;
205 goto exit_free_device;
208 dev->irq = irq;
209 priv->base = addr;
210 priv->device = &pdev->dev;
211 priv->can.clock.freq = clk_get_rate(clk);
212 priv->priv = clk;
213 priv->type = id->driver_data;
215 platform_set_drvdata(pdev, dev);
216 SET_NETDEV_DEV(dev, &pdev->dev);
218 ret = register_c_can_dev(dev);
219 if (ret) {
220 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
221 KBUILD_MODNAME, ret);
222 goto exit_free_device;
225 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
226 KBUILD_MODNAME, priv->base, dev->irq);
227 return 0;
229 exit_free_device:
230 free_c_can_dev(dev);
231 exit_iounmap:
232 iounmap(addr);
233 exit_release_mem:
234 release_mem_region(mem->start, resource_size(mem));
235 exit_free_clk:
236 clk_put(clk);
237 exit:
238 dev_err(&pdev->dev, "probe failed\n");
240 return ret;
243 static int c_can_plat_remove(struct platform_device *pdev)
245 struct net_device *dev = platform_get_drvdata(pdev);
246 struct c_can_priv *priv = netdev_priv(dev);
247 struct resource *mem;
249 unregister_c_can_dev(dev);
251 free_c_can_dev(dev);
252 iounmap(priv->base);
254 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
255 release_mem_region(mem->start, resource_size(mem));
257 clk_put(priv->priv);
259 return 0;
262 #ifdef CONFIG_PM
263 static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
265 int ret;
266 struct net_device *ndev = platform_get_drvdata(pdev);
267 struct c_can_priv *priv = netdev_priv(ndev);
269 if (priv->type != BOSCH_D_CAN) {
270 dev_warn(&pdev->dev, "Not supported\n");
271 return 0;
274 if (netif_running(ndev)) {
275 netif_stop_queue(ndev);
276 netif_device_detach(ndev);
279 ret = c_can_power_down(ndev);
280 if (ret) {
281 netdev_err(ndev, "failed to enter power down mode\n");
282 return ret;
285 priv->can.state = CAN_STATE_SLEEPING;
287 return 0;
290 static int c_can_resume(struct platform_device *pdev)
292 int ret;
293 struct net_device *ndev = platform_get_drvdata(pdev);
294 struct c_can_priv *priv = netdev_priv(ndev);
296 if (priv->type != BOSCH_D_CAN) {
297 dev_warn(&pdev->dev, "Not supported\n");
298 return 0;
301 ret = c_can_power_up(ndev);
302 if (ret) {
303 netdev_err(ndev, "Still in power down mode\n");
304 return ret;
307 priv->can.state = CAN_STATE_ERROR_ACTIVE;
309 if (netif_running(ndev)) {
310 netif_device_attach(ndev);
311 netif_start_queue(ndev);
314 return 0;
316 #else
317 #define c_can_suspend NULL
318 #define c_can_resume NULL
319 #endif
321 static struct platform_driver c_can_plat_driver = {
322 .driver = {
323 .name = KBUILD_MODNAME,
324 .owner = THIS_MODULE,
325 .of_match_table = of_match_ptr(c_can_of_table),
327 .probe = c_can_plat_probe,
328 .remove = c_can_plat_remove,
329 .suspend = c_can_suspend,
330 .resume = c_can_resume,
331 .id_table = c_can_id_table,
334 module_platform_driver(c_can_plat_driver);
336 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
337 MODULE_LICENSE("GPL v2");
338 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");