diff mbox series

firmware: vpd: Add an interface to read VPD value

Message ID 20191007071610.65714-1-cychiang@chromium.org (mailing list archive)
State New, archived
Headers show
Series firmware: vpd: Add an interface to read VPD value | expand

Commit Message

Cheng-yi Chiang Oct. 7, 2019, 7:16 a.m. UTC
Add an interface for other driver to query VPD value.
This will be used for ASoC machine driver to query calibration
data stored in VPD for smart amplifier speaker resistor
calibration.

Signed-off-by: Cheng-Yi Chiang <cychiang@chromium.org>
---
 drivers/firmware/google/vpd.c              | 16 ++++++++++++++++
 include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++
 2 files changed, 34 insertions(+)
 create mode 100644 include/linux/firmware/google/google_vpd.h

Comments

Tzung-Bi Shih Oct. 7, 2019, 8:03 a.m. UTC | #1
On Mon, Oct 7, 2019 at 3:16 PM Cheng-Yi Chiang <cychiang@chromium.org> wrote:
>
> Add an interface for other driver to query VPD value.
> This will be used for ASoC machine driver to query calibration
> data stored in VPD for smart amplifier speaker resistor
> calibration.
>
> Signed-off-by: Cheng-Yi Chiang <cychiang@chromium.org>
> ---
>  drivers/firmware/google/vpd.c              | 16 ++++++++++++++++
>  include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++
>  2 files changed, 34 insertions(+)
>  create mode 100644 include/linux/firmware/google/google_vpd.h
>
> diff --git a/drivers/firmware/google/vpd.c b/drivers/firmware/google/vpd.c
> index db0812263d46..71e9d2da63be 100644
> --- a/drivers/firmware/google/vpd.c
> +++ b/drivers/firmware/google/vpd.c
> @@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp,
>                                        info->bin_attr.size);
>  }
>
> +int vpd_attribute_read_value(bool ro, const char *key,
> +                            char **value, u32 value_len)
> +{
> +       struct vpd_attrib_info *info;
> +       struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd;
> +
> +       list_for_each_entry(info, &sec->attribs, list) {
> +               if (strcmp(info->key, key) == 0) {
> +                       *value = kstrndup(info->value, value_len, GFP_KERNEL);

Value is not necessary a NULL-terminated string.
kmalloc(info->bin_attr.size) and memcpy(...) would make the most
sense.

The value_len parameter makes less sense.  It seems the caller knows
the length of the value in advance.
Suggest to change the value_len to report the length of value.  I.e.
*value_len = info->bin_attr.size;

Also please check the return value for memory allocation-like
functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but
the function returned 0.

> +                       return 0;
> +               }
> +       }
> +       return -EINVAL;
> +}
> +EXPORT_SYMBOL(vpd_attribute_read_value);
> +
>  /*
>   * vpd_section_check_key_name()
>   *
> diff --git a/include/linux/firmware/google/google_vpd.h b/include/linux/firmware/google/google_vpd.h
> new file mode 100644
> index 000000000000..6f1160f28af8
> --- /dev/null
> +++ b/include/linux/firmware/google/google_vpd.h
> @@ -0,0 +1,18 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Google VPD interface.
> + *
> + * Copyright 2019 Google Inc.
> + */
> +
> +/* Interface for reading VPD value on Chrome platform. */
> +
> +#ifndef __GOOGLE_VPD_H
> +#define __GOOGLE_VPD_H
> +
> +#include <linux/types.h>
> +
> +int vpd_attribute_read_value(bool ro, const char *key,
> +                            char **value, u32 value_len);
> +
> +#endif  /* __GOOGLE_VPD_H */
> --
> 2.23.0.581.g78d2f28ef7-goog
>
Cheng-yi Chiang Oct. 7, 2019, 8:52 a.m. UTC | #2
On Mon, Oct 7, 2019 at 4:03 PM Tzung-Bi Shih <tzungbi@google.com> wrote:
>
> On Mon, Oct 7, 2019 at 3:16 PM Cheng-Yi Chiang <cychiang@chromium.org> wrote:
> >
> > Add an interface for other driver to query VPD value.
> > This will be used for ASoC machine driver to query calibration
> > data stored in VPD for smart amplifier speaker resistor
> > calibration.
> >
> > Signed-off-by: Cheng-Yi Chiang <cychiang@chromium.org>
> > ---
> >  drivers/firmware/google/vpd.c              | 16 ++++++++++++++++
> >  include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++
> >  2 files changed, 34 insertions(+)
> >  create mode 100644 include/linux/firmware/google/google_vpd.h
> >
> > diff --git a/drivers/firmware/google/vpd.c b/drivers/firmware/google/vpd.c
> > index db0812263d46..71e9d2da63be 100644
> > --- a/drivers/firmware/google/vpd.c
> > +++ b/drivers/firmware/google/vpd.c
> > @@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp,
> >                                        info->bin_attr.size);
> >  }
> >
> > +int vpd_attribute_read_value(bool ro, const char *key,
> > +                            char **value, u32 value_len)
> > +{
> > +       struct vpd_attrib_info *info;
> > +       struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd;
> > +
> > +       list_for_each_entry(info, &sec->attribs, list) {
> > +               if (strcmp(info->key, key) == 0) {
> > +                       *value = kstrndup(info->value, value_len, GFP_KERNEL);
>
> Value is not necessary a NULL-terminated string.
> kmalloc(info->bin_attr.size) and memcpy(...) would make the most
> sense.
>
> The value_len parameter makes less sense.  It seems the caller knows
> the length of the value in advance.
> Suggest to change the value_len to report the length of value.  I.e.
> *value_len = info->bin_attr.size;
>
> Also please check the return value for memory allocation-like
> functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but
> the function returned 0.

Thanks for the review.
I will them in v2.

>
> > +                       return 0;
> > +               }
> > +       }
> > +       return -EINVAL;
> > +}
> > +EXPORT_SYMBOL(vpd_attribute_read_value);
> > +
> >  /*
> >   * vpd_section_check_key_name()
> >   *
> > diff --git a/include/linux/firmware/google/google_vpd.h b/include/linux/firmware/google/google_vpd.h
> > new file mode 100644
> > index 000000000000..6f1160f28af8
> > --- /dev/null
> > +++ b/include/linux/firmware/google/google_vpd.h
> > @@ -0,0 +1,18 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +/*
> > + * Google VPD interface.
> > + *
> > + * Copyright 2019 Google Inc.
> > + */
> > +
> > +/* Interface for reading VPD value on Chrome platform. */
> > +
> > +#ifndef __GOOGLE_VPD_H
> > +#define __GOOGLE_VPD_H
> > +
> > +#include <linux/types.h>
> > +
> > +int vpd_attribute_read_value(bool ro, const char *key,
> > +                            char **value, u32 value_len);
> > +
> > +#endif  /* __GOOGLE_VPD_H */
> > --
> > 2.23.0.581.g78d2f28ef7-goog
> >
Guenter Roeck Oct. 7, 2019, 12:27 p.m. UTC | #3
On 10/7/19 1:03 AM, Tzung-Bi Shih wrote:
> On Mon, Oct 7, 2019 at 3:16 PM Cheng-Yi Chiang <cychiang@chromium.org> wrote:
>>
>> Add an interface for other driver to query VPD value.
>> This will be used for ASoC machine driver to query calibration
>> data stored in VPD for smart amplifier speaker resistor
>> calibration.
>>
>> Signed-off-by: Cheng-Yi Chiang <cychiang@chromium.org>
>> ---
>>   drivers/firmware/google/vpd.c              | 16 ++++++++++++++++
>>   include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++
>>   2 files changed, 34 insertions(+)
>>   create mode 100644 include/linux/firmware/google/google_vpd.h
>>
>> diff --git a/drivers/firmware/google/vpd.c b/drivers/firmware/google/vpd.c
>> index db0812263d46..71e9d2da63be 100644
>> --- a/drivers/firmware/google/vpd.c
>> +++ b/drivers/firmware/google/vpd.c
>> @@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp,
>>                                         info->bin_attr.size);
>>   }
>>
>> +int vpd_attribute_read_value(bool ro, const char *key,
>> +                            char **value, u32 value_len)

