Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Summit Microelectronics SMB347 Battery Charger Driver |
| 4 | * |
| 5 | * Copyright (C) 2011, Intel Corporation |
| 6 | * |
| 7 | * Authors: Bruce E. Robertson <bruce.e.robertson@intel.com> |
| 8 | * Mika Westerberg <mika.westerberg@linux.intel.com> |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
David Heidelberg | fa7cc725 | 2020-03-29 18:15:45 +0200 | [diff] [blame] | 11 | #include <linux/delay.h> |
Mika Westerberg | 96facd2 | 2012-05-07 11:26:37 +0300 | [diff] [blame] | 12 | #include <linux/err.h> |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/i2c.h> |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 18 | #include <linux/power_supply.h> |
Sebastian Reichel | f385e2f | 2020-08-26 16:41:59 +0200 | [diff] [blame] | 19 | #include <linux/property.h> |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 20 | #include <linux/regmap.h> |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 21 | #include <linux/regulator/driver.h> |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 22 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 23 | #include <dt-bindings/power/summit,smb347-charger.h> |
| 24 | |
| 25 | /* Use the default compensation method */ |
| 26 | #define SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT -1 |
| 27 | |
| 28 | /* Use default factory programmed value for hard/soft temperature limit */ |
| 29 | #define SMB3XX_TEMP_USE_DEFAULT -273 |
| 30 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 31 | /* |
| 32 | * Configuration registers. These are mirrored to volatile RAM and can be |
| 33 | * written once %CMD_A_ALLOW_WRITE is set in %CMD_A register. They will be |
| 34 | * reloaded from non-volatile registers after POR. |
| 35 | */ |
| 36 | #define CFG_CHARGE_CURRENT 0x00 |
| 37 | #define CFG_CHARGE_CURRENT_FCC_MASK 0xe0 |
| 38 | #define CFG_CHARGE_CURRENT_FCC_SHIFT 5 |
| 39 | #define CFG_CHARGE_CURRENT_PCC_MASK 0x18 |
| 40 | #define CFG_CHARGE_CURRENT_PCC_SHIFT 3 |
| 41 | #define CFG_CHARGE_CURRENT_TC_MASK 0x07 |
| 42 | #define CFG_CURRENT_LIMIT 0x01 |
| 43 | #define CFG_CURRENT_LIMIT_DC_MASK 0xf0 |
| 44 | #define CFG_CURRENT_LIMIT_DC_SHIFT 4 |
| 45 | #define CFG_CURRENT_LIMIT_USB_MASK 0x0f |
| 46 | #define CFG_FLOAT_VOLTAGE 0x03 |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 47 | #define CFG_FLOAT_VOLTAGE_FLOAT_MASK 0x3f |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 48 | #define CFG_FLOAT_VOLTAGE_THRESHOLD_MASK 0xc0 |
| 49 | #define CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT 6 |
| 50 | #define CFG_STAT 0x05 |
| 51 | #define CFG_STAT_DISABLED BIT(5) |
| 52 | #define CFG_STAT_ACTIVE_HIGH BIT(7) |
| 53 | #define CFG_PIN 0x06 |
| 54 | #define CFG_PIN_EN_CTRL_MASK 0x60 |
| 55 | #define CFG_PIN_EN_CTRL_ACTIVE_HIGH 0x40 |
| 56 | #define CFG_PIN_EN_CTRL_ACTIVE_LOW 0x60 |
| 57 | #define CFG_PIN_EN_APSD_IRQ BIT(1) |
| 58 | #define CFG_PIN_EN_CHARGER_ERROR BIT(2) |
Dmitry Osipenko | efe2175 | 2021-07-31 20:38:38 +0300 | [diff] [blame] | 59 | #define CFG_PIN_EN_CTRL BIT(4) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 60 | #define CFG_THERM 0x07 |
| 61 | #define CFG_THERM_SOFT_HOT_COMPENSATION_MASK 0x03 |
| 62 | #define CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT 0 |
| 63 | #define CFG_THERM_SOFT_COLD_COMPENSATION_MASK 0x0c |
| 64 | #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2 |
| 65 | #define CFG_THERM_MONITOR_DISABLED BIT(4) |
| 66 | #define CFG_SYSOK 0x08 |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 67 | #define CFG_SYSOK_INOK_ACTIVE_HIGH BIT(0) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 68 | #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2) |
| 69 | #define CFG_OTHER 0x09 |
| 70 | #define CFG_OTHER_RID_MASK 0xc0 |
| 71 | #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0 |
| 72 | #define CFG_OTG 0x0a |
| 73 | #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30 |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 74 | #define CFG_OTG_CURRENT_LIMIT_250mA BIT(2) |
| 75 | #define CFG_OTG_CURRENT_LIMIT_750mA BIT(3) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 76 | #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4 |
| 77 | #define CFG_OTG_CC_COMPENSATION_MASK 0xc0 |
| 78 | #define CFG_OTG_CC_COMPENSATION_SHIFT 6 |
| 79 | #define CFG_TEMP_LIMIT 0x0b |
| 80 | #define CFG_TEMP_LIMIT_SOFT_HOT_MASK 0x03 |
| 81 | #define CFG_TEMP_LIMIT_SOFT_HOT_SHIFT 0 |
| 82 | #define CFG_TEMP_LIMIT_SOFT_COLD_MASK 0x0c |
| 83 | #define CFG_TEMP_LIMIT_SOFT_COLD_SHIFT 2 |
| 84 | #define CFG_TEMP_LIMIT_HARD_HOT_MASK 0x30 |
| 85 | #define CFG_TEMP_LIMIT_HARD_HOT_SHIFT 4 |
| 86 | #define CFG_TEMP_LIMIT_HARD_COLD_MASK 0xc0 |
| 87 | #define CFG_TEMP_LIMIT_HARD_COLD_SHIFT 6 |
| 88 | #define CFG_FAULT_IRQ 0x0c |
| 89 | #define CFG_FAULT_IRQ_DCIN_UV BIT(2) |
| 90 | #define CFG_STATUS_IRQ 0x0d |
| 91 | #define CFG_STATUS_IRQ_TERMINATION_OR_TAPER BIT(4) |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 92 | #define CFG_STATUS_IRQ_CHARGE_TIMEOUT BIT(7) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 93 | #define CFG_ADDRESS 0x0e |
| 94 | |
| 95 | /* Command registers */ |
| 96 | #define CMD_A 0x30 |
| 97 | #define CMD_A_CHG_ENABLED BIT(1) |
| 98 | #define CMD_A_SUSPEND_ENABLED BIT(2) |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 99 | #define CMD_A_OTG_ENABLED BIT(4) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 100 | #define CMD_A_ALLOW_WRITE BIT(7) |
| 101 | #define CMD_B 0x31 |
| 102 | #define CMD_C 0x33 |
| 103 | |
| 104 | /* Interrupt Status registers */ |
| 105 | #define IRQSTAT_A 0x35 |
| 106 | #define IRQSTAT_C 0x37 |
| 107 | #define IRQSTAT_C_TERMINATION_STAT BIT(0) |
| 108 | #define IRQSTAT_C_TERMINATION_IRQ BIT(1) |
| 109 | #define IRQSTAT_C_TAPER_IRQ BIT(3) |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 110 | #define IRQSTAT_D 0x38 |
| 111 | #define IRQSTAT_D_CHARGE_TIMEOUT_STAT BIT(2) |
| 112 | #define IRQSTAT_D_CHARGE_TIMEOUT_IRQ BIT(3) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 113 | #define IRQSTAT_E 0x39 |
| 114 | #define IRQSTAT_E_USBIN_UV_STAT BIT(0) |
| 115 | #define IRQSTAT_E_USBIN_UV_IRQ BIT(1) |
| 116 | #define IRQSTAT_E_DCIN_UV_STAT BIT(4) |
| 117 | #define IRQSTAT_E_DCIN_UV_IRQ BIT(5) |
| 118 | #define IRQSTAT_F 0x3a |
| 119 | |
| 120 | /* Status registers */ |
| 121 | #define STAT_A 0x3b |
| 122 | #define STAT_A_FLOAT_VOLTAGE_MASK 0x3f |
| 123 | #define STAT_B 0x3c |
| 124 | #define STAT_C 0x3d |
| 125 | #define STAT_C_CHG_ENABLED BIT(0) |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 126 | #define STAT_C_HOLDOFF_STAT BIT(3) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 127 | #define STAT_C_CHG_MASK 0x06 |
| 128 | #define STAT_C_CHG_SHIFT 1 |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 129 | #define STAT_C_CHG_TERM BIT(5) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 130 | #define STAT_C_CHARGER_ERROR BIT(6) |
| 131 | #define STAT_E 0x3f |
| 132 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 133 | #define SMB347_MAX_REGISTER 0x3f |
| 134 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 135 | /** |
| 136 | * struct smb347_charger - smb347 charger instance |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 137 | * @dev: pointer to device |
| 138 | * @regmap: pointer to driver regmap |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 139 | * @mains: power_supply instance for AC/DC power |
| 140 | * @usb: power_supply instance for USB power |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 141 | * @usb_rdev: USB VBUS regulator device |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 142 | * @id: SMB charger ID |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 143 | * @mains_online: is AC/DC input connected |
| 144 | * @usb_online: is USB input connected |
Dmitry Osipenko | 6996312 | 2021-01-22 22:17:34 +0300 | [diff] [blame] | 145 | * @irq_unsupported: is interrupt unsupported by SMB hardware |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 146 | * @usb_vbus_enabled: is USB VBUS powered by SMB charger |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 147 | * @max_charge_current: maximum current (in uA) the battery can be charged |
| 148 | * @max_charge_voltage: maximum voltage (in uV) the battery can be charged |
| 149 | * @pre_charge_current: current (in uA) to use in pre-charging phase |
| 150 | * @termination_current: current (in uA) used to determine when the |
| 151 | * charging cycle terminates |
| 152 | * @pre_to_fast_voltage: voltage (in uV) treshold used for transitioning to |
| 153 | * pre-charge to fast charge mode |
| 154 | * @mains_current_limit: maximum input current drawn from AC/DC input (in uA) |
| 155 | * @usb_hc_current_limit: maximum input high current (in uA) drawn from USB |
| 156 | * input |
| 157 | * @chip_temp_threshold: die temperature where device starts limiting charge |
| 158 | * current [%100 - %130] (in degree C) |
| 159 | * @soft_cold_temp_limit: soft cold temperature limit [%0 - %15] (in degree C), |
| 160 | * granularity is 5 deg C. |
| 161 | * @soft_hot_temp_limit: soft hot temperature limit [%40 - %55] (in degree C), |
| 162 | * granularity is 5 deg C. |
| 163 | * @hard_cold_temp_limit: hard cold temperature limit [%-5 - %10] (in degree C), |
| 164 | * granularity is 5 deg C. |
| 165 | * @hard_hot_temp_limit: hard hot temperature limit [%50 - %65] (in degree C), |
| 166 | * granularity is 5 deg C. |
| 167 | * @suspend_on_hard_temp_limit: suspend charging when hard limit is hit |
| 168 | * @soft_temp_limit_compensation: compensation method when soft temperature |
| 169 | * limit is hit |
| 170 | * @charge_current_compensation: current (in uA) for charging compensation |
| 171 | * current when temperature hits soft limits |
| 172 | * @use_mains: AC/DC input can be used |
| 173 | * @use_usb: USB input can be used |
| 174 | * @use_usb_otg: USB OTG output can be used (not implemented yet) |
| 175 | * @enable_control: how charging enable/disable is controlled |
| 176 | * (driver/pin controls) |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 177 | * @inok_polarity: polarity of INOK signal which denotes presence of external |
| 178 | * power supply |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 179 | * |
| 180 | * @use_main, @use_usb, and @use_usb_otg are means to enable/disable |
| 181 | * hardware support for these. This is useful when we want to have for |
| 182 | * example OTG charging controlled via OTG transceiver driver and not by |
| 183 | * the SMB347 hardware. |
| 184 | * |
| 185 | * Hard and soft temperature limit values are given as described in the |
| 186 | * device data sheet and assuming NTC beta value is %3750. Even if this is |
| 187 | * not the case, these values should be used. They can be mapped to the |
| 188 | * corresponding NTC beta values with the help of table %2 in the data |
| 189 | * sheet. So for example if NTC beta is %3375 and we want to program hard |
| 190 | * hot limit to be %53 deg C, @hard_hot_temp_limit should be set to %50. |
| 191 | * |
| 192 | * If zero value is given in any of the current and voltage values, the |
| 193 | * factory programmed default will be used. For soft/hard temperature |
| 194 | * values, pass in %SMB3XX_TEMP_USE_DEFAULT instead. |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 195 | */ |
| 196 | struct smb347_charger { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 197 | struct device *dev; |
| 198 | struct regmap *regmap; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 199 | struct power_supply *mains; |
| 200 | struct power_supply *usb; |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 201 | struct regulator_dev *usb_rdev; |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 202 | unsigned int id; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 203 | bool mains_online; |
| 204 | bool usb_online; |
Dmitry Osipenko | 6996312 | 2021-01-22 22:17:34 +0300 | [diff] [blame] | 205 | bool irq_unsupported; |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 206 | bool usb_vbus_enabled; |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 207 | |
| 208 | unsigned int max_charge_current; |
| 209 | unsigned int max_charge_voltage; |
| 210 | unsigned int pre_charge_current; |
| 211 | unsigned int termination_current; |
| 212 | unsigned int pre_to_fast_voltage; |
| 213 | unsigned int mains_current_limit; |
| 214 | unsigned int usb_hc_current_limit; |
| 215 | unsigned int chip_temp_threshold; |
| 216 | int soft_cold_temp_limit; |
| 217 | int soft_hot_temp_limit; |
| 218 | int hard_cold_temp_limit; |
| 219 | int hard_hot_temp_limit; |
| 220 | bool suspend_on_hard_temp_limit; |
| 221 | unsigned int soft_temp_limit_compensation; |
| 222 | unsigned int charge_current_compensation; |
| 223 | bool use_mains; |
| 224 | bool use_usb; |
| 225 | bool use_usb_otg; |
| 226 | unsigned int enable_control; |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 227 | unsigned int inok_polarity; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 228 | }; |
| 229 | |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 230 | enum smb_charger_chipid { |
| 231 | SMB345, |
| 232 | SMB347, |
| 233 | SMB358, |
| 234 | NUM_CHIP_TYPES, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 237 | /* Fast charge current in uA */ |
| 238 | static const unsigned int fcc_tbl[NUM_CHIP_TYPES][8] = { |
| 239 | [SMB345] = { 200000, 450000, 600000, 900000, |
| 240 | 1300000, 1500000, 1800000, 2000000 }, |
| 241 | [SMB347] = { 700000, 900000, 1200000, 1500000, |
| 242 | 1800000, 2000000, 2200000, 2500000 }, |
| 243 | [SMB358] = { 200000, 450000, 600000, 900000, |
| 244 | 1300000, 1500000, 1800000, 2000000 }, |
| 245 | }; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 246 | /* Pre-charge current in uA */ |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 247 | static const unsigned int pcc_tbl[NUM_CHIP_TYPES][4] = { |
| 248 | [SMB345] = { 150000, 250000, 350000, 450000 }, |
| 249 | [SMB347] = { 100000, 150000, 200000, 250000 }, |
| 250 | [SMB358] = { 150000, 250000, 350000, 450000 }, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | /* Termination current in uA */ |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 254 | static const unsigned int tc_tbl[NUM_CHIP_TYPES][8] = { |
| 255 | [SMB345] = { 30000, 40000, 60000, 80000, |
| 256 | 100000, 125000, 150000, 200000 }, |
| 257 | [SMB347] = { 37500, 50000, 100000, 150000, |
| 258 | 200000, 250000, 500000, 600000 }, |
| 259 | [SMB358] = { 30000, 40000, 60000, 80000, |
| 260 | 100000, 125000, 150000, 200000 }, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | /* Input current limit in uA */ |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 264 | static const unsigned int icl_tbl[NUM_CHIP_TYPES][10] = { |
| 265 | [SMB345] = { 300000, 500000, 700000, 1000000, 1500000, |
| 266 | 1800000, 2000000, 2000000, 2000000, 2000000 }, |
| 267 | [SMB347] = { 300000, 500000, 700000, 900000, 1200000, |
| 268 | 1500000, 1800000, 2000000, 2200000, 2500000 }, |
| 269 | [SMB358] = { 300000, 500000, 700000, 1000000, 1500000, |
| 270 | 1800000, 2000000, 2000000, 2000000, 2000000 }, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | /* Charge current compensation in uA */ |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 274 | static const unsigned int ccc_tbl[NUM_CHIP_TYPES][4] = { |
| 275 | [SMB345] = { 200000, 450000, 600000, 900000 }, |
| 276 | [SMB347] = { 250000, 700000, 900000, 1200000 }, |
| 277 | [SMB358] = { 200000, 450000, 600000, 900000 }, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
Ramakrishna Pallala | 19dd6bc | 2012-06-19 16:45:05 -0700 | [diff] [blame] | 280 | /* Convert register value to current using lookup table */ |
| 281 | static int hw_to_current(const unsigned int *tbl, size_t size, unsigned int val) |
| 282 | { |
| 283 | if (val >= size) |
| 284 | return -EINVAL; |
| 285 | return tbl[val]; |
| 286 | } |
| 287 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 288 | /* Convert current to register value using lookup table */ |
| 289 | static int current_to_hw(const unsigned int *tbl, size_t size, unsigned int val) |
| 290 | { |
| 291 | size_t i; |
| 292 | |
| 293 | for (i = 0; i < size; i++) |
| 294 | if (val < tbl[i]) |
| 295 | break; |
| 296 | return i > 0 ? i - 1 : -EINVAL; |
| 297 | } |
| 298 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 299 | /** |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 300 | * smb347_update_ps_status - refreshes the power source status |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 301 | * @smb: pointer to smb347 charger instance |
| 302 | * |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 303 | * Function checks whether any power source is connected to the charger and |
| 304 | * updates internal state accordingly. If there is a change to previous state |
| 305 | * function returns %1, otherwise %0 and negative errno in case of errror. |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 306 | */ |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 307 | static int smb347_update_ps_status(struct smb347_charger *smb) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 308 | { |
| 309 | bool usb = false; |
| 310 | bool dc = false; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 311 | unsigned int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 312 | int ret; |
| 313 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 314 | ret = regmap_read(smb->regmap, IRQSTAT_E, &val); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 315 | if (ret < 0) |
| 316 | return ret; |
| 317 | |
| 318 | /* |
| 319 | * Dc and usb are set depending on whether they are enabled in |
| 320 | * platform data _and_ whether corresponding undervoltage is set. |
| 321 | */ |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 322 | if (smb->use_mains) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 323 | dc = !(val & IRQSTAT_E_DCIN_UV_STAT); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 324 | if (smb->use_usb) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 325 | usb = !(val & IRQSTAT_E_USBIN_UV_STAT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 326 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 327 | ret = smb->mains_online != dc || smb->usb_online != usb; |
| 328 | smb->mains_online = dc; |
| 329 | smb->usb_online = usb; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | /* |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 335 | * smb347_is_ps_online - returns whether input power source is connected |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 336 | * @smb: pointer to smb347 charger instance |
| 337 | * |
| 338 | * Returns %true if input power source is connected. Note that this is |
| 339 | * dependent on what platform has configured for usable power sources. For |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 340 | * example if USB is disabled, this will return %false even if the USB cable |
| 341 | * is connected. |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 342 | */ |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 343 | static bool smb347_is_ps_online(struct smb347_charger *smb) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 344 | { |
Dmitry Osipenko | 99298de | 2020-08-14 00:34:07 +0300 | [diff] [blame] | 345 | return smb->usb_online || smb->mains_online; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * smb347_charging_status - returns status of charging |
| 350 | * @smb: pointer to smb347 charger instance |
| 351 | * |
| 352 | * Function returns charging status. %0 means no charging is in progress, |
| 353 | * %1 means pre-charging, %2 fast-charging and %3 taper-charging. |
| 354 | */ |
| 355 | static int smb347_charging_status(struct smb347_charger *smb) |
| 356 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 357 | unsigned int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 358 | int ret; |
| 359 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 360 | if (!smb347_is_ps_online(smb)) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 361 | return 0; |
| 362 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 363 | ret = regmap_read(smb->regmap, STAT_C, &val); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 364 | if (ret < 0) |
| 365 | return 0; |
| 366 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 367 | return (val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static int smb347_charging_set(struct smb347_charger *smb, bool enable) |
| 371 | { |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 372 | if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 373 | dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 377 | if (enable && smb->usb_vbus_enabled) { |
| 378 | dev_dbg(smb->dev, "charging not enabled because USB is in host mode\n"); |
| 379 | return 0; |
| 380 | } |
| 381 | |
Dmitry Osipenko | 17e7bc5 | 2021-07-31 20:38:37 +0300 | [diff] [blame] | 382 | return regmap_update_bits(smb->regmap, CMD_A, CMD_A_CHG_ENABLED, |
| 383 | enable ? CMD_A_CHG_ENABLED : 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static inline int smb347_charging_enable(struct smb347_charger *smb) |
| 387 | { |
| 388 | return smb347_charging_set(smb, true); |
| 389 | } |
| 390 | |
| 391 | static inline int smb347_charging_disable(struct smb347_charger *smb) |
| 392 | { |
| 393 | return smb347_charging_set(smb, false); |
| 394 | } |
| 395 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 396 | static int smb347_start_stop_charging(struct smb347_charger *smb) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 397 | { |
| 398 | int ret; |
| 399 | |
| 400 | /* |
| 401 | * Depending on whether valid power source is connected or not, we |
| 402 | * disable or enable the charging. We do it manually because it |
| 403 | * depends on how the platform has configured the valid inputs. |
| 404 | */ |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 405 | if (smb347_is_ps_online(smb)) { |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 406 | ret = smb347_charging_enable(smb); |
| 407 | if (ret < 0) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 408 | dev_err(smb->dev, "failed to enable charging\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 409 | } else { |
| 410 | ret = smb347_charging_disable(smb); |
| 411 | if (ret < 0) |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 412 | dev_err(smb->dev, "failed to disable charging\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static int smb347_set_charge_current(struct smb347_charger *smb) |
| 419 | { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 420 | unsigned int id = smb->id; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 421 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 422 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 423 | if (smb->max_charge_current) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 424 | ret = current_to_hw(fcc_tbl[id], ARRAY_SIZE(fcc_tbl[id]), |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 425 | smb->max_charge_current); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 426 | if (ret < 0) |
| 427 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 428 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 429 | ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, |
| 430 | CFG_CHARGE_CURRENT_FCC_MASK, |
| 431 | ret << CFG_CHARGE_CURRENT_FCC_SHIFT); |
| 432 | if (ret < 0) |
| 433 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 436 | if (smb->pre_charge_current) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 437 | ret = current_to_hw(pcc_tbl[id], ARRAY_SIZE(pcc_tbl[id]), |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 438 | smb->pre_charge_current); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 439 | if (ret < 0) |
| 440 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 441 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 442 | ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, |
| 443 | CFG_CHARGE_CURRENT_PCC_MASK, |
| 444 | ret << CFG_CHARGE_CURRENT_PCC_SHIFT); |
| 445 | if (ret < 0) |
| 446 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 449 | if (smb->termination_current) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 450 | ret = current_to_hw(tc_tbl[id], ARRAY_SIZE(tc_tbl[id]), |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 451 | smb->termination_current); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 452 | if (ret < 0) |
| 453 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 454 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 455 | ret = regmap_update_bits(smb->regmap, CFG_CHARGE_CURRENT, |
| 456 | CFG_CHARGE_CURRENT_TC_MASK, ret); |
| 457 | if (ret < 0) |
| 458 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 461 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static int smb347_set_current_limits(struct smb347_charger *smb) |
| 465 | { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 466 | unsigned int id = smb->id; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 467 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 468 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 469 | if (smb->mains_current_limit) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 470 | ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]), |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 471 | smb->mains_current_limit); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 472 | if (ret < 0) |
| 473 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 474 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 475 | ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, |
| 476 | CFG_CURRENT_LIMIT_DC_MASK, |
| 477 | ret << CFG_CURRENT_LIMIT_DC_SHIFT); |
| 478 | if (ret < 0) |
| 479 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 482 | if (smb->usb_hc_current_limit) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 483 | ret = current_to_hw(icl_tbl[id], ARRAY_SIZE(icl_tbl[id]), |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 484 | smb->usb_hc_current_limit); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 485 | if (ret < 0) |
| 486 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 487 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 488 | ret = regmap_update_bits(smb->regmap, CFG_CURRENT_LIMIT, |
| 489 | CFG_CURRENT_LIMIT_USB_MASK, ret); |
| 490 | if (ret < 0) |
| 491 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 494 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int smb347_set_voltage_limits(struct smb347_charger *smb) |
| 498 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 499 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 500 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 501 | if (smb->pre_to_fast_voltage) { |
| 502 | ret = smb->pre_to_fast_voltage; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 503 | |
| 504 | /* uV */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 505 | ret = clamp_val(ret, 2400000, 3000000) - 2400000; |
| 506 | ret /= 200000; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 507 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 508 | ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, |
| 509 | CFG_FLOAT_VOLTAGE_THRESHOLD_MASK, |
| 510 | ret << CFG_FLOAT_VOLTAGE_THRESHOLD_SHIFT); |
| 511 | if (ret < 0) |
| 512 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 515 | if (smb->max_charge_voltage) { |
| 516 | ret = smb->max_charge_voltage; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 517 | |
| 518 | /* uV */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 519 | ret = clamp_val(ret, 3500000, 4500000) - 3500000; |
| 520 | ret /= 20000; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 521 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 522 | ret = regmap_update_bits(smb->regmap, CFG_FLOAT_VOLTAGE, |
| 523 | CFG_FLOAT_VOLTAGE_FLOAT_MASK, ret); |
| 524 | if (ret < 0) |
| 525 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 528 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static int smb347_set_temp_limits(struct smb347_charger *smb) |
| 532 | { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 533 | unsigned int id = smb->id; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 534 | bool enable_therm_monitor = false; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 535 | int ret = 0; |
| 536 | int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 537 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 538 | if (smb->chip_temp_threshold) { |
| 539 | val = smb->chip_temp_threshold; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 540 | |
| 541 | /* degree C */ |
| 542 | val = clamp_val(val, 100, 130) - 100; |
| 543 | val /= 10; |
| 544 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 545 | ret = regmap_update_bits(smb->regmap, CFG_OTG, |
| 546 | CFG_OTG_TEMP_THRESHOLD_MASK, |
| 547 | val << CFG_OTG_TEMP_THRESHOLD_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 548 | if (ret < 0) |
| 549 | return ret; |
| 550 | } |
| 551 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 552 | if (smb->soft_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
| 553 | val = smb->soft_cold_temp_limit; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 554 | |
| 555 | val = clamp_val(val, 0, 15); |
| 556 | val /= 5; |
| 557 | /* this goes from higher to lower so invert the value */ |
| 558 | val = ~val & 0x3; |
| 559 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 560 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 561 | CFG_TEMP_LIMIT_SOFT_COLD_MASK, |
| 562 | val << CFG_TEMP_LIMIT_SOFT_COLD_SHIFT); |
| 563 | if (ret < 0) |
| 564 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 565 | |
| 566 | enable_therm_monitor = true; |
| 567 | } |
| 568 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 569 | if (smb->soft_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
| 570 | val = smb->soft_hot_temp_limit; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 571 | |
| 572 | val = clamp_val(val, 40, 55) - 40; |
| 573 | val /= 5; |
| 574 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 575 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 576 | CFG_TEMP_LIMIT_SOFT_HOT_MASK, |
| 577 | val << CFG_TEMP_LIMIT_SOFT_HOT_SHIFT); |
| 578 | if (ret < 0) |
| 579 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 580 | |
| 581 | enable_therm_monitor = true; |
| 582 | } |
| 583 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 584 | if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
| 585 | val = smb->hard_cold_temp_limit; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 586 | |
| 587 | val = clamp_val(val, -5, 10) + 5; |
| 588 | val /= 5; |
| 589 | /* this goes from higher to lower so invert the value */ |
| 590 | val = ~val & 0x3; |
| 591 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 592 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 593 | CFG_TEMP_LIMIT_HARD_COLD_MASK, |
| 594 | val << CFG_TEMP_LIMIT_HARD_COLD_SHIFT); |
| 595 | if (ret < 0) |
| 596 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 597 | |
| 598 | enable_therm_monitor = true; |
| 599 | } |
| 600 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 601 | if (smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) { |
| 602 | val = smb->hard_hot_temp_limit; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 603 | |
| 604 | val = clamp_val(val, 50, 65) - 50; |
| 605 | val /= 5; |
| 606 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 607 | ret = regmap_update_bits(smb->regmap, CFG_TEMP_LIMIT, |
| 608 | CFG_TEMP_LIMIT_HARD_HOT_MASK, |
| 609 | val << CFG_TEMP_LIMIT_HARD_HOT_SHIFT); |
| 610 | if (ret < 0) |
| 611 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 612 | |
| 613 | enable_therm_monitor = true; |
| 614 | } |
| 615 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 616 | /* |
| 617 | * If any of the temperature limits are set, we also enable the |
| 618 | * thermistor monitoring. |
| 619 | * |
| 620 | * When soft limits are hit, the device will start to compensate |
| 621 | * current and/or voltage depending on the configuration. |
| 622 | * |
| 623 | * When hard limit is hit, the device will suspend charging |
| 624 | * depending on the configuration. |
| 625 | */ |
| 626 | if (enable_therm_monitor) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 627 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
| 628 | CFG_THERM_MONITOR_DISABLED, 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 629 | if (ret < 0) |
| 630 | return ret; |
| 631 | } |
| 632 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 633 | if (smb->suspend_on_hard_temp_limit) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 634 | ret = regmap_update_bits(smb->regmap, CFG_SYSOK, |
| 635 | CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED, 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 636 | if (ret < 0) |
| 637 | return ret; |
| 638 | } |
| 639 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 640 | if (smb->soft_temp_limit_compensation != |
| 641 | SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT) { |
| 642 | val = smb->soft_temp_limit_compensation & 0x3; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 643 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 644 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
| 645 | CFG_THERM_SOFT_HOT_COMPENSATION_MASK, |
| 646 | val << CFG_THERM_SOFT_HOT_COMPENSATION_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 647 | if (ret < 0) |
| 648 | return ret; |
| 649 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 650 | ret = regmap_update_bits(smb->regmap, CFG_THERM, |
| 651 | CFG_THERM_SOFT_COLD_COMPENSATION_MASK, |
| 652 | val << CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 653 | if (ret < 0) |
| 654 | return ret; |
| 655 | } |
| 656 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 657 | if (smb->charge_current_compensation) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 658 | val = current_to_hw(ccc_tbl[id], ARRAY_SIZE(ccc_tbl[id]), |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 659 | smb->charge_current_compensation); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 660 | if (val < 0) |
| 661 | return val; |
| 662 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 663 | ret = regmap_update_bits(smb->regmap, CFG_OTG, |
| 664 | CFG_OTG_CC_COMPENSATION_MASK, |
| 665 | (val & 0x3) << CFG_OTG_CC_COMPENSATION_SHIFT); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 666 | if (ret < 0) |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * smb347_set_writable - enables/disables writing to non-volatile registers |
| 675 | * @smb: pointer to smb347 charger instance |
| 676 | * |
| 677 | * You can enable/disable writing to the non-volatile configuration |
| 678 | * registers by calling this function. |
| 679 | * |
| 680 | * Returns %0 on success and negative errno in case of failure. |
| 681 | */ |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 682 | static int smb347_set_writable(struct smb347_charger *smb, bool writable, |
| 683 | bool irq_toggle) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 684 | { |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 685 | struct i2c_client *client = to_i2c_client(smb->dev); |
| 686 | int ret; |
| 687 | |
| 688 | if (writable && irq_toggle && !smb->irq_unsupported) |
| 689 | disable_irq(client->irq); |
| 690 | |
| 691 | ret = regmap_update_bits(smb->regmap, CMD_A, CMD_A_ALLOW_WRITE, |
| 692 | writable ? CMD_A_ALLOW_WRITE : 0); |
| 693 | |
| 694 | if ((!writable || ret) && irq_toggle && !smb->irq_unsupported) |
| 695 | enable_irq(client->irq); |
| 696 | |
| 697 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static int smb347_hw_init(struct smb347_charger *smb) |
| 701 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 702 | unsigned int val; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 703 | int ret; |
| 704 | |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 705 | ret = smb347_set_writable(smb, true, false); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 706 | if (ret < 0) |
| 707 | return ret; |
| 708 | |
| 709 | /* |
| 710 | * Program the platform specific configuration values to the device |
| 711 | * first. |
| 712 | */ |
| 713 | ret = smb347_set_charge_current(smb); |
| 714 | if (ret < 0) |
| 715 | goto fail; |
| 716 | |
| 717 | ret = smb347_set_current_limits(smb); |
| 718 | if (ret < 0) |
| 719 | goto fail; |
| 720 | |
| 721 | ret = smb347_set_voltage_limits(smb); |
| 722 | if (ret < 0) |
| 723 | goto fail; |
| 724 | |
| 725 | ret = smb347_set_temp_limits(smb); |
| 726 | if (ret < 0) |
| 727 | goto fail; |
| 728 | |
| 729 | /* If USB charging is disabled we put the USB in suspend mode */ |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 730 | if (!smb->use_usb) { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 731 | ret = regmap_update_bits(smb->regmap, CMD_A, |
| 732 | CMD_A_SUSPEND_ENABLED, |
| 733 | CMD_A_SUSPEND_ENABLED); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 734 | if (ret < 0) |
| 735 | goto fail; |
| 736 | } |
| 737 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 738 | /* |
| 739 | * If configured by platform data, we enable hardware Auto-OTG |
| 740 | * support for driving VBUS. Otherwise we disable it. |
| 741 | */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 742 | ret = regmap_update_bits(smb->regmap, CFG_OTHER, CFG_OTHER_RID_MASK, |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 743 | smb->use_usb_otg ? CFG_OTHER_RID_ENABLED_AUTO_OTG : 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 744 | if (ret < 0) |
| 745 | goto fail; |
| 746 | |
Dmitry Osipenko | efe2175 | 2021-07-31 20:38:38 +0300 | [diff] [blame] | 747 | /* Activate pin control, making it writable. */ |
| 748 | switch (smb->enable_control) { |
| 749 | case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW: |
| 750 | case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH: |
| 751 | ret = regmap_set_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL); |
| 752 | if (ret < 0) |
| 753 | goto fail; |
| 754 | } |
| 755 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 756 | /* |
| 757 | * Make the charging functionality controllable by a write to the |
| 758 | * command register unless pin control is specified in the platform |
| 759 | * data. |
| 760 | */ |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 761 | switch (smb->enable_control) { |
| 762 | case SMB3XX_CHG_ENABLE_PIN_ACTIVE_LOW: |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 763 | val = CFG_PIN_EN_CTRL_ACTIVE_LOW; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 764 | break; |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 765 | case SMB3XX_CHG_ENABLE_PIN_ACTIVE_HIGH: |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 766 | val = CFG_PIN_EN_CTRL_ACTIVE_HIGH; |
| 767 | break; |
| 768 | default: |
| 769 | val = 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 770 | break; |
| 771 | } |
| 772 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 773 | ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CTRL_MASK, |
| 774 | val); |
| 775 | if (ret < 0) |
| 776 | goto fail; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 777 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 778 | /* Disable Automatic Power Source Detection (APSD) interrupt. */ |
| 779 | ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_APSD_IRQ, 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 780 | if (ret < 0) |
| 781 | goto fail; |
| 782 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 783 | ret = smb347_update_ps_status(smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 784 | if (ret < 0) |
| 785 | goto fail; |
| 786 | |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 787 | ret = smb347_start_stop_charging(smb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 788 | |
| 789 | fail: |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 790 | smb347_set_writable(smb, false, false); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | static irqreturn_t smb347_interrupt(int irq, void *data) |
| 795 | { |
| 796 | struct smb347_charger *smb = data; |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 797 | unsigned int stat_c, irqstat_c, irqstat_d, irqstat_e; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 798 | bool handled = false; |
| 799 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 800 | |
David Heidelberg | fa7cc725 | 2020-03-29 18:15:45 +0200 | [diff] [blame] | 801 | /* SMB347 it needs at least 20ms for setting IRQSTAT_E_*IN_UV_IRQ */ |
| 802 | usleep_range(25000, 35000); |
| 803 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 804 | ret = regmap_read(smb->regmap, STAT_C, &stat_c); |
| 805 | if (ret < 0) { |
| 806 | dev_warn(smb->dev, "reading STAT_C failed\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 807 | return IRQ_NONE; |
| 808 | } |
| 809 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 810 | ret = regmap_read(smb->regmap, IRQSTAT_C, &irqstat_c); |
| 811 | if (ret < 0) { |
| 812 | dev_warn(smb->dev, "reading IRQSTAT_C failed\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 813 | return IRQ_NONE; |
| 814 | } |
| 815 | |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 816 | ret = regmap_read(smb->regmap, IRQSTAT_D, &irqstat_d); |
| 817 | if (ret < 0) { |
| 818 | dev_warn(smb->dev, "reading IRQSTAT_D failed\n"); |
| 819 | return IRQ_NONE; |
| 820 | } |
| 821 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 822 | ret = regmap_read(smb->regmap, IRQSTAT_E, &irqstat_e); |
| 823 | if (ret < 0) { |
| 824 | dev_warn(smb->dev, "reading IRQSTAT_E failed\n"); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 825 | return IRQ_NONE; |
| 826 | } |
| 827 | |
| 828 | /* |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 829 | * If we get charger error we report the error back to user. |
| 830 | * If the error is recovered charging will resume again. |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 831 | */ |
| 832 | if (stat_c & STAT_C_CHARGER_ERROR) { |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 833 | dev_err(smb->dev, "charging stopped due to charger error\n"); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 834 | if (smb->use_mains) |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 835 | power_supply_changed(smb->mains); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 836 | if (smb->use_usb) |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 837 | power_supply_changed(smb->usb); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 838 | handled = true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* |
| 842 | * If we reached the termination current the battery is charged and |
| 843 | * we can update the status now. Charging is automatically |
| 844 | * disabled by the hardware. |
| 845 | */ |
| 846 | if (irqstat_c & (IRQSTAT_C_TERMINATION_IRQ | IRQSTAT_C_TAPER_IRQ)) { |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 847 | if (irqstat_c & IRQSTAT_C_TERMINATION_STAT) { |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 848 | if (smb->use_mains) |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 849 | power_supply_changed(smb->mains); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 850 | if (smb->use_usb) |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 851 | power_supply_changed(smb->usb); |
| 852 | } |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 853 | dev_dbg(smb->dev, "going to HW maintenance mode\n"); |
| 854 | handled = true; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * If we got a charger timeout INT that means the charge |
| 859 | * full is not detected with in charge timeout value. |
| 860 | */ |
| 861 | if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_IRQ) { |
| 862 | dev_dbg(smb->dev, "total Charge Timeout INT received\n"); |
| 863 | |
| 864 | if (irqstat_d & IRQSTAT_D_CHARGE_TIMEOUT_STAT) |
| 865 | dev_warn(smb->dev, "charging stopped due to timeout\n"); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 866 | if (smb->use_mains) |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 867 | power_supply_changed(smb->mains); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 868 | if (smb->use_usb) |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 869 | power_supply_changed(smb->usb); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 870 | handled = true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /* |
| 874 | * If we got an under voltage interrupt it means that AC/USB input |
| 875 | * was connected or disconnected. |
| 876 | */ |
| 877 | if (irqstat_e & (IRQSTAT_E_USBIN_UV_IRQ | IRQSTAT_E_DCIN_UV_IRQ)) { |
Mika Westerberg | 055d7f0 | 2012-04-16 11:48:39 +0300 | [diff] [blame] | 878 | if (smb347_update_ps_status(smb) > 0) { |
| 879 | smb347_start_stop_charging(smb); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 880 | if (smb->use_mains) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 881 | power_supply_changed(smb->mains); |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 882 | if (smb->use_usb) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 883 | power_supply_changed(smb->usb); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 884 | } |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 885 | handled = true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 888 | return handled ? IRQ_HANDLED : IRQ_NONE; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static int smb347_irq_set(struct smb347_charger *smb, bool enable) |
| 892 | { |
| 893 | int ret; |
| 894 | |
Dmitry Osipenko | 6996312 | 2021-01-22 22:17:34 +0300 | [diff] [blame] | 895 | if (smb->irq_unsupported) |
| 896 | return 0; |
| 897 | |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 898 | ret = smb347_set_writable(smb, true, true); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 899 | if (ret < 0) |
| 900 | return ret; |
| 901 | |
| 902 | /* |
| 903 | * Enable/disable interrupts for: |
| 904 | * - under voltage |
| 905 | * - termination current reached |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 906 | * - charger timeout |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 907 | * - charger error |
| 908 | */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 909 | ret = regmap_update_bits(smb->regmap, CFG_FAULT_IRQ, 0xff, |
| 910 | enable ? CFG_FAULT_IRQ_DCIN_UV : 0); |
| 911 | if (ret < 0) |
| 912 | goto fail; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 913 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 914 | ret = regmap_update_bits(smb->regmap, CFG_STATUS_IRQ, 0xff, |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 915 | enable ? (CFG_STATUS_IRQ_TERMINATION_OR_TAPER | |
| 916 | CFG_STATUS_IRQ_CHARGE_TIMEOUT) : 0); |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 917 | if (ret < 0) |
| 918 | goto fail; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 919 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 920 | ret = regmap_update_bits(smb->regmap, CFG_PIN, CFG_PIN_EN_CHARGER_ERROR, |
| 921 | enable ? CFG_PIN_EN_CHARGER_ERROR : 0); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 922 | fail: |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 923 | smb347_set_writable(smb, false, true); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | static inline int smb347_irq_enable(struct smb347_charger *smb) |
| 928 | { |
| 929 | return smb347_irq_set(smb, true); |
| 930 | } |
| 931 | |
| 932 | static inline int smb347_irq_disable(struct smb347_charger *smb) |
| 933 | { |
| 934 | return smb347_irq_set(smb, false); |
| 935 | } |
| 936 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 937 | static int smb347_irq_init(struct smb347_charger *smb, |
| 938 | struct i2c_client *client) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 939 | { |
David Heidelberg | 2d52f71 | 2020-08-14 00:34:03 +0300 | [diff] [blame] | 940 | int ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 941 | |
Dmitry Osipenko | d33b3f7 | 2021-03-02 13:10:26 +0300 | [diff] [blame] | 942 | smb->irq_unsupported = true; |
| 943 | |
| 944 | /* |
| 945 | * Interrupt pin is optional. If it is connected, we setup the |
| 946 | * interrupt support here. |
| 947 | */ |
| 948 | if (!client->irq) |
| 949 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 950 | |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 951 | ret = smb347_set_writable(smb, true, false); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 952 | if (ret < 0) |
David Heidelberg | 2d52f71 | 2020-08-14 00:34:03 +0300 | [diff] [blame] | 953 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 954 | |
| 955 | /* |
| 956 | * Configure the STAT output to be suitable for interrupts: disable |
| 957 | * all other output (except interrupts) and make it active low. |
| 958 | */ |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 959 | ret = regmap_update_bits(smb->regmap, CFG_STAT, |
| 960 | CFG_STAT_ACTIVE_HIGH | CFG_STAT_DISABLED, |
| 961 | CFG_STAT_DISABLED); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 962 | |
Dmitry Osipenko | 4ac59d8 | 2021-07-31 20:38:36 +0300 | [diff] [blame] | 963 | smb347_set_writable(smb, false, false); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 964 | |
Dmitry Osipenko | d33b3f7 | 2021-03-02 13:10:26 +0300 | [diff] [blame] | 965 | if (ret < 0) { |
| 966 | dev_warn(smb->dev, "failed to initialize IRQ: %d\n", ret); |
| 967 | dev_warn(smb->dev, "disabling IRQ support\n"); |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | ret = devm_request_threaded_irq(smb->dev, client->irq, NULL, |
| 972 | smb347_interrupt, IRQF_ONESHOT, |
| 973 | client->name, smb); |
| 974 | if (ret) |
| 975 | return ret; |
| 976 | |
| 977 | smb->irq_unsupported = false; |
| 978 | |
| 979 | ret = smb347_irq_enable(smb); |
| 980 | if (ret < 0) |
| 981 | return ret; |
| 982 | |
| 983 | return 0; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Ramakrishna Pallala | 19dd6bc | 2012-06-19 16:45:05 -0700 | [diff] [blame] | 986 | /* |
| 987 | * Returns the constant charge current programmed |
| 988 | * into the charger in uA. |
| 989 | */ |
| 990 | static int get_const_charge_current(struct smb347_charger *smb) |
| 991 | { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 992 | unsigned int id = smb->id; |
Ramakrishna Pallala | 19dd6bc | 2012-06-19 16:45:05 -0700 | [diff] [blame] | 993 | int ret, intval; |
| 994 | unsigned int v; |
| 995 | |
| 996 | if (!smb347_is_ps_online(smb)) |
| 997 | return -ENODATA; |
| 998 | |
| 999 | ret = regmap_read(smb->regmap, STAT_B, &v); |
| 1000 | if (ret < 0) |
| 1001 | return ret; |
| 1002 | |
| 1003 | /* |
| 1004 | * The current value is composition of FCC and PCC values |
| 1005 | * and we can detect which table to use from bit 5. |
| 1006 | */ |
| 1007 | if (v & 0x20) { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 1008 | intval = hw_to_current(fcc_tbl[id], |
| 1009 | ARRAY_SIZE(fcc_tbl[id]), v & 7); |
Ramakrishna Pallala | 19dd6bc | 2012-06-19 16:45:05 -0700 | [diff] [blame] | 1010 | } else { |
| 1011 | v >>= 3; |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 1012 | intval = hw_to_current(pcc_tbl[id], |
| 1013 | ARRAY_SIZE(pcc_tbl[id]), v & 7); |
Ramakrishna Pallala | 19dd6bc | 2012-06-19 16:45:05 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | return intval; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Returns the constant charge voltage programmed |
| 1021 | * into the charger in uV. |
| 1022 | */ |
| 1023 | static int get_const_charge_voltage(struct smb347_charger *smb) |
| 1024 | { |
| 1025 | int ret, intval; |
| 1026 | unsigned int v; |
| 1027 | |
| 1028 | if (!smb347_is_ps_online(smb)) |
| 1029 | return -ENODATA; |
| 1030 | |
| 1031 | ret = regmap_read(smb->regmap, STAT_A, &v); |
| 1032 | if (ret < 0) |
| 1033 | return ret; |
| 1034 | |
| 1035 | v &= STAT_A_FLOAT_VOLTAGE_MASK; |
| 1036 | if (v > 0x3d) |
| 1037 | v = 0x3d; |
| 1038 | |
| 1039 | intval = 3500000 + v * 20000; |
| 1040 | |
| 1041 | return intval; |
| 1042 | } |
| 1043 | |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1044 | static int smb347_get_charging_status(struct smb347_charger *smb, |
| 1045 | struct power_supply *psy) |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 1046 | { |
| 1047 | int ret, status; |
| 1048 | unsigned int val; |
| 1049 | |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1050 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
| 1051 | if (!smb->usb_online) |
| 1052 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 1053 | } else { |
| 1054 | if (!smb->mains_online) |
| 1055 | return POWER_SUPPLY_STATUS_DISCHARGING; |
| 1056 | } |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 1057 | |
| 1058 | ret = regmap_read(smb->regmap, STAT_C, &val); |
| 1059 | if (ret < 0) |
| 1060 | return ret; |
| 1061 | |
| 1062 | if ((val & STAT_C_CHARGER_ERROR) || |
| 1063 | (val & STAT_C_HOLDOFF_STAT)) { |
| 1064 | /* |
| 1065 | * set to NOT CHARGING upon charger error |
| 1066 | * or charging has stopped. |
| 1067 | */ |
| 1068 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1069 | } else { |
| 1070 | if ((val & STAT_C_CHG_MASK) >> STAT_C_CHG_SHIFT) { |
| 1071 | /* |
| 1072 | * set to charging if battery is in pre-charge, |
| 1073 | * fast charge or taper charging mode. |
| 1074 | */ |
| 1075 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 1076 | } else if (val & STAT_C_CHG_TERM) { |
| 1077 | /* |
| 1078 | * set the status to FULL if battery is not in pre |
| 1079 | * charge, fast charge or taper charging mode AND |
| 1080 | * charging is terminated at least once. |
| 1081 | */ |
| 1082 | status = POWER_SUPPLY_STATUS_FULL; |
| 1083 | } else { |
| 1084 | /* |
| 1085 | * in this case no charger error or termination |
| 1086 | * occured but charging is not in progress!!! |
| 1087 | */ |
| 1088 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | return status; |
| 1093 | } |
| 1094 | |
Dmitry Osipenko | 99298de | 2020-08-14 00:34:07 +0300 | [diff] [blame] | 1095 | static int smb347_get_property_locked(struct power_supply *psy, |
| 1096 | enum power_supply_property prop, |
| 1097 | union power_supply_propval *val) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1098 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1099 | struct smb347_charger *smb = power_supply_get_drvdata(psy); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1100 | int ret; |
| 1101 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1102 | switch (prop) { |
| 1103 | case POWER_SUPPLY_PROP_STATUS: |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1104 | ret = smb347_get_charging_status(smb, psy); |
Ramakrishna Pallala | 1502cfe | 2012-09-18 21:58:07 +0530 | [diff] [blame] | 1105 | if (ret < 0) |
| 1106 | return ret; |
| 1107 | val->intval = ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1108 | break; |
| 1109 | |
| 1110 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1111 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) { |
| 1112 | if (!smb->usb_online) |
| 1113 | return -ENODATA; |
| 1114 | } else { |
| 1115 | if (!smb->mains_online) |
| 1116 | return -ENODATA; |
| 1117 | } |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1118 | |
| 1119 | /* |
| 1120 | * We handle trickle and pre-charging the same, and taper |
| 1121 | * and none the same. |
| 1122 | */ |
| 1123 | switch (smb347_charging_status(smb)) { |
| 1124 | case 1: |
| 1125 | val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; |
| 1126 | break; |
| 1127 | case 2: |
| 1128 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 1129 | break; |
| 1130 | default: |
| 1131 | val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 1132 | break; |
| 1133 | } |
| 1134 | break; |
| 1135 | |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1136 | case POWER_SUPPLY_PROP_ONLINE: |
| 1137 | if (psy->desc->type == POWER_SUPPLY_TYPE_USB) |
| 1138 | val->intval = smb->usb_online; |
| 1139 | else |
| 1140 | val->intval = smb->mains_online; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1143 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: |
| 1144 | ret = get_const_charge_voltage(smb); |
| 1145 | if (ret < 0) |
| 1146 | return ret; |
| 1147 | val->intval = ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1150 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: |
| 1151 | ret = get_const_charge_current(smb); |
| 1152 | if (ret < 0) |
| 1153 | return ret; |
| 1154 | val->intval = ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | |
| 1157 | default: |
| 1158 | return -EINVAL; |
| 1159 | } |
| 1160 | |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
Dmitry Osipenko | 99298de | 2020-08-14 00:34:07 +0300 | [diff] [blame] | 1164 | static int smb347_get_property(struct power_supply *psy, |
| 1165 | enum power_supply_property prop, |
| 1166 | union power_supply_propval *val) |
| 1167 | { |
| 1168 | struct smb347_charger *smb = power_supply_get_drvdata(psy); |
| 1169 | struct i2c_client *client = to_i2c_client(smb->dev); |
| 1170 | int ret; |
| 1171 | |
Dmitry Osipenko | d33b3f7 | 2021-03-02 13:10:26 +0300 | [diff] [blame] | 1172 | if (!smb->irq_unsupported) |
| 1173 | disable_irq(client->irq); |
| 1174 | |
Dmitry Osipenko | 99298de | 2020-08-14 00:34:07 +0300 | [diff] [blame] | 1175 | ret = smb347_get_property_locked(psy, prop, val); |
Dmitry Osipenko | d33b3f7 | 2021-03-02 13:10:26 +0300 | [diff] [blame] | 1176 | |
| 1177 | if (!smb->irq_unsupported) |
| 1178 | enable_irq(client->irq); |
Dmitry Osipenko | 99298de | 2020-08-14 00:34:07 +0300 | [diff] [blame] | 1179 | |
| 1180 | return ret; |
| 1181 | } |
| 1182 | |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1183 | static enum power_supply_property smb347_properties[] = { |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1184 | POWER_SUPPLY_PROP_STATUS, |
| 1185 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1186 | POWER_SUPPLY_PROP_ONLINE, |
| 1187 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, |
| 1188 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1189 | }; |
| 1190 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1191 | static bool smb347_volatile_reg(struct device *dev, unsigned int reg) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1192 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1193 | switch (reg) { |
| 1194 | case IRQSTAT_A: |
| 1195 | case IRQSTAT_C: |
Dmitry Osipenko | c32ea07 | 2020-03-29 18:15:44 +0200 | [diff] [blame] | 1196 | case IRQSTAT_D: |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1197 | case IRQSTAT_E: |
| 1198 | case IRQSTAT_F: |
| 1199 | case STAT_A: |
| 1200 | case STAT_B: |
| 1201 | case STAT_C: |
| 1202 | case STAT_E: |
| 1203 | return true; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1206 | return false; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1209 | static bool smb347_readable_reg(struct device *dev, unsigned int reg) |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1210 | { |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1211 | switch (reg) { |
| 1212 | case CFG_CHARGE_CURRENT: |
| 1213 | case CFG_CURRENT_LIMIT: |
| 1214 | case CFG_FLOAT_VOLTAGE: |
| 1215 | case CFG_STAT: |
| 1216 | case CFG_PIN: |
| 1217 | case CFG_THERM: |
| 1218 | case CFG_SYSOK: |
| 1219 | case CFG_OTHER: |
| 1220 | case CFG_OTG: |
| 1221 | case CFG_TEMP_LIMIT: |
| 1222 | case CFG_FAULT_IRQ: |
| 1223 | case CFG_STATUS_IRQ: |
| 1224 | case CFG_ADDRESS: |
| 1225 | case CMD_A: |
| 1226 | case CMD_B: |
| 1227 | case CMD_C: |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | return smb347_volatile_reg(dev, reg); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1234 | static void smb347_dt_parse_dev_info(struct smb347_charger *smb) |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1235 | { |
Sebastian Reichel | f385e2f | 2020-08-26 16:41:59 +0200 | [diff] [blame] | 1236 | struct device *dev = smb->dev; |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1237 | |
| 1238 | smb->soft_temp_limit_compensation = |
| 1239 | SMB3XX_SOFT_TEMP_COMPENSATE_DEFAULT; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1240 | /* |
| 1241 | * These properties come from the battery info, still we need to |
| 1242 | * pre-initialize the values. See smb347_get_battery_info() below. |
| 1243 | */ |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1244 | smb->soft_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
| 1245 | smb->hard_cold_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
| 1246 | smb->soft_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
| 1247 | smb->hard_hot_temp_limit = SMB3XX_TEMP_USE_DEFAULT; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1248 | |
| 1249 | /* Charging constraints */ |
Sebastian Reichel | f385e2f | 2020-08-26 16:41:59 +0200 | [diff] [blame] | 1250 | device_property_read_u32(dev, "summit,fast-voltage-threshold-microvolt", |
| 1251 | &smb->pre_to_fast_voltage); |
| 1252 | device_property_read_u32(dev, "summit,mains-current-limit-microamp", |
| 1253 | &smb->mains_current_limit); |
| 1254 | device_property_read_u32(dev, "summit,usb-current-limit-microamp", |
| 1255 | &smb->usb_hc_current_limit); |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1256 | |
| 1257 | /* For thermometer monitoring */ |
Sebastian Reichel | f385e2f | 2020-08-26 16:41:59 +0200 | [diff] [blame] | 1258 | device_property_read_u32(dev, "summit,chip-temperature-threshold-celsius", |
| 1259 | &smb->chip_temp_threshold); |
| 1260 | device_property_read_u32(dev, "summit,soft-compensation-method", |
| 1261 | &smb->soft_temp_limit_compensation); |
| 1262 | device_property_read_u32(dev, "summit,charge-current-compensation-microamp", |
| 1263 | &smb->charge_current_compensation); |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1264 | |
| 1265 | /* Supported charging mode */ |
Sebastian Reichel | f385e2f | 2020-08-26 16:41:59 +0200 | [diff] [blame] | 1266 | smb->use_mains = device_property_read_bool(dev, "summit,enable-mains-charging"); |
| 1267 | smb->use_usb = device_property_read_bool(dev, "summit,enable-usb-charging"); |
| 1268 | smb->use_usb_otg = device_property_read_bool(dev, "summit,enable-otg-charging"); |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1269 | |
| 1270 | /* Select charging control */ |
Sebastian Reichel | f385e2f | 2020-08-26 16:41:59 +0200 | [diff] [blame] | 1271 | device_property_read_u32(dev, "summit,enable-charge-control", |
| 1272 | &smb->enable_control); |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1273 | |
| 1274 | /* |
| 1275 | * Polarity of INOK signal indicating presence of external power |
| 1276 | * supply connected to the charger. |
| 1277 | */ |
| 1278 | device_property_read_u32(dev, "summit,inok-polarity", |
| 1279 | &smb->inok_polarity); |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | static int smb347_get_battery_info(struct smb347_charger *smb) |
| 1283 | { |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1284 | struct power_supply_battery_info *info; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1285 | struct power_supply *supply; |
| 1286 | int err; |
| 1287 | |
| 1288 | if (smb->mains) |
| 1289 | supply = smb->mains; |
| 1290 | else |
| 1291 | supply = smb->usb; |
| 1292 | |
| 1293 | err = power_supply_get_battery_info(supply, &info); |
| 1294 | if (err == -ENXIO || err == -ENODEV) |
| 1295 | return 0; |
| 1296 | if (err) |
| 1297 | return err; |
| 1298 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1299 | if (info->constant_charge_current_max_ua != -EINVAL) |
| 1300 | smb->max_charge_current = info->constant_charge_current_max_ua; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1301 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1302 | if (info->constant_charge_voltage_max_uv != -EINVAL) |
| 1303 | smb->max_charge_voltage = info->constant_charge_voltage_max_uv; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1304 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1305 | if (info->precharge_current_ua != -EINVAL) |
| 1306 | smb->pre_charge_current = info->precharge_current_ua; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1307 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1308 | if (info->charge_term_current_ua != -EINVAL) |
| 1309 | smb->termination_current = info->charge_term_current_ua; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1310 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1311 | if (info->temp_alert_min != INT_MIN) |
| 1312 | smb->soft_cold_temp_limit = info->temp_alert_min; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1313 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1314 | if (info->temp_alert_max != INT_MAX) |
| 1315 | smb->soft_hot_temp_limit = info->temp_alert_max; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1316 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1317 | if (info->temp_min != INT_MIN) |
| 1318 | smb->hard_cold_temp_limit = info->temp_min; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1319 | |
Linus Walleij | 25fd330 | 2021-12-15 02:01:18 +0100 | [diff] [blame] | 1320 | if (info->temp_max != INT_MAX) |
| 1321 | smb->hard_hot_temp_limit = info->temp_max; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1322 | |
| 1323 | /* Suspend when battery temperature is outside hard limits */ |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1324 | if (smb->hard_cold_temp_limit != SMB3XX_TEMP_USE_DEFAULT || |
| 1325 | smb->hard_hot_temp_limit != SMB3XX_TEMP_USE_DEFAULT) |
| 1326 | smb->suspend_on_hard_temp_limit = true; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1331 | static int smb347_usb_vbus_get_current_limit(struct regulator_dev *rdev) |
| 1332 | { |
| 1333 | struct smb347_charger *smb = rdev_get_drvdata(rdev); |
| 1334 | unsigned int val; |
| 1335 | int ret; |
| 1336 | |
| 1337 | ret = regmap_read(smb->regmap, CFG_OTG, &val); |
| 1338 | if (ret < 0) |
| 1339 | return ret; |
| 1340 | |
| 1341 | /* |
| 1342 | * It's unknown what happens if this bit is unset due to lack of |
| 1343 | * access to the datasheet, assume it's limit-enable. |
| 1344 | */ |
| 1345 | if (!(val & CFG_OTG_CURRENT_LIMIT_250mA)) |
| 1346 | return 0; |
| 1347 | |
| 1348 | return val & CFG_OTG_CURRENT_LIMIT_750mA ? 750000 : 250000; |
| 1349 | } |
| 1350 | |
| 1351 | static int smb347_usb_vbus_set_new_current_limit(struct smb347_charger *smb, |
| 1352 | int max_uA) |
| 1353 | { |
| 1354 | const unsigned int mask = CFG_OTG_CURRENT_LIMIT_750mA | |
| 1355 | CFG_OTG_CURRENT_LIMIT_250mA; |
| 1356 | unsigned int val = CFG_OTG_CURRENT_LIMIT_250mA; |
| 1357 | int ret; |
| 1358 | |
| 1359 | if (max_uA >= 750000) |
| 1360 | val |= CFG_OTG_CURRENT_LIMIT_750mA; |
| 1361 | |
| 1362 | ret = regmap_update_bits(smb->regmap, CFG_OTG, mask, val); |
| 1363 | if (ret < 0) |
| 1364 | dev_err(smb->dev, "failed to change USB current limit\n"); |
| 1365 | |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
| 1369 | static int smb347_usb_vbus_set_current_limit(struct regulator_dev *rdev, |
| 1370 | int min_uA, int max_uA) |
| 1371 | { |
| 1372 | struct smb347_charger *smb = rdev_get_drvdata(rdev); |
| 1373 | int ret; |
| 1374 | |
| 1375 | ret = smb347_set_writable(smb, true, true); |
| 1376 | if (ret < 0) |
| 1377 | return ret; |
| 1378 | |
| 1379 | ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA); |
| 1380 | smb347_set_writable(smb, false, true); |
| 1381 | |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
| 1385 | static int smb347_usb_vbus_regulator_enable(struct regulator_dev *rdev) |
| 1386 | { |
| 1387 | struct smb347_charger *smb = rdev_get_drvdata(rdev); |
| 1388 | int ret, max_uA; |
| 1389 | |
| 1390 | ret = smb347_set_writable(smb, true, true); |
| 1391 | if (ret < 0) |
| 1392 | return ret; |
| 1393 | |
| 1394 | smb347_charging_disable(smb); |
| 1395 | |
| 1396 | if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) { |
| 1397 | unsigned int sysok = 0; |
| 1398 | |
| 1399 | if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_LOW) |
| 1400 | sysok = CFG_SYSOK_INOK_ACTIVE_HIGH; |
| 1401 | |
| 1402 | /* |
| 1403 | * VBUS won't be powered if INOK is active, so we need to |
| 1404 | * manually disable INOK on some platforms. |
| 1405 | */ |
| 1406 | ret = regmap_update_bits(smb->regmap, CFG_SYSOK, |
| 1407 | CFG_SYSOK_INOK_ACTIVE_HIGH, sysok); |
| 1408 | if (ret < 0) { |
| 1409 | dev_err(smb->dev, "failed to disable INOK\n"); |
| 1410 | goto done; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | ret = smb347_usb_vbus_get_current_limit(rdev); |
| 1415 | if (ret < 0) { |
| 1416 | dev_err(smb->dev, "failed to get USB VBUS current limit\n"); |
| 1417 | goto done; |
| 1418 | } |
| 1419 | |
| 1420 | max_uA = ret; |
| 1421 | |
| 1422 | ret = smb347_usb_vbus_set_new_current_limit(smb, 250000); |
| 1423 | if (ret < 0) { |
| 1424 | dev_err(smb->dev, "failed to preset USB VBUS current limit\n"); |
| 1425 | goto done; |
| 1426 | } |
| 1427 | |
| 1428 | ret = regmap_set_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED); |
| 1429 | if (ret < 0) { |
| 1430 | dev_err(smb->dev, "failed to enable USB VBUS\n"); |
| 1431 | goto done; |
| 1432 | } |
| 1433 | |
| 1434 | smb->usb_vbus_enabled = true; |
| 1435 | |
| 1436 | ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA); |
| 1437 | if (ret < 0) { |
| 1438 | dev_err(smb->dev, "failed to restore USB VBUS current limit\n"); |
| 1439 | goto done; |
| 1440 | } |
| 1441 | done: |
| 1442 | smb347_set_writable(smb, false, true); |
| 1443 | |
| 1444 | return ret; |
| 1445 | } |
| 1446 | |
| 1447 | static int smb347_usb_vbus_regulator_disable(struct regulator_dev *rdev) |
| 1448 | { |
| 1449 | struct smb347_charger *smb = rdev_get_drvdata(rdev); |
| 1450 | int ret; |
| 1451 | |
| 1452 | ret = smb347_set_writable(smb, true, true); |
| 1453 | if (ret < 0) |
| 1454 | return ret; |
| 1455 | |
| 1456 | ret = regmap_clear_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED); |
| 1457 | if (ret < 0) { |
| 1458 | dev_err(smb->dev, "failed to disable USB VBUS\n"); |
| 1459 | goto done; |
| 1460 | } |
| 1461 | |
| 1462 | smb->usb_vbus_enabled = false; |
| 1463 | |
| 1464 | if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) { |
| 1465 | unsigned int sysok = 0; |
| 1466 | |
| 1467 | if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_HIGH) |
| 1468 | sysok = CFG_SYSOK_INOK_ACTIVE_HIGH; |
| 1469 | |
| 1470 | ret = regmap_update_bits(smb->regmap, CFG_SYSOK, |
| 1471 | CFG_SYSOK_INOK_ACTIVE_HIGH, sysok); |
| 1472 | if (ret < 0) { |
| 1473 | dev_err(smb->dev, "failed to enable INOK\n"); |
| 1474 | goto done; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | smb347_start_stop_charging(smb); |
| 1479 | done: |
| 1480 | smb347_set_writable(smb, false, true); |
| 1481 | |
| 1482 | return ret; |
| 1483 | } |
| 1484 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1485 | static const struct regmap_config smb347_regmap = { |
| 1486 | .reg_bits = 8, |
| 1487 | .val_bits = 8, |
| 1488 | .max_register = SMB347_MAX_REGISTER, |
| 1489 | .volatile_reg = smb347_volatile_reg, |
| 1490 | .readable_reg = smb347_readable_reg, |
Dmitry Osipenko | 17e7bc5 | 2021-07-31 20:38:37 +0300 | [diff] [blame] | 1491 | .cache_type = REGCACHE_FLAT, |
| 1492 | .num_reg_defaults_raw = SMB347_MAX_REGISTER, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1493 | }; |
| 1494 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1495 | static const struct regulator_ops smb347_usb_vbus_regulator_ops = { |
| 1496 | .is_enabled = regulator_is_enabled_regmap, |
| 1497 | .enable = smb347_usb_vbus_regulator_enable, |
| 1498 | .disable = smb347_usb_vbus_regulator_disable, |
| 1499 | .get_current_limit = smb347_usb_vbus_get_current_limit, |
| 1500 | .set_current_limit = smb347_usb_vbus_set_current_limit, |
| 1501 | }; |
| 1502 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1503 | static const struct power_supply_desc smb347_mains_desc = { |
| 1504 | .name = "smb347-mains", |
| 1505 | .type = POWER_SUPPLY_TYPE_MAINS, |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1506 | .get_property = smb347_get_property, |
| 1507 | .properties = smb347_properties, |
| 1508 | .num_properties = ARRAY_SIZE(smb347_properties), |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1509 | }; |
| 1510 | |
| 1511 | static const struct power_supply_desc smb347_usb_desc = { |
| 1512 | .name = "smb347-usb", |
| 1513 | .type = POWER_SUPPLY_TYPE_USB, |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1514 | .get_property = smb347_get_property, |
| 1515 | .properties = smb347_properties, |
| 1516 | .num_properties = ARRAY_SIZE(smb347_properties), |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1517 | }; |
| 1518 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1519 | static const struct regulator_desc smb347_usb_vbus_regulator_desc = { |
| 1520 | .name = "smb347-usb-vbus", |
| 1521 | .of_match = of_match_ptr("usb-vbus"), |
| 1522 | .ops = &smb347_usb_vbus_regulator_ops, |
| 1523 | .type = REGULATOR_VOLTAGE, |
| 1524 | .owner = THIS_MODULE, |
| 1525 | .enable_reg = CMD_A, |
| 1526 | .enable_mask = CMD_A_OTG_ENABLED, |
| 1527 | .enable_val = CMD_A_OTG_ENABLED, |
| 1528 | .fixed_uV = 5000000, |
| 1529 | .n_voltages = 1, |
| 1530 | }; |
| 1531 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1532 | static int smb347_probe(struct i2c_client *client, |
| 1533 | const struct i2c_device_id *id) |
| 1534 | { |
David Heidelberg | db14d3b | 2020-08-14 00:34:06 +0300 | [diff] [blame] | 1535 | struct power_supply_config mains_usb_cfg = {}; |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1536 | struct regulator_config usb_rdev_cfg = {}; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1537 | struct device *dev = &client->dev; |
| 1538 | struct smb347_charger *smb; |
| 1539 | int ret; |
| 1540 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1541 | smb = devm_kzalloc(dev, sizeof(*smb), GFP_KERNEL); |
| 1542 | if (!smb) |
| 1543 | return -ENOMEM; |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1544 | smb->dev = &client->dev; |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 1545 | smb->id = id->driver_data; |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1546 | i2c_set_clientdata(client, smb); |
| 1547 | |
| 1548 | smb347_dt_parse_dev_info(smb); |
| 1549 | if (!smb->use_mains && !smb->use_usb) |
| 1550 | return -EINVAL; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1551 | |
Mika Westerberg | 34298d4 | 2012-04-16 11:48:41 +0300 | [diff] [blame] | 1552 | smb->regmap = devm_regmap_init_i2c(client, &smb347_regmap); |
| 1553 | if (IS_ERR(smb->regmap)) |
| 1554 | return PTR_ERR(smb->regmap); |
| 1555 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1556 | mains_usb_cfg.drv_data = smb; |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1557 | mains_usb_cfg.of_node = dev->of_node; |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1558 | if (smb->use_mains) { |
David Heidelberg | 2d52f71 | 2020-08-14 00:34:03 +0300 | [diff] [blame] | 1559 | smb->mains = devm_power_supply_register(dev, &smb347_mains_desc, |
| 1560 | &mains_usb_cfg); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1561 | if (IS_ERR(smb->mains)) |
| 1562 | return PTR_ERR(smb->mains); |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 1563 | } |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1564 | |
Sebastian Reichel | b6f3e21 | 2020-08-26 16:41:58 +0200 | [diff] [blame] | 1565 | if (smb->use_usb) { |
David Heidelberg | 2d52f71 | 2020-08-14 00:34:03 +0300 | [diff] [blame] | 1566 | smb->usb = devm_power_supply_register(dev, &smb347_usb_desc, |
| 1567 | &mains_usb_cfg); |
| 1568 | if (IS_ERR(smb->usb)) |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 1569 | return PTR_ERR(smb->usb); |
Ramakrishna Pallala | e6fe359 | 2012-04-19 10:00:18 +0530 | [diff] [blame] | 1570 | } |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1571 | |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1572 | ret = smb347_get_battery_info(smb); |
| 1573 | if (ret) |
| 1574 | return ret; |
| 1575 | |
| 1576 | ret = smb347_hw_init(smb); |
| 1577 | if (ret < 0) |
| 1578 | return ret; |
| 1579 | |
Dmitry Osipenko | d33b3f7 | 2021-03-02 13:10:26 +0300 | [diff] [blame] | 1580 | ret = smb347_irq_init(smb, client); |
| 1581 | if (ret) |
| 1582 | return ret; |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1583 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1584 | usb_rdev_cfg.dev = dev; |
| 1585 | usb_rdev_cfg.driver_data = smb; |
| 1586 | usb_rdev_cfg.regmap = smb->regmap; |
| 1587 | |
| 1588 | smb->usb_rdev = devm_regulator_register(dev, |
| 1589 | &smb347_usb_vbus_regulator_desc, |
| 1590 | &usb_rdev_cfg); |
| 1591 | if (IS_ERR(smb->usb_rdev)) { |
| 1592 | smb347_irq_disable(smb); |
| 1593 | return PTR_ERR(smb->usb_rdev); |
| 1594 | } |
| 1595 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | static int smb347_remove(struct i2c_client *client) |
| 1600 | { |
| 1601 | struct smb347_charger *smb = i2c_get_clientdata(client); |
| 1602 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1603 | smb347_usb_vbus_regulator_disable(smb->usb_rdev); |
Dmitry Osipenko | 6996312 | 2021-01-22 22:17:34 +0300 | [diff] [blame] | 1604 | smb347_irq_disable(smb); |
| 1605 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1606 | return 0; |
| 1607 | } |
| 1608 | |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1609 | static void smb347_shutdown(struct i2c_client *client) |
| 1610 | { |
| 1611 | smb347_remove(client); |
| 1612 | } |
| 1613 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1614 | static const struct i2c_device_id smb347_id[] = { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 1615 | { "smb345", SMB345 }, |
| 1616 | { "smb347", SMB347 }, |
| 1617 | { "smb358", SMB358 }, |
| 1618 | { }, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1619 | }; |
| 1620 | MODULE_DEVICE_TABLE(i2c, smb347_id); |
| 1621 | |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1622 | static const struct of_device_id smb3xx_of_match[] = { |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 1623 | { .compatible = "summit,smb345" }, |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1624 | { .compatible = "summit,smb347" }, |
David Heidelberg | de76fd2 | 2020-08-14 00:34:05 +0300 | [diff] [blame] | 1625 | { .compatible = "summit,smb358" }, |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1626 | { }, |
| 1627 | }; |
| 1628 | MODULE_DEVICE_TABLE(of, smb3xx_of_match); |
| 1629 | |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1630 | static struct i2c_driver smb347_driver = { |
| 1631 | .driver = { |
| 1632 | .name = "smb347", |
David Heidelberg | 364bec7 | 2020-08-14 00:34:04 +0300 | [diff] [blame] | 1633 | .of_match_table = smb3xx_of_match, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1634 | }, |
Dmitry Osipenko | 2552e38 | 2021-03-02 13:10:27 +0300 | [diff] [blame] | 1635 | .probe = smb347_probe, |
| 1636 | .remove = smb347_remove, |
Dmitry Osipenko | 565efae | 2021-07-31 20:38:39 +0300 | [diff] [blame] | 1637 | .shutdown = smb347_shutdown, |
Dmitry Osipenko | 2552e38 | 2021-03-02 13:10:27 +0300 | [diff] [blame] | 1638 | .id_table = smb347_id, |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1639 | }; |
Mika Westerberg | b75ef1d | 2012-04-16 11:48:38 +0300 | [diff] [blame] | 1640 | module_i2c_driver(smb347_driver); |
Bruce E. Robertson | ed1a230 | 2012-02-06 15:59:01 +0000 | [diff] [blame] | 1641 | |
| 1642 | MODULE_AUTHOR("Bruce E. Robertson <bruce.e.robertson@intel.com>"); |
| 1643 | MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); |
| 1644 | MODULE_DESCRIPTION("SMB347 battery charger driver"); |
| 1645 | MODULE_LICENSE("GPL"); |