1aca58a66SDavid Daney /*
2aca58a66SDavid Daney * This file is subject to the terms and conditions of the GNU General Public
3aca58a66SDavid Daney * License. See the file "COPYING" in the main directory of this archive
4aca58a66SDavid Daney * for more details.
5aca58a66SDavid Daney *
6aca58a66SDavid Daney * Copyright (C) 2011, 2012 Cavium Inc.
7aca58a66SDavid Daney */
8aca58a66SDavid Daney
9aca58a66SDavid Daney #include <linux/platform_device.h>
10aca58a66SDavid Daney #include <linux/kernel.h>
11aca58a66SDavid Daney #include <linux/module.h>
12be1f20a4SLinus Walleij #include <linux/gpio/driver.h>
13aca58a66SDavid Daney #include <linux/io.h>
14aca58a66SDavid Daney
15aca58a66SDavid Daney #include <asm/octeon/octeon.h>
16aca58a66SDavid Daney #include <asm/octeon/cvmx-gpio-defs.h>
17aca58a66SDavid Daney
18aca58a66SDavid Daney #define RX_DAT 0x80
19aca58a66SDavid Daney #define TX_SET 0x88
20aca58a66SDavid Daney #define TX_CLEAR 0x90
21aca58a66SDavid Daney /*
22aca58a66SDavid Daney * The address offset of the GPIO configuration register for a given
23aca58a66SDavid Daney * line.
24aca58a66SDavid Daney */
bit_cfg_reg(unsigned int offset)25aca58a66SDavid Daney static unsigned int bit_cfg_reg(unsigned int offset)
26aca58a66SDavid Daney {
27aca58a66SDavid Daney /*
28aca58a66SDavid Daney * The register stride is 8, with a discontinuity after the
29aca58a66SDavid Daney * first 16.
30aca58a66SDavid Daney */
31aca58a66SDavid Daney if (offset < 16)
32aca58a66SDavid Daney return 8 * offset;
33aca58a66SDavid Daney else
34aca58a66SDavid Daney return 8 * (offset - 16) + 0x100;
35aca58a66SDavid Daney }
36aca58a66SDavid Daney
37aca58a66SDavid Daney struct octeon_gpio {
38aca58a66SDavid Daney struct gpio_chip chip;
39aca58a66SDavid Daney u64 register_base;
40aca58a66SDavid Daney };
41aca58a66SDavid Daney
octeon_gpio_dir_in(struct gpio_chip * chip,unsigned offset)42aca58a66SDavid Daney static int octeon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
43aca58a66SDavid Daney {
44f5cc554eSLinus Walleij struct octeon_gpio *gpio = gpiochip_get_data(chip);
45aca58a66SDavid Daney
46aca58a66SDavid Daney cvmx_write_csr(gpio->register_base + bit_cfg_reg(offset), 0);
47aca58a66SDavid Daney return 0;
48aca58a66SDavid Daney }
49aca58a66SDavid Daney
octeon_gpio_set(struct gpio_chip * chip,unsigned offset,int value)50aca58a66SDavid Daney static void octeon_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
51aca58a66SDavid Daney {
52f5cc554eSLinus Walleij struct octeon_gpio *gpio = gpiochip_get_data(chip);
53aca58a66SDavid Daney u64 mask = 1ull << offset;
54aca58a66SDavid Daney u64 reg = gpio->register_base + (value ? TX_SET : TX_CLEAR);
55aca58a66SDavid Daney cvmx_write_csr(reg, mask);
56aca58a66SDavid Daney }
57aca58a66SDavid Daney
octeon_gpio_dir_out(struct gpio_chip * chip,unsigned offset,int value)58aca58a66SDavid Daney static int octeon_gpio_dir_out(struct gpio_chip *chip, unsigned offset,
59aca58a66SDavid Daney int value)
60aca58a66SDavid Daney {
61f5cc554eSLinus Walleij struct octeon_gpio *gpio = gpiochip_get_data(chip);
62aca58a66SDavid Daney union cvmx_gpio_bit_cfgx cfgx;
63aca58a66SDavid Daney
64aca58a66SDavid Daney octeon_gpio_set(chip, offset, value);
65aca58a66SDavid Daney
66aca58a66SDavid Daney cfgx.u64 = 0;
67aca58a66SDavid Daney cfgx.s.tx_oe = 1;
68aca58a66SDavid Daney
69aca58a66SDavid Daney cvmx_write_csr(gpio->register_base + bit_cfg_reg(offset), cfgx.u64);
70aca58a66SDavid Daney return 0;
71aca58a66SDavid Daney }
72aca58a66SDavid Daney
octeon_gpio_get(struct gpio_chip * chip,unsigned offset)73aca58a66SDavid Daney static int octeon_gpio_get(struct gpio_chip *chip, unsigned offset)
74aca58a66SDavid Daney {
75f5cc554eSLinus Walleij struct octeon_gpio *gpio = gpiochip_get_data(chip);
76aca58a66SDavid Daney u64 read_bits = cvmx_read_csr(gpio->register_base + RX_DAT);
77aca58a66SDavid Daney
78aca58a66SDavid Daney return ((1ull << offset) & read_bits) != 0;
79aca58a66SDavid Daney }
80aca58a66SDavid Daney
octeon_gpio_probe(struct platform_device * pdev)81aca58a66SDavid Daney static int octeon_gpio_probe(struct platform_device *pdev)
82aca58a66SDavid Daney {
83aca58a66SDavid Daney struct octeon_gpio *gpio;
84aca58a66SDavid Daney struct gpio_chip *chip;
85592569deSAxel Lin void __iomem *reg_base;
86aca58a66SDavid Daney int err = 0;
87aca58a66SDavid Daney
88aca58a66SDavid Daney gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
89aca58a66SDavid Daney if (!gpio)
90aca58a66SDavid Daney return -ENOMEM;
91aca58a66SDavid Daney chip = &gpio->chip;
92aca58a66SDavid Daney
93*037ae5bcSEnrico Weigelt, metux IT consult reg_base = devm_platform_ioremap_resource(pdev, 0);
94592569deSAxel Lin if (IS_ERR(reg_base))
95592569deSAxel Lin return PTR_ERR(reg_base);
96aca58a66SDavid Daney
97592569deSAxel Lin gpio->register_base = (u64)reg_base;
98aca58a66SDavid Daney pdev->dev.platform_data = chip;
99aca58a66SDavid Daney chip->label = "octeon-gpio";
10058383c78SLinus Walleij chip->parent = &pdev->dev;
101aca58a66SDavid Daney chip->owner = THIS_MODULE;
102aca58a66SDavid Daney chip->base = 0;
1039fb1f39eSLinus Walleij chip->can_sleep = false;
104aca58a66SDavid Daney chip->ngpio = 20;
105aca58a66SDavid Daney chip->direction_input = octeon_gpio_dir_in;
106aca58a66SDavid Daney chip->get = octeon_gpio_get;
107aca58a66SDavid Daney chip->direction_output = octeon_gpio_dir_out;
108aca58a66SDavid Daney chip->set = octeon_gpio_set;
1091533d4fdSLaxman Dewangan err = devm_gpiochip_add_data(&pdev->dev, chip, gpio);
110aca58a66SDavid Daney if (err)
111592569deSAxel Lin return err;
112aca58a66SDavid Daney
113aca58a66SDavid Daney dev_info(&pdev->dev, "OCTEON GPIO driver probed.\n");
114592569deSAxel Lin return 0;
115aca58a66SDavid Daney }
116aca58a66SDavid Daney
117b6d055b1SAxel Lin static const struct of_device_id octeon_gpio_match[] = {
118aca58a66SDavid Daney {
119aca58a66SDavid Daney .compatible = "cavium,octeon-3860-gpio",
120aca58a66SDavid Daney },
121aca58a66SDavid Daney {},
122aca58a66SDavid Daney };
123aca58a66SDavid Daney MODULE_DEVICE_TABLE(of, octeon_gpio_match);
124aca58a66SDavid Daney
125aca58a66SDavid Daney static struct platform_driver octeon_gpio_driver = {
126aca58a66SDavid Daney .driver = {
127aca58a66SDavid Daney .name = "octeon_gpio",
128aca58a66SDavid Daney .of_match_table = octeon_gpio_match,
129aca58a66SDavid Daney },
130aca58a66SDavid Daney .probe = octeon_gpio_probe,
131aca58a66SDavid Daney };
132aca58a66SDavid Daney
133aca58a66SDavid Daney module_platform_driver(octeon_gpio_driver);
134aca58a66SDavid Daney
135aca58a66SDavid Daney MODULE_DESCRIPTION("Cavium Inc. OCTEON GPIO Driver");
136aca58a66SDavid Daney MODULE_AUTHOR("David Daney");
137aca58a66SDavid Daney MODULE_LICENSE("GPL");
138