FWIW, I don't think the "_value" in this function name adds any value,
unless there is going to be some other read function.

The API should be documented, and state clearly that the caller must release
the returned value.

>> +{
>> +       struct vpd_attrib_info *info;
>> +       struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd;
>> +
>> +       list_for_each_entry(info, &sec->attribs, list) {
>> +               if (strcmp(info->key, key) == 0) {
>> +                       *value = kstrndup(info->value, value_len, GFP_KERNEL);
> 
> Value is not necessary a NULL-terminated string.
> kmalloc(info->bin_attr.size) and memcpy(...) would make the most
> sense.
> 
kmemdup() ?

> The value_len parameter makes less sense.  It seems the caller knows
> the length of the value in advance.
> Suggest to change the value_len to report the length of value.  I.e.
> *value_len = info->bin_attr.size;
>  > Also please check the return value for memory allocation-like
> functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but
> the function returned 0.
> 
>> +                       return 0;
>> +               }
>> +       }
>> +       return -EINVAL;

Maybe something like -ENOENT would be more appropriate here.

>> +}
>> +EXPORT_SYMBOL(vpd_attribute_read_value);
>> +

I would suggest to use EXPORT_SYMBOL_GPL().

>>   /*
>>    * vpd_section_check_key_name()
>>    *
>> diff --git a/include/linux/firmware/google/google_vpd.h b/include/linux/firmware/google/google_vpd.h
>> new file mode 100644
>> index 000000000000..6f1160f28af8
>> --- /dev/null
>> +++ b/include/linux/firmware/google/google_vpd.h
>> @@ -0,0 +1,18 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +/*
>> + * Google VPD interface.
>> + *
>> + * Copyright 2019 Google Inc.
>> + */
>> +
>> +/* Interface for reading VPD value on Chrome platform. */
>> +
>> +#ifndef __GOOGLE_VPD_H
>> +#define __GOOGLE_VPD_H
>> +
>> +#include <linux/types.h>
>> +
>> +int vpd_attribute_read_value(bool ro, const char *key,
>> +                            char **value, u32 value_len);
>> +
>> +#endif  /* __GOOGLE_VPD_H */
>> --
>> 2.23.0.581.g78d2f28ef7-goog
>>
>
Cheng-yi Chiang Oct. 7, 2019, 1:58 p.m. UTC | #4
On Mon, Oct 7, 2019 at 8:27 PM Guenter Roeck <linux@roeck-us.net> wrote:
>
> On 10/7/19 1:03 AM, Tzung-Bi Shih wrote:
> > On Mon, Oct 7, 2019 at 3:16 PM Cheng-Yi Chiang <cychiang@chromium.org> wrote:
> >>
> >> Add an interface for other driver to query VPD value.
> >> This will be used for ASoC machine driver to query calibration
> >> data stored in VPD for smart amplifier speaker resistor
> >> calibration.
> >>
> >> Signed-off-by: Cheng-Yi Chiang <cychiang@chromium.org>
> >> ---
> >>   drivers/firmware/google/vpd.c              | 16 ++++++++++++++++
> >>   include/linux/firmware/google/google_vpd.h | 18 ++++++++++++++++++
> >>   2 files changed, 34 insertions(+)
> >>   create mode 100644 include/linux/firmware/google/google_vpd.h
> >>
> >> diff --git a/drivers/firmware/google/vpd.c b/drivers/firmware/google/vpd.c
> >> index db0812263d46..71e9d2da63be 100644
> >> --- a/drivers/firmware/google/vpd.c
> >> +++ b/drivers/firmware/google/vpd.c
> >> @@ -65,6 +65,22 @@ static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp,
> >>                                         info->bin_attr.size);
> >>   }
> >>
> >> +int vpd_attribute_read_value(bool ro, const char *key,
> >> +                            char **value, u32 value_len)
>
> FWIW, I don't think the "_value" in this function name adds any value,
> unless there is going to be some other read function.
ACK
>
> The API should be documented, and state clearly that the caller must release
> the returned value.
ACK
>
> >> +{
> >> +       struct vpd_attrib_info *info;
> >> +       struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd;
> >> +
> >> +       list_for_each_entry(info, &sec->attribs, list) {
> >> +               if (strcmp(info->key, key) == 0) {
> >> +                       *value = kstrndup(info->value, value_len, GFP_KERNEL);
> >
> > Value is not necessary a NULL-terminated string.
> > kmalloc(info->bin_attr.size) and memcpy(...) would make the most
> > sense.
> >
> kmemdup() ?
ACK
>
> > The value_len parameter makes less sense.  It seems the caller knows
> > the length of the value in advance.
> > Suggest to change the value_len to report the length of value.  I.e.
> > *value_len = info->bin_attr.size;
> >  > Also please check the return value for memory allocation-like
> > functions (e.g. kstrndup, kmalloc) so that *value won't be NULL but
> > the function returned 0.
> >
> >> +                       return 0;
> >> +               }
> >> +       }
> >> +       return -EINVAL;
>
> Maybe something like -ENOENT would be more appropriate here.
>
ACK
> >> +}
> >> +EXPORT_SYMBOL(vpd_attribute_read_value);
> >> +
>
> I would suggest to use EXPORT_SYMBOL_GPL().
>
ACK

