bq2415x - set correct charger mode when driver is loaded, rewritten math code and...
[kernel-power] / kernel-power-2.6.28 / debian / patches / bq2415x_charger.patch
index 4d5c51f..a2796b8 100644 (file)
@@ -1,6 +1,6 @@
---- /dev/null  2012-04-29 08:55:34.366920721 +0200
-+++ kernel-power/drivers/power/bq2415x_charger.c       2012-04-29 17:53:57.799826239 +0200
-@@ -0,0 +1,1564 @@
+--- /dev/null
++++ kernel-power/drivers/power/bq2415x_charger.c
+@@ -0,0 +1,1569 @@
 +/*
 +    bq2415x_charger.c - bq2415x charger driver
 +    Copyright (C) 2011-2012  Pali Rohár <pali.rohar@gmail.com>
 +      BQ24158,
 +};
 +
-+enum bq2415x_mode {
-+      BQ2415X_MODE_NONE,
-+      BQ2415X_MODE_HOST_CHARGER,
-+      BQ2415X_MODE_DEDICATED_CHARGER,
-+      BQ2415X_MODE_BOOST,
-+};
-+
 +static char *bq2415x_chip_name[] = {
 +      "unknown",
 +      "bq24150",
 +      struct bq2415x_platform_data init_data;
 +      struct power_supply charger;
 +      struct delayed_work work;
-+      enum bq2415x_mode charger_mode; /* mode reported by hook function */
-+      enum bq2415x_mode mode;         /* actual setted mode */
++      enum bq2415x_mode reported_mode;/* mode reported by hook function */
++      enum bq2415x_mode mode;         /* current configured mode */
 +      enum bq2415x_chip chip;
 +      char *model;
 +      char *name;
 +
 +static int bq2415x_set_weak_battery_voltage(struct bq2415x_device *bq, int mV)
 +{
-+      int val = mV/100 + (mV%100 > 0 ? 1 : 0) - 34;
-+
-+      if (val < 0)
++      /* round to 100mV */
++      int val;
++      if (mV <= 3400 + 50)
 +              val = 0;
-+      else if (val > 3)
++      else if (mV <= 3500 + 50)
++              val = 1;
++      else if (mV <= 3600 + 50)
++              val = 2;
++      else
 +              val = 3;
-+
 +      return bq2415x_i2c_write_mask(bq, BQ2415X_REG_CONTROL, val,
 +                      BQ2415X_MASK_VLOWV, BQ2415X_SHIFT_VLOWV);
 +}
 +static int bq2415x_set_battery_regulation_voltage(struct bq2415x_device *bq,
 +                                              int mV)
 +{
-+      int val = (mV/10 + (mV%10 > 0 ? 1 : 0) - 350) / 2;
++      int val = (mV/10 - 350) / 2;
 +
 +      if (val < 0)
 +              val = 0;
-+      else if (val > 94) /* FIXME: Max is 94 or 122 ? */
++      else if (val > 94) /* FIXME: Max is 94 or 122 ? Set max value ? */
 +              return -EINVAL;
 +
 +      return bq2415x_i2c_write_mask(bq, BQ2415X_REG_VOLTAGE, val,
 +      if (bq->init_data.resistor_sense <= 0)
 +              return -ENOSYS;
 +
-+      val = (mA * bq->init_data.resistor_sense - 37400);
-+      val = val/6800 + (val%6800 > 0 ? 1 : 0);
++      val = (mA * bq->init_data.resistor_sense - 37400) / 6800;
 +
 +      if (val < 0)
 +              val = 0;
 +      if (bq->init_data.resistor_sense <= 0)
 +              return -ENOSYS;
 +
-+      val = (mA * bq->init_data.resistor_sense - 3400);
-+      val = val/3400 + (val%3400 > 0 ? 1 : 0);
++      val = (mA * bq->init_data.resistor_sense - 3400) / 3400;
 +
 +      if (val < 0)
 +              val = 0;
 +
 +}
 +
-+static void bq2415x_set_charger_type(int type, void *data)
++static void bq2415x_hook_function(enum bq2415x_mode mode, void *data)
 +{
 +      struct bq2415x_device *bq = data;
 +
 +      if (!bq)
 +              return;
 +
-+      switch (type) {
-+      case 0:
-+              bq->charger_mode = BQ2415X_MODE_NONE;
-+              break;
-+      case 1:
-+              bq->charger_mode = BQ2415X_MODE_HOST_CHARGER;
-+              break;
-+      case 2:
-+              bq->charger_mode = BQ2415X_MODE_DEDICATED_CHARGER;
-+              break;
-+      default:
-+              return;
-+      }
++      bq->reported_mode = mode;
 +
 +      if (bq->automode < 1)
 +              return;
 +
-+      /* TODO: Detect USB Host mode */
-+
-+      bq2415x_set_mode(bq, bq->charger_mode);
++      bq2415x_set_mode(bq, bq->reported_mode);
 +
 +}
 +
 +              if (bq->automode < 0)
 +                      return -ENOSYS;
 +              bq->automode = 1;
-+              mode = bq->charger_mode;
++              mode = bq->reported_mode;
 +      } else if (strncmp(buf, "none", 4) == 0) {
 +              if (bq->automode > 0)
 +                      bq->automode = 0;
 +      return ret;
 +}
 +
