staging/iio: (iio_hwmon) Use devm_kzalloc
Signed-off-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
This commit is contained in:
parent
4d2e4fc224
commit
c4ac7b98bd
1 changed files with 22 additions and 48 deletions
|
@ -55,63 +55,47 @@ static ssize_t iio_hwmon_read_val(struct device *dev,
|
||||||
return sprintf(buf, "%d\n", result);
|
return sprintf(buf, "%d\n", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void iio_hwmon_free_attrs(struct iio_hwmon_state *st)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
struct sensor_device_attribute *a;
|
|
||||||
for (i = 0; i < st->num_channels; i++)
|
|
||||||
if (st->attrs[i]) {
|
|
||||||
a = to_sensor_dev_attr(
|
|
||||||
container_of(st->attrs[i],
|
|
||||||
struct device_attribute,
|
|
||||||
attr));
|
|
||||||
kfree(a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int iio_hwmon_probe(struct platform_device *pdev)
|
static int iio_hwmon_probe(struct platform_device *pdev)
|
||||||
{
|
{
|
||||||
|
struct device *dev = &pdev->dev;
|
||||||
struct iio_hwmon_state *st;
|
struct iio_hwmon_state *st;
|
||||||
struct sensor_device_attribute *a;
|
struct sensor_device_attribute *a;
|
||||||
int ret, i;
|
int ret, i;
|
||||||
int in_i = 1, temp_i = 1, curr_i = 1;
|
int in_i = 1, temp_i = 1, curr_i = 1;
|
||||||
enum iio_chan_type type;
|
enum iio_chan_type type;
|
||||||
|
|
||||||
st = kzalloc(sizeof(*st), GFP_KERNEL);
|
st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL);
|
||||||
if (st == NULL) {
|
if (st == NULL)
|
||||||
ret = -ENOMEM;
|
return -ENOMEM;
|
||||||
goto error_ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
st->channels = iio_channel_get_all(dev_name(&pdev->dev));
|
st->channels = iio_channel_get_all(dev_name(dev));
|
||||||
if (IS_ERR(st->channels)) {
|
if (IS_ERR(st->channels))
|
||||||
ret = PTR_ERR(st->channels);
|
return PTR_ERR(st->channels);
|
||||||
goto error_free_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* count how many attributes we have */
|
/* count how many attributes we have */
|
||||||
while (st->channels[st->num_channels].indio_dev)
|
while (st->channels[st->num_channels].indio_dev)
|
||||||
st->num_channels++;
|
st->num_channels++;
|
||||||
|
|
||||||
st->attrs = kzalloc(sizeof(*st->attrs) * (st->num_channels + 1),
|
st->attrs = devm_kzalloc(dev,
|
||||||
|
sizeof(*st->attrs) * (st->num_channels + 1),
|
||||||
GFP_KERNEL);
|
GFP_KERNEL);
|
||||||
if (st->attrs == NULL) {
|
if (st->attrs == NULL) {
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto error_release_channels;
|
goto error_release_channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < st->num_channels; i++) {
|
for (i = 0; i < st->num_channels; i++) {
|
||||||
a = kzalloc(sizeof(*a), GFP_KERNEL);
|
a = devm_kzalloc(dev, sizeof(*a), GFP_KERNEL);
|
||||||
if (a == NULL) {
|
if (a == NULL) {
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto error_free_attrs;
|
goto error_release_channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
sysfs_attr_init(&a->dev_attr.attr);
|
sysfs_attr_init(&a->dev_attr.attr);
|
||||||
ret = iio_get_channel_type(&st->channels[i], &type);
|
ret = iio_get_channel_type(&st->channels[i], &type);
|
||||||
if (ret < 0) {
|
if (ret < 0)
|
||||||
kfree(a);
|
goto error_release_channels;
|
||||||
goto error_free_attrs;
|
|
||||||
}
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case IIO_VOLTAGE:
|
case IIO_VOLTAGE:
|
||||||
a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
|
a->dev_attr.attr.name = kasprintf(GFP_KERNEL,
|
||||||
|
@ -130,13 +114,11 @@ static int iio_hwmon_probe(struct platform_device *pdev)
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
ret = -EINVAL;
|
ret = -EINVAL;
|
||||||
kfree(a);
|
goto error_release_channels;
|
||||||
goto error_free_attrs;
|
|
||||||
}
|
}
|
||||||
if (a->dev_attr.attr.name == NULL) {
|
if (a->dev_attr.attr.name == NULL) {
|
||||||
kfree(a);
|
|
||||||
ret = -ENOMEM;
|
ret = -ENOMEM;
|
||||||
goto error_free_attrs;
|
goto error_release_channels;
|
||||||
}
|
}
|
||||||
a->dev_attr.show = iio_hwmon_read_val;
|
a->dev_attr.show = iio_hwmon_read_val;
|
||||||
a->dev_attr.attr.mode = S_IRUGO;
|
a->dev_attr.attr.mode = S_IRUGO;
|
||||||
|
@ -146,11 +128,11 @@ static int iio_hwmon_probe(struct platform_device *pdev)
|
||||||
|
|
||||||
st->attr_group.attrs = st->attrs;
|
st->attr_group.attrs = st->attrs;
|
||||||
platform_set_drvdata(pdev, st);
|
platform_set_drvdata(pdev, st);
|
||||||
ret = sysfs_create_group(&pdev->dev.kobj, &st->attr_group);
|
ret = sysfs_create_group(&dev->kobj, &st->attr_group);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto error_free_attrs;
|
goto error_release_channels;
|
||||||
|
|
||||||
st->hwmon_dev = hwmon_device_register(&pdev->dev);
|
st->hwmon_dev = hwmon_device_register(dev);
|
||||||
if (IS_ERR(st->hwmon_dev)) {
|
if (IS_ERR(st->hwmon_dev)) {
|
||||||
ret = PTR_ERR(st->hwmon_dev);
|
ret = PTR_ERR(st->hwmon_dev);
|
||||||
goto error_remove_group;
|
goto error_remove_group;
|
||||||
|
@ -158,15 +140,9 @@ static int iio_hwmon_probe(struct platform_device *pdev)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_remove_group:
|
error_remove_group:
|
||||||
sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
|
sysfs_remove_group(&dev->kobj, &st->attr_group);
|
||||||
error_free_attrs:
|
|
||||||
iio_hwmon_free_attrs(st);
|
|
||||||
kfree(st->attrs);
|
|
||||||
error_release_channels:
|
error_release_channels:
|
||||||
iio_channel_release_all(st->channels);
|
iio_channel_release_all(st->channels);
|
||||||
error_free_state:
|
|
||||||
kfree(st);
|
|
||||||
error_ret:
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,8 +152,6 @@ static int iio_hwmon_remove(struct platform_device *pdev)
|
||||||
|
|
||||||
hwmon_device_unregister(st->hwmon_dev);
|
hwmon_device_unregister(st->hwmon_dev);
|
||||||
sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
|
sysfs_remove_group(&pdev->dev.kobj, &st->attr_group);
|
||||||
iio_hwmon_free_attrs(st);
|
|
||||||
kfree(st->attrs);
|
|
||||||
iio_channel_release_all(st->channels);
|
iio_channel_release_all(st->channels);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue