* 'linux-4.14.y' of github.com:openela/kernel-lts: LTS: Update to 4.14.355 Revert "parisc: Use irq_enter_rcu() to fix warning at kernel/context_tracking.c:367" netns: restore ops before calling ops_exit_list cx82310_eth: fix error return code in cx82310_bind() rtmutex: Drop rt_mutex::wait_lock before scheduling locking/rtmutex: Handle non enqueued waiters gracefully in remove_waiter() drm/i915/fence: Mark debug_fence_free() with __maybe_unused ACPI: processor: Fix memory leaks in error paths of processor_add() ACPI: processor: Return an error if acpi_processor_get_info() fails in processor_add() netns: add pre_exit method to struct pernet_operations net: Add comment about pernet_operations methods and synchronization nilfs2: protect references to superblock parameters exposed in sysfs nilfs2: replace snprintf in show functions with sysfs_emit nilfs2: use time64_t internally tracing: Avoid possible softlockup in tracing_iter_reset() ring-buffer: Rename ring_buffer_read() to read_buffer_iter_advance() uprobes: Use kzalloc to allocate xol area clocksource/drivers/imx-tpm: Fix next event not taking effect sometime clocksource/drivers/imx-tpm: Fix return -ETIME when delta exceeds INT_MAX VMCI: Fix use-after-free when removing resource in vmci_resource_remove() nvmem: Fix return type of devm_nvmem_device_get() in kerneldoc iio: fix scale application in iio_convert_raw_to_processed_unlocked iio: buffer-dmaengine: fix releasing dma channel on error ata: pata_macio: Use WARN instead of BUG of/irq: Prevent device address out-of-bounds read in interrupt map walk Squashfs: sanity check symbolic link size usbnet: ipheth: race between ipheth_close and error handling Input: uinput - reject requests with unreasonable number of slots btrfs: initialize location to fix -Wmaybe-uninitialized in btrfs_lookup_dentry() PCI: Add missing bridge lock to pci_bus_lock() btrfs: clean up our handling of refs == 0 in snapshot delete btrfs: replace BUG_ON with ASSERT in walk_down_proc() smp: Add missing destroy_work_on_stack() call in smp_call_on_cpu() wifi: mwifiex: Do not return unused priv in mwifiex_get_priv_by_id() hwmon: (w83627ehf) Fix underflows seen when writing limit attributes hwmon: (nct6775-core) Fix underflows seen when writing limit attributes hwmon: (lm95234) Fix underflows seen when writing limit attributes hwmon: (adc128d818) Fix underflows seen when writing limit attributes pci/hotplug/pnv_php: Fix hotplug driver crash on Powernv devres: Initialize an uninitialized struct member um: line: always fill *error_out in setup_one_line() cgroup: Protect css->cgroup write under css_set_lock iommu/vt-d: Handle volatile descriptor status read rfkill: fix spelling mistake contidion to condition usbnet: modern method to get random MAC net: usb: don't write directly to netdev->dev_addr drivers/net/usb: Remove all strcpy() uses cx82310_eth: re-enable ethernet mode after router reboot igb: Fix not clearing TimeSync interrupts for 82580 can: bcm: Remove proc entry when dev is unregistered. pcmcia: Use resource_size function on resource object media: qcom: camss: Add check for v4l2_fwnode_endpoint_parse wifi: brcmsmac: advertise MFP_CAPABLE to enable WPA3 af_unix: Remove put_pid()/put_cred() in copy_peercred(). irqchip/armada-370-xp: Do not allow mapping IRQ 0 and 1 smack: unix sockets: fix accept()ed socket label ALSA: hda: Add input value sanity checks to HDMI channel map controls nilfs2: fix state management in error path of log writing function nilfs2: fix missing cleanup on rollforward recovery error fuse: use unsigned type for getxattr/listxattr size truncation mmc: dw_mmc: Fix IDMAC operation with pages bigger than 4K ALSA: hda/conexant: Add pincfg quirk to enable top speakers on Sirius devices sch/netem: fix use after free in netem_dequeue ALSA: usb-audio: Fix gpf in snd_usb_pipe_sanity_check ALSA: usb-audio: Sanity checks for each pipe and EP types ALSA: usb-audio: add boot quirk for Axe-Fx III udf: Limit file size to 4TB block: initialize integrity buffer to zero before writing it to media media: uvcvideo: Enforce alignment of frame and interval smack: tcp: ipv4, fix incorrect labeling usbip: Don't submit special requests twice apparmor: fix possible NULL pointer dereference drm/amdgpu: fix mc_data out-of-bounds read warning drm/amdgpu: fix ucode out-of-bounds read warning drm/amdgpu: fix overflowed array index read warning drm/amdgpu: Fix uninitialized variable warning in amdgpu_afmt_acr usb: dwc3: st: add missing depopulate in probe error path usb: dwc3: st: Add of_node_put() before return in probe function net: usb: qmi_wwan: add MeiG Smart SRM825L LTS: Update to 4.14.354 drm/fb-helper: set x/yres_virtual in drm_fb_helper_check_var ipc: remove memcg accounting for sops objects in do_semtimedop() scsi: aacraid: Fix double-free on probe failure usb: core: sysfs: Unmerge @usb3_hardware_lpm_attr_group in remove_power_attributes() usb: dwc3: st: fix probed platform device ref count on probe error path usb: dwc3: core: Prevent USB core invalid event buffer address access usb: dwc3: omap: add missing depopulate in probe error path USB: serial: option: add MeiG Smart SRM825L cdc-acm: Add DISABLE_ECHO quirk for GE HealthCare UI Controller net: busy-poll: use ktime_get_ns() instead of local_clock() gtp: fix a potential NULL pointer dereference net: prevent mss overflow in skb_segment() ida: Fix crash in ida_free when the bitmap is empty net:rds: Fix possible deadlock in rds_message_put fbmem: Check virtual screen sizes in fb_set_var() fbcon: Prevent that screen size is smaller than font size printk: Export is_console_locked memcg: enable accounting of ipc resources cgroup/cpuset: Prevent UAF in proc_cpuset_show() media: uvcvideo: Fix integer overflow calculating timestamp media: uvcvideo: Use ktime_t for timestamps filelock: Correct the filelock owner in fcntl_setlk/fcntl_setlk64 scsi: mpt3sas: Avoid IOMMU page faults on REPORT ZONES dm suspend: return -ERESTARTSYS instead of -EINTR wifi: mwifiex: duplicate static structs used in driver instances pinctrl: single: fix potential NULL dereference in pcs_get_function() drm/amdgpu: Using uninitialized value *size when calling amdgpu_vce_cs_reloc Input: MT - limit max slots Bluetooth: hci_ldisc: check HCI_UART_PROTO_READY flag in HCIUARTGETPROTO ALSA: timer: Relax start tick time check for slave timer elements mmc: dw_mmc: allow biu and ciu clocks to defer HID: wacom: Defer calculation of resolution until resolution_code is known Bluetooth: MGMT: Add error handling to pair_device() mmc: mmc_test: Fix NULL dereference on allocation failure net: xilinx: axienet: Always disable promiscuous mode ipv6: prevent UAF in ip6_send_skb() netfilter: nft_counter: Synchronize nft_counter_reset() against reader. kcm: Serialise kcm_sendmsg() for the same socket. Bluetooth: hci_core: Fix LE quote calculation Bluetooth: hci_core: Fix not handling link timeouts propertly Bluetooth: Make use of __check_timeout on hci_sched_le block: use "unsigned long" for blk_validate_block_size(). gtp: pull network headers in gtp_dev_xmit() hrtimer: Prevent queuing of hrtimer without a function callback nvmet-rdma: fix possible bad dereference when freeing rsps ext4: set the type of max_zeroout to unsigned int to avoid overflow irqchip/gic-v3-its: Remove BUG_ON in its_vpe_irq_domain_alloc usb: dwc3: core: Skip setting event buffers for host only controllers s390/iucv: fix receive buffer virtual vs physical address confusion openrisc: Call setup_memory() earlier in the init sequence NFS: avoid infinite loop in pnfs_update_layout. Bluetooth: bnep: Fix out-of-bound access usb: gadget: fsl: Increase size of name buffer for endpoints f2fs: fix to do sanity check in update_sit_entry btrfs: delete pointless BUG_ON check on quota root in btrfs_qgroup_account_extent() btrfs: send: handle unexpected data in header buffer in begin_cmd() btrfs: handle invalid root reference found in may_destroy_subvol() btrfs: change BUG_ON to assertion when checking for delayed_node root powerpc/boot: Only free if realloc() succeeds powerpc/boot: Handle allocation failure in simple_realloc() parisc: Use irq_enter_rcu() to fix warning at kernel/context_tracking.c:367 md: clean up invalid BUG_ON in md_ioctl net/sun3_82586: Avoid reading past buffer in debug output scsi: lpfc: Initialize status local variable in lpfc_sli4_repost_sgl_list() fs: binfmt_elf_efpic: don't use missing interpreter's properties media: pci: cx23885: check cx23885_vdev_init() return quota: Remove BUG_ON from dqget() ext4: do not trim the group with corrupted block bitmap powerpc/xics: Check return value of kasprintf in icp_native_map_one_cpu wifi: iwlwifi: abort scan when rfkill on but device enabled gfs2: setattr_chown: Add missing initialization scsi: spi: Fix sshdr use binfmt_misc: cleanup on filesystem umount staging: ks7010: disable bh on tx_dev_lock wifi: cw1200: Avoid processing an invalid TIM IE ssb: Fix division by zero issue in ssb_calc_clock_rate atm: idt77252: prevent use after free in dequeue_rx() btrfs: rename bitmap_set_bits() -> btrfs_bitmap_set_bits() overflow: Implement size_t saturating arithmetic helpers overflow.h: Add flex_array_size() helper s390/cio: rename bitmap_size() -> idset_bitmap_size() memcg_write_event_control(): fix a user-triggerable oops drm/amdgpu: Actually check flags for all context ops. selinux: fix potential counting error in avc_add_xperms_decision() include/linux/bitmap.h: make bitmap_fill() and bitmap_zero() consistent dm persistent data: fix memory allocation failure dm resume: don't return EINVAL when signalled ALSA: usb-audio: Support Yamaha P-125 quirk entry fuse: Initialize beyond-EOF page contents before setting uptodate Conflicts: drivers/mmc/core/mmc_test.c drivers/net/usb/usbnet.c Change-Id: I718c5312df474678aea17713b30a8a669bdae348
972 lines
22 KiB
C
972 lines
22 KiB
C
/* The industrial I/O core in kernel channel mapping
|
|
*
|
|
* Copyright (c) 2011 Jonathan Cameron
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
* the Free Software Foundation.
|
|
*/
|
|
#include <linux/err.h>
|
|
#include <linux/export.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/iio/iio.h>
|
|
#include "iio_core.h"
|
|
#include <linux/iio/machine.h>
|
|
#include <linux/iio/driver.h>
|
|
#include <linux/iio/consumer.h>
|
|
|
|
struct iio_map_internal {
|
|
struct iio_dev *indio_dev;
|
|
struct iio_map *map;
|
|
struct list_head l;
|
|
};
|
|
|
|
static LIST_HEAD(iio_map_list);
|
|
static DEFINE_MUTEX(iio_map_list_lock);
|
|
|
|
int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
|
|
{
|
|
int i = 0, ret = 0;
|
|
struct iio_map_internal *mapi;
|
|
|
|
if (maps == NULL)
|
|
return 0;
|
|
|
|
mutex_lock(&iio_map_list_lock);
|
|
while (maps[i].consumer_dev_name != NULL) {
|
|
mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
|
|
if (mapi == NULL) {
|
|
ret = -ENOMEM;
|
|
goto error_ret;
|
|
}
|
|
mapi->map = &maps[i];
|
|
mapi->indio_dev = indio_dev;
|
|
list_add_tail(&mapi->l, &iio_map_list);
|
|
i++;
|
|
}
|
|
error_ret:
|
|
mutex_unlock(&iio_map_list_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_map_array_register);
|
|
|
|
|
|
/*
|
|
* Remove all map entries associated with the given iio device
|
|
*/
|
|
int iio_map_array_unregister(struct iio_dev *indio_dev)
|
|
{
|
|
int ret = -ENODEV;
|
|
struct iio_map_internal *mapi, *next;
|
|
|
|
mutex_lock(&iio_map_list_lock);
|
|
list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
|
|
if (indio_dev == mapi->indio_dev) {
|
|
list_del(&mapi->l);
|
|
kfree(mapi);
|
|
ret = 0;
|
|
}
|
|
}
|
|
mutex_unlock(&iio_map_list_lock);
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_map_array_unregister);
|
|
|
|
static const struct iio_chan_spec
|
|
*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
|
|
{
|
|
int i;
|
|
const struct iio_chan_spec *chan = NULL;
|
|
|
|
for (i = 0; i < indio_dev->num_channels; i++)
|
|
if (indio_dev->channels[i].datasheet_name &&
|
|
strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
|
|
chan = &indio_dev->channels[i];
|
|
break;
|
|
}
|
|
return chan;
|
|
}
|
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static int iio_dev_node_match(struct device *dev, void *data)
|
|
{
|
|
return dev->of_node == data && dev->type == &iio_device_type;
|
|
}
|
|
|
|
/**
|
|
* __of_iio_simple_xlate - translate iiospec to the IIO channel index
|
|
* @indio_dev: pointer to the iio_dev structure
|
|
* @iiospec: IIO specifier as found in the device tree
|
|
*
|
|
* This is simple translation function, suitable for the most 1:1 mapped
|
|
* channels in IIO chips. This function performs only one sanity check:
|
|
* whether IIO index is less than num_channels (that is specified in the
|
|
* iio_dev).
|
|
*/
|
|
static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
|
|
const struct of_phandle_args *iiospec)
|
|
{
|
|
if (!iiospec->args_count)
|
|
return 0;
|
|
|
|
if (iiospec->args[0] >= indio_dev->num_channels) {
|
|
dev_err(&indio_dev->dev, "invalid channel index %u\n",
|
|
iiospec->args[0]);
|
|
return -EINVAL;
|
|
}
|
|
|
|
return iiospec->args[0];
|
|
}
|
|
|
|
static int __of_iio_channel_get(struct iio_channel *channel,
|
|
struct device_node *np, int index)
|
|
{
|
|
struct device *idev;
|
|
struct iio_dev *indio_dev;
|
|
int err;
|
|
struct of_phandle_args iiospec;
|
|
|
|
err = of_parse_phandle_with_args(np, "io-channels",
|
|
"#io-channel-cells",
|
|
index, &iiospec);
|
|
if (err)
|
|
return err;
|
|
|
|
idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
|
|
iio_dev_node_match);
|
|
if (idev == NULL) {
|
|
of_node_put(iiospec.np);
|
|
return -EPROBE_DEFER;
|
|
}
|
|
|
|
indio_dev = dev_to_iio_dev(idev);
|
|
channel->indio_dev = indio_dev;
|
|
if (indio_dev->info->of_xlate)
|
|
index = indio_dev->info->of_xlate(indio_dev, &iiospec);
|
|
else
|
|
index = __of_iio_simple_xlate(indio_dev, &iiospec);
|
|
of_node_put(iiospec.np);
|
|
if (index < 0)
|
|
goto err_put;
|
|
channel->channel = &indio_dev->channels[index];
|
|
|
|
return 0;
|
|
|
|
err_put:
|
|
iio_device_put(indio_dev);
|
|
return index;
|
|
}
|
|
|
|
static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
|
|
{
|
|
struct iio_channel *channel;
|
|
int err;
|
|
|
|
if (index < 0)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
channel = kzalloc(sizeof(*channel), GFP_KERNEL);
|
|
if (channel == NULL)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
err = __of_iio_channel_get(channel, np, index);
|
|
if (err)
|
|
goto err_free_channel;
|
|
|
|
return channel;
|
|
|
|
err_free_channel:
|
|
kfree(channel);
|
|
return ERR_PTR(err);
|
|
}
|
|
|
|
static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
|
|
const char *name)
|
|
{
|
|
struct iio_channel *chan = NULL;
|
|
|
|
/* Walk up the tree of devices looking for a matching iio channel */
|
|
while (np) {
|
|
int index = 0;
|
|
|
|
/*
|
|
* For named iio channels, first look up the name in the
|
|
* "io-channel-names" property. If it cannot be found, the
|
|
* index will be an error code, and of_iio_channel_get()
|
|
* will fail.
|
|
*/
|
|
if (name)
|
|
index = of_property_match_string(np, "io-channel-names",
|
|
name);
|
|
chan = of_iio_channel_get(np, index);
|
|
if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
|
|
break;
|
|
else if (name && index >= 0) {
|
|
pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
|
|
np, name ? name : "", index);
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* No matching IIO channel found on this node.
|
|
* If the parent node has a "io-channel-ranges" property,
|
|
* then we can try one of its channels.
|
|
*/
|
|
np = np->parent;
|
|
if (np && !of_get_property(np, "io-channel-ranges", NULL))
|
|
return NULL;
|
|
}
|
|
|
|
return chan;
|
|
}
|
|
|
|
static struct iio_channel *of_iio_channel_get_all(struct device *dev)
|
|
{
|
|
struct iio_channel *chans;
|
|
int i, mapind, nummaps = 0;
|
|
int ret;
|
|
|
|
do {
|
|
ret = of_parse_phandle_with_args(dev->of_node,
|
|
"io-channels",
|
|
"#io-channel-cells",
|
|
nummaps, NULL);
|
|
if (ret < 0)
|
|
break;
|
|
} while (++nummaps);
|
|
|
|
if (nummaps == 0) /* no error, return NULL to search map table */
|
|
return NULL;
|
|
|
|
/* NULL terminated array to save passing size */
|
|
chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
|
|
if (chans == NULL)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
/* Search for OF matches */
|
|
for (mapind = 0; mapind < nummaps; mapind++) {
|
|
ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
|
|
mapind);
|
|
if (ret)
|
|
goto error_free_chans;
|
|
}
|
|
return chans;
|
|
|
|
error_free_chans:
|
|
for (i = 0; i < mapind; i++)
|
|
iio_device_put(chans[i].indio_dev);
|
|
kfree(chans);
|
|
return ERR_PTR(ret);
|
|
}
|
|
|
|
#else /* CONFIG_OF */
|
|
|
|
static inline struct iio_channel *
|
|
of_iio_channel_get_by_name(struct device_node *np, const char *name)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
#endif /* CONFIG_OF */
|
|
|
|
static struct iio_channel *iio_channel_get_sys(const char *name,
|
|
const char *channel_name)
|
|
{
|
|
struct iio_map_internal *c_i = NULL, *c = NULL;
|
|
struct iio_channel *channel;
|
|
int err;
|
|
|
|
if (name == NULL && channel_name == NULL)
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
/* first find matching entry the channel map */
|
|
mutex_lock(&iio_map_list_lock);
|
|
list_for_each_entry(c_i, &iio_map_list, l) {
|
|
if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
|
|
(channel_name &&
|
|
strcmp(channel_name, c_i->map->consumer_channel) != 0))
|
|
continue;
|
|
c = c_i;
|
|
iio_device_get(c->indio_dev);
|
|
break;
|
|
}
|
|
mutex_unlock(&iio_map_list_lock);
|
|
if (c == NULL)
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
channel = kzalloc(sizeof(*channel), GFP_KERNEL);
|
|
if (channel == NULL) {
|
|
err = -ENOMEM;
|
|
goto error_no_mem;
|
|
}
|
|
|
|
channel->indio_dev = c->indio_dev;
|
|
|
|
if (c->map->adc_channel_label) {
|
|
channel->channel =
|
|
iio_chan_spec_from_name(channel->indio_dev,
|
|
c->map->adc_channel_label);
|
|
|
|
if (channel->channel == NULL) {
|
|
err = -EINVAL;
|
|
goto error_no_chan;
|
|
}
|
|
}
|
|
|
|
return channel;
|
|
|
|
error_no_chan:
|
|
kfree(channel);
|
|
error_no_mem:
|
|
iio_device_put(c->indio_dev);
|
|
return ERR_PTR(err);
|
|
}
|
|
|
|
struct iio_channel *iio_channel_get(struct device *dev,
|
|
const char *channel_name)
|
|
{
|
|
const char *name = dev ? dev_name(dev) : NULL;
|
|
struct iio_channel *channel;
|
|
|
|
if (dev) {
|
|
channel = of_iio_channel_get_by_name(dev->of_node,
|
|
channel_name);
|
|
if (channel != NULL)
|
|
return channel;
|
|
}
|
|
|
|
return iio_channel_get_sys(name, channel_name);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_channel_get);
|
|
|
|
void iio_channel_release(struct iio_channel *channel)
|
|
{
|
|
if (!channel)
|
|
return;
|
|
iio_device_put(channel->indio_dev);
|
|
kfree(channel);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_channel_release);
|
|
|
|
static void devm_iio_channel_free(struct device *dev, void *res)
|
|
{
|
|
struct iio_channel *channel = *(struct iio_channel **)res;
|
|
|
|
iio_channel_release(channel);
|
|
}
|
|
|
|
static int devm_iio_channel_match(struct device *dev, void *res, void *data)
|
|
{
|
|
struct iio_channel **r = res;
|
|
|
|
if (!r || !*r) {
|
|
WARN_ON(!r || !*r);
|
|
return 0;
|
|
}
|
|
|
|
return *r == data;
|
|
}
|
|
|
|
struct iio_channel *devm_iio_channel_get(struct device *dev,
|
|
const char *channel_name)
|
|
{
|
|
struct iio_channel **ptr, *channel;
|
|
|
|
ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
|
|
if (!ptr)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
channel = iio_channel_get(dev, channel_name);
|
|
if (IS_ERR(channel)) {
|
|
devres_free(ptr);
|
|
return channel;
|
|
}
|
|
|
|
*ptr = channel;
|
|
devres_add(dev, ptr);
|
|
|
|
return channel;
|
|
}
|
|
EXPORT_SYMBOL_GPL(devm_iio_channel_get);
|
|
|
|
void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
|
|
{
|
|
WARN_ON(devres_release(dev, devm_iio_channel_free,
|
|
devm_iio_channel_match, channel));
|
|
}
|
|
EXPORT_SYMBOL_GPL(devm_iio_channel_release);
|
|
|
|
struct iio_channel *iio_channel_get_all(struct device *dev)
|
|
{
|
|
const char *name;
|
|
struct iio_channel *chans;
|
|
struct iio_map_internal *c = NULL;
|
|
int nummaps = 0;
|
|
int mapind = 0;
|
|
int i, ret;
|
|
|
|
if (dev == NULL)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
chans = of_iio_channel_get_all(dev);
|
|
if (chans)
|
|
return chans;
|
|
|
|
name = dev_name(dev);
|
|
|
|
mutex_lock(&iio_map_list_lock);
|
|
/* first count the matching maps */
|
|
list_for_each_entry(c, &iio_map_list, l)
|
|
if (name && strcmp(name, c->map->consumer_dev_name) != 0)
|
|
continue;
|
|
else
|
|
nummaps++;
|
|
|
|
if (nummaps == 0) {
|
|
ret = -ENODEV;
|
|
goto error_ret;
|
|
}
|
|
|
|
/* NULL terminated array to save passing size */
|
|
chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
|
|
if (chans == NULL) {
|
|
ret = -ENOMEM;
|
|
goto error_ret;
|
|
}
|
|
|
|
/* for each map fill in the chans element */
|
|
list_for_each_entry(c, &iio_map_list, l) {
|
|
if (name && strcmp(name, c->map->consumer_dev_name) != 0)
|
|
continue;
|
|
chans[mapind].indio_dev = c->indio_dev;
|
|
chans[mapind].data = c->map->consumer_data;
|
|
chans[mapind].channel =
|
|
iio_chan_spec_from_name(chans[mapind].indio_dev,
|
|
c->map->adc_channel_label);
|
|
if (chans[mapind].channel == NULL) {
|
|
ret = -EINVAL;
|
|
goto error_free_chans;
|
|
}
|
|
iio_device_get(chans[mapind].indio_dev);
|
|
mapind++;
|
|
}
|
|
if (mapind == 0) {
|
|
ret = -ENODEV;
|
|
goto error_free_chans;
|
|
}
|
|
mutex_unlock(&iio_map_list_lock);
|
|
|
|
return chans;
|
|
|
|
error_free_chans:
|
|
for (i = 0; i < nummaps; i++)
|
|
iio_device_put(chans[i].indio_dev);
|
|
kfree(chans);
|
|
error_ret:
|
|
mutex_unlock(&iio_map_list_lock);
|
|
|
|
return ERR_PTR(ret);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_channel_get_all);
|
|
|
|
void iio_channel_release_all(struct iio_channel *channels)
|
|
{
|
|
struct iio_channel *chan = &channels[0];
|
|
|
|
while (chan->indio_dev) {
|
|
iio_device_put(chan->indio_dev);
|
|
chan++;
|
|
}
|
|
kfree(channels);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_channel_release_all);
|
|
|
|
static void devm_iio_channel_free_all(struct device *dev, void *res)
|
|
{
|
|
struct iio_channel *channels = *(struct iio_channel **)res;
|
|
|
|
iio_channel_release_all(channels);
|
|
}
|
|
|
|
struct iio_channel *devm_iio_channel_get_all(struct device *dev)
|
|
{
|
|
struct iio_channel **ptr, *channels;
|
|
|
|
ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
|
|
if (!ptr)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
channels = iio_channel_get_all(dev);
|
|
if (IS_ERR(channels)) {
|
|
devres_free(ptr);
|
|
return channels;
|
|
}
|
|
|
|
*ptr = channels;
|
|
devres_add(dev, ptr);
|
|
|
|
return channels;
|
|
}
|
|
EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
|
|
|
|
void devm_iio_channel_release_all(struct device *dev,
|
|
struct iio_channel *channels)
|
|
{
|
|
WARN_ON(devres_release(dev, devm_iio_channel_free_all,
|
|
devm_iio_channel_match, channels));
|
|
}
|
|
EXPORT_SYMBOL_GPL(devm_iio_channel_release_all);
|
|
|
|
static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
|
|
enum iio_chan_info_enum info)
|
|
{
|
|
int unused;
|
|
int vals[INDIO_MAX_RAW_ELEMENTS];
|
|
int ret;
|
|
int val_len = 2;
|
|
|
|
if (val2 == NULL)
|
|
val2 = &unused;
|
|
|
|
if (!iio_channel_has_info(chan->channel, info))
|
|
return -EINVAL;
|
|
|
|
if (chan->indio_dev->info->read_raw_multi) {
|
|
ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
|
|
chan->channel, INDIO_MAX_RAW_ELEMENTS,
|
|
vals, &val_len, info);
|
|
*val = vals[0];
|
|
*val2 = vals[1];
|
|
} else
|
|
ret = chan->indio_dev->info->read_raw(chan->indio_dev,
|
|
chan->channel, val, val2, info);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int iio_read_channel_raw(struct iio_channel *chan, int *val)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_raw);
|
|
|
|
int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
|
|
|
|
static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
|
|
int raw, int *processed, unsigned int scale)
|
|
{
|
|
int scale_type, scale_val, scale_val2;
|
|
int offset_type, offset_val, offset_val2;
|
|
s64 raw64 = raw;
|
|
|
|
offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
|
|
IIO_CHAN_INFO_OFFSET);
|
|
if (offset_type >= 0) {
|
|
switch (offset_type) {
|
|
case IIO_VAL_INT:
|
|
break;
|
|
case IIO_VAL_INT_PLUS_MICRO:
|
|
case IIO_VAL_INT_PLUS_NANO:
|
|
/*
|
|
* Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
|
|
* implicitely truncate the offset to it's integer form.
|
|
*/
|
|
break;
|
|
case IIO_VAL_FRACTIONAL:
|
|
offset_val /= offset_val2;
|
|
break;
|
|
case IIO_VAL_FRACTIONAL_LOG2:
|
|
offset_val >>= offset_val2;
|
|
break;
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
|
|
raw64 += offset_val;
|
|
}
|
|
|
|
scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
|
|
IIO_CHAN_INFO_SCALE);
|
|
if (scale_type < 0) {
|
|
/*
|
|
* If no channel scaling is available apply consumer scale to
|
|
* raw value and return.
|
|
*/
|
|
*processed = raw * scale;
|
|
return 0;
|
|
}
|
|
|
|
switch (scale_type) {
|
|
case IIO_VAL_INT:
|
|
*processed = raw64 * scale_val * scale;
|
|
break;
|
|
case IIO_VAL_INT_PLUS_MICRO:
|
|
if (scale_val2 < 0)
|
|
*processed = -raw64 * scale_val * scale;
|
|
else
|
|
*processed = raw64 * scale_val * scale;
|
|
*processed += div_s64(raw64 * (s64)scale_val2 * scale,
|
|
1000000LL);
|
|
break;
|
|
case IIO_VAL_INT_PLUS_NANO:
|
|
if (scale_val2 < 0)
|
|
*processed = -raw64 * scale_val * scale;
|
|
else
|
|
*processed = raw64 * scale_val * scale;
|
|
*processed += div_s64(raw64 * (s64)scale_val2 * scale,
|
|
1000000000LL);
|
|
break;
|
|
case IIO_VAL_FRACTIONAL:
|
|
*processed = div_s64(raw64 * (s64)scale_val * scale,
|
|
scale_val2);
|
|
break;
|
|
case IIO_VAL_FRACTIONAL_LOG2:
|
|
*processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
|
|
break;
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
|
|
int *processed, unsigned int scale)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
|
|
scale);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
|
|
|
|
static int iio_read_channel_attribute(struct iio_channel *chan,
|
|
int *val, int *val2,
|
|
enum iio_chan_info_enum attribute)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_read(chan, val, val2, attribute);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
|
|
{
|
|
return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_offset);
|
|
|
|
int iio_read_channel_processed(struct iio_channel *chan, int *val)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
|
|
ret = iio_channel_read(chan, val, NULL,
|
|
IIO_CHAN_INFO_PROCESSED);
|
|
} else {
|
|
ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
|
|
if (ret < 0)
|
|
goto err_unlock;
|
|
ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
|
|
}
|
|
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_processed);
|
|
|
|
int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
|
|
{
|
|
return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_scale);
|
|
|
|
static int iio_channel_read_avail(struct iio_channel *chan,
|
|
const int **vals, int *type, int *length,
|
|
enum iio_chan_info_enum info)
|
|
{
|
|
if (!iio_channel_has_available(chan->channel, info))
|
|
return -EINVAL;
|
|
|
|
return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
|
|
vals, type, length, info);
|
|
}
|
|
|
|
int iio_read_avail_channel_raw(struct iio_channel *chan,
|
|
const int **vals, int *length)
|
|
{
|
|
int ret;
|
|
int type;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (!chan->indio_dev->info) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_read_avail(chan,
|
|
vals, &type, length, IIO_CHAN_INFO_RAW);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
if (ret >= 0 && type != IIO_VAL_INT)
|
|
/* raw values are assumed to be IIO_VAL_INT */
|
|
ret = -EINVAL;
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
|
|
|
|
static int iio_channel_read_max(struct iio_channel *chan,
|
|
int *val, int *val2, int *type,
|
|
enum iio_chan_info_enum info)
|
|
{
|
|
int unused;
|
|
const int *vals;
|
|
int length;
|
|
int ret;
|
|
|
|
if (!val2)
|
|
val2 = &unused;
|
|
|
|
ret = iio_channel_read_avail(chan, &vals, type, &length, info);
|
|
switch (ret) {
|
|
case IIO_AVAIL_RANGE:
|
|
switch (*type) {
|
|
case IIO_VAL_INT:
|
|
*val = vals[2];
|
|
break;
|
|
default:
|
|
*val = vals[4];
|
|
*val2 = vals[5];
|
|
}
|
|
return 0;
|
|
|
|
case IIO_AVAIL_LIST:
|
|
if (length <= 0)
|
|
return -EINVAL;
|
|
switch (*type) {
|
|
case IIO_VAL_INT:
|
|
*val = vals[--length];
|
|
while (length) {
|
|
if (vals[--length] > *val)
|
|
*val = vals[length];
|
|
}
|
|
break;
|
|
default:
|
|
/* FIXME: learn about max for other iio values */
|
|
return -EINVAL;
|
|
}
|
|
return 0;
|
|
|
|
default:
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
|
|
{
|
|
int ret;
|
|
int type;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (!chan->indio_dev->info) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
|
|
|
|
int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
|
|
{
|
|
int ret = 0;
|
|
/* Need to verify underlying driver has not gone away */
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
*type = chan->channel->type;
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_get_channel_type);
|
|
|
|
static int iio_channel_write(struct iio_channel *chan, int val, int val2,
|
|
enum iio_chan_info_enum info)
|
|
{
|
|
return chan->indio_dev->info->write_raw(chan->indio_dev,
|
|
chan->channel, val, val2, info);
|
|
}
|
|
|
|
int iio_write_channel_raw(struct iio_channel *chan, int val)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_write_channel_raw);
|
|
|
|
unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
|
|
{
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
unsigned int i = 0;
|
|
|
|
if (!chan->channel->ext_info)
|
|
return i;
|
|
|
|
for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
|
|
++i;
|
|
|
|
return i;
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
|
|
|
|
static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
|
|
const struct iio_channel *chan,
|
|
const char *attr)
|
|
{
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
if (!chan->channel->ext_info)
|
|
return NULL;
|
|
|
|
for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
|
|
if (!strcmp(attr, ext_info->name))
|
|
return ext_info;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
|
|
const char *attr, char *buf)
|
|
{
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
ext_info = iio_lookup_ext_info(chan, attr);
|
|
if (!ext_info)
|
|
return -EINVAL;
|
|
|
|
return ext_info->read(chan->indio_dev, ext_info->private,
|
|
chan->channel, buf);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
|
|
|
|
ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
|
|
const char *buf, size_t len)
|
|
{
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
ext_info = iio_lookup_ext_info(chan, attr);
|
|
if (!ext_info)
|
|
return -EINVAL;
|
|
|
|
return ext_info->write(chan->indio_dev, ext_info->private,
|
|
chan->channel, buf, len);
|
|
}
|
|
EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
|
|
|
|
int iio_write_channel_processed(struct iio_channel *chan, int val)
|
|
{
|
|
int ret;
|
|
|
|
mutex_lock(&chan->indio_dev->info_exist_lock);
|
|
if (chan->indio_dev->info == NULL) {
|
|
ret = -ENODEV;
|
|
goto err_unlock;
|
|
}
|
|
|
|
ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_PROCESSED);
|
|
err_unlock:
|
|
mutex_unlock(&chan->indio_dev->info_exist_lock);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL(iio_write_channel_processed);
|