Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1 | /* |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 2 | * Gas Gauge driver for SBS Compliant Batteries |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2010, NVIDIA Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/power_supply.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/slab.h> |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/gpio.h> |
| 30 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 31 | #include <linux/power/sbs-battery.h> |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 32 | |
| 33 | enum { |
| 34 | REG_MANUFACTURER_DATA, |
| 35 | REG_TEMPERATURE, |
| 36 | REG_VOLTAGE, |
| 37 | REG_CURRENT, |
| 38 | REG_CAPACITY, |
| 39 | REG_TIME_TO_EMPTY, |
| 40 | REG_TIME_TO_FULL, |
| 41 | REG_STATUS, |
| 42 | REG_CYCLE_COUNT, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 43 | REG_SERIAL_NUMBER, |
| 44 | REG_REMAINING_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 45 | REG_REMAINING_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 46 | REG_FULL_CHARGE_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 47 | REG_FULL_CHARGE_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 48 | REG_DESIGN_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 49 | REG_DESIGN_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 50 | REG_DESIGN_VOLTAGE, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 53 | /* Battery Mode defines */ |
| 54 | #define BATTERY_MODE_OFFSET 0x03 |
| 55 | #define BATTERY_MODE_MASK 0x8000 |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 56 | enum sbs_battery_mode { |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 57 | BATTERY_MODE_AMPS, |
| 58 | BATTERY_MODE_WATTS |
| 59 | }; |
| 60 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 61 | /* manufacturer access defines */ |
| 62 | #define MANUFACTURER_ACCESS_STATUS 0x0006 |
| 63 | #define MANUFACTURER_ACCESS_SLEEP 0x0011 |
| 64 | |
| 65 | /* battery status value bits */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 66 | #define BATTERY_DISCHARGING 0x40 |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 67 | #define BATTERY_FULL_CHARGED 0x20 |
| 68 | #define BATTERY_FULL_DISCHARGED 0x10 |
| 69 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 70 | #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 71 | .psp = _psp, \ |
| 72 | .addr = _addr, \ |
| 73 | .min_value = _min_value, \ |
| 74 | .max_value = _max_value, \ |
| 75 | } |
| 76 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 77 | static const struct chip_data { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 78 | enum power_supply_property psp; |
| 79 | u8 addr; |
| 80 | int min_value; |
| 81 | int max_value; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 82 | } sbs_data[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 83 | [REG_MANUFACTURER_DATA] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 84 | SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 85 | [REG_TEMPERATURE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 86 | SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 87 | [REG_VOLTAGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 88 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 89 | [REG_CURRENT] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 90 | SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 91 | [REG_CAPACITY] = |
Nikolaus Voss | b1f092f | 2012-04-25 08:59:03 +0200 | [diff] [blame] | 92 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 93 | [REG_REMAINING_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 94 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 95 | [REG_REMAINING_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 96 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 97 | [REG_FULL_CHARGE_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 98 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 99 | [REG_FULL_CHARGE_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 100 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 101 | [REG_TIME_TO_EMPTY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 102 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 103 | [REG_TIME_TO_FULL] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 104 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 105 | [REG_STATUS] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 106 | SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 107 | [REG_CYCLE_COUNT] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 108 | SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 109 | [REG_DESIGN_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 110 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 111 | [REG_DESIGN_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 112 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 113 | [REG_DESIGN_VOLTAGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 114 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 115 | [REG_SERIAL_NUMBER] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 116 | SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 119 | static enum power_supply_property sbs_properties[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 120 | POWER_SUPPLY_PROP_STATUS, |
| 121 | POWER_SUPPLY_PROP_HEALTH, |
| 122 | POWER_SUPPLY_PROP_PRESENT, |
| 123 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 124 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 125 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 126 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 127 | POWER_SUPPLY_PROP_CAPACITY, |
| 128 | POWER_SUPPLY_PROP_TEMP, |
| 129 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 130 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 131 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 132 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 133 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 134 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 135 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 136 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 137 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 138 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 141 | struct sbs_info { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 142 | struct i2c_client *client; |
| 143 | struct power_supply power_supply; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 144 | struct sbs_platform_data *pdata; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 145 | bool is_present; |
| 146 | bool gpio_detect; |
| 147 | bool enable_detection; |
| 148 | int irq; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 149 | int last_state; |
| 150 | int poll_time; |
| 151 | struct delayed_work work; |
| 152 | int ignore_changes; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 155 | static int sbs_read_word_data(struct i2c_client *client, u8 address) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 156 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 157 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 158 | s32 ret = 0; |
| 159 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 160 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 161 | if (chip->pdata) |
| 162 | retries = max(chip->pdata->i2c_retry_count + 1, 1); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 163 | |
| 164 | while (retries > 0) { |
| 165 | ret = i2c_smbus_read_word_data(client, address); |
| 166 | if (ret >= 0) |
| 167 | break; |
| 168 | retries--; |
| 169 | } |
| 170 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 171 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 172 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 173 | "%s: i2c read at address 0x%x failed\n", |
| 174 | __func__, address); |
| 175 | return ret; |
| 176 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 177 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 178 | return le16_to_cpu(ret); |
| 179 | } |
| 180 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 181 | static int sbs_write_word_data(struct i2c_client *client, u8 address, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 182 | u16 value) |
| 183 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 184 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 185 | s32 ret = 0; |
| 186 | int retries = 1; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 187 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 188 | if (chip->pdata) |
| 189 | retries = max(chip->pdata->i2c_retry_count + 1, 1); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 190 | |
| 191 | while (retries > 0) { |
| 192 | ret = i2c_smbus_write_word_data(client, address, |
| 193 | le16_to_cpu(value)); |
| 194 | if (ret >= 0) |
| 195 | break; |
| 196 | retries--; |
| 197 | } |
| 198 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 199 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 200 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 201 | "%s: i2c write to address 0x%x failed\n", |
| 202 | __func__, address); |
| 203 | return ret; |
| 204 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 205 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 209 | static int sbs_get_battery_presence_and_health( |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 210 | struct i2c_client *client, enum power_supply_property psp, |
| 211 | union power_supply_propval *val) |
| 212 | { |
| 213 | s32 ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 214 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 215 | |
| 216 | if (psp == POWER_SUPPLY_PROP_PRESENT && |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 217 | chip->gpio_detect) { |
| 218 | ret = gpio_get_value(chip->pdata->battery_detect); |
| 219 | if (ret == chip->pdata->battery_detect_present) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 220 | val->intval = 1; |
| 221 | else |
| 222 | val->intval = 0; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 223 | chip->is_present = val->intval; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 224 | return ret; |
| 225 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 226 | |
| 227 | /* Write to ManufacturerAccess with |
| 228 | * ManufacturerAccess command and then |
| 229 | * read the status */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 230 | ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
| 231 | MANUFACTURER_ACCESS_STATUS); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 232 | if (ret < 0) { |
| 233 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 234 | val->intval = 0; /* battery removed */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 235 | return ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 236 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 237 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 238 | ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 242 | if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || |
| 243 | ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 244 | val->intval = 0; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* Mask the upper nibble of 2nd byte and |
| 249 | * lower byte of response then |
| 250 | * shift the result by 8 to get status*/ |
| 251 | ret &= 0x0F00; |
| 252 | ret >>= 8; |
| 253 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 254 | if (ret == 0x0F) |
| 255 | /* battery removed */ |
| 256 | val->intval = 0; |
| 257 | else |
| 258 | val->intval = 1; |
| 259 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { |
| 260 | if (ret == 0x09) |
| 261 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 262 | else if (ret == 0x0B) |
| 263 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 264 | else if (ret == 0x0C) |
| 265 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 266 | else |
| 267 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 273 | static int sbs_get_battery_property(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 274 | int reg_offset, enum power_supply_property psp, |
| 275 | union power_supply_propval *val) |
| 276 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 277 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 278 | s32 ret; |
| 279 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 280 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | |
| 284 | /* returned values are 16 bit */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 285 | if (sbs_data[reg_offset].min_value < 0) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 286 | ret = (s16)ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 287 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 288 | if (ret >= sbs_data[reg_offset].min_value && |
| 289 | ret <= sbs_data[reg_offset].max_value) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 290 | val->intval = ret; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 291 | if (psp != POWER_SUPPLY_PROP_STATUS) |
| 292 | return 0; |
| 293 | |
| 294 | if (ret & BATTERY_FULL_CHARGED) |
| 295 | val->intval = POWER_SUPPLY_STATUS_FULL; |
| 296 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 297 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 298 | else if (ret & BATTERY_DISCHARGING) |
| 299 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 300 | else |
| 301 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 302 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 303 | if (chip->poll_time == 0) |
| 304 | chip->last_state = val->intval; |
| 305 | else if (chip->last_state != val->intval) { |
| 306 | cancel_delayed_work_sync(&chip->work); |
| 307 | power_supply_changed(&chip->power_supply); |
| 308 | chip->poll_time = 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 309 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 310 | } else { |
| 311 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 312 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
| 313 | else |
| 314 | val->intval = 0; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 320 | static void sbs_unit_adjustment(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 321 | enum power_supply_property psp, union power_supply_propval *val) |
| 322 | { |
| 323 | #define BASE_UNIT_CONVERSION 1000 |
| 324 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 325 | #define TIME_UNIT_CONVERSION 60 |
| 326 | #define TEMP_KELVIN_TO_CELSIUS 2731 |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 327 | switch (psp) { |
| 328 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 329 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 330 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 331 | /* sbs provides energy in units of 10mWh. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 332 | * Convert to µWh |
| 333 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 334 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 335 | break; |
| 336 | |
| 337 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 338 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 339 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 340 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 341 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 342 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 343 | val->intval *= BASE_UNIT_CONVERSION; |
| 344 | break; |
| 345 | |
| 346 | case POWER_SUPPLY_PROP_TEMP: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 347 | /* sbs provides battery temperature in 0.1K |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 348 | * so convert it to 0.1°C |
| 349 | */ |
| 350 | val->intval -= TEMP_KELVIN_TO_CELSIUS; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 354 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 355 | /* sbs provides time to empty and time to full in minutes. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 356 | * Convert to seconds |
| 357 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 358 | val->intval *= TIME_UNIT_CONVERSION; |
| 359 | break; |
| 360 | |
| 361 | default: |
| 362 | dev_dbg(&client->dev, |
| 363 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 364 | } |
| 365 | } |
| 366 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 367 | static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client, |
| 368 | enum sbs_battery_mode mode) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 369 | { |
| 370 | int ret, original_val; |
| 371 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 372 | original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 373 | if (original_val < 0) |
| 374 | return original_val; |
| 375 | |
| 376 | if ((original_val & BATTERY_MODE_MASK) == mode) |
| 377 | return mode; |
| 378 | |
| 379 | if (mode == BATTERY_MODE_AMPS) |
| 380 | ret = original_val & ~BATTERY_MODE_MASK; |
| 381 | else |
| 382 | ret = original_val | BATTERY_MODE_MASK; |
| 383 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 384 | ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 385 | if (ret < 0) |
| 386 | return ret; |
| 387 | |
| 388 | return original_val & BATTERY_MODE_MASK; |
| 389 | } |
| 390 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 391 | static int sbs_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 392 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 393 | union power_supply_propval *val) |
| 394 | { |
| 395 | s32 ret; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 396 | enum sbs_battery_mode mode = BATTERY_MODE_WATTS; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 397 | |
| 398 | if (power_supply_is_amp_property(psp)) |
| 399 | mode = BATTERY_MODE_AMPS; |
| 400 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 401 | mode = sbs_set_battery_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 402 | if (mode < 0) |
| 403 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 404 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 405 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 406 | if (ret < 0) |
| 407 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 408 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 409 | if (psp == POWER_SUPPLY_PROP_CAPACITY) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 410 | /* sbs spec says that this can be >100 % |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 411 | * even if max value is 100 % */ |
| 412 | val->intval = min(ret, 100); |
| 413 | } else |
| 414 | val->intval = ret; |
| 415 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 416 | ret = sbs_set_battery_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 417 | if (ret < 0) |
| 418 | return ret; |
| 419 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 423 | static char sbs_serial[5]; |
| 424 | static int sbs_get_battery_serial_number(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 425 | union power_supply_propval *val) |
| 426 | { |
| 427 | int ret; |
| 428 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 429 | ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 430 | if (ret < 0) |
| 431 | return ret; |
| 432 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 433 | ret = sprintf(sbs_serial, "%04x", ret); |
| 434 | val->strval = sbs_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 439 | static int sbs_get_property_index(struct i2c_client *client, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 440 | enum power_supply_property psp) |
| 441 | { |
| 442 | int count; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 443 | for (count = 0; count < ARRAY_SIZE(sbs_data); count++) |
| 444 | if (psp == sbs_data[count].psp) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 445 | return count; |
| 446 | |
| 447 | dev_warn(&client->dev, |
| 448 | "%s: Invalid Property - %d\n", __func__, psp); |
| 449 | |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 453 | static int sbs_get_property(struct power_supply *psy, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 454 | enum power_supply_property psp, |
| 455 | union power_supply_propval *val) |
| 456 | { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 457 | int ret = 0; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 458 | struct sbs_info *chip = container_of(psy, |
| 459 | struct sbs_info, power_supply); |
| 460 | struct i2c_client *client = chip->client; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 461 | |
| 462 | switch (psp) { |
| 463 | case POWER_SUPPLY_PROP_PRESENT: |
| 464 | case POWER_SUPPLY_PROP_HEALTH: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 465 | ret = sbs_get_battery_presence_and_health(client, psp, val); |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 466 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 467 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 468 | break; |
| 469 | |
| 470 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 471 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
Nikolaus Voss | 5da5098 | 2012-05-09 08:30:44 +0200 | [diff] [blame] | 472 | goto done; /* don't trigger power_supply_changed()! */ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 473 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 474 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 475 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 476 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 477 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 478 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 479 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 480 | case POWER_SUPPLY_PROP_CAPACITY: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 481 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 482 | if (ret < 0) |
| 483 | break; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 484 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 485 | ret = sbs_get_battery_capacity(client, ret, psp, val); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 486 | break; |
| 487 | |
| 488 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 489 | ret = sbs_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 490 | break; |
| 491 | |
| 492 | case POWER_SUPPLY_PROP_STATUS: |
| 493 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 494 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 495 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 496 | case POWER_SUPPLY_PROP_TEMP: |
| 497 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 498 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 499 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 500 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 501 | if (ret < 0) |
| 502 | break; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 503 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 504 | ret = sbs_get_battery_property(client, ret, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 505 | break; |
| 506 | |
| 507 | default: |
| 508 | dev_err(&client->dev, |
| 509 | "%s: INVALID property\n", __func__); |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 513 | if (!chip->enable_detection) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 514 | goto done; |
| 515 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 516 | if (!chip->gpio_detect && |
| 517 | chip->is_present != (ret >= 0)) { |
| 518 | chip->is_present = (ret >= 0); |
| 519 | power_supply_changed(&chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | done: |
| 523 | if (!ret) { |
| 524 | /* Convert units to match requirements for power supply class */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 525 | sbs_unit_adjustment(client, psp, val); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 526 | } |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 527 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 528 | dev_dbg(&client->dev, |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 529 | "%s: property = %d, value = %x\n", __func__, psp, val->intval); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 530 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 531 | if (ret && chip->is_present) |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 532 | return ret; |
| 533 | |
| 534 | /* battery not present, so return NODATA for properties */ |
| 535 | if (ret) |
| 536 | return -ENODATA; |
| 537 | |
| 538 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 541 | static irqreturn_t sbs_irq(int irq, void *devid) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 542 | { |
| 543 | struct power_supply *battery = devid; |
| 544 | |
| 545 | power_supply_changed(battery); |
| 546 | |
| 547 | return IRQ_HANDLED; |
| 548 | } |
| 549 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 550 | static void sbs_external_power_changed(struct power_supply *psy) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 551 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 552 | struct sbs_info *chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 553 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 554 | chip = container_of(psy, struct sbs_info, power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 555 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 556 | if (chip->ignore_changes > 0) { |
| 557 | chip->ignore_changes--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
| 561 | /* cancel outstanding work */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 562 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 563 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 564 | schedule_delayed_work(&chip->work, HZ); |
| 565 | chip->poll_time = chip->pdata->poll_retry_count; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 568 | static void sbs_delayed_work(struct work_struct *work) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 569 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 570 | struct sbs_info *chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 571 | s32 ret; |
| 572 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 573 | chip = container_of(work, struct sbs_info, work.work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 574 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 575 | ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 576 | /* if the read failed, give up on this work */ |
| 577 | if (ret < 0) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 578 | chip->poll_time = 0; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 579 | return; |
| 580 | } |
| 581 | |
| 582 | if (ret & BATTERY_FULL_CHARGED) |
| 583 | ret = POWER_SUPPLY_STATUS_FULL; |
| 584 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 585 | ret = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 586 | else if (ret & BATTERY_DISCHARGING) |
| 587 | ret = POWER_SUPPLY_STATUS_DISCHARGING; |
| 588 | else |
| 589 | ret = POWER_SUPPLY_STATUS_CHARGING; |
| 590 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 591 | if (chip->last_state != ret) { |
| 592 | chip->poll_time = 0; |
| 593 | power_supply_changed(&chip->power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 594 | return; |
| 595 | } |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 596 | if (chip->poll_time > 0) { |
| 597 | schedule_delayed_work(&chip->work, HZ); |
| 598 | chip->poll_time--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | } |
| 602 | |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 603 | #if defined(CONFIG_OF) |
| 604 | |
| 605 | #include <linux/of_device.h> |
| 606 | #include <linux/of_gpio.h> |
| 607 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 608 | static const struct of_device_id sbs_dt_ids[] = { |
| 609 | { .compatible = "sbs,sbs-battery" }, |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 610 | { .compatible = "ti,bq20z75" }, |
| 611 | { } |
| 612 | }; |
Olof Johansson | 62df393 | 2012-01-06 05:45:34 +0400 | [diff] [blame] | 613 | MODULE_DEVICE_TABLE(of, sbs_dt_ids); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 614 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 615 | static struct sbs_platform_data *sbs_of_populate_pdata( |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 616 | struct i2c_client *client) |
| 617 | { |
| 618 | struct device_node *of_node = client->dev.of_node; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 619 | struct sbs_platform_data *pdata = client->dev.platform_data; |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 620 | enum of_gpio_flags gpio_flags; |
| 621 | int rc; |
| 622 | u32 prop; |
| 623 | |
| 624 | /* verify this driver matches this device */ |
| 625 | if (!of_node) |
| 626 | return NULL; |
| 627 | |
| 628 | /* if platform data is set, honor it */ |
| 629 | if (pdata) |
| 630 | return pdata; |
| 631 | |
| 632 | /* first make sure at least one property is set, otherwise |
| 633 | * it won't change behavior from running without pdata. |
| 634 | */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 635 | if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) && |
| 636 | !of_get_property(of_node, "sbs,poll-retry-count", NULL) && |
| 637 | !of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 638 | goto of_out; |
| 639 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 640 | pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data), |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 641 | GFP_KERNEL); |
| 642 | if (!pdata) |
| 643 | goto of_out; |
| 644 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 645 | rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 646 | if (!rc) |
| 647 | pdata->i2c_retry_count = prop; |
| 648 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 649 | rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 650 | if (!rc) |
| 651 | pdata->poll_retry_count = prop; |
| 652 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 653 | if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) { |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 654 | pdata->battery_detect = -1; |
| 655 | goto of_out; |
| 656 | } |
| 657 | |
| 658 | pdata->battery_detect = of_get_named_gpio_flags(of_node, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 659 | "sbs,battery-detect-gpios", 0, &gpio_flags); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 660 | |
| 661 | if (gpio_flags & OF_GPIO_ACTIVE_LOW) |
| 662 | pdata->battery_detect_present = 0; |
| 663 | else |
| 664 | pdata->battery_detect_present = 1; |
| 665 | |
| 666 | of_out: |
| 667 | return pdata; |
| 668 | } |
| 669 | #else |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 670 | #define sbs_dt_ids NULL |
| 671 | static struct sbs_platform_data *sbs_of_populate_pdata( |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 672 | struct i2c_client *client) |
| 673 | { |
| 674 | return client->dev.platform_data; |
| 675 | } |
| 676 | #endif |
| 677 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 678 | static int sbs_probe(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 679 | const struct i2c_device_id *id) |
| 680 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 681 | struct sbs_info *chip; |
| 682 | struct sbs_platform_data *pdata = client->dev.platform_data; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 683 | int rc; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 684 | int irq; |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 685 | char *name; |
| 686 | |
| 687 | name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev)); |
| 688 | if (!name) { |
| 689 | dev_err(&client->dev, "Failed to allocate device name\n"); |
| 690 | return -ENOMEM; |
| 691 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 692 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 693 | chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL); |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 694 | if (!chip) { |
| 695 | rc = -ENOMEM; |
| 696 | goto exit_free_name; |
| 697 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 698 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 699 | chip->client = client; |
| 700 | chip->enable_detection = false; |
| 701 | chip->gpio_detect = false; |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 702 | chip->power_supply.name = name; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 703 | chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY; |
| 704 | chip->power_supply.properties = sbs_properties; |
| 705 | chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties); |
| 706 | chip->power_supply.get_property = sbs_get_property; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 707 | /* ignore first notification of external change, it is generated |
| 708 | * from the power_supply_register call back |
| 709 | */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 710 | chip->ignore_changes = 1; |
| 711 | chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; |
| 712 | chip->power_supply.external_power_changed = sbs_external_power_changed; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 713 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 714 | pdata = sbs_of_populate_pdata(client); |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 715 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 716 | if (pdata) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 717 | chip->gpio_detect = gpio_is_valid(pdata->battery_detect); |
| 718 | chip->pdata = pdata; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 721 | i2c_set_clientdata(client, chip); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 722 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 723 | if (!chip->gpio_detect) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 724 | goto skip_gpio; |
| 725 | |
| 726 | rc = gpio_request(pdata->battery_detect, dev_name(&client->dev)); |
| 727 | if (rc) { |
| 728 | dev_warn(&client->dev, "Failed to request gpio: %d\n", rc); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 729 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 730 | goto skip_gpio; |
| 731 | } |
| 732 | |
| 733 | rc = gpio_direction_input(pdata->battery_detect); |
| 734 | if (rc) { |
| 735 | dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc); |
| 736 | gpio_free(pdata->battery_detect); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 737 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 738 | goto skip_gpio; |
| 739 | } |
| 740 | |
| 741 | irq = gpio_to_irq(pdata->battery_detect); |
| 742 | if (irq <= 0) { |
| 743 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); |
| 744 | gpio_free(pdata->battery_detect); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 745 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 746 | goto skip_gpio; |
| 747 | } |
| 748 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 749 | rc = request_irq(irq, sbs_irq, |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 750 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 751 | dev_name(&client->dev), &chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 752 | if (rc) { |
| 753 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); |
| 754 | gpio_free(pdata->battery_detect); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 755 | chip->gpio_detect = false; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 756 | goto skip_gpio; |
| 757 | } |
| 758 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 759 | chip->irq = irq; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 760 | |
| 761 | skip_gpio: |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 762 | /* |
| 763 | * Before we register, we need to make sure we can actually talk |
| 764 | * to the battery. |
| 765 | */ |
| 766 | rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); |
| 767 | if (rc < 0) { |
| 768 | dev_err(&client->dev, "%s: Failed to get device status\n", |
| 769 | __func__); |
| 770 | goto exit_psupply; |
| 771 | } |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 772 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 773 | rc = power_supply_register(&client->dev, &chip->power_supply); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 774 | if (rc) { |
| 775 | dev_err(&client->dev, |
| 776 | "%s: Failed to register power supply\n", __func__); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 777 | goto exit_psupply; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | dev_info(&client->dev, |
| 781 | "%s: battery gas gauge device registered\n", client->name); |
| 782 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 783 | INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 784 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 785 | chip->enable_detection = true; |
Rhyland Klein | ee177d9 | 2011-05-24 12:06:50 -0700 | [diff] [blame] | 786 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 787 | return 0; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 788 | |
| 789 | exit_psupply: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 790 | if (chip->irq) |
| 791 | free_irq(chip->irq, &chip->power_supply); |
| 792 | if (chip->gpio_detect) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 793 | gpio_free(pdata->battery_detect); |
| 794 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 795 | kfree(chip); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 796 | |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 797 | exit_free_name: |
| 798 | kfree(name); |
| 799 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 800 | return rc; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 803 | static int sbs_remove(struct i2c_client *client) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 804 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 805 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 806 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 807 | if (chip->irq) |
| 808 | free_irq(chip->irq, &chip->power_supply); |
| 809 | if (chip->gpio_detect) |
| 810 | gpio_free(chip->pdata->battery_detect); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 811 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 812 | power_supply_unregister(&chip->power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 813 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 814 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 815 | |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 816 | kfree(chip->power_supply.name); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 817 | kfree(chip); |
| 818 | chip = NULL; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | #if defined CONFIG_PM |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 824 | static int sbs_suspend(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 825 | pm_message_t state) |
| 826 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 827 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 828 | s32 ret; |
| 829 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 830 | if (chip->poll_time > 0) |
| 831 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 832 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 833 | /* write to manufacturer access with sleep command */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 834 | ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 835 | MANUFACTURER_ACCESS_SLEEP); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 836 | if (chip->is_present && ret < 0) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 837 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | #else |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 842 | #define sbs_suspend NULL |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 843 | #endif |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 844 | /* any smbus transaction will wake up sbs */ |
| 845 | #define sbs_resume NULL |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 846 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 847 | static const struct i2c_device_id sbs_id[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 848 | { "bq20z75", 0 }, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 849 | { "sbs-battery", 1 }, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 850 | {} |
| 851 | }; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 852 | MODULE_DEVICE_TABLE(i2c, sbs_id); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 853 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 854 | static struct i2c_driver sbs_battery_driver = { |
| 855 | .probe = sbs_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 856 | .remove = sbs_remove, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 857 | .suspend = sbs_suspend, |
| 858 | .resume = sbs_resume, |
| 859 | .id_table = sbs_id, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 860 | .driver = { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 861 | .name = "sbs-battery", |
| 862 | .of_match_table = sbs_dt_ids, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 863 | }, |
| 864 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 865 | module_i2c_driver(sbs_battery_driver); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 866 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 867 | MODULE_DESCRIPTION("SBS battery monitor driver"); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 868 | MODULE_LICENSE("GPL"); |