0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/err.h>
0010 #include <linux/gpio/driver.h>
0011 #include <linux/io.h>
0012 #include <linux/init.h>
0013 #include <linux/of.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/types.h>
0016
0017
0018 #define NUM_OF_GPIO 4
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 struct spear_spics {
0043 void __iomem *base;
0044 u32 perip_cfg;
0045 u32 sw_enable_bit;
0046 u32 cs_value_bit;
0047 u32 cs_enable_mask;
0048 u32 cs_enable_shift;
0049 unsigned long use_count;
0050 int last_off;
0051 struct gpio_chip chip;
0052 };
0053
0054
0055 static int spics_get_value(struct gpio_chip *chip, unsigned offset)
0056 {
0057 return -ENXIO;
0058 }
0059
0060 static void spics_set_value(struct gpio_chip *chip, unsigned offset, int value)
0061 {
0062 struct spear_spics *spics = gpiochip_get_data(chip);
0063 u32 tmp;
0064
0065
0066 tmp = readl_relaxed(spics->base + spics->perip_cfg);
0067 if (spics->last_off != offset) {
0068 spics->last_off = offset;
0069 tmp &= ~(spics->cs_enable_mask << spics->cs_enable_shift);
0070 tmp |= offset << spics->cs_enable_shift;
0071 }
0072
0073
0074 tmp &= ~(0x1 << spics->cs_value_bit);
0075 tmp |= value << spics->cs_value_bit;
0076 writel_relaxed(tmp, spics->base + spics->perip_cfg);
0077 }
0078
0079 static int spics_direction_input(struct gpio_chip *chip, unsigned offset)
0080 {
0081 return -ENXIO;
0082 }
0083
0084 static int spics_direction_output(struct gpio_chip *chip, unsigned offset,
0085 int value)
0086 {
0087 spics_set_value(chip, offset, value);
0088 return 0;
0089 }
0090
0091 static int spics_request(struct gpio_chip *chip, unsigned offset)
0092 {
0093 struct spear_spics *spics = gpiochip_get_data(chip);
0094 u32 tmp;
0095
0096 if (!spics->use_count++) {
0097 tmp = readl_relaxed(spics->base + spics->perip_cfg);
0098 tmp |= 0x1 << spics->sw_enable_bit;
0099 tmp |= 0x1 << spics->cs_value_bit;
0100 writel_relaxed(tmp, spics->base + spics->perip_cfg);
0101 }
0102
0103 return 0;
0104 }
0105
0106 static void spics_free(struct gpio_chip *chip, unsigned offset)
0107 {
0108 struct spear_spics *spics = gpiochip_get_data(chip);
0109 u32 tmp;
0110
0111 if (!--spics->use_count) {
0112 tmp = readl_relaxed(spics->base + spics->perip_cfg);
0113 tmp &= ~(0x1 << spics->sw_enable_bit);
0114 writel_relaxed(tmp, spics->base + spics->perip_cfg);
0115 }
0116 }
0117
0118 static int spics_gpio_probe(struct platform_device *pdev)
0119 {
0120 struct device_node *np = pdev->dev.of_node;
0121 struct spear_spics *spics;
0122
0123 spics = devm_kzalloc(&pdev->dev, sizeof(*spics), GFP_KERNEL);
0124 if (!spics)
0125 return -ENOMEM;
0126
0127 spics->base = devm_platform_ioremap_resource(pdev, 0);
0128 if (IS_ERR(spics->base))
0129 return PTR_ERR(spics->base);
0130
0131 if (of_property_read_u32(np, "st-spics,peripcfg-reg",
0132 &spics->perip_cfg))
0133 goto err_dt_data;
0134 if (of_property_read_u32(np, "st-spics,sw-enable-bit",
0135 &spics->sw_enable_bit))
0136 goto err_dt_data;
0137 if (of_property_read_u32(np, "st-spics,cs-value-bit",
0138 &spics->cs_value_bit))
0139 goto err_dt_data;
0140 if (of_property_read_u32(np, "st-spics,cs-enable-mask",
0141 &spics->cs_enable_mask))
0142 goto err_dt_data;
0143 if (of_property_read_u32(np, "st-spics,cs-enable-shift",
0144 &spics->cs_enable_shift))
0145 goto err_dt_data;
0146
0147 spics->chip.ngpio = NUM_OF_GPIO;
0148 spics->chip.base = -1;
0149 spics->chip.request = spics_request;
0150 spics->chip.free = spics_free;
0151 spics->chip.direction_input = spics_direction_input;
0152 spics->chip.direction_output = spics_direction_output;
0153 spics->chip.get = spics_get_value;
0154 spics->chip.set = spics_set_value;
0155 spics->chip.label = dev_name(&pdev->dev);
0156 spics->chip.parent = &pdev->dev;
0157 spics->chip.owner = THIS_MODULE;
0158 spics->last_off = -1;
0159
0160 return devm_gpiochip_add_data(&pdev->dev, &spics->chip, spics);
0161
0162 err_dt_data:
0163 dev_err(&pdev->dev, "DT probe failed\n");
0164 return -EINVAL;
0165 }
0166
0167 static const struct of_device_id spics_gpio_of_match[] = {
0168 { .compatible = "st,spear-spics-gpio" },
0169 {}
0170 };
0171
0172 static struct platform_driver spics_gpio_driver = {
0173 .probe = spics_gpio_probe,
0174 .driver = {
0175 .name = "spear-spics-gpio",
0176 .of_match_table = spics_gpio_of_match,
0177 },
0178 };
0179
0180 static int __init spics_gpio_init(void)
0181 {
0182 return platform_driver_register(&spics_gpio_driver);
0183 }
0184 subsys_initcall(spics_gpio_init);