0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/gpio/driver.h>
0009 #include <linux/i2c.h>
0010 #include <linux/mfd/max77650.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regmap.h>
0014
0015 #define MAX77650_GPIO_DIR_MASK BIT(0)
0016 #define MAX77650_GPIO_INVAL_MASK BIT(1)
0017 #define MAX77650_GPIO_DRV_MASK BIT(2)
0018 #define MAX77650_GPIO_OUTVAL_MASK BIT(3)
0019 #define MAX77650_GPIO_DEBOUNCE_MASK BIT(4)
0020
0021 #define MAX77650_GPIO_DIR_OUT 0x00
0022 #define MAX77650_GPIO_DIR_IN BIT(0)
0023 #define MAX77650_GPIO_OUT_LOW 0x00
0024 #define MAX77650_GPIO_OUT_HIGH BIT(3)
0025 #define MAX77650_GPIO_DRV_OPEN_DRAIN 0x00
0026 #define MAX77650_GPIO_DRV_PUSH_PULL BIT(2)
0027 #define MAX77650_GPIO_DEBOUNCE BIT(4)
0028
0029 #define MAX77650_GPIO_DIR_BITS(_reg) \
0030 ((_reg) & MAX77650_GPIO_DIR_MASK)
0031 #define MAX77650_GPIO_INVAL_BITS(_reg) \
0032 (((_reg) & MAX77650_GPIO_INVAL_MASK) >> 1)
0033
0034 struct max77650_gpio_chip {
0035 struct regmap *map;
0036 struct gpio_chip gc;
0037 int irq;
0038 };
0039
0040 static int max77650_gpio_direction_input(struct gpio_chip *gc,
0041 unsigned int offset)
0042 {
0043 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0044
0045 return regmap_update_bits(chip->map,
0046 MAX77650_REG_CNFG_GPIO,
0047 MAX77650_GPIO_DIR_MASK,
0048 MAX77650_GPIO_DIR_IN);
0049 }
0050
0051 static int max77650_gpio_direction_output(struct gpio_chip *gc,
0052 unsigned int offset, int value)
0053 {
0054 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0055 int mask, regval;
0056
0057 mask = MAX77650_GPIO_DIR_MASK | MAX77650_GPIO_OUTVAL_MASK;
0058 regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW;
0059 regval |= MAX77650_GPIO_DIR_OUT;
0060
0061 return regmap_update_bits(chip->map,
0062 MAX77650_REG_CNFG_GPIO, mask, regval);
0063 }
0064
0065 static void max77650_gpio_set_value(struct gpio_chip *gc,
0066 unsigned int offset, int value)
0067 {
0068 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0069 int rv, regval;
0070
0071 regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW;
0072
0073 rv = regmap_update_bits(chip->map, MAX77650_REG_CNFG_GPIO,
0074 MAX77650_GPIO_OUTVAL_MASK, regval);
0075 if (rv)
0076 dev_err(gc->parent, "cannot set GPIO value: %d\n", rv);
0077 }
0078
0079 static int max77650_gpio_get_value(struct gpio_chip *gc,
0080 unsigned int offset)
0081 {
0082 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0083 unsigned int val;
0084 int rv;
0085
0086 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
0087 if (rv)
0088 return rv;
0089
0090 return MAX77650_GPIO_INVAL_BITS(val);
0091 }
0092
0093 static int max77650_gpio_get_direction(struct gpio_chip *gc,
0094 unsigned int offset)
0095 {
0096 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0097 unsigned int val;
0098 int rv;
0099
0100 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val);
0101 if (rv)
0102 return rv;
0103
0104 return MAX77650_GPIO_DIR_BITS(val);
0105 }
0106
0107 static int max77650_gpio_set_config(struct gpio_chip *gc,
0108 unsigned int offset, unsigned long cfg)
0109 {
0110 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0111
0112 switch (pinconf_to_config_param(cfg)) {
0113 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0114 return regmap_update_bits(chip->map,
0115 MAX77650_REG_CNFG_GPIO,
0116 MAX77650_GPIO_DRV_MASK,
0117 MAX77650_GPIO_DRV_OPEN_DRAIN);
0118 case PIN_CONFIG_DRIVE_PUSH_PULL:
0119 return regmap_update_bits(chip->map,
0120 MAX77650_REG_CNFG_GPIO,
0121 MAX77650_GPIO_DRV_MASK,
0122 MAX77650_GPIO_DRV_PUSH_PULL);
0123 case PIN_CONFIG_INPUT_DEBOUNCE:
0124 return regmap_update_bits(chip->map,
0125 MAX77650_REG_CNFG_GPIO,
0126 MAX77650_GPIO_DEBOUNCE_MASK,
0127 MAX77650_GPIO_DEBOUNCE);
0128 default:
0129 return -ENOTSUPP;
0130 }
0131 }
0132
0133 static int max77650_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
0134 {
0135 struct max77650_gpio_chip *chip = gpiochip_get_data(gc);
0136
0137 return chip->irq;
0138 }
0139
0140 static int max77650_gpio_probe(struct platform_device *pdev)
0141 {
0142 struct max77650_gpio_chip *chip;
0143 struct device *dev, *parent;
0144 struct i2c_client *i2c;
0145
0146 dev = &pdev->dev;
0147 parent = dev->parent;
0148 i2c = to_i2c_client(parent);
0149
0150 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
0151 if (!chip)
0152 return -ENOMEM;
0153
0154 chip->map = dev_get_regmap(parent, NULL);
0155 if (!chip->map)
0156 return -ENODEV;
0157
0158 chip->irq = platform_get_irq_byname(pdev, "GPI");
0159 if (chip->irq < 0)
0160 return chip->irq;
0161
0162 chip->gc.base = -1;
0163 chip->gc.ngpio = 1;
0164 chip->gc.label = i2c->name;
0165 chip->gc.parent = dev;
0166 chip->gc.owner = THIS_MODULE;
0167 chip->gc.can_sleep = true;
0168
0169 chip->gc.direction_input = max77650_gpio_direction_input;
0170 chip->gc.direction_output = max77650_gpio_direction_output;
0171 chip->gc.set = max77650_gpio_set_value;
0172 chip->gc.get = max77650_gpio_get_value;
0173 chip->gc.get_direction = max77650_gpio_get_direction;
0174 chip->gc.set_config = max77650_gpio_set_config;
0175 chip->gc.to_irq = max77650_gpio_to_irq;
0176
0177 return devm_gpiochip_add_data(dev, &chip->gc, chip);
0178 }
0179
0180 static struct platform_driver max77650_gpio_driver = {
0181 .driver = {
0182 .name = "max77650-gpio",
0183 },
0184 .probe = max77650_gpio_probe,
0185 };
0186 module_platform_driver(max77650_gpio_driver);
0187
0188 MODULE_DESCRIPTION("MAXIM 77650/77651 GPIO driver");
0189 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
0190 MODULE_LICENSE("GPL v2");
0191 MODULE_ALIAS("platform:max77650-gpio");