Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Generic battery driver code using IIO
0003  * Copyright (C) 2012, Anish Kumar <anish198519851985@gmail.com>
0004  * based on jz4740-battery.c
0005  * based on s3c_adc_battery.c
0006  *
0007  * This file is subject to the terms and conditions of the GNU General Public
0008  * License.  See the file COPYING in the main directory of this archive for
0009  * more details.
0010  *
0011  */
0012 #include <linux/interrupt.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/power_supply.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/err.h>
0017 #include <linux/timer.h>
0018 #include <linux/jiffies.h>
0019 #include <linux/errno.h>
0020 #include <linux/init.h>
0021 #include <linux/module.h>
0022 #include <linux/slab.h>
0023 #include <linux/iio/consumer.h>
0024 #include <linux/iio/types.h>
0025 #include <linux/power/generic-adc-battery.h>
0026 
0027 #define JITTER_DEFAULT 10 /* hope 10ms is enough */
0028 
0029 enum gab_chan_type {
0030     GAB_VOLTAGE = 0,
0031     GAB_CURRENT,
0032     GAB_POWER,
0033     GAB_MAX_CHAN_TYPE
0034 };
0035 
0036 /*
0037  * gab_chan_name suggests the standard channel names for commonly used
0038  * channel types.
0039  */
0040 static const char *const gab_chan_name[] = {
0041     [GAB_VOLTAGE]   = "voltage",
0042     [GAB_CURRENT]   = "current",
0043     [GAB_POWER]     = "power",
0044 };
0045 
0046 struct gab {
0047     struct power_supply     *psy;
0048     struct power_supply_desc    psy_desc;
0049     struct iio_channel  *channel[GAB_MAX_CHAN_TYPE];
0050     struct gab_platform_data    *pdata;
0051     struct delayed_work bat_work;
0052     int level;
0053     int status;
0054     bool cable_plugged;
0055     struct gpio_desc *charge_finished;
0056 };
0057 
0058 static struct gab *to_generic_bat(struct power_supply *psy)
0059 {
0060     return power_supply_get_drvdata(psy);
0061 }
0062 
0063 static void gab_ext_power_changed(struct power_supply *psy)
0064 {
0065     struct gab *adc_bat = to_generic_bat(psy);
0066 
0067     schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0));
0068 }
0069 
0070 static const enum power_supply_property gab_props[] = {
0071     POWER_SUPPLY_PROP_STATUS,
0072     POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
0073     POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
0074     POWER_SUPPLY_PROP_CHARGE_NOW,
0075     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0076     POWER_SUPPLY_PROP_CURRENT_NOW,
0077     POWER_SUPPLY_PROP_TECHNOLOGY,
0078     POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
0079     POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
0080     POWER_SUPPLY_PROP_MODEL_NAME,
0081 };
0082 
0083 /*
0084  * This properties are set based on the received platform data and this
0085  * should correspond one-to-one with enum chan_type.
0086  */
0087 static const enum power_supply_property gab_dyn_props[] = {
0088     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0089     POWER_SUPPLY_PROP_CURRENT_NOW,
0090     POWER_SUPPLY_PROP_POWER_NOW,
0091 };
0092 
0093 static bool gab_charge_finished(struct gab *adc_bat)
0094 {
0095     if (!adc_bat->charge_finished)
0096         return false;
0097     return gpiod_get_value(adc_bat->charge_finished);
0098 }
0099 
0100 static int gab_get_status(struct gab *adc_bat)
0101 {
0102     struct gab_platform_data *pdata = adc_bat->pdata;
0103     struct power_supply_info *bat_info;
0104 
0105     bat_info = &pdata->battery_info;
0106     if (adc_bat->level == bat_info->charge_full_design)
0107         return POWER_SUPPLY_STATUS_FULL;
0108     return adc_bat->status;
0109 }
0110 
0111 static enum gab_chan_type gab_prop_to_chan(enum power_supply_property psp)
0112 {
0113     switch (psp) {
0114     case POWER_SUPPLY_PROP_POWER_NOW:
0115         return GAB_POWER;
0116     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0117         return GAB_VOLTAGE;
0118     case POWER_SUPPLY_PROP_CURRENT_NOW:
0119         return GAB_CURRENT;
0120     default:
0121         WARN_ON(1);
0122         break;
0123     }
0124     return GAB_POWER;
0125 }
0126 
0127 static int read_channel(struct gab *adc_bat, enum power_supply_property psp,
0128         int *result)
0129 {
0130     int ret;
0131     int chan_index;
0132 
0133     chan_index = gab_prop_to_chan(psp);
0134     ret = iio_read_channel_processed(adc_bat->channel[chan_index],
0135             result);
0136     if (ret < 0)
0137         pr_err("read channel error\n");
0138     return ret;
0139 }
0140 
0141 static int gab_get_property(struct power_supply *psy,
0142         enum power_supply_property psp, union power_supply_propval *val)
0143 {
0144     struct gab *adc_bat;
0145     struct gab_platform_data *pdata;
0146     struct power_supply_info *bat_info;
0147     int result = 0;
0148     int ret = 0;
0149 
0150     adc_bat = to_generic_bat(psy);
0151     if (!adc_bat) {
0152         dev_err(&psy->dev, "no battery infos ?!\n");
0153         return -EINVAL;
0154     }
0155     pdata = adc_bat->pdata;
0156     bat_info = &pdata->battery_info;
0157 
0158     switch (psp) {
0159     case POWER_SUPPLY_PROP_STATUS:
0160         val->intval = gab_get_status(adc_bat);
0161         break;
0162     case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
0163         val->intval = 0;
0164         break;
0165     case POWER_SUPPLY_PROP_CHARGE_NOW:
0166         val->intval = pdata->cal_charge(result);
0167         break;
0168     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0169     case POWER_SUPPLY_PROP_CURRENT_NOW:
0170     case POWER_SUPPLY_PROP_POWER_NOW:
0171         ret = read_channel(adc_bat, psp, &result);
0172         if (ret < 0)
0173             goto err;
0174         val->intval = result;
0175         break;
0176     case POWER_SUPPLY_PROP_TECHNOLOGY:
0177         val->intval = bat_info->technology;
0178         break;
0179     case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
0180         val->intval = bat_info->voltage_min_design;
0181         break;
0182     case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
0183         val->intval = bat_info->voltage_max_design;
0184         break;
0185     case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
0186         val->intval = bat_info->charge_full_design;
0187         break;
0188     case POWER_SUPPLY_PROP_MODEL_NAME:
0189         val->strval = bat_info->name;
0190         break;
0191     default:
0192         return -EINVAL;
0193     }
0194 err:
0195     return ret;
0196 }
0197 
0198 static void gab_work(struct work_struct *work)
0199 {
0200     struct gab *adc_bat;
0201     struct delayed_work *delayed_work;
0202     bool is_plugged;
0203     int status;
0204 
0205     delayed_work = to_delayed_work(work);
0206     adc_bat = container_of(delayed_work, struct gab, bat_work);
0207     status = adc_bat->status;
0208 
0209     is_plugged = power_supply_am_i_supplied(adc_bat->psy);
0210     adc_bat->cable_plugged = is_plugged;
0211 
0212     if (!is_plugged)
0213         adc_bat->status =  POWER_SUPPLY_STATUS_DISCHARGING;
0214     else if (gab_charge_finished(adc_bat))
0215         adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
0216     else
0217         adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
0218 
0219     if (status != adc_bat->status)
0220         power_supply_changed(adc_bat->psy);
0221 }
0222 
0223 static irqreturn_t gab_charged(int irq, void *dev_id)
0224 {
0225     struct gab *adc_bat = dev_id;
0226     struct gab_platform_data *pdata = adc_bat->pdata;
0227     int delay;
0228 
0229     delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
0230     schedule_delayed_work(&adc_bat->bat_work,
0231             msecs_to_jiffies(delay));
0232     return IRQ_HANDLED;
0233 }
0234 
0235 static int gab_probe(struct platform_device *pdev)
0236 {
0237     struct gab *adc_bat;
0238     struct power_supply_desc *psy_desc;
0239     struct power_supply_config psy_cfg = {};
0240     struct gab_platform_data *pdata = pdev->dev.platform_data;
0241     enum power_supply_property *properties;
0242     int ret = 0;
0243     int chan;
0244     int index = ARRAY_SIZE(gab_props);
0245     bool any = false;
0246 
0247     adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
0248     if (!adc_bat) {
0249         dev_err(&pdev->dev, "failed to allocate memory\n");
0250         return -ENOMEM;
0251     }
0252 
0253     psy_cfg.drv_data = adc_bat;
0254     psy_desc = &adc_bat->psy_desc;
0255     psy_desc->name = pdata->battery_info.name;
0256 
0257     /* bootup default values for the battery */
0258     adc_bat->cable_plugged = false;
0259     adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
0260     psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
0261     psy_desc->get_property = gab_get_property;
0262     psy_desc->external_power_changed = gab_ext_power_changed;
0263     adc_bat->pdata = pdata;
0264 
0265     /*
0266      * copying the static properties and allocating extra memory for holding
0267      * the extra configurable properties received from platform data.
0268      */
0269     properties = kcalloc(ARRAY_SIZE(gab_props) +
0270                  ARRAY_SIZE(gab_chan_name),
0271                  sizeof(*properties),
0272                  GFP_KERNEL);
0273     if (!properties) {
0274         ret = -ENOMEM;
0275         goto first_mem_fail;
0276     }
0277 
0278     memcpy(properties, gab_props, sizeof(gab_props));
0279 
0280     /*
0281      * getting channel from iio and copying the battery properties
0282      * based on the channel supported by consumer device.
0283      */
0284     for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
0285         adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
0286                              gab_chan_name[chan]);
0287         if (IS_ERR(adc_bat->channel[chan])) {
0288             ret = PTR_ERR(adc_bat->channel[chan]);
0289             adc_bat->channel[chan] = NULL;
0290         } else {
0291             /* copying properties for supported channels only */
0292             int index2;
0293 
0294             for (index2 = 0; index2 < index; index2++) {
0295                 if (properties[index2] == gab_dyn_props[chan])
0296                     break;  /* already known */
0297             }
0298             if (index2 == index)    /* really new */
0299                 properties[index++] = gab_dyn_props[chan];
0300             any = true;
0301         }
0302     }
0303 
0304     /* none of the channels are supported so let's bail out */
0305     if (!any) {
0306         ret = -ENODEV;
0307         goto second_mem_fail;
0308     }
0309 
0310     /*
0311      * Total number of properties is equal to static properties
0312      * plus the dynamic properties.Some properties may not be set
0313      * as come channels may be not be supported by the device.So
0314      * we need to take care of that.
0315      */
0316     psy_desc->properties = properties;
0317     psy_desc->num_properties = index;
0318 
0319     adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
0320     if (IS_ERR(adc_bat->psy)) {
0321         ret = PTR_ERR(adc_bat->psy);
0322         goto err_reg_fail;
0323     }
0324 
0325     INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
0326 
0327     adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev,
0328                                "charged", GPIOD_IN);
0329     if (adc_bat->charge_finished) {
0330         int irq;
0331 
0332         irq = gpiod_to_irq(adc_bat->charge_finished);
0333         ret = request_any_context_irq(irq, gab_charged,
0334                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
0335                 "battery charged", adc_bat);
0336         if (ret < 0)
0337             goto gpio_req_fail;
0338     }
0339 
0340     platform_set_drvdata(pdev, adc_bat);
0341 
0342     /* Schedule timer to check current status */
0343     schedule_delayed_work(&adc_bat->bat_work,
0344             msecs_to_jiffies(0));
0345     return 0;
0346 
0347 gpio_req_fail:
0348     power_supply_unregister(adc_bat->psy);
0349 err_reg_fail:
0350     for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
0351         if (adc_bat->channel[chan])
0352             iio_channel_release(adc_bat->channel[chan]);
0353     }
0354 second_mem_fail:
0355     kfree(properties);
0356 first_mem_fail:
0357     return ret;
0358 }
0359 
0360 static int gab_remove(struct platform_device *pdev)
0361 {
0362     int chan;
0363     struct gab *adc_bat = platform_get_drvdata(pdev);
0364 
0365     power_supply_unregister(adc_bat->psy);
0366 
0367     if (adc_bat->charge_finished)
0368         free_irq(gpiod_to_irq(adc_bat->charge_finished), adc_bat);
0369 
0370     for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
0371         if (adc_bat->channel[chan])
0372             iio_channel_release(adc_bat->channel[chan]);
0373     }
0374 
0375     kfree(adc_bat->psy_desc.properties);
0376     cancel_delayed_work_sync(&adc_bat->bat_work);
0377     return 0;
0378 }
0379 
0380 static int __maybe_unused gab_suspend(struct device *dev)
0381 {
0382     struct gab *adc_bat = dev_get_drvdata(dev);
0383 
0384     cancel_delayed_work_sync(&adc_bat->bat_work);
0385     adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
0386     return 0;
0387 }
0388 
0389 static int __maybe_unused gab_resume(struct device *dev)
0390 {
0391     struct gab *adc_bat = dev_get_drvdata(dev);
0392     struct gab_platform_data *pdata = adc_bat->pdata;
0393     int delay;
0394 
0395     delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
0396 
0397     /* Schedule timer to check current status */
0398     schedule_delayed_work(&adc_bat->bat_work,
0399             msecs_to_jiffies(delay));
0400     return 0;
0401 }
0402 
0403 static SIMPLE_DEV_PM_OPS(gab_pm_ops, gab_suspend, gab_resume);
0404 
0405 static struct platform_driver gab_driver = {
0406     .driver     = {
0407         .name   = "generic-adc-battery",
0408         .pm = &gab_pm_ops,
0409     },
0410     .probe      = gab_probe,
0411     .remove     = gab_remove,
0412 };
0413 module_platform_driver(gab_driver);
0414 
0415 MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
0416 MODULE_DESCRIPTION("generic battery driver using IIO");
0417 MODULE_LICENSE("GPL");