123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- /*
- * sec_fuelgauge.c
- * Samsung Mobile Fuel Gauge Driver
- *
- * Copyright (C) 2012 Samsung Electronics
- *
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
- #include <linux/battery/sec_fuelgauge.h>
- #include <linux/battery/sec_charger.h>
- #include <linux/battery/sec_battery.h>
- #include <linux/of_gpio.h>
- static struct device_attribute sec_fg_attrs[] = {
- SEC_FG_ATTR(reg),
- SEC_FG_ATTR(data),
- SEC_FG_ATTR(regs),
- };
- static enum power_supply_property sec_fuelgauge_props[] = {
- POWER_SUPPLY_PROP_STATUS,
- POWER_SUPPLY_PROP_PRESENT,
- POWER_SUPPLY_PROP_VOLTAGE_NOW,
- POWER_SUPPLY_PROP_VOLTAGE_AVG,
- POWER_SUPPLY_PROP_CURRENT_NOW,
- POWER_SUPPLY_PROP_CURRENT_AVG,
- POWER_SUPPLY_PROP_CHARGE_FULL,
- POWER_SUPPLY_PROP_ENERGY_NOW,
- POWER_SUPPLY_PROP_CAPACITY,
- POWER_SUPPLY_PROP_TEMP,
- POWER_SUPPLY_PROP_TEMP_AMBIENT,
- POWER_SUPPLY_PROP_ENERGY_FULL,
- POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
- };
- /* capacity is 0.1% unit */
- static void sec_fg_get_scaled_capacity(
- struct sec_fuelgauge_info *fuelgauge,
- union power_supply_propval *val)
- {
- val->intval = (val->intval < fuelgauge->pdata->capacity_min) ?
- 0 : ((val->intval - fuelgauge->pdata->capacity_min) * 1000 /
- (fuelgauge->capacity_max - fuelgauge->pdata->capacity_min));
- dev_info(&fuelgauge->client->dev,
- "%s: scaled capacity (%d.%d)\n",
- __func__, val->intval/10, val->intval%10);
- #if defined(CONFIG_MACH_KLIMT)|| defined(CONFIG_MACH_CHAGALL)
- /* Reduce soc jump when battery is full
- change capacity_max to initial value */
- if (fuelgauge->is_charging) {
- if (fuelgauge->capacity_max > fuelgauge->pdata->capacity_max)
- fuelgauge->capacity_max--;
- }
- #endif
- }
- /* capacity is integer */
- static void sec_fg_get_atomic_capacity(
- struct sec_fuelgauge_info *fuelgauge,
- union power_supply_propval *val)
- {
- if (fuelgauge->pdata->capacity_calculation_type &
- SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC) {
- if (fuelgauge->capacity_old < val->intval)
- val->intval = fuelgauge->capacity_old + 1;
- else if (fuelgauge->capacity_old > val->intval)
- val->intval = fuelgauge->capacity_old - 1;
- }
- /* keep SOC stable in abnormal status */
- if (fuelgauge->pdata->capacity_calculation_type &
- SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL) {
- if (!fuelgauge->is_charging &&
- fuelgauge->capacity_old < val->intval) {
- dev_err(&fuelgauge->client->dev,
- "%s: capacity (old %d : new %d)\n",
- __func__, fuelgauge->capacity_old, val->intval);
- val->intval = fuelgauge->capacity_old;
- }
- }
- /* updated old capacity */
- fuelgauge->capacity_old = val->intval;
- }
- static int sec_fg_get_property(struct power_supply *psy,
- enum power_supply_property psp,
- union power_supply_propval *val)
- {
- struct sec_fuelgauge_info *fuelgauge =
- container_of(psy, struct sec_fuelgauge_info, psy_fg);
- int soc_type = val->intval;
- switch (psp) {
- case POWER_SUPPLY_PROP_VOLTAGE_NOW:
- case POWER_SUPPLY_PROP_VOLTAGE_AVG:
- case POWER_SUPPLY_PROP_CURRENT_NOW:
- case POWER_SUPPLY_PROP_CURRENT_AVG:
- case POWER_SUPPLY_PROP_ENERGY_NOW:
- case POWER_SUPPLY_PROP_CAPACITY:
- case POWER_SUPPLY_PROP_TEMP:
- case POWER_SUPPLY_PROP_TEMP_AMBIENT:
- case POWER_SUPPLY_PROP_ENERGY_FULL:
- if (!sec_hal_fg_get_property(fuelgauge->client, psp, val))
- return -EINVAL;
- if (psp == POWER_SUPPLY_PROP_CAPACITY) {
- if (soc_type == SEC_FUELGAUGE_CAPACITY_TYPE_RAW)
- break;
- /* check whether doing the __pm_relax */
- if ((val->intval > fuelgauge->pdata->fuel_alert_soc) &&
- fuelgauge->is_fuel_alerted) {
- __pm_relax(&fuelgauge->fuel_alert_ws);
- sec_hal_fg_fuelalert_init(fuelgauge->client,
- fuelgauge->pdata->fuel_alert_soc);
- }
- if (fuelgauge->pdata->capacity_calculation_type &
- (SEC_FUELGAUGE_CAPACITY_TYPE_SCALE |
- SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE))
- sec_fg_get_scaled_capacity(fuelgauge, val);
- /* capacity should be between 0% and 100%
- * (0.1% degree)
- */
- if (val->intval > 1000)
- val->intval = 1000;
- if (val->intval < 0)
- val->intval = 0;
- /* get only integer part */
- val->intval /= 10;
- /* (Only for atomic capacity)
- * In initial time, capacity_old is 0.
- * and in resume from sleep,
- * capacity_old is too different from actual soc.
- * should update capacity_old
- * by val->intval in booting or resume.
- */
- if (fuelgauge->initial_update_of_soc) {
- /* updated old capacity */
- fuelgauge->capacity_old = val->intval;
- fuelgauge->initial_update_of_soc = false;
- break;
- }
- if (fuelgauge->pdata->capacity_calculation_type &
- (SEC_FUELGAUGE_CAPACITY_TYPE_ATOMIC |
- SEC_FUELGAUGE_CAPACITY_TYPE_SKIP_ABNORMAL))
- sec_fg_get_atomic_capacity(fuelgauge, val);
- }
- break;
- case POWER_SUPPLY_PROP_PRESENT:
- if (fuelgauge->pdata->bat_irq_gpio > 0) {
- val->intval = !gpio_get_value(fuelgauge->pdata->bat_irq_gpio);
- if (val->intval == 0) {
- dev_info(&fuelgauge->client->dev, "%s: Battery status(%d)\n",
- __func__, val->intval);
- }
- } else {
- dev_err(&fuelgauge->client->dev, "%s: bat irq gpio is invalid (%d)\n",
- __func__, fuelgauge->pdata->bat_irq_gpio);
- val->intval = 1;
- }
- break;
- case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
- val->intval = fuelgauge->capacity_max;
- break;
- case POWER_SUPPLY_PROP_STATUS:
- case POWER_SUPPLY_PROP_CHARGE_FULL:
- return -ENODATA;
- default:
- return -EINVAL;
- }
- return 0;
- }
- static int sec_fg_calculate_dynamic_scale(
- struct sec_fuelgauge_info *fuelgauge)
- {
- union power_supply_propval raw_soc_val;
- raw_soc_val.intval = SEC_FUELGAUGE_CAPACITY_TYPE_RAW;
- if (!sec_hal_fg_get_property(fuelgauge->client,
- POWER_SUPPLY_PROP_CAPACITY,
- &raw_soc_val))
- return -EINVAL;
- raw_soc_val.intval /= 10;
- if (raw_soc_val.intval <
- fuelgauge->pdata->capacity_max -
- fuelgauge->pdata->capacity_max_margin) {
- fuelgauge->capacity_max =
- fuelgauge->pdata->capacity_max -
- fuelgauge->pdata->capacity_max_margin;
- dev_dbg(&fuelgauge->client->dev, "%s: capacity_max (%d)",
- __func__, fuelgauge->capacity_max);
- } else {
- fuelgauge->capacity_max =
- (raw_soc_val.intval >
- fuelgauge->pdata->capacity_max +
- fuelgauge->pdata->capacity_max_margin) ?
- (fuelgauge->pdata->capacity_max +
- fuelgauge->pdata->capacity_max_margin) :
- raw_soc_val.intval;
- dev_dbg(&fuelgauge->client->dev, "%s: raw soc (%d)",
- __func__, fuelgauge->capacity_max);
- }
- fuelgauge->capacity_max =
- (fuelgauge->capacity_max * 99 / 100);
- /* update capacity_old for sec_fg_get_atomic_capacity algorithm */
- fuelgauge->capacity_old = 100;
- dev_info(&fuelgauge->client->dev, "%s: %d is used for capacity_max\n",
- __func__, fuelgauge->capacity_max);
- return fuelgauge->capacity_max;
- }
- static int sec_fg_set_property(struct power_supply *psy,
- enum power_supply_property psp,
- const union power_supply_propval *val)
- {
- struct sec_fuelgauge_info *fuelgauge =
- container_of(psy, struct sec_fuelgauge_info, psy_fg);
- sec_battery_platform_data_t *pdata = fuelgauge->pdata;
- switch (psp) {
- case POWER_SUPPLY_PROP_CHARGE_TYPE:
- if (!pdata->jig_irq)
- sec_hal_fg_set_property(fuelgauge->client, psp, val);
- break;
- case POWER_SUPPLY_PROP_STATUS:
- if (val->intval == POWER_SUPPLY_STATUS_FULL)
- sec_hal_fg_full_charged(fuelgauge->client);
- break;
- case POWER_SUPPLY_PROP_CHARGE_FULL:
- if (val->intval == POWER_SUPPLY_TYPE_BATTERY) {
- if (fuelgauge->pdata->capacity_calculation_type &
- SEC_FUELGAUGE_CAPACITY_TYPE_DYNAMIC_SCALE)
- sec_fg_calculate_dynamic_scale(fuelgauge);
- }
- break;
- case POWER_SUPPLY_PROP_ONLINE:
- fuelgauge->cable_type = val->intval;
- if (val->intval == POWER_SUPPLY_TYPE_BATTERY)
- fuelgauge->is_charging = false;
- else
- fuelgauge->is_charging = true;
- /* fall through */
- case POWER_SUPPLY_PROP_CAPACITY:
- if (val->intval == SEC_FUELGAUGE_CAPACITY_TYPE_RESET) {
- fuelgauge->initial_update_of_soc = true;
- if (!sec_hal_fg_reset(fuelgauge->client))
- return -EINVAL;
- else
- break;
- }
- case POWER_SUPPLY_PROP_TEMP:
- case POWER_SUPPLY_PROP_TEMP_AMBIENT:
- if (!sec_hal_fg_set_property(fuelgauge->client, psp, val))
- return -EINVAL;
- break;
- case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
- dev_info(&fuelgauge->client->dev,
- "%s: capacity_max changed, %d -> %d\n",
- __func__, fuelgauge->capacity_max, val->intval);
- fuelgauge->capacity_max = val->intval;
- fuelgauge->initial_update_of_soc = true;
- break;
- default:
- return -EINVAL;
- }
- return 0;
- }
- static void sec_fg_isr_work(struct work_struct *work)
- {
- struct sec_fuelgauge_info *fuelgauge =
- container_of(work, struct sec_fuelgauge_info, isr_work.work);
- /* process for fuel gauge chip */
- sec_hal_fg_fuelalert_process(fuelgauge, fuelgauge->is_fuel_alerted);
- /* process for others */
- if (fuelgauge->pdata->fuelalert_process != NULL)
- fuelgauge->pdata->fuelalert_process(fuelgauge->is_fuel_alerted);
- }
- static irqreturn_t sec_fg_irq_thread(int irq, void *irq_data)
- {
- struct sec_fuelgauge_info *fuelgauge = irq_data;
- bool fuel_alerted;
- if (fuelgauge->pdata->fuel_alert_soc >= 0) {
- fuel_alerted =
- sec_hal_fg_is_fuelalerted(fuelgauge->client);
- dev_info(&fuelgauge->client->dev,
- "%s: Fuel-alert %salerted!\n",
- __func__, fuel_alerted ? "" : "NOT ");
- if (fuel_alerted == fuelgauge->is_fuel_alerted) {
- if (!fuelgauge->pdata->repeated_fuelalert) {
- dev_dbg(&fuelgauge->client->dev,
- "%s: Fuel-alert Repeated (%d)\n",
- __func__, fuelgauge->is_fuel_alerted);
- return IRQ_HANDLED;
- }
- }
- if (fuel_alerted)
- __pm_stay_awake(&fuelgauge->fuel_alert_ws);
- else
- __pm_relax(&fuelgauge->fuel_alert_ws);
- schedule_delayed_work(&fuelgauge->isr_work, 0);
- fuelgauge->is_fuel_alerted = fuel_alerted;
- }
- return IRQ_HANDLED;
- }
- static int sec_fg_create_attrs(struct device *dev)
- {
- int i, rc;
- for (i = 0; i < ARRAY_SIZE(sec_fg_attrs); i++) {
- rc = device_create_file(dev, &sec_fg_attrs[i]);
- if (rc)
- goto create_attrs_failed;
- }
- goto create_attrs_succeed;
- create_attrs_failed:
- dev_err(dev, "%s: failed (%d)\n", __func__, rc);
- while (i--)
- device_remove_file(dev, &sec_fg_attrs[i]);
- create_attrs_succeed:
- return rc;
- }
- ssize_t sec_fg_show_attrs(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- const ptrdiff_t offset = attr - sec_fg_attrs;
- int i = 0;
- switch (offset) {
- case FG_REG:
- case FG_DATA:
- case FG_REGS:
- i = sec_hal_fg_show_attrs(dev, offset, buf);
- break;
- default:
- i = -EINVAL;
- break;
- }
- return i;
- }
- ssize_t sec_fg_store_attrs(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
- {
- const ptrdiff_t offset = attr - sec_fg_attrs;
- int ret = 0;
- switch (offset) {
- case FG_REG:
- case FG_DATA:
- ret = sec_hal_fg_store_attrs(dev, offset, buf, count);
- break;
- default:
- ret = -EINVAL;
- break;
- }
- return ret;
- }
- #ifdef CONFIG_OF
- static int fuelgauge_parse_dt(struct device *dev,
- struct sec_fuelgauge_info *fuelgauge)
- {
- struct device_node *np = dev->of_node;
- #if !defined(CONFIG_FUELGAUGE_MAX17050)
- struct device_node *bnp = of_find_node_by_name(NULL, "battery");
- #endif
- sec_battery_platform_data_t *pdata = fuelgauge->pdata;
- int ret;
- #if 0
- int ta_int_gpio;
- sec_battery_platform_data_t sec_battery_pdata;
- #endif
- /*#if defined(CONFIG_FUELGAUGE_MAX17050)*/
- #if 0
- int cn, rn;
- u32 low_battery_table_temp[CURRENT_RANGE_MAX_NUM*ELEMENT_N];
- u32 temp_adjust_table_temp[TEMP_RANGE_MAX_NUM*ELEMENT_N];
- #endif
- /* reset, irq gpio info */
- if (np == NULL) {
- pr_err("%s np NULL\n", __func__);
- } else {
- #if defined(CONFIG_FUELGAUGE_MAX17050)
- pdata->fg_irq = of_get_named_gpio(np, "fuelgauge,fuel_int", 0);
- if (pdata->fg_irq < 0)
- pr_err("%s error reading fg_irq = %d\n", __func__, pdata->fg_irq);
- ret = of_property_read_u32(np, "fuelgauge,capacity_calculation_type",
- &pdata->capacity_calculation_type);
- if (ret < 0)
- pr_err("%s error reading capacity_calculation_type %d\n",
- __func__, ret);
- ret = of_property_read_u32(np, "fuelgauge,fuel_alert_soc",
- &pdata->fuel_alert_soc);
- if (ret < 0)
- pr_err("%s error reading pdata->fuel_alert_soc %d\n",
- __func__, ret);
- pdata->repeated_fuelalert = of_property_read_bool(np,
- "fuelgaguge,repeated_fuelalert");
- pdata->jig_irq = of_get_named_gpio(np, "fuelgauge,jig_gpio", 0);
- if (pdata->jig_irq < 0) {
- pr_err("%s error reading jig_gpio = %d\n",
- __func__,pdata->jig_irq);
- pdata->jig_irq = 0;
- } else {
- pdata->jig_irq_attr = IRQF_TRIGGER_RISING;
- }
- pr_info("%s: fg_irq: %d, "
- "calculation_type: 0x%x, fuel_alert_soc: %d,"
- "repeated_fuelalert: %d, jig_irq : %d\n", __func__, pdata->fg_irq,
- pdata->capacity_calculation_type,
- pdata->fuel_alert_soc, pdata->repeated_fuelalert, pdata->jig_irq);
- #else
- pdata->charger_name = "sec-charger";
- if (bnp == NULL) {
- pr_err("%s bnp NULL, Forced set to sec-charger\n", __func__);
- } else {
- ret = of_property_read_string(bnp,
- "battery,charger_name", (char const **)&pdata->charger_name);
- if (ret)
- pr_info("%s: Vendor is Empty. Forced set to sec-charger\n", __func__);
- }
- ret = of_get_named_gpio(np, "fuelgauge,fuel_int", 0);
- if (ret > 0) {
- pdata->fg_irq = ret;
- pr_info("%s reading fg_irq = %d\n", __func__, ret);
- }
- ret = of_get_named_gpio(np, "fuelgauge,bat_int", 0);
- if (ret > 0) {
- pdata->bat_irq_gpio = ret;
- pdata->bat_irq = gpio_to_irq(ret);
- pr_info("%s reading bat_int_gpio = %d\n", __func__, ret);
- }
- ret = of_property_read_u32(np, "fuelgauge,capacity_calculation_type",
- &pdata->capacity_calculation_type);
- if (ret < 0)
- pr_err("%s error reading capacity_calculation_type %d\n",
- __func__, ret);
- ret = of_property_read_u32(np, "fuelgauge,fuel_alert_soc",
- &pdata->fuel_alert_soc);
- if (ret < 0)
- pr_err("%s error reading pdata->fuel_alert_soc %d\n",
- __func__, ret);
- pdata->repeated_fuelalert = of_property_read_bool(np,
- "fuelgaguge,repeated_fuelalert");
- pr_info("%s: fg_irq: %d, "
- "calculation_type: 0x%x, fuel_alert_soc: %d,\n"
- "repeated_fuelalert: %d\n", __func__, pdata->fg_irq,
- pdata->capacity_calculation_type,
- pdata->fuel_alert_soc, pdata->repeated_fuelalert
- );
- #endif
- }
- return 0;
- }
- #else
- static int fuelgauge_parse_dt(struct device *dev,
- struct synaptics_rmi4_power_data *pdata)
- {
- return -ENODEV;
- }
- #endif
- static int __devinit sec_fuelgauge_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
- struct sec_fuelgauge_info *fuelgauge;
- sec_battery_platform_data_t *pdata = NULL;
- struct battery_data_t *battery_data = NULL;
- int ret = 0;
- union power_supply_propval raw_soc_val;
- dev_info(&client->dev,
- "%s: SEC Fuelgauge Driver Loading\n", __func__);
- if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
- return -EIO;
- fuelgauge = kzalloc(sizeof(*fuelgauge), GFP_KERNEL);
- if (!fuelgauge)
- return -ENOMEM;
- mutex_init(&fuelgauge->fg_lock);
- fuelgauge->client = client;
- if (client->dev.of_node) {
- int error;
- pdata = devm_kzalloc(&client->dev,
- sizeof(sec_battery_platform_data_t),
- GFP_KERNEL);
- if (!pdata) {
- dev_err(&client->dev, "Failed to allocate memory\n");
- ret = -ENOMEM;
- goto err_free;
- }
- fuelgauge->pdata = pdata;
- error = fuelgauge_parse_dt(&client->dev, fuelgauge);
- if (error) {
- dev_err(&client->dev,
- "%s: Failed to get fuel_int\n", __func__);
- }
- } else {
- dev_err(&client->dev,
- "%s: Failed to get of_node\n", __func__);
- fuelgauge->pdata = client->dev.platform_data;
- }
- i2c_set_clientdata(client, fuelgauge);
- if (fuelgauge->pdata->fg_gpio_init != NULL) {
- if (!fuelgauge->pdata->fg_gpio_init()) {
- dev_err(&client->dev,
- "%s: Failed to Initialize GPIO\n", __func__);
- goto err_devm_free;
- }
- }
- if (!sec_hal_fg_init(fuelgauge->client)) {
- dev_err(&client->dev,
- "%s: Failed to Initialize Fuelgauge\n", __func__);
- goto err_devm_free;
- }
- fuelgauge->psy_fg.name = "sec-fuelgauge";
- fuelgauge->psy_fg.type = POWER_SUPPLY_TYPE_UNKNOWN;
- fuelgauge->psy_fg.get_property = sec_fg_get_property;
- fuelgauge->psy_fg.set_property = sec_fg_set_property;
- fuelgauge->psy_fg.properties = sec_fuelgauge_props;
- fuelgauge->psy_fg.num_properties =
- ARRAY_SIZE(sec_fuelgauge_props);
- fuelgauge->capacity_max = fuelgauge->pdata->capacity_max;
- raw_soc_val.intval = SEC_FUELGAUGE_CAPACITY_TYPE_RAW;
- sec_hal_fg_get_property(fuelgauge->client,
- POWER_SUPPLY_PROP_CAPACITY, &raw_soc_val);
- raw_soc_val.intval /= 10;
- if(raw_soc_val.intval > fuelgauge->pdata->capacity_max)
- sec_fg_calculate_dynamic_scale(fuelgauge);
- ret = power_supply_register(&client->dev, &fuelgauge->psy_fg);
- if (ret) {
- dev_err(&client->dev,
- "%s: Failed to Register psy_fg\n", __func__);
- goto err_devm_free;
- }
- fuelgauge->is_fuel_alerted = false;
- if (fuelgauge->pdata->fuel_alert_soc >= 0) {
- if (sec_hal_fg_fuelalert_init(fuelgauge->client,
- fuelgauge->pdata->fuel_alert_soc))
- wakeup_source_init(&fuelgauge->fuel_alert_ws, "fuel_alerted");
- else {
- dev_err(&client->dev,
- "%s: Failed to Initialize Fuel-alert\n",
- __func__);
- goto err_supply_unreg;
- }
- }
- if (fuelgauge->pdata->fg_irq > 0) {
- INIT_DELAYED_WORK_DEFERRABLE(
- &fuelgauge->isr_work, sec_fg_isr_work);
- fuelgauge->fg_irq = gpio_to_irq(fuelgauge->pdata->fg_irq);
- dev_info(&client->dev,
- "%s: fg_irq = %d\n", __func__, fuelgauge->fg_irq);
- if (fuelgauge->fg_irq > 0) {
- ret = request_threaded_irq(fuelgauge->fg_irq,
- NULL, sec_fg_irq_thread,
- IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
- "fuelgauge-irq", fuelgauge);
- if (ret) {
- dev_err(&client->dev,
- "%s: Failed to Reqeust IRQ\n", __func__);
- goto err_supply_unreg;
- }
- ret = enable_irq_wake(fuelgauge->fg_irq);
- if (ret < 0)
- dev_err(&client->dev,
- "%s: Failed to Enable Wakeup Source(%d)\n",
- __func__, ret);
- } else {
- dev_err(&client->dev, "%s: Failed gpio_to_irq(%d)\n",
- __func__, fuelgauge->fg_irq);
- goto err_supply_unreg;
- }
- }
- fuelgauge->initial_update_of_soc = true;
- ret = sec_fg_create_attrs(fuelgauge->psy_fg.dev);
- if (ret) {
- dev_err(&client->dev,
- "%s : Failed to create_attrs\n", __func__);
- goto err_irq;
- }
- dev_info(&client->dev,
- "%s: SEC Fuelgauge Driver Loaded\n", __func__);
- return 0;
- err_irq:
- if (fuelgauge->fg_irq > 0)
- free_irq(fuelgauge->fg_irq, fuelgauge);
- wakeup_source_trash(&fuelgauge->fuel_alert_ws);
- err_supply_unreg:
- power_supply_unregister(&fuelgauge->psy_fg);
- err_devm_free:
- if(pdata)
- devm_kfree(&client->dev, pdata);
- if(battery_data)
- devm_kfree(&client->dev, battery_data);
- err_free:
- mutex_destroy(&fuelgauge->fg_lock);
- kfree(fuelgauge);
- dev_info(&client->dev, "%s: Fuel gauge probe failed\n", __func__);
- return ret;
- }
- static int __devexit sec_fuelgauge_remove(
- struct i2c_client *client)
- {
- struct sec_fuelgauge_info *fuelgauge = i2c_get_clientdata(client);
- if (fuelgauge->pdata->fuel_alert_soc >= 0)
- wakeup_source_trash(&fuelgauge->fuel_alert_ws);
- return 0;
- }
- static int sec_fuelgauge_suspend(struct device *dev)
- {
- struct sec_fuelgauge_info *fuelgauge = dev_get_drvdata(dev);
- struct power_supply *psy_battery;
- psy_battery = get_power_supply_by_name("battery");
- if (!psy_battery) {
- pr_err("%s : can't get battery psy\n", __func__);
- } else {
- struct sec_battery_info *battery;
- battery = container_of(psy_battery, struct sec_battery_info, psy_bat);
- battery->fuelgauge_in_sleep = true;
- dev_info(&fuelgauge->client->dev, "%s fuelgauge in sleep (%d)\n",
- __func__, battery->fuelgauge_in_sleep);
- }
- if (!sec_hal_fg_suspend(fuelgauge->client))
- dev_err(&fuelgauge->client->dev,
- "%s: Failed to Suspend Fuelgauge\n", __func__);
- return 0;
- }
- static int sec_fuelgauge_resume(struct device *dev)
- {
- struct sec_fuelgauge_info *fuelgauge = dev_get_drvdata(dev);
- struct power_supply *psy_battery;
- psy_battery = get_power_supply_by_name("battery");
- if (!psy_battery) {
- pr_err("%s : can't get battery psy\n", __func__);
- } else {
- struct sec_battery_info *battery;
- battery = container_of(psy_battery, struct sec_battery_info, psy_bat);
- battery->fuelgauge_in_sleep = false;
- dev_info(&fuelgauge->client->dev, "%s fuelgauge in sleep (%d)\n",
- __func__, battery->fuelgauge_in_sleep);
- }
- if (!sec_hal_fg_resume(fuelgauge->client))
- dev_err(&fuelgauge->client->dev,
- "%s: Failed to Resume Fuelgauge\n", __func__);
- return 0;
- }
- static void sec_fuelgauge_shutdown(struct i2c_client *client)
- {
- }
- static const struct i2c_device_id sec_fuelgauge_id[] = {
- {"sec-fuelgauge", 0},
- {}
- };
- static const struct dev_pm_ops sec_fuelgauge_pm_ops = {
- .suspend = sec_fuelgauge_suspend,
- .resume = sec_fuelgauge_resume,
- };
- MODULE_DEVICE_TABLE(i2c, sec_fuelgauge_id);
- static struct of_device_id fuelgague_i2c_match_table[] = {
- { .compatible = "sec-fuelgauge,i2c", },
- { },
- };
- MODULE_DEVICE_TABLE(i2c, fuelgague_i2c_match_table);
- static struct i2c_driver sec_fuelgauge_driver = {
- .driver = {
- .name = "sec-fuelgauge",
- .owner = THIS_MODULE,
- .of_match_table = fuelgague_i2c_match_table,
- #ifdef CONFIG_PM
- .pm = &sec_fuelgauge_pm_ops,
- #endif
- },
- .probe = sec_fuelgauge_probe,
- .remove = __devexit_p(sec_fuelgauge_remove),
- .shutdown = sec_fuelgauge_shutdown,
- .id_table = sec_fuelgauge_id,
- };
- static int __init sec_fuelgauge_init(void)
- {
- return i2c_add_driver(&sec_fuelgauge_driver);
- }
- static void __exit sec_fuelgauge_exit(void)
- {
- i2c_del_driver(&sec_fuelgauge_driver);
- }
- module_init(sec_fuelgauge_init);
- module_exit(sec_fuelgauge_exit);
- MODULE_DESCRIPTION("Samsung Fuel Gauge Driver");
- MODULE_AUTHOR("Samsung Electronics");
- MODULE_LICENSE("GPL");
|