Hi Guenter,
Thanks for the quick review.
I'll update accordingly in v2.

> >>   /*
> >>    * vpd_section_check_key_name()
> >>    *
> >> diff --git a/include/linux/firmware/google/google_vpd.h b/include/linux/firmware/google/google_vpd.h
> >> new file mode 100644
> >> index 000000000000..6f1160f28af8
> >> --- /dev/null
> >> +++ b/include/linux/firmware/google/google_vpd.h
> >> @@ -0,0 +1,18 @@
> >> +/* SPDX-License-Identifier: GPL-2.0 */
> >> +/*
> >> + * Google VPD interface.
> >> + *
> >> + * Copyright 2019 Google Inc.
> >> + */
> >> +
> >> +/* Interface for reading VPD value on Chrome platform. */
> >> +
> >> +#ifndef __GOOGLE_VPD_H
> >> +#define __GOOGLE_VPD_H
> >> +
> >> +#include <linux/types.h>
> >> +
> >> +int vpd_attribute_read_value(bool ro, const char *key,
> >> +                            char **value, u32 value_len);
> >> +
> >> +#endif  /* __GOOGLE_VPD_H */
> >> --
> >> 2.23.0.581.g78d2f28ef7-goog
> >>
> >
>
Stephen Boyd Oct. 7, 2019, 3:35 p.m. UTC | #5
Quoting Cheng-yi Chiang (2019-10-07 06:58:41)
> 
> Hi Guenter,
> Thanks for the quick review.
> I'll update accordingly in v2.

