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 300f98dd1bSBamvor Jian Zhang 31ca409160SBartosz Golaszewski enum { 32c650c00cSBartosz Golaszewski GPIO_MOCKUP_DIR_OUT = 0, 33c650c00cSBartosz Golaszewski GPIO_MOCKUP_DIR_IN = 1, 340f98dd1bSBamvor Jian Zhang }; 350f98dd1bSBamvor Jian Zhang 360f98dd1bSBamvor Jian Zhang /* 370f98dd1bSBamvor Jian Zhang * struct gpio_pin_status - structure describing a GPIO status 380f98dd1bSBamvor Jian Zhang * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out 390f98dd1bSBamvor Jian Zhang * @value: Configures status of the gpio as 0(low) or 1(high) 400f98dd1bSBamvor Jian Zhang */ 41ca409160SBartosz Golaszewski struct gpio_mockup_line_status { 42ca409160SBartosz Golaszewski int dir; 430f98dd1bSBamvor Jian Zhang bool value; 44c7f5326fSBartosz Golaszewski bool irq_enabled; 450f98dd1bSBamvor Jian Zhang }; 460f98dd1bSBamvor Jian Zhang 47e2ff7408SBartosz Golaszewski struct gpio_mockup_irq_context { 48e2ff7408SBartosz Golaszewski struct irq_work work; 49e2ff7408SBartosz Golaszewski int irq; 50e2ff7408SBartosz Golaszewski }; 51e2ff7408SBartosz Golaszewski 52ca409160SBartosz Golaszewski struct gpio_mockup_chip { 530f98dd1bSBamvor Jian Zhang struct gpio_chip gc; 54ca409160SBartosz Golaszewski struct gpio_mockup_line_status *lines; 55e2ff7408SBartosz Golaszewski struct gpio_mockup_irq_context irq_ctx; 569202ba23SBartosz Golaszewski struct dentry *dbg_dir; 579202ba23SBartosz Golaszewski }; 589202ba23SBartosz Golaszewski 599202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private { 609202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip; 619202ba23SBartosz Golaszewski struct gpio_desc *desc; 629202ba23SBartosz Golaszewski int offset; 630f98dd1bSBamvor Jian Zhang }; 640f98dd1bSBamvor Jian Zhang 65ca409160SBartosz Golaszewski static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_GC << 1]; 660f98dd1bSBamvor Jian Zhang static int gpio_mockup_params_nr; 670f98dd1bSBamvor Jian Zhang module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400); 680f98dd1bSBamvor Jian Zhang 698a68ea00SBartosz Golaszewski static bool gpio_mockup_named_lines; 708a68ea00SBartosz Golaszewski module_param_named(gpio_mockup_named_lines, 718a68ea00SBartosz Golaszewski gpio_mockup_named_lines, bool, 0400); 728a68ea00SBartosz Golaszewski 73ca409160SBartosz Golaszewski static const char gpio_mockup_name_start = 'A'; 749202ba23SBartosz Golaszewski static struct dentry *gpio_mockup_dbg_dir; 750f98dd1bSBamvor Jian Zhang 76ca409160SBartosz Golaszewski static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) 770f98dd1bSBamvor Jian Zhang { 78ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 790f98dd1bSBamvor Jian Zhang 80ca409160SBartosz Golaszewski return chip->lines[offset].value; 810f98dd1bSBamvor Jian Zhang } 820f98dd1bSBamvor Jian Zhang 83ca409160SBartosz Golaszewski static void gpio_mockup_set(struct gpio_chip *gc, unsigned int offset, 840f98dd1bSBamvor Jian Zhang int value) 850f98dd1bSBamvor Jian Zhang { 86ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 870f98dd1bSBamvor Jian Zhang 88ca409160SBartosz Golaszewski chip->lines[offset].value = !!value; 890f98dd1bSBamvor Jian Zhang } 900f98dd1bSBamvor Jian Zhang 91ca409160SBartosz Golaszewski static int gpio_mockup_dirout(struct gpio_chip *gc, unsigned int offset, 920f98dd1bSBamvor Jian Zhang int value) 930f98dd1bSBamvor Jian Zhang { 94ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 950f98dd1bSBamvor Jian Zhang 96ca409160SBartosz Golaszewski gpio_mockup_set(gc, offset, value); 97c650c00cSBartosz Golaszewski chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; 98ca409160SBartosz Golaszewski 990f98dd1bSBamvor Jian Zhang return 0; 1000f98dd1bSBamvor Jian Zhang } 1010f98dd1bSBamvor Jian Zhang 102ca409160SBartosz Golaszewski static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) 1030f98dd1bSBamvor Jian Zhang { 104ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1050f98dd1bSBamvor Jian Zhang 106c650c00cSBartosz Golaszewski chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; 107ca409160SBartosz Golaszewski 1080f98dd1bSBamvor Jian Zhang return 0; 1090f98dd1bSBamvor Jian Zhang } 1100f98dd1bSBamvor Jian Zhang 111ca409160SBartosz Golaszewski static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) 1120f98dd1bSBamvor Jian Zhang { 113ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1140f98dd1bSBamvor Jian Zhang 115ca409160SBartosz Golaszewski return chip->lines[offset].dir; 1160f98dd1bSBamvor Jian Zhang } 1170f98dd1bSBamvor Jian Zhang 1188a68ea00SBartosz Golaszewski static int gpio_mockup_name_lines(struct device *dev, 1198a68ea00SBartosz Golaszewski struct gpio_mockup_chip *chip) 1208a68ea00SBartosz Golaszewski { 1218a68ea00SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 1228a68ea00SBartosz Golaszewski char **names; 1238a68ea00SBartosz Golaszewski int i; 1248a68ea00SBartosz Golaszewski 1258a68ea00SBartosz Golaszewski names = devm_kzalloc(dev, sizeof(char *) * gc->ngpio, GFP_KERNEL); 1268a68ea00SBartosz Golaszewski if (!names) 1278a68ea00SBartosz Golaszewski return -ENOMEM; 1288a68ea00SBartosz Golaszewski 1298a68ea00SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 1308a68ea00SBartosz Golaszewski names[i] = devm_kasprintf(dev, GFP_KERNEL, 1318a68ea00SBartosz Golaszewski "%s-%d", gc->label, i); 1328a68ea00SBartosz Golaszewski if (!names[i]) 1338a68ea00SBartosz Golaszewski return -ENOMEM; 1348a68ea00SBartosz Golaszewski } 1358a68ea00SBartosz Golaszewski 1368a68ea00SBartosz Golaszewski gc->names = (const char *const *)names; 1378a68ea00SBartosz Golaszewski 1388a68ea00SBartosz Golaszewski return 0; 1398a68ea00SBartosz Golaszewski } 1408a68ea00SBartosz Golaszewski 141e2ff7408SBartosz Golaszewski static int gpio_mockup_to_irq(struct gpio_chip *chip, unsigned int offset) 142e2ff7408SBartosz Golaszewski { 143e2ff7408SBartosz Golaszewski return chip->irq_base + offset; 144e2ff7408SBartosz Golaszewski } 145e2ff7408SBartosz Golaszewski 146c7f5326fSBartosz Golaszewski static void gpio_mockup_irqmask(struct irq_data *data) 147c7f5326fSBartosz Golaszewski { 148c7f5326fSBartosz Golaszewski struct gpio_chip *gc = irq_data_get_irq_chip_data(data); 149c7f5326fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 150c7f5326fSBartosz Golaszewski 151c7f5326fSBartosz Golaszewski chip->lines[data->irq - gc->irq_base].irq_enabled = false; 152c7f5326fSBartosz Golaszewski } 153c7f5326fSBartosz Golaszewski 154c7f5326fSBartosz Golaszewski static void gpio_mockup_irqunmask(struct irq_data *data) 155c7f5326fSBartosz Golaszewski { 156c7f5326fSBartosz Golaszewski struct gpio_chip *gc = irq_data_get_irq_chip_data(data); 157c7f5326fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 158c7f5326fSBartosz Golaszewski 159c7f5326fSBartosz Golaszewski chip->lines[data->irq - gc->irq_base].irq_enabled = true; 160c7f5326fSBartosz Golaszewski } 161e2ff7408SBartosz Golaszewski 162e2ff7408SBartosz Golaszewski static struct irq_chip gpio_mockup_irqchip = { 163e2ff7408SBartosz Golaszewski .name = GPIO_MOCKUP_NAME, 164e2ff7408SBartosz Golaszewski .irq_mask = gpio_mockup_irqmask, 165e2ff7408SBartosz Golaszewski .irq_unmask = gpio_mockup_irqunmask, 166e2ff7408SBartosz Golaszewski }; 167e2ff7408SBartosz Golaszewski 168e2ff7408SBartosz Golaszewski static void gpio_mockup_handle_irq(struct irq_work *work) 169e2ff7408SBartosz Golaszewski { 170e2ff7408SBartosz Golaszewski struct gpio_mockup_irq_context *irq_ctx; 171e2ff7408SBartosz Golaszewski 172e2ff7408SBartosz Golaszewski irq_ctx = container_of(work, struct gpio_mockup_irq_context, work); 173e2ff7408SBartosz Golaszewski handle_simple_irq(irq_to_desc(irq_ctx->irq)); 174e2ff7408SBartosz Golaszewski } 175e2ff7408SBartosz Golaszewski 176e2ff7408SBartosz Golaszewski static int gpio_mockup_irqchip_setup(struct device *dev, 177e2ff7408SBartosz Golaszewski struct gpio_mockup_chip *chip) 178e2ff7408SBartosz Golaszewski { 179e2ff7408SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 180e2ff7408SBartosz Golaszewski int irq_base, i; 181e2ff7408SBartosz Golaszewski 18262a7ca07SBartosz Golaszewski irq_base = devm_irq_alloc_descs(dev, -1, 0, gc->ngpio, 0); 183e2ff7408SBartosz Golaszewski if (irq_base < 0) 184e2ff7408SBartosz Golaszewski return irq_base; 185e2ff7408SBartosz Golaszewski 186e2ff7408SBartosz Golaszewski gc->irq_base = irq_base; 187e2ff7408SBartosz Golaszewski gc->irqchip = &gpio_mockup_irqchip; 188e2ff7408SBartosz Golaszewski 189e2ff7408SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 190e2ff7408SBartosz Golaszewski irq_set_chip(irq_base + i, gc->irqchip); 191c7f5326fSBartosz Golaszewski irq_set_chip_data(irq_base + i, gc); 192e2ff7408SBartosz Golaszewski irq_set_handler(irq_base + i, &handle_simple_irq); 193e2ff7408SBartosz Golaszewski irq_modify_status(irq_base + i, 194e2ff7408SBartosz Golaszewski IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE); 195e2ff7408SBartosz Golaszewski } 196e2ff7408SBartosz Golaszewski 197e2ff7408SBartosz Golaszewski init_irq_work(&chip->irq_ctx.work, gpio_mockup_handle_irq); 198e2ff7408SBartosz Golaszewski 199e2ff7408SBartosz Golaszewski return 0; 200e2ff7408SBartosz Golaszewski } 201e2ff7408SBartosz Golaszewski 2029202ba23SBartosz Golaszewski static ssize_t gpio_mockup_event_write(struct file *file, 2039202ba23SBartosz Golaszewski const char __user *usr_buf, 2049202ba23SBartosz Golaszewski size_t size, loff_t *ppos) 2059202ba23SBartosz Golaszewski { 2069202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 2079202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip; 2089202ba23SBartosz Golaszewski struct seq_file *sfile; 2099202ba23SBartosz Golaszewski struct gpio_desc *desc; 2109202ba23SBartosz Golaszewski struct gpio_chip *gc; 2116f9b3e77SBartosz Golaszewski int rv, val; 2129202ba23SBartosz Golaszewski 2136f9b3e77SBartosz Golaszewski rv = kstrtoint_from_user(usr_buf, size, 0, &val); 2146f9b3e77SBartosz Golaszewski if (rv) 2156f9b3e77SBartosz Golaszewski return rv; 2166f9b3e77SBartosz Golaszewski if (val != 0 && val != 1) 2176f9b3e77SBartosz Golaszewski return -EINVAL; 2186f9b3e77SBartosz Golaszewski 219650b57b0SBartosz Golaszewski sfile = file->private_data; 220650b57b0SBartosz Golaszewski priv = sfile->private; 221650b57b0SBartosz Golaszewski desc = priv->desc; 222650b57b0SBartosz Golaszewski chip = priv->chip; 223650b57b0SBartosz Golaszewski gc = &chip->gc; 224c7f5326fSBartosz Golaszewski 225650b57b0SBartosz Golaszewski if (chip->lines[priv->offset].irq_enabled) { 2269202ba23SBartosz Golaszewski gpiod_set_value_cansleep(desc, val); 2279202ba23SBartosz Golaszewski priv->chip->irq_ctx.irq = gc->irq_base + priv->offset; 2289202ba23SBartosz Golaszewski irq_work_queue(&priv->chip->irq_ctx.work); 229650b57b0SBartosz Golaszewski } 2309202ba23SBartosz Golaszewski 2319202ba23SBartosz Golaszewski return size; 2329202ba23SBartosz Golaszewski } 2339202ba23SBartosz Golaszewski 2349202ba23SBartosz Golaszewski static int gpio_mockup_event_open(struct inode *inode, struct file *file) 2359202ba23SBartosz Golaszewski { 2369202ba23SBartosz Golaszewski return single_open(file, NULL, inode->i_private); 2379202ba23SBartosz Golaszewski } 2389202ba23SBartosz Golaszewski 2399202ba23SBartosz Golaszewski static const struct file_operations gpio_mockup_event_ops = { 2409202ba23SBartosz Golaszewski .owner = THIS_MODULE, 2419202ba23SBartosz Golaszewski .open = gpio_mockup_event_open, 2429202ba23SBartosz Golaszewski .write = gpio_mockup_event_write, 2439202ba23SBartosz Golaszewski .llseek = no_llseek, 2449202ba23SBartosz Golaszewski }; 2459202ba23SBartosz Golaszewski 2469202ba23SBartosz Golaszewski static void gpio_mockup_debugfs_setup(struct device *dev, 2479202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip) 2489202ba23SBartosz Golaszewski { 2499202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 2509202ba23SBartosz Golaszewski struct dentry *evfile; 2519202ba23SBartosz Golaszewski struct gpio_chip *gc; 2529202ba23SBartosz Golaszewski char *name; 2539202ba23SBartosz Golaszewski int i; 2549202ba23SBartosz Golaszewski 2559202ba23SBartosz Golaszewski gc = &chip->gc; 2569202ba23SBartosz Golaszewski 2579202ba23SBartosz Golaszewski chip->dbg_dir = debugfs_create_dir(gc->label, gpio_mockup_dbg_dir); 2589202ba23SBartosz Golaszewski if (!chip->dbg_dir) 2599202ba23SBartosz Golaszewski goto err; 2609202ba23SBartosz Golaszewski 2619202ba23SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 2629202ba23SBartosz Golaszewski name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); 2639202ba23SBartosz Golaszewski if (!name) 2649202ba23SBartosz Golaszewski goto err; 2659202ba23SBartosz Golaszewski 2669202ba23SBartosz Golaszewski priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 2679202ba23SBartosz Golaszewski if (!priv) 2689202ba23SBartosz Golaszewski goto err; 2699202ba23SBartosz Golaszewski 2709202ba23SBartosz Golaszewski priv->chip = chip; 2719202ba23SBartosz Golaszewski priv->offset = i; 2729202ba23SBartosz Golaszewski priv->desc = &gc->gpiodev->descs[i]; 2739202ba23SBartosz Golaszewski 2749202ba23SBartosz Golaszewski evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, 2759202ba23SBartosz Golaszewski &gpio_mockup_event_ops); 2769202ba23SBartosz Golaszewski if (!evfile) 2779202ba23SBartosz Golaszewski goto err; 2789202ba23SBartosz Golaszewski } 2799202ba23SBartosz Golaszewski 2809202ba23SBartosz Golaszewski return; 2819202ba23SBartosz Golaszewski 2829202ba23SBartosz Golaszewski err: 2839202ba23SBartosz Golaszewski dev_err(dev, "error creating debugfs directory\n"); 2849202ba23SBartosz Golaszewski } 2859202ba23SBartosz Golaszewski 286ca409160SBartosz Golaszewski static int gpio_mockup_add(struct device *dev, 287ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip, 2880f98dd1bSBamvor Jian Zhang const char *name, int base, int ngpio) 2890f98dd1bSBamvor Jian Zhang { 290ca409160SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 2918a68ea00SBartosz Golaszewski int ret; 2920f98dd1bSBamvor Jian Zhang 293ca409160SBartosz Golaszewski gc->base = base; 294ca409160SBartosz Golaszewski gc->ngpio = ngpio; 295ca409160SBartosz Golaszewski gc->label = name; 296ca409160SBartosz Golaszewski gc->owner = THIS_MODULE; 297ca409160SBartosz Golaszewski gc->parent = dev; 298ca409160SBartosz Golaszewski gc->get = gpio_mockup_get; 299ca409160SBartosz Golaszewski gc->set = gpio_mockup_set; 300ca409160SBartosz Golaszewski gc->direction_output = gpio_mockup_dirout; 301ca409160SBartosz Golaszewski gc->direction_input = gpio_mockup_dirin; 302ca409160SBartosz Golaszewski gc->get_direction = gpio_mockup_get_direction; 303e2ff7408SBartosz Golaszewski gc->to_irq = gpio_mockup_to_irq; 304ca409160SBartosz Golaszewski 305ca409160SBartosz Golaszewski chip->lines = devm_kzalloc(dev, sizeof(*chip->lines) * gc->ngpio, 3060f98dd1bSBamvor Jian Zhang GFP_KERNEL); 307e4ba07bfSBartosz Golaszewski if (!chip->lines) 308e4ba07bfSBartosz Golaszewski return -ENOMEM; 309ca409160SBartosz Golaszewski 3108a68ea00SBartosz Golaszewski if (gpio_mockup_named_lines) { 3118a68ea00SBartosz Golaszewski ret = gpio_mockup_name_lines(dev, chip); 3128a68ea00SBartosz Golaszewski if (ret) 3138a68ea00SBartosz Golaszewski return ret; 3148a68ea00SBartosz Golaszewski } 3158a68ea00SBartosz Golaszewski 316e2ff7408SBartosz Golaszewski ret = gpio_mockup_irqchip_setup(dev, chip); 317e2ff7408SBartosz Golaszewski if (ret) 318e2ff7408SBartosz Golaszewski return ret; 319e2ff7408SBartosz Golaszewski 3209202ba23SBartosz Golaszewski ret = devm_gpiochip_add_data(dev, &chip->gc, chip); 3219202ba23SBartosz Golaszewski if (ret) 3229202ba23SBartosz Golaszewski return ret; 3239202ba23SBartosz Golaszewski 3249202ba23SBartosz Golaszewski if (gpio_mockup_dbg_dir) 3259202ba23SBartosz Golaszewski gpio_mockup_debugfs_setup(dev, chip); 3269202ba23SBartosz Golaszewski 3279202ba23SBartosz Golaszewski return 0; 3280f98dd1bSBamvor Jian Zhang } 3290f98dd1bSBamvor Jian Zhang 330ca409160SBartosz Golaszewski static int gpio_mockup_probe(struct platform_device *pdev) 3310f98dd1bSBamvor Jian Zhang { 332ca409160SBartosz Golaszewski struct gpio_mockup_chip *chips; 333364ac456SBartosz Golaszewski struct device *dev = &pdev->dev; 334364ac456SBartosz Golaszewski int ret, i, base, ngpio; 335ad6d8004SBartosz Golaszewski char *chip_name; 3360f98dd1bSBamvor Jian Zhang 337*b6c2e77dSBartosz Golaszewski if (gpio_mockup_params_nr < 2 || (gpio_mockup_params_nr % 2)) 3380f98dd1bSBamvor Jian Zhang return -EINVAL; 3390f98dd1bSBamvor Jian Zhang 340ca409160SBartosz Golaszewski chips = devm_kzalloc(dev, 341ca409160SBartosz Golaszewski sizeof(*chips) * (gpio_mockup_params_nr >> 1), 3420f98dd1bSBamvor Jian Zhang GFP_KERNEL); 343ca409160SBartosz Golaszewski if (!chips) 3440f98dd1bSBamvor Jian Zhang return -ENOMEM; 3450f98dd1bSBamvor Jian Zhang 346ca409160SBartosz Golaszewski platform_set_drvdata(pdev, chips); 3470f98dd1bSBamvor Jian Zhang 3480f98dd1bSBamvor Jian Zhang for (i = 0; i < gpio_mockup_params_nr >> 1; i++) { 3490f98dd1bSBamvor Jian Zhang base = gpio_mockup_ranges[i * 2]; 350ca409160SBartosz Golaszewski 3510f98dd1bSBamvor Jian Zhang if (base == -1) 3520f98dd1bSBamvor Jian Zhang ngpio = gpio_mockup_ranges[i * 2 + 1]; 3530f98dd1bSBamvor Jian Zhang else 3540f98dd1bSBamvor Jian Zhang ngpio = gpio_mockup_ranges[i * 2 + 1] - base; 3550f98dd1bSBamvor Jian Zhang 3560f98dd1bSBamvor Jian Zhang if (ngpio >= 0) { 357ad6d8004SBartosz Golaszewski chip_name = devm_kasprintf(dev, GFP_KERNEL, 358ca409160SBartosz Golaszewski "%s-%c", GPIO_MOCKUP_NAME, 359ca409160SBartosz Golaszewski gpio_mockup_name_start + i); 360ad6d8004SBartosz Golaszewski if (!chip_name) 361ad6d8004SBartosz Golaszewski return -ENOMEM; 362ad6d8004SBartosz Golaszewski 363ca409160SBartosz Golaszewski ret = gpio_mockup_add(dev, &chips[i], 3640f98dd1bSBamvor Jian Zhang chip_name, base, ngpio); 3650f98dd1bSBamvor Jian Zhang } else { 3660f98dd1bSBamvor Jian Zhang ret = -1; 3670f98dd1bSBamvor Jian Zhang } 368ca409160SBartosz Golaszewski 3690f98dd1bSBamvor Jian Zhang if (ret) { 3700f98dd1bSBamvor Jian Zhang dev_err(dev, "gpio<%d..%d> add failed\n", 371e4ba07bfSBartosz Golaszewski base, base < 0 ? ngpio : base + ngpio); 3720f98dd1bSBamvor Jian Zhang 3730f98dd1bSBamvor Jian Zhang return ret; 3740f98dd1bSBamvor Jian Zhang } 3750f98dd1bSBamvor Jian Zhang } 3760f98dd1bSBamvor Jian Zhang 3770f98dd1bSBamvor Jian Zhang return 0; 3780f98dd1bSBamvor Jian Zhang } 3790f98dd1bSBamvor Jian Zhang 380ca409160SBartosz Golaszewski static struct platform_driver gpio_mockup_driver = { 3810f98dd1bSBamvor Jian Zhang .driver = { 382ca409160SBartosz Golaszewski .name = GPIO_MOCKUP_NAME, 3830f98dd1bSBamvor Jian Zhang }, 384ca409160SBartosz Golaszewski .probe = gpio_mockup_probe, 3850f98dd1bSBamvor Jian Zhang }; 3860f98dd1bSBamvor Jian Zhang 3870f98dd1bSBamvor Jian Zhang static struct platform_device *pdev; 3880f98dd1bSBamvor Jian Zhang static int __init mock_device_init(void) 3890f98dd1bSBamvor Jian Zhang { 3900f98dd1bSBamvor Jian Zhang int err; 3910f98dd1bSBamvor Jian Zhang 3929202ba23SBartosz Golaszewski gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup-event", NULL); 3939202ba23SBartosz Golaszewski if (!gpio_mockup_dbg_dir) 3949202ba23SBartosz Golaszewski pr_err("%s: error creating debugfs directory\n", 3959202ba23SBartosz Golaszewski GPIO_MOCKUP_NAME); 3969202ba23SBartosz Golaszewski 397ca409160SBartosz Golaszewski pdev = platform_device_alloc(GPIO_MOCKUP_NAME, -1); 3980f98dd1bSBamvor Jian Zhang if (!pdev) 3990f98dd1bSBamvor Jian Zhang return -ENOMEM; 4000f98dd1bSBamvor Jian Zhang 4010f98dd1bSBamvor Jian Zhang err = platform_device_add(pdev); 4020f98dd1bSBamvor Jian Zhang if (err) { 4030f98dd1bSBamvor Jian Zhang platform_device_put(pdev); 4040f98dd1bSBamvor Jian Zhang return err; 4050f98dd1bSBamvor Jian Zhang } 4060f98dd1bSBamvor Jian Zhang 407ca409160SBartosz Golaszewski err = platform_driver_register(&gpio_mockup_driver); 4080f98dd1bSBamvor Jian Zhang if (err) { 4090f98dd1bSBamvor Jian Zhang platform_device_unregister(pdev); 4100f98dd1bSBamvor Jian Zhang return err; 4110f98dd1bSBamvor Jian Zhang } 4120f98dd1bSBamvor Jian Zhang 4130f98dd1bSBamvor Jian Zhang return 0; 4140f98dd1bSBamvor Jian Zhang } 4150f98dd1bSBamvor Jian Zhang 4160f98dd1bSBamvor Jian Zhang static void __exit mock_device_exit(void) 4170f98dd1bSBamvor Jian Zhang { 4189202ba23SBartosz Golaszewski debugfs_remove_recursive(gpio_mockup_dbg_dir); 419ca409160SBartosz Golaszewski platform_driver_unregister(&gpio_mockup_driver); 4200f98dd1bSBamvor Jian Zhang platform_device_unregister(pdev); 4210f98dd1bSBamvor Jian Zhang } 4220f98dd1bSBamvor Jian Zhang 4230f98dd1bSBamvor Jian Zhang module_init(mock_device_init); 4240f98dd1bSBamvor Jian Zhang module_exit(mock_device_exit); 4250f98dd1bSBamvor Jian Zhang 4260f98dd1bSBamvor Jian Zhang MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); 4270f98dd1bSBamvor Jian Zhang MODULE_AUTHOR("Bamvor Jian Zhang <bamvor.zhangjian@linaro.org>"); 4280f98dd1bSBamvor Jian Zhang MODULE_DESCRIPTION("GPIO Testing driver"); 4290f98dd1bSBamvor Jian Zhang MODULE_LICENSE("GPL v2"); 430