Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 1 | /* |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 2 | * Copyright 2012 ST Ericsson. |
| 3 | * |
Lee Jones | 2fa5b0f | 2013-01-23 14:33:47 +0000 | [diff] [blame^] | 4 | * Power supply driver for ST Ericsson pm2xxx_charger charger |
| 5 | * |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/power_supply.h> |
| 19 | #include <linux/completion.h> |
| 20 | #include <linux/regulator/consumer.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/kobject.h> |
| 25 | #include <linux/mfd/abx500.h> |
| 26 | #include <linux/mfd/abx500/ab8500.h> |
| 27 | #include <linux/mfd/abx500/ab8500-bm.h> |
| 28 | #include <linux/mfd/abx500/ab8500-gpadc.h> |
| 29 | #include <linux/mfd/abx500/ux500_chargalg.h> |
| 30 | #include <linux/pm2301_charger.h> |
| 31 | |
Lee Jones | 2fa5b0f | 2013-01-23 14:33:47 +0000 | [diff] [blame^] | 32 | #include "pm2301_charger.h" |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 33 | |
| 34 | #define to_pm2xxx_charger_ac_device_info(x) container_of((x), \ |
| 35 | struct pm2xxx_charger, ac_chg) |
| 36 | |
| 37 | static int pm2xxx_interrupt_registers[] = { |
| 38 | PM2XXX_REG_INT1, |
| 39 | PM2XXX_REG_INT2, |
| 40 | PM2XXX_REG_INT3, |
| 41 | PM2XXX_REG_INT4, |
| 42 | PM2XXX_REG_INT5, |
| 43 | PM2XXX_REG_INT6, |
| 44 | }; |
| 45 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 46 | static enum power_supply_property pm2xxx_charger_ac_props[] = { |
| 47 | POWER_SUPPLY_PROP_HEALTH, |
| 48 | POWER_SUPPLY_PROP_PRESENT, |
| 49 | POWER_SUPPLY_PROP_ONLINE, |
| 50 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 51 | POWER_SUPPLY_PROP_VOLTAGE_AVG, |
| 52 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 53 | }; |
| 54 | |
| 55 | static int pm2xxx_charger_voltage_map[] = { |
| 56 | 3500, |
| 57 | 3525, |
| 58 | 3550, |
| 59 | 3575, |
| 60 | 3600, |
| 61 | 3625, |
| 62 | 3650, |
| 63 | 3675, |
| 64 | 3700, |
| 65 | 3725, |
| 66 | 3750, |
| 67 | 3775, |
| 68 | 3800, |
| 69 | 3825, |
| 70 | 3850, |
| 71 | 3875, |
| 72 | 3900, |
| 73 | 3925, |
| 74 | 3950, |
| 75 | 3975, |
| 76 | 4000, |
| 77 | 4025, |
| 78 | 4050, |
| 79 | 4075, |
| 80 | 4100, |
| 81 | 4125, |
| 82 | 4150, |
| 83 | 4175, |
| 84 | 4200, |
| 85 | 4225, |
| 86 | 4250, |
| 87 | 4275, |
| 88 | 4300, |
| 89 | }; |
| 90 | |
| 91 | static int pm2xxx_charger_current_map[] = { |
| 92 | 200, |
| 93 | 200, |
| 94 | 400, |
| 95 | 600, |
| 96 | 800, |
| 97 | 1000, |
| 98 | 1200, |
| 99 | 1400, |
| 100 | 1600, |
| 101 | 1800, |
| 102 | 2000, |
| 103 | 2200, |
| 104 | 2400, |
| 105 | 2600, |
| 106 | 2800, |
| 107 | 3000, |
| 108 | }; |
| 109 | |
Michel JAOUEN | 01ec8c5 | 2012-04-26 10:00:04 +0200 | [diff] [blame] | 110 | static const struct i2c_device_id pm2xxx_ident[] = { |
| 111 | { "pm2301", 0 }, |
| 112 | { } |
| 113 | }; |
| 114 | |
| 115 | static int pm2xxx_reg_read(struct pm2xxx_charger *pm2, int reg, u8 *val) |
| 116 | { |
| 117 | int ret; |
| 118 | |
| 119 | ret = i2c_smbus_read_i2c_block_data(pm2->config.pm2xxx_i2c, reg, |
| 120 | 1, val); |
| 121 | if (ret < 0) |
| 122 | dev_err(pm2->dev, "Error reading register at 0x%x\n", reg); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static int pm2xxx_reg_write(struct pm2xxx_charger *pm2, int reg, u8 val) |
| 128 | { |
| 129 | int ret; |
| 130 | |
| 131 | ret = i2c_smbus_write_i2c_block_data(pm2->config.pm2xxx_i2c, reg, |
| 132 | 1, &val); |
| 133 | if (ret < 0) |
| 134 | dev_err(pm2->dev, "Error writing register at 0x%x\n", reg); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static int pm2xxx_charging_enable_mngt(struct pm2xxx_charger *pm2) |
| 140 | { |
| 141 | int ret; |
| 142 | |
| 143 | /* Enable charging */ |
| 144 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2, |
| 145 | (PM2XXX_CH_AUTO_RESUME_EN | PM2XXX_CHARGER_ENA)); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static int pm2xxx_charging_disable_mngt(struct pm2xxx_charger *pm2) |
| 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | /* Disable charging */ |
| 155 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG2, |
| 156 | (PM2XXX_CH_AUTO_RESUME_DIS | PM2XXX_CHARGER_DIS)); |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int pm2xxx_charger_batt_therm_mngt(struct pm2xxx_charger *pm2, int val) |
| 162 | { |
| 163 | queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | int pm2xxx_charger_die_therm_mngt(struct pm2xxx_charger *pm2, int val) |
| 170 | { |
| 171 | queue_work(pm2->charger_wq, &pm2->check_main_thermal_prot_work); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int pm2xxx_charger_ovv_mngt(struct pm2xxx_charger *pm2, int val) |
| 177 | { |
| 178 | int ret = 0; |
| 179 | |
| 180 | pm2->failure_input_ovv++; |
| 181 | if (pm2->failure_input_ovv < 4) { |
| 182 | ret = pm2xxx_charging_enable_mngt(pm2); |
| 183 | goto out; |
| 184 | } else { |
| 185 | pm2->failure_input_ovv = 0; |
| 186 | dev_err(pm2->dev, "Overvoltage detected\n"); |
| 187 | pm2->flags.ovv = true; |
| 188 | power_supply_changed(&pm2->ac_chg.psy); |
| 189 | } |
| 190 | |
| 191 | out: |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static int pm2xxx_charger_wd_exp_mngt(struct pm2xxx_charger *pm2, int val) |
| 196 | { |
| 197 | dev_dbg(pm2->dev , "20 minutes watchdog occured\n"); |
| 198 | |
| 199 | pm2->ac.wd_expired = true; |
| 200 | power_supply_changed(&pm2->ac_chg.psy); |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int pm2xxx_charger_vbat_lsig_mngt(struct pm2xxx_charger *pm2, int val) |
| 206 | { |
| 207 | switch (val) { |
| 208 | case PM2XXX_INT1_ITVBATLOWR: |
| 209 | dev_dbg(pm2->dev, "VBAT grows above VBAT_LOW level\n"); |
| 210 | break; |
| 211 | |
| 212 | case PM2XXX_INT1_ITVBATLOWF: |
| 213 | dev_dbg(pm2->dev, "VBAT drops below VBAT_LOW level\n"); |
| 214 | break; |
| 215 | |
| 216 | default: |
| 217 | dev_err(pm2->dev, "Unknown VBAT level\n"); |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int pm2xxx_charger_bat_disc_mngt(struct pm2xxx_charger *pm2, int val) |
| 224 | { |
| 225 | dev_dbg(pm2->dev, "battery disconnected\n"); |
| 226 | |
| 227 | return (pm2xxx_charging_disable_mngt(pm2)); |
| 228 | } |
| 229 | |
| 230 | static int pm2xxx_charger_detection(struct pm2xxx_charger *pm2, u8 *val) |
| 231 | { |
| 232 | int ret = 0; |
| 233 | |
| 234 | ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT2, val); |
| 235 | |
| 236 | if (ret < 0) { |
| 237 | dev_err(pm2->dev, "Charger detection failed\n"); |
| 238 | goto out; |
| 239 | } |
| 240 | |
| 241 | *val &= (PM2XXX_INT2_S_ITVPWR1PLUG | PM2XXX_INT2_S_ITVPWR2PLUG); |
| 242 | out: |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | static int pm2xxx_charger_itv_pwr_plug_mngt(struct pm2xxx_charger *pm2, int val) |
| 247 | { |
| 248 | |
| 249 | int ret; |
| 250 | u8 read_val; |
| 251 | |
| 252 | /* |
| 253 | * Since we can't be sure that the events are received |
| 254 | * synchronously, we have the check if the main charger is |
| 255 | * connected by reading the interrupt source register. |
| 256 | */ |
| 257 | ret = pm2xxx_charger_detection(pm2, &read_val); |
| 258 | |
| 259 | if ((ret == 0) && read_val) { |
| 260 | pm2->ac.charger_connected = 1; |
| 261 | pm2->ac_conn = true; |
| 262 | queue_work(pm2->charger_wq, &pm2->ac_work); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static int pm2xxx_charger_itv_pwr_unplug_mngt(struct pm2xxx_charger *pm2, |
| 270 | int val) |
| 271 | { |
| 272 | pm2->ac.charger_connected = 0; |
| 273 | queue_work(pm2->charger_wq, &pm2->ac_work); |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int pm2_int_reg0(struct pm2xxx_charger *pm2) |
| 279 | { |
| 280 | int ret = 0; |
| 281 | |
| 282 | if (pm2->pm2_int[0] & |
| 283 | (PM2XXX_INT1_ITVBATLOWR | PM2XXX_INT1_ITVBATLOWF)) { |
| 284 | ret = pm2xxx_charger_vbat_lsig_mngt(pm2, pm2->pm2_int[0] & |
| 285 | (PM2XXX_INT1_ITVBATLOWR | PM2XXX_INT1_ITVBATLOWF)); |
| 286 | } |
| 287 | |
| 288 | if (pm2->pm2_int[0] & PM2XXX_INT1_ITVBATDISCONNECT) { |
| 289 | ret = pm2xxx_charger_bat_disc_mngt(pm2, |
| 290 | PM2XXX_INT1_ITVBATDISCONNECT); |
| 291 | } |
| 292 | |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int pm2_int_reg1(struct pm2xxx_charger *pm2) |
| 297 | { |
| 298 | int ret = 0; |
| 299 | |
| 300 | if (pm2->pm2_int[1] & |
| 301 | (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)) { |
| 302 | dev_dbg(pm2->dev , "Main charger plugged\n"); |
| 303 | ret = pm2xxx_charger_itv_pwr_plug_mngt(pm2, pm2->pm2_int[1] & |
| 304 | (PM2XXX_INT2_ITVPWR1PLUG | PM2XXX_INT2_ITVPWR2PLUG)); |
| 305 | } |
| 306 | |
| 307 | if (pm2->pm2_int[1] & |
| 308 | (PM2XXX_INT2_ITVPWR1UNPLUG | PM2XXX_INT2_ITVPWR2UNPLUG)) { |
| 309 | dev_dbg(pm2->dev , "Main charger unplugged\n"); |
| 310 | ret = pm2xxx_charger_itv_pwr_unplug_mngt(pm2, pm2->pm2_int[1] & |
| 311 | (PM2XXX_INT2_ITVPWR1UNPLUG | |
| 312 | PM2XXX_INT2_ITVPWR2UNPLUG)); |
| 313 | } |
| 314 | |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | static int pm2_int_reg2(struct pm2xxx_charger *pm2) |
| 319 | { |
| 320 | int ret = 0; |
| 321 | |
| 322 | if (pm2->pm2_int[2] & PM2XXX_INT3_ITAUTOTIMEOUTWD) |
| 323 | ret = pm2xxx_charger_wd_exp_mngt(pm2, pm2->pm2_int[2]); |
| 324 | |
| 325 | if (pm2->pm2_int[2] & (PM2XXX_INT3_ITCHPRECHARGEWD | |
| 326 | PM2XXX_INT3_ITCHCCWD | PM2XXX_INT3_ITCHCVWD)) { |
| 327 | dev_dbg(pm2->dev, |
| 328 | "Watchdog occured for precharge, CC and CV charge\n"); |
| 329 | } |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static int pm2_int_reg3(struct pm2xxx_charger *pm2) |
| 335 | { |
| 336 | int ret = 0; |
| 337 | |
| 338 | if (pm2->pm2_int[3] & (PM2XXX_INT4_ITCHARGINGON)) { |
| 339 | dev_dbg(pm2->dev , |
| 340 | "chargind operation has started\n"); |
| 341 | } |
| 342 | |
| 343 | if (pm2->pm2_int[3] & (PM2XXX_INT4_ITVRESUME)) { |
| 344 | dev_dbg(pm2->dev, |
| 345 | "battery discharged down to VResume threshold\n"); |
| 346 | } |
| 347 | |
| 348 | if (pm2->pm2_int[3] & (PM2XXX_INT4_ITBATTFULL)) { |
| 349 | dev_dbg(pm2->dev , "battery fully detected\n"); |
| 350 | } |
| 351 | |
| 352 | if (pm2->pm2_int[3] & (PM2XXX_INT4_ITCVPHASE)) { |
| 353 | dev_dbg(pm2->dev, "CV phase enter with 0.5C charging\n"); |
| 354 | } |
| 355 | |
| 356 | if (pm2->pm2_int[3] & |
| 357 | (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)) { |
| 358 | pm2->failure_case = VPWR_OVV; |
| 359 | ret = pm2xxx_charger_ovv_mngt(pm2, pm2->pm2_int[3] & |
| 360 | (PM2XXX_INT4_ITVPWR2OVV | PM2XXX_INT4_ITVPWR1OVV)); |
| 361 | dev_dbg(pm2->dev, "VPWR/VSYSTEM overvoltage detected\n"); |
| 362 | } |
| 363 | |
| 364 | if (pm2->pm2_int[3] & (PM2XXX_INT4_S_ITBATTEMPCOLD | |
| 365 | PM2XXX_INT4_S_ITBATTEMPHOT)) { |
| 366 | ret = pm2xxx_charger_batt_therm_mngt(pm2, |
| 367 | pm2->pm2_int[3] & (PM2XXX_INT4_S_ITBATTEMPCOLD | |
| 368 | PM2XXX_INT4_S_ITBATTEMPHOT)); |
| 369 | dev_dbg(pm2->dev, "BTEMP is too Low/High\n"); |
| 370 | } |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static int pm2_int_reg4(struct pm2xxx_charger *pm2) |
| 376 | { |
| 377 | int ret = 0; |
| 378 | |
| 379 | if (pm2->pm2_int[4] & PM2XXX_INT5_ITVSYSTEMOVV) { |
| 380 | pm2->failure_case = VSYSTEM_OVV; |
| 381 | ret = pm2xxx_charger_ovv_mngt(pm2, pm2->pm2_int[4] & |
| 382 | PM2XXX_INT5_ITVSYSTEMOVV); |
| 383 | dev_dbg(pm2->dev, "VSYSTEM overvoltage detected\n"); |
| 384 | } |
| 385 | |
| 386 | if (pm2->pm2_int[4] & (PM2XXX_INT5_ITTHERMALWARNINGFALL | |
| 387 | PM2XXX_INT5_ITTHERMALWARNINGRISE | |
| 388 | PM2XXX_INT5_ITTHERMALSHUTDOWNFALL | |
| 389 | PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)) { |
| 390 | dev_dbg(pm2->dev, "BTEMP die temperature is too Low/High\n"); |
| 391 | ret = pm2xxx_charger_die_therm_mngt(pm2, pm2->pm2_int[4] & |
| 392 | (PM2XXX_INT5_ITTHERMALWARNINGFALL | |
| 393 | PM2XXX_INT5_ITTHERMALWARNINGRISE | |
| 394 | PM2XXX_INT5_ITTHERMALSHUTDOWNFALL | |
| 395 | PM2XXX_INT5_ITTHERMALSHUTDOWNRISE)); |
| 396 | } |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static int pm2_int_reg5(struct pm2xxx_charger *pm2) |
| 402 | { |
| 403 | |
| 404 | if (pm2->pm2_int[5] |
| 405 | & (PM2XXX_INT6_ITVPWR2DROP | PM2XXX_INT6_ITVPWR1DROP)) { |
| 406 | dev_dbg(pm2->dev, "VMPWR drop to VBAT level\n"); |
| 407 | } |
| 408 | |
| 409 | if (pm2->pm2_int[5] & (PM2XXX_INT6_ITVPWR2VALIDRISE | |
| 410 | PM2XXX_INT6_ITVPWR1VALIDRISE | |
| 411 | PM2XXX_INT6_ITVPWR2VALIDFALL | |
| 412 | PM2XXX_INT6_ITVPWR1VALIDFALL)) { |
| 413 | dev_dbg(pm2->dev, "Falling/Rising edge on WPWR1/2\n"); |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static irqreturn_t pm2xxx_irq_int(int irq, void *data) |
| 420 | { |
| 421 | struct pm2xxx_charger *pm2 = data; |
| 422 | int ret, i; |
| 423 | |
| 424 | for (i = 0; i < ARRAY_SIZE(pm2->pm2_int); i++) { |
| 425 | ret = pm2xxx_reg_read(pm2, pm2xxx_interrupt_registers[i], |
| 426 | &(pm2->pm2_int[i])); |
| 427 | } |
| 428 | |
| 429 | pm2_int_reg0(pm2); |
| 430 | pm2_int_reg1(pm2); |
| 431 | pm2_int_reg2(pm2); |
| 432 | pm2_int_reg3(pm2); |
| 433 | pm2_int_reg4(pm2); |
| 434 | pm2_int_reg5(pm2); |
| 435 | |
| 436 | return IRQ_HANDLED; |
| 437 | } |
| 438 | |
| 439 | static int pm2xxx_charger_get_ac_voltage(struct pm2xxx_charger *pm2) |
| 440 | { |
| 441 | int vch = 0; |
| 442 | |
| 443 | if (pm2->ac.charger_connected) { |
| 444 | vch = ab8500_gpadc_convert(pm2->gpadc, MAIN_CHARGER_V); |
| 445 | if (vch < 0) |
| 446 | dev_err(pm2->dev, "%s gpadc conv failed,\n", __func__); |
| 447 | } |
| 448 | |
| 449 | return vch; |
| 450 | } |
| 451 | |
| 452 | static int pm2xxx_charger_get_ac_cv(struct pm2xxx_charger *pm2) |
| 453 | { |
| 454 | int ret = 0; |
| 455 | u8 val; |
| 456 | |
| 457 | if (pm2->ac.charger_connected && pm2->ac.charger_online) { |
| 458 | |
| 459 | ret = pm2xxx_reg_read(pm2, PM2XXX_SRCE_REG_INT4, &val); |
| 460 | if (ret < 0) { |
| 461 | dev_err(pm2->dev, "%s pm2xxx read failed\n", __func__); |
| 462 | goto out; |
| 463 | } |
| 464 | |
| 465 | if (val & PM2XXX_INT4_S_ITCVPHASE) |
| 466 | ret = PM2XXX_CONST_VOLT; |
| 467 | else |
| 468 | ret = PM2XXX_CONST_CURR; |
| 469 | } |
| 470 | out: |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | static int pm2xxx_charger_get_ac_current(struct pm2xxx_charger *pm2) |
| 475 | { |
| 476 | int ich = 0; |
| 477 | |
| 478 | if (pm2->ac.charger_online) { |
| 479 | ich = ab8500_gpadc_convert(pm2->gpadc, MAIN_CHARGER_C); |
| 480 | if (ich < 0) |
| 481 | dev_err(pm2->dev, "%s gpadc conv failed\n", __func__); |
| 482 | } |
| 483 | |
| 484 | return ich; |
| 485 | } |
| 486 | |
| 487 | static int pm2xxx_current_to_regval(int curr) |
| 488 | { |
| 489 | int i; |
| 490 | |
| 491 | if (curr < pm2xxx_charger_current_map[0]) |
| 492 | return 0; |
| 493 | |
| 494 | for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_current_map); i++) { |
| 495 | if (curr < pm2xxx_charger_current_map[i]) |
| 496 | return (i - 1); |
| 497 | } |
| 498 | |
| 499 | i = ARRAY_SIZE(pm2xxx_charger_current_map) - 1; |
| 500 | if (curr == pm2xxx_charger_current_map[i]) |
| 501 | return i; |
| 502 | else |
| 503 | return -EINVAL; |
| 504 | } |
| 505 | |
| 506 | static int pm2xxx_voltage_to_regval(int curr) |
| 507 | { |
| 508 | int i; |
| 509 | |
| 510 | if (curr < pm2xxx_charger_voltage_map[0]) |
| 511 | return 0; |
| 512 | |
| 513 | for (i = 1; i < ARRAY_SIZE(pm2xxx_charger_voltage_map); i++) { |
| 514 | if (curr < pm2xxx_charger_voltage_map[i]) |
| 515 | return i - 1; |
| 516 | } |
| 517 | |
| 518 | i = ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1; |
| 519 | if (curr == pm2xxx_charger_voltage_map[i]) |
| 520 | return i; |
| 521 | else |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
| 525 | static int pm2xxx_charger_update_charger_current(struct ux500_charger *charger, |
| 526 | int ich_out) |
| 527 | { |
| 528 | int ret; |
| 529 | int curr_index; |
| 530 | struct pm2xxx_charger *pm2; |
| 531 | u8 val; |
| 532 | |
| 533 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) |
| 534 | pm2 = to_pm2xxx_charger_ac_device_info(charger); |
| 535 | else |
| 536 | return -ENXIO; |
| 537 | |
| 538 | curr_index = pm2xxx_current_to_regval(ich_out); |
| 539 | if (curr_index < 0) { |
| 540 | dev_err(pm2->dev, |
| 541 | "Charger current too high: charging not started\n"); |
| 542 | return -ENXIO; |
| 543 | } |
| 544 | |
| 545 | ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val); |
| 546 | if (ret >= 0) { |
| 547 | val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK; |
| 548 | val |= curr_index; |
| 549 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val); |
| 550 | if (ret < 0) { |
| 551 | dev_err(pm2->dev, |
| 552 | "%s write failed\n", __func__); |
| 553 | } |
| 554 | } |
| 555 | else |
| 556 | dev_err(pm2->dev, "%s read failed\n", __func__); |
| 557 | |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | static int pm2xxx_charger_ac_get_property(struct power_supply *psy, |
| 562 | enum power_supply_property psp, |
| 563 | union power_supply_propval *val) |
| 564 | { |
| 565 | struct pm2xxx_charger *pm2; |
| 566 | |
| 567 | pm2 = to_pm2xxx_charger_ac_device_info(psy_to_ux500_charger(psy)); |
| 568 | |
| 569 | switch (psp) { |
| 570 | case POWER_SUPPLY_PROP_HEALTH: |
| 571 | if (pm2->flags.mainextchnotok) |
| 572 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; |
| 573 | else if (pm2->ac.wd_expired) |
| 574 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
| 575 | else if (pm2->flags.main_thermal_prot) |
| 576 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; |
| 577 | else |
| 578 | val->intval = POWER_SUPPLY_HEALTH_GOOD; |
| 579 | break; |
| 580 | case POWER_SUPPLY_PROP_ONLINE: |
| 581 | val->intval = pm2->ac.charger_online; |
| 582 | break; |
| 583 | case POWER_SUPPLY_PROP_PRESENT: |
| 584 | val->intval = pm2->ac.charger_connected; |
| 585 | break; |
| 586 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 587 | pm2->ac.charger_voltage = pm2xxx_charger_get_ac_voltage(pm2); |
| 588 | val->intval = pm2->ac.charger_voltage * 1000; |
| 589 | break; |
| 590 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
| 591 | pm2->ac.cv_active = pm2xxx_charger_get_ac_cv(pm2); |
| 592 | val->intval = pm2->ac.cv_active; |
| 593 | break; |
| 594 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 595 | val->intval = pm2xxx_charger_get_ac_current(pm2) * 1000; |
| 596 | break; |
| 597 | default: |
| 598 | return -EINVAL; |
| 599 | } |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static int pm2xxx_charging_init(struct pm2xxx_charger *pm2) |
| 604 | { |
| 605 | int ret = 0; |
| 606 | |
| 607 | /* enable CC and CV watchdog */ |
| 608 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG3, |
| 609 | (PM2XXX_CH_WD_CV_PHASE_60MIN | PM2XXX_CH_WD_CC_PHASE_60MIN)); |
| 610 | if( ret < 0) |
| 611 | return ret; |
| 612 | |
| 613 | /* enable precharge watchdog */ |
| 614 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG4, |
| 615 | PM2XXX_CH_WD_PRECH_PHASE_60MIN); |
| 616 | |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | static int pm2xxx_charger_ac_en(struct ux500_charger *charger, |
| 621 | int enable, int vset, int iset) |
| 622 | { |
| 623 | int ret; |
| 624 | int volt_index; |
| 625 | int curr_index; |
| 626 | u8 val; |
| 627 | |
| 628 | struct pm2xxx_charger *pm2 = to_pm2xxx_charger_ac_device_info(charger); |
| 629 | |
| 630 | if (enable) { |
| 631 | if (!pm2->ac.charger_connected) { |
| 632 | dev_dbg(pm2->dev, "AC charger not connected\n"); |
| 633 | return -ENXIO; |
| 634 | } |
| 635 | |
| 636 | dev_dbg(pm2->dev, "Enable AC: %dmV %dmA\n", vset, iset); |
| 637 | if (!pm2->vddadc_en_ac) { |
| 638 | regulator_enable(pm2->regu); |
| 639 | pm2->vddadc_en_ac = true; |
| 640 | } |
| 641 | |
| 642 | ret = pm2xxx_charging_init(pm2); |
| 643 | if (ret < 0) { |
| 644 | dev_err(pm2->dev, "%s charging init failed\n", |
| 645 | __func__); |
| 646 | goto error_occured; |
| 647 | } |
| 648 | |
| 649 | volt_index = pm2xxx_voltage_to_regval(vset); |
| 650 | curr_index = pm2xxx_current_to_regval(iset); |
| 651 | |
| 652 | if (volt_index < 0 || curr_index < 0) { |
| 653 | dev_err(pm2->dev, |
| 654 | "Charger voltage or current too high, " |
| 655 | "charging not started\n"); |
| 656 | return -ENXIO; |
| 657 | } |
| 658 | |
| 659 | ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG8, &val); |
| 660 | if (ret >= 0) { |
| 661 | val &= ~PM2XXX_CH_VOLT_MASK; |
| 662 | val |= volt_index; |
| 663 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG8, val); |
| 664 | |
| 665 | if (ret < 0) { |
| 666 | dev_err(pm2->dev, |
| 667 | "%s write failed\n", __func__); |
| 668 | goto error_occured; |
| 669 | } |
| 670 | else |
| 671 | dev_err(pm2->dev, "%s read failed\n", __func__); |
| 672 | } |
| 673 | |
| 674 | ret = pm2xxx_reg_read(pm2, PM2XXX_BATT_CTRL_REG6, &val); |
| 675 | if (ret >= 0) { |
| 676 | val &= ~PM2XXX_DIR_CH_CC_CURRENT_MASK; |
| 677 | val |= curr_index; |
| 678 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_CTRL_REG6, val); |
| 679 | if (ret < 0) { |
| 680 | dev_err(pm2->dev, |
| 681 | "%s write failed\n", __func__); |
| 682 | goto error_occured; |
| 683 | } |
| 684 | else |
| 685 | dev_err(pm2->dev, "%s read failed\n", __func__); |
| 686 | } |
| 687 | |
| 688 | if (!pm2->bat->enable_overshoot) { |
| 689 | ret = pm2xxx_reg_read(pm2, PM2XXX_LED_CTRL_REG, &val); |
| 690 | if (ret >= 0) { |
| 691 | val |= PM2XXX_ANTI_OVERSHOOT_EN; |
| 692 | ret = pm2xxx_reg_write(pm2, PM2XXX_LED_CTRL_REG, |
| 693 | val); |
| 694 | if (ret < 0){ |
| 695 | dev_err(pm2->dev, "%s write failed\n", |
| 696 | __func__); |
| 697 | goto error_occured; |
| 698 | } |
| 699 | } |
| 700 | else |
| 701 | dev_err(pm2->dev, "%s read failed\n", __func__); |
| 702 | } |
| 703 | |
| 704 | ret = pm2xxx_charging_enable_mngt(pm2); |
| 705 | if (ret) { |
| 706 | dev_err(pm2->dev, "%s write failed\n", __func__); |
| 707 | goto error_occured; |
| 708 | } |
| 709 | |
| 710 | pm2->ac.charger_online = 1; |
| 711 | } else { |
| 712 | pm2->ac.charger_online = 0; |
| 713 | pm2->ac.wd_expired = false; |
| 714 | |
| 715 | /* Disable regulator if enabled */ |
| 716 | if (pm2->vddadc_en_ac) { |
| 717 | regulator_disable(pm2->regu); |
| 718 | pm2->vddadc_en_ac = false; |
| 719 | } |
| 720 | |
| 721 | ret = pm2xxx_charging_disable_mngt(pm2); |
| 722 | if (ret) { |
| 723 | dev_err(pm2->dev, "%s write failed\n", __func__); |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | dev_dbg(pm2->dev, "PM2301: " "Disabled AC charging\n"); |
| 728 | } |
| 729 | power_supply_changed(&pm2->ac_chg.psy); |
| 730 | |
| 731 | error_occured: |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | static int pm2xxx_charger_watchdog_kick(struct ux500_charger *charger) |
| 736 | { |
| 737 | int ret; |
| 738 | struct pm2xxx_charger *pm2; |
| 739 | |
| 740 | if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) |
| 741 | pm2 = to_pm2xxx_charger_ac_device_info(charger); |
| 742 | else |
| 743 | return -ENXIO; |
| 744 | |
| 745 | ret = pm2xxx_reg_write(pm2, PM2XXX_BATT_WD_KICK, WD_TIMER); |
| 746 | if (ret) |
| 747 | dev_err(pm2->dev, "Failed to kick WD!\n"); |
| 748 | |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | static void pm2xxx_charger_ac_work(struct work_struct *work) |
| 753 | { |
| 754 | struct pm2xxx_charger *pm2 = container_of(work, |
| 755 | struct pm2xxx_charger, ac_work); |
| 756 | |
| 757 | |
| 758 | power_supply_changed(&pm2->ac_chg.psy); |
| 759 | sysfs_notify(&pm2->ac_chg.psy.dev->kobj, NULL, "present"); |
| 760 | }; |
| 761 | |
| 762 | static void pm2xxx_charger_check_main_thermal_prot_work( |
| 763 | struct work_struct *work) |
| 764 | { |
| 765 | }; |
| 766 | |
| 767 | static struct pm2xxx_irq pm2xxx_charger_irq[] = { |
| 768 | {"PM2XXX_IRQ_INT", pm2xxx_irq_int}, |
| 769 | }; |
| 770 | |
| 771 | static int pm2xxx_wall_charger_resume(struct i2c_client *i2c_client) |
| 772 | { |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static int pm2xxx_wall_charger_suspend(struct i2c_client *i2c_client, |
| 777 | pm_message_t state) |
| 778 | { |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int __devinit pm2xxx_wall_charger_probe(struct i2c_client *i2c_client, |
| 783 | const struct i2c_device_id *id) |
| 784 | { |
| 785 | struct pm2xxx_platform_data *pl_data = i2c_client->dev.platform_data; |
| 786 | struct pm2xxx_charger *pm2; |
| 787 | int ret = 0; |
| 788 | u8 val; |
| 789 | |
| 790 | pm2 = kzalloc(sizeof(struct pm2xxx_charger), GFP_KERNEL); |
| 791 | if (!pm2) { |
| 792 | dev_err(pm2->dev, "pm2xxx_charger allocation failed\n"); |
| 793 | return -ENOMEM; |
| 794 | } |
| 795 | |
| 796 | /* get parent data */ |
| 797 | pm2->dev = &i2c_client->dev; |
| 798 | pm2->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); |
| 799 | |
| 800 | /* get charger spcific platform data */ |
| 801 | if (!pl_data->wall_charger) { |
| 802 | dev_err(pm2->dev, "no charger platform data supplied\n"); |
| 803 | ret = -EINVAL; |
| 804 | goto free_device_info; |
| 805 | } |
| 806 | |
| 807 | pm2->pdata = pl_data->wall_charger; |
| 808 | |
| 809 | /* get battery specific platform data */ |
| 810 | if (!pl_data->battery) { |
| 811 | dev_err(pm2->dev, "no battery platform data supplied\n"); |
| 812 | ret = -EINVAL; |
| 813 | goto free_device_info; |
| 814 | } |
| 815 | |
| 816 | pm2->bat = pl_data->battery; |
| 817 | |
| 818 | if (!i2c_check_functionality(i2c_client->adapter, |
| 819 | I2C_FUNC_SMBUS_BYTE_DATA | |
| 820 | I2C_FUNC_SMBUS_READ_WORD_DATA)) { |
| 821 | ret = -ENODEV; |
| 822 | dev_info(pm2->dev, "pm2301 i2c_check_functionality failed\n"); |
| 823 | goto free_device_info; |
| 824 | } |
| 825 | |
| 826 | pm2->config.pm2xxx_i2c = i2c_client; |
| 827 | pm2->config.pm2xxx_id = (struct i2c_device_id *) id; |
| 828 | i2c_set_clientdata(i2c_client, pm2); |
| 829 | |
| 830 | /* AC supply */ |
| 831 | /* power_supply base class */ |
| 832 | pm2->ac_chg.psy.name = pm2->pdata->label; |
| 833 | pm2->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS; |
| 834 | pm2->ac_chg.psy.properties = pm2xxx_charger_ac_props; |
| 835 | pm2->ac_chg.psy.num_properties = ARRAY_SIZE(pm2xxx_charger_ac_props); |
| 836 | pm2->ac_chg.psy.get_property = pm2xxx_charger_ac_get_property; |
| 837 | pm2->ac_chg.psy.supplied_to = pm2->pdata->supplied_to; |
| 838 | pm2->ac_chg.psy.num_supplicants = pm2->pdata->num_supplicants; |
| 839 | /* pm2xxx_charger sub-class */ |
| 840 | pm2->ac_chg.ops.enable = &pm2xxx_charger_ac_en; |
| 841 | pm2->ac_chg.ops.kick_wd = &pm2xxx_charger_watchdog_kick; |
| 842 | pm2->ac_chg.ops.update_curr = &pm2xxx_charger_update_charger_current; |
| 843 | pm2->ac_chg.max_out_volt = pm2xxx_charger_voltage_map[ |
| 844 | ARRAY_SIZE(pm2xxx_charger_voltage_map) - 1]; |
| 845 | pm2->ac_chg.max_out_curr = pm2xxx_charger_current_map[ |
| 846 | ARRAY_SIZE(pm2xxx_charger_current_map) - 1]; |
| 847 | |
| 848 | /* Create a work queue for the charger */ |
| 849 | pm2->charger_wq = |
| 850 | create_singlethread_workqueue("pm2xxx_charger_wq"); |
| 851 | if (pm2->charger_wq == NULL) { |
| 852 | dev_err(pm2->dev, "failed to create work queue\n"); |
| 853 | goto free_device_info; |
| 854 | } |
| 855 | |
| 856 | /* Init work for charger detection */ |
| 857 | INIT_WORK(&pm2->ac_work, pm2xxx_charger_ac_work); |
| 858 | |
| 859 | /* Init work for checking HW status */ |
| 860 | INIT_WORK(&pm2->check_main_thermal_prot_work, |
| 861 | pm2xxx_charger_check_main_thermal_prot_work); |
| 862 | |
| 863 | /* |
| 864 | * VDD ADC supply needs to be enabled from this driver when there |
| 865 | * is a charger connected to avoid erroneous BTEMP_HIGH/LOW |
| 866 | * interrupts during charging |
| 867 | */ |
| 868 | pm2->regu = regulator_get(pm2->dev, "vddadc"); |
| 869 | if (IS_ERR(pm2->regu)) { |
| 870 | ret = PTR_ERR(pm2->regu); |
| 871 | dev_err(pm2->dev, "failed to get vddadc regulator\n"); |
| 872 | goto free_charger_wq; |
| 873 | } |
| 874 | |
| 875 | /* Register AC charger class */ |
| 876 | ret = power_supply_register(pm2->dev, &pm2->ac_chg.psy); |
| 877 | if (ret) { |
| 878 | dev_err(pm2->dev, "failed to register AC charger\n"); |
| 879 | goto free_regulator; |
| 880 | } |
| 881 | |
| 882 | /* Register interrupts */ |
| 883 | ret = request_threaded_irq(pm2->pdata->irq_number, NULL, |
| 884 | pm2xxx_charger_irq[0].isr, |
| 885 | pm2->pdata->irq_type, |
| 886 | pm2xxx_charger_irq[0].name, pm2); |
| 887 | |
| 888 | if (ret != 0) { |
| 889 | dev_err(pm2->dev, "failed to request %s IRQ %d: %d\n", |
| 890 | pm2xxx_charger_irq[0].name, pm2->pdata->irq_number, ret); |
| 891 | goto unregister_pm2xxx_charger; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * I2C Read/Write will fail, if AC adaptor is not connected. |
| 896 | * fix the charger detection mechanism. |
| 897 | */ |
| 898 | ret = pm2xxx_charger_detection(pm2, &val); |
| 899 | |
| 900 | if ((ret == 0) && val) { |
| 901 | pm2->ac.charger_connected = 1; |
| 902 | pm2->ac_conn = true; |
| 903 | power_supply_changed(&pm2->ac_chg.psy); |
| 904 | sysfs_notify(&pm2->ac_chg.psy.dev->kobj, NULL, "present"); |
| 905 | } |
| 906 | |
| 907 | return 0; |
| 908 | |
| 909 | unregister_pm2xxx_charger: |
| 910 | /* unregister power supply */ |
| 911 | power_supply_unregister(&pm2->ac_chg.psy); |
| 912 | free_regulator: |
| 913 | /* disable the regulator */ |
| 914 | regulator_put(pm2->regu); |
| 915 | free_charger_wq: |
| 916 | destroy_workqueue(pm2->charger_wq); |
| 917 | free_device_info: |
| 918 | kfree(pm2); |
| 919 | return ret; |
| 920 | } |
| 921 | |
| 922 | static int __devexit pm2xxx_wall_charger_remove(struct i2c_client *i2c_client) |
| 923 | { |
| 924 | struct pm2xxx_charger *pm2 = i2c_get_clientdata(i2c_client); |
| 925 | |
| 926 | /* Disable AC charging */ |
| 927 | pm2xxx_charger_ac_en(&pm2->ac_chg, false, 0, 0); |
| 928 | |
| 929 | /* Disable interrupts */ |
| 930 | free_irq(pm2->pdata->irq_number, pm2); |
| 931 | |
| 932 | /* Delete the work queue */ |
| 933 | destroy_workqueue(pm2->charger_wq); |
| 934 | |
| 935 | flush_scheduled_work(); |
| 936 | |
| 937 | /* disable the regulator */ |
| 938 | regulator_put(pm2->regu); |
| 939 | |
| 940 | power_supply_unregister(&pm2->ac_chg.psy); |
| 941 | |
| 942 | kfree(pm2); |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static const struct i2c_device_id pm2xxx_id[] = { |
| 948 | { "pm2301", 0 }, |
| 949 | { } |
| 950 | }; |
| 951 | |
| 952 | MODULE_DEVICE_TABLE(i2c, pm2xxx_id); |
| 953 | |
| 954 | static struct i2c_driver pm2xxx_charger_driver = { |
| 955 | .probe = pm2xxx_wall_charger_probe, |
| 956 | .remove = __devexit_p(pm2xxx_wall_charger_remove), |
| 957 | .suspend = pm2xxx_wall_charger_suspend, |
| 958 | .resume = pm2xxx_wall_charger_resume, |
| 959 | .driver = { |
| 960 | .name = "pm2xxx-wall_charger", |
| 961 | .owner = THIS_MODULE, |
| 962 | }, |
| 963 | .id_table = pm2xxx_id, |
| 964 | }; |
| 965 | |
| 966 | static int __init pm2xxx_charger_init(void) |
| 967 | { |
| 968 | return i2c_add_driver(&pm2xxx_charger_driver); |
| 969 | } |
| 970 | |
| 971 | static void __exit pm2xxx_charger_exit(void) |
| 972 | { |
| 973 | i2c_del_driver(&pm2xxx_charger_driver); |
| 974 | } |
| 975 | |
| 976 | subsys_initcall_sync(pm2xxx_charger_init); |
| 977 | module_exit(pm2xxx_charger_exit); |
| 978 | |
| 979 | MODULE_LICENSE("GPL v2"); |
| 980 | MODULE_AUTHOR("Rajkumar kasirajan, Olivier Launay"); |
| 981 | MODULE_ALIAS("platform:pm2xxx-charger"); |
| 982 | MODULE_DESCRIPTION("PM2xxx charger management driver"); |
| 983 | |