Skip to contentSkip to conversation

input: mma8450: Add chip ID check in probe

PATCH Superseded Frank Li 2 participants 2 messages Dec 16, 2024 – Dec 17, 2024

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Frank Li

author maintainer
#0 lore raw

PATCH v2 1/1 input: mma8450: Add chip ID check in probe

From: Luwei Zhou <[email protected]>

Prevent continuous polling error logs by adding a chip ID check in the probe function. This ensures the driver only proceeds when the mma8450 is present, avoiding issues in scenarios like missing add-on cards.

---
change from v1 to v2
- Use *adapter = c->adapter
- Use if (!i2c_check_functionality())

---
 drivers/input/misc/mma8450.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/input/misc/mma8450.c +12 -1
index 08412239b8e69..a1adb49fb5f33 100644
@@ -38,6 +38,8 @@
 
 #define MMA8450_CTRL_REG1	0x38
 #define MMA8450_CTRL_REG2	0x39
+#define MMA8450_ID		0xc6
+#define MMA8450_WHO_AM_I	0x0f
 
 static int mma8450_read(struct i2c_client *c, unsigned int off)
 {
@@ -148,8 +150,17 @@ static void mma8450_close(struct input_dev *input)
  */
 static int mma8450_probe(struct i2c_client *c)
 {
+	struct i2c_adapter *adapter = c->adapter;
 	struct input_dev *input;
-	int err;
+	int err, client_id;
+
+	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA))
+		return dev_err_probe(&c->dev, -EINVAL, "I2C adapter can't support SMBUS BYTE");
+
+	client_id = i2c_smbus_read_byte_data(c, MMA8450_WHO_AM_I);
+	if (client_id != MMA8450_ID)
+		return dev_err_probe(&c->dev, -EINVAL, "read chip ID 0x%x is not equal to 0x%x!\n",
+				     client_id, MMA8450_ID);
 
 	input = devm_input_allocate_device(&c->dev);
 	if (!input)
Signature · 1 line
2.34.1
#1 lore raw

Hello,

Quoted 38 lines · Frank Li

From: Luwei Zhou <[email protected]>

Prevent continuous polling error logs by adding a chip ID check in the probe function. This ensures the driver only proceeds when the mma8450 is present, avoiding issues in scenarios like missing add-on cards.

---
change from v1 to v2
- Use *adapter = c->adapter
- Use if (!i2c_check_functionality())

---
 drivers/input/misc/mma8450.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/input/misc/mma8450.c +3 -0
index 08412239b8e69..a1adb49fb5f33 100644
@@ -38,6 +38,8 @@
 
 #define MMA8450_CTRL_REG1	0x38
 #define MMA8450_CTRL_REG2	0x39
+#define MMA8450_ID		0xc6
+#define MMA8450_WHO_AM_I	0x0f
 
 static int mma8450_read(struct i2c_client *c, unsigned int off)
 {
@@ -148,8 +150,17 @@ static void mma8450_close(struct input_dev *input)
  */
 static int mma8450_probe(struct i2c_client *c)
 {
+	struct i2c_adapter *adapter = c->adapter;
 	struct input_dev *input;

drivers/input/misc/mma8450.c static void mma8450_close(struct input_dev *input) · line 163

Quoted 10 lines
drivers/input/misc/mma8450.c +9 -1
-	int err;
+	int err, client_id;
+
+	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_BYTE_DATA))
+		return dev_err_probe(&c->dev, -EINVAL, "I2C adapter can't support SMBUS BYTE");
+
+	client_id = i2c_smbus_read_byte_data(c, MMA8450_WHO_AM_I);
+	if (client_id != MMA8450_ID)
+		return dev_err_probe(&c->dev, -EINVAL, "read chip ID 0x%x is not equal to 0x%x!\n",
+				     client_id, MMA8450_ID);

i2c_smbus_read_byte_data() might return a negative errno. So the right handling is:

	client_id = i2c_smbus_read_byte_data(c, MMA8450_WHO_AM_I);
	if (client_id < 0)
		return dev_err_probe(&c->dev, client_id, "Failed to read chip ID\n");
	if (client_id != MMA8450_ID)
		return dev_err_probe(&c->dev, -EINVAL, "read chip ID 0x%x is not equal to 0x%x!\n",
				     client_id, MMA8450_ID);

Best regards
Uwe