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