1058de348SBartosz Golaszewski // SPDX-License-Identifier: GPL-2.0+ 20f98dd1bSBamvor Jian Zhang /* 30f98dd1bSBamvor Jian Zhang * GPIO Testing Device Driver 40f98dd1bSBamvor Jian Zhang * 50f98dd1bSBamvor Jian Zhang * Copyright (C) 2014 Kamlakant Patel <kamlakant.patel@broadcom.com> 64f9a4cd6SLinus Walleij * Copyright (C) 2015-2016 Bamvor Jian Zhang <bamv2005@gmail.com> 7c60c7f4cSBartosz Golaszewski * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl> 80f98dd1bSBamvor Jian Zhang */ 90f98dd1bSBamvor Jian Zhang 100f98dd1bSBamvor Jian Zhang #include <linux/init.h> 110f98dd1bSBamvor Jian Zhang #include <linux/module.h> 120f98dd1bSBamvor Jian Zhang #include <linux/gpio/driver.h> 139202ba23SBartosz Golaszewski #include <linux/gpio/consumer.h> 140f98dd1bSBamvor Jian Zhang #include <linux/platform_device.h> 158a68ea00SBartosz Golaszewski #include <linux/slab.h> 16e2ff7408SBartosz Golaszewski #include <linux/interrupt.h> 17e2ff7408SBartosz Golaszewski #include <linux/irq.h> 18b4c495f0SBartosz Golaszewski #include <linux/irq_sim.h> 199202ba23SBartosz Golaszewski #include <linux/debugfs.h> 209202ba23SBartosz Golaszewski #include <linux/uaccess.h> 213ea47b44SBartosz Golaszewski #include <linux/property.h> 229202ba23SBartosz Golaszewski 239202ba23SBartosz Golaszewski #include "gpiolib.h" 240f98dd1bSBamvor Jian Zhang 25ca409160SBartosz Golaszewski #define GPIO_MOCKUP_NAME "gpio-mockup" 26ca409160SBartosz Golaszewski #define GPIO_MOCKUP_MAX_GC 10 27b652336dSBartosz Golaszewski /* 28b652336dSBartosz Golaszewski * We're storing two values per chip: the GPIO base and the number 29b652336dSBartosz Golaszewski * of GPIO lines. 30b652336dSBartosz Golaszewski */ 31b652336dSBartosz Golaszewski #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) 323ea47b44SBartosz Golaszewski /* Maximum of three properties + the sentinel. */ 333ea47b44SBartosz Golaszewski #define GPIO_MOCKUP_MAX_PROP 4 340f98dd1bSBamvor Jian Zhang 35f1bec99aSBartosz Golaszewski #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__) 36f1bec99aSBartosz Golaszewski 37ca409160SBartosz Golaszewski enum { 38bff466baSBartosz Golaszewski GPIO_MOCKUP_DIR_IN = 0, 39bff466baSBartosz Golaszewski GPIO_MOCKUP_DIR_OUT = 1, 400f98dd1bSBamvor Jian Zhang }; 410f98dd1bSBamvor Jian Zhang 420f98dd1bSBamvor Jian Zhang /* 430f98dd1bSBamvor Jian Zhang * struct gpio_pin_status - structure describing a GPIO status 440f98dd1bSBamvor Jian Zhang * @dir: Configures direction of gpio as "in" or "out", 0=in, 1=out 450f98dd1bSBamvor Jian Zhang * @value: Configures status of the gpio as 0(low) or 1(high) 460f98dd1bSBamvor Jian Zhang */ 47ca409160SBartosz Golaszewski struct gpio_mockup_line_status { 48ca409160SBartosz Golaszewski int dir; 495b7908c0SBartosz Golaszewski int value; 502a9e2740SBartosz Golaszewski int pull; 51e2ff7408SBartosz Golaszewski }; 52e2ff7408SBartosz Golaszewski 53ca409160SBartosz Golaszewski struct gpio_mockup_chip { 540f98dd1bSBamvor Jian Zhang struct gpio_chip gc; 55ca409160SBartosz Golaszewski struct gpio_mockup_line_status *lines; 56b4c495f0SBartosz Golaszewski struct irq_sim irqsim; 579202ba23SBartosz Golaszewski struct dentry *dbg_dir; 589212492fSBartosz Golaszewski struct mutex lock; 599202ba23SBartosz Golaszewski }; 609202ba23SBartosz Golaszewski 619202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private { 629202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip; 639202ba23SBartosz Golaszewski struct gpio_desc *desc; 6483336668SBartosz Golaszewski unsigned int offset; 650f98dd1bSBamvor Jian Zhang }; 660f98dd1bSBamvor Jian Zhang 67b652336dSBartosz Golaszewski static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; 68e63a006fSBartosz Golaszewski static int gpio_mockup_num_ranges; 69e63a006fSBartosz Golaszewski module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400); 700f98dd1bSBamvor Jian Zhang 718a68ea00SBartosz Golaszewski static bool gpio_mockup_named_lines; 728a68ea00SBartosz Golaszewski module_param_named(gpio_mockup_named_lines, 738a68ea00SBartosz Golaszewski gpio_mockup_named_lines, bool, 0400); 748a68ea00SBartosz Golaszewski 759202ba23SBartosz Golaszewski static struct dentry *gpio_mockup_dbg_dir; 760f98dd1bSBamvor Jian Zhang 77cd9835f1SBartosz Golaszewski static int gpio_mockup_range_base(unsigned int index) 78cd9835f1SBartosz Golaszewski { 79cd9835f1SBartosz Golaszewski return gpio_mockup_ranges[index * 2]; 80cd9835f1SBartosz Golaszewski } 81cd9835f1SBartosz Golaszewski 82cd9835f1SBartosz Golaszewski static int gpio_mockup_range_ngpio(unsigned int index) 83cd9835f1SBartosz Golaszewski { 84cd9835f1SBartosz Golaszewski return gpio_mockup_ranges[index * 2 + 1]; 85cd9835f1SBartosz Golaszewski } 86cd9835f1SBartosz Golaszewski 87e09313ceSBartosz Golaszewski static int __gpio_mockup_get(struct gpio_mockup_chip *chip, 88e09313ceSBartosz Golaszewski unsigned int offset) 890f98dd1bSBamvor Jian Zhang { 90ca409160SBartosz Golaszewski return chip->lines[offset].value; 910f98dd1bSBamvor Jian Zhang } 920f98dd1bSBamvor Jian Zhang 939212492fSBartosz Golaszewski static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset) 949212492fSBartosz Golaszewski { 959212492fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 969212492fSBartosz Golaszewski int val; 979212492fSBartosz Golaszewski 989212492fSBartosz Golaszewski mutex_lock(&chip->lock); 99e09313ceSBartosz Golaszewski val = __gpio_mockup_get(chip, offset); 1009212492fSBartosz Golaszewski mutex_unlock(&chip->lock); 1019212492fSBartosz Golaszewski 1029212492fSBartosz Golaszewski return val; 1039212492fSBartosz Golaszewski } 1049212492fSBartosz Golaszewski 105cbf1e092SBartosz Golaszewski static int gpio_mockup_get_multiple(struct gpio_chip *gc, 106cbf1e092SBartosz Golaszewski unsigned long *mask, unsigned long *bits) 107cbf1e092SBartosz Golaszewski { 108cbf1e092SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 109cbf1e092SBartosz Golaszewski unsigned int bit, val; 110cbf1e092SBartosz Golaszewski 111cbf1e092SBartosz Golaszewski mutex_lock(&chip->lock); 112cbf1e092SBartosz Golaszewski for_each_set_bit(bit, mask, gc->ngpio) { 113e09313ceSBartosz Golaszewski val = __gpio_mockup_get(chip, bit); 114cbf1e092SBartosz Golaszewski __assign_bit(bit, bits, val); 115cbf1e092SBartosz Golaszewski } 116cbf1e092SBartosz Golaszewski mutex_unlock(&chip->lock); 117cbf1e092SBartosz Golaszewski 118cbf1e092SBartosz Golaszewski return 0; 119cbf1e092SBartosz Golaszewski } 120cbf1e092SBartosz Golaszewski 121e09313ceSBartosz Golaszewski static void __gpio_mockup_set(struct gpio_mockup_chip *chip, 1222a9d742cSBartosz Golaszewski unsigned int offset, int value) 1230f98dd1bSBamvor Jian Zhang { 124ca409160SBartosz Golaszewski chip->lines[offset].value = !!value; 1250f98dd1bSBamvor Jian Zhang } 1260f98dd1bSBamvor Jian Zhang 1279212492fSBartosz Golaszewski static void gpio_mockup_set(struct gpio_chip *gc, 1289212492fSBartosz Golaszewski unsigned int offset, int value) 1299212492fSBartosz Golaszewski { 1309212492fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1319212492fSBartosz Golaszewski 1329212492fSBartosz Golaszewski mutex_lock(&chip->lock); 133e09313ceSBartosz Golaszewski __gpio_mockup_set(chip, offset, value); 1349212492fSBartosz Golaszewski mutex_unlock(&chip->lock); 1359212492fSBartosz Golaszewski } 1369212492fSBartosz Golaszewski 137c47bee95SBartosz Golaszewski static void gpio_mockup_set_multiple(struct gpio_chip *gc, 138c47bee95SBartosz Golaszewski unsigned long *mask, unsigned long *bits) 139c47bee95SBartosz Golaszewski { 1409212492fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 141c47bee95SBartosz Golaszewski unsigned int bit; 142c47bee95SBartosz Golaszewski 1439212492fSBartosz Golaszewski mutex_lock(&chip->lock); 144c47bee95SBartosz Golaszewski for_each_set_bit(bit, mask, gc->ngpio) 145e09313ceSBartosz Golaszewski __gpio_mockup_set(chip, bit, test_bit(bit, bits)); 1469212492fSBartosz Golaszewski mutex_unlock(&chip->lock); 147c47bee95SBartosz Golaszewski } 148c47bee95SBartosz Golaszewski 1492a9d742cSBartosz Golaszewski static int gpio_mockup_dirout(struct gpio_chip *gc, 1502a9d742cSBartosz Golaszewski unsigned int offset, int value) 1510f98dd1bSBamvor Jian Zhang { 152ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1530f98dd1bSBamvor Jian Zhang 1549212492fSBartosz Golaszewski mutex_lock(&chip->lock); 155c650c00cSBartosz Golaszewski chip->lines[offset].dir = GPIO_MOCKUP_DIR_OUT; 156e09313ceSBartosz Golaszewski __gpio_mockup_set(chip, offset, value); 1579212492fSBartosz Golaszewski mutex_unlock(&chip->lock); 158ca409160SBartosz Golaszewski 1590f98dd1bSBamvor Jian Zhang return 0; 1600f98dd1bSBamvor Jian Zhang } 1610f98dd1bSBamvor Jian Zhang 162ca409160SBartosz Golaszewski static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset) 1630f98dd1bSBamvor Jian Zhang { 164ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1650f98dd1bSBamvor Jian Zhang 1669212492fSBartosz Golaszewski mutex_lock(&chip->lock); 167c650c00cSBartosz Golaszewski chip->lines[offset].dir = GPIO_MOCKUP_DIR_IN; 1689212492fSBartosz Golaszewski mutex_unlock(&chip->lock); 169ca409160SBartosz Golaszewski 1700f98dd1bSBamvor Jian Zhang return 0; 1710f98dd1bSBamvor Jian Zhang } 1720f98dd1bSBamvor Jian Zhang 173ca409160SBartosz Golaszewski static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset) 1740f98dd1bSBamvor Jian Zhang { 175ca409160SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1769212492fSBartosz Golaszewski int direction; 1770f98dd1bSBamvor Jian Zhang 1789212492fSBartosz Golaszewski mutex_lock(&chip->lock); 1799212492fSBartosz Golaszewski direction = !chip->lines[offset].dir; 1809212492fSBartosz Golaszewski mutex_unlock(&chip->lock); 1819212492fSBartosz Golaszewski 1829212492fSBartosz Golaszewski return direction; 1830f98dd1bSBamvor Jian Zhang } 1840f98dd1bSBamvor Jian Zhang 185b4c495f0SBartosz Golaszewski static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset) 186e2ff7408SBartosz Golaszewski { 187c7f5326fSBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 188c7f5326fSBartosz Golaszewski 189b4c495f0SBartosz Golaszewski return irq_sim_irqnum(&chip->irqsim, offset); 190e2ff7408SBartosz Golaszewski } 191e2ff7408SBartosz Golaszewski 1922a9e2740SBartosz Golaszewski static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset) 1932a9e2740SBartosz Golaszewski { 1942a9e2740SBartosz Golaszewski struct gpio_mockup_chip *chip = gpiochip_get_data(gc); 1952a9e2740SBartosz Golaszewski 1962a9e2740SBartosz Golaszewski __gpio_mockup_set(chip, offset, chip->lines[offset].pull); 1972a9e2740SBartosz Golaszewski } 1982a9e2740SBartosz Golaszewski 1992a9e2740SBartosz Golaszewski static ssize_t gpio_mockup_debugfs_read(struct file *file, 2002a9e2740SBartosz Golaszewski char __user *usr_buf, 2019202ba23SBartosz Golaszewski size_t size, loff_t *ppos) 2029202ba23SBartosz Golaszewski { 2039202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 2049202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip; 2059202ba23SBartosz Golaszewski struct seq_file *sfile; 2062a9e2740SBartosz Golaszewski struct gpio_chip *gc; 207*2583303dSBartosz Golaszewski int val, rv, cnt; 2082a9e2740SBartosz Golaszewski char buf[3]; 209*2583303dSBartosz Golaszewski 2102a9e2740SBartosz Golaszewski 2112a9e2740SBartosz Golaszewski if (*ppos != 0) 2122a9e2740SBartosz Golaszewski return 0; 2132a9e2740SBartosz Golaszewski 2142a9e2740SBartosz Golaszewski sfile = file->private_data; 2152a9e2740SBartosz Golaszewski priv = sfile->private; 2162a9e2740SBartosz Golaszewski chip = priv->chip; 2172a9e2740SBartosz Golaszewski gc = &chip->gc; 2182a9e2740SBartosz Golaszewski 2192a9e2740SBartosz Golaszewski val = gpio_mockup_get(gc, priv->offset); 220*2583303dSBartosz Golaszewski cnt = snprintf(buf, sizeof(buf), "%d\n", val); 2212a9e2740SBartosz Golaszewski 222*2583303dSBartosz Golaszewski rv = copy_to_user(usr_buf, buf, cnt); 2232a9e2740SBartosz Golaszewski if (rv) 2242a9e2740SBartosz Golaszewski return rv; 2252a9e2740SBartosz Golaszewski 226*2583303dSBartosz Golaszewski *ppos += cnt; 227*2583303dSBartosz Golaszewski return cnt; 2282a9e2740SBartosz Golaszewski } 2292a9e2740SBartosz Golaszewski 2302a9e2740SBartosz Golaszewski static ssize_t gpio_mockup_debugfs_write(struct file *file, 2312a9e2740SBartosz Golaszewski const char __user *usr_buf, 2322a9e2740SBartosz Golaszewski size_t size, loff_t *ppos) 2332a9e2740SBartosz Golaszewski { 2342a9e2740SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 2352a9e2740SBartosz Golaszewski int rv, val, curr, irq, irq_type; 2362a9e2740SBartosz Golaszewski struct gpio_mockup_chip *chip; 2372a9e2740SBartosz Golaszewski struct seq_file *sfile; 2389202ba23SBartosz Golaszewski struct gpio_desc *desc; 2392a9e2740SBartosz Golaszewski struct gpio_chip *gc; 2402a9e2740SBartosz Golaszewski struct irq_sim *sim; 2412a9e2740SBartosz Golaszewski 2422a9e2740SBartosz Golaszewski if (*ppos != 0) 2432a9e2740SBartosz Golaszewski return -EINVAL; 2449202ba23SBartosz Golaszewski 2456f9b3e77SBartosz Golaszewski rv = kstrtoint_from_user(usr_buf, size, 0, &val); 2466f9b3e77SBartosz Golaszewski if (rv) 2476f9b3e77SBartosz Golaszewski return rv; 2486f9b3e77SBartosz Golaszewski if (val != 0 && val != 1) 2496f9b3e77SBartosz Golaszewski return -EINVAL; 2506f9b3e77SBartosz Golaszewski 251650b57b0SBartosz Golaszewski sfile = file->private_data; 252650b57b0SBartosz Golaszewski priv = sfile->private; 253650b57b0SBartosz Golaszewski chip = priv->chip; 2542a9e2740SBartosz Golaszewski gc = &chip->gc; 2552a9e2740SBartosz Golaszewski desc = &gc->gpiodev->descs[priv->offset]; 2562a9e2740SBartosz Golaszewski sim = &chip->irqsim; 257c7f5326fSBartosz Golaszewski 2582a9e2740SBartosz Golaszewski mutex_lock(&chip->lock); 2592a9e2740SBartosz Golaszewski 2602a9e2740SBartosz Golaszewski if (test_bit(FLAG_REQUESTED, &desc->flags) && 2612a9e2740SBartosz Golaszewski !test_bit(FLAG_IS_OUT, &desc->flags)) { 2622a9e2740SBartosz Golaszewski curr = __gpio_mockup_get(chip, priv->offset); 2632a9e2740SBartosz Golaszewski if (curr == val) 2642a9e2740SBartosz Golaszewski goto out; 2652a9e2740SBartosz Golaszewski 2662a9e2740SBartosz Golaszewski irq = irq_sim_irqnum(sim, priv->offset); 2672a9e2740SBartosz Golaszewski irq_type = irq_get_trigger_type(irq); 2682a9e2740SBartosz Golaszewski 2692a9e2740SBartosz Golaszewski if ((val == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) || 2702a9e2740SBartosz Golaszewski (val == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) 2712a9e2740SBartosz Golaszewski irq_sim_fire(sim, priv->offset); 2722a9e2740SBartosz Golaszewski } 2732a9e2740SBartosz Golaszewski 2742a9e2740SBartosz Golaszewski /* Change the value unless we're actively driving the line. */ 2752a9e2740SBartosz Golaszewski if (!test_bit(FLAG_REQUESTED, &desc->flags) || 2762a9e2740SBartosz Golaszewski !test_bit(FLAG_IS_OUT, &desc->flags)) 2772a9e2740SBartosz Golaszewski __gpio_mockup_set(chip, priv->offset, val); 2782a9e2740SBartosz Golaszewski 2792a9e2740SBartosz Golaszewski out: 2802a9e2740SBartosz Golaszewski chip->lines[priv->offset].pull = val; 2812a9e2740SBartosz Golaszewski mutex_unlock(&chip->lock); 2829202ba23SBartosz Golaszewski 2839202ba23SBartosz Golaszewski return size; 2849202ba23SBartosz Golaszewski } 2859202ba23SBartosz Golaszewski 2862a9e2740SBartosz Golaszewski static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file) 2879202ba23SBartosz Golaszewski { 2889202ba23SBartosz Golaszewski return single_open(file, NULL, inode->i_private); 2899202ba23SBartosz Golaszewski } 2909202ba23SBartosz Golaszewski 2912a9e2740SBartosz Golaszewski /* 2922a9e2740SBartosz Golaszewski * Each mockup chip is represented by a directory named after the chip's device 2932a9e2740SBartosz Golaszewski * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by 2942a9e2740SBartosz Golaszewski * a file using the line's offset as the name under the chip's directory. 2952a9e2740SBartosz Golaszewski * 2962a9e2740SBartosz Golaszewski * Reading from the line's file yields the current *value*, writing to the 2972a9e2740SBartosz Golaszewski * line's file changes the current *pull*. Default pull for mockup lines is 2982a9e2740SBartosz Golaszewski * down. 2992a9e2740SBartosz Golaszewski * 3002a9e2740SBartosz Golaszewski * Examples: 3012a9e2740SBartosz Golaszewski * - when a line pulled down is requested in output mode and driven high, its 3022a9e2740SBartosz Golaszewski * value will return to 0 once it's released 3032a9e2740SBartosz Golaszewski * - when the line is requested in output mode and driven high, writing 0 to 3042a9e2740SBartosz Golaszewski * the corresponding debugfs file will change the pull to down but the 3052a9e2740SBartosz Golaszewski * reported value will still be 1 until the line is released 3062a9e2740SBartosz Golaszewski * - line requested in input mode always reports the same value as its pull 3072a9e2740SBartosz Golaszewski * configuration 3082a9e2740SBartosz Golaszewski * - when the line is requested in input mode and monitored for events, writing 3092a9e2740SBartosz Golaszewski * the same value to the debugfs file will be a noop, while writing the 3102a9e2740SBartosz Golaszewski * opposite value will generate a dummy interrupt with an appropriate edge 3112a9e2740SBartosz Golaszewski */ 3122a9e2740SBartosz Golaszewski static const struct file_operations gpio_mockup_debugfs_ops = { 3139202ba23SBartosz Golaszewski .owner = THIS_MODULE, 3142a9e2740SBartosz Golaszewski .open = gpio_mockup_debugfs_open, 3152a9e2740SBartosz Golaszewski .read = gpio_mockup_debugfs_read, 3162a9e2740SBartosz Golaszewski .write = gpio_mockup_debugfs_write, 3179202ba23SBartosz Golaszewski .llseek = no_llseek, 3189202ba23SBartosz Golaszewski }; 3199202ba23SBartosz Golaszewski 3209202ba23SBartosz Golaszewski static void gpio_mockup_debugfs_setup(struct device *dev, 3219202ba23SBartosz Golaszewski struct gpio_mockup_chip *chip) 3229202ba23SBartosz Golaszewski { 3239202ba23SBartosz Golaszewski struct gpio_mockup_dbgfs_private *priv; 324d51ee07aSBartosz Golaszewski struct dentry *evfile; 3259202ba23SBartosz Golaszewski struct gpio_chip *gc; 326ca8792afSBartosz Golaszewski const char *devname; 3279202ba23SBartosz Golaszewski char *name; 3289202ba23SBartosz Golaszewski int i; 3299202ba23SBartosz Golaszewski 3309202ba23SBartosz Golaszewski gc = &chip->gc; 331ca8792afSBartosz Golaszewski devname = dev_name(&gc->gpiodev->dev); 3329202ba23SBartosz Golaszewski 333ca8792afSBartosz Golaszewski chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir); 33446526c15SBartosz Golaszewski if (IS_ERR_OR_NULL(chip->dbg_dir)) 3359202ba23SBartosz Golaszewski goto err; 3369202ba23SBartosz Golaszewski 3379202ba23SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 3389202ba23SBartosz Golaszewski name = devm_kasprintf(dev, GFP_KERNEL, "%d", i); 3399202ba23SBartosz Golaszewski if (!name) 3409202ba23SBartosz Golaszewski goto err; 3419202ba23SBartosz Golaszewski 3429202ba23SBartosz Golaszewski priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 3439202ba23SBartosz Golaszewski if (!priv) 3449202ba23SBartosz Golaszewski goto err; 3459202ba23SBartosz Golaszewski 3469202ba23SBartosz Golaszewski priv->chip = chip; 3479202ba23SBartosz Golaszewski priv->offset = i; 3489202ba23SBartosz Golaszewski priv->desc = &gc->gpiodev->descs[i]; 3499202ba23SBartosz Golaszewski 3509202ba23SBartosz Golaszewski evfile = debugfs_create_file(name, 0200, chip->dbg_dir, priv, 3512a9e2740SBartosz Golaszewski &gpio_mockup_debugfs_ops); 35246526c15SBartosz Golaszewski if (IS_ERR_OR_NULL(evfile)) 3539202ba23SBartosz Golaszewski goto err; 3549202ba23SBartosz Golaszewski } 3559202ba23SBartosz Golaszewski 3569202ba23SBartosz Golaszewski return; 3579202ba23SBartosz Golaszewski 3589202ba23SBartosz Golaszewski err: 3592a9e2740SBartosz Golaszewski dev_err(dev, "error creating debugfs files\n"); 3609202ba23SBartosz Golaszewski } 3619202ba23SBartosz Golaszewski 3621cea4734SBartosz Golaszewski static int gpio_mockup_name_lines(struct device *dev, 3631cea4734SBartosz Golaszewski struct gpio_mockup_chip *chip) 3641cea4734SBartosz Golaszewski { 3651cea4734SBartosz Golaszewski struct gpio_chip *gc = &chip->gc; 3661cea4734SBartosz Golaszewski char **names; 3671cea4734SBartosz Golaszewski int i; 3681cea4734SBartosz Golaszewski 3691cea4734SBartosz Golaszewski names = devm_kcalloc(dev, gc->ngpio, sizeof(char *), GFP_KERNEL); 3701cea4734SBartosz Golaszewski if (!names) 3711cea4734SBartosz Golaszewski return -ENOMEM; 3721cea4734SBartosz Golaszewski 3731cea4734SBartosz Golaszewski for (i = 0; i < gc->ngpio; i++) { 3741cea4734SBartosz Golaszewski names[i] = devm_kasprintf(dev, GFP_KERNEL, 3751cea4734SBartosz Golaszewski "%s-%d", gc->label, i); 3761cea4734SBartosz Golaszewski if (!names[i]) 3771cea4734SBartosz Golaszewski return -ENOMEM; 3781cea4734SBartosz Golaszewski } 3791cea4734SBartosz Golaszewski 3801cea4734SBartosz Golaszewski gc->names = (const char *const *)names; 3811cea4734SBartosz Golaszewski 3821cea4734SBartosz Golaszewski return 0; 3831cea4734SBartosz Golaszewski } 3841cea4734SBartosz Golaszewski 385ca409160SBartosz Golaszewski static int gpio_mockup_probe(struct platform_device *pdev) 3860f98dd1bSBamvor Jian Zhang { 3878a39f597SBartosz Golaszewski struct gpio_mockup_chip *chip; 3886d974d32SBartosz Golaszewski struct gpio_chip *gc; 3898a39f597SBartosz Golaszewski struct device *dev; 3903ea47b44SBartosz Golaszewski const char *name; 3913ea47b44SBartosz Golaszewski int rv, base; 3923ea47b44SBartosz Golaszewski u16 ngpio; 3930f98dd1bSBamvor Jian Zhang 3948a39f597SBartosz Golaszewski dev = &pdev->dev; 3953ea47b44SBartosz Golaszewski 3963ea47b44SBartosz Golaszewski rv = device_property_read_u32(dev, "gpio-base", &base); 3973ea47b44SBartosz Golaszewski if (rv) 3983ea47b44SBartosz Golaszewski base = -1; 3993ea47b44SBartosz Golaszewski 4003ea47b44SBartosz Golaszewski rv = device_property_read_u16(dev, "nr-gpios", &ngpio); 4013ea47b44SBartosz Golaszewski if (rv) 4023ea47b44SBartosz Golaszewski return rv; 4033ea47b44SBartosz Golaszewski 4043ea47b44SBartosz Golaszewski rv = device_property_read_string(dev, "chip-name", &name); 4053ea47b44SBartosz Golaszewski if (rv) 4063ea47b44SBartosz Golaszewski name = NULL; 407b652336dSBartosz Golaszewski 4088a39f597SBartosz Golaszewski chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 4098a39f597SBartosz Golaszewski if (!chip) 4100f98dd1bSBamvor Jian Zhang return -ENOMEM; 4110f98dd1bSBamvor Jian Zhang 4123ea47b44SBartosz Golaszewski if (!name) { 4133ea47b44SBartosz Golaszewski name = devm_kasprintf(dev, GFP_KERNEL, 4143ea47b44SBartosz Golaszewski "%s-%c", pdev->name, pdev->id + 'A'); 4158a39f597SBartosz Golaszewski if (!name) 416ad6d8004SBartosz Golaszewski return -ENOMEM; 4173ea47b44SBartosz Golaszewski } 418ad6d8004SBartosz Golaszewski 4199212492fSBartosz Golaszewski mutex_init(&chip->lock); 4209212492fSBartosz Golaszewski 4216d974d32SBartosz Golaszewski gc = &chip->gc; 4226d974d32SBartosz Golaszewski gc->base = base; 4236d974d32SBartosz Golaszewski gc->ngpio = ngpio; 4246d974d32SBartosz Golaszewski gc->label = name; 4256d974d32SBartosz Golaszewski gc->owner = THIS_MODULE; 4266d974d32SBartosz Golaszewski gc->parent = dev; 4276d974d32SBartosz Golaszewski gc->get = gpio_mockup_get; 4286d974d32SBartosz Golaszewski gc->set = gpio_mockup_set; 429cbf1e092SBartosz Golaszewski gc->get_multiple = gpio_mockup_get_multiple; 430c47bee95SBartosz Golaszewski gc->set_multiple = gpio_mockup_set_multiple; 4316d974d32SBartosz Golaszewski gc->direction_output = gpio_mockup_dirout; 4326d974d32SBartosz Golaszewski gc->direction_input = gpio_mockup_dirin; 4336d974d32SBartosz Golaszewski gc->get_direction = gpio_mockup_get_direction; 4346d974d32SBartosz Golaszewski gc->to_irq = gpio_mockup_to_irq; 4352a9e2740SBartosz Golaszewski gc->free = gpio_mockup_free; 4366d974d32SBartosz Golaszewski 4376d974d32SBartosz Golaszewski chip->lines = devm_kcalloc(dev, gc->ngpio, 4386d974d32SBartosz Golaszewski sizeof(*chip->lines), GFP_KERNEL); 4396d974d32SBartosz Golaszewski if (!chip->lines) 4406d974d32SBartosz Golaszewski return -ENOMEM; 4416d974d32SBartosz Golaszewski 4423ea47b44SBartosz Golaszewski if (device_property_read_bool(dev, "named-gpio-lines")) { 4436d974d32SBartosz Golaszewski rv = gpio_mockup_name_lines(dev, chip); 4446d974d32SBartosz Golaszewski if (rv) 4458a39f597SBartosz Golaszewski return rv; 4460f98dd1bSBamvor Jian Zhang } 4470f98dd1bSBamvor Jian Zhang 4486d974d32SBartosz Golaszewski rv = devm_irq_sim_init(dev, &chip->irqsim, gc->ngpio); 4491e0dca67SBartosz Golaszewski if (rv < 0) 4506d974d32SBartosz Golaszewski return rv; 4516d974d32SBartosz Golaszewski 4526d974d32SBartosz Golaszewski rv = devm_gpiochip_add_data(dev, &chip->gc, chip); 4536d974d32SBartosz Golaszewski if (rv) 4546d974d32SBartosz Golaszewski return rv; 4556d974d32SBartosz Golaszewski 4566cb9215bSBartosz Golaszewski if (!IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) 4576d974d32SBartosz Golaszewski gpio_mockup_debugfs_setup(dev, chip); 4586d974d32SBartosz Golaszewski 4590f98dd1bSBamvor Jian Zhang return 0; 4600f98dd1bSBamvor Jian Zhang } 4610f98dd1bSBamvor Jian Zhang 462ca409160SBartosz Golaszewski static struct platform_driver gpio_mockup_driver = { 4630f98dd1bSBamvor Jian Zhang .driver = { 464ca409160SBartosz Golaszewski .name = GPIO_MOCKUP_NAME, 4650f98dd1bSBamvor Jian Zhang }, 466ca409160SBartosz Golaszewski .probe = gpio_mockup_probe, 4670f98dd1bSBamvor Jian Zhang }; 4680f98dd1bSBamvor Jian Zhang 4698a39f597SBartosz Golaszewski static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC]; 4708a39f597SBartosz Golaszewski 4718a39f597SBartosz Golaszewski static void gpio_mockup_unregister_pdevs(void) 4728a39f597SBartosz Golaszewski { 4738a39f597SBartosz Golaszewski struct platform_device *pdev; 4748a39f597SBartosz Golaszewski int i; 4758a39f597SBartosz Golaszewski 4768a39f597SBartosz Golaszewski for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) { 4778a39f597SBartosz Golaszewski pdev = gpio_mockup_pdevs[i]; 4788a39f597SBartosz Golaszewski 4798a39f597SBartosz Golaszewski if (pdev) 4808a39f597SBartosz Golaszewski platform_device_unregister(pdev); 4818a39f597SBartosz Golaszewski } 4828a39f597SBartosz Golaszewski } 483f3b47170SBartosz Golaszewski 484f3b47170SBartosz Golaszewski static int __init gpio_mockup_init(void) 4850f98dd1bSBamvor Jian Zhang { 4863ea47b44SBartosz Golaszewski struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; 4873ea47b44SBartosz Golaszewski int i, prop, num_chips, err = 0, base; 4883ea47b44SBartosz Golaszewski struct platform_device_info pdevinfo; 4898a39f597SBartosz Golaszewski struct platform_device *pdev; 4903ea47b44SBartosz Golaszewski u16 ngpio; 4910f98dd1bSBamvor Jian Zhang 492e63a006fSBartosz Golaszewski if ((gpio_mockup_num_ranges < 2) || 493e63a006fSBartosz Golaszewski (gpio_mockup_num_ranges % 2) || 494e63a006fSBartosz Golaszewski (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES)) 49520c35ac4SBartosz Golaszewski return -EINVAL; 49620c35ac4SBartosz Golaszewski 4978a39f597SBartosz Golaszewski /* Each chip is described by two values. */ 498e63a006fSBartosz Golaszewski num_chips = gpio_mockup_num_ranges / 2; 4998a39f597SBartosz Golaszewski 500fa86963aSBartosz Golaszewski /* 501fa86963aSBartosz Golaszewski * The second value in the <base GPIO - number of GPIOS> pair must 502fa86963aSBartosz Golaszewski * always be greater than 0. 503fa86963aSBartosz Golaszewski */ 504fa86963aSBartosz Golaszewski for (i = 0; i < num_chips; i++) { 505cd9835f1SBartosz Golaszewski if (gpio_mockup_range_ngpio(i) < 0) 506fa86963aSBartosz Golaszewski return -EINVAL; 507fa86963aSBartosz Golaszewski } 508fa86963aSBartosz Golaszewski 5092a9e2740SBartosz Golaszewski gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL); 51046526c15SBartosz Golaszewski if (IS_ERR_OR_NULL(gpio_mockup_dbg_dir)) 511f1bec99aSBartosz Golaszewski gpio_mockup_err("error creating debugfs directory\n"); 5129202ba23SBartosz Golaszewski 5138a39f597SBartosz Golaszewski err = platform_driver_register(&gpio_mockup_driver); 5140f98dd1bSBamvor Jian Zhang if (err) { 515f1bec99aSBartosz Golaszewski gpio_mockup_err("error registering platform driver\n"); 5160f98dd1bSBamvor Jian Zhang return err; 5170f98dd1bSBamvor Jian Zhang } 5180f98dd1bSBamvor Jian Zhang 5198a39f597SBartosz Golaszewski for (i = 0; i < num_chips; i++) { 5203ea47b44SBartosz Golaszewski memset(properties, 0, sizeof(properties)); 5213ea47b44SBartosz Golaszewski memset(&pdevinfo, 0, sizeof(pdevinfo)); 5223ea47b44SBartosz Golaszewski prop = 0; 5238a39f597SBartosz Golaszewski 5243ea47b44SBartosz Golaszewski base = gpio_mockup_range_base(i); 5253ea47b44SBartosz Golaszewski if (base >= 0) 5263ea47b44SBartosz Golaszewski properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", 5273ea47b44SBartosz Golaszewski base); 5283ea47b44SBartosz Golaszewski 5293ea47b44SBartosz Golaszewski ngpio = base < 0 ? gpio_mockup_range_ngpio(i) 5303ea47b44SBartosz Golaszewski : gpio_mockup_range_ngpio(i) - base; 5313ea47b44SBartosz Golaszewski properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); 5323ea47b44SBartosz Golaszewski 5333ea47b44SBartosz Golaszewski if (gpio_mockup_named_lines) 5343ea47b44SBartosz Golaszewski properties[prop++] = PROPERTY_ENTRY_BOOL( 5353ea47b44SBartosz Golaszewski "named-gpio-lines"); 5363ea47b44SBartosz Golaszewski 5373ea47b44SBartosz Golaszewski pdevinfo.name = GPIO_MOCKUP_NAME; 5383ea47b44SBartosz Golaszewski pdevinfo.id = i; 5393ea47b44SBartosz Golaszewski pdevinfo.properties = properties; 5403ea47b44SBartosz Golaszewski 5413ea47b44SBartosz Golaszewski pdev = platform_device_register_full(&pdevinfo); 542c4b54e13SBartosz Golaszewski if (IS_ERR(pdev)) { 543f1bec99aSBartosz Golaszewski gpio_mockup_err("error registering device"); 5448a39f597SBartosz Golaszewski platform_driver_unregister(&gpio_mockup_driver); 5458a39f597SBartosz Golaszewski gpio_mockup_unregister_pdevs(); 546c4b54e13SBartosz Golaszewski return PTR_ERR(pdev); 5478a39f597SBartosz Golaszewski } 5488a39f597SBartosz Golaszewski 5498a39f597SBartosz Golaszewski gpio_mockup_pdevs[i] = pdev; 5500f98dd1bSBamvor Jian Zhang } 5510f98dd1bSBamvor Jian Zhang 5520f98dd1bSBamvor Jian Zhang return 0; 5530f98dd1bSBamvor Jian Zhang } 5540f98dd1bSBamvor Jian Zhang 555f3b47170SBartosz Golaszewski static void __exit gpio_mockup_exit(void) 5560f98dd1bSBamvor Jian Zhang { 5579202ba23SBartosz Golaszewski debugfs_remove_recursive(gpio_mockup_dbg_dir); 558ca409160SBartosz Golaszewski platform_driver_unregister(&gpio_mockup_driver); 5598a39f597SBartosz Golaszewski gpio_mockup_unregister_pdevs(); 5600f98dd1bSBamvor Jian Zhang } 5610f98dd1bSBamvor Jian Zhang 562f3b47170SBartosz Golaszewski module_init(gpio_mockup_init); 563f3b47170SBartosz Golaszewski module_exit(gpio_mockup_exit); 5640f98dd1bSBamvor Jian Zhang 5650f98dd1bSBamvor Jian Zhang MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>"); 5664f9a4cd6SLinus Walleij MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>"); 567c60c7f4cSBartosz Golaszewski MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>"); 5680f98dd1bSBamvor Jian Zhang MODULE_DESCRIPTION("GPIO Testing driver"); 5690f98dd1bSBamvor Jian Zhang MODULE_LICENSE("GPL v2"); 570