root/drivers/power/supply/sbs-charger.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sbs_get_property
  2. sbs_check_state
  3. sbs_delayed_work
  4. sbs_irq_thread
  5. sbs_readable_reg
  6. sbs_volatile_reg
  7. sbs_probe
  8. sbs_remove

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * Copyright (c) 2016, Prodys S.L.
   4  *
   5  * This adds support for sbs-charger compilant chips as defined here:
   6  * http://sbs-forum.org/specs/sbc110.pdf
   7  *
   8  * Implemetation based on sbs-battery.c
   9  */
  10 
  11 #include <linux/init.h>
  12 #include <linux/module.h>
  13 #include <linux/kernel.h>
  14 #include <linux/err.h>
  15 #include <linux/power_supply.h>
  16 #include <linux/i2c.h>
  17 #include <linux/slab.h>
  18 #include <linux/interrupt.h>
  19 #include <linux/gpio.h>
  20 #include <linux/regmap.h>
  21 #include <linux/of_gpio.h>
  22 #include <linux/bitops.h>
  23 
  24 #define SBS_CHARGER_REG_SPEC_INFO               0x11
  25 #define SBS_CHARGER_REG_STATUS                  0x13
  26 #define SBS_CHARGER_REG_ALARM_WARNING           0x16
  27 
  28 #define SBS_CHARGER_STATUS_CHARGE_INHIBITED     BIT(1)
  29 #define SBS_CHARGER_STATUS_RES_COLD             BIT(9)
  30 #define SBS_CHARGER_STATUS_RES_HOT              BIT(10)
  31 #define SBS_CHARGER_STATUS_BATTERY_PRESENT      BIT(14)
  32 #define SBS_CHARGER_STATUS_AC_PRESENT           BIT(15)
  33 
  34 #define SBS_CHARGER_POLL_TIME                   500
  35 
  36 struct sbs_info {
  37         struct i2c_client               *client;
  38         struct power_supply             *power_supply;
  39         struct regmap                   *regmap;
  40         struct delayed_work             work;
  41         unsigned int                    last_state;
  42 };
  43 
  44 static int sbs_get_property(struct power_supply *psy,
  45                             enum power_supply_property psp,
  46                             union power_supply_propval *val)
  47 {
  48         struct sbs_info *chip = power_supply_get_drvdata(psy);
  49         unsigned int reg;
  50 
  51         reg = chip->last_state;
  52 
  53         switch (psp) {
  54         case POWER_SUPPLY_PROP_PRESENT:
  55                 val->intval = !!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT);
  56                 break;
  57 
  58         case POWER_SUPPLY_PROP_ONLINE:
  59                 val->intval = !!(reg & SBS_CHARGER_STATUS_AC_PRESENT);
  60                 break;
  61 
  62         case POWER_SUPPLY_PROP_STATUS:
  63                 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  64 
  65                 if (!(reg & SBS_CHARGER_STATUS_BATTERY_PRESENT))
  66                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  67                 else if (reg & SBS_CHARGER_STATUS_AC_PRESENT &&
  68                          !(reg & SBS_CHARGER_STATUS_CHARGE_INHIBITED))
  69                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
  70                 else
  71                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  72 
  73                 break;
  74 
  75         case POWER_SUPPLY_PROP_HEALTH:
  76                 if (reg & SBS_CHARGER_STATUS_RES_COLD)
  77                         val->intval = POWER_SUPPLY_HEALTH_COLD;
  78                 if (reg & SBS_CHARGER_STATUS_RES_HOT)
  79                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  80                 else
  81                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
  82 
  83                 break;
  84 
  85         default:
  86                 return -EINVAL;
  87         }
  88 
  89         return 0;
  90 }
  91 
  92 static int sbs_check_state(struct sbs_info *chip)
  93 {
  94         unsigned int reg;
  95         int ret;
  96 
  97         ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &reg);
  98         if (!ret && reg != chip->last_state) {
  99                 chip->last_state = reg;
 100                 power_supply_changed(chip->power_supply);
 101                 return 1;
 102         }
 103 
 104         return 0;
 105 }
 106 
 107 static void sbs_delayed_work(struct work_struct *work)
 108 {
 109         struct sbs_info *chip = container_of(work, struct sbs_info, work.work);
 110 
 111         sbs_check_state(chip);
 112 
 113         schedule_delayed_work(&chip->work,
 114                               msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
 115 }
 116 
 117 static irqreturn_t sbs_irq_thread(int irq, void *data)
 118 {
 119         struct sbs_info *chip = data;
 120         int ret;
 121 
 122         ret = sbs_check_state(chip);
 123 
 124         return ret ? IRQ_HANDLED : IRQ_NONE;
 125 }
 126 
 127 static enum power_supply_property sbs_properties[] = {
 128         POWER_SUPPLY_PROP_STATUS,
 129         POWER_SUPPLY_PROP_PRESENT,
 130         POWER_SUPPLY_PROP_ONLINE,
 131         POWER_SUPPLY_PROP_HEALTH,
 132 };
 133 
 134 static bool sbs_readable_reg(struct device *dev, unsigned int reg)
 135 {
 136         return reg >= SBS_CHARGER_REG_SPEC_INFO;
 137 }
 138 
 139 static bool sbs_volatile_reg(struct device *dev, unsigned int reg)
 140 {
 141         switch (reg) {
 142         case SBS_CHARGER_REG_STATUS:
 143                 return true;
 144         }
 145 
 146         return false;
 147 }
 148 
 149 static const struct regmap_config sbs_regmap = {
 150         .reg_bits       = 8,
 151         .val_bits       = 16,
 152         .max_register   = SBS_CHARGER_REG_ALARM_WARNING,
 153         .readable_reg   = sbs_readable_reg,
 154         .volatile_reg   = sbs_volatile_reg,
 155         .val_format_endian = REGMAP_ENDIAN_LITTLE, /* since based on SMBus */
 156 };
 157 
 158 static const struct power_supply_desc sbs_desc = {
 159         .name = "sbs-charger",
 160         .type = POWER_SUPPLY_TYPE_MAINS,
 161         .properties = sbs_properties,
 162         .num_properties = ARRAY_SIZE(sbs_properties),
 163         .get_property = sbs_get_property,
 164 };
 165 
 166 static int sbs_probe(struct i2c_client *client,
 167                      const struct i2c_device_id *id)
 168 {
 169         struct power_supply_config psy_cfg = {};
 170         struct sbs_info *chip;
 171         int ret, val;
 172 
 173         chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
 174         if (!chip)
 175                 return -ENOMEM;
 176 
 177         chip->client = client;
 178         psy_cfg.of_node = client->dev.of_node;
 179         psy_cfg.drv_data = chip;
 180 
 181         i2c_set_clientdata(client, chip);
 182 
 183         chip->regmap = devm_regmap_init_i2c(client, &sbs_regmap);
 184         if (IS_ERR(chip->regmap))
 185                 return PTR_ERR(chip->regmap);
 186 
 187         /*
 188          * Before we register, we need to make sure we can actually talk
 189          * to the battery.
 190          */
 191         ret = regmap_read(chip->regmap, SBS_CHARGER_REG_STATUS, &val);
 192         if (ret) {
 193                 dev_err(&client->dev, "Failed to get device status\n");
 194                 return ret;
 195         }
 196         chip->last_state = val;
 197 
 198         chip->power_supply = devm_power_supply_register(&client->dev, &sbs_desc,
 199                                                         &psy_cfg);
 200         if (IS_ERR(chip->power_supply)) {
 201                 dev_err(&client->dev, "Failed to register power supply\n");
 202                 return PTR_ERR(chip->power_supply);
 203         }
 204 
 205         /*
 206          * The sbs-charger spec doesn't impose the use of an interrupt. So in
 207          * the case it wasn't provided we use polling in order get the charger's
 208          * status.
 209          */
 210         if (client->irq) {
 211                 ret = devm_request_threaded_irq(&client->dev, client->irq,
 212                                         NULL, sbs_irq_thread,
 213                                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 214                                         dev_name(&client->dev), chip);
 215                 if (ret) {
 216                         dev_err(&client->dev, "Failed to request irq, %d\n", ret);
 217                         return ret;
 218                 }
 219         } else {
 220                 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
 221                 schedule_delayed_work(&chip->work,
 222                                       msecs_to_jiffies(SBS_CHARGER_POLL_TIME));
 223         }
 224 
 225         dev_info(&client->dev,
 226                  "%s: smart charger device registered\n", client->name);
 227 
 228         return 0;
 229 }
 230 
 231 static int sbs_remove(struct i2c_client *client)
 232 {
 233         struct sbs_info *chip = i2c_get_clientdata(client);
 234 
 235         cancel_delayed_work_sync(&chip->work);
 236 
 237         return 0;
 238 }
 239 
 240 #ifdef CONFIG_OF
 241 static const struct of_device_id sbs_dt_ids[] = {
 242         { .compatible = "sbs,sbs-charger" },
 243         { },
 244 };
 245 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
 246 #endif
 247 
 248 static const struct i2c_device_id sbs_id[] = {
 249         { "sbs-charger", 0 },
 250         { }
 251 };
 252 MODULE_DEVICE_TABLE(i2c, sbs_id);
 253 
 254 static struct i2c_driver sbs_driver = {
 255         .probe          = sbs_probe,
 256         .remove         = sbs_remove,
 257         .id_table       = sbs_id,
 258         .driver = {
 259                 .name   = "sbs-charger",
 260                 .of_match_table = of_match_ptr(sbs_dt_ids),
 261         },
 262 };
 263 module_i2c_driver(sbs_driver);
 264 
 265 MODULE_AUTHOR("Nicolas Saenz Julienne <nicolassaenzj@gmail.com>");
 266 MODULE_DESCRIPTION("SBS smart charger driver");
 267 MODULE_LICENSE("GPL v2");

/* [<][>][^][v][top][bottom][index][help] */