Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 1 | /* |
| 2 | * pinmux driver for CSR SiRFprimaII |
| 3 | * |
Paul Gortmaker | a9784e5 | 2016-08-23 17:19:44 -0400 | [diff] [blame] | 4 | * Authors: |
| 5 | * Rongjun Ying <rongjun.ying@csr.com> |
| 6 | * Yuping Luo <yuping.luo@csr.com> |
| 7 | * Barry Song <baohua.song@csr.com> |
| 8 | * |
Barry Song | 019c12f | 2014-02-12 21:54:47 +0800 | [diff] [blame] | 9 | * Copyright (c) 2011 - 2014 Cambridge Silicon Radio Limited, a CSR plc group |
| 10 | * company. |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 11 | * |
| 12 | * Licensed under GPLv2 or later. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 16 | #include <linux/irq.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/err.h> |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 21 | #include <linux/pinctrl/pinctrl.h> |
| 22 | #include <linux/pinctrl/pinmux.h> |
| 23 | #include <linux/pinctrl/consumer.h> |
| 24 | #include <linux/pinctrl/machine.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_address.h> |
| 27 | #include <linux/of_device.h> |
| 28 | #include <linux/of_platform.h> |
| 29 | #include <linux/bitops.h> |
| 30 | #include <linux/gpio.h> |
| 31 | #include <linux/of_gpio.h> |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 32 | |
| 33 | #include "pinctrl-sirf.h" |
| 34 | |
| 35 | #define DRIVER_NAME "pinmux-sirf" |
| 36 | |
| 37 | struct sirfsoc_gpio_bank { |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 38 | int id; |
| 39 | int parent_irq; |
| 40 | spinlock_t lock; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 41 | }; |
| 42 | |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 43 | struct sirfsoc_gpio_chip { |
| 44 | struct of_mm_gpio_chip chip; |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 45 | struct sirfsoc_gpio_bank sgpio_bank[SIRFSOC_GPIO_NO_OF_BANKS]; |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 46 | spinlock_t lock; |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 47 | }; |
| 48 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 49 | static struct sirfsoc_pin_group *sirfsoc_pin_groups; |
| 50 | static int sirfsoc_pingrp_cnt; |
| 51 | |
| 52 | static int sirfsoc_get_groups_count(struct pinctrl_dev *pctldev) |
| 53 | { |
| 54 | return sirfsoc_pingrp_cnt; |
| 55 | } |
| 56 | |
| 57 | static const char *sirfsoc_get_group_name(struct pinctrl_dev *pctldev, |
| 58 | unsigned selector) |
| 59 | { |
| 60 | return sirfsoc_pin_groups[selector].name; |
| 61 | } |
| 62 | |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 63 | static int sirfsoc_get_group_pins(struct pinctrl_dev *pctldev, |
| 64 | unsigned selector, |
| 65 | const unsigned **pins, |
| 66 | unsigned *num_pins) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 67 | { |
| 68 | *pins = sirfsoc_pin_groups[selector].pins; |
| 69 | *num_pins = sirfsoc_pin_groups[selector].num_pins; |
| 70 | return 0; |
| 71 | } |
| 72 | |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 73 | static void sirfsoc_pin_dbg_show(struct pinctrl_dev *pctldev, |
| 74 | struct seq_file *s, unsigned offset) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 75 | { |
| 76 | seq_printf(s, " " DRIVER_NAME); |
| 77 | } |
| 78 | |
| 79 | static int sirfsoc_dt_node_to_map(struct pinctrl_dev *pctldev, |
| 80 | struct device_node *np_config, |
| 81 | struct pinctrl_map **map, unsigned *num_maps) |
| 82 | { |
| 83 | struct sirfsoc_pmx *spmx = pinctrl_dev_get_drvdata(pctldev); |
| 84 | struct device_node *np; |
| 85 | struct property *prop; |
| 86 | const char *function, *group; |
| 87 | int ret, index = 0, count = 0; |
| 88 | |
| 89 | /* calculate number of maps required */ |
| 90 | for_each_child_of_node(np_config, np) { |
| 91 | ret = of_property_read_string(np, "sirf,function", &function); |
Julia Lawall | 2d98023c | 2015-12-21 17:39:45 +0100 | [diff] [blame] | 92 | if (ret < 0) { |
| 93 | of_node_put(np); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 94 | return ret; |
Julia Lawall | 2d98023c | 2015-12-21 17:39:45 +0100 | [diff] [blame] | 95 | } |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 96 | |
| 97 | ret = of_property_count_strings(np, "sirf,pins"); |
Julia Lawall | 2d98023c | 2015-12-21 17:39:45 +0100 | [diff] [blame] | 98 | if (ret < 0) { |
| 99 | of_node_put(np); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 100 | return ret; |
Julia Lawall | 2d98023c | 2015-12-21 17:39:45 +0100 | [diff] [blame] | 101 | } |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 102 | |
| 103 | count += ret; |
| 104 | } |
| 105 | |
| 106 | if (!count) { |
| 107 | dev_err(spmx->dev, "No child nodes passed via DT\n"); |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | |
| 111 | *map = kzalloc(sizeof(**map) * count, GFP_KERNEL); |
| 112 | if (!*map) |
| 113 | return -ENOMEM; |
| 114 | |
| 115 | for_each_child_of_node(np_config, np) { |
| 116 | of_property_read_string(np, "sirf,function", &function); |
| 117 | of_property_for_each_string(np, "sirf,pins", prop, group) { |
| 118 | (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP; |
| 119 | (*map)[index].data.mux.group = group; |
| 120 | (*map)[index].data.mux.function = function; |
| 121 | index++; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | *num_maps = count; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static void sirfsoc_dt_free_map(struct pinctrl_dev *pctldev, |
| 131 | struct pinctrl_map *map, unsigned num_maps) |
| 132 | { |
| 133 | kfree(map); |
| 134 | } |
| 135 | |
| 136 | static struct pinctrl_ops sirfsoc_pctrl_ops = { |
| 137 | .get_groups_count = sirfsoc_get_groups_count, |
| 138 | .get_group_name = sirfsoc_get_group_name, |
| 139 | .get_group_pins = sirfsoc_get_group_pins, |
| 140 | .pin_dbg_show = sirfsoc_pin_dbg_show, |
| 141 | .dt_node_to_map = sirfsoc_dt_node_to_map, |
| 142 | .dt_free_map = sirfsoc_dt_free_map, |
| 143 | }; |
| 144 | |
| 145 | static struct sirfsoc_pmx_func *sirfsoc_pmx_functions; |
| 146 | static int sirfsoc_pmxfunc_cnt; |
| 147 | |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 148 | static void sirfsoc_pinmux_endisable(struct sirfsoc_pmx *spmx, |
| 149 | unsigned selector, bool enable) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 150 | { |
| 151 | int i; |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 152 | const struct sirfsoc_padmux *mux = |
| 153 | sirfsoc_pmx_functions[selector].padmux; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 154 | const struct sirfsoc_muxmask *mask = mux->muxmask; |
| 155 | |
| 156 | for (i = 0; i < mux->muxmask_counts; i++) { |
| 157 | u32 muxval; |
Barry Song | a17272a | 2015-01-11 21:56:41 +0800 | [diff] [blame] | 158 | muxval = readl(spmx->gpio_virtbase + |
| 159 | SIRFSOC_GPIO_PAD_EN(mask[i].group)); |
| 160 | if (enable) |
| 161 | muxval = muxval & ~mask[i].mask; |
| 162 | else |
| 163 | muxval = muxval | mask[i].mask; |
| 164 | writel(muxval, spmx->gpio_virtbase + |
| 165 | SIRFSOC_GPIO_PAD_EN(mask[i].group)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (mux->funcmask && enable) { |
| 169 | u32 func_en_val; |
Rong Wang | 6a08a92 | 2013-09-29 22:27:59 +0800 | [diff] [blame] | 170 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 171 | func_en_val = |
Rong Wang | 6a08a92 | 2013-09-29 22:27:59 +0800 | [diff] [blame] | 172 | readl(spmx->rsc_virtbase + mux->ctrlreg); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 173 | func_en_val = |
Rong Wang | 6a08a92 | 2013-09-29 22:27:59 +0800 | [diff] [blame] | 174 | (func_en_val & ~mux->funcmask) | (mux->funcval); |
| 175 | writel(func_en_val, spmx->rsc_virtbase + mux->ctrlreg); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 179 | static int sirfsoc_pinmux_set_mux(struct pinctrl_dev *pmxdev, |
| 180 | unsigned selector, |
| 181 | unsigned group) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 182 | { |
| 183 | struct sirfsoc_pmx *spmx; |
| 184 | |
| 185 | spmx = pinctrl_dev_get_drvdata(pmxdev); |
| 186 | sirfsoc_pinmux_endisable(spmx, selector, true); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 191 | static int sirfsoc_pinmux_get_funcs_count(struct pinctrl_dev *pmxdev) |
| 192 | { |
| 193 | return sirfsoc_pmxfunc_cnt; |
| 194 | } |
| 195 | |
| 196 | static const char *sirfsoc_pinmux_get_func_name(struct pinctrl_dev *pctldev, |
| 197 | unsigned selector) |
| 198 | { |
| 199 | return sirfsoc_pmx_functions[selector].name; |
| 200 | } |
| 201 | |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 202 | static int sirfsoc_pinmux_get_groups(struct pinctrl_dev *pctldev, |
| 203 | unsigned selector, |
| 204 | const char * const **groups, |
| 205 | unsigned * const num_groups) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 206 | { |
| 207 | *groups = sirfsoc_pmx_functions[selector].groups; |
| 208 | *num_groups = sirfsoc_pmx_functions[selector].num_groups; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int sirfsoc_pinmux_request_gpio(struct pinctrl_dev *pmxdev, |
| 213 | struct pinctrl_gpio_range *range, unsigned offset) |
| 214 | { |
| 215 | struct sirfsoc_pmx *spmx; |
| 216 | |
| 217 | int group = range->id; |
| 218 | |
| 219 | u32 muxval; |
| 220 | |
| 221 | spmx = pinctrl_dev_get_drvdata(pmxdev); |
| 222 | |
Barry Song | a17272a | 2015-01-11 21:56:41 +0800 | [diff] [blame] | 223 | muxval = readl(spmx->gpio_virtbase + |
| 224 | SIRFSOC_GPIO_PAD_EN(group)); |
| 225 | muxval = muxval | (1 << (offset - range->pin_base)); |
| 226 | writel(muxval, spmx->gpio_virtbase + |
| 227 | SIRFSOC_GPIO_PAD_EN(group)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static struct pinmux_ops sirfsoc_pinmux_ops = { |
Linus Walleij | 03e9f0c | 2014-09-03 13:02:56 +0200 | [diff] [blame] | 233 | .set_mux = sirfsoc_pinmux_set_mux, |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 234 | .get_functions_count = sirfsoc_pinmux_get_funcs_count, |
| 235 | .get_function_name = sirfsoc_pinmux_get_func_name, |
| 236 | .get_function_groups = sirfsoc_pinmux_get_groups, |
| 237 | .gpio_request_enable = sirfsoc_pinmux_request_gpio, |
| 238 | }; |
| 239 | |
| 240 | static struct pinctrl_desc sirfsoc_pinmux_desc = { |
| 241 | .name = DRIVER_NAME, |
| 242 | .pctlops = &sirfsoc_pctrl_ops, |
| 243 | .pmxops = &sirfsoc_pinmux_ops, |
| 244 | .owner = THIS_MODULE, |
| 245 | }; |
| 246 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 247 | static void __iomem *sirfsoc_rsc_of_iomap(void) |
| 248 | { |
| 249 | const struct of_device_id rsc_ids[] = { |
| 250 | { .compatible = "sirf,prima2-rsc" }, |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 251 | {} |
| 252 | }; |
| 253 | struct device_node *np; |
| 254 | |
| 255 | np = of_find_matching_node(NULL, rsc_ids); |
| 256 | if (!np) |
| 257 | panic("unable to find compatible rsc node in dtb\n"); |
| 258 | |
| 259 | return of_iomap(np, 0); |
| 260 | } |
| 261 | |
| 262 | static int sirfsoc_gpio_of_xlate(struct gpio_chip *gc, |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 263 | const struct of_phandle_args *gpiospec, |
| 264 | u32 *flags) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 265 | { |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 266 | if (gpiospec->args[0] > SIRFSOC_GPIO_NO_OF_BANKS * SIRFSOC_GPIO_BANK_SIZE) |
Rongjun Ying | 9c95690 | 2013-07-04 15:55:28 +0800 | [diff] [blame] | 267 | return -EINVAL; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 268 | |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 269 | if (flags) |
Rongjun Ying | 9c95690 | 2013-07-04 15:55:28 +0800 | [diff] [blame] | 270 | *flags = gpiospec->args[1]; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 271 | |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 272 | return gpiospec->args[0]; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static const struct of_device_id pinmux_ids[] = { |
| 276 | { .compatible = "sirf,prima2-pinctrl", .data = &prima2_pinctrl_data, }, |
| 277 | { .compatible = "sirf,atlas6-pinctrl", .data = &atlas6_pinctrl_data, }, |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 278 | {} |
| 279 | }; |
| 280 | |
| 281 | static int sirfsoc_pinmux_probe(struct platform_device *pdev) |
| 282 | { |
| 283 | int ret; |
| 284 | struct sirfsoc_pmx *spmx; |
| 285 | struct device_node *np = pdev->dev.of_node; |
| 286 | const struct sirfsoc_pinctrl_data *pdata; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 287 | |
| 288 | /* Create state holders etc for this driver */ |
| 289 | spmx = devm_kzalloc(&pdev->dev, sizeof(*spmx), GFP_KERNEL); |
| 290 | if (!spmx) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | spmx->dev = &pdev->dev; |
| 294 | |
| 295 | platform_set_drvdata(pdev, spmx); |
| 296 | |
| 297 | spmx->gpio_virtbase = of_iomap(np, 0); |
| 298 | if (!spmx->gpio_virtbase) { |
| 299 | dev_err(&pdev->dev, "can't map gpio registers\n"); |
| 300 | return -ENOMEM; |
| 301 | } |
| 302 | |
| 303 | spmx->rsc_virtbase = sirfsoc_rsc_of_iomap(); |
| 304 | if (!spmx->rsc_virtbase) { |
| 305 | ret = -ENOMEM; |
| 306 | dev_err(&pdev->dev, "can't map rsc registers\n"); |
| 307 | goto out_no_rsc_remap; |
| 308 | } |
| 309 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 310 | pdata = of_match_node(pinmux_ids, np)->data; |
| 311 | sirfsoc_pin_groups = pdata->grps; |
| 312 | sirfsoc_pingrp_cnt = pdata->grps_cnt; |
| 313 | sirfsoc_pmx_functions = pdata->funcs; |
| 314 | sirfsoc_pmxfunc_cnt = pdata->funcs_cnt; |
| 315 | sirfsoc_pinmux_desc.pins = pdata->pads; |
| 316 | sirfsoc_pinmux_desc.npins = pdata->pads_cnt; |
| 317 | |
| 318 | |
| 319 | /* Now register the pin controller and all pins it handles */ |
| 320 | spmx->pmx = pinctrl_register(&sirfsoc_pinmux_desc, &pdev->dev, spmx); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 321 | if (IS_ERR(spmx->pmx)) { |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 322 | dev_err(&pdev->dev, "could not register SIRFSOC pinmux driver\n"); |
Masahiro Yamada | 323de9e | 2015-06-09 13:01:16 +0900 | [diff] [blame] | 323 | ret = PTR_ERR(spmx->pmx); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 324 | goto out_no_pmx; |
| 325 | } |
| 326 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 327 | dev_info(&pdev->dev, "initialized SIRFSOC pinmux driver\n"); |
| 328 | |
| 329 | return 0; |
| 330 | |
| 331 | out_no_pmx: |
| 332 | iounmap(spmx->rsc_virtbase); |
| 333 | out_no_rsc_remap: |
| 334 | iounmap(spmx->gpio_virtbase); |
| 335 | return ret; |
| 336 | } |
| 337 | |
Barry Song | bc8d25a | 2013-05-16 11:17:09 +0800 | [diff] [blame] | 338 | #ifdef CONFIG_PM_SLEEP |
| 339 | static int sirfsoc_pinmux_suspend_noirq(struct device *dev) |
| 340 | { |
| 341 | int i, j; |
| 342 | struct sirfsoc_pmx *spmx = dev_get_drvdata(dev); |
| 343 | |
| 344 | for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) { |
| 345 | for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) { |
| 346 | spmx->gpio_regs[i][j] = readl(spmx->gpio_virtbase + |
| 347 | SIRFSOC_GPIO_CTRL(i, j)); |
| 348 | } |
| 349 | spmx->ints_regs[i] = readl(spmx->gpio_virtbase + |
| 350 | SIRFSOC_GPIO_INT_STATUS(i)); |
| 351 | spmx->paden_regs[i] = readl(spmx->gpio_virtbase + |
| 352 | SIRFSOC_GPIO_PAD_EN(i)); |
| 353 | } |
| 354 | spmx->dspen_regs = readl(spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0); |
| 355 | |
| 356 | for (i = 0; i < 3; i++) |
| 357 | spmx->rsc_regs[i] = readl(spmx->rsc_virtbase + 4 * i); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static int sirfsoc_pinmux_resume_noirq(struct device *dev) |
| 363 | { |
| 364 | int i, j; |
| 365 | struct sirfsoc_pmx *spmx = dev_get_drvdata(dev); |
| 366 | |
| 367 | for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) { |
| 368 | for (j = 0; j < SIRFSOC_GPIO_BANK_SIZE; j++) { |
| 369 | writel(spmx->gpio_regs[i][j], spmx->gpio_virtbase + |
| 370 | SIRFSOC_GPIO_CTRL(i, j)); |
| 371 | } |
| 372 | writel(spmx->ints_regs[i], spmx->gpio_virtbase + |
| 373 | SIRFSOC_GPIO_INT_STATUS(i)); |
| 374 | writel(spmx->paden_regs[i], spmx->gpio_virtbase + |
| 375 | SIRFSOC_GPIO_PAD_EN(i)); |
| 376 | } |
| 377 | writel(spmx->dspen_regs, spmx->gpio_virtbase + SIRFSOC_GPIO_DSP_EN0); |
| 378 | |
| 379 | for (i = 0; i < 3; i++) |
| 380 | writel(spmx->rsc_regs[i], spmx->rsc_virtbase + 4 * i); |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | static const struct dev_pm_ops sirfsoc_pinmux_pm_ops = { |
| 386 | .suspend_noirq = sirfsoc_pinmux_suspend_noirq, |
| 387 | .resume_noirq = sirfsoc_pinmux_resume_noirq, |
Barry Song | f6b1788 | 2013-07-04 15:59:53 +0800 | [diff] [blame] | 388 | .freeze_noirq = sirfsoc_pinmux_suspend_noirq, |
| 389 | .restore_noirq = sirfsoc_pinmux_resume_noirq, |
Barry Song | bc8d25a | 2013-05-16 11:17:09 +0800 | [diff] [blame] | 390 | }; |
| 391 | #endif |
| 392 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 393 | static struct platform_driver sirfsoc_pinmux_driver = { |
| 394 | .driver = { |
| 395 | .name = DRIVER_NAME, |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 396 | .of_match_table = pinmux_ids, |
Barry Song | bc8d25a | 2013-05-16 11:17:09 +0800 | [diff] [blame] | 397 | #ifdef CONFIG_PM_SLEEP |
| 398 | .pm = &sirfsoc_pinmux_pm_ops, |
| 399 | #endif |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 400 | }, |
| 401 | .probe = sirfsoc_pinmux_probe, |
| 402 | }; |
| 403 | |
| 404 | static int __init sirfsoc_pinmux_init(void) |
| 405 | { |
| 406 | return platform_driver_register(&sirfsoc_pinmux_driver); |
| 407 | } |
| 408 | arch_initcall(sirfsoc_pinmux_init); |
| 409 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 410 | static inline struct sirfsoc_gpio_bank * |
| 411 | sirfsoc_gpio_to_bank(struct sirfsoc_gpio_chip *sgpio, unsigned int offset) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 412 | { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 413 | return &sgpio->sgpio_bank[offset / SIRFSOC_GPIO_BANK_SIZE]; |
| 414 | } |
| 415 | |
| 416 | static inline int sirfsoc_gpio_to_bankoff(unsigned int offset) |
| 417 | { |
| 418 | return offset % SIRFSOC_GPIO_BANK_SIZE; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 419 | } |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 420 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 421 | static void sirfsoc_gpio_irq_ack(struct irq_data *d) |
| 422 | { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 423 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 424 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 425 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq); |
| 426 | int idx = sirfsoc_gpio_to_bankoff(d->hwirq); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 427 | u32 val, offset; |
| 428 | unsigned long flags; |
| 429 | |
| 430 | offset = SIRFSOC_GPIO_CTRL(bank->id, idx); |
| 431 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 432 | spin_lock_irqsave(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 433 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 434 | val = readl(sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 435 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 436 | writel(val, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 437 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 438 | spin_unlock_irqrestore(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 439 | } |
| 440 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 441 | static void __sirfsoc_gpio_irq_mask(struct sirfsoc_gpio_chip *sgpio, |
| 442 | struct sirfsoc_gpio_bank *bank, |
| 443 | int idx) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 444 | { |
| 445 | u32 val, offset; |
| 446 | unsigned long flags; |
| 447 | |
| 448 | offset = SIRFSOC_GPIO_CTRL(bank->id, idx); |
| 449 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 450 | spin_lock_irqsave(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 451 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 452 | val = readl(sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 453 | val &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK; |
| 454 | val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 455 | writel(val, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 456 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 457 | spin_unlock_irqrestore(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void sirfsoc_gpio_irq_mask(struct irq_data *d) |
| 461 | { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 462 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 463 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 464 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 465 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 466 | __sirfsoc_gpio_irq_mask(sgpio, bank, d->hwirq % SIRFSOC_GPIO_BANK_SIZE); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static void sirfsoc_gpio_irq_unmask(struct irq_data *d) |
| 470 | { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 471 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 472 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 473 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq); |
| 474 | int idx = sirfsoc_gpio_to_bankoff(d->hwirq); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 475 | u32 val, offset; |
| 476 | unsigned long flags; |
| 477 | |
| 478 | offset = SIRFSOC_GPIO_CTRL(bank->id, idx); |
| 479 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 480 | spin_lock_irqsave(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 481 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 482 | val = readl(sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 483 | val &= ~SIRFSOC_GPIO_CTL_INTR_STS_MASK; |
| 484 | val |= SIRFSOC_GPIO_CTL_INTR_EN_MASK; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 485 | writel(val, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 486 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 487 | spin_unlock_irqrestore(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static int sirfsoc_gpio_irq_type(struct irq_data *d, unsigned type) |
| 491 | { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 492 | struct gpio_chip *gc = irq_data_get_irq_chip_data(d); |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 493 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 494 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, d->hwirq); |
| 495 | int idx = sirfsoc_gpio_to_bankoff(d->hwirq); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 496 | u32 val, offset; |
| 497 | unsigned long flags; |
| 498 | |
| 499 | offset = SIRFSOC_GPIO_CTRL(bank->id, idx); |
| 500 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 501 | spin_lock_irqsave(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 502 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 503 | val = readl(sgpio->chip.regs + offset); |
Barry Song | b07ddcd | 2014-01-11 16:48:43 +0800 | [diff] [blame] | 504 | val &= ~(SIRFSOC_GPIO_CTL_INTR_STS_MASK | SIRFSOC_GPIO_CTL_OUT_EN_MASK); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 505 | |
| 506 | switch (type) { |
| 507 | case IRQ_TYPE_NONE: |
| 508 | break; |
| 509 | case IRQ_TYPE_EDGE_RISING: |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 510 | val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | |
| 511 | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 512 | val &= ~SIRFSOC_GPIO_CTL_INTR_LOW_MASK; |
| 513 | break; |
| 514 | case IRQ_TYPE_EDGE_FALLING: |
| 515 | val &= ~SIRFSOC_GPIO_CTL_INTR_HIGH_MASK; |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 516 | val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK | |
| 517 | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 518 | break; |
| 519 | case IRQ_TYPE_EDGE_BOTH: |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 520 | val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | |
| 521 | SIRFSOC_GPIO_CTL_INTR_LOW_MASK | |
| 522 | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 523 | break; |
| 524 | case IRQ_TYPE_LEVEL_LOW: |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 525 | val &= ~(SIRFSOC_GPIO_CTL_INTR_HIGH_MASK | |
| 526 | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 527 | val |= SIRFSOC_GPIO_CTL_INTR_LOW_MASK; |
| 528 | break; |
| 529 | case IRQ_TYPE_LEVEL_HIGH: |
| 530 | val |= SIRFSOC_GPIO_CTL_INTR_HIGH_MASK; |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 531 | val &= ~(SIRFSOC_GPIO_CTL_INTR_LOW_MASK | |
| 532 | SIRFSOC_GPIO_CTL_INTR_TYPE_MASK); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 536 | writel(val, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 537 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 538 | spin_unlock_irqrestore(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static struct irq_chip sirfsoc_irq_chip = { |
| 544 | .name = "sirf-gpio-irq", |
| 545 | .irq_ack = sirfsoc_gpio_irq_ack, |
| 546 | .irq_mask = sirfsoc_gpio_irq_mask, |
| 547 | .irq_unmask = sirfsoc_gpio_irq_unmask, |
| 548 | .irq_set_type = sirfsoc_gpio_irq_type, |
| 549 | }; |
| 550 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 551 | static void sirfsoc_gpio_handle_irq(struct irq_desc *desc) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 552 | { |
Thomas Gleixner | 3b0d156 | 2015-07-13 01:54:35 +0200 | [diff] [blame] | 553 | unsigned int irq = irq_desc_get_irq(desc); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 554 | struct gpio_chip *gc = irq_desc_get_handler_data(desc); |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 555 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(gc); |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 556 | struct sirfsoc_gpio_bank *bank; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 557 | u32 status, ctrl; |
| 558 | int idx = 0; |
Jiang Liu | 5663bb2 | 2015-06-04 12:13:16 +0800 | [diff] [blame] | 559 | struct irq_chip *chip = irq_desc_get_chip(desc); |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 560 | int i; |
| 561 | |
Barry Song | 648e42e | 2014-05-25 16:54:23 +0800 | [diff] [blame] | 562 | for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) { |
Linus Walleij | 29c7f1f | 2014-05-30 09:52:43 +0200 | [diff] [blame] | 563 | bank = &sgpio->sgpio_bank[i]; |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 564 | if (bank->parent_irq == irq) |
| 565 | break; |
| 566 | } |
Barry Song | 648e42e | 2014-05-25 16:54:23 +0800 | [diff] [blame] | 567 | BUG_ON(i == SIRFSOC_GPIO_NO_OF_BANKS); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 568 | |
| 569 | chained_irq_enter(chip, desc); |
| 570 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 571 | status = readl(sgpio->chip.regs + SIRFSOC_GPIO_INT_STATUS(bank->id)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 572 | if (!status) { |
| 573 | printk(KERN_WARNING |
Colin Ian King | 28b30c3 | 2015-02-28 20:46:24 +0000 | [diff] [blame] | 574 | "%s: gpio id %d status %#x no interrupt is flagged\n", |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 575 | __func__, bank->id, status); |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 576 | handle_bad_irq(desc); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | |
| 580 | while (status) { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 581 | ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, idx)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * Here we must check whether the corresponding GPIO's interrupt |
| 585 | * has been enabled, otherwise just skip it |
| 586 | */ |
| 587 | if ((status & 0x1) && (ctrl & SIRFSOC_GPIO_CTL_INTR_EN_MASK)) { |
| 588 | pr_debug("%s: gpio id %d idx %d happens\n", |
| 589 | __func__, bank->id, idx); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 590 | generic_handle_irq(irq_find_mapping(gc->irqdomain, idx + |
Barry Song | 8daeffb | 2014-01-11 16:48:42 +0800 | [diff] [blame] | 591 | bank->id * SIRFSOC_GPIO_BANK_SIZE)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | idx++; |
| 595 | status = status >> 1; |
| 596 | } |
| 597 | |
| 598 | chained_irq_exit(chip, desc); |
| 599 | } |
| 600 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 601 | static inline void sirfsoc_gpio_set_input(struct sirfsoc_gpio_chip *sgpio, |
| 602 | unsigned ctrl_offset) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 603 | { |
| 604 | u32 val; |
| 605 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 606 | val = readl(sgpio->chip.regs + ctrl_offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 607 | val &= ~SIRFSOC_GPIO_CTL_OUT_EN_MASK; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 608 | writel(val, sgpio->chip.regs + ctrl_offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static int sirfsoc_gpio_request(struct gpio_chip *chip, unsigned offset) |
| 612 | { |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 613 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 614 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 615 | unsigned long flags; |
| 616 | |
| 617 | if (pinctrl_request_gpio(chip->base + offset)) |
| 618 | return -ENODEV; |
| 619 | |
| 620 | spin_lock_irqsave(&bank->lock, flags); |
| 621 | |
| 622 | /* |
| 623 | * default status: |
| 624 | * set direction as input and mask irq |
| 625 | */ |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 626 | sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset)); |
| 627 | __sirfsoc_gpio_irq_mask(sgpio, bank, offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 628 | |
| 629 | spin_unlock_irqrestore(&bank->lock, flags); |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static void sirfsoc_gpio_free(struct gpio_chip *chip, unsigned offset) |
| 635 | { |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 636 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 637 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 638 | unsigned long flags; |
| 639 | |
| 640 | spin_lock_irqsave(&bank->lock, flags); |
| 641 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 642 | __sirfsoc_gpio_irq_mask(sgpio, bank, offset); |
| 643 | sirfsoc_gpio_set_input(sgpio, SIRFSOC_GPIO_CTRL(bank->id, offset)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 644 | |
| 645 | spin_unlock_irqrestore(&bank->lock, flags); |
| 646 | |
| 647 | pinctrl_free_gpio(chip->base + offset); |
| 648 | } |
| 649 | |
| 650 | static int sirfsoc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio) |
| 651 | { |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 652 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 653 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio); |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 654 | int idx = sirfsoc_gpio_to_bankoff(gpio); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 655 | unsigned long flags; |
| 656 | unsigned offset; |
| 657 | |
| 658 | offset = SIRFSOC_GPIO_CTRL(bank->id, idx); |
| 659 | |
| 660 | spin_lock_irqsave(&bank->lock, flags); |
| 661 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 662 | sirfsoc_gpio_set_input(sgpio, offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 663 | |
| 664 | spin_unlock_irqrestore(&bank->lock, flags); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 669 | static inline void sirfsoc_gpio_set_output(struct sirfsoc_gpio_chip *sgpio, |
| 670 | struct sirfsoc_gpio_bank *bank, |
| 671 | unsigned offset, |
| 672 | int value) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 673 | { |
| 674 | u32 out_ctrl; |
| 675 | unsigned long flags; |
| 676 | |
| 677 | spin_lock_irqsave(&bank->lock, flags); |
| 678 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 679 | out_ctrl = readl(sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 680 | if (value) |
| 681 | out_ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK; |
| 682 | else |
| 683 | out_ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK; |
| 684 | |
| 685 | out_ctrl &= ~SIRFSOC_GPIO_CTL_INTR_EN_MASK; |
| 686 | out_ctrl |= SIRFSOC_GPIO_CTL_OUT_EN_MASK; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 687 | writel(out_ctrl, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 688 | |
| 689 | spin_unlock_irqrestore(&bank->lock, flags); |
| 690 | } |
| 691 | |
Bin Shi | c09f80d | 2014-08-18 16:49:21 +0800 | [diff] [blame] | 692 | static int sirfsoc_gpio_direction_output(struct gpio_chip *chip, |
| 693 | unsigned gpio, int value) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 694 | { |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 695 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 696 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, gpio); |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 697 | int idx = sirfsoc_gpio_to_bankoff(gpio); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 698 | u32 offset; |
| 699 | unsigned long flags; |
| 700 | |
| 701 | offset = SIRFSOC_GPIO_CTRL(bank->id, idx); |
| 702 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 703 | spin_lock_irqsave(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 704 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 705 | sirfsoc_gpio_set_output(sgpio, bank, offset, value); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 706 | |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 707 | spin_unlock_irqrestore(&sgpio->lock, flags); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int sirfsoc_gpio_get_value(struct gpio_chip *chip, unsigned offset) |
| 713 | { |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 714 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 715 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 716 | u32 val; |
| 717 | unsigned long flags; |
| 718 | |
| 719 | spin_lock_irqsave(&bank->lock, flags); |
| 720 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 721 | val = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 722 | |
| 723 | spin_unlock_irqrestore(&bank->lock, flags); |
| 724 | |
| 725 | return !!(val & SIRFSOC_GPIO_CTL_DATAIN_MASK); |
| 726 | } |
| 727 | |
| 728 | static void sirfsoc_gpio_set_value(struct gpio_chip *chip, unsigned offset, |
| 729 | int value) |
| 730 | { |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 731 | struct sirfsoc_gpio_chip *sgpio = gpiochip_get_data(chip); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 732 | struct sirfsoc_gpio_bank *bank = sirfsoc_gpio_to_bank(sgpio, offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 733 | u32 ctrl; |
| 734 | unsigned long flags; |
| 735 | |
| 736 | spin_lock_irqsave(&bank->lock, flags); |
| 737 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 738 | ctrl = readl(sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 739 | if (value) |
| 740 | ctrl |= SIRFSOC_GPIO_CTL_DATAOUT_MASK; |
| 741 | else |
| 742 | ctrl &= ~SIRFSOC_GPIO_CTL_DATAOUT_MASK; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 743 | writel(ctrl, sgpio->chip.regs + SIRFSOC_GPIO_CTRL(bank->id, offset)); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 744 | |
| 745 | spin_unlock_irqrestore(&bank->lock, flags); |
| 746 | } |
| 747 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 748 | static void sirfsoc_gpio_set_pullup(struct sirfsoc_gpio_chip *sgpio, |
| 749 | const u32 *pullups) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 750 | { |
| 751 | int i, n; |
| 752 | const unsigned long *p = (const unsigned long *)pullups; |
| 753 | |
| 754 | for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) { |
| 755 | for_each_set_bit(n, p + i, BITS_PER_LONG) { |
| 756 | u32 offset = SIRFSOC_GPIO_CTRL(i, n); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 757 | u32 val = readl(sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 758 | val |= SIRFSOC_GPIO_CTL_PULL_MASK; |
| 759 | val |= SIRFSOC_GPIO_CTL_PULL_HIGH; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 760 | writel(val, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 765 | static void sirfsoc_gpio_set_pulldown(struct sirfsoc_gpio_chip *sgpio, |
| 766 | const u32 *pulldowns) |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 767 | { |
| 768 | int i, n; |
| 769 | const unsigned long *p = (const unsigned long *)pulldowns; |
| 770 | |
| 771 | for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) { |
| 772 | for_each_set_bit(n, p + i, BITS_PER_LONG) { |
| 773 | u32 offset = SIRFSOC_GPIO_CTRL(i, n); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 774 | u32 val = readl(sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 775 | val |= SIRFSOC_GPIO_CTL_PULL_MASK; |
| 776 | val &= ~SIRFSOC_GPIO_CTL_PULL_HIGH; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 777 | writel(val, sgpio->chip.regs + offset); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | static int sirfsoc_gpio_probe(struct device_node *np) |
| 783 | { |
| 784 | int i, err = 0; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 785 | static struct sirfsoc_gpio_chip *sgpio; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 786 | struct sirfsoc_gpio_bank *bank; |
Jingoo Han | 2c9fdcf | 2013-08-06 18:14:12 +0900 | [diff] [blame] | 787 | void __iomem *regs; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 788 | struct platform_device *pdev; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 789 | |
| 790 | u32 pullups[SIRFSOC_GPIO_NO_OF_BANKS], pulldowns[SIRFSOC_GPIO_NO_OF_BANKS]; |
| 791 | |
| 792 | pdev = of_find_device_by_node(np); |
| 793 | if (!pdev) |
| 794 | return -ENODEV; |
| 795 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 796 | sgpio = devm_kzalloc(&pdev->dev, sizeof(*sgpio), GFP_KERNEL); |
| 797 | if (!sgpio) |
| 798 | return -ENOMEM; |
Linus Walleij | 1dfe0d1 | 2014-04-23 23:13:01 +0200 | [diff] [blame] | 799 | spin_lock_init(&sgpio->lock); |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 800 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 801 | regs = of_iomap(np, 0); |
| 802 | if (!regs) |
| 803 | return -ENOMEM; |
| 804 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 805 | sgpio->chip.gc.request = sirfsoc_gpio_request; |
| 806 | sgpio->chip.gc.free = sirfsoc_gpio_free; |
| 807 | sgpio->chip.gc.direction_input = sirfsoc_gpio_direction_input; |
| 808 | sgpio->chip.gc.get = sirfsoc_gpio_get_value; |
| 809 | sgpio->chip.gc.direction_output = sirfsoc_gpio_direction_output; |
| 810 | sgpio->chip.gc.set = sirfsoc_gpio_set_value; |
| 811 | sgpio->chip.gc.base = 0; |
| 812 | sgpio->chip.gc.ngpio = SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS; |
| 813 | sgpio->chip.gc.label = kstrdup(np->full_name, GFP_KERNEL); |
| 814 | sgpio->chip.gc.of_node = np; |
| 815 | sgpio->chip.gc.of_xlate = sirfsoc_gpio_of_xlate; |
| 816 | sgpio->chip.gc.of_gpio_n_cells = 2; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 817 | sgpio->chip.gc.parent = &pdev->dev; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 818 | sgpio->chip.regs = regs; |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 819 | |
Linus Walleij | 192d350 | 2015-12-08 10:29:35 +0100 | [diff] [blame] | 820 | err = gpiochip_add_data(&sgpio->chip.gc, sgpio); |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 821 | if (err) { |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 822 | dev_err(&pdev->dev, "%s: error in probe function with status %d\n", |
Barry Song | c5eb757 | 2014-04-15 14:43:46 +0800 | [diff] [blame] | 823 | np->full_name, err); |
| 824 | goto out; |
| 825 | } |
| 826 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 827 | err = gpiochip_irqchip_add(&sgpio->chip.gc, |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 828 | &sirfsoc_irq_chip, |
| 829 | 0, handle_level_irq, |
| 830 | IRQ_TYPE_NONE); |
| 831 | if (err) { |
| 832 | dev_err(&pdev->dev, |
| 833 | "could not connect irqchip to gpiochip\n"); |
Pramod Gurav | 0a5d667 | 2014-08-30 16:43:00 +0530 | [diff] [blame] | 834 | goto out_banks; |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 835 | } |
| 836 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 837 | for (i = 0; i < SIRFSOC_GPIO_NO_OF_BANKS; i++) { |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 838 | bank = &sgpio->sgpio_bank[i]; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 839 | spin_lock_init(&bank->lock); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 840 | bank->parent_irq = platform_get_irq(pdev, i); |
| 841 | if (bank->parent_irq < 0) { |
| 842 | err = bank->parent_irq; |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 843 | goto out_banks; |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 844 | } |
| 845 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 846 | gpiochip_set_chained_irqchip(&sgpio->chip.gc, |
Linus Walleij | 7420d2d | 2014-04-15 14:43:47 +0800 | [diff] [blame] | 847 | &sirfsoc_irq_chip, |
| 848 | bank->parent_irq, |
| 849 | sirfsoc_gpio_handle_irq); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 850 | } |
| 851 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 852 | err = gpiochip_add_pin_range(&sgpio->chip.gc, dev_name(&pdev->dev), |
| 853 | 0, 0, SIRFSOC_GPIO_BANK_SIZE * SIRFSOC_GPIO_NO_OF_BANKS); |
| 854 | if (err) { |
| 855 | dev_err(&pdev->dev, |
| 856 | "could not add gpiochip pin range\n"); |
| 857 | goto out_no_range; |
| 858 | } |
| 859 | |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 860 | if (!of_property_read_u32_array(np, "sirf,pullups", pullups, |
| 861 | SIRFSOC_GPIO_NO_OF_BANKS)) |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 862 | sirfsoc_gpio_set_pullup(sgpio, pullups); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 863 | |
| 864 | if (!of_property_read_u32_array(np, "sirf,pulldowns", pulldowns, |
| 865 | SIRFSOC_GPIO_NO_OF_BANKS)) |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 866 | sirfsoc_gpio_set_pulldown(sgpio, pulldowns); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 867 | |
| 868 | return 0; |
| 869 | |
Linus Walleij | 294d135 | 2014-04-23 23:08:02 +0200 | [diff] [blame] | 870 | out_no_range: |
| 871 | out_banks: |
Linus Walleij | 2fcea6c | 2014-09-16 15:05:41 -0700 | [diff] [blame] | 872 | gpiochip_remove(&sgpio->chip.gc); |
Barry Song | 3370dc9 | 2013-05-14 22:17:58 +0800 | [diff] [blame] | 873 | out: |
| 874 | iounmap(regs); |
| 875 | return err; |
| 876 | } |
| 877 | |
| 878 | static int __init sirfsoc_gpio_init(void) |
| 879 | { |
| 880 | |
| 881 | struct device_node *np; |
| 882 | |
| 883 | np = of_find_matching_node(NULL, pinmux_ids); |
| 884 | |
| 885 | if (!np) |
| 886 | return -ENODEV; |
| 887 | |
| 888 | return sirfsoc_gpio_probe(np); |
| 889 | } |
| 890 | subsys_initcall(sirfsoc_gpio_init); |