| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * iio/adc/max1363.c |
|---|
| 3 | 4 | * Copyright (C) 2008-2010 Jonathan Cameron |
|---|
| .. | .. |
|---|
| 9 | 10 | * Copyright (C) 2000 Russell King |
|---|
| 10 | 11 | * |
|---|
| 11 | 12 | * Driver for max1363 and similar chips. |
|---|
| 12 | | - * |
|---|
| 13 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 14 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 15 | | - * published by the Free Software Foundation. |
|---|
| 16 | 13 | */ |
|---|
| 17 | 14 | |
|---|
| 18 | 15 | #include <linux/interrupt.h> |
|---|
| .. | .. |
|---|
| 25 | 22 | #include <linux/slab.h> |
|---|
| 26 | 23 | #include <linux/err.h> |
|---|
| 27 | 24 | #include <linux/module.h> |
|---|
| 28 | | -#include <linux/of.h> |
|---|
| 29 | | -#include <linux/of_device.h> |
|---|
| 25 | +#include <linux/mod_devicetable.h> |
|---|
| 26 | +#include <linux/property.h> |
|---|
| 30 | 27 | |
|---|
| 31 | 28 | #include <linux/iio/iio.h> |
|---|
| 32 | 29 | #include <linux/iio/sysfs.h> |
|---|
| .. | .. |
|---|
| 153 | 150 | * @current_mode: the scan mode of this chip |
|---|
| 154 | 151 | * @requestedmask: a valid requested set of channels |
|---|
| 155 | 152 | * @reg: supply regulator |
|---|
| 153 | + * @lock: lock to ensure state is consistent |
|---|
| 156 | 154 | * @monitor_on: whether monitor mode is enabled |
|---|
| 157 | 155 | * @monitor_speed: parameter corresponding to device monitor speed setting |
|---|
| 158 | 156 | * @mask_high: bitmask for enabled high thresholds |
|---|
| .. | .. |
|---|
| 172 | 170 | const struct max1363_mode *current_mode; |
|---|
| 173 | 171 | u32 requestedmask; |
|---|
| 174 | 172 | struct regulator *reg; |
|---|
| 173 | + struct mutex lock; |
|---|
| 175 | 174 | |
|---|
| 176 | 175 | /* Using monitor modes and buffer at the same time is |
|---|
| 177 | 176 | currently not supported */ |
|---|
| .. | .. |
|---|
| 367 | 366 | struct max1363_state *st = iio_priv(indio_dev); |
|---|
| 368 | 367 | struct i2c_client *client = st->client; |
|---|
| 369 | 368 | |
|---|
| 370 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 369 | + ret = iio_device_claim_direct_mode(indio_dev); |
|---|
| 370 | + if (ret) |
|---|
| 371 | + return ret; |
|---|
| 372 | + mutex_lock(&st->lock); |
|---|
| 373 | + |
|---|
| 371 | 374 | /* |
|---|
| 372 | 375 | * If monitor mode is enabled, the method for reading a single |
|---|
| 373 | 376 | * channel will have to be rather different and has not yet |
|---|
| .. | .. |
|---|
| 375 | 378 | * |
|---|
| 376 | 379 | * Also, cannot read directly if buffered capture enabled. |
|---|
| 377 | 380 | */ |
|---|
| 378 | | - if (st->monitor_on || iio_buffer_enabled(indio_dev)) { |
|---|
| 381 | + if (st->monitor_on) { |
|---|
| 379 | 382 | ret = -EBUSY; |
|---|
| 380 | 383 | goto error_ret; |
|---|
| 381 | 384 | } |
|---|
| .. | .. |
|---|
| 407 | 410 | data = rxbuf[0]; |
|---|
| 408 | 411 | } |
|---|
| 409 | 412 | *val = data; |
|---|
| 413 | + |
|---|
| 410 | 414 | error_ret: |
|---|
| 411 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 415 | + mutex_unlock(&st->lock); |
|---|
| 416 | + iio_device_release_direct_mode(indio_dev); |
|---|
| 412 | 417 | return ret; |
|---|
| 413 | 418 | |
|---|
| 414 | 419 | } |
|---|
| .. | .. |
|---|
| 708 | 713 | if (!found) |
|---|
| 709 | 714 | return -EINVAL; |
|---|
| 710 | 715 | |
|---|
| 711 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 716 | + mutex_lock(&st->lock); |
|---|
| 712 | 717 | st->monitor_speed = i; |
|---|
| 713 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 718 | + mutex_unlock(&st->lock); |
|---|
| 714 | 719 | |
|---|
| 715 | 720 | return 0; |
|---|
| 716 | 721 | } |
|---|
| .. | .. |
|---|
| 813 | 818 | int val; |
|---|
| 814 | 819 | int number = chan->channel; |
|---|
| 815 | 820 | |
|---|
| 816 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 821 | + mutex_lock(&st->lock); |
|---|
| 817 | 822 | if (dir == IIO_EV_DIR_FALLING) |
|---|
| 818 | 823 | val = (1 << number) & st->mask_low; |
|---|
| 819 | 824 | else |
|---|
| 820 | 825 | val = (1 << number) & st->mask_high; |
|---|
| 821 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 826 | + mutex_unlock(&st->lock); |
|---|
| 822 | 827 | |
|---|
| 823 | 828 | return val; |
|---|
| 824 | 829 | } |
|---|
| .. | .. |
|---|
| 965 | 970 | u16 unifiedmask; |
|---|
| 966 | 971 | int number = chan->channel; |
|---|
| 967 | 972 | |
|---|
| 968 | | - mutex_lock(&indio_dev->mlock); |
|---|
| 973 | + ret = iio_device_claim_direct_mode(indio_dev); |
|---|
| 974 | + if (ret) |
|---|
| 975 | + return ret; |
|---|
| 976 | + mutex_lock(&st->lock); |
|---|
| 977 | + |
|---|
| 969 | 978 | unifiedmask = st->mask_low | st->mask_high; |
|---|
| 970 | 979 | if (dir == IIO_EV_DIR_FALLING) { |
|---|
| 971 | 980 | |
|---|
| .. | .. |
|---|
| 992 | 1001 | |
|---|
| 993 | 1002 | max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low)); |
|---|
| 994 | 1003 | error_ret: |
|---|
| 995 | | - mutex_unlock(&indio_dev->mlock); |
|---|
| 1004 | + mutex_unlock(&st->lock); |
|---|
| 1005 | + iio_device_release_direct_mode(indio_dev); |
|---|
| 996 | 1006 | |
|---|
| 997 | 1007 | return ret; |
|---|
| 998 | 1008 | } |
|---|
| .. | .. |
|---|
| 1519 | 1529 | return IRQ_HANDLED; |
|---|
| 1520 | 1530 | } |
|---|
| 1521 | 1531 | |
|---|
| 1522 | | -#ifdef CONFIG_OF |
|---|
| 1523 | | - |
|---|
| 1524 | 1532 | #define MAX1363_COMPATIBLE(of_compatible, cfg) { \ |
|---|
| 1525 | 1533 | .compatible = of_compatible, \ |
|---|
| 1526 | 1534 | .data = &max1363_chip_info_tbl[cfg], \ |
|---|
| .. | .. |
|---|
| 1568 | 1576 | { /* sentinel */ } |
|---|
| 1569 | 1577 | }; |
|---|
| 1570 | 1578 | MODULE_DEVICE_TABLE(of, max1363_of_match); |
|---|
| 1571 | | -#endif |
|---|
| 1572 | 1579 | |
|---|
| 1573 | 1580 | static int max1363_probe(struct i2c_client *client, |
|---|
| 1574 | 1581 | const struct i2c_device_id *id) |
|---|
| .. | .. |
|---|
| 1583 | 1590 | if (!indio_dev) |
|---|
| 1584 | 1591 | return -ENOMEM; |
|---|
| 1585 | 1592 | |
|---|
| 1586 | | - indio_dev->dev.of_node = client->dev.of_node; |
|---|
| 1587 | 1593 | ret = iio_map_array_register(indio_dev, client->dev.platform_data); |
|---|
| 1588 | 1594 | if (ret < 0) |
|---|
| 1589 | 1595 | return ret; |
|---|
| 1590 | 1596 | |
|---|
| 1591 | 1597 | st = iio_priv(indio_dev); |
|---|
| 1592 | 1598 | |
|---|
| 1599 | + mutex_init(&st->lock); |
|---|
| 1593 | 1600 | st->reg = devm_regulator_get(&client->dev, "vcc"); |
|---|
| 1594 | 1601 | if (IS_ERR(st->reg)) { |
|---|
| 1595 | 1602 | ret = PTR_ERR(st->reg); |
|---|
| .. | .. |
|---|
| 1603 | 1610 | /* this is only used for device removal purposes */ |
|---|
| 1604 | 1611 | i2c_set_clientdata(client, indio_dev); |
|---|
| 1605 | 1612 | |
|---|
| 1606 | | - st->chip_info = of_device_get_match_data(&client->dev); |
|---|
| 1613 | + st->chip_info = device_get_match_data(&client->dev); |
|---|
| 1607 | 1614 | if (!st->chip_info) |
|---|
| 1608 | 1615 | st->chip_info = &max1363_chip_info_tbl[id->driver_data]; |
|---|
| 1609 | 1616 | st->client = client; |
|---|
| .. | .. |
|---|
| 1641 | 1648 | if (ret) |
|---|
| 1642 | 1649 | goto error_disable_reg; |
|---|
| 1643 | 1650 | |
|---|
| 1644 | | - /* Establish that the iio_dev is a child of the i2c device */ |
|---|
| 1645 | | - indio_dev->dev.parent = &client->dev; |
|---|
| 1646 | | - indio_dev->dev.of_node = client->dev.of_node; |
|---|
| 1647 | 1651 | indio_dev->name = id->name; |
|---|
| 1648 | 1652 | indio_dev->channels = st->chip_info->channels; |
|---|
| 1649 | 1653 | indio_dev->num_channels = st->chip_info->num_channels; |
|---|
| .. | .. |
|---|
| 1749 | 1753 | static struct i2c_driver max1363_driver = { |
|---|
| 1750 | 1754 | .driver = { |
|---|
| 1751 | 1755 | .name = "max1363", |
|---|
| 1752 | | - .of_match_table = of_match_ptr(max1363_of_match), |
|---|
| 1756 | + .of_match_table = max1363_of_match, |
|---|
| 1753 | 1757 | }, |
|---|
| 1754 | 1758 | .probe = max1363_probe, |
|---|
| 1755 | 1759 | .remove = max1363_remove, |
|---|