1 /* 2 * GPIO driver for Exar XR17V35X chip 3 * 4 * Copyright (C) 2015 Sudip Mukherjee <sudip.mukherjee@codethink.co.uk> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 #include <linux/bitops.h> 11 #include <linux/device.h> 12 #include <linux/gpio/driver.h> 13 #include <linux/init.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/pci.h> 17 #include <linux/platform_device.h> 18 19 #define EXAR_OFFSET_MPIOLVL_LO 0x90 20 #define EXAR_OFFSET_MPIOSEL_LO 0x93 21 #define EXAR_OFFSET_MPIOLVL_HI 0x96 22 #define EXAR_OFFSET_MPIOSEL_HI 0x99 23 24 #define DRIVER_NAME "gpio_exar" 25 26 static DEFINE_IDA(ida_index); 27 28 struct exar_gpio_chip { 29 struct gpio_chip gpio_chip; 30 struct mutex lock; 31 int index; 32 void __iomem *regs; 33 char name[20]; 34 unsigned int first_pin; 35 }; 36 37 static void exar_update(struct gpio_chip *chip, unsigned int reg, int val, 38 unsigned int offset) 39 { 40 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); 41 int temp; 42 43 mutex_lock(&exar_gpio->lock); 44 temp = readb(exar_gpio->regs + reg); 45 temp &= ~BIT(offset); 46 if (val) 47 temp |= BIT(offset); 48 writeb(temp, exar_gpio->regs + reg); 49 mutex_unlock(&exar_gpio->lock); 50 } 51 52 static int exar_set_direction(struct gpio_chip *chip, int direction, 53 unsigned int offset) 54 { 55 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); 56 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? 57 EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO; 58 unsigned int bit = (offset + exar_gpio->first_pin) % 8; 59 60 exar_update(chip, addr, direction, bit); 61 return 0; 62 } 63 64 static int exar_get(struct gpio_chip *chip, unsigned int reg) 65 { 66 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); 67 int value; 68 69 mutex_lock(&exar_gpio->lock); 70 value = readb(exar_gpio->regs + reg); 71 mutex_unlock(&exar_gpio->lock); 72 73 return value; 74 } 75 76 static int exar_get_direction(struct gpio_chip *chip, unsigned int offset) 77 { 78 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); 79 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? 80 EXAR_OFFSET_MPIOSEL_HI : EXAR_OFFSET_MPIOSEL_LO; 81 unsigned int bit = (offset + exar_gpio->first_pin) % 8; 82 83 return !!(exar_get(chip, addr) & BIT(bit)); 84 } 85 86 static int exar_get_value(struct gpio_chip *chip, unsigned int offset) 87 { 88 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); 89 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? 90 EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO; 91 unsigned int bit = (offset + exar_gpio->first_pin) % 8; 92 93 return !!(exar_get(chip, addr) & BIT(bit)); 94 } 95 96 static void exar_set_value(struct gpio_chip *chip, unsigned int offset, 97 int value) 98 { 99 struct exar_gpio_chip *exar_gpio = gpiochip_get_data(chip); 100 unsigned int addr = (offset + exar_gpio->first_pin) / 8 ? 101 EXAR_OFFSET_MPIOLVL_HI : EXAR_OFFSET_MPIOLVL_LO; 102 unsigned int bit = (offset + exar_gpio->first_pin) % 8; 103 104 exar_update(chip, addr, value, bit); 105 } 106 107 static int exar_direction_output(struct gpio_chip *chip, unsigned int offset, 108 int value) 109 { 110 exar_set_value(chip, offset, value); 111 return exar_set_direction(chip, 0, offset); 112 } 113 114 static int exar_direction_input(struct gpio_chip *chip, unsigned int offset) 115 { 116 return exar_set_direction(chip, 1, offset); 117 } 118 119 static int gpio_exar_probe(struct platform_device *pdev) 120 { 121 struct pci_dev *pcidev = to_pci_dev(pdev->dev.parent); 122 struct exar_gpio_chip *exar_gpio; 123 u32 first_pin, ngpios; 124 void __iomem *p; 125 int index, ret; 126 127 /* 128 * The UART driver must have mapped region 0 prior to registering this 129 * device - use it. 130 */ 131 p = pcim_iomap_table(pcidev)[0]; 132 if (!p) 133 return -ENOMEM; 134 135 ret = device_property_read_u32(&pdev->dev, "exar,first-pin", 136 &first_pin); 137 if (ret) 138 return ret; 139 140 ret = device_property_read_u32(&pdev->dev, "ngpios", &ngpios); 141 if (ret) 142 return ret; 143 144 exar_gpio = devm_kzalloc(&pdev->dev, sizeof(*exar_gpio), GFP_KERNEL); 145 if (!exar_gpio) 146 return -ENOMEM; 147 148 mutex_init(&exar_gpio->lock); 149 150 index = ida_simple_get(&ida_index, 0, 0, GFP_KERNEL); 151 if (index < 0) 152 goto err_destroy; 153 154 sprintf(exar_gpio->name, "exar_gpio%d", index); 155 exar_gpio->gpio_chip.label = exar_gpio->name; 156 exar_gpio->gpio_chip.parent = &pdev->dev; 157 exar_gpio->gpio_chip.direction_output = exar_direction_output; 158 exar_gpio->gpio_chip.direction_input = exar_direction_input; 159 exar_gpio->gpio_chip.get_direction = exar_get_direction; 160 exar_gpio->gpio_chip.get = exar_get_value; 161 exar_gpio->gpio_chip.set = exar_set_value; 162 exar_gpio->gpio_chip.base = -1; 163 exar_gpio->gpio_chip.ngpio = ngpios; 164 exar_gpio->regs = p; 165 exar_gpio->index = index; 166 exar_gpio->first_pin = first_pin; 167 168 ret = devm_gpiochip_add_data(&pdev->dev, 169 &exar_gpio->gpio_chip, exar_gpio); 170 if (ret) 171 goto err_destroy; 172 173 platform_set_drvdata(pdev, exar_gpio); 174 175 return 0; 176 177 err_destroy: 178 ida_simple_remove(&ida_index, index); 179 mutex_destroy(&exar_gpio->lock); 180 return ret; 181 } 182 183 static int gpio_exar_remove(struct platform_device *pdev) 184 { 185 struct exar_gpio_chip *exar_gpio = platform_get_drvdata(pdev); 186 187 ida_simple_remove(&ida_index, exar_gpio->index); 188 mutex_destroy(&exar_gpio->lock); 189 190 return 0; 191 } 192 193 static struct platform_driver gpio_exar_driver = { 194 .probe = gpio_exar_probe, 195 .remove = gpio_exar_remove, 196 .driver = { 197 .name = DRIVER_NAME, 198 }, 199 }; 200 201 module_platform_driver(gpio_exar_driver); 202 203 MODULE_ALIAS("platform:" DRIVER_NAME); 204 MODULE_DESCRIPTION("Exar GPIO driver"); 205 MODULE_AUTHOR("Sudip Mukherjee <sudip.mukherjee@codethink.co.uk>"); 206 MODULE_LICENSE("GPL"); 207