Rabin Vincent | fbf1eadf | 2010-09-29 19:46:32 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2010 |
| 3 | * |
| 4 | * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson |
| 5 | * License terms: GNU General Public License (GPL), version 2. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/dma-mapping.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/irq.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/amba/bus.h> |
| 15 | |
Linus Walleij | 0f33286 | 2011-08-22 08:33:30 +0100 | [diff] [blame] | 16 | #include <plat/gpio-nomadik.h> |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 17 | |
Rabin Vincent | fbf1eadf | 2010-09-29 19:46:32 +0530 | [diff] [blame] | 18 | #include <mach/hardware.h> |
| 19 | |
| 20 | #include "devices-common.h" |
| 21 | |
| 22 | struct amba_device * |
Lee Jones | 1840342 | 2012-02-06 11:22:21 -0800 | [diff] [blame] | 23 | dbx500_add_amba_device(struct device *parent, const char *name, |
| 24 | resource_size_t base, int irq, void *pdata, |
| 25 | unsigned int periphid) |
Rabin Vincent | fbf1eadf | 2010-09-29 19:46:32 +0530 | [diff] [blame] | 26 | { |
| 27 | struct amba_device *dev; |
| 28 | int ret; |
| 29 | |
| 30 | dev = kzalloc(sizeof *dev, GFP_KERNEL); |
| 31 | if (!dev) |
| 32 | return ERR_PTR(-ENOMEM); |
| 33 | |
| 34 | dev->dev.init_name = name; |
| 35 | |
| 36 | dev->res.start = base; |
| 37 | dev->res.end = base + SZ_4K - 1; |
| 38 | dev->res.flags = IORESOURCE_MEM; |
| 39 | |
| 40 | dev->dma_mask = DMA_BIT_MASK(32); |
| 41 | dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 42 | |
| 43 | dev->irq[0] = irq; |
| 44 | dev->irq[1] = NO_IRQ; |
| 45 | |
| 46 | dev->periphid = periphid; |
| 47 | |
| 48 | dev->dev.platform_data = pdata; |
| 49 | |
Lee Jones | b024a0c | 2012-02-06 11:22:25 -0800 | [diff] [blame^] | 50 | dev->dev.parent = parent; |
| 51 | |
Rabin Vincent | fbf1eadf | 2010-09-29 19:46:32 +0530 | [diff] [blame] | 52 | ret = amba_device_register(dev, &iomem_resource); |
| 53 | if (ret) { |
| 54 | kfree(dev); |
| 55 | return ERR_PTR(ret); |
| 56 | } |
| 57 | |
| 58 | return dev; |
| 59 | } |
| 60 | |
| 61 | static struct platform_device * |
| 62 | dbx500_add_platform_device(const char *name, int id, void *pdata, |
| 63 | struct resource *res, int resnum) |
| 64 | { |
| 65 | struct platform_device *dev; |
| 66 | int ret; |
| 67 | |
| 68 | dev = platform_device_alloc(name, id); |
| 69 | if (!dev) |
| 70 | return ERR_PTR(-ENOMEM); |
| 71 | |
| 72 | dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
| 73 | dev->dev.dma_mask = &dev->dev.coherent_dma_mask; |
| 74 | |
| 75 | ret = platform_device_add_resources(dev, res, resnum); |
| 76 | if (ret) |
| 77 | goto out_free; |
| 78 | |
| 79 | dev->dev.platform_data = pdata; |
| 80 | |
| 81 | ret = platform_device_add(dev); |
| 82 | if (ret) |
| 83 | goto out_free; |
| 84 | |
| 85 | return dev; |
| 86 | |
| 87 | out_free: |
| 88 | platform_device_put(dev); |
| 89 | return ERR_PTR(ret); |
| 90 | } |
| 91 | |
| 92 | struct platform_device * |
| 93 | dbx500_add_platform_device_4k1irq(const char *name, int id, |
| 94 | resource_size_t base, |
| 95 | int irq, void *pdata) |
| 96 | { |
| 97 | struct resource resources[] = { |
| 98 | [0] = { |
| 99 | .start = base, |
| 100 | .end = base + SZ_4K - 1, |
| 101 | .flags = IORESOURCE_MEM, |
| 102 | }, |
| 103 | [1] = { |
| 104 | .start = irq, |
| 105 | .end = irq, |
| 106 | .flags = IORESOURCE_IRQ, |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | return dbx500_add_platform_device(name, id, pdata, resources, |
| 111 | ARRAY_SIZE(resources)); |
| 112 | } |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 113 | |
| 114 | static struct platform_device * |
Lee Jones | 1840342 | 2012-02-06 11:22:21 -0800 | [diff] [blame] | 115 | dbx500_add_gpio(struct device *parent, int id, resource_size_t addr, int irq, |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 116 | struct nmk_gpio_platform_data *pdata) |
| 117 | { |
| 118 | struct resource resources[] = { |
| 119 | { |
| 120 | .start = addr, |
| 121 | .end = addr + 127, |
| 122 | .flags = IORESOURCE_MEM, |
| 123 | }, |
| 124 | { |
| 125 | .start = irq, |
| 126 | .end = irq, |
| 127 | .flags = IORESOURCE_IRQ, |
| 128 | } |
| 129 | }; |
| 130 | |
Lee Jones | b024a0c | 2012-02-06 11:22:25 -0800 | [diff] [blame^] | 131 | return platform_device_register_resndata( |
| 132 | parent, |
| 133 | "gpio", |
| 134 | id, |
| 135 | resources, |
| 136 | ARRAY_SIZE(resources), |
| 137 | pdata, |
| 138 | sizeof(*pdata)); |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 139 | } |
| 140 | |
Lee Jones | 1840342 | 2012-02-06 11:22:21 -0800 | [diff] [blame] | 141 | void dbx500_add_gpios(struct device *parent, resource_size_t *base, int num, |
| 142 | int irq, struct nmk_gpio_platform_data *pdata) |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 143 | { |
| 144 | int first = 0; |
| 145 | int i; |
| 146 | |
| 147 | for (i = 0; i < num; i++, first += 32, irq++) { |
| 148 | pdata->first_gpio = first; |
| 149 | pdata->first_irq = NOMADIK_GPIO_TO_IRQ(first); |
Rabin Vincent | e493e06 | 2010-03-18 12:35:22 +0530 | [diff] [blame] | 150 | pdata->num_gpio = 32; |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 151 | |
Lee Jones | 1840342 | 2012-02-06 11:22:21 -0800 | [diff] [blame] | 152 | dbx500_add_gpio(parent, i, base[i], irq, pdata); |
Rabin Vincent | 01afdd1 | 2010-12-08 11:07:55 +0530 | [diff] [blame] | 153 | } |
| 154 | } |