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

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>

authored by

Dmitry Baryshkov and committed by
Jonathan Cameron
6e39b145 9695a2a5

+62 -8
+26 -8
drivers/iio/inkern.c
··· 191 191 return ERR_PTR(err); 192 192 } 193 193 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) 196 196 { 197 197 struct iio_channel *chan = NULL; 198 198 ··· 230 230 231 231 return chan; 232 232 } 233 + EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name); 233 234 234 235 static struct iio_channel *of_iio_channel_get_all(struct device *dev) 235 236 { ··· 272 271 } 273 272 274 273 #else /* CONFIG_OF */ 275 - 276 - static inline struct iio_channel * 277 - of_iio_channel_get_by_name(struct device_node *np, const char *name) 278 - { 279 - return NULL; 280 - } 281 274 282 275 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev) 283 276 { ··· 387 392 return channel; 388 393 } 389 394 EXPORT_SYMBOL_GPL(devm_iio_channel_get); 395 + 396 + struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev, 397 + struct device_node *np, 398 + const char *channel_name) 399 + { 400 + struct iio_channel **ptr, *channel; 401 + 402 + ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL); 403 + if (!ptr) 404 + return ERR_PTR(-ENOMEM); 405 + 406 + channel = of_iio_channel_get_by_name(np, channel_name); 407 + if (IS_ERR(channel)) { 408 + devres_free(ptr); 409 + return channel; 410 + } 411 + 412 + *ptr = channel; 413 + devres_add(dev, ptr); 414 + 415 + return channel; 416 + } 417 + EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name); 390 418 391 419 struct iio_channel *iio_channel_get_all(struct device *dev) 392 420 {
+36
include/linux/iio/consumer.h
··· 13 13 struct iio_dev; 14 14 struct iio_chan_spec; 15 15 struct device; 16 + struct device_node; 16 17 17 18 /** 18 19 * struct iio_channel - everything needed for a consumer to use a channel ··· 97 96 * unbounded. 98 97 */ 99 98 struct iio_channel *devm_iio_channel_get_all(struct device *dev); 99 + 100 + /** 101 + * of_iio_channel_get_by_name() - get description of all that is needed to access channel. 102 + * @np: Pointer to consumer device tree node 103 + * @consumer_channel: Unique name to identify the channel on the consumer 104 + * side. This typically describes the channels use within 105 + * the consumer. E.g. 'battery_voltage' 106 + */ 107 + #ifdef CONFIG_OF 108 + struct iio_channel *of_iio_channel_get_by_name(struct device_node *np, const char *name); 109 + #else 110 + static inline struct iio_channel * 111 + of_iio_channel_get_by_name(struct device_node *np, const char *name) 112 + { 113 + return NULL; 114 + } 115 + #endif 116 + 117 + /** 118 + * devm_of_iio_channel_get_by_name() - Resource managed version of of_iio_channel_get_by_name(). 119 + * @dev: Pointer to consumer device. 120 + * @np: Pointer to consumer device tree node 121 + * @consumer_channel: Unique name to identify the channel on the consumer 122 + * side. This typically describes the channels use within 123 + * the consumer. E.g. 'battery_voltage' 124 + * 125 + * Returns a pointer to negative errno if it is not able to get the iio channel 126 + * otherwise returns valid pointer for iio channel. 127 + * 128 + * The allocated iio channel is automatically released when the device is 129 + * unbound. 130 + */ 131 + struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev, 132 + struct device_node *np, 133 + const char *consumer_channel); 100 134 101 135 struct iio_cb_buffer; 102 136 /**