0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/gpio/driver.h>
0011 #include <linux/mfd/altera-a10sr.h>
0012 #include <linux/module.h>
0013 #include <linux/property.h>
0014
0015
0016
0017
0018
0019
0020 struct altr_a10sr_gpio {
0021 struct gpio_chip gp;
0022 struct regmap *regmap;
0023 };
0024
0025 static int altr_a10sr_gpio_get(struct gpio_chip *chip, unsigned int offset)
0026 {
0027 struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
0028 int ret, val;
0029
0030 ret = regmap_read(gpio->regmap, ALTR_A10SR_PBDSW_REG, &val);
0031 if (ret < 0)
0032 return ret;
0033
0034 return !!(val & BIT(offset - ALTR_A10SR_LED_VALID_SHIFT));
0035 }
0036
0037 static void altr_a10sr_gpio_set(struct gpio_chip *chip, unsigned int offset,
0038 int value)
0039 {
0040 struct altr_a10sr_gpio *gpio = gpiochip_get_data(chip);
0041
0042 regmap_update_bits(gpio->regmap, ALTR_A10SR_LED_REG,
0043 BIT(ALTR_A10SR_LED_VALID_SHIFT + offset),
0044 value ? BIT(ALTR_A10SR_LED_VALID_SHIFT + offset)
0045 : 0);
0046 }
0047
0048 static int altr_a10sr_gpio_direction_input(struct gpio_chip *gc,
0049 unsigned int nr)
0050 {
0051 if (nr < (ALTR_A10SR_IN_VALID_RANGE_LO - ALTR_A10SR_LED_VALID_SHIFT))
0052 return -EINVAL;
0053
0054 return 0;
0055 }
0056
0057 static int altr_a10sr_gpio_direction_output(struct gpio_chip *gc,
0058 unsigned int nr, int value)
0059 {
0060 if (nr > (ALTR_A10SR_OUT_VALID_RANGE_HI - ALTR_A10SR_LED_VALID_SHIFT))
0061 return -EINVAL;
0062
0063 altr_a10sr_gpio_set(gc, nr, value);
0064 return 0;
0065 }
0066
0067 static const struct gpio_chip altr_a10sr_gc = {
0068 .label = "altr_a10sr_gpio",
0069 .owner = THIS_MODULE,
0070 .get = altr_a10sr_gpio_get,
0071 .set = altr_a10sr_gpio_set,
0072 .direction_input = altr_a10sr_gpio_direction_input,
0073 .direction_output = altr_a10sr_gpio_direction_output,
0074 .can_sleep = true,
0075 .ngpio = 12,
0076 .base = -1,
0077 };
0078
0079 static int altr_a10sr_gpio_probe(struct platform_device *pdev)
0080 {
0081 struct altr_a10sr_gpio *gpio;
0082 struct altr_a10sr *a10sr = dev_get_drvdata(pdev->dev.parent);
0083
0084 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
0085 if (!gpio)
0086 return -ENOMEM;
0087
0088 gpio->regmap = a10sr->regmap;
0089
0090 gpio->gp = altr_a10sr_gc;
0091 gpio->gp.parent = pdev->dev.parent;
0092 gpio->gp.fwnode = dev_fwnode(&pdev->dev);
0093
0094 return devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio);
0095 }
0096
0097 static const struct of_device_id altr_a10sr_gpio_of_match[] = {
0098 { .compatible = "altr,a10sr-gpio" },
0099 { },
0100 };
0101 MODULE_DEVICE_TABLE(of, altr_a10sr_gpio_of_match);
0102
0103 static struct platform_driver altr_a10sr_gpio_driver = {
0104 .probe = altr_a10sr_gpio_probe,
0105 .driver = {
0106 .name = "altr_a10sr_gpio",
0107 .of_match_table = of_match_ptr(altr_a10sr_gpio_of_match),
0108 },
0109 };
0110 module_platform_driver(altr_a10sr_gpio_driver);
0111
0112 MODULE_LICENSE("GPL v2");
0113 MODULE_AUTHOR("Thor Thayer <tthayer@opensource.altera.com>");
0114 MODULE_DESCRIPTION("Altera Arria10 System Resource Chip GPIO");