@@ -456,15 +456,16 @@ EXPORT_SYMBOL_GPL(of_pwm_single_xlate);
static void *pwmchip_priv(struct pwm_chip *chip)
{
- return (void *)chip + sizeof(*chip);
+ return (void *)chip + struct_size(chip, pwms, chip->npwm);
}
struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv)
{
struct pwm_chip *chip;
size_t alloc_size;
+ unsigned int i;
- alloc_size = size_add(sizeof(*chip), sizeof_priv);
+ alloc_size = size_add(struct_size(chip, pwms, npwm), sizeof_priv);
chip = devm_kzalloc(parent, alloc_size, GFP_KERNEL);
if (!chip)
@@ -476,6 +477,13 @@ struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, si
pwmchip_set_drvdata(chip, pwmchip_priv(chip));
+ for (i = 0; i < chip->npwm; i++) {
+ struct pwm_device *pwm = &chip->pwms[i];
+
+ pwm->chip = chip;
+ pwm->hwpwm = i;
+ }
+
return chip;
}
EXPORT_SYMBOL_GPL(devm_pwmchip_alloc);
@@ -523,7 +531,6 @@ static bool pwm_ops_check(const struct pwm_chip *chip)
*/
int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
{
- unsigned int i;
int ret;
if (!chip || !chip->dev || !chip->ops || !chip->npwm)
@@ -543,28 +550,16 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
chip->owner = owner;
- chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL);
- if (!chip->pwms)
- return -ENOMEM;
-
mutex_lock(&pwm_lock);
ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL);
if (ret < 0) {
mutex_unlock(&pwm_lock);
- kfree(chip->pwms);
return ret;
}
chip->id = ret;
- for (i = 0; i < chip->npwm; i++) {
- struct pwm_device *pwm = &chip->pwms[i];
-
- pwm->chip = chip;
- pwm->hwpwm = i;
- }
-
mutex_unlock(&pwm_lock);
if (IS_ENABLED(CONFIG_OF))
@@ -594,8 +589,6 @@ void pwmchip_remove(struct pwm_chip *chip)
idr_remove(&pwm_chips, chip->id);
mutex_unlock(&pwm_lock);
-
- kfree(chip->pwms);
}
EXPORT_SYMBOL_GPL(pwmchip_remove);
@@ -290,7 +290,7 @@ struct pwm_chip {
/* only used internally by the PWM framework */
bool uses_pwmchip_alloc;
void *driver_data;
- struct pwm_device *pwms;
+ struct pwm_device pwms[] __counted_by(npwm);
};
static inline struct device *pwmchip_parent(struct pwm_chip *chip)