I'd prefer this use the nvmem framework which already handles many of
the requirements discussed here. Implement an nvmem provider and figure
out how to wire that up to the kernel users. Also, please include a user
of the added support, otherwise it is impossible to understand how this
code is used.
Cheng-yi Chiang Oct. 7, 2019, 6:50 p.m. UTC | #6
On Mon, Oct 7, 2019 at 11:35 PM Stephen Boyd <swboyd@chromium.org> wrote:
>
> Quoting Cheng-yi Chiang (2019-10-07 06:58:41)
> >
> > Hi Guenter,
> > Thanks for the quick review.
> > I'll update accordingly in v2.
>
> I'd prefer this use the nvmem framework which already handles many of
> the requirements discussed here. Implement an nvmem provider and figure
> out how to wire that up to the kernel users. Also, please include a user
> of the added support, otherwise it is impossible to understand how this
> code is used.
>
Hi Stephen,
Thanks for the suggestion.
My usage is for Intel machine driver to read a string for speaker calibration.

https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/1838091/4/sound/soc/intel/boards/cml_rt1011_rt5682.c#325

Based on the comments in this thread, its usage would look like

#define DSM_CALIB_KEY "dsm_calib"
static int load_calibration_data(struct cml_card_private *ctx) {
          char *data = NULL;
          int ret;
          u32 value_len;

          /* Read calibration data from VPD. */
          ret = vpd_attribute_read(1, DSM_CALIB_KEY,
                                         (u8 **)&data, &value_len);

          /* Parsing of this string...*/
}

