Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Universal power supply monitor class
0004  *
0005  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
0006  *  Copyright © 2004  Szabolcs Gyurko
0007  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
0008  *
0009  *  Modified: 2004, Oct     Szabolcs Gyurko
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/types.h>
0014 #include <linux/init.h>
0015 #include <linux/slab.h>
0016 #include <linux/delay.h>
0017 #include <linux/device.h>
0018 #include <linux/notifier.h>
0019 #include <linux/err.h>
0020 #include <linux/of.h>
0021 #include <linux/power_supply.h>
0022 #include <linux/property.h>
0023 #include <linux/thermal.h>
0024 #include <linux/fixp-arith.h>
0025 #include "power_supply.h"
0026 #include "samsung-sdi-battery.h"
0027 
0028 /* exported for the APM Power driver, APM emulation */
0029 struct class *power_supply_class;
0030 EXPORT_SYMBOL_GPL(power_supply_class);
0031 
0032 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
0033 EXPORT_SYMBOL_GPL(power_supply_notifier);
0034 
0035 static struct device_type power_supply_dev_type;
0036 
0037 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
0038 
0039 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
0040                      struct power_supply *supply)
0041 {
0042     int i;
0043 
0044     if (!supply->supplied_from && !supplier->supplied_to)
0045         return false;
0046 
0047     /* Support both supplied_to and supplied_from modes */
0048     if (supply->supplied_from) {
0049         if (!supplier->desc->name)
0050             return false;
0051         for (i = 0; i < supply->num_supplies; i++)
0052             if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
0053                 return true;
0054     } else {
0055         if (!supply->desc->name)
0056             return false;
0057         for (i = 0; i < supplier->num_supplicants; i++)
0058             if (!strcmp(supplier->supplied_to[i], supply->desc->name))
0059                 return true;
0060     }
0061 
0062     return false;
0063 }
0064 
0065 static int __power_supply_changed_work(struct device *dev, void *data)
0066 {
0067     struct power_supply *psy = data;
0068     struct power_supply *pst = dev_get_drvdata(dev);
0069 
0070     if (__power_supply_is_supplied_by(psy, pst)) {
0071         if (pst->desc->external_power_changed)
0072             pst->desc->external_power_changed(pst);
0073     }
0074 
0075     return 0;
0076 }
0077 
0078 static void power_supply_changed_work(struct work_struct *work)
0079 {
0080     unsigned long flags;
0081     struct power_supply *psy = container_of(work, struct power_supply,
0082                         changed_work);
0083 
0084     dev_dbg(&psy->dev, "%s\n", __func__);
0085 
0086     spin_lock_irqsave(&psy->changed_lock, flags);
0087     /*
0088      * Check 'changed' here to avoid issues due to race between
0089      * power_supply_changed() and this routine. In worst case
0090      * power_supply_changed() can be called again just before we take above
0091      * lock. During the first call of this routine we will mark 'changed' as
0092      * false and it will stay false for the next call as well.
0093      */
0094     if (likely(psy->changed)) {
0095         psy->changed = false;
0096         spin_unlock_irqrestore(&psy->changed_lock, flags);
0097         class_for_each_device(power_supply_class, NULL, psy,
0098                       __power_supply_changed_work);
0099         power_supply_update_leds(psy);
0100         atomic_notifier_call_chain(&power_supply_notifier,
0101                 PSY_EVENT_PROP_CHANGED, psy);
0102         kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
0103         spin_lock_irqsave(&psy->changed_lock, flags);
0104     }
0105 
0106     /*
0107      * Hold the wakeup_source until all events are processed.
0108      * power_supply_changed() might have called again and have set 'changed'
0109      * to true.
0110      */
0111     if (likely(!psy->changed))
0112         pm_relax(&psy->dev);
0113     spin_unlock_irqrestore(&psy->changed_lock, flags);
0114 }
0115 
0116 void power_supply_changed(struct power_supply *psy)
0117 {
0118     unsigned long flags;
0119 
0120     dev_dbg(&psy->dev, "%s\n", __func__);
0121 
0122     spin_lock_irqsave(&psy->changed_lock, flags);
0123     psy->changed = true;
0124     pm_stay_awake(&psy->dev);
0125     spin_unlock_irqrestore(&psy->changed_lock, flags);
0126     schedule_work(&psy->changed_work);
0127 }
0128 EXPORT_SYMBOL_GPL(power_supply_changed);
0129 
0130 /*
0131  * Notify that power supply was registered after parent finished the probing.
0132  *
0133  * Often power supply is registered from driver's probe function. However
0134  * calling power_supply_changed() directly from power_supply_register()
0135  * would lead to execution of get_property() function provided by the driver
0136  * too early - before the probe ends.
0137  *
0138  * Avoid that by waiting on parent's mutex.
0139  */
0140 static void power_supply_deferred_register_work(struct work_struct *work)
0141 {
0142     struct power_supply *psy = container_of(work, struct power_supply,
0143                         deferred_register_work.work);
0144 
0145     if (psy->dev.parent) {
0146         while (!mutex_trylock(&psy->dev.parent->mutex)) {
0147             if (psy->removing)
0148                 return;
0149             msleep(10);
0150         }
0151     }
0152 
0153     power_supply_changed(psy);
0154 
0155     if (psy->dev.parent)
0156         mutex_unlock(&psy->dev.parent->mutex);
0157 }
0158 
0159 #ifdef CONFIG_OF
0160 static int __power_supply_populate_supplied_from(struct device *dev,
0161                          void *data)
0162 {
0163     struct power_supply *psy = data;
0164     struct power_supply *epsy = dev_get_drvdata(dev);
0165     struct device_node *np;
0166     int i = 0;
0167 
0168     do {
0169         np = of_parse_phandle(psy->of_node, "power-supplies", i++);
0170         if (!np)
0171             break;
0172 
0173         if (np == epsy->of_node) {
0174             dev_dbg(&psy->dev, "%s: Found supply : %s\n",
0175                 psy->desc->name, epsy->desc->name);
0176             psy->supplied_from[i-1] = (char *)epsy->desc->name;
0177             psy->num_supplies++;
0178             of_node_put(np);
0179             break;
0180         }
0181         of_node_put(np);
0182     } while (np);
0183 
0184     return 0;
0185 }
0186 
0187 static int power_supply_populate_supplied_from(struct power_supply *psy)
0188 {
0189     int error;
0190 
0191     error = class_for_each_device(power_supply_class, NULL, psy,
0192                       __power_supply_populate_supplied_from);
0193 
0194     dev_dbg(&psy->dev, "%s %d\n", __func__, error);
0195 
0196     return error;
0197 }
0198 
0199 static int  __power_supply_find_supply_from_node(struct device *dev,
0200                          void *data)
0201 {
0202     struct device_node *np = data;
0203     struct power_supply *epsy = dev_get_drvdata(dev);
0204 
0205     /* returning non-zero breaks out of class_for_each_device loop */
0206     if (epsy->of_node == np)
0207         return 1;
0208 
0209     return 0;
0210 }
0211 
0212 static int power_supply_find_supply_from_node(struct device_node *supply_node)
0213 {
0214     int error;
0215 
0216     /*
0217      * class_for_each_device() either returns its own errors or values
0218      * returned by __power_supply_find_supply_from_node().
0219      *
0220      * __power_supply_find_supply_from_node() will return 0 (no match)
0221      * or 1 (match).
0222      *
0223      * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
0224      * it returned 0, or error as returned by it.
0225      */
0226     error = class_for_each_device(power_supply_class, NULL, supply_node,
0227                        __power_supply_find_supply_from_node);
0228 
0229     return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
0230 }
0231 
0232 static int power_supply_check_supplies(struct power_supply *psy)
0233 {
0234     struct device_node *np;
0235     int cnt = 0;
0236 
0237     /* If there is already a list honor it */
0238     if (psy->supplied_from && psy->num_supplies > 0)
0239         return 0;
0240 
0241     /* No device node found, nothing to do */
0242     if (!psy->of_node)
0243         return 0;
0244 
0245     do {
0246         int ret;
0247 
0248         np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
0249         if (!np)
0250             break;
0251 
0252         ret = power_supply_find_supply_from_node(np);
0253         of_node_put(np);
0254 
0255         if (ret) {
0256             dev_dbg(&psy->dev, "Failed to find supply!\n");
0257             return ret;
0258         }
0259     } while (np);
0260 
0261     /* Missing valid "power-supplies" entries */
0262     if (cnt == 1)
0263         return 0;
0264 
0265     /* All supplies found, allocate char ** array for filling */
0266     psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(*psy->supplied_from),
0267                       GFP_KERNEL);
0268     if (!psy->supplied_from)
0269         return -ENOMEM;
0270 
0271     *psy->supplied_from = devm_kcalloc(&psy->dev,
0272                        cnt - 1, sizeof(**psy->supplied_from),
0273                        GFP_KERNEL);
0274     if (!*psy->supplied_from)
0275         return -ENOMEM;
0276 
0277     return power_supply_populate_supplied_from(psy);
0278 }
0279 #else
0280 static int power_supply_check_supplies(struct power_supply *psy)
0281 {
0282     int nval, ret;
0283 
0284     if (!psy->dev.parent)
0285         return 0;
0286 
0287     nval = device_property_string_array_count(psy->dev.parent, "supplied-from");
0288     if (nval <= 0)
0289         return 0;
0290 
0291     psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
0292                         sizeof(char *), GFP_KERNEL);
0293     if (!psy->supplied_from)
0294         return -ENOMEM;
0295 
0296     ret = device_property_read_string_array(psy->dev.parent,
0297         "supplied-from", (const char **)psy->supplied_from, nval);
0298     if (ret < 0)
0299         return ret;
0300 
0301     psy->num_supplies = nval;
0302 
0303     return 0;
0304 }
0305 #endif
0306 
0307 struct psy_am_i_supplied_data {
0308     struct power_supply *psy;
0309     unsigned int count;
0310 };
0311 
0312 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
0313 {
0314     union power_supply_propval ret = {0,};
0315     struct power_supply *epsy = dev_get_drvdata(dev);
0316     struct psy_am_i_supplied_data *data = _data;
0317 
0318     if (__power_supply_is_supplied_by(epsy, data->psy)) {
0319         data->count++;
0320         if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
0321                     &ret))
0322             return ret.intval;
0323     }
0324 
0325     return 0;
0326 }
0327 
0328 int power_supply_am_i_supplied(struct power_supply *psy)
0329 {
0330     struct psy_am_i_supplied_data data = { psy, 0 };
0331     int error;
0332 
0333     error = class_for_each_device(power_supply_class, NULL, &data,
0334                       __power_supply_am_i_supplied);
0335 
0336     dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
0337 
0338     if (data.count == 0)
0339         return -ENODEV;
0340 
0341     return error;
0342 }
0343 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
0344 
0345 static int __power_supply_is_system_supplied(struct device *dev, void *data)
0346 {
0347     union power_supply_propval ret = {0,};
0348     struct power_supply *psy = dev_get_drvdata(dev);
0349     unsigned int *count = data;
0350 
0351     (*count)++;
0352     if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
0353         if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
0354                     &ret))
0355             return ret.intval;
0356 
0357     return 0;
0358 }
0359 
0360 int power_supply_is_system_supplied(void)
0361 {
0362     int error;
0363     unsigned int count = 0;
0364 
0365     error = class_for_each_device(power_supply_class, NULL, &count,
0366                       __power_supply_is_system_supplied);
0367 
0368     /*
0369      * If no power class device was found at all, most probably we are
0370      * running on a desktop system, so assume we are on mains power.
0371      */
0372     if (count == 0)
0373         return 1;
0374 
0375     return error;
0376 }
0377 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
0378 
0379 struct psy_get_supplier_prop_data {
0380     struct power_supply *psy;
0381     enum power_supply_property psp;
0382     union power_supply_propval *val;
0383 };
0384 
0385 static int __power_supply_get_supplier_property(struct device *dev, void *_data)
0386 {
0387     struct power_supply *epsy = dev_get_drvdata(dev);
0388     struct psy_get_supplier_prop_data *data = _data;
0389 
0390     if (__power_supply_is_supplied_by(epsy, data->psy))
0391         if (!epsy->desc->get_property(epsy, data->psp, data->val))
0392             return 1; /* Success */
0393 
0394     return 0; /* Continue iterating */
0395 }
0396 
0397 int power_supply_get_property_from_supplier(struct power_supply *psy,
0398                         enum power_supply_property psp,
0399                         union power_supply_propval *val)
0400 {
0401     struct psy_get_supplier_prop_data data = {
0402         .psy = psy,
0403         .psp = psp,
0404         .val = val,
0405     };
0406     int ret;
0407 
0408     /*
0409      * This function is not intended for use with a supply with multiple
0410      * suppliers, we simply pick the first supply to report the psp.
0411      */
0412     ret = class_for_each_device(power_supply_class, NULL, &data,
0413                     __power_supply_get_supplier_property);
0414     if (ret < 0)
0415         return ret;
0416     if (ret == 0)
0417         return -ENODEV;
0418 
0419     return 0;
0420 }
0421 EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier);
0422 
0423 int power_supply_set_battery_charged(struct power_supply *psy)
0424 {
0425     if (atomic_read(&psy->use_cnt) >= 0 &&
0426             psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
0427             psy->desc->set_charged) {
0428         psy->desc->set_charged(psy);
0429         return 0;
0430     }
0431 
0432     return -EINVAL;
0433 }
0434 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
0435 
0436 static int power_supply_match_device_by_name(struct device *dev, const void *data)
0437 {
0438     const char *name = data;
0439     struct power_supply *psy = dev_get_drvdata(dev);
0440 
0441     return strcmp(psy->desc->name, name) == 0;
0442 }
0443 
0444 /**
0445  * power_supply_get_by_name() - Search for a power supply and returns its ref
0446  * @name: Power supply name to fetch
0447  *
0448  * If power supply was found, it increases reference count for the
0449  * internal power supply's device. The user should power_supply_put()
0450  * after usage.
0451  *
0452  * Return: On success returns a reference to a power supply with
0453  * matching name equals to @name, a NULL otherwise.
0454  */
0455 struct power_supply *power_supply_get_by_name(const char *name)
0456 {
0457     struct power_supply *psy = NULL;
0458     struct device *dev = class_find_device(power_supply_class, NULL, name,
0459                     power_supply_match_device_by_name);
0460 
0461     if (dev) {
0462         psy = dev_get_drvdata(dev);
0463         atomic_inc(&psy->use_cnt);
0464     }
0465 
0466     return psy;
0467 }
0468 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
0469 
0470 /**
0471  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
0472  * @psy: Reference to put
0473  *
0474  * The reference to power supply should be put before unregistering
0475  * the power supply.
0476  */
0477 void power_supply_put(struct power_supply *psy)
0478 {
0479     might_sleep();
0480 
0481     atomic_dec(&psy->use_cnt);
0482     put_device(&psy->dev);
0483 }
0484 EXPORT_SYMBOL_GPL(power_supply_put);
0485 
0486 #ifdef CONFIG_OF
0487 static int power_supply_match_device_node(struct device *dev, const void *data)
0488 {
0489     return dev->parent && dev->parent->of_node == data;
0490 }
0491 
0492 /**
0493  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
0494  * @np: Pointer to device node holding phandle property
0495  * @property: Name of property holding a power supply name
0496  *
0497  * If power supply was found, it increases reference count for the
0498  * internal power supply's device. The user should power_supply_put()
0499  * after usage.
0500  *
0501  * Return: On success returns a reference to a power supply with
0502  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
0503  */
0504 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
0505                             const char *property)
0506 {
0507     struct device_node *power_supply_np;
0508     struct power_supply *psy = NULL;
0509     struct device *dev;
0510 
0511     power_supply_np = of_parse_phandle(np, property, 0);
0512     if (!power_supply_np)
0513         return ERR_PTR(-ENODEV);
0514 
0515     dev = class_find_device(power_supply_class, NULL, power_supply_np,
0516                         power_supply_match_device_node);
0517 
0518     of_node_put(power_supply_np);
0519 
0520     if (dev) {
0521         psy = dev_get_drvdata(dev);
0522         atomic_inc(&psy->use_cnt);
0523     }
0524 
0525     return psy;
0526 }
0527 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
0528 
0529 static void devm_power_supply_put(struct device *dev, void *res)
0530 {
0531     struct power_supply **psy = res;
0532 
0533     power_supply_put(*psy);
0534 }
0535 
0536 /**
0537  * devm_power_supply_get_by_phandle() - Resource managed version of
0538  *  power_supply_get_by_phandle()
0539  * @dev: Pointer to device holding phandle property
0540  * @property: Name of property holding a power supply phandle
0541  *
0542  * Return: On success returns a reference to a power supply with
0543  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
0544  */
0545 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
0546                               const char *property)
0547 {
0548     struct power_supply **ptr, *psy;
0549 
0550     if (!dev->of_node)
0551         return ERR_PTR(-ENODEV);
0552 
0553     ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
0554     if (!ptr)
0555         return ERR_PTR(-ENOMEM);
0556 
0557     psy = power_supply_get_by_phandle(dev->of_node, property);
0558     if (IS_ERR_OR_NULL(psy)) {
0559         devres_free(ptr);
0560     } else {
0561         *ptr = psy;
0562         devres_add(dev, ptr);
0563     }
0564     return psy;
0565 }
0566 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
0567 #endif /* CONFIG_OF */
0568 
0569 int power_supply_get_battery_info(struct power_supply *psy,
0570                   struct power_supply_battery_info **info_out)
0571 {
0572     struct power_supply_resistance_temp_table *resist_table;
0573     struct power_supply_battery_info *info;
0574     struct device_node *battery_np = NULL;
0575     struct fwnode_reference_args args;
0576     struct fwnode_handle *fwnode;
0577     const char *value;
0578     int err, len, index;
0579     const __be32 *list;
0580     u32 min_max[2];
0581 
0582     if (psy->of_node) {
0583         battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
0584         if (!battery_np)
0585             return -ENODEV;
0586 
0587         fwnode = fwnode_handle_get(of_fwnode_handle(battery_np));
0588     } else {
0589         err = fwnode_property_get_reference_args(
0590                     dev_fwnode(psy->dev.parent),
0591                     "monitored-battery", NULL, 0, 0, &args);
0592         if (err)
0593             return err;
0594 
0595         fwnode = args.fwnode;
0596     }
0597 
0598     err = fwnode_property_read_string(fwnode, "compatible", &value);
0599     if (err)
0600         goto out_put_node;
0601 
0602 
0603     /* Try static batteries first */
0604     err = samsung_sdi_battery_get_info(&psy->dev, value, &info);
0605     if (!err)
0606         goto out_ret_pointer;
0607     else if (err == -ENODEV)
0608         /*
0609          * Device does not have a static battery.
0610          * Proceed to look for a simple battery.
0611          */
0612         err = 0;
0613 
0614     if (strcmp("simple-battery", value)) {
0615         err = -ENODEV;
0616         goto out_put_node;
0617     }
0618 
0619     info = devm_kzalloc(&psy->dev, sizeof(*info), GFP_KERNEL);
0620     if (!info) {
0621         err = -ENOMEM;
0622         goto out_put_node;
0623     }
0624 
0625     info->technology                     = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
0626     info->energy_full_design_uwh         = -EINVAL;
0627     info->charge_full_design_uah         = -EINVAL;
0628     info->voltage_min_design_uv          = -EINVAL;
0629     info->voltage_max_design_uv          = -EINVAL;
0630     info->precharge_current_ua           = -EINVAL;
0631     info->charge_term_current_ua         = -EINVAL;
0632     info->constant_charge_current_max_ua = -EINVAL;
0633     info->constant_charge_voltage_max_uv = -EINVAL;
0634     info->tricklecharge_current_ua       = -EINVAL;
0635     info->precharge_voltage_max_uv       = -EINVAL;
0636     info->charge_restart_voltage_uv      = -EINVAL;
0637     info->overvoltage_limit_uv           = -EINVAL;
0638     info->maintenance_charge             = NULL;
0639     info->alert_low_temp_charge_current_ua = -EINVAL;
0640     info->alert_low_temp_charge_voltage_uv = -EINVAL;
0641     info->alert_high_temp_charge_current_ua = -EINVAL;
0642     info->alert_high_temp_charge_voltage_uv = -EINVAL;
0643     info->temp_ambient_alert_min         = INT_MIN;
0644     info->temp_ambient_alert_max         = INT_MAX;
0645     info->temp_alert_min                 = INT_MIN;
0646     info->temp_alert_max                 = INT_MAX;
0647     info->temp_min                       = INT_MIN;
0648     info->temp_max                       = INT_MAX;
0649     info->factory_internal_resistance_uohm  = -EINVAL;
0650     info->resist_table                   = NULL;
0651     info->bti_resistance_ohm             = -EINVAL;
0652     info->bti_resistance_tolerance       = -EINVAL;
0653 
0654     for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
0655         info->ocv_table[index]       = NULL;
0656         info->ocv_temp[index]        = -EINVAL;
0657         info->ocv_table_size[index]  = -EINVAL;
0658     }
0659 
0660     /* The property and field names below must correspond to elements
0661      * in enum power_supply_property. For reasoning, see
0662      * Documentation/power/power_supply_class.rst.
0663      */
0664 
0665     if (!fwnode_property_read_string(fwnode, "device-chemistry", &value)) {
0666         if (!strcmp("nickel-cadmium", value))
0667             info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
0668         else if (!strcmp("nickel-metal-hydride", value))
0669             info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
0670         else if (!strcmp("lithium-ion", value))
0671             /* Imprecise lithium-ion type */
0672             info->technology = POWER_SUPPLY_TECHNOLOGY_LION;
0673         else if (!strcmp("lithium-ion-polymer", value))
0674             info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
0675         else if (!strcmp("lithium-ion-iron-phosphate", value))
0676             info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe;
0677         else if (!strcmp("lithium-ion-manganese-oxide", value))
0678             info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn;
0679         else
0680             dev_warn(&psy->dev, "%s unknown battery type\n", value);
0681     }
0682 
0683     fwnode_property_read_u32(fwnode, "energy-full-design-microwatt-hours",
0684                  &info->energy_full_design_uwh);
0685     fwnode_property_read_u32(fwnode, "charge-full-design-microamp-hours",
0686                  &info->charge_full_design_uah);
0687     fwnode_property_read_u32(fwnode, "voltage-min-design-microvolt",
0688                  &info->voltage_min_design_uv);
0689     fwnode_property_read_u32(fwnode, "voltage-max-design-microvolt",
0690                  &info->voltage_max_design_uv);
0691     fwnode_property_read_u32(fwnode, "trickle-charge-current-microamp",
0692                  &info->tricklecharge_current_ua);
0693     fwnode_property_read_u32(fwnode, "precharge-current-microamp",
0694                  &info->precharge_current_ua);
0695     fwnode_property_read_u32(fwnode, "precharge-upper-limit-microvolt",
0696                  &info->precharge_voltage_max_uv);
0697     fwnode_property_read_u32(fwnode, "charge-term-current-microamp",
0698                  &info->charge_term_current_ua);
0699     fwnode_property_read_u32(fwnode, "re-charge-voltage-microvolt",
0700                  &info->charge_restart_voltage_uv);
0701     fwnode_property_read_u32(fwnode, "over-voltage-threshold-microvolt",
0702                  &info->overvoltage_limit_uv);
0703     fwnode_property_read_u32(fwnode, "constant-charge-current-max-microamp",
0704                  &info->constant_charge_current_max_ua);
0705     fwnode_property_read_u32(fwnode, "constant-charge-voltage-max-microvolt",
0706                  &info->constant_charge_voltage_max_uv);
0707     fwnode_property_read_u32(fwnode, "factory-internal-resistance-micro-ohms",
0708                  &info->factory_internal_resistance_uohm);
0709 
0710     if (!fwnode_property_read_u32_array(fwnode, "ambient-celsius",
0711                         min_max, ARRAY_SIZE(min_max))) {
0712         info->temp_ambient_alert_min = min_max[0];
0713         info->temp_ambient_alert_max = min_max[1];
0714     }
0715     if (!fwnode_property_read_u32_array(fwnode, "alert-celsius",
0716                         min_max, ARRAY_SIZE(min_max))) {
0717         info->temp_alert_min = min_max[0];
0718         info->temp_alert_max = min_max[1];
0719     }
0720     if (!fwnode_property_read_u32_array(fwnode, "operating-range-celsius",
0721                         min_max, ARRAY_SIZE(min_max))) {
0722         info->temp_min = min_max[0];
0723         info->temp_max = min_max[1];
0724     }
0725 
0726     /*
0727      * The below code uses raw of-data parsing to parse
0728      * /schemas/types.yaml#/definitions/uint32-matrix
0729      * data, so for now this is only support with of.
0730      */
0731     if (!battery_np)
0732         goto out_ret_pointer;
0733 
0734     len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
0735     if (len < 0 && len != -EINVAL) {
0736         err = len;
0737         goto out_put_node;
0738     } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
0739         dev_err(&psy->dev, "Too many temperature values\n");
0740         err = -EINVAL;
0741         goto out_put_node;
0742     } else if (len > 0) {
0743         of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
0744                        info->ocv_temp, len);
0745     }
0746 
0747     for (index = 0; index < len; index++) {
0748         struct power_supply_battery_ocv_table *table;
0749         char *propname;
0750         int i, tab_len, size;
0751 
0752         propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
0753         list = of_get_property(battery_np, propname, &size);
0754         if (!list || !size) {
0755             dev_err(&psy->dev, "failed to get %s\n", propname);
0756             kfree(propname);
0757             power_supply_put_battery_info(psy, info);
0758             err = -EINVAL;
0759             goto out_put_node;
0760         }
0761 
0762         kfree(propname);
0763         tab_len = size / (2 * sizeof(__be32));
0764         info->ocv_table_size[index] = tab_len;
0765 
0766         table = info->ocv_table[index] =
0767             devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
0768         if (!info->ocv_table[index]) {
0769             power_supply_put_battery_info(psy, info);
0770             err = -ENOMEM;
0771             goto out_put_node;
0772         }
0773 
0774         for (i = 0; i < tab_len; i++) {
0775             table[i].ocv = be32_to_cpu(*list);
0776             list++;
0777             table[i].capacity = be32_to_cpu(*list);
0778             list++;
0779         }
0780     }
0781 
0782     list = of_get_property(battery_np, "resistance-temp-table", &len);
0783     if (!list || !len)
0784         goto out_ret_pointer;
0785 
0786     info->resist_table_size = len / (2 * sizeof(__be32));
0787     resist_table = info->resist_table = devm_kcalloc(&psy->dev,
0788                              info->resist_table_size,
0789                              sizeof(*resist_table),
0790                              GFP_KERNEL);
0791     if (!info->resist_table) {
0792         power_supply_put_battery_info(psy, info);
0793         err = -ENOMEM;
0794         goto out_put_node;
0795     }
0796 
0797     for (index = 0; index < info->resist_table_size; index++) {
0798         resist_table[index].temp = be32_to_cpu(*list++);
0799         resist_table[index].resistance = be32_to_cpu(*list++);
0800     }
0801 
0802 out_ret_pointer:
0803     /* Finally return the whole thing */
0804     *info_out = info;
0805 
0806 out_put_node:
0807     fwnode_handle_put(fwnode);
0808     of_node_put(battery_np);
0809     return err;
0810 }
0811 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
0812 
0813 void power_supply_put_battery_info(struct power_supply *psy,
0814                    struct power_supply_battery_info *info)
0815 {
0816     int i;
0817 
0818     for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
0819         if (info->ocv_table[i])
0820             devm_kfree(&psy->dev, info->ocv_table[i]);
0821     }
0822 
0823     if (info->resist_table)
0824         devm_kfree(&psy->dev, info->resist_table);
0825 
0826     devm_kfree(&psy->dev, info);
0827 }
0828 EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
0829 
0830 /**
0831  * power_supply_temp2resist_simple() - find the battery internal resistance
0832  * percent from temperature
0833  * @table: Pointer to battery resistance temperature table
0834  * @table_len: The table length
0835  * @temp: Current temperature
0836  *
0837  * This helper function is used to look up battery internal resistance percent
0838  * according to current temperature value from the resistance temperature table,
0839  * and the table must be ordered descending. Then the actual battery internal
0840  * resistance = the ideal battery internal resistance * percent / 100.
0841  *
0842  * Return: the battery internal resistance percent
0843  */
0844 int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
0845                     int table_len, int temp)
0846 {
0847     int i, high, low;
0848 
0849     for (i = 0; i < table_len; i++)
0850         if (temp > table[i].temp)
0851             break;
0852 
0853     /* The library function will deal with high == low */
0854     if (i == 0)
0855         high = low = i;
0856     else if (i == table_len)
0857         high = low = i - 1;
0858     else
0859         high = (low = i) - 1;
0860 
0861     return fixp_linear_interpolate(table[low].temp,
0862                        table[low].resistance,
0863                        table[high].temp,
0864                        table[high].resistance,
0865                        temp);
0866 }
0867 EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
0868 
0869 /**
0870  * power_supply_vbat2ri() - find the battery internal resistance
0871  * from the battery voltage
0872  * @info: The battery information container
0873  * @table: Pointer to battery resistance temperature table
0874  * @vbat_uv: The battery voltage in microvolt
0875  * @charging: If we are charging (true) or not (false)
0876  *
0877  * This helper function is used to look up battery internal resistance
0878  * according to current battery voltage. Depending on whether the battery
0879  * is currently charging or not, different resistance will be returned.
0880  *
0881  * Returns the internal resistance in microohm or negative error code.
0882  */
0883 int power_supply_vbat2ri(struct power_supply_battery_info *info,
0884              int vbat_uv, bool charging)
0885 {
0886     struct power_supply_vbat_ri_table *vbat2ri;
0887     int table_len;
0888     int i, high, low;
0889 
0890     /*
0891      * If we are charging, and the battery supplies a separate table
0892      * for this state, we use that in order to compensate for the
0893      * charging voltage. Otherwise we use the main table.
0894      */
0895     if (charging && info->vbat2ri_charging) {
0896         vbat2ri = info->vbat2ri_charging;
0897         table_len = info->vbat2ri_charging_size;
0898     } else {
0899         vbat2ri = info->vbat2ri_discharging;
0900         table_len = info->vbat2ri_discharging_size;
0901     }
0902 
0903     /*
0904      * If no tables are specified, or if we are above the highest voltage in
0905      * the voltage table, just return the factory specified internal resistance.
0906      */
0907     if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) {
0908         if (charging && (info->factory_internal_resistance_charging_uohm > 0))
0909             return info->factory_internal_resistance_charging_uohm;
0910         else
0911             return info->factory_internal_resistance_uohm;
0912     }
0913 
0914     /* Break loop at table_len - 1 because that is the highest index */
0915     for (i = 0; i < table_len - 1; i++)
0916         if (vbat_uv > vbat2ri[i].vbat_uv)
0917             break;
0918 
0919     /* The library function will deal with high == low */
0920     if ((i == 0) || (i == (table_len - 1)))
0921         high = i;
0922     else
0923         high = i - 1;
0924     low = i;
0925 
0926     return fixp_linear_interpolate(vbat2ri[low].vbat_uv,
0927                        vbat2ri[low].ri_uohm,
0928                        vbat2ri[high].vbat_uv,
0929                        vbat2ri[high].ri_uohm,
0930                        vbat_uv);
0931 }
0932 EXPORT_SYMBOL_GPL(power_supply_vbat2ri);
0933 
0934 struct power_supply_maintenance_charge_table *
0935 power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info,
0936                           int index)
0937 {
0938     if (index >= info->maintenance_charge_size)
0939         return NULL;
0940     return &info->maintenance_charge[index];
0941 }
0942 EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting);
0943 
0944 /**
0945  * power_supply_ocv2cap_simple() - find the battery capacity
0946  * @table: Pointer to battery OCV lookup table
0947  * @table_len: OCV table length
0948  * @ocv: Current OCV value
0949  *
0950  * This helper function is used to look up battery capacity according to
0951  * current OCV value from one OCV table, and the OCV table must be ordered
0952  * descending.
0953  *
0954  * Return: the battery capacity.
0955  */
0956 int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
0957                 int table_len, int ocv)
0958 {
0959     int i, high, low;
0960 
0961     for (i = 0; i < table_len; i++)
0962         if (ocv > table[i].ocv)
0963             break;
0964 
0965     /* The library function will deal with high == low */
0966     if (i == 0)
0967         high = low = i;
0968     else if (i == table_len)
0969         high = low = i - 1;
0970     else
0971         high = (low = i) - 1;
0972 
0973     return fixp_linear_interpolate(table[low].ocv,
0974                        table[low].capacity,
0975                        table[high].ocv,
0976                        table[high].capacity,
0977                        ocv);
0978 }
0979 EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
0980 
0981 struct power_supply_battery_ocv_table *
0982 power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
0983                 int temp, int *table_len)
0984 {
0985     int best_temp_diff = INT_MAX, temp_diff;
0986     u8 i, best_index = 0;
0987 
0988     if (!info->ocv_table[0])
0989         return NULL;
0990 
0991     for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
0992         /* Out of capacity tables */
0993         if (!info->ocv_table[i])
0994             break;
0995 
0996         temp_diff = abs(info->ocv_temp[i] - temp);
0997 
0998         if (temp_diff < best_temp_diff) {
0999             best_temp_diff = temp_diff;
1000             best_index = i;
1001         }
1002     }
1003 
1004     *table_len = info->ocv_table_size[best_index];
1005     return info->ocv_table[best_index];
1006 }
1007 EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
1008 
1009 int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
1010                  int ocv, int temp)
1011 {
1012     struct power_supply_battery_ocv_table *table;
1013     int table_len;
1014 
1015     table = power_supply_find_ocv2cap_table(info, temp, &table_len);
1016     if (!table)
1017         return -EINVAL;
1018 
1019     return power_supply_ocv2cap_simple(table, table_len, ocv);
1020 }
1021 EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
1022 
1023 bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info,
1024                        int resistance)
1025 {
1026     int low, high;
1027 
1028     /* Nothing like this can be checked */
1029     if (info->bti_resistance_ohm <= 0)
1030         return false;
1031 
1032     /* This will be extremely strict and unlikely to work */
1033     if (info->bti_resistance_tolerance <= 0)
1034         return (info->bti_resistance_ohm == resistance);
1035 
1036     low = info->bti_resistance_ohm -
1037         (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1038     high = info->bti_resistance_ohm +
1039         (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100;
1040 
1041     return ((resistance >= low) && (resistance <= high));
1042 }
1043 EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range);
1044 
1045 int power_supply_get_property(struct power_supply *psy,
1046                 enum power_supply_property psp,
1047                 union power_supply_propval *val)
1048 {
1049     if (atomic_read(&psy->use_cnt) <= 0) {
1050         if (!psy->initialized)
1051             return -EAGAIN;
1052         return -ENODEV;
1053     }
1054 
1055     return psy->desc->get_property(psy, psp, val);
1056 }
1057 EXPORT_SYMBOL_GPL(power_supply_get_property);
1058 
1059 int power_supply_set_property(struct power_supply *psy,
1060                 enum power_supply_property psp,
1061                 const union power_supply_propval *val)
1062 {
1063     if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
1064         return -ENODEV;
1065 
1066     return psy->desc->set_property(psy, psp, val);
1067 }
1068 EXPORT_SYMBOL_GPL(power_supply_set_property);
1069 
1070 int power_supply_property_is_writeable(struct power_supply *psy,
1071                     enum power_supply_property psp)
1072 {
1073     if (atomic_read(&psy->use_cnt) <= 0 ||
1074             !psy->desc->property_is_writeable)
1075         return -ENODEV;
1076 
1077     return psy->desc->property_is_writeable(psy, psp);
1078 }
1079 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
1080 
1081 void power_supply_external_power_changed(struct power_supply *psy)
1082 {
1083     if (atomic_read(&psy->use_cnt) <= 0 ||
1084             !psy->desc->external_power_changed)
1085         return;
1086 
1087     psy->desc->external_power_changed(psy);
1088 }
1089 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
1090 
1091 int power_supply_powers(struct power_supply *psy, struct device *dev)
1092 {
1093     return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
1094 }
1095 EXPORT_SYMBOL_GPL(power_supply_powers);
1096 
1097 static void power_supply_dev_release(struct device *dev)
1098 {
1099     struct power_supply *psy = to_power_supply(dev);
1100     dev_dbg(dev, "%s\n", __func__);
1101     kfree(psy);
1102 }
1103 
1104 int power_supply_reg_notifier(struct notifier_block *nb)
1105 {
1106     return atomic_notifier_chain_register(&power_supply_notifier, nb);
1107 }
1108 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
1109 
1110 void power_supply_unreg_notifier(struct notifier_block *nb)
1111 {
1112     atomic_notifier_chain_unregister(&power_supply_notifier, nb);
1113 }
1114 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
1115 
1116 static bool psy_has_property(const struct power_supply_desc *psy_desc,
1117                  enum power_supply_property psp)
1118 {
1119     bool found = false;
1120     int i;
1121 
1122     for (i = 0; i < psy_desc->num_properties; i++) {
1123         if (psy_desc->properties[i] == psp) {
1124             found = true;
1125             break;
1126         }
1127     }
1128 
1129     return found;
1130 }
1131 
1132 #ifdef CONFIG_THERMAL
1133 static int power_supply_read_temp(struct thermal_zone_device *tzd,
1134         int *temp)
1135 {
1136     struct power_supply *psy;
1137     union power_supply_propval val;
1138     int ret;
1139 
1140     WARN_ON(tzd == NULL);
1141     psy = tzd->devdata;
1142     ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
1143     if (ret)
1144         return ret;
1145 
1146     /* Convert tenths of degree Celsius to milli degree Celsius. */
1147     *temp = val.intval * 100;
1148 
1149     return ret;
1150 }
1151 
1152 static struct thermal_zone_device_ops psy_tzd_ops = {
1153     .get_temp = power_supply_read_temp,
1154 };
1155 
1156 static int psy_register_thermal(struct power_supply *psy)
1157 {
1158     int ret;
1159 
1160     if (psy->desc->no_thermal)
1161         return 0;
1162 
1163     /* Register battery zone device psy reports temperature */
1164     if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) {
1165         psy->tzd = thermal_zone_device_register(psy->desc->name,
1166                 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
1167         if (IS_ERR(psy->tzd))
1168             return PTR_ERR(psy->tzd);
1169         ret = thermal_zone_device_enable(psy->tzd);
1170         if (ret)
1171             thermal_zone_device_unregister(psy->tzd);
1172         return ret;
1173     }
1174 
1175     return 0;
1176 }
1177 
1178 static void psy_unregister_thermal(struct power_supply *psy)
1179 {
1180     if (IS_ERR_OR_NULL(psy->tzd))
1181         return;
1182     thermal_zone_device_unregister(psy->tzd);
1183 }
1184 
1185 /* thermal cooling device callbacks */
1186 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
1187                     unsigned long *state)
1188 {
1189     struct power_supply *psy;
1190     union power_supply_propval val;
1191     int ret;
1192 
1193     psy = tcd->devdata;
1194     ret = power_supply_get_property(psy,
1195             POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
1196     if (ret)
1197         return ret;
1198 
1199     *state = val.intval;
1200 
1201     return ret;
1202 }
1203 
1204 static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1205                     unsigned long *state)
1206 {
1207     struct power_supply *psy;
1208     union power_supply_propval val;
1209     int ret;
1210 
1211     psy = tcd->devdata;
1212     ret = power_supply_get_property(psy,
1213             POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1214     if (ret)
1215         return ret;
1216 
1217     *state = val.intval;
1218 
1219     return ret;
1220 }
1221 
1222 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1223                     unsigned long state)
1224 {
1225     struct power_supply *psy;
1226     union power_supply_propval val;
1227     int ret;
1228 
1229     psy = tcd->devdata;
1230     val.intval = state;
1231     ret = psy->desc->set_property(psy,
1232         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1233 
1234     return ret;
1235 }
1236 
1237 static const struct thermal_cooling_device_ops psy_tcd_ops = {
1238     .get_max_state = ps_get_max_charge_cntl_limit,
1239     .get_cur_state = ps_get_cur_charge_cntl_limit,
1240     .set_cur_state = ps_set_cur_charge_cntl_limit,
1241 };
1242 
1243 static int psy_register_cooler(struct power_supply *psy)
1244 {
1245     /* Register for cooling device if psy can control charging */
1246     if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT)) {
1247         psy->tcd = thermal_cooling_device_register(
1248             (char *)psy->desc->name,
1249             psy, &psy_tcd_ops);
1250         return PTR_ERR_OR_ZERO(psy->tcd);
1251     }
1252 
1253     return 0;
1254 }
1255 
1256 static void psy_unregister_cooler(struct power_supply *psy)
1257 {
1258     if (IS_ERR_OR_NULL(psy->tcd))
1259         return;
1260     thermal_cooling_device_unregister(psy->tcd);
1261 }
1262 #else
1263 static int psy_register_thermal(struct power_supply *psy)
1264 {
1265     return 0;
1266 }
1267 
1268 static void psy_unregister_thermal(struct power_supply *psy)
1269 {
1270 }
1271 
1272 static int psy_register_cooler(struct power_supply *psy)
1273 {
1274     return 0;
1275 }
1276 
1277 static void psy_unregister_cooler(struct power_supply *psy)
1278 {
1279 }
1280 #endif
1281 
1282 static struct power_supply *__must_check
1283 __power_supply_register(struct device *parent,
1284                    const struct power_supply_desc *desc,
1285                    const struct power_supply_config *cfg,
1286                    bool ws)
1287 {
1288     struct device *dev;
1289     struct power_supply *psy;
1290     int rc;
1291 
1292     if (!parent)
1293         pr_warn("%s: Expected proper parent device for '%s'\n",
1294             __func__, desc->name);
1295 
1296     if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1297         return ERR_PTR(-EINVAL);
1298 
1299     if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
1300         (!desc->usb_types || !desc->num_usb_types))
1301         return ERR_PTR(-EINVAL);
1302 
1303     psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1304     if (!psy)
1305         return ERR_PTR(-ENOMEM);
1306 
1307     dev = &psy->dev;
1308 
1309     device_initialize(dev);
1310 
1311     dev->class = power_supply_class;
1312     dev->type = &power_supply_dev_type;
1313     dev->parent = parent;
1314     dev->release = power_supply_dev_release;
1315     dev_set_drvdata(dev, psy);
1316     psy->desc = desc;
1317     if (cfg) {
1318         dev->groups = cfg->attr_grp;
1319         psy->drv_data = cfg->drv_data;
1320         psy->of_node =
1321             cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1322         psy->supplied_to = cfg->supplied_to;
1323         psy->num_supplicants = cfg->num_supplicants;
1324     }
1325 
1326     rc = dev_set_name(dev, "%s", desc->name);
1327     if (rc)
1328         goto dev_set_name_failed;
1329 
1330     INIT_WORK(&psy->changed_work, power_supply_changed_work);
1331     INIT_DELAYED_WORK(&psy->deferred_register_work,
1332               power_supply_deferred_register_work);
1333 
1334     rc = power_supply_check_supplies(psy);
1335     if (rc) {
1336         dev_dbg(dev, "Not all required supplies found, defer probe\n");
1337         goto check_supplies_failed;
1338     }
1339 
1340     spin_lock_init(&psy->changed_lock);
1341     rc = device_add(dev);
1342     if (rc)
1343         goto device_add_failed;
1344 
1345     rc = device_init_wakeup(dev, ws);
1346     if (rc)
1347         goto wakeup_init_failed;
1348 
1349     rc = psy_register_thermal(psy);
1350     if (rc)
1351         goto register_thermal_failed;
1352 
1353     rc = psy_register_cooler(psy);
1354     if (rc)
1355         goto register_cooler_failed;
1356 
1357     rc = power_supply_create_triggers(psy);
1358     if (rc)
1359         goto create_triggers_failed;
1360 
1361     rc = power_supply_add_hwmon_sysfs(psy);
1362     if (rc)
1363         goto add_hwmon_sysfs_failed;
1364 
1365     /*
1366      * Update use_cnt after any uevents (most notably from device_add()).
1367      * We are here still during driver's probe but
1368      * the power_supply_uevent() calls back driver's get_property
1369      * method so:
1370      * 1. Driver did not assigned the returned struct power_supply,
1371      * 2. Driver could not finish initialization (anything in its probe
1372      *    after calling power_supply_register()).
1373      */
1374     atomic_inc(&psy->use_cnt);
1375     psy->initialized = true;
1376 
1377     queue_delayed_work(system_power_efficient_wq,
1378                &psy->deferred_register_work,
1379                POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1380 
1381     return psy;
1382 
1383 add_hwmon_sysfs_failed:
1384     power_supply_remove_triggers(psy);
1385 create_triggers_failed:
1386     psy_unregister_cooler(psy);
1387 register_cooler_failed:
1388     psy_unregister_thermal(psy);
1389 register_thermal_failed:
1390     device_del(dev);
1391 wakeup_init_failed:
1392 device_add_failed:
1393 check_supplies_failed:
1394 dev_set_name_failed:
1395     put_device(dev);
1396     return ERR_PTR(rc);
1397 }
1398 
1399 /**
1400  * power_supply_register() - Register new power supply
1401  * @parent: Device to be a parent of power supply's device, usually
1402  *      the device which probe function calls this
1403  * @desc:   Description of power supply, must be valid through whole
1404  *      lifetime of this power supply
1405  * @cfg:    Run-time specific configuration accessed during registering,
1406  *      may be NULL
1407  *
1408  * Return: A pointer to newly allocated power_supply on success
1409  * or ERR_PTR otherwise.
1410  * Use power_supply_unregister() on returned power_supply pointer to release
1411  * resources.
1412  */
1413 struct power_supply *__must_check power_supply_register(struct device *parent,
1414         const struct power_supply_desc *desc,
1415         const struct power_supply_config *cfg)
1416 {
1417     return __power_supply_register(parent, desc, cfg, true);
1418 }
1419 EXPORT_SYMBOL_GPL(power_supply_register);
1420 
1421 /**
1422  * power_supply_register_no_ws() - Register new non-waking-source power supply
1423  * @parent: Device to be a parent of power supply's device, usually
1424  *      the device which probe function calls this
1425  * @desc:   Description of power supply, must be valid through whole
1426  *      lifetime of this power supply
1427  * @cfg:    Run-time specific configuration accessed during registering,
1428  *      may be NULL
1429  *
1430  * Return: A pointer to newly allocated power_supply on success
1431  * or ERR_PTR otherwise.
1432  * Use power_supply_unregister() on returned power_supply pointer to release
1433  * resources.
1434  */
1435 struct power_supply *__must_check
1436 power_supply_register_no_ws(struct device *parent,
1437         const struct power_supply_desc *desc,
1438         const struct power_supply_config *cfg)
1439 {
1440     return __power_supply_register(parent, desc, cfg, false);
1441 }
1442 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1443 
1444 static void devm_power_supply_release(struct device *dev, void *res)
1445 {
1446     struct power_supply **psy = res;
1447 
1448     power_supply_unregister(*psy);
1449 }
1450 
1451 /**
1452  * devm_power_supply_register() - Register managed power supply
1453  * @parent: Device to be a parent of power supply's device, usually
1454  *      the device which probe function calls this
1455  * @desc:   Description of power supply, must be valid through whole
1456  *      lifetime of this power supply
1457  * @cfg:    Run-time specific configuration accessed during registering,
1458  *      may be NULL
1459  *
1460  * Return: A pointer to newly allocated power_supply on success
1461  * or ERR_PTR otherwise.
1462  * The returned power_supply pointer will be automatically unregistered
1463  * on driver detach.
1464  */
1465 struct power_supply *__must_check
1466 devm_power_supply_register(struct device *parent,
1467         const struct power_supply_desc *desc,
1468         const struct power_supply_config *cfg)
1469 {
1470     struct power_supply **ptr, *psy;
1471 
1472     ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1473 
1474     if (!ptr)
1475         return ERR_PTR(-ENOMEM);
1476     psy = __power_supply_register(parent, desc, cfg, true);
1477     if (IS_ERR(psy)) {
1478         devres_free(ptr);
1479     } else {
1480         *ptr = psy;
1481         devres_add(parent, ptr);
1482     }
1483     return psy;
1484 }
1485 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1486 
1487 /**
1488  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1489  * @parent: Device to be a parent of power supply's device, usually
1490  *      the device which probe function calls this
1491  * @desc:   Description of power supply, must be valid through whole
1492  *      lifetime of this power supply
1493  * @cfg:    Run-time specific configuration accessed during registering,
1494  *      may be NULL
1495  *
1496  * Return: A pointer to newly allocated power_supply on success
1497  * or ERR_PTR otherwise.
1498  * The returned power_supply pointer will be automatically unregistered
1499  * on driver detach.
1500  */
1501 struct power_supply *__must_check
1502 devm_power_supply_register_no_ws(struct device *parent,
1503         const struct power_supply_desc *desc,
1504         const struct power_supply_config *cfg)
1505 {
1506     struct power_supply **ptr, *psy;
1507 
1508     ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1509 
1510     if (!ptr)
1511         return ERR_PTR(-ENOMEM);
1512     psy = __power_supply_register(parent, desc, cfg, false);
1513     if (IS_ERR(psy)) {
1514         devres_free(ptr);
1515     } else {
1516         *ptr = psy;
1517         devres_add(parent, ptr);
1518     }
1519     return psy;
1520 }
1521 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1522 
1523 /**
1524  * power_supply_unregister() - Remove this power supply from system
1525  * @psy:    Pointer to power supply to unregister
1526  *
1527  * Remove this power supply from the system. The resources of power supply
1528  * will be freed here or on last power_supply_put() call.
1529  */
1530 void power_supply_unregister(struct power_supply *psy)
1531 {
1532     WARN_ON(atomic_dec_return(&psy->use_cnt));
1533     psy->removing = true;
1534     cancel_work_sync(&psy->changed_work);
1535     cancel_delayed_work_sync(&psy->deferred_register_work);
1536     sysfs_remove_link(&psy->dev.kobj, "powers");
1537     power_supply_remove_hwmon_sysfs(psy);
1538     power_supply_remove_triggers(psy);
1539     psy_unregister_cooler(psy);
1540     psy_unregister_thermal(psy);
1541     device_init_wakeup(&psy->dev, false);
1542     device_unregister(&psy->dev);
1543 }
1544 EXPORT_SYMBOL_GPL(power_supply_unregister);
1545 
1546 void *power_supply_get_drvdata(struct power_supply *psy)
1547 {
1548     return psy->drv_data;
1549 }
1550 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1551 
1552 static int __init power_supply_class_init(void)
1553 {
1554     power_supply_class = class_create(THIS_MODULE, "power_supply");
1555 
1556     if (IS_ERR(power_supply_class))
1557         return PTR_ERR(power_supply_class);
1558 
1559     power_supply_class->dev_uevent = power_supply_uevent;
1560     power_supply_init_attrs(&power_supply_dev_type);
1561 
1562     return 0;
1563 }
1564 
1565 static void __exit power_supply_class_exit(void)
1566 {
1567     class_destroy(power_supply_class);
1568 }
1569 
1570 subsys_initcall(power_supply_class_init);
1571 module_exit(power_supply_class_exit);
1572 
1573 MODULE_DESCRIPTION("Universal power supply monitor class");
1574 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1575           "Szabolcs Gyurko, "
1576           "Anton Vorontsov <cbou@mail.ru>");
1577 MODULE_LICENSE("GPL");