Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4402084pxb; Mon, 21 Feb 2022 20:24:28 -0800 (PST) X-Google-Smtp-Source: ABdhPJy73STVzlizeGkfKbto0eWsEqufEw+uLcGcd0/rhEkaB12DQvPfOq5hgDFWQ2YyAe+3qpGh X-Received: by 2002:a05:6a00:22ce:b0:4f0:ed5c:7166 with SMTP id f14-20020a056a0022ce00b004f0ed5c7166mr17555766pfj.79.1645503868205; Mon, 21 Feb 2022 20:24:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645503868; cv=none; d=google.com; s=arc-20160816; b=ULT2LHw/g8Z9GeFkcsqlc0CXcGLMrOPmPfslJW+4nO67O+qT+r6sTuN5BrsoS4Bhle geUfDc5dwQCU2mKOCpUI0mrmtkFnniAePFuyDSWhZOsp4sIyxTJzbldC5llHxxS7Xhrw Hm2L2QuvgLr4YtiM0xqG6iuw5g9waZwjMg526rBVE2DO8mbyHtK3wZUR9vxBMn4rRRdG GCSCWTooYgVcFtjIyt/2tgD7yrVig9Zpw/O93ZTrw/90ZMMMuzs5SfFdvR5RpuF7Ae6S VXA92nxb00WpTBxMcKUOzDfLzH0IQU14XXtv60Bm+IXixn6LEwR6dXq06MiTWzbMd9eR o0lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=q6Ei0SlXMY3U/GoKunxXGFN3d3mmMhFrtGn6z+uSaC0=; b=gdcV7zR96FGrX7ZehEYHVXikWHiHW6omkztn3qS0/jgnMv4yAEwEIpK2n8U1RZzULR 6yY1C5OHiiJWVEVG6KtuAt8phJuql2+sK7Xt09fNl9g3Ob//PskIwvJsGG3V8xjR4zUm p3CTL5n62w0KlZmrlgoXWHKyzjh54yrzWlvN3J4xbKCUFATbT7y1+mYItwdobo8/zjyV QnUr8z2nUCr8ZVEos+rxPV6fU9lr3jAkCM88C0d3MhlHXnflC2yEkUGQypKtyjj0WxmG dcDMgqTwhRCaDWixT3BxRO/IamAA1gg1pOR5d1Br9F0kwKrJ5vaQb7T+5dxMmPNpC0Nl xw9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NoakwNhd; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id j15si37751200plx.415.2022.02.21.20.24.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Feb 2022 20:24:28 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NoakwNhd; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EA56B8EB79; Mon, 21 Feb 2022 20:19:23 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233694AbiBUU3A (ORCPT + 99 others); Mon, 21 Feb 2022 15:29:00 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:49670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229451AbiBUU25 (ORCPT ); Mon, 21 Feb 2022 15:28:57 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A0C122BFF; Mon, 21 Feb 2022 12:28:33 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id p9so36136619ejd.6; Mon, 21 Feb 2022 12:28:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=q6Ei0SlXMY3U/GoKunxXGFN3d3mmMhFrtGn6z+uSaC0=; b=NoakwNhd8fsNBSn2f119ciSVyPnsAJM1JwLA9Zrmt+mf+4bNHRgomzv0sQHHVrEI8f HIZfzeu9yPeL7oZCHeYC0sO74zDMqqOur1h2QUvye1XwcSq0yKT8rlJytPqBNS9I6ZyX Jdu4tnIdkSezU/azVi6oiGgtjyjklFStzDW6GUi9DNRNuxrn6dCitFd1KByOaKo/v5Qv XTK3D2Fp3plPwPXmPieVsq1fhH3bFbCQjrxv+JaM6Fi8DxgWxioklPkRRg8BT6lhbsVB fd42r3Mlui5vEaMwRrk/6ZLkIbSSjX4lfvx3zbVBjE6hol134NMAzHwECg1x5+4I857a LDbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=q6Ei0SlXMY3U/GoKunxXGFN3d3mmMhFrtGn6z+uSaC0=; b=T6mCe2gvZrPO18eG8jAYkl6q3FnB0a5kl1vVO3thRgtYPT7+u+Rl1/M+Fwb2MZVm1G yfXdabM36JBtH7CqgZ7X84dhMkU3cDy11vlJz4qJ5AL7X/dqh38xmVVQMNtimUlRES4v U/WVluIFxzR2LjJ1EStBnyFfO1CxB2OpUWh2k+Kb0uySyrBL1SA7UMxN+x0MFbOVRy2n g8saCYhtUMAb4nNHbjSRx7DV/f93c/V4oIA4N3LNTvAXjfAPN8ZO+uxqBXr7YPMf0o/3 lXv4Mx4p1Z4VDkJ8kru3ZIhqU+1CHa9ZaYxHib0cpTOStU+PlcHtF1YkI1ROyogXr/Yu 9S1g== X-Gm-Message-State: AOAM531eqnDjzUGTSoy4w+mC9y0DPPsIbC5uxl0DndTTLFHsNY1vJg2h PmaRl3DgGqvTGAq8g39uHr1Cp1K8/ucRiCNrCKWXMzKX+v60PA== X-Received: by 2002:a17:906:cc12:b0:6b5:ec8f:fdf2 with SMTP id ml18-20020a170906cc1200b006b5ec8ffdf2mr16711397ejb.579.1645475311926; Mon, 21 Feb 2022 12:28:31 -0800 (PST) MIME-Version: 1.0 References: <20220217162710.33615-1-andrea.merello@gmail.com> <20220217162710.33615-12-andrea.merello@gmail.com> In-Reply-To: <20220217162710.33615-12-andrea.merello@gmail.com> From: Andy Shevchenko Date: Mon, 21 Feb 2022 21:27:55 +0100 Message-ID: Subject: Re: [v3 11/13] iio: imu: add BNO055 serdev driver To: Andrea Merello Cc: jic23@kernel.org, mchehab+huawei@kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, lars@metafoo.de, robh+dt@kernel.org, matt.ranostay@konsulko.com, ardeleanalex@gmail.com, jacopo@jmondi.org, Andrea Merello Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 17, 2022 at 5:27 PM Andrea Merello wrote: > > This path adds a serdev driver for communicating to a BNO055 IMU via > serial bus, and it enables the BNO055 core driver to work in this > scenario. > drivers/iio/imu/bno055/bno055_sl.c | 557 +++++++++++++++++++++++++++++ Can we use the suffix _ser instead of _sl? ... > +config BOSCH_BNO055_SERIAL > + tristate "Bosch BNO055 attached via serial bus" Serial is too broad, it can cover a lot of buses, can we be more specific? ... > + help > + Enable this to support Bosch BNO055 IMUs attached via serial bus. Ditto. ... > +struct bno055_sl_priv { > + struct serdev_device *serdev; > + struct completion cmd_complete; > + enum { > + CMD_NONE, > + CMD_READ, > + CMD_WRITE, > + } expect_response; > + int expected_data_len; > + u8 *response_buf; > + > + /** > + * enum cmd_status - represent the status of a command sent to the HW. > + * @STATUS_OK: The command executed successfully. > + * @STATUS_FAIL: The command failed: HW responded with an error. > + * @STATUS_CRIT: The command failed: the serial communication failed. > + */ > + enum { > + STATUS_OK = 0, > + STATUS_FAIL = 1, > + STATUS_CRIT = -1 + Comma and sort them by value? For the second part is an additional question, why negative? > + } cmd_status; > + struct mutex lock; > + > + /* Only accessed in RX callback context. No lock needed. */ > + struct { > + enum { > + RX_IDLE, > + RX_START, > + RX_DATA + Comma. > + } state; > + int databuf_count; > + int expected_len; > + int type; > + } rx; > + > + /* Never accessed in behalf of RX callback context. No lock needed */ > + bool cmd_stale; > +}; ... > + dev_dbg(&priv->serdev->dev, "send (len: %d): %*ph", len, len, data); Not a fan of this and similar. Can't you introduce a trace events for this driver? ... > + ret = serdev_device_write(priv->serdev, data, len, > + msecs_to_jiffies(25)); One line? ... > + int i = 0; > + while (1) { > + ret = bno055_sl_send_chunk(priv, hdr + i * 2, 2); > + if (ret) > + goto fail; > + > + if (i++ == 1) > + break; > + usleep_range(2000, 3000); > + } The infinite loops are hard to read and understand. Can you convert it to the regular while or for one? Also, this looks like a repetition of something (however it seems that it's two sequencial packets to send). ... > + const int retry_max = 5; > + int retry = retry_max; > + while (retry--) { Instead simply use unsigned int retries = 5; do { ... } while (--retries); which is much better to understand. ... > + if (retry != (retry_max - 1)) > + dev_dbg(&priv->serdev->dev, "cmd retry: %d", > + retry_max - retry); This is an invariant to the loop. > + ret = bno055_sl_do_send_cmd(priv, read, addr, len, data); > + if (ret) > + continue; ... > + if (ret == -ERESTARTSYS) { > + priv->cmd_stale = true; > + return -ERESTARTSYS; > + } else if (!ret) { Redundant 'else' > + return -ETIMEDOUT; > + } Also {} can be dropped. ... > + if (priv->cmd_status == STATUS_OK) > + return 0; > + else if (priv->cmd_status == STATUS_CRIT) Redundant 'else' > + return -EIO; ... > +static int bno055_sl_write_reg(void *context, const void *_data, size_t count) > +{ > + u8 *data = (u8 *)_data; Why casting? ... > + if (val_size > 128) { > + dev_err(&priv->serdev->dev, "Invalid read valsize %d", > + val_size); One line? > + return -EINVAL; > + } ... > + reg_addr = ((u8 *)reg)[0]; This looks ugly. Can't you supply the data struct pointer instead of void pointer? ... > + if (serdev_device_set_baudrate(serdev, 115200) != 115200) { Is it limitation / requirement by the hardware? Otherwise it should come from DT / ACPI. ... > + ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE); Ditto. ... > + regmap = devm_regmap_init(&serdev->dev, &bno055_sl_regmap_bus, > + priv, &bno055_regmap_config); > + if (IS_ERR(regmap)) { > + dev_err(&serdev->dev, "Unable to init register map"); > + return PTR_ERR(regmap); > + } return dev_err_probe(); -- With Best Regards, Andy Shevchenko