1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <linux/module.h> #include <linux/gpio/driver.h> #include <linux/io.h> #include <linux/ioport.h> #include <linux/platform_device.h>
#define DRIVER_NAME "simple_lp_gpio" #define GPIO_BASE 0x1c00 #define GPIO_NUM 95
#define CONFIG1_OFFSET(n) (0x100 + (n) * 8) #define CONFIG2_OFFSET(n) (0x104 + (n) * 8)
#define OUT_LVL_BIT BIT(31) #define IN_LVL_BIT BIT(30) #define DIR_BIT BIT(2) #define USE_SEL_MASK GENMASK(1, 0) #define USE_SEL_GPIO 1
static void __iomem *gpio_base;
static int simple_gpio_get(struct gpio_chip *chip, unsigned int offset) { u32 val = ioread32(gpio_base + CONFIG1_OFFSET(offset)); return !!(val & IN_LVL_BIT); }
static int simple_gpio_set(struct gpio_chip *chip, unsigned int offset, int value) { void __iomem *reg = gpio_base + CONFIG1_OFFSET(offset); u32 val = ioread32(reg);
if (value) val |= OUT_LVL_BIT; else val &= ~OUT_LVL_BIT;
iowrite32(val, reg); return 0; }
static int simple_gpio_dir_input(struct gpio_chip *chip, unsigned int offset) { void __iomem *reg = gpio_base + CONFIG1_OFFSET(offset); u32 val = ioread32(reg);
val |= DIR_BIT; val = (val & ~USE_SEL_MASK) | USE_SEL_GPIO;
iowrite32(val, reg); return 0; }
static int simple_gpio_dir_output(struct gpio_chip *chip, unsigned int offset, int value) { void __iomem *reg = gpio_base + CONFIG1_OFFSET(offset); u32 val = ioread32(reg);
if (value) val |= OUT_LVL_BIT; else val &= ~OUT_LVL_BIT;
val &= ~DIR_BIT; val = (val & ~USE_SEL_MASK) | USE_SEL_GPIO;
iowrite32(val, reg); return 0; }
static int simple_gpio_get_direction(struct gpio_chip *chip, unsigned int offset) { u32 val = ioread32(gpio_base + CONFIG1_OFFSET(offset)); if (val & DIR_BIT) return GPIO_LINE_DIRECTION_IN; else return GPIO_LINE_DIRECTION_OUT; }
static struct gpio_chip simple_gpio_chip = { .label = DRIVER_NAME, .owner = THIS_MODULE, .get = simple_gpio_get, .set = simple_gpio_set, .direction_input = simple_gpio_dir_input, .direction_output = simple_gpio_dir_output, .get_direction = simple_gpio_get_direction, .base = -1, .ngpio = GPIO_NUM, .can_sleep = false, };
static int simple_gpio_probe(struct platform_device *pdev) { struct resource *res; int ret;
res = platform_get_resource(pdev, IORESOURCE_IO, 0); if (!res) { dev_err(&pdev->dev, "No IO resource found\n"); return -ENODEV; }
if (!request_region(res->start, resource_size(res), DRIVER_NAME)) { dev_err(&pdev->dev, "IO region 0x%lx busy\n", (unsigned long)res->start); return -EBUSY; }
gpio_base = ioport_map(res->start, resource_size(res)); if (!gpio_base) { dev_err(&pdev->dev, "ioport_map failed\n"); release_region(res->start, resource_size(res)); return -ENOMEM; }
ret = gpiochip_add_data(&simple_gpio_chip, NULL); if (ret) { dev_err(&pdev->dev, "Failed to register gpio chip: %d\n", ret); ioport_unmap(gpio_base); release_region(res->start, resource_size(res)); return ret; }
platform_set_drvdata(pdev, &simple_gpio_chip); dev_info(&pdev->dev, "Registered %d GPIOs at IO 0x%lx\n", GPIO_NUM, (unsigned long)res->start); return 0; }
static void simple_gpio_remove(struct platform_device *pdev) { struct gpio_chip *chip = platform_get_drvdata(pdev); struct resource *res = platform_get_resource(pdev, IORESOURCE_IO, 0);
gpiochip_remove(chip); if (res) { ioport_unmap(gpio_base); release_region(res->start, resource_size(res)); } }
static struct platform_driver simple_gpio_driver = { .probe = simple_gpio_probe, .remove = simple_gpio_remove, .driver = { .name = DRIVER_NAME, }, };
static struct platform_device *simple_gpio_device;
static int __init simple_gpio_init(void) { int ret;
ret = platform_driver_register(&simple_gpio_driver); if (ret) return ret;
simple_gpio_device = platform_device_alloc(DRIVER_NAME, -1); if (!simple_gpio_device) { platform_driver_unregister(&simple_gpio_driver); return -ENOMEM; }
struct resource res = { .start = GPIO_BASE, .end = GPIO_BASE + 0x3ff, .flags = IORESOURCE_IO, }; ret = platform_device_add_resources(simple_gpio_device, &res, 1); if (ret) goto err;
ret = platform_device_add(simple_gpio_device); if (ret) goto err;
return 0;
err: platform_device_put(simple_gpio_device); platform_driver_unregister(&simple_gpio_driver); return ret; }
static void __exit simple_gpio_exit(void) { platform_device_unregister(simple_gpio_device); platform_driver_unregister(&simple_gpio_driver); }
module_init(simple_gpio_init); module_exit(simple_gpio_exit);
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Simple GPIO driver for Intel Lynxpoint PCH (IO port based)");
|