Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 2 | /* |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 3 | * Gas Gauge driver for SBS Compliant Batteries |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2010, NVIDIA Corporation. |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 8 | #include <linux/bits.h> |
Phil Reid | adcf04c | 2017-07-11 17:43:43 +0800 | [diff] [blame] | 9 | #include <linux/delay.h> |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 10 | #include <linux/err.h> |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 11 | #include <linux/gpio/consumer.h> |
Phil Reid | b70f0a2 | 2017-07-11 17:43:42 +0800 | [diff] [blame] | 12 | #include <linux/i2c.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
Sachin Kamat | 075ed03 | 2013-03-14 15:49:46 +0530 | [diff] [blame] | 17 | #include <linux/of.h> |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 18 | #include <linux/of_device.h> |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 19 | #include <linux/power/sbs-battery.h> |
Phil Reid | b70f0a2 | 2017-07-11 17:43:42 +0800 | [diff] [blame] | 20 | #include <linux/power_supply.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/stat.h> |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 23 | |
| 24 | enum { |
| 25 | REG_MANUFACTURER_DATA, |
| 26 | REG_TEMPERATURE, |
| 27 | REG_VOLTAGE, |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 28 | REG_CURRENT_NOW, |
| 29 | REG_CURRENT_AVG, |
Sebastian Reichel | d6f5632 | 2020-05-13 20:56:02 +0200 | [diff] [blame] | 30 | REG_MAX_ERR, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 31 | REG_CAPACITY, |
| 32 | REG_TIME_TO_EMPTY, |
| 33 | REG_TIME_TO_FULL, |
| 34 | REG_STATUS, |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 35 | REG_CAPACITY_LEVEL, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 36 | REG_CYCLE_COUNT, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 37 | REG_SERIAL_NUMBER, |
| 38 | REG_REMAINING_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 39 | REG_REMAINING_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 40 | REG_FULL_CHARGE_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 41 | REG_FULL_CHARGE_CAPACITY_CHARGE, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 42 | REG_DESIGN_CAPACITY, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 43 | REG_DESIGN_CAPACITY_CHARGE, |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 44 | REG_DESIGN_VOLTAGE_MIN, |
| 45 | REG_DESIGN_VOLTAGE_MAX, |
Sebastian Reichel | 3e9544f | 2020-05-13 20:56:06 +0200 | [diff] [blame] | 46 | REG_CHEMISTRY, |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 47 | REG_MANUFACTURER, |
| 48 | REG_MODEL_NAME, |
Sebastian Reichel | 787fdbc | 2020-05-13 20:56:07 +0200 | [diff] [blame^] | 49 | REG_CHARGE_CURRENT, |
| 50 | REG_CHARGE_VOLTAGE, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Sebastian Reichel | 79bcd5a | 2020-05-13 20:56:04 +0200 | [diff] [blame] | 53 | #define REG_ADDR_SPEC_INFO 0x1A |
| 54 | #define SPEC_INFO_VERSION_MASK GENMASK(7, 4) |
| 55 | #define SPEC_INFO_VERSION_SHIFT 4 |
| 56 | |
| 57 | #define SBS_VERSION_1_0 1 |
| 58 | #define SBS_VERSION_1_1 2 |
| 59 | #define SBS_VERSION_1_1_WITH_PEC 3 |
| 60 | |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 61 | /* Battery Mode defines */ |
| 62 | #define BATTERY_MODE_OFFSET 0x03 |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 63 | #define BATTERY_MODE_CAPACITY_MASK BIT(15) |
| 64 | enum sbs_capacity_mode { |
| 65 | CAPACITY_MODE_AMPS = 0, |
| 66 | CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 69 | /* manufacturer access defines */ |
| 70 | #define MANUFACTURER_ACCESS_STATUS 0x0006 |
| 71 | #define MANUFACTURER_ACCESS_SLEEP 0x0011 |
| 72 | |
| 73 | /* battery status value bits */ |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 74 | #define BATTERY_INITIALIZED 0x80 |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 75 | #define BATTERY_DISCHARGING 0x40 |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 76 | #define BATTERY_FULL_CHARGED 0x20 |
| 77 | #define BATTERY_FULL_DISCHARGED 0x10 |
| 78 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 79 | /* min_value and max_value are only valid for numerical data */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 80 | #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 81 | .psp = _psp, \ |
| 82 | .addr = _addr, \ |
| 83 | .min_value = _min_value, \ |
| 84 | .max_value = _max_value, \ |
| 85 | } |
| 86 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 87 | static const struct chip_data { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 88 | enum power_supply_property psp; |
| 89 | u8 addr; |
| 90 | int min_value; |
| 91 | int max_value; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 92 | } sbs_data[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 93 | [REG_MANUFACTURER_DATA] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 94 | SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 95 | [REG_TEMPERATURE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 96 | SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 97 | [REG_VOLTAGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 98 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000), |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 99 | [REG_CURRENT_NOW] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 100 | SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767), |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 101 | [REG_CURRENT_AVG] = |
| 102 | SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767), |
Sebastian Reichel | d6f5632 | 2020-05-13 20:56:02 +0200 | [diff] [blame] | 103 | [REG_MAX_ERR] = |
| 104 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 105 | [REG_CAPACITY] = |
Nikolaus Voss | b1f092f | 2012-04-25 08:59:03 +0200 | [diff] [blame] | 106 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 107 | [REG_REMAINING_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 108 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 109 | [REG_REMAINING_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 110 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 111 | [REG_FULL_CHARGE_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 112 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 113 | [REG_FULL_CHARGE_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 114 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 115 | [REG_TIME_TO_EMPTY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 116 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 117 | [REG_TIME_TO_FULL] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 118 | SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535), |
Sebastian Reichel | 787fdbc | 2020-05-13 20:56:07 +0200 | [diff] [blame^] | 119 | [REG_CHARGE_CURRENT] = |
| 120 | SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535), |
| 121 | [REG_CHARGE_VOLTAGE] = |
| 122 | SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 123 | [REG_STATUS] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 124 | SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535), |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 125 | [REG_CAPACITY_LEVEL] = |
| 126 | SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 127 | [REG_CYCLE_COUNT] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 128 | SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535), |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 129 | [REG_DESIGN_CAPACITY] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 130 | SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535), |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 131 | [REG_DESIGN_CAPACITY_CHARGE] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 132 | SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535), |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 133 | [REG_DESIGN_VOLTAGE_MIN] = |
| 134 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535), |
| 135 | [REG_DESIGN_VOLTAGE_MAX] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 136 | SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535), |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 137 | [REG_SERIAL_NUMBER] = |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 138 | SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535), |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 139 | /* Properties of type `const char *' */ |
| 140 | [REG_MANUFACTURER] = |
| 141 | SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535), |
| 142 | [REG_MODEL_NAME] = |
Sebastian Reichel | 3e9544f | 2020-05-13 20:56:06 +0200 | [diff] [blame] | 143 | SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535), |
| 144 | [REG_CHEMISTRY] = |
| 145 | SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 148 | static enum power_supply_property sbs_properties[] = { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 149 | POWER_SUPPLY_PROP_STATUS, |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 150 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 151 | POWER_SUPPLY_PROP_HEALTH, |
| 152 | POWER_SUPPLY_PROP_PRESENT, |
| 153 | POWER_SUPPLY_PROP_TECHNOLOGY, |
| 154 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
| 155 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 156 | POWER_SUPPLY_PROP_CURRENT_NOW, |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 157 | POWER_SUPPLY_PROP_CURRENT_AVG, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 158 | POWER_SUPPLY_PROP_CAPACITY, |
Sebastian Reichel | d6f5632 | 2020-05-13 20:56:02 +0200 | [diff] [blame] | 159 | POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 160 | POWER_SUPPLY_PROP_TEMP, |
| 161 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 162 | POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, |
| 163 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 164 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 165 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
| 166 | POWER_SUPPLY_PROP_ENERGY_NOW, |
| 167 | POWER_SUPPLY_PROP_ENERGY_FULL, |
| 168 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 169 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 170 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 171 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
Sebastian Reichel | 787fdbc | 2020-05-13 20:56:07 +0200 | [diff] [blame^] | 172 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
| 173 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 174 | /* Properties of type `const char *' */ |
| 175 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 176 | POWER_SUPPLY_PROP_MODEL_NAME |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Sebastian Reichel | 0ff9691 | 2020-05-13 20:56:01 +0200 | [diff] [blame] | 179 | /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */ |
| 180 | #define SBS_FLAGS_TI_BQ20ZX5 BIT(0) |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 181 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 182 | struct sbs_info { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 183 | struct i2c_client *client; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 184 | struct power_supply *power_supply; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 185 | bool is_present; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 186 | struct gpio_desc *gpio_detect; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 187 | bool enable_detection; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 188 | int last_state; |
| 189 | int poll_time; |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 190 | u32 i2c_retry_count; |
| 191 | u32 poll_retry_count; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 192 | struct delayed_work work; |
Shawn Nematbakhsh | fe8a653 | 2017-06-13 10:53:25 -0700 | [diff] [blame] | 193 | struct mutex mode_lock; |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 194 | u32 flags; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 197 | static char model_name[I2C_SMBUS_BLOCK_MAX + 1]; |
| 198 | static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1]; |
Sebastian Reichel | 3e9544f | 2020-05-13 20:56:06 +0200 | [diff] [blame] | 199 | static char chemistry[I2C_SMBUS_BLOCK_MAX + 1]; |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 200 | static bool force_load; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 201 | |
Sebastian Reichel | 79bcd5a | 2020-05-13 20:56:04 +0200 | [diff] [blame] | 202 | static int sbs_update_presence(struct sbs_info *chip, bool is_present) |
| 203 | { |
| 204 | struct i2c_client *client = chip->client; |
| 205 | int retries = chip->i2c_retry_count; |
| 206 | s32 ret = 0; |
| 207 | u8 version; |
| 208 | |
| 209 | if (chip->is_present == is_present) |
| 210 | return 0; |
| 211 | |
| 212 | if (!is_present) { |
| 213 | chip->is_present = false; |
| 214 | /* Disable PEC when no device is present */ |
| 215 | client->flags &= ~I2C_CLIENT_PEC; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* Check if device supports packet error checking and use it */ |
| 220 | while (retries > 0) { |
| 221 | ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO); |
| 222 | if (ret >= 0) |
| 223 | break; |
| 224 | |
| 225 | /* |
| 226 | * Some batteries trigger the detection pin before the |
| 227 | * I2C bus is properly connected. This works around the |
| 228 | * issue. |
| 229 | */ |
| 230 | msleep(100); |
| 231 | |
| 232 | retries--; |
| 233 | } |
| 234 | |
| 235 | if (ret < 0) { |
| 236 | dev_dbg(&client->dev, "failed to read spec info: %d\n", ret); |
| 237 | |
| 238 | /* fallback to old behaviour */ |
| 239 | client->flags &= ~I2C_CLIENT_PEC; |
| 240 | chip->is_present = true; |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT; |
| 246 | |
| 247 | if (version == SBS_VERSION_1_1_WITH_PEC) |
| 248 | client->flags |= I2C_CLIENT_PEC; |
| 249 | else |
| 250 | client->flags &= ~I2C_CLIENT_PEC; |
| 251 | |
| 252 | dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ? |
| 253 | "enabled" : "disabled"); |
| 254 | |
| 255 | chip->is_present = true; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 260 | static int sbs_read_word_data(struct i2c_client *client, u8 address) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 261 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 262 | struct sbs_info *chip = i2c_get_clientdata(client); |
Wolfram Sang | 9410b7d | 2017-10-29 00:42:10 +0200 | [diff] [blame] | 263 | int retries = chip->i2c_retry_count; |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 264 | s32 ret = 0; |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 265 | |
| 266 | while (retries > 0) { |
| 267 | ret = i2c_smbus_read_word_data(client, address); |
| 268 | if (ret >= 0) |
| 269 | break; |
| 270 | retries--; |
| 271 | } |
| 272 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 273 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 274 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 275 | "%s: i2c read at address 0x%x failed\n", |
| 276 | __func__, address); |
| 277 | return ret; |
| 278 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 279 | |
Phil Reid | a1bbec7 | 2017-06-15 21:59:29 +0800 | [diff] [blame] | 280 | return ret; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 283 | static int sbs_read_string_data(struct i2c_client *client, u8 address, |
| 284 | char *values) |
| 285 | { |
| 286 | struct sbs_info *chip = i2c_get_clientdata(client); |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 287 | int retries = chip->i2c_retry_count; |
| 288 | s32 ret = 0; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 289 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 290 | if (!i2c_check_functionality(client->adapter, |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 291 | I2C_FUNC_SMBUS_READ_BLOCK_DATA)) { |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 292 | return -ENODEV; |
| 293 | } |
| 294 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 295 | /* Get the block data */ |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 296 | while (retries > 0) { |
| 297 | ret = i2c_smbus_read_block_data(client, address, values); |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 298 | if (ret >= 0) |
| 299 | break; |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 300 | retries--; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (ret < 0) { |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 304 | dev_dbg(&client->dev, "%s: failed to read block 0x%x: %d\n", |
| 305 | __func__, address, ret); |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 306 | return ret; |
| 307 | } |
| 308 | |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 309 | /* add string termination */ |
| 310 | values[ret] = '\0'; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 311 | |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 312 | return 0; |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 315 | static int sbs_write_word_data(struct i2c_client *client, u8 address, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 316 | u16 value) |
| 317 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 318 | struct sbs_info *chip = i2c_get_clientdata(client); |
Wolfram Sang | 9410b7d | 2017-10-29 00:42:10 +0200 | [diff] [blame] | 319 | int retries = chip->i2c_retry_count; |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 320 | s32 ret = 0; |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 321 | |
| 322 | while (retries > 0) { |
Phil Reid | a1bbec7 | 2017-06-15 21:59:29 +0800 | [diff] [blame] | 323 | ret = i2c_smbus_write_word_data(client, address, value); |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 324 | if (ret >= 0) |
| 325 | break; |
| 326 | retries--; |
| 327 | } |
| 328 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 329 | if (ret < 0) { |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 330 | dev_dbg(&client->dev, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 331 | "%s: i2c write to address 0x%x failed\n", |
| 332 | __func__, address); |
| 333 | return ret; |
| 334 | } |
Rhyland Klein | ff28fce | 2011-02-28 16:55:29 -0800 | [diff] [blame] | 335 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
Paul Kocialkowski | 7f93e1f | 2017-04-25 17:09:05 +0200 | [diff] [blame] | 339 | static int sbs_status_correct(struct i2c_client *client, int *intval) |
| 340 | { |
| 341 | int ret; |
| 342 | |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 343 | ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr); |
Paul Kocialkowski | 7f93e1f | 2017-04-25 17:09:05 +0200 | [diff] [blame] | 344 | if (ret < 0) |
| 345 | return ret; |
| 346 | |
| 347 | ret = (s16)ret; |
| 348 | |
| 349 | /* Not drawing current means full (cannot be not charging) */ |
| 350 | if (ret == 0) |
| 351 | *intval = POWER_SUPPLY_STATUS_FULL; |
| 352 | |
| 353 | if (*intval == POWER_SUPPLY_STATUS_FULL) { |
| 354 | /* Drawing or providing current when full */ |
| 355 | if (ret > 0) |
| 356 | *intval = POWER_SUPPLY_STATUS_CHARGING; |
| 357 | else if (ret < 0) |
| 358 | *intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 364 | static int sbs_get_battery_presence_and_health( |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 365 | struct i2c_client *client, enum power_supply_property psp, |
| 366 | union power_supply_propval *val) |
| 367 | { |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 368 | int ret; |
| 369 | |
Michael Nosthoff | fe55e77 | 2019-08-16 09:58:42 +0200 | [diff] [blame] | 370 | /* Dummy command; if it succeeds, battery is present. */ |
| 371 | ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); |
| 372 | |
| 373 | if (ret < 0) { /* battery not present*/ |
| 374 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 375 | val->intval = 0; |
| 376 | return 0; |
| 377 | } |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 382 | val->intval = 1; /* battery present */ |
| 383 | else /* POWER_SUPPLY_PROP_HEALTH */ |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 384 | /* SBS spec doesn't have a general health command. */ |
| 385 | val->intval = POWER_SUPPLY_HEALTH_UNKNOWN; |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int sbs_get_ti_battery_presence_and_health( |
| 391 | struct i2c_client *client, enum power_supply_property psp, |
| 392 | union power_supply_propval *val) |
| 393 | { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 394 | s32 ret; |
| 395 | |
Guenter Roeck | 17c6d39 | 2016-09-08 19:10:00 -0700 | [diff] [blame] | 396 | /* |
| 397 | * Write to ManufacturerAccess with ManufacturerAccess command |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 398 | * and then read the status. |
Guenter Roeck | 17c6d39 | 2016-09-08 19:10:00 -0700 | [diff] [blame] | 399 | */ |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 400 | ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr, |
| 401 | MANUFACTURER_ACCESS_STATUS); |
| 402 | if (ret < 0) { |
| 403 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 404 | val->intval = 0; /* battery removed */ |
| 405 | return ret; |
| 406 | } |
Guenter Roeck | 17c6d39 | 2016-09-08 19:10:00 -0700 | [diff] [blame] | 407 | |
| 408 | ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 409 | if (ret < 0) { |
| 410 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 411 | val->intval = 0; /* battery removed */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 412 | return ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 413 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 414 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 415 | if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value || |
| 416 | ret > sbs_data[REG_MANUFACTURER_DATA].max_value) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 417 | val->intval = 0; |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | /* Mask the upper nibble of 2nd byte and |
| 422 | * lower byte of response then |
| 423 | * shift the result by 8 to get status*/ |
| 424 | ret &= 0x0F00; |
| 425 | ret >>= 8; |
| 426 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 427 | if (ret == 0x0F) |
| 428 | /* battery removed */ |
| 429 | val->intval = 0; |
| 430 | else |
| 431 | val->intval = 1; |
| 432 | } else if (psp == POWER_SUPPLY_PROP_HEALTH) { |
| 433 | if (ret == 0x09) |
| 434 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 435 | else if (ret == 0x0B) |
| 436 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 437 | else if (ret == 0x0C) |
| 438 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 439 | else |
| 440 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 446 | static int sbs_get_battery_property(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 447 | int reg_offset, enum power_supply_property psp, |
| 448 | union power_supply_propval *val) |
| 449 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 450 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 451 | s32 ret; |
| 452 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 453 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 454 | if (ret < 0) |
| 455 | return ret; |
| 456 | |
| 457 | /* returned values are 16 bit */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 458 | if (sbs_data[reg_offset].min_value < 0) |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 459 | ret = (s16)ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 460 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 461 | if (ret >= sbs_data[reg_offset].min_value && |
| 462 | ret <= sbs_data[reg_offset].max_value) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 463 | val->intval = ret; |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 464 | if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) { |
| 465 | if (!(ret & BATTERY_INITIALIZED)) |
| 466 | val->intval = |
| 467 | POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; |
| 468 | else if (ret & BATTERY_FULL_CHARGED) |
| 469 | val->intval = |
| 470 | POWER_SUPPLY_CAPACITY_LEVEL_FULL; |
| 471 | else if (ret & BATTERY_FULL_DISCHARGED) |
| 472 | val->intval = |
| 473 | POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; |
| 474 | else |
| 475 | val->intval = |
| 476 | POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 477 | return 0; |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 478 | } else if (psp != POWER_SUPPLY_PROP_STATUS) { |
| 479 | return 0; |
| 480 | } |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 481 | |
| 482 | if (ret & BATTERY_FULL_CHARGED) |
| 483 | val->intval = POWER_SUPPLY_STATUS_FULL; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 484 | else if (ret & BATTERY_DISCHARGING) |
| 485 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; |
| 486 | else |
| 487 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
| 488 | |
Paul Kocialkowski | 7f93e1f | 2017-04-25 17:09:05 +0200 | [diff] [blame] | 489 | sbs_status_correct(client, &val->intval); |
| 490 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 491 | if (chip->poll_time == 0) |
| 492 | chip->last_state = val->intval; |
| 493 | else if (chip->last_state != val->intval) { |
| 494 | cancel_delayed_work_sync(&chip->work); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 495 | power_supply_changed(chip->power_supply); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 496 | chip->poll_time = 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 497 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 498 | } else { |
| 499 | if (psp == POWER_SUPPLY_PROP_STATUS) |
| 500 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; |
Shawn Nematbakhsh | bfa953d | 2017-06-13 10:53:26 -0700 | [diff] [blame] | 501 | else if (psp == POWER_SUPPLY_PROP_CAPACITY) |
| 502 | /* sbs spec says that this can be >100 % |
| 503 | * even if max value is 100 % |
| 504 | */ |
| 505 | val->intval = min(ret, 100); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 506 | else |
| 507 | val->intval = 0; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 513 | static int sbs_get_battery_string_property(struct i2c_client *client, |
| 514 | int reg_offset, enum power_supply_property psp, char *val) |
| 515 | { |
Sebastian Reichel | c4b12a2 | 2020-05-13 20:56:03 +0200 | [diff] [blame] | 516 | return sbs_read_string_data(client, sbs_data[reg_offset].addr, val); |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 519 | static void sbs_unit_adjustment(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 520 | enum power_supply_property psp, union power_supply_propval *val) |
| 521 | { |
| 522 | #define BASE_UNIT_CONVERSION 1000 |
| 523 | #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION) |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 524 | #define TIME_UNIT_CONVERSION 60 |
| 525 | #define TEMP_KELVIN_TO_CELSIUS 2731 |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 526 | switch (psp) { |
| 527 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 528 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 529 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 530 | /* sbs provides energy in units of 10mWh. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 531 | * Convert to µWh |
| 532 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 533 | val->intval *= BATTERY_MODE_CAP_MULT_WATT; |
| 534 | break; |
| 535 | |
| 536 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 537 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 538 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
| 539 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 540 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 541 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
Sebastian Reichel | 787fdbc | 2020-05-13 20:56:07 +0200 | [diff] [blame^] | 542 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| 543 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 544 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 545 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 546 | val->intval *= BASE_UNIT_CONVERSION; |
| 547 | break; |
| 548 | |
| 549 | case POWER_SUPPLY_PROP_TEMP: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 550 | /* sbs provides battery temperature in 0.1K |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 551 | * so convert it to 0.1°C |
| 552 | */ |
| 553 | val->intval -= TEMP_KELVIN_TO_CELSIUS; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 554 | break; |
| 555 | |
| 556 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 557 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 558 | /* sbs provides time to empty and time to full in minutes. |
Benson Leung | 909a78b | 2011-02-27 17:41:48 -0800 | [diff] [blame] | 559 | * Convert to seconds |
| 560 | */ |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 561 | val->intval *= TIME_UNIT_CONVERSION; |
| 562 | break; |
| 563 | |
| 564 | default: |
| 565 | dev_dbg(&client->dev, |
| 566 | "%s: no need for unit conversion %d\n", __func__, psp); |
| 567 | } |
| 568 | } |
| 569 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 570 | static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client, |
| 571 | enum sbs_capacity_mode mode) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 572 | { |
| 573 | int ret, original_val; |
| 574 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 575 | original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 576 | if (original_val < 0) |
| 577 | return original_val; |
| 578 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 579 | if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 580 | return mode; |
| 581 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 582 | if (mode == CAPACITY_MODE_AMPS) |
| 583 | ret = original_val & ~BATTERY_MODE_CAPACITY_MASK; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 584 | else |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 585 | ret = original_val | BATTERY_MODE_CAPACITY_MASK; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 586 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 587 | ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 588 | if (ret < 0) |
| 589 | return ret; |
| 590 | |
Phil Reid | adcf04c | 2017-07-11 17:43:43 +0800 | [diff] [blame] | 591 | usleep_range(1000, 2000); |
| 592 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 593 | return original_val & BATTERY_MODE_CAPACITY_MASK; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 596 | static int sbs_get_battery_capacity(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 597 | int reg_offset, enum power_supply_property psp, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 598 | union power_supply_propval *val) |
| 599 | { |
| 600 | s32 ret; |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 601 | enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 602 | |
| 603 | if (power_supply_is_amp_property(psp)) |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 604 | mode = CAPACITY_MODE_AMPS; |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 605 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 606 | mode = sbs_set_capacity_mode(client, mode); |
Dan Carpenter | eb368de | 2019-09-25 14:01:28 +0300 | [diff] [blame] | 607 | if ((int)mode < 0) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 608 | return mode; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 609 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 610 | ret = sbs_read_word_data(client, sbs_data[reg_offset].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 611 | if (ret < 0) |
| 612 | return ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 613 | |
Shawn Nematbakhsh | bfa953d | 2017-06-13 10:53:26 -0700 | [diff] [blame] | 614 | val->intval = ret; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 615 | |
Jean-Francois Dagenais | e2ec6ae | 2019-11-01 15:07:03 -0400 | [diff] [blame] | 616 | ret = sbs_set_capacity_mode(client, mode); |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 617 | if (ret < 0) |
| 618 | return ret; |
| 619 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 620 | return 0; |
| 621 | } |
| 622 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 623 | static char sbs_serial[5]; |
| 624 | static int sbs_get_battery_serial_number(struct i2c_client *client, |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 625 | union power_supply_propval *val) |
| 626 | { |
| 627 | int ret; |
| 628 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 629 | ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 630 | if (ret < 0) |
| 631 | return ret; |
| 632 | |
Wolfram Sang | 5e9bee5 | 2017-10-29 00:45:59 +0200 | [diff] [blame] | 633 | sprintf(sbs_serial, "%04x", ret); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 634 | val->strval = sbs_serial; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 639 | static int sbs_get_property_index(struct i2c_client *client, |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 640 | enum power_supply_property psp) |
| 641 | { |
| 642 | int count; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 643 | for (count = 0; count < ARRAY_SIZE(sbs_data); count++) |
| 644 | if (psp == sbs_data[count].psp) |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 645 | return count; |
| 646 | |
| 647 | dev_warn(&client->dev, |
| 648 | "%s: Invalid Property - %d\n", __func__, psp); |
| 649 | |
| 650 | return -EINVAL; |
| 651 | } |
| 652 | |
Sebastian Reichel | 3e9544f | 2020-05-13 20:56:06 +0200 | [diff] [blame] | 653 | static int sbs_get_chemistry(struct i2c_client *client, |
| 654 | union power_supply_propval *val) |
| 655 | { |
| 656 | enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY; |
| 657 | int ret; |
| 658 | |
| 659 | ret = sbs_get_property_index(client, psp); |
| 660 | if (ret < 0) |
| 661 | return ret; |
| 662 | |
| 663 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 664 | chemistry); |
| 665 | if (ret < 0) |
| 666 | return ret; |
| 667 | |
| 668 | if (!strncasecmp(chemistry, "LION", 4)) |
| 669 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 670 | else if (!strncasecmp(chemistry, "LiP", 3)) |
| 671 | val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO; |
| 672 | else if (!strncasecmp(chemistry, "NiCd", 4)) |
| 673 | val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd; |
| 674 | else if (!strncasecmp(chemistry, "NiMH", 4)) |
| 675 | val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; |
| 676 | else |
| 677 | val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
| 678 | |
| 679 | if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN) |
| 680 | dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry); |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 685 | static int sbs_get_property(struct power_supply *psy, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 686 | enum power_supply_property psp, |
| 687 | union power_supply_propval *val) |
| 688 | { |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 689 | int ret = 0; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 690 | struct sbs_info *chip = power_supply_get_drvdata(psy); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 691 | struct i2c_client *client = chip->client; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 692 | |
Phil Reid | 1cf8555 | 2017-08-24 17:31:10 +0800 | [diff] [blame] | 693 | if (chip->gpio_detect) { |
| 694 | ret = gpiod_get_value_cansleep(chip->gpio_detect); |
| 695 | if (ret < 0) |
| 696 | return ret; |
| 697 | if (psp == POWER_SUPPLY_PROP_PRESENT) { |
| 698 | val->intval = ret; |
Sebastian Reichel | 79bcd5a | 2020-05-13 20:56:04 +0200 | [diff] [blame] | 699 | sbs_update_presence(chip, ret); |
Phil Reid | 1cf8555 | 2017-08-24 17:31:10 +0800 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | if (ret == 0) |
| 703 | return -ENODATA; |
| 704 | } |
| 705 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 706 | switch (psp) { |
| 707 | case POWER_SUPPLY_PROP_PRESENT: |
| 708 | case POWER_SUPPLY_PROP_HEALTH: |
Sebastian Reichel | 0ff9691 | 2020-05-13 20:56:01 +0200 | [diff] [blame] | 709 | if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 710 | ret = sbs_get_ti_battery_presence_and_health(client, |
| 711 | psp, val); |
| 712 | else |
| 713 | ret = sbs_get_battery_presence_and_health(client, psp, |
| 714 | val); |
Michael Nosthoff | fe55e77 | 2019-08-16 09:58:42 +0200 | [diff] [blame] | 715 | |
| 716 | /* this can only be true if no gpio is used */ |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 717 | if (psp == POWER_SUPPLY_PROP_PRESENT) |
| 718 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 719 | break; |
| 720 | |
| 721 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
Sebastian Reichel | 3e9544f | 2020-05-13 20:56:06 +0200 | [diff] [blame] | 722 | ret = sbs_get_chemistry(client, val); |
| 723 | if (ret < 0) |
| 724 | break; |
| 725 | |
Nikolaus Voss | 5da5098 | 2012-05-09 08:30:44 +0200 | [diff] [blame] | 726 | goto done; /* don't trigger power_supply_changed()! */ |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 727 | |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 728 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 729 | case POWER_SUPPLY_PROP_ENERGY_FULL: |
| 730 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
Rhyland Klein | 51d0756 | 2011-01-25 11:10:06 -0800 | [diff] [blame] | 731 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 732 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 733 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 734 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 735 | if (ret < 0) |
| 736 | break; |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 737 | |
Shawn Nematbakhsh | fe8a653 | 2017-06-13 10:53:25 -0700 | [diff] [blame] | 738 | /* sbs_get_battery_capacity() will change the battery mode |
| 739 | * temporarily to read the requested attribute. Ensure we stay |
| 740 | * in the desired mode for the duration of the attribute read. |
| 741 | */ |
| 742 | mutex_lock(&chip->mode_lock); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 743 | ret = sbs_get_battery_capacity(client, ret, psp, val); |
Shawn Nematbakhsh | fe8a653 | 2017-06-13 10:53:25 -0700 | [diff] [blame] | 744 | mutex_unlock(&chip->mode_lock); |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 745 | break; |
| 746 | |
| 747 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 748 | ret = sbs_get_battery_serial_number(client, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 749 | break; |
| 750 | |
| 751 | case POWER_SUPPLY_PROP_STATUS: |
Joshua Clayton | 957cb72 | 2016-08-11 09:59:12 -0700 | [diff] [blame] | 752 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 753 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 754 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 755 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Sebastian Reichel | 8ce6ee4 | 2020-05-13 20:56:05 +0200 | [diff] [blame] | 756 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 757 | case POWER_SUPPLY_PROP_TEMP: |
| 758 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
| 759 | case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG: |
Simon Que | 4495b0a | 2014-08-04 13:47:46 +0200 | [diff] [blame] | 760 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 761 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
Sebastian Reichel | 787fdbc | 2020-05-13 20:56:07 +0200 | [diff] [blame^] | 762 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
| 763 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: |
Shawn Nematbakhsh | bfa953d | 2017-06-13 10:53:26 -0700 | [diff] [blame] | 764 | case POWER_SUPPLY_PROP_CAPACITY: |
Sebastian Reichel | d6f5632 | 2020-05-13 20:56:02 +0200 | [diff] [blame] | 765 | case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN: |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 766 | ret = sbs_get_property_index(client, psp); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 767 | if (ret < 0) |
| 768 | break; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 769 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 770 | ret = sbs_get_battery_property(client, ret, psp, val); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 771 | break; |
| 772 | |
Cheng-Yi Chiang | 9ea8940 | 2014-08-04 13:47:45 +0200 | [diff] [blame] | 773 | case POWER_SUPPLY_PROP_MODEL_NAME: |
| 774 | ret = sbs_get_property_index(client, psp); |
| 775 | if (ret < 0) |
| 776 | break; |
| 777 | |
| 778 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 779 | model_name); |
| 780 | val->strval = model_name; |
| 781 | break; |
| 782 | |
| 783 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 784 | ret = sbs_get_property_index(client, psp); |
| 785 | if (ret < 0) |
| 786 | break; |
| 787 | |
| 788 | ret = sbs_get_battery_string_property(client, ret, psp, |
| 789 | manufacturer); |
| 790 | val->strval = manufacturer; |
| 791 | break; |
| 792 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 793 | default: |
| 794 | dev_err(&client->dev, |
| 795 | "%s: INVALID property\n", __func__); |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 799 | if (!chip->enable_detection) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 800 | goto done; |
| 801 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 802 | if (!chip->gpio_detect && |
| 803 | chip->is_present != (ret >= 0)) { |
Sebastian Reichel | 79bcd5a | 2020-05-13 20:56:04 +0200 | [diff] [blame] | 804 | sbs_update_presence(chip, (ret >= 0)); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 805 | power_supply_changed(chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | done: |
| 809 | if (!ret) { |
| 810 | /* Convert units to match requirements for power supply class */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 811 | sbs_unit_adjustment(client, psp, val); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 812 | } |
Rhyland Klein | d3ab61e | 2010-09-21 15:33:55 -0700 | [diff] [blame] | 813 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 814 | dev_dbg(&client->dev, |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 815 | "%s: property = %d, value = %x\n", __func__, psp, val->intval); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 816 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 817 | if (ret && chip->is_present) |
Rhyland Klein | a7d9ace | 2011-03-09 16:18:02 -0800 | [diff] [blame] | 818 | return ret; |
| 819 | |
| 820 | /* battery not present, so return NODATA for properties */ |
| 821 | if (ret) |
| 822 | return -ENODATA; |
| 823 | |
| 824 | return 0; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Phil Reid | cda3b01 | 2017-05-01 16:49:58 +0800 | [diff] [blame] | 827 | static void sbs_supply_changed(struct sbs_info *chip) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 828 | { |
Phil Reid | d2cec82 | 2016-07-25 10:42:58 +0800 | [diff] [blame] | 829 | struct power_supply *battery = chip->power_supply; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 830 | int ret; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 831 | |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 832 | ret = gpiod_get_value_cansleep(chip->gpio_detect); |
| 833 | if (ret < 0) |
Phil Reid | cda3b01 | 2017-05-01 16:49:58 +0800 | [diff] [blame] | 834 | return; |
Sebastian Reichel | 79bcd5a | 2020-05-13 20:56:04 +0200 | [diff] [blame] | 835 | sbs_update_presence(chip, ret); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 836 | power_supply_changed(battery); |
Phil Reid | cda3b01 | 2017-05-01 16:49:58 +0800 | [diff] [blame] | 837 | } |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 838 | |
Phil Reid | cda3b01 | 2017-05-01 16:49:58 +0800 | [diff] [blame] | 839 | static irqreturn_t sbs_irq(int irq, void *devid) |
| 840 | { |
| 841 | sbs_supply_changed(devid); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 842 | return IRQ_HANDLED; |
| 843 | } |
| 844 | |
Phil Reid | cda3b01 | 2017-05-01 16:49:58 +0800 | [diff] [blame] | 845 | static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot, |
| 846 | unsigned int data) |
| 847 | { |
| 848 | sbs_supply_changed(i2c_get_clientdata(client)); |
| 849 | } |
| 850 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 851 | static void sbs_external_power_changed(struct power_supply *psy) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 852 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 853 | struct sbs_info *chip = power_supply_get_drvdata(psy); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 854 | |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 855 | /* cancel outstanding work */ |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 856 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 857 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 858 | schedule_delayed_work(&chip->work, HZ); |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 859 | chip->poll_time = chip->poll_retry_count; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 862 | static void sbs_delayed_work(struct work_struct *work) |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 863 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 864 | struct sbs_info *chip; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 865 | s32 ret; |
| 866 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 867 | chip = container_of(work, struct sbs_info, work.work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 868 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 869 | ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 870 | /* if the read failed, give up on this work */ |
| 871 | if (ret < 0) { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 872 | chip->poll_time = 0; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | |
| 876 | if (ret & BATTERY_FULL_CHARGED) |
| 877 | ret = POWER_SUPPLY_STATUS_FULL; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 878 | else if (ret & BATTERY_DISCHARGING) |
| 879 | ret = POWER_SUPPLY_STATUS_DISCHARGING; |
| 880 | else |
| 881 | ret = POWER_SUPPLY_STATUS_CHARGING; |
| 882 | |
Paul Kocialkowski | 7f93e1f | 2017-04-25 17:09:05 +0200 | [diff] [blame] | 883 | sbs_status_correct(chip->client, &ret); |
| 884 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 885 | if (chip->last_state != ret) { |
| 886 | chip->poll_time = 0; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 887 | power_supply_changed(chip->power_supply); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 888 | return; |
| 889 | } |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 890 | if (chip->poll_time > 0) { |
| 891 | schedule_delayed_work(&chip->work, HZ); |
| 892 | chip->poll_time--; |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | } |
| 896 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 897 | static const struct power_supply_desc sbs_default_desc = { |
| 898 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 899 | .properties = sbs_properties, |
| 900 | .num_properties = ARRAY_SIZE(sbs_properties), |
| 901 | .get_property = sbs_get_property, |
| 902 | .external_power_changed = sbs_external_power_changed, |
| 903 | }; |
| 904 | |
Bill Pemberton | c8afa64 | 2012-11-19 13:22:23 -0500 | [diff] [blame] | 905 | static int sbs_probe(struct i2c_client *client, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 906 | const struct i2c_device_id *id) |
| 907 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 908 | struct sbs_info *chip; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 909 | struct power_supply_desc *sbs_desc; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 910 | struct sbs_platform_data *pdata = client->dev.platform_data; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 911 | struct power_supply_config psy_cfg = {}; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 912 | int rc; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 913 | int irq; |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 914 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 915 | sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc, |
| 916 | sizeof(*sbs_desc), GFP_KERNEL); |
| 917 | if (!sbs_desc) |
Rhyland Klein | 52f56c6 | 2011-12-05 17:50:49 -0800 | [diff] [blame] | 918 | return -ENOMEM; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 919 | |
| 920 | sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s", |
| 921 | dev_name(&client->dev)); |
| 922 | if (!sbs_desc->name) |
| 923 | return -ENOMEM; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 924 | |
Phil Reid | 9239a86 | 2016-07-25 10:42:57 +0800 | [diff] [blame] | 925 | chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 926 | if (!chip) |
| 927 | return -ENOMEM; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 928 | |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 929 | chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 930 | chip->client = client; |
| 931 | chip->enable_detection = false; |
Krzysztof Kozlowski | 2dc9215 | 2015-03-12 08:44:02 +0100 | [diff] [blame] | 932 | psy_cfg.of_node = client->dev.of_node; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 933 | psy_cfg.drv_data = chip; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 934 | chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN; |
Shawn Nematbakhsh | fe8a653 | 2017-06-13 10:53:25 -0700 | [diff] [blame] | 935 | mutex_init(&chip->mode_lock); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 936 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 937 | /* use pdata if available, fall back to DT properties, |
| 938 | * or hardcoded defaults if not |
| 939 | */ |
| 940 | rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count", |
| 941 | &chip->i2c_retry_count); |
| 942 | if (rc) |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 943 | chip->i2c_retry_count = 0; |
Rhyland Klein | 6c75ea1 | 2011-09-14 13:19:07 -0700 | [diff] [blame] | 944 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 945 | rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count", |
| 946 | &chip->poll_retry_count); |
| 947 | if (rc) |
| 948 | chip->poll_retry_count = 0; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 949 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 950 | if (pdata) { |
| 951 | chip->poll_retry_count = pdata->poll_retry_count; |
| 952 | chip->i2c_retry_count = pdata->i2c_retry_count; |
| 953 | } |
Phil Reid | 389958b | 2016-09-20 09:01:12 +0800 | [diff] [blame] | 954 | chip->i2c_retry_count = chip->i2c_retry_count + 1; |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 955 | |
| 956 | chip->gpio_detect = devm_gpiod_get_optional(&client->dev, |
| 957 | "sbs,battery-detect", GPIOD_IN); |
| 958 | if (IS_ERR(chip->gpio_detect)) { |
| 959 | dev_err(&client->dev, "Failed to get gpio: %ld\n", |
| 960 | PTR_ERR(chip->gpio_detect)); |
| 961 | return PTR_ERR(chip->gpio_detect); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 962 | } |
| 963 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 964 | i2c_set_clientdata(client, chip); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 965 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 966 | if (!chip->gpio_detect) |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 967 | goto skip_gpio; |
| 968 | |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 969 | irq = gpiod_to_irq(chip->gpio_detect); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 970 | if (irq <= 0) { |
| 971 | dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 972 | goto skip_gpio; |
| 973 | } |
| 974 | |
Phil Reid | d2cec82 | 2016-07-25 10:42:58 +0800 | [diff] [blame] | 975 | rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq, |
Ryosuke Saito | bb8fe8e | 2017-04-21 12:13:17 +0900 | [diff] [blame] | 976 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
Phil Reid | d2cec82 | 2016-07-25 10:42:58 +0800 | [diff] [blame] | 977 | dev_name(&client->dev), chip); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 978 | if (rc) { |
| 979 | dev_warn(&client->dev, "Failed to request irq: %d\n", rc); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 980 | goto skip_gpio; |
| 981 | } |
| 982 | |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 983 | skip_gpio: |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 984 | /* |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 985 | * Before we register, we might need to make sure we can actually talk |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 986 | * to the battery. |
| 987 | */ |
Phil Reid | 3b5dd3a | 2016-09-01 15:50:52 +0800 | [diff] [blame] | 988 | if (!(force_load || chip->gpio_detect)) { |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 989 | rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr); |
| 990 | |
| 991 | if (rc < 0) { |
| 992 | dev_err(&client->dev, "%s: Failed to get device status\n", |
| 993 | __func__); |
| 994 | goto exit_psupply; |
| 995 | } |
Olof Johansson | a22b41a | 2012-09-06 11:32:29 -0700 | [diff] [blame] | 996 | } |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 997 | |
Phil Reid | 492ff9d8 | 2016-07-25 10:42:59 +0800 | [diff] [blame] | 998 | chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc, |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 999 | &psy_cfg); |
| 1000 | if (IS_ERR(chip->power_supply)) { |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1001 | dev_err(&client->dev, |
| 1002 | "%s: Failed to register power supply\n", __func__); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1003 | rc = PTR_ERR(chip->power_supply); |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 1004 | goto exit_psupply; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | dev_info(&client->dev, |
| 1008 | "%s: battery gas gauge device registered\n", client->name); |
| 1009 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1010 | INIT_DELAYED_WORK(&chip->work, sbs_delayed_work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 1011 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1012 | chip->enable_detection = true; |
Rhyland Klein | ee177d9 | 2011-05-24 12:06:50 -0700 | [diff] [blame] | 1013 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1014 | return 0; |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 1015 | |
| 1016 | exit_psupply: |
Rhyland Klein | bb87910 | 2011-02-28 16:55:28 -0800 | [diff] [blame] | 1017 | return rc; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Bill Pemberton | 415ec69 | 2012-11-19 13:26:07 -0500 | [diff] [blame] | 1020 | static int sbs_remove(struct i2c_client *client) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1021 | { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1022 | struct sbs_info *chip = i2c_get_clientdata(client); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1023 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1024 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 1025 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1026 | return 0; |
| 1027 | } |
| 1028 | |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 1029 | #if defined CONFIG_PM_SLEEP |
| 1030 | |
| 1031 | static int sbs_suspend(struct device *dev) |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1032 | { |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 1033 | struct i2c_client *client = to_i2c_client(dev); |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1034 | struct sbs_info *chip = i2c_get_clientdata(client); |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 1035 | int ret; |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1036 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1037 | if (chip->poll_time > 0) |
| 1038 | cancel_delayed_work_sync(&chip->work); |
Rhyland Klein | 58ddafa | 2011-05-24 12:05:59 -0700 | [diff] [blame] | 1039 | |
Sebastian Reichel | 0ff9691 | 2020-05-13 20:56:01 +0200 | [diff] [blame] | 1040 | if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) { |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 1041 | /* Write to manufacturer access with sleep command. */ |
| 1042 | ret = sbs_write_word_data(client, |
| 1043 | sbs_data[REG_MANUFACTURER_DATA].addr, |
| 1044 | MANUFACTURER_ACCESS_SLEEP); |
| 1045 | if (chip->is_present && ret < 0) |
| 1046 | return ret; |
| 1047 | } |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
| 1050 | } |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 1051 | |
| 1052 | static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL); |
| 1053 | #define SBS_PM_OPS (&sbs_pm_ops) |
| 1054 | |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1055 | #else |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 1056 | #define SBS_PM_OPS NULL |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1057 | #endif |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1058 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1059 | static const struct i2c_device_id sbs_id[] = { |
Sebastian Reichel | 0ff9691 | 2020-05-13 20:56:01 +0200 | [diff] [blame] | 1060 | { "bq20z65", 0 }, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1061 | { "bq20z75", 0 }, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1062 | { "sbs-battery", 1 }, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1063 | {} |
| 1064 | }; |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1065 | MODULE_DEVICE_TABLE(i2c, sbs_id); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1066 | |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 1067 | static const struct of_device_id sbs_dt_ids[] = { |
| 1068 | { .compatible = "sbs,sbs-battery" }, |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 1069 | { |
Sebastian Reichel | 0ff9691 | 2020-05-13 20:56:01 +0200 | [diff] [blame] | 1070 | .compatible = "ti,bq20z65", |
| 1071 | .data = (void *)SBS_FLAGS_TI_BQ20ZX5, |
| 1072 | }, |
| 1073 | { |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 1074 | .compatible = "ti,bq20z75", |
Sebastian Reichel | 0ff9691 | 2020-05-13 20:56:01 +0200 | [diff] [blame] | 1075 | .data = (void *)SBS_FLAGS_TI_BQ20ZX5, |
Brian Norris | 76b16f4c | 2018-06-12 13:20:41 -0700 | [diff] [blame] | 1076 | }, |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 1077 | { } |
| 1078 | }; |
| 1079 | MODULE_DEVICE_TABLE(of, sbs_dt_ids); |
| 1080 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1081 | static struct i2c_driver sbs_battery_driver = { |
| 1082 | .probe = sbs_probe, |
Bill Pemberton | 28ea73f | 2012-11-19 13:20:40 -0500 | [diff] [blame] | 1083 | .remove = sbs_remove, |
Phil Reid | cda3b01 | 2017-05-01 16:49:58 +0800 | [diff] [blame] | 1084 | .alert = sbs_alert, |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1085 | .id_table = sbs_id, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1086 | .driver = { |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1087 | .name = "sbs-battery", |
Arnd Bergmann | 9edeaad | 2016-09-06 15:16:33 +0200 | [diff] [blame] | 1088 | .of_match_table = sbs_dt_ids, |
Lars-Peter Clausen | 9c1d1af | 2013-03-10 14:34:07 +0100 | [diff] [blame] | 1089 | .pm = SBS_PM_OPS, |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1090 | }, |
| 1091 | }; |
Axel Lin | 5ff92e7 | 2012-01-21 14:42:54 +0800 | [diff] [blame] | 1092 | module_i2c_driver(sbs_battery_driver); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1093 | |
Rhyland Klein | 3ddca062 | 2011-12-05 17:50:46 -0800 | [diff] [blame] | 1094 | MODULE_DESCRIPTION("SBS battery monitor driver"); |
Rhyland Klein | a7640bf | 2010-09-05 15:31:23 -0700 | [diff] [blame] | 1095 | MODULE_LICENSE("GPL"); |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 1096 | |
Jean-Francois Dagenais | 75d8a84 | 2019-11-01 15:06:59 -0400 | [diff] [blame] | 1097 | module_param(force_load, bool, 0444); |
Frans Klaver | f4ed950 | 2015-06-10 14:16:56 +0200 | [diff] [blame] | 1098 | MODULE_PARM_DESC(force_load, |
| 1099 | "Attempt to load the driver even if no battery is connected"); |