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