power: supply: cpcap-charger: get the battery inserted infomation from cpcap-battery
authorCarl Philipp Klemm <philipp@uvos.xyz>
Fri, 23 Apr 2021 13:00:57 +0000 (15:00 +0200)
committerSebastian Reichel <sre@kernel.org>
Fri, 4 Jun 2021 12:36:17 +0000 (14:36 +0200)
This avoids reimplementing the detection logic twice and removes the
possibility of activating charging with 500mA even if a battery is not
detected.

Signed-off-by: Carl Philipp Klemm <philipp@uvos.xyz>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
drivers/power/supply/cpcap-charger.c

index df01abc49ce8fd6dd1006c41c6abf7378cfb9db5..60e0ce105a295428ff8f76b4696dbc8bd18968ed 100644 (file)
@@ -173,23 +173,6 @@ static enum power_supply_property cpcap_charger_props[] = {
        POWER_SUPPLY_PROP_CURRENT_NOW,
 };
 
-/* No battery always shows temperature of -40000 */
-static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
-{
-       struct iio_channel *channel;
-       int error, temperature;
-
-       channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
-       error = iio_read_channel_processed(channel, &temperature);
-       if (error < 0) {
-               dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
-
-               return false;
-       }
-
-       return temperature > -20000 && temperature < 60000;
-}
-
 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
 {
        struct iio_channel *channel;
@@ -700,11 +683,29 @@ static void cpcap_usb_detect(struct work_struct *work)
 
        if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
            s.chrgcurr1) {
-               int max_current = 532000;
+               int max_current;
                int vchrg, ichrg;
+               union power_supply_propval val;
+               struct power_supply *battery;
 
-               if (cpcap_charger_battery_found(ddata))
+               battery = power_supply_get_by_name("battery");
+               if (IS_ERR_OR_NULL(battery)) {
+                       dev_err(ddata->dev, "battery power_supply not available %li\n",
+                                       PTR_ERR(battery));
+                       return;
+               }
+
+               error = power_supply_get_property(battery, POWER_SUPPLY_PROP_PRESENT, &val);
+               power_supply_put(battery);
+               if (error)
+                       goto out_err;
+
+               if (val.intval) {
                        max_current = 1596000;
+               } else {
+                       dev_info(ddata->dev, "battery not inserted, charging disabled\n");
+                       max_current = 0;
+               }
 
                if (max_current > ddata->limit_current)
                        max_current = ddata->limit_current;