public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Przemyslaw Marczak <p.marczak@samsung.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH V2 06/11] dm: adc: add simple ADC uclass implementation
Date: Tue, 13 Oct 2015 13:57:30 +0200	[thread overview]
Message-ID: <561CF1AA.3010802@samsung.com> (raw)
In-Reply-To: <CAPnjgZ3ffc088YzsLP-g4UebR-jaz_zszNGrYJty_n6jCDVMxA@mail.gmail.com>

Hello Simon,

On 10/03/2015 04:28 PM, Simon Glass wrote:
> Hi Przemyslaw,
>
> On 21 September 2015 at 13:26, Przemyslaw Marczak <p.marczak@samsung.com> wrote:
>> This commit adds:
>> - new uclass id: UCLASS_ADC
>> - new uclass driver: drivers/adc/adc-uclass.c
>>
>> The uclass's implementation is as simple as needed and provides functions:
>> - adc_init() - init ADC conversion
>> - adc_data() - convert and return data
>> - adc_data_mask() - return ADC data mask
>> - adc_channel_single_shot() - function for single ADC convertion
>>
>> Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
>> ---
>> Changes V2:
>> - new commit - introduce ADC uclass driver
>> ---
>>   drivers/Kconfig          |  2 ++
>>   drivers/Makefile         |  1 +
>>   drivers/adc/Kconfig      |  8 +++++
>>   drivers/adc/Makefile     |  8 +++++
>>   drivers/adc/adc-uclass.c | 76 +++++++++++++++++++++++++++++++++++++++++
>>   include/adc.h            | 88 ++++++++++++++++++++++++++++++++++++++++++++++++
>>   include/dm/uclass-id.h   |  1 +
>>   7 files changed, 184 insertions(+)
>>   create mode 100644 drivers/adc/Kconfig
>>   create mode 100644 drivers/adc/Makefile
>>   create mode 100644 drivers/adc/adc-uclass.c
>>   create mode 100644 include/adc.h
>
> Sorry I have quite a lot of questions and comments on this.
>

Yes, ok.

>>
>> diff --git a/drivers/Kconfig b/drivers/Kconfig
>> index 63c92c5..ad9ae3a 100644
>> --- a/drivers/Kconfig
>> +++ b/drivers/Kconfig
>> @@ -4,6 +4,8 @@ source "drivers/core/Kconfig"
>>
>>   # types of drivers sorted in alphabetical order
>>
>> +source "drivers/adc/Kconfig"
>> +
>>   source "drivers/block/Kconfig"
>>
>>   source "drivers/clk/Kconfig"
>> diff --git a/drivers/Makefile b/drivers/Makefile
>> index 9d0a595..d7d5e9f 100644
>> --- a/drivers/Makefile
>> +++ b/drivers/Makefile
>> @@ -35,6 +35,7 @@ obj-$(CONFIG_SPL_SATA_SUPPORT) += block/
>>
>>   else
>>
>> +obj-y += adc/
>>   obj-$(CONFIG_DM_DEMO) += demo/
>>   obj-$(CONFIG_BIOSEMU) += bios_emulator/
>>   obj-y += block/
>> diff --git a/drivers/adc/Kconfig b/drivers/adc/Kconfig
>> new file mode 100644
>> index 0000000..1cb1a8d
>> --- /dev/null
>> +++ b/drivers/adc/Kconfig
>> @@ -0,0 +1,8 @@
>> +config ADC
>> +       bool "Enable ADC drivers using Driver Model"
>> +       help
>> +         This allows drivers to be provided for ADCs to drive their features,
>> +         trough simple ADC uclass driver interface, with operations:
>
> through a simple
>

Ok.

