Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Bartosz Golaszewski and committed by
Linus Walleij
3ea47b44 90fd2270

+50 -30
+50 -30
drivers/gpio/gpio-mockup.c
··· 18 18 #include <linux/irq_sim.h> 19 19 #include <linux/debugfs.h> 20 20 #include <linux/uaccess.h> 21 + #include <linux/property.h> 21 22 22 23 #include "gpiolib.h" 23 24 ··· 29 28 * of GPIO lines. 30 29 */ 31 30 #define GPIO_MOCKUP_MAX_RANGES (GPIO_MOCKUP_MAX_GC * 2) 31 + /* Maximum of three properties + the sentinel. */ 32 + #define GPIO_MOCKUP_MAX_PROP 4 32 33 33 34 #define gpio_mockup_err(...) pr_err(GPIO_MOCKUP_NAME ": " __VA_ARGS__) 34 35 ··· 60 57 struct gpio_mockup_chip *chip; 61 58 struct gpio_desc *desc; 62 59 int offset; 63 - }; 64 - 65 - struct gpio_mockup_platform_data { 66 - int base; 67 - int ngpio; 68 - int index; 69 - bool named_lines; 70 60 }; 71 61 72 62 static int gpio_mockup_ranges[GPIO_MOCKUP_MAX_RANGES]; ··· 251 255 252 256 static int gpio_mockup_probe(struct platform_device *pdev) 253 257 { 254 - struct gpio_mockup_platform_data *pdata; 255 258 struct gpio_mockup_chip *chip; 256 259 struct gpio_chip *gc; 257 - int rv, base, ngpio; 258 260 struct device *dev; 259 - char *name; 261 + const char *name; 262 + int rv, base; 263 + u16 ngpio; 260 264 261 265 dev = &pdev->dev; 262 - pdata = dev_get_platdata(dev); 263 - base = pdata->base; 264 - ngpio = pdata->ngpio; 266 + 267 + rv = device_property_read_u32(dev, "gpio-base", &base); 268 + if (rv) 269 + base = -1; 270 + 271 + rv = device_property_read_u16(dev, "nr-gpios", &ngpio); 272 + if (rv) 273 + return rv; 274 + 275 + rv = device_property_read_string(dev, "chip-name", &name); 276 + if (rv) 277 + name = NULL; 265 278 266 279 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 267 280 if (!chip) 268 281 return -ENOMEM; 269 282 270 - name = devm_kasprintf(dev, GFP_KERNEL, "%s-%c", 271 - pdev->name, pdata->index); 272 - if (!name) 273 - return -ENOMEM; 283 + if (!name) { 284 + name = devm_kasprintf(dev, GFP_KERNEL, 285 + "%s-%c", pdev->name, pdev->id + 'A'); 286 + if (!name) 287 + return -ENOMEM; 288 + } 274 289 275 290 gc = &chip->gc; 276 291 gc->base = base; ··· 302 295 if (!chip->lines) 303 296 return -ENOMEM; 304 297 305 - if (pdata->named_lines) { 298 + if (device_property_read_bool(dev, "named-gpio-lines")) { 306 299 rv = gpio_mockup_name_lines(dev, chip); 307 300 if (rv) 308 301 return rv; ··· 346 339 347 340 static int __init gpio_mockup_init(void) 348 341 { 349 - int i, num_chips, err = 0, index = 'A'; 350 - struct gpio_mockup_platform_data pdata; 342 + struct property_entry properties[GPIO_MOCKUP_MAX_PROP]; 343 + int i, prop, num_chips, err = 0, base; 344 + struct platform_device_info pdevinfo; 351 345 struct platform_device *pdev; 346 + u16 ngpio; 352 347 353 348 if ((gpio_mockup_num_ranges < 2) || 354 349 (gpio_mockup_num_ranges % 2) || ··· 380 371 } 381 372 382 373 for (i = 0; i < num_chips; i++) { 383 - pdata.index = index++; 384 - pdata.base = gpio_mockup_range_base(i); 385 - pdata.ngpio = pdata.base < 0 386 - ? gpio_mockup_range_ngpio(i) 387 - : gpio_mockup_range_ngpio(i) - pdata.base; 388 - pdata.named_lines = gpio_mockup_named_lines; 374 + memset(properties, 0, sizeof(properties)); 375 + memset(&pdevinfo, 0, sizeof(pdevinfo)); 376 + prop = 0; 389 377 390 - pdev = platform_device_register_resndata(NULL, 391 - GPIO_MOCKUP_NAME, 392 - i, NULL, 0, &pdata, 393 - sizeof(pdata)); 378 + base = gpio_mockup_range_base(i); 379 + if (base >= 0) 380 + properties[prop++] = PROPERTY_ENTRY_U32("gpio-base", 381 + base); 382 + 383 + ngpio = base < 0 ? gpio_mockup_range_ngpio(i) 384 + : gpio_mockup_range_ngpio(i) - base; 385 + properties[prop++] = PROPERTY_ENTRY_U16("nr-gpios", ngpio); 386 + 387 + if (gpio_mockup_named_lines) 388 + properties[prop++] = PROPERTY_ENTRY_BOOL( 389 + "named-gpio-lines"); 390 + 391 + pdevinfo.name = GPIO_MOCKUP_NAME; 392 + pdevinfo.id = i; 393 + pdevinfo.properties = properties; 394 + 395 + pdev = platform_device_register_full(&pdevinfo); 394 396 if (IS_ERR(pdev)) { 395 397 gpio_mockup_err("error registering device"); 396 398 platform_driver_unregister(&gpio_mockup_driver);