Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.6-rc4 2792 lines 76 kB view raw
1/* 2 * Copyright (C) ST-Ericsson SA 2012 3 * 4 * Charger driver for AB8500 5 * 6 * License Terms: GNU General Public License v2 7 * Author: 8 * Johan Palsson <johan.palsson@stericsson.com> 9 * Karl Komierowski <karl.komierowski@stericsson.com> 10 * Arun R Murthy <arun.murthy@stericsson.com> 11 */ 12 13#include <linux/init.h> 14#include <linux/module.h> 15#include <linux/device.h> 16#include <linux/interrupt.h> 17#include <linux/delay.h> 18#include <linux/slab.h> 19#include <linux/platform_device.h> 20#include <linux/power_supply.h> 21#include <linux/completion.h> 22#include <linux/regulator/consumer.h> 23#include <linux/err.h> 24#include <linux/workqueue.h> 25#include <linux/kobject.h> 26#include <linux/mfd/abx500/ab8500.h> 27#include <linux/mfd/abx500.h> 28#include <linux/mfd/abx500/ab8500-bm.h> 29#include <linux/mfd/abx500/ab8500-gpadc.h> 30#include <linux/mfd/abx500/ux500_chargalg.h> 31#include <linux/usb/otg.h> 32 33/* Charger constants */ 34#define NO_PW_CONN 0 35#define AC_PW_CONN 1 36#define USB_PW_CONN 2 37 38#define MAIN_WDOG_ENA 0x01 39#define MAIN_WDOG_KICK 0x02 40#define MAIN_WDOG_DIS 0x00 41#define CHARG_WD_KICK 0x01 42#define MAIN_CH_ENA 0x01 43#define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02 44#define USB_CH_ENA 0x01 45#define USB_CHG_NO_OVERSHOOT_ENA_N 0x02 46#define MAIN_CH_DET 0x01 47#define MAIN_CH_CV_ON 0x04 48#define USB_CH_CV_ON 0x08 49#define VBUS_DET_DBNC100 0x02 50#define VBUS_DET_DBNC1 0x01 51#define OTP_ENABLE_WD 0x01 52 53#define MAIN_CH_INPUT_CURR_SHIFT 4 54#define VBUS_IN_CURR_LIM_SHIFT 4 55 56#define LED_INDICATOR_PWM_ENA 0x01 57#define LED_INDICATOR_PWM_DIS 0x00 58#define LED_IND_CUR_5MA 0x04 59#define LED_INDICATOR_PWM_DUTY_252_256 0xBF 60 61/* HW failure constants */ 62#define MAIN_CH_TH_PROT 0x02 63#define VBUS_CH_NOK 0x08 64#define USB_CH_TH_PROT 0x02 65#define VBUS_OVV_TH 0x01 66#define MAIN_CH_NOK 0x01 67#define VBUS_DET 0x80 68 69/* UsbLineStatus register bit masks */ 70#define AB8500_USB_LINK_STATUS 0x78 71#define AB8500_STD_HOST_SUSP 0x18 72 73/* Watchdog timeout constant */ 74#define WD_TIMER 0x30 /* 4min */ 75#define WD_KICK_INTERVAL (60 * HZ) 76 77/* Lowest charger voltage is 3.39V -> 0x4E */ 78#define LOW_VOLT_REG 0x4E 79 80/* UsbLineStatus register - usb types */ 81enum ab8500_charger_link_status { 82 USB_STAT_NOT_CONFIGURED, 83 USB_STAT_STD_HOST_NC, 84 USB_STAT_STD_HOST_C_NS, 85 USB_STAT_STD_HOST_C_S, 86 USB_STAT_HOST_CHG_NM, 87 USB_STAT_HOST_CHG_HS, 88 USB_STAT_HOST_CHG_HS_CHIRP, 89 USB_STAT_DEDICATED_CHG, 90 USB_STAT_ACA_RID_A, 91 USB_STAT_ACA_RID_B, 92 USB_STAT_ACA_RID_C_NM, 93 USB_STAT_ACA_RID_C_HS, 94 USB_STAT_ACA_RID_C_HS_CHIRP, 95 USB_STAT_HM_IDGND, 96 USB_STAT_RESERVED, 97 USB_STAT_NOT_VALID_LINK, 98}; 99 100enum ab8500_usb_state { 101 AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */ 102 AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */ 103 AB8500_BM_USB_STATE_CONFIGURED, 104 AB8500_BM_USB_STATE_SUSPEND, 105 AB8500_BM_USB_STATE_RESUME, 106 AB8500_BM_USB_STATE_MAX, 107}; 108 109/* VBUS input current limits supported in AB8500 in mA */ 110#define USB_CH_IP_CUR_LVL_0P05 50 111#define USB_CH_IP_CUR_LVL_0P09 98 112#define USB_CH_IP_CUR_LVL_0P19 193 113#define USB_CH_IP_CUR_LVL_0P29 290 114#define USB_CH_IP_CUR_LVL_0P38 380 115#define USB_CH_IP_CUR_LVL_0P45 450 116#define USB_CH_IP_CUR_LVL_0P5 500 117#define USB_CH_IP_CUR_LVL_0P6 600 118#define USB_CH_IP_CUR_LVL_0P7 700 119#define USB_CH_IP_CUR_LVL_0P8 800 120#define USB_CH_IP_CUR_LVL_0P9 900 121#define USB_CH_IP_CUR_LVL_1P0 1000 122#define USB_CH_IP_CUR_LVL_1P1 1100 123#define USB_CH_IP_CUR_LVL_1P3 1300 124#define USB_CH_IP_CUR_LVL_1P4 1400 125#define USB_CH_IP_CUR_LVL_1P5 1500 126 127#define VBAT_TRESH_IP_CUR_RED 3800 128 129#define to_ab8500_charger_usb_device_info(x) container_of((x), \ 130 struct ab8500_charger, usb_chg) 131#define to_ab8500_charger_ac_device_info(x) container_of((x), \ 132 struct ab8500_charger, ac_chg) 133 134/** 135 * struct ab8500_charger_interrupts - ab8500 interupts 136 * @name: name of the interrupt 137 * @isr function pointer to the isr 138 */ 139struct ab8500_charger_interrupts { 140 char *name; 141 irqreturn_t (*isr)(int irq, void *data); 142}; 143 144struct ab8500_charger_info { 145 int charger_connected; 146 int charger_online; 147 int charger_voltage; 148 int cv_active; 149 bool wd_expired; 150}; 151 152struct ab8500_charger_event_flags { 153 bool mainextchnotok; 154 bool main_thermal_prot; 155 bool usb_thermal_prot; 156 bool vbus_ovv; 157 bool usbchargernotok; 158 bool chgwdexp; 159 bool vbus_collapse; 160}; 161 162struct ab8500_charger_usb_state { 163 bool usb_changed; 164 int usb_current; 165 enum ab8500_usb_state state; 166 spinlock_t usb_lock; 167}; 168 169/** 170 * struct ab8500_charger - ab8500 Charger device information 171 * @dev: Pointer to the structure device 172 * @max_usb_in_curr: Max USB charger input current 173 * @vbus_detected: VBUS detected 174 * @vbus_detected_start: 175 * VBUS detected during startup 176 * @ac_conn: This will be true when the AC charger has been plugged 177 * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC 178 * charger is enabled 179 * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB 180 * charger is enabled 181 * @vbat Battery voltage 182 * @old_vbat Previously measured battery voltage 183 * @autopower Indicate if we should have automatic pwron after pwrloss 184 * @parent: Pointer to the struct ab8500 185 * @gpadc: Pointer to the struct gpadc 186 * @pdata: Pointer to the abx500_charger platform data 187 * @bat: Pointer to the abx500_bm platform data 188 * @flags: Structure for information about events triggered 189 * @usb_state: Structure for usb stack information 190 * @ac_chg: AC charger power supply 191 * @usb_chg: USB charger power supply 192 * @ac: Structure that holds the AC charger properties 193 * @usb: Structure that holds the USB charger properties 194 * @regu: Pointer to the struct regulator 195 * @charger_wq: Work queue for the IRQs and checking HW state 196 * @check_vbat_work Work for checking vbat threshold to adjust vbus current 197 * @check_hw_failure_work: Work for checking HW state 198 * @check_usbchgnotok_work: Work for checking USB charger not ok status 199 * @kick_wd_work: Work for kicking the charger watchdog in case 200 * of ABB rev 1.* due to the watchog logic bug 201 * @ac_work: Work for checking AC charger connection 202 * @detect_usb_type_work: Work for detecting the USB type connected 203 * @usb_link_status_work: Work for checking the new USB link status 204 * @usb_state_changed_work: Work for checking USB state 205 * @check_main_thermal_prot_work: 206 * Work for checking Main thermal status 207 * @check_usb_thermal_prot_work: 208 * Work for checking USB thermal status 209 */ 210struct ab8500_charger { 211 struct device *dev; 212 int max_usb_in_curr; 213 bool vbus_detected; 214 bool vbus_detected_start; 215 bool ac_conn; 216 bool vddadc_en_ac; 217 bool vddadc_en_usb; 218 int vbat; 219 int old_vbat; 220 bool autopower; 221 struct ab8500 *parent; 222 struct ab8500_gpadc *gpadc; 223 struct abx500_charger_platform_data *pdata; 224 struct abx500_bm_data *bat; 225 struct ab8500_charger_event_flags flags; 226 struct ab8500_charger_usb_state usb_state; 227 struct ux500_charger ac_chg; 228 struct ux500_charger usb_chg; 229 struct ab8500_charger_info ac; 230 struct ab8500_charger_info usb; 231 struct regulator *regu; 232 struct workqueue_struct *charger_wq; 233 struct delayed_work check_vbat_work; 234 struct delayed_work check_hw_failure_work; 235 struct delayed_work check_usbchgnotok_work; 236 struct delayed_work kick_wd_work; 237 struct work_struct ac_work; 238 struct work_struct detect_usb_type_work; 239 struct work_struct usb_link_status_work; 240 struct work_struct usb_state_changed_work; 241 struct work_struct check_main_thermal_prot_work; 242 struct work_struct check_usb_thermal_prot_work; 243 struct usb_phy *usb_phy; 244 struct notifier_block nb; 245}; 246 247/* AC properties */ 248static enum power_supply_property ab8500_charger_ac_props[] = { 249 POWER_SUPPLY_PROP_HEALTH, 250 POWER_SUPPLY_PROP_PRESENT, 251 POWER_SUPPLY_PROP_ONLINE, 252 POWER_SUPPLY_PROP_VOLTAGE_NOW, 253 POWER_SUPPLY_PROP_VOLTAGE_AVG, 254 POWER_SUPPLY_PROP_CURRENT_NOW, 255}; 256 257/* USB properties */ 258static enum power_supply_property ab8500_charger_usb_props[] = { 259 POWER_SUPPLY_PROP_HEALTH, 260 POWER_SUPPLY_PROP_CURRENT_AVG, 261 POWER_SUPPLY_PROP_PRESENT, 262 POWER_SUPPLY_PROP_ONLINE, 263 POWER_SUPPLY_PROP_VOLTAGE_NOW, 264 POWER_SUPPLY_PROP_VOLTAGE_AVG, 265 POWER_SUPPLY_PROP_CURRENT_NOW, 266}; 267 268/** 269 * ab8500_power_loss_handling - set how we handle powerloss. 270 * @di: pointer to the ab8500_charger structure 271 * 272 * Magic nummbers are from STE HW department. 273 */ 274static void ab8500_power_loss_handling(struct ab8500_charger *di) 275{ 276 u8 reg; 277 int ret; 278 279 dev_dbg(di->dev, "Autopower : %d\n", di->autopower); 280 281 /* read the autopower register */ 282 ret = abx500_get_register_interruptible(di->dev, 0x15, 0x00, &reg); 283 if (ret) { 284 dev_err(di->dev, "%d write failed\n", __LINE__); 285 return; 286 } 287 288 /* enable the OPT emulation registers */ 289 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2); 290 if (ret) { 291 dev_err(di->dev, "%d write failed\n", __LINE__); 292 return; 293 } 294 295 if (di->autopower) 296 reg |= 0x8; 297 else 298 reg &= ~0x8; 299 300 /* write back the changed value to autopower reg */ 301 ret = abx500_set_register_interruptible(di->dev, 0x15, 0x00, reg); 302 if (ret) { 303 dev_err(di->dev, "%d write failed\n", __LINE__); 304 return; 305 } 306 307 /* disable the set OTP registers again */ 308 ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0); 309 if (ret) { 310 dev_err(di->dev, "%d write failed\n", __LINE__); 311 return; 312 } 313} 314 315/** 316 * ab8500_power_supply_changed - a wrapper with local extentions for 317 * power_supply_changed 318 * @di: pointer to the ab8500_charger structure 319 * @psy: pointer to power_supply_that have changed. 320 * 321 */ 322static void ab8500_power_supply_changed(struct ab8500_charger *di, 323 struct power_supply *psy) 324{ 325 if (di->pdata->autopower_cfg) { 326 if (!di->usb.charger_connected && 327 !di->ac.charger_connected && 328 di->autopower) { 329 di->autopower = false; 330 ab8500_power_loss_handling(di); 331 } else if (!di->autopower && 332 (di->ac.charger_connected || 333 di->usb.charger_connected)) { 334 di->autopower = true; 335 ab8500_power_loss_handling(di); 336 } 337 } 338 power_supply_changed(psy); 339} 340 341static void ab8500_charger_set_usb_connected(struct ab8500_charger *di, 342 bool connected) 343{ 344 if (connected != di->usb.charger_connected) { 345 dev_dbg(di->dev, "USB connected:%i\n", connected); 346 di->usb.charger_connected = connected; 347 sysfs_notify(&di->usb_chg.psy.dev->kobj, NULL, "present"); 348 } 349} 350 351/** 352 * ab8500_charger_get_ac_voltage() - get ac charger voltage 353 * @di: pointer to the ab8500_charger structure 354 * 355 * Returns ac charger voltage (on success) 356 */ 357static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di) 358{ 359 int vch; 360 361 /* Only measure voltage if the charger is connected */ 362 if (di->ac.charger_connected) { 363 vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V); 364 if (vch < 0) 365 dev_err(di->dev, "%s gpadc conv failed,\n", __func__); 366 } else { 367 vch = 0; 368 } 369 return vch; 370} 371 372/** 373 * ab8500_charger_ac_cv() - check if the main charger is in CV mode 374 * @di: pointer to the ab8500_charger structure 375 * 376 * Returns ac charger CV mode (on success) else error code 377 */ 378static int ab8500_charger_ac_cv(struct ab8500_charger *di) 379{ 380 u8 val; 381 int ret = 0; 382 383 /* Only check CV mode if the charger is online */ 384 if (di->ac.charger_online) { 385 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 386 AB8500_CH_STATUS1_REG, &val); 387 if (ret < 0) { 388 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 389 return 0; 390 } 391 392 if (val & MAIN_CH_CV_ON) 393 ret = 1; 394 else 395 ret = 0; 396 } 397 398 return ret; 399} 400 401/** 402 * ab8500_charger_get_vbus_voltage() - get vbus voltage 403 * @di: pointer to the ab8500_charger structure 404 * 405 * This function returns the vbus voltage. 406 * Returns vbus voltage (on success) 407 */ 408static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di) 409{ 410 int vch; 411 412 /* Only measure voltage if the charger is connected */ 413 if (di->usb.charger_connected) { 414 vch = ab8500_gpadc_convert(di->gpadc, VBUS_V); 415 if (vch < 0) 416 dev_err(di->dev, "%s gpadc conv failed\n", __func__); 417 } else { 418 vch = 0; 419 } 420 return vch; 421} 422 423/** 424 * ab8500_charger_get_usb_current() - get usb charger current 425 * @di: pointer to the ab8500_charger structure 426 * 427 * This function returns the usb charger current. 428 * Returns usb current (on success) and error code on failure 429 */ 430static int ab8500_charger_get_usb_current(struct ab8500_charger *di) 431{ 432 int ich; 433 434 /* Only measure current if the charger is online */ 435 if (di->usb.charger_online) { 436 ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C); 437 if (ich < 0) 438 dev_err(di->dev, "%s gpadc conv failed\n", __func__); 439 } else { 440 ich = 0; 441 } 442 return ich; 443} 444 445/** 446 * ab8500_charger_get_ac_current() - get ac charger current 447 * @di: pointer to the ab8500_charger structure 448 * 449 * This function returns the ac charger current. 450 * Returns ac current (on success) and error code on failure. 451 */ 452static int ab8500_charger_get_ac_current(struct ab8500_charger *di) 453{ 454 int ich; 455 456 /* Only measure current if the charger is online */ 457 if (di->ac.charger_online) { 458 ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C); 459 if (ich < 0) 460 dev_err(di->dev, "%s gpadc conv failed\n", __func__); 461 } else { 462 ich = 0; 463 } 464 return ich; 465} 466 467/** 468 * ab8500_charger_usb_cv() - check if the usb charger is in CV mode 469 * @di: pointer to the ab8500_charger structure 470 * 471 * Returns ac charger CV mode (on success) else error code 472 */ 473static int ab8500_charger_usb_cv(struct ab8500_charger *di) 474{ 475 int ret; 476 u8 val; 477 478 /* Only check CV mode if the charger is online */ 479 if (di->usb.charger_online) { 480 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 481 AB8500_CH_USBCH_STAT1_REG, &val); 482 if (ret < 0) { 483 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 484 return 0; 485 } 486 487 if (val & USB_CH_CV_ON) 488 ret = 1; 489 else 490 ret = 0; 491 } else { 492 ret = 0; 493 } 494 495 return ret; 496} 497 498/** 499 * ab8500_charger_detect_chargers() - Detect the connected chargers 500 * @di: pointer to the ab8500_charger structure 501 * 502 * Returns the type of charger connected. 503 * For USB it will not mean we can actually charge from it 504 * but that there is a USB cable connected that we have to 505 * identify. This is used during startup when we don't get 506 * interrupts of the charger detection 507 * 508 * Returns an integer value, that means, 509 * NO_PW_CONN no power supply is connected 510 * AC_PW_CONN if the AC power supply is connected 511 * USB_PW_CONN if the USB power supply is connected 512 * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected 513 */ 514static int ab8500_charger_detect_chargers(struct ab8500_charger *di) 515{ 516 int result = NO_PW_CONN; 517 int ret; 518 u8 val; 519 520 /* Check for AC charger */ 521 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 522 AB8500_CH_STATUS1_REG, &val); 523 if (ret < 0) { 524 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 525 return ret; 526 } 527 528 if (val & MAIN_CH_DET) 529 result = AC_PW_CONN; 530 531 /* Check for USB charger */ 532 ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER, 533 AB8500_CH_USBCH_STAT1_REG, &val); 534 if (ret < 0) { 535 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 536 return ret; 537 } 538 539 if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100)) 540 result |= USB_PW_CONN; 541 542 return result; 543} 544 545/** 546 * ab8500_charger_max_usb_curr() - get the max curr for the USB type 547 * @di: pointer to the ab8500_charger structure 548 * @link_status: the identified USB type 549 * 550 * Get the maximum current that is allowed to be drawn from the host 551 * based on the USB type. 552 * Returns error code in case of failure else 0 on success 553 */ 554static int ab8500_charger_max_usb_curr(struct ab8500_charger *di, 555 enum ab8500_charger_link_status link_status) 556{ 557 int ret = 0; 558 559 switch (link_status) { 560 case USB_STAT_STD_HOST_NC: 561 case USB_STAT_STD_HOST_C_NS: 562 case USB_STAT_STD_HOST_C_S: 563 dev_dbg(di->dev, "USB Type - Standard host is " 564 "detected through USB driver\n"); 565 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; 566 break; 567 case USB_STAT_HOST_CHG_HS_CHIRP: 568 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; 569 break; 570 case USB_STAT_HOST_CHG_HS: 571 case USB_STAT_ACA_RID_C_HS: 572 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P9; 573 break; 574 case USB_STAT_ACA_RID_A: 575 /* 576 * Dedicated charger level minus maximum current accessory 577 * can consume (300mA). Closest level is 1100mA 578 */ 579 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P1; 580 break; 581 case USB_STAT_ACA_RID_B: 582 /* 583 * Dedicated charger level minus 120mA (20mA for ACA and 584 * 100mA for potential accessory). Closest level is 1300mA 585 */ 586 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P3; 587 break; 588 case USB_STAT_DEDICATED_CHG: 589 case USB_STAT_HOST_CHG_NM: 590 case USB_STAT_ACA_RID_C_HS_CHIRP: 591 case USB_STAT_ACA_RID_C_NM: 592 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_1P5; 593 break; 594 case USB_STAT_RESERVED: 595 /* 596 * This state is used to indicate that VBUS has dropped below 597 * the detection level 4 times in a row. This is due to the 598 * charger output current is set to high making the charger 599 * voltage collapse. This have to be propagated through to 600 * chargalg. This is done using the property 601 * POWER_SUPPLY_PROP_CURRENT_AVG = 1 602 */ 603 di->flags.vbus_collapse = true; 604 dev_dbg(di->dev, "USB Type - USB_STAT_RESERVED " 605 "VBUS has collapsed\n"); 606 ret = -1; 607 break; 608 case USB_STAT_HM_IDGND: 609 case USB_STAT_NOT_CONFIGURED: 610 case USB_STAT_NOT_VALID_LINK: 611 dev_err(di->dev, "USB Type - Charging not allowed\n"); 612 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 613 ret = -ENXIO; 614 break; 615 default: 616 dev_err(di->dev, "USB Type - Unknown\n"); 617 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 618 ret = -ENXIO; 619 break; 620 }; 621 622 dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", 623 link_status, di->max_usb_in_curr); 624 625 return ret; 626} 627 628/** 629 * ab8500_charger_read_usb_type() - read the type of usb connected 630 * @di: pointer to the ab8500_charger structure 631 * 632 * Detect the type of the plugged USB 633 * Returns error code in case of failure else 0 on success 634 */ 635static int ab8500_charger_read_usb_type(struct ab8500_charger *di) 636{ 637 int ret; 638 u8 val; 639 640 ret = abx500_get_register_interruptible(di->dev, 641 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val); 642 if (ret < 0) { 643 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 644 return ret; 645 } 646 ret = abx500_get_register_interruptible(di->dev, AB8500_USB, 647 AB8500_USB_LINE_STAT_REG, &val); 648 if (ret < 0) { 649 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 650 return ret; 651 } 652 653 /* get the USB type */ 654 val = (val & AB8500_USB_LINK_STATUS) >> 3; 655 ret = ab8500_charger_max_usb_curr(di, 656 (enum ab8500_charger_link_status) val); 657 658 return ret; 659} 660 661/** 662 * ab8500_charger_detect_usb_type() - get the type of usb connected 663 * @di: pointer to the ab8500_charger structure 664 * 665 * Detect the type of the plugged USB 666 * Returns error code in case of failure else 0 on success 667 */ 668static int ab8500_charger_detect_usb_type(struct ab8500_charger *di) 669{ 670 int i, ret; 671 u8 val; 672 673 /* 674 * On getting the VBUS rising edge detect interrupt there 675 * is a 250ms delay after which the register UsbLineStatus 676 * is filled with valid data. 677 */ 678 for (i = 0; i < 10; i++) { 679 msleep(250); 680 ret = abx500_get_register_interruptible(di->dev, 681 AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, 682 &val); 683 if (ret < 0) { 684 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 685 return ret; 686 } 687 ret = abx500_get_register_interruptible(di->dev, AB8500_USB, 688 AB8500_USB_LINE_STAT_REG, &val); 689 if (ret < 0) { 690 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 691 return ret; 692 } 693 /* 694 * Until the IT source register is read the UsbLineStatus 695 * register is not updated, hence doing the same 696 * Revisit this: 697 */ 698 699 /* get the USB type */ 700 val = (val & AB8500_USB_LINK_STATUS) >> 3; 701 if (val) 702 break; 703 } 704 ret = ab8500_charger_max_usb_curr(di, 705 (enum ab8500_charger_link_status) val); 706 707 return ret; 708} 709 710/* 711 * This array maps the raw hex value to charger voltage used by the AB8500 712 * Values taken from the UM0836 713 */ 714static int ab8500_charger_voltage_map[] = { 715 3500 , 716 3525 , 717 3550 , 718 3575 , 719 3600 , 720 3625 , 721 3650 , 722 3675 , 723 3700 , 724 3725 , 725 3750 , 726 3775 , 727 3800 , 728 3825 , 729 3850 , 730 3875 , 731 3900 , 732 3925 , 733 3950 , 734 3975 , 735 4000 , 736 4025 , 737 4050 , 738 4060 , 739 4070 , 740 4080 , 741 4090 , 742 4100 , 743 4110 , 744 4120 , 745 4130 , 746 4140 , 747 4150 , 748 4160 , 749 4170 , 750 4180 , 751 4190 , 752 4200 , 753 4210 , 754 4220 , 755 4230 , 756 4240 , 757 4250 , 758 4260 , 759 4270 , 760 4280 , 761 4290 , 762 4300 , 763 4310 , 764 4320 , 765 4330 , 766 4340 , 767 4350 , 768 4360 , 769 4370 , 770 4380 , 771 4390 , 772 4400 , 773 4410 , 774 4420 , 775 4430 , 776 4440 , 777 4450 , 778 4460 , 779 4470 , 780 4480 , 781 4490 , 782 4500 , 783 4510 , 784 4520 , 785 4530 , 786 4540 , 787 4550 , 788 4560 , 789 4570 , 790 4580 , 791 4590 , 792 4600 , 793}; 794 795/* 796 * This array maps the raw hex value to charger current used by the AB8500 797 * Values taken from the UM0836 798 */ 799static int ab8500_charger_current_map[] = { 800 100 , 801 200 , 802 300 , 803 400 , 804 500 , 805 600 , 806 700 , 807 800 , 808 900 , 809 1000 , 810 1100 , 811 1200 , 812 1300 , 813 1400 , 814 1500 , 815}; 816 817/* 818 * This array maps the raw hex value to VBUS input current used by the AB8500 819 * Values taken from the UM0836 820 */ 821static int ab8500_charger_vbus_in_curr_map[] = { 822 USB_CH_IP_CUR_LVL_0P05, 823 USB_CH_IP_CUR_LVL_0P09, 824 USB_CH_IP_CUR_LVL_0P19, 825 USB_CH_IP_CUR_LVL_0P29, 826 USB_CH_IP_CUR_LVL_0P38, 827 USB_CH_IP_CUR_LVL_0P45, 828 USB_CH_IP_CUR_LVL_0P5, 829 USB_CH_IP_CUR_LVL_0P6, 830 USB_CH_IP_CUR_LVL_0P7, 831 USB_CH_IP_CUR_LVL_0P8, 832 USB_CH_IP_CUR_LVL_0P9, 833 USB_CH_IP_CUR_LVL_1P0, 834 USB_CH_IP_CUR_LVL_1P1, 835 USB_CH_IP_CUR_LVL_1P3, 836 USB_CH_IP_CUR_LVL_1P4, 837 USB_CH_IP_CUR_LVL_1P5, 838}; 839 840static int ab8500_voltage_to_regval(int voltage) 841{ 842 int i; 843 844 /* Special case for voltage below 3.5V */ 845 if (voltage < ab8500_charger_voltage_map[0]) 846 return LOW_VOLT_REG; 847 848 for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) { 849 if (voltage < ab8500_charger_voltage_map[i]) 850 return i - 1; 851 } 852 853 /* If not last element, return error */ 854 i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1; 855 if (voltage == ab8500_charger_voltage_map[i]) 856 return i; 857 else 858 return -1; 859} 860 861static int ab8500_current_to_regval(int curr) 862{ 863 int i; 864 865 if (curr < ab8500_charger_current_map[0]) 866 return 0; 867 868 for (i = 0; i < ARRAY_SIZE(ab8500_charger_current_map); i++) { 869 if (curr < ab8500_charger_current_map[i]) 870 return i - 1; 871 } 872 873 /* If not last element, return error */ 874 i = ARRAY_SIZE(ab8500_charger_current_map) - 1; 875 if (curr == ab8500_charger_current_map[i]) 876 return i; 877 else 878 return -1; 879} 880 881static int ab8500_vbus_in_curr_to_regval(int curr) 882{ 883 int i; 884 885 if (curr < ab8500_charger_vbus_in_curr_map[0]) 886 return 0; 887 888 for (i = 0; i < ARRAY_SIZE(ab8500_charger_vbus_in_curr_map); i++) { 889 if (curr < ab8500_charger_vbus_in_curr_map[i]) 890 return i - 1; 891 } 892 893 /* If not last element, return error */ 894 i = ARRAY_SIZE(ab8500_charger_vbus_in_curr_map) - 1; 895 if (curr == ab8500_charger_vbus_in_curr_map[i]) 896 return i; 897 else 898 return -1; 899} 900 901/** 902 * ab8500_charger_get_usb_cur() - get usb current 903 * @di: pointer to the ab8500_charger structre 904 * 905 * The usb stack provides the maximum current that can be drawn from 906 * the standard usb host. This will be in mA. 907 * This function converts current in mA to a value that can be written 908 * to the register. Returns -1 if charging is not allowed 909 */ 910static int ab8500_charger_get_usb_cur(struct ab8500_charger *di) 911{ 912 switch (di->usb_state.usb_current) { 913 case 100: 914 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P09; 915 break; 916 case 200: 917 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P19; 918 break; 919 case 300: 920 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P29; 921 break; 922 case 400: 923 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P38; 924 break; 925 case 500: 926 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P5; 927 break; 928 default: 929 di->max_usb_in_curr = USB_CH_IP_CUR_LVL_0P05; 930 return -1; 931 break; 932 }; 933 return 0; 934} 935 936/** 937 * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit 938 * @di: pointer to the ab8500_charger structure 939 * @ich_in: charger input current limit 940 * 941 * Sets the current that can be drawn from the USB host 942 * Returns error code in case of failure else 0(on success) 943 */ 944static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di, 945 int ich_in) 946{ 947 int ret; 948 int input_curr_index; 949 int min_value; 950 951 /* We should always use to lowest current limit */ 952 min_value = min(di->bat->chg_params->usb_curr_max, ich_in); 953 954 switch (min_value) { 955 case 100: 956 if (di->vbat < VBAT_TRESH_IP_CUR_RED) 957 min_value = USB_CH_IP_CUR_LVL_0P05; 958 break; 959 case 500: 960 if (di->vbat < VBAT_TRESH_IP_CUR_RED) 961 min_value = USB_CH_IP_CUR_LVL_0P45; 962 break; 963 default: 964 break; 965 } 966 967 input_curr_index = ab8500_vbus_in_curr_to_regval(min_value); 968 if (input_curr_index < 0) { 969 dev_err(di->dev, "VBUS input current limit too high\n"); 970 return -ENXIO; 971 } 972 973 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 974 AB8500_USBCH_IPT_CRNTLVL_REG, 975 input_curr_index << VBUS_IN_CURR_LIM_SHIFT); 976 if (ret) 977 dev_err(di->dev, "%s write failed\n", __func__); 978 979 return ret; 980} 981 982/** 983 * ab8500_charger_led_en() - turn on/off chargign led 984 * @di: pointer to the ab8500_charger structure 985 * @on: flag to turn on/off the chargign led 986 * 987 * Power ON/OFF charging LED indication 988 * Returns error code in case of failure else 0(on success) 989 */ 990static int ab8500_charger_led_en(struct ab8500_charger *di, int on) 991{ 992 int ret; 993 994 if (on) { 995 /* Power ON charging LED indicator, set LED current to 5mA */ 996 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 997 AB8500_LED_INDICATOR_PWM_CTRL, 998 (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA)); 999 if (ret) { 1000 dev_err(di->dev, "Power ON LED failed\n"); 1001 return ret; 1002 } 1003 /* LED indicator PWM duty cycle 252/256 */ 1004 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1005 AB8500_LED_INDICATOR_PWM_DUTY, 1006 LED_INDICATOR_PWM_DUTY_252_256); 1007 if (ret) { 1008 dev_err(di->dev, "Set LED PWM duty cycle failed\n"); 1009 return ret; 1010 } 1011 } else { 1012 /* Power off charging LED indicator */ 1013 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1014 AB8500_LED_INDICATOR_PWM_CTRL, 1015 LED_INDICATOR_PWM_DIS); 1016 if (ret) { 1017 dev_err(di->dev, "Power-off LED failed\n"); 1018 return ret; 1019 } 1020 } 1021 1022 return ret; 1023} 1024 1025/** 1026 * ab8500_charger_ac_en() - enable or disable ac charging 1027 * @di: pointer to the ab8500_charger structure 1028 * @enable: enable/disable flag 1029 * @vset: charging voltage 1030 * @iset: charging current 1031 * 1032 * Enable/Disable AC/Mains charging and turns on/off the charging led 1033 * respectively. 1034 **/ 1035static int ab8500_charger_ac_en(struct ux500_charger *charger, 1036 int enable, int vset, int iset) 1037{ 1038 int ret; 1039 int volt_index; 1040 int curr_index; 1041 int input_curr_index; 1042 u8 overshoot = 0; 1043 1044 struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger); 1045 1046 if (enable) { 1047 /* Check if AC is connected */ 1048 if (!di->ac.charger_connected) { 1049 dev_err(di->dev, "AC charger not connected\n"); 1050 return -ENXIO; 1051 } 1052 1053 /* Enable AC charging */ 1054 dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset); 1055 1056 /* 1057 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts 1058 * will be triggered everytime we enable the VDD ADC supply. 1059 * This will turn off charging for a short while. 1060 * It can be avoided by having the supply on when 1061 * there is a charger enabled. Normally the VDD ADC supply 1062 * is enabled everytime a GPADC conversion is triggered. We will 1063 * force it to be enabled from this driver to have 1064 * the GPADC module independant of the AB8500 chargers 1065 */ 1066 if (!di->vddadc_en_ac) { 1067 regulator_enable(di->regu); 1068 di->vddadc_en_ac = true; 1069 } 1070 1071 /* Check if the requested voltage or current is valid */ 1072 volt_index = ab8500_voltage_to_regval(vset); 1073 curr_index = ab8500_current_to_regval(iset); 1074 input_curr_index = ab8500_current_to_regval( 1075 di->bat->chg_params->ac_curr_max); 1076 if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) { 1077 dev_err(di->dev, 1078 "Charger voltage or current too high, " 1079 "charging not started\n"); 1080 return -ENXIO; 1081 } 1082 1083 /* ChVoltLevel: maximum battery charging voltage */ 1084 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1085 AB8500_CH_VOLT_LVL_REG, (u8) volt_index); 1086 if (ret) { 1087 dev_err(di->dev, "%s write failed\n", __func__); 1088 return ret; 1089 } 1090 /* MainChInputCurr: current that can be drawn from the charger*/ 1091 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1092 AB8500_MCH_IPT_CURLVL_REG, 1093 input_curr_index << MAIN_CH_INPUT_CURR_SHIFT); 1094 if (ret) { 1095 dev_err(di->dev, "%s write failed\n", __func__); 1096 return ret; 1097 } 1098 /* ChOutputCurentLevel: protected output current */ 1099 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1100 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1101 if (ret) { 1102 dev_err(di->dev, "%s write failed\n", __func__); 1103 return ret; 1104 } 1105 1106 /* Check if VBAT overshoot control should be enabled */ 1107 if (!di->bat->enable_overshoot) 1108 overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N; 1109 1110 /* Enable Main Charger */ 1111 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1112 AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot); 1113 if (ret) { 1114 dev_err(di->dev, "%s write failed\n", __func__); 1115 return ret; 1116 } 1117 1118 /* Power on charging LED indication */ 1119 ret = ab8500_charger_led_en(di, true); 1120 if (ret < 0) 1121 dev_err(di->dev, "failed to enable LED\n"); 1122 1123 di->ac.charger_online = 1; 1124 } else { 1125 /* Disable AC charging */ 1126 if (is_ab8500_1p1_or_earlier(di->parent)) { 1127 /* 1128 * For ABB revision 1.0 and 1.1 there is a bug in the 1129 * watchdog logic. That means we have to continously 1130 * kick the charger watchdog even when no charger is 1131 * connected. This is only valid once the AC charger 1132 * has been enabled. This is a bug that is not handled 1133 * by the algorithm and the watchdog have to be kicked 1134 * by the charger driver when the AC charger 1135 * is disabled 1136 */ 1137 if (di->ac_conn) { 1138 queue_delayed_work(di->charger_wq, 1139 &di->kick_wd_work, 1140 round_jiffies(WD_KICK_INTERVAL)); 1141 } 1142 1143 /* 1144 * We can't turn off charging completely 1145 * due to a bug in AB8500 cut1. 1146 * If we do, charging will not start again. 1147 * That is why we set the lowest voltage 1148 * and current possible 1149 */ 1150 ret = abx500_set_register_interruptible(di->dev, 1151 AB8500_CHARGER, 1152 AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5); 1153 if (ret) { 1154 dev_err(di->dev, 1155 "%s write failed\n", __func__); 1156 return ret; 1157 } 1158 1159 ret = abx500_set_register_interruptible(di->dev, 1160 AB8500_CHARGER, 1161 AB8500_CH_OPT_CRNTLVL_REG, CH_OP_CUR_LVL_0P1); 1162 if (ret) { 1163 dev_err(di->dev, 1164 "%s write failed\n", __func__); 1165 return ret; 1166 } 1167 } else { 1168 ret = abx500_set_register_interruptible(di->dev, 1169 AB8500_CHARGER, 1170 AB8500_MCH_CTRL1, 0); 1171 if (ret) { 1172 dev_err(di->dev, 1173 "%s write failed\n", __func__); 1174 return ret; 1175 } 1176 } 1177 1178 ret = ab8500_charger_led_en(di, false); 1179 if (ret < 0) 1180 dev_err(di->dev, "failed to disable LED\n"); 1181 1182 di->ac.charger_online = 0; 1183 di->ac.wd_expired = false; 1184 1185 /* Disable regulator if enabled */ 1186 if (di->vddadc_en_ac) { 1187 regulator_disable(di->regu); 1188 di->vddadc_en_ac = false; 1189 } 1190 1191 dev_dbg(di->dev, "%s Disabled AC charging\n", __func__); 1192 } 1193 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1194 1195 return ret; 1196} 1197 1198/** 1199 * ab8500_charger_usb_en() - enable usb charging 1200 * @di: pointer to the ab8500_charger structure 1201 * @enable: enable/disable flag 1202 * @vset: charging voltage 1203 * @ich_out: charger output current 1204 * 1205 * Enable/Disable USB charging and turns on/off the charging led respectively. 1206 * Returns error code in case of failure else 0(on success) 1207 */ 1208static int ab8500_charger_usb_en(struct ux500_charger *charger, 1209 int enable, int vset, int ich_out) 1210{ 1211 int ret; 1212 int volt_index; 1213 int curr_index; 1214 u8 overshoot = 0; 1215 1216 struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger); 1217 1218 if (enable) { 1219 /* Check if USB is connected */ 1220 if (!di->usb.charger_connected) { 1221 dev_err(di->dev, "USB charger not connected\n"); 1222 return -ENXIO; 1223 } 1224 1225 /* 1226 * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts 1227 * will be triggered everytime we enable the VDD ADC supply. 1228 * This will turn off charging for a short while. 1229 * It can be avoided by having the supply on when 1230 * there is a charger enabled. Normally the VDD ADC supply 1231 * is enabled everytime a GPADC conversion is triggered. We will 1232 * force it to be enabled from this driver to have 1233 * the GPADC module independant of the AB8500 chargers 1234 */ 1235 if (!di->vddadc_en_usb) { 1236 regulator_enable(di->regu); 1237 di->vddadc_en_usb = true; 1238 } 1239 1240 /* Enable USB charging */ 1241 dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out); 1242 1243 /* Check if the requested voltage or current is valid */ 1244 volt_index = ab8500_voltage_to_regval(vset); 1245 curr_index = ab8500_current_to_regval(ich_out); 1246 if (volt_index < 0 || curr_index < 0) { 1247 dev_err(di->dev, 1248 "Charger voltage or current too high, " 1249 "charging not started\n"); 1250 return -ENXIO; 1251 } 1252 1253 /* ChVoltLevel: max voltage upto which battery can be charged */ 1254 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1255 AB8500_CH_VOLT_LVL_REG, (u8) volt_index); 1256 if (ret) { 1257 dev_err(di->dev, "%s write failed\n", __func__); 1258 return ret; 1259 } 1260 /* USBChInputCurr: current that can be drawn from the usb */ 1261 ret = ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); 1262 if (ret) { 1263 dev_err(di->dev, "setting USBChInputCurr failed\n"); 1264 return ret; 1265 } 1266 /* ChOutputCurentLevel: protected output current */ 1267 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1268 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1269 if (ret) { 1270 dev_err(di->dev, "%s write failed\n", __func__); 1271 return ret; 1272 } 1273 /* Check if VBAT overshoot control should be enabled */ 1274 if (!di->bat->enable_overshoot) 1275 overshoot = USB_CHG_NO_OVERSHOOT_ENA_N; 1276 1277 /* Enable USB Charger */ 1278 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1279 AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot); 1280 if (ret) { 1281 dev_err(di->dev, "%s write failed\n", __func__); 1282 return ret; 1283 } 1284 1285 /* If success power on charging LED indication */ 1286 ret = ab8500_charger_led_en(di, true); 1287 if (ret < 0) 1288 dev_err(di->dev, "failed to enable LED\n"); 1289 1290 queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ); 1291 1292 di->usb.charger_online = 1; 1293 } else { 1294 /* Disable USB charging */ 1295 ret = abx500_set_register_interruptible(di->dev, 1296 AB8500_CHARGER, 1297 AB8500_USBCH_CTRL1_REG, 0); 1298 if (ret) { 1299 dev_err(di->dev, 1300 "%s write failed\n", __func__); 1301 return ret; 1302 } 1303 1304 ret = ab8500_charger_led_en(di, false); 1305 if (ret < 0) 1306 dev_err(di->dev, "failed to disable LED\n"); 1307 1308 di->usb.charger_online = 0; 1309 di->usb.wd_expired = false; 1310 1311 /* Disable regulator if enabled */ 1312 if (di->vddadc_en_usb) { 1313 regulator_disable(di->regu); 1314 di->vddadc_en_usb = false; 1315 } 1316 1317 dev_dbg(di->dev, "%s Disabled USB charging\n", __func__); 1318 1319 /* Cancel any pending Vbat check work */ 1320 if (delayed_work_pending(&di->check_vbat_work)) 1321 cancel_delayed_work(&di->check_vbat_work); 1322 1323 } 1324 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1325 1326 return ret; 1327} 1328 1329/** 1330 * ab8500_charger_watchdog_kick() - kick charger watchdog 1331 * @di: pointer to the ab8500_charger structure 1332 * 1333 * Kick charger watchdog 1334 * Returns error code in case of failure else 0(on success) 1335 */ 1336static int ab8500_charger_watchdog_kick(struct ux500_charger *charger) 1337{ 1338 int ret; 1339 struct ab8500_charger *di; 1340 1341 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) 1342 di = to_ab8500_charger_ac_device_info(charger); 1343 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) 1344 di = to_ab8500_charger_usb_device_info(charger); 1345 else 1346 return -ENXIO; 1347 1348 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1349 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 1350 if (ret) 1351 dev_err(di->dev, "Failed to kick WD!\n"); 1352 1353 return ret; 1354} 1355 1356/** 1357 * ab8500_charger_update_charger_current() - update charger current 1358 * @di: pointer to the ab8500_charger structure 1359 * 1360 * Update the charger output current for the specified charger 1361 * Returns error code in case of failure else 0(on success) 1362 */ 1363static int ab8500_charger_update_charger_current(struct ux500_charger *charger, 1364 int ich_out) 1365{ 1366 int ret; 1367 int curr_index; 1368 struct ab8500_charger *di; 1369 1370 if (charger->psy.type == POWER_SUPPLY_TYPE_MAINS) 1371 di = to_ab8500_charger_ac_device_info(charger); 1372 else if (charger->psy.type == POWER_SUPPLY_TYPE_USB) 1373 di = to_ab8500_charger_usb_device_info(charger); 1374 else 1375 return -ENXIO; 1376 1377 curr_index = ab8500_current_to_regval(ich_out); 1378 if (curr_index < 0) { 1379 dev_err(di->dev, 1380 "Charger current too high, " 1381 "charging not started\n"); 1382 return -ENXIO; 1383 } 1384 1385 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1386 AB8500_CH_OPT_CRNTLVL_REG, (u8) curr_index); 1387 if (ret) { 1388 dev_err(di->dev, "%s write failed\n", __func__); 1389 return ret; 1390 } 1391 1392 /* Reset the main and usb drop input current measurement counter */ 1393 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1394 AB8500_CHARGER_CTRL, 1395 0x1); 1396 if (ret) { 1397 dev_err(di->dev, "%s write failed\n", __func__); 1398 return ret; 1399 } 1400 1401 return ret; 1402} 1403 1404static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data) 1405{ 1406 struct power_supply *psy; 1407 struct power_supply *ext; 1408 struct ab8500_charger *di; 1409 union power_supply_propval ret; 1410 int i, j; 1411 bool psy_found = false; 1412 struct ux500_charger *usb_chg; 1413 1414 usb_chg = (struct ux500_charger *)data; 1415 psy = &usb_chg->psy; 1416 1417 di = to_ab8500_charger_usb_device_info(usb_chg); 1418 1419 ext = dev_get_drvdata(dev); 1420 1421 /* For all psy where the driver name appears in any supplied_to */ 1422 for (i = 0; i < ext->num_supplicants; i++) { 1423 if (!strcmp(ext->supplied_to[i], psy->name)) 1424 psy_found = true; 1425 } 1426 1427 if (!psy_found) 1428 return 0; 1429 1430 /* Go through all properties for the psy */ 1431 for (j = 0; j < ext->num_properties; j++) { 1432 enum power_supply_property prop; 1433 prop = ext->properties[j]; 1434 1435 if (ext->get_property(ext, prop, &ret)) 1436 continue; 1437 1438 switch (prop) { 1439 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 1440 switch (ext->type) { 1441 case POWER_SUPPLY_TYPE_BATTERY: 1442 di->vbat = ret.intval / 1000; 1443 break; 1444 default: 1445 break; 1446 } 1447 break; 1448 default: 1449 break; 1450 } 1451 } 1452 return 0; 1453} 1454 1455/** 1456 * ab8500_charger_check_vbat_work() - keep vbus current within spec 1457 * @work pointer to the work_struct structure 1458 * 1459 * Due to a asic bug it is necessary to lower the input current to the vbus 1460 * charger when charging with at some specific levels. This issue is only valid 1461 * for below a certain battery voltage. This function makes sure that the 1462 * the allowed current limit isn't exceeded. 1463 */ 1464static void ab8500_charger_check_vbat_work(struct work_struct *work) 1465{ 1466 int t = 10; 1467 struct ab8500_charger *di = container_of(work, 1468 struct ab8500_charger, check_vbat_work.work); 1469 1470 class_for_each_device(power_supply_class, NULL, 1471 &di->usb_chg.psy, ab8500_charger_get_ext_psy_data); 1472 1473 /* First run old_vbat is 0. */ 1474 if (di->old_vbat == 0) 1475 di->old_vbat = di->vbat; 1476 1477 if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED && 1478 di->vbat <= VBAT_TRESH_IP_CUR_RED) || 1479 (di->old_vbat > VBAT_TRESH_IP_CUR_RED && 1480 di->vbat > VBAT_TRESH_IP_CUR_RED))) { 1481 1482 dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d," 1483 " old: %d\n", di->max_usb_in_curr, di->vbat, 1484 di->old_vbat); 1485 ab8500_charger_set_vbus_in_curr(di, di->max_usb_in_curr); 1486 power_supply_changed(&di->usb_chg.psy); 1487 } 1488 1489 di->old_vbat = di->vbat; 1490 1491 /* 1492 * No need to check the battery voltage every second when not close to 1493 * the threshold. 1494 */ 1495 if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) && 1496 (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100))) 1497 t = 1; 1498 1499 queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ); 1500} 1501 1502/** 1503 * ab8500_charger_check_hw_failure_work() - check main charger failure 1504 * @work: pointer to the work_struct structure 1505 * 1506 * Work queue function for checking the main charger status 1507 */ 1508static void ab8500_charger_check_hw_failure_work(struct work_struct *work) 1509{ 1510 int ret; 1511 u8 reg_value; 1512 1513 struct ab8500_charger *di = container_of(work, 1514 struct ab8500_charger, check_hw_failure_work.work); 1515 1516 /* Check if the status bits for HW failure is still active */ 1517 if (di->flags.mainextchnotok) { 1518 ret = abx500_get_register_interruptible(di->dev, 1519 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value); 1520 if (ret < 0) { 1521 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1522 return; 1523 } 1524 if (!(reg_value & MAIN_CH_NOK)) { 1525 di->flags.mainextchnotok = false; 1526 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1527 } 1528 } 1529 if (di->flags.vbus_ovv) { 1530 ret = abx500_get_register_interruptible(di->dev, 1531 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, 1532 &reg_value); 1533 if (ret < 0) { 1534 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1535 return; 1536 } 1537 if (!(reg_value & VBUS_OVV_TH)) { 1538 di->flags.vbus_ovv = false; 1539 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1540 } 1541 } 1542 /* If we still have a failure, schedule a new check */ 1543 if (di->flags.mainextchnotok || di->flags.vbus_ovv) { 1544 queue_delayed_work(di->charger_wq, 1545 &di->check_hw_failure_work, round_jiffies(HZ)); 1546 } 1547} 1548 1549/** 1550 * ab8500_charger_kick_watchdog_work() - kick the watchdog 1551 * @work: pointer to the work_struct structure 1552 * 1553 * Work queue function for kicking the charger watchdog. 1554 * 1555 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 1556 * logic. That means we have to continously kick the charger 1557 * watchdog even when no charger is connected. This is only 1558 * valid once the AC charger has been enabled. This is 1559 * a bug that is not handled by the algorithm and the 1560 * watchdog have to be kicked by the charger driver 1561 * when the AC charger is disabled 1562 */ 1563static void ab8500_charger_kick_watchdog_work(struct work_struct *work) 1564{ 1565 int ret; 1566 1567 struct ab8500_charger *di = container_of(work, 1568 struct ab8500_charger, kick_wd_work.work); 1569 1570 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 1571 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 1572 if (ret) 1573 dev_err(di->dev, "Failed to kick WD!\n"); 1574 1575 /* Schedule a new watchdog kick */ 1576 queue_delayed_work(di->charger_wq, 1577 &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL)); 1578} 1579 1580/** 1581 * ab8500_charger_ac_work() - work to get and set main charger status 1582 * @work: pointer to the work_struct structure 1583 * 1584 * Work queue function for checking the main charger status 1585 */ 1586static void ab8500_charger_ac_work(struct work_struct *work) 1587{ 1588 int ret; 1589 1590 struct ab8500_charger *di = container_of(work, 1591 struct ab8500_charger, ac_work); 1592 1593 /* 1594 * Since we can't be sure that the events are received 1595 * synchronously, we have the check if the main charger is 1596 * connected by reading the status register 1597 */ 1598 ret = ab8500_charger_detect_chargers(di); 1599 if (ret < 0) 1600 return; 1601 1602 if (ret & AC_PW_CONN) { 1603 di->ac.charger_connected = 1; 1604 di->ac_conn = true; 1605 } else { 1606 di->ac.charger_connected = 0; 1607 } 1608 1609 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1610 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); 1611} 1612 1613/** 1614 * ab8500_charger_detect_usb_type_work() - work to detect USB type 1615 * @work: Pointer to the work_struct structure 1616 * 1617 * Detect the type of USB plugged 1618 */ 1619static void ab8500_charger_detect_usb_type_work(struct work_struct *work) 1620{ 1621 int ret; 1622 1623 struct ab8500_charger *di = container_of(work, 1624 struct ab8500_charger, detect_usb_type_work); 1625 1626 /* 1627 * Since we can't be sure that the events are received 1628 * synchronously, we have the check if is 1629 * connected by reading the status register 1630 */ 1631 ret = ab8500_charger_detect_chargers(di); 1632 if (ret < 0) 1633 return; 1634 1635 if (!(ret & USB_PW_CONN)) { 1636 di->vbus_detected = 0; 1637 ab8500_charger_set_usb_connected(di, false); 1638 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1639 } else { 1640 di->vbus_detected = 1; 1641 1642 if (is_ab8500_1p1_or_earlier(di->parent)) { 1643 ret = ab8500_charger_detect_usb_type(di); 1644 if (!ret) { 1645 ab8500_charger_set_usb_connected(di, true); 1646 ab8500_power_supply_changed(di, 1647 &di->usb_chg.psy); 1648 } 1649 } else { 1650 /* For ABB cut2.0 and onwards we have an IRQ, 1651 * USB_LINK_STATUS that will be triggered when the USB 1652 * link status changes. The exception is USB connected 1653 * during startup. Then we don't get a 1654 * USB_LINK_STATUS IRQ 1655 */ 1656 if (di->vbus_detected_start) { 1657 di->vbus_detected_start = false; 1658 ret = ab8500_charger_detect_usb_type(di); 1659 if (!ret) { 1660 ab8500_charger_set_usb_connected(di, 1661 true); 1662 ab8500_power_supply_changed(di, 1663 &di->usb_chg.psy); 1664 } 1665 } 1666 } 1667 } 1668} 1669 1670/** 1671 * ab8500_charger_usb_link_status_work() - work to detect USB type 1672 * @work: pointer to the work_struct structure 1673 * 1674 * Detect the type of USB plugged 1675 */ 1676static void ab8500_charger_usb_link_status_work(struct work_struct *work) 1677{ 1678 int ret; 1679 1680 struct ab8500_charger *di = container_of(work, 1681 struct ab8500_charger, usb_link_status_work); 1682 1683 /* 1684 * Since we can't be sure that the events are received 1685 * synchronously, we have the check if is 1686 * connected by reading the status register 1687 */ 1688 ret = ab8500_charger_detect_chargers(di); 1689 if (ret < 0) 1690 return; 1691 1692 if (!(ret & USB_PW_CONN)) { 1693 di->vbus_detected = 0; 1694 ab8500_charger_set_usb_connected(di, false); 1695 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1696 } else { 1697 di->vbus_detected = 1; 1698 ret = ab8500_charger_read_usb_type(di); 1699 if (!ret) { 1700 /* Update maximum input current */ 1701 ret = ab8500_charger_set_vbus_in_curr(di, 1702 di->max_usb_in_curr); 1703 if (ret) 1704 return; 1705 1706 ab8500_charger_set_usb_connected(di, true); 1707 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1708 } else if (ret == -ENXIO) { 1709 /* No valid charger type detected */ 1710 ab8500_charger_set_usb_connected(di, false); 1711 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1712 } 1713 } 1714} 1715 1716static void ab8500_charger_usb_state_changed_work(struct work_struct *work) 1717{ 1718 int ret; 1719 unsigned long flags; 1720 1721 struct ab8500_charger *di = container_of(work, 1722 struct ab8500_charger, usb_state_changed_work); 1723 1724 if (!di->vbus_detected) 1725 return; 1726 1727 spin_lock_irqsave(&di->usb_state.usb_lock, flags); 1728 di->usb_state.usb_changed = false; 1729 spin_unlock_irqrestore(&di->usb_state.usb_lock, flags); 1730 1731 /* 1732 * wait for some time until you get updates from the usb stack 1733 * and negotiations are completed 1734 */ 1735 msleep(250); 1736 1737 if (di->usb_state.usb_changed) 1738 return; 1739 1740 dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n", 1741 __func__, di->usb_state.state, di->usb_state.usb_current); 1742 1743 switch (di->usb_state.state) { 1744 case AB8500_BM_USB_STATE_RESET_HS: 1745 case AB8500_BM_USB_STATE_RESET_FS: 1746 case AB8500_BM_USB_STATE_SUSPEND: 1747 case AB8500_BM_USB_STATE_MAX: 1748 ab8500_charger_set_usb_connected(di, false); 1749 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1750 break; 1751 1752 case AB8500_BM_USB_STATE_RESUME: 1753 /* 1754 * when suspend->resume there should be delay 1755 * of 1sec for enabling charging 1756 */ 1757 msleep(1000); 1758 /* Intentional fall through */ 1759 case AB8500_BM_USB_STATE_CONFIGURED: 1760 /* 1761 * USB is configured, enable charging with the charging 1762 * input current obtained from USB driver 1763 */ 1764 if (!ab8500_charger_get_usb_cur(di)) { 1765 /* Update maximum input current */ 1766 ret = ab8500_charger_set_vbus_in_curr(di, 1767 di->max_usb_in_curr); 1768 if (ret) 1769 return; 1770 1771 ab8500_charger_set_usb_connected(di, true); 1772 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1773 } 1774 break; 1775 1776 default: 1777 break; 1778 }; 1779} 1780 1781/** 1782 * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status 1783 * @work: pointer to the work_struct structure 1784 * 1785 * Work queue function for checking the USB charger Not OK status 1786 */ 1787static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work) 1788{ 1789 int ret; 1790 u8 reg_value; 1791 bool prev_status; 1792 1793 struct ab8500_charger *di = container_of(work, 1794 struct ab8500_charger, check_usbchgnotok_work.work); 1795 1796 /* Check if the status bit for usbchargernotok is still active */ 1797 ret = abx500_get_register_interruptible(di->dev, 1798 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value); 1799 if (ret < 0) { 1800 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1801 return; 1802 } 1803 prev_status = di->flags.usbchargernotok; 1804 1805 if (reg_value & VBUS_CH_NOK) { 1806 di->flags.usbchargernotok = true; 1807 /* Check again in 1sec */ 1808 queue_delayed_work(di->charger_wq, 1809 &di->check_usbchgnotok_work, HZ); 1810 } else { 1811 di->flags.usbchargernotok = false; 1812 di->flags.vbus_collapse = false; 1813 } 1814 1815 if (prev_status != di->flags.usbchargernotok) 1816 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1817} 1818 1819/** 1820 * ab8500_charger_check_main_thermal_prot_work() - check main thermal status 1821 * @work: pointer to the work_struct structure 1822 * 1823 * Work queue function for checking the Main thermal prot status 1824 */ 1825static void ab8500_charger_check_main_thermal_prot_work( 1826 struct work_struct *work) 1827{ 1828 int ret; 1829 u8 reg_value; 1830 1831 struct ab8500_charger *di = container_of(work, 1832 struct ab8500_charger, check_main_thermal_prot_work); 1833 1834 /* Check if the status bit for main_thermal_prot is still active */ 1835 ret = abx500_get_register_interruptible(di->dev, 1836 AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value); 1837 if (ret < 0) { 1838 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1839 return; 1840 } 1841 if (reg_value & MAIN_CH_TH_PROT) 1842 di->flags.main_thermal_prot = true; 1843 else 1844 di->flags.main_thermal_prot = false; 1845 1846 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1847} 1848 1849/** 1850 * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status 1851 * @work: pointer to the work_struct structure 1852 * 1853 * Work queue function for checking the USB thermal prot status 1854 */ 1855static void ab8500_charger_check_usb_thermal_prot_work( 1856 struct work_struct *work) 1857{ 1858 int ret; 1859 u8 reg_value; 1860 1861 struct ab8500_charger *di = container_of(work, 1862 struct ab8500_charger, check_usb_thermal_prot_work); 1863 1864 /* Check if the status bit for usb_thermal_prot is still active */ 1865 ret = abx500_get_register_interruptible(di->dev, 1866 AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value); 1867 if (ret < 0) { 1868 dev_err(di->dev, "%s ab8500 read failed\n", __func__); 1869 return; 1870 } 1871 if (reg_value & USB_CH_TH_PROT) 1872 di->flags.usb_thermal_prot = true; 1873 else 1874 di->flags.usb_thermal_prot = false; 1875 1876 ab8500_power_supply_changed(di, &di->usb_chg.psy); 1877} 1878 1879/** 1880 * ab8500_charger_mainchunplugdet_handler() - main charger unplugged 1881 * @irq: interrupt number 1882 * @_di: pointer to the ab8500_charger structure 1883 * 1884 * Returns IRQ status(IRQ_HANDLED) 1885 */ 1886static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di) 1887{ 1888 struct ab8500_charger *di = _di; 1889 1890 dev_dbg(di->dev, "Main charger unplugged\n"); 1891 queue_work(di->charger_wq, &di->ac_work); 1892 1893 return IRQ_HANDLED; 1894} 1895 1896/** 1897 * ab8500_charger_mainchplugdet_handler() - main charger plugged 1898 * @irq: interrupt number 1899 * @_di: pointer to the ab8500_charger structure 1900 * 1901 * Returns IRQ status(IRQ_HANDLED) 1902 */ 1903static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di) 1904{ 1905 struct ab8500_charger *di = _di; 1906 1907 dev_dbg(di->dev, "Main charger plugged\n"); 1908 queue_work(di->charger_wq, &di->ac_work); 1909 1910 return IRQ_HANDLED; 1911} 1912 1913/** 1914 * ab8500_charger_mainextchnotok_handler() - main charger not ok 1915 * @irq: interrupt number 1916 * @_di: pointer to the ab8500_charger structure 1917 * 1918 * Returns IRQ status(IRQ_HANDLED) 1919 */ 1920static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di) 1921{ 1922 struct ab8500_charger *di = _di; 1923 1924 dev_dbg(di->dev, "Main charger not ok\n"); 1925 di->flags.mainextchnotok = true; 1926 ab8500_power_supply_changed(di, &di->ac_chg.psy); 1927 1928 /* Schedule a new HW failure check */ 1929 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); 1930 1931 return IRQ_HANDLED; 1932} 1933 1934/** 1935 * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger 1936 * thermal protection threshold 1937 * @irq: interrupt number 1938 * @_di: pointer to the ab8500_charger structure 1939 * 1940 * Returns IRQ status(IRQ_HANDLED) 1941 */ 1942static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di) 1943{ 1944 struct ab8500_charger *di = _di; 1945 1946 dev_dbg(di->dev, 1947 "Die temp above Main charger thermal protection threshold\n"); 1948 queue_work(di->charger_wq, &di->check_main_thermal_prot_work); 1949 1950 return IRQ_HANDLED; 1951} 1952 1953/** 1954 * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger 1955 * thermal protection threshold 1956 * @irq: interrupt number 1957 * @_di: pointer to the ab8500_charger structure 1958 * 1959 * Returns IRQ status(IRQ_HANDLED) 1960 */ 1961static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di) 1962{ 1963 struct ab8500_charger *di = _di; 1964 1965 dev_dbg(di->dev, 1966 "Die temp ok for Main charger thermal protection threshold\n"); 1967 queue_work(di->charger_wq, &di->check_main_thermal_prot_work); 1968 1969 return IRQ_HANDLED; 1970} 1971 1972/** 1973 * ab8500_charger_vbusdetf_handler() - VBUS falling detected 1974 * @irq: interrupt number 1975 * @_di: pointer to the ab8500_charger structure 1976 * 1977 * Returns IRQ status(IRQ_HANDLED) 1978 */ 1979static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di) 1980{ 1981 struct ab8500_charger *di = _di; 1982 1983 dev_dbg(di->dev, "VBUS falling detected\n"); 1984 queue_work(di->charger_wq, &di->detect_usb_type_work); 1985 1986 return IRQ_HANDLED; 1987} 1988 1989/** 1990 * ab8500_charger_vbusdetr_handler() - VBUS rising detected 1991 * @irq: interrupt number 1992 * @_di: pointer to the ab8500_charger structure 1993 * 1994 * Returns IRQ status(IRQ_HANDLED) 1995 */ 1996static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di) 1997{ 1998 struct ab8500_charger *di = _di; 1999 2000 di->vbus_detected = true; 2001 dev_dbg(di->dev, "VBUS rising detected\n"); 2002 queue_work(di->charger_wq, &di->detect_usb_type_work); 2003 2004 return IRQ_HANDLED; 2005} 2006 2007/** 2008 * ab8500_charger_usblinkstatus_handler() - USB link status has changed 2009 * @irq: interrupt number 2010 * @_di: pointer to the ab8500_charger structure 2011 * 2012 * Returns IRQ status(IRQ_HANDLED) 2013 */ 2014static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di) 2015{ 2016 struct ab8500_charger *di = _di; 2017 2018 dev_dbg(di->dev, "USB link status changed\n"); 2019 2020 queue_work(di->charger_wq, &di->usb_link_status_work); 2021 2022 return IRQ_HANDLED; 2023} 2024 2025/** 2026 * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger 2027 * thermal protection threshold 2028 * @irq: interrupt number 2029 * @_di: pointer to the ab8500_charger structure 2030 * 2031 * Returns IRQ status(IRQ_HANDLED) 2032 */ 2033static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di) 2034{ 2035 struct ab8500_charger *di = _di; 2036 2037 dev_dbg(di->dev, 2038 "Die temp above USB charger thermal protection threshold\n"); 2039 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); 2040 2041 return IRQ_HANDLED; 2042} 2043 2044/** 2045 * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger 2046 * thermal protection threshold 2047 * @irq: interrupt number 2048 * @_di: pointer to the ab8500_charger structure 2049 * 2050 * Returns IRQ status(IRQ_HANDLED) 2051 */ 2052static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di) 2053{ 2054 struct ab8500_charger *di = _di; 2055 2056 dev_dbg(di->dev, 2057 "Die temp ok for USB charger thermal protection threshold\n"); 2058 queue_work(di->charger_wq, &di->check_usb_thermal_prot_work); 2059 2060 return IRQ_HANDLED; 2061} 2062 2063/** 2064 * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected 2065 * @irq: interrupt number 2066 * @_di: pointer to the ab8500_charger structure 2067 * 2068 * Returns IRQ status(IRQ_HANDLED) 2069 */ 2070static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di) 2071{ 2072 struct ab8500_charger *di = _di; 2073 2074 dev_dbg(di->dev, "Not allowed USB charger detected\n"); 2075 queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0); 2076 2077 return IRQ_HANDLED; 2078} 2079 2080/** 2081 * ab8500_charger_chwdexp_handler() - Charger watchdog expired 2082 * @irq: interrupt number 2083 * @_di: pointer to the ab8500_charger structure 2084 * 2085 * Returns IRQ status(IRQ_HANDLED) 2086 */ 2087static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di) 2088{ 2089 struct ab8500_charger *di = _di; 2090 2091 dev_dbg(di->dev, "Charger watchdog expired\n"); 2092 2093 /* 2094 * The charger that was online when the watchdog expired 2095 * needs to be restarted for charging to start again 2096 */ 2097 if (di->ac.charger_online) { 2098 di->ac.wd_expired = true; 2099 ab8500_power_supply_changed(di, &di->ac_chg.psy); 2100 } 2101 if (di->usb.charger_online) { 2102 di->usb.wd_expired = true; 2103 ab8500_power_supply_changed(di, &di->usb_chg.psy); 2104 } 2105 2106 return IRQ_HANDLED; 2107} 2108 2109/** 2110 * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected 2111 * @irq: interrupt number 2112 * @_di: pointer to the ab8500_charger structure 2113 * 2114 * Returns IRQ status(IRQ_HANDLED) 2115 */ 2116static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di) 2117{ 2118 struct ab8500_charger *di = _di; 2119 2120 dev_dbg(di->dev, "VBUS overvoltage detected\n"); 2121 di->flags.vbus_ovv = true; 2122 ab8500_power_supply_changed(di, &di->usb_chg.psy); 2123 2124 /* Schedule a new HW failure check */ 2125 queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0); 2126 2127 return IRQ_HANDLED; 2128} 2129 2130/** 2131 * ab8500_charger_ac_get_property() - get the ac/mains properties 2132 * @psy: pointer to the power_supply structure 2133 * @psp: pointer to the power_supply_property structure 2134 * @val: pointer to the power_supply_propval union 2135 * 2136 * This function gets called when an application tries to get the ac/mains 2137 * properties by reading the sysfs files. 2138 * AC/Mains properties are online, present and voltage. 2139 * online: ac/mains charging is in progress or not 2140 * present: presence of the ac/mains 2141 * voltage: AC/Mains voltage 2142 * Returns error code in case of failure else 0(on success) 2143 */ 2144static int ab8500_charger_ac_get_property(struct power_supply *psy, 2145 enum power_supply_property psp, 2146 union power_supply_propval *val) 2147{ 2148 struct ab8500_charger *di; 2149 2150 di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy)); 2151 2152 switch (psp) { 2153 case POWER_SUPPLY_PROP_HEALTH: 2154 if (di->flags.mainextchnotok) 2155 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 2156 else if (di->ac.wd_expired || di->usb.wd_expired) 2157 val->intval = POWER_SUPPLY_HEALTH_DEAD; 2158 else if (di->flags.main_thermal_prot) 2159 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 2160 else 2161 val->intval = POWER_SUPPLY_HEALTH_GOOD; 2162 break; 2163 case POWER_SUPPLY_PROP_ONLINE: 2164 val->intval = di->ac.charger_online; 2165 break; 2166 case POWER_SUPPLY_PROP_PRESENT: 2167 val->intval = di->ac.charger_connected; 2168 break; 2169 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2170 di->ac.charger_voltage = ab8500_charger_get_ac_voltage(di); 2171 val->intval = di->ac.charger_voltage * 1000; 2172 break; 2173 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 2174 /* 2175 * This property is used to indicate when CV mode is entered 2176 * for the AC charger 2177 */ 2178 di->ac.cv_active = ab8500_charger_ac_cv(di); 2179 val->intval = di->ac.cv_active; 2180 break; 2181 case POWER_SUPPLY_PROP_CURRENT_NOW: 2182 val->intval = ab8500_charger_get_ac_current(di) * 1000; 2183 break; 2184 default: 2185 return -EINVAL; 2186 } 2187 return 0; 2188} 2189 2190/** 2191 * ab8500_charger_usb_get_property() - get the usb properties 2192 * @psy: pointer to the power_supply structure 2193 * @psp: pointer to the power_supply_property structure 2194 * @val: pointer to the power_supply_propval union 2195 * 2196 * This function gets called when an application tries to get the usb 2197 * properties by reading the sysfs files. 2198 * USB properties are online, present and voltage. 2199 * online: usb charging is in progress or not 2200 * present: presence of the usb 2201 * voltage: vbus voltage 2202 * Returns error code in case of failure else 0(on success) 2203 */ 2204static int ab8500_charger_usb_get_property(struct power_supply *psy, 2205 enum power_supply_property psp, 2206 union power_supply_propval *val) 2207{ 2208 struct ab8500_charger *di; 2209 2210 di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy)); 2211 2212 switch (psp) { 2213 case POWER_SUPPLY_PROP_HEALTH: 2214 if (di->flags.usbchargernotok) 2215 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; 2216 else if (di->ac.wd_expired || di->usb.wd_expired) 2217 val->intval = POWER_SUPPLY_HEALTH_DEAD; 2218 else if (di->flags.usb_thermal_prot) 2219 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; 2220 else if (di->flags.vbus_ovv) 2221 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; 2222 else 2223 val->intval = POWER_SUPPLY_HEALTH_GOOD; 2224 break; 2225 case POWER_SUPPLY_PROP_ONLINE: 2226 val->intval = di->usb.charger_online; 2227 break; 2228 case POWER_SUPPLY_PROP_PRESENT: 2229 val->intval = di->usb.charger_connected; 2230 break; 2231 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 2232 di->usb.charger_voltage = ab8500_charger_get_vbus_voltage(di); 2233 val->intval = di->usb.charger_voltage * 1000; 2234 break; 2235 case POWER_SUPPLY_PROP_VOLTAGE_AVG: 2236 /* 2237 * This property is used to indicate when CV mode is entered 2238 * for the USB charger 2239 */ 2240 di->usb.cv_active = ab8500_charger_usb_cv(di); 2241 val->intval = di->usb.cv_active; 2242 break; 2243 case POWER_SUPPLY_PROP_CURRENT_NOW: 2244 val->intval = ab8500_charger_get_usb_current(di) * 1000; 2245 break; 2246 case POWER_SUPPLY_PROP_CURRENT_AVG: 2247 /* 2248 * This property is used to indicate when VBUS has collapsed 2249 * due to too high output current from the USB charger 2250 */ 2251 if (di->flags.vbus_collapse) 2252 val->intval = 1; 2253 else 2254 val->intval = 0; 2255 break; 2256 default: 2257 return -EINVAL; 2258 } 2259 return 0; 2260} 2261 2262/** 2263 * ab8500_charger_init_hw_registers() - Set up charger related registers 2264 * @di: pointer to the ab8500_charger structure 2265 * 2266 * Set up charger OVV, watchdog and maximum voltage registers as well as 2267 * charging of the backup battery 2268 */ 2269static int ab8500_charger_init_hw_registers(struct ab8500_charger *di) 2270{ 2271 int ret = 0; 2272 2273 /* Setup maximum charger current and voltage for ABB cut2.0 */ 2274 if (!is_ab8500_1p1_or_earlier(di->parent)) { 2275 ret = abx500_set_register_interruptible(di->dev, 2276 AB8500_CHARGER, 2277 AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6); 2278 if (ret) { 2279 dev_err(di->dev, 2280 "failed to set CH_VOLT_LVL_MAX_REG\n"); 2281 goto out; 2282 } 2283 2284 ret = abx500_set_register_interruptible(di->dev, 2285 AB8500_CHARGER, 2286 AB8500_CH_OPT_CRNTLVL_MAX_REG, CH_OP_CUR_LVL_1P6); 2287 if (ret) { 2288 dev_err(di->dev, 2289 "failed to set CH_OPT_CRNTLVL_MAX_REG\n"); 2290 goto out; 2291 } 2292 } 2293 2294 /* VBUS OVV set to 6.3V and enable automatic current limitiation */ 2295 ret = abx500_set_register_interruptible(di->dev, 2296 AB8500_CHARGER, 2297 AB8500_USBCH_CTRL2_REG, 2298 VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA); 2299 if (ret) { 2300 dev_err(di->dev, "failed to set VBUS OVV\n"); 2301 goto out; 2302 } 2303 2304 /* Enable main watchdog in OTP */ 2305 ret = abx500_set_register_interruptible(di->dev, 2306 AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD); 2307 if (ret) { 2308 dev_err(di->dev, "failed to enable main WD in OTP\n"); 2309 goto out; 2310 } 2311 2312 /* Enable main watchdog */ 2313 ret = abx500_set_register_interruptible(di->dev, 2314 AB8500_SYS_CTRL2_BLOCK, 2315 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA); 2316 if (ret) { 2317 dev_err(di->dev, "faile to enable main watchdog\n"); 2318 goto out; 2319 } 2320 2321 /* 2322 * Due to internal synchronisation, Enable and Kick watchdog bits 2323 * cannot be enabled in a single write. 2324 * A minimum delay of 2*32 kHz period (62.5µs) must be inserted 2325 * between writing Enable then Kick bits. 2326 */ 2327 udelay(63); 2328 2329 /* Kick main watchdog */ 2330 ret = abx500_set_register_interruptible(di->dev, 2331 AB8500_SYS_CTRL2_BLOCK, 2332 AB8500_MAIN_WDOG_CTRL_REG, 2333 (MAIN_WDOG_ENA | MAIN_WDOG_KICK)); 2334 if (ret) { 2335 dev_err(di->dev, "failed to kick main watchdog\n"); 2336 goto out; 2337 } 2338 2339 /* Disable main watchdog */ 2340 ret = abx500_set_register_interruptible(di->dev, 2341 AB8500_SYS_CTRL2_BLOCK, 2342 AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS); 2343 if (ret) { 2344 dev_err(di->dev, "failed to disable main watchdog\n"); 2345 goto out; 2346 } 2347 2348 /* Set watchdog timeout */ 2349 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 2350 AB8500_CH_WD_TIMER_REG, WD_TIMER); 2351 if (ret) { 2352 dev_err(di->dev, "failed to set charger watchdog timeout\n"); 2353 goto out; 2354 } 2355 2356 /* Backup battery voltage and current */ 2357 ret = abx500_set_register_interruptible(di->dev, 2358 AB8500_RTC, 2359 AB8500_RTC_BACKUP_CHG_REG, 2360 di->bat->bkup_bat_v | 2361 di->bat->bkup_bat_i); 2362 if (ret) { 2363 dev_err(di->dev, "failed to setup backup battery charging\n"); 2364 goto out; 2365 } 2366 2367 /* Enable backup battery charging */ 2368 abx500_mask_and_set_register_interruptible(di->dev, 2369 AB8500_RTC, AB8500_RTC_CTRL_REG, 2370 RTC_BUP_CH_ENA, RTC_BUP_CH_ENA); 2371 if (ret < 0) 2372 dev_err(di->dev, "%s mask and set failed\n", __func__); 2373 2374out: 2375 return ret; 2376} 2377 2378/* 2379 * ab8500 charger driver interrupts and their respective isr 2380 */ 2381static struct ab8500_charger_interrupts ab8500_charger_irq[] = { 2382 {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler}, 2383 {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler}, 2384 {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler}, 2385 {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler}, 2386 {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler}, 2387 {"VBUS_DET_F", ab8500_charger_vbusdetf_handler}, 2388 {"VBUS_DET_R", ab8500_charger_vbusdetr_handler}, 2389 {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler}, 2390 {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler}, 2391 {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler}, 2392 {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler}, 2393 {"VBUS_OVV", ab8500_charger_vbusovv_handler}, 2394 {"CH_WD_EXP", ab8500_charger_chwdexp_handler}, 2395}; 2396 2397static int ab8500_charger_usb_notifier_call(struct notifier_block *nb, 2398 unsigned long event, void *power) 2399{ 2400 struct ab8500_charger *di = 2401 container_of(nb, struct ab8500_charger, nb); 2402 enum ab8500_usb_state bm_usb_state; 2403 unsigned mA = *((unsigned *)power); 2404 2405 if (event != USB_EVENT_VBUS) { 2406 dev_dbg(di->dev, "not a standard host, returning\n"); 2407 return NOTIFY_DONE; 2408 } 2409 2410 /* TODO: State is fabricate here. See if charger really needs USB 2411 * state or if mA is enough 2412 */ 2413 if ((di->usb_state.usb_current == 2) && (mA > 2)) 2414 bm_usb_state = AB8500_BM_USB_STATE_RESUME; 2415 else if (mA == 0) 2416 bm_usb_state = AB8500_BM_USB_STATE_RESET_HS; 2417 else if (mA == 2) 2418 bm_usb_state = AB8500_BM_USB_STATE_SUSPEND; 2419 else if (mA >= 8) /* 8, 100, 500 */ 2420 bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED; 2421 else /* Should never occur */ 2422 bm_usb_state = AB8500_BM_USB_STATE_RESET_FS; 2423 2424 dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n", 2425 __func__, bm_usb_state, mA); 2426 2427 spin_lock(&di->usb_state.usb_lock); 2428 di->usb_state.usb_changed = true; 2429 spin_unlock(&di->usb_state.usb_lock); 2430 2431 di->usb_state.state = bm_usb_state; 2432 di->usb_state.usb_current = mA; 2433 2434 queue_work(di->charger_wq, &di->usb_state_changed_work); 2435 2436 return NOTIFY_OK; 2437} 2438 2439#if defined(CONFIG_PM) 2440static int ab8500_charger_resume(struct platform_device *pdev) 2441{ 2442 int ret; 2443 struct ab8500_charger *di = platform_get_drvdata(pdev); 2444 2445 /* 2446 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 2447 * logic. That means we have to continously kick the charger 2448 * watchdog even when no charger is connected. This is only 2449 * valid once the AC charger has been enabled. This is 2450 * a bug that is not handled by the algorithm and the 2451 * watchdog have to be kicked by the charger driver 2452 * when the AC charger is disabled 2453 */ 2454 if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) { 2455 ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER, 2456 AB8500_CHARG_WD_CTRL, CHARG_WD_KICK); 2457 if (ret) 2458 dev_err(di->dev, "Failed to kick WD!\n"); 2459 2460 /* If not already pending start a new timer */ 2461 if (!delayed_work_pending( 2462 &di->kick_wd_work)) { 2463 queue_delayed_work(di->charger_wq, &di->kick_wd_work, 2464 round_jiffies(WD_KICK_INTERVAL)); 2465 } 2466 } 2467 2468 /* If we still have a HW failure, schedule a new check */ 2469 if (di->flags.mainextchnotok || di->flags.vbus_ovv) { 2470 queue_delayed_work(di->charger_wq, 2471 &di->check_hw_failure_work, 0); 2472 } 2473 2474 return 0; 2475} 2476 2477static int ab8500_charger_suspend(struct platform_device *pdev, 2478 pm_message_t state) 2479{ 2480 struct ab8500_charger *di = platform_get_drvdata(pdev); 2481 2482 /* Cancel any pending HW failure check */ 2483 if (delayed_work_pending(&di->check_hw_failure_work)) 2484 cancel_delayed_work(&di->check_hw_failure_work); 2485 2486 return 0; 2487} 2488#else 2489#define ab8500_charger_suspend NULL 2490#define ab8500_charger_resume NULL 2491#endif 2492 2493static int __devexit ab8500_charger_remove(struct platform_device *pdev) 2494{ 2495 struct ab8500_charger *di = platform_get_drvdata(pdev); 2496 int i, irq, ret; 2497 2498 /* Disable AC charging */ 2499 ab8500_charger_ac_en(&di->ac_chg, false, 0, 0); 2500 2501 /* Disable USB charging */ 2502 ab8500_charger_usb_en(&di->usb_chg, false, 0, 0); 2503 2504 /* Disable interrupts */ 2505 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { 2506 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2507 free_irq(irq, di); 2508 } 2509 2510 /* disable the regulator */ 2511 regulator_put(di->regu); 2512 2513 /* Backup battery voltage and current disable */ 2514 ret = abx500_mask_and_set_register_interruptible(di->dev, 2515 AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0); 2516 if (ret < 0) 2517 dev_err(di->dev, "%s mask and set failed\n", __func__); 2518 2519 usb_unregister_notifier(di->usb_phy, &di->nb); 2520 usb_put_phy(di->usb_phy); 2521 2522 /* Delete the work queue */ 2523 destroy_workqueue(di->charger_wq); 2524 2525 flush_scheduled_work(); 2526 power_supply_unregister(&di->usb_chg.psy); 2527 power_supply_unregister(&di->ac_chg.psy); 2528 platform_set_drvdata(pdev, NULL); 2529 kfree(di); 2530 2531 return 0; 2532} 2533 2534static int __devinit ab8500_charger_probe(struct platform_device *pdev) 2535{ 2536 int irq, i, charger_status, ret = 0; 2537 struct abx500_bm_plat_data *plat_data = pdev->dev.platform_data; 2538 struct ab8500_charger *di; 2539 2540 if (!plat_data) { 2541 dev_err(&pdev->dev, "No platform data\n"); 2542 return -EINVAL; 2543 } 2544 2545 di = kzalloc(sizeof(*di), GFP_KERNEL); 2546 if (!di) 2547 return -ENOMEM; 2548 2549 /* get parent data */ 2550 di->dev = &pdev->dev; 2551 di->parent = dev_get_drvdata(pdev->dev.parent); 2552 di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0"); 2553 2554 /* initialize lock */ 2555 spin_lock_init(&di->usb_state.usb_lock); 2556 2557 /* get charger specific platform data */ 2558 di->pdata = plat_data->charger; 2559 if (!di->pdata) { 2560 dev_err(di->dev, "no charger platform data supplied\n"); 2561 ret = -EINVAL; 2562 goto free_device_info; 2563 } 2564 2565 /* get battery specific platform data */ 2566 di->bat = plat_data->battery; 2567 if (!di->bat) { 2568 dev_err(di->dev, "no battery platform data supplied\n"); 2569 ret = -EINVAL; 2570 goto free_device_info; 2571 } 2572 2573 di->autopower = false; 2574 2575 /* AC supply */ 2576 /* power_supply base class */ 2577 di->ac_chg.psy.name = "ab8500_ac"; 2578 di->ac_chg.psy.type = POWER_SUPPLY_TYPE_MAINS; 2579 di->ac_chg.psy.properties = ab8500_charger_ac_props; 2580 di->ac_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_ac_props); 2581 di->ac_chg.psy.get_property = ab8500_charger_ac_get_property; 2582 di->ac_chg.psy.supplied_to = di->pdata->supplied_to; 2583 di->ac_chg.psy.num_supplicants = di->pdata->num_supplicants; 2584 /* ux500_charger sub-class */ 2585 di->ac_chg.ops.enable = &ab8500_charger_ac_en; 2586 di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; 2587 di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current; 2588 di->ac_chg.max_out_volt = ab8500_charger_voltage_map[ 2589 ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; 2590 di->ac_chg.max_out_curr = ab8500_charger_current_map[ 2591 ARRAY_SIZE(ab8500_charger_current_map) - 1]; 2592 2593 /* USB supply */ 2594 /* power_supply base class */ 2595 di->usb_chg.psy.name = "ab8500_usb"; 2596 di->usb_chg.psy.type = POWER_SUPPLY_TYPE_USB; 2597 di->usb_chg.psy.properties = ab8500_charger_usb_props; 2598 di->usb_chg.psy.num_properties = ARRAY_SIZE(ab8500_charger_usb_props); 2599 di->usb_chg.psy.get_property = ab8500_charger_usb_get_property; 2600 di->usb_chg.psy.supplied_to = di->pdata->supplied_to; 2601 di->usb_chg.psy.num_supplicants = di->pdata->num_supplicants; 2602 /* ux500_charger sub-class */ 2603 di->usb_chg.ops.enable = &ab8500_charger_usb_en; 2604 di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick; 2605 di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current; 2606 di->usb_chg.max_out_volt = ab8500_charger_voltage_map[ 2607 ARRAY_SIZE(ab8500_charger_voltage_map) - 1]; 2608 di->usb_chg.max_out_curr = ab8500_charger_current_map[ 2609 ARRAY_SIZE(ab8500_charger_current_map) - 1]; 2610 2611 2612 /* Create a work queue for the charger */ 2613 di->charger_wq = 2614 create_singlethread_workqueue("ab8500_charger_wq"); 2615 if (di->charger_wq == NULL) { 2616 dev_err(di->dev, "failed to create work queue\n"); 2617 goto free_device_info; 2618 } 2619 2620 /* Init work for HW failure check */ 2621 INIT_DELAYED_WORK_DEFERRABLE(&di->check_hw_failure_work, 2622 ab8500_charger_check_hw_failure_work); 2623 INIT_DELAYED_WORK_DEFERRABLE(&di->check_usbchgnotok_work, 2624 ab8500_charger_check_usbchargernotok_work); 2625 2626 /* 2627 * For ABB revision 1.0 and 1.1 there is a bug in the watchdog 2628 * logic. That means we have to continously kick the charger 2629 * watchdog even when no charger is connected. This is only 2630 * valid once the AC charger has been enabled. This is 2631 * a bug that is not handled by the algorithm and the 2632 * watchdog have to be kicked by the charger driver 2633 * when the AC charger is disabled 2634 */ 2635 INIT_DELAYED_WORK_DEFERRABLE(&di->kick_wd_work, 2636 ab8500_charger_kick_watchdog_work); 2637 2638 INIT_DELAYED_WORK_DEFERRABLE(&di->check_vbat_work, 2639 ab8500_charger_check_vbat_work); 2640 2641 /* Init work for charger detection */ 2642 INIT_WORK(&di->usb_link_status_work, 2643 ab8500_charger_usb_link_status_work); 2644 INIT_WORK(&di->ac_work, ab8500_charger_ac_work); 2645 INIT_WORK(&di->detect_usb_type_work, 2646 ab8500_charger_detect_usb_type_work); 2647 2648 INIT_WORK(&di->usb_state_changed_work, 2649 ab8500_charger_usb_state_changed_work); 2650 2651 /* Init work for checking HW status */ 2652 INIT_WORK(&di->check_main_thermal_prot_work, 2653 ab8500_charger_check_main_thermal_prot_work); 2654 INIT_WORK(&di->check_usb_thermal_prot_work, 2655 ab8500_charger_check_usb_thermal_prot_work); 2656 2657 /* 2658 * VDD ADC supply needs to be enabled from this driver when there 2659 * is a charger connected to avoid erroneous BTEMP_HIGH/LOW 2660 * interrupts during charging 2661 */ 2662 di->regu = regulator_get(di->dev, "vddadc"); 2663 if (IS_ERR(di->regu)) { 2664 ret = PTR_ERR(di->regu); 2665 dev_err(di->dev, "failed to get vddadc regulator\n"); 2666 goto free_charger_wq; 2667 } 2668 2669 2670 /* Initialize OVV, and other registers */ 2671 ret = ab8500_charger_init_hw_registers(di); 2672 if (ret) { 2673 dev_err(di->dev, "failed to initialize ABB registers\n"); 2674 goto free_regulator; 2675 } 2676 2677 /* Register AC charger class */ 2678 ret = power_supply_register(di->dev, &di->ac_chg.psy); 2679 if (ret) { 2680 dev_err(di->dev, "failed to register AC charger\n"); 2681 goto free_regulator; 2682 } 2683 2684 /* Register USB charger class */ 2685 ret = power_supply_register(di->dev, &di->usb_chg.psy); 2686 if (ret) { 2687 dev_err(di->dev, "failed to register USB charger\n"); 2688 goto free_ac; 2689 } 2690 2691 di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2); 2692 if (IS_ERR_OR_NULL(di->usb_phy)) { 2693 dev_err(di->dev, "failed to get usb transceiver\n"); 2694 ret = -EINVAL; 2695 goto free_usb; 2696 } 2697 di->nb.notifier_call = ab8500_charger_usb_notifier_call; 2698 ret = usb_register_notifier(di->usb_phy, &di->nb); 2699 if (ret) { 2700 dev_err(di->dev, "failed to register usb notifier\n"); 2701 goto put_usb_phy; 2702 } 2703 2704 /* Identify the connected charger types during startup */ 2705 charger_status = ab8500_charger_detect_chargers(di); 2706 if (charger_status & AC_PW_CONN) { 2707 di->ac.charger_connected = 1; 2708 di->ac_conn = true; 2709 ab8500_power_supply_changed(di, &di->ac_chg.psy); 2710 sysfs_notify(&di->ac_chg.psy.dev->kobj, NULL, "present"); 2711 } 2712 2713 if (charger_status & USB_PW_CONN) { 2714 dev_dbg(di->dev, "VBUS Detect during startup\n"); 2715 di->vbus_detected = true; 2716 di->vbus_detected_start = true; 2717 queue_work(di->charger_wq, 2718 &di->detect_usb_type_work); 2719 } 2720 2721 /* Register interrupts */ 2722 for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) { 2723 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2724 ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr, 2725 IRQF_SHARED | IRQF_NO_SUSPEND, 2726 ab8500_charger_irq[i].name, di); 2727 2728 if (ret != 0) { 2729 dev_err(di->dev, "failed to request %s IRQ %d: %d\n" 2730 , ab8500_charger_irq[i].name, irq, ret); 2731 goto free_irq; 2732 } 2733 dev_dbg(di->dev, "Requested %s IRQ %d: %d\n", 2734 ab8500_charger_irq[i].name, irq, ret); 2735 } 2736 2737 platform_set_drvdata(pdev, di); 2738 2739 return ret; 2740 2741free_irq: 2742 usb_unregister_notifier(di->usb_phy, &di->nb); 2743 2744 /* We also have to free all successfully registered irqs */ 2745 for (i = i - 1; i >= 0; i--) { 2746 irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name); 2747 free_irq(irq, di); 2748 } 2749put_usb_phy: 2750 usb_put_phy(di->usb_phy); 2751free_usb: 2752 power_supply_unregister(&di->usb_chg.psy); 2753free_ac: 2754 power_supply_unregister(&di->ac_chg.psy); 2755free_regulator: 2756 regulator_put(di->regu); 2757free_charger_wq: 2758 destroy_workqueue(di->charger_wq); 2759free_device_info: 2760 kfree(di); 2761 2762 return ret; 2763} 2764 2765static struct platform_driver ab8500_charger_driver = { 2766 .probe = ab8500_charger_probe, 2767 .remove = __devexit_p(ab8500_charger_remove), 2768 .suspend = ab8500_charger_suspend, 2769 .resume = ab8500_charger_resume, 2770 .driver = { 2771 .name = "ab8500-charger", 2772 .owner = THIS_MODULE, 2773 }, 2774}; 2775 2776static int __init ab8500_charger_init(void) 2777{ 2778 return platform_driver_register(&ab8500_charger_driver); 2779} 2780 2781static void __exit ab8500_charger_exit(void) 2782{ 2783 platform_driver_unregister(&ab8500_charger_driver); 2784} 2785 2786subsys_initcall_sync(ab8500_charger_init); 2787module_exit(ab8500_charger_exit); 2788 2789MODULE_LICENSE("GPL v2"); 2790MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy"); 2791MODULE_ALIAS("platform:ab8500-charger"); 2792MODULE_DESCRIPTION("AB8500 charger management driver");