1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright (C) 2018 ROHM Semiconductors 3 4 #include <linux/gpio/driver.h> 5 #include <linux/mfd/rohm-bd71828.h> 6 #include <linux/module.h> 7 #include <linux/platform_device.h> 8 #include <linux/regmap.h> 9 10 #define GPIO_OUT_REG(off) (BD71828_REG_GPIO_CTRL1 + (off)) 11 #define HALL_GPIO_OFFSET 3 12 13 /* 14 * These defines can be removed when 15 * "gpio: Add definition for GPIO direction" 16 * (9208b1e77d6e8e9776f34f46ef4079ecac9c3c25 in GPIO tree) gets merged, 17 */ 18 #ifndef GPIO_LINE_DIRECTION_IN 19 #define GPIO_LINE_DIRECTION_IN 1 20 #define GPIO_LINE_DIRECTION_OUT 0 21 #endif 22 23 struct bd71828_gpio { 24 struct rohm_regmap_dev chip; 25 struct gpio_chip gpio; 26 }; 27 28 static void bd71828_gpio_set(struct gpio_chip *chip, unsigned int offset, 29 int value) 30 { 31 int ret; 32 struct bd71828_gpio *bdgpio = gpiochip_get_data(chip); 33 u8 val = (value) ? BD71828_GPIO_OUT_HI : BD71828_GPIO_OUT_LO; 34 35 /* 36 * The HALL input pin can only be used as input. If this is the pin 37 * we are dealing with - then we are done 38 */ 39 if (offset == HALL_GPIO_OFFSET) 40 return; 41 42 ret = regmap_update_bits(bdgpio->chip.regmap, GPIO_OUT_REG(offset), 43 BD71828_GPIO_OUT_MASK, val); 44 if (ret) 45 dev_err(bdgpio->chip.dev, "Could not set gpio to %d\n", value); 46 } 47 48 static int bd71828_gpio_get(struct gpio_chip *chip, unsigned int offset) 49 { 50 int ret; 51 unsigned int val; 52 struct bd71828_gpio *bdgpio = gpiochip_get_data(chip); 53 54 if (offset == HALL_GPIO_OFFSET) 55 ret = regmap_read(bdgpio->chip.regmap, BD71828_REG_IO_STAT, 56 &val); 57 else 58 ret = regmap_read(bdgpio->chip.regmap, GPIO_OUT_REG(offset), 59 &val); 60 if (!ret) 61 ret = (val & BD71828_GPIO_OUT_MASK); 62 63 return ret; 64 } 65 66 static int bd71828_gpio_set_config(struct gpio_chip *chip, unsigned int offset, 67 unsigned long config) 68 { 69 struct bd71828_gpio *bdgpio = gpiochip_get_data(chip); 70 71 if (offset == HALL_GPIO_OFFSET) 72 return -ENOTSUPP; 73 74 switch (pinconf_to_config_param(config)) { 75 case PIN_CONFIG_DRIVE_OPEN_DRAIN: 76 return regmap_update_bits(bdgpio->chip.regmap, 77 GPIO_OUT_REG(offset), 78 BD71828_GPIO_DRIVE_MASK, 79 BD71828_GPIO_OPEN_DRAIN); 80 case PIN_CONFIG_DRIVE_PUSH_PULL: 81 return regmap_update_bits(bdgpio->chip.regmap, 82 GPIO_OUT_REG(offset), 83 BD71828_GPIO_DRIVE_MASK, 84 BD71828_GPIO_PUSH_PULL); 85 default: 86 break; 87 } 88 return -ENOTSUPP; 89 } 90 91 static int bd71828_get_direction(struct gpio_chip *chip, unsigned int offset) 92 { 93 /* 94 * Pin usage is selected by OTP data. We can't read it runtime. Hence 95 * we trust that if the pin is not excluded by "gpio-reserved-ranges" 96 * the OTP configuration is set to OUT. (Other pins but HALL input pin 97 * on BD71828 can't really be used for general purpose input - input 98 * states are used for specific cases like regulator control or 99 * PMIC_ON_REQ. 100 */ 101 if (offset == HALL_GPIO_OFFSET) 102 return GPIO_LINE_DIRECTION_IN; 103 104 return GPIO_LINE_DIRECTION_OUT; 105 } 106 107 static int bd71828_probe(struct platform_device *pdev) 108 { 109 struct bd71828_gpio *bdgpio; 110 struct rohm_regmap_dev *bd71828; 111 112 bd71828 = dev_get_drvdata(pdev->dev.parent); 113 if (!bd71828) { 114 dev_err(&pdev->dev, "No MFD driver data\n"); 115 return -EINVAL; 116 } 117 118 bdgpio = devm_kzalloc(&pdev->dev, sizeof(*bdgpio), 119 GFP_KERNEL); 120 if (!bdgpio) 121 return -ENOMEM; 122 123 bdgpio->chip.dev = &pdev->dev; 124 bdgpio->gpio.parent = pdev->dev.parent; 125 bdgpio->gpio.label = "bd71828-gpio"; 126 bdgpio->gpio.owner = THIS_MODULE; 127 bdgpio->gpio.get_direction = bd71828_get_direction; 128 bdgpio->gpio.set_config = bd71828_gpio_set_config; 129 bdgpio->gpio.can_sleep = true; 130 bdgpio->gpio.get = bd71828_gpio_get; 131 bdgpio->gpio.set = bd71828_gpio_set; 132 bdgpio->gpio.base = -1; 133 134 /* 135 * See if we need some implementation to mark some PINs as 136 * not controllable based on DT info or if core can handle 137 * "gpio-reserved-ranges" and exclude them from control 138 */ 139 bdgpio->gpio.ngpio = 4; 140 bdgpio->gpio.of_node = pdev->dev.parent->of_node; 141 bdgpio->chip.regmap = bd71828->regmap; 142 143 return devm_gpiochip_add_data(&pdev->dev, &bdgpio->gpio, 144 bdgpio); 145 } 146 147 static struct platform_driver bd71828_gpio = { 148 .driver = { 149 .name = "bd71828-gpio" 150 }, 151 .probe = bd71828_probe, 152 }; 153 154 module_platform_driver(bd71828_gpio); 155 156 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); 157 MODULE_DESCRIPTION("BD71828 voltage regulator driver"); 158 MODULE_LICENSE("GPL"); 159 MODULE_ALIAS("platform:bd71828-gpio"); 160