It is currently pending on unmerged machine driver cml_rt1011_rt5682.c
in ASoC so I can not post it for review for now.

As for nvmem approach, I looked into examples of nvmem usage, and have
a rough idea how to do this.

1) In vpd.c, as it parses key and value in the VPD section, add nvmem cell  with
{
.name=key,
.offset=consumed,   // need some change in vpd_decodec.c to get the
offset of value in the section.
.bytes=value
}
Implement read function with vpd_section as context.

2) In vpd.c, register an nvm_device using devm_nvmem_register in
coreboot_driver's probe function vpd_probe.

3) As my use case does not use device tree, it is hard for ASoC
machine to access nvmem device. I am wondering if I can use
nvm_cell_lookup so machine driver can find the nvmem device using a
con_id. But currently the cell lookup API requires a matched device,
which does not fit my usage because there will be different machine
drivers requesting the value.
I think I can still workaround this by adding the lookup table in
machine driver. This would seem to be a bit weird because I found that
most lookup table is added in provider side, not consumer side. Not
sure if this is logically correct.

IMO the nvmem approach would create more complexity to support this
simple usage. Plus, the underlying assumption of accessing data with
offset in a buffer does not fit well with the already parsed VPD
values in a list of vpd_attrib_info. But if you strongly feel that
this is a better approach I can work toward this.

Thanks!
Stephen Boyd Oct. 8, 2019, 3:14 p.m. UTC | #7
Quoting Cheng-yi Chiang (2019-10-07 11:50:31)
> On Mon, Oct 7, 2019 at 11:35 PM Stephen Boyd <swboyd@chromium.org> wrote:
> >
> > Quoting Cheng-yi Chiang (2019-10-07 06:58:41)
> > >
> > > Hi Guenter,
> > > Thanks for the quick review.
> > > I'll update accordingly in v2.
> >
> > I'd prefer this use the nvmem framework which already handles many of
> > the requirements discussed here. Implement an nvmem provider and figure
> > out how to wire that up to the kernel users. Also, please include a user
> > of the added support, otherwise it is impossible to understand how this
> > code is used.
> >
> Hi Stephen,
> Thanks for the suggestion.
> My usage is for Intel machine driver to read a string for speaker calibration.
> 
> https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/1838091/4/sound/soc/intel/boards/cml_rt1011_rt5682.c#325
> 
> Based on the comments in this thread, its usage would look like
> 
> #define DSM_CALIB_KEY "dsm_calib"
> static int load_calibration_data(struct cml_card_private *ctx) {
>           char *data = NULL;
>           int ret;
>           u32 value_len;
> 
>           /* Read calibration data from VPD. */
>           ret = vpd_attribute_read(1, DSM_CALIB_KEY,
>                                          (u8 **)&data, &value_len);
> 
>           /* Parsing of this string...*/
> }
> 
> It is currently pending on unmerged machine driver cml_rt1011_rt5682.c
> in ASoC so I can not post it for review for now.
> 
> As for nvmem approach, I looked into examples of nvmem usage, and have
> a rough idea how to do this.
> 
> 1) In vpd.c, as it parses key and value in the VPD section, add nvmem cell  with
> {
> .name=key,
> .offset=consumed,   // need some change in vpd_decodec.c to get the
> offset of value in the section.
> .bytes=value
> }
> Implement read function with vpd_section as context.
> 
> 2) In vpd.c, register an nvm_device using devm_nvmem_register in
> coreboot_driver's probe function vpd_probe.
> 
> 3) As my use case does not use device tree, it is hard for ASoC
> machine to access nvmem device. I am wondering if I can use
> nvm_cell_lookup so machine driver can find the nvmem device using a
> con_id. But currently the cell lookup API requires a matched device,
> which does not fit my usage because there will be different machine
> drivers requesting the value.
> I think I can still workaround this by adding the lookup table in
> machine driver. This would seem to be a bit weird because I found that
> most lookup table is added in provider side, not consumer side. Not
> sure if this is logically correct.

