From patchwork Fri Mar 31 11:45:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrice Gasnier X-Patchwork-Id: 9656071 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 397B660351 for ; Fri, 31 Mar 2017 11:46:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2706F28643 for ; Fri, 31 Mar 2017 11:46:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1947A28686; Fri, 31 Mar 2017 11:46:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 926FF28643 for ; Fri, 31 Mar 2017 11:46:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=I8NL6estQDLSG3Q8NZHU+aSfjuxeehsYUKYIgNInXkA=; b=FeDAjMOxtzEl84 MuOCebNzUuBB2x6qBl1MYzs0Pc7XKL7xEPfzPpdDO+8lAfXnXqTw7PNGlf+YMiKO8LOXLCywYhg/q qWdaFhHYC7a/+IzPPN1ZufJFu6nzG63ADdqmxICxHYOaJO4mJv66oSPmZQUobRG9DtgmkEXBr+vqb fidWWjl7oSTUQBAeK2plFjYjreOmqQC1ZisIvpHmHDyFTPf6tcA7cG6pmcfLaeQCkSraNn0yhvs5e wqOrRt6fcKQEL/Igl17kv0G4qEkHaXRVaYx2/n+Zg0DX0s0eotticPRuD1ZuGcUnuemuMApYZe9Un dEzzt0CbhSU27a1jR/FA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1ctv0z-0001ab-PV; Fri, 31 Mar 2017 11:46:49 +0000 Received: from mx07-00178001.pphosted.com ([62.209.51.94]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1ctv0R-00014A-2T for linux-arm-kernel@lists.infradead.org; Fri, 31 Mar 2017 11:46:19 +0000 Received: from pps.filterd (m0046668.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id v2VBgAN3019057; Fri, 31 Mar 2017 13:45:33 +0200 Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-.pphosted.com with ESMTP id 29ggrtknux-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 31 Mar 2017 13:45:33 +0200 Received: from zeta.dmz-eu.st.com (zeta.dmz-eu.st.com [164.129.230.9]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id DCED738; Fri, 31 Mar 2017 11:45:32 +0000 (GMT) Received: from Webmail-eu.st.com (sfhdag5node3.st.com [10.75.127.15]) by zeta.dmz-eu.st.com (STMicroelectronics) with ESMTP id A29D128B5; Fri, 31 Mar 2017 11:45:32 +0000 (GMT) Received: from localhost (10.75.127.47) by SFHDAG5NODE3.st.com (10.75.127.15) with Microsoft SMTP Server (TLS) id 15.0.1178.4; Fri, 31 Mar 2017 13:45:32 +0200 From: Fabrice Gasnier To: , , , , , Subject: [PATCH 4/4] iio: dac: stm32: add support for waveform generator Date: Fri, 31 Mar 2017 13:45:07 +0200 Message-ID: <1490960707-22422-5-git-send-email-fabrice.gasnier@st.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1490960707-22422-1-git-send-email-fabrice.gasnier@st.com> References: <1490960707-22422-1-git-send-email-fabrice.gasnier@st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.47] X-ClientProxiedBy: SFHDAG3NODE1.st.com (10.75.127.7) To SFHDAG5NODE3.st.com (10.75.127.15) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-03-31_11:, , signatures=0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170331_044615_578330_036E43CE X-CRM114-Status: GOOD ( 16.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, benjamin.gaignard@linaro.org, lars@metafoo.de, alexandre.torgue@st.com, linux-iio@vger.kernel.org, pmeerw@pmeerw.net, amelie.delaunay@st.com, mcoquelin.stm32@gmail.com, knaack.h@gmx.de, fabrice.gasnier@st.com, linus.walleij@linaro.org, benjamin.gaignard@st.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP STM32 DAC has built-in noise or triangle waveform generator. Waveform generator requires trigger to be configured. - "wave" extended attribute selects noise or triangle. - "mamp" extended attribute selects either LFSR (linear feedback shift register) mask for noise waveform, OR triangle amplitude. Signed-off-by: Fabrice Gasnier --- Documentation/ABI/testing/sysfs-bus-iio-dac-stm32 | 32 ++++++ drivers/iio/dac/stm32-dac.c | 124 ++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-iio-dac-stm32 diff --git a/Documentation/ABI/testing/sysfs-bus-iio-dac-stm32 b/Documentation/ABI/testing/sysfs-bus-iio-dac-stm32 new file mode 100644 index 0000000..c2432e1 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-bus-iio-dac-stm32 @@ -0,0 +1,32 @@ +What: /sys/bus/iio/devices/iio:deviceX/wave +What: /sys/bus/iio/devices/iio:deviceX/wave_available +KernelVersion: 4.12 +Contact: fabrice.gasnier@st.com +Description: + List and/or select waveform generation provided by STM32 DAC: + - "none": (default) means normal DAC operations + - "noise": select noise waveform + - "triangle": select triangle waveform + Note: when waveform generator is used, writing _raw sysfs entry + adds a DC offset to generated waveform. Reading it reports + current output value. + +What: /sys/bus/iio/devices/iio:deviceX/mamp +What: /sys/bus/iio/devices/iio:deviceX/mamp_available +KernelVersion: 4.12 +Contact: fabrice.gasnier@st.com +Description: + List and select mask/amplitude used for noise/triangle waveform + generator, which are: + - "0": unmask bit 0 of LFSR / triangle amplitude equal to 1 + - "1": unmask bit [1:0] of LFSR / triangle amplitude equal to 3 + - "2": unmask bit [2:0] of LFSR / triangle amplitude equal to 7 + - "3": unmask bit [3:0] of LFSR / triangle amplitude equal to 15 + - "4": unmask bit [4:0] of LFSR / triangle amplitude equal to 31 + - "5": unmask bit [5:0] of LFSR / triangle amplitude equal to 63 + - "6": unmask bit [6:0] of LFSR / triangle amplitude equal to 127 + - "7": unmask bit [7:0] of LFSR / triangle amplitude equal to 255 + - "8": unmask bit [8:0] of LFSR / triangle amplitude equal to 511 + - "9": unmask bit [9:0] of LFSR / triangle amplitude equal to 1023 + - "10": unmask bit [10:0] of LFSR / triangle amplitude equal to 2047 + - "11": unmask bit [11:0] of LFSR / triangle amplitude equal to 4095 diff --git a/drivers/iio/dac/stm32-dac.c b/drivers/iio/dac/stm32-dac.c index 62e43e9..d7dda78 100644 --- a/drivers/iio/dac/stm32-dac.c +++ b/drivers/iio/dac/stm32-dac.c @@ -41,10 +41,14 @@ /** * struct stm32_dac - private data of DAC driver * @common: reference to DAC common data + * @wave: waveform generator + * @mamp: waveform mask/amplitude * @swtrig: Using software trigger */ struct stm32_dac { struct stm32_dac_common *common; + u32 wave; + u32 mamp; bool swtrig; }; @@ -157,6 +161,24 @@ static int stm32_dac_is_enabled(struct stm32_dac *dac, int channel) return !!en; } +static int stm32_dac_wavegen(struct stm32_dac *dac, int channel) +{ + struct regmap *regmap = dac->common->regmap; + u32 mask, val; + + if (channel == STM32_DAC_CHANNEL_1) { + val = FIELD_PREP(STM32_DAC_CR_WAVE1, dac->wave) | + FIELD_PREP(STM32_DAC_CR_MAMP1, dac->mamp); + mask = STM32_DAC_CR_WAVE1 | STM32_DAC_CR_MAMP1; + } else { + val = FIELD_PREP(STM32_DAC_CR_WAVE2, dac->wave) | + FIELD_PREP(STM32_DAC_CR_MAMP2, dac->mamp); + mask = STM32_DAC_CR_WAVE2 | STM32_DAC_CR_MAMP2; + } + + return regmap_update_bits(regmap, STM32_DAC_CR, mask, val); +} + static int stm32_dac_enable(struct iio_dev *indio_dev, int channel) { struct stm32_dac *dac = iio_priv(indio_dev); @@ -164,6 +186,17 @@ static int stm32_dac_enable(struct iio_dev *indio_dev, int channel) STM32_DAC_CR_EN1 : STM32_DAC_CR_EN2; int ret; + if (dac->wave && !indio_dev->trig) { + dev_err(&indio_dev->dev, "Wavegen requires a trigger\n"); + return -EINVAL; + } + + ret = stm32_dac_wavegen(dac, channel); + if (ret < 0) { + dev_err(&indio_dev->dev, "Wavegen setup failed\n"); + return ret; + } + ret = stm32_dac_set_trig(dac, indio_dev->trig, channel); if (ret < 0) { dev_err(&indio_dev->dev, "Trigger setup failed\n"); @@ -291,6 +324,96 @@ static int stm32_dac_debugfs_reg_access(struct iio_dev *indio_dev, .driver_module = THIS_MODULE, }; +/* waveform generator wave selection */ +static const char * const stm32_dac_wave_desc[] = { + "none", + "noise", + "triangle", +}; + +static int stm32_dac_set_wave(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + unsigned int type) +{ + struct stm32_dac *dac = iio_priv(indio_dev); + + if (stm32_dac_is_enabled(dac, chan->channel)) + return -EBUSY; + dac->wave = type; + + return 0; +} + +static int stm32_dac_get_wave(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan) +{ + struct stm32_dac *dac = iio_priv(indio_dev); + + return dac->wave; +} + +static const struct iio_enum stm32_dac_wave_enum = { + .items = stm32_dac_wave_desc, + .num_items = ARRAY_SIZE(stm32_dac_wave_desc), + .get = stm32_dac_get_wave, + .set = stm32_dac_set_wave, +}; + +/* + * waveform generator mask/amplitude selection: + * - noise: LFSR mask (linear feedback shift register, umasks bit 0, [1:0]...) + * - triangle: amplitude (equal to 1, 3, 5, 7... 4095) + */ +static const char * const stm32_dac_mamp_desc[] = { + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", +}; + +static int stm32_dac_set_mamp(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + unsigned int type) +{ + struct stm32_dac *dac = iio_priv(indio_dev); + + if (stm32_dac_is_enabled(dac, chan->channel)) + return -EBUSY; + dac->mamp = type; + + return 0; +} + +static int stm32_dac_get_mamp(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan) +{ + struct stm32_dac *dac = iio_priv(indio_dev); + + return dac->mamp; +} + +static const struct iio_enum stm32_dac_mamp_enum = { + .items = stm32_dac_mamp_desc, + .num_items = ARRAY_SIZE(stm32_dac_mamp_desc), + .get = stm32_dac_get_mamp, + .set = stm32_dac_set_mamp, +}; + +static const struct iio_chan_spec_ext_info stm32_dac_ext_info[] = { + IIO_ENUM("wave", IIO_SHARED_BY_ALL, &stm32_dac_wave_enum), + { + .name = "wave_available", + .shared = IIO_SHARED_BY_ALL, + .read = iio_enum_available_read, + .private = (uintptr_t)&stm32_dac_wave_enum, + }, + IIO_ENUM("mamp", IIO_SHARED_BY_ALL, &stm32_dac_mamp_enum), + { + .name = "mamp_available", + .shared = IIO_SHARED_BY_ALL, + .read = iio_enum_available_read, + .private = (uintptr_t)&stm32_dac_mamp_enum, + }, + {}, +}; + #define STM32_DAC_CHANNEL(chan, name) { \ .type = IIO_VOLTAGE, \ .indexed = 1, \ @@ -306,6 +429,7 @@ static int stm32_dac_debugfs_reg_access(struct iio_dev *indio_dev, .storagebits = 16, \ }, \ .datasheet_name = name, \ + .ext_info = stm32_dac_ext_info \ } static const struct iio_chan_spec stm32_dac_channels[] = {