++static ssize_t bq2415x_sysfs_show_reported_mode(struct device *dev,
++              struct device_attribute *attr, char *buf)
++{
++      struct power_supply *psy = dev_get_drvdata(dev);
++      struct bq2415x_device *bq = container_of(psy, struct bq2415x_device,
++                                              charger);
++
++      switch (bq->reported_mode) {
++      case BQ2415X_MODE_NONE:
++              return sprintf(buf, "none\n");
++      case BQ2415X_MODE_HOST_CHARGER:
++              return sprintf(buf, "host\n");
++      case BQ2415X_MODE_DEDICATED_CHARGER:
++              return sprintf(buf, "dedicated\n");
++      case BQ2415X_MODE_BOOST:
++              return sprintf(buf, "boost\n");
++      }
++
++      return -EINVAL;
++}
++
 +static ssize_t bq2415x_sysfs_set_registers(struct device *dev,
 +              struct device_attribute *attr, const char *buf, size_t count)
 +{
 +static DEVICE_ATTR(stat_pin_enable, S_IWUSR | S_IRUGO,
 +              bq2415x_sysfs_show_enable, bq2415x_sysfs_set_enable);
 +
++static DEVICE_ATTR(reported_mode, S_IRUGO,
++              bq2415x_sysfs_show_reported_mode, NULL);
 +static DEVICE_ATTR(mode, S_IWUSR | S_IRUGO,
 +              bq2415x_sysfs_show_mode, bq2415x_sysfs_set_mode);
 +static DEVICE_ATTR(timer, S_IWUSR | S_IRUGO,
 +      &dev_attr_otg_pin_enable.attr,
 +      &dev_attr_stat_pin_enable.attr,
 +
++      &dev_attr_reported_mode.attr,
 +      &dev_attr_mode.attr,
 +      &dev_attr_timer.attr,
 +
 +      bq->chip = id->driver_data;
 +      bq->name = name;
 +      bq->mode = BQ2415X_MODE_NONE;
-+      bq->charger_mode = BQ2415X_MODE_NONE;
++      bq->reported_mode = BQ2415X_MODE_NONE;
 +      bq->autotimer = 0;
 +      bq->automode = 0;
 +
 +              goto error_5;
 +      }
 +
-+      if (bq->init_data.set_charger_type_hook) {
-+              if (bq->init_data.set_charger_type_hook(
-+                              bq2415x_set_charger_type, bq)) {
++      if (bq->init_data.set_mode_hook) {
++              if (bq->init_data.set_mode_hook(
++                              bq2415x_hook_function, bq)) {
 +                      bq->automode = 1;
++                      bq2415x_set_mode(bq, bq->reported_mode);
 +                      dev_info(bq->dev, "automode enabled\n");
 +              } else {
 +                      bq->automode = -1;
-+                      dev_info(bq->dev, "automode not supported\n");
++                      dev_info(bq->dev, "automode failed\n");
 +              }
 +      } else {
 +              bq->automode = -1;
 +{
 +      struct bq2415x_device *bq = i2c_get_clientdata(client);
 +
-+      if (bq->init_data.set_charger_type_hook)
-+              bq->init_data.set_charger_type_hook(NULL, NULL);
++      if (bq->init_data.set_mode_hook)
++              bq->init_data.set_mode_hook(NULL, NULL);
 +
 +      bq2415x_sysfs_exit(bq);
 +      bq2415x_power_supply_exit(bq);
 +MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
 +MODULE_DESCRIPTION("bq2415x charger driver");
 +MODULE_LICENSE("GPL");
---- /dev/null  2012-04-29 08:55:34.366920721 +0200
-+++ kernel-power/include/linux/power/bq2415x_charger.h 2012-01-27 15:47:45.689585447 +0100
-@@ -0,0 +1,78 @@
+--- /dev/null
++++ kernel-power/include/linux/power/bq2415x_charger.h
+@@ -0,0 +1,90 @@
 +/*
 +    bq2415x_charger.h - bq2415x charger driver
 +    Copyright (C) 2011-2012  Pali Rohár <pali.rohar@gmail.com>
 +#define BQ2415X_CHARGER_H
 +
 +/*
-+  This is platform data for bq2415x chip. It contains default board volatages
++  This is platform data for bq2415x chip. It contains default board voltages
 +  and currents which can be also later configured via sysfs. If value is -1
 +  then default chip value (specified in datasheet) will be used.
 +
 +  current. It it is less or equal to zero, configuring charge and termination
 +  current will not be possible.
 +
-+  Function set_charger_type_hook is needed for automode (setting correct
-+  current limit when charger is connected/disconnected). When is NULL,
++  Function set_mode_hook is needed for automode (setting correct current limit
++  when charger is connected/disconnected or setting boost mode). When is NULL,
 +  automode function is disabled. When is not NULL, it must have this prototype:
 +
-+    int (*set_charger_type_hook)(void (*hook)(int type, void *data), void *data)
++    int (*set_mode_hook)(
++      void (*hook)(enum bq2415x_mode mode, void *data),
++      void *data)
 +
-+  and bq2415x driver will call it as:
++  hook is hook function (see below) and data is pointer to driver private data
 +
-+    platform_data->set_charger_type_hook(bq_hook_function, bq_private_data);
++  bq2415x driver will call it as:
 +
-+  Board/platform function set_charger_type_hook return non zero when hook
++    platform_data->set_mode_hook(bq2415x_hook_function, bq2415x_device);
++
++  Board/platform function set_mode_hook return non zero value when hook
 +  function was successfull registred. Platform code should call that hook
 +  function (which get from pointer, with data) every time when charger was
-+  connected/disconnected. bq driver then set correct current limit.
++  connected/disconnected or require to enable boost mode. bq2415x driver then
++  will set correct current limit, enable/disable charger or boost mode.
 +
 +  Hook function has this prototype:
 +
-+    void hook(int type, void *data);
-+
-+  type is:
-+    0 - for unknown or none charger (max current limit is 100mA)
-+    1 - for usb host/hub charger (max current limit is 500mA)
-+    2 - for dedicated charger (unlimited)
++    void hook(enum bq2415x_mode mode, void *data);
 +
-+  data is pointer to bq_private_data (which get from set_charger_type_hook)
++  mode is bq2415x mode (charger or boost)
++  data is pointer to driver private data (which get from set_charger_type_hook)
 +
 +  When bq driver is being unloaded, it call function:
 +
-+    platform_data->set_charger_type_hook(NULL, NULL);
++    platform_data->set_mode_hook(NULL, NULL);
 +
-+  After thet board/platform code must not call bq hook function! It is
-+  possible that pointer to hook function will not be valid.
++  (hook function and driver private data are NULL)
++
++  After thet board/platform code must not call driver hook function! It is
++  possible that pointer to hook function will not be valid and calling will
++  cause undefined result.
 +
 +*/
 +
++/* Supported modes with maximal current limit */
++enum bq2415x_mode {
++      BQ2415X_MODE_NONE,              /* unknown or no charger (100mA) */
++      BQ2415X_MODE_HOST_CHARGER,      /* usb host/hub charger (500mA) */
++      BQ2415X_MODE_DEDICATED_CHARGER, /* dedicated charger (unlimited) */
++      BQ2415X_MODE_BOOST,             /* boost mode (charging disabled) */
++};
++
 +struct bq2415x_platform_data {
 +      int current_limit;              /* mA */
 +      int weak_battery_voltage;       /* mV */
 +      int charge_current;             /* mA */
 +      int termination_current;        /* mA */
 +      int resistor_sense;             /* m ohm */
-+      int (*set_charger_type_hook)(void (*hook)(int type, void *data),
-+                                   void *data);
++      int (*set_mode_hook)(void (*hook)(enum bq2415x_mode mode, void *data),
++                           void *data);
 +};
 +
 +#endif