Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 1 | /* |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 2 | * max14577_charger.c - Battery charger driver for the Maxim 14577/77836 |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 3 | * |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 4 | * Copyright (C) 2013,2014 Samsung Electronics |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 5 | * Krzysztof Kozlowski <k.kozlowski@samsung.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/power_supply.h> |
| 21 | #include <linux/mfd/max14577-private.h> |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 22 | #include <linux/mfd/max14577.h> |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 23 | |
| 24 | struct max14577_charger { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 25 | struct device *dev; |
| 26 | struct max14577 *max14577; |
| 27 | struct power_supply *charger; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 28 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 29 | struct max14577_charger_platform_data *pdata; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 32 | /* |
| 33 | * Helper function for mapping values of STATUS2/CHGTYP register on max14577 |
| 34 | * and max77836 chipsets to enum maxim_muic_charger_type. |
| 35 | */ |
| 36 | static enum max14577_muic_charger_type maxim_get_charger_type( |
| 37 | enum maxim_device_type dev_type, u8 val) { |
| 38 | switch (val) { |
| 39 | case MAX14577_CHARGER_TYPE_NONE: |
| 40 | case MAX14577_CHARGER_TYPE_USB: |
| 41 | case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT: |
| 42 | case MAX14577_CHARGER_TYPE_DEDICATED_CHG: |
| 43 | case MAX14577_CHARGER_TYPE_SPECIAL_500MA: |
| 44 | case MAX14577_CHARGER_TYPE_SPECIAL_1A: |
| 45 | return val; |
| 46 | case MAX14577_CHARGER_TYPE_DEAD_BATTERY: |
| 47 | case MAX14577_CHARGER_TYPE_RESERVED: |
| 48 | if (dev_type == MAXIM_DEVICE_TYPE_MAX77836) |
| 49 | val |= 0x8; |
| 50 | return val; |
| 51 | default: |
| 52 | WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val); |
| 53 | return val; |
| 54 | } |
| 55 | } |
| 56 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 57 | static int max14577_get_charger_state(struct max14577_charger *chg, int *val) |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 58 | { |
| 59 | struct regmap *rmap = chg->max14577->regmap; |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 60 | int ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 61 | u8 reg_data; |
| 62 | |
| 63 | /* |
| 64 | * Charging occurs only if: |
| 65 | * - CHGCTRL2/MBCHOSTEN == 1 |
| 66 | * - STATUS2/CGMBC == 1 |
| 67 | * |
| 68 | * TODO: |
| 69 | * - handle FULL after Top-off timer (EOC register may be off |
| 70 | * and the charger won't be charging although MBCHOSTEN is on) |
| 71 | * - handle properly dead-battery charging (respect timer) |
| 72 | * - handle timers (fast-charge and prequal) /MBCCHGERR/ |
| 73 | */ |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 74 | ret = max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, ®_data); |
| 75 | if (ret < 0) |
| 76 | goto out; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 77 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 78 | if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0) { |
| 79 | *val = POWER_SUPPLY_STATUS_DISCHARGING; |
| 80 | goto out; |
| 81 | } |
| 82 | |
| 83 | ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, ®_data); |
| 84 | if (ret < 0) |
| 85 | goto out; |
| 86 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 87 | if (reg_data & STATUS3_CGMBC_MASK) { |
| 88 | /* Charger or USB-cable is connected */ |
| 89 | if (reg_data & STATUS3_EOC_MASK) |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 90 | *val = POWER_SUPPLY_STATUS_FULL; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 91 | else |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 92 | *val = POWER_SUPPLY_STATUS_CHARGING; |
| 93 | goto out; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 96 | *val = POWER_SUPPLY_STATUS_DISCHARGING; |
| 97 | |
| 98 | out: |
| 99 | return ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Supported charge types: |
| 104 | * - POWER_SUPPLY_CHARGE_TYPE_NONE |
| 105 | * - POWER_SUPPLY_CHARGE_TYPE_FAST |
| 106 | */ |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 107 | static int max14577_get_charge_type(struct max14577_charger *chg, int *val) |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 108 | { |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 109 | int ret, charging; |
| 110 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 111 | /* |
| 112 | * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)? |
| 113 | * As spec says: |
| 114 | * [after reaching EOC interrupt] |
| 115 | * "When the battery is fully charged, the 30-minute (typ) |
| 116 | * top-off timer starts. The device continues to trickle |
| 117 | * charge the battery until the top-off timer runs out." |
| 118 | */ |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 119 | ret = max14577_get_charger_state(chg, &charging); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | |
| 123 | if (charging == POWER_SUPPLY_STATUS_CHARGING) |
| 124 | *val = POWER_SUPPLY_CHARGE_TYPE_FAST; |
| 125 | else |
| 126 | *val = POWER_SUPPLY_CHARGE_TYPE_NONE; |
| 127 | |
| 128 | return 0; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 131 | static int max14577_get_online(struct max14577_charger *chg, int *val) |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 132 | { |
| 133 | struct regmap *rmap = chg->max14577->regmap; |
| 134 | u8 reg_data; |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 135 | int ret; |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 136 | enum max14577_muic_charger_type chg_type; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 137 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 138 | ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, ®_data); |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 142 | reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT); |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 143 | chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data); |
| 144 | switch (chg_type) { |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 145 | case MAX14577_CHARGER_TYPE_USB: |
| 146 | case MAX14577_CHARGER_TYPE_DEDICATED_CHG: |
| 147 | case MAX14577_CHARGER_TYPE_SPECIAL_500MA: |
| 148 | case MAX14577_CHARGER_TYPE_SPECIAL_1A: |
| 149 | case MAX14577_CHARGER_TYPE_DEAD_BATTERY: |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 150 | case MAX77836_CHARGER_TYPE_SPECIAL_BIAS: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 151 | *val = 1; |
| 152 | break; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 153 | case MAX14577_CHARGER_TYPE_NONE: |
| 154 | case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT: |
| 155 | case MAX14577_CHARGER_TYPE_RESERVED: |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 156 | case MAX77836_CHARGER_TYPE_RESERVED: |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 157 | default: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 158 | *val = 0; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 159 | } |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 160 | |
| 161 | return 0; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Supported health statuses: |
| 166 | * - POWER_SUPPLY_HEALTH_DEAD |
| 167 | * - POWER_SUPPLY_HEALTH_OVERVOLTAGE |
| 168 | * - POWER_SUPPLY_HEALTH_GOOD |
| 169 | */ |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 170 | static int max14577_get_battery_health(struct max14577_charger *chg, int *val) |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 171 | { |
| 172 | struct regmap *rmap = chg->max14577->regmap; |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 173 | int ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 174 | u8 reg_data; |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 175 | enum max14577_muic_charger_type chg_type; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 176 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 177 | ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, ®_data); |
| 178 | if (ret < 0) |
| 179 | goto out; |
| 180 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 181 | reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT); |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 182 | chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data); |
| 183 | if (chg_type == MAX14577_CHARGER_TYPE_DEAD_BATTERY) { |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 184 | *val = POWER_SUPPLY_HEALTH_DEAD; |
| 185 | goto out; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 188 | ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, ®_data); |
| 189 | if (ret < 0) |
| 190 | goto out; |
| 191 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 192 | if (reg_data & STATUS3_OVP_MASK) { |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 193 | *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE; |
| 194 | goto out; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 197 | /* Not dead, not overvoltage */ |
| 198 | *val = POWER_SUPPLY_HEALTH_GOOD; |
| 199 | |
| 200 | out: |
| 201 | return ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Always returns 1. |
| 206 | * The max14577 chip doesn't report any status of battery presence. |
| 207 | * Lets assume that it will always be used with some battery. |
| 208 | */ |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 209 | static int max14577_get_present(struct max14577_charger *chg, int *val) |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 210 | { |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 211 | *val = 1; |
| 212 | |
| 213 | return 0; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 216 | static int max14577_set_fast_charge_timer(struct max14577_charger *chg, |
| 217 | unsigned long hours) |
| 218 | { |
| 219 | u8 reg_data; |
| 220 | |
| 221 | switch (hours) { |
| 222 | case 5 ... 7: |
| 223 | reg_data = hours - 3; |
| 224 | break; |
| 225 | case 0: |
| 226 | /* Disable */ |
| 227 | reg_data = 0x7; |
| 228 | break; |
| 229 | default: |
| 230 | dev_err(chg->dev, "Wrong value for Fast-Charge Timer: %lu\n", |
| 231 | hours); |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | reg_data <<= CHGCTRL1_TCHW_SHIFT; |
| 235 | |
| 236 | return max14577_update_reg(chg->max14577->regmap, |
| 237 | MAX14577_REG_CHGCTRL1, CHGCTRL1_TCHW_MASK, reg_data); |
| 238 | } |
| 239 | |
| 240 | static int max14577_init_constant_voltage(struct max14577_charger *chg, |
| 241 | unsigned int uvolt) |
| 242 | { |
| 243 | u8 reg_data; |
| 244 | |
| 245 | if (uvolt < MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN || |
| 246 | uvolt > MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | if (uvolt == 4200000) |
| 250 | reg_data = 0x0; |
| 251 | else if (uvolt == MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX) |
| 252 | reg_data = 0x1f; |
| 253 | else if (uvolt <= 4280000) { |
| 254 | unsigned int val = uvolt; |
| 255 | |
| 256 | val -= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN; |
| 257 | val /= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP; |
| 258 | if (uvolt <= 4180000) |
| 259 | reg_data = 0x1 + val; |
| 260 | else |
| 261 | reg_data = val; /* Fix for gap between 4.18V and 4.22V */ |
| 262 | } else |
| 263 | return -EINVAL; |
| 264 | |
| 265 | reg_data <<= CHGCTRL3_MBCCVWRC_SHIFT; |
| 266 | |
| 267 | return max14577_write_reg(chg->max14577->regmap, |
| 268 | MAX14577_CHG_REG_CHG_CTRL3, reg_data); |
| 269 | } |
| 270 | |
| 271 | static int max14577_init_eoc(struct max14577_charger *chg, |
| 272 | unsigned int uamp) |
| 273 | { |
| 274 | unsigned int current_bits = 0xf; |
| 275 | u8 reg_data; |
| 276 | |
| 277 | switch (chg->max14577->dev_type) { |
| 278 | case MAXIM_DEVICE_TYPE_MAX77836: |
| 279 | if (uamp < 5000) |
| 280 | return -EINVAL; /* Requested current is too low */ |
| 281 | |
| 282 | if (uamp >= 7500 && uamp < 10000) |
| 283 | current_bits = 0x0; |
| 284 | else if (uamp <= 50000) { |
| 285 | /* <5000, 7499> and <10000, 50000> */ |
| 286 | current_bits = uamp / 5000; |
| 287 | } else { |
| 288 | uamp = min(uamp, 100000U) - 50000U; |
| 289 | current_bits = 0xa + uamp / 10000; |
| 290 | } |
| 291 | break; |
| 292 | |
| 293 | case MAXIM_DEVICE_TYPE_MAX14577: |
| 294 | default: |
| 295 | if (uamp < MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN) |
| 296 | return -EINVAL; /* Requested current is too low */ |
| 297 | |
| 298 | uamp = min(uamp, MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX); |
| 299 | uamp -= MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN; |
| 300 | current_bits = uamp / MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | reg_data = current_bits << CHGCTRL5_EOCS_SHIFT; |
| 305 | |
| 306 | return max14577_update_reg(chg->max14577->regmap, |
| 307 | MAX14577_CHG_REG_CHG_CTRL5, CHGCTRL5_EOCS_MASK, |
| 308 | reg_data); |
| 309 | } |
| 310 | |
| 311 | static int max14577_init_fast_charge(struct max14577_charger *chg, |
| 312 | unsigned int uamp) |
| 313 | { |
| 314 | u8 reg_data; |
| 315 | int ret; |
| 316 | const struct maxim_charger_current *limits = |
| 317 | &maxim_charger_currents[chg->max14577->dev_type]; |
| 318 | |
| 319 | ret = maxim_charger_calc_reg_current(limits, uamp, uamp, ®_data); |
| 320 | if (ret) { |
| 321 | dev_err(chg->dev, "Wrong value for fast charge: %u\n", uamp); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | return max14577_update_reg(chg->max14577->regmap, |
| 326 | MAX14577_CHG_REG_CHG_CTRL4, |
| 327 | CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK, |
| 328 | reg_data); |
| 329 | } |
| 330 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 331 | /* |
| 332 | * Sets charger registers to proper and safe default values. |
| 333 | * Some of these values are equal to defaults in MAX14577E |
| 334 | * data sheet but there are minor differences. |
| 335 | */ |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 336 | static int max14577_charger_reg_init(struct max14577_charger *chg) |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 337 | { |
| 338 | struct regmap *rmap = chg->max14577->regmap; |
| 339 | u8 reg_data; |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 340 | int ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0) |
| 344 | * Charger-Detection Enable, default on (set CHGDETEN to 1) |
| 345 | * Combined mask of CHGDETEN and CHGTYPMAN will zero the CHGTYPMAN bit |
| 346 | */ |
| 347 | reg_data = 0x1 << CDETCTRL1_CHGDETEN_SHIFT; |
| 348 | max14577_update_reg(rmap, MAX14577_REG_CDETCTRL1, |
| 349 | CDETCTRL1_CHGDETEN_MASK | CDETCTRL1_CHGTYPMAN_MASK, |
| 350 | reg_data); |
| 351 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 352 | /* |
| 353 | * Wall-Adapter Rapid Charge, default on |
| 354 | * Battery-Charger, default on |
| 355 | */ |
| 356 | reg_data = 0x1 << CHGCTRL2_VCHGR_RC_SHIFT; |
| 357 | reg_data |= 0x1 << CHGCTRL2_MBCHOSTEN_SHIFT; |
| 358 | max14577_write_reg(rmap, MAX14577_REG_CHGCTRL2, reg_data); |
| 359 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 360 | /* Auto Charging Stop, default off */ |
| 361 | reg_data = 0x0 << CHGCTRL6_AUTOSTOP_SHIFT; |
| 362 | max14577_write_reg(rmap, MAX14577_REG_CHGCTRL6, reg_data); |
| 363 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 364 | ret = max14577_init_constant_voltage(chg, chg->pdata->constant_uvolt); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | |
| 368 | ret = max14577_init_eoc(chg, chg->pdata->eoc_uamp); |
| 369 | if (ret) |
| 370 | return ret; |
| 371 | |
| 372 | ret = max14577_init_fast_charge(chg, chg->pdata->fast_charge_uamp); |
| 373 | if (ret) |
| 374 | return ret; |
| 375 | |
| 376 | ret = max14577_set_fast_charge_timer(chg, |
| 377 | MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | |
| 381 | /* Initialize Overvoltage-Protection Threshold */ |
| 382 | switch (chg->pdata->ovp_uvolt) { |
| 383 | case 7500000: |
| 384 | reg_data = 0x0; |
| 385 | break; |
| 386 | case 6000000: |
| 387 | case 6500000: |
| 388 | case 7000000: |
| 389 | reg_data = 0x1 + (chg->pdata->ovp_uvolt - 6000000) / 500000; |
| 390 | break; |
| 391 | default: |
| 392 | dev_err(chg->dev, "Wrong value for OVP: %u\n", |
| 393 | chg->pdata->ovp_uvolt); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | reg_data <<= CHGCTRL7_OTPCGHCVS_SHIFT; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 397 | max14577_write_reg(rmap, MAX14577_REG_CHGCTRL7, reg_data); |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 398 | |
| 399 | return 0; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* Support property from charger */ |
| 403 | static enum power_supply_property max14577_charger_props[] = { |
| 404 | POWER_SUPPLY_PROP_STATUS, |
| 405 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
| 406 | POWER_SUPPLY_PROP_HEALTH, |
| 407 | POWER_SUPPLY_PROP_PRESENT, |
| 408 | POWER_SUPPLY_PROP_ONLINE, |
| 409 | POWER_SUPPLY_PROP_MODEL_NAME, |
| 410 | POWER_SUPPLY_PROP_MANUFACTURER, |
| 411 | }; |
| 412 | |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 413 | static const char * const model_names[] = { |
| 414 | [MAXIM_DEVICE_TYPE_UNKNOWN] = "MAX14577-like", |
| 415 | [MAXIM_DEVICE_TYPE_MAX14577] = "MAX14577", |
| 416 | [MAXIM_DEVICE_TYPE_MAX77836] = "MAX77836", |
| 417 | }; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 418 | static const char *manufacturer = "Maxim Integrated"; |
| 419 | |
| 420 | static int max14577_charger_get_property(struct power_supply *psy, |
| 421 | enum power_supply_property psp, |
| 422 | union power_supply_propval *val) |
| 423 | { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 424 | struct max14577_charger *chg = power_supply_get_drvdata(psy); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 425 | int ret = 0; |
| 426 | |
| 427 | switch (psp) { |
| 428 | case POWER_SUPPLY_PROP_STATUS: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 429 | ret = max14577_get_charger_state(chg, &val->intval); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 430 | break; |
| 431 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 432 | ret = max14577_get_charge_type(chg, &val->intval); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 433 | break; |
| 434 | case POWER_SUPPLY_PROP_HEALTH: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 435 | ret = max14577_get_battery_health(chg, &val->intval); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 436 | break; |
| 437 | case POWER_SUPPLY_PROP_PRESENT: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 438 | ret = max14577_get_present(chg, &val->intval); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 439 | break; |
| 440 | case POWER_SUPPLY_PROP_ONLINE: |
Krzysztof Kozlowski | dd18a66 | 2015-02-24 10:54:45 +0100 | [diff] [blame] | 441 | ret = max14577_get_online(chg, &val->intval); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 442 | break; |
| 443 | case POWER_SUPPLY_PROP_MODEL_NAME: |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 444 | BUILD_BUG_ON(ARRAY_SIZE(model_names) != MAXIM_DEVICE_TYPE_NUM); |
| 445 | val->strval = model_names[chg->max14577->dev_type]; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 446 | break; |
| 447 | case POWER_SUPPLY_PROP_MANUFACTURER: |
| 448 | val->strval = manufacturer; |
| 449 | break; |
| 450 | default: |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 457 | static const struct power_supply_desc max14577_charger_desc = { |
| 458 | .name = "max14577-charger", |
| 459 | .type = POWER_SUPPLY_TYPE_BATTERY, |
| 460 | .properties = max14577_charger_props, |
| 461 | .num_properties = ARRAY_SIZE(max14577_charger_props), |
| 462 | .get_property = max14577_charger_get_property, |
| 463 | }; |
| 464 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 465 | #ifdef CONFIG_OF |
| 466 | static struct max14577_charger_platform_data *max14577_charger_dt_init( |
| 467 | struct platform_device *pdev) |
| 468 | { |
| 469 | struct max14577_charger_platform_data *pdata; |
| 470 | struct device_node *np = pdev->dev.of_node; |
| 471 | int ret; |
| 472 | |
| 473 | if (!np) { |
| 474 | dev_err(&pdev->dev, "No charger OF node\n"); |
| 475 | return ERR_PTR(-EINVAL); |
| 476 | } |
| 477 | |
| 478 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 479 | if (!pdata) |
| 480 | return ERR_PTR(-ENOMEM); |
| 481 | |
| 482 | ret = of_property_read_u32(np, "maxim,constant-uvolt", |
| 483 | &pdata->constant_uvolt); |
| 484 | if (ret) { |
| 485 | dev_err(&pdev->dev, "Cannot parse maxim,constant-uvolt field from DT\n"); |
| 486 | return ERR_PTR(ret); |
| 487 | } |
| 488 | |
| 489 | ret = of_property_read_u32(np, "maxim,fast-charge-uamp", |
| 490 | &pdata->fast_charge_uamp); |
| 491 | if (ret) { |
| 492 | dev_err(&pdev->dev, "Cannot parse maxim,fast-charge-uamp field from DT\n"); |
| 493 | return ERR_PTR(ret); |
| 494 | } |
| 495 | |
| 496 | ret = of_property_read_u32(np, "maxim,eoc-uamp", &pdata->eoc_uamp); |
| 497 | if (ret) { |
| 498 | dev_err(&pdev->dev, "Cannot parse maxim,eoc-uamp field from DT\n"); |
| 499 | return ERR_PTR(ret); |
| 500 | } |
| 501 | |
| 502 | ret = of_property_read_u32(np, "maxim,ovp-uvolt", &pdata->ovp_uvolt); |
| 503 | if (ret) { |
| 504 | dev_err(&pdev->dev, "Cannot parse maxim,ovp-uvolt field from DT\n"); |
| 505 | return ERR_PTR(ret); |
| 506 | } |
| 507 | |
| 508 | return pdata; |
| 509 | } |
| 510 | #else /* CONFIG_OF */ |
| 511 | static struct max14577_charger_platform_data *max14577_charger_dt_init( |
| 512 | struct platform_device *pdev) |
| 513 | { |
| 514 | return NULL; |
| 515 | } |
| 516 | #endif /* CONFIG_OF */ |
| 517 | |
| 518 | static ssize_t show_fast_charge_timer(struct device *dev, |
| 519 | struct device_attribute *attr, char *buf) |
| 520 | { |
| 521 | struct max14577_charger *chg = dev_get_drvdata(dev); |
| 522 | u8 reg_data; |
| 523 | int ret; |
| 524 | unsigned int val; |
| 525 | |
| 526 | ret = max14577_read_reg(chg->max14577->regmap, MAX14577_REG_CHGCTRL1, |
| 527 | ®_data); |
| 528 | if (ret) |
| 529 | return ret; |
| 530 | |
| 531 | reg_data &= CHGCTRL1_TCHW_MASK; |
| 532 | reg_data >>= CHGCTRL1_TCHW_SHIFT; |
| 533 | switch (reg_data) { |
| 534 | case 0x2 ... 0x4: |
| 535 | val = reg_data + 3; |
| 536 | break; |
| 537 | case 0x7: |
| 538 | val = 0; |
| 539 | break; |
| 540 | default: |
| 541 | val = 5; |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | return scnprintf(buf, PAGE_SIZE, "%u\n", val); |
| 546 | } |
| 547 | |
| 548 | static ssize_t store_fast_charge_timer(struct device *dev, |
| 549 | struct device_attribute *attr, const char *buf, size_t count) |
| 550 | { |
| 551 | struct max14577_charger *chg = dev_get_drvdata(dev); |
| 552 | unsigned long val; |
| 553 | int ret; |
| 554 | |
| 555 | ret = kstrtoul(buf, 10, &val); |
| 556 | if (ret) |
| 557 | return ret; |
| 558 | |
| 559 | ret = max14577_set_fast_charge_timer(chg, val); |
| 560 | if (ret) |
| 561 | return ret; |
| 562 | |
| 563 | return count; |
| 564 | } |
| 565 | |
| 566 | static DEVICE_ATTR(fast_charge_timer, S_IRUGO | S_IWUSR, |
| 567 | show_fast_charge_timer, store_fast_charge_timer); |
| 568 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 569 | static int max14577_charger_probe(struct platform_device *pdev) |
| 570 | { |
| 571 | struct max14577_charger *chg; |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 572 | struct power_supply_config psy_cfg = {}; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 573 | struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent); |
| 574 | int ret; |
| 575 | |
| 576 | chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL); |
| 577 | if (!chg) |
| 578 | return -ENOMEM; |
| 579 | |
| 580 | platform_set_drvdata(pdev, chg); |
| 581 | chg->dev = &pdev->dev; |
| 582 | chg->max14577 = max14577; |
| 583 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 584 | chg->pdata = max14577_charger_dt_init(pdev); |
| 585 | if (IS_ERR_OR_NULL(chg->pdata)) |
| 586 | return PTR_ERR(chg->pdata); |
| 587 | |
| 588 | ret = max14577_charger_reg_init(chg); |
| 589 | if (ret) |
| 590 | return ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 591 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 592 | ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 593 | if (ret) { |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 594 | dev_err(&pdev->dev, "failed: create sysfs entry\n"); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 595 | return ret; |
| 596 | } |
| 597 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 598 | psy_cfg.drv_data = chg; |
| 599 | chg->charger = power_supply_register(&pdev->dev, &max14577_charger_desc, |
| 600 | &psy_cfg); |
| 601 | if (IS_ERR(chg->charger)) { |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 602 | dev_err(&pdev->dev, "failed: power supply register\n"); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 603 | ret = PTR_ERR(chg->charger); |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 604 | goto err; |
| 605 | } |
| 606 | |
| 607 | /* Check for valid values for charger */ |
| 608 | BUILD_BUG_ON(MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN + |
| 609 | MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP * 0xf != |
| 610 | MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 611 | return 0; |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 612 | |
| 613 | err: |
| 614 | device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); |
| 615 | |
| 616 | return ret; |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static int max14577_charger_remove(struct platform_device *pdev) |
| 620 | { |
| 621 | struct max14577_charger *chg = platform_get_drvdata(pdev); |
| 622 | |
Krzysztof Kozlowski | e30110e | 2014-09-12 08:53:57 +0200 | [diff] [blame] | 623 | device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer); |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 624 | power_supply_unregister(chg->charger); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 629 | static const struct platform_device_id max14577_charger_id[] = { |
| 630 | { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577, }, |
| 631 | { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836, }, |
| 632 | { } |
| 633 | }; |
| 634 | MODULE_DEVICE_TABLE(platform, max14577_charger_id); |
| 635 | |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 636 | static struct platform_driver max14577_charger_driver = { |
| 637 | .driver = { |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 638 | .name = "max14577-charger", |
| 639 | }, |
| 640 | .probe = max14577_charger_probe, |
| 641 | .remove = max14577_charger_remove, |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 642 | .id_table = max14577_charger_id, |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 643 | }; |
| 644 | module_platform_driver(max14577_charger_driver); |
| 645 | |
| 646 | MODULE_AUTHOR("Krzysztof Kozlowski <k.kozlowski@samsung.com>"); |
Krzysztof Kozlowski | 3682a8e | 2014-09-12 08:53:55 +0200 | [diff] [blame] | 647 | MODULE_DESCRIPTION("Maxim 14577/77836 charger driver"); |
Krzysztof Kozlowski | 55e6470 | 2013-12-06 12:32:12 +0100 | [diff] [blame] | 648 | MODULE_LICENSE("GPL"); |