From patchwork Wed Jun 30 13:16:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 12351961 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52201C11F65 for ; Wed, 30 Jun 2021 13:17:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E2BF61455 for ; Wed, 30 Jun 2021 13:17:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234853AbhF3NUD (ORCPT ); Wed, 30 Jun 2021 09:20:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234806AbhF3NUB (ORCPT ); Wed, 30 Jun 2021 09:20:01 -0400 Received: from mail-ej1-x62d.google.com (mail-ej1-x62d.google.com [IPv6:2a00:1450:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 392ABC061766 for ; Wed, 30 Jun 2021 06:17:31 -0700 (PDT) Received: by mail-ej1-x62d.google.com with SMTP id nd37so4187168ejc.3 for ; Wed, 30 Jun 2021 06:17:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=deviqon.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=xiTb0xD8le1nLQtDW6IFqKE9RONZcsoN+E9Y8GN3Uak=; b=ouYycHezo0GkOlzma46gP0wox4xgLOqMBUPFAjGWmz6jR9FV03kFcQUzmutNsgVU2I eCjzXZRIoVxF/4NXBwkAIfphL00xvSG7pvwNC1bB1Jvn8L/NPAcLXdsrpQC84mPsDScc nYQqsu/oNUwv9ocJhjUXs7SuULNIz+BLL1SP6WpqStQew1sPU5mZ7+l6Zopgau0+9Zh2 AMo6jhK9ee0cbm8O8pz1fNJyQscjFUIO+UAzCvjLB1VgshsGKWE4gez98Xz/yfTSBZgl vLhMY11GK9yxv3bz8vIfd9P1A8zU3gm/aPR0PbTEwFgPZYxgeA00KvG6yEMQBnrR8zxh Ba9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=xiTb0xD8le1nLQtDW6IFqKE9RONZcsoN+E9Y8GN3Uak=; b=kmD4UOKojLPmb9TlwsmFHqvE7SEBaqptqzgns3I9rZkmmlRb9YBav9wUpGXHQj3O5V mKR0gJf5feCVsRhHIzxq0cx772W28SapT5egnDqVmDJj8OGAUSbXTr1WCuUpDHKvyjeb lfuBFiNntFgcV9yf9w4NPNbLF8wQuw9vrZDHy6WrsdDHskWotsiSWZItdtsqZw6yKZZf xb5u6RWQLKecoGpo58pbwWR4tnzT9BZzttym+Fi8hVl3epi14BK2Ko06QJrJVSXaNCRp htWgVHEgv9NIqEzrVxNBW4t0IRgYqBm1Y2RXAUra9q1ssC/5NCoRSPIqGQ9WgbLNvONq FLSw== X-Gm-Message-State: AOAM532To9SrF89VCEyGLiQokfRQcpmh638cmux3jSnzpOzA5mBhWNpZ giRnXRI7xWlGXNiJVonQ49NfXCQWfqIkrXQD X-Google-Smtp-Source: ABdhPJwqLnHfx4Dc0iEwF0a1zdqrchPCQxBKN1WprVuk/qHh4jQ4fFBfzqlr7q8Vw/RS6vWt4RllQQ== X-Received: by 2002:a17:906:cb81:: with SMTP id mf1mr36138317ejb.199.1625059049450; Wed, 30 Jun 2021 06:17:29 -0700 (PDT) Received: from neptune.. ([5.2.193.191]) by smtp.gmail.com with ESMTPSA id md14sm39511ejb.125.2021.06.30.06.17.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 06:17:29 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: jic23@kernel.org, Alexandru Ardelean Subject: [PATCH 1/2] iio: proximity: rfd77402: use i2c_client for rfd77402_{init,powerdown}() Date: Wed, 30 Jun 2021 16:16:35 +0300 Message-Id: <20210630131636.1563148-1-aardelean@deviqon.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org These 2 functions only do I2C reads/writes and don't require any of the private data of the driver. They're also used in the PM suspend/resume part of the driver. Converting them to take an i2c_client object as parameter simplifies things a bit (especially in the suspend/resume) as the driver mostly needs the reference for i2c_client, so no need to jump through hoops to get it from the private data (as was done in many places). The rfd77402_measure() function has also been converted to take an i2c_client object, since it also does only I2C ops. Signed-off-by: Alexandru Ardelean --- drivers/iio/proximity/rfd77402.c | 60 ++++++++++++++------------------ 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/drivers/iio/proximity/rfd77402.c b/drivers/iio/proximity/rfd77402.c index 7a0472323f17..f349526421f3 100644 --- a/drivers/iio/proximity/rfd77402.c +++ b/drivers/iio/proximity/rfd77402.c @@ -90,18 +90,18 @@ static const struct iio_chan_spec rfd77402_channels[] = { }, }; -static int rfd77402_set_state(struct rfd77402_data *data, u8 state, u16 check) +static int rfd77402_set_state(struct i2c_client *client, u8 state, u16 check) { int ret; - ret = i2c_smbus_write_byte_data(data->client, RFD77402_CMD_R, + ret = i2c_smbus_write_byte_data(client, RFD77402_CMD_R, state | RFD77402_CMD_VALID); if (ret < 0) return ret; usleep_range(10000, 20000); - ret = i2c_smbus_read_word_data(data->client, RFD77402_STATUS_R); + ret = i2c_smbus_read_word_data(client, RFD77402_STATUS_R); if (ret < 0) return ret; if ((ret & RFD77402_STATUS_PM_MASK) != check) @@ -110,24 +110,24 @@ static int rfd77402_set_state(struct rfd77402_data *data, u8 state, u16 check) return 0; } -static int rfd77402_measure(struct rfd77402_data *data) +static int rfd77402_measure(struct i2c_client *client) { int ret; int tries = 10; - ret = rfd77402_set_state(data, RFD77402_CMD_MCPU_ON, + ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON, RFD77402_STATUS_MCPU_ON); if (ret < 0) return ret; - ret = i2c_smbus_write_byte_data(data->client, RFD77402_CMD_R, + ret = i2c_smbus_write_byte_data(client, RFD77402_CMD_R, RFD77402_CMD_SINGLE | RFD77402_CMD_VALID); if (ret < 0) goto err; while (tries-- > 0) { - ret = i2c_smbus_read_byte_data(data->client, RFD77402_ICSR); + ret = i2c_smbus_read_byte_data(client, RFD77402_ICSR); if (ret < 0) goto err; if (ret & RFD77402_ICSR_RESULT) @@ -140,7 +140,7 @@ static int rfd77402_measure(struct rfd77402_data *data) goto err; } - ret = i2c_smbus_read_word_data(data->client, RFD77402_RESULT_R); + ret = i2c_smbus_read_word_data(client, RFD77402_RESULT_R); if (ret < 0) goto err; @@ -153,7 +153,7 @@ static int rfd77402_measure(struct rfd77402_data *data) return (ret & RFD77402_RESULT_DIST_MASK) >> 2; err: - rfd77402_set_state(data, RFD77402_CMD_MCPU_OFF, + rfd77402_set_state(client, RFD77402_CMD_MCPU_OFF, RFD77402_STATUS_MCPU_OFF); return ret; } @@ -168,7 +168,7 @@ static int rfd77402_read_raw(struct iio_dev *indio_dev, switch (mask) { case IIO_CHAN_INFO_RAW: mutex_lock(&data->lock); - ret = rfd77402_measure(data); + ret = rfd77402_measure(data->client); mutex_unlock(&data->lock); if (ret < 0) return ret; @@ -188,23 +188,23 @@ static const struct iio_info rfd77402_info = { .read_raw = rfd77402_read_raw, }; -static int rfd77402_init(struct rfd77402_data *data) +static int rfd77402_init(struct i2c_client *client) { int ret, i; - ret = rfd77402_set_state(data, RFD77402_CMD_STANDBY, + ret = rfd77402_set_state(client, RFD77402_CMD_STANDBY, RFD77402_STATUS_STANDBY); if (ret < 0) return ret; /* configure INT pad as push-pull, active low */ - ret = i2c_smbus_write_byte_data(data->client, RFD77402_ICSR, + ret = i2c_smbus_write_byte_data(client, RFD77402_ICSR, RFD77402_ICSR_INT_MODE); if (ret < 0) return ret; /* I2C configuration */ - ret = i2c_smbus_write_word_data(data->client, RFD77402_I2C_INIT_CFG, + ret = i2c_smbus_write_word_data(client, RFD77402_I2C_INIT_CFG, RFD77402_I2C_ADDR_INCR | RFD77402_I2C_DATA_INCR | RFD77402_I2C_HOST_DEBUG | @@ -213,42 +213,42 @@ static int rfd77402_init(struct rfd77402_data *data) return ret; /* set initialization */ - ret = i2c_smbus_write_word_data(data->client, RFD77402_PMU_CFG, 0x0500); + ret = i2c_smbus_write_word_data(client, RFD77402_PMU_CFG, 0x0500); if (ret < 0) return ret; - ret = rfd77402_set_state(data, RFD77402_CMD_MCPU_OFF, + ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_OFF, RFD77402_STATUS_MCPU_OFF); if (ret < 0) return ret; /* set initialization */ - ret = i2c_smbus_write_word_data(data->client, RFD77402_PMU_CFG, 0x0600); + ret = i2c_smbus_write_word_data(client, RFD77402_PMU_CFG, 0x0600); if (ret < 0) return ret; - ret = rfd77402_set_state(data, RFD77402_CMD_MCPU_ON, + ret = rfd77402_set_state(client, RFD77402_CMD_MCPU_ON, RFD77402_STATUS_MCPU_ON); if (ret < 0) return ret; for (i = 0; i < ARRAY_SIZE(rf77402_tof_config); i++) { - ret = i2c_smbus_write_word_data(data->client, + ret = i2c_smbus_write_word_data(client, rf77402_tof_config[i].reg, rf77402_tof_config[i].val); if (ret < 0) return ret; } - ret = rfd77402_set_state(data, RFD77402_CMD_STANDBY, + ret = rfd77402_set_state(client, RFD77402_CMD_STANDBY, RFD77402_STATUS_STANDBY); return ret; } -static int rfd77402_powerdown(struct rfd77402_data *data) +static int rfd77402_powerdown(struct i2c_client *client) { - return rfd77402_set_state(data, RFD77402_CMD_STANDBY, + return rfd77402_set_state(client, RFD77402_CMD_STANDBY, RFD77402_STATUS_STANDBY); } @@ -280,7 +280,7 @@ static int rfd77402_probe(struct i2c_client *client, indio_dev->name = RFD77402_DRV_NAME; indio_dev->modes = INDIO_DIRECT_MODE; - ret = rfd77402_init(data); + ret = rfd77402_init(client); if (ret < 0) return ret; @@ -291,7 +291,7 @@ static int rfd77402_probe(struct i2c_client *client, return 0; err_powerdown: - rfd77402_powerdown(data); + rfd77402_powerdown(client); return ret; } @@ -300,7 +300,7 @@ static int rfd77402_remove(struct i2c_client *client) struct iio_dev *indio_dev = i2c_get_clientdata(client); iio_device_unregister(indio_dev); - rfd77402_powerdown(iio_priv(indio_dev)); + rfd77402_powerdown(client); return 0; } @@ -308,18 +308,12 @@ static int rfd77402_remove(struct i2c_client *client) #ifdef CONFIG_PM_SLEEP static int rfd77402_suspend(struct device *dev) { - struct rfd77402_data *data = iio_priv(i2c_get_clientdata( - to_i2c_client(dev))); - - return rfd77402_powerdown(data); + return rfd77402_powerdown(to_i2c_client(dev)); } static int rfd77402_resume(struct device *dev) { - struct rfd77402_data *data = iio_priv(i2c_get_clientdata( - to_i2c_client(dev))); - - return rfd77402_init(data); + return rfd77402_init(to_i2c_client(dev)); } #endif From patchwork Wed Jun 30 13:16:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 12351963 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5DA0C11F68 for ; Wed, 30 Jun 2021 13:17:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AFAFF61455 for ; Wed, 30 Jun 2021 13:17:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234806AbhF3NUE (ORCPT ); Wed, 30 Jun 2021 09:20:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234841AbhF3NUC (ORCPT ); Wed, 30 Jun 2021 09:20:02 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24C42C061768 for ; Wed, 30 Jun 2021 06:17:32 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id o5so4162882ejy.7 for ; Wed, 30 Jun 2021 06:17:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=deviqon.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XH4RKf95uj0XGgjr7ofqfjtHRyfEBIs1/NXlXnVrwYI=; b=ax2et6Fq3SGMp+nmQS31Tgr4Z7CKWxguDv2PSQ03cdoudNQkw3+6hYf4ABbioe94/5 ykmaOBtoqURlgq3vRWFLbjr9r+YYCfuCG7wKxmQI7+1PV/6cFJW9OYDnobz4fUj0IqYF gURDBI970v7SoJPEXTAeVpm7aXwVZIY1Wp+cNetyzch9aBb5qfr4N5NAN6zPK4TlqpyM AdN0Mbsg2P0ex+Mbt3zTdj6PLGqlxZyBz6Dqqhm8S8+1eROIQ1aaA4B2xEhGRotz/vBb z14RF+79DIPx4uTAZ/l9ZQpFQI52HVxxqNQTazelQhbj9lpGRzLE8Qj2pfIPCHN4gqnu lFkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XH4RKf95uj0XGgjr7ofqfjtHRyfEBIs1/NXlXnVrwYI=; b=Zjw3FCCPSyUJPynMY/ppIGcnv/cBKqSF107YIrNKHs1IDfvjb2ZIQxtDrAmu8x1obP SQksPkWeuZ8x5vClf5aWFtfPws8PPJdheG33EjfOAU8kTeAkDZjKycp6w6Z+X3PCEvcS wMK8xkzmKlQ8Nwvd3N8fqEU11PxNgJHjmfmO/5k7I6VJ2FT4zI8DmEOzZX47cZVjAfAG c+uDZUNSmQ1l3Z5VSCxiVpwNJic1Kw2RA/gbrunO3rboe0m6STgGckYDXkZeMvSC2K+z 6Hf6+W21MWrPtglhFSxvYKBwq3YLtz2lsTJpPCumh3srJ1pDKxAcC3snGumM5pMWdt3w CdvA== X-Gm-Message-State: AOAM533l3KqIWufl4q1hqGwp4NcbuMg03rwJsRZyKMIlWZidJgTa/WR9 EynArroihNt5ikMB1v1I5hnY6xy6WSWUd+Ju X-Google-Smtp-Source: ABdhPJyGQ0J8ACUJhNNJ8MLetWxk0lXprsnPX+MhLU9yn8pn9wm9bA4xIV972M6LjqIPKrMrWHgPdw== X-Received: by 2002:a17:907:2da6:: with SMTP id gt38mr34186045ejc.528.1625059050398; Wed, 30 Jun 2021 06:17:30 -0700 (PDT) Received: from neptune.. ([5.2.193.191]) by smtp.gmail.com with ESMTPSA id md14sm39511ejb.125.2021.06.30.06.17.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Jun 2021 06:17:30 -0700 (PDT) From: Alexandru Ardelean To: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: jic23@kernel.org, Alexandru Ardelean Subject: [PATCH 2/2] iio: proximity: rfd77402: convert probe to device-managed functions Date: Wed, 30 Jun 2021 16:16:36 +0300 Message-Id: <20210630131636.1563148-2-aardelean@deviqon.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210630131636.1563148-1-aardelean@deviqon.com> References: <20210630131636.1563148-1-aardelean@deviqon.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This change converts the probe hook to register the IIO device with devm_iio_device_register() and register a hook with devm_add_action_or_reset() to put the device in powerdown when the driver gets unloaded. Since the PM suspend/resume functions need only a reference to the i2c_client object (which can be obtained from the base device object), the i2c_set_clientdata() call can be removed. Signed-off-by: Alexandru Ardelean --- drivers/iio/proximity/rfd77402.c | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/drivers/iio/proximity/rfd77402.c b/drivers/iio/proximity/rfd77402.c index f349526421f3..8c06d02139b6 100644 --- a/drivers/iio/proximity/rfd77402.c +++ b/drivers/iio/proximity/rfd77402.c @@ -252,6 +252,11 @@ static int rfd77402_powerdown(struct i2c_client *client) RFD77402_STATUS_STANDBY); } +static void rfd77402_disable(void *client) +{ + rfd77402_powerdown(client); +} + static int rfd77402_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -270,7 +275,6 @@ static int rfd77402_probe(struct i2c_client *client, return -ENOMEM; data = iio_priv(indio_dev); - i2c_set_clientdata(client, indio_dev); data->client = client; mutex_init(&data->lock); @@ -284,25 +288,11 @@ static int rfd77402_probe(struct i2c_client *client, if (ret < 0) return ret; - ret = iio_device_register(indio_dev); + ret = devm_add_action_or_reset(&client->dev, rfd77402_disable, client); if (ret) - goto err_powerdown; - - return 0; - -err_powerdown: - rfd77402_powerdown(client); - return ret; -} - -static int rfd77402_remove(struct i2c_client *client) -{ - struct iio_dev *indio_dev = i2c_get_clientdata(client); - - iio_device_unregister(indio_dev); - rfd77402_powerdown(client); + return ret; - return 0; + return devm_iio_device_register(&client->dev, indio_dev); } #ifdef CONFIG_PM_SLEEP @@ -331,7 +321,6 @@ static struct i2c_driver rfd77402_driver = { .pm = &rfd77402_pm_ops, }, .probe = rfd77402_probe, - .remove = rfd77402_remove, .id_table = rfd77402_id, };