linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Matt Ranostay <matt.ranostay@konsulko.com>
Cc: lars@metafoo.de, linux-input@vger.kernel.org,
	linux-iio@vger.kernel.org, Rishi Gupta <gupt21@gmail.com>,
	Lee Jones <lee.jones@linaro.org>
Subject: Re: [PATCH v2] HID: mcp2221: add ADC support
Date: Sat, 21 Nov 2020 15:13:58 +0000	[thread overview]
Message-ID: <20201121151358.5b82798a@archlinux> (raw)
In-Reply-To: <20201121072833.40326-1-matt.ranostay@konsulko.com>

On Sat, 21 Nov 2020 09:28:33 +0200
Matt Ranostay <matt.ranostay@konsulko.com> wrote:

> Add support for the three 10-bit ADC channels registered via
> the IIO subsystem.
> 
> Cc: linux-input@vger.kernel.org
> Cc: linux-iio@vger.kernel.org
> CC: Rishi Gupta <gupt21@gmail.com>
> Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>

If we do decide to go this way, I'd rather see this done via a separate
.c file and appropriate ifdefs all confined to a header.

You'll need to stub out a few more things, but will probably end up
with a cleaner architecture as a result.

I'm still unconvinced this shouldn't be an MFD...
+CC Lee for his view on that option.

Jonathan

> ---
> 
> Changes from v1:
> 
> * Removed 'select IIO' from Kconfig
> * Removed useless iio/sysfs.h include
> * Add IS_REACHABLE checks for IIO subsystem 
> 
>  drivers/hid/hid-mcp2221.c | 89 +++++++++++++++++++++++++++++++++++++++
>  1 file changed, 89 insertions(+)
> 
> diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c
> index 0d27ccb55dd9..36fa9336c4c5 100644
> --- a/drivers/hid/hid-mcp2221.c
> +++ b/drivers/hid/hid-mcp2221.c
> @@ -16,6 +16,7 @@
>  #include <linux/hidraw.h>
>  #include <linux/i2c.h>
>  #include <linux/gpio/driver.h>
> +#include <linux/iio/iio.h>
>  #include "hid-ids.h"
>  
>  /* Commands codes in a raw output report */
> @@ -56,6 +57,7 @@ enum {
>   */
>  struct mcp2221 {
>  	struct hid_device *hdev;
> +	struct iio_dev *indio_dev;
>  	struct i2c_adapter adapter;
>  	struct mutex lock;
>  	struct completion wait_in_report;
> @@ -67,6 +69,11 @@ struct mcp2221 {
>  	struct gpio_chip *gc;
>  	u8 gp_idx;
>  	u8 gpio_dir;
> +	u16 adc_values[3];
> +};
> +
> +struct mcp2221_iio {
> +	struct mcp2221 *mcp;
>  };
>  
>  /*
> @@ -712,6 +719,7 @@ static int mcp2221_raw_event(struct hid_device *hdev,
>  				break;
>  			}
>  			mcp->status = mcp_get_i2c_eng_state(mcp, data, 8);
> +			memcpy(&mcp->adc_values, &data[50], 6);
>  			break;
>  		default:
>  			mcp->status = -EIO;
> @@ -791,6 +799,79 @@ static int mcp2221_raw_event(struct hid_device *hdev,
>  	return 1;
>  }
>  
> +#if IS_REACHABLE(CONFIG_IIO)
> +
> +static int mcp2221_read_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *channel, int *val,
> +			    int *val2, long mask)
> +{
> +
> +	struct mcp2221_iio *priv = iio_priv(indio_dev);
> +	struct mcp2221 *mcp = priv->mcp;
> +	int ret;
> +
> +	mutex_lock(&mcp->lock);
> +
> +	/* Read ADC values */
> +	ret = mcp_chk_last_cmd_status(mcp);
> +	if (ret < 0)
> +		return ret;
> +
> +	*val = le16_to_cpu(mcp->adc_values[channel->channel]);
> +
> +	mutex_unlock(&mcp->lock);
> +
> +	return IIO_VAL_INT;
> +}
> +
> +static const struct iio_info mcp2221_info = {
> +	.read_raw = &mcp2221_read_raw,
> +};
> +
> +#define MCP2221_ADC_CHANNEL(idx) \
> +	{ \
> +		.type = IIO_VOLTAGE, \
> +		.indexed = 1, \
> +		.channel = idx, \
> +		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
> +		.scan_index = -1, \
> +	}
> +
> +static const struct iio_chan_spec mcp2221_channels[] = {
> +	MCP2221_ADC_CHANNEL(0),
> +	MCP2221_ADC_CHANNEL(1),
> +	MCP2221_ADC_CHANNEL(2),
> +};
> +
> +static int mcp2221_register_adcs(struct device *dev, struct mcp2221 *mcp)
> +{
> +	struct mcp2221_iio *iio;
> +
> +	mcp->indio_dev = devm_iio_device_alloc(dev, sizeof(*iio));
> +	if (!mcp->indio_dev)
> +		return -ENOMEM;
> +
> +	iio = iio_priv(mcp->indio_dev);
> +	iio->mcp = mcp;
> +
> +	mcp->indio_dev->name = "mcp2221_adc";
> +	mcp->indio_dev->modes = INDIO_DIRECT_MODE;
> +	mcp->indio_dev->info = &mcp2221_info;
> +	mcp->indio_dev->channels = mcp2221_channels;
> +	mcp->indio_dev->num_channels = ARRAY_SIZE(mcp2221_channels);
> +
> +	return iio_device_register(mcp->indio_dev);
> +}
> +
> +#else
> +
> +static int mcp2221_register_adcs(struct device *dev, struct mcp2221 *mcp)
> +{
> +	return 0;
> +}
> +
> +#endif
> +
>  static int mcp2221_probe(struct hid_device *hdev,
>  					const struct hid_device_id *id)
>  {
> @@ -869,6 +950,10 @@ static int mcp2221_probe(struct hid_device *hdev,
>  	if (ret)
>  		goto err_gc;
>  
> +	ret = mcp2221_register_adcs(&hdev->dev, mcp);
> +	if (ret)
> +		goto err_gc;
> +
>  	return 0;
>  
>  err_gc:
> @@ -884,6 +969,10 @@ static void mcp2221_remove(struct hid_device *hdev)
>  {
>  	struct mcp2221 *mcp = hid_get_drvdata(hdev);
>  
> +#if IS_REACHABLE(CONFIG_IIO)
> +	iio_device_unregister(mcp->indio_dev);
> +#endif
> +
>  	i2c_del_adapter(&mcp->adapter);
>  	hid_hw_close(mcp->hdev);
>  	hid_hw_stop(mcp->hdev);


      reply	other threads:[~2020-11-21 15:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-21  7:28 [PATCH v2] HID: mcp2221: add ADC support Matt Ranostay
2020-11-21 15:13 ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201121151358.5b82798a@archlinux \
    --to=jic23@kernel.org \
    --cc=gupt21@gmail.com \
    --cc=lars@metafoo.de \
    --cc=lee.jones@linaro.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=matt.ranostay@konsulko.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).