Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/init.h> |
| 3 | #include <linux/gpio.h> |
| 4 | #include <linux/of_gpio.h> |
| 5 | #include <linux/io.h> |
| 6 | #include <linux/platform_device.h> |
| 7 | #include <linux/basic_mmio_gpio.h> |
| 8 | |
| 9 | #define ETRAX_FS_rw_pa_dout 0 |
| 10 | #define ETRAX_FS_r_pa_din 4 |
| 11 | #define ETRAX_FS_rw_pa_oe 8 |
| 12 | #define ETRAX_FS_rw_intr_cfg 12 |
| 13 | #define ETRAX_FS_rw_intr_mask 16 |
| 14 | #define ETRAX_FS_rw_ack_intr 20 |
| 15 | #define ETRAX_FS_r_intr 24 |
| 16 | #define ETRAX_FS_rw_pb_dout 32 |
| 17 | #define ETRAX_FS_r_pb_din 36 |
| 18 | #define ETRAX_FS_rw_pb_oe 40 |
| 19 | #define ETRAX_FS_rw_pc_dout 48 |
| 20 | #define ETRAX_FS_r_pc_din 52 |
| 21 | #define ETRAX_FS_rw_pc_oe 56 |
| 22 | #define ETRAX_FS_rw_pd_dout 64 |
| 23 | #define ETRAX_FS_r_pd_din 68 |
| 24 | #define ETRAX_FS_rw_pd_oe 72 |
| 25 | #define ETRAX_FS_rw_pe_dout 80 |
| 26 | #define ETRAX_FS_r_pe_din 84 |
| 27 | #define ETRAX_FS_rw_pe_oe 88 |
| 28 | |
Rabin Vincent | d705073 | 2015-07-22 15:05:19 +0200 | [diff] [blame^] | 29 | #define ARTPEC3_r_pa_din 0 |
| 30 | #define ARTPEC3_rw_pa_dout 4 |
| 31 | #define ARTPEC3_rw_pa_oe 8 |
| 32 | #define ARTPEC3_r_pb_din 44 |
| 33 | #define ARTPEC3_rw_pb_dout 48 |
| 34 | #define ARTPEC3_rw_pb_oe 52 |
| 35 | #define ARTPEC3_r_pc_din 88 |
| 36 | #define ARTPEC3_rw_pc_dout 92 |
| 37 | #define ARTPEC3_rw_pc_oe 96 |
| 38 | #define ARTPEC3_r_pd_din 116 |
| 39 | |
Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 40 | struct etraxfs_gpio_port { |
| 41 | const char *label; |
| 42 | unsigned int oe; |
| 43 | unsigned int dout; |
| 44 | unsigned int din; |
| 45 | unsigned int ngpio; |
| 46 | }; |
| 47 | |
| 48 | struct etraxfs_gpio_info { |
| 49 | unsigned int num_ports; |
| 50 | const struct etraxfs_gpio_port *ports; |
| 51 | }; |
| 52 | |
| 53 | static const struct etraxfs_gpio_port etraxfs_gpio_etraxfs_ports[] = { |
| 54 | { |
| 55 | .label = "A", |
| 56 | .ngpio = 8, |
| 57 | .oe = ETRAX_FS_rw_pa_oe, |
| 58 | .dout = ETRAX_FS_rw_pa_dout, |
| 59 | .din = ETRAX_FS_r_pa_din, |
| 60 | }, |
| 61 | { |
| 62 | .label = "B", |
| 63 | .ngpio = 18, |
| 64 | .oe = ETRAX_FS_rw_pb_oe, |
| 65 | .dout = ETRAX_FS_rw_pb_dout, |
| 66 | .din = ETRAX_FS_r_pb_din, |
| 67 | }, |
| 68 | { |
| 69 | .label = "C", |
| 70 | .ngpio = 18, |
| 71 | .oe = ETRAX_FS_rw_pc_oe, |
| 72 | .dout = ETRAX_FS_rw_pc_dout, |
| 73 | .din = ETRAX_FS_r_pc_din, |
| 74 | }, |
| 75 | { |
| 76 | .label = "D", |
| 77 | .ngpio = 18, |
| 78 | .oe = ETRAX_FS_rw_pd_oe, |
| 79 | .dout = ETRAX_FS_rw_pd_dout, |
| 80 | .din = ETRAX_FS_r_pd_din, |
| 81 | }, |
| 82 | { |
| 83 | .label = "E", |
| 84 | .ngpio = 18, |
| 85 | .oe = ETRAX_FS_rw_pe_oe, |
| 86 | .dout = ETRAX_FS_rw_pe_dout, |
| 87 | .din = ETRAX_FS_r_pe_din, |
| 88 | }, |
| 89 | }; |
| 90 | |
| 91 | static const struct etraxfs_gpio_info etraxfs_gpio_etraxfs = { |
| 92 | .num_ports = ARRAY_SIZE(etraxfs_gpio_etraxfs_ports), |
| 93 | .ports = etraxfs_gpio_etraxfs_ports, |
| 94 | }; |
| 95 | |
Rabin Vincent | d705073 | 2015-07-22 15:05:19 +0200 | [diff] [blame^] | 96 | static const struct etraxfs_gpio_port etraxfs_gpio_artpec3_ports[] = { |
| 97 | { |
| 98 | .label = "A", |
| 99 | .ngpio = 32, |
| 100 | .oe = ARTPEC3_rw_pa_oe, |
| 101 | .dout = ARTPEC3_rw_pa_dout, |
| 102 | .din = ARTPEC3_r_pa_din, |
| 103 | }, |
| 104 | { |
| 105 | .label = "B", |
| 106 | .ngpio = 32, |
| 107 | .oe = ARTPEC3_rw_pb_oe, |
| 108 | .dout = ARTPEC3_rw_pb_dout, |
| 109 | .din = ARTPEC3_r_pb_din, |
| 110 | }, |
| 111 | { |
| 112 | .label = "C", |
| 113 | .ngpio = 16, |
| 114 | .oe = ARTPEC3_rw_pc_oe, |
| 115 | .dout = ARTPEC3_rw_pc_dout, |
| 116 | .din = ARTPEC3_r_pc_din, |
| 117 | }, |
| 118 | { |
| 119 | .label = "D", |
| 120 | .ngpio = 32, |
| 121 | .din = ARTPEC3_r_pd_din, |
| 122 | }, |
| 123 | }; |
| 124 | |
| 125 | static const struct etraxfs_gpio_info etraxfs_gpio_artpec3 = { |
| 126 | .num_ports = ARRAY_SIZE(etraxfs_gpio_artpec3_ports), |
| 127 | .ports = etraxfs_gpio_artpec3_ports, |
| 128 | }; |
| 129 | |
Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 130 | static int etraxfs_gpio_of_xlate(struct gpio_chip *gc, |
| 131 | const struct of_phandle_args *gpiospec, |
| 132 | u32 *flags) |
| 133 | { |
| 134 | /* |
| 135 | * Port numbers are A to E, and the properties are integers, so we |
| 136 | * specify them as 0xA - 0xE. |
| 137 | */ |
| 138 | if (gc->label[0] - 'A' + 0xA != gpiospec->args[2]) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | return of_gpio_simple_xlate(gc, gpiospec, flags); |
| 142 | } |
| 143 | |
| 144 | static const struct of_device_id etraxfs_gpio_of_table[] = { |
| 145 | { |
| 146 | .compatible = "axis,etraxfs-gio", |
| 147 | .data = &etraxfs_gpio_etraxfs, |
| 148 | }, |
Rabin Vincent | d705073 | 2015-07-22 15:05:19 +0200 | [diff] [blame^] | 149 | { |
| 150 | .compatible = "axis,artpec3-gio", |
| 151 | .data = &etraxfs_gpio_artpec3, |
| 152 | }, |
Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 153 | {}, |
| 154 | }; |
| 155 | |
| 156 | static int etraxfs_gpio_probe(struct platform_device *pdev) |
| 157 | { |
| 158 | struct device *dev = &pdev->dev; |
| 159 | const struct etraxfs_gpio_info *info; |
| 160 | const struct of_device_id *match; |
| 161 | struct bgpio_chip *chips; |
| 162 | struct resource *res; |
| 163 | void __iomem *regs; |
| 164 | int ret; |
| 165 | int i; |
| 166 | |
| 167 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 168 | regs = devm_ioremap_resource(dev, res); |
Krzysztof Kozlowski | 0154031 | 2015-07-09 22:19:53 +0900 | [diff] [blame] | 169 | if (IS_ERR(regs)) |
| 170 | return PTR_ERR(regs); |
Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 171 | |
| 172 | match = of_match_node(etraxfs_gpio_of_table, dev->of_node); |
| 173 | if (!match) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | info = match->data; |
| 177 | |
| 178 | chips = devm_kzalloc(dev, sizeof(*chips) * info->num_ports, GFP_KERNEL); |
| 179 | if (!chips) |
| 180 | return -ENOMEM; |
| 181 | |
| 182 | for (i = 0; i < info->num_ports; i++) { |
| 183 | struct bgpio_chip *bgc = &chips[i]; |
| 184 | const struct etraxfs_gpio_port *port = &info->ports[i]; |
Rabin Vincent | d705073 | 2015-07-22 15:05:19 +0200 | [diff] [blame^] | 185 | unsigned long flags = BGPIOF_READ_OUTPUT_REG_SET; |
| 186 | void __iomem *dat = regs + port->din; |
| 187 | void __iomem *set = regs + port->dout; |
| 188 | void __iomem *dirout = regs + port->oe; |
| 189 | |
| 190 | if (dirout == set) { |
| 191 | dirout = set = NULL; |
| 192 | flags = BGPIOF_NO_OUTPUT; |
| 193 | } |
Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 194 | |
| 195 | ret = bgpio_init(bgc, dev, 4, |
Rabin Vincent | d705073 | 2015-07-22 15:05:19 +0200 | [diff] [blame^] | 196 | dat, set, NULL, dirout, NULL, |
| 197 | flags); |
Rabin Vincent | d342571 | 2015-06-06 22:30:40 +0200 | [diff] [blame] | 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | bgc->gc.ngpio = port->ngpio; |
| 202 | bgc->gc.label = port->label; |
| 203 | |
| 204 | bgc->gc.of_node = dev->of_node; |
| 205 | bgc->gc.of_gpio_n_cells = 3; |
| 206 | bgc->gc.of_xlate = etraxfs_gpio_of_xlate; |
| 207 | |
| 208 | ret = gpiochip_add(&bgc->gc); |
| 209 | if (ret) |
| 210 | dev_err(dev, "Unable to register port %s\n", |
| 211 | bgc->gc.label); |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static struct platform_driver etraxfs_gpio_driver = { |
| 218 | .driver = { |
| 219 | .name = "etraxfs-gpio", |
| 220 | .of_match_table = of_match_ptr(etraxfs_gpio_of_table), |
| 221 | }, |
| 222 | .probe = etraxfs_gpio_probe, |
| 223 | }; |
| 224 | |
| 225 | static int __init etraxfs_gpio_init(void) |
| 226 | { |
| 227 | return platform_driver_register(&etraxfs_gpio_driver); |
| 228 | } |
| 229 | |
| 230 | device_initcall(etraxfs_gpio_init); |