>> +         - device enable
>> +         - conversion init
>> +         - conversion start and return data with data mask
>> diff --git a/drivers/adc/Makefile b/drivers/adc/Makefile
>> new file mode 100644
>> index 0000000..c4d9618
>> --- /dev/null
>> +++ b/drivers/adc/Makefile
>> @@ -0,0 +1,8 @@
>> +#
>> +# Copyright (C) 2015 Samsung Electronics
>> +# Przemyslaw Marczak <p.marczak@samsung.com>
>> +#
>> +# SPDX-License-Identifier:     GPL-2.0+
>> +#
>> +
>> +obj-$(CONFIG_ADC) += adc-uclass.o
>> diff --git a/drivers/adc/adc-uclass.c b/drivers/adc/adc-uclass.c
>> new file mode 100644
>> index 0000000..bb71b6e
>> --- /dev/null
>> +++ b/drivers/adc/adc-uclass.c
>> @@ -0,0 +1,76 @@
>> +/*
>> + * Copyright (C) 2015 Samsung Electronics
>> + * Przemyslaw Marczak <p.marczak@samsung.com>
>> + *
>> + * SPDX-License-Identifier:    GPL-2.0+
>> + */
>> +
>> +#include <common.h>
>> +#include <errno.h>
>> +#include <dm.h>
>> +#include <dm/lists.h>
>> +#include <dm/device-internal.h>
>> +#include <dm/uclass-internal.h>
>> +#include <adc.h>
>> +
>> +#define ADC_UCLASS_PLATDATA_SIZE       sizeof(struct adc_uclass_platdata)
>
> Can we drop #define?
>

Don't you think, that single line initialization looks better than those 
with line breaks? This is the purpose of that define.

>> +
>> +int adc_init(struct udevice *dev, int channel)
>> +{
>> +       const struct adc_ops *ops = dev_get_driver_ops(dev);
>> +
>> +       if (ops->adc_init)
>> +               return ops->adc_init(dev, channel);
>> +
>> +       return -ENOSYS;
>
> Let's turn each of these around so that errors are the exception, not
> the normal path.
>
> if (!ops->adc_init)
>     return -ENOSYS;
>
> return ops->...
>

Ok, I will turn it.

>> +}
>> +
>> +int adc_data(struct udevice *dev, unsigned int *data)
>> +{
>> +       const struct adc_ops *ops = dev_get_driver_ops(dev);
>> +
>> +       *data = 0;
>> +
>> +       if (ops->adc_data)
>> +               return ops->adc_data(dev, data);
>> +
>> +       return -ENOSYS;
>> +}
>> +
>> +int adc_data_mask(struct udevice *dev)
>> +{
>> +       struct adc_uclass_platdata *uc_pdata = dev_get_uclass_platdata(dev);
>> +
>> +       if (uc_pdata)
>> +               return uc_pdata->data_mask;
>> +
>> +       return -ENOSYS;
>> +}
>> +
>> +int adc_channel_single_shot(const char *name, int channel, unsigned int *data)
>> +{
>> +       struct udevice *dev;
>> +       int ret;
>> +
>> +       *data = 0;
>
> I don't think we need this assignment. This can be undefined if an
> error is returned.
>

Ok.

>> +
>> +       ret = uclass_get_device_by_name(UCLASS_ADC, name, &dev);
>> +       if (ret)
>> +               return ret;
>> +
>> +       ret = adc_init(dev, channel);
>> +       if (ret)
>> +               return ret;
>> +
>> +       ret = adc_data(dev, data);
>> +       if (ret)
>> +               return ret;
>> +
>> +       return 0;
>> +}
>> +
>> +UCLASS_DRIVER(adc) = {
>> +       .id     = UCLASS_ADC,
>> +       .name   = "adc",
>> +       .per_device_platdata_auto_alloc_size = ADC_UCLASS_PLATDATA_SIZE,
>> +};
>> diff --git a/include/adc.h b/include/adc.h
>> new file mode 100644
>> index 0000000..57b9281
>> --- /dev/null
>> +++ b/include/adc.h
>> @@ -0,0 +1,88 @@
>> +/*
>> + * Copyright (C) 2015 Samsung Electronics
>> + * Przemyslaw Marczak <p.marczak@samsung.com>
>> + *
>> + * SPDX-License-Identifier:    GPL-2.0+
>> + */
>> +
>> +#ifndef _ADC_H_
>> +#define _ADC_H_
>> +
>> +/**
>> + * struct adc_uclass_platdata - ADC power supply and reference Voltage info
>> + *
>> + * @data_mask     - conversion output data mask
>> + * @channels_num  - number of analog channels input
>> + * @vdd_supply    - positive reference voltage supply
>> + * @vss_supply    - negative reference voltage supply
>> + */
>> +struct adc_uclass_platdata {
>> +       unsigned int data_mask;
>> +       unsigned int channels_num;
>> +       struct udevice *vdd_supply;
>> +       struct udevice *vss_supply;
>> +};
>> +
>> +/**
>> + * struct adc_ops - ADC device operations
>> + */
>> +struct adc_ops {
>> +       /**
>> +        * conversion_init() - init device's default conversion parameters
>> +        *
>> +        * @dev:     ADC device to init
>> +        * @channel: analog channel number
>> +        * @return:  0 if OK, -ve on error
>> +        */
>> +       int (*adc_init)(struct udevice *dev, int channel);
>
> s/adc_init/init/
>
> Same below. Also it seems like this starts the conversion, so how
> about using the name start().
>

Ok, I will modify the API.

>> +
>> +       /**
>> +        * conversion_data() - get output data of given channel conversion
>> +        *
>> +        * @dev:          ADC device to trigger
>> +        * @channel_data: pointer to returned channel's data
>> +        * @return:       0 if OK, -ve on error
>> +        */
>> +       int (*adc_data)(struct udevice *dev, unsigned int *channel_data);
>> +};
>> +
>> +/**
>> + * adc_init() - init device's default conversion parameters for the given
>> + * analog input channel.
>> + *
>> + * @dev:     ADC device to init
>> + * @channel: analog channel number
>> + * @return:  0 if OK, -ve on error
>> + */
>> +int adc_init(struct udevice *dev, int channel);
>
> adc_start()?
>
>> +
>> +/**
>> + * adc_data() - get conversion data for the channel inited by adc_init().
>> + *
>> + * @dev:    ADC device to trigger
>> + * @data:   pointer to returned channel's data
>> + * @return: 0 if OK, -ve on error
>> + */
>> +int adc_data(struct udevice *dev, unsigned int *data);
>
> This seems a bit wonky. Why not pass in the channel with this call?
> What if I want to start conversions on multiple channels at the same
> time?
>

