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