1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Copyright (C) 2018 BayLibre SAS 4 // Author: Bartosz Golaszewski <bgolaszewski@baylibre.com> 5 // 6 // GPIO driver for MAXIM 77650/77651 charger/power-supply. 7 8 #include <linux/gpio/driver.h> 9 #include <linux/i2c.h> 10 #include <linux/mfd/max77650.h> 11 #include <linux/module.h> 12 #include <linux/platform_device.h> 13 #include <linux/regmap.h> 14 15 #define MAX77650_GPIO_DIR_MASK BIT(0) 16 #define MAX77650_GPIO_INVAL_MASK BIT(1) 17 #define MAX77650_GPIO_DRV_MASK BIT(2) 18 #define MAX77650_GPIO_OUTVAL_MASK BIT(3) 19 #define MAX77650_GPIO_DEBOUNCE_MASK BIT(4) 20 21 #define MAX77650_GPIO_DIR_OUT 0x00 22 #define MAX77650_GPIO_DIR_IN BIT(0) 23 #define MAX77650_GPIO_OUT_LOW 0x00 24 #define MAX77650_GPIO_OUT_HIGH BIT(3) 25 #define MAX77650_GPIO_DRV_OPEN_DRAIN 0x00 26 #define MAX77650_GPIO_DRV_PUSH_PULL BIT(2) 27 #define MAX77650_GPIO_DEBOUNCE BIT(4) 28 29 #define MAX77650_GPIO_DIR_BITS(_reg) \ 30 ((_reg) & MAX77650_GPIO_DIR_MASK) 31 #define MAX77650_GPIO_INVAL_BITS(_reg) \ 32 (((_reg) & MAX77650_GPIO_INVAL_MASK) >> 1) 33 34 struct max77650_gpio_chip { 35 struct regmap *map; 36 struct gpio_chip gc; 37 int irq; 38 }; 39 40 static int max77650_gpio_direction_input(struct gpio_chip *gc, 41 unsigned int offset) 42 { 43 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 44 45 return regmap_update_bits(chip->map, 46 MAX77650_REG_CNFG_GPIO, 47 MAX77650_GPIO_DIR_MASK, 48 MAX77650_GPIO_DIR_IN); 49 } 50 51 static int max77650_gpio_direction_output(struct gpio_chip *gc, 52 unsigned int offset, int value) 53 { 54 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 55 int mask, regval; 56 57 mask = MAX77650_GPIO_DIR_MASK | MAX77650_GPIO_OUTVAL_MASK; 58 regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW; 59 regval |= MAX77650_GPIO_DIR_OUT; 60 61 return regmap_update_bits(chip->map, 62 MAX77650_REG_CNFG_GPIO, mask, regval); 63 } 64 65 static int max77650_gpio_set_value(struct gpio_chip *gc, 66 unsigned int offset, int value) 67 { 68 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 69 int regval; 70 71 regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW; 72 73 return regmap_update_bits(chip->map, MAX77650_REG_CNFG_GPIO, 74 MAX77650_GPIO_OUTVAL_MASK, regval); 75 } 76 77 static int max77650_gpio_get_value(struct gpio_chip *gc, 78 unsigned int offset) 79 { 80 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 81 unsigned int val; 82 int rv; 83 84 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val); 85 if (rv) 86 return rv; 87 88 return MAX77650_GPIO_INVAL_BITS(val); 89 } 90 91 static int max77650_gpio_get_direction(struct gpio_chip *gc, 92 unsigned int offset) 93 { 94 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 95 unsigned int val; 96 int rv; 97 98 rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val); 99 if (rv) 100 return rv; 101 102 return MAX77650_GPIO_DIR_BITS(val); 103 } 104 105 static int max77650_gpio_set_config(struct gpio_chip *gc, 106 unsigned int offset, unsigned long cfg) 107 { 108 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 109 110 switch (pinconf_to_config_param(cfg)) { 111 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 112 return regmap_update_bits(chip->map, 113 MAX77650_REG_CNFG_GPIO, 114 MAX77650_GPIO_DRV_MASK, 115 MAX77650_GPIO_DRV_OPEN_DRAIN); 116 case PIN_CONFIG_DRIVE_PUSH_PULL: 117 return regmap_update_bits(chip->map, 118 MAX77650_REG_CNFG_GPIO, 119 MAX77650_GPIO_DRV_MASK, 120 MAX77650_GPIO_DRV_PUSH_PULL); 121 case PIN_CONFIG_INPUT_DEBOUNCE: 122 return regmap_update_bits(chip->map, 123 MAX77650_REG_CNFG_GPIO, 124 MAX77650_GPIO_DEBOUNCE_MASK, 125 MAX77650_GPIO_DEBOUNCE); 126 default: 127 return -ENOTSUPP; 128 } 129 } 130 131 static int max77650_gpio_to_irq(struct gpio_chip *gc, unsigned int offset) 132 { 133 struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 134 135 return chip->irq; 136 } 137 138 static int max77650_gpio_probe(struct platform_device *pdev) 139 { 140 struct max77650_gpio_chip *chip; 141 struct device *dev, *parent; 142 struct i2c_client *i2c; 143 144 dev = &pdev->dev; 145 parent = dev->parent; 146 i2c = to_i2c_client(parent); 147 148 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 149 if (!chip) 150 return -ENOMEM; 151 152 chip->map = dev_get_regmap(parent, NULL); 153 if (!chip->map) 154 return -ENODEV; 155 156 chip->irq = platform_get_irq_byname(pdev, "GPI"); 157 if (chip->irq < 0) 158 return chip->irq; 159 160 chip->gc.base = -1; 161 chip->gc.ngpio = 1; 162 chip->gc.label = i2c->name; 163 chip->gc.parent = dev; 164 chip->gc.owner = THIS_MODULE; 165 chip->gc.can_sleep = true; 166 167 chip->gc.direction_input = max77650_gpio_direction_input; 168 chip->gc.direction_output = max77650_gpio_direction_output; 169 chip->gc.set_rv = max77650_gpio_set_value; 170 chip->gc.get = max77650_gpio_get_value; 171 chip->gc.get_direction = max77650_gpio_get_direction; 172 chip->gc.set_config = max77650_gpio_set_config; 173 chip->gc.to_irq = max77650_gpio_to_irq; 174 175 return devm_gpiochip_add_data(dev, &chip->gc, chip); 176 } 177 178 static struct platform_driver max77650_gpio_driver = { 179 .driver = { 180 .name = "max77650-gpio", 181 }, 182 .probe = max77650_gpio_probe, 183 }; 184 module_platform_driver(max77650_gpio_driver); 185 186 MODULE_DESCRIPTION("MAXIM 77650/77651 GPIO driver"); 187 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>"); 188 MODULE_LICENSE("GPL v2"); 189 MODULE_ALIAS("platform:max77650-gpio"); 190