Right, this could be more flexible. I will modify it.

>> +
>> +/**
>> + * adc_data_mask() - get data mask (ADC resolution mask) for given ADC device.
>> + * This can be used if adc uclass platform data is filled.
>> + *
>> + * @dev:    ADC device to check
>> + * @return: 0 if OK, -ve on error
>
> If it always returns 0 unless there is an error, what is the point? Or
> is this comment incorrect?
>

Yes, that is a mistake.

>> + */
>> +int adc_data_mask(struct udevice *dev);
>> +
>> +/**
>> + * adc_channel_single_shot() - get output data of convertion for the ADC
>> + * device's channel. This function search for the device with the given name,
>> + * init the given channel and returns the convertion data.
>
> It also inits the device.
>
> I would prefer a function that finds a device by name and inits it.
>

Ah, ok.

>> + *
>> + * @name:    device's name to search
>> + * @channel: device's input channel to init
>> + * @data:    pointer to convertion output data
>
> conversion
>

Okay.

>> + */
>> +int adc_channel_single_shot(const char *name, int channel, unsigned int *data);
>> +
>> +#endif
>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
>> index 1eeec74..0f7e7da 100644
>> --- a/include/dm/uclass-id.h
>> +++ b/include/dm/uclass-id.h
>> @@ -25,6 +25,7 @@ enum uclass_id {
>>          UCLASS_SIMPLE_BUS,      /* bus with child devices */
>>
>>          /* U-Boot uclasses start here - in alphabetical order */
>> +       UCLASS_ADC,             /* Analog-to-digital converter */
>>          UCLASS_CLK,             /* Clock source, e.g. used by peripherals */
>>          UCLASS_CPU,             /* CPU, typically part of an SoC */
>>          UCLASS_CROS_EC,         /* Chrome OS EC */
>> --
>> 1.9.1
>>
>
> Regards,
> Simon
>

Thanks for comments. The ADC API is not ideal, however it fits my needs. 
Also it looks like nobody was using ADC before in U-Boot, so I can 
suppose it can be as simple as possible. I will clean this :)

Best regards
-- 
Przemyslaw Marczak
Samsung R&D Institute Poland
Samsung Electronics
p.marczak at samsung.com

  reply	other threads:[~2015-10-13 11:57 UTC|newest]

