Skip to content

Commit 6e39b14

Browse files
lumagjic23
authored andcommitted
iio: provide of_iio_channel_get_by_name() and devm_ version it
There might be cases when the IIO channel is attached to the device subnode instead of being attached to the main device node. Allow drivers to query IIO channels by using device tree nodes. Signed-off-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org> Link: https://lore.kernel.org/r/20201204025509.1075506-8-dmitry.baryshkov@linaro.org Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 9695a2a commit 6e39b14

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

drivers/iio/inkern.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
191191
return ERR_PTR(err);
192192
}
193193

194-
static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
195-
const char *name)
194+
struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
195+
const char *name)
196196
{
197197
struct iio_channel *chan = NULL;
198198

@@ -230,6 +230,7 @@ static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
230230

231231
return chan;
232232
}
233+
EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name);
233234

234235
static struct iio_channel *of_iio_channel_get_all(struct device *dev)
235236
{
@@ -272,12 +273,6 @@ static struct iio_channel *of_iio_channel_get_all(struct device *dev)
272273

273274
#else /* CONFIG_OF */
274275

275-
static inline struct iio_channel *
276-
of_iio_channel_get_by_name(struct device_node *np, const char *name)
277-
{
278-
return NULL;
279-
}
280-
281276
static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
282277
{
283278
return NULL;
@@ -393,6 +388,29 @@ struct iio_channel *devm_iio_channel_get(struct device *dev,
393388
}
394389
EXPORT_SYMBOL_GPL(devm_iio_channel_get);
395390

391+
struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev,
392+
struct device_node *np,
393+
const char *channel_name)
394+
{
395+
struct iio_channel **ptr, *channel;
396+
397+
ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
398+
if (!ptr)
399+
return ERR_PTR(-ENOMEM);
400+
401+
channel = of_iio_channel_get_by_name(np, channel_name);
402+
if (IS_ERR(channel)) {
403+
devres_free(ptr);
404+
return channel;
405+
}
406+
407+
*ptr = channel;
408+
devres_add(dev, ptr);
409+
410+
return channel;
411+
}
412+
EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name);
413+
396414
struct iio_channel *iio_channel_get_all(struct device *dev)
397415
{
398416
const char *name;

include/linux/iio/consumer.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
struct iio_dev;
1414
struct iio_chan_spec;
1515
struct device;
16+
struct device_node;
1617

1718
/**
1819
* struct iio_channel - everything needed for a consumer to use a channel
@@ -97,6 +98,41 @@ void iio_channel_release_all(struct iio_channel *chan);
9798
*/
9899
struct iio_channel *devm_iio_channel_get_all(struct device *dev);
99100

101+
/**
102+
* of_iio_channel_get_by_name() - get description of all that is needed to access channel.
103+
* @np: Pointer to consumer device tree node
104+
* @consumer_channel: Unique name to identify the channel on the consumer
105+
* side. This typically describes the channels use within
106+
* the consumer. E.g. 'battery_voltage'
107+
*/
108+
#ifdef CONFIG_OF
109+
struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, const char *name);
110+
#else
111+
static inline struct iio_channel *
112+
of_iio_channel_get_by_name(struct device_node *np, const char *name)
113+
{
114+
return NULL;
115+
}
116+
#endif
117+
118+
/**
119+
* devm_of_iio_channel_get_by_name() - Resource managed version of of_iio_channel_get_by_name().
120+
* @dev: Pointer to consumer device.
121+
* @np: Pointer to consumer device tree node
122+
* @consumer_channel: Unique name to identify the channel on the consumer
123+
* side. This typically describes the channels use within
124+
* the consumer. E.g. 'battery_voltage'
125+
*
126+
* Returns a pointer to negative errno if it is not able to get the iio channel
127+
* otherwise returns valid pointer for iio channel.
128+
*
129+
* The allocated iio channel is automatically released when the device is
130+
* unbound.
131+
*/
132+
struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev,
133+
struct device_node *np,
134+
const char *consumer_channel);
135+
100136
struct iio_cb_buffer;
101137
/**
102138
* iio_channel_get_all_cb() - register callback for triggered capture

0 commit comments

Comments
 (0)