Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp141241lqr; Wed, 5 Jun 2024 01:13:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCURTsiF7Ocz1JWHl2VOlfmJB9qwaBn1oAYpi1JKs0OwR5141nGlSzyiV+TT0cPbQQEW9TvmIrRPMhKhGsNKF5HSBPjgzGjdqvSQFE84Kw== X-Google-Smtp-Source: AGHT+IFEOF/9coJ7f+6f8y/exP+GUaNpw7vFn9ieIxzq3oKwrzIkBCIgxKNHS94Q5yW4sJhc7CWK X-Received: by 2002:a05:6a21:318b:b0:1b2:91a0:6255 with SMTP id adf61e73a8af0-1b2b71b5cdbmr2545572637.62.1717575218794; Wed, 05 Jun 2024 01:13:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717575218; cv=pass; d=google.com; s=arc-20160816; b=BfmnKtV9f46xjkUhQtulFQsEYbzDM5w+l7gdkprPQvITyjznSWLjJRVOlx46mUC8mq jzwfKHXGHe8OaZMP9P2TbKtUvFwx7Q0EnZ6iiSFjTYFAhpDDRFZFL+SvLB9DFByFy5ej 0eL/drZxhRd2KOW04tcCAZa3j43kBC1JUuLYUQaqpk6pFXLJ+WIikuSWscF59cePfxT+ RmzA1JcoNSs7Rm9bFw3YepMsRjVwoOgX3Hw4yee/tSKKcht94HB72N70pUrpQptrvweo QSK1hb13xpPzu6Q3a+0bzRTS1PZag/IbjDSY1VhbA86FqcIFt2oMEEXZW9e4rqF68zk5 1O2g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=d938GCY+0VmkKPW+ZUE+VmgbyLC1bhkjlDIYYNMduOE=; fh=jFkK2+z0TAbTLUyAuzR6nBvh95eLga0OowRGZO2ut2Y=; b=Pignclro9AkxjG3hVVqn/eWezR3Wm7c6TpuZpFED35Ltppz9LVVtkfFrUw6Nx3t6kb LClSZf/USp2xK9lgzXRN9xnClpBGTMa9fUUMJipqAWh7X6LP/LEHm5gJ8NQjUsEqi4Eb Bb7CTcHNhvPQHvzgTomGM7XGcSOcx0gb/0U3IVBKv9OJB2XfHqerw77eTl3XfvUMqz31 C4BINmQJxAFL5n0MD2gMMFJHBmE8phY1tv2Hf81TGRJs7L4dwj8qeW0sUbaMeMrSXgmW NNC6m3BHTgHwhM514xNBXqyjvMAmwDUQX0V0yHWRGwPngAYBVIJ0TCk9vUKUchPoIBjY Klbg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fArANUOv; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-201910-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201910-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-70286727effsi2339961b3a.122.2024.06.05.01.13.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 01:13:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-201910-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=fArANUOv; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-201910-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201910-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 1F0CAB2699E for ; Wed, 5 Jun 2024 07:53:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9796118C356; Wed, 5 Jun 2024 07:52:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fArANUOv" Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5E0318F2FB; Wed, 5 Jun 2024 07:52:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573968; cv=none; b=lSfLMHHborcaeKH7u7jBBAFKLSnh8lnqnvdNV6XQJttj0TTOtEsYJEu5RhC8fi3NuIvXjvHCbavTM0O+M/YOel3AElBZopQsrCECjgR4QNrAKxdgeLyI050HCMRugoPr8czHI1aDN2PcbV+KZj1HbOrFzcd4L85OMty+il/s/JA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717573968; c=relaxed/simple; bh=XrfSRrJ7EF0t1bW4E2n/AMkEy5nbGXCiOMHr7XSDs5w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IWs/wZh8l/AGNvISKN516pJCnUZtEUoy08rkE0W9oZQ2EKVyzVHLD1OlWg+gy8Md7EfM4fLBrHuCBTb3ucNIjHDXG2qM40kKn3f3GRaLdiFJdz0acc7jiIMZ8X5VugO102rv5olUWKfizyKs4Uu1dOD4YlhxqFxeXOcGdUeOdbk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=fArANUOv; arc=none smtp.client-ip=209.85.128.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-42159283989so1965685e9.3; Wed, 05 Jun 2024 00:52:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717573965; x=1718178765; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=d938GCY+0VmkKPW+ZUE+VmgbyLC1bhkjlDIYYNMduOE=; b=fArANUOv3sZlxcQLsxmsYDE/3vgQIcUp4k5X6wN/nex6zPxCV9H4AOGzegoKQDbjcq O5sx6j9MJK4HMcfnR/WbQJwjMTfeqRSHLXwjRJRmbWyV0EcZNMLaAZrQ4XR1UFNNm7CF 46o6FeWX3FPMdbrz3jLJcvNB+I7JvPQkArk/vmnkOiEvaa0s4Cr7ND/LatSKd7jqWJLL LNosjZTgrt2ExWcmbvxAglccwLXWM9cYviditUH6GqNueRwJjrulMV3RxkXDfafNdfat g4kuS/xjTUbAWj2RVufpSWyaRwxlotVzWPH7dB7OCXtRKHsDudqeQI4zUmQ9P72mkf96 PqSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717573965; x=1718178765; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d938GCY+0VmkKPW+ZUE+VmgbyLC1bhkjlDIYYNMduOE=; b=iZ2mMhGzdVlfkY77st2IWLefgjXYz7hKw37850dy91R8b/PZnDp0Iy+VrsQtPBlY26 H6uBTMRAGKIv7LYtqZFD7EZCuZ1gNrLdnCss0HT6g67ho+taKIHM+Ak58uJLHc9UTMAF f+dBGLiR4xzRNV3kM79PPcJFOsq2Xj/fkuA1vyW9n03DpdYngOewdnRpXHHyfZEOTHJV 6bBM9CiqsSRcHIiovC7hApqQmliX28X3086FA2HSdsQnKJTtzncvRGQTLcd7GCBgA38D sIyDBbY7KzhVTk5+G9e7ssBDZkrQLcVR+4XAOTxPDwYBdHkC9KruxRf7o++5CGliWhOv DUxA== X-Forwarded-Encrypted: i=1; AJvYcCV9cYKedMqpgOnypiAxRDR1EDyHQnocnGWMt4IVkuEYBaU6+w3uQKzwUuurX7lt3NQjYoVL/kcuf1q6ozUQ6nileadAkXXOnn6vTmhlurz4jN9rV3gCblvIdEWEhlxCbi4qqf9hvZaut1EiGzduclm/WzdyiAfs1BQ/v53e9ohYgFN7uA== X-Gm-Message-State: AOJu0YwnlIG7bzgOo4UntEwda2CXXANHgDsZ4xQOZcVnkOjHyx133FiA AksqdoSj/1btgAAIOlAcuqpnVwDQPStj5BJt4jFblWf4hyZewtsc X-Received: by 2002:a5d:5f91:0:b0:354:f612:5f7e with SMTP id ffacd0b85a97d-35e8ef94989mr1966577f8f.56.1717573964904; Wed, 05 Jun 2024 00:52:44 -0700 (PDT) Received: from spiri.. ([2a02:2f08:a10a:2300:8e59:f160:bdc8:6311]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-35dd04c0839sm13760638f8f.23.2024.06.05.00.52.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 00:52:44 -0700 (PDT) From: Alisa-Dariana Roman X-Google-Original-From: Alisa-Dariana Roman To: Alisa-Dariana Roman , Jonathan Cameron , Michael Hennerich , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Lars-Peter Clausen , Michael Hennerich , Alexandru Tachici , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Liam Girdwood , Mark Brown Subject: [PATCH v2 3/3] iio: adc: ad7192: Fix clock config Date: Wed, 5 Jun 2024 10:51:54 +0300 Message-Id: <20240605075154.625123-3-alisa.roman@analog.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240605075154.625123-1-alisa.roman@analog.com> References: <20240605075154.625123-1-alisa.roman@analog.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit There are actually 4 configuration modes of clock source for AD719X devices. Either a crystal can be attached externally between MCLK1 and MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 pin. The other 2 modes make use of the 4.92MHz internal clock, which can be made available on the MCLK2 pin. Rename mclk to ext_clk for clarity. Note that the fix tag is for the commit that moved the driver out of staging. Fixes: b581f748cce0 ("staging: iio: adc: ad7192: move out of staging") Signed-off-by: Alisa-Dariana Roman --- drivers/iio/adc/ad7192.c | 153 ++++++++++++++++++++++++++++++--------- 1 file changed, 119 insertions(+), 34 deletions(-) diff --git a/drivers/iio/adc/ad7192.c b/drivers/iio/adc/ad7192.c index f06cb7ac4b42..75b0724142b1 100644 --- a/drivers/iio/adc/ad7192.c +++ b/drivers/iio/adc/ad7192.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -202,7 +203,8 @@ struct ad7192_state { const struct ad7192_chip_info *chip_info; struct regulator *avdd; struct regulator *vref; - struct clk *mclk; + struct clk *ext_clk; + struct clk_hw int_clk_hw; u16 int_vref_mv; u32 aincom_mv; u32 fclk; @@ -398,27 +400,6 @@ static inline bool ad7192_valid_external_frequency(u32 freq) freq <= AD7192_EXT_FREQ_MHZ_MAX); } -static int ad7192_clock_select(struct ad7192_state *st) -{ - struct device *dev = &st->sd.spi->dev; - unsigned int clock_sel; - - clock_sel = AD7192_CLK_INT; - - /* use internal clock */ - if (!st->mclk) { - if (device_property_read_bool(dev, "adi,int-clock-output-enable")) - clock_sel = AD7192_CLK_INT_CO; - } else { - if (device_property_read_bool(dev, "adi,clock-xtal")) - clock_sel = AD7192_CLK_EXT_MCLK1_2; - else - clock_sel = AD7192_CLK_EXT_MCLK2; - } - - return clock_sel; -} - static int ad7192_setup(struct iio_dev *indio_dev, struct device *dev) { struct ad7192_state *st = iio_priv(indio_dev); @@ -1194,6 +1175,96 @@ static void ad7192_reg_disable(void *reg) regulator_disable(reg); } +static const char *const ad7192_clock_names[] = { + "xtal", + "clk" +}; + +static struct ad7192_state *clk_hw_to_ad7192(struct clk_hw *hw) +{ + return container_of(hw, struct ad7192_state, int_clk_hw); +} + +static void ad7192_clk_disable_unprepare(void *clk) +{ + clk_disable_unprepare(clk); +} + +static unsigned long ad7192_clk_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + return AD7192_INT_FREQ_MHZ; +} + +static int ad7192_clk_output_is_enabled(struct clk_hw *hw) +{ + struct ad7192_state *st = clk_hw_to_ad7192(hw); + + return st->clock_sel == AD7192_CLK_INT_CO; +} + +static int ad7192_clk_prepare(struct clk_hw *hw) +{ + struct ad7192_state *st = clk_hw_to_ad7192(hw); + int ret; + + st->mode &= ~AD7192_MODE_CLKSRC_MASK; + st->mode |= AD7192_CLK_INT_CO; + + ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode); + if (ret) + return ret; + + st->clock_sel = AD7192_CLK_INT_CO; + + return 0; +} + +static void ad7192_clk_unprepare(struct clk_hw *hw) +{ + struct ad7192_state *st = clk_hw_to_ad7192(hw); + int ret; + + st->mode &= ~AD7192_MODE_CLKSRC_MASK; + st->mode |= AD7192_CLK_INT; + + ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode); + if (ret) + return; + + st->clock_sel = AD7192_CLK_INT; +} + +static const struct clk_ops ad7192_int_clk_ops = { + .recalc_rate = ad7192_clk_recalc_rate, + .is_enabled = ad7192_clk_output_is_enabled, + .prepare = ad7192_clk_prepare, + .unprepare = ad7192_clk_unprepare, +}; + +static int ad7192_register_clk_provider(struct iio_dev *indio_dev) +{ + struct ad7192_state *st = iio_priv(indio_dev); + struct device *dev = indio_dev->dev.parent; + struct fwnode_handle *fwnode = dev_fwnode(dev); + struct clk_init_data init = {}; + int ret; + + if (!IS_ENABLED(CONFIG_COMMON_CLK)) + return 0; + + init.name = fwnode_get_name(fwnode); + init.ops = &ad7192_int_clk_ops; + + st->int_clk_hw.init = &init; + ret = devm_clk_hw_register(dev, &st->int_clk_hw); + if (ret) + return ret; + + return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, + &st->int_clk_hw); +} + static int ad7192_probe(struct spi_device *spi) { struct device *dev = &spi->dev; @@ -1312,20 +1383,34 @@ static int ad7192_probe(struct spi_device *spi) st->fclk = AD7192_INT_FREQ_MHZ; - st->mclk = devm_clk_get_optional_enabled(dev, "mclk"); - if (IS_ERR(st->mclk)) - return PTR_ERR(st->mclk); + ret = device_property_match_property_string(dev, "clock-names", + ad7192_clock_names, + ARRAY_SIZE(ad7192_clock_names)); + if (ret < 0) { + st->clock_sel = AD7192_CLK_INT; + st->fclk = AD7192_INT_FREQ_MHZ; - st->clock_sel = ad7192_clock_select(st); + ret = ad7192_register_clk_provider(indio_dev); + if (ret) + return dev_err_probe(dev, ret, + "Registration of clock provider failed\n"); + } else { + st->clock_sel = AD7192_CLK_EXT_MCLK1_2 + ret; - if (st->clock_sel == AD7192_CLK_EXT_MCLK1_2 || - st->clock_sel == AD7192_CLK_EXT_MCLK2) { - st->fclk = clk_get_rate(st->mclk); - if (!ad7192_valid_external_frequency(st->fclk)) { - dev_err(dev, - "External clock frequency out of bounds\n"); - return -EINVAL; - } + st->ext_clk = devm_clk_get_enabled(dev, ad7192_clock_names[ret]); + if (IS_ERR(st->ext_clk)) + return PTR_ERR(st->ext_clk); + + ret = devm_add_action_or_reset(dev, + ad7192_clk_disable_unprepare, + st->ext_clk); + if (ret) + return ret; + + st->fclk = clk_get_rate(st->ext_clk); + if (!ad7192_valid_external_frequency(st->fclk)) + return dev_err_probe(dev, -EINVAL, + "External clock frequency out of bounds\n"); } ret = ad7192_setup(indio_dev, dev); -- 2.34.1