gpio: mockup: use device properties instead of platform_data

Some users want to introduce device tree support to the mockup driver.
Let's make it easier by switching to using generic device properties.
The driver stays compatible with previous use cases and after this
conversion there'll be no need to change the way probing of mockup
GPIO chips works.

Tested with libgpiod test suite.

Signed-off-by: Bartosz Golaszewski <brgl@bgdev.pl>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
This commit is contained in:
Bartosz Golaszewski 2018-09-25 13:29:46 +02:00 committed by Linus Walleij
parent 90fd227029
commit 3ea47b447d
1 changed files with 50 additions and 30 deletions

View File

@ -18,6 +18,7 @@
#include <linux/irq_sim.h> #include <linux/irq_sim.h>
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/property.h>
#include "gpiolib.h" #include "gpiolib.h"
@ -28,6 +29,8 @@
* of GPIO lines. * of GPIO lines.
*/ */
#define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2)
/* Maximum of three properties + the sentinel. */
#define GPIO_MOCKUP_MAX_PROP 4
#define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__) #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__)
@ -59,13 +62,6 @@ struct gpio_mockup_dbgfs_private {
int offset; int offset;
}; };
struct gpio_mockup_platform_data {
int base;
int ngpio;
int index;
bool named_lines;
};
static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES];
static int gpio_mockup_num_ranges; static int gpio_mockup_num_ranges;
module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400); module_param_array(gpio_mockup_ranges, int, &gpio_mockup_num_ranges, 0400);
@ -255,26 +251,37 @@ static int gpio_mockup_name_lines(struct device *dev,
static int gpio_mockup_probe(struct platform_device *pdev) static int gpio_mockup_probe(struct platform_device *pdev)
{ {
struct gpio_mockup_platform_data *pdata;
struct gpio_mockup_chip *chip; struct gpio_mockup_chip *chip;
struct gpio_chip *gc; struct gpio_chip *gc;
int rv, base, ngpio;
struct device *dev; struct device *dev;
char *name; const char *name;
int rv, base;
u16 ngpio;
dev = &pdev->dev; dev = &pdev->dev;
pdata = dev_get_platdata(dev);
base = pdata->base; rv = device_property_read_u32(dev, "gpio-base", &base);
ngpio = pdata->ngpio; if (rv)
base = -1;
rv = device_property_read_u16(dev, "nr-gpios", &ngpio);
if (rv)
return rv;
rv = device_property_read_string(dev, "chip-name", &name);
if (rv)
name = NULL;
chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
if (!chip) if (!chip)
return -ENOMEM; return -ENOMEM;
name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c", if (!name) {
pdev->name, pdata->index); name = devm_kasprintf(dev, GFP_KERNEL,
"%s-%c", pdev->name, pdev->id + 'A');
if (!name) if (!name)
return -ENOMEM; return -ENOMEM;
}
gc = &chip->gc; gc = &chip->gc;
gc->base = base; gc->base = base;
@ -295,7 +302,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
if (!chip->lines) if (!chip->lines)
return -ENOMEM; return -ENOMEM;
if (pdata->named_lines) { if (device_property_read_bool(dev, "named-gpio-lines")) {
rv = gpio_mockup_name_lines(dev, chip); rv = gpio_mockup_name_lines(dev, chip);
if (rv) if (rv)
return rv; return rv;
@ -339,9 +346,11 @@ static void gpio_mockup_unregister_pdevs(void)
static int __init gpio_mockup_init(void) static int __init gpio_mockup_init(void)
{ {
int i, num_chips, err = 0, index = 'A'; struct property_entry properties[GPIO_MOCKUP_MAX_PROP];
struct gpio_mockup_platform_data pdata; int i, prop, num_chips, err = 0, base;
struct platform_device_info pdevinfo;
struct platform_device *pdev; struct platform_device *pdev;
u16 ngpio;
if ((gpio_mockup_num_ranges < 2) || if ((gpio_mockup_num_ranges < 2) ||
(gpio_mockup_num_ranges % 2) || (gpio_mockup_num_ranges % 2) ||
@ -371,17 +380,28 @@ static int __init gpio_mockup_init(void)
} }
for (i = 0; i < num_chips; i++) { for (i = 0; i < num_chips; i++) {
pdata.index = index++; memset(properties, 0, sizeof(properties));
pdata.base = gpio_mockup_range_base(i); memset(&pdevinfo, 0, sizeof(pdevinfo));
pdata.ngpio = pdata.base < 0 prop = 0;
? gpio_mockup_range_ngpio(i)
: gpio_mockup_range_ngpio(i) - pdata.base;
pdata.named_lines = gpio_mockup_named_lines;
pdev = platform_device_register_resndata(NULL, base = gpio_mockup_range_base(i);
GPIO_MOCKUP_NAME, if (base >= 0)
i, NULL, 0, &pdata, properties[prop++] = PROPERTY_ENTRY_U32("gpio-base",
sizeof(pdata)); base);
ngpio = base < 0 ? gpio_mockup_range_ngpio(i)
: gpio_mockup_range_ngpio(i) - base;
properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio);
if (gpio_mockup_named_lines)
properties[prop++] = PROPERTY_ENTRY_BOOL(
"named-gpio-lines");
pdevinfo.name = GPIO_MOCKUP_NAME;
pdevinfo.id = i;
pdevinfo.properties = properties;
pdev = platform_device_register_full(&pdevinfo);
if (IS_ERR(pdev)) { if (IS_ERR(pdev)) {
gpio_mockup_err("error registering device"); gpio_mockup_err("error registering device");
platform_driver_unregister(&gpio_mockup_driver); platform_driver_unregister(&gpio_mockup_driver);