Maybe Srini has some input here. It looks like your main concern is
consumer to provider mapping?

> 
> IMO the nvmem approach would create more complexity to support this
> simple usage. Plus, the underlying assumption of accessing data with
> offset in a buffer does not fit well with the already parsed VPD
> values in a list of vpd_attrib_info. But if you strongly feel that
> this is a better approach I can work toward this.
> 

I'm not sure how an ACPI system like this would work because my exposure
to ACPI is extremely limited. I would expect there to be some sort of
firmware property indicating that an nvmem should be used and it's
provided by VPD or for firmware to parse VPD itself and put the
information into the ACPI table for this device.

Has either of those things been done? If it is a reference/property in
firmware then it should be possible to connect consumer devices like the
audio device you mention to VPD via the nvmem APIs with some changes to
the nvmem framework assuming there's an approach for nvmem in ACPI in
some "standard" way. 

I'd like to use nvmem for two reasons. First, it is a kernel framework
for reading non-volatile memories, which is fairly close to what VPD is
for. Second, it makes a standard, i.e. non-vpd/coreboot specific, API
that drivers can use to read memories. Of course in ASoC the machine
driver is already very platform specific, but the idea is to avoid
platform specific APIs so that drivers are loosely coupled with the rest
of the kernel. We shouldn't push against that goal by introducing more
platform specific APIs.
Mark Brown Oct. 9, 2019, 1:37 p.m. UTC | #8
On Tue, Oct 08, 2019 at 08:14:43AM -0700, Stephen Boyd wrote:
> Quoting Cheng-yi Chiang (2019-10-07 11:50:31)

> > IMO the nvmem approach would create more complexity to support this
> > simple usage. Plus, the underlying assumption of accessing data with
> > offset in a buffer does not fit well with the already parsed VPD
> > values in a list of vpd_attrib_info. But if you strongly feel that
> > this is a better approach I can work toward this.

> I'm not sure how an ACPI system like this would work because my exposure
> to ACPI is extremely limited. I would expect there to be some sort of
> firmware property indicating that an nvmem should be used and it's
> provided by VPD or for firmware to parse VPD itself and put the
> information into the ACPI table for this device.

I fear this is optimistic.  It's fairly idiomatic for ACPI for stuff
like this to be keyed off DMI information rather than integrated with
anything, basically once you get out of the bits that are explictly
standardized you're into board file territory.  There is the _DSD stuff
for using DT properties in ACPI but it's had limited use AFAICT.
Srinivas Kandagatla Oct. 9, 2019, 2:05 p.m. UTC | #9
On 08/10/2019 16:14, Stephen Boyd wrote:
>> 3) As my use case does not use device tree, it is hard for ASoC
>> machine to access nvmem device. I am wondering if I can use
>> nvm_cell_lookup so machine driver can find the nvmem device using a
>> con_id. But currently the cell lookup API requires a matched device,
>> which does not fit my usage because there will be different machine
>> drivers requesting the value.
>> I think I can still workaround this by adding the lookup table in
>> machine driver. This would seem to be a bit weird because I found that
>> most lookup table is added in provider side, not consumer side. Not
>> sure if this is logically correct.
> Maybe Srini has some input here. It looks like your main concern is
> consumer to provider mapping?
> 

In non-DT setup, there are various ways to lookup nvmem provider.

1> nvmem_device_get()/put() using provider devid/name. I think you 
should be able to use this in your case.
2> nvmem_register_notifier() which notifies when nvmem provider is added 
to system.
3> nvmem_device_find() with own match function this will be merged in 
next window (https://lkml.org/lkml/2019/10/3/215)


If none of these are of any help, could explain what exactly are you 
looking for w.r.t nvmem to be able to move to what Stephen Boyd suggested?

--srini
Cheng-yi Chiang Oct. 14, 2019, 3:21 a.m. UTC | #10
On Wed, Oct 9, 2019 at 10:05 PM Srinivas Kandagatla
<srinivas.kandagatla@linaro.org> wrote:
>
>
>
> On 08/10/2019 16:14, Stephen Boyd wrote:
> >> 3) As my use case does not use device tree, it is hard for ASoC
> >> machine to access nvmem device. I am wondering if I can use
> >> nvm_cell_lookup so machine driver can find the nvmem device using a
> >> con_id. But currently the cell lookup API requires a matched device,
> >> which does not fit my usage because there will be different machine
> >> drivers requesting the value.
> >> I think I can still workaround this by adding the lookup table in
> >> machine driver. This would seem to be a bit weird because I found that
> >> most lookup table is added in provider side, not consumer side. Not
> >> sure if this is logically correct.
> > Maybe Srini has some input here. It looks like your main concern is
> > consumer to provider mapping?
> >
>
> In non-DT setup, there are various ways to lookup nvmem provider.
>
> 1> nvmem_device_get()/put() using provider devid/name. I think you
> should be able to use this in your case.
> 2> nvmem_register_notifier() which notifies when nvmem provider is added
> to system.
> 3> nvmem_device_find() with own match function this will be merged in
> next window (https://lkml.org/lkml/2019/10/3/215)
>
>
> If none of these are of any help, could explain what exactly are you
> looking for w.r.t nvmem to be able to move to what Stephen Boyd suggested?
>
> --srini
>

Hi Stephen, Mark and Srinivas,
Thank you all for the suggestions.
In my non-DT setup, I have been working on coreboot changes to prepare
data in _DSD following suggestion in
https://patchwork.kernel.org/patch/11179237
The basic idea is that codec driver should just get data it needs from
device property.
The coreboot approach works in my local setup, but I have not sent the
change to coreboot upstream yet.
If that path work, then the change needed in kernel will be much simpler.

In the future, there might be DT setup use case, and I think it should
be doable for VPD to register a nvmem device, and let codec driver
gets the property.
But I would drop this path for now.
Thanks!
diff mbox series

Patch

diff --git a/drivers/firmware/google/vpd.c b/drivers/firmware/google/vpd.c
index db0812263d46..71e9d2da63be 100644
--- a/drivers/firmware/google/vpd.c
+++ b/drivers/firmware/google/vpd.c
@@ -65,6 +65,22 @@  static ssize_t vpd_attrib_read(struct file *filp, struct kobject *kobp,
 				       info->bin_attr.size);
 }
 
+int vpd_attribute_read_value(bool ro, const char *key,
+			     char **value, u32 value_len)
+{
+	struct vpd_attrib_info *info;
+	struct vpd_section *sec = ro ? &ro_vpd : &rw_vpd;
+
+	list_for_each_entry(info, &sec->attribs, list) {
+		if (strcmp(info->key, key) == 0) {
+			*value = kstrndup(info->value, value_len, GFP_KERNEL);
+			return 0;
+		}
+	}
+	return -EINVAL;
+}
+EXPORT_SYMBOL(vpd_attribute_read_value);
+
 /*
  * vpd_section_check_key_name()
  *
diff --git a/include/linux/firmware/google/google_vpd.h b/include/linux/firmware/google/google_vpd.h
new file mode 100644
index 000000000000..6f1160f28af8
--- /dev/null
+++ b/include/linux/firmware/google/google_vpd.h
@@ -0,0 +1,18 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Google VPD interface.
+ *
+ * Copyright 2019 Google Inc.
+ */
+
+/* Interface for reading VPD value on Chrome platform. */
+
+#ifndef __GOOGLE_VPD_H
+#define __GOOGLE_VPD_H
+
+#include <linux/types.h>
+
+int vpd_attribute_read_value(bool ro, const char *key,
+			     char **value, u32 value_len);
+
+#endif  /* __GOOGLE_VPD_H */