Thread overview: 106+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-28 13:59 [U-Boot] [PATCH 0/7] Add board detection for Odroid XU3 / XU3Lite / XU4 Przemyslaw Marczak
2015-08-28 13:59 ` [U-Boot] [PATCH 1/7] s5p: cpu_info: use defined CPU name if available Przemyslaw Marczak
2015-09-01  0:33   ` Simon Glass
2015-09-04 15:04     ` Przemyslaw Marczak
2015-08-28 13:59 ` [U-Boot] [PATCH 2/7] peach-pi: define CPU name for SoC Exynos5800 Przemyslaw Marczak
2015-08-28 13:59 ` [U-Boot] [PATCH 3/7] Exynos5422/5800: set cpu id to 0x5422 Przemyslaw Marczak
2015-09-01  0:33   ` Simon Glass
2015-08-28 13:59 ` [U-Boot] [PATCH 4/7] dm: pmic: add s2mps11 PMIC I/O driver Przemyslaw Marczak
2015-09-01  0:33   ` Simon Glass
2015-09-04 15:04     ` Przemyslaw Marczak
2015-09-07  0:11   ` Minkyu Kang
2015-09-09 10:31     ` Przemyslaw Marczak
2015-08-28 13:59 ` [U-Boot] [PATCH 5/7] odroid-xu3: enable s2mps11 PMIC support Przemyslaw Marczak
2015-09-01  0:33   ` Simon Glass
2015-08-28 13:59 ` [U-Boot] [PATCH 6/7] Exynos: add internal ADC driver Przemyslaw Marczak
2015-09-01  0:33   ` Simon Glass
2015-09-04 15:04     ` Przemyslaw Marczak
2015-08-28 13:59 ` [U-Boot] [PATCH 7/7] exynos5-dt: add board detection for Odroid XU3/XU3L/XU4 Przemyslaw Marczak
2015-09-01  0:33   ` Simon Glass
2015-09-04 15:04     ` Przemyslaw Marczak
2015-08-30 19:03 ` [U-Boot] [PATCH 0/7] Add board detection for Odroid XU3 / XU3Lite / XU4 Anand Moon
2015-08-31 12:17   ` Przemyslaw Marczak
2015-10-21  1:58   ` Siarhei Siamashka
2015-10-21  9:47     ` Anand Moon
2015-10-21  9:57     ` Anand Moon
2015-10-21 10:13       ` Przemyslaw Marczak
2015-08-31 13:22 ` Simon Glass
2015-08-31 18:29   ` Przemyslaw Marczak
2015-08-31 23:13     ` Simon Glass
2015-09-04 15:04       ` Przemyslaw Marczak
2015-09-21 12:26 ` [U-Boot] [PATCH V2 00/11] " Przemyslaw Marczak
2015-09-21 12:26   ` [U-Boot] [PATCH V2 01/11] samsung: board/misc: check returned pointer for get_board_type() calls Przemyslaw Marczak
2015-10-03 14:27     ` Simon Glass
2015-09-21 12:26   ` [U-Boot] [PATCH V2 02/11] s5p: cpu_info: print "cpu-model" if exists in dts Przemyslaw Marczak
2015-10-03 14:27     ` Simon Glass
2015-10-13 11:57       ` Przemyslaw Marczak
2015-09-21 12:26   ` [U-Boot] [PATCH V2 03/11] Peach-Pi: dts: add cpu-model string Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-10-13 11:57       ` Przemyslaw Marczak
2015-09-21 12:26   ` [U-Boot] [PATCH V2 04/11] Exynos5422/5800: set cpu id to 0x5422 Przemyslaw Marczak
2015-09-21 12:47     ` Jaehoon Chung
2015-09-21 13:01       ` Przemyslaw Marczak
2015-09-21 12:26   ` [U-Boot] [PATCH V2 05/11] dm: pmic: add s2mps11 PMIC I/O driver Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-10-13 11:57       ` Przemyslaw Marczak
2015-09-21 12:26   ` [U-Boot] [PATCH V2 06/11] dm: adc: add simple ADC uclass implementation Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-10-13 11:57       ` Przemyslaw Marczak [this message]
2015-09-21 12:26   ` [U-Boot] [PATCH V2 07/11] dm: adc: add Exynos54xx compatible ADC driver Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-10-13 11:58       ` Przemyslaw Marczak
2015-10-19  2:20         ` Simon Glass
2015-09-21 12:26   ` [U-Boot] [PATCH V2 08/11] Odroid-XU3: enable s2mps11 PMIC support Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-09-21 12:26   ` [U-Boot] [PATCH V2 09/11] Exynos54xx: dts: add ADC node Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-09-21 12:26   ` [U-Boot] [PATCH V2 10/11] Odroid-XU3: dts: enable ADC, with request for pre-reloc bind Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-09-21 12:26   ` [U-Boot] [PATCH V2 11/11] exynos5-dt-types: add board detection for Odroid XU3/XU3L/XU4 Przemyslaw Marczak
2015-10-03 14:28     ` Simon Glass
2015-10-13 11:59       ` Przemyslaw Marczak
2015-10-19  2:21         ` Simon Glass
2015-09-27 12:20   ` [U-Boot] [PATCH V2 00/11] Add board detection for Odroid XU3 / XU3Lite / XU4 Anand Moon
2015-09-28 10:19     ` Przemyslaw Marczak
2015-10-01 11:07   ` Przemyslaw Marczak
2015-10-03 14:30     ` Simon Glass
2015-10-13 11:59       ` Przemyslaw Marczak
2015-10-27 12:07   ` [U-Boot] [PATCH V3 00/14] " Przemyslaw Marczak
2015-10-27 12:07     ` [U-Boot] [PATCH V3 01/14] samsung: board/misc: check returned pointer for get_board_type() calls Przemyslaw Marczak
2015-10-28 18:50       ` Simon Glass
2015-10-30  3:00         ` Anand Moon
2015-10-27 12:07     ` [U-Boot] [PATCH V3 02/14] s5p: cpu_info: print "cpu-model" if exists in dts Przemyslaw Marczak
2015-10-28 18:50       ` Simon Glass
2015-10-30  3:01       ` Anand Moon
2015-10-27 12:07     ` [U-Boot] [PATCH V3 03/14] Peach-Pi: dts: add cpu-model string Przemyslaw Marczak
2015-10-28 18:50       ` Simon Glass
2015-10-30  3:02       ` Anand Moon
2015-10-27 12:07     ` [U-Boot] [PATCH V3 04/14] Exynos5422/5800: set cpu id to 0x5422 Przemyslaw Marczak
2015-10-30  3:03       ` Anand Moon
2015-10-27 12:07     ` [U-Boot] [PATCH V3 05/14] dm: pmic: add s2mps11 PMIC I/O driver Przemyslaw Marczak
2015-10-28 18:50       ` Simon Glass
2015-10-30  3:04       ` Anand Moon
2015-10-27 12:07     ` [U-Boot] [PATCH V3 06/14] dm: regulator: add function device_get_supply_regulator() Przemyslaw Marczak
2015-11-05 18:25       ` Simon Glass
2015-10-27 12:08     ` [U-Boot] [PATCH V3 07/14] dm: adc: add simple ADC uclass implementation Przemyslaw Marczak
2015-10-27 13:53       ` Przemyslaw Marczak
2015-11-05 18:25       ` Simon Glass
2015-10-27 12:08     ` [U-Boot] [PATCH V3 08/14] dm: adc: add Exynos54xx compatible ADC driver Przemyslaw Marczak
2015-11-06  0:15       ` Simon Glass
2015-10-27 12:08     ` [U-Boot] [PATCH V3 09/14] Odroid-XU3: enable s2mps11 PMIC support Przemyslaw Marczak
2015-10-30  3:09       ` Anand Moon
2015-10-27 12:08     ` [U-Boot] [PATCH V3 10/14] Exynos54xx: dts: add ADC node Przemyslaw Marczak
2015-10-28 18:50       ` Simon Glass
2015-10-29 13:58         ` Przemyslaw Marczak
2015-11-06  3:15           ` Simon Glass
2015-11-06  8:48             ` Przemyslaw Marczak
2015-10-27 12:08     ` [U-Boot] [PATCH V3 11/14] Odroid-XU3: dts: enable ADC, with request for pre-reloc bind Przemyslaw Marczak
2015-10-27 12:08     ` [U-Boot] [PATCH V3 12/14] exynos5-dt-types: add board detection for Odroid XU3/XU3L/XU4 Przemyslaw Marczak
2015-10-30  3:06       ` Anand Moon
2015-10-27 12:08     ` [U-Boot] [PATCH V3 13/14] sandbox: add ADC driver Przemyslaw Marczak
2015-11-04  9:52       ` [U-Boot] [PATCH] Add missing file: include/sandbox-adc.h Przemyslaw Marczak
2015-11-06 12:07         ` Simon Glass
2015-11-06  0:15       ` [U-Boot] [PATCH V3 13/14] sandbox: add ADC driver Simon Glass
2015-10-27 12:08     ` [U-Boot] [PATCH V3 14/14] sandbox: add ADC unit tests Przemyslaw Marczak
2015-11-06  0:15       ` Simon Glass
2015-11-02  5:37     ` [U-Boot] [PATCH V3 00/14] Add board detection for Odroid XU3 / XU3Lite / XU4 Minkyu Kang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=561CF1AA.3010802@samsung.com \
    --to=p.marczak@samsung.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox