Re: [PATCH v4] iio: accel: adxl345: Add ACPI HID table

From: Kai-Heng Feng
Date: Thu Feb 17 2022 - 22:46:30 EST


On Thu, Feb 17, 2022 at 6:57 PM Andy Shevchenko
<andy.shevchenko@xxxxxxxxx> wrote:
>
>
>
> On Thursday, February 17, 2022, Kai-Heng Feng <kai.heng.feng@xxxxxxxxxxxxx> wrote:
>>
>> x86 boards may use ACPI HID "ADS0345" for adxl345 device.
>>
>> Analog replied:
>> "ADS034X is not a valid PNP ID. ADS0345 would be.
>> I'm not aware that this ID is already taken.
>> Feel free to submit a mainline Linux input mailing list patch."
>>
>> So add an ACPI match table for that accordingly.
>>
>> Since ACPI device may not match to any I2C ID, use the name and type
>> directly from ACPI ID table in absence of I2C ID.
>>
>> Suggested-by: Andy Shevchenko <andy.shevchenko@xxxxxxxxx>
>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@xxxxxxxxxxxxx>
>> ---
>> v4:
>> - Drop "ADS0345:00" and use driver_date from ACPI table directly.
>>
>> v3:
>> - Convert the driver from input to iio.
>>
>> v2:
>> - Drop ACPI_PTR()
>> - Drop redundant empty line and comma
>> - Add info from vendor
>>
>> drivers/iio/accel/adxl345_i2c.c | 29 ++++++++++++++++++++++++-----
>> 1 file changed, 24 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/iio/accel/adxl345_i2c.c b/drivers/iio/accel/adxl345_i2c.c
>> index a431cba216e69..cdcc3ef1f1d33 100644
>> --- a/drivers/iio/accel/adxl345_i2c.c
>> +++ b/drivers/iio/accel/adxl345_i2c.c
>> @@ -22,20 +22,32 @@ static const struct regmap_config adxl345_i2c_regmap_config = {
>> static int adxl345_i2c_probe(struct i2c_client *client,
>> const struct i2c_device_id *id)
>> {
>> + struct device *dev = &client->dev;
>> + const struct acpi_device_id *acpi_id;
>> + enum adxl345_device_type type;
>> + const char *name;
>> struct regmap *regmap;
>>
>> - if (!id)
>> - return -ENODEV;
>> + if (id) {
>> + type = id->driver_data;
>> + name = id->name;
>> + } else {
>> + acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
>> + if (acpi_id) {
>> + type = acpi_id->driver_data;
>> + name = acpi_id->id;
>> + } else
>> + return -ENODEV;
>> + }
>>
>
>
> Thanks, but can we do this in ACPI agnostic way?
>
> Can be as simple as
>
> if (id)
> ...
> else {
> match = device_get_match_data(dev);
> if (!match)
> return -ENODEV;
> }
>
> Note, it might require to reconsider what is put in the driver data (either convert to pointers, or be sure that valid type is never a 0/NULL).

Unlike acpi_match_device(), device_get_match_data() only get
driver_data, so we need a new struct to provide both name and type.

>
> Also note, in both cases using ID name for name us fragile. Probably we have to fix that first. Let me check today’s evening.

Can you please explain more on this? How does ID name make it fragile?

Kai-Heng

>
>>
>> regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
>> if (IS_ERR(regmap)) {
>> - dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
>> + dev_err(dev, "Error initializing i2c regmap: %ld\n",
>> PTR_ERR(regmap));
>> return PTR_ERR(regmap);
>> }
>>
>> - return adxl345_core_probe(&client->dev, regmap, id->driver_data,
>> - id->name);
>> + return adxl345_core_probe(&client->dev, regmap, type, name);
>> }
>>
>> static const struct i2c_device_id adxl345_i2c_id[] = {
>> @@ -54,10 +66,17 @@ static const struct of_device_id adxl345_of_match[] = {
>>
>> MODULE_DEVICE_TABLE(of, adxl345_of_match);
>>
>> +static const struct acpi_device_id adxl345_acpi_match[] = {
>> + { "ADS0345", ADXL345 },
>> + { }
>> +};
>> +MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
>> +
>> static struct i2c_driver adxl345_i2c_driver = {
>> .driver = {
>> .name = "adxl345_i2c",
>> .of_match_table = adxl345_of_match,
>> + .acpi_match_table = adxl345_acpi_match,
>> },
>> .probe = adxl345_i2c_probe,
>> .id_table = adxl345_i2c_id,
>> --
>> 2.34.1
>>
>
>
> --
> With Best Regards,
> Andy Shevchenko
>
>