162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci// 362306a36Sopenharmony_ci// Copyright (C) 2018 BayLibre SAS 462306a36Sopenharmony_ci// Author: Bartosz Golaszewski <bgolaszewski@baylibre.com> 562306a36Sopenharmony_ci// 662306a36Sopenharmony_ci// GPIO driver for MAXIM 77650/77651 charger/power-supply. 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/gpio/driver.h> 962306a36Sopenharmony_ci#include <linux/i2c.h> 1062306a36Sopenharmony_ci#include <linux/mfd/max77650.h> 1162306a36Sopenharmony_ci#include <linux/module.h> 1262306a36Sopenharmony_ci#include <linux/platform_device.h> 1362306a36Sopenharmony_ci#include <linux/regmap.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#define MAX77650_GPIO_DIR_MASK BIT(0) 1662306a36Sopenharmony_ci#define MAX77650_GPIO_INVAL_MASK BIT(1) 1762306a36Sopenharmony_ci#define MAX77650_GPIO_DRV_MASK BIT(2) 1862306a36Sopenharmony_ci#define MAX77650_GPIO_OUTVAL_MASK BIT(3) 1962306a36Sopenharmony_ci#define MAX77650_GPIO_DEBOUNCE_MASK BIT(4) 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci#define MAX77650_GPIO_DIR_OUT 0x00 2262306a36Sopenharmony_ci#define MAX77650_GPIO_DIR_IN BIT(0) 2362306a36Sopenharmony_ci#define MAX77650_GPIO_OUT_LOW 0x00 2462306a36Sopenharmony_ci#define MAX77650_GPIO_OUT_HIGH BIT(3) 2562306a36Sopenharmony_ci#define MAX77650_GPIO_DRV_OPEN_DRAIN 0x00 2662306a36Sopenharmony_ci#define MAX77650_GPIO_DRV_PUSH_PULL BIT(2) 2762306a36Sopenharmony_ci#define MAX77650_GPIO_DEBOUNCE BIT(4) 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#define MAX77650_GPIO_DIR_BITS(_reg) \ 3062306a36Sopenharmony_ci ((_reg) & MAX77650_GPIO_DIR_MASK) 3162306a36Sopenharmony_ci#define MAX77650_GPIO_INVAL_BITS(_reg) \ 3262306a36Sopenharmony_ci (((_reg) & MAX77650_GPIO_INVAL_MASK) >> 1) 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_cistruct max77650_gpio_chip { 3562306a36Sopenharmony_ci struct regmap *map; 3662306a36Sopenharmony_ci struct gpio_chip gc; 3762306a36Sopenharmony_ci int irq; 3862306a36Sopenharmony_ci}; 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_cistatic int max77650_gpio_direction_input(struct gpio_chip *gc, 4162306a36Sopenharmony_ci unsigned int offset) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci return regmap_update_bits(chip->map, 4662306a36Sopenharmony_ci MAX77650_REG_CNFG_GPIO, 4762306a36Sopenharmony_ci MAX77650_GPIO_DIR_MASK, 4862306a36Sopenharmony_ci MAX77650_GPIO_DIR_IN); 4962306a36Sopenharmony_ci} 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_cistatic int max77650_gpio_direction_output(struct gpio_chip *gc, 5262306a36Sopenharmony_ci unsigned int offset, int value) 5362306a36Sopenharmony_ci{ 5462306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 5562306a36Sopenharmony_ci int mask, regval; 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci mask = MAX77650_GPIO_DIR_MASK | MAX77650_GPIO_OUTVAL_MASK; 5862306a36Sopenharmony_ci regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW; 5962306a36Sopenharmony_ci regval |= MAX77650_GPIO_DIR_OUT; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci return regmap_update_bits(chip->map, 6262306a36Sopenharmony_ci MAX77650_REG_CNFG_GPIO, mask, regval); 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_cistatic void max77650_gpio_set_value(struct gpio_chip *gc, 6662306a36Sopenharmony_ci unsigned int offset, int value) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 6962306a36Sopenharmony_ci int rv, regval; 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci regval = value ? MAX77650_GPIO_OUT_HIGH : MAX77650_GPIO_OUT_LOW; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci rv = regmap_update_bits(chip->map, MAX77650_REG_CNFG_GPIO, 7462306a36Sopenharmony_ci MAX77650_GPIO_OUTVAL_MASK, regval); 7562306a36Sopenharmony_ci if (rv) 7662306a36Sopenharmony_ci dev_err(gc->parent, "cannot set GPIO value: %d\n", rv); 7762306a36Sopenharmony_ci} 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_cistatic int max77650_gpio_get_value(struct gpio_chip *gc, 8062306a36Sopenharmony_ci unsigned int offset) 8162306a36Sopenharmony_ci{ 8262306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 8362306a36Sopenharmony_ci unsigned int val; 8462306a36Sopenharmony_ci int rv; 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val); 8762306a36Sopenharmony_ci if (rv) 8862306a36Sopenharmony_ci return rv; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci return MAX77650_GPIO_INVAL_BITS(val); 9162306a36Sopenharmony_ci} 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_cistatic int max77650_gpio_get_direction(struct gpio_chip *gc, 9462306a36Sopenharmony_ci unsigned int offset) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 9762306a36Sopenharmony_ci unsigned int val; 9862306a36Sopenharmony_ci int rv; 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci rv = regmap_read(chip->map, MAX77650_REG_CNFG_GPIO, &val); 10162306a36Sopenharmony_ci if (rv) 10262306a36Sopenharmony_ci return rv; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci return MAX77650_GPIO_DIR_BITS(val); 10562306a36Sopenharmony_ci} 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_cistatic int max77650_gpio_set_config(struct gpio_chip *gc, 10862306a36Sopenharmony_ci unsigned int offset, unsigned long cfg) 10962306a36Sopenharmony_ci{ 11062306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci switch (pinconf_to_config_param(cfg)) { 11362306a36Sopenharmony_ci case PIN_CONFIG_DRIVE_OPEN_DRAIN: 11462306a36Sopenharmony_ci return regmap_update_bits(chip->map, 11562306a36Sopenharmony_ci MAX77650_REG_CNFG_GPIO, 11662306a36Sopenharmony_ci MAX77650_GPIO_DRV_MASK, 11762306a36Sopenharmony_ci MAX77650_GPIO_DRV_OPEN_DRAIN); 11862306a36Sopenharmony_ci case PIN_CONFIG_DRIVE_PUSH_PULL: 11962306a36Sopenharmony_ci return regmap_update_bits(chip->map, 12062306a36Sopenharmony_ci MAX77650_REG_CNFG_GPIO, 12162306a36Sopenharmony_ci MAX77650_GPIO_DRV_MASK, 12262306a36Sopenharmony_ci MAX77650_GPIO_DRV_PUSH_PULL); 12362306a36Sopenharmony_ci case PIN_CONFIG_INPUT_DEBOUNCE: 12462306a36Sopenharmony_ci return regmap_update_bits(chip->map, 12562306a36Sopenharmony_ci MAX77650_REG_CNFG_GPIO, 12662306a36Sopenharmony_ci MAX77650_GPIO_DEBOUNCE_MASK, 12762306a36Sopenharmony_ci MAX77650_GPIO_DEBOUNCE); 12862306a36Sopenharmony_ci default: 12962306a36Sopenharmony_ci return -ENOTSUPP; 13062306a36Sopenharmony_ci } 13162306a36Sopenharmony_ci} 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cistatic int max77650_gpio_to_irq(struct gpio_chip *gc, unsigned int offset) 13462306a36Sopenharmony_ci{ 13562306a36Sopenharmony_ci struct max77650_gpio_chip *chip = gpiochip_get_data(gc); 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci return chip->irq; 13862306a36Sopenharmony_ci} 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_cistatic int max77650_gpio_probe(struct platform_device *pdev) 14162306a36Sopenharmony_ci{ 14262306a36Sopenharmony_ci struct max77650_gpio_chip *chip; 14362306a36Sopenharmony_ci struct device *dev, *parent; 14462306a36Sopenharmony_ci struct i2c_client *i2c; 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci dev = &pdev->dev; 14762306a36Sopenharmony_ci parent = dev->parent; 14862306a36Sopenharmony_ci i2c = to_i2c_client(parent); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 15162306a36Sopenharmony_ci if (!chip) 15262306a36Sopenharmony_ci return -ENOMEM; 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_ci chip->map = dev_get_regmap(parent, NULL); 15562306a36Sopenharmony_ci if (!chip->map) 15662306a36Sopenharmony_ci return -ENODEV; 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci chip->irq = platform_get_irq_byname(pdev, "GPI"); 15962306a36Sopenharmony_ci if (chip->irq < 0) 16062306a36Sopenharmony_ci return chip->irq; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci chip->gc.base = -1; 16362306a36Sopenharmony_ci chip->gc.ngpio = 1; 16462306a36Sopenharmony_ci chip->gc.label = i2c->name; 16562306a36Sopenharmony_ci chip->gc.parent = dev; 16662306a36Sopenharmony_ci chip->gc.owner = THIS_MODULE; 16762306a36Sopenharmony_ci chip->gc.can_sleep = true; 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci chip->gc.direction_input = max77650_gpio_direction_input; 17062306a36Sopenharmony_ci chip->gc.direction_output = max77650_gpio_direction_output; 17162306a36Sopenharmony_ci chip->gc.set = max77650_gpio_set_value; 17262306a36Sopenharmony_ci chip->gc.get = max77650_gpio_get_value; 17362306a36Sopenharmony_ci chip->gc.get_direction = max77650_gpio_get_direction; 17462306a36Sopenharmony_ci chip->gc.set_config = max77650_gpio_set_config; 17562306a36Sopenharmony_ci chip->gc.to_irq = max77650_gpio_to_irq; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci return devm_gpiochip_add_data(dev, &chip->gc, chip); 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_cistatic struct platform_driver max77650_gpio_driver = { 18162306a36Sopenharmony_ci .driver = { 18262306a36Sopenharmony_ci .name = "max77650-gpio", 18362306a36Sopenharmony_ci }, 18462306a36Sopenharmony_ci .probe = max77650_gpio_probe, 18562306a36Sopenharmony_ci}; 18662306a36Sopenharmony_cimodule_platform_driver(max77650_gpio_driver); 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ciMODULE_DESCRIPTION("MAXIM 77650/77651 GPIO driver"); 18962306a36Sopenharmony_ciMODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>"); 19062306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 19162306a36Sopenharmony_ciMODULE_ALIAS("platform:max77650-gpio"); 192