1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * GPIO Testing Device Driver 4 * 5 * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com> 6 * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com> 7 * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> 8 */ 9 10 #include <linux/init.h> 11 #include <linux/module.h> 12 #include <linux/gpio/driver.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/platform_device.h> 15 #include <linux/slab.h> 16 #include <linux/interrupt.h> 17 #include <linux/irq.h> 18 #include <linux/irq_sim.h> 19 #include <linux/debugfs.h> 20 #include <linux/uaccess.h> 21 #include <linux/property.h> 22 23 #include "gpiolib.h" 24 25 #define GPIO_MOCKUP_NAME "gpio-mockup" 26 #define GPIO_MOCKUP_MAX_GC 10 27 /* 28 * We're storing two values per chip: the GPIO base and the number 29 * of GPIO lines. 30 */ 31 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) 32 /* Maximum of three properties + the sentinel. */ 33 #define GPIO_MOCKUP_MAX_PROP 4 34 35 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__) 36 37 enum { 38 GPIO_MOCKUP_DIR_OUT = 0, 39 GPIO_MOCKUP_DIR_IN = 1, 40 }; 41 42 /* 43 * struct gpio_pin_status - structure describing a GPIO status 44 * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out 45 * @value: Configures status of the gpio as 0(low) or 1(high) 46 */ 47 struct gpio_mockup_line_status { 48 int dir; 49 int value; 50 }; 51 52 struct gpio_mockup_chip { 53 struct gpio_chip gc; 54 struct gpio_mockup_line_status *lines; 55 struct irq_sim irqsim; 56 struct dentry *dbg_dir; 57 }; 58 59 struct gpio_mockup_dbgfs_private { 60 struct gpio_mockup_chip *chip; 61 struct gpio_desc *desc; 62 int offset; 63 }; 64 65 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; 66 static int gpio_mockup_num_ranges; 67 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400); 68 69 static bool gpio_mockup_named_lines; 70 module_param_named(gpio_mockup_named_lines, 71 gpio_mockup_named_lines, bool, 0400); 72 73 static struct dentry *gpio_mockup_dbg_dir; 74 75 static int gpio_mockup_range_base(unsigned int index) 76 { 77 return gpio_mockup_ranges[index * 2]; 78 } 79 80 static int gpio_mockup_range_ngpio(unsigned int index) 81 { 82 return gpio_mockup_ranges[index * 2 + 1]; 83 } 84 85 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) 86 { 87 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 88 89 return chip->lines[offset].value; 90 } 91 92 static void gpio_mockup_set(struct gpio_chip *gc, 93 unsigned int offset, int value) 94 { 95 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 96 97 chip->lines[offset].value = !!value; 98 } 99 100 static void gpio_mockup_set_multiple(struct gpio_chip *gc, 101 unsigned long *mask, unsigned long *bits) 102 { 103 unsigned int bit; 104 105 for_each_set_bit(bit, mask, gc->ngpio) 106 gpio_mockup_set(gc, bit, test_bit(bit, bits)); 107 108 } 109 110 static int gpio_mockup_dirout(struct gpio_chip *gc, 111 unsigned int offset, int value) 112 { 113 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 114 115 gpio_mockup_set(gc, offset, value); 116 chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; 117 118 return 0; 119 } 120 121 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) 122 { 123 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 124 125 chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; 126 127 return 0; 128 } 129 130 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) 131 { 132 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 133 134 return chip->lines[offset].dir; 135 } 136 137 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset) 138 { 139 struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 140 141 return irq_sim_irqnum(&chip->irqsim, offset); 142 } 143 144 static ssize_t gpio_mockup_event_write(struct file *file, 145 const char __user *usr_buf, 146 size_t size, loff_t *ppos) 147 { 148 struct gpio_mockup_dbgfs_private *priv; 149 struct gpio_mockup_chip *chip; 150 struct seq_file *sfile; 151 struct gpio_desc *desc; 152 int rv, val; 153 154 rv = kstrtoint_from_user(usr_buf, size, 0, &val); 155 if (rv) 156 return rv; 157 if (val != 0 && val != 1) 158 return -EINVAL; 159 160 sfile = file->private_data; 161 priv = sfile->private; 162 desc = priv->desc; 163 chip = priv->chip; 164 165 gpiod_set_value_cansleep(desc, val); 166 irq_sim_fire(&chip->irqsim, priv->offset); 167 168 return size; 169 } 170 171 static int gpio_mockup_event_open(struct inode *inode, struct file *file) 172 { 173 return single_open(file, NULL, inode->i_private); 174 } 175 176 static const struct file_operations gpio_mockup_event_ops = { 177 .owner = THIS_MODULE, 178 .open = gpio_mockup_event_open, 179 .write = gpio_mockup_event_write, 180 .llseek = no_llseek, 181 }; 182 183 static void gpio_mockup_debugfs_setup(struct device *dev, 184 struct gpio_mockup_chip *chip) 185 { 186 struct gpio_mockup_dbgfs_private *priv; 187 struct dentry *evfile, *link; 188 struct gpio_chip *gc; 189 const char *devname; 190 char *name; 191 int i; 192 193 gc = &chip->gc; 194 devname = dev_name(&gc->gpiodev->dev); 195 196 chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); 197 if (IS_ERR_OR_NULL(chip->dbg_dir)) 198 goto err; 199 200 link = debugfs_create_symlink(gc->label, gpio_mockup_dbg_dir, devname); 201 if (IS_ERR_OR_NULL(link)) 202 goto err; 203 204 for (i = 0; i < gc->ngpio; i++) { 205 name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); 206 if (!name) 207 goto err; 208 209 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 210 if (!priv) 211 goto err; 212 213 priv->chip = chip; 214 priv->offset = i; 215 priv->desc = &gc->gpiodev->descs[i]; 216 217 evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, 218 &gpio_mockup_event_ops); 219 if (IS_ERR_OR_NULL(evfile)) 220 goto err; 221 } 222 223 return; 224 225 err: 226 dev_err(dev, "error creating debugfs event files\n"); 227 } 228 229 static int gpio_mockup_name_lines(struct device *dev, 230 struct gpio_mockup_chip *chip) 231 { 232 struct gpio_chip *gc = &chip->gc; 233 char **names; 234 int i; 235 236 names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL); 237 if (!names) 238 return -ENOMEM; 239 240 for (i = 0; i < gc->ngpio; i++) { 241 names[i] = devm_kasprintf(dev, GFP_KERNEL, 242 "%s-%d", gc->label, i); 243 if (!names[i]) 244 return -ENOMEM; 245 } 246 247 gc->names = (const char *const *)names; 248 249 return 0; 250 } 251 252 static int gpio_mockup_probe(struct platform_device *pdev) 253 { 254 struct gpio_mockup_chip *chip; 255 struct gpio_chip *gc; 256 struct device *dev; 257 const char *name; 258 int rv, base; 259 u16 ngpio; 260 261 dev = &pdev->dev; 262 263 rv = device_property_read_u32(dev, "gpio-base", &base); 264 if (rv) 265 base = -1; 266 267 rv = device_property_read_u16(dev, "nr-gpios", &ngpio); 268 if (rv) 269 return rv; 270 271 rv = device_property_read_string(dev, "chip-name", &name); 272 if (rv) 273 name = NULL; 274 275 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 276 if (!chip) 277 return -ENOMEM; 278 279 if (!name) { 280 name = devm_kasprintf(dev, GFP_KERNEL, 281 "%s-%c", pdev->name, pdev->id + 'A'); 282 if (!name) 283 return -ENOMEM; 284 } 285 286 gc = &chip->gc; 287 gc->base = base; 288 gc->ngpio = ngpio; 289 gc->label = name; 290 gc->owner = THIS_MODULE; 291 gc->parent = dev; 292 gc->get = gpio_mockup_get; 293 gc->set = gpio_mockup_set; 294 gc->set_multiple = gpio_mockup_set_multiple; 295 gc->direction_output = gpio_mockup_dirout; 296 gc->direction_input = gpio_mockup_dirin; 297 gc->get_direction = gpio_mockup_get_direction; 298 gc->to_irq = gpio_mockup_to_irq; 299 300 chip->lines = devm_kcalloc(dev, gc->ngpio, 301 sizeof(*chip->lines), GFP_KERNEL); 302 if (!chip->lines) 303 return -ENOMEM; 304 305 if (device_property_read_bool(dev, "named-gpio-lines")) { 306 rv = gpio_mockup_name_lines(dev, chip); 307 if (rv) 308 return rv; 309 } 310 311 rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio); 312 if (rv < 0) 313 return rv; 314 315 rv = devm_gpiochip_add_data(dev, &chip->gc, chip); 316 if (rv) 317 return rv; 318 319 if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) 320 gpio_mockup_debugfs_setup(dev, chip); 321 322 return 0; 323 } 324 325 static struct platform_driver gpio_mockup_driver = { 326 .driver = { 327 .name = GPIO_MOCKUP_NAME, 328 }, 329 .probe = gpio_mockup_probe, 330 }; 331 332 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; 333 334 static void gpio_mockup_unregister_pdevs(void) 335 { 336 struct platform_device *pdev; 337 int i; 338 339 for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { 340 pdev = gpio_mockup_pdevs[i]; 341 342 if (pdev) 343 platform_device_unregister(pdev); 344 } 345 } 346 347 static int __init gpio_mockup_init(void) 348 { 349 struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; 350 int i, prop, num_chips, err = 0, base; 351 struct platform_device_info pdevinfo; 352 struct platform_device *pdev; 353 u16 ngpio; 354 355 if ((gpio_mockup_num_ranges < 2) || 356 (gpio_mockup_num_ranges % 2) || 357 (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) 358 return -EINVAL; 359 360 /* Each chip is described by two values. */ 361 num_chips = gpio_mockup_num_ranges / 2; 362 363 /* 364 * The second value in the <base GPIO - number of GPIOS> pair must 365 * always be greater than 0. 366 */ 367 for (i = 0; i < num_chips; i++) { 368 if (gpio_mockup_range_ngpio(i) < 0) 369 return -EINVAL; 370 } 371 372 gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); 373 if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) 374 gpio_mockup_err("error creating debugfs directory\n"); 375 376 err = platform_driver_register(&gpio_mockup_driver); 377 if (err) { 378 gpio_mockup_err("error registering platform driver\n"); 379 return err; 380 } 381 382 for (i = 0; i < num_chips; i++) { 383 memset(properties, 0, sizeof(properties)); 384 memset(&pdevinfo, 0, sizeof(pdevinfo)); 385 prop = 0; 386 387 base = gpio_mockup_range_base(i); 388 if (base >= 0) 389 properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", 390 base); 391 392 ngpio = base < 0 ? gpio_mockup_range_ngpio(i) 393 : gpio_mockup_range_ngpio(i) - base; 394 properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); 395 396 if (gpio_mockup_named_lines) 397 properties[prop++] = PROPERTY_ENTRY_BOOL( 398 "named-gpio-lines"); 399 400 pdevinfo.name = GPIO_MOCKUP_NAME; 401 pdevinfo.id = i; 402 pdevinfo.properties = properties; 403 404 pdev = platform_device_register_full(&pdevinfo); 405 if (IS_ERR(pdev)) { 406 gpio_mockup_err("error registering device"); 407 platform_driver_unregister(&gpio_mockup_driver); 408 gpio_mockup_unregister_pdevs(); 409 return PTR_ERR(pdev); 410 } 411 412 gpio_mockup_pdevs[i] = pdev; 413 } 414 415 return 0; 416 } 417 418 static void __exit gpio_mockup_exit(void) 419 { 420 debugfs_remove_recursive(gpio_mockup_dbg_dir); 421 platform_driver_unregister(&gpio_mockup_driver); 422 gpio_mockup_unregister_pdevs(); 423 } 424 425 module_init(gpio_mockup_init); 426 module_exit(gpio_mockup_exit); 427 428 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); 429 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>"); 430 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); 431 MODULE_DESCRIPTION("GPIO Testing driver"); 432 MODULE_LICENSE("GPL v2"); 433