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

leds: pca963x: use flexible array

Instead of doing two allocations, allocate only once, by utilizing
flexible array members.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Cc: Peter Meerwald <p.meerwald@bct-electronic.com>
Cc: Ricardo Ribalda <ribalda@kernel.org>
Cc: Zahari Petkov <zahari@balena.io>
Signed-off-by: Pavel Machek <pavel@ucw.cz>

authored by

Marek Behún and committed by
Pavel Machek
b5a3b44f 5db85093

+12 -16
+12 -16
drivers/leds/leds-pca963x.c
··· 96 96 }; 97 97 MODULE_DEVICE_TABLE(i2c, pca963x_id); 98 98 99 - struct pca963x_led; 100 - 101 - struct pca963x { 102 - struct pca963x_chipdef *chipdef; 103 - struct mutex mutex; 104 - struct i2c_client *client; 105 - struct pca963x_led *leds; 106 - unsigned long leds_on; 107 - }; 99 + struct pca963x; 108 100 109 101 struct pca963x_led { 110 102 struct pca963x *chip; ··· 105 113 char name[32]; 106 114 u8 gdc; 107 115 u8 gfrq; 116 + }; 117 + 118 + struct pca963x { 119 + struct pca963x_chipdef *chipdef; 120 + struct mutex mutex; 121 + struct i2c_client *client; 122 + unsigned long leds_on; 123 + struct pca963x_led leds[]; 108 124 }; 109 125 110 126 static int pca963x_brightness(struct pca963x_led *led, ··· 367 367 struct device *dev = &client->dev; 368 368 struct pca963x_chipdef *chipdef; 369 369 struct pca963x_platform_data *pdata; 370 - struct pca963x_led *leds; 371 370 struct pca963x *chip; 372 371 int i, err; 373 372 ··· 388 389 return -EINVAL; 389 390 } 390 391 391 - chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); 392 + chip = devm_kzalloc(dev, struct_size(chip, leds, chipdef->n_leds), 393 + GFP_KERNEL); 392 394 if (!chip) 393 - return -ENOMEM; 394 - leds = devm_kcalloc(dev, chipdef->n_leds, sizeof(*leds), GFP_KERNEL); 395 - if (!leds) 396 395 return -ENOMEM; 397 396 398 397 i2c_set_clientdata(client, chip); ··· 398 401 mutex_init(&chip->mutex); 399 402 chip->chipdef = chipdef; 400 403 chip->client = client; 401 - chip->leds = leds; 402 404 403 405 /* Turn off LEDs by default*/ 404 406 for (i = 0; i < chipdef->n_leds / 4; i++) 405 407 i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00); 406 408 407 409 for (i = 0; i < chipdef->n_leds; i++) { 408 - struct pca963x_led *led = &leds[i]; 410 + struct pca963x_led *led = &chip->leds[i]; 409 411 410 412 led->led_num = i; 411 413 led->chip = chip;