BigW Consortium Gitlab

Commit b90edecf by David Frey

Fix i2c_get/put_adapter() in mangOH modules

parent 1166d736
...@@ -116,6 +116,7 @@ static void mangoh_green_release(struct device* dev) ...@@ -116,6 +116,7 @@ static void mangoh_green_release(struct device* dev)
static int mangoh_green_probe(struct platform_device* pdev) static int mangoh_green_probe(struct platform_device* pdev)
{ {
int ret = 0;
struct i2c_adapter* adapter; struct i2c_adapter* adapter;
dev_info(&pdev->dev, "In the probe\n"); dev_info(&pdev->dev, "In the probe\n");
...@@ -134,7 +135,8 @@ static int mangoh_green_probe(struct platform_device* pdev) ...@@ -134,7 +135,8 @@ static int mangoh_green_probe(struct platform_device* pdev)
dev_err(&pdev->dev, dev_err(&pdev->dev,
"Failed to get the primary I2C adapter (%d).\n", "Failed to get the primary I2C adapter (%d).\n",
PRIMARY_I2C_BUS); PRIMARY_I2C_BUS);
return -ENODEV; ret = -ENODEV;
goto done;
} }
/* Map the I2C switch */ /* Map the I2C switch */
...@@ -146,21 +148,18 @@ static int mangoh_green_probe(struct platform_device* pdev) ...@@ -146,21 +148,18 @@ static int mangoh_green_probe(struct platform_device* pdev)
&pdev->dev, &pdev->dev,
"Failed to register %s\n", "Failed to register %s\n",
mangoh_green_pca954x_device_info.type); mangoh_green_pca954x_device_info.type);
return -ENODEV; ret = -ENODEV;
goto cleanup;
} }
/* Map the accelerometer */ /* Map the accelerometer */
dev_dbg(&pdev->dev, "mapping bmi160 accelerometer\n"); dev_dbg(&pdev->dev, "mapping bmi160 accelerometer\n");
adapter = i2c_get_adapter(PRIMARY_I2C_BUS);
if (!adapter) {
dev_err(&pdev->dev, "No I2C bus %d.\n", 0);
return -ENODEV;
}
mangoh_green_driver_data.accelerometer = mangoh_green_driver_data.accelerometer =
i2c_new_device(adapter, &mangoh_green_lsm6ds3_devinfo); i2c_new_device(adapter, &mangoh_green_lsm6ds3_devinfo);
if (!mangoh_green_driver_data.accelerometer) { if (!mangoh_green_driver_data.accelerometer) {
dev_err(&pdev->dev, "Accelerometer is missing\n"); dev_err(&pdev->dev, "Accelerometer is missing\n");
return -ENODEV; ret = -ENODEV;
goto cleanup;
} }
/* /*
...@@ -185,18 +184,19 @@ static int mangoh_green_probe(struct platform_device* pdev) ...@@ -185,18 +184,19 @@ static int mangoh_green_probe(struct platform_device* pdev)
* https://chromium.googlesource.com/chromiumos/platform/ec/+/master/driver/charger/bq24192.c * https://chromium.googlesource.com/chromiumos/platform/ec/+/master/driver/charger/bq24192.c
*/ */
return 0; cleanup:
i2c_put_adapter(adapter);
if (ret != 0)
mangoh_green_remove(pdev);
done:
return ret;
} }
static int mangoh_green_remove(struct platform_device* pdev) static int mangoh_green_remove(struct platform_device* pdev)
{ {
dev_info(&pdev->dev, "In the remove\n"); dev_info(&pdev->dev, "In the remove\n");
i2c_unregister_device(mangoh_green_driver_data.accelerometer); i2c_unregister_device(mangoh_green_driver_data.accelerometer);
i2c_put_adapter(mangoh_green_driver_data.accelerometer->adapter);
i2c_unregister_device(mangoh_green_driver_data.i2c_switch); i2c_unregister_device(mangoh_green_driver_data.i2c_switch);
i2c_put_adapter(mangoh_green_driver_data.i2c_switch->adapter);
return 0; return 0;
} }
......
...@@ -230,13 +230,13 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -230,13 +230,13 @@ static int mangoh_red_probe(struct platform_device* pdev)
#endif /* ENABLE_IOT_SLOT */ #endif /* ENABLE_IOT_SLOT */
struct gpio_chip *gpio_expander; struct gpio_chip *gpio_expander;
struct i2c_board_info *accelerometer_board_info; struct i2c_board_info *accelerometer_board_info;
struct i2c_adapter *other_adapter = NULL; struct i2c_adapter *i2c_adapter_primary, *i2c_adapter_gpio_exp = NULL,
struct i2c_adapter *main_adapter; *i2c_adapter_batt_charger = NULL;
dev_info(&pdev->dev, "%s(): probe\n", __func__); dev_info(&pdev->dev, "%s(): probe\n", __func__);
main_adapter = i2c_get_adapter(PRIMARY_I2C_BUS); i2c_adapter_primary = i2c_get_adapter(PRIMARY_I2C_BUS);
if (!main_adapter) { if (!i2c_adapter_primary) {
dev_err(&pdev->dev, "Failed to get primary I2C adapter (%d).\n", dev_err(&pdev->dev, "Failed to get primary I2C adapter (%d).\n",
PRIMARY_I2C_BUS); PRIMARY_I2C_BUS);
ret = -ENODEV; ret = -ENODEV;
...@@ -254,7 +254,7 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -254,7 +254,7 @@ static int mangoh_red_probe(struct platform_device* pdev)
/* Map the I2C switch */ /* Map the I2C switch */
dev_dbg(&pdev->dev, "mapping i2c switch\n"); dev_dbg(&pdev->dev, "mapping i2c switch\n");
mangoh_red_driver_data.i2c_switch = mangoh_red_driver_data.i2c_switch =
i2c_new_device(main_adapter, &mangoh_red_pca954x_device_info); i2c_new_device(i2c_adapter_primary, &mangoh_red_pca954x_device_info);
if (!mangoh_red_driver_data.i2c_switch) { if (!mangoh_red_driver_data.i2c_switch) {
dev_err(&pdev->dev, "Failed to register %s\n", dev_err(&pdev->dev, "Failed to register %s\n",
mangoh_red_pca954x_device_info.type); mangoh_red_pca954x_device_info.type);
...@@ -262,19 +262,22 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -262,19 +262,22 @@ static int mangoh_red_probe(struct platform_device* pdev)
goto cleanup; goto cleanup;
} }
/* Map the GPIO expander */ /* Get other i2c adapters required for probe */
dev_dbg(&pdev->dev, "mapping gpio expander\n"); i2c_adapter_gpio_exp =
other_adapter = i2c_get_adapter(MANGOH_RED_I2C_BUS_GPIO_EXPANDER); i2c_get_adapter(MANGOH_RED_I2C_BUS_GPIO_EXPANDER);
if (!other_adapter) { i2c_adapter_batt_charger =
i2c_get_adapter(MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
if (!i2c_adapter_gpio_exp || !i2c_adapter_batt_charger) {
dev_err(&pdev->dev, dev_err(&pdev->dev,
"Couldn't get I2C bus %d to add the GPIO expander.\n", "Couldn't get necessary I2C buses downstream of I2C switch\n");
MANGOH_RED_I2C_BUS_GPIO_EXPANDER);
ret = -ENODEV; ret = -ENODEV;
goto cleanup; goto cleanup;
} }
/* Map the GPIO expander */
dev_dbg(&pdev->dev, "mapping gpio expander\n");
mangoh_red_driver_data.gpio_expander = mangoh_red_driver_data.gpio_expander =
i2c_new_device(other_adapter, &mangoh_red_gpio_expander_devinfo); i2c_new_device(i2c_adapter_gpio_exp, &mangoh_red_gpio_expander_devinfo);
i2c_put_adapter(other_adapter);
if (!mangoh_red_driver_data.gpio_expander) { if (!mangoh_red_driver_data.gpio_expander) {
dev_err(&pdev->dev, "Failed to register %s\n", dev_err(&pdev->dev, "Failed to register %s\n",
mangoh_red_gpio_expander_devinfo.type); mangoh_red_gpio_expander_devinfo.type);
...@@ -324,7 +327,7 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -324,7 +327,7 @@ static int mangoh_red_probe(struct platform_device* pdev)
mangoh_red_pdata.board_rev == MANGOH_RED_BOARD_REV_DV2 ? mangoh_red_pdata.board_rev == MANGOH_RED_BOARD_REV_DV2 ?
&mangoh_red_lsm6ds3_devinfo : &mangoh_red_bmi160_devinfo; &mangoh_red_lsm6ds3_devinfo : &mangoh_red_bmi160_devinfo;
mangoh_red_driver_data.accelerometer = mangoh_red_driver_data.accelerometer =
i2c_new_device(main_adapter, accelerometer_board_info); i2c_new_device(i2c_adapter_primary, accelerometer_board_info);
if (!mangoh_red_driver_data.accelerometer) { if (!mangoh_red_driver_data.accelerometer) {
dev_err(&pdev->dev, "Accelerometer is missing\n"); dev_err(&pdev->dev, "Accelerometer is missing\n");
return -ENODEV; return -ENODEV;
...@@ -333,7 +336,7 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -333,7 +336,7 @@ static int mangoh_red_probe(struct platform_device* pdev)
/* Map the I2C BMP280 pressure sensor */ /* Map the I2C BMP280 pressure sensor */
dev_dbg(&pdev->dev, "mapping bmp280 pressure sensor\n"); dev_dbg(&pdev->dev, "mapping bmp280 pressure sensor\n");
mangoh_red_driver_data.pressure = mangoh_red_driver_data.pressure =
i2c_new_device(main_adapter, &mangoh_red_pressure_devinfo); i2c_new_device(i2c_adapter_primary, &mangoh_red_pressure_devinfo);
if (!mangoh_red_driver_data.pressure) { if (!mangoh_red_driver_data.pressure) {
dev_err(&pdev->dev, "Pressure sensor is missing\n"); dev_err(&pdev->dev, "Pressure sensor is missing\n");
return -ENODEV; return -ENODEV;
...@@ -341,42 +344,26 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -341,42 +344,26 @@ static int mangoh_red_probe(struct platform_device* pdev)
/* Map the I2C BQ24296 driver: for now use the BQ24190 driver code */ /* Map the I2C BQ24296 driver: for now use the BQ24190 driver code */
dev_dbg(&pdev->dev, "mapping bq24296 driver\n"); dev_dbg(&pdev->dev, "mapping bq24296 driver\n");
other_adapter = i2c_get_adapter(MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
if(!other_adapter) {
dev_err(&pdev->dev, "No I2C bus %d.\n",
MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
ret = -ENODEV;
goto cleanup;
}
mangoh_red_driver_data.battery_charger = i2c_new_device( mangoh_red_driver_data.battery_charger = i2c_new_device(
other_adapter, &mangoh_red_battery_charger_devinfo); i2c_adapter_batt_charger, &mangoh_red_battery_charger_devinfo);
i2c_put_adapter(other_adapter);
if (!mangoh_red_driver_data.battery_charger) { if (!mangoh_red_driver_data.battery_charger) {
dev_err(&pdev->dev, "battery charger is missing\n"); dev_err(&pdev->dev, "battery charger is missing\n");
ret = -ENODEV; ret = -ENODEV;
goto cleanup; goto cleanup;
} }
if (mangoh_red_pdata.board_rev != MANGOH_RED_BOARD_REV_DV3) { if (mangoh_red_pdata.board_rev != MANGOH_RED_BOARD_REV_DV3) {
/* Map the I2C ltc2942 battery gauge */ /* Map the I2C ltc2942 battery gauge */
dev_dbg(&pdev->dev, "mapping ltc2942 battery gauge\n"); dev_dbg(&pdev->dev, "mapping ltc2942 battery gauge\n");
other_adapter =
i2c_get_adapter(MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
if (!other_adapter) {
dev_err(&pdev->dev, "No I2C bus %d.\n",
MANGOH_RED_I2C_BUS_BATTERY_CHARGER);
ret = -ENODEV;
goto cleanup;
}
mangoh_red_driver_data.battery_gauge = i2c_new_device( mangoh_red_driver_data.battery_gauge = i2c_new_device(
other_adapter, &mangoh_red_battery_gauge_devinfo); i2c_adapter_batt_charger,
i2c_put_adapter(other_adapter); &mangoh_red_battery_gauge_devinfo);
if (!mangoh_red_driver_data.battery_gauge) { if (!mangoh_red_driver_data.battery_gauge) {
dev_err(&pdev->dev, "battery gauge is missing\n"); dev_err(&pdev->dev, "battery gauge is missing\n");
ret = -ENODEV; ret = -ENODEV;
goto cleanup; goto cleanup;
} }
} }
/* /*
* TODO: * TODO:
* 3503 USB Hub: 0x08 * 3503 USB Hub: 0x08
...@@ -386,21 +373,15 @@ static int mangoh_red_probe(struct platform_device* pdev) ...@@ -386,21 +373,15 @@ static int mangoh_red_probe(struct platform_device* pdev)
*/ */
cleanup: cleanup:
i2c_put_adapter(main_adapter); i2c_put_adapter(i2c_adapter_primary);
i2c_put_adapter(i2c_adapter_gpio_exp);
i2c_put_adapter(i2c_adapter_batt_charger);
if (ret != 0) if (ret != 0)
mangoh_red_remove(pdev); mangoh_red_remove(pdev);
done: done:
return ret; return ret;
} }
static void try_unregister_i2c_device(struct i2c_client *client)
{
if (client != NULL) {
i2c_unregister_device(client);
i2c_put_adapter(client->adapter);
}
}
static int mangoh_red_remove(struct platform_device* pdev) static int mangoh_red_remove(struct platform_device* pdev)
{ {
struct mangoh_red_driver_data *dd = platform_get_drvdata(pdev); struct mangoh_red_driver_data *dd = platform_get_drvdata(pdev);
...@@ -408,11 +389,11 @@ static int mangoh_red_remove(struct platform_device* pdev) ...@@ -408,11 +389,11 @@ static int mangoh_red_remove(struct platform_device* pdev)
dev_info(&pdev->dev, "Removing mangoh red platform device\n"); dev_info(&pdev->dev, "Removing mangoh red platform device\n");
if (mangoh_red_pdata.board_rev != MANGOH_RED_BOARD_REV_DV3) if (mangoh_red_pdata.board_rev != MANGOH_RED_BOARD_REV_DV3)
try_unregister_i2c_device(dd->battery_gauge); i2c_unregister_device(dd->battery_gauge);
try_unregister_i2c_device(dd->battery_charger); i2c_unregister_device(dd->battery_charger);
try_unregister_i2c_device(dd->pressure); i2c_unregister_device(dd->pressure);
try_unregister_i2c_device(dd->accelerometer); i2c_unregister_device(dd->accelerometer);
if (dd->led_registered) if (dd->led_registered)
platform_device_unregister(&mangoh_red_led); platform_device_unregister(&mangoh_red_led);
...@@ -425,8 +406,8 @@ static int mangoh_red_remove(struct platform_device* pdev) ...@@ -425,8 +406,8 @@ static int mangoh_red_remove(struct platform_device* pdev)
mangoh_red_mux_deinit(); mangoh_red_mux_deinit();
#endif /* ENABLE_IOT_SLOT */ #endif /* ENABLE_IOT_SLOT */
try_unregister_i2c_device(dd->gpio_expander); i2c_unregister_device(dd->gpio_expander);
try_unregister_i2c_device(dd->i2c_switch); i2c_unregister_device(dd->i2c_switch);
return 0; return 0;
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
*----------------------------------------------------------------------------- *-----------------------------------------------------------------------------
*/ */
#define MANGOH_YELLOW_I2C_SW_BUS_BASE (PRIMARY_I2C_BUS + 1) #define MANGOH_YELLOW_I2C_SW_BUS_BASE (PRIMARY_I2C_BUS + 1)
#define MANGOH_YELLOW_I2C_BUS_IOT0 (MANGOH_YELLOW_I2C_SW_BUS_BASE + 0) #define MANGOH_YELLOW_I2C_BUS_IOT0 (MANGOH_YELLOW_I2C_SW_BUS_BASE + 0)
#define MANGOH_YELLOW_I2C_BUS_BATTERY_CHARGER (MANGOH_YELLOW_I2C_SW_BUS_BASE + 1) #define MANGOH_YELLOW_I2C_BUS_BATTERY_CHARGER (MANGOH_YELLOW_I2C_SW_BUS_BASE + 1)
#define MANGOH_YELLOW_I2C_BUS_USB_HUB (MANGOH_YELLOW_I2C_SW_BUS_BASE + 1) #define MANGOH_YELLOW_I2C_BUS_USB_HUB (MANGOH_YELLOW_I2C_SW_BUS_BASE + 1)
#define MANGOH_YELLOW_I2C_BUS_GPIO_EXPANDER (MANGOH_YELLOW_I2C_SW_BUS_BASE + 2) #define MANGOH_YELLOW_I2C_BUS_GPIO_EXPANDER (MANGOH_YELLOW_I2C_SW_BUS_BASE + 2)
...@@ -62,9 +62,9 @@ static struct mangoh_yellow_platform_data { ...@@ -62,9 +62,9 @@ static struct mangoh_yellow_platform_data {
} mangoh_yellow_pdata; } mangoh_yellow_pdata;
static struct mangoh_yellow_driver_data { static struct mangoh_yellow_driver_data {
struct i2c_client* bme680; struct i2c_client* bme680;
struct i2c_client* bmi088a; struct i2c_client* bmi088a;
struct i2c_client* bmi088g; struct i2c_client* bmi088g;
} mangoh_yellow_driver_data = { } mangoh_yellow_driver_data = {
}; };
...@@ -97,12 +97,12 @@ static void mangoh_yellow_release(struct device* dev) ...@@ -97,12 +97,12 @@ static void mangoh_yellow_release(struct device* dev)
static int mangoh_yellow_probe(struct platform_device* pdev) static int mangoh_yellow_probe(struct platform_device* pdev)
{ {
int ret = 0; int ret = 0;
struct i2c_adapter *main_adapter; struct i2c_adapter *i2c_adapter_primary;
dev_info(&pdev->dev, "%s(): probe\n", __func__); dev_info(&pdev->dev, "%s(): probe\n", __func__);
main_adapter = i2c_get_adapter(PRIMARY_I2C_BUS); i2c_adapter_primary = i2c_get_adapter(PRIMARY_I2C_BUS);
if (!main_adapter) { if (!i2c_adapter_primary) {
dev_err(&pdev->dev, "Failed to get primary I2C adapter (%d).\n", dev_err(&pdev->dev, "Failed to get primary I2C adapter (%d).\n",
PRIMARY_I2C_BUS); PRIMARY_I2C_BUS);
ret = -ENODEV; ret = -ENODEV;
...@@ -117,62 +117,51 @@ static int mangoh_yellow_probe(struct platform_device* pdev) ...@@ -117,62 +117,51 @@ static int mangoh_yellow_probe(struct platform_device* pdev)
platform_set_drvdata(pdev, &mangoh_yellow_driver_data); platform_set_drvdata(pdev, &mangoh_yellow_driver_data);
/* Map the I2C BME680 humidity/gas/temp/pressure sensor */ /* Map the I2C BME680 humidity/gas/temp/pressure sensor */
dev_dbg(&pdev->dev, "mapping bme680 gas/temperature/pressure sensor\n"); dev_dbg(&pdev->dev, "mapping bme680 gas/temperature/pressure sensor\n");
mangoh_yellow_driver_data.bme680 = mangoh_yellow_driver_data.bme680 =
i2c_new_device(main_adapter, &mangoh_yellow_bme680_devinfo); i2c_new_device(i2c_adapter_primary, &mangoh_yellow_bme680_devinfo);
i2c_put_adapter(main_adapter);
if (!mangoh_yellow_driver_data.bme680) { if (!mangoh_yellow_driver_data.bme680) {
dev_err(&pdev->dev, "Gas/Humidity/Temp sensor is missing\n"); dev_err(&pdev->dev, "Gas/Humidity/Temp sensor is missing\n");
ret = -ENODEV; ret = -ENODEV;
goto cleanup; goto cleanup;
} }
/* Map the I2C BMI088 gyro/accel sensor */ /* Map the I2C BMI088 gyro/accel sensor */
dev_dbg(&pdev->dev, "mapping bmi088 gyro/accel sensor\n"); dev_dbg(&pdev->dev, "mapping bmi088 gyro/accel sensor\n");
mangoh_yellow_driver_data.bmi088a = mangoh_yellow_driver_data.bmi088a =
i2c_new_device(main_adapter, &mangoh_yellow_bmi088a_devinfo); i2c_new_device(i2c_adapter_primary, &mangoh_yellow_bmi088a_devinfo);
i2c_put_adapter(main_adapter);
if (!mangoh_yellow_driver_data.bmi088a) { if (!mangoh_yellow_driver_data.bmi088a) {
dev_err(&pdev->dev, "bmi088 accel sensor is missing\n"); dev_err(&pdev->dev, "bmi088 accel sensor is missing\n");
ret = -ENODEV; ret = -ENODEV;
goto cleanup; goto cleanup;
} }
mangoh_yellow_driver_data.bmi088g = mangoh_yellow_driver_data.bmi088g =
i2c_new_device(main_adapter, &mangoh_yellow_bmi088g_devinfo); i2c_new_device(i2c_adapter_primary, &mangoh_yellow_bmi088g_devinfo);
i2c_put_adapter(main_adapter);
if (!mangoh_yellow_driver_data.bmi088g) { if (!mangoh_yellow_driver_data.bmi088g) {
dev_err(&pdev->dev, "bmi088 gyro sensor is missing\n"); dev_err(&pdev->dev, "bmi088 gyro sensor is missing\n");
ret = -ENODEV; ret = -ENODEV;
goto cleanup; goto cleanup;
} }
cleanup: cleanup:
i2c_put_adapter(main_adapter); i2c_put_adapter(i2c_adapter_primary);
if (ret != 0) if (ret != 0)
mangoh_yellow_remove(pdev); mangoh_yellow_remove(pdev);
done: done:
return ret; return ret;
} }
static void try_unregister_i2c_device(struct i2c_client *client)
{
if (client != NULL) {
i2c_unregister_device(client);
i2c_put_adapter(client->adapter);
}
}
static int mangoh_yellow_remove(struct platform_device* pdev) static int mangoh_yellow_remove(struct platform_device* pdev)
{ {
struct mangoh_yellow_driver_data *dd = platform_get_drvdata(pdev); struct mangoh_yellow_driver_data *dd = platform_get_drvdata(pdev);
dev_info(&pdev->dev, "Removing mangoh red platform device\n"); dev_info(&pdev->dev, "Removing mangoh red platform device\n");
try_unregister_i2c_device(dd->bmi088a); i2c_unregister_device(dd->bmi088a);
try_unregister_i2c_device(dd->bmi088g); i2c_unregister_device(dd->bmi088g);
try_unregister_i2c_device(dd->bme680); i2c_unregister_device(dd->bme680);
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment