Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5017342pxv; Tue, 20 Jul 2021 17:13:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5fpZtB5UqL+eFQisIgPpqPCgLH4/m36+TyrbfIMjNDhTW8xHRwKHOE+IcpFMDtkYEQAEL X-Received: by 2002:a6b:ba02:: with SMTP id k2mr20454606iof.164.1626826392351; Tue, 20 Jul 2021 17:13:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626826392; cv=none; d=google.com; s=arc-20160816; b=q5kGI4WQ3W6pa2Cn17BPabKqdnwFNQj+FLy1iFHfSzB33+yTn/iB6qMQg5ap4X993e QAayuLb2J3bAAnZqeTC0Mhpz3nh9Aj5alGcs8lHjbZLXbZCB3tY4OmWOyHIeMIBgePc9 297Oh44zEFYx56Zc7YQKoNQ4CmaIu8KbUPilULRFN+TswNupyES59mkx3YOuf27tDWFe /mkPpGmFN8In49OYkfHGPfpxOM8J4vWKiQpbXXuVhH7qD6eDFV/AFRSlvy6WxBP6PypT ufdStzq9PRr06Y6IvpBqUYtrI5Aw4+MdmKluylQb5ML+HXC6Jq0l+GEwpJexfQ3gXTp0 aLsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=yft8d0QKQYEK6jK1cRdXCSL3/Fc2IYBV0nFOZMJwK8M=; b=GR5XPyWJv/9xLrH74QPcTTYQaMrLCWubxANL0V3eKY36xqZKeLP1pNzppz6ph30e+r kzhQTg6VMrd7pThT5hjbqIQXud5ct5d//UnepPCkCiGI588sA0ENOW4KFnTVSVLz6Cha JMSJIZ6/1U2RhlQHOqsNHVlojcNbgVOfB9K8sNofXMAZ5VY78+E3ocAVydLKHecAk0cQ 1YRpnWbkTtcG2jtBI1V4HXbHfWLmOq0CvgM2UUtsjvRmppiTs7WaimBhpszUQipBFa7C +x2xry3oCx01nmF+1bdGzcpCCRcd37Y/iCAVcg/7J8govSSLyhHIg6o6XB7Sd304XWCU E/AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@usp.br header.s=usp-google header.b=mth2Xcw7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=usp.br Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b9si10204579jal.24.2021.07.20.17.12.59; Tue, 20 Jul 2021 17:13:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@usp.br header.s=usp-google header.b=mth2Xcw7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=usp.br Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231371AbhGTXb2 (ORCPT + 99 others); Tue, 20 Jul 2021 19:31:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230340AbhGTXbV (ORCPT ); Tue, 20 Jul 2021 19:31:21 -0400 Received: from mail-il1-x12f.google.com (mail-il1-x12f.google.com [IPv6:2607:f8b0:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40298C061574 for ; Tue, 20 Jul 2021 17:11:58 -0700 (PDT) Received: by mail-il1-x12f.google.com with SMTP id b6so815102iln.12 for ; Tue, 20 Jul 2021 17:11:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=yft8d0QKQYEK6jK1cRdXCSL3/Fc2IYBV0nFOZMJwK8M=; b=mth2Xcw77G1fhqx5Z6yTnexgH++YcdpJkPxVeLiB2HaUCwihpVQJL+0e6ziFZcp7Tw xmxbsdUrG9lXyAVamXp7i8cZEKGjKX7lN7k1Jx1k4J665LG1ubyclJC0mYyjEBvXU9Og xqKC7bfhgl3yn3BRVZI8Mtj3crBqgU8g9pQnkIfCjiX6cWm3ZX4pR1vtF/P+t7EJw52R Xw+14ovmIQyxfeLan2f5M5FB1JvoGt75i31qbLDbKz+KVCwjkMvWplYaeI/OpUAO8mvV f1P50ps+izwc7xOgdbitpz5AyoqLAWaxFfpoWB+5n/GoGtrWXrEOCR6Vob/X4ddRqrX1 ZKCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=yft8d0QKQYEK6jK1cRdXCSL3/Fc2IYBV0nFOZMJwK8M=; b=oTDwAdKT6/JT6EKlNDt2rcZ5Ah/G9NLpWeNpBbdxo8hhl+IZGmRKpe+IzyAC/vX4RG hiE8kCWhNiFZZinzkAootu4vuMibuohTZyXEfvkoxCcOoQ0bfV0IE7vSnoqQHA/mSKGj qROaf2lJlucfjPJ6W5qmsLC4F54RHoOkLRekpxCqVYY573PqwleJ/GDOYYYQBwLia07A qO7ww0ePj4Te4dNcwwlD+6i+gAhTVJDWBPPhBODTb8l5v6IreYVMxyYAOW9D3qSjN7+3 SXrT+z2iru8ISCERoYGLNYm3BhgMtYGb2EyboiJEC1KHcjq9dCTT8kQ4bwmvhzsZ5rS1 /iOw== X-Gm-Message-State: AOAM530UY5JlH3fQoGxULVC6epUBJY7JE9WaZTKgEw637oLj40eOpXp6 oyQ9OxEOPHRZ0nRZHqE3QMK0JXClPyuzkIB4AAa3/A== X-Received: by 2002:a05:6e02:d41:: with SMTP id h1mr21391622ilj.191.1626826317665; Tue, 20 Jul 2021 17:11:57 -0700 (PDT) MIME-Version: 1.0 References: <20210718203746.7159-1-theobf@usp.br> In-Reply-To: From: =?UTF-8?Q?Th=C3=A9o_Bor=C3=A9m_Fabris?= Date: Tue, 20 Jul 2021 21:11:20 -0300 Message-ID: Subject: Re: [PATCH] iio: dac: max5821: convert device register to device managed function To: Alexandru Ardelean Cc: Jonathan Cameron , Lars-Peter Clausen , linux-iio , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em ter., 20 de jul. de 2021 =C3=A0s 04:22, Alexandru Ardelean escreveu: > > On Mon, Jul 19, 2021 at 6:25 PM Th=C3=A9o Bor=C3=A9m Fabris wrote: > > > > Hi, Alexandru. > > > > Em seg., 19 de jul. de 2021 =C3=A0s 04:33, Alexandru Ardelean > > escreveu: > > > > > > On Sun, Jul 18, 2021 at 11:42 PM Th=C3=A9o Bor=C3=A9m Fabris wrote: > > > > > > > > Add a device managed hook, via devm_add_action_or_reset() and > > > > max5821_regulator_disable(), to disable voltage regulator on device > > > > detach. > > > > Replace iio_device_register() by devm_iio_device_register() and rem= ove > > > > the max5821_remove() function used to unregister the device and dis= able the > > > > voltage regulator. > > > > Remove i2c_set_clientdata() from the probe function, since > > > > i2c_get_clientdata() is not used anymore. > > > > > > Looks good overall. > > > A few comments inline. > > > > > > > > > > > Signed-off-by: Th=C3=A9o Bor=C3=A9m Fabris > > > > --- > > > > drivers/iio/dac/max5821.c | 30 ++++++++++++++++-------------- > > > > 1 file changed, 16 insertions(+), 14 deletions(-) > > > > > > > > diff --git a/drivers/iio/dac/max5821.c b/drivers/iio/dac/max5821.c > > > > index bd6e75699a63..44c04ae70b32 100644 > > > > --- a/drivers/iio/dac/max5821.c > > > > +++ b/drivers/iio/dac/max5821.c > > > > @@ -294,6 +294,13 @@ static const struct iio_info max5821_info =3D = { > > > > .write_raw =3D max5821_write_raw, > > > > }; > > > > > > > > +static void max5821_regulator_disable(void *data) > > > > +{ > > > > + struct regulator *rdata =3D data; > > > > + > > > > + regulator_disable(rdata); > > > > > > This can be simplified a bit: > > > > > > static void max5821_regulator_disable(void *reg) > > > { > > > regulator_disable(reg); > > > } > > > > > > I used to do explicit casting, but then I also figured that it's not = necessary. > > > > > Ok. > > > > > > +} > > > > + > > > > static int max5821_probe(struct i2c_client *client, > > > > const struct i2c_device_id *id) > > > > { > > > > @@ -306,7 +313,6 @@ static int max5821_probe(struct i2c_client *cli= ent, > > > > if (!indio_dev) > > > > return -ENOMEM; > > > > data =3D iio_priv(indio_dev); > > > > - i2c_set_clientdata(client, indio_dev); > > > > data->client =3D client; > > > > mutex_init(&data->lock); > > > > > > > > @@ -331,6 +337,14 @@ static int max5821_probe(struct i2c_client *cl= ient, > > > > goto error_free_reg; > > > > } > > > > > > > > + ret =3D devm_add_action_or_reset(&client->dev, max5821_regu= lator_disable, > > > > + data->vref_reg); > > > > + if (ret) { > > > > + dev_err(&client->dev, > > > > + "Failed to add action to managed regulator:= %d\n", ret); > > > > + goto error_disable_reg; > > > > > > return ret; > > > > > > devm_add_action_or_reset() should call max5821_regulator_disable() in > > > case of error > > > > > Ok. > > > > > > + } > > > > + > > > > ret =3D regulator_get_voltage(data->vref_reg); > > > > if (ret < 0) { > > > > dev_err(&client->dev, > > > > @@ -346,7 +360,7 @@ static int max5821_probe(struct i2c_client *cli= ent, > > > > indio_dev->modes =3D INDIO_DIRECT_MODE; > > > > indio_dev->info =3D &max5821_info; > > > > > > > > - return iio_device_register(indio_dev); > > > > + return devm_iio_device_register(&client->dev, indio_dev); > > > > > > > > error_disable_reg: > > > > > > This entire goto block should be removed. > > > The idea of using only devm_ functions is to not have these goto stat= ements. > > > > > I thought the action added via devm_add_action (and devres_add) was cal= led only > > on driver detach, thus the error_disable_reg label would be necessary > > to handle the > > devm_add_action() yes > this is devm_add_action_or_reset() which looks like this: > > static inline int devm_add_action_or_reset(struct device *dev, > void (*action)(void *), void *data) > { > int ret; > > ret =3D devm_add_action(dev, action, data); > if (ret) > action(data); > > return ret; > } > > it can be found in "include/linux/device.h" > > > possible error on regulator_get_voltage. Could you please clarify for > > me when does > > a driver detach happen? > > a driver detach happens when: > * the kmod is unloaded (assuming the driver is running as a kmod) > * manually unbinding the driver from sysfs ; > a quick article about this [it's for USB, but other interfaces use > the same mechanism]: > https://lwn.net/Articles/143397/ > there should be something under /sys/bus/spi/drivers/xxxx/unbind > * when the system powers down (reboots) > * maybe there is some other new method to do this [that I don't know abou= t] > Thank you so much. So, should this goto error_disable_reg be replaced by "regulator_disable(.); return ret;"? ret =3D regulator_get_voltage(data->vref_reg); if (ret < 0) { dev_err(&client->dev, "Failed to get voltage on regulator: %d\n", ret); goto error_disable_reg; } > > > > Thanks for your reply, > > Th=C3=A9o > > > > > > regulator_disable(data->vref_reg); > > > > @@ -356,17 +370,6 @@ static int max5821_probe(struct i2c_client *cl= ient, > > > > return ret; > > > > } > > > > > > > > -static int max5821_remove(struct i2c_client *client) > > > > -{ > > > > - struct iio_dev *indio_dev =3D i2c_get_clientdata(client); > > > > - struct max5821_data *data =3D iio_priv(indio_dev); > > > > - > > > > - iio_device_unregister(indio_dev); > > > > - regulator_disable(data->vref_reg); > > > > - > > > > - return 0; > > > > -} > > > > - > > > > static const struct i2c_device_id max5821_id[] =3D { > > > > { "max5821", ID_MAX5821 }, > > > > { } > > > > @@ -386,7 +389,6 @@ static struct i2c_driver max5821_driver =3D { > > > > .pm =3D &max5821_pm_ops, > > > > }, > > > > .probe =3D max5821_probe, > > > > - .remove =3D max5821_remove, > > > > .id_table =3D max5821_id, > > > > }; > > > > module_i2c_driver(max5821_driver); > > > > -- > > > > 2.20.1 > > > >