1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Congatec Board Controller GPIO driver 4 * 5 * Copyright (C) 2024 Bootlin 6 * Author: Thomas Richard <thomas.richard@bootlin.com> 7 */ 8 9 #include <linux/gpio/driver.h> 10 #include <linux/mfd/cgbc.h> 11 #include <linux/module.h> 12 #include <linux/mutex.h> 13 #include <linux/platform_device.h> 14 15 #define CGBC_GPIO_NGPIO 14 16 17 #define CGBC_GPIO_CMD_GET 0x64 18 #define CGBC_GPIO_CMD_SET 0x65 19 #define CGBC_GPIO_CMD_DIR_GET 0x66 20 #define CGBC_GPIO_CMD_DIR_SET 0x67 21 22 struct cgbc_gpio_data { 23 struct gpio_chip chip; 24 struct cgbc_device_data *cgbc; 25 struct mutex lock; 26 }; 27 28 static int cgbc_gpio_cmd(struct cgbc_device_data *cgbc, 29 u8 cmd0, u8 cmd1, u8 cmd2, u8 *value) 30 { 31 u8 cmd[3] = {cmd0, cmd1, cmd2}; 32 33 return cgbc_command(cgbc, cmd, sizeof(cmd), value, 1, NULL); 34 } 35 36 static int cgbc_gpio_get(struct gpio_chip *chip, unsigned int offset) 37 { 38 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 39 struct cgbc_device_data *cgbc = gpio->cgbc; 40 int ret; 41 u8 val; 42 43 scoped_guard(mutex, &gpio->lock) 44 ret = cgbc_gpio_cmd(cgbc, CGBC_GPIO_CMD_GET, (offset > 7) ? 1 : 0, 0, &val); 45 46 offset %= 8; 47 48 if (ret) 49 return ret; 50 else 51 return (int)(val & (u8)BIT(offset)); 52 } 53 54 static int __cgbc_gpio_set(struct gpio_chip *chip, unsigned int offset, 55 int value) 56 { 57 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 58 struct cgbc_device_data *cgbc = gpio->cgbc; 59 u8 val; 60 int ret; 61 62 ret = cgbc_gpio_cmd(cgbc, CGBC_GPIO_CMD_GET, (offset > 7) ? 1 : 0, 0, &val); 63 if (ret) 64 return ret; 65 66 if (value) 67 val |= BIT(offset % 8); 68 else 69 val &= ~(BIT(offset % 8)); 70 71 return cgbc_gpio_cmd(cgbc, CGBC_GPIO_CMD_SET, (offset > 7) ? 1 : 0, val, &val); 72 } 73 74 static int cgbc_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) 75 { 76 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 77 78 guard(mutex)(&gpio->lock); 79 80 return __cgbc_gpio_set(chip, offset, value); 81 } 82 83 static int cgbc_gpio_direction_set(struct gpio_chip *chip, 84 unsigned int offset, int direction) 85 { 86 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 87 struct cgbc_device_data *cgbc = gpio->cgbc; 88 int ret; 89 u8 val; 90 91 ret = cgbc_gpio_cmd(cgbc, CGBC_GPIO_CMD_DIR_GET, (offset > 7) ? 1 : 0, 0, &val); 92 if (ret) 93 goto end; 94 95 if (direction == GPIO_LINE_DIRECTION_IN) 96 val &= ~(BIT(offset % 8)); 97 else 98 val |= BIT(offset % 8); 99 100 ret = cgbc_gpio_cmd(cgbc, CGBC_GPIO_CMD_DIR_SET, (offset > 7) ? 1 : 0, val, &val); 101 102 end: 103 return ret; 104 } 105 106 static int cgbc_gpio_direction_input(struct gpio_chip *chip, 107 unsigned int offset) 108 { 109 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 110 111 guard(mutex)(&gpio->lock); 112 return cgbc_gpio_direction_set(chip, offset, GPIO_LINE_DIRECTION_IN); 113 } 114 115 static int cgbc_gpio_direction_output(struct gpio_chip *chip, 116 unsigned int offset, int value) 117 { 118 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 119 int ret; 120 121 guard(mutex)(&gpio->lock); 122 123 ret = __cgbc_gpio_set(chip, offset, value); 124 if (ret) 125 return ret; 126 127 return cgbc_gpio_direction_set(chip, offset, GPIO_LINE_DIRECTION_OUT); 128 } 129 130 static int cgbc_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) 131 { 132 struct cgbc_gpio_data *gpio = gpiochip_get_data(chip); 133 struct cgbc_device_data *cgbc = gpio->cgbc; 134 int ret; 135 u8 val; 136 137 scoped_guard(mutex, &gpio->lock) 138 ret = cgbc_gpio_cmd(cgbc, CGBC_GPIO_CMD_DIR_GET, (offset > 7) ? 1 : 0, 0, &val); 139 140 if (ret) 141 return ret; 142 143 if (val & BIT(offset % 8)) 144 return GPIO_LINE_DIRECTION_OUT; 145 else 146 return GPIO_LINE_DIRECTION_IN; 147 } 148 149 static int cgbc_gpio_probe(struct platform_device *pdev) 150 { 151 struct device *dev = &pdev->dev; 152 struct cgbc_device_data *cgbc = dev_get_drvdata(dev->parent); 153 struct cgbc_gpio_data *gpio; 154 struct gpio_chip *chip; 155 int ret; 156 157 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL); 158 if (!gpio) 159 return -ENOMEM; 160 161 gpio->cgbc = cgbc; 162 163 platform_set_drvdata(pdev, gpio); 164 165 chip = &gpio->chip; 166 chip->label = dev_name(&pdev->dev); 167 chip->owner = THIS_MODULE; 168 chip->parent = dev; 169 chip->base = -1; 170 chip->direction_input = cgbc_gpio_direction_input; 171 chip->direction_output = cgbc_gpio_direction_output; 172 chip->get_direction = cgbc_gpio_get_direction; 173 chip->get = cgbc_gpio_get; 174 chip->set_rv = cgbc_gpio_set; 175 chip->ngpio = CGBC_GPIO_NGPIO; 176 177 ret = devm_mutex_init(dev, &gpio->lock); 178 if (ret) 179 return ret; 180 181 ret = devm_gpiochip_add_data(dev, chip, gpio); 182 if (ret) 183 return dev_err_probe(dev, ret, "Could not register GPIO chip\n"); 184 185 return 0; 186 } 187 188 static struct platform_driver cgbc_gpio_driver = { 189 .driver = { 190 .name = "cgbc-gpio", 191 }, 192 .probe = cgbc_gpio_probe, 193 }; 194 195 module_platform_driver(cgbc_gpio_driver); 196 197 MODULE_DESCRIPTION("Congatec Board Controller GPIO Driver"); 198 MODULE_AUTHOR("Thomas Richard <thomas.richard@bootlin.com>"); 199 MODULE_LICENSE("GPL"); 200 MODULE_ALIAS("platform:cgbc-gpio"); 201