iio: humidity: hdc100x: add manufacturer and device ID check
v3 cover iio: humidity: hdc100x: add manufacturer and device ID check
In this patch series, we move the callback from probe to probe_new, and add manufacturer and device ID check for hdc100x driver.
changes v1 --> v2:
- fix typo in commit message
- change to use probe_new
- use device_get_match_data instead of i2c_of_match_device
changes v2 --> v3:
- move probe_new part into a separate patch
- remove unsed variable
- remove redundant checking of matched data
Potin Lai (2):
iio: humidity: hdc100x: switch to probe_new callback
iio: humidity: hdc100x: add manufacturer and device ID check
drivers/iio/humidity/hdc100x.c | 75 +++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 14 deletions(-)
Signature · 1 line
2.31.1
PATCH v3 1/2 iio: humidity: hdc100x: switch to probe_new callback
Switch to probe_new callback due to probe is deprecated soon.
--- drivers/iio/humidity/hdc100x.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/iio/humidity/hdc100x.c +2 -3
@@ -351,8 +351,7 @@ static const struct iio_info hdc100x_info = { .attrs = &hdc100x_attribute_group, }; -static int hdc100x_probe (struct i2c_client *client, -const struct i2c_device_id *id) +static int hdc100x_probe (struct i2c_client *client) {struct iio_dev *indio_dev;struct hdc100x_data *data;
@@ -422,7 +421,7 @@ static struct i2c_driver hdc100x_driver = { .name = "hdc100x", .of_match_table = hdc100x_dt_ids, }, - .probe = hdc100x_probe, + .probe_new = hdc100x_probe, .id_table = hdc100x_id, };module_i2c_driver (hdc100x_driver);
Signature · 1 line
2.31.1
Re: [PATCH v3 1/2] iio: humidity: hdc100x: switch to probe_new callback
Quoted 2 lines · Potin Lai
Switch to
probe_newcallback due to probe is deprecated soon.
Quoted 31 lines
Signed-off-by Potin Lai <[email protected]>--- drivers/iio/humidity/hdc100x.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)drivers/iio/humidity/hdc100x.c +2
-4@@ -351,8 +351,7 @@ static const struct iio_info hdc100x_info = { .attrs = &hdc100x_attribute_group, }; -static int hdc100x_probe (struct i2c_client *client, -const struct i2c_device_id *id) +static int hdc100x_probe (struct i2c_client *client) {struct iio_dev *indio_dev;struct hdc100x_data *data;@@ -422,7 +421,7 @@ static struct i2c_driver hdc100x_driver = { .name = "hdc100x", .of_match_table = hdc100x_dt_ids, }, - .probe = hdc100x_probe, + .probe_new = hdc100x_probe, .id_table = hdc100x_id, };module_i2c_driver (hdc100x_driver); --2.31.1
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v3 2/2 iio: humidity: hdc100x: add manufacturer and device ID check
In reply to Potin LaiAdd manufacturer and device ID checking during probe, and skip the checking if chip model not supported.
Supported:
- HDC1000
- HDC1010
- HDC1050
- HDC1080
Not supported:
- HDC1008
--- drivers/iio/humidity/hdc100x.c | 70 ++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 11 deletions(-)
drivers/iio/humidity/hdc100x.c +59 -11
@@ -34,6 +34,23 @@#define HDC100X_REG_CONFIG_ACQ_MODE BIT(12)#define HDC100X_REG_CONFIG_HEATER_EN BIT(13) +#define HDC100X_REG_MFR_ID 0xFE +#define HDC100X_REG_DEV_ID 0xFF + +#define HDC100X_MFR_ID 0x5449 + +struct hdc100x_chip_data { +bool support_mfr_check; +}; + +static const struct hdc100x_chip_data hdc100x_chip_data = { + .support_mfr_check = true, +}; + +static const struct hdc100x_chip_data hdc1008_chip_data = { + .support_mfr_check = false, +}; +struct hdc100x_data {struct i2c_client *client;struct mutex lock;
@@ -351,6 +368,34 @@ static const struct iio_info hdc100x_info = { .attrs = &hdc100x_attribute_group, }; +static int hdc100x_read_mfr_id (struct i2c_client *client) +{ +return i2c_smbus_read_word_swapped (client,HDC100X_REG_MFR_ID ); +} + +static int hdc100x_read_dev_id (struct i2c_client *client) +{ +return i2c_smbus_read_word_swapped (client,HDC100X_REG_DEV_ID ); +} + +static bool is_valid_hdc100x (struct i2c_client *client) +{ +const struct hdc100x_chip_data *data; +int mfr_id, dev_id; + + data =device_get_match_data (&client->dev); +if (!data->support_mfr_check) +return true; + + mfr_id =hdc100x_read_mfr_id (client); + dev_id =hdc100x_read_dev_id (client); +if (mfr_id ==HDC100X_MFR_ID && + (dev_id ==0x1000 || dev_id ==0x1050 )) +return true; + +return false; +} +static int hdc100x_probe (struct i2c_client *client) {struct iio_dev *indio_dev;
@@ -361,6 +406,9 @@ static int hdc100x_probe(struct i2c_client *client) I2C_FUNC_SMBUS_BYTE | I2C_FUNC_I2C))return -EOPNOTSUPP ; +if (!is_valid_hdc100x (client)) +return -EINVAL ; + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data));if (!indio_dev)return -ENOMEM ;
@@ -396,22 +444,22 @@ static int hdc100x_probe(struct i2c_client *client) }static const struct i2c_device_id hdc100x_id[] = { - {"hdc100x" ,0 }, - {"hdc1000" ,0 }, - {"hdc1008" ,0 }, - {"hdc1010" ,0 }, - {"hdc1050" ,0 }, - {"hdc1080" ,0 }, + {"hdc100X" , (kernel_ulong_t)&hdc100x_chip_data }, + {"hdc1000" , (kernel_ulong_t)&hdc100x_chip_data }, + {"hdc1008" , (kernel_ulong_t)&hdc1008_chip_data }, + {"hdc1010" , (kernel_ulong_t)&hdc100x_chip_data }, + {"hdc1050" , (kernel_ulong_t)&hdc100x_chip_data }, + {"hdc1080" , (kernel_ulong_t)&hdc100x_chip_data }, { } };MODULE_DEVICE_TABLE (i2c, hdc100x_id);static const struct of_device_id hdc100x_dt_ids[] = { - { .compatible ="ti,hdc1000" }, - { .compatible ="ti,hdc1008" }, - { .compatible ="ti,hdc1010" }, - { .compatible ="ti,hdc1050" }, - { .compatible ="ti,hdc1080" }, + { .compatible ="ti,hdc1000" , .data = &hdc100x_chip_data }, + { .compatible ="ti,hdc1008" , .data = &hdc1008_chip_data }, + { .compatible ="ti,hdc1010" , .data = &hdc100x_chip_data }, + { .compatible ="ti,hdc1050" , .data = &hdc100x_chip_data }, + { .compatible ="ti,hdc1080" , .data = &hdc100x_chip_data }, { } };MODULE_DEVICE_TABLE (of, hdc100x_dt_ids);
Signature · 1 line
2.31.1
Quoted 4 lines · Potin Lai
Add manufacturer and device ID checking during probe, and skip the checking if chip model not supported.
Quoted 8 lines
Supported:
- HDC1000
- HDC1010
- HDC1050
- HDC1080Not supported:
- HDC1008
Almost there!
...
drivers/iio/humidity/hdc100x.c static const struct iio_info hdc100x_info = { · line 388
Quoted 8 lines
drivers/iio/humidity/hdc100x.c +7
-0+static bool is_valid_hdc100x (struct i2c_client *client) +{ +const struct hdc100x_chip_data *data; +int mfr_id, dev_id; + data =device_get_match_data (&client->dev); +if (!data->support_mfr_check) +return true;
Logically this part does belong to ->probe() and doesn't here (too wide for this specific helper).
Quoted 8 lines
drivers/iio/humidity/hdc100x.c +8
-0+ mfr_id =hdc100x_read_mfr_id (client); + dev_id =hdc100x_read_dev_id (client); +if (mfr_id ==HDC100X_MFR_ID && + (dev_id ==0x1000 || dev_id ==0x1050 )) +return true; + +return false; +}
...
drivers/iio/humidity/hdc100x.c static int hdc100x_probe(struct i2c_client *client) · line 410
Quoted 2 lines
drivers/iio/humidity/hdc100x.c +2
-0+if (!is_valid_hdc100x (client)) +return -EINVAL ;
Means here you add
const struct hdc100x_chip_data *data;
struct device *dev = &client->dev;
...
data = device_get_match_data(dev);
if (data->support_mfr_check && !is_valid_hdc100x(client))
return -EINVAL;(Introducing a temporary variable for struct device pointer might also help in future to refactor to make code neater.)
Signature · 2 lines
With Best Regards, Andy Shevchenko