Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * GPIO Testing Device Driver
0004  *
0005  * Copyright (C) 2014  Kamlakant Patel <kamlakant.patel@broadcom.com>
0006  * Copyright (C) 2015-2016  Bamvor Jian Zhang <bamv2005@gmail.com>
0007  * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/debugfs.h>
0013 #include <linux/gpio/driver.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/irq.h>
0016 #include <linux/irq_sim.h>
0017 #include <linux/irqdomain.h>
0018 #include <linux/mod_devicetable.h>
0019 #include <linux/module.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/property.h>
0022 #include <linux/slab.h>
0023 #include <linux/string_helpers.h>
0024 #include <linux/uaccess.h>
0025 
0026 #include "gpiolib.h"
0027 
0028 #define GPIO_MOCKUP_MAX_GC  10
0029 /*
0030  * We're storing two values per chip: the GPIO base and the number
0031  * of GPIO lines.
0032  */
0033 #define GPIO_MOCKUP_MAX_RANGES  (GPIO_MOCKUP_MAX_GC * 2)
0034 /* Maximum of four properties + the sentinel. */
0035 #define GPIO_MOCKUP_MAX_PROP    5
0036 
0037 /*
0038  * struct gpio_pin_status - structure describing a GPIO status
0039  * @dir:       Configures direction of gpio as "in" or "out"
0040  * @value:     Configures status of the gpio as 0(low) or 1(high)
0041  */
0042 struct gpio_mockup_line_status {
0043     int dir;
0044     int value;
0045     int pull;
0046 };
0047 
0048 struct gpio_mockup_chip {
0049     struct gpio_chip gc;
0050     struct gpio_mockup_line_status *lines;
0051     struct irq_domain *irq_sim_domain;
0052     struct dentry *dbg_dir;
0053     struct mutex lock;
0054 };
0055 
0056 struct gpio_mockup_dbgfs_private {
0057     struct gpio_mockup_chip *chip;
0058     struct gpio_desc *desc;
0059     unsigned int offset;
0060 };
0061 
0062 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
0063 static int gpio_mockup_num_ranges;
0064 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
0065 
0066 static bool gpio_mockup_named_lines;
0067 module_param_named(gpio_mockup_named_lines,
0068            gpio_mockup_named_lines, bool, 0400);
0069 
0070 static struct dentry *gpio_mockup_dbg_dir;
0071 
0072 static int gpio_mockup_range_base(unsigned int index)
0073 {
0074     return gpio_mockup_ranges[index * 2];
0075 }
0076 
0077 static int gpio_mockup_range_ngpio(unsigned int index)
0078 {
0079     return gpio_mockup_ranges[index * 2 + 1];
0080 }
0081 
0082 static int __gpio_mockup_get(struct gpio_mockup_chip *chip,
0083                  unsigned int offset)
0084 {
0085     return chip->lines[offset].value;
0086 }
0087 
0088 static int gpio_mockup_get(struct gpio_chip *gc, unsigned int offset)
0089 {
0090     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0091     int val;
0092 
0093     mutex_lock(&chip->lock);
0094     val = __gpio_mockup_get(chip, offset);
0095     mutex_unlock(&chip->lock);
0096 
0097     return val;
0098 }
0099 
0100 static int gpio_mockup_get_multiple(struct gpio_chip *gc,
0101                     unsigned long *mask, unsigned long *bits)
0102 {
0103     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0104     unsigned int bit, val;
0105 
0106     mutex_lock(&chip->lock);
0107     for_each_set_bit(bit, mask, gc->ngpio) {
0108         val = __gpio_mockup_get(chip, bit);
0109         __assign_bit(bit, bits, val);
0110     }
0111     mutex_unlock(&chip->lock);
0112 
0113     return 0;
0114 }
0115 
0116 static void __gpio_mockup_set(struct gpio_mockup_chip *chip,
0117                   unsigned int offset, int value)
0118 {
0119     chip->lines[offset].value = !!value;
0120 }
0121 
0122 static void gpio_mockup_set(struct gpio_chip *gc,
0123                unsigned int offset, int value)
0124 {
0125     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0126 
0127     mutex_lock(&chip->lock);
0128     __gpio_mockup_set(chip, offset, value);
0129     mutex_unlock(&chip->lock);
0130 }
0131 
0132 static void gpio_mockup_set_multiple(struct gpio_chip *gc,
0133                      unsigned long *mask, unsigned long *bits)
0134 {
0135     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0136     unsigned int bit;
0137 
0138     mutex_lock(&chip->lock);
0139     for_each_set_bit(bit, mask, gc->ngpio)
0140         __gpio_mockup_set(chip, bit, test_bit(bit, bits));
0141     mutex_unlock(&chip->lock);
0142 }
0143 
0144 static int gpio_mockup_apply_pull(struct gpio_mockup_chip *chip,
0145                   unsigned int offset, int value)
0146 {
0147     struct gpio_chip *gc = &chip->gc;
0148     struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
0149     int curr, irq, irq_type, ret = 0;
0150 
0151     mutex_lock(&chip->lock);
0152 
0153     if (test_bit(FLAG_REQUESTED, &desc->flags) &&
0154         !test_bit(FLAG_IS_OUT, &desc->flags)) {
0155         curr = __gpio_mockup_get(chip, offset);
0156         if (curr == value)
0157             goto out;
0158 
0159         irq = irq_find_mapping(chip->irq_sim_domain, offset);
0160         if (!irq)
0161             /*
0162              * This is fine - it just means, nobody is listening
0163              * for interrupts on this line, otherwise
0164              * irq_create_mapping() would have been called from
0165              * the to_irq() callback.
0166              */
0167             goto set_value;
0168 
0169         irq_type = irq_get_trigger_type(irq);
0170 
0171         if ((value == 1 && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
0172             (value == 0 && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
0173             ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
0174                             true);
0175             if (ret)
0176                 goto out;
0177         }
0178     }
0179 
0180 set_value:
0181     /* Change the value unless we're actively driving the line. */
0182     if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
0183         !test_bit(FLAG_IS_OUT, &desc->flags))
0184         __gpio_mockup_set(chip, offset, value);
0185 
0186 out:
0187     chip->lines[offset].pull = value;
0188     mutex_unlock(&chip->lock);
0189     return ret;
0190 }
0191 
0192 static int gpio_mockup_set_config(struct gpio_chip *gc,
0193                   unsigned int offset, unsigned long config)
0194 {
0195     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0196 
0197     switch (pinconf_to_config_param(config)) {
0198     case PIN_CONFIG_BIAS_PULL_UP:
0199         return gpio_mockup_apply_pull(chip, offset, 1);
0200     case PIN_CONFIG_BIAS_PULL_DOWN:
0201         return gpio_mockup_apply_pull(chip, offset, 0);
0202     default:
0203         break;
0204     }
0205     return -ENOTSUPP;
0206 }
0207 
0208 static int gpio_mockup_dirout(struct gpio_chip *gc,
0209                   unsigned int offset, int value)
0210 {
0211     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0212 
0213     mutex_lock(&chip->lock);
0214     chip->lines[offset].dir = GPIO_LINE_DIRECTION_OUT;
0215     __gpio_mockup_set(chip, offset, value);
0216     mutex_unlock(&chip->lock);
0217 
0218     return 0;
0219 }
0220 
0221 static int gpio_mockup_dirin(struct gpio_chip *gc, unsigned int offset)
0222 {
0223     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0224 
0225     mutex_lock(&chip->lock);
0226     chip->lines[offset].dir = GPIO_LINE_DIRECTION_IN;
0227     mutex_unlock(&chip->lock);
0228 
0229     return 0;
0230 }
0231 
0232 static int gpio_mockup_get_direction(struct gpio_chip *gc, unsigned int offset)
0233 {
0234     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0235     int direction;
0236 
0237     mutex_lock(&chip->lock);
0238     direction = chip->lines[offset].dir;
0239     mutex_unlock(&chip->lock);
0240 
0241     return direction;
0242 }
0243 
0244 static int gpio_mockup_to_irq(struct gpio_chip *gc, unsigned int offset)
0245 {
0246     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0247 
0248     return irq_create_mapping(chip->irq_sim_domain, offset);
0249 }
0250 
0251 static void gpio_mockup_free(struct gpio_chip *gc, unsigned int offset)
0252 {
0253     struct gpio_mockup_chip *chip = gpiochip_get_data(gc);
0254 
0255     __gpio_mockup_set(chip, offset, chip->lines[offset].pull);
0256 }
0257 
0258 static ssize_t gpio_mockup_debugfs_read(struct file *file,
0259                     char __user *usr_buf,
0260                     size_t size, loff_t *ppos)
0261 {
0262     struct gpio_mockup_dbgfs_private *priv;
0263     struct gpio_mockup_chip *chip;
0264     struct seq_file *sfile;
0265     struct gpio_chip *gc;
0266     int val, cnt;
0267     char buf[3];
0268 
0269     if (*ppos != 0)
0270         return 0;
0271 
0272     sfile = file->private_data;
0273     priv = sfile->private;
0274     chip = priv->chip;
0275     gc = &chip->gc;
0276 
0277     val = gpio_mockup_get(gc, priv->offset);
0278     cnt = snprintf(buf, sizeof(buf), "%d\n", val);
0279 
0280     return simple_read_from_buffer(usr_buf, size, ppos, buf, cnt);
0281 }
0282 
0283 static ssize_t gpio_mockup_debugfs_write(struct file *file,
0284                      const char __user *usr_buf,
0285                      size_t size, loff_t *ppos)
0286 {
0287     struct gpio_mockup_dbgfs_private *priv;
0288     int rv, val;
0289     struct seq_file *sfile;
0290 
0291     if (*ppos != 0)
0292         return -EINVAL;
0293 
0294     rv = kstrtoint_from_user(usr_buf, size, 0, &val);
0295     if (rv)
0296         return rv;
0297     if (val != 0 && val != 1)
0298         return -EINVAL;
0299 
0300     sfile = file->private_data;
0301     priv = sfile->private;
0302     rv = gpio_mockup_apply_pull(priv->chip, priv->offset, val);
0303     if (rv)
0304         return rv;
0305 
0306     return size;
0307 }
0308 
0309 static int gpio_mockup_debugfs_open(struct inode *inode, struct file *file)
0310 {
0311     return single_open(file, NULL, inode->i_private);
0312 }
0313 
0314 /*
0315  * Each mockup chip is represented by a directory named after the chip's device
0316  * name under /sys/kernel/debug/gpio-mockup/. Each line is represented by
0317  * a file using the line's offset as the name under the chip's directory.
0318  *
0319  * Reading from the line's file yields the current *value*, writing to the
0320  * line's file changes the current *pull*. Default pull for mockup lines is
0321  * down.
0322  *
0323  * Examples:
0324  * - when a line pulled down is requested in output mode and driven high, its
0325  *   value will return to 0 once it's released
0326  * - when the line is requested in output mode and driven high, writing 0 to
0327  *   the corresponding debugfs file will change the pull to down but the
0328  *   reported value will still be 1 until the line is released
0329  * - line requested in input mode always reports the same value as its pull
0330  *   configuration
0331  * - when the line is requested in input mode and monitored for events, writing
0332  *   the same value to the debugfs file will be a noop, while writing the
0333  *   opposite value will generate a dummy interrupt with an appropriate edge
0334  */
0335 static const struct file_operations gpio_mockup_debugfs_ops = {
0336     .owner = THIS_MODULE,
0337     .open = gpio_mockup_debugfs_open,
0338     .read = gpio_mockup_debugfs_read,
0339     .write = gpio_mockup_debugfs_write,
0340     .llseek = no_llseek,
0341     .release = single_release,
0342 };
0343 
0344 static void gpio_mockup_debugfs_setup(struct device *dev,
0345                       struct gpio_mockup_chip *chip)
0346 {
0347     struct gpio_mockup_dbgfs_private *priv;
0348     struct gpio_chip *gc;
0349     const char *devname;
0350     char *name;
0351     int i;
0352 
0353     gc = &chip->gc;
0354     devname = dev_name(&gc->gpiodev->dev);
0355 
0356     chip->dbg_dir = debugfs_create_dir(devname, gpio_mockup_dbg_dir);
0357 
0358     for (i = 0; i < gc->ngpio; i++) {
0359         name = devm_kasprintf(dev, GFP_KERNEL, "%d", i);
0360         if (!name)
0361             return;
0362 
0363         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0364         if (!priv)
0365             return;
0366 
0367         priv->chip = chip;
0368         priv->offset = i;
0369         priv->desc = gpiochip_get_desc(gc, i);
0370 
0371         debugfs_create_file(name, 0200, chip->dbg_dir, priv,
0372                     &gpio_mockup_debugfs_ops);
0373     }
0374 }
0375 
0376 static void gpio_mockup_debugfs_cleanup(void *data)
0377 {
0378     struct gpio_mockup_chip *chip = data;
0379 
0380     debugfs_remove_recursive(chip->dbg_dir);
0381 }
0382 
0383 static void gpio_mockup_dispose_mappings(void *data)
0384 {
0385     struct gpio_mockup_chip *chip = data;
0386     struct gpio_chip *gc = &chip->gc;
0387     int i, irq;
0388 
0389     for (i = 0; i < gc->ngpio; i++) {
0390         irq = irq_find_mapping(chip->irq_sim_domain, i);
0391         if (irq)
0392             irq_dispose_mapping(irq);
0393     }
0394 }
0395 
0396 static int gpio_mockup_probe(struct platform_device *pdev)
0397 {
0398     struct gpio_mockup_chip *chip;
0399     struct gpio_chip *gc;
0400     struct device *dev;
0401     const char *name;
0402     int rv, base, i;
0403     u16 ngpio;
0404 
0405     dev = &pdev->dev;
0406 
0407     rv = device_property_read_u32(dev, "gpio-base", &base);
0408     if (rv)
0409         base = -1;
0410 
0411     rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
0412     if (rv)
0413         return rv;
0414 
0415     rv = device_property_read_string(dev, "chip-label", &name);
0416     if (rv)
0417         name = dev_name(dev);
0418 
0419     chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
0420     if (!chip)
0421         return -ENOMEM;
0422 
0423     mutex_init(&chip->lock);
0424 
0425     gc = &chip->gc;
0426     gc->base = base;
0427     gc->ngpio = ngpio;
0428     gc->label = name;
0429     gc->owner = THIS_MODULE;
0430     gc->parent = dev;
0431     gc->get = gpio_mockup_get;
0432     gc->set = gpio_mockup_set;
0433     gc->get_multiple = gpio_mockup_get_multiple;
0434     gc->set_multiple = gpio_mockup_set_multiple;
0435     gc->direction_output = gpio_mockup_dirout;
0436     gc->direction_input = gpio_mockup_dirin;
0437     gc->get_direction = gpio_mockup_get_direction;
0438     gc->set_config = gpio_mockup_set_config;
0439     gc->to_irq = gpio_mockup_to_irq;
0440     gc->free = gpio_mockup_free;
0441 
0442     chip->lines = devm_kcalloc(dev, gc->ngpio,
0443                    sizeof(*chip->lines), GFP_KERNEL);
0444     if (!chip->lines)
0445         return -ENOMEM;
0446 
0447     for (i = 0; i < gc->ngpio; i++)
0448         chip->lines[i].dir = GPIO_LINE_DIRECTION_IN;
0449 
0450     chip->irq_sim_domain = devm_irq_domain_create_sim(dev, NULL,
0451                               gc->ngpio);
0452     if (IS_ERR(chip->irq_sim_domain))
0453         return PTR_ERR(chip->irq_sim_domain);
0454 
0455     rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
0456     if (rv)
0457         return rv;
0458 
0459     rv = devm_gpiochip_add_data(dev, &chip->gc, chip);
0460     if (rv)
0461         return rv;
0462 
0463     gpio_mockup_debugfs_setup(dev, chip);
0464 
0465     return devm_add_action_or_reset(dev, gpio_mockup_debugfs_cleanup, chip);
0466 }
0467 
0468 static const struct of_device_id gpio_mockup_of_match[] = {
0469     { .compatible = "gpio-mockup", },
0470     {},
0471 };
0472 MODULE_DEVICE_TABLE(of, gpio_mockup_of_match);
0473 
0474 static struct platform_driver gpio_mockup_driver = {
0475     .driver = {
0476         .name = "gpio-mockup",
0477         .of_match_table = gpio_mockup_of_match,
0478     },
0479     .probe = gpio_mockup_probe,
0480 };
0481 
0482 static struct platform_device *gpio_mockup_pdevs[GPIO_MOCKUP_MAX_GC];
0483 
0484 static void gpio_mockup_unregister_pdevs(void)
0485 {
0486     struct platform_device *pdev;
0487     struct fwnode_handle *fwnode;
0488     int i;
0489 
0490     for (i = 0; i < GPIO_MOCKUP_MAX_GC; i++) {
0491         pdev = gpio_mockup_pdevs[i];
0492         if (!pdev)
0493             continue;
0494 
0495         fwnode = dev_fwnode(&pdev->dev);
0496         platform_device_unregister(pdev);
0497         fwnode_remove_software_node(fwnode);
0498     }
0499 }
0500 
0501 static int __init gpio_mockup_register_chip(int idx)
0502 {
0503     struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
0504     struct platform_device_info pdevinfo;
0505     struct platform_device *pdev;
0506     struct fwnode_handle *fwnode;
0507     char **line_names = NULL;
0508     char chip_label[32];
0509     int prop = 0, base;
0510     u16 ngpio;
0511 
0512     memset(properties, 0, sizeof(properties));
0513     memset(&pdevinfo, 0, sizeof(pdevinfo));
0514 
0515     snprintf(chip_label, sizeof(chip_label), "gpio-mockup-%c", idx + 'A');
0516     properties[prop++] = PROPERTY_ENTRY_STRING("chip-label", chip_label);
0517 
0518     base = gpio_mockup_range_base(idx);
0519     if (base >= 0)
0520         properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", base);
0521 
0522     ngpio = base < 0 ? gpio_mockup_range_ngpio(idx)
0523              : gpio_mockup_range_ngpio(idx) - base;
0524     properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
0525 
0526     if (gpio_mockup_named_lines) {
0527         line_names = kasprintf_strarray(GFP_KERNEL, chip_label, ngpio);
0528         if (!line_names)
0529             return -ENOMEM;
0530 
0531         properties[prop++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
0532                     "gpio-line-names", line_names, ngpio);
0533     }
0534 
0535     fwnode = fwnode_create_software_node(properties, NULL);
0536     if (IS_ERR(fwnode)) {
0537         kfree_strarray(line_names, ngpio);
0538         return PTR_ERR(fwnode);
0539     }
0540 
0541     pdevinfo.name = "gpio-mockup";
0542     pdevinfo.id = idx;
0543     pdevinfo.fwnode = fwnode;
0544 
0545     pdev = platform_device_register_full(&pdevinfo);
0546     kfree_strarray(line_names, ngpio);
0547     if (IS_ERR(pdev)) {
0548         fwnode_remove_software_node(fwnode);
0549         pr_err("error registering device");
0550         return PTR_ERR(pdev);
0551     }
0552 
0553     gpio_mockup_pdevs[idx] = pdev;
0554 
0555     return 0;
0556 }
0557 
0558 static int __init gpio_mockup_init(void)
0559 {
0560     int i, num_chips, err;
0561 
0562     if ((gpio_mockup_num_ranges % 2) ||
0563         (gpio_mockup_num_ranges > GPIO_MOCKUP_MAX_RANGES))
0564         return -EINVAL;
0565 
0566     /* Each chip is described by two values. */
0567     num_chips = gpio_mockup_num_ranges / 2;
0568 
0569     /*
0570      * The second value in the <base GPIO - number of GPIOS> pair must
0571      * always be greater than 0.
0572      */
0573     for (i = 0; i < num_chips; i++) {
0574         if (gpio_mockup_range_ngpio(i) < 0)
0575             return -EINVAL;
0576     }
0577 
0578     gpio_mockup_dbg_dir = debugfs_create_dir("gpio-mockup", NULL);
0579 
0580     err = platform_driver_register(&gpio_mockup_driver);
0581     if (err) {
0582         pr_err("error registering platform driver\n");
0583         debugfs_remove_recursive(gpio_mockup_dbg_dir);
0584         return err;
0585     }
0586 
0587     for (i = 0; i < num_chips; i++) {
0588         err = gpio_mockup_register_chip(i);
0589         if (err) {
0590             platform_driver_unregister(&gpio_mockup_driver);
0591             gpio_mockup_unregister_pdevs();
0592             debugfs_remove_recursive(gpio_mockup_dbg_dir);
0593             return err;
0594         }
0595     }
0596 
0597     return 0;
0598 }
0599 
0600 static void __exit gpio_mockup_exit(void)
0601 {
0602     gpio_mockup_unregister_pdevs();
0603     debugfs_remove_recursive(gpio_mockup_dbg_dir);
0604     platform_driver_unregister(&gpio_mockup_driver);
0605 }
0606 
0607 module_init(gpio_mockup_init);
0608 module_exit(gpio_mockup_exit);
0609 
0610 MODULE_AUTHOR("Kamlakant Patel <kamlakant.patel@broadcom.com>");
0611 MODULE_AUTHOR("Bamvor Jian Zhang <bamv2005@gmail.com>");
0612 MODULE_AUTHOR("Bartosz Golaszewski <brgl@bgdev.pl>");
0613 MODULE_DESCRIPTION("GPIO Testing driver");
0614 MODULE_LICENSE("GPL v2");