10f98dd1bSBamvor Jian Zhang /* 20f98dd1bSBamvor Jian Zhang * GPIO Testing Device Driver 30f98dd1bSBamvor Jian Zhang * 40f98dd1bSBamvor Jian Zhang * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com> 50f98dd1bSBamvor Jian Zhang * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamvor.zhangjian@linaro.org> 60f98dd1bSBamvor Jian Zhang * 70f98dd1bSBamvor Jian Zhang * This program is free software; you can redistribute it and/or modify it 80f98dd1bSBamvor Jian Zhang * under the terms of the GNU General Public License as published by the 90f98dd1bSBamvor Jian Zhang * Free Software Foundation; either version 2 of the License, or (at your 100f98dd1bSBamvor Jian Zhang * option) any later version. 110f98dd1bSBamvor Jian Zhang * 120f98dd1bSBamvor Jian Zhang */ 130f98dd1bSBamvor Jian Zhang 140f98dd1bSBamvor Jian Zhang #include <linux/init.h> 150f98dd1bSBamvor Jian Zhang #include <linux/module.h> 160f98dd1bSBamvor Jian Zhang #include <linux/gpio/driver.h> 179202ba23SBartosz Golaszewski #include <linux/gpio/consumer.h> 180f98dd1bSBamvor Jian Zhang #include <linux/platform_device.h> 198a68ea00SBartosz Golaszewski #include <linux/slab.h> 20e2ff7408SBartosz Golaszewski #include <linux/interrupt.h> 21e2ff7408SBartosz Golaszewski #include <linux/irq.h> 22e2ff7408SBartosz Golaszewski #include <linux/irq_work.h> 239202ba23SBartosz Golaszewski #include <linux/debugfs.h> 249202ba23SBartosz Golaszewski #include <linux/uaccess.h> 259202ba23SBartosz Golaszewski 269202ba23SBartosz Golaszewski #include "gpiolib.h" 270f98dd1bSBamvor Jian Zhang 28ca409160SBartosz Golaszewski #define GPIO_MOCKUP_NAME "gpio-mockup" 29ca409160SBartosz Golaszewski #define GPIO_MOCKUP_MAX_GC 10 30*b652336dSBartosz Golaszewski /* 31*b652336dSBartosz Golaszewski * We're storing two values per chip: the GPIO base and the number 32*b652336dSBartosz Golaszewski * of GPIO lines. 33*b652336dSBartosz Golaszewski */ 34*b652336dSBartosz Golaszewski #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) 350f98dd1bSBamvor Jian Zhang 36ca409160SBartosz Golaszewski enum { 37c650c00cSBartosz Golaszewski GPIO_MOCKUP_DIR_OUT = 0, 38c650c00cSBartosz Golaszewski GPIO_MOCKUP_DIR_IN = 1, 390f98dd1bSBamvor Jian Zhang }; 400f98dd1bSBamvor Jian Zhang 410f98dd1bSBamvor Jian Zhang /* 420f98dd1bSBamvor Jian Zhang * struct gpio_pin_status - structure describing a GPIO status 430f98dd1bSBamvor Jian Zhang * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out 440f98dd1bSBamvor Jian Zhang * @value: Configures status of the gpio as 0(low) or 1(high) 450f98dd1bSBamvor Jian Zhang */ 46ca409160SBartosz Golaszewski struct gpio_mockup_line_status { 47ca409160SBartosz Golaszewski int dir; 480f98dd1bSBamvor Jian Zhang bool value; 49c7f5326fSBartosz Golaszewski bool irq_enabled; 500f98dd1bSBamvor Jian Zhang }; 510f98dd1bSBamvor Jian Zhang 52e2ff7408SBartosz Golaszewski struct gpio_mockup_irq_context { 53e2ff7408SBartosz Golaszewski struct irq_work work; 54e2ff7408SBartosz Golaszewski int irq; 55e2ff7408SBartosz Golaszewski }; 56e2ff7408SBartosz Golaszewski 57ca409160SBartosz Golaszewski struct gpio_mockup_chip { 580f98dd1bSBamvor Jian Zhang struct gpio_chip gc; 59ca409160SBartosz Golaszewski struct gpio_mockup_line_status *lines; 60e2ff7408SBartosz Golaszewski struct gpio_mockup_irq_context irq_ctx; 619202ba23SBartosz Golaszewski struct dentry *dbg_dir; 629202ba23SBartosz Golaszewski }; 639202ba23SBartosz Golaszewski 649202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private { 659202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip; 669202ba23SBartosz Golaszewski struct gpio_desc *desc; 679202ba23SBartosz Golaszewski int offset; 680f98dd1bSBamvor Jian Zhang }; 690f98dd1bSBamvor Jian Zhang 70*b652336dSBartosz Golaszewski static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; 710f98dd1bSBamvor Jian Zhang static int gpio_mockup_params_nr; 720f98dd1bSBamvor Jian Zhang module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); 730f98dd1bSBamvor Jian Zhang 748a68ea00SBartosz Golaszewski static bool gpio_mockup_named_lines; 758a68ea00SBartosz Golaszewski module_param_named(gpio_mockup_named_lines, 768a68ea00SBartosz Golaszewski gpio_mockup_named_lines, bool, 0400); 778a68ea00SBartosz Golaszewski 78ca409160SBartosz Golaszewski static const char gpio_mockup_name_start = 'A'; 799202ba23SBartosz Golaszewski static struct dentry *gpio_mockup_dbg_dir; 800f98dd1bSBamvor Jian Zhang 81ca409160SBartosz Golaszewski static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) 820f98dd1bSBamvor Jian Zhang { 83ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 840f98dd1bSBamvor Jian Zhang 85ca409160SBartosz Golaszewski return chip->lines[offset].value; 860f98dd1bSBamvor Jian Zhang } 870f98dd1bSBamvor Jian Zhang 88ca409160SBartosz Golaszewski static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset, 890f98dd1bSBamvor Jian Zhang int value) 900f98dd1bSBamvor Jian Zhang { 91ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 920f98dd1bSBamvor Jian Zhang 93ca409160SBartosz Golaszewski chip->lines[offset].value = !!value; 940f98dd1bSBamvor Jian Zhang } 950f98dd1bSBamvor Jian Zhang 96ca409160SBartosz Golaszewski static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset, 970f98dd1bSBamvor Jian Zhang int value) 980f98dd1bSBamvor Jian Zhang { 99ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1000f98dd1bSBamvor Jian Zhang 101ca409160SBartosz Golaszewski gpio_mockup_set(gc, offset, value); 102c650c00cSBartosz Golaszewski chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; 103ca409160SBartosz Golaszewski 1040f98dd1bSBamvor Jian Zhang return 0; 1050f98dd1bSBamvor Jian Zhang } 1060f98dd1bSBamvor Jian Zhang 107ca409160SBartosz Golaszewski static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) 1080f98dd1bSBamvor Jian Zhang { 109ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1100f98dd1bSBamvor Jian Zhang 111c650c00cSBartosz Golaszewski chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; 112ca409160SBartosz Golaszewski 1130f98dd1bSBamvor Jian Zhang return 0; 1140f98dd1bSBamvor Jian Zhang } 1150f98dd1bSBamvor Jian Zhang 116ca409160SBartosz Golaszewski static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) 1170f98dd1bSBamvor Jian Zhang { 118ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1190f98dd1bSBamvor Jian Zhang 120ca409160SBartosz Golaszewski return chip->lines[offset].dir; 1210f98dd1bSBamvor Jian Zhang } 1220f98dd1bSBamvor Jian Zhang 1238a68ea00SBartosz Golaszewski static int gpio_mockup_name_lines(struct device *dev, 1248a68ea00SBartosz Golaszewski struct gpio_mockup_chip *chip) 1258a68ea00SBartosz Golaszewski { 1268a68ea00SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 1278a68ea00SBartosz Golaszewski char **names; 1288a68ea00SBartosz Golaszewski int i; 1298a68ea00SBartosz Golaszewski 1308a68ea00SBartosz Golaszewski names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL); 1318a68ea00SBartosz Golaszewski if (!names) 1328a68ea00SBartosz Golaszewski return -ENOMEM; 1338a68ea00SBartosz Golaszewski 1348a68ea00SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 1358a68ea00SBartosz Golaszewski names[i] = devm_kasprintf(dev, GFP_KERNEL, 1368a68ea00SBartosz Golaszewski "%s-%d", gc->label, i); 1378a68ea00SBartosz Golaszewski if (!names[i]) 1388a68ea00SBartosz Golaszewski return -ENOMEM; 1398a68ea00SBartosz Golaszewski } 1408a68ea00SBartosz Golaszewski 1418a68ea00SBartosz Golaszewski gc->names = (const char *const *)names; 1428a68ea00SBartosz Golaszewski 1438a68ea00SBartosz Golaszewski return 0; 1448a68ea00SBartosz Golaszewski } 1458a68ea00SBartosz Golaszewski 146e2ff7408SBartosz Golaszewski static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset) 147e2ff7408SBartosz Golaszewski { 148e2ff7408SBartosz Golaszewski return chip->irq_base + offset; 149e2ff7408SBartosz Golaszewski } 150e2ff7408SBartosz Golaszewski 151c7f5326fSBartosz Golaszewski static void gpio_mockup_irqmask(struct irq_data *data) 152c7f5326fSBartosz Golaszewski { 153c7f5326fSBartosz Golaszewski struct gpio_chip *gc = irq_data_get_irq_chip_data(data); 154c7f5326fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 155c7f5326fSBartosz Golaszewski 156c7f5326fSBartosz Golaszewski chip->lines[data->irq - gc->irq_base].irq_enabled = false; 157c7f5326fSBartosz Golaszewski } 158c7f5326fSBartosz Golaszewski 159c7f5326fSBartosz Golaszewski static void gpio_mockup_irqunmask(struct irq_data *data) 160c7f5326fSBartosz Golaszewski { 161c7f5326fSBartosz Golaszewski struct gpio_chip *gc = irq_data_get_irq_chip_data(data); 162c7f5326fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 163c7f5326fSBartosz Golaszewski 164c7f5326fSBartosz Golaszewski chip->lines[data->irq - gc->irq_base].irq_enabled = true; 165c7f5326fSBartosz Golaszewski } 166e2ff7408SBartosz Golaszewski 167e2ff7408SBartosz Golaszewski static struct irq_chip gpio_mockup_irqchip = { 168e2ff7408SBartosz Golaszewski .name = GPIO_MOCKUP_NAME, 169e2ff7408SBartosz Golaszewski .irq_mask = gpio_mockup_irqmask, 170e2ff7408SBartosz Golaszewski .irq_unmask = gpio_mockup_irqunmask, 171e2ff7408SBartosz Golaszewski }; 172e2ff7408SBartosz Golaszewski 173e2ff7408SBartosz Golaszewski static void gpio_mockup_handle_irq(struct irq_work *work) 174e2ff7408SBartosz Golaszewski { 175e2ff7408SBartosz Golaszewski struct gpio_mockup_irq_context *irq_ctx; 176e2ff7408SBartosz Golaszewski 177e2ff7408SBartosz Golaszewski irq_ctx = container_of(work, struct gpio_mockup_irq_context, work); 178e2ff7408SBartosz Golaszewski handle_simple_irq(irq_to_desc(irq_ctx->irq)); 179e2ff7408SBartosz Golaszewski } 180e2ff7408SBartosz Golaszewski 181e2ff7408SBartosz Golaszewski static int gpio_mockup_irqchip_setup(struct device *dev, 182e2ff7408SBartosz Golaszewski struct gpio_mockup_chip *chip) 183e2ff7408SBartosz Golaszewski { 184e2ff7408SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 185e2ff7408SBartosz Golaszewski int irq_base, i; 186e2ff7408SBartosz Golaszewski 18762a7ca07SBartosz Golaszewski irq_base = devm_irq_alloc_descs(dev, -1, 0, gc->ngpio, 0); 188e2ff7408SBartosz Golaszewski if (irq_base < 0) 189e2ff7408SBartosz Golaszewski return irq_base; 190e2ff7408SBartosz Golaszewski 191e2ff7408SBartosz Golaszewski gc->irq_base = irq_base; 192e2ff7408SBartosz Golaszewski gc->irqchip = &gpio_mockup_irqchip; 193e2ff7408SBartosz Golaszewski 194e2ff7408SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 195e2ff7408SBartosz Golaszewski irq_set_chip(irq_base + i, gc->irqchip); 196c7f5326fSBartosz Golaszewski irq_set_chip_data(irq_base + i, gc); 197e2ff7408SBartosz Golaszewski irq_set_handler(irq_base + i, &handle_simple_irq); 198e2ff7408SBartosz Golaszewski irq_modify_status(irq_base + i, 199e2ff7408SBartosz Golaszewski IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE); 200e2ff7408SBartosz Golaszewski } 201e2ff7408SBartosz Golaszewski 202e2ff7408SBartosz Golaszewski init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq); 203e2ff7408SBartosz Golaszewski 204e2ff7408SBartosz Golaszewski return 0; 205e2ff7408SBartosz Golaszewski } 206e2ff7408SBartosz Golaszewski 2079202ba23SBartosz Golaszewski static ssize_t gpio_mockup_event_write(struct file *file, 2089202ba23SBartosz Golaszewski const char __user *usr_buf, 2099202ba23SBartosz Golaszewski size_t size, loff_t *ppos) 2109202ba23SBartosz Golaszewski { 2119202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 2129202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip; 2139202ba23SBartosz Golaszewski struct seq_file *sfile; 2149202ba23SBartosz Golaszewski struct gpio_desc *desc; 2159202ba23SBartosz Golaszewski struct gpio_chip *gc; 2166f9b3e77SBartosz Golaszewski int rv, val; 2179202ba23SBartosz Golaszewski 2186f9b3e77SBartosz Golaszewski rv = kstrtoint_from_user(usr_buf, size, 0, &val); 2196f9b3e77SBartosz Golaszewski if (rv) 2206f9b3e77SBartosz Golaszewski return rv; 2216f9b3e77SBartosz Golaszewski if (val != 0 && val != 1) 2226f9b3e77SBartosz Golaszewski return -EINVAL; 2236f9b3e77SBartosz Golaszewski 224650b57b0SBartosz Golaszewski sfile = file->private_data; 225650b57b0SBartosz Golaszewski priv = sfile->private; 226650b57b0SBartosz Golaszewski desc = priv->desc; 227650b57b0SBartosz Golaszewski chip = priv->chip; 228650b57b0SBartosz Golaszewski gc = &chip->gc; 229c7f5326fSBartosz Golaszewski 230650b57b0SBartosz Golaszewski if (chip->lines[priv->offset].irq_enabled) { 2319202ba23SBartosz Golaszewski gpiod_set_value_cansleep(desc, val); 2329202ba23SBartosz Golaszewski priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; 2339202ba23SBartosz Golaszewski irq_work_queue(&priv->chip->irq_ctx.work); 234650b57b0SBartosz Golaszewski } 2359202ba23SBartosz Golaszewski 2369202ba23SBartosz Golaszewski return size; 2379202ba23SBartosz Golaszewski } 2389202ba23SBartosz Golaszewski 2399202ba23SBartosz Golaszewski static int gpio_mockup_event_open(struct inode *inode, struct file *file) 2409202ba23SBartosz Golaszewski { 2419202ba23SBartosz Golaszewski return single_open(file, NULL, inode->i_private); 2429202ba23SBartosz Golaszewski } 2439202ba23SBartosz Golaszewski 2449202ba23SBartosz Golaszewski static const struct file_operations gpio_mockup_event_ops = { 2459202ba23SBartosz Golaszewski .owner = THIS_MODULE, 2469202ba23SBartosz Golaszewski .open = gpio_mockup_event_open, 2479202ba23SBartosz Golaszewski .write = gpio_mockup_event_write, 2489202ba23SBartosz Golaszewski .llseek = no_llseek, 2499202ba23SBartosz Golaszewski }; 2509202ba23SBartosz Golaszewski 2519202ba23SBartosz Golaszewski static void gpio_mockup_debugfs_setup(struct device *dev, 2529202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip) 2539202ba23SBartosz Golaszewski { 2549202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 2559202ba23SBartosz Golaszewski struct dentry *evfile; 2569202ba23SBartosz Golaszewski struct gpio_chip *gc; 2579202ba23SBartosz Golaszewski char *name; 2589202ba23SBartosz Golaszewski int i; 2599202ba23SBartosz Golaszewski 2609202ba23SBartosz Golaszewski gc = &chip->gc; 2619202ba23SBartosz Golaszewski 2629202ba23SBartosz Golaszewski chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir); 2639202ba23SBartosz Golaszewski if (!chip->dbg_dir) 2649202ba23SBartosz Golaszewski goto err; 2659202ba23SBartosz Golaszewski 2669202ba23SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 2679202ba23SBartosz Golaszewski name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); 2689202ba23SBartosz Golaszewski if (!name) 2699202ba23SBartosz Golaszewski goto err; 2709202ba23SBartosz Golaszewski 2719202ba23SBartosz Golaszewski priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 2729202ba23SBartosz Golaszewski if (!priv) 2739202ba23SBartosz Golaszewski goto err; 2749202ba23SBartosz Golaszewski 2759202ba23SBartosz Golaszewski priv->chip = chip; 2769202ba23SBartosz Golaszewski priv->offset = i; 2779202ba23SBartosz Golaszewski priv->desc = &gc->gpiodev->descs[i]; 2789202ba23SBartosz Golaszewski 2799202ba23SBartosz Golaszewski evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, 2809202ba23SBartosz Golaszewski &gpio_mockup_event_ops); 2819202ba23SBartosz Golaszewski if (!evfile) 2829202ba23SBartosz Golaszewski goto err; 2839202ba23SBartosz Golaszewski } 2849202ba23SBartosz Golaszewski 2859202ba23SBartosz Golaszewski return; 2869202ba23SBartosz Golaszewski 2879202ba23SBartosz Golaszewski err: 2889202ba23SBartosz Golaszewski dev_err(dev, "error creating debugfs directory\n"); 2899202ba23SBartosz Golaszewski } 2909202ba23SBartosz Golaszewski 291ca409160SBartosz Golaszewski static int gpio_mockup_add(struct device *dev, 292ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip, 2930f98dd1bSBamvor Jian Zhang const char *name, int base, int ngpio) 2940f98dd1bSBamvor Jian Zhang { 295ca409160SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 2968a68ea00SBartosz Golaszewski int ret; 2970f98dd1bSBamvor Jian Zhang 298ca409160SBartosz Golaszewski gc->base = base; 299ca409160SBartosz Golaszewski gc->ngpio = ngpio; 300ca409160SBartosz Golaszewski gc->label = name; 301ca409160SBartosz Golaszewski gc->owner = THIS_MODULE; 302ca409160SBartosz Golaszewski gc->parent = dev; 303ca409160SBartosz Golaszewski gc->get = gpio_mockup_get; 304ca409160SBartosz Golaszewski gc->set = gpio_mockup_set; 305ca409160SBartosz Golaszewski gc->direction_output = gpio_mockup_dirout; 306ca409160SBartosz Golaszewski gc->direction_input = gpio_mockup_dirin; 307ca409160SBartosz Golaszewski gc->get_direction = gpio_mockup_get_direction; 308e2ff7408SBartosz Golaszewski gc->to_irq = gpio_mockup_to_irq; 309ca409160SBartosz Golaszewski 310ca409160SBartosz Golaszewski chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio, 3110f98dd1bSBamvor Jian Zhang GFP_KERNEL); 312e4ba07bfSBartosz Golaszewski if (!chip->lines) 313e4ba07bfSBartosz Golaszewski return -ENOMEM; 314ca409160SBartosz Golaszewski 3158a68ea00SBartosz Golaszewski if (gpio_mockup_named_lines) { 3168a68ea00SBartosz Golaszewski ret = gpio_mockup_name_lines(dev, chip); 3178a68ea00SBartosz Golaszewski if (ret) 3188a68ea00SBartosz Golaszewski return ret; 3198a68ea00SBartosz Golaszewski } 3208a68ea00SBartosz Golaszewski 321e2ff7408SBartosz Golaszewski ret = gpio_mockup_irqchip_setup(dev, chip); 322e2ff7408SBartosz Golaszewski if (ret) 323e2ff7408SBartosz Golaszewski return ret; 324e2ff7408SBartosz Golaszewski 3259202ba23SBartosz Golaszewski ret = devm_gpiochip_add_data(dev, &chip->gc, chip); 3269202ba23SBartosz Golaszewski if (ret) 3279202ba23SBartosz Golaszewski return ret; 3289202ba23SBartosz Golaszewski 3299202ba23SBartosz Golaszewski if (gpio_mockup_dbg_dir) 3309202ba23SBartosz Golaszewski gpio_mockup_debugfs_setup(dev, chip); 3319202ba23SBartosz Golaszewski 3329202ba23SBartosz Golaszewski return 0; 3330f98dd1bSBamvor Jian Zhang } 3340f98dd1bSBamvor Jian Zhang 335ca409160SBartosz Golaszewski static int gpio_mockup_probe(struct platform_device *pdev) 3360f98dd1bSBamvor Jian Zhang { 337*b652336dSBartosz Golaszewski int ret, i, base, ngpio, num_chips; 338364ac456SBartosz Golaszewski struct device *dev = &pdev->dev; 339*b652336dSBartosz Golaszewski struct gpio_mockup_chip *chips; 340ad6d8004SBartosz Golaszewski char *chip_name; 3410f98dd1bSBamvor Jian Zhang 342b6c2e77dSBartosz Golaszewski if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2)) 3430f98dd1bSBamvor Jian Zhang return -EINVAL; 3440f98dd1bSBamvor Jian Zhang 345*b652336dSBartosz Golaszewski /* Each chip is described by two values. */ 346*b652336dSBartosz Golaszewski num_chips = gpio_mockup_params_nr / 2; 347*b652336dSBartosz Golaszewski 348*b652336dSBartosz Golaszewski chips = devm_kzalloc(dev, sizeof(*chips) * num_chips, GFP_KERNEL); 349ca409160SBartosz Golaszewski if (!chips) 3500f98dd1bSBamvor Jian Zhang return -ENOMEM; 3510f98dd1bSBamvor Jian Zhang 352ca409160SBartosz Golaszewski platform_set_drvdata(pdev, chips); 3530f98dd1bSBamvor Jian Zhang 354*b652336dSBartosz Golaszewski for (i = 0; i < num_chips; i++) { 3550f98dd1bSBamvor Jian Zhang base = gpio_mockup_ranges[i * 2]; 356ca409160SBartosz Golaszewski 3570f98dd1bSBamvor Jian Zhang if (base == -1) 3580f98dd1bSBamvor Jian Zhang ngpio = gpio_mockup_ranges[i * 2 + 1]; 3590f98dd1bSBamvor Jian Zhang else 3600f98dd1bSBamvor Jian Zhang ngpio = gpio_mockup_ranges[i * 2 + 1] - base; 3610f98dd1bSBamvor Jian Zhang 3620f98dd1bSBamvor Jian Zhang if (ngpio >= 0) { 363ad6d8004SBartosz Golaszewski chip_name = devm_kasprintf(dev, GFP_KERNEL, 364ca409160SBartosz Golaszewski "%s-%c", GPIO_MOCKUP_NAME, 365ca409160SBartosz Golaszewski gpio_mockup_name_start + i); 366ad6d8004SBartosz Golaszewski if (!chip_name) 367ad6d8004SBartosz Golaszewski return -ENOMEM; 368ad6d8004SBartosz Golaszewski 369ca409160SBartosz Golaszewski ret = gpio_mockup_add(dev, &chips[i], 3700f98dd1bSBamvor Jian Zhang chip_name, base, ngpio); 3710f98dd1bSBamvor Jian Zhang } else { 3720f98dd1bSBamvor Jian Zhang ret = -1; 3730f98dd1bSBamvor Jian Zhang } 374ca409160SBartosz Golaszewski 3750f98dd1bSBamvor Jian Zhang if (ret) { 3760f98dd1bSBamvor Jian Zhang dev_err(dev, "gpio<%d..%d> add failed\n", 377e4ba07bfSBartosz Golaszewski base, base < 0 ? ngpio : base + ngpio); 3780f98dd1bSBamvor Jian Zhang 3790f98dd1bSBamvor Jian Zhang return ret; 3800f98dd1bSBamvor Jian Zhang } 3810f98dd1bSBamvor Jian Zhang } 3820f98dd1bSBamvor Jian Zhang 3830f98dd1bSBamvor Jian Zhang return 0; 3840f98dd1bSBamvor Jian Zhang } 3850f98dd1bSBamvor Jian Zhang 386ca409160SBartosz Golaszewski static struct platform_driver gpio_mockup_driver = { 3870f98dd1bSBamvor Jian Zhang .driver = { 388ca409160SBartosz Golaszewski .name = GPIO_MOCKUP_NAME, 3890f98dd1bSBamvor Jian Zhang }, 390ca409160SBartosz Golaszewski .probe = gpio_mockup_probe, 3910f98dd1bSBamvor Jian Zhang }; 3920f98dd1bSBamvor Jian Zhang 3930f98dd1bSBamvor Jian Zhang static struct platform_device *pdev; 3940f98dd1bSBamvor Jian Zhang static int __init mock_device_init(void) 3950f98dd1bSBamvor Jian Zhang { 3960f98dd1bSBamvor Jian Zhang int err; 3970f98dd1bSBamvor Jian Zhang 3989202ba23SBartosz Golaszewski gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); 3999202ba23SBartosz Golaszewski if (!gpio_mockup_dbg_dir) 4009202ba23SBartosz Golaszewski pr_err("%s: error creating debugfs directory\n", 4019202ba23SBartosz Golaszewski GPIO_MOCKUP_NAME); 4029202ba23SBartosz Golaszewski 403ca409160SBartosz Golaszewski pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1); 4040f98dd1bSBamvor Jian Zhang if (!pdev) 4050f98dd1bSBamvor Jian Zhang return -ENOMEM; 4060f98dd1bSBamvor Jian Zhang 4070f98dd1bSBamvor Jian Zhang err = platform_device_add(pdev); 4080f98dd1bSBamvor Jian Zhang if (err) { 4090f98dd1bSBamvor Jian Zhang platform_device_put(pdev); 4100f98dd1bSBamvor Jian Zhang return err; 4110f98dd1bSBamvor Jian Zhang } 4120f98dd1bSBamvor Jian Zhang 413ca409160SBartosz Golaszewski err = platform_driver_register(&gpio_mockup_driver); 4140f98dd1bSBamvor Jian Zhang if (err) { 4150f98dd1bSBamvor Jian Zhang platform_device_unregister(pdev); 4160f98dd1bSBamvor Jian Zhang return err; 4170f98dd1bSBamvor Jian Zhang } 4180f98dd1bSBamvor Jian Zhang 4190f98dd1bSBamvor Jian Zhang return 0; 4200f98dd1bSBamvor Jian Zhang } 4210f98dd1bSBamvor Jian Zhang 4220f98dd1bSBamvor Jian Zhang static void __exit mock_device_exit(void) 4230f98dd1bSBamvor Jian Zhang { 4249202ba23SBartosz Golaszewski debugfs_remove_recursive(gpio_mockup_dbg_dir); 425ca409160SBartosz Golaszewski platform_driver_unregister(&gpio_mockup_driver); 4260f98dd1bSBamvor Jian Zhang platform_device_unregister(pdev); 4270f98dd1bSBamvor Jian Zhang } 4280f98dd1bSBamvor Jian Zhang 4290f98dd1bSBamvor Jian Zhang module_init(mock_device_init); 4300f98dd1bSBamvor Jian Zhang module_exit(mock_device_exit); 4310f98dd1bSBamvor Jian Zhang 4320f98dd1bSBamvor Jian Zhang MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); 4330f98dd1bSBamvor Jian Zhang MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); 4340f98dd1bSBamvor Jian Zhang MODULE_DESCRIPTION("GPIO Testing driver"); 4350f98dd1bSBamvor Jian Zhang MODULE_LICENSE("GPL v2"); 436