Skip to content

Commit 129c549

Browse files
amiclausjic23
authored andcommitted
iio: backend: add devm_iio_backend_get_by_index()
Add a new function to get an IIO backend by its index in the io-backends device tree property. This is useful for multi-channel devices that have multiple backends, where looking up by index is more straightforward than using named backends. Extract __devm_iio_backend_fwnode_get_by_index() from the existing __devm_iio_backend_fwnode_get(), taking the index directly as a parameter. The new public API devm_iio_backend_get_by_index() uses the index to find the backend reference in the io-backends property, avoiding the need for io-backend-names. Reviewed-by: David Lechner <dlechner@baylibre.com> Reviewed-by: Nuno Sá <nuno.sa@analog.com> Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 793e056 commit 129c549

2 files changed

Lines changed: 39 additions & 15 deletions

File tree

drivers/iio/industrialio-backend.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -964,23 +964,13 @@ int iio_backend_data_transfer_addr(struct iio_backend *back, u32 address)
964964
}
965965
EXPORT_SYMBOL_NS_GPL(iio_backend_data_transfer_addr, "IIO_BACKEND");
966966

967-
static struct iio_backend *__devm_iio_backend_fwnode_get(struct device *dev, const char *name,
968-
struct fwnode_handle *fwnode)
967+
static struct iio_backend *__devm_iio_backend_fwnode_get_by_index(struct device *dev,
968+
struct fwnode_handle *fwnode,
969+
unsigned int index)
969970
{
970971
struct iio_backend *back;
971-
unsigned int index;
972972
int ret;
973973

974-
if (name) {
975-
ret = device_property_match_string(dev, "io-backend-names",
976-
name);
977-
if (ret < 0)
978-
return ERR_PTR(ret);
979-
index = ret;
980-
} else {
981-
index = 0;
982-
}
983-
984974
struct fwnode_handle *fwnode_back __free(fwnode_handle) =
985975
fwnode_find_reference(fwnode, "io-backends", index);
986976
if (IS_ERR(fwnode_back))
@@ -996,15 +986,32 @@ static struct iio_backend *__devm_iio_backend_fwnode_get(struct device *dev, con
996986
if (ret)
997987
return ERR_PTR(ret);
998988

999-
if (name)
1000-
back->idx = index;
989+
back->idx = index;
1001990

1002991
return back;
1003992
}
1004993

1005994
return ERR_PTR(-EPROBE_DEFER);
1006995
}
1007996

997+
static struct iio_backend *__devm_iio_backend_fwnode_get(struct device *dev, const char *name,
998+
struct fwnode_handle *fwnode)
999+
{
1000+
unsigned int index;
1001+
int ret;
1002+
1003+
if (name) {
1004+
ret = device_property_match_string(dev, "io-backend-names", name);
1005+
if (ret < 0)
1006+
return ERR_PTR(ret);
1007+
index = ret;
1008+
} else {
1009+
index = 0;
1010+
}
1011+
1012+
return __devm_iio_backend_fwnode_get_by_index(dev, fwnode, index);
1013+
}
1014+
10081015
/**
10091016
* devm_iio_backend_get - Device managed backend device get
10101017
* @dev: Consumer device for the backend
@@ -1021,6 +1028,22 @@ struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name)
10211028
}
10221029
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, "IIO_BACKEND");
10231030

1031+
/**
1032+
* devm_iio_backend_get_by_index - Device managed backend device get by index
1033+
* @dev: Consumer device for the backend
1034+
* @index: Index of the backend in the io-backends property
1035+
*
1036+
* Gets the backend at @index associated with @dev.
1037+
*
1038+
* RETURNS:
1039+
* A backend pointer, negative error pointer otherwise.
1040+
*/
1041+
struct iio_backend *devm_iio_backend_get_by_index(struct device *dev, unsigned int index)
1042+
{
1043+
return __devm_iio_backend_fwnode_get_by_index(dev, dev_fwnode(dev), index);
1044+
}
1045+
EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get_by_index, "IIO_BACKEND");
1046+
10241047
/**
10251048
* devm_iio_backend_fwnode_get - Device managed backend firmware node get
10261049
* @dev: Consumer device for the backend

include/linux/iio/backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ int iio_backend_extend_chan_spec(struct iio_backend *back,
261261
bool iio_backend_has_caps(struct iio_backend *back, u32 caps);
262262
void *iio_backend_get_priv(const struct iio_backend *conv);
263263
struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name);
264+
struct iio_backend *devm_iio_backend_get_by_index(struct device *dev, unsigned int index);
264265
struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev,
265266
const char *name,
266267
struct fwnode_handle *fwnode);

0 commit comments

Comments
 (0)