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