LCOV - code coverage report
Current view: top level - drivers/power/supply - power_supply_core.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 6 406 1.5 %
Date: 2020-09-30 20:25:40 Functions: 1 54 1.9 %
Branches: 1 260 0.4 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  *  Universal power supply monitor class
       4                 :            :  *
       5                 :            :  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
       6                 :            :  *  Copyright © 2004  Szabolcs Gyurko
       7                 :            :  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
       8                 :            :  *
       9                 :            :  *  Modified: 2004, Oct     Szabolcs Gyurko
      10                 :            :  */
      11                 :            : 
      12                 :            : #include <linux/module.h>
      13                 :            : #include <linux/types.h>
      14                 :            : #include <linux/init.h>
      15                 :            : #include <linux/slab.h>
      16                 :            : #include <linux/delay.h>
      17                 :            : #include <linux/device.h>
      18                 :            : #include <linux/notifier.h>
      19                 :            : #include <linux/err.h>
      20                 :            : #include <linux/of.h>
      21                 :            : #include <linux/power_supply.h>
      22                 :            : #include <linux/property.h>
      23                 :            : #include <linux/thermal.h>
      24                 :            : #include "power_supply.h"
      25                 :            : 
      26                 :            : /* exported for the APM Power driver, APM emulation */
      27                 :            : struct class *power_supply_class;
      28                 :            : EXPORT_SYMBOL_GPL(power_supply_class);
      29                 :            : 
      30                 :            : ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
      31                 :            : EXPORT_SYMBOL_GPL(power_supply_notifier);
      32                 :            : 
      33                 :            : static struct device_type power_supply_dev_type;
      34                 :            : 
      35                 :            : #define POWER_SUPPLY_DEFERRED_REGISTER_TIME     msecs_to_jiffies(10)
      36                 :            : 
      37                 :          0 : static bool __power_supply_is_supplied_by(struct power_supply *supplier,
      38                 :            :                                          struct power_supply *supply)
      39                 :            : {
      40                 :            :         int i;
      41                 :            : 
      42   [ #  #  #  # ]:          0 :         if (!supply->supplied_from && !supplier->supplied_to)
      43                 :            :                 return false;
      44                 :            : 
      45                 :            :         /* Support both supplied_to and supplied_from modes */
      46         [ #  # ]:          0 :         if (supply->supplied_from) {
      47         [ #  # ]:          0 :                 if (!supplier->desc->name)
      48                 :            :                         return false;
      49         [ #  # ]:          0 :                 for (i = 0; i < supply->num_supplies; i++)
      50         [ #  # ]:          0 :                         if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
      51                 :            :                                 return true;
      52                 :            :         } else {
      53         [ #  # ]:          0 :                 if (!supply->desc->name)
      54                 :            :                         return false;
      55         [ #  # ]:          0 :                 for (i = 0; i < supplier->num_supplicants; i++)
      56         [ #  # ]:          0 :                         if (!strcmp(supplier->supplied_to[i], supply->desc->name))
      57                 :            :                                 return true;
      58                 :            :         }
      59                 :            : 
      60                 :            :         return false;
      61                 :            : }
      62                 :            : 
      63                 :          0 : static int __power_supply_changed_work(struct device *dev, void *data)
      64                 :            : {
      65                 :            :         struct power_supply *psy = data;
      66                 :            :         struct power_supply *pst = dev_get_drvdata(dev);
      67                 :            : 
      68         [ #  # ]:          0 :         if (__power_supply_is_supplied_by(psy, pst)) {
      69         [ #  # ]:          0 :                 if (pst->desc->external_power_changed)
      70                 :          0 :                         pst->desc->external_power_changed(pst);
      71                 :            :         }
      72                 :            : 
      73                 :          0 :         return 0;
      74                 :            : }
      75                 :            : 
      76                 :          0 : static void power_supply_changed_work(struct work_struct *work)
      77                 :            : {
      78                 :            :         unsigned long flags;
      79                 :          0 :         struct power_supply *psy = container_of(work, struct power_supply,
      80                 :            :                                                 changed_work);
      81                 :            : 
      82                 :            :         dev_dbg(&psy->dev, "%s\n", __func__);
      83                 :            : 
      84                 :          0 :         spin_lock_irqsave(&psy->changed_lock, flags);
      85                 :            :         /*
      86                 :            :          * Check 'changed' here to avoid issues due to race between
      87                 :            :          * power_supply_changed() and this routine. In worst case
      88                 :            :          * power_supply_changed() can be called again just before we take above
      89                 :            :          * lock. During the first call of this routine we will mark 'changed' as
      90                 :            :          * false and it will stay false for the next call as well.
      91                 :            :          */
      92         [ #  # ]:          0 :         if (likely(psy->changed)) {
      93                 :          0 :                 psy->changed = false;
      94                 :            :                 spin_unlock_irqrestore(&psy->changed_lock, flags);
      95                 :          0 :                 class_for_each_device(power_supply_class, NULL, psy,
      96                 :            :                                       __power_supply_changed_work);
      97                 :          0 :                 power_supply_update_leds(psy);
      98                 :          0 :                 atomic_notifier_call_chain(&power_supply_notifier,
      99                 :            :                                 PSY_EVENT_PROP_CHANGED, psy);
     100                 :          0 :                 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
     101                 :          0 :                 spin_lock_irqsave(&psy->changed_lock, flags);
     102                 :            :         }
     103                 :            : 
     104                 :            :         /*
     105                 :            :          * Hold the wakeup_source until all events are processed.
     106                 :            :          * power_supply_changed() might have called again and have set 'changed'
     107                 :            :          * to true.
     108                 :            :          */
     109                 :            :         if (likely(!psy->changed))
     110                 :            :                 pm_relax(&psy->dev);
     111                 :            :         spin_unlock_irqrestore(&psy->changed_lock, flags);
     112                 :          0 : }
     113                 :            : 
     114                 :          0 : void power_supply_changed(struct power_supply *psy)
     115                 :            : {
     116                 :            :         unsigned long flags;
     117                 :            : 
     118                 :            :         dev_dbg(&psy->dev, "%s\n", __func__);
     119                 :            : 
     120                 :          0 :         spin_lock_irqsave(&psy->changed_lock, flags);
     121                 :          0 :         psy->changed = true;
     122                 :            :         pm_stay_awake(&psy->dev);
     123                 :            :         spin_unlock_irqrestore(&psy->changed_lock, flags);
     124                 :          0 :         schedule_work(&psy->changed_work);
     125                 :          0 : }
     126                 :            : EXPORT_SYMBOL_GPL(power_supply_changed);
     127                 :            : 
     128                 :            : /*
     129                 :            :  * Notify that power supply was registered after parent finished the probing.
     130                 :            :  *
     131                 :            :  * Often power supply is registered from driver's probe function. However
     132                 :            :  * calling power_supply_changed() directly from power_supply_register()
     133                 :            :  * would lead to execution of get_property() function provided by the driver
     134                 :            :  * too early - before the probe ends.
     135                 :            :  *
     136                 :            :  * Avoid that by waiting on parent's mutex.
     137                 :            :  */
     138                 :          0 : static void power_supply_deferred_register_work(struct work_struct *work)
     139                 :            : {
     140                 :          0 :         struct power_supply *psy = container_of(work, struct power_supply,
     141                 :            :                                                 deferred_register_work.work);
     142                 :            : 
     143         [ #  # ]:          0 :         if (psy->dev.parent) {
     144         [ #  # ]:          0 :                 while (!mutex_trylock(&psy->dev.parent->mutex)) {
     145         [ #  # ]:          0 :                         if (psy->removing)
     146                 :          0 :                                 return;
     147                 :          0 :                         msleep(10);
     148                 :            :                 }
     149                 :            :         }
     150                 :            : 
     151                 :          0 :         power_supply_changed(psy);
     152                 :            : 
     153         [ #  # ]:          0 :         if (psy->dev.parent)
     154                 :          0 :                 mutex_unlock(&psy->dev.parent->mutex);
     155                 :            : }
     156                 :            : 
     157                 :            : #ifdef CONFIG_OF
     158                 :          0 : static int __power_supply_populate_supplied_from(struct device *dev,
     159                 :            :                                                  void *data)
     160                 :            : {
     161                 :            :         struct power_supply *psy = data;
     162                 :            :         struct power_supply *epsy = dev_get_drvdata(dev);
     163                 :            :         struct device_node *np;
     164                 :            :         int i = 0;
     165                 :            : 
     166                 :            :         do {
     167                 :          0 :                 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
     168         [ #  # ]:          0 :                 if (!np)
     169                 :            :                         break;
     170                 :            : 
     171         [ #  # ]:          0 :                 if (np == epsy->of_node) {
     172                 :          0 :                         dev_info(&psy->dev, "%s: Found supply : %s\n",
     173                 :            :                                 psy->desc->name, epsy->desc->name);
     174                 :          0 :                         psy->supplied_from[i-1] = (char *)epsy->desc->name;
     175                 :          0 :                         psy->num_supplies++;
     176                 :          0 :                         of_node_put(np);
     177                 :          0 :                         break;
     178                 :            :                 }
     179                 :          0 :                 of_node_put(np);
     180         [ #  # ]:          0 :         } while (np);
     181                 :            : 
     182                 :          0 :         return 0;
     183                 :            : }
     184                 :            : 
     185                 :            : static int power_supply_populate_supplied_from(struct power_supply *psy)
     186                 :            : {
     187                 :            :         int error;
     188                 :            : 
     189                 :          0 :         error = class_for_each_device(power_supply_class, NULL, psy,
     190                 :            :                                       __power_supply_populate_supplied_from);
     191                 :            : 
     192                 :            :         dev_dbg(&psy->dev, "%s %d\n", __func__, error);
     193                 :            : 
     194                 :            :         return error;
     195                 :            : }
     196                 :            : 
     197                 :          0 : static int  __power_supply_find_supply_from_node(struct device *dev,
     198                 :            :                                                  void *data)
     199                 :            : {
     200                 :            :         struct device_node *np = data;
     201                 :            :         struct power_supply *epsy = dev_get_drvdata(dev);
     202                 :            : 
     203                 :            :         /* returning non-zero breaks out of class_for_each_device loop */
     204         [ #  # ]:          0 :         if (epsy->of_node == np)
     205                 :            :                 return 1;
     206                 :            : 
     207                 :          0 :         return 0;
     208                 :            : }
     209                 :            : 
     210                 :          0 : static int power_supply_find_supply_from_node(struct device_node *supply_node)
     211                 :            : {
     212                 :            :         int error;
     213                 :            : 
     214                 :            :         /*
     215                 :            :          * class_for_each_device() either returns its own errors or values
     216                 :            :          * returned by __power_supply_find_supply_from_node().
     217                 :            :          *
     218                 :            :          * __power_supply_find_supply_from_node() will return 0 (no match)
     219                 :            :          * or 1 (match).
     220                 :            :          *
     221                 :            :          * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
     222                 :            :          * it returned 0, or error as returned by it.
     223                 :            :          */
     224                 :          0 :         error = class_for_each_device(power_supply_class, NULL, supply_node,
     225                 :            :                                        __power_supply_find_supply_from_node);
     226                 :            : 
     227   [ #  #  #  # ]:          0 :         return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
     228                 :            : }
     229                 :            : 
     230                 :          0 : static int power_supply_check_supplies(struct power_supply *psy)
     231                 :            : {
     232                 :            :         struct device_node *np;
     233                 :            :         int cnt = 0;
     234                 :            : 
     235                 :            :         /* If there is already a list honor it */
     236   [ #  #  #  # ]:          0 :         if (psy->supplied_from && psy->num_supplies > 0)
     237                 :            :                 return 0;
     238                 :            : 
     239                 :            :         /* No device node found, nothing to do */
     240         [ #  # ]:          0 :         if (!psy->of_node)
     241                 :            :                 return 0;
     242                 :            : 
     243                 :            :         do {
     244                 :            :                 int ret;
     245                 :            : 
     246                 :          0 :                 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
     247         [ #  # ]:          0 :                 if (!np)
     248                 :            :                         break;
     249                 :            : 
     250                 :          0 :                 ret = power_supply_find_supply_from_node(np);
     251                 :          0 :                 of_node_put(np);
     252                 :            : 
     253         [ #  # ]:          0 :                 if (ret) {
     254                 :            :                         dev_dbg(&psy->dev, "Failed to find supply!\n");
     255                 :          0 :                         return ret;
     256                 :            :                 }
     257         [ #  # ]:          0 :         } while (np);
     258                 :            : 
     259                 :            :         /* Missing valid "power-supplies" entries */
     260         [ #  # ]:          0 :         if (cnt == 1)
     261                 :            :                 return 0;
     262                 :            : 
     263                 :            :         /* All supplies found, allocate char ** array for filling */
     264                 :          0 :         psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
     265                 :            :                                           GFP_KERNEL);
     266         [ #  # ]:          0 :         if (!psy->supplied_from)
     267                 :            :                 return -ENOMEM;
     268                 :            : 
     269                 :          0 :         *psy->supplied_from = devm_kcalloc(&psy->dev,
     270                 :          0 :                                            cnt - 1, sizeof(char *),
     271                 :            :                                            GFP_KERNEL);
     272         [ #  # ]:          0 :         if (!*psy->supplied_from)
     273                 :            :                 return -ENOMEM;
     274                 :            : 
     275                 :          0 :         return power_supply_populate_supplied_from(psy);
     276                 :            : }
     277                 :            : #else
     278                 :            : static int power_supply_check_supplies(struct power_supply *psy)
     279                 :            : {
     280                 :            :         int nval, ret;
     281                 :            : 
     282                 :            :         if (!psy->dev.parent)
     283                 :            :                 return 0;
     284                 :            : 
     285                 :            :         nval = device_property_read_string_array(psy->dev.parent,
     286                 :            :                                                  "supplied-from", NULL, 0);
     287                 :            :         if (nval <= 0)
     288                 :            :                 return 0;
     289                 :            : 
     290                 :            :         psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
     291                 :            :                                                 sizeof(char *), GFP_KERNEL);
     292                 :            :         if (!psy->supplied_from)
     293                 :            :                 return -ENOMEM;
     294                 :            : 
     295                 :            :         ret = device_property_read_string_array(psy->dev.parent,
     296                 :            :                 "supplied-from", (const char **)psy->supplied_from, nval);
     297                 :            :         if (ret < 0)
     298                 :            :                 return ret;
     299                 :            : 
     300                 :            :         psy->num_supplies = nval;
     301                 :            : 
     302                 :            :         return 0;
     303                 :            : }
     304                 :            : #endif
     305                 :            : 
     306                 :            : struct psy_am_i_supplied_data {
     307                 :            :         struct power_supply *psy;
     308                 :            :         unsigned int count;
     309                 :            : };
     310                 :            : 
     311                 :          0 : static int __power_supply_am_i_supplied(struct device *dev, void *_data)
     312                 :            : {
     313                 :          0 :         union power_supply_propval ret = {0,};
     314                 :            :         struct power_supply *epsy = dev_get_drvdata(dev);
     315                 :            :         struct psy_am_i_supplied_data *data = _data;
     316                 :            : 
     317         [ #  # ]:          0 :         if (__power_supply_is_supplied_by(epsy, data->psy)) {
     318                 :          0 :                 data->count++;
     319         [ #  # ]:          0 :                 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
     320                 :            :                                         &ret))
     321                 :          0 :                         return ret.intval;
     322                 :            :         }
     323                 :            : 
     324                 :            :         return 0;
     325                 :            : }
     326                 :            : 
     327                 :          0 : int power_supply_am_i_supplied(struct power_supply *psy)
     328                 :            : {
     329                 :          0 :         struct psy_am_i_supplied_data data = { psy, 0 };
     330                 :            :         int error;
     331                 :            : 
     332                 :          0 :         error = class_for_each_device(power_supply_class, NULL, &data,
     333                 :            :                                       __power_supply_am_i_supplied);
     334                 :            : 
     335                 :            :         dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
     336                 :            : 
     337         [ #  # ]:          0 :         if (data.count == 0)
     338                 :            :                 return -ENODEV;
     339                 :            : 
     340                 :          0 :         return error;
     341                 :            : }
     342                 :            : EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
     343                 :            : 
     344                 :          0 : static int __power_supply_is_system_supplied(struct device *dev, void *data)
     345                 :            : {
     346                 :          0 :         union power_supply_propval ret = {0,};
     347                 :            :         struct power_supply *psy = dev_get_drvdata(dev);
     348                 :            :         unsigned int *count = data;
     349                 :            : 
     350                 :          0 :         (*count)++;
     351         [ #  # ]:          0 :         if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
     352         [ #  # ]:          0 :                 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
     353                 :            :                                         &ret))
     354                 :          0 :                         return ret.intval;
     355                 :            : 
     356                 :            :         return 0;
     357                 :            : }
     358                 :            : 
     359                 :          0 : int power_supply_is_system_supplied(void)
     360                 :            : {
     361                 :            :         int error;
     362                 :          0 :         unsigned int count = 0;
     363                 :            : 
     364                 :          0 :         error = class_for_each_device(power_supply_class, NULL, &count,
     365                 :            :                                       __power_supply_is_system_supplied);
     366                 :            : 
     367                 :            :         /*
     368                 :            :          * If no power class device was found at all, most probably we are
     369                 :            :          * running on a desktop system, so assume we are on mains power.
     370                 :            :          */
     371         [ #  # ]:          0 :         if (count == 0)
     372                 :            :                 return 1;
     373                 :            : 
     374                 :          0 :         return error;
     375                 :            : }
     376                 :            : EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
     377                 :            : 
     378                 :          0 : static int __power_supply_get_supplier_max_current(struct device *dev,
     379                 :            :                                                    void *data)
     380                 :            : {
     381                 :          0 :         union power_supply_propval ret = {0,};
     382                 :            :         struct power_supply *epsy = dev_get_drvdata(dev);
     383                 :            :         struct power_supply *psy = data;
     384                 :            : 
     385         [ #  # ]:          0 :         if (__power_supply_is_supplied_by(epsy, psy))
     386         [ #  # ]:          0 :                 if (!epsy->desc->get_property(epsy,
     387                 :            :                                               POWER_SUPPLY_PROP_CURRENT_MAX,
     388                 :            :                                               &ret))
     389                 :          0 :                         return ret.intval;
     390                 :            : 
     391                 :            :         return 0;
     392                 :            : }
     393                 :            : 
     394                 :          0 : int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
     395                 :            : {
     396                 :          0 :         union power_supply_propval val = {0,};
     397                 :            :         int curr;
     398                 :            : 
     399         [ #  # ]:          0 :         if (!psy->desc->set_property)
     400                 :            :                 return -EINVAL;
     401                 :            : 
     402                 :            :         /*
     403                 :            :          * This function is not intended for use with a supply with multiple
     404                 :            :          * suppliers, we simply pick the first supply to report a non 0
     405                 :            :          * max-current.
     406                 :            :          */
     407                 :          0 :         curr = class_for_each_device(power_supply_class, NULL, psy,
     408                 :            :                                       __power_supply_get_supplier_max_current);
     409         [ #  # ]:          0 :         if (curr <= 0)
     410         [ #  # ]:          0 :                 return (curr == 0) ? -ENODEV : curr;
     411                 :            : 
     412                 :          0 :         val.intval = curr;
     413                 :            : 
     414                 :          0 :         return psy->desc->set_property(psy,
     415                 :            :                                 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
     416                 :            : }
     417                 :            : EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
     418                 :            : 
     419                 :          0 : int power_supply_set_battery_charged(struct power_supply *psy)
     420                 :            : {
     421   [ #  #  #  # ]:          0 :         if (atomic_read(&psy->use_cnt) >= 0 &&
     422         [ #  # ]:          0 :                         psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
     423                 :          0 :                         psy->desc->set_charged) {
     424                 :          0 :                 psy->desc->set_charged(psy);
     425                 :          0 :                 return 0;
     426                 :            :         }
     427                 :            : 
     428                 :            :         return -EINVAL;
     429                 :            : }
     430                 :            : EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
     431                 :            : 
     432                 :          0 : static int power_supply_match_device_by_name(struct device *dev, const void *data)
     433                 :            : {
     434                 :            :         const char *name = data;
     435                 :            :         struct power_supply *psy = dev_get_drvdata(dev);
     436                 :            : 
     437                 :          0 :         return strcmp(psy->desc->name, name) == 0;
     438                 :            : }
     439                 :            : 
     440                 :            : /**
     441                 :            :  * power_supply_get_by_name() - Search for a power supply and returns its ref
     442                 :            :  * @name: Power supply name to fetch
     443                 :            :  *
     444                 :            :  * If power supply was found, it increases reference count for the
     445                 :            :  * internal power supply's device. The user should power_supply_put()
     446                 :            :  * after usage.
     447                 :            :  *
     448                 :            :  * Return: On success returns a reference to a power supply with
     449                 :            :  * matching name equals to @name, a NULL otherwise.
     450                 :            :  */
     451                 :          0 : struct power_supply *power_supply_get_by_name(const char *name)
     452                 :            : {
     453                 :            :         struct power_supply *psy = NULL;
     454                 :          0 :         struct device *dev = class_find_device(power_supply_class, NULL, name,
     455                 :            :                                         power_supply_match_device_by_name);
     456                 :            : 
     457         [ #  # ]:          0 :         if (dev) {
     458                 :            :                 psy = dev_get_drvdata(dev);
     459                 :          0 :                 atomic_inc(&psy->use_cnt);
     460                 :            :         }
     461                 :            : 
     462                 :          0 :         return psy;
     463                 :            : }
     464                 :            : EXPORT_SYMBOL_GPL(power_supply_get_by_name);
     465                 :            : 
     466                 :            : /**
     467                 :            :  * power_supply_put() - Drop reference obtained with power_supply_get_by_name
     468                 :            :  * @psy: Reference to put
     469                 :            :  *
     470                 :            :  * The reference to power supply should be put before unregistering
     471                 :            :  * the power supply.
     472                 :            :  */
     473                 :          0 : void power_supply_put(struct power_supply *psy)
     474                 :            : {
     475                 :          0 :         might_sleep();
     476                 :            : 
     477                 :          0 :         atomic_dec(&psy->use_cnt);
     478                 :          0 :         put_device(&psy->dev);
     479                 :          0 : }
     480                 :            : EXPORT_SYMBOL_GPL(power_supply_put);
     481                 :            : 
     482                 :            : #ifdef CONFIG_OF
     483                 :          0 : static int power_supply_match_device_node(struct device *dev, const void *data)
     484                 :            : {
     485   [ #  #  #  # ]:          0 :         return dev->parent && dev->parent->of_node == data;
     486                 :            : }
     487                 :            : 
     488                 :            : /**
     489                 :            :  * power_supply_get_by_phandle() - Search for a power supply and returns its ref
     490                 :            :  * @np: Pointer to device node holding phandle property
     491                 :            :  * @property: Name of property holding a power supply name
     492                 :            :  *
     493                 :            :  * If power supply was found, it increases reference count for the
     494                 :            :  * internal power supply's device. The user should power_supply_put()
     495                 :            :  * after usage.
     496                 :            :  *
     497                 :            :  * Return: On success returns a reference to a power supply with
     498                 :            :  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
     499                 :            :  */
     500                 :          0 : struct power_supply *power_supply_get_by_phandle(struct device_node *np,
     501                 :            :                                                         const char *property)
     502                 :            : {
     503                 :            :         struct device_node *power_supply_np;
     504                 :            :         struct power_supply *psy = NULL;
     505                 :            :         struct device *dev;
     506                 :            : 
     507                 :          0 :         power_supply_np = of_parse_phandle(np, property, 0);
     508         [ #  # ]:          0 :         if (!power_supply_np)
     509                 :            :                 return ERR_PTR(-ENODEV);
     510                 :            : 
     511                 :          0 :         dev = class_find_device(power_supply_class, NULL, power_supply_np,
     512                 :            :                                                 power_supply_match_device_node);
     513                 :            : 
     514                 :          0 :         of_node_put(power_supply_np);
     515                 :            : 
     516         [ #  # ]:          0 :         if (dev) {
     517                 :            :                 psy = dev_get_drvdata(dev);
     518                 :          0 :                 atomic_inc(&psy->use_cnt);
     519                 :            :         }
     520                 :            : 
     521                 :          0 :         return psy;
     522                 :            : }
     523                 :            : EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
     524                 :            : 
     525                 :          0 : static void devm_power_supply_put(struct device *dev, void *res)
     526                 :            : {
     527                 :            :         struct power_supply **psy = res;
     528                 :            : 
     529                 :          0 :         power_supply_put(*psy);
     530                 :          0 : }
     531                 :            : 
     532                 :            : /**
     533                 :            :  * devm_power_supply_get_by_phandle() - Resource managed version of
     534                 :            :  *  power_supply_get_by_phandle()
     535                 :            :  * @dev: Pointer to device holding phandle property
     536                 :            :  * @property: Name of property holding a power supply phandle
     537                 :            :  *
     538                 :            :  * Return: On success returns a reference to a power supply with
     539                 :            :  * matching name equals to value under @property, NULL or ERR_PTR otherwise.
     540                 :            :  */
     541                 :          0 : struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
     542                 :            :                                                       const char *property)
     543                 :            : {
     544                 :            :         struct power_supply **ptr, *psy;
     545                 :            : 
     546         [ #  # ]:          0 :         if (!dev->of_node)
     547                 :            :                 return ERR_PTR(-ENODEV);
     548                 :            : 
     549                 :            :         ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
     550         [ #  # ]:          0 :         if (!ptr)
     551                 :            :                 return ERR_PTR(-ENOMEM);
     552                 :            : 
     553                 :          0 :         psy = power_supply_get_by_phandle(dev->of_node, property);
     554         [ #  # ]:          0 :         if (IS_ERR_OR_NULL(psy)) {
     555                 :          0 :                 devres_free(ptr);
     556                 :            :         } else {
     557                 :          0 :                 *ptr = psy;
     558                 :          0 :                 devres_add(dev, ptr);
     559                 :            :         }
     560                 :          0 :         return psy;
     561                 :            : }
     562                 :            : EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
     563                 :            : #endif /* CONFIG_OF */
     564                 :            : 
     565                 :          0 : int power_supply_get_battery_info(struct power_supply *psy,
     566                 :            :                                   struct power_supply_battery_info *info)
     567                 :            : {
     568                 :            :         struct device_node *battery_np;
     569                 :            :         const char *value;
     570                 :            :         int err, len, index;
     571                 :            : 
     572                 :          0 :         info->energy_full_design_uwh         = -EINVAL;
     573                 :          0 :         info->charge_full_design_uah         = -EINVAL;
     574                 :          0 :         info->voltage_min_design_uv          = -EINVAL;
     575                 :          0 :         info->voltage_max_design_uv          = -EINVAL;
     576                 :          0 :         info->precharge_current_ua           = -EINVAL;
     577                 :          0 :         info->charge_term_current_ua         = -EINVAL;
     578                 :          0 :         info->constant_charge_current_max_ua = -EINVAL;
     579                 :          0 :         info->constant_charge_voltage_max_uv = -EINVAL;
     580                 :          0 :         info->factory_internal_resistance_uohm  = -EINVAL;
     581                 :            : 
     582         [ #  # ]:          0 :         for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
     583                 :          0 :                 info->ocv_table[index]       = NULL;
     584                 :          0 :                 info->ocv_temp[index]        = -EINVAL;
     585                 :          0 :                 info->ocv_table_size[index]  = -EINVAL;
     586                 :            :         }
     587                 :            : 
     588         [ #  # ]:          0 :         if (!psy->of_node) {
     589                 :          0 :                 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
     590                 :            :                          __func__);
     591                 :          0 :                 return -ENXIO;
     592                 :            :         }
     593                 :            : 
     594                 :          0 :         battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
     595         [ #  # ]:          0 :         if (!battery_np)
     596                 :            :                 return -ENODEV;
     597                 :            : 
     598                 :          0 :         err = of_property_read_string(battery_np, "compatible", &value);
     599         [ #  # ]:          0 :         if (err)
     600                 :            :                 goto out_put_node;
     601                 :            : 
     602         [ #  # ]:          0 :         if (strcmp("simple-battery", value)) {
     603                 :            :                 err = -ENODEV;
     604                 :            :                 goto out_put_node;
     605                 :            :         }
     606                 :            : 
     607                 :            :         /* The property and field names below must correspond to elements
     608                 :            :          * in enum power_supply_property. For reasoning, see
     609                 :            :          * Documentation/power/power_supply_class.rst.
     610                 :            :          */
     611                 :            : 
     612                 :            :         of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
     613                 :          0 :                              &info->energy_full_design_uwh);
     614                 :            :         of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
     615                 :          0 :                              &info->charge_full_design_uah);
     616                 :            :         of_property_read_u32(battery_np, "voltage-min-design-microvolt",
     617                 :          0 :                              &info->voltage_min_design_uv);
     618                 :            :         of_property_read_u32(battery_np, "voltage-max-design-microvolt",
     619                 :          0 :                              &info->voltage_max_design_uv);
     620                 :            :         of_property_read_u32(battery_np, "precharge-current-microamp",
     621                 :          0 :                              &info->precharge_current_ua);
     622                 :            :         of_property_read_u32(battery_np, "charge-term-current-microamp",
     623                 :          0 :                              &info->charge_term_current_ua);
     624                 :            :         of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
     625                 :          0 :                              &info->constant_charge_current_max_ua);
     626                 :            :         of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
     627                 :          0 :                              &info->constant_charge_voltage_max_uv);
     628                 :            :         of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
     629                 :          0 :                              &info->factory_internal_resistance_uohm);
     630                 :            : 
     631                 :            :         len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
     632         [ #  # ]:          0 :         if (len < 0 && len != -EINVAL) {
     633                 :            :                 err = len;
     634                 :            :                 goto out_put_node;
     635         [ #  # ]:          0 :         } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
     636                 :          0 :                 dev_err(&psy->dev, "Too many temperature values\n");
     637                 :            :                 err = -EINVAL;
     638                 :          0 :                 goto out_put_node;
     639         [ #  # ]:          0 :         } else if (len > 0) {
     640                 :          0 :                 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
     641                 :          0 :                                            info->ocv_temp, len);
     642                 :            :         }
     643                 :            : 
     644         [ #  # ]:          0 :         for (index = 0; index < len; index++) {
     645                 :            :                 struct power_supply_battery_ocv_table *table;
     646                 :            :                 char *propname;
     647                 :            :                 const __be32 *list;
     648                 :            :                 int i, tab_len, size;
     649                 :            : 
     650                 :          0 :                 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
     651                 :          0 :                 list = of_get_property(battery_np, propname, &size);
     652   [ #  #  #  # ]:          0 :                 if (!list || !size) {
     653                 :          0 :                         dev_err(&psy->dev, "failed to get %s\n", propname);
     654                 :          0 :                         kfree(propname);
     655                 :          0 :                         power_supply_put_battery_info(psy, info);
     656                 :            :                         err = -EINVAL;
     657                 :          0 :                         goto out_put_node;
     658                 :            :                 }
     659                 :            : 
     660                 :          0 :                 kfree(propname);
     661                 :          0 :                 tab_len = size / (2 * sizeof(__be32));
     662                 :          0 :                 info->ocv_table_size[index] = tab_len;
     663                 :            : 
     664                 :          0 :                 table = info->ocv_table[index] =
     665                 :          0 :                         devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
     666         [ #  # ]:          0 :                 if (!info->ocv_table[index]) {
     667                 :          0 :                         power_supply_put_battery_info(psy, info);
     668                 :            :                         err = -ENOMEM;
     669                 :          0 :                         goto out_put_node;
     670                 :            :                 }
     671                 :            : 
     672         [ #  # ]:          0 :                 for (i = 0; i < tab_len; i++) {
     673                 :          0 :                         table[i].ocv = be32_to_cpu(*list);
     674                 :            :                         list++;
     675                 :          0 :                         table[i].capacity = be32_to_cpu(*list);
     676                 :          0 :                         list++;
     677                 :            :                 }
     678                 :            :         }
     679                 :            : 
     680                 :            : out_put_node:
     681                 :          0 :         of_node_put(battery_np);
     682                 :          0 :         return err;
     683                 :            : }
     684                 :            : EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
     685                 :            : 
     686                 :          0 : void power_supply_put_battery_info(struct power_supply *psy,
     687                 :            :                                    struct power_supply_battery_info *info)
     688                 :            : {
     689                 :            :         int i;
     690                 :            : 
     691         [ #  # ]:          0 :         for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
     692         [ #  # ]:          0 :                 if (info->ocv_table[i])
     693                 :          0 :                         devm_kfree(&psy->dev, info->ocv_table[i]);
     694                 :            :         }
     695                 :          0 : }
     696                 :            : EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
     697                 :            : 
     698                 :            : /**
     699                 :            :  * power_supply_ocv2cap_simple() - find the battery capacity
     700                 :            :  * @table: Pointer to battery OCV lookup table
     701                 :            :  * @table_len: OCV table length
     702                 :            :  * @ocv: Current OCV value
     703                 :            :  *
     704                 :            :  * This helper function is used to look up battery capacity according to
     705                 :            :  * current OCV value from one OCV table, and the OCV table must be ordered
     706                 :            :  * descending.
     707                 :            :  *
     708                 :            :  * Return: the battery capacity.
     709                 :            :  */
     710                 :          0 : int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
     711                 :            :                                 int table_len, int ocv)
     712                 :            : {
     713                 :            :         int i, cap, tmp;
     714                 :            : 
     715         [ #  # ]:          0 :         for (i = 0; i < table_len; i++)
     716         [ #  # ]:          0 :                 if (ocv > table[i].ocv)
     717                 :            :                         break;
     718                 :            : 
     719         [ #  # ]:          0 :         if (i > 0 && i < table_len) {
     720                 :          0 :                 tmp = (table[i - 1].capacity - table[i].capacity) *
     721                 :          0 :                         (ocv - table[i].ocv);
     722                 :          0 :                 tmp /= table[i - 1].ocv - table[i].ocv;
     723                 :          0 :                 cap = tmp + table[i].capacity;
     724         [ #  # ]:          0 :         } else if (i == 0) {
     725                 :          0 :                 cap = table[0].capacity;
     726                 :            :         } else {
     727                 :          0 :                 cap = table[table_len - 1].capacity;
     728                 :            :         }
     729                 :            : 
     730                 :          0 :         return cap;
     731                 :            : }
     732                 :            : EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
     733                 :            : 
     734                 :            : struct power_supply_battery_ocv_table *
     735                 :          0 : power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
     736                 :            :                                 int temp, int *table_len)
     737                 :            : {
     738                 :            :         int best_temp_diff = INT_MAX, temp_diff;
     739                 :            :         u8 i, best_index = 0;
     740                 :            : 
     741   [ #  #  #  # ]:          0 :         if (!info->ocv_table[0])
     742                 :            :                 return NULL;
     743                 :            : 
     744   [ #  #  #  # ]:          0 :         for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
     745                 :          0 :                 temp_diff = abs(info->ocv_temp[i] - temp);
     746                 :            : 
     747   [ #  #  #  # ]:          0 :                 if (temp_diff < best_temp_diff) {
     748                 :            :                         best_temp_diff = temp_diff;
     749                 :            :                         best_index = i;
     750                 :            :                 }
     751                 :            :         }
     752                 :            : 
     753                 :          0 :         *table_len = info->ocv_table_size[best_index];
     754                 :          0 :         return info->ocv_table[best_index];
     755                 :            : }
     756                 :            : EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
     757                 :            : 
     758                 :          0 : int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
     759                 :            :                                  int ocv, int temp)
     760                 :            : {
     761                 :            :         struct power_supply_battery_ocv_table *table;
     762                 :            :         int table_len;
     763                 :            : 
     764                 :            :         table = power_supply_find_ocv2cap_table(info, temp, &table_len);
     765         [ #  # ]:          0 :         if (!table)
     766                 :            :                 return -EINVAL;
     767                 :            : 
     768                 :          0 :         return power_supply_ocv2cap_simple(table, table_len, ocv);
     769                 :            : }
     770                 :            : EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
     771                 :            : 
     772                 :          0 : int power_supply_get_property(struct power_supply *psy,
     773                 :            :                             enum power_supply_property psp,
     774                 :            :                             union power_supply_propval *val)
     775                 :            : {
     776   [ #  #  #  #  :          0 :         if (atomic_read(&psy->use_cnt) <= 0) {
             #  #  #  # ]
     777   [ #  #  #  #  :          0 :                 if (!psy->initialized)
             #  #  #  # ]
     778                 :            :                         return -EAGAIN;
     779                 :          0 :                 return -ENODEV;
     780                 :            :         }
     781                 :            : 
     782                 :          0 :         return psy->desc->get_property(psy, psp, val);
     783                 :            : }
     784                 :            : EXPORT_SYMBOL_GPL(power_supply_get_property);
     785                 :            : 
     786                 :          0 : int power_supply_set_property(struct power_supply *psy,
     787                 :            :                             enum power_supply_property psp,
     788                 :            :                             const union power_supply_propval *val)
     789                 :            : {
     790   [ #  #  #  # ]:          0 :         if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
     791                 :            :                 return -ENODEV;
     792                 :            : 
     793                 :          0 :         return psy->desc->set_property(psy, psp, val);
     794                 :            : }
     795                 :            : EXPORT_SYMBOL_GPL(power_supply_set_property);
     796                 :            : 
     797                 :          0 : int power_supply_property_is_writeable(struct power_supply *psy,
     798                 :            :                                         enum power_supply_property psp)
     799                 :            : {
     800   [ #  #  #  # ]:          0 :         if (atomic_read(&psy->use_cnt) <= 0 ||
     801                 :          0 :                         !psy->desc->property_is_writeable)
     802                 :            :                 return -ENODEV;
     803                 :            : 
     804                 :          0 :         return psy->desc->property_is_writeable(psy, psp);
     805                 :            : }
     806                 :            : EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
     807                 :            : 
     808                 :          0 : void power_supply_external_power_changed(struct power_supply *psy)
     809                 :            : {
     810   [ #  #  #  # ]:          0 :         if (atomic_read(&psy->use_cnt) <= 0 ||
     811                 :          0 :                         !psy->desc->external_power_changed)
     812                 :          0 :                 return;
     813                 :            : 
     814                 :          0 :         psy->desc->external_power_changed(psy);
     815                 :            : }
     816                 :            : EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
     817                 :            : 
     818                 :          0 : int power_supply_powers(struct power_supply *psy, struct device *dev)
     819                 :            : {
     820                 :          0 :         return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
     821                 :            : }
     822                 :            : EXPORT_SYMBOL_GPL(power_supply_powers);
     823                 :            : 
     824                 :          0 : static void power_supply_dev_release(struct device *dev)
     825                 :            : {
     826                 :          0 :         struct power_supply *psy = to_power_supply(dev);
     827                 :            :         dev_dbg(dev, "%s\n", __func__);
     828                 :          0 :         kfree(psy);
     829                 :          0 : }
     830                 :            : 
     831                 :          0 : int power_supply_reg_notifier(struct notifier_block *nb)
     832                 :            : {
     833                 :          0 :         return atomic_notifier_chain_register(&power_supply_notifier, nb);
     834                 :            : }
     835                 :            : EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
     836                 :            : 
     837                 :          0 : void power_supply_unreg_notifier(struct notifier_block *nb)
     838                 :            : {
     839                 :          0 :         atomic_notifier_chain_unregister(&power_supply_notifier, nb);
     840                 :          0 : }
     841                 :            : EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
     842                 :            : 
     843                 :            : #ifdef CONFIG_THERMAL
     844                 :          0 : static int power_supply_read_temp(struct thermal_zone_device *tzd,
     845                 :            :                 int *temp)
     846                 :            : {
     847                 :            :         struct power_supply *psy;
     848                 :            :         union power_supply_propval val;
     849                 :            :         int ret;
     850                 :            : 
     851         [ #  # ]:          0 :         WARN_ON(tzd == NULL);
     852                 :          0 :         psy = tzd->devdata;
     853                 :            :         ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
     854         [ #  # ]:          0 :         if (ret)
     855                 :            :                 return ret;
     856                 :            : 
     857                 :            :         /* Convert tenths of degree Celsius to milli degree Celsius. */
     858                 :          0 :         *temp = val.intval * 100;
     859                 :            : 
     860                 :          0 :         return ret;
     861                 :            : }
     862                 :            : 
     863                 :            : static struct thermal_zone_device_ops psy_tzd_ops = {
     864                 :            :         .get_temp = power_supply_read_temp,
     865                 :            : };
     866                 :            : 
     867                 :          0 : static int psy_register_thermal(struct power_supply *psy)
     868                 :            : {
     869                 :            :         int i;
     870                 :            : 
     871         [ #  # ]:          0 :         if (psy->desc->no_thermal)
     872                 :            :                 return 0;
     873                 :            : 
     874                 :            :         /* Register battery zone device psy reports temperature */
     875         [ #  # ]:          0 :         for (i = 0; i < psy->desc->num_properties; i++) {
     876         [ #  # ]:          0 :                 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
     877                 :          0 :                         psy->tzd = thermal_zone_device_register(psy->desc->name,
     878                 :            :                                         0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
     879                 :          0 :                         return PTR_ERR_OR_ZERO(psy->tzd);
     880                 :            :                 }
     881                 :            :         }
     882                 :            :         return 0;
     883                 :            : }
     884                 :            : 
     885                 :          0 : static void psy_unregister_thermal(struct power_supply *psy)
     886                 :            : {
     887         [ #  # ]:          0 :         if (IS_ERR_OR_NULL(psy->tzd))
     888                 :          0 :                 return;
     889                 :          0 :         thermal_zone_device_unregister(psy->tzd);
     890                 :            : }
     891                 :            : 
     892                 :            : /* thermal cooling device callbacks */
     893                 :          0 : static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
     894                 :            :                                         unsigned long *state)
     895                 :            : {
     896                 :            :         struct power_supply *psy;
     897                 :            :         union power_supply_propval val;
     898                 :            :         int ret;
     899                 :            : 
     900                 :          0 :         psy = tcd->devdata;
     901                 :            :         ret = power_supply_get_property(psy,
     902                 :            :                         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
     903         [ #  # ]:          0 :         if (ret)
     904                 :            :                 return ret;
     905                 :            : 
     906                 :          0 :         *state = val.intval;
     907                 :            : 
     908                 :          0 :         return ret;
     909                 :            : }
     910                 :            : 
     911                 :          0 : static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
     912                 :            :                                         unsigned long *state)
     913                 :            : {
     914                 :            :         struct power_supply *psy;
     915                 :            :         union power_supply_propval val;
     916                 :            :         int ret;
     917                 :            : 
     918                 :          0 :         psy = tcd->devdata;
     919                 :            :         ret = power_supply_get_property(psy,
     920                 :            :                         POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
     921         [ #  # ]:          0 :         if (ret)
     922                 :            :                 return ret;
     923                 :            : 
     924                 :          0 :         *state = val.intval;
     925                 :            : 
     926                 :          0 :         return ret;
     927                 :            : }
     928                 :            : 
     929                 :          0 : static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
     930                 :            :                                         unsigned long state)
     931                 :            : {
     932                 :            :         struct power_supply *psy;
     933                 :            :         union power_supply_propval val;
     934                 :            :         int ret;
     935                 :            : 
     936                 :          0 :         psy = tcd->devdata;
     937                 :          0 :         val.intval = state;
     938                 :          0 :         ret = psy->desc->set_property(psy,
     939                 :            :                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
     940                 :            : 
     941                 :          0 :         return ret;
     942                 :            : }
     943                 :            : 
     944                 :            : static const struct thermal_cooling_device_ops psy_tcd_ops = {
     945                 :            :         .get_max_state = ps_get_max_charge_cntl_limit,
     946                 :            :         .get_cur_state = ps_get_cur_charge_cntl_limit,
     947                 :            :         .set_cur_state = ps_set_cur_charge_cntl_limit,
     948                 :            : };
     949                 :            : 
     950                 :          0 : static int psy_register_cooler(struct power_supply *psy)
     951                 :            : {
     952                 :            :         int i;
     953                 :            : 
     954                 :            :         /* Register for cooling device if psy can control charging */
     955         [ #  # ]:          0 :         for (i = 0; i < psy->desc->num_properties; i++) {
     956         [ #  # ]:          0 :                 if (psy->desc->properties[i] ==
     957                 :            :                                 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
     958                 :          0 :                         psy->tcd = thermal_cooling_device_register(
     959                 :          0 :                                                         (char *)psy->desc->name,
     960                 :            :                                                         psy, &psy_tcd_ops);
     961                 :          0 :                         return PTR_ERR_OR_ZERO(psy->tcd);
     962                 :            :                 }
     963                 :            :         }
     964                 :            :         return 0;
     965                 :            : }
     966                 :            : 
     967                 :          0 : static void psy_unregister_cooler(struct power_supply *psy)
     968                 :            : {
     969         [ #  # ]:          0 :         if (IS_ERR_OR_NULL(psy->tcd))
     970                 :          0 :                 return;
     971                 :          0 :         thermal_cooling_device_unregister(psy->tcd);
     972                 :            : }
     973                 :            : #else
     974                 :            : static int psy_register_thermal(struct power_supply *psy)
     975                 :            : {
     976                 :            :         return 0;
     977                 :            : }
     978                 :            : 
     979                 :            : static void psy_unregister_thermal(struct power_supply *psy)
     980                 :            : {
     981                 :            : }
     982                 :            : 
     983                 :            : static int psy_register_cooler(struct power_supply *psy)
     984                 :            : {
     985                 :            :         return 0;
     986                 :            : }
     987                 :            : 
     988                 :            : static void psy_unregister_cooler(struct power_supply *psy)
     989                 :            : {
     990                 :            : }
     991                 :            : #endif
     992                 :            : 
     993                 :            : static struct power_supply *__must_check
     994                 :          0 : __power_supply_register(struct device *parent,
     995                 :            :                                    const struct power_supply_desc *desc,
     996                 :            :                                    const struct power_supply_config *cfg,
     997                 :            :                                    bool ws)
     998                 :            : {
     999                 :            :         struct device *dev;
    1000                 :            :         struct power_supply *psy;
    1001                 :            :         int i, rc;
    1002                 :            : 
    1003         [ #  # ]:          0 :         if (!parent)
    1004                 :          0 :                 pr_warn("%s: Expected proper parent device for '%s'\n",
    1005                 :            :                         __func__, desc->name);
    1006                 :            : 
    1007   [ #  #  #  #  :          0 :         if (!desc || !desc->name || !desc->properties || !desc->num_properties)
             #  #  #  # ]
    1008                 :            :                 return ERR_PTR(-EINVAL);
    1009                 :            : 
    1010         [ #  # ]:          0 :         for (i = 0; i < desc->num_properties; ++i) {
    1011   [ #  #  #  # ]:          0 :                 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
    1012         [ #  # ]:          0 :                     (!desc->usb_types || !desc->num_usb_types))
    1013                 :            :                         return ERR_PTR(-EINVAL);
    1014                 :            :         }
    1015                 :            : 
    1016                 :          0 :         psy = kzalloc(sizeof(*psy), GFP_KERNEL);
    1017         [ #  # ]:          0 :         if (!psy)
    1018                 :            :                 return ERR_PTR(-ENOMEM);
    1019                 :            : 
    1020                 :          0 :         dev = &psy->dev;
    1021                 :            : 
    1022                 :          0 :         device_initialize(dev);
    1023                 :            : 
    1024                 :          0 :         dev->class = power_supply_class;
    1025                 :          0 :         dev->type = &power_supply_dev_type;
    1026                 :          0 :         dev->parent = parent;
    1027                 :          0 :         dev->release = power_supply_dev_release;
    1028                 :            :         dev_set_drvdata(dev, psy);
    1029                 :          0 :         psy->desc = desc;
    1030         [ #  # ]:          0 :         if (cfg) {
    1031                 :          0 :                 dev->groups = cfg->attr_grp;
    1032                 :          0 :                 psy->drv_data = cfg->drv_data;
    1033                 :          0 :                 psy->of_node =
    1034   [ #  #  #  # ]:          0 :                         cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
    1035                 :          0 :                 psy->supplied_to = cfg->supplied_to;
    1036                 :          0 :                 psy->num_supplicants = cfg->num_supplicants;
    1037                 :            :         }
    1038                 :            : 
    1039                 :          0 :         rc = dev_set_name(dev, "%s", desc->name);
    1040         [ #  # ]:          0 :         if (rc)
    1041                 :            :                 goto dev_set_name_failed;
    1042                 :            : 
    1043                 :          0 :         INIT_WORK(&psy->changed_work, power_supply_changed_work);
    1044                 :          0 :         INIT_DELAYED_WORK(&psy->deferred_register_work,
    1045                 :            :                           power_supply_deferred_register_work);
    1046                 :            : 
    1047                 :          0 :         rc = power_supply_check_supplies(psy);
    1048         [ #  # ]:          0 :         if (rc) {
    1049                 :          0 :                 dev_info(dev, "Not all required supplies found, defer probe\n");
    1050                 :          0 :                 goto check_supplies_failed;
    1051                 :            :         }
    1052                 :            : 
    1053                 :          0 :         spin_lock_init(&psy->changed_lock);
    1054                 :          0 :         rc = device_add(dev);
    1055         [ #  # ]:          0 :         if (rc)
    1056                 :            :                 goto device_add_failed;
    1057                 :            : 
    1058                 :            :         rc = device_init_wakeup(dev, ws);
    1059                 :            :         if (rc)
    1060                 :            :                 goto wakeup_init_failed;
    1061                 :            : 
    1062                 :          0 :         rc = psy_register_thermal(psy);
    1063         [ #  # ]:          0 :         if (rc)
    1064                 :            :                 goto register_thermal_failed;
    1065                 :            : 
    1066                 :          0 :         rc = psy_register_cooler(psy);
    1067         [ #  # ]:          0 :         if (rc)
    1068                 :            :                 goto register_cooler_failed;
    1069                 :            : 
    1070                 :          0 :         rc = power_supply_create_triggers(psy);
    1071         [ #  # ]:          0 :         if (rc)
    1072                 :            :                 goto create_triggers_failed;
    1073                 :            : 
    1074                 :          0 :         rc = power_supply_add_hwmon_sysfs(psy);
    1075         [ #  # ]:          0 :         if (rc)
    1076                 :            :                 goto add_hwmon_sysfs_failed;
    1077                 :            : 
    1078                 :            :         /*
    1079                 :            :          * Update use_cnt after any uevents (most notably from device_add()).
    1080                 :            :          * We are here still during driver's probe but
    1081                 :            :          * the power_supply_uevent() calls back driver's get_property
    1082                 :            :          * method so:
    1083                 :            :          * 1. Driver did not assigned the returned struct power_supply,
    1084                 :            :          * 2. Driver could not finish initialization (anything in its probe
    1085                 :            :          *    after calling power_supply_register()).
    1086                 :            :          */
    1087                 :          0 :         atomic_inc(&psy->use_cnt);
    1088                 :          0 :         psy->initialized = true;
    1089                 :            : 
    1090                 :          0 :         queue_delayed_work(system_power_efficient_wq,
    1091                 :            :                            &psy->deferred_register_work,
    1092                 :            :                            POWER_SUPPLY_DEFERRED_REGISTER_TIME);
    1093                 :            : 
    1094                 :          0 :         return psy;
    1095                 :            : 
    1096                 :            : add_hwmon_sysfs_failed:
    1097                 :          0 :         power_supply_remove_triggers(psy);
    1098                 :            : create_triggers_failed:
    1099                 :          0 :         psy_unregister_cooler(psy);
    1100                 :            : register_cooler_failed:
    1101                 :          0 :         psy_unregister_thermal(psy);
    1102                 :            : register_thermal_failed:
    1103                 :          0 :         device_del(dev);
    1104                 :            : wakeup_init_failed:
    1105                 :            : device_add_failed:
    1106                 :            : check_supplies_failed:
    1107                 :            : dev_set_name_failed:
    1108                 :          0 :         put_device(dev);
    1109                 :          0 :         return ERR_PTR(rc);
    1110                 :            : }
    1111                 :            : 
    1112                 :            : /**
    1113                 :            :  * power_supply_register() - Register new power supply
    1114                 :            :  * @parent:     Device to be a parent of power supply's device, usually
    1115                 :            :  *              the device which probe function calls this
    1116                 :            :  * @desc:       Description of power supply, must be valid through whole
    1117                 :            :  *              lifetime of this power supply
    1118                 :            :  * @cfg:        Run-time specific configuration accessed during registering,
    1119                 :            :  *              may be NULL
    1120                 :            :  *
    1121                 :            :  * Return: A pointer to newly allocated power_supply on success
    1122                 :            :  * or ERR_PTR otherwise.
    1123                 :            :  * Use power_supply_unregister() on returned power_supply pointer to release
    1124                 :            :  * resources.
    1125                 :            :  */
    1126                 :          0 : struct power_supply *__must_check power_supply_register(struct device *parent,
    1127                 :            :                 const struct power_supply_desc *desc,
    1128                 :            :                 const struct power_supply_config *cfg)
    1129                 :            : {
    1130                 :          0 :         return __power_supply_register(parent, desc, cfg, true);
    1131                 :            : }
    1132                 :            : EXPORT_SYMBOL_GPL(power_supply_register);
    1133                 :            : 
    1134                 :            : /**
    1135                 :            :  * power_supply_register_no_ws() - Register new non-waking-source power supply
    1136                 :            :  * @parent:     Device to be a parent of power supply's device, usually
    1137                 :            :  *              the device which probe function calls this
    1138                 :            :  * @desc:       Description of power supply, must be valid through whole
    1139                 :            :  *              lifetime of this power supply
    1140                 :            :  * @cfg:        Run-time specific configuration accessed during registering,
    1141                 :            :  *              may be NULL
    1142                 :            :  *
    1143                 :            :  * Return: A pointer to newly allocated power_supply on success
    1144                 :            :  * or ERR_PTR otherwise.
    1145                 :            :  * Use power_supply_unregister() on returned power_supply pointer to release
    1146                 :            :  * resources.
    1147                 :            :  */
    1148                 :            : struct power_supply *__must_check
    1149                 :          0 : power_supply_register_no_ws(struct device *parent,
    1150                 :            :                 const struct power_supply_desc *desc,
    1151                 :            :                 const struct power_supply_config *cfg)
    1152                 :            : {
    1153                 :          0 :         return __power_supply_register(parent, desc, cfg, false);
    1154                 :            : }
    1155                 :            : EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
    1156                 :            : 
    1157                 :          0 : static void devm_power_supply_release(struct device *dev, void *res)
    1158                 :            : {
    1159                 :            :         struct power_supply **psy = res;
    1160                 :            : 
    1161                 :          0 :         power_supply_unregister(*psy);
    1162                 :          0 : }
    1163                 :            : 
    1164                 :            : /**
    1165                 :            :  * devm_power_supply_register() - Register managed power supply
    1166                 :            :  * @parent:     Device to be a parent of power supply's device, usually
    1167                 :            :  *              the device which probe function calls this
    1168                 :            :  * @desc:       Description of power supply, must be valid through whole
    1169                 :            :  *              lifetime of this power supply
    1170                 :            :  * @cfg:        Run-time specific configuration accessed during registering,
    1171                 :            :  *              may be NULL
    1172                 :            :  *
    1173                 :            :  * Return: A pointer to newly allocated power_supply on success
    1174                 :            :  * or ERR_PTR otherwise.
    1175                 :            :  * The returned power_supply pointer will be automatically unregistered
    1176                 :            :  * on driver detach.
    1177                 :            :  */
    1178                 :            : struct power_supply *__must_check
    1179                 :          0 : devm_power_supply_register(struct device *parent,
    1180                 :            :                 const struct power_supply_desc *desc,
    1181                 :            :                 const struct power_supply_config *cfg)
    1182                 :            : {
    1183                 :            :         struct power_supply **ptr, *psy;
    1184                 :            : 
    1185                 :            :         ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
    1186                 :            : 
    1187         [ #  # ]:          0 :         if (!ptr)
    1188                 :            :                 return ERR_PTR(-ENOMEM);
    1189                 :          0 :         psy = __power_supply_register(parent, desc, cfg, true);
    1190         [ #  # ]:          0 :         if (IS_ERR(psy)) {
    1191                 :          0 :                 devres_free(ptr);
    1192                 :            :         } else {
    1193                 :          0 :                 *ptr = psy;
    1194                 :          0 :                 devres_add(parent, ptr);
    1195                 :            :         }
    1196                 :          0 :         return psy;
    1197                 :            : }
    1198                 :            : EXPORT_SYMBOL_GPL(devm_power_supply_register);
    1199                 :            : 
    1200                 :            : /**
    1201                 :            :  * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
    1202                 :            :  * @parent:     Device to be a parent of power supply's device, usually
    1203                 :            :  *              the device which probe function calls this
    1204                 :            :  * @desc:       Description of power supply, must be valid through whole
    1205                 :            :  *              lifetime of this power supply
    1206                 :            :  * @cfg:        Run-time specific configuration accessed during registering,
    1207                 :            :  *              may be NULL
    1208                 :            :  *
    1209                 :            :  * Return: A pointer to newly allocated power_supply on success
    1210                 :            :  * or ERR_PTR otherwise.
    1211                 :            :  * The returned power_supply pointer will be automatically unregistered
    1212                 :            :  * on driver detach.
    1213                 :            :  */
    1214                 :            : struct power_supply *__must_check
    1215                 :          0 : devm_power_supply_register_no_ws(struct device *parent,
    1216                 :            :                 const struct power_supply_desc *desc,
    1217                 :            :                 const struct power_supply_config *cfg)
    1218                 :            : {
    1219                 :            :         struct power_supply **ptr, *psy;
    1220                 :            : 
    1221                 :            :         ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
    1222                 :            : 
    1223         [ #  # ]:          0 :         if (!ptr)
    1224                 :            :                 return ERR_PTR(-ENOMEM);
    1225                 :          0 :         psy = __power_supply_register(parent, desc, cfg, false);
    1226         [ #  # ]:          0 :         if (IS_ERR(psy)) {
    1227                 :          0 :                 devres_free(ptr);
    1228                 :            :         } else {
    1229                 :          0 :                 *ptr = psy;
    1230                 :          0 :                 devres_add(parent, ptr);
    1231                 :            :         }
    1232                 :          0 :         return psy;
    1233                 :            : }
    1234                 :            : EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
    1235                 :            : 
    1236                 :            : /**
    1237                 :            :  * power_supply_unregister() - Remove this power supply from system
    1238                 :            :  * @psy:        Pointer to power supply to unregister
    1239                 :            :  *
    1240                 :            :  * Remove this power supply from the system. The resources of power supply
    1241                 :            :  * will be freed here or on last power_supply_put() call.
    1242                 :            :  */
    1243                 :          0 : void power_supply_unregister(struct power_supply *psy)
    1244                 :            : {
    1245         [ #  # ]:          0 :         WARN_ON(atomic_dec_return(&psy->use_cnt));
    1246                 :          0 :         psy->removing = true;
    1247                 :          0 :         cancel_work_sync(&psy->changed_work);
    1248                 :          0 :         cancel_delayed_work_sync(&psy->deferred_register_work);
    1249                 :          0 :         sysfs_remove_link(&psy->dev.kobj, "powers");
    1250                 :          0 :         power_supply_remove_hwmon_sysfs(psy);
    1251                 :          0 :         power_supply_remove_triggers(psy);
    1252                 :          0 :         psy_unregister_cooler(psy);
    1253                 :          0 :         psy_unregister_thermal(psy);
    1254                 :            :         device_init_wakeup(&psy->dev, false);
    1255                 :          0 :         device_unregister(&psy->dev);
    1256                 :          0 : }
    1257                 :            : EXPORT_SYMBOL_GPL(power_supply_unregister);
    1258                 :            : 
    1259                 :          0 : void *power_supply_get_drvdata(struct power_supply *psy)
    1260                 :            : {
    1261                 :          0 :         return psy->drv_data;
    1262                 :            : }
    1263                 :            : EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
    1264                 :            : 
    1265                 :        207 : static int __init power_supply_class_init(void)
    1266                 :            : {
    1267                 :        207 :         power_supply_class = class_create(THIS_MODULE, "power_supply");
    1268                 :            : 
    1269         [ -  + ]:        207 :         if (IS_ERR(power_supply_class))
    1270                 :          0 :                 return PTR_ERR(power_supply_class);
    1271                 :            : 
    1272                 :        207 :         power_supply_class->dev_uevent = power_supply_uevent;
    1273                 :        207 :         power_supply_init_attrs(&power_supply_dev_type);
    1274                 :            : 
    1275                 :        207 :         return 0;
    1276                 :            : }
    1277                 :            : 
    1278                 :          0 : static void __exit power_supply_class_exit(void)
    1279                 :            : {
    1280                 :          0 :         class_destroy(power_supply_class);
    1281                 :          0 : }
    1282                 :            : 
    1283                 :            : subsys_initcall(power_supply_class_init);
    1284                 :            : module_exit(power_supply_class_exit);
    1285                 :            : 
    1286                 :            : MODULE_DESCRIPTION("Universal power supply monitor class");
    1287                 :            : MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
    1288                 :            :               "Szabolcs Gyurko, "
    1289                 :            :               "Anton Vorontsov <cbou@mail.ru>